fib_hash.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. int fib_table_lookup(struct fib_table *tb,
  209. 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. fz->fz_order);
  227. if (err <= 0)
  228. goto out;
  229. }
  230. }
  231. err = 1;
  232. out:
  233. read_unlock(&fib_hash_lock);
  234. return err;
  235. }
  236. void fib_table_select_default(struct fib_table *tb,
  237. const struct flowi *flp, struct fib_result *res)
  238. {
  239. int order, last_idx;
  240. struct hlist_node *node;
  241. struct fib_node *f;
  242. struct fib_info *fi = NULL;
  243. struct fib_info *last_resort;
  244. struct fn_hash *t = (struct fn_hash *)tb->tb_data;
  245. struct fn_zone *fz = t->fn_zones[0];
  246. if (fz == NULL)
  247. return;
  248. last_idx = -1;
  249. last_resort = NULL;
  250. order = -1;
  251. read_lock(&fib_hash_lock);
  252. hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
  253. struct fib_alias *fa;
  254. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  255. struct fib_info *next_fi = fa->fa_info;
  256. if (fa->fa_scope != res->scope ||
  257. fa->fa_type != RTN_UNICAST)
  258. continue;
  259. if (next_fi->fib_priority > res->fi->fib_priority)
  260. break;
  261. if (!next_fi->fib_nh[0].nh_gw ||
  262. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  263. continue;
  264. fa->fa_state |= FA_S_ACCESSED;
  265. if (fi == NULL) {
  266. if (next_fi != res->fi)
  267. break;
  268. } else if (!fib_detect_death(fi, order, &last_resort,
  269. &last_idx, tb->tb_default)) {
  270. fib_result_assign(res, fi);
  271. tb->tb_default = order;
  272. goto out;
  273. }
  274. fi = next_fi;
  275. order++;
  276. }
  277. }
  278. if (order <= 0 || fi == NULL) {
  279. tb->tb_default = -1;
  280. goto out;
  281. }
  282. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  283. tb->tb_default)) {
  284. fib_result_assign(res, fi);
  285. tb->tb_default = order;
  286. goto out;
  287. }
  288. if (last_idx >= 0)
  289. fib_result_assign(res, last_resort);
  290. tb->tb_default = last_idx;
  291. out:
  292. read_unlock(&fib_hash_lock);
  293. }
  294. /* Insert node F to FZ. */
  295. static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
  296. {
  297. struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  298. hlist_add_head(&f->fn_hash, head);
  299. }
  300. /* Return the node in FZ matching KEY. */
  301. static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
  302. {
  303. struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
  304. struct hlist_node *node;
  305. struct fib_node *f;
  306. hlist_for_each_entry(f, node, head, fn_hash) {
  307. if (f->fn_key == key)
  308. return f;
  309. }
  310. return NULL;
  311. }
  312. int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
  313. {
  314. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  315. struct fib_node *new_f = NULL;
  316. struct fib_node *f;
  317. struct fib_alias *fa, *new_fa;
  318. struct fn_zone *fz;
  319. struct fib_info *fi;
  320. u8 tos = cfg->fc_tos;
  321. __be32 key;
  322. int err;
  323. if (cfg->fc_dst_len > 32)
  324. return -EINVAL;
  325. fz = table->fn_zones[cfg->fc_dst_len];
  326. if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
  327. return -ENOBUFS;
  328. key = 0;
  329. if (cfg->fc_dst) {
  330. if (cfg->fc_dst & ~FZ_MASK(fz))
  331. return -EINVAL;
  332. key = fz_key(cfg->fc_dst, fz);
  333. }
  334. fi = fib_create_info(cfg);
  335. if (IS_ERR(fi))
  336. return PTR_ERR(fi);
  337. if (fz->fz_nent > (fz->fz_divisor<<1) &&
  338. fz->fz_divisor < FZ_MAX_DIVISOR &&
  339. (cfg->fc_dst_len == 32 ||
  340. (1 << cfg->fc_dst_len) > fz->fz_divisor))
  341. fn_rehash_zone(fz);
  342. f = fib_find_node(fz, key);
  343. if (!f)
  344. fa = NULL;
  345. else
  346. fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
  347. /* Now fa, if non-NULL, points to the first fib alias
  348. * with the same keys [prefix,tos,priority], if such key already
  349. * exists or to the node before which we will insert new one.
  350. *
  351. * If fa is NULL, we will need to allocate a new one and
  352. * insert to the head of f.
  353. *
  354. * If f is NULL, no fib node matched the destination key
  355. * and we need to allocate a new one of those as well.
  356. */
  357. if (fa && fa->fa_tos == tos &&
  358. fa->fa_info->fib_priority == fi->fib_priority) {
  359. struct fib_alias *fa_first, *fa_match;
  360. err = -EEXIST;
  361. if (cfg->fc_nlflags & NLM_F_EXCL)
  362. goto out;
  363. /* We have 2 goals:
  364. * 1. Find exact match for type, scope, fib_info to avoid
  365. * duplicate routes
  366. * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
  367. */
  368. fa_match = NULL;
  369. fa_first = fa;
  370. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  371. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  372. if (fa->fa_tos != tos)
  373. break;
  374. if (fa->fa_info->fib_priority != fi->fib_priority)
  375. break;
  376. if (fa->fa_type == cfg->fc_type &&
  377. fa->fa_scope == cfg->fc_scope &&
  378. fa->fa_info == fi) {
  379. fa_match = fa;
  380. break;
  381. }
  382. }
  383. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  384. struct fib_info *fi_drop;
  385. u8 state;
  386. fa = fa_first;
  387. if (fa_match) {
  388. if (fa == fa_match)
  389. err = 0;
  390. goto out;
  391. }
  392. write_lock_bh(&fib_hash_lock);
  393. fi_drop = fa->fa_info;
  394. fa->fa_info = fi;
  395. fa->fa_type = cfg->fc_type;
  396. fa->fa_scope = cfg->fc_scope;
  397. state = fa->fa_state;
  398. fa->fa_state &= ~FA_S_ACCESSED;
  399. fib_hash_genid++;
  400. write_unlock_bh(&fib_hash_lock);
  401. fib_release_info(fi_drop);
  402. if (state & FA_S_ACCESSED)
  403. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  404. rtmsg_fib(RTM_NEWROUTE, key, fa, cfg->fc_dst_len, tb->tb_id,
  405. &cfg->fc_nlinfo, NLM_F_REPLACE);
  406. return 0;
  407. }
  408. /* Error if we find a perfect match which
  409. * uses the same scope, type, and nexthop
  410. * information.
  411. */
  412. if (fa_match)
  413. goto out;
  414. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  415. fa = fa_first;
  416. }
  417. err = -ENOENT;
  418. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  419. goto out;
  420. err = -ENOBUFS;
  421. if (!f) {
  422. new_f = kmem_cache_zalloc(fn_hash_kmem, GFP_KERNEL);
  423. if (new_f == NULL)
  424. goto out;
  425. INIT_HLIST_NODE(&new_f->fn_hash);
  426. INIT_LIST_HEAD(&new_f->fn_alias);
  427. new_f->fn_key = key;
  428. f = new_f;
  429. }
  430. new_fa = &f->fn_embedded_alias;
  431. if (new_fa->fa_info != NULL) {
  432. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  433. if (new_fa == NULL)
  434. goto out;
  435. }
  436. new_fa->fa_info = fi;
  437. new_fa->fa_tos = tos;
  438. new_fa->fa_type = cfg->fc_type;
  439. new_fa->fa_scope = cfg->fc_scope;
  440. new_fa->fa_state = 0;
  441. /*
  442. * Insert new entry to the list.
  443. */
  444. write_lock_bh(&fib_hash_lock);
  445. if (new_f)
  446. fib_insert_node(fz, new_f);
  447. list_add_tail(&new_fa->fa_list,
  448. (fa ? &fa->fa_list : &f->fn_alias));
  449. fib_hash_genid++;
  450. write_unlock_bh(&fib_hash_lock);
  451. if (new_f)
  452. fz->fz_nent++;
  453. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  454. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
  455. &cfg->fc_nlinfo, 0);
  456. return 0;
  457. out:
  458. if (new_f)
  459. kmem_cache_free(fn_hash_kmem, new_f);
  460. fib_release_info(fi);
  461. return err;
  462. }
  463. int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
  464. {
  465. struct fn_hash *table = (struct fn_hash *)tb->tb_data;
  466. struct fib_node *f;
  467. struct fib_alias *fa, *fa_to_delete;
  468. struct fn_zone *fz;
  469. __be32 key;
  470. if (cfg->fc_dst_len > 32)
  471. return -EINVAL;
  472. if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
  473. return -ESRCH;
  474. key = 0;
  475. if (cfg->fc_dst) {
  476. if (cfg->fc_dst & ~FZ_MASK(fz))
  477. return -EINVAL;
  478. key = fz_key(cfg->fc_dst, fz);
  479. }
  480. f = fib_find_node(fz, key);
  481. if (!f)
  482. fa = NULL;
  483. else
  484. fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
  485. if (!fa)
  486. return -ESRCH;
  487. fa_to_delete = NULL;
  488. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  489. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  490. struct fib_info *fi = fa->fa_info;
  491. if (fa->fa_tos != cfg->fc_tos)
  492. break;
  493. if ((!cfg->fc_type ||
  494. fa->fa_type == cfg->fc_type) &&
  495. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  496. fa->fa_scope == cfg->fc_scope) &&
  497. (!cfg->fc_protocol ||
  498. fi->fib_protocol == cfg->fc_protocol) &&
  499. fib_nh_match(cfg, fi) == 0) {
  500. fa_to_delete = fa;
  501. break;
  502. }
  503. }
  504. if (fa_to_delete) {
  505. int kill_fn;
  506. fa = fa_to_delete;
  507. rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
  508. tb->tb_id, &cfg->fc_nlinfo, 0);
  509. kill_fn = 0;
  510. write_lock_bh(&fib_hash_lock);
  511. list_del(&fa->fa_list);
  512. if (list_empty(&f->fn_alias)) {
  513. hlist_del(&f->fn_hash);
  514. kill_fn = 1;
  515. }
  516. fib_hash_genid++;
  517. write_unlock_bh(&fib_hash_lock);
  518. if (fa->fa_state & FA_S_ACCESSED)
  519. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  520. fn_free_alias(fa, f);
  521. if (kill_fn) {
  522. fn_free_node(f);
  523. fz->fz_nent--;
  524. }
  525. return 0;
  526. }
  527. return -ESRCH;
  528. }
  529. static int fn_flush_list(struct fn_zone *fz, int idx)
  530. {
  531. struct hlist_head *head = &fz->fz_hash[idx];
  532. struct hlist_node *node, *n;
  533. struct fib_node *f;
  534. int found = 0;
  535. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  536. struct fib_alias *fa, *fa_node;
  537. int kill_f;
  538. kill_f = 0;
  539. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  540. struct fib_info *fi = fa->fa_info;
  541. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  542. write_lock_bh(&fib_hash_lock);
  543. list_del(&fa->fa_list);
  544. if (list_empty(&f->fn_alias)) {
  545. hlist_del(&f->fn_hash);
  546. kill_f = 1;
  547. }
  548. fib_hash_genid++;
  549. write_unlock_bh(&fib_hash_lock);
  550. fn_free_alias(fa, f);
  551. found++;
  552. }
  553. }
  554. if (kill_f) {
  555. fn_free_node(f);
  556. fz->fz_nent--;
  557. }
  558. }
  559. return found;
  560. }
  561. int fib_table_flush(struct fib_table *tb)
  562. {
  563. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  564. struct fn_zone *fz;
  565. int found = 0;
  566. for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
  567. int i;
  568. for (i = fz->fz_divisor - 1; i >= 0; i--)
  569. found += fn_flush_list(fz, i);
  570. }
  571. return found;
  572. }
  573. static inline int
  574. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  575. struct fib_table *tb,
  576. struct fn_zone *fz,
  577. struct hlist_head *head)
  578. {
  579. struct hlist_node *node;
  580. struct fib_node *f;
  581. int i, s_i;
  582. s_i = cb->args[4];
  583. i = 0;
  584. hlist_for_each_entry(f, node, head, fn_hash) {
  585. struct fib_alias *fa;
  586. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  587. if (i < s_i)
  588. goto next;
  589. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  590. cb->nlh->nlmsg_seq,
  591. RTM_NEWROUTE,
  592. tb->tb_id,
  593. fa->fa_type,
  594. fa->fa_scope,
  595. f->fn_key,
  596. fz->fz_order,
  597. fa->fa_tos,
  598. fa->fa_info,
  599. NLM_F_MULTI) < 0) {
  600. cb->args[4] = i;
  601. return -1;
  602. }
  603. next:
  604. i++;
  605. }
  606. }
  607. cb->args[4] = i;
  608. return skb->len;
  609. }
  610. static inline int
  611. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  612. struct fib_table *tb,
  613. struct fn_zone *fz)
  614. {
  615. int h, s_h;
  616. if (fz->fz_hash == NULL)
  617. return skb->len;
  618. s_h = cb->args[3];
  619. for (h = s_h; h < fz->fz_divisor; h++) {
  620. if (hlist_empty(&fz->fz_hash[h]))
  621. continue;
  622. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h]) < 0) {
  623. cb->args[3] = h;
  624. return -1;
  625. }
  626. memset(&cb->args[4], 0,
  627. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  628. }
  629. cb->args[3] = h;
  630. return skb->len;
  631. }
  632. int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
  633. 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. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  671. return tb;
  672. }
  673. /* ------------------------------------------------------------------------ */
  674. #ifdef CONFIG_PROC_FS
  675. struct fib_iter_state {
  676. struct seq_net_private p;
  677. struct fn_zone *zone;
  678. int bucket;
  679. struct hlist_head *hash_head;
  680. struct fib_node *fn;
  681. struct fib_alias *fa;
  682. loff_t pos;
  683. unsigned int genid;
  684. int valid;
  685. };
  686. static struct fib_alias *fib_get_first(struct seq_file *seq)
  687. {
  688. struct fib_iter_state *iter = seq->private;
  689. struct fib_table *main_table;
  690. struct fn_hash *table;
  691. main_table = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
  692. table = (struct fn_hash *)main_table->tb_data;
  693. iter->bucket = 0;
  694. iter->hash_head = NULL;
  695. iter->fn = NULL;
  696. iter->fa = NULL;
  697. iter->pos = 0;
  698. iter->genid = fib_hash_genid;
  699. iter->valid = 1;
  700. for (iter->zone = table->fn_zone_list; iter->zone;
  701. iter->zone = iter->zone->fz_next) {
  702. int maxslot;
  703. if (!iter->zone->fz_nent)
  704. continue;
  705. iter->hash_head = iter->zone->fz_hash;
  706. maxslot = iter->zone->fz_divisor;
  707. for (iter->bucket = 0; iter->bucket < maxslot;
  708. ++iter->bucket, ++iter->hash_head) {
  709. struct hlist_node *node;
  710. struct fib_node *fn;
  711. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  712. struct fib_alias *fa;
  713. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  714. iter->fn = fn;
  715. iter->fa = fa;
  716. goto out;
  717. }
  718. }
  719. }
  720. }
  721. out:
  722. return iter->fa;
  723. }
  724. static struct fib_alias *fib_get_next(struct seq_file *seq)
  725. {
  726. struct fib_iter_state *iter = seq->private;
  727. struct fib_node *fn;
  728. struct fib_alias *fa;
  729. /* Advance FA, if any. */
  730. fn = iter->fn;
  731. fa = iter->fa;
  732. if (fa) {
  733. BUG_ON(!fn);
  734. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  735. iter->fa = fa;
  736. goto out;
  737. }
  738. }
  739. fa = iter->fa = NULL;
  740. /* Advance FN. */
  741. if (fn) {
  742. struct hlist_node *node = &fn->fn_hash;
  743. hlist_for_each_entry_continue(fn, node, fn_hash) {
  744. iter->fn = fn;
  745. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  746. iter->fa = fa;
  747. goto out;
  748. }
  749. }
  750. }
  751. fn = iter->fn = NULL;
  752. /* Advance hash chain. */
  753. if (!iter->zone)
  754. goto out;
  755. for (;;) {
  756. struct hlist_node *node;
  757. int maxslot;
  758. maxslot = iter->zone->fz_divisor;
  759. while (++iter->bucket < maxslot) {
  760. iter->hash_head++;
  761. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  762. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  763. iter->fn = fn;
  764. iter->fa = fa;
  765. goto out;
  766. }
  767. }
  768. }
  769. iter->zone = iter->zone->fz_next;
  770. if (!iter->zone)
  771. goto out;
  772. iter->bucket = 0;
  773. iter->hash_head = iter->zone->fz_hash;
  774. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  775. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  776. iter->fn = fn;
  777. iter->fa = fa;
  778. goto out;
  779. }
  780. }
  781. }
  782. out:
  783. iter->pos++;
  784. return fa;
  785. }
  786. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  787. {
  788. struct fib_iter_state *iter = seq->private;
  789. struct fib_alias *fa;
  790. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  791. fa = iter->fa;
  792. pos -= iter->pos;
  793. } else
  794. fa = fib_get_first(seq);
  795. if (fa)
  796. while (pos && (fa = fib_get_next(seq)))
  797. --pos;
  798. return pos ? NULL : fa;
  799. }
  800. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  801. __acquires(fib_hash_lock)
  802. {
  803. void *v = NULL;
  804. read_lock(&fib_hash_lock);
  805. if (fib_get_table(seq_file_net(seq), RT_TABLE_MAIN))
  806. v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  807. return v;
  808. }
  809. static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  810. {
  811. ++*pos;
  812. return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
  813. }
  814. static void fib_seq_stop(struct seq_file *seq, void *v)
  815. __releases(fib_hash_lock)
  816. {
  817. read_unlock(&fib_hash_lock);
  818. }
  819. static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
  820. {
  821. static const unsigned type2flags[RTN_MAX + 1] = {
  822. [7] = RTF_REJECT, [8] = RTF_REJECT,
  823. };
  824. unsigned flags = type2flags[type];
  825. if (fi && fi->fib_nh->nh_gw)
  826. flags |= RTF_GATEWAY;
  827. if (mask == htonl(0xFFFFFFFF))
  828. flags |= RTF_HOST;
  829. flags |= RTF_UP;
  830. return flags;
  831. }
  832. /*
  833. * This outputs /proc/net/route.
  834. *
  835. * It always works in backward compatibility mode.
  836. * The format of the file is not supposed to be changed.
  837. */
  838. static int fib_seq_show(struct seq_file *seq, void *v)
  839. {
  840. struct fib_iter_state *iter;
  841. int len;
  842. __be32 prefix, mask;
  843. unsigned flags;
  844. struct fib_node *f;
  845. struct fib_alias *fa;
  846. struct fib_info *fi;
  847. if (v == SEQ_START_TOKEN) {
  848. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  849. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  850. "\tWindow\tIRTT");
  851. goto out;
  852. }
  853. iter = seq->private;
  854. f = iter->fn;
  855. fa = iter->fa;
  856. fi = fa->fa_info;
  857. prefix = f->fn_key;
  858. mask = FZ_MASK(iter->zone);
  859. flags = fib_flag_trans(fa->fa_type, mask, fi);
  860. if (fi)
  861. seq_printf(seq,
  862. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
  863. fi->fib_dev ? fi->fib_dev->name : "*", prefix,
  864. fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
  865. mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
  866. fi->fib_window,
  867. fi->fib_rtt >> 3, &len);
  868. else
  869. seq_printf(seq,
  870. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
  871. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0, &len);
  872. seq_printf(seq, "%*s\n", 127 - len, "");
  873. out:
  874. return 0;
  875. }
  876. static const struct seq_operations fib_seq_ops = {
  877. .start = fib_seq_start,
  878. .next = fib_seq_next,
  879. .stop = fib_seq_stop,
  880. .show = fib_seq_show,
  881. };
  882. static int fib_seq_open(struct inode *inode, struct file *file)
  883. {
  884. return seq_open_net(inode, file, &fib_seq_ops,
  885. sizeof(struct fib_iter_state));
  886. }
  887. static const struct file_operations fib_seq_fops = {
  888. .owner = THIS_MODULE,
  889. .open = fib_seq_open,
  890. .read = seq_read,
  891. .llseek = seq_lseek,
  892. .release = seq_release_net,
  893. };
  894. int __net_init fib_proc_init(struct net *net)
  895. {
  896. if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_seq_fops))
  897. return -ENOMEM;
  898. return 0;
  899. }
  900. void __net_exit fib_proc_exit(struct net *net)
  901. {
  902. proc_net_remove(net, "route");
  903. }
  904. #endif /* CONFIG_PROC_FS */