1 // SPDX-License-Identifier: GPL-2.0
3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * DECnet Routing Forwarding Information Base (Glue/Info List)
9 * Author: Steve Whitehouse <SteveW@ACM.org>
13 * Alexey Kuznetsov : SMP locking changes
14 * Steve Whitehouse : Rewrote it... Well to be more correct, I
15 * copied most of it from the ipv4 fib code.
16 * Steve Whitehouse : Updated it in style and fixed a few bugs
17 * which were fixed in the ipv4 code since
18 * this code was copied from it.
21 #include <linux/string.h>
22 #include <linux/net.h>
23 #include <linux/socket.h>
24 #include <linux/slab.h>
25 #include <linux/sockios.h>
26 #include <linux/init.h>
27 #include <linux/skbuff.h>
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/proc_fs.h>
31 #include <linux/netdevice.h>
32 #include <linux/timer.h>
33 #include <linux/spinlock.h>
34 #include <linux/atomic.h>
35 #include <linux/uaccess.h>
36 #include <net/neighbour.h>
39 #include <net/fib_rules.h>
41 #include <net/dn_route.h>
42 #include <net/dn_fib.h>
43 #include <net/dn_neigh.h>
44 #include <net/dn_dev.h>
47 #define RT_MIN_TABLE 1
49 #define for_fib_info() { struct dn_fib_info *fi;\
50 for(fi = dn_fib_info_list; fi; fi = fi->fib_next)
51 #define endfor_fib_info() }
53 #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
54 for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
56 #define change_nexthops(fi) { int nhsel; struct dn_fib_nh *nh;\
57 for(nhsel = 0, nh = (struct dn_fib_nh *)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++)
59 #define endfor_nexthops(fi) }
61 static DEFINE_SPINLOCK(dn_fib_multipath_lock);
62 static struct dn_fib_info *dn_fib_info_list;
63 static DEFINE_SPINLOCK(dn_fib_info_lock);
69 } dn_fib_props[RTN_MAX+1] = {
70 [RTN_UNSPEC] = { .error = 0, .scope = RT_SCOPE_NOWHERE },
71 [RTN_UNICAST] = { .error = 0, .scope = RT_SCOPE_UNIVERSE },
72 [RTN_LOCAL] = { .error = 0, .scope = RT_SCOPE_HOST },
73 [RTN_BROADCAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
74 [RTN_ANYCAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
75 [RTN_MULTICAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
76 [RTN_BLACKHOLE] = { .error = -EINVAL, .scope = RT_SCOPE_UNIVERSE },
77 [RTN_UNREACHABLE] = { .error = -EHOSTUNREACH, .scope = RT_SCOPE_UNIVERSE },
78 [RTN_PROHIBIT] = { .error = -EACCES, .scope = RT_SCOPE_UNIVERSE },
79 [RTN_THROW] = { .error = -EAGAIN, .scope = RT_SCOPE_UNIVERSE },
80 [RTN_NAT] = { .error = 0, .scope = RT_SCOPE_NOWHERE },
81 [RTN_XRESOLVE] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
84 static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force);
85 static int dn_fib_sync_up(struct net_device *dev);
87 void dn_fib_free_info(struct dn_fib_info *fi)
89 if (fi->fib_dead == 0) {
90 printk(KERN_DEBUG "DECnet: BUG! Attempt to free alive dn_fib_info\n");
97 } endfor_nexthops(fi);
101 void dn_fib_release_info(struct dn_fib_info *fi)
103 spin_lock(&dn_fib_info_lock);
104 if (fi && refcount_dec_and_test(&fi->fib_treeref)) {
106 fi->fib_next->fib_prev = fi->fib_prev;
108 fi->fib_prev->fib_next = fi->fib_next;
109 if (fi == dn_fib_info_list)
110 dn_fib_info_list = fi->fib_next;
114 spin_unlock(&dn_fib_info_lock);
117 static inline int dn_fib_nh_comp(const struct dn_fib_info *fi, const struct dn_fib_info *ofi)
119 const struct dn_fib_nh *onh = ofi->fib_nh;
122 if (nh->nh_oif != onh->nh_oif ||
123 nh->nh_gw != onh->nh_gw ||
124 nh->nh_scope != onh->nh_scope ||
125 nh->nh_weight != onh->nh_weight ||
126 ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
129 } endfor_nexthops(fi);
133 static inline struct dn_fib_info *dn_fib_find_info(const struct dn_fib_info *nfi)
136 if (fi->fib_nhs != nfi->fib_nhs)
138 if (nfi->fib_protocol == fi->fib_protocol &&
139 nfi->fib_prefsrc == fi->fib_prefsrc &&
140 nfi->fib_priority == fi->fib_priority &&
141 memcmp(nfi->fib_metrics, fi->fib_metrics, sizeof(fi->fib_metrics)) == 0 &&
142 ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
143 (nfi->fib_nhs == 0 || dn_fib_nh_comp(fi, nfi) == 0))
149 static int dn_fib_count_nhs(const struct nlattr *attr)
151 struct rtnexthop *nhp = nla_data(attr);
152 int nhs = 0, nhlen = nla_len(attr);
154 while (rtnh_ok(nhp, nhlen)) {
156 nhp = rtnh_next(nhp, &nhlen);
159 /* leftover implies invalid nexthop configuration, discard it */
160 return nhlen > 0 ? 0 : nhs;
163 static int dn_fib_get_nhs(struct dn_fib_info *fi, const struct nlattr *attr,
164 const struct rtmsg *r)
166 struct rtnexthop *nhp = nla_data(attr);
167 int nhlen = nla_len(attr);
169 change_nexthops(fi) {
172 if (!rtnh_ok(nhp, nhlen))
175 nh->nh_flags = (r->rtm_flags&~0xFF) | nhp->rtnh_flags;
176 nh->nh_oif = nhp->rtnh_ifindex;
177 nh->nh_weight = nhp->rtnh_hops + 1;
179 attrlen = rtnh_attrlen(nhp);
181 struct nlattr *gw_attr;
183 gw_attr = nla_find((struct nlattr *) (nhp + 1), attrlen, RTA_GATEWAY);
184 nh->nh_gw = gw_attr ? nla_get_le16(gw_attr) : 0;
187 nhp = rtnh_next(nhp, &nhlen);
188 } endfor_nexthops(fi);
194 static int dn_fib_check_nh(const struct rtmsg *r, struct dn_fib_info *fi, struct dn_fib_nh *nh)
200 struct dn_fib_res res;
202 if (nh->nh_flags&RTNH_F_ONLINK) {
203 struct net_device *dev;
205 if (r->rtm_scope >= RT_SCOPE_LINK)
207 if (dnet_addr_type(nh->nh_gw) != RTN_UNICAST)
209 if ((dev = __dev_get_by_index(&init_net, nh->nh_oif)) == NULL)
211 if (!(dev->flags&IFF_UP))
215 nh->nh_scope = RT_SCOPE_LINK;
219 memset(&fld, 0, sizeof(fld));
220 fld.daddr = nh->nh_gw;
221 fld.flowidn_oif = nh->nh_oif;
222 fld.flowidn_scope = r->rtm_scope + 1;
224 if (fld.flowidn_scope < RT_SCOPE_LINK)
225 fld.flowidn_scope = RT_SCOPE_LINK;
227 if ((err = dn_fib_lookup(&fld, &res)) != 0)
231 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
233 nh->nh_scope = res.scope;
234 nh->nh_oif = DN_FIB_RES_OIF(res);
235 nh->nh_dev = DN_FIB_RES_DEV(res);
236 if (nh->nh_dev == NULL)
238 dev_hold(nh->nh_dev);
240 if (!(nh->nh_dev->flags & IFF_UP))
244 dn_fib_res_put(&res);
247 struct net_device *dev;
249 if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK))
252 dev = __dev_get_by_index(&init_net, nh->nh_oif);
253 if (dev == NULL || dev->dn_ptr == NULL)
255 if (!(dev->flags&IFF_UP))
258 dev_hold(nh->nh_dev);
259 nh->nh_scope = RT_SCOPE_HOST;
266 struct dn_fib_info *dn_fib_create_info(const struct rtmsg *r, struct nlattr *attrs[],
267 const struct nlmsghdr *nlh, int *errp)
270 struct dn_fib_info *fi = NULL;
271 struct dn_fib_info *ofi;
274 if (r->rtm_type > RTN_MAX)
277 if (dn_fib_props[r->rtm_type].scope > r->rtm_scope)
280 if (attrs[RTA_MULTIPATH] &&
281 (nhs = dn_fib_count_nhs(attrs[RTA_MULTIPATH])) == 0)
284 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
289 fi->fib_protocol = r->rtm_protocol;
291 fi->fib_flags = r->rtm_flags;
293 if (attrs[RTA_PRIORITY])
294 fi->fib_priority = nla_get_u32(attrs[RTA_PRIORITY]);
296 if (attrs[RTA_METRICS]) {
300 nla_for_each_nested(attr, attrs[RTA_METRICS], rem) {
301 int type = nla_type(attr);
304 if (type > RTAX_MAX || type == RTAX_CC_ALGO ||
308 fi->fib_metrics[type-1] = nla_get_u32(attr);
313 if (attrs[RTA_PREFSRC])
314 fi->fib_prefsrc = nla_get_le16(attrs[RTA_PREFSRC]);
316 if (attrs[RTA_MULTIPATH]) {
317 if ((err = dn_fib_get_nhs(fi, attrs[RTA_MULTIPATH], r)) != 0)
320 if (attrs[RTA_OIF] &&
321 fi->fib_nh->nh_oif != nla_get_u32(attrs[RTA_OIF]))
324 if (attrs[RTA_GATEWAY] &&
325 fi->fib_nh->nh_gw != nla_get_le16(attrs[RTA_GATEWAY]))
328 struct dn_fib_nh *nh = fi->fib_nh;
331 nh->nh_oif = nla_get_u32(attrs[RTA_OIF]);
333 if (attrs[RTA_GATEWAY])
334 nh->nh_gw = nla_get_le16(attrs[RTA_GATEWAY]);
336 nh->nh_flags = r->rtm_flags;
340 if (r->rtm_type == RTN_NAT) {
341 if (!attrs[RTA_GATEWAY] || nhs != 1 || attrs[RTA_OIF])
344 fi->fib_nh->nh_gw = nla_get_le16(attrs[RTA_GATEWAY]);
348 if (dn_fib_props[r->rtm_type].error) {
349 if (attrs[RTA_GATEWAY] || attrs[RTA_OIF] || attrs[RTA_MULTIPATH])
355 if (r->rtm_scope > RT_SCOPE_HOST)
358 if (r->rtm_scope == RT_SCOPE_HOST) {
359 struct dn_fib_nh *nh = fi->fib_nh;
361 /* Local address is added */
362 if (nhs != 1 || nh->nh_gw)
364 nh->nh_scope = RT_SCOPE_NOWHERE;
365 nh->nh_dev = dev_get_by_index(&init_net, fi->fib_nh->nh_oif);
367 if (nh->nh_dev == NULL)
370 change_nexthops(fi) {
371 if ((err = dn_fib_check_nh(r, fi, nh)) != 0)
373 } endfor_nexthops(fi)
376 if (fi->fib_prefsrc) {
377 if (r->rtm_type != RTN_LOCAL || !attrs[RTA_DST] ||
378 fi->fib_prefsrc != nla_get_le16(attrs[RTA_DST]))
379 if (dnet_addr_type(fi->fib_prefsrc) != RTN_LOCAL)
384 if ((ofi = dn_fib_find_info(fi)) != NULL) {
386 dn_fib_free_info(fi);
387 refcount_inc(&ofi->fib_treeref);
391 refcount_set(&fi->fib_treeref, 1);
392 refcount_set(&fi->fib_clntref, 1);
393 spin_lock(&dn_fib_info_lock);
394 fi->fib_next = dn_fib_info_list;
396 if (dn_fib_info_list)
397 dn_fib_info_list->fib_prev = fi;
398 dn_fib_info_list = fi;
399 spin_unlock(&dn_fib_info_lock);
409 dn_fib_free_info(fi);
415 int dn_fib_semantic_match(int type, struct dn_fib_info *fi, const struct flowidn *fld, struct dn_fib_res *res)
417 int err = dn_fib_props[type].error;
420 if (fi->fib_flags & RTNH_F_DEAD)
427 DN_FIB_RES_RESET(*res);
428 refcount_inc(&fi->fib_clntref);
433 if (nh->nh_flags & RTNH_F_DEAD)
435 if (!fld->flowidn_oif ||
436 fld->flowidn_oif == nh->nh_oif)
439 if (nhsel < fi->fib_nhs) {
441 refcount_inc(&fi->fib_clntref);
448 net_err_ratelimited("DECnet: impossible routing event : dn_fib_semantic_match type=%d\n",
457 void dn_fib_select_multipath(const struct flowidn *fld, struct dn_fib_res *res)
459 struct dn_fib_info *fi = res->fi;
462 spin_lock_bh(&dn_fib_multipath_lock);
463 if (fi->fib_power <= 0) {
465 change_nexthops(fi) {
466 if (!(nh->nh_flags&RTNH_F_DEAD)) {
467 power += nh->nh_weight;
468 nh->nh_power = nh->nh_weight;
470 } endfor_nexthops(fi);
471 fi->fib_power = power;
473 spin_unlock_bh(&dn_fib_multipath_lock);
479 w = jiffies % fi->fib_power;
481 change_nexthops(fi) {
482 if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
483 if ((w -= nh->nh_power) <= 0) {
487 spin_unlock_bh(&dn_fib_multipath_lock);
491 } endfor_nexthops(fi);
493 spin_unlock_bh(&dn_fib_multipath_lock);
496 static inline u32 rtm_get_table(struct nlattr *attrs[], u8 table)
498 if (attrs[RTA_TABLE])
499 table = nla_get_u32(attrs[RTA_TABLE]);
504 static int dn_fib_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
505 struct netlink_ext_ack *extack)
507 struct net *net = sock_net(skb->sk);
508 struct dn_fib_table *tb;
509 struct rtmsg *r = nlmsg_data(nlh);
510 struct nlattr *attrs[RTA_MAX+1];
513 if (!netlink_capable(skb, CAP_NET_ADMIN))
516 if (!net_eq(net, &init_net))
519 err = nlmsg_parse_deprecated(nlh, sizeof(*r), attrs, RTA_MAX,
520 rtm_dn_policy, extack);
524 tb = dn_fib_get_table(rtm_get_table(attrs, r->rtm_table), 0);
528 return tb->delete(tb, r, attrs, nlh, &NETLINK_CB(skb));
531 static int dn_fib_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
532 struct netlink_ext_ack *extack)
534 struct net *net = sock_net(skb->sk);
535 struct dn_fib_table *tb;
536 struct rtmsg *r = nlmsg_data(nlh);
537 struct nlattr *attrs[RTA_MAX+1];
540 if (!netlink_capable(skb, CAP_NET_ADMIN))
543 if (!net_eq(net, &init_net))
546 err = nlmsg_parse_deprecated(nlh, sizeof(*r), attrs, RTA_MAX,
547 rtm_dn_policy, extack);
551 tb = dn_fib_get_table(rtm_get_table(attrs, r->rtm_table), 1);
555 return tb->insert(tb, r, attrs, nlh, &NETLINK_CB(skb));
558 static void fib_magic(int cmd, int type, __le16 dst, int dst_len, struct dn_ifaddr *ifa)
560 struct dn_fib_table *tb;
575 .prefsrc = ifa->ifa_local,
581 .oif = ifa->ifa_dev->dev->ifindex,
583 struct nlattr *attrs[RTA_MAX+1] = {
584 [RTA_DST] = (struct nlattr *) &dst_attr,
585 [RTA_PREFSRC] = (struct nlattr * ) &prefsrc_attr,
586 [RTA_OIF] = (struct nlattr *) &oif_attr,
589 memset(&req.rtm, 0, sizeof(req.rtm));
591 if (type == RTN_UNICAST)
592 tb = dn_fib_get_table(RT_MIN_TABLE, 1);
594 tb = dn_fib_get_table(RT_TABLE_LOCAL, 1);
599 req.nlh.nlmsg_len = sizeof(req);
600 req.nlh.nlmsg_type = cmd;
601 req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND;
602 req.nlh.nlmsg_pid = 0;
603 req.nlh.nlmsg_seq = 0;
605 req.rtm.rtm_dst_len = dst_len;
606 req.rtm.rtm_table = tb->n;
607 req.rtm.rtm_protocol = RTPROT_KERNEL;
608 req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST);
609 req.rtm.rtm_type = type;
611 if (cmd == RTM_NEWROUTE)
612 tb->insert(tb, &req.rtm, attrs, &req.nlh, NULL);
614 tb->delete(tb, &req.rtm, attrs, &req.nlh, NULL);
617 static void dn_fib_add_ifaddr(struct dn_ifaddr *ifa)
620 fib_magic(RTM_NEWROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);
623 if (!(dev->flags&IFF_UP))
625 /* In the future, we will want to add default routes here */
630 static void dn_fib_del_ifaddr(struct dn_ifaddr *ifa)
633 struct net_device *dev;
634 struct dn_dev *dn_db;
635 struct dn_ifaddr *ifa2;
639 /* Scan device list */
641 for_each_netdev_rcu(&init_net, dev) {
642 dn_db = rcu_dereference(dev->dn_ptr);
645 for (ifa2 = rcu_dereference(dn_db->ifa_list);
647 ifa2 = rcu_dereference(ifa2->ifa_next)) {
648 if (ifa2->ifa_local == ifa->ifa_local) {
657 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);
659 if (dnet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
660 if (dn_fib_sync_down(ifa->ifa_local, NULL, 0))
666 static void dn_fib_disable_addr(struct net_device *dev, int force)
668 if (dn_fib_sync_down(0, dev, force))
670 dn_rt_cache_flush(0);
671 neigh_ifdown(&dn_neigh_table, dev);
674 static int dn_fib_dnaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
676 struct dn_ifaddr *ifa = (struct dn_ifaddr *)ptr;
680 dn_fib_add_ifaddr(ifa);
681 dn_fib_sync_up(ifa->ifa_dev->dev);
682 dn_rt_cache_flush(-1);
685 dn_fib_del_ifaddr(ifa);
686 if (ifa->ifa_dev && ifa->ifa_dev->ifa_list == NULL) {
687 dn_fib_disable_addr(ifa->ifa_dev->dev, 1);
689 dn_rt_cache_flush(-1);
696 static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force)
699 int scope = RT_SCOPE_NOWHERE;
706 * This makes no sense for DECnet.... we will almost
707 * certainly have more than one local address the same
708 * over all our interfaces. It needs thinking about
711 if (local && fi->fib_prefsrc == local) {
712 fi->fib_flags |= RTNH_F_DEAD;
714 } else if (dev && fi->fib_nhs) {
717 change_nexthops(fi) {
718 if (nh->nh_flags&RTNH_F_DEAD)
720 else if (nh->nh_dev == dev &&
721 nh->nh_scope != scope) {
722 spin_lock_bh(&dn_fib_multipath_lock);
723 nh->nh_flags |= RTNH_F_DEAD;
724 fi->fib_power -= nh->nh_power;
726 spin_unlock_bh(&dn_fib_multipath_lock);
729 } endfor_nexthops(fi)
730 if (dead == fi->fib_nhs) {
731 fi->fib_flags |= RTNH_F_DEAD;
740 static int dn_fib_sync_up(struct net_device *dev)
744 if (!(dev->flags&IFF_UP))
750 change_nexthops(fi) {
751 if (!(nh->nh_flags&RTNH_F_DEAD)) {
755 if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
757 if (nh->nh_dev != dev || dev->dn_ptr == NULL)
760 spin_lock_bh(&dn_fib_multipath_lock);
762 nh->nh_flags &= ~RTNH_F_DEAD;
763 spin_unlock_bh(&dn_fib_multipath_lock);
764 } endfor_nexthops(fi);
767 fi->fib_flags &= ~RTNH_F_DEAD;
774 static struct notifier_block dn_fib_dnaddr_notifier = {
775 .notifier_call = dn_fib_dnaddr_event,
778 void __exit dn_fib_cleanup(void)
780 dn_fib_table_cleanup();
781 dn_fib_rules_cleanup();
783 unregister_dnaddr_notifier(&dn_fib_dnaddr_notifier);
787 void __init dn_fib_init(void)
792 register_dnaddr_notifier(&dn_fib_dnaddr_notifier);
794 rtnl_register_module(THIS_MODULE, PF_DECnet, RTM_NEWROUTE,
795 dn_fib_rtm_newroute, NULL, 0);
796 rtnl_register_module(THIS_MODULE, PF_DECnet, RTM_DELROUTE,
797 dn_fib_rtm_delroute, NULL, 0);