dn_table.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 (Routing Tables)
  7. *
  8. * Author: Steve Whitehouse <SteveW@ACM.org>
  9. * Mostly copied from the IPv4 routing code
  10. *
  11. *
  12. * Changes:
  13. *
  14. */
  15. #include <linux/string.h>
  16. #include <linux/net.h>
  17. #include <linux/socket.h>
  18. #include <linux/slab.h>
  19. #include <linux/sockios.h>
  20. #include <linux/init.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/netlink.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/timer.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/atomic.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/route.h> /* RTF_xxx */
  31. #include <net/neighbour.h>
  32. #include <net/netlink.h>
  33. #include <net/dst.h>
  34. #include <net/flow.h>
  35. #include <net/fib_rules.h>
  36. #include <net/dn.h>
  37. #include <net/dn_route.h>
  38. #include <net/dn_fib.h>
  39. #include <net/dn_neigh.h>
  40. #include <net/dn_dev.h>
  41. struct dn_zone
  42. {
  43. struct dn_zone *dz_next;
  44. struct dn_fib_node **dz_hash;
  45. int dz_nent;
  46. int dz_divisor;
  47. u32 dz_hashmask;
  48. #define DZ_HASHMASK(dz) ((dz)->dz_hashmask)
  49. int dz_order;
  50. __le16 dz_mask;
  51. #define DZ_MASK(dz) ((dz)->dz_mask)
  52. };
  53. struct dn_hash
  54. {
  55. struct dn_zone *dh_zones[17];
  56. struct dn_zone *dh_zone_list;
  57. };
  58. #define dz_key_0(key) ((key).datum = 0)
  59. #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
  60. for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
  61. #define endfor_nexthops(fi) }
  62. #define DN_MAX_DIVISOR 1024
  63. #define DN_S_ZOMBIE 1
  64. #define DN_S_ACCESSED 2
  65. #define DN_FIB_SCAN(f, fp) \
  66. for( ; ((f) = *(fp)) != NULL; (fp) = &(f)->fn_next)
  67. #define DN_FIB_SCAN_KEY(f, fp, key) \
  68. for( ; ((f) = *(fp)) != NULL && dn_key_eq((f)->fn_key, (key)); (fp) = &(f)->fn_next)
  69. #define RT_TABLE_MIN 1
  70. #define DN_FIB_TABLE_HASHSZ 256
  71. static struct hlist_head dn_fib_table_hash[DN_FIB_TABLE_HASHSZ];
  72. static DEFINE_RWLOCK(dn_fib_tables_lock);
  73. static struct kmem_cache *dn_hash_kmem __read_mostly;
  74. static int dn_fib_hash_zombies;
  75. static inline dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
  76. {
  77. u16 h = le16_to_cpu(key.datum)>>(16 - dz->dz_order);
  78. h ^= (h >> 10);
  79. h ^= (h >> 6);
  80. h &= DZ_HASHMASK(dz);
  81. return *(dn_fib_idx_t *)&h;
  82. }
  83. static inline dn_fib_key_t dz_key(__le16 dst, struct dn_zone *dz)
  84. {
  85. dn_fib_key_t k;
  86. k.datum = dst & DZ_MASK(dz);
  87. return k;
  88. }
  89. static inline struct dn_fib_node **dn_chain_p(dn_fib_key_t key, struct dn_zone *dz)
  90. {
  91. return &dz->dz_hash[dn_hash(key, dz).datum];
  92. }
  93. static inline struct dn_fib_node *dz_chain(dn_fib_key_t key, struct dn_zone *dz)
  94. {
  95. return dz->dz_hash[dn_hash(key, dz).datum];
  96. }
  97. static inline int dn_key_eq(dn_fib_key_t a, dn_fib_key_t b)
  98. {
  99. return a.datum == b.datum;
  100. }
  101. static inline int dn_key_leq(dn_fib_key_t a, dn_fib_key_t b)
  102. {
  103. return a.datum <= b.datum;
  104. }
  105. static inline void dn_rebuild_zone(struct dn_zone *dz,
  106. struct dn_fib_node **old_ht,
  107. int old_divisor)
  108. {
  109. struct dn_fib_node *f, **fp, *next;
  110. int i;
  111. for(i = 0; i < old_divisor; i++) {
  112. for(f = old_ht[i]; f; f = next) {
  113. next = f->fn_next;
  114. for(fp = dn_chain_p(f->fn_key, dz);
  115. *fp && dn_key_leq((*fp)->fn_key, f->fn_key);
  116. fp = &(*fp)->fn_next)
  117. /* NOTHING */;
  118. f->fn_next = *fp;
  119. *fp = f;
  120. }
  121. }
  122. }
  123. static void dn_rehash_zone(struct dn_zone *dz)
  124. {
  125. struct dn_fib_node **ht, **old_ht;
  126. int old_divisor, new_divisor;
  127. u32 new_hashmask;
  128. old_divisor = dz->dz_divisor;
  129. switch (old_divisor) {
  130. case 16:
  131. new_divisor = 256;
  132. new_hashmask = 0xFF;
  133. break;
  134. default:
  135. printk(KERN_DEBUG "DECnet: dn_rehash_zone: BUG! %d\n",
  136. old_divisor);
  137. case 256:
  138. new_divisor = 1024;
  139. new_hashmask = 0x3FF;
  140. break;
  141. }
  142. ht = kcalloc(new_divisor, sizeof(struct dn_fib_node*), GFP_KERNEL);
  143. if (ht == NULL)
  144. return;
  145. write_lock_bh(&dn_fib_tables_lock);
  146. old_ht = dz->dz_hash;
  147. dz->dz_hash = ht;
  148. dz->dz_hashmask = new_hashmask;
  149. dz->dz_divisor = new_divisor;
  150. dn_rebuild_zone(dz, old_ht, old_divisor);
  151. write_unlock_bh(&dn_fib_tables_lock);
  152. kfree(old_ht);
  153. }
  154. static void dn_free_node(struct dn_fib_node *f)
  155. {
  156. dn_fib_release_info(DN_FIB_INFO(f));
  157. kmem_cache_free(dn_hash_kmem, f);
  158. }
  159. static struct dn_zone *dn_new_zone(struct dn_hash *table, int z)
  160. {
  161. int i;
  162. struct dn_zone *dz = kzalloc(sizeof(struct dn_zone), GFP_KERNEL);
  163. if (!dz)
  164. return NULL;
  165. if (z) {
  166. dz->dz_divisor = 16;
  167. dz->dz_hashmask = 0x0F;
  168. } else {
  169. dz->dz_divisor = 1;
  170. dz->dz_hashmask = 0;
  171. }
  172. dz->dz_hash = kcalloc(dz->dz_divisor, sizeof(struct dn_fib_node *), GFP_KERNEL);
  173. if (!dz->dz_hash) {
  174. kfree(dz);
  175. return NULL;
  176. }
  177. dz->dz_order = z;
  178. dz->dz_mask = dnet_make_mask(z);
  179. for(i = z + 1; i <= 16; i++)
  180. if (table->dh_zones[i])
  181. break;
  182. write_lock_bh(&dn_fib_tables_lock);
  183. if (i>16) {
  184. dz->dz_next = table->dh_zone_list;
  185. table->dh_zone_list = dz;
  186. } else {
  187. dz->dz_next = table->dh_zones[i]->dz_next;
  188. table->dh_zones[i]->dz_next = dz;
  189. }
  190. table->dh_zones[z] = dz;
  191. write_unlock_bh(&dn_fib_tables_lock);
  192. return dz;
  193. }
  194. static int dn_fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct dn_kern_rta *rta, struct dn_fib_info *fi)
  195. {
  196. struct rtnexthop *nhp;
  197. int nhlen;
  198. if (rta->rta_priority && *rta->rta_priority != fi->fib_priority)
  199. return 1;
  200. if (rta->rta_oif || rta->rta_gw) {
  201. if ((!rta->rta_oif || *rta->rta_oif == fi->fib_nh->nh_oif) &&
  202. (!rta->rta_gw || memcmp(rta->rta_gw, &fi->fib_nh->nh_gw, 2) == 0))
  203. return 0;
  204. return 1;
  205. }
  206. if (rta->rta_mp == NULL)
  207. return 0;
  208. nhp = RTA_DATA(rta->rta_mp);
  209. nhlen = RTA_PAYLOAD(rta->rta_mp);
  210. for_nexthops(fi) {
  211. int attrlen = nhlen - sizeof(struct rtnexthop);
  212. __le16 gw;
  213. if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
  214. return -EINVAL;
  215. if (nhp->rtnh_ifindex && nhp->rtnh_ifindex != nh->nh_oif)
  216. return 1;
  217. if (attrlen) {
  218. gw = dn_fib_get_attr16(RTNH_DATA(nhp), attrlen, RTA_GATEWAY);
  219. if (gw && gw != nh->nh_gw)
  220. return 1;
  221. }
  222. nhp = RTNH_NEXT(nhp);
  223. } endfor_nexthops(fi);
  224. return 0;
  225. }
  226. static inline size_t dn_fib_nlmsg_size(struct dn_fib_info *fi)
  227. {
  228. size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
  229. + nla_total_size(4) /* RTA_TABLE */
  230. + nla_total_size(2) /* RTA_DST */
  231. + nla_total_size(4); /* RTA_PRIORITY */
  232. /* space for nested metrics */
  233. payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
  234. if (fi->fib_nhs) {
  235. /* Also handles the special case fib_nhs == 1 */
  236. /* each nexthop is packed in an attribute */
  237. size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
  238. /* may contain a gateway attribute */
  239. nhsize += nla_total_size(4);
  240. /* all nexthops are packed in a nested attribute */
  241. payload += nla_total_size(fi->fib_nhs * nhsize);
  242. }
  243. return payload;
  244. }
  245. static int dn_fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
  246. u32 tb_id, u8 type, u8 scope, void *dst, int dst_len,
  247. struct dn_fib_info *fi, unsigned int flags)
  248. {
  249. struct rtmsg *rtm;
  250. struct nlmsghdr *nlh;
  251. unsigned char *b = skb_tail_pointer(skb);
  252. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags);
  253. rtm = NLMSG_DATA(nlh);
  254. rtm->rtm_family = AF_DECnet;
  255. rtm->rtm_dst_len = dst_len;
  256. rtm->rtm_src_len = 0;
  257. rtm->rtm_tos = 0;
  258. rtm->rtm_table = tb_id;
  259. RTA_PUT_U32(skb, RTA_TABLE, tb_id);
  260. rtm->rtm_flags = fi->fib_flags;
  261. rtm->rtm_scope = scope;
  262. rtm->rtm_type = type;
  263. if (rtm->rtm_dst_len)
  264. RTA_PUT(skb, RTA_DST, 2, dst);
  265. rtm->rtm_protocol = fi->fib_protocol;
  266. if (fi->fib_priority)
  267. RTA_PUT(skb, RTA_PRIORITY, 4, &fi->fib_priority);
  268. if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
  269. goto rtattr_failure;
  270. if (fi->fib_nhs == 1) {
  271. if (fi->fib_nh->nh_gw)
  272. RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
  273. if (fi->fib_nh->nh_oif)
  274. RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
  275. }
  276. if (fi->fib_nhs > 1) {
  277. struct rtnexthop *nhp;
  278. struct rtattr *mp_head;
  279. if (skb_tailroom(skb) <= RTA_SPACE(0))
  280. goto rtattr_failure;
  281. mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
  282. for_nexthops(fi) {
  283. if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
  284. goto rtattr_failure;
  285. nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
  286. nhp->rtnh_flags = nh->nh_flags & 0xFF;
  287. nhp->rtnh_hops = nh->nh_weight - 1;
  288. nhp->rtnh_ifindex = nh->nh_oif;
  289. if (nh->nh_gw)
  290. RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
  291. nhp->rtnh_len = skb_tail_pointer(skb) - (unsigned char *)nhp;
  292. } endfor_nexthops(fi);
  293. mp_head->rta_type = RTA_MULTIPATH;
  294. mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
  295. }
  296. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  297. return skb->len;
  298. nlmsg_failure:
  299. rtattr_failure:
  300. nlmsg_trim(skb, b);
  301. return -EMSGSIZE;
  302. }
  303. static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, u32 tb_id,
  304. struct nlmsghdr *nlh, struct netlink_skb_parms *req)
  305. {
  306. struct sk_buff *skb;
  307. u32 pid = req ? req->pid : 0;
  308. int err = -ENOBUFS;
  309. skb = nlmsg_new(dn_fib_nlmsg_size(DN_FIB_INFO(f)), GFP_KERNEL);
  310. if (skb == NULL)
  311. goto errout;
  312. err = dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id,
  313. f->fn_type, f->fn_scope, &f->fn_key, z,
  314. DN_FIB_INFO(f), 0);
  315. if (err < 0) {
  316. /* -EMSGSIZE implies BUG in dn_fib_nlmsg_size() */
  317. WARN_ON(err == -EMSGSIZE);
  318. kfree_skb(skb);
  319. goto errout;
  320. }
  321. rtnl_notify(skb, &init_net, pid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
  322. return;
  323. errout:
  324. if (err < 0)
  325. rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_ROUTE, err);
  326. }
  327. static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
  328. struct netlink_callback *cb,
  329. struct dn_fib_table *tb,
  330. struct dn_zone *dz,
  331. struct dn_fib_node *f)
  332. {
  333. int i, s_i;
  334. s_i = cb->args[4];
  335. for(i = 0; f; i++, f = f->fn_next) {
  336. if (i < s_i)
  337. continue;
  338. if (f->fn_state & DN_S_ZOMBIE)
  339. continue;
  340. if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  341. cb->nlh->nlmsg_seq,
  342. RTM_NEWROUTE,
  343. tb->n,
  344. (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
  345. f->fn_scope, &f->fn_key, dz->dz_order,
  346. f->fn_info, NLM_F_MULTI) < 0) {
  347. cb->args[4] = i;
  348. return -1;
  349. }
  350. }
  351. cb->args[4] = i;
  352. return skb->len;
  353. }
  354. static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
  355. struct netlink_callback *cb,
  356. struct dn_fib_table *tb,
  357. struct dn_zone *dz)
  358. {
  359. int h, s_h;
  360. s_h = cb->args[3];
  361. for(h = 0; h < dz->dz_divisor; h++) {
  362. if (h < s_h)
  363. continue;
  364. if (h > s_h)
  365. memset(&cb->args[4], 0, sizeof(cb->args) - 4*sizeof(cb->args[0]));
  366. if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
  367. continue;
  368. if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
  369. cb->args[3] = h;
  370. return -1;
  371. }
  372. }
  373. cb->args[3] = h;
  374. return skb->len;
  375. }
  376. static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
  377. struct netlink_callback *cb)
  378. {
  379. int m, s_m;
  380. struct dn_zone *dz;
  381. struct dn_hash *table = (struct dn_hash *)tb->data;
  382. s_m = cb->args[2];
  383. read_lock(&dn_fib_tables_lock);
  384. for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
  385. if (m < s_m)
  386. continue;
  387. if (m > s_m)
  388. memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
  389. if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
  390. cb->args[2] = m;
  391. read_unlock(&dn_fib_tables_lock);
  392. return -1;
  393. }
  394. }
  395. read_unlock(&dn_fib_tables_lock);
  396. cb->args[2] = m;
  397. return skb->len;
  398. }
  399. int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
  400. {
  401. struct net *net = sock_net(skb->sk);
  402. unsigned int h, s_h;
  403. unsigned int e = 0, s_e;
  404. struct dn_fib_table *tb;
  405. struct hlist_node *node;
  406. int dumped = 0;
  407. if (!net_eq(net, &init_net))
  408. return 0;
  409. if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
  410. ((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
  411. return dn_cache_dump(skb, cb);
  412. s_h = cb->args[0];
  413. s_e = cb->args[1];
  414. for (h = s_h; h < DN_FIB_TABLE_HASHSZ; h++, s_h = 0) {
  415. e = 0;
  416. hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist) {
  417. if (e < s_e)
  418. goto next;
  419. if (dumped)
  420. memset(&cb->args[2], 0, sizeof(cb->args) -
  421. 2 * sizeof(cb->args[0]));
  422. if (tb->dump(tb, skb, cb) < 0)
  423. goto out;
  424. dumped = 1;
  425. next:
  426. e++;
  427. }
  428. }
  429. out:
  430. cb->args[1] = e;
  431. cb->args[0] = h;
  432. return skb->len;
  433. }
  434. static int dn_fib_table_insert(struct dn_fib_table *tb, struct rtmsg *r, struct dn_kern_rta *rta, struct nlmsghdr *n, struct netlink_skb_parms *req)
  435. {
  436. struct dn_hash *table = (struct dn_hash *)tb->data;
  437. struct dn_fib_node *new_f, *f, **fp, **del_fp;
  438. struct dn_zone *dz;
  439. struct dn_fib_info *fi;
  440. int z = r->rtm_dst_len;
  441. int type = r->rtm_type;
  442. dn_fib_key_t key;
  443. int err;
  444. if (z > 16)
  445. return -EINVAL;
  446. dz = table->dh_zones[z];
  447. if (!dz && !(dz = dn_new_zone(table, z)))
  448. return -ENOBUFS;
  449. dz_key_0(key);
  450. if (rta->rta_dst) {
  451. __le16 dst;
  452. memcpy(&dst, rta->rta_dst, 2);
  453. if (dst & ~DZ_MASK(dz))
  454. return -EINVAL;
  455. key = dz_key(dst, dz);
  456. }
  457. if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
  458. return err;
  459. if (dz->dz_nent > (dz->dz_divisor << 2) &&
  460. dz->dz_divisor > DN_MAX_DIVISOR &&
  461. (z==16 || (1<<z) > dz->dz_divisor))
  462. dn_rehash_zone(dz);
  463. fp = dn_chain_p(key, dz);
  464. DN_FIB_SCAN(f, fp) {
  465. if (dn_key_leq(key, f->fn_key))
  466. break;
  467. }
  468. del_fp = NULL;
  469. if (f && (f->fn_state & DN_S_ZOMBIE) &&
  470. dn_key_eq(f->fn_key, key)) {
  471. del_fp = fp;
  472. fp = &f->fn_next;
  473. f = *fp;
  474. goto create;
  475. }
  476. DN_FIB_SCAN_KEY(f, fp, key) {
  477. if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
  478. break;
  479. }
  480. if (f && dn_key_eq(f->fn_key, key) &&
  481. fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
  482. struct dn_fib_node **ins_fp;
  483. err = -EEXIST;
  484. if (n->nlmsg_flags & NLM_F_EXCL)
  485. goto out;
  486. if (n->nlmsg_flags & NLM_F_REPLACE) {
  487. del_fp = fp;
  488. fp = &f->fn_next;
  489. f = *fp;
  490. goto replace;
  491. }
  492. ins_fp = fp;
  493. err = -EEXIST;
  494. DN_FIB_SCAN_KEY(f, fp, key) {
  495. if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
  496. break;
  497. if (f->fn_type == type &&
  498. f->fn_scope == r->rtm_scope &&
  499. DN_FIB_INFO(f) == fi)
  500. goto out;
  501. }
  502. if (!(n->nlmsg_flags & NLM_F_APPEND)) {
  503. fp = ins_fp;
  504. f = *fp;
  505. }
  506. }
  507. create:
  508. err = -ENOENT;
  509. if (!(n->nlmsg_flags & NLM_F_CREATE))
  510. goto out;
  511. replace:
  512. err = -ENOBUFS;
  513. new_f = kmem_cache_zalloc(dn_hash_kmem, GFP_KERNEL);
  514. if (new_f == NULL)
  515. goto out;
  516. new_f->fn_key = key;
  517. new_f->fn_type = type;
  518. new_f->fn_scope = r->rtm_scope;
  519. DN_FIB_INFO(new_f) = fi;
  520. new_f->fn_next = f;
  521. write_lock_bh(&dn_fib_tables_lock);
  522. *fp = new_f;
  523. write_unlock_bh(&dn_fib_tables_lock);
  524. dz->dz_nent++;
  525. if (del_fp) {
  526. f = *del_fp;
  527. write_lock_bh(&dn_fib_tables_lock);
  528. *del_fp = f->fn_next;
  529. write_unlock_bh(&dn_fib_tables_lock);
  530. if (!(f->fn_state & DN_S_ZOMBIE))
  531. dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
  532. if (f->fn_state & DN_S_ACCESSED)
  533. dn_rt_cache_flush(-1);
  534. dn_free_node(f);
  535. dz->dz_nent--;
  536. } else {
  537. dn_rt_cache_flush(-1);
  538. }
  539. dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
  540. return 0;
  541. out:
  542. dn_fib_release_info(fi);
  543. return err;
  544. }
  545. static int dn_fib_table_delete(struct dn_fib_table *tb, struct rtmsg *r, struct dn_kern_rta *rta, struct nlmsghdr *n, struct netlink_skb_parms *req)
  546. {
  547. struct dn_hash *table = (struct dn_hash*)tb->data;
  548. struct dn_fib_node **fp, **del_fp, *f;
  549. int z = r->rtm_dst_len;
  550. struct dn_zone *dz;
  551. dn_fib_key_t key;
  552. int matched;
  553. if (z > 16)
  554. return -EINVAL;
  555. if ((dz = table->dh_zones[z]) == NULL)
  556. return -ESRCH;
  557. dz_key_0(key);
  558. if (rta->rta_dst) {
  559. __le16 dst;
  560. memcpy(&dst, rta->rta_dst, 2);
  561. if (dst & ~DZ_MASK(dz))
  562. return -EINVAL;
  563. key = dz_key(dst, dz);
  564. }
  565. fp = dn_chain_p(key, dz);
  566. DN_FIB_SCAN(f, fp) {
  567. if (dn_key_eq(f->fn_key, key))
  568. break;
  569. if (dn_key_leq(key, f->fn_key))
  570. return -ESRCH;
  571. }
  572. matched = 0;
  573. del_fp = NULL;
  574. DN_FIB_SCAN_KEY(f, fp, key) {
  575. struct dn_fib_info *fi = DN_FIB_INFO(f);
  576. if (f->fn_state & DN_S_ZOMBIE)
  577. return -ESRCH;
  578. matched++;
  579. if (del_fp == NULL &&
  580. (!r->rtm_type || f->fn_type == r->rtm_type) &&
  581. (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
  582. (!r->rtm_protocol ||
  583. fi->fib_protocol == r->rtm_protocol) &&
  584. dn_fib_nh_match(r, n, rta, fi) == 0)
  585. del_fp = fp;
  586. }
  587. if (del_fp) {
  588. f = *del_fp;
  589. dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
  590. if (matched != 1) {
  591. write_lock_bh(&dn_fib_tables_lock);
  592. *del_fp = f->fn_next;
  593. write_unlock_bh(&dn_fib_tables_lock);
  594. if (f->fn_state & DN_S_ACCESSED)
  595. dn_rt_cache_flush(-1);
  596. dn_free_node(f);
  597. dz->dz_nent--;
  598. } else {
  599. f->fn_state |= DN_S_ZOMBIE;
  600. if (f->fn_state & DN_S_ACCESSED) {
  601. f->fn_state &= ~DN_S_ACCESSED;
  602. dn_rt_cache_flush(-1);
  603. }
  604. if (++dn_fib_hash_zombies > 128)
  605. dn_fib_flush();
  606. }
  607. return 0;
  608. }
  609. return -ESRCH;
  610. }
  611. static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
  612. {
  613. int found = 0;
  614. struct dn_fib_node *f;
  615. while((f = *fp) != NULL) {
  616. struct dn_fib_info *fi = DN_FIB_INFO(f);
  617. if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
  618. write_lock_bh(&dn_fib_tables_lock);
  619. *fp = f->fn_next;
  620. write_unlock_bh(&dn_fib_tables_lock);
  621. dn_free_node(f);
  622. found++;
  623. continue;
  624. }
  625. fp = &f->fn_next;
  626. }
  627. return found;
  628. }
  629. static int dn_fib_table_flush(struct dn_fib_table *tb)
  630. {
  631. struct dn_hash *table = (struct dn_hash *)tb->data;
  632. struct dn_zone *dz;
  633. int found = 0;
  634. dn_fib_hash_zombies = 0;
  635. for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
  636. int i;
  637. int tmp = 0;
  638. for(i = dz->dz_divisor-1; i >= 0; i--)
  639. tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
  640. dz->dz_nent -= tmp;
  641. found += tmp;
  642. }
  643. return found;
  644. }
  645. static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowidn *flp, struct dn_fib_res *res)
  646. {
  647. int err;
  648. struct dn_zone *dz;
  649. struct dn_hash *t = (struct dn_hash *)tb->data;
  650. read_lock(&dn_fib_tables_lock);
  651. for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
  652. struct dn_fib_node *f;
  653. dn_fib_key_t k = dz_key(flp->daddr, dz);
  654. for(f = dz_chain(k, dz); f; f = f->fn_next) {
  655. if (!dn_key_eq(k, f->fn_key)) {
  656. if (dn_key_leq(k, f->fn_key))
  657. break;
  658. else
  659. continue;
  660. }
  661. f->fn_state |= DN_S_ACCESSED;
  662. if (f->fn_state&DN_S_ZOMBIE)
  663. continue;
  664. if (f->fn_scope < flp->flowidn_scope)
  665. continue;
  666. err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
  667. if (err == 0) {
  668. res->type = f->fn_type;
  669. res->scope = f->fn_scope;
  670. res->prefixlen = dz->dz_order;
  671. goto out;
  672. }
  673. if (err < 0)
  674. goto out;
  675. }
  676. }
  677. err = 1;
  678. out:
  679. read_unlock(&dn_fib_tables_lock);
  680. return err;
  681. }
  682. struct dn_fib_table *dn_fib_get_table(u32 n, int create)
  683. {
  684. struct dn_fib_table *t;
  685. struct hlist_node *node;
  686. unsigned int h;
  687. if (n < RT_TABLE_MIN)
  688. return NULL;
  689. if (n > RT_TABLE_MAX)
  690. return NULL;
  691. h = n & (DN_FIB_TABLE_HASHSZ - 1);
  692. rcu_read_lock();
  693. hlist_for_each_entry_rcu(t, node, &dn_fib_table_hash[h], hlist) {
  694. if (t->n == n) {
  695. rcu_read_unlock();
  696. return t;
  697. }
  698. }
  699. rcu_read_unlock();
  700. if (!create)
  701. return NULL;
  702. if (in_interrupt()) {
  703. net_dbg_ratelimited("DECnet: BUG! Attempt to create routing table from interrupt\n");
  704. return NULL;
  705. }
  706. t = kzalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash),
  707. GFP_KERNEL);
  708. if (t == NULL)
  709. return NULL;
  710. t->n = n;
  711. t->insert = dn_fib_table_insert;
  712. t->delete = dn_fib_table_delete;
  713. t->lookup = dn_fib_table_lookup;
  714. t->flush = dn_fib_table_flush;
  715. t->dump = dn_fib_table_dump;
  716. hlist_add_head_rcu(&t->hlist, &dn_fib_table_hash[h]);
  717. return t;
  718. }
  719. struct dn_fib_table *dn_fib_empty_table(void)
  720. {
  721. u32 id;
  722. for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
  723. if (dn_fib_get_table(id, 0) == NULL)
  724. return dn_fib_get_table(id, 1);
  725. return NULL;
  726. }
  727. void dn_fib_flush(void)
  728. {
  729. int flushed = 0;
  730. struct dn_fib_table *tb;
  731. struct hlist_node *node;
  732. unsigned int h;
  733. for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
  734. hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist)
  735. flushed += tb->flush(tb);
  736. }
  737. if (flushed)
  738. dn_rt_cache_flush(-1);
  739. }
  740. void __init dn_fib_table_init(void)
  741. {
  742. dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
  743. sizeof(struct dn_fib_info),
  744. 0, SLAB_HWCACHE_ALIGN,
  745. NULL);
  746. }
  747. void __exit dn_fib_table_cleanup(void)
  748. {
  749. struct dn_fib_table *t;
  750. struct hlist_node *node, *next;
  751. unsigned int h;
  752. write_lock(&dn_fib_tables_lock);
  753. for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
  754. hlist_for_each_entry_safe(t, node, next, &dn_fib_table_hash[h],
  755. hlist) {
  756. hlist_del(&t->hlist);
  757. kfree(t);
  758. }
  759. }
  760. write_unlock(&dn_fib_tables_lock);
  761. }