dn_table.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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/sockios.h>
  19. #include <linux/init.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/netlink.h>
  22. #include <linux/rtnetlink.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/timer.h>
  26. #include <linux/spinlock.h>
  27. #include <asm/atomic.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/route.h> /* RTF_xxx */
  30. #include <net/neighbour.h>
  31. #include <net/dst.h>
  32. #include <net/flow.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. __le16 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. #define RT_TABLE_MIN 1
  68. static DEFINE_RWLOCK(dn_fib_tables_lock);
  69. struct dn_fib_table *dn_fib_tables[RT_TABLE_MAX + 1];
  70. static kmem_cache_t *dn_hash_kmem __read_mostly;
  71. static int dn_fib_hash_zombies;
  72. static inline dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
  73. {
  74. u16 h = dn_ntohs(key.datum)>>(16 - dz->dz_order);
  75. h ^= (h >> 10);
  76. h ^= (h >> 6);
  77. h &= DZ_HASHMASK(dz);
  78. return *(dn_fib_idx_t *)&h;
  79. }
  80. static inline dn_fib_key_t dz_key(__le16 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! %d\n", 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. __le16 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, unsigned int flags)
  228. {
  229. struct rtmsg *rtm;
  230. struct nlmsghdr *nlh;
  231. unsigned char *b = skb->tail;
  232. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags);
  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 (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
  248. goto rtattr_failure;
  249. if (fi->fib_nhs == 1) {
  250. if (fi->fib_nh->nh_gw)
  251. RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
  252. if (fi->fib_nh->nh_oif)
  253. RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
  254. }
  255. if (fi->fib_nhs > 1) {
  256. struct rtnexthop *nhp;
  257. struct rtattr *mp_head;
  258. if (skb_tailroom(skb) <= RTA_SPACE(0))
  259. goto rtattr_failure;
  260. mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
  261. for_nexthops(fi) {
  262. if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
  263. goto rtattr_failure;
  264. nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
  265. nhp->rtnh_flags = nh->nh_flags & 0xFF;
  266. nhp->rtnh_hops = nh->nh_weight - 1;
  267. nhp->rtnh_ifindex = nh->nh_oif;
  268. if (nh->nh_gw)
  269. RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
  270. nhp->rtnh_len = skb->tail - (unsigned char *)nhp;
  271. } endfor_nexthops(fi);
  272. mp_head->rta_type = RTA_MULTIPATH;
  273. mp_head->rta_len = skb->tail - (u8*)mp_head;
  274. }
  275. nlh->nlmsg_len = skb->tail - b;
  276. return skb->len;
  277. nlmsg_failure:
  278. rtattr_failure:
  279. skb_trim(skb, b - skb->data);
  280. return -1;
  281. }
  282. static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, int tb_id,
  283. struct nlmsghdr *nlh, struct netlink_skb_parms *req)
  284. {
  285. struct sk_buff *skb;
  286. u32 pid = req ? req->pid : 0;
  287. int size = NLMSG_SPACE(sizeof(struct rtmsg) + 256);
  288. skb = alloc_skb(size, GFP_KERNEL);
  289. if (!skb)
  290. return;
  291. if (dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id,
  292. f->fn_type, f->fn_scope, &f->fn_key, z,
  293. DN_FIB_INFO(f), 0) < 0) {
  294. kfree_skb(skb);
  295. return;
  296. }
  297. NETLINK_CB(skb).dst_group = RTNLGRP_DECnet_ROUTE;
  298. if (nlh->nlmsg_flags & NLM_F_ECHO)
  299. atomic_inc(&skb->users);
  300. netlink_broadcast(rtnl, skb, pid, RTNLGRP_DECnet_ROUTE, GFP_KERNEL);
  301. if (nlh->nlmsg_flags & NLM_F_ECHO)
  302. netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  303. }
  304. static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
  305. struct netlink_callback *cb,
  306. struct dn_fib_table *tb,
  307. struct dn_zone *dz,
  308. struct dn_fib_node *f)
  309. {
  310. int i, s_i;
  311. s_i = cb->args[3];
  312. for(i = 0; f; i++, f = f->fn_next) {
  313. if (i < s_i)
  314. continue;
  315. if (f->fn_state & DN_S_ZOMBIE)
  316. continue;
  317. if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  318. cb->nlh->nlmsg_seq,
  319. RTM_NEWROUTE,
  320. tb->n,
  321. (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
  322. f->fn_scope, &f->fn_key, dz->dz_order,
  323. f->fn_info, NLM_F_MULTI) < 0) {
  324. cb->args[3] = i;
  325. return -1;
  326. }
  327. }
  328. cb->args[3] = i;
  329. return skb->len;
  330. }
  331. static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
  332. struct netlink_callback *cb,
  333. struct dn_fib_table *tb,
  334. struct dn_zone *dz)
  335. {
  336. int h, s_h;
  337. s_h = cb->args[2];
  338. for(h = 0; h < dz->dz_divisor; h++) {
  339. if (h < s_h)
  340. continue;
  341. if (h > s_h)
  342. memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
  343. if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
  344. continue;
  345. if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
  346. cb->args[2] = h;
  347. return -1;
  348. }
  349. }
  350. cb->args[2] = h;
  351. return skb->len;
  352. }
  353. static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
  354. struct netlink_callback *cb)
  355. {
  356. int m, s_m;
  357. struct dn_zone *dz;
  358. struct dn_hash *table = (struct dn_hash *)tb->data;
  359. s_m = cb->args[1];
  360. read_lock(&dn_fib_tables_lock);
  361. for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
  362. if (m < s_m)
  363. continue;
  364. if (m > s_m)
  365. memset(&cb->args[2], 0, sizeof(cb->args) - 2*sizeof(cb->args[0]));
  366. if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
  367. cb->args[1] = m;
  368. read_unlock(&dn_fib_tables_lock);
  369. return -1;
  370. }
  371. }
  372. read_unlock(&dn_fib_tables_lock);
  373. cb->args[1] = m;
  374. return skb->len;
  375. }
  376. 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)
  377. {
  378. struct dn_hash *table = (struct dn_hash *)tb->data;
  379. struct dn_fib_node *new_f, *f, **fp, **del_fp;
  380. struct dn_zone *dz;
  381. struct dn_fib_info *fi;
  382. int z = r->rtm_dst_len;
  383. int type = r->rtm_type;
  384. dn_fib_key_t key;
  385. int err;
  386. if (z > 16)
  387. return -EINVAL;
  388. dz = table->dh_zones[z];
  389. if (!dz && !(dz = dn_new_zone(table, z)))
  390. return -ENOBUFS;
  391. dz_key_0(key);
  392. if (rta->rta_dst) {
  393. __le16 dst;
  394. memcpy(&dst, rta->rta_dst, 2);
  395. if (dst & ~DZ_MASK(dz))
  396. return -EINVAL;
  397. key = dz_key(dst, dz);
  398. }
  399. if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
  400. return err;
  401. if (dz->dz_nent > (dz->dz_divisor << 2) &&
  402. dz->dz_divisor > DN_MAX_DIVISOR &&
  403. (z==16 || (1<<z) > dz->dz_divisor))
  404. dn_rehash_zone(dz);
  405. fp = dn_chain_p(key, dz);
  406. DN_FIB_SCAN(f, fp) {
  407. if (dn_key_leq(key, f->fn_key))
  408. break;
  409. }
  410. del_fp = NULL;
  411. if (f && (f->fn_state & DN_S_ZOMBIE) &&
  412. dn_key_eq(f->fn_key, key)) {
  413. del_fp = fp;
  414. fp = &f->fn_next;
  415. f = *fp;
  416. goto create;
  417. }
  418. DN_FIB_SCAN_KEY(f, fp, key) {
  419. if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
  420. break;
  421. }
  422. if (f && dn_key_eq(f->fn_key, key) &&
  423. fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
  424. struct dn_fib_node **ins_fp;
  425. err = -EEXIST;
  426. if (n->nlmsg_flags & NLM_F_EXCL)
  427. goto out;
  428. if (n->nlmsg_flags & NLM_F_REPLACE) {
  429. del_fp = fp;
  430. fp = &f->fn_next;
  431. f = *fp;
  432. goto replace;
  433. }
  434. ins_fp = fp;
  435. err = -EEXIST;
  436. DN_FIB_SCAN_KEY(f, fp, key) {
  437. if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
  438. break;
  439. if (f->fn_type == type && f->fn_scope == r->rtm_scope
  440. && DN_FIB_INFO(f) == fi)
  441. goto out;
  442. }
  443. if (!(n->nlmsg_flags & NLM_F_APPEND)) {
  444. fp = ins_fp;
  445. f = *fp;
  446. }
  447. }
  448. create:
  449. err = -ENOENT;
  450. if (!(n->nlmsg_flags & NLM_F_CREATE))
  451. goto out;
  452. replace:
  453. err = -ENOBUFS;
  454. new_f = kmem_cache_alloc(dn_hash_kmem, SLAB_KERNEL);
  455. if (new_f == NULL)
  456. goto out;
  457. memset(new_f, 0, sizeof(struct dn_fib_node));
  458. new_f->fn_key = key;
  459. new_f->fn_type = type;
  460. new_f->fn_scope = r->rtm_scope;
  461. DN_FIB_INFO(new_f) = fi;
  462. new_f->fn_next = f;
  463. write_lock_bh(&dn_fib_tables_lock);
  464. *fp = new_f;
  465. write_unlock_bh(&dn_fib_tables_lock);
  466. dz->dz_nent++;
  467. if (del_fp) {
  468. f = *del_fp;
  469. write_lock_bh(&dn_fib_tables_lock);
  470. *del_fp = f->fn_next;
  471. write_unlock_bh(&dn_fib_tables_lock);
  472. if (!(f->fn_state & DN_S_ZOMBIE))
  473. dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
  474. if (f->fn_state & DN_S_ACCESSED)
  475. dn_rt_cache_flush(-1);
  476. dn_free_node(f);
  477. dz->dz_nent--;
  478. } else {
  479. dn_rt_cache_flush(-1);
  480. }
  481. dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
  482. return 0;
  483. out:
  484. dn_fib_release_info(fi);
  485. return err;
  486. }
  487. 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)
  488. {
  489. struct dn_hash *table = (struct dn_hash*)tb->data;
  490. struct dn_fib_node **fp, **del_fp, *f;
  491. int z = r->rtm_dst_len;
  492. struct dn_zone *dz;
  493. dn_fib_key_t key;
  494. int matched;
  495. if (z > 16)
  496. return -EINVAL;
  497. if ((dz = table->dh_zones[z]) == NULL)
  498. return -ESRCH;
  499. dz_key_0(key);
  500. if (rta->rta_dst) {
  501. __le16 dst;
  502. memcpy(&dst, rta->rta_dst, 2);
  503. if (dst & ~DZ_MASK(dz))
  504. return -EINVAL;
  505. key = dz_key(dst, dz);
  506. }
  507. fp = dn_chain_p(key, dz);
  508. DN_FIB_SCAN(f, fp) {
  509. if (dn_key_eq(f->fn_key, key))
  510. break;
  511. if (dn_key_leq(key, f->fn_key))
  512. return -ESRCH;
  513. }
  514. matched = 0;
  515. del_fp = NULL;
  516. DN_FIB_SCAN_KEY(f, fp, key) {
  517. struct dn_fib_info *fi = DN_FIB_INFO(f);
  518. if (f->fn_state & DN_S_ZOMBIE)
  519. return -ESRCH;
  520. matched++;
  521. if (del_fp == NULL &&
  522. (!r->rtm_type || f->fn_type == r->rtm_type) &&
  523. (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
  524. (!r->rtm_protocol ||
  525. fi->fib_protocol == r->rtm_protocol) &&
  526. dn_fib_nh_match(r, n, rta, fi) == 0)
  527. del_fp = fp;
  528. }
  529. if (del_fp) {
  530. f = *del_fp;
  531. dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
  532. if (matched != 1) {
  533. write_lock_bh(&dn_fib_tables_lock);
  534. *del_fp = f->fn_next;
  535. write_unlock_bh(&dn_fib_tables_lock);
  536. if (f->fn_state & DN_S_ACCESSED)
  537. dn_rt_cache_flush(-1);
  538. dn_free_node(f);
  539. dz->dz_nent--;
  540. } else {
  541. f->fn_state |= DN_S_ZOMBIE;
  542. if (f->fn_state & DN_S_ACCESSED) {
  543. f->fn_state &= ~DN_S_ACCESSED;
  544. dn_rt_cache_flush(-1);
  545. }
  546. if (++dn_fib_hash_zombies > 128)
  547. dn_fib_flush();
  548. }
  549. return 0;
  550. }
  551. return -ESRCH;
  552. }
  553. static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
  554. {
  555. int found = 0;
  556. struct dn_fib_node *f;
  557. while((f = *fp) != NULL) {
  558. struct dn_fib_info *fi = DN_FIB_INFO(f);
  559. if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
  560. write_lock_bh(&dn_fib_tables_lock);
  561. *fp = f->fn_next;
  562. write_unlock_bh(&dn_fib_tables_lock);
  563. dn_free_node(f);
  564. found++;
  565. continue;
  566. }
  567. fp = &f->fn_next;
  568. }
  569. return found;
  570. }
  571. static int dn_fib_table_flush(struct dn_fib_table *tb)
  572. {
  573. struct dn_hash *table = (struct dn_hash *)tb->data;
  574. struct dn_zone *dz;
  575. int found = 0;
  576. dn_fib_hash_zombies = 0;
  577. for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
  578. int i;
  579. int tmp = 0;
  580. for(i = dz->dz_divisor-1; i >= 0; i--)
  581. tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
  582. dz->dz_nent -= tmp;
  583. found += tmp;
  584. }
  585. return found;
  586. }
  587. static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp, struct dn_fib_res *res)
  588. {
  589. int err;
  590. struct dn_zone *dz;
  591. struct dn_hash *t = (struct dn_hash *)tb->data;
  592. read_lock(&dn_fib_tables_lock);
  593. for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
  594. struct dn_fib_node *f;
  595. dn_fib_key_t k = dz_key(flp->fld_dst, dz);
  596. for(f = dz_chain(k, dz); f; f = f->fn_next) {
  597. if (!dn_key_eq(k, f->fn_key)) {
  598. if (dn_key_leq(k, f->fn_key))
  599. break;
  600. else
  601. continue;
  602. }
  603. f->fn_state |= DN_S_ACCESSED;
  604. if (f->fn_state&DN_S_ZOMBIE)
  605. continue;
  606. if (f->fn_scope < flp->fld_scope)
  607. continue;
  608. err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
  609. if (err == 0) {
  610. res->type = f->fn_type;
  611. res->scope = f->fn_scope;
  612. res->prefixlen = dz->dz_order;
  613. goto out;
  614. }
  615. if (err < 0)
  616. goto out;
  617. }
  618. }
  619. err = 1;
  620. out:
  621. read_unlock(&dn_fib_tables_lock);
  622. return err;
  623. }
  624. struct dn_fib_table *dn_fib_get_table(int n, int create)
  625. {
  626. struct dn_fib_table *t;
  627. if (n < RT_TABLE_MIN)
  628. return NULL;
  629. if (n > RT_TABLE_MAX)
  630. return NULL;
  631. if (dn_fib_tables[n])
  632. return dn_fib_tables[n];
  633. if (!create)
  634. return NULL;
  635. if (in_interrupt() && net_ratelimit()) {
  636. printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n");
  637. return NULL;
  638. }
  639. if ((t = kmalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash), GFP_KERNEL)) == NULL)
  640. return NULL;
  641. memset(t, 0, sizeof(struct dn_fib_table));
  642. t->n = n;
  643. t->insert = dn_fib_table_insert;
  644. t->delete = dn_fib_table_delete;
  645. t->lookup = dn_fib_table_lookup;
  646. t->flush = dn_fib_table_flush;
  647. t->dump = dn_fib_table_dump;
  648. memset(t->data, 0, sizeof(struct dn_hash));
  649. dn_fib_tables[n] = t;
  650. return t;
  651. }
  652. static void dn_fib_del_tree(int n)
  653. {
  654. struct dn_fib_table *t;
  655. write_lock(&dn_fib_tables_lock);
  656. t = dn_fib_tables[n];
  657. dn_fib_tables[n] = NULL;
  658. write_unlock(&dn_fib_tables_lock);
  659. kfree(t);
  660. }
  661. struct dn_fib_table *dn_fib_empty_table(void)
  662. {
  663. int id;
  664. for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
  665. if (dn_fib_tables[id] == NULL)
  666. return dn_fib_get_table(id, 1);
  667. return NULL;
  668. }
  669. void __init dn_fib_table_init(void)
  670. {
  671. dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
  672. sizeof(struct dn_fib_info),
  673. 0, SLAB_HWCACHE_ALIGN,
  674. NULL, NULL);
  675. }
  676. void __exit dn_fib_table_cleanup(void)
  677. {
  678. int i;
  679. for (i = RT_TABLE_MIN; i <= RT_TABLE_MAX; ++i)
  680. dn_fib_del_tree(i);
  681. return;
  682. }