dn_table.c 20 KB

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