fib_hash.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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) < 0) {
  598. cb->args[3] = i;
  599. return -1;
  600. }
  601. next:
  602. i++;
  603. }
  604. }
  605. cb->args[3] = i;
  606. return skb->len;
  607. }
  608. static inline int
  609. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  610. struct fib_table *tb,
  611. struct fn_zone *fz)
  612. {
  613. int h, s_h;
  614. s_h = cb->args[2];
  615. for (h=0; h < fz->fz_divisor; h++) {
  616. if (h < s_h) continue;
  617. if (h > s_h)
  618. memset(&cb->args[3], 0,
  619. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  620. if (fz->fz_hash == NULL ||
  621. hlist_empty(&fz->fz_hash[h]))
  622. continue;
  623. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
  624. cb->args[2] = h;
  625. return -1;
  626. }
  627. }
  628. cb->args[2] = h;
  629. return skb->len;
  630. }
  631. static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
  632. {
  633. int m, s_m;
  634. struct fn_zone *fz;
  635. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  636. s_m = cb->args[1];
  637. read_lock(&fib_hash_lock);
  638. for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
  639. if (m < s_m) continue;
  640. if (m > s_m)
  641. memset(&cb->args[2], 0,
  642. sizeof(cb->args) - 2*sizeof(cb->args[0]));
  643. if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  644. cb->args[1] = m;
  645. read_unlock(&fib_hash_lock);
  646. return -1;
  647. }
  648. }
  649. read_unlock(&fib_hash_lock);
  650. cb->args[1] = m;
  651. return skb->len;
  652. }
  653. #ifdef CONFIG_IP_MULTIPLE_TABLES
  654. struct fib_table * fib_hash_init(int id)
  655. #else
  656. struct fib_table * __init fib_hash_init(int id)
  657. #endif
  658. {
  659. struct fib_table *tb;
  660. if (fn_hash_kmem == NULL)
  661. fn_hash_kmem = kmem_cache_create("ip_fib_hash",
  662. sizeof(struct fib_node),
  663. 0, SLAB_HWCACHE_ALIGN,
  664. NULL, NULL);
  665. if (fn_alias_kmem == NULL)
  666. fn_alias_kmem = kmem_cache_create("ip_fib_alias",
  667. sizeof(struct fib_alias),
  668. 0, SLAB_HWCACHE_ALIGN,
  669. NULL, NULL);
  670. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
  671. GFP_KERNEL);
  672. if (tb == NULL)
  673. return NULL;
  674. tb->tb_id = id;
  675. tb->tb_lookup = fn_hash_lookup;
  676. tb->tb_insert = fn_hash_insert;
  677. tb->tb_delete = fn_hash_delete;
  678. tb->tb_flush = fn_hash_flush;
  679. tb->tb_select_default = fn_hash_select_default;
  680. tb->tb_dump = fn_hash_dump;
  681. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  682. return tb;
  683. }
  684. /* ------------------------------------------------------------------------ */
  685. #ifdef CONFIG_PROC_FS
  686. struct fib_iter_state {
  687. struct fn_zone *zone;
  688. int bucket;
  689. struct hlist_head *hash_head;
  690. struct fib_node *fn;
  691. struct fib_alias *fa;
  692. loff_t pos;
  693. unsigned int genid;
  694. int valid;
  695. };
  696. static struct fib_alias *fib_get_first(struct seq_file *seq)
  697. {
  698. struct fib_iter_state *iter = seq->private;
  699. struct fn_hash *table = (struct fn_hash *) ip_fib_main_table->tb_data;
  700. iter->bucket = 0;
  701. iter->hash_head = NULL;
  702. iter->fn = NULL;
  703. iter->fa = NULL;
  704. iter->pos = 0;
  705. iter->genid = fib_hash_genid;
  706. iter->valid = 1;
  707. for (iter->zone = table->fn_zone_list; iter->zone;
  708. iter->zone = iter->zone->fz_next) {
  709. int maxslot;
  710. if (!iter->zone->fz_nent)
  711. continue;
  712. iter->hash_head = iter->zone->fz_hash;
  713. maxslot = iter->zone->fz_divisor;
  714. for (iter->bucket = 0; iter->bucket < maxslot;
  715. ++iter->bucket, ++iter->hash_head) {
  716. struct hlist_node *node;
  717. struct fib_node *fn;
  718. hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
  719. struct fib_alias *fa;
  720. list_for_each_entry(fa,&fn->fn_alias,fa_list) {
  721. iter->fn = fn;
  722. iter->fa = fa;
  723. goto out;
  724. }
  725. }
  726. }
  727. }
  728. out:
  729. return iter->fa;
  730. }
  731. static struct fib_alias *fib_get_next(struct seq_file *seq)
  732. {
  733. struct fib_iter_state *iter = seq->private;
  734. struct fib_node *fn;
  735. struct fib_alias *fa;
  736. /* Advance FA, if any. */
  737. fn = iter->fn;
  738. fa = iter->fa;
  739. if (fa) {
  740. BUG_ON(!fn);
  741. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  742. iter->fa = fa;
  743. goto out;
  744. }
  745. }
  746. fa = iter->fa = NULL;
  747. /* Advance FN. */
  748. if (fn) {
  749. struct hlist_node *node = &fn->fn_hash;
  750. hlist_for_each_entry_continue(fn, node, fn_hash) {
  751. iter->fn = fn;
  752. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  753. iter->fa = fa;
  754. goto out;
  755. }
  756. }
  757. }
  758. fn = iter->fn = NULL;
  759. /* Advance hash chain. */
  760. if (!iter->zone)
  761. goto out;
  762. for (;;) {
  763. struct hlist_node *node;
  764. int maxslot;
  765. maxslot = iter->zone->fz_divisor;
  766. while (++iter->bucket < maxslot) {
  767. iter->hash_head++;
  768. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  769. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  770. iter->fn = fn;
  771. iter->fa = fa;
  772. goto out;
  773. }
  774. }
  775. }
  776. iter->zone = iter->zone->fz_next;
  777. if (!iter->zone)
  778. goto out;
  779. iter->bucket = 0;
  780. iter->hash_head = iter->zone->fz_hash;
  781. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  782. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  783. iter->fn = fn;
  784. iter->fa = fa;
  785. goto out;
  786. }
  787. }
  788. }
  789. out:
  790. iter->pos++;
  791. return fa;
  792. }
  793. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  794. {
  795. struct fib_iter_state *iter = seq->private;
  796. struct fib_alias *fa;
  797. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  798. fa = iter->fa;
  799. pos -= iter->pos;
  800. } else
  801. fa = fib_get_first(seq);
  802. if (fa)
  803. while (pos && (fa = fib_get_next(seq)))
  804. --pos;
  805. return pos ? NULL : fa;
  806. }
  807. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  808. {
  809. void *v = NULL;
  810. read_lock(&fib_hash_lock);
  811. if (ip_fib_main_table)
  812. v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  813. return v;
  814. }
  815. static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  816. {
  817. ++*pos;
  818. return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
  819. }
  820. static void fib_seq_stop(struct seq_file *seq, void *v)
  821. {
  822. read_unlock(&fib_hash_lock);
  823. }
  824. static unsigned fib_flag_trans(int type, u32 mask, struct fib_info *fi)
  825. {
  826. static unsigned type2flags[RTN_MAX + 1] = {
  827. [7] = RTF_REJECT, [8] = RTF_REJECT,
  828. };
  829. unsigned flags = type2flags[type];
  830. if (fi && fi->fib_nh->nh_gw)
  831. flags |= RTF_GATEWAY;
  832. if (mask == 0xFFFFFFFF)
  833. flags |= RTF_HOST;
  834. flags |= RTF_UP;
  835. return flags;
  836. }
  837. /*
  838. * This outputs /proc/net/route.
  839. *
  840. * It always works in backward compatibility mode.
  841. * The format of the file is not supposed to be changed.
  842. */
  843. static int fib_seq_show(struct seq_file *seq, void *v)
  844. {
  845. struct fib_iter_state *iter;
  846. char bf[128];
  847. u32 prefix, mask;
  848. unsigned flags;
  849. struct fib_node *f;
  850. struct fib_alias *fa;
  851. struct fib_info *fi;
  852. if (v == SEQ_START_TOKEN) {
  853. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  854. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  855. "\tWindow\tIRTT");
  856. goto out;
  857. }
  858. iter = seq->private;
  859. f = iter->fn;
  860. fa = iter->fa;
  861. fi = fa->fa_info;
  862. prefix = f->fn_key;
  863. mask = FZ_MASK(iter->zone);
  864. flags = fib_flag_trans(fa->fa_type, mask, fi);
  865. if (fi)
  866. snprintf(bf, sizeof(bf),
  867. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  868. fi->fib_dev ? fi->fib_dev->name : "*", prefix,
  869. fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
  870. mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
  871. fi->fib_window,
  872. fi->fib_rtt >> 3);
  873. else
  874. snprintf(bf, sizeof(bf),
  875. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  876. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
  877. seq_printf(seq, "%-127s\n", bf);
  878. out:
  879. return 0;
  880. }
  881. static struct seq_operations fib_seq_ops = {
  882. .start = fib_seq_start,
  883. .next = fib_seq_next,
  884. .stop = fib_seq_stop,
  885. .show = fib_seq_show,
  886. };
  887. static int fib_seq_open(struct inode *inode, struct file *file)
  888. {
  889. struct seq_file *seq;
  890. int rc = -ENOMEM;
  891. struct fib_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  892. if (!s)
  893. goto out;
  894. rc = seq_open(file, &fib_seq_ops);
  895. if (rc)
  896. goto out_kfree;
  897. seq = file->private_data;
  898. seq->private = s;
  899. memset(s, 0, sizeof(*s));
  900. out:
  901. return rc;
  902. out_kfree:
  903. kfree(s);
  904. goto out;
  905. }
  906. static struct file_operations fib_seq_fops = {
  907. .owner = THIS_MODULE,
  908. .open = fib_seq_open,
  909. .read = seq_read,
  910. .llseek = seq_lseek,
  911. .release = seq_release_private,
  912. };
  913. int __init fib_proc_init(void)
  914. {
  915. if (!proc_net_fops_create("route", S_IRUGO, &fib_seq_fops))
  916. return -ENOMEM;
  917. return 0;
  918. }
  919. void __init fib_proc_exit(void)
  920. {
  921. proc_net_remove("route");
  922. }
  923. #endif /* CONFIG_PROC_FS */