fib_hash.c 23 KB

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