fib_hash.c 23 KB

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