fib_hash.c 23 KB

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