dn_table.c 21 KB

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