dn_fib.c 20 KB

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