dn_table.c 20 KB

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