dn_fib.c 20 KB

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