fib_hash.c 23 KB

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