dn_fib.c 18 KB

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