fib_hash.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IPv4 FIB: lookup engine and maintenance routines.
  7. *
  8. * Version: $Id: fib_hash.c,v 1.13 2001/10/31 21:55:54 davem Exp $
  9. *
  10. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <asm/uaccess.h>
  18. #include <asm/system.h>
  19. #include <linux/bitops.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/string.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/errno.h>
  27. #include <linux/in.h>
  28. #include <linux/inet.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/if_arp.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/netlink.h>
  35. #include <linux/init.h>
  36. #include <net/net_namespace.h>
  37. #include <net/ip.h>
  38. #include <net/protocol.h>
  39. #include <net/route.h>
  40. #include <net/tcp.h>
  41. #include <net/sock.h>
  42. #include <net/ip_fib.h>
  43. #include "fib_lookup.h"
  44. static struct kmem_cache *fn_hash_kmem __read_mostly;
  45. static struct kmem_cache *fn_alias_kmem __read_mostly;
  46. struct fib_node {
  47. struct hlist_node fn_hash;
  48. struct list_head fn_alias;
  49. __be32 fn_key;
  50. };
  51. struct fn_zone {
  52. struct fn_zone *fz_next; /* Next not empty zone */
  53. struct hlist_head *fz_hash; /* Hash table pointer */
  54. int fz_nent; /* Number of entries */
  55. int fz_divisor; /* Hash divisor */
  56. u32 fz_hashmask; /* (fz_divisor - 1) */
  57. #define FZ_HASHMASK(fz) ((fz)->fz_hashmask)
  58. int fz_order; /* Zone order */
  59. __be32 fz_mask;
  60. #define FZ_MASK(fz) ((fz)->fz_mask)
  61. };
  62. /* NOTE. On fast computers evaluation of fz_hashmask and fz_mask
  63. * can be cheaper than memory lookup, so that FZ_* macros are used.
  64. */
  65. struct fn_hash {
  66. struct fn_zone *fn_zones[33];
  67. struct fn_zone *fn_zone_list;
  68. };
  69. static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
  70. {
  71. u32 h = ntohl(key)>>(32 - fz->fz_order);
  72. h ^= (h>>20);
  73. h ^= (h>>10);
  74. h ^= (h>>5);
  75. h &= FZ_HASHMASK(fz);
  76. return h;
  77. }
  78. static inline __be32 fz_key(__be32 dst, struct fn_zone *fz)
  79. {
  80. return dst & FZ_MASK(fz);
  81. }
  82. static DEFINE_RWLOCK(fib_hash_lock);
  83. static unsigned int fib_hash_genid;
  84. #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
  85. static struct hlist_head *fz_hash_alloc(int divisor)
  86. {
  87. unsigned long size = divisor * sizeof(struct hlist_head);
  88. if (size <= PAGE_SIZE) {
  89. return kzalloc(size, GFP_KERNEL);
  90. } else {
  91. return (struct hlist_head *)
  92. __get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(size));
  93. }
  94. }
  95. /* The fib hash lock must be held when this is called. */
  96. static inline void fn_rebuild_zone(struct fn_zone *fz,
  97. struct hlist_head *old_ht,
  98. int old_divisor)
  99. {
  100. int i;
  101. for (i = 0; i < old_divisor; i++) {
  102. struct hlist_node *node, *n;
  103. struct fib_node *f;
  104. hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
  105. struct hlist_head *new_head;
  106. hlist_del(&f->fn_hash);
  107. new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  108. hlist_add_head(&f->fn_hash, new_head);
  109. }
  110. }
  111. }
  112. static void fz_hash_free(struct hlist_head *hash, int divisor)
  113. {
  114. unsigned long size = divisor * sizeof(struct hlist_head);
  115. if (size <= PAGE_SIZE)
  116. kfree(hash);
  117. else
  118. free_pages((unsigned long)hash, get_order(size));
  119. }
  120. static void fn_rehash_zone(struct fn_zone *fz)
  121. {
  122. struct hlist_head *ht, *old_ht;
  123. int old_divisor, new_divisor;
  124. u32 new_hashmask;
  125. old_divisor = fz->fz_divisor;
  126. switch (old_divisor) {
  127. case 16:
  128. new_divisor = 256;
  129. break;
  130. case 256:
  131. new_divisor = 1024;
  132. break;
  133. default:
  134. if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
  135. printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
  136. return;
  137. }
  138. new_divisor = (old_divisor << 1);
  139. break;
  140. }
  141. new_hashmask = (new_divisor - 1);
  142. #if RT_CACHE_DEBUG >= 2
  143. printk("fn_rehash_zone: hash for zone %d grows from %d\n", fz->fz_order, old_divisor);
  144. #endif
  145. ht = fz_hash_alloc(new_divisor);
  146. if (ht) {
  147. write_lock_bh(&fib_hash_lock);
  148. old_ht = fz->fz_hash;
  149. fz->fz_hash = ht;
  150. fz->fz_hashmask = new_hashmask;
  151. fz->fz_divisor = new_divisor;
  152. fn_rebuild_zone(fz, old_ht, old_divisor);
  153. fib_hash_genid++;
  154. write_unlock_bh(&fib_hash_lock);
  155. fz_hash_free(old_ht, old_divisor);
  156. }
  157. }
  158. static inline void fn_free_node(struct fib_node * f)
  159. {
  160. kmem_cache_free(fn_hash_kmem, f);
  161. }
  162. static inline void fn_free_alias(struct fib_alias *fa)
  163. {
  164. fib_release_info(fa->fa_info);
  165. kmem_cache_free(fn_alias_kmem, fa);
  166. }
  167. static struct fn_zone *
  168. fn_new_zone(struct fn_hash *table, int z)
  169. {
  170. int i;
  171. struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
  172. if (!fz)
  173. return NULL;
  174. if (z) {
  175. fz->fz_divisor = 16;
  176. } else {
  177. fz->fz_divisor = 1;
  178. }
  179. fz->fz_hashmask = (fz->fz_divisor - 1);
  180. fz->fz_hash = fz_hash_alloc(fz->fz_divisor);
  181. if (!fz->fz_hash) {
  182. kfree(fz);
  183. return NULL;
  184. }
  185. fz->fz_order = z;
  186. fz->fz_mask = inet_make_mask(z);
  187. /* Find the first not empty zone with more specific mask */
  188. for (i=z+1; i<=32; i++)
  189. if (table->fn_zones[i])
  190. break;
  191. write_lock_bh(&fib_hash_lock);
  192. if (i>32) {
  193. /* No more specific masks, we are the first. */
  194. fz->fz_next = table->fn_zone_list;
  195. table->fn_zone_list = fz;
  196. } else {
  197. fz->fz_next = table->fn_zones[i]->fz_next;
  198. table->fn_zones[i]->fz_next = fz;
  199. }
  200. table->fn_zones[z] = fz;
  201. fib_hash_genid++;
  202. write_unlock_bh(&fib_hash_lock);
  203. return fz;
  204. }
  205. static int
  206. fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
  207. {
  208. int err;
  209. struct fn_zone *fz;
  210. struct fn_hash *t = (struct fn_hash*)tb->tb_data;
  211. read_lock(&fib_hash_lock);
  212. for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
  213. struct hlist_head *head;
  214. struct hlist_node *node;
  215. struct fib_node *f;
  216. __be32 k = fz_key(flp->fl4_dst, fz);
  217. head = &fz->fz_hash[fn_hash(k, fz)];
  218. hlist_for_each_entry(f, node, head, fn_hash) {
  219. if (f->fn_key != k)
  220. continue;
  221. err = fib_semantic_match(&f->fn_alias,
  222. flp, res,
  223. f->fn_key, fz->fz_mask,
  224. fz->fz_order);
  225. if (err <= 0)
  226. goto out;
  227. }
  228. }
  229. err = 1;
  230. out:
  231. read_unlock(&fib_hash_lock);
  232. return err;
  233. }
  234. static void
  235. fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
  236. {
  237. int order, last_idx;
  238. struct hlist_node *node;
  239. struct fib_node *f;
  240. struct fib_info *fi = NULL;
  241. struct fib_info *last_resort;
  242. struct fn_hash *t = (struct fn_hash*)tb->tb_data;
  243. struct fn_zone *fz = t->fn_zones[0];
  244. if (fz == NULL)
  245. return;
  246. last_idx = -1;
  247. last_resort = NULL;
  248. order = -1;
  249. read_lock(&fib_hash_lock);
  250. hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
  251. struct fib_alias *fa;
  252. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  253. struct fib_info *next_fi = fa->fa_info;
  254. if (fa->fa_scope != res->scope ||
  255. fa->fa_type != RTN_UNICAST)
  256. continue;
  257. if (next_fi->fib_priority > res->fi->fib_priority)
  258. break;
  259. if (!next_fi->fib_nh[0].nh_gw ||
  260. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  261. continue;
  262. fa->fa_state |= FA_S_ACCESSED;
  263. if (fi == NULL) {
  264. if (next_fi != res->fi)
  265. break;
  266. } else if (!fib_detect_death(fi, order, &last_resort,
  267. &last_idx, tb->tb_default)) {
  268. fib_result_assign(res, fi);
  269. tb->tb_default = order;
  270. goto out;
  271. }
  272. fi = next_fi;
  273. order++;
  274. }
  275. }
  276. if (order <= 0 || fi == NULL) {
  277. tb->tb_default = -1;
  278. goto out;
  279. }
  280. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  281. tb->tb_default)) {
  282. fib_result_assign(res, fi);
  283. tb->tb_default = order;
  284. goto out;
  285. }
  286. if (last_idx >= 0)
  287. fib_result_assign(res, last_resort);
  288. tb->tb_default = last_idx;
  289. out:
  290. read_unlock(&fib_hash_lock);
  291. }
  292. /* Insert node F to FZ. */
  293. static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
  294. {
  295. struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  296. hlist_add_head(&f->fn_hash, head);
  297. }
  298. /* Return the node in FZ matching KEY. */
  299. static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
  300. {
  301. struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
  302. struct hlist_node *node;
  303. struct fib_node *f;
  304. hlist_for_each_entry(f, node, head, fn_hash) {
  305. if (f->fn_key == key)
  306. return f;
  307. }
  308. return NULL;
  309. }
  310. static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
  311. {
  312. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  313. struct fib_node *new_f, *f;
  314. struct fib_alias *fa, *new_fa;
  315. struct fn_zone *fz;
  316. struct fib_info *fi;
  317. u8 tos = cfg->fc_tos;
  318. __be32 key;
  319. int err;
  320. if (cfg->fc_dst_len > 32)
  321. return -EINVAL;
  322. fz = table->fn_zones[cfg->fc_dst_len];
  323. if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
  324. return -ENOBUFS;
  325. key = 0;
  326. if (cfg->fc_dst) {
  327. if (cfg->fc_dst & ~FZ_MASK(fz))
  328. return -EINVAL;
  329. key = fz_key(cfg->fc_dst, fz);
  330. }
  331. fi = fib_create_info(cfg);
  332. if (IS_ERR(fi))
  333. return PTR_ERR(fi);
  334. if (fz->fz_nent > (fz->fz_divisor<<1) &&
  335. fz->fz_divisor < FZ_MAX_DIVISOR &&
  336. (cfg->fc_dst_len == 32 ||
  337. (1 << cfg->fc_dst_len) > fz->fz_divisor))
  338. fn_rehash_zone(fz);
  339. f = fib_find_node(fz, key);
  340. if (!f)
  341. fa = NULL;
  342. else
  343. fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
  344. /* Now fa, if non-NULL, points to the first fib alias
  345. * with the same keys [prefix,tos,priority], if such key already
  346. * exists or to the node before which we will insert new one.
  347. *
  348. * If fa is NULL, we will need to allocate a new one and
  349. * insert to the head of f.
  350. *
  351. * If f is NULL, no fib node matched the destination key
  352. * and we need to allocate a new one of those as well.
  353. */
  354. if (fa && fa->fa_tos == tos &&
  355. fa->fa_info->fib_priority == fi->fib_priority) {
  356. struct fib_alias *fa_orig;
  357. err = -EEXIST;
  358. if (cfg->fc_nlflags & NLM_F_EXCL)
  359. goto out;
  360. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  361. struct fib_info *fi_drop;
  362. u8 state;
  363. if (fi->fib_treeref > 1)
  364. goto out;
  365. write_lock_bh(&fib_hash_lock);
  366. fi_drop = fa->fa_info;
  367. fa->fa_info = fi;
  368. fa->fa_type = cfg->fc_type;
  369. fa->fa_scope = cfg->fc_scope;
  370. state = fa->fa_state;
  371. fa->fa_state &= ~FA_S_ACCESSED;
  372. fib_hash_genid++;
  373. write_unlock_bh(&fib_hash_lock);
  374. fib_release_info(fi_drop);
  375. if (state & FA_S_ACCESSED)
  376. rt_cache_flush(-1);
  377. rtmsg_fib(RTM_NEWROUTE, key, fa, cfg->fc_dst_len, tb->tb_id,
  378. &cfg->fc_nlinfo, NLM_F_REPLACE);
  379. return 0;
  380. }
  381. /* Error if we find a perfect match which
  382. * uses the same scope, type, and nexthop
  383. * information.
  384. */
  385. fa_orig = fa;
  386. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  387. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  388. if (fa->fa_tos != tos)
  389. break;
  390. if (fa->fa_info->fib_priority != fi->fib_priority)
  391. break;
  392. if (fa->fa_type == cfg->fc_type &&
  393. fa->fa_scope == cfg->fc_scope &&
  394. fa->fa_info == fi)
  395. goto out;
  396. }
  397. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  398. fa = fa_orig;
  399. }
  400. err = -ENOENT;
  401. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  402. goto out;
  403. err = -ENOBUFS;
  404. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  405. if (new_fa == NULL)
  406. goto out;
  407. new_f = NULL;
  408. if (!f) {
  409. new_f = kmem_cache_alloc(fn_hash_kmem, GFP_KERNEL);
  410. if (new_f == NULL)
  411. goto out_free_new_fa;
  412. INIT_HLIST_NODE(&new_f->fn_hash);
  413. INIT_LIST_HEAD(&new_f->fn_alias);
  414. new_f->fn_key = key;
  415. f = new_f;
  416. }
  417. new_fa->fa_info = fi;
  418. new_fa->fa_tos = tos;
  419. new_fa->fa_type = cfg->fc_type;
  420. new_fa->fa_scope = cfg->fc_scope;
  421. new_fa->fa_state = 0;
  422. /*
  423. * Insert new entry to the list.
  424. */
  425. write_lock_bh(&fib_hash_lock);
  426. if (new_f)
  427. fib_insert_node(fz, new_f);
  428. list_add_tail(&new_fa->fa_list,
  429. (fa ? &fa->fa_list : &f->fn_alias));
  430. fib_hash_genid++;
  431. write_unlock_bh(&fib_hash_lock);
  432. if (new_f)
  433. fz->fz_nent++;
  434. rt_cache_flush(-1);
  435. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
  436. &cfg->fc_nlinfo, 0);
  437. return 0;
  438. out_free_new_fa:
  439. kmem_cache_free(fn_alias_kmem, new_fa);
  440. out:
  441. fib_release_info(fi);
  442. return err;
  443. }
  444. static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
  445. {
  446. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  447. struct fib_node *f;
  448. struct fib_alias *fa, *fa_to_delete;
  449. struct fn_zone *fz;
  450. __be32 key;
  451. if (cfg->fc_dst_len > 32)
  452. return -EINVAL;
  453. if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
  454. return -ESRCH;
  455. key = 0;
  456. if (cfg->fc_dst) {
  457. if (cfg->fc_dst & ~FZ_MASK(fz))
  458. return -EINVAL;
  459. key = fz_key(cfg->fc_dst, fz);
  460. }
  461. f = fib_find_node(fz, key);
  462. if (!f)
  463. fa = NULL;
  464. else
  465. fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
  466. if (!fa)
  467. return -ESRCH;
  468. fa_to_delete = NULL;
  469. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  470. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  471. struct fib_info *fi = fa->fa_info;
  472. if (fa->fa_tos != cfg->fc_tos)
  473. break;
  474. if ((!cfg->fc_type ||
  475. fa->fa_type == cfg->fc_type) &&
  476. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  477. fa->fa_scope == cfg->fc_scope) &&
  478. (!cfg->fc_protocol ||
  479. fi->fib_protocol == cfg->fc_protocol) &&
  480. fib_nh_match(cfg, fi) == 0) {
  481. fa_to_delete = fa;
  482. break;
  483. }
  484. }
  485. if (fa_to_delete) {
  486. int kill_fn;
  487. fa = fa_to_delete;
  488. rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
  489. tb->tb_id, &cfg->fc_nlinfo, 0);
  490. kill_fn = 0;
  491. write_lock_bh(&fib_hash_lock);
  492. list_del(&fa->fa_list);
  493. if (list_empty(&f->fn_alias)) {
  494. hlist_del(&f->fn_hash);
  495. kill_fn = 1;
  496. }
  497. fib_hash_genid++;
  498. write_unlock_bh(&fib_hash_lock);
  499. if (fa->fa_state & FA_S_ACCESSED)
  500. rt_cache_flush(-1);
  501. fn_free_alias(fa);
  502. if (kill_fn) {
  503. fn_free_node(f);
  504. fz->fz_nent--;
  505. }
  506. return 0;
  507. }
  508. return -ESRCH;
  509. }
  510. static int fn_flush_list(struct fn_zone *fz, int idx)
  511. {
  512. struct hlist_head *head = &fz->fz_hash[idx];
  513. struct hlist_node *node, *n;
  514. struct fib_node *f;
  515. int found = 0;
  516. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  517. struct fib_alias *fa, *fa_node;
  518. int kill_f;
  519. kill_f = 0;
  520. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  521. struct fib_info *fi = fa->fa_info;
  522. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  523. write_lock_bh(&fib_hash_lock);
  524. list_del(&fa->fa_list);
  525. if (list_empty(&f->fn_alias)) {
  526. hlist_del(&f->fn_hash);
  527. kill_f = 1;
  528. }
  529. fib_hash_genid++;
  530. write_unlock_bh(&fib_hash_lock);
  531. fn_free_alias(fa);
  532. found++;
  533. }
  534. }
  535. if (kill_f) {
  536. fn_free_node(f);
  537. fz->fz_nent--;
  538. }
  539. }
  540. return found;
  541. }
  542. static int fn_hash_flush(struct fib_table *tb)
  543. {
  544. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  545. struct fn_zone *fz;
  546. int found = 0;
  547. for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
  548. int i;
  549. for (i = fz->fz_divisor - 1; i >= 0; i--)
  550. found += fn_flush_list(fz, i);
  551. }
  552. return found;
  553. }
  554. static inline int
  555. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  556. struct fib_table *tb,
  557. struct fn_zone *fz,
  558. struct hlist_head *head)
  559. {
  560. struct hlist_node *node;
  561. struct fib_node *f;
  562. int i, s_i;
  563. s_i = cb->args[4];
  564. i = 0;
  565. hlist_for_each_entry(f, node, head, fn_hash) {
  566. struct fib_alias *fa;
  567. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  568. if (i < s_i)
  569. goto next;
  570. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  571. cb->nlh->nlmsg_seq,
  572. RTM_NEWROUTE,
  573. tb->tb_id,
  574. fa->fa_type,
  575. fa->fa_scope,
  576. f->fn_key,
  577. fz->fz_order,
  578. fa->fa_tos,
  579. fa->fa_info,
  580. NLM_F_MULTI) < 0) {
  581. cb->args[4] = i;
  582. return -1;
  583. }
  584. next:
  585. i++;
  586. }
  587. }
  588. cb->args[4] = i;
  589. return skb->len;
  590. }
  591. static inline int
  592. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  593. struct fib_table *tb,
  594. struct fn_zone *fz)
  595. {
  596. int h, s_h;
  597. if (fz->fz_hash == NULL)
  598. return skb->len;
  599. s_h = cb->args[3];
  600. for (h = s_h; h < fz->fz_divisor; h++) {
  601. if (hlist_empty(&fz->fz_hash[h]))
  602. continue;
  603. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h]) < 0) {
  604. cb->args[3] = h;
  605. return -1;
  606. }
  607. memset(&cb->args[4], 0,
  608. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  609. }
  610. cb->args[3] = h;
  611. return skb->len;
  612. }
  613. static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
  614. {
  615. int m, s_m;
  616. struct fn_zone *fz;
  617. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  618. s_m = cb->args[2];
  619. read_lock(&fib_hash_lock);
  620. for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
  621. if (m < s_m) continue;
  622. if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  623. cb->args[2] = m;
  624. read_unlock(&fib_hash_lock);
  625. return -1;
  626. }
  627. memset(&cb->args[3], 0,
  628. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  629. }
  630. read_unlock(&fib_hash_lock);
  631. cb->args[2] = m;
  632. return skb->len;
  633. }
  634. struct fib_table *fib_hash_init(u32 id)
  635. {
  636. struct fib_table *tb;
  637. if (fn_hash_kmem == NULL)
  638. fn_hash_kmem = kmem_cache_create("ip_fib_hash",
  639. sizeof(struct fib_node),
  640. 0, SLAB_HWCACHE_ALIGN,
  641. NULL);
  642. if (fn_alias_kmem == NULL)
  643. fn_alias_kmem = kmem_cache_create("ip_fib_alias",
  644. sizeof(struct fib_alias),
  645. 0, SLAB_HWCACHE_ALIGN,
  646. NULL);
  647. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
  648. GFP_KERNEL);
  649. if (tb == NULL)
  650. return NULL;
  651. tb->tb_id = id;
  652. tb->tb_default = -1;
  653. tb->tb_lookup = fn_hash_lookup;
  654. tb->tb_insert = fn_hash_insert;
  655. tb->tb_delete = fn_hash_delete;
  656. tb->tb_flush = fn_hash_flush;
  657. tb->tb_select_default = fn_hash_select_default;
  658. tb->tb_dump = fn_hash_dump;
  659. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  660. return tb;
  661. }
  662. /* ------------------------------------------------------------------------ */
  663. #ifdef CONFIG_PROC_FS
  664. struct fib_iter_state {
  665. struct fn_zone *zone;
  666. int bucket;
  667. struct hlist_head *hash_head;
  668. struct fib_node *fn;
  669. struct fib_alias *fa;
  670. loff_t pos;
  671. unsigned int genid;
  672. int valid;
  673. };
  674. static struct fib_alias *fib_get_first(struct seq_file *seq)
  675. {
  676. struct fib_iter_state *iter = seq->private;
  677. struct fib_table *main_table = fib_get_table(&init_net, RT_TABLE_MAIN);
  678. struct fn_hash *table = (struct fn_hash *)main_table->tb_data;
  679. iter->bucket = 0;
  680. iter->hash_head = NULL;
  681. iter->fn = NULL;
  682. iter->fa = NULL;
  683. iter->pos = 0;
  684. iter->genid = fib_hash_genid;
  685. iter->valid = 1;
  686. for (iter->zone = table->fn_zone_list; iter->zone;
  687. iter->zone = iter->zone->fz_next) {
  688. int maxslot;
  689. if (!iter->zone->fz_nent)
  690. continue;
  691. iter->hash_head = iter->zone->fz_hash;
  692. maxslot = iter->zone->fz_divisor;
  693. for (iter->bucket = 0; iter->bucket < maxslot;
  694. ++iter->bucket, ++iter->hash_head) {
  695. struct hlist_node *node;
  696. struct fib_node *fn;
  697. hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
  698. struct fib_alias *fa;
  699. list_for_each_entry(fa,&fn->fn_alias,fa_list) {
  700. iter->fn = fn;
  701. iter->fa = fa;
  702. goto out;
  703. }
  704. }
  705. }
  706. }
  707. out:
  708. return iter->fa;
  709. }
  710. static struct fib_alias *fib_get_next(struct seq_file *seq)
  711. {
  712. struct fib_iter_state *iter = seq->private;
  713. struct fib_node *fn;
  714. struct fib_alias *fa;
  715. /* Advance FA, if any. */
  716. fn = iter->fn;
  717. fa = iter->fa;
  718. if (fa) {
  719. BUG_ON(!fn);
  720. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  721. iter->fa = fa;
  722. goto out;
  723. }
  724. }
  725. fa = iter->fa = NULL;
  726. /* Advance FN. */
  727. if (fn) {
  728. struct hlist_node *node = &fn->fn_hash;
  729. hlist_for_each_entry_continue(fn, node, fn_hash) {
  730. iter->fn = fn;
  731. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  732. iter->fa = fa;
  733. goto out;
  734. }
  735. }
  736. }
  737. fn = iter->fn = NULL;
  738. /* Advance hash chain. */
  739. if (!iter->zone)
  740. goto out;
  741. for (;;) {
  742. struct hlist_node *node;
  743. int maxslot;
  744. maxslot = iter->zone->fz_divisor;
  745. while (++iter->bucket < maxslot) {
  746. iter->hash_head++;
  747. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  748. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  749. iter->fn = fn;
  750. iter->fa = fa;
  751. goto out;
  752. }
  753. }
  754. }
  755. iter->zone = iter->zone->fz_next;
  756. if (!iter->zone)
  757. goto out;
  758. iter->bucket = 0;
  759. iter->hash_head = iter->zone->fz_hash;
  760. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  761. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  762. iter->fn = fn;
  763. iter->fa = fa;
  764. goto out;
  765. }
  766. }
  767. }
  768. out:
  769. iter->pos++;
  770. return fa;
  771. }
  772. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  773. {
  774. struct fib_iter_state *iter = seq->private;
  775. struct fib_alias *fa;
  776. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  777. fa = iter->fa;
  778. pos -= iter->pos;
  779. } else
  780. fa = fib_get_first(seq);
  781. if (fa)
  782. while (pos && (fa = fib_get_next(seq)))
  783. --pos;
  784. return pos ? NULL : fa;
  785. }
  786. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  787. __acquires(fib_hash_lock)
  788. {
  789. void *v = NULL;
  790. read_lock(&fib_hash_lock);
  791. if (fib_get_table(&init_net, RT_TABLE_MAIN))
  792. v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  793. return v;
  794. }
  795. static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  796. {
  797. ++*pos;
  798. return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
  799. }
  800. static void fib_seq_stop(struct seq_file *seq, void *v)
  801. __releases(fib_hash_lock)
  802. {
  803. read_unlock(&fib_hash_lock);
  804. }
  805. static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
  806. {
  807. static const unsigned type2flags[RTN_MAX + 1] = {
  808. [7] = RTF_REJECT, [8] = RTF_REJECT,
  809. };
  810. unsigned flags = type2flags[type];
  811. if (fi && fi->fib_nh->nh_gw)
  812. flags |= RTF_GATEWAY;
  813. if (mask == htonl(0xFFFFFFFF))
  814. flags |= RTF_HOST;
  815. flags |= RTF_UP;
  816. return flags;
  817. }
  818. /*
  819. * This outputs /proc/net/route.
  820. *
  821. * It always works in backward compatibility mode.
  822. * The format of the file is not supposed to be changed.
  823. */
  824. static int fib_seq_show(struct seq_file *seq, void *v)
  825. {
  826. struct fib_iter_state *iter;
  827. char bf[128];
  828. __be32 prefix, mask;
  829. unsigned flags;
  830. struct fib_node *f;
  831. struct fib_alias *fa;
  832. struct fib_info *fi;
  833. if (v == SEQ_START_TOKEN) {
  834. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  835. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  836. "\tWindow\tIRTT");
  837. goto out;
  838. }
  839. iter = seq->private;
  840. f = iter->fn;
  841. fa = iter->fa;
  842. fi = fa->fa_info;
  843. prefix = f->fn_key;
  844. mask = FZ_MASK(iter->zone);
  845. flags = fib_flag_trans(fa->fa_type, mask, fi);
  846. if (fi)
  847. snprintf(bf, sizeof(bf),
  848. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  849. fi->fib_dev ? fi->fib_dev->name : "*", prefix,
  850. fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
  851. mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
  852. fi->fib_window,
  853. fi->fib_rtt >> 3);
  854. else
  855. snprintf(bf, sizeof(bf),
  856. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  857. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
  858. seq_printf(seq, "%-127s\n", bf);
  859. out:
  860. return 0;
  861. }
  862. static const struct seq_operations fib_seq_ops = {
  863. .start = fib_seq_start,
  864. .next = fib_seq_next,
  865. .stop = fib_seq_stop,
  866. .show = fib_seq_show,
  867. };
  868. static int fib_seq_open(struct inode *inode, struct file *file)
  869. {
  870. return seq_open_private(file, &fib_seq_ops,
  871. sizeof(struct fib_iter_state));
  872. }
  873. static const struct file_operations fib_seq_fops = {
  874. .owner = THIS_MODULE,
  875. .open = fib_seq_open,
  876. .read = seq_read,
  877. .llseek = seq_lseek,
  878. .release = seq_release_private,
  879. };
  880. int __net_init fib_proc_init(struct net *net)
  881. {
  882. if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_seq_fops))
  883. return -ENOMEM;
  884. return 0;
  885. }
  886. void __net_exit fib_proc_exit(struct net *net)
  887. {
  888. proc_net_remove(net, "route");
  889. }
  890. #endif /* CONFIG_PROC_FS */