fib_hash.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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/ip.h>
  37. #include <net/protocol.h>
  38. #include <net/route.h>
  39. #include <net/tcp.h>
  40. #include <net/sock.h>
  41. #include <net/ip_fib.h>
  42. #include "fib_lookup.h"
  43. static struct kmem_cache *fn_hash_kmem __read_mostly;
  44. static struct kmem_cache *fn_alias_kmem __read_mostly;
  45. struct fib_node {
  46. struct hlist_node fn_hash;
  47. struct list_head fn_alias;
  48. __be32 fn_key;
  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 kmalloc(size, GFP_KERNEL);
  89. } else {
  90. return (struct hlist_head *)
  91. __get_free_pages(GFP_KERNEL, 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("fn_rehash_zone: hash for zone %d grows from %d\n", fz->fz_order, old_divisor);
  143. #endif
  144. ht = fz_hash_alloc(new_divisor);
  145. if (ht) {
  146. memset(ht, 0, new_divisor * sizeof(struct hlist_head));
  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. memset(fz->fz_hash, 0, fz->fz_divisor * sizeof(struct hlist_head *));
  186. fz->fz_order = z;
  187. fz->fz_mask = inet_make_mask(z);
  188. /* Find the first not empty zone with more specific mask */
  189. for (i=z+1; i<=32; i++)
  190. if (table->fn_zones[i])
  191. break;
  192. write_lock_bh(&fib_hash_lock);
  193. if (i>32) {
  194. /* No more specific masks, we are the first. */
  195. fz->fz_next = table->fn_zone_list;
  196. table->fn_zone_list = fz;
  197. } else {
  198. fz->fz_next = table->fn_zones[i]->fz_next;
  199. table->fn_zones[i]->fz_next = fz;
  200. }
  201. table->fn_zones[z] = fz;
  202. fib_hash_genid++;
  203. write_unlock_bh(&fib_hash_lock);
  204. return fz;
  205. }
  206. static int
  207. fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
  208. {
  209. int err;
  210. struct fn_zone *fz;
  211. struct fn_hash *t = (struct fn_hash*)tb->tb_data;
  212. read_lock(&fib_hash_lock);
  213. for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
  214. struct hlist_head *head;
  215. struct hlist_node *node;
  216. struct fib_node *f;
  217. __be32 k = fz_key(flp->fl4_dst, fz);
  218. head = &fz->fz_hash[fn_hash(k, fz)];
  219. hlist_for_each_entry(f, node, head, fn_hash) {
  220. if (f->fn_key != k)
  221. continue;
  222. err = fib_semantic_match(&f->fn_alias,
  223. flp, res,
  224. f->fn_key, fz->fz_mask,
  225. fz->fz_order);
  226. if (err <= 0)
  227. goto out;
  228. }
  229. }
  230. err = 1;
  231. out:
  232. read_unlock(&fib_hash_lock);
  233. return err;
  234. }
  235. static int fn_hash_last_dflt=-1;
  236. static void
  237. fn_hash_select_default(struct fib_table *tb, 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, &fn_hash_last_dflt)) {
  270. if (res->fi)
  271. fib_info_put(res->fi);
  272. res->fi = fi;
  273. atomic_inc(&fi->fib_clntref);
  274. fn_hash_last_dflt = order;
  275. goto out;
  276. }
  277. fi = next_fi;
  278. order++;
  279. }
  280. }
  281. if (order <= 0 || fi == NULL) {
  282. fn_hash_last_dflt = -1;
  283. goto out;
  284. }
  285. if (!fib_detect_death(fi, order, &last_resort, &last_idx, &fn_hash_last_dflt)) {
  286. if (res->fi)
  287. fib_info_put(res->fi);
  288. res->fi = fi;
  289. atomic_inc(&fi->fib_clntref);
  290. fn_hash_last_dflt = order;
  291. goto out;
  292. }
  293. if (last_idx >= 0) {
  294. if (res->fi)
  295. fib_info_put(res->fi);
  296. res->fi = last_resort;
  297. if (last_resort)
  298. atomic_inc(&last_resort->fib_clntref);
  299. }
  300. fn_hash_last_dflt = last_idx;
  301. out:
  302. read_unlock(&fib_hash_lock);
  303. }
  304. /* Insert node F to FZ. */
  305. static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
  306. {
  307. struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  308. hlist_add_head(&f->fn_hash, head);
  309. }
  310. /* Return the node in FZ matching KEY. */
  311. static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
  312. {
  313. struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
  314. struct hlist_node *node;
  315. struct fib_node *f;
  316. hlist_for_each_entry(f, node, head, fn_hash) {
  317. if (f->fn_key == key)
  318. return f;
  319. }
  320. return NULL;
  321. }
  322. static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
  323. {
  324. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  325. struct fib_node *new_f, *f;
  326. struct fib_alias *fa, *new_fa;
  327. struct fn_zone *fz;
  328. struct fib_info *fi;
  329. u8 tos = cfg->fc_tos;
  330. __be32 key;
  331. int err;
  332. if (cfg->fc_dst_len > 32)
  333. return -EINVAL;
  334. fz = table->fn_zones[cfg->fc_dst_len];
  335. if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
  336. return -ENOBUFS;
  337. key = 0;
  338. if (cfg->fc_dst) {
  339. if (cfg->fc_dst & ~FZ_MASK(fz))
  340. return -EINVAL;
  341. key = fz_key(cfg->fc_dst, fz);
  342. }
  343. fi = fib_create_info(cfg);
  344. if (IS_ERR(fi))
  345. return PTR_ERR(fi);
  346. if (fz->fz_nent > (fz->fz_divisor<<1) &&
  347. fz->fz_divisor < FZ_MAX_DIVISOR &&
  348. (cfg->fc_dst_len == 32 ||
  349. (1 << cfg->fc_dst_len) > fz->fz_divisor))
  350. fn_rehash_zone(fz);
  351. f = fib_find_node(fz, key);
  352. if (!f)
  353. fa = NULL;
  354. else
  355. fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
  356. /* Now fa, if non-NULL, points to the first fib alias
  357. * with the same keys [prefix,tos,priority], if such key already
  358. * exists or to the node before which we will insert new one.
  359. *
  360. * If fa is NULL, we will need to allocate a new one and
  361. * insert to the head of f.
  362. *
  363. * If f is NULL, no fib node matched the destination key
  364. * and we need to allocate a new one of those as well.
  365. */
  366. if (fa && fa->fa_tos == tos &&
  367. fa->fa_info->fib_priority == fi->fib_priority) {
  368. struct fib_alias *fa_orig;
  369. err = -EEXIST;
  370. if (cfg->fc_nlflags & NLM_F_EXCL)
  371. goto out;
  372. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  373. struct fib_info *fi_drop;
  374. u8 state;
  375. write_lock_bh(&fib_hash_lock);
  376. fi_drop = fa->fa_info;
  377. fa->fa_info = fi;
  378. fa->fa_type = cfg->fc_type;
  379. fa->fa_scope = cfg->fc_scope;
  380. state = fa->fa_state;
  381. fa->fa_state &= ~FA_S_ACCESSED;
  382. fib_hash_genid++;
  383. write_unlock_bh(&fib_hash_lock);
  384. fib_release_info(fi_drop);
  385. if (state & FA_S_ACCESSED)
  386. rt_cache_flush(-1);
  387. return 0;
  388. }
  389. /* Error if we find a perfect match which
  390. * uses the same scope, type, and nexthop
  391. * information.
  392. */
  393. fa_orig = fa;
  394. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  395. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  396. if (fa->fa_tos != tos)
  397. break;
  398. if (fa->fa_info->fib_priority != fi->fib_priority)
  399. break;
  400. if (fa->fa_type == cfg->fc_type &&
  401. fa->fa_scope == cfg->fc_scope &&
  402. fa->fa_info == fi)
  403. goto out;
  404. }
  405. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  406. fa = fa_orig;
  407. }
  408. err = -ENOENT;
  409. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  410. goto out;
  411. err = -ENOBUFS;
  412. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  413. if (new_fa == NULL)
  414. goto out;
  415. new_f = NULL;
  416. if (!f) {
  417. new_f = kmem_cache_alloc(fn_hash_kmem, GFP_KERNEL);
  418. if (new_f == NULL)
  419. goto out_free_new_fa;
  420. INIT_HLIST_NODE(&new_f->fn_hash);
  421. INIT_LIST_HEAD(&new_f->fn_alias);
  422. new_f->fn_key = key;
  423. f = new_f;
  424. }
  425. new_fa->fa_info = fi;
  426. new_fa->fa_tos = tos;
  427. new_fa->fa_type = cfg->fc_type;
  428. new_fa->fa_scope = cfg->fc_scope;
  429. new_fa->fa_state = 0;
  430. /*
  431. * Insert new entry to the list.
  432. */
  433. write_lock_bh(&fib_hash_lock);
  434. if (new_f)
  435. fib_insert_node(fz, new_f);
  436. list_add_tail(&new_fa->fa_list,
  437. (fa ? &fa->fa_list : &f->fn_alias));
  438. fib_hash_genid++;
  439. write_unlock_bh(&fib_hash_lock);
  440. if (new_f)
  441. fz->fz_nent++;
  442. rt_cache_flush(-1);
  443. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
  444. &cfg->fc_nlinfo);
  445. return 0;
  446. out_free_new_fa:
  447. kmem_cache_free(fn_alias_kmem, new_fa);
  448. out:
  449. fib_release_info(fi);
  450. return err;
  451. }
  452. static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
  453. {
  454. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  455. struct fib_node *f;
  456. struct fib_alias *fa, *fa_to_delete;
  457. struct fn_zone *fz;
  458. __be32 key;
  459. if (cfg->fc_dst_len > 32)
  460. return -EINVAL;
  461. if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
  462. return -ESRCH;
  463. key = 0;
  464. if (cfg->fc_dst) {
  465. if (cfg->fc_dst & ~FZ_MASK(fz))
  466. return -EINVAL;
  467. key = fz_key(cfg->fc_dst, fz);
  468. }
  469. f = fib_find_node(fz, key);
  470. if (!f)
  471. fa = NULL;
  472. else
  473. fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
  474. if (!fa)
  475. return -ESRCH;
  476. fa_to_delete = NULL;
  477. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  478. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  479. struct fib_info *fi = fa->fa_info;
  480. if (fa->fa_tos != cfg->fc_tos)
  481. break;
  482. if ((!cfg->fc_type ||
  483. fa->fa_type == cfg->fc_type) &&
  484. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  485. fa->fa_scope == cfg->fc_scope) &&
  486. (!cfg->fc_protocol ||
  487. fi->fib_protocol == cfg->fc_protocol) &&
  488. fib_nh_match(cfg, fi) == 0) {
  489. fa_to_delete = fa;
  490. break;
  491. }
  492. }
  493. if (fa_to_delete) {
  494. int kill_fn;
  495. fa = fa_to_delete;
  496. rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
  497. tb->tb_id, &cfg->fc_nlinfo);
  498. kill_fn = 0;
  499. write_lock_bh(&fib_hash_lock);
  500. list_del(&fa->fa_list);
  501. if (list_empty(&f->fn_alias)) {
  502. hlist_del(&f->fn_hash);
  503. kill_fn = 1;
  504. }
  505. fib_hash_genid++;
  506. write_unlock_bh(&fib_hash_lock);
  507. if (fa->fa_state & FA_S_ACCESSED)
  508. rt_cache_flush(-1);
  509. fn_free_alias(fa);
  510. if (kill_fn) {
  511. fn_free_node(f);
  512. fz->fz_nent--;
  513. }
  514. return 0;
  515. }
  516. return -ESRCH;
  517. }
  518. static int fn_flush_list(struct fn_zone *fz, int idx)
  519. {
  520. struct hlist_head *head = &fz->fz_hash[idx];
  521. struct hlist_node *node, *n;
  522. struct fib_node *f;
  523. int found = 0;
  524. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  525. struct fib_alias *fa, *fa_node;
  526. int kill_f;
  527. kill_f = 0;
  528. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  529. struct fib_info *fi = fa->fa_info;
  530. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  531. write_lock_bh(&fib_hash_lock);
  532. list_del(&fa->fa_list);
  533. if (list_empty(&f->fn_alias)) {
  534. hlist_del(&f->fn_hash);
  535. kill_f = 1;
  536. }
  537. fib_hash_genid++;
  538. write_unlock_bh(&fib_hash_lock);
  539. fn_free_alias(fa);
  540. found++;
  541. }
  542. }
  543. if (kill_f) {
  544. fn_free_node(f);
  545. fz->fz_nent--;
  546. }
  547. }
  548. return found;
  549. }
  550. static int fn_hash_flush(struct fib_table *tb)
  551. {
  552. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  553. struct fn_zone *fz;
  554. int found = 0;
  555. for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
  556. int i;
  557. for (i = fz->fz_divisor - 1; i >= 0; i--)
  558. found += fn_flush_list(fz, i);
  559. }
  560. return found;
  561. }
  562. static inline int
  563. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  564. struct fib_table *tb,
  565. struct fn_zone *fz,
  566. struct hlist_head *head)
  567. {
  568. struct hlist_node *node;
  569. struct fib_node *f;
  570. int i, s_i;
  571. s_i = cb->args[4];
  572. i = 0;
  573. hlist_for_each_entry(f, node, head, fn_hash) {
  574. struct fib_alias *fa;
  575. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  576. if (i < s_i)
  577. goto next;
  578. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  579. cb->nlh->nlmsg_seq,
  580. RTM_NEWROUTE,
  581. tb->tb_id,
  582. fa->fa_type,
  583. fa->fa_scope,
  584. f->fn_key,
  585. fz->fz_order,
  586. fa->fa_tos,
  587. fa->fa_info,
  588. NLM_F_MULTI) < 0) {
  589. cb->args[4] = i;
  590. return -1;
  591. }
  592. next:
  593. i++;
  594. }
  595. }
  596. cb->args[4] = i;
  597. return skb->len;
  598. }
  599. static inline int
  600. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  601. struct fib_table *tb,
  602. struct fn_zone *fz)
  603. {
  604. int h, s_h;
  605. s_h = cb->args[3];
  606. for (h=0; h < fz->fz_divisor; h++) {
  607. if (h < s_h) continue;
  608. if (h > s_h)
  609. memset(&cb->args[4], 0,
  610. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  611. if (fz->fz_hash == NULL ||
  612. hlist_empty(&fz->fz_hash[h]))
  613. continue;
  614. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
  615. cb->args[3] = h;
  616. return -1;
  617. }
  618. }
  619. cb->args[3] = h;
  620. return skb->len;
  621. }
  622. static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
  623. {
  624. int m, s_m;
  625. struct fn_zone *fz;
  626. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  627. s_m = cb->args[2];
  628. read_lock(&fib_hash_lock);
  629. for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
  630. if (m < s_m) continue;
  631. if (m > s_m)
  632. memset(&cb->args[3], 0,
  633. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  634. if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  635. cb->args[2] = m;
  636. read_unlock(&fib_hash_lock);
  637. return -1;
  638. }
  639. }
  640. read_unlock(&fib_hash_lock);
  641. cb->args[2] = m;
  642. return skb->len;
  643. }
  644. #ifdef CONFIG_IP_MULTIPLE_TABLES
  645. struct fib_table * fib_hash_init(u32 id)
  646. #else
  647. struct fib_table * __init fib_hash_init(u32 id)
  648. #endif
  649. {
  650. struct fib_table *tb;
  651. if (fn_hash_kmem == NULL)
  652. fn_hash_kmem = kmem_cache_create("ip_fib_hash",
  653. sizeof(struct fib_node),
  654. 0, SLAB_HWCACHE_ALIGN,
  655. NULL, NULL);
  656. if (fn_alias_kmem == NULL)
  657. fn_alias_kmem = kmem_cache_create("ip_fib_alias",
  658. sizeof(struct fib_alias),
  659. 0, SLAB_HWCACHE_ALIGN,
  660. NULL, NULL);
  661. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
  662. GFP_KERNEL);
  663. if (tb == NULL)
  664. return NULL;
  665. tb->tb_id = id;
  666. tb->tb_lookup = fn_hash_lookup;
  667. tb->tb_insert = fn_hash_insert;
  668. tb->tb_delete = fn_hash_delete;
  669. tb->tb_flush = fn_hash_flush;
  670. tb->tb_select_default = fn_hash_select_default;
  671. tb->tb_dump = fn_hash_dump;
  672. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  673. return tb;
  674. }
  675. /* ------------------------------------------------------------------------ */
  676. #ifdef CONFIG_PROC_FS
  677. struct fib_iter_state {
  678. struct fn_zone *zone;
  679. int bucket;
  680. struct hlist_head *hash_head;
  681. struct fib_node *fn;
  682. struct fib_alias *fa;
  683. loff_t pos;
  684. unsigned int genid;
  685. int valid;
  686. };
  687. static struct fib_alias *fib_get_first(struct seq_file *seq)
  688. {
  689. struct fib_iter_state *iter = seq->private;
  690. struct fn_hash *table = (struct fn_hash *) ip_fib_main_table->tb_data;
  691. iter->bucket = 0;
  692. iter->hash_head = NULL;
  693. iter->fn = NULL;
  694. iter->fa = NULL;
  695. iter->pos = 0;
  696. iter->genid = fib_hash_genid;
  697. iter->valid = 1;
  698. for (iter->zone = table->fn_zone_list; iter->zone;
  699. iter->zone = iter->zone->fz_next) {
  700. int maxslot;
  701. if (!iter->zone->fz_nent)
  702. continue;
  703. iter->hash_head = iter->zone->fz_hash;
  704. maxslot = iter->zone->fz_divisor;
  705. for (iter->bucket = 0; iter->bucket < maxslot;
  706. ++iter->bucket, ++iter->hash_head) {
  707. struct hlist_node *node;
  708. struct fib_node *fn;
  709. hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
  710. struct fib_alias *fa;
  711. list_for_each_entry(fa,&fn->fn_alias,fa_list) {
  712. iter->fn = fn;
  713. iter->fa = fa;
  714. goto out;
  715. }
  716. }
  717. }
  718. }
  719. out:
  720. return iter->fa;
  721. }
  722. static struct fib_alias *fib_get_next(struct seq_file *seq)
  723. {
  724. struct fib_iter_state *iter = seq->private;
  725. struct fib_node *fn;
  726. struct fib_alias *fa;
  727. /* Advance FA, if any. */
  728. fn = iter->fn;
  729. fa = iter->fa;
  730. if (fa) {
  731. BUG_ON(!fn);
  732. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  733. iter->fa = fa;
  734. goto out;
  735. }
  736. }
  737. fa = iter->fa = NULL;
  738. /* Advance FN. */
  739. if (fn) {
  740. struct hlist_node *node = &fn->fn_hash;
  741. hlist_for_each_entry_continue(fn, node, fn_hash) {
  742. iter->fn = fn;
  743. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  744. iter->fa = fa;
  745. goto out;
  746. }
  747. }
  748. }
  749. fn = iter->fn = NULL;
  750. /* Advance hash chain. */
  751. if (!iter->zone)
  752. goto out;
  753. for (;;) {
  754. struct hlist_node *node;
  755. int maxslot;
  756. maxslot = iter->zone->fz_divisor;
  757. while (++iter->bucket < maxslot) {
  758. iter->hash_head++;
  759. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  760. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  761. iter->fn = fn;
  762. iter->fa = fa;
  763. goto out;
  764. }
  765. }
  766. }
  767. iter->zone = iter->zone->fz_next;
  768. if (!iter->zone)
  769. goto out;
  770. iter->bucket = 0;
  771. iter->hash_head = iter->zone->fz_hash;
  772. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  773. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  774. iter->fn = fn;
  775. iter->fa = fa;
  776. goto out;
  777. }
  778. }
  779. }
  780. out:
  781. iter->pos++;
  782. return fa;
  783. }
  784. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  785. {
  786. struct fib_iter_state *iter = seq->private;
  787. struct fib_alias *fa;
  788. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  789. fa = iter->fa;
  790. pos -= iter->pos;
  791. } else
  792. fa = fib_get_first(seq);
  793. if (fa)
  794. while (pos && (fa = fib_get_next(seq)))
  795. --pos;
  796. return pos ? NULL : fa;
  797. }
  798. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  799. {
  800. void *v = NULL;
  801. read_lock(&fib_hash_lock);
  802. if (ip_fib_main_table)
  803. v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  804. return v;
  805. }
  806. static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  807. {
  808. ++*pos;
  809. return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
  810. }
  811. static void fib_seq_stop(struct seq_file *seq, void *v)
  812. {
  813. read_unlock(&fib_hash_lock);
  814. }
  815. static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
  816. {
  817. static const unsigned type2flags[RTN_MAX + 1] = {
  818. [7] = RTF_REJECT, [8] = RTF_REJECT,
  819. };
  820. unsigned flags = type2flags[type];
  821. if (fi && fi->fib_nh->nh_gw)
  822. flags |= RTF_GATEWAY;
  823. if (mask == htonl(0xFFFFFFFF))
  824. flags |= RTF_HOST;
  825. flags |= RTF_UP;
  826. return flags;
  827. }
  828. /*
  829. * This outputs /proc/net/route.
  830. *
  831. * It always works in backward compatibility mode.
  832. * The format of the file is not supposed to be changed.
  833. */
  834. static int fib_seq_show(struct seq_file *seq, void *v)
  835. {
  836. struct fib_iter_state *iter;
  837. char bf[128];
  838. __be32 prefix, mask;
  839. unsigned flags;
  840. struct fib_node *f;
  841. struct fib_alias *fa;
  842. struct fib_info *fi;
  843. if (v == SEQ_START_TOKEN) {
  844. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  845. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  846. "\tWindow\tIRTT");
  847. goto out;
  848. }
  849. iter = seq->private;
  850. f = iter->fn;
  851. fa = iter->fa;
  852. fi = fa->fa_info;
  853. prefix = f->fn_key;
  854. mask = FZ_MASK(iter->zone);
  855. flags = fib_flag_trans(fa->fa_type, mask, fi);
  856. if (fi)
  857. snprintf(bf, sizeof(bf),
  858. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  859. fi->fib_dev ? fi->fib_dev->name : "*", prefix,
  860. fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
  861. mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
  862. fi->fib_window,
  863. fi->fib_rtt >> 3);
  864. else
  865. snprintf(bf, sizeof(bf),
  866. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  867. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
  868. seq_printf(seq, "%-127s\n", bf);
  869. out:
  870. return 0;
  871. }
  872. static const struct seq_operations fib_seq_ops = {
  873. .start = fib_seq_start,
  874. .next = fib_seq_next,
  875. .stop = fib_seq_stop,
  876. .show = fib_seq_show,
  877. };
  878. static int fib_seq_open(struct inode *inode, struct file *file)
  879. {
  880. struct seq_file *seq;
  881. int rc = -ENOMEM;
  882. struct fib_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  883. if (!s)
  884. goto out;
  885. rc = seq_open(file, &fib_seq_ops);
  886. if (rc)
  887. goto out_kfree;
  888. seq = file->private_data;
  889. seq->private = s;
  890. out:
  891. return rc;
  892. out_kfree:
  893. kfree(s);
  894. goto out;
  895. }
  896. static const struct file_operations fib_seq_fops = {
  897. .owner = THIS_MODULE,
  898. .open = fib_seq_open,
  899. .read = seq_read,
  900. .llseek = seq_lseek,
  901. .release = seq_release_private,
  902. };
  903. int __init fib_proc_init(void)
  904. {
  905. if (!proc_net_fops_create("route", S_IRUGO, &fib_seq_fops))
  906. return -ENOMEM;
  907. return 0;
  908. }
  909. void __init fib_proc_exit(void)
  910. {
  911. proc_net_remove("route");
  912. }
  913. #endif /* CONFIG_PROC_FS */