fib_hash.c 23 KB

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