dn_table.c 20 KB

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