dn_table.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:21k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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