dn_fib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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/sockios.h>
  24. #include <linux/init.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/netlink.h>
  27. #include <linux/rtnetlink.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/timer.h>
  31. #include <linux/spinlock.h>
  32. #include <asm/atomic.h>
  33. #include <asm/uaccess.h>
  34. #include <net/neighbour.h>
  35. #include <net/dst.h>
  36. #include <net/flow.h>
  37. #include <net/fib_rules.h>
  38. #include <net/dn.h>
  39. #include <net/dn_route.h>
  40. #include <net/dn_fib.h>
  41. #include <net/dn_neigh.h>
  42. #include <net/dn_dev.h>
  43. #define RT_MIN_TABLE 1
  44. #define for_fib_info() { struct dn_fib_info *fi;\
  45. for(fi = dn_fib_info_list; fi; fi = fi->fib_next)
  46. #define endfor_fib_info() }
  47. #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
  48. for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
  49. #define change_nexthops(fi) { int nhsel; struct dn_fib_nh *nh;\
  50. for(nhsel = 0, nh = (struct dn_fib_nh *)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++)
  51. #define endfor_nexthops(fi) }
  52. static DEFINE_SPINLOCK(dn_fib_multipath_lock);
  53. static struct dn_fib_info *dn_fib_info_list;
  54. static DEFINE_SPINLOCK(dn_fib_info_lock);
  55. static struct
  56. {
  57. int error;
  58. u8 scope;
  59. } dn_fib_props[RTN_MAX+1] = {
  60. [RTN_UNSPEC] = { .error = 0, .scope = RT_SCOPE_NOWHERE },
  61. [RTN_UNICAST] = { .error = 0, .scope = RT_SCOPE_UNIVERSE },
  62. [RTN_LOCAL] = { .error = 0, .scope = RT_SCOPE_HOST },
  63. [RTN_BROADCAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
  64. [RTN_ANYCAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
  65. [RTN_MULTICAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
  66. [RTN_BLACKHOLE] = { .error = -EINVAL, .scope = RT_SCOPE_UNIVERSE },
  67. [RTN_UNREACHABLE] = { .error = -EHOSTUNREACH, .scope = RT_SCOPE_UNIVERSE },
  68. [RTN_PROHIBIT] = { .error = -EACCES, .scope = RT_SCOPE_UNIVERSE },
  69. [RTN_THROW] = { .error = -EAGAIN, .scope = RT_SCOPE_UNIVERSE },
  70. [RTN_NAT] = { .error = 0, .scope = RT_SCOPE_NOWHERE },
  71. [RTN_XRESOLVE] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
  72. };
  73. static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force);
  74. static int dn_fib_sync_up(struct net_device *dev);
  75. void dn_fib_free_info(struct dn_fib_info *fi)
  76. {
  77. if (fi->fib_dead == 0) {
  78. printk(KERN_DEBUG "DECnet: BUG! Attempt to free alive dn_fib_info\n");
  79. return;
  80. }
  81. change_nexthops(fi) {
  82. if (nh->nh_dev)
  83. dev_put(nh->nh_dev);
  84. nh->nh_dev = NULL;
  85. } endfor_nexthops(fi);
  86. kfree(fi);
  87. }
  88. void dn_fib_release_info(struct dn_fib_info *fi)
  89. {
  90. spin_lock(&dn_fib_info_lock);
  91. if (fi && --fi->fib_treeref == 0) {
  92. if (fi->fib_next)
  93. fi->fib_next->fib_prev = fi->fib_prev;
  94. if (fi->fib_prev)
  95. fi->fib_prev->fib_next = fi->fib_next;
  96. if (fi == dn_fib_info_list)
  97. dn_fib_info_list = fi->fib_next;
  98. fi->fib_dead = 1;
  99. dn_fib_info_put(fi);
  100. }
  101. spin_unlock(&dn_fib_info_lock);
  102. }
  103. static inline int dn_fib_nh_comp(const struct dn_fib_info *fi, const struct dn_fib_info *ofi)
  104. {
  105. const struct dn_fib_nh *onh = ofi->fib_nh;
  106. for_nexthops(fi) {
  107. if (nh->nh_oif != onh->nh_oif ||
  108. nh->nh_gw != onh->nh_gw ||
  109. nh->nh_scope != onh->nh_scope ||
  110. nh->nh_weight != onh->nh_weight ||
  111. ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
  112. return -1;
  113. onh++;
  114. } endfor_nexthops(fi);
  115. return 0;
  116. }
  117. static inline struct dn_fib_info *dn_fib_find_info(const struct dn_fib_info *nfi)
  118. {
  119. for_fib_info() {
  120. if (fi->fib_nhs != nfi->fib_nhs)
  121. continue;
  122. if (nfi->fib_protocol == fi->fib_protocol &&
  123. nfi->fib_prefsrc == fi->fib_prefsrc &&
  124. nfi->fib_priority == fi->fib_priority &&
  125. memcmp(nfi->fib_metrics, fi->fib_metrics, sizeof(fi->fib_metrics)) == 0 &&
  126. ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
  127. (nfi->fib_nhs == 0 || dn_fib_nh_comp(fi, nfi) == 0))
  128. return fi;
  129. } endfor_fib_info();
  130. return NULL;
  131. }
  132. __le16 dn_fib_get_attr16(struct rtattr *attr, int attrlen, int type)
  133. {
  134. while(RTA_OK(attr,attrlen)) {
  135. if (attr->rta_type == type)
  136. return *(__le16*)RTA_DATA(attr);
  137. attr = RTA_NEXT(attr, attrlen);
  138. }
  139. return 0;
  140. }
  141. static int dn_fib_count_nhs(struct rtattr *rta)
  142. {
  143. int nhs = 0;
  144. struct rtnexthop *nhp = RTA_DATA(rta);
  145. int nhlen = RTA_PAYLOAD(rta);
  146. while(nhlen >= (int)sizeof(struct rtnexthop)) {
  147. if ((nhlen -= nhp->rtnh_len) < 0)
  148. return 0;
  149. nhs++;
  150. nhp = RTNH_NEXT(nhp);
  151. }
  152. return nhs;
  153. }
  154. static int dn_fib_get_nhs(struct dn_fib_info *fi, const struct rtattr *rta, const struct rtmsg *r)
  155. {
  156. struct rtnexthop *nhp = RTA_DATA(rta);
  157. int nhlen = RTA_PAYLOAD(rta);
  158. change_nexthops(fi) {
  159. int attrlen = nhlen - sizeof(struct rtnexthop);
  160. if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
  161. return -EINVAL;
  162. nh->nh_flags = (r->rtm_flags&~0xFF) | nhp->rtnh_flags;
  163. nh->nh_oif = nhp->rtnh_ifindex;
  164. nh->nh_weight = nhp->rtnh_hops + 1;
  165. if (attrlen) {
  166. nh->nh_gw = dn_fib_get_attr16(RTNH_DATA(nhp), attrlen, RTA_GATEWAY);
  167. }
  168. nhp = RTNH_NEXT(nhp);
  169. } endfor_nexthops(fi);
  170. return 0;
  171. }
  172. static int dn_fib_check_nh(const struct rtmsg *r, struct dn_fib_info *fi, struct dn_fib_nh *nh)
  173. {
  174. int err;
  175. if (nh->nh_gw) {
  176. struct flowi fl;
  177. struct dn_fib_res res;
  178. memset(&fl, 0, sizeof(fl));
  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(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(&fl, 0, sizeof(fl));
  195. fl.fld_dst = nh->nh_gw;
  196. fl.oif = nh->nh_oif;
  197. fl.fld_scope = r->rtm_scope + 1;
  198. if (fl.fld_scope < RT_SCOPE_LINK)
  199. fl.fld_scope = RT_SCOPE_LINK;
  200. if ((err = dn_fib_lookup(&fl, &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(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(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 flowi *fl, 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 (!fl->oif || fl->oif == nh->nh_oif)
  368. break;
  369. }
  370. if (nhsel < fi->fib_nhs) {
  371. res->nh_sel = nhsel;
  372. atomic_inc(&fi->fib_clntref);
  373. return 0;
  374. }
  375. endfor_nexthops(fi);
  376. res->fi = NULL;
  377. return 1;
  378. default:
  379. if (net_ratelimit())
  380. printk("DECnet: impossible routing event : dn_fib_semantic_match type=%d\n", type);
  381. res->fi = NULL;
  382. return -EINVAL;
  383. }
  384. }
  385. return err;
  386. }
  387. void dn_fib_select_multipath(const struct flowi *fl, struct dn_fib_res *res)
  388. {
  389. struct dn_fib_info *fi = res->fi;
  390. int w;
  391. spin_lock_bh(&dn_fib_multipath_lock);
  392. if (fi->fib_power <= 0) {
  393. int power = 0;
  394. change_nexthops(fi) {
  395. if (!(nh->nh_flags&RTNH_F_DEAD)) {
  396. power += nh->nh_weight;
  397. nh->nh_power = nh->nh_weight;
  398. }
  399. } endfor_nexthops(fi);
  400. fi->fib_power = power;
  401. if (power < 0) {
  402. spin_unlock_bh(&dn_fib_multipath_lock);
  403. res->nh_sel = 0;
  404. return;
  405. }
  406. }
  407. w = jiffies % fi->fib_power;
  408. change_nexthops(fi) {
  409. if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
  410. if ((w -= nh->nh_power) <= 0) {
  411. nh->nh_power--;
  412. fi->fib_power--;
  413. res->nh_sel = nhsel;
  414. spin_unlock_bh(&dn_fib_multipath_lock);
  415. return;
  416. }
  417. }
  418. } endfor_nexthops(fi);
  419. res->nh_sel = 0;
  420. spin_unlock_bh(&dn_fib_multipath_lock);
  421. }
  422. static int dn_fib_check_attr(struct rtmsg *r, struct rtattr **rta)
  423. {
  424. int i;
  425. for(i = 1; i <= RTA_MAX; i++) {
  426. struct rtattr *attr = rta[i-1];
  427. if (attr) {
  428. if (RTA_PAYLOAD(attr) < 4 && RTA_PAYLOAD(attr) != 2)
  429. return -EINVAL;
  430. if (i != RTA_MULTIPATH && i != RTA_METRICS &&
  431. i != RTA_TABLE)
  432. rta[i-1] = (struct rtattr *)RTA_DATA(attr);
  433. }
  434. }
  435. return 0;
  436. }
  437. int dn_fib_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  438. {
  439. struct dn_fib_table *tb;
  440. struct rtattr **rta = arg;
  441. struct rtmsg *r = NLMSG_DATA(nlh);
  442. if (dn_fib_check_attr(r, rta))
  443. return -EINVAL;
  444. tb = dn_fib_get_table(rtm_get_table(rta, r->rtm_table), 0);
  445. if (tb)
  446. return tb->delete(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb));
  447. return -ESRCH;
  448. }
  449. int dn_fib_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  450. {
  451. struct dn_fib_table *tb;
  452. struct rtattr **rta = arg;
  453. struct rtmsg *r = NLMSG_DATA(nlh);
  454. if (dn_fib_check_attr(r, rta))
  455. return -EINVAL;
  456. tb = dn_fib_get_table(rtm_get_table(rta, r->rtm_table), 1);
  457. if (tb)
  458. return tb->insert(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb));
  459. return -ENOBUFS;
  460. }
  461. static void fib_magic(int cmd, int type, __le16 dst, int dst_len, struct dn_ifaddr *ifa)
  462. {
  463. struct dn_fib_table *tb;
  464. struct {
  465. struct nlmsghdr nlh;
  466. struct rtmsg rtm;
  467. } req;
  468. struct dn_kern_rta rta;
  469. memset(&req.rtm, 0, sizeof(req.rtm));
  470. memset(&rta, 0, sizeof(rta));
  471. if (type == RTN_UNICAST)
  472. tb = dn_fib_get_table(RT_MIN_TABLE, 1);
  473. else
  474. tb = dn_fib_get_table(RT_TABLE_LOCAL, 1);
  475. if (tb == NULL)
  476. return;
  477. req.nlh.nlmsg_len = sizeof(req);
  478. req.nlh.nlmsg_type = cmd;
  479. req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND;
  480. req.nlh.nlmsg_pid = 0;
  481. req.nlh.nlmsg_seq = 0;
  482. req.rtm.rtm_dst_len = dst_len;
  483. req.rtm.rtm_table = tb->n;
  484. req.rtm.rtm_protocol = RTPROT_KERNEL;
  485. req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST);
  486. req.rtm.rtm_type = type;
  487. rta.rta_dst = &dst;
  488. rta.rta_prefsrc = &ifa->ifa_local;
  489. rta.rta_oif = &ifa->ifa_dev->dev->ifindex;
  490. if (cmd == RTM_NEWROUTE)
  491. tb->insert(tb, &req.rtm, &rta, &req.nlh, NULL);
  492. else
  493. tb->delete(tb, &req.rtm, &rta, &req.nlh, NULL);
  494. }
  495. static void dn_fib_add_ifaddr(struct dn_ifaddr *ifa)
  496. {
  497. fib_magic(RTM_NEWROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);
  498. #if 0
  499. if (!(dev->flags&IFF_UP))
  500. return;
  501. /* In the future, we will want to add default routes here */
  502. #endif
  503. }
  504. static void dn_fib_del_ifaddr(struct dn_ifaddr *ifa)
  505. {
  506. int found_it = 0;
  507. struct net_device *dev;
  508. struct dn_dev *dn_db;
  509. struct dn_ifaddr *ifa2;
  510. ASSERT_RTNL();
  511. /* Scan device list */
  512. read_lock(&dev_base_lock);
  513. for(dev = dev_base; dev; dev = dev->next) {
  514. dn_db = dev->dn_ptr;
  515. if (dn_db == NULL)
  516. continue;
  517. for(ifa2 = dn_db->ifa_list; ifa2; ifa2 = ifa2->ifa_next) {
  518. if (ifa2->ifa_local == ifa->ifa_local) {
  519. found_it = 1;
  520. break;
  521. }
  522. }
  523. }
  524. read_unlock(&dev_base_lock);
  525. if (found_it == 0) {
  526. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);
  527. if (dnet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
  528. if (dn_fib_sync_down(ifa->ifa_local, NULL, 0))
  529. dn_fib_flush();
  530. }
  531. }
  532. }
  533. static void dn_fib_disable_addr(struct net_device *dev, int force)
  534. {
  535. if (dn_fib_sync_down(0, dev, force))
  536. dn_fib_flush();
  537. dn_rt_cache_flush(0);
  538. neigh_ifdown(&dn_neigh_table, dev);
  539. }
  540. static int dn_fib_dnaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  541. {
  542. struct dn_ifaddr *ifa = (struct dn_ifaddr *)ptr;
  543. switch(event) {
  544. case NETDEV_UP:
  545. dn_fib_add_ifaddr(ifa);
  546. dn_fib_sync_up(ifa->ifa_dev->dev);
  547. dn_rt_cache_flush(-1);
  548. break;
  549. case NETDEV_DOWN:
  550. dn_fib_del_ifaddr(ifa);
  551. if (ifa->ifa_dev && ifa->ifa_dev->ifa_list == NULL) {
  552. dn_fib_disable_addr(ifa->ifa_dev->dev, 1);
  553. } else {
  554. dn_rt_cache_flush(-1);
  555. }
  556. break;
  557. }
  558. return NOTIFY_DONE;
  559. }
  560. static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force)
  561. {
  562. int ret = 0;
  563. int scope = RT_SCOPE_NOWHERE;
  564. if (force)
  565. scope = -1;
  566. for_fib_info() {
  567. /*
  568. * This makes no sense for DECnet.... we will almost
  569. * certainly have more than one local address the same
  570. * over all our interfaces. It needs thinking about
  571. * some more.
  572. */
  573. if (local && fi->fib_prefsrc == local) {
  574. fi->fib_flags |= RTNH_F_DEAD;
  575. ret++;
  576. } else if (dev && fi->fib_nhs) {
  577. int dead = 0;
  578. change_nexthops(fi) {
  579. if (nh->nh_flags&RTNH_F_DEAD)
  580. dead++;
  581. else if (nh->nh_dev == dev &&
  582. nh->nh_scope != scope) {
  583. spin_lock_bh(&dn_fib_multipath_lock);
  584. nh->nh_flags |= RTNH_F_DEAD;
  585. fi->fib_power -= nh->nh_power;
  586. nh->nh_power = 0;
  587. spin_unlock_bh(&dn_fib_multipath_lock);
  588. dead++;
  589. }
  590. } endfor_nexthops(fi)
  591. if (dead == fi->fib_nhs) {
  592. fi->fib_flags |= RTNH_F_DEAD;
  593. ret++;
  594. }
  595. }
  596. } endfor_fib_info();
  597. return ret;
  598. }
  599. static int dn_fib_sync_up(struct net_device *dev)
  600. {
  601. int ret = 0;
  602. if (!(dev->flags&IFF_UP))
  603. return 0;
  604. for_fib_info() {
  605. int alive = 0;
  606. change_nexthops(fi) {
  607. if (!(nh->nh_flags&RTNH_F_DEAD)) {
  608. alive++;
  609. continue;
  610. }
  611. if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
  612. continue;
  613. if (nh->nh_dev != dev || dev->dn_ptr == NULL)
  614. continue;
  615. alive++;
  616. spin_lock_bh(&dn_fib_multipath_lock);
  617. nh->nh_power = 0;
  618. nh->nh_flags &= ~RTNH_F_DEAD;
  619. spin_unlock_bh(&dn_fib_multipath_lock);
  620. } endfor_nexthops(fi);
  621. if (alive > 0) {
  622. fi->fib_flags &= ~RTNH_F_DEAD;
  623. ret++;
  624. }
  625. } endfor_fib_info();
  626. return ret;
  627. }
  628. static struct notifier_block dn_fib_dnaddr_notifier = {
  629. .notifier_call = dn_fib_dnaddr_event,
  630. };
  631. void __exit dn_fib_cleanup(void)
  632. {
  633. dn_fib_table_cleanup();
  634. dn_fib_rules_cleanup();
  635. unregister_dnaddr_notifier(&dn_fib_dnaddr_notifier);
  636. }
  637. void __init dn_fib_init(void)
  638. {
  639. dn_fib_table_init();
  640. dn_fib_rules_init();
  641. register_dnaddr_notifier(&dn_fib_dnaddr_notifier);
  642. }