dn_fib.c 18 KB

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