dn_fib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. if (nh->nh_flags&RTNH_F_ONLINK) {
  179. struct net_device *dev;
  180. if (r->rtm_scope >= RT_SCOPE_LINK)
  181. return -EINVAL;
  182. if (dnet_addr_type(nh->nh_gw) != RTN_UNICAST)
  183. return -EINVAL;
  184. if ((dev = __dev_get_by_index(&init_net, nh->nh_oif)) == NULL)
  185. return -ENODEV;
  186. if (!(dev->flags&IFF_UP))
  187. return -ENETDOWN;
  188. nh->nh_dev = dev;
  189. dev_hold(dev);
  190. nh->nh_scope = RT_SCOPE_LINK;
  191. return 0;
  192. }
  193. memset(&fl, 0, sizeof(fl));
  194. fl.fld_dst = nh->nh_gw;
  195. fl.oif = nh->nh_oif;
  196. fl.fld_scope = r->rtm_scope + 1;
  197. if (fl.fld_scope < RT_SCOPE_LINK)
  198. fl.fld_scope = RT_SCOPE_LINK;
  199. if ((err = dn_fib_lookup(&fl, &res)) != 0)
  200. return err;
  201. err = -EINVAL;
  202. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
  203. goto out;
  204. nh->nh_scope = res.scope;
  205. nh->nh_oif = DN_FIB_RES_OIF(res);
  206. nh->nh_dev = DN_FIB_RES_DEV(res);
  207. if (nh->nh_dev == NULL)
  208. goto out;
  209. dev_hold(nh->nh_dev);
  210. err = -ENETDOWN;
  211. if (!(nh->nh_dev->flags & IFF_UP))
  212. goto out;
  213. err = 0;
  214. out:
  215. dn_fib_res_put(&res);
  216. return err;
  217. } else {
  218. struct net_device *dev;
  219. if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK))
  220. return -EINVAL;
  221. dev = __dev_get_by_index(&init_net, nh->nh_oif);
  222. if (dev == NULL || dev->dn_ptr == NULL)
  223. return -ENODEV;
  224. if (!(dev->flags&IFF_UP))
  225. return -ENETDOWN;
  226. nh->nh_dev = dev;
  227. dev_hold(nh->nh_dev);
  228. nh->nh_scope = RT_SCOPE_HOST;
  229. }
  230. return 0;
  231. }
  232. struct dn_fib_info *dn_fib_create_info(const struct rtmsg *r, struct dn_kern_rta *rta, const struct nlmsghdr *nlh, int *errp)
  233. {
  234. int err;
  235. struct dn_fib_info *fi = NULL;
  236. struct dn_fib_info *ofi;
  237. int nhs = 1;
  238. if (r->rtm_type > RTN_MAX)
  239. goto err_inval;
  240. if (dn_fib_props[r->rtm_type].scope > r->rtm_scope)
  241. goto err_inval;
  242. if (rta->rta_mp) {
  243. nhs = dn_fib_count_nhs(rta->rta_mp);
  244. if (nhs == 0)
  245. goto err_inval;
  246. }
  247. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct dn_fib_nh), GFP_KERNEL);
  248. err = -ENOBUFS;
  249. if (fi == NULL)
  250. goto failure;
  251. fi->fib_protocol = r->rtm_protocol;
  252. fi->fib_nhs = nhs;
  253. fi->fib_flags = r->rtm_flags;
  254. if (rta->rta_priority)
  255. fi->fib_priority = *rta->rta_priority;
  256. if (rta->rta_mx) {
  257. int attrlen = RTA_PAYLOAD(rta->rta_mx);
  258. struct rtattr *attr = RTA_DATA(rta->rta_mx);
  259. while(RTA_OK(attr, attrlen)) {
  260. unsigned flavour = attr->rta_type;
  261. if (flavour) {
  262. if (flavour > RTAX_MAX)
  263. goto err_inval;
  264. fi->fib_metrics[flavour-1] = *(unsigned*)RTA_DATA(attr);
  265. }
  266. attr = RTA_NEXT(attr, attrlen);
  267. }
  268. }
  269. if (rta->rta_prefsrc)
  270. memcpy(&fi->fib_prefsrc, rta->rta_prefsrc, 2);
  271. if (rta->rta_mp) {
  272. if ((err = dn_fib_get_nhs(fi, rta->rta_mp, r)) != 0)
  273. goto failure;
  274. if (rta->rta_oif && fi->fib_nh->nh_oif != *rta->rta_oif)
  275. goto err_inval;
  276. if (rta->rta_gw && memcmp(&fi->fib_nh->nh_gw, rta->rta_gw, 2))
  277. goto err_inval;
  278. } else {
  279. struct dn_fib_nh *nh = fi->fib_nh;
  280. if (rta->rta_oif)
  281. nh->nh_oif = *rta->rta_oif;
  282. if (rta->rta_gw)
  283. memcpy(&nh->nh_gw, rta->rta_gw, 2);
  284. nh->nh_flags = r->rtm_flags;
  285. nh->nh_weight = 1;
  286. }
  287. if (r->rtm_type == RTN_NAT) {
  288. if (rta->rta_gw == NULL || nhs != 1 || rta->rta_oif)
  289. goto err_inval;
  290. memcpy(&fi->fib_nh->nh_gw, rta->rta_gw, 2);
  291. goto link_it;
  292. }
  293. if (dn_fib_props[r->rtm_type].error) {
  294. if (rta->rta_gw || rta->rta_oif || rta->rta_mp)
  295. goto err_inval;
  296. goto link_it;
  297. }
  298. if (r->rtm_scope > RT_SCOPE_HOST)
  299. goto err_inval;
  300. if (r->rtm_scope == RT_SCOPE_HOST) {
  301. struct dn_fib_nh *nh = fi->fib_nh;
  302. /* Local address is added */
  303. if (nhs != 1 || nh->nh_gw)
  304. goto err_inval;
  305. nh->nh_scope = RT_SCOPE_NOWHERE;
  306. nh->nh_dev = dev_get_by_index(&init_net, fi->fib_nh->nh_oif);
  307. err = -ENODEV;
  308. if (nh->nh_dev == NULL)
  309. goto failure;
  310. } else {
  311. change_nexthops(fi) {
  312. if ((err = dn_fib_check_nh(r, fi, nh)) != 0)
  313. goto failure;
  314. } endfor_nexthops(fi)
  315. }
  316. if (fi->fib_prefsrc) {
  317. if (r->rtm_type != RTN_LOCAL || rta->rta_dst == NULL ||
  318. memcmp(&fi->fib_prefsrc, rta->rta_dst, 2))
  319. if (dnet_addr_type(fi->fib_prefsrc) != RTN_LOCAL)
  320. goto err_inval;
  321. }
  322. link_it:
  323. if ((ofi = dn_fib_find_info(fi)) != NULL) {
  324. fi->fib_dead = 1;
  325. dn_fib_free_info(fi);
  326. ofi->fib_treeref++;
  327. return ofi;
  328. }
  329. fi->fib_treeref++;
  330. atomic_inc(&fi->fib_clntref);
  331. spin_lock(&dn_fib_info_lock);
  332. fi->fib_next = dn_fib_info_list;
  333. fi->fib_prev = NULL;
  334. if (dn_fib_info_list)
  335. dn_fib_info_list->fib_prev = fi;
  336. dn_fib_info_list = fi;
  337. spin_unlock(&dn_fib_info_lock);
  338. return fi;
  339. err_inval:
  340. err = -EINVAL;
  341. failure:
  342. *errp = err;
  343. if (fi) {
  344. fi->fib_dead = 1;
  345. dn_fib_free_info(fi);
  346. }
  347. return NULL;
  348. }
  349. int dn_fib_semantic_match(int type, struct dn_fib_info *fi, const struct flowi *fl, struct dn_fib_res *res)
  350. {
  351. int err = dn_fib_props[type].error;
  352. if (err == 0) {
  353. if (fi->fib_flags & RTNH_F_DEAD)
  354. return 1;
  355. res->fi = fi;
  356. switch(type) {
  357. case RTN_NAT:
  358. DN_FIB_RES_RESET(*res);
  359. atomic_inc(&fi->fib_clntref);
  360. return 0;
  361. case RTN_UNICAST:
  362. case RTN_LOCAL:
  363. for_nexthops(fi) {
  364. if (nh->nh_flags & RTNH_F_DEAD)
  365. continue;
  366. if (!fl->oif || fl->oif == nh->nh_oif)
  367. break;
  368. }
  369. if (nhsel < fi->fib_nhs) {
  370. res->nh_sel = nhsel;
  371. atomic_inc(&fi->fib_clntref);
  372. return 0;
  373. }
  374. endfor_nexthops(fi);
  375. res->fi = NULL;
  376. return 1;
  377. default:
  378. if (net_ratelimit())
  379. printk("DECnet: impossible routing event : dn_fib_semantic_match type=%d\n", type);
  380. res->fi = NULL;
  381. return -EINVAL;
  382. }
  383. }
  384. return err;
  385. }
  386. void dn_fib_select_multipath(const struct flowi *fl, struct dn_fib_res *res)
  387. {
  388. struct dn_fib_info *fi = res->fi;
  389. int w;
  390. spin_lock_bh(&dn_fib_multipath_lock);
  391. if (fi->fib_power <= 0) {
  392. int power = 0;
  393. change_nexthops(fi) {
  394. if (!(nh->nh_flags&RTNH_F_DEAD)) {
  395. power += nh->nh_weight;
  396. nh->nh_power = nh->nh_weight;
  397. }
  398. } endfor_nexthops(fi);
  399. fi->fib_power = power;
  400. if (power < 0) {
  401. spin_unlock_bh(&dn_fib_multipath_lock);
  402. res->nh_sel = 0;
  403. return;
  404. }
  405. }
  406. w = jiffies % fi->fib_power;
  407. change_nexthops(fi) {
  408. if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
  409. if ((w -= nh->nh_power) <= 0) {
  410. nh->nh_power--;
  411. fi->fib_power--;
  412. res->nh_sel = nhsel;
  413. spin_unlock_bh(&dn_fib_multipath_lock);
  414. return;
  415. }
  416. }
  417. } endfor_nexthops(fi);
  418. res->nh_sel = 0;
  419. spin_unlock_bh(&dn_fib_multipath_lock);
  420. }
  421. static int dn_fib_check_attr(struct rtmsg *r, struct rtattr **rta)
  422. {
  423. int i;
  424. for(i = 1; i <= RTA_MAX; i++) {
  425. struct rtattr *attr = rta[i-1];
  426. if (attr) {
  427. if (RTA_PAYLOAD(attr) < 4 && RTA_PAYLOAD(attr) != 2)
  428. return -EINVAL;
  429. if (i != RTA_MULTIPATH && i != RTA_METRICS &&
  430. i != RTA_TABLE)
  431. rta[i-1] = (struct rtattr *)RTA_DATA(attr);
  432. }
  433. }
  434. return 0;
  435. }
  436. static int dn_fib_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  437. {
  438. struct net *net = sock_net(skb->sk);
  439. struct dn_fib_table *tb;
  440. struct rtattr **rta = arg;
  441. struct rtmsg *r = NLMSG_DATA(nlh);
  442. if (net != &init_net)
  443. return -EINVAL;
  444. if (dn_fib_check_attr(r, rta))
  445. return -EINVAL;
  446. tb = dn_fib_get_table(rtm_get_table(rta, r->rtm_table), 0);
  447. if (tb)
  448. return tb->delete(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb));
  449. return -ESRCH;
  450. }
  451. static int dn_fib_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  452. {
  453. struct net *net = sock_net(skb->sk);
  454. struct dn_fib_table *tb;
  455. struct rtattr **rta = arg;
  456. struct rtmsg *r = NLMSG_DATA(nlh);
  457. if (net != &init_net)
  458. return -EINVAL;
  459. if (dn_fib_check_attr(r, rta))
  460. return -EINVAL;
  461. tb = dn_fib_get_table(rtm_get_table(rta, r->rtm_table), 1);
  462. if (tb)
  463. return tb->insert(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb));
  464. return -ENOBUFS;
  465. }
  466. static void fib_magic(int cmd, int type, __le16 dst, int dst_len, struct dn_ifaddr *ifa)
  467. {
  468. struct dn_fib_table *tb;
  469. struct {
  470. struct nlmsghdr nlh;
  471. struct rtmsg rtm;
  472. } req;
  473. struct dn_kern_rta rta;
  474. memset(&req.rtm, 0, sizeof(req.rtm));
  475. memset(&rta, 0, sizeof(rta));
  476. if (type == RTN_UNICAST)
  477. tb = dn_fib_get_table(RT_MIN_TABLE, 1);
  478. else
  479. tb = dn_fib_get_table(RT_TABLE_LOCAL, 1);
  480. if (tb == NULL)
  481. return;
  482. req.nlh.nlmsg_len = sizeof(req);
  483. req.nlh.nlmsg_type = cmd;
  484. req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND;
  485. req.nlh.nlmsg_pid = 0;
  486. req.nlh.nlmsg_seq = 0;
  487. req.rtm.rtm_dst_len = dst_len;
  488. req.rtm.rtm_table = tb->n;
  489. req.rtm.rtm_protocol = RTPROT_KERNEL;
  490. req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST);
  491. req.rtm.rtm_type = type;
  492. rta.rta_dst = &dst;
  493. rta.rta_prefsrc = &ifa->ifa_local;
  494. rta.rta_oif = &ifa->ifa_dev->dev->ifindex;
  495. if (cmd == RTM_NEWROUTE)
  496. tb->insert(tb, &req.rtm, &rta, &req.nlh, NULL);
  497. else
  498. tb->delete(tb, &req.rtm, &rta, &req.nlh, NULL);
  499. }
  500. static void dn_fib_add_ifaddr(struct dn_ifaddr *ifa)
  501. {
  502. fib_magic(RTM_NEWROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);
  503. #if 0
  504. if (!(dev->flags&IFF_UP))
  505. return;
  506. /* In the future, we will want to add default routes here */
  507. #endif
  508. }
  509. static void dn_fib_del_ifaddr(struct dn_ifaddr *ifa)
  510. {
  511. int found_it = 0;
  512. struct net_device *dev;
  513. struct dn_dev *dn_db;
  514. struct dn_ifaddr *ifa2;
  515. ASSERT_RTNL();
  516. /* Scan device list */
  517. read_lock(&dev_base_lock);
  518. for_each_netdev(&init_net, dev) {
  519. dn_db = dev->dn_ptr;
  520. if (dn_db == NULL)
  521. continue;
  522. for(ifa2 = dn_db->ifa_list; ifa2; ifa2 = ifa2->ifa_next) {
  523. if (ifa2->ifa_local == ifa->ifa_local) {
  524. found_it = 1;
  525. break;
  526. }
  527. }
  528. }
  529. read_unlock(&dev_base_lock);
  530. if (found_it == 0) {
  531. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);
  532. if (dnet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
  533. if (dn_fib_sync_down(ifa->ifa_local, NULL, 0))
  534. dn_fib_flush();
  535. }
  536. }
  537. }
  538. static void dn_fib_disable_addr(struct net_device *dev, int force)
  539. {
  540. if (dn_fib_sync_down(0, dev, force))
  541. dn_fib_flush();
  542. dn_rt_cache_flush(0);
  543. neigh_ifdown(&dn_neigh_table, dev);
  544. }
  545. static int dn_fib_dnaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  546. {
  547. struct dn_ifaddr *ifa = (struct dn_ifaddr *)ptr;
  548. switch(event) {
  549. case NETDEV_UP:
  550. dn_fib_add_ifaddr(ifa);
  551. dn_fib_sync_up(ifa->ifa_dev->dev);
  552. dn_rt_cache_flush(-1);
  553. break;
  554. case NETDEV_DOWN:
  555. dn_fib_del_ifaddr(ifa);
  556. if (ifa->ifa_dev && ifa->ifa_dev->ifa_list == NULL) {
  557. dn_fib_disable_addr(ifa->ifa_dev->dev, 1);
  558. } else {
  559. dn_rt_cache_flush(-1);
  560. }
  561. break;
  562. }
  563. return NOTIFY_DONE;
  564. }
  565. static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force)
  566. {
  567. int ret = 0;
  568. int scope = RT_SCOPE_NOWHERE;
  569. if (force)
  570. scope = -1;
  571. for_fib_info() {
  572. /*
  573. * This makes no sense for DECnet.... we will almost
  574. * certainly have more than one local address the same
  575. * over all our interfaces. It needs thinking about
  576. * some more.
  577. */
  578. if (local && fi->fib_prefsrc == local) {
  579. fi->fib_flags |= RTNH_F_DEAD;
  580. ret++;
  581. } else if (dev && fi->fib_nhs) {
  582. int dead = 0;
  583. change_nexthops(fi) {
  584. if (nh->nh_flags&RTNH_F_DEAD)
  585. dead++;
  586. else if (nh->nh_dev == dev &&
  587. nh->nh_scope != scope) {
  588. spin_lock_bh(&dn_fib_multipath_lock);
  589. nh->nh_flags |= RTNH_F_DEAD;
  590. fi->fib_power -= nh->nh_power;
  591. nh->nh_power = 0;
  592. spin_unlock_bh(&dn_fib_multipath_lock);
  593. dead++;
  594. }
  595. } endfor_nexthops(fi)
  596. if (dead == fi->fib_nhs) {
  597. fi->fib_flags |= RTNH_F_DEAD;
  598. ret++;
  599. }
  600. }
  601. } endfor_fib_info();
  602. return ret;
  603. }
  604. static int dn_fib_sync_up(struct net_device *dev)
  605. {
  606. int ret = 0;
  607. if (!(dev->flags&IFF_UP))
  608. return 0;
  609. for_fib_info() {
  610. int alive = 0;
  611. change_nexthops(fi) {
  612. if (!(nh->nh_flags&RTNH_F_DEAD)) {
  613. alive++;
  614. continue;
  615. }
  616. if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
  617. continue;
  618. if (nh->nh_dev != dev || dev->dn_ptr == NULL)
  619. continue;
  620. alive++;
  621. spin_lock_bh(&dn_fib_multipath_lock);
  622. nh->nh_power = 0;
  623. nh->nh_flags &= ~RTNH_F_DEAD;
  624. spin_unlock_bh(&dn_fib_multipath_lock);
  625. } endfor_nexthops(fi);
  626. if (alive > 0) {
  627. fi->fib_flags &= ~RTNH_F_DEAD;
  628. ret++;
  629. }
  630. } endfor_fib_info();
  631. return ret;
  632. }
  633. static struct notifier_block dn_fib_dnaddr_notifier = {
  634. .notifier_call = dn_fib_dnaddr_event,
  635. };
  636. void __exit dn_fib_cleanup(void)
  637. {
  638. dn_fib_table_cleanup();
  639. dn_fib_rules_cleanup();
  640. unregister_dnaddr_notifier(&dn_fib_dnaddr_notifier);
  641. }
  642. void __init dn_fib_init(void)
  643. {
  644. dn_fib_table_init();
  645. dn_fib_rules_init();
  646. register_dnaddr_notifier(&dn_fib_dnaddr_notifier);
  647. rtnl_register(PF_DECnet, RTM_NEWROUTE, dn_fib_rtm_newroute, NULL);
  648. rtnl_register(PF_DECnet, RTM_DELROUTE, dn_fib_rtm_delroute, NULL);
  649. }