fib_hash.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <asm/uaccess.h>
  16. #include <asm/system.h>
  17. #include <linux/bitops.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/string.h>
  22. #include <linux/socket.h>
  23. #include <linux/sockios.h>
  24. #include <linux/errno.h>
  25. #include <linux/in.h>
  26. #include <linux/inet.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/netlink.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <net/net_namespace.h>
  36. #include <net/ip.h>
  37. #include <net/protocol.h>
  38. #include <net/route.h>
  39. #include <net/tcp.h>
  40. #include <net/sock.h>
  41. #include <net/ip_fib.h>
  42. #include "fib_lookup.h"
  43. static struct kmem_cache *fn_hash_kmem __read_mostly;
  44. static struct kmem_cache *fn_alias_kmem __read_mostly;
  45. struct fib_node {
  46. struct hlist_node fn_hash;
  47. struct list_head fn_alias;
  48. __be32 fn_key;
  49. struct fib_alias fn_embedded_alias;
  50. };
  51. #define EMBEDDED_HASH_SIZE (L1_CACHE_BYTES / sizeof(struct hlist_head))
  52. struct fn_zone {
  53. struct fn_zone *fz_next; /* Next not empty zone */
  54. struct hlist_head *fz_hash; /* Hash table pointer */
  55. u32 fz_hashmask; /* (fz_divisor - 1) */
  56. u8 fz_order; /* Zone order (0..32) */
  57. u8 fz_revorder; /* 32 - fz_order */
  58. __be32 fz_mask; /* inet_make_mask(order) */
  59. #define FZ_MASK(fz) ((fz)->fz_mask)
  60. struct hlist_head fz_embedded_hash[EMBEDDED_HASH_SIZE];
  61. int fz_nent; /* Number of entries */
  62. int fz_divisor; /* Hash size (mask+1) */
  63. };
  64. struct fn_hash {
  65. struct fn_zone *fn_zones[33];
  66. struct fn_zone *fn_zone_list;
  67. };
  68. static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
  69. {
  70. u32 h = ntohl(key) >> fz->fz_revorder;
  71. h ^= (h>>20);
  72. h ^= (h>>10);
  73. h ^= (h>>5);
  74. h &= fz->fz_hashmask;
  75. return h;
  76. }
  77. static inline __be32 fz_key(__be32 dst, struct fn_zone *fz)
  78. {
  79. return dst & FZ_MASK(fz);
  80. }
  81. static DEFINE_RWLOCK(fib_hash_lock);
  82. static unsigned int fib_hash_genid;
  83. #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
  84. static struct hlist_head *fz_hash_alloc(int divisor)
  85. {
  86. unsigned long size = divisor * sizeof(struct hlist_head);
  87. if (size <= PAGE_SIZE) {
  88. return kzalloc(size, GFP_KERNEL);
  89. } else {
  90. return (struct hlist_head *)
  91. __get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(size));
  92. }
  93. }
  94. /* The fib hash lock must be held when this is called. */
  95. static inline void fn_rebuild_zone(struct fn_zone *fz,
  96. struct hlist_head *old_ht,
  97. int old_divisor)
  98. {
  99. int i;
  100. for (i = 0; i < old_divisor; i++) {
  101. struct hlist_node *node, *n;
  102. struct fib_node *f;
  103. hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
  104. struct hlist_head *new_head;
  105. hlist_del(&f->fn_hash);
  106. new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  107. hlist_add_head(&f->fn_hash, new_head);
  108. }
  109. }
  110. }
  111. static void fz_hash_free(struct hlist_head *hash, int divisor)
  112. {
  113. unsigned long size = divisor * sizeof(struct hlist_head);
  114. if (size <= PAGE_SIZE)
  115. kfree(hash);
  116. else
  117. free_pages((unsigned long)hash, get_order(size));
  118. }
  119. static void fn_rehash_zone(struct fn_zone *fz)
  120. {
  121. struct hlist_head *ht, *old_ht;
  122. int old_divisor, new_divisor;
  123. u32 new_hashmask;
  124. new_divisor = old_divisor = fz->fz_divisor;
  125. switch (old_divisor) {
  126. case EMBEDDED_HASH_SIZE:
  127. new_divisor *= EMBEDDED_HASH_SIZE;
  128. break;
  129. case EMBEDDED_HASH_SIZE*EMBEDDED_HASH_SIZE:
  130. new_divisor *= (EMBEDDED_HASH_SIZE/2);
  131. break;
  132. default:
  133. if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
  134. printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
  135. return;
  136. }
  137. new_divisor = (old_divisor << 1);
  138. break;
  139. }
  140. new_hashmask = (new_divisor - 1);
  141. #if RT_CACHE_DEBUG >= 2
  142. printk(KERN_DEBUG "fn_rehash_zone: hash for zone %d grows from %d\n",
  143. 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. if (old_ht != fz->fz_embedded_hash)
  156. fz_hash_free(old_ht, old_divisor);
  157. }
  158. }
  159. static inline void fn_free_node(struct fib_node * f)
  160. {
  161. kmem_cache_free(fn_hash_kmem, f);
  162. }
  163. static inline void fn_free_alias(struct fib_alias *fa, struct fib_node *f)
  164. {
  165. fib_release_info(fa->fa_info);
  166. if (fa == &f->fn_embedded_alias)
  167. fa->fa_info = NULL;
  168. else
  169. kmem_cache_free(fn_alias_kmem, fa);
  170. }
  171. static struct fn_zone *
  172. fn_new_zone(struct fn_hash *table, int z)
  173. {
  174. int i;
  175. struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
  176. if (!fz)
  177. return NULL;
  178. fz->fz_divisor = z ? EMBEDDED_HASH_SIZE : 1;
  179. fz->fz_hashmask = fz->fz_divisor - 1;
  180. fz->fz_hash = fz->fz_embedded_hash;
  181. fz->fz_order = z;
  182. fz->fz_revorder = 32 - z;
  183. fz->fz_mask = inet_make_mask(z);
  184. /* Find the first not empty zone with more specific mask */
  185. for (i=z+1; i<=32; i++)
  186. if (table->fn_zones[i])
  187. break;
  188. write_lock_bh(&fib_hash_lock);
  189. if (i>32) {
  190. /* No more specific masks, we are the first. */
  191. fz->fz_next = table->fn_zone_list;
  192. table->fn_zone_list = fz;
  193. } else {
  194. fz->fz_next = table->fn_zones[i]->fz_next;
  195. table->fn_zones[i]->fz_next = fz;
  196. }
  197. table->fn_zones[z] = fz;
  198. fib_hash_genid++;
  199. write_unlock_bh(&fib_hash_lock);
  200. return fz;
  201. }
  202. int fib_table_lookup(struct fib_table *tb,
  203. const struct flowi *flp, struct fib_result *res,
  204. int fib_flags)
  205. {
  206. int err;
  207. struct fn_zone *fz;
  208. struct fn_hash *t = (struct fn_hash *)tb->tb_data;
  209. read_lock(&fib_hash_lock);
  210. for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
  211. struct hlist_head *head;
  212. struct hlist_node *node;
  213. struct fib_node *f;
  214. __be32 k = fz_key(flp->fl4_dst, fz);
  215. head = &fz->fz_hash[fn_hash(k, fz)];
  216. hlist_for_each_entry(f, node, head, fn_hash) {
  217. if (f->fn_key != k)
  218. continue;
  219. err = fib_semantic_match(&f->fn_alias,
  220. flp, res,
  221. fz->fz_order, fib_flags);
  222. if (err <= 0)
  223. goto out;
  224. }
  225. }
  226. err = 1;
  227. out:
  228. read_unlock(&fib_hash_lock);
  229. return err;
  230. }
  231. void fib_table_select_default(struct fib_table *tb,
  232. const struct flowi *flp, struct fib_result *res)
  233. {
  234. int order, last_idx;
  235. struct hlist_node *node;
  236. struct fib_node *f;
  237. struct fib_info *fi = NULL;
  238. struct fib_info *last_resort;
  239. struct fn_hash *t = (struct fn_hash *)tb->tb_data;
  240. struct fn_zone *fz = t->fn_zones[0];
  241. if (fz == NULL)
  242. return;
  243. last_idx = -1;
  244. last_resort = NULL;
  245. order = -1;
  246. read_lock(&fib_hash_lock);
  247. hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
  248. struct fib_alias *fa;
  249. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  250. struct fib_info *next_fi = fa->fa_info;
  251. if (fa->fa_scope != res->scope ||
  252. fa->fa_type != RTN_UNICAST)
  253. continue;
  254. if (next_fi->fib_priority > res->fi->fib_priority)
  255. break;
  256. if (!next_fi->fib_nh[0].nh_gw ||
  257. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  258. continue;
  259. fa->fa_state |= FA_S_ACCESSED;
  260. if (fi == NULL) {
  261. if (next_fi != res->fi)
  262. break;
  263. } else if (!fib_detect_death(fi, order, &last_resort,
  264. &last_idx, tb->tb_default)) {
  265. fib_result_assign(res, fi);
  266. tb->tb_default = order;
  267. goto out;
  268. }
  269. fi = next_fi;
  270. order++;
  271. }
  272. }
  273. if (order <= 0 || fi == NULL) {
  274. tb->tb_default = -1;
  275. goto out;
  276. }
  277. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  278. tb->tb_default)) {
  279. fib_result_assign(res, fi);
  280. tb->tb_default = order;
  281. goto out;
  282. }
  283. if (last_idx >= 0)
  284. fib_result_assign(res, last_resort);
  285. tb->tb_default = last_idx;
  286. out:
  287. read_unlock(&fib_hash_lock);
  288. }
  289. /* Insert node F to FZ. */
  290. static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
  291. {
  292. struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  293. hlist_add_head(&f->fn_hash, head);
  294. }
  295. /* Return the node in FZ matching KEY. */
  296. static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
  297. {
  298. struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
  299. struct hlist_node *node;
  300. struct fib_node *f;
  301. hlist_for_each_entry(f, node, head, fn_hash) {
  302. if (f->fn_key == key)
  303. return f;
  304. }
  305. return NULL;
  306. }
  307. int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
  308. {
  309. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  310. struct fib_node *new_f = NULL;
  311. struct fib_node *f;
  312. struct fib_alias *fa, *new_fa;
  313. struct fn_zone *fz;
  314. struct fib_info *fi;
  315. u8 tos = cfg->fc_tos;
  316. __be32 key;
  317. int err;
  318. if (cfg->fc_dst_len > 32)
  319. return -EINVAL;
  320. fz = table->fn_zones[cfg->fc_dst_len];
  321. if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
  322. return -ENOBUFS;
  323. key = 0;
  324. if (cfg->fc_dst) {
  325. if (cfg->fc_dst & ~FZ_MASK(fz))
  326. return -EINVAL;
  327. key = fz_key(cfg->fc_dst, fz);
  328. }
  329. fi = fib_create_info(cfg);
  330. if (IS_ERR(fi))
  331. return PTR_ERR(fi);
  332. if (fz->fz_nent > (fz->fz_divisor<<1) &&
  333. fz->fz_divisor < FZ_MAX_DIVISOR &&
  334. (cfg->fc_dst_len == 32 ||
  335. (1 << cfg->fc_dst_len) > fz->fz_divisor))
  336. fn_rehash_zone(fz);
  337. f = fib_find_node(fz, key);
  338. if (!f)
  339. fa = NULL;
  340. else
  341. fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
  342. /* Now fa, if non-NULL, points to the first fib alias
  343. * with the same keys [prefix,tos,priority], if such key already
  344. * exists or to the node before which we will insert new one.
  345. *
  346. * If fa is NULL, we will need to allocate a new one and
  347. * insert to the head of f.
  348. *
  349. * If f is NULL, no fib node matched the destination key
  350. * and we need to allocate a new one of those as well.
  351. */
  352. if (fa && fa->fa_tos == tos &&
  353. fa->fa_info->fib_priority == fi->fib_priority) {
  354. struct fib_alias *fa_first, *fa_match;
  355. err = -EEXIST;
  356. if (cfg->fc_nlflags & NLM_F_EXCL)
  357. goto out;
  358. /* We have 2 goals:
  359. * 1. Find exact match for type, scope, fib_info to avoid
  360. * duplicate routes
  361. * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
  362. */
  363. fa_match = NULL;
  364. fa_first = fa;
  365. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  366. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  367. if (fa->fa_tos != tos)
  368. break;
  369. if (fa->fa_info->fib_priority != fi->fib_priority)
  370. break;
  371. if (fa->fa_type == cfg->fc_type &&
  372. fa->fa_scope == cfg->fc_scope &&
  373. fa->fa_info == fi) {
  374. fa_match = fa;
  375. break;
  376. }
  377. }
  378. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  379. struct fib_info *fi_drop;
  380. u8 state;
  381. fa = fa_first;
  382. if (fa_match) {
  383. if (fa == fa_match)
  384. err = 0;
  385. goto out;
  386. }
  387. write_lock_bh(&fib_hash_lock);
  388. fi_drop = fa->fa_info;
  389. fa->fa_info = fi;
  390. fa->fa_type = cfg->fc_type;
  391. fa->fa_scope = cfg->fc_scope;
  392. state = fa->fa_state;
  393. fa->fa_state &= ~FA_S_ACCESSED;
  394. fib_hash_genid++;
  395. write_unlock_bh(&fib_hash_lock);
  396. fib_release_info(fi_drop);
  397. if (state & FA_S_ACCESSED)
  398. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  399. rtmsg_fib(RTM_NEWROUTE, key, fa, cfg->fc_dst_len, tb->tb_id,
  400. &cfg->fc_nlinfo, NLM_F_REPLACE);
  401. return 0;
  402. }
  403. /* Error if we find a perfect match which
  404. * uses the same scope, type, and nexthop
  405. * information.
  406. */
  407. if (fa_match)
  408. goto out;
  409. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  410. fa = fa_first;
  411. }
  412. err = -ENOENT;
  413. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  414. goto out;
  415. err = -ENOBUFS;
  416. if (!f) {
  417. new_f = kmem_cache_zalloc(fn_hash_kmem, GFP_KERNEL);
  418. if (new_f == NULL)
  419. goto out;
  420. INIT_HLIST_NODE(&new_f->fn_hash);
  421. INIT_LIST_HEAD(&new_f->fn_alias);
  422. new_f->fn_key = key;
  423. f = new_f;
  424. }
  425. new_fa = &f->fn_embedded_alias;
  426. if (new_fa->fa_info != NULL) {
  427. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  428. if (new_fa == NULL)
  429. goto out;
  430. }
  431. new_fa->fa_info = fi;
  432. new_fa->fa_tos = tos;
  433. new_fa->fa_type = cfg->fc_type;
  434. new_fa->fa_scope = cfg->fc_scope;
  435. new_fa->fa_state = 0;
  436. /*
  437. * Insert new entry to the list.
  438. */
  439. write_lock_bh(&fib_hash_lock);
  440. if (new_f)
  441. fib_insert_node(fz, new_f);
  442. list_add_tail(&new_fa->fa_list,
  443. (fa ? &fa->fa_list : &f->fn_alias));
  444. fib_hash_genid++;
  445. write_unlock_bh(&fib_hash_lock);
  446. if (new_f)
  447. fz->fz_nent++;
  448. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  449. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
  450. &cfg->fc_nlinfo, 0);
  451. return 0;
  452. out:
  453. if (new_f)
  454. kmem_cache_free(fn_hash_kmem, new_f);
  455. fib_release_info(fi);
  456. return err;
  457. }
  458. int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
  459. {
  460. struct fn_hash *table = (struct fn_hash *)tb->tb_data;
  461. struct fib_node *f;
  462. struct fib_alias *fa, *fa_to_delete;
  463. struct fn_zone *fz;
  464. __be32 key;
  465. if (cfg->fc_dst_len > 32)
  466. return -EINVAL;
  467. if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
  468. return -ESRCH;
  469. key = 0;
  470. if (cfg->fc_dst) {
  471. if (cfg->fc_dst & ~FZ_MASK(fz))
  472. return -EINVAL;
  473. key = fz_key(cfg->fc_dst, fz);
  474. }
  475. f = fib_find_node(fz, key);
  476. if (!f)
  477. fa = NULL;
  478. else
  479. fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
  480. if (!fa)
  481. return -ESRCH;
  482. fa_to_delete = NULL;
  483. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  484. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  485. struct fib_info *fi = fa->fa_info;
  486. if (fa->fa_tos != cfg->fc_tos)
  487. break;
  488. if ((!cfg->fc_type ||
  489. fa->fa_type == cfg->fc_type) &&
  490. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  491. fa->fa_scope == cfg->fc_scope) &&
  492. (!cfg->fc_protocol ||
  493. fi->fib_protocol == cfg->fc_protocol) &&
  494. fib_nh_match(cfg, fi) == 0) {
  495. fa_to_delete = fa;
  496. break;
  497. }
  498. }
  499. if (fa_to_delete) {
  500. int kill_fn;
  501. fa = fa_to_delete;
  502. rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
  503. tb->tb_id, &cfg->fc_nlinfo, 0);
  504. kill_fn = 0;
  505. write_lock_bh(&fib_hash_lock);
  506. list_del(&fa->fa_list);
  507. if (list_empty(&f->fn_alias)) {
  508. hlist_del(&f->fn_hash);
  509. kill_fn = 1;
  510. }
  511. fib_hash_genid++;
  512. write_unlock_bh(&fib_hash_lock);
  513. if (fa->fa_state & FA_S_ACCESSED)
  514. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  515. fn_free_alias(fa, f);
  516. if (kill_fn) {
  517. fn_free_node(f);
  518. fz->fz_nent--;
  519. }
  520. return 0;
  521. }
  522. return -ESRCH;
  523. }
  524. static int fn_flush_list(struct fn_zone *fz, int idx)
  525. {
  526. struct hlist_head *head = &fz->fz_hash[idx];
  527. struct hlist_node *node, *n;
  528. struct fib_node *f;
  529. int found = 0;
  530. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  531. struct fib_alias *fa, *fa_node;
  532. int kill_f;
  533. kill_f = 0;
  534. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  535. struct fib_info *fi = fa->fa_info;
  536. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  537. write_lock_bh(&fib_hash_lock);
  538. list_del(&fa->fa_list);
  539. if (list_empty(&f->fn_alias)) {
  540. hlist_del(&f->fn_hash);
  541. kill_f = 1;
  542. }
  543. fib_hash_genid++;
  544. write_unlock_bh(&fib_hash_lock);
  545. fn_free_alias(fa, f);
  546. found++;
  547. }
  548. }
  549. if (kill_f) {
  550. fn_free_node(f);
  551. fz->fz_nent--;
  552. }
  553. }
  554. return found;
  555. }
  556. int fib_table_flush(struct fib_table *tb)
  557. {
  558. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  559. struct fn_zone *fz;
  560. int found = 0;
  561. for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
  562. int i;
  563. for (i = fz->fz_divisor - 1; i >= 0; i--)
  564. found += fn_flush_list(fz, i);
  565. }
  566. return found;
  567. }
  568. static inline int
  569. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  570. struct fib_table *tb,
  571. struct fn_zone *fz,
  572. struct hlist_head *head)
  573. {
  574. struct hlist_node *node;
  575. struct fib_node *f;
  576. int i, s_i;
  577. s_i = cb->args[4];
  578. i = 0;
  579. hlist_for_each_entry(f, node, head, fn_hash) {
  580. struct fib_alias *fa;
  581. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  582. if (i < s_i)
  583. goto next;
  584. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  585. cb->nlh->nlmsg_seq,
  586. RTM_NEWROUTE,
  587. tb->tb_id,
  588. fa->fa_type,
  589. fa->fa_scope,
  590. f->fn_key,
  591. fz->fz_order,
  592. fa->fa_tos,
  593. fa->fa_info,
  594. NLM_F_MULTI) < 0) {
  595. cb->args[4] = i;
  596. return -1;
  597. }
  598. next:
  599. i++;
  600. }
  601. }
  602. cb->args[4] = i;
  603. return skb->len;
  604. }
  605. static inline int
  606. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  607. struct fib_table *tb,
  608. struct fn_zone *fz)
  609. {
  610. int h, s_h;
  611. if (fz->fz_hash == NULL)
  612. return skb->len;
  613. s_h = cb->args[3];
  614. for (h = s_h; h < fz->fz_divisor; h++) {
  615. if (hlist_empty(&fz->fz_hash[h]))
  616. continue;
  617. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h]) < 0) {
  618. cb->args[3] = h;
  619. return -1;
  620. }
  621. memset(&cb->args[4], 0,
  622. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  623. }
  624. cb->args[3] = h;
  625. return skb->len;
  626. }
  627. int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
  628. struct netlink_callback *cb)
  629. {
  630. int m, s_m;
  631. struct fn_zone *fz;
  632. struct fn_hash *table = (struct fn_hash *)tb->tb_data;
  633. s_m = cb->args[2];
  634. read_lock(&fib_hash_lock);
  635. for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
  636. if (m < s_m) continue;
  637. if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  638. cb->args[2] = m;
  639. read_unlock(&fib_hash_lock);
  640. return -1;
  641. }
  642. memset(&cb->args[3], 0,
  643. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  644. }
  645. read_unlock(&fib_hash_lock);
  646. cb->args[2] = m;
  647. return skb->len;
  648. }
  649. void __init fib_hash_init(void)
  650. {
  651. fn_hash_kmem = kmem_cache_create("ip_fib_hash", sizeof(struct fib_node),
  652. 0, SLAB_PANIC, NULL);
  653. fn_alias_kmem = kmem_cache_create("ip_fib_alias", sizeof(struct fib_alias),
  654. 0, SLAB_PANIC, NULL);
  655. }
  656. struct fib_table *fib_hash_table(u32 id)
  657. {
  658. struct fib_table *tb;
  659. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
  660. GFP_KERNEL);
  661. if (tb == NULL)
  662. return NULL;
  663. tb->tb_id = id;
  664. tb->tb_default = -1;
  665. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  666. return tb;
  667. }
  668. /* ------------------------------------------------------------------------ */
  669. #ifdef CONFIG_PROC_FS
  670. struct fib_iter_state {
  671. struct seq_net_private p;
  672. struct fn_zone *zone;
  673. int bucket;
  674. struct hlist_head *hash_head;
  675. struct fib_node *fn;
  676. struct fib_alias *fa;
  677. loff_t pos;
  678. unsigned int genid;
  679. int valid;
  680. };
  681. static struct fib_alias *fib_get_first(struct seq_file *seq)
  682. {
  683. struct fib_iter_state *iter = seq->private;
  684. struct fib_table *main_table;
  685. struct fn_hash *table;
  686. main_table = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
  687. table = (struct fn_hash *)main_table->tb_data;
  688. iter->bucket = 0;
  689. iter->hash_head = NULL;
  690. iter->fn = NULL;
  691. iter->fa = NULL;
  692. iter->pos = 0;
  693. iter->genid = fib_hash_genid;
  694. iter->valid = 1;
  695. for (iter->zone = table->fn_zone_list; iter->zone;
  696. iter->zone = iter->zone->fz_next) {
  697. int maxslot;
  698. if (!iter->zone->fz_nent)
  699. continue;
  700. iter->hash_head = iter->zone->fz_hash;
  701. maxslot = iter->zone->fz_divisor;
  702. for (iter->bucket = 0; iter->bucket < maxslot;
  703. ++iter->bucket, ++iter->hash_head) {
  704. struct hlist_node *node;
  705. struct fib_node *fn;
  706. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  707. struct fib_alias *fa;
  708. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  709. iter->fn = fn;
  710. iter->fa = fa;
  711. goto out;
  712. }
  713. }
  714. }
  715. }
  716. out:
  717. return iter->fa;
  718. }
  719. static struct fib_alias *fib_get_next(struct seq_file *seq)
  720. {
  721. struct fib_iter_state *iter = seq->private;
  722. struct fib_node *fn;
  723. struct fib_alias *fa;
  724. /* Advance FA, if any. */
  725. fn = iter->fn;
  726. fa = iter->fa;
  727. if (fa) {
  728. BUG_ON(!fn);
  729. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  730. iter->fa = fa;
  731. goto out;
  732. }
  733. }
  734. fa = iter->fa = NULL;
  735. /* Advance FN. */
  736. if (fn) {
  737. struct hlist_node *node = &fn->fn_hash;
  738. hlist_for_each_entry_continue(fn, node, fn_hash) {
  739. iter->fn = fn;
  740. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  741. iter->fa = fa;
  742. goto out;
  743. }
  744. }
  745. }
  746. fn = iter->fn = NULL;
  747. /* Advance hash chain. */
  748. if (!iter->zone)
  749. goto out;
  750. for (;;) {
  751. struct hlist_node *node;
  752. int maxslot;
  753. maxslot = iter->zone->fz_divisor;
  754. while (++iter->bucket < maxslot) {
  755. iter->hash_head++;
  756. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  757. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  758. iter->fn = fn;
  759. iter->fa = fa;
  760. goto out;
  761. }
  762. }
  763. }
  764. iter->zone = iter->zone->fz_next;
  765. if (!iter->zone)
  766. goto out;
  767. iter->bucket = 0;
  768. iter->hash_head = iter->zone->fz_hash;
  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. out:
  778. iter->pos++;
  779. return fa;
  780. }
  781. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  782. {
  783. struct fib_iter_state *iter = seq->private;
  784. struct fib_alias *fa;
  785. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  786. fa = iter->fa;
  787. pos -= iter->pos;
  788. } else
  789. fa = fib_get_first(seq);
  790. if (fa)
  791. while (pos && (fa = fib_get_next(seq)))
  792. --pos;
  793. return pos ? NULL : fa;
  794. }
  795. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  796. __acquires(fib_hash_lock)
  797. {
  798. void *v = NULL;
  799. read_lock(&fib_hash_lock);
  800. if (fib_get_table(seq_file_net(seq), 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. int len;
  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. seq_printf(seq,
  857. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
  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, &len);
  863. else
  864. seq_printf(seq,
  865. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
  866. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0, &len);
  867. seq_printf(seq, "%*s\n", 127 - len, "");
  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 */