dn_fib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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.
  6. *
  7. * DECnet Routing Forwarding Information Base (Glue/Info List)
  8. *
  9. * Author: Steve Whitehouse <SteveW@ACM.org>
  10. *
  11. *
  12. * Changes:
  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.
  19. *
  20. */
  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>
  37. #include <net/dst.h>
  38. #include <net/flow.h>
  39. #include <net/fib_rules.h>
  40. #include <net/dn.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>
  45. #include <net/nexthop.h>
  46. #define RT_MIN_TABLE 1
  47. #define for_fib_info() { struct dn_fib_info *fi;\
  48. for(fi = dn_fib_info_list; fi; fi = fi->fib_next)
  49. #define endfor_fib_info() }
  50. #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
  51. for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
  52. #define change_nexthops(fi) { int nhsel; struct dn_fib_nh *nh;\
  53. for(nhsel = 0, nh = (struct dn_fib_nh *)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++)
  54. #define endfor_nexthops(fi) }
  55. static DEFINE_SPINLOCK(dn_fib_multipath_lock);
  56. static struct dn_fib_info *dn_fib_info_list;
  57. static DEFINE_SPINLOCK(dn_fib_info_lock);
  58. static struct
  59. {
  60. int error;
  61. u8 scope;
  62. } dn_fib_props[RTN_MAX+1] = {
  63. [RTN_UNSPEC] = { .error = 0, .scope = RT_SCOPE_NOWHERE },
  64. [RTN_UNICAST] = { .error = 0, .scope = RT_SCOPE_UNIVERSE },
  65. [RTN_LOCAL] = { .error = 0, .scope = RT_SCOPE_HOST },
  66. [RTN_BROADCAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
  67. [RTN_ANYCAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
  68. [RTN_MULTICAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
  69. [RTN_BLACKHOLE] = { .error = -EINVAL, .scope = RT_SCOPE_UNIVERSE },
  70. [RTN_UNREACHABLE] = { .error = -EHOSTUNREACH, .scope = RT_SCOPE_UNIVERSE },
  71. [RTN_PROHIBIT] = { .error = -EACCES, .scope = RT_SCOPE_UNIVERSE },
  72. [RTN_THROW] = { .error = -EAGAIN, .scope = RT_SCOPE_UNIVERSE },
  73. [RTN_NAT] = { .error = 0, .scope = RT_SCOPE_NOWHERE },
  74. [RTN_XRESOLVE] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
  75. };
  76. static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force);
  77. static int dn_fib_sync_up(struct net_device *dev);
  78. void dn_fib_free_info(struct dn_fib_info *fi)
  79. {
  80. if (fi->fib_dead == 0) {
  81. printk(KERN_DEBUG "DECnet: BUG! Attempt to free alive dn_fib_info\n");
  82. return;
  83. }
  84. change_nexthops(fi) {
  85. if (nh->nh_dev)
  86. dev_put(nh->nh_dev);
  87. nh->nh_dev = NULL;
  88. } endfor_nexthops(fi);
  89. kfree(fi);
  90. }
  91. void dn_fib_release_info(struct dn_fib_info *fi)
  92. {
  93. spin_lock(&dn_fib_info_lock);
  94. if (fi && --fi->fib_treeref == 0) {
  95. if (fi->fib_next)
  96. fi->fib_next->fib_prev = fi->fib_prev;
  97. if (fi->fib_prev)
  98. fi->fib_prev->fib_next = fi->fib_next;
  99. if (fi == dn_fib_info_list)
  100. dn_fib_info_list = fi->fib_next;
  101. fi->fib_dead = 1;
  102. dn_fib_info_put(fi);
  103. }
  104. spin_unlock(&dn_fib_info_lock);
  105. }
  106. static inline int dn_fib_nh_comp(const struct dn_fib_info *fi, const struct dn_fib_info *ofi)
  107. {
  108. const struct dn_fib_nh *onh = ofi->fib_nh;
  109. for_nexthops(fi) {
  110. if (nh->nh_oif != onh->nh_oif ||
  111. nh->nh_gw != onh->nh_gw ||
  112. nh->nh_scope != onh->nh_scope ||
  113. nh->nh_weight != onh->nh_weight ||
  114. ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
  115. return -1;
  116. onh++;
  117. } endfor_nexthops(fi);
  118. return 0;
  119. }
  120. static inline struct dn_fib_info *dn_fib_find_info(const struct dn_fib_info *nfi)
  121. {
  122. for_fib_info() {
  123. if (fi->fib_nhs != nfi->fib_nhs)
  124. continue;
  125. if (nfi->fib_protocol == fi->fib_protocol &&
  126. nfi->fib_prefsrc == fi->fib_prefsrc &&
  127. nfi->fib_priority == fi->fib_priority &&
  128. memcmp(nfi->fib_metrics, fi->fib_metrics, sizeof(fi->fib_metrics)) == 0 &&
  129. ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
  130. (nfi->fib_nhs == 0 || dn_fib_nh_comp(fi, nfi) == 0))
  131. return fi;
  132. } endfor_fib_info();
  133. return NULL;
  134. }
  135. static int dn_fib_count_nhs(const struct nlattr *attr)
  136. {
  137. struct rtnexthop *nhp = nla_data(attr);
  138. int nhs = 0, nhlen = nla_len(attr);
  139. while (rtnh_ok(nhp, nhlen)) {
  140. nhs++;
  141. nhp = rtnh_next(nhp, &nhlen);
  142. }
  143. /* leftover implies invalid nexthop configuration, discard it */
  144. return nhlen > 0 ? 0 : nhs;
  145. }
  146. static int dn_fib_get_nhs(struct dn_fib_info *fi, const struct nlattr *attr,
  147. const struct rtmsg *r)
  148. {
  149. struct rtnexthop *nhp = nla_data(attr);
  150. int nhlen = nla_len(attr);
  151. change_nexthops(fi) {
  152. int attrlen;
  153. if (!rtnh_ok(nhp, nhlen))
  154. return -EINVAL;
  155. nh->nh_flags = (r->rtm_flags&~0xFF) | nhp->rtnh_flags;
  156. nh->nh_oif = nhp->rtnh_ifindex;
  157. nh->nh_weight = nhp->rtnh_hops + 1;
  158. attrlen = rtnh_attrlen(nhp);
  159. if (attrlen > 0) {
  160. struct nlattr *gw_attr;
  161. gw_attr = nla_find((struct nlattr *) (nhp + 1), attrlen, RTA_GATEWAY);
  162. nh->nh_gw = gw_attr ? nla_get_le16(gw_attr) : 0;
  163. }
  164. nhp = rtnh_next(nhp, &nhlen);
  165. } endfor_nexthops(fi);
  166. return 0;
  167. }
  168. static int dn_fib_check_nh(const struct rtmsg *r, struct dn_fib_info *fi, struct dn_fib_nh *nh)
  169. {
  170. int err;
  171. if (nh->nh_gw) {
  172. struct flowidn fld;
  173. struct dn_fib_res res;
  174. if (nh->nh_flags&RTNH_F_ONLINK) {
  175. struct net_device *dev;
  176. if (r->rtm_scope >= RT_SCOPE_LINK)
  177. return -EINVAL;
  178. if (dnet_addr_type(nh->nh_gw) != RTN_UNICAST)
  179. return -EINVAL;
  180. if ((dev = __dev_get_by_index(&init_net, nh->nh_oif)) == NULL)
  181. return -ENODEV;
  182. if (!(dev->flags&IFF_UP))
  183. return -ENETDOWN;
  184. nh->nh_dev = dev;
  185. dev_hold(dev);
  186. nh->nh_scope = RT_SCOPE_LINK;
  187. return 0;
  188. }
  189. memset(&fld, 0, sizeof(fld));
  190. fld.daddr = nh->nh_gw;
  191. fld.flowidn_oif = nh->nh_oif;
  192. fld.flowidn_scope = r->rtm_scope + 1;
  193. if (fld.flowidn_scope < RT_SCOPE_LINK)
  194. fld.flowidn_scope = RT_SCOPE_LINK;
  195. if ((err = dn_fib_lookup(&fld, &res)) != 0)
  196. return err;
  197. err = -EINVAL;
  198. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
  199. goto out;
  200. nh->nh_scope = res.scope;
  201. nh->nh_oif = DN_FIB_RES_OIF(res);
  202. nh->nh_dev = DN_FIB_RES_DEV(res);
  203. if (nh->nh_dev == NULL)
  204. goto out;
  205. dev_hold(nh->nh_dev);
  206. err = -ENETDOWN;
  207. if (!(nh->nh_dev->flags & IFF_UP))
  208. goto out;
  209. err = 0;
  210. out:
  211. dn_fib_res_put(&res);
  212. return err;
  213. } else {
  214. struct net_device *dev;
  215. if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK))
  216. return -EINVAL;
  217. dev = __dev_get_by_index(&init_net, nh->nh_oif);
  218. if (dev == NULL || dev->dn_ptr == NULL)
  219. return -ENODEV;
  220. if (!(dev->flags&IFF_UP))
  221. return -ENETDOWN;
  222. nh->nh_dev = dev;
  223. dev_hold(nh->nh_dev);
  224. nh->nh_scope = RT_SCOPE_HOST;
  225. }
  226. return 0;
  227. }
  228. struct dn_fib_info *dn_fib_create_info(const struct rtmsg *r, struct nlattr *attrs[],
  229. const struct nlmsghdr *nlh, int *errp)
  230. {
  231. int err;
  232. struct dn_fib_info *fi = NULL;
  233. struct dn_fib_info *ofi;
  234. int nhs = 1;
  235. if (r->rtm_type > RTN_MAX)
  236. goto err_inval;
  237. if (dn_fib_props[r->rtm_type].scope > r->rtm_scope)
  238. goto err_inval;
  239. if (attrs[RTA_MULTIPATH] &&
  240. (nhs = dn_fib_count_nhs(attrs[RTA_MULTIPATH])) == 0)
  241. goto err_inval;
  242. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct dn_fib_nh), GFP_KERNEL);
  243. err = -ENOBUFS;
  244. if (fi == NULL)
  245. goto failure;
  246. fi->fib_protocol = r->rtm_protocol;
  247. fi->fib_nhs = nhs;
  248. fi->fib_flags = r->rtm_flags;
  249. if (attrs[RTA_PRIORITY])
  250. fi->fib_priority = nla_get_u32(attrs[RTA_PRIORITY]);
  251. if (attrs[RTA_METRICS]) {
  252. struct nlattr *attr;
  253. int rem;
  254. nla_for_each_nested(attr, attrs[RTA_METRICS], rem) {
  255. int type = nla_type(attr);
  256. if (type) {
  257. if (type > RTAX_MAX || type == RTAX_CC_ALGO ||
  258. nla_len(attr) < 4)
  259. goto err_inval;
  260. fi->fib_metrics[type-1] = nla_get_u32(attr);
  261. }
  262. }
  263. }
  264. if (attrs[RTA_PREFSRC])
  265. fi->fib_prefsrc = nla_get_le16(attrs[RTA_PREFSRC]);
  266. if (attrs[RTA_MULTIPATH]) {
  267. if ((err = dn_fib_get_nhs(fi, attrs[RTA_MULTIPATH], r)) != 0)
  268. goto failure;
  269. if (attrs[RTA_OIF] &&
  270. fi->fib_nh->nh_oif != nla_get_u32(attrs[RTA_OIF]))
  271. goto err_inval;
  272. if (attrs[RTA_GATEWAY] &&
  273. fi->fib_nh->nh_gw != nla_get_le16(attrs[RTA_GATEWAY]))
  274. goto err_inval;
  275. } else {
  276. struct dn_fib_nh *nh = fi->fib_nh;
  277. if (attrs[RTA_OIF])
  278. nh->nh_oif = nla_get_u32(attrs[RTA_OIF]);
  279. if (attrs[RTA_GATEWAY])
  280. nh->nh_gw = nla_get_le16(attrs[RTA_GATEWAY]);
  281. nh->nh_flags = r->rtm_flags;
  282. nh->nh_weight = 1;
  283. }
  284. if (r->rtm_type == RTN_NAT) {
  285. if (!attrs[RTA_GATEWAY] || nhs != 1 || attrs[RTA_OIF])
  286. goto err_inval;
  287. fi->fib_nh->nh_gw = nla_get_le16(attrs[RTA_GATEWAY]);
  288. goto link_it;
  289. }
  290. if (dn_fib_props[r->rtm_type].error) {
  291. if (attrs[RTA_GATEWAY] || attrs[RTA_OIF] || attrs[RTA_MULTIPATH])
  292. goto err_inval;
  293. goto link_it;
  294. }
  295. if (r->rtm_scope > RT_SCOPE_HOST)
  296. goto err_inval;
  297. if (r->rtm_scope == RT_SCOPE_HOST) {
  298. struct dn_fib_nh *nh = fi->fib_nh;
  299. /* Local address is added */
  300. if (nhs != 1 || nh->nh_gw)
  301. goto err_inval;
  302. nh->nh_scope = RT_SCOPE_NOWHERE;
  303. nh->nh_dev = dev_get_by_index(&init_net, fi->fib_nh->nh_oif);
  304. err = -ENODEV;
  305. if (nh->nh_dev == NULL)
  306. goto failure;
  307. } else {
  308. change_nexthops(fi) {
  309. if ((err = dn_fib_check_nh(r, fi, nh)) != 0)
  310. goto failure;
  311. } endfor_nexthops(fi)
  312. }
  313. if (fi->fib_prefsrc) {
  314. if (r->rtm_type != RTN_LOCAL || !attrs[RTA_DST] ||
  315. fi->fib_prefsrc != nla_get_le16(attrs[RTA_DST]))
  316. if (dnet_addr_type(fi->fib_prefsrc) != RTN_LOCAL)
  317. goto err_inval;
  318. }
  319. link_it:
  320. if ((ofi = dn_fib_find_info(fi)) != NULL) {
  321. fi->fib_dead = 1;
  322. dn_fib_free_info(fi);
  323. ofi->fib_treeref++;
  324. return ofi;
  325. }
  326. fi->fib_treeref++;
  327. refcount_set(&fi->fib_clntref, 1);
  328. spin_lock(&dn_fib_info_lock);
  329. fi->fib_next = dn_fib_info_list;
  330. fi->fib_prev = NULL;
  331. if (dn_fib_info_list)
  332. dn_fib_info_list->fib_prev = fi;
  333. dn_fib_info_list = fi;
  334. spin_unlock(&dn_fib_info_lock);
  335. return fi;
  336. err_inval:
  337. err = -EINVAL;
  338. failure:
  339. *errp = err;
  340. if (fi) {
  341. fi->fib_dead = 1;
  342. dn_fib_free_info(fi);
  343. }
  344. return NULL;
  345. }
  346. int dn_fib_semantic_match(int type, struct dn_fib_info *fi, const struct flowidn *fld, struct dn_fib_res *res)
  347. {
  348. int err = dn_fib_props[type].error;
  349. if (err == 0) {
  350. if (fi->fib_flags & RTNH_F_DEAD)
  351. return 1;
  352. res->fi = fi;
  353. switch (type) {
  354. case RTN_NAT:
  355. DN_FIB_RES_RESET(*res);
  356. refcount_inc(&fi->fib_clntref);
  357. return 0;
  358. case RTN_UNICAST:
  359. case RTN_LOCAL:
  360. for_nexthops(fi) {
  361. if (nh->nh_flags & RTNH_F_DEAD)
  362. continue;
  363. if (!fld->flowidn_oif ||
  364. fld->flowidn_oif == nh->nh_oif)
  365. break;
  366. }
  367. if (nhsel < fi->fib_nhs) {
  368. res->nh_sel = nhsel;
  369. refcount_inc(&fi->fib_clntref);
  370. return 0;
  371. }
  372. endfor_nexthops(fi);
  373. res->fi = NULL;
  374. return 1;
  375. default:
  376. net_err_ratelimited("DECnet: impossible routing event : dn_fib_semantic_match type=%d\n",
  377. type);
  378. res->fi = NULL;
  379. return -EINVAL;
  380. }
  381. }
  382. return err;
  383. }
  384. void dn_fib_select_multipath(const struct flowidn *fld, struct dn_fib_res *res)
  385. {
  386. struct dn_fib_info *fi = res->fi;
  387. int w;
  388. spin_lock_bh(&dn_fib_multipath_lock);
  389. if (fi->fib_power <= 0) {
  390. int power = 0;
  391. change_nexthops(fi) {
  392. if (!(nh->nh_flags&RTNH_F_DEAD)) {
  393. power += nh->nh_weight;
  394. nh->nh_power = nh->nh_weight;
  395. }
  396. } endfor_nexthops(fi);
  397. fi->fib_power = power;
  398. if (power < 0) {
  399. spin_unlock_bh(&dn_fib_multipath_lock);
  400. res->nh_sel = 0;
  401. return;
  402. }
  403. }
  404. w = jiffies % fi->fib_power;
  405. change_nexthops(fi) {
  406. if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
  407. if ((w -= nh->nh_power) <= 0) {
  408. nh->nh_power--;
  409. fi->fib_power--;
  410. res->nh_sel = nhsel;
  411. spin_unlock_bh(&dn_fib_multipath_lock);
  412. return;
  413. }
  414. }
  415. } endfor_nexthops(fi);
  416. res->nh_sel = 0;
  417. spin_unlock_bh(&dn_fib_multipath_lock);
  418. }
  419. static inline u32 rtm_get_table(struct nlattr *attrs[], u8 table)
  420. {
  421. if (attrs[RTA_TABLE])
  422. table = nla_get_u32(attrs[RTA_TABLE]);
  423. return table;
  424. }
  425. static int dn_fib_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  426. struct netlink_ext_ack *extack)
  427. {
  428. struct net *net = sock_net(skb->sk);
  429. struct dn_fib_table *tb;
  430. struct rtmsg *r = nlmsg_data(nlh);
  431. struct nlattr *attrs[RTA_MAX+1];
  432. int err;
  433. if (!netlink_capable(skb, CAP_NET_ADMIN))
  434. return -EPERM;
  435. if (!net_eq(net, &init_net))
  436. return -EINVAL;
  437. err = nlmsg_parse(nlh, sizeof(*r), attrs, RTA_MAX, rtm_dn_policy,
  438. extack);
  439. if (err < 0)
  440. return err;
  441. tb = dn_fib_get_table(rtm_get_table(attrs, r->rtm_table), 0);
  442. if (!tb)
  443. return -ESRCH;
  444. return tb->delete(tb, r, attrs, nlh, &NETLINK_CB(skb));
  445. }
  446. static int dn_fib_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  447. struct netlink_ext_ack *extack)
  448. {
  449. struct net *net = sock_net(skb->sk);
  450. struct dn_fib_table *tb;
  451. struct rtmsg *r = nlmsg_data(nlh);
  452. struct nlattr *attrs[RTA_MAX+1];
  453. int err;
  454. if (!netlink_capable(skb, CAP_NET_ADMIN))
  455. return -EPERM;
  456. if (!net_eq(net, &init_net))
  457. return -EINVAL;
  458. err = nlmsg_parse(nlh, sizeof(*r), attrs, RTA_MAX, rtm_dn_policy,
  459. extack);
  460. if (err < 0)
  461. return err;
  462. tb = dn_fib_get_table(rtm_get_table(attrs, r->rtm_table), 1);
  463. if (!tb)
  464. return -ENOBUFS;
  465. return tb->insert(tb, r, attrs, nlh, &NETLINK_CB(skb));
  466. }
  467. static void fib_magic(int cmd, int type, __le16 dst, int dst_len, struct dn_ifaddr *ifa)
  468. {
  469. struct dn_fib_table *tb;
  470. struct {
  471. struct nlmsghdr nlh;
  472. struct rtmsg rtm;
  473. } req;
  474. struct {
  475. struct nlattr hdr;
  476. __le16 dst;
  477. } dst_attr = {
  478. .dst = dst,
  479. };
  480. struct {
  481. struct nlattr hdr;
  482. __le16 prefsrc;
  483. } prefsrc_attr = {
  484. .prefsrc = ifa->ifa_local,
  485. };
  486. struct {
  487. struct nlattr hdr;
  488. u32 oif;
  489. } oif_attr = {
  490. .oif = ifa->ifa_dev->dev->ifindex,
  491. };
  492. struct nlattr *attrs[RTA_MAX+1] = {
  493. [RTA_DST] = (struct nlattr *) &dst_attr,
  494. [RTA_PREFSRC] = (struct nlattr * ) &prefsrc_attr,
  495. [RTA_OIF] = (struct nlattr *) &oif_attr,
  496. };
  497. memset(&req.rtm, 0, sizeof(req.rtm));
  498. if (type == RTN_UNICAST)
  499. tb = dn_fib_get_table(RT_MIN_TABLE, 1);
  500. else
  501. tb = dn_fib_get_table(RT_TABLE_LOCAL, 1);
  502. if (tb == NULL)
  503. return;
  504. req.nlh.nlmsg_len = sizeof(req);
  505. req.nlh.nlmsg_type = cmd;
  506. req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND;
  507. req.nlh.nlmsg_pid = 0;
  508. req.nlh.nlmsg_seq = 0;
  509. req.rtm.rtm_dst_len = dst_len;
  510. req.rtm.rtm_table = tb->n;
  511. req.rtm.rtm_protocol = RTPROT_KERNEL;
  512. req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST);
  513. req.rtm.rtm_type = type;
  514. if (cmd == RTM_NEWROUTE)
  515. tb->insert(tb, &req.rtm, attrs, &req.nlh, NULL);
  516. else
  517. tb->delete(tb, &req.rtm, attrs, &req.nlh, NULL);
  518. }
  519. static void dn_fib_add_ifaddr(struct dn_ifaddr *ifa)
  520. {
  521. fib_magic(RTM_NEWROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);
  522. #if 0
  523. if (!(dev->flags&IFF_UP))
  524. return;
  525. /* In the future, we will want to add default routes here */
  526. #endif
  527. }
  528. static void dn_fib_del_ifaddr(struct dn_ifaddr *ifa)
  529. {
  530. int found_it = 0;
  531. struct net_device *dev;
  532. struct dn_dev *dn_db;
  533. struct dn_ifaddr *ifa2;
  534. ASSERT_RTNL();
  535. /* Scan device list */
  536. rcu_read_lock();
  537. for_each_netdev_rcu(&init_net, dev) {
  538. dn_db = rcu_dereference(dev->dn_ptr);
  539. if (dn_db == NULL)
  540. continue;
  541. for (ifa2 = rcu_dereference(dn_db->ifa_list);
  542. ifa2 != NULL;
  543. ifa2 = rcu_dereference(ifa2->ifa_next)) {
  544. if (ifa2->ifa_local == ifa->ifa_local) {
  545. found_it = 1;
  546. break;
  547. }
  548. }
  549. }
  550. rcu_read_unlock();
  551. if (found_it == 0) {
  552. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);
  553. if (dnet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
  554. if (dn_fib_sync_down(ifa->ifa_local, NULL, 0))
  555. dn_fib_flush();
  556. }
  557. }
  558. }
  559. static void dn_fib_disable_addr(struct net_device *dev, int force)
  560. {
  561. if (dn_fib_sync_down(0, dev, force))
  562. dn_fib_flush();
  563. dn_rt_cache_flush(0);
  564. neigh_ifdown(&dn_neigh_table, dev);
  565. }
  566. static int dn_fib_dnaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  567. {
  568. struct dn_ifaddr *ifa = (struct dn_ifaddr *)ptr;
  569. switch (event) {
  570. case NETDEV_UP:
  571. dn_fib_add_ifaddr(ifa);
  572. dn_fib_sync_up(ifa->ifa_dev->dev);
  573. dn_rt_cache_flush(-1);
  574. break;
  575. case NETDEV_DOWN:
  576. dn_fib_del_ifaddr(ifa);
  577. if (ifa->ifa_dev && ifa->ifa_dev->ifa_list == NULL) {
  578. dn_fib_disable_addr(ifa->ifa_dev->dev, 1);
  579. } else {
  580. dn_rt_cache_flush(-1);
  581. }
  582. break;
  583. }
  584. return NOTIFY_DONE;
  585. }
  586. static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force)
  587. {
  588. int ret = 0;
  589. int scope = RT_SCOPE_NOWHERE;
  590. if (force)
  591. scope = -1;
  592. for_fib_info() {
  593. /*
  594. * This makes no sense for DECnet.... we will almost
  595. * certainly have more than one local address the same
  596. * over all our interfaces. It needs thinking about
  597. * some more.
  598. */
  599. if (local && fi->fib_prefsrc == local) {
  600. fi->fib_flags |= RTNH_F_DEAD;
  601. ret++;
  602. } else if (dev && fi->fib_nhs) {
  603. int dead = 0;
  604. change_nexthops(fi) {
  605. if (nh->nh_flags&RTNH_F_DEAD)
  606. dead++;
  607. else if (nh->nh_dev == dev &&
  608. nh->nh_scope != scope) {
  609. spin_lock_bh(&dn_fib_multipath_lock);
  610. nh->nh_flags |= RTNH_F_DEAD;
  611. fi->fib_power -= nh->nh_power;
  612. nh->nh_power = 0;
  613. spin_unlock_bh(&dn_fib_multipath_lock);
  614. dead++;
  615. }
  616. } endfor_nexthops(fi)
  617. if (dead == fi->fib_nhs) {
  618. fi->fib_flags |= RTNH_F_DEAD;
  619. ret++;
  620. }
  621. }
  622. } endfor_fib_info();
  623. return ret;
  624. }
  625. static int dn_fib_sync_up(struct net_device *dev)
  626. {
  627. int ret = 0;
  628. if (!(dev->flags&IFF_UP))
  629. return 0;
  630. for_fib_info() {
  631. int alive = 0;
  632. change_nexthops(fi) {
  633. if (!(nh->nh_flags&RTNH_F_DEAD)) {
  634. alive++;
  635. continue;
  636. }
  637. if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
  638. continue;
  639. if (nh->nh_dev != dev || dev->dn_ptr == NULL)
  640. continue;
  641. alive++;
  642. spin_lock_bh(&dn_fib_multipath_lock);
  643. nh->nh_power = 0;
  644. nh->nh_flags &= ~RTNH_F_DEAD;
  645. spin_unlock_bh(&dn_fib_multipath_lock);
  646. } endfor_nexthops(fi);
  647. if (alive > 0) {
  648. fi->fib_flags &= ~RTNH_F_DEAD;
  649. ret++;
  650. }
  651. } endfor_fib_info();
  652. return ret;
  653. }
  654. static struct notifier_block dn_fib_dnaddr_notifier = {
  655. .notifier_call = dn_fib_dnaddr_event,
  656. };
  657. void __exit dn_fib_cleanup(void)
  658. {
  659. dn_fib_table_cleanup();
  660. dn_fib_rules_cleanup();
  661. unregister_dnaddr_notifier(&dn_fib_dnaddr_notifier);
  662. }
  663. void __init dn_fib_init(void)
  664. {
  665. dn_fib_table_init();
  666. dn_fib_rules_init();
  667. register_dnaddr_notifier(&dn_fib_dnaddr_notifier);
  668. rtnl_register_module(THIS_MODULE, PF_DECnet, RTM_NEWROUTE,
  669. dn_fib_rtm_newroute, NULL, 0);
  670. rtnl_register_module(THIS_MODULE, PF_DECnet, RTM_DELROUTE,
  671. dn_fib_rtm_delroute, NULL, 0);
  672. }