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