fib_hash.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 <linux/config.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/system.h>
  20. #include <linux/bitops.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/mm.h>
  25. #include <linux/string.h>
  26. #include <linux/socket.h>
  27. #include <linux/sockios.h>
  28. #include <linux/errno.h>
  29. #include <linux/in.h>
  30. #include <linux/inet.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/netlink.h>
  36. #include <linux/init.h>
  37. #include <net/ip.h>
  38. #include <net/protocol.h>
  39. #include <net/route.h>
  40. #include <net/tcp.h>
  41. #include <net/sock.h>
  42. #include <net/ip_fib.h>
  43. #include "fib_lookup.h"
  44. static kmem_cache_t *fn_hash_kmem;
  45. static kmem_cache_t *fn_alias_kmem;
  46. struct fib_node {
  47. struct hlist_node fn_hash;
  48. struct list_head fn_alias;
  49. u32 fn_key;
  50. };
  51. struct fn_zone {
  52. struct fn_zone *fz_next; /* Next not empty zone */
  53. struct hlist_head *fz_hash; /* Hash table pointer */
  54. int fz_nent; /* Number of entries */
  55. int fz_divisor; /* Hash divisor */
  56. u32 fz_hashmask; /* (fz_divisor - 1) */
  57. #define FZ_HASHMASK(fz) ((fz)->fz_hashmask)
  58. int fz_order; /* Zone order */
  59. u32 fz_mask;
  60. #define FZ_MASK(fz) ((fz)->fz_mask)
  61. };
  62. /* NOTE. On fast computers evaluation of fz_hashmask and fz_mask
  63. * can be cheaper than memory lookup, so that FZ_* macros are used.
  64. */
  65. struct fn_hash {
  66. struct fn_zone *fn_zones[33];
  67. struct fn_zone *fn_zone_list;
  68. };
  69. static inline u32 fn_hash(u32 key, struct fn_zone *fz)
  70. {
  71. u32 h = ntohl(key)>>(32 - fz->fz_order);
  72. h ^= (h>>20);
  73. h ^= (h>>10);
  74. h ^= (h>>5);
  75. h &= FZ_HASHMASK(fz);
  76. return h;
  77. }
  78. static inline u32 fz_key(u32 dst, struct fn_zone *fz)
  79. {
  80. return dst & FZ_MASK(fz);
  81. }
  82. static DEFINE_RWLOCK(fib_hash_lock);
  83. static unsigned int fib_hash_genid;
  84. #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
  85. static struct hlist_head *fz_hash_alloc(int divisor)
  86. {
  87. unsigned long size = divisor * sizeof(struct hlist_head);
  88. if (size <= PAGE_SIZE) {
  89. return kmalloc(size, GFP_KERNEL);
  90. } else {
  91. return (struct hlist_head *)
  92. __get_free_pages(GFP_KERNEL, get_order(size));
  93. }
  94. }
  95. /* The fib hash lock must be held when this is called. */
  96. static inline void fn_rebuild_zone(struct fn_zone *fz,
  97. struct hlist_head *old_ht,
  98. int old_divisor)
  99. {
  100. int i;
  101. for (i = 0; i < old_divisor; i++) {
  102. struct hlist_node *node, *n;
  103. struct fib_node *f;
  104. hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
  105. struct hlist_head *new_head;
  106. hlist_del(&f->fn_hash);
  107. new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  108. hlist_add_head(&f->fn_hash, new_head);
  109. }
  110. }
  111. }
  112. static void fz_hash_free(struct hlist_head *hash, int divisor)
  113. {
  114. unsigned long size = divisor * sizeof(struct hlist_head);
  115. if (size <= PAGE_SIZE)
  116. kfree(hash);
  117. else
  118. free_pages((unsigned long)hash, get_order(size));
  119. }
  120. static void fn_rehash_zone(struct fn_zone *fz)
  121. {
  122. struct hlist_head *ht, *old_ht;
  123. int old_divisor, new_divisor;
  124. u32 new_hashmask;
  125. old_divisor = fz->fz_divisor;
  126. switch (old_divisor) {
  127. case 16:
  128. new_divisor = 256;
  129. break;
  130. case 256:
  131. new_divisor = 1024;
  132. break;
  133. default:
  134. if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
  135. printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
  136. return;
  137. }
  138. new_divisor = (old_divisor << 1);
  139. break;
  140. }
  141. new_hashmask = (new_divisor - 1);
  142. #if RT_CACHE_DEBUG >= 2
  143. printk("fn_rehash_zone: hash for zone %d grows from %d\n", fz->fz_order, old_divisor);
  144. #endif
  145. ht = fz_hash_alloc(new_divisor);
  146. if (ht) {
  147. memset(ht, 0, new_divisor * sizeof(struct hlist_head));
  148. write_lock_bh(&fib_hash_lock);
  149. old_ht = fz->fz_hash;
  150. fz->fz_hash = ht;
  151. fz->fz_hashmask = new_hashmask;
  152. fz->fz_divisor = new_divisor;
  153. fn_rebuild_zone(fz, old_ht, old_divisor);
  154. fib_hash_genid++;
  155. write_unlock_bh(&fib_hash_lock);
  156. fz_hash_free(old_ht, old_divisor);
  157. }
  158. }
  159. static inline void fn_free_node(struct fib_node * f)
  160. {
  161. kmem_cache_free(fn_hash_kmem, f);
  162. }
  163. static inline void fn_free_alias(struct fib_alias *fa)
  164. {
  165. fib_release_info(fa->fa_info);
  166. kmem_cache_free(fn_alias_kmem, fa);
  167. }
  168. static struct fn_zone *
  169. fn_new_zone(struct fn_hash *table, int z)
  170. {
  171. int i;
  172. struct fn_zone *fz = kmalloc(sizeof(struct fn_zone), GFP_KERNEL);
  173. if (!fz)
  174. return NULL;
  175. memset(fz, 0, sizeof(struct fn_zone));
  176. if (z) {
  177. fz->fz_divisor = 16;
  178. } else {
  179. fz->fz_divisor = 1;
  180. }
  181. fz->fz_hashmask = (fz->fz_divisor - 1);
  182. fz->fz_hash = fz_hash_alloc(fz->fz_divisor);
  183. if (!fz->fz_hash) {
  184. kfree(fz);
  185. return NULL;
  186. }
  187. memset(fz->fz_hash, 0, fz->fz_divisor * sizeof(struct hlist_head *));
  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. u32 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 int fn_hash_last_dflt=-1;
  238. static void
  239. fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
  240. {
  241. int order, last_idx;
  242. struct hlist_node *node;
  243. struct fib_node *f;
  244. struct fib_info *fi = NULL;
  245. struct fib_info *last_resort;
  246. struct fn_hash *t = (struct fn_hash*)tb->tb_data;
  247. struct fn_zone *fz = t->fn_zones[0];
  248. if (fz == NULL)
  249. return;
  250. last_idx = -1;
  251. last_resort = NULL;
  252. order = -1;
  253. read_lock(&fib_hash_lock);
  254. hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
  255. struct fib_alias *fa;
  256. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  257. struct fib_info *next_fi = fa->fa_info;
  258. if (fa->fa_scope != res->scope ||
  259. fa->fa_type != RTN_UNICAST)
  260. continue;
  261. if (next_fi->fib_priority > res->fi->fib_priority)
  262. break;
  263. if (!next_fi->fib_nh[0].nh_gw ||
  264. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  265. continue;
  266. fa->fa_state |= FA_S_ACCESSED;
  267. if (fi == NULL) {
  268. if (next_fi != res->fi)
  269. break;
  270. } else if (!fib_detect_death(fi, order, &last_resort,
  271. &last_idx, &fn_hash_last_dflt)) {
  272. if (res->fi)
  273. fib_info_put(res->fi);
  274. res->fi = fi;
  275. atomic_inc(&fi->fib_clntref);
  276. fn_hash_last_dflt = order;
  277. goto out;
  278. }
  279. fi = next_fi;
  280. order++;
  281. }
  282. }
  283. if (order <= 0 || fi == NULL) {
  284. fn_hash_last_dflt = -1;
  285. goto out;
  286. }
  287. if (!fib_detect_death(fi, order, &last_resort, &last_idx, &fn_hash_last_dflt)) {
  288. if (res->fi)
  289. fib_info_put(res->fi);
  290. res->fi = fi;
  291. atomic_inc(&fi->fib_clntref);
  292. fn_hash_last_dflt = order;
  293. goto out;
  294. }
  295. if (last_idx >= 0) {
  296. if (res->fi)
  297. fib_info_put(res->fi);
  298. res->fi = last_resort;
  299. if (last_resort)
  300. atomic_inc(&last_resort->fib_clntref);
  301. }
  302. fn_hash_last_dflt = last_idx;
  303. out:
  304. read_unlock(&fib_hash_lock);
  305. }
  306. /* Insert node F to FZ. */
  307. static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
  308. {
  309. struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  310. hlist_add_head(&f->fn_hash, head);
  311. }
  312. /* Return the node in FZ matching KEY. */
  313. static struct fib_node *fib_find_node(struct fn_zone *fz, u32 key)
  314. {
  315. struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
  316. struct hlist_node *node;
  317. struct fib_node *f;
  318. hlist_for_each_entry(f, node, head, fn_hash) {
  319. if (f->fn_key == key)
  320. return f;
  321. }
  322. return NULL;
  323. }
  324. static int
  325. fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
  326. struct nlmsghdr *n, struct netlink_skb_parms *req)
  327. {
  328. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  329. struct fib_node *new_f, *f;
  330. struct fib_alias *fa, *new_fa;
  331. struct fn_zone *fz;
  332. struct fib_info *fi;
  333. int z = r->rtm_dst_len;
  334. int type = r->rtm_type;
  335. u8 tos = r->rtm_tos;
  336. u32 key;
  337. int err;
  338. if (z > 32)
  339. return -EINVAL;
  340. fz = table->fn_zones[z];
  341. if (!fz && !(fz = fn_new_zone(table, z)))
  342. return -ENOBUFS;
  343. key = 0;
  344. if (rta->rta_dst) {
  345. u32 dst;
  346. memcpy(&dst, rta->rta_dst, 4);
  347. if (dst & ~FZ_MASK(fz))
  348. return -EINVAL;
  349. key = fz_key(dst, fz);
  350. }
  351. if ((fi = fib_create_info(r, rta, n, &err)) == NULL)
  352. return err;
  353. if (fz->fz_nent > (fz->fz_divisor<<1) &&
  354. fz->fz_divisor < FZ_MAX_DIVISOR &&
  355. (z==32 || (1<<z) > fz->fz_divisor))
  356. fn_rehash_zone(fz);
  357. f = fib_find_node(fz, key);
  358. if (!f)
  359. fa = NULL;
  360. else
  361. fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
  362. /* Now fa, if non-NULL, points to the first fib alias
  363. * with the same keys [prefix,tos,priority], if such key already
  364. * exists or to the node before which we will insert new one.
  365. *
  366. * If fa is NULL, we will need to allocate a new one and
  367. * insert to the head of f.
  368. *
  369. * If f is NULL, no fib node matched the destination key
  370. * and we need to allocate a new one of those as well.
  371. */
  372. if (fa && fa->fa_tos == tos &&
  373. fa->fa_info->fib_priority == fi->fib_priority) {
  374. struct fib_alias *fa_orig;
  375. err = -EEXIST;
  376. if (n->nlmsg_flags & NLM_F_EXCL)
  377. goto out;
  378. if (n->nlmsg_flags & NLM_F_REPLACE) {
  379. struct fib_info *fi_drop;
  380. u8 state;
  381. write_lock_bh(&fib_hash_lock);
  382. fi_drop = fa->fa_info;
  383. fa->fa_info = fi;
  384. fa->fa_type = type;
  385. fa->fa_scope = r->rtm_scope;
  386. state = fa->fa_state;
  387. fa->fa_state &= ~FA_S_ACCESSED;
  388. fib_hash_genid++;
  389. write_unlock_bh(&fib_hash_lock);
  390. fib_release_info(fi_drop);
  391. if (state & FA_S_ACCESSED)
  392. rt_cache_flush(-1);
  393. return 0;
  394. }
  395. /* Error if we find a perfect match which
  396. * uses the same scope, type, and nexthop
  397. * information.
  398. */
  399. fa_orig = fa;
  400. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  401. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  402. if (fa->fa_tos != tos)
  403. break;
  404. if (fa->fa_info->fib_priority != fi->fib_priority)
  405. break;
  406. if (fa->fa_type == type &&
  407. fa->fa_scope == r->rtm_scope &&
  408. fa->fa_info == fi)
  409. goto out;
  410. }
  411. if (!(n->nlmsg_flags & NLM_F_APPEND))
  412. fa = fa_orig;
  413. }
  414. err = -ENOENT;
  415. if (!(n->nlmsg_flags&NLM_F_CREATE))
  416. goto out;
  417. err = -ENOBUFS;
  418. new_fa = kmem_cache_alloc(fn_alias_kmem, SLAB_KERNEL);
  419. if (new_fa == NULL)
  420. goto out;
  421. new_f = NULL;
  422. if (!f) {
  423. new_f = kmem_cache_alloc(fn_hash_kmem, SLAB_KERNEL);
  424. if (new_f == NULL)
  425. goto out_free_new_fa;
  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->fa_info = fi;
  432. new_fa->fa_tos = tos;
  433. new_fa->fa_type = type;
  434. new_fa->fa_scope = r->rtm_scope;
  435. new_fa->fa_state = 0;
  436. /*
  437. * Insert new entry to the list.
  438. */
  439. write_lock_bh(&fib_hash_lock);
  440. if (new_f)
  441. fib_insert_node(fz, new_f);
  442. list_add_tail(&new_fa->fa_list,
  443. (fa ? &fa->fa_list : &f->fn_alias));
  444. fib_hash_genid++;
  445. write_unlock_bh(&fib_hash_lock);
  446. if (new_f)
  447. fz->fz_nent++;
  448. rt_cache_flush(-1);
  449. rtmsg_fib(RTM_NEWROUTE, key, new_fa, z, tb->tb_id, n, req);
  450. return 0;
  451. out_free_new_fa:
  452. kmem_cache_free(fn_alias_kmem, new_fa);
  453. out:
  454. fib_release_info(fi);
  455. return err;
  456. }
  457. static int
  458. fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
  459. struct nlmsghdr *n, struct netlink_skb_parms *req)
  460. {
  461. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  462. struct fib_node *f;
  463. struct fib_alias *fa, *fa_to_delete;
  464. int z = r->rtm_dst_len;
  465. struct fn_zone *fz;
  466. u32 key;
  467. u8 tos = r->rtm_tos;
  468. if (z > 32)
  469. return -EINVAL;
  470. if ((fz = table->fn_zones[z]) == NULL)
  471. return -ESRCH;
  472. key = 0;
  473. if (rta->rta_dst) {
  474. u32 dst;
  475. memcpy(&dst, rta->rta_dst, 4);
  476. if (dst & ~FZ_MASK(fz))
  477. return -EINVAL;
  478. key = fz_key(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, 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 != tos)
  492. break;
  493. if ((!r->rtm_type ||
  494. fa->fa_type == r->rtm_type) &&
  495. (r->rtm_scope == RT_SCOPE_NOWHERE ||
  496. fa->fa_scope == r->rtm_scope) &&
  497. (!r->rtm_protocol ||
  498. fi->fib_protocol == r->rtm_protocol) &&
  499. fib_nh_match(r, n, rta, 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, z, tb->tb_id, n, req);
  508. kill_fn = 0;
  509. write_lock_bh(&fib_hash_lock);
  510. list_del(&fa->fa_list);
  511. if (list_empty(&f->fn_alias)) {
  512. hlist_del(&f->fn_hash);
  513. kill_fn = 1;
  514. }
  515. fib_hash_genid++;
  516. write_unlock_bh(&fib_hash_lock);
  517. if (fa->fa_state & FA_S_ACCESSED)
  518. rt_cache_flush(-1);
  519. fn_free_alias(fa);
  520. if (kill_fn) {
  521. fn_free_node(f);
  522. fz->fz_nent--;
  523. }
  524. return 0;
  525. }
  526. return -ESRCH;
  527. }
  528. static int fn_flush_list(struct fn_zone *fz, int idx)
  529. {
  530. struct hlist_head *head = &fz->fz_hash[idx];
  531. struct hlist_node *node, *n;
  532. struct fib_node *f;
  533. int found = 0;
  534. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  535. struct fib_alias *fa, *fa_node;
  536. int kill_f;
  537. kill_f = 0;
  538. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  539. struct fib_info *fi = fa->fa_info;
  540. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  541. write_lock_bh(&fib_hash_lock);
  542. list_del(&fa->fa_list);
  543. if (list_empty(&f->fn_alias)) {
  544. hlist_del(&f->fn_hash);
  545. kill_f = 1;
  546. }
  547. fib_hash_genid++;
  548. write_unlock_bh(&fib_hash_lock);
  549. fn_free_alias(fa);
  550. found++;
  551. }
  552. }
  553. if (kill_f) {
  554. fn_free_node(f);
  555. fz->fz_nent--;
  556. }
  557. }
  558. return found;
  559. }
  560. static int fn_hash_flush(struct fib_table *tb)
  561. {
  562. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  563. struct fn_zone *fz;
  564. int found = 0;
  565. for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
  566. int i;
  567. for (i = fz->fz_divisor - 1; i >= 0; i--)
  568. found += fn_flush_list(fz, i);
  569. }
  570. return found;
  571. }
  572. static inline int
  573. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  574. struct fib_table *tb,
  575. struct fn_zone *fz,
  576. struct hlist_head *head)
  577. {
  578. struct hlist_node *node;
  579. struct fib_node *f;
  580. int i, s_i;
  581. s_i = cb->args[3];
  582. i = 0;
  583. hlist_for_each_entry(f, node, head, fn_hash) {
  584. struct fib_alias *fa;
  585. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  586. if (i < s_i)
  587. goto next;
  588. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  589. cb->nlh->nlmsg_seq,
  590. RTM_NEWROUTE,
  591. tb->tb_id,
  592. fa->fa_type,
  593. fa->fa_scope,
  594. &f->fn_key,
  595. fz->fz_order,
  596. fa->fa_tos,
  597. fa->fa_info,
  598. NLM_F_MULTI) < 0) {
  599. cb->args[3] = i;
  600. return -1;
  601. }
  602. next:
  603. i++;
  604. }
  605. }
  606. cb->args[3] = i;
  607. return skb->len;
  608. }
  609. static inline int
  610. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  611. struct fib_table *tb,
  612. struct fn_zone *fz)
  613. {
  614. int h, s_h;
  615. s_h = cb->args[2];
  616. for (h=0; h < fz->fz_divisor; h++) {
  617. if (h < s_h) continue;
  618. if (h > s_h)
  619. memset(&cb->args[3], 0,
  620. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  621. if (fz->fz_hash == NULL ||
  622. hlist_empty(&fz->fz_hash[h]))
  623. continue;
  624. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
  625. cb->args[2] = h;
  626. return -1;
  627. }
  628. }
  629. cb->args[2] = h;
  630. return skb->len;
  631. }
  632. static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
  633. {
  634. int m, s_m;
  635. struct fn_zone *fz;
  636. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  637. s_m = cb->args[1];
  638. read_lock(&fib_hash_lock);
  639. for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
  640. if (m < s_m) continue;
  641. if (m > s_m)
  642. memset(&cb->args[2], 0,
  643. sizeof(cb->args) - 2*sizeof(cb->args[0]));
  644. if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  645. cb->args[1] = m;
  646. read_unlock(&fib_hash_lock);
  647. return -1;
  648. }
  649. }
  650. read_unlock(&fib_hash_lock);
  651. cb->args[1] = m;
  652. return skb->len;
  653. }
  654. #ifdef CONFIG_IP_MULTIPLE_TABLES
  655. struct fib_table * fib_hash_init(int id)
  656. #else
  657. struct fib_table * __init fib_hash_init(int id)
  658. #endif
  659. {
  660. struct fib_table *tb;
  661. if (fn_hash_kmem == NULL)
  662. fn_hash_kmem = kmem_cache_create("ip_fib_hash",
  663. sizeof(struct fib_node),
  664. 0, SLAB_HWCACHE_ALIGN,
  665. NULL, NULL);
  666. if (fn_alias_kmem == NULL)
  667. fn_alias_kmem = kmem_cache_create("ip_fib_alias",
  668. sizeof(struct fib_alias),
  669. 0, SLAB_HWCACHE_ALIGN,
  670. NULL, NULL);
  671. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
  672. GFP_KERNEL);
  673. if (tb == NULL)
  674. return NULL;
  675. tb->tb_id = id;
  676. tb->tb_lookup = fn_hash_lookup;
  677. tb->tb_insert = fn_hash_insert;
  678. tb->tb_delete = fn_hash_delete;
  679. tb->tb_flush = fn_hash_flush;
  680. tb->tb_select_default = fn_hash_select_default;
  681. tb->tb_dump = fn_hash_dump;
  682. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  683. return tb;
  684. }
  685. /* ------------------------------------------------------------------------ */
  686. #ifdef CONFIG_PROC_FS
  687. struct fib_iter_state {
  688. struct fn_zone *zone;
  689. int bucket;
  690. struct hlist_head *hash_head;
  691. struct fib_node *fn;
  692. struct fib_alias *fa;
  693. loff_t pos;
  694. unsigned int genid;
  695. int valid;
  696. };
  697. static struct fib_alias *fib_get_first(struct seq_file *seq)
  698. {
  699. struct fib_iter_state *iter = seq->private;
  700. struct fn_hash *table = (struct fn_hash *) ip_fib_main_table->tb_data;
  701. iter->bucket = 0;
  702. iter->hash_head = NULL;
  703. iter->fn = NULL;
  704. iter->fa = NULL;
  705. iter->pos = 0;
  706. iter->genid = fib_hash_genid;
  707. iter->valid = 1;
  708. for (iter->zone = table->fn_zone_list; iter->zone;
  709. iter->zone = iter->zone->fz_next) {
  710. int maxslot;
  711. if (!iter->zone->fz_nent)
  712. continue;
  713. iter->hash_head = iter->zone->fz_hash;
  714. maxslot = iter->zone->fz_divisor;
  715. for (iter->bucket = 0; iter->bucket < maxslot;
  716. ++iter->bucket, ++iter->hash_head) {
  717. struct hlist_node *node;
  718. struct fib_node *fn;
  719. hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
  720. struct fib_alias *fa;
  721. list_for_each_entry(fa,&fn->fn_alias,fa_list) {
  722. iter->fn = fn;
  723. iter->fa = fa;
  724. goto out;
  725. }
  726. }
  727. }
  728. }
  729. out:
  730. return iter->fa;
  731. }
  732. static struct fib_alias *fib_get_next(struct seq_file *seq)
  733. {
  734. struct fib_iter_state *iter = seq->private;
  735. struct fib_node *fn;
  736. struct fib_alias *fa;
  737. /* Advance FA, if any. */
  738. fn = iter->fn;
  739. fa = iter->fa;
  740. if (fa) {
  741. BUG_ON(!fn);
  742. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  743. iter->fa = fa;
  744. goto out;
  745. }
  746. }
  747. fa = iter->fa = NULL;
  748. /* Advance FN. */
  749. if (fn) {
  750. struct hlist_node *node = &fn->fn_hash;
  751. hlist_for_each_entry_continue(fn, node, fn_hash) {
  752. iter->fn = fn;
  753. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  754. iter->fa = fa;
  755. goto out;
  756. }
  757. }
  758. }
  759. fn = iter->fn = NULL;
  760. /* Advance hash chain. */
  761. if (!iter->zone)
  762. goto out;
  763. for (;;) {
  764. struct hlist_node *node;
  765. int maxslot;
  766. maxslot = iter->zone->fz_divisor;
  767. while (++iter->bucket < maxslot) {
  768. iter->hash_head++;
  769. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  770. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  771. iter->fn = fn;
  772. iter->fa = fa;
  773. goto out;
  774. }
  775. }
  776. }
  777. iter->zone = iter->zone->fz_next;
  778. if (!iter->zone)
  779. goto out;
  780. iter->bucket = 0;
  781. iter->hash_head = iter->zone->fz_hash;
  782. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  783. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  784. iter->fn = fn;
  785. iter->fa = fa;
  786. goto out;
  787. }
  788. }
  789. }
  790. out:
  791. iter->pos++;
  792. return fa;
  793. }
  794. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  795. {
  796. struct fib_iter_state *iter = seq->private;
  797. struct fib_alias *fa;
  798. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  799. fa = iter->fa;
  800. pos -= iter->pos;
  801. } else
  802. fa = fib_get_first(seq);
  803. if (fa)
  804. while (pos && (fa = fib_get_next(seq)))
  805. --pos;
  806. return pos ? NULL : fa;
  807. }
  808. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  809. {
  810. void *v = NULL;
  811. read_lock(&fib_hash_lock);
  812. if (ip_fib_main_table)
  813. v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  814. return v;
  815. }
  816. static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  817. {
  818. ++*pos;
  819. return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
  820. }
  821. static void fib_seq_stop(struct seq_file *seq, void *v)
  822. {
  823. read_unlock(&fib_hash_lock);
  824. }
  825. static unsigned fib_flag_trans(int type, u32 mask, struct fib_info *fi)
  826. {
  827. static 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 == 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. char bf[128];
  848. u32 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. snprintf(bf, sizeof(bf),
  868. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  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);
  874. else
  875. snprintf(bf, sizeof(bf),
  876. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  877. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
  878. seq_printf(seq, "%-127s\n", bf);
  879. out:
  880. return 0;
  881. }
  882. static 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. struct seq_file *seq;
  891. int rc = -ENOMEM;
  892. struct fib_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  893. if (!s)
  894. goto out;
  895. rc = seq_open(file, &fib_seq_ops);
  896. if (rc)
  897. goto out_kfree;
  898. seq = file->private_data;
  899. seq->private = s;
  900. memset(s, 0, sizeof(*s));
  901. out:
  902. return rc;
  903. out_kfree:
  904. kfree(s);
  905. goto out;
  906. }
  907. static struct file_operations fib_seq_fops = {
  908. .owner = THIS_MODULE,
  909. .open = fib_seq_open,
  910. .read = seq_read,
  911. .llseek = seq_lseek,
  912. .release = seq_release_private,
  913. };
  914. int __init fib_proc_init(void)
  915. {
  916. if (!proc_net_fops_create("route", S_IRUGO, &fib_seq_fops))
  917. return -ENOMEM;
  918. return 0;
  919. }
  920. void __init fib_proc_exit(void)
  921. {
  922. proc_net_remove("route");
  923. }
  924. #endif /* CONFIG_PROC_FS */