dn_table.c 19 KB

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