fib_hash.c 23 KB

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