dn_table.c 20 KB

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