fib_hash.c 23 KB

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