dn_table.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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 -EMSGSIZE;
  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. if (err < 0) {
  314. /* -EMSGSIZE implies BUG in dn_fib_nlmsg_size() */
  315. WARN_ON(err == -EMSGSIZE);
  316. kfree_skb(skb);
  317. goto errout;
  318. }
  319. err = rtnl_notify(skb, pid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
  320. errout:
  321. if (err < 0)
  322. rtnl_set_sk_err(RTNLGRP_DECnet_ROUTE, err);
  323. }
  324. static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
  325. struct netlink_callback *cb,
  326. struct dn_fib_table *tb,
  327. struct dn_zone *dz,
  328. struct dn_fib_node *f)
  329. {
  330. int i, s_i;
  331. s_i = cb->args[4];
  332. for(i = 0; f; i++, f = f->fn_next) {
  333. if (i < s_i)
  334. continue;
  335. if (f->fn_state & DN_S_ZOMBIE)
  336. continue;
  337. if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  338. cb->nlh->nlmsg_seq,
  339. RTM_NEWROUTE,
  340. tb->n,
  341. (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
  342. f->fn_scope, &f->fn_key, dz->dz_order,
  343. f->fn_info, NLM_F_MULTI) < 0) {
  344. cb->args[4] = i;
  345. return -1;
  346. }
  347. }
  348. cb->args[4] = i;
  349. return skb->len;
  350. }
  351. static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
  352. struct netlink_callback *cb,
  353. struct dn_fib_table *tb,
  354. struct dn_zone *dz)
  355. {
  356. int h, s_h;
  357. s_h = cb->args[3];
  358. for(h = 0; h < dz->dz_divisor; h++) {
  359. if (h < s_h)
  360. continue;
  361. if (h > s_h)
  362. memset(&cb->args[4], 0, sizeof(cb->args) - 4*sizeof(cb->args[0]));
  363. if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
  364. continue;
  365. if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
  366. cb->args[3] = h;
  367. return -1;
  368. }
  369. }
  370. cb->args[3] = h;
  371. return skb->len;
  372. }
  373. static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
  374. struct netlink_callback *cb)
  375. {
  376. int m, s_m;
  377. struct dn_zone *dz;
  378. struct dn_hash *table = (struct dn_hash *)tb->data;
  379. s_m = cb->args[2];
  380. read_lock(&dn_fib_tables_lock);
  381. for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
  382. if (m < s_m)
  383. continue;
  384. if (m > s_m)
  385. memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
  386. if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
  387. cb->args[2] = m;
  388. read_unlock(&dn_fib_tables_lock);
  389. return -1;
  390. }
  391. }
  392. read_unlock(&dn_fib_tables_lock);
  393. cb->args[2] = m;
  394. return skb->len;
  395. }
  396. int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
  397. {
  398. unsigned int h, s_h;
  399. unsigned int e = 0, s_e;
  400. struct dn_fib_table *tb;
  401. struct hlist_node *node;
  402. int dumped = 0;
  403. if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
  404. ((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
  405. return dn_cache_dump(skb, cb);
  406. s_h = cb->args[0];
  407. s_e = cb->args[1];
  408. for (h = s_h; h < DN_FIB_TABLE_HASHSZ; h++, s_h = 0) {
  409. e = 0;
  410. hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist) {
  411. if (e < s_e)
  412. goto next;
  413. if (dumped)
  414. memset(&cb->args[2], 0, sizeof(cb->args) -
  415. 2 * sizeof(cb->args[0]));
  416. if (tb->dump(tb, skb, cb) < 0)
  417. goto out;
  418. dumped = 1;
  419. next:
  420. e++;
  421. }
  422. }
  423. out:
  424. cb->args[1] = e;
  425. cb->args[0] = h;
  426. return skb->len;
  427. }
  428. 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)
  429. {
  430. struct dn_hash *table = (struct dn_hash *)tb->data;
  431. struct dn_fib_node *new_f, *f, **fp, **del_fp;
  432. struct dn_zone *dz;
  433. struct dn_fib_info *fi;
  434. int z = r->rtm_dst_len;
  435. int type = r->rtm_type;
  436. dn_fib_key_t key;
  437. int err;
  438. if (z > 16)
  439. return -EINVAL;
  440. dz = table->dh_zones[z];
  441. if (!dz && !(dz = dn_new_zone(table, z)))
  442. return -ENOBUFS;
  443. dz_key_0(key);
  444. if (rta->rta_dst) {
  445. __le16 dst;
  446. memcpy(&dst, rta->rta_dst, 2);
  447. if (dst & ~DZ_MASK(dz))
  448. return -EINVAL;
  449. key = dz_key(dst, dz);
  450. }
  451. if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
  452. return err;
  453. if (dz->dz_nent > (dz->dz_divisor << 2) &&
  454. dz->dz_divisor > DN_MAX_DIVISOR &&
  455. (z==16 || (1<<z) > dz->dz_divisor))
  456. dn_rehash_zone(dz);
  457. fp = dn_chain_p(key, dz);
  458. DN_FIB_SCAN(f, fp) {
  459. if (dn_key_leq(key, f->fn_key))
  460. break;
  461. }
  462. del_fp = NULL;
  463. if (f && (f->fn_state & DN_S_ZOMBIE) &&
  464. dn_key_eq(f->fn_key, key)) {
  465. del_fp = fp;
  466. fp = &f->fn_next;
  467. f = *fp;
  468. goto create;
  469. }
  470. DN_FIB_SCAN_KEY(f, fp, key) {
  471. if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
  472. break;
  473. }
  474. if (f && dn_key_eq(f->fn_key, key) &&
  475. fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
  476. struct dn_fib_node **ins_fp;
  477. err = -EEXIST;
  478. if (n->nlmsg_flags & NLM_F_EXCL)
  479. goto out;
  480. if (n->nlmsg_flags & NLM_F_REPLACE) {
  481. del_fp = fp;
  482. fp = &f->fn_next;
  483. f = *fp;
  484. goto replace;
  485. }
  486. ins_fp = fp;
  487. err = -EEXIST;
  488. DN_FIB_SCAN_KEY(f, fp, key) {
  489. if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
  490. break;
  491. if (f->fn_type == type && f->fn_scope == r->rtm_scope
  492. && DN_FIB_INFO(f) == fi)
  493. goto out;
  494. }
  495. if (!(n->nlmsg_flags & NLM_F_APPEND)) {
  496. fp = ins_fp;
  497. f = *fp;
  498. }
  499. }
  500. create:
  501. err = -ENOENT;
  502. if (!(n->nlmsg_flags & NLM_F_CREATE))
  503. goto out;
  504. replace:
  505. err = -ENOBUFS;
  506. new_f = kmem_cache_zalloc(dn_hash_kmem, GFP_KERNEL);
  507. if (new_f == NULL)
  508. goto out;
  509. new_f->fn_key = key;
  510. new_f->fn_type = type;
  511. new_f->fn_scope = r->rtm_scope;
  512. DN_FIB_INFO(new_f) = fi;
  513. new_f->fn_next = f;
  514. write_lock_bh(&dn_fib_tables_lock);
  515. *fp = new_f;
  516. write_unlock_bh(&dn_fib_tables_lock);
  517. dz->dz_nent++;
  518. if (del_fp) {
  519. f = *del_fp;
  520. write_lock_bh(&dn_fib_tables_lock);
  521. *del_fp = f->fn_next;
  522. write_unlock_bh(&dn_fib_tables_lock);
  523. if (!(f->fn_state & DN_S_ZOMBIE))
  524. dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
  525. if (f->fn_state & DN_S_ACCESSED)
  526. dn_rt_cache_flush(-1);
  527. dn_free_node(f);
  528. dz->dz_nent--;
  529. } else {
  530. dn_rt_cache_flush(-1);
  531. }
  532. dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
  533. return 0;
  534. out:
  535. dn_fib_release_info(fi);
  536. return err;
  537. }
  538. 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)
  539. {
  540. struct dn_hash *table = (struct dn_hash*)tb->data;
  541. struct dn_fib_node **fp, **del_fp, *f;
  542. int z = r->rtm_dst_len;
  543. struct dn_zone *dz;
  544. dn_fib_key_t key;
  545. int matched;
  546. if (z > 16)
  547. return -EINVAL;
  548. if ((dz = table->dh_zones[z]) == NULL)
  549. return -ESRCH;
  550. dz_key_0(key);
  551. if (rta->rta_dst) {
  552. __le16 dst;
  553. memcpy(&dst, rta->rta_dst, 2);
  554. if (dst & ~DZ_MASK(dz))
  555. return -EINVAL;
  556. key = dz_key(dst, dz);
  557. }
  558. fp = dn_chain_p(key, dz);
  559. DN_FIB_SCAN(f, fp) {
  560. if (dn_key_eq(f->fn_key, key))
  561. break;
  562. if (dn_key_leq(key, f->fn_key))
  563. return -ESRCH;
  564. }
  565. matched = 0;
  566. del_fp = NULL;
  567. DN_FIB_SCAN_KEY(f, fp, key) {
  568. struct dn_fib_info *fi = DN_FIB_INFO(f);
  569. if (f->fn_state & DN_S_ZOMBIE)
  570. return -ESRCH;
  571. matched++;
  572. if (del_fp == NULL &&
  573. (!r->rtm_type || f->fn_type == r->rtm_type) &&
  574. (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
  575. (!r->rtm_protocol ||
  576. fi->fib_protocol == r->rtm_protocol) &&
  577. dn_fib_nh_match(r, n, rta, fi) == 0)
  578. del_fp = fp;
  579. }
  580. if (del_fp) {
  581. f = *del_fp;
  582. dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
  583. if (matched != 1) {
  584. write_lock_bh(&dn_fib_tables_lock);
  585. *del_fp = f->fn_next;
  586. write_unlock_bh(&dn_fib_tables_lock);
  587. if (f->fn_state & DN_S_ACCESSED)
  588. dn_rt_cache_flush(-1);
  589. dn_free_node(f);
  590. dz->dz_nent--;
  591. } else {
  592. f->fn_state |= DN_S_ZOMBIE;
  593. if (f->fn_state & DN_S_ACCESSED) {
  594. f->fn_state &= ~DN_S_ACCESSED;
  595. dn_rt_cache_flush(-1);
  596. }
  597. if (++dn_fib_hash_zombies > 128)
  598. dn_fib_flush();
  599. }
  600. return 0;
  601. }
  602. return -ESRCH;
  603. }
  604. static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
  605. {
  606. int found = 0;
  607. struct dn_fib_node *f;
  608. while((f = *fp) != NULL) {
  609. struct dn_fib_info *fi = DN_FIB_INFO(f);
  610. if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
  611. write_lock_bh(&dn_fib_tables_lock);
  612. *fp = f->fn_next;
  613. write_unlock_bh(&dn_fib_tables_lock);
  614. dn_free_node(f);
  615. found++;
  616. continue;
  617. }
  618. fp = &f->fn_next;
  619. }
  620. return found;
  621. }
  622. static int dn_fib_table_flush(struct dn_fib_table *tb)
  623. {
  624. struct dn_hash *table = (struct dn_hash *)tb->data;
  625. struct dn_zone *dz;
  626. int found = 0;
  627. dn_fib_hash_zombies = 0;
  628. for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
  629. int i;
  630. int tmp = 0;
  631. for(i = dz->dz_divisor-1; i >= 0; i--)
  632. tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
  633. dz->dz_nent -= tmp;
  634. found += tmp;
  635. }
  636. return found;
  637. }
  638. static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp, struct dn_fib_res *res)
  639. {
  640. int err;
  641. struct dn_zone *dz;
  642. struct dn_hash *t = (struct dn_hash *)tb->data;
  643. read_lock(&dn_fib_tables_lock);
  644. for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
  645. struct dn_fib_node *f;
  646. dn_fib_key_t k = dz_key(flp->fld_dst, dz);
  647. for(f = dz_chain(k, dz); f; f = f->fn_next) {
  648. if (!dn_key_eq(k, f->fn_key)) {
  649. if (dn_key_leq(k, f->fn_key))
  650. break;
  651. else
  652. continue;
  653. }
  654. f->fn_state |= DN_S_ACCESSED;
  655. if (f->fn_state&DN_S_ZOMBIE)
  656. continue;
  657. if (f->fn_scope < flp->fld_scope)
  658. continue;
  659. err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
  660. if (err == 0) {
  661. res->type = f->fn_type;
  662. res->scope = f->fn_scope;
  663. res->prefixlen = dz->dz_order;
  664. goto out;
  665. }
  666. if (err < 0)
  667. goto out;
  668. }
  669. }
  670. err = 1;
  671. out:
  672. read_unlock(&dn_fib_tables_lock);
  673. return err;
  674. }
  675. struct dn_fib_table *dn_fib_get_table(u32 n, int create)
  676. {
  677. struct dn_fib_table *t;
  678. struct hlist_node *node;
  679. unsigned int h;
  680. if (n < RT_TABLE_MIN)
  681. return NULL;
  682. if (n > RT_TABLE_MAX)
  683. return NULL;
  684. h = n & (DN_FIB_TABLE_HASHSZ - 1);
  685. rcu_read_lock();
  686. hlist_for_each_entry_rcu(t, node, &dn_fib_table_hash[h], hlist) {
  687. if (t->n == n) {
  688. rcu_read_unlock();
  689. return t;
  690. }
  691. }
  692. rcu_read_unlock();
  693. if (!create)
  694. return NULL;
  695. if (in_interrupt() && net_ratelimit()) {
  696. printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n");
  697. return NULL;
  698. }
  699. t = kzalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash),
  700. GFP_KERNEL);
  701. if (t == NULL)
  702. return NULL;
  703. t->n = n;
  704. t->insert = dn_fib_table_insert;
  705. t->delete = dn_fib_table_delete;
  706. t->lookup = dn_fib_table_lookup;
  707. t->flush = dn_fib_table_flush;
  708. t->dump = dn_fib_table_dump;
  709. hlist_add_head_rcu(&t->hlist, &dn_fib_table_hash[h]);
  710. return t;
  711. }
  712. struct dn_fib_table *dn_fib_empty_table(void)
  713. {
  714. u32 id;
  715. for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
  716. if (dn_fib_get_table(id, 0) == NULL)
  717. return dn_fib_get_table(id, 1);
  718. return NULL;
  719. }
  720. void dn_fib_flush(void)
  721. {
  722. int flushed = 0;
  723. struct dn_fib_table *tb;
  724. struct hlist_node *node;
  725. unsigned int h;
  726. for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
  727. hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist)
  728. flushed += tb->flush(tb);
  729. }
  730. if (flushed)
  731. dn_rt_cache_flush(-1);
  732. }
  733. void __init dn_fib_table_init(void)
  734. {
  735. dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
  736. sizeof(struct dn_fib_info),
  737. 0, SLAB_HWCACHE_ALIGN,
  738. NULL, NULL);
  739. }
  740. void __exit dn_fib_table_cleanup(void)
  741. {
  742. struct dn_fib_table *t;
  743. struct hlist_node *node, *next;
  744. unsigned int h;
  745. write_lock(&dn_fib_tables_lock);
  746. for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
  747. hlist_for_each_entry_safe(t, node, next, &dn_fib_table_hash[h],
  748. hlist) {
  749. hlist_del(&t->hlist);
  750. kfree(t);
  751. }
  752. }
  753. write_unlock(&dn_fib_tables_lock);
  754. }