dn_fib.c 18 KB

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