dn_fib.c 20 KB

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