fib_hash.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 kzalloc(size, GFP_KERNEL);
  90. } else {
  91. return (struct hlist_head *)
  92. __get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(size));
  93. }
  94. }
  95. /* The fib hash lock must be held when this is called. */
  96. static inline void fn_rebuild_zone(struct fn_zone *fz,
  97. struct hlist_head *old_ht,
  98. int old_divisor)
  99. {
  100. int i;
  101. for (i = 0; i < old_divisor; i++) {
  102. struct hlist_node *node, *n;
  103. struct fib_node *f;
  104. hlist_for_each_entry_safe(f, node, n, &old_ht[i], fn_hash) {
  105. struct hlist_head *new_head;
  106. hlist_del(&f->fn_hash);
  107. new_head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  108. hlist_add_head(&f->fn_hash, new_head);
  109. }
  110. }
  111. }
  112. static void fz_hash_free(struct hlist_head *hash, int divisor)
  113. {
  114. unsigned long size = divisor * sizeof(struct hlist_head);
  115. if (size <= PAGE_SIZE)
  116. kfree(hash);
  117. else
  118. free_pages((unsigned long)hash, get_order(size));
  119. }
  120. static void fn_rehash_zone(struct fn_zone *fz)
  121. {
  122. struct hlist_head *ht, *old_ht;
  123. int old_divisor, new_divisor;
  124. u32 new_hashmask;
  125. old_divisor = fz->fz_divisor;
  126. switch (old_divisor) {
  127. case 16:
  128. new_divisor = 256;
  129. break;
  130. case 256:
  131. new_divisor = 1024;
  132. break;
  133. default:
  134. if ((old_divisor << 1) > FZ_MAX_DIVISOR) {
  135. printk(KERN_CRIT "route.c: bad divisor %d!\n", old_divisor);
  136. return;
  137. }
  138. new_divisor = (old_divisor << 1);
  139. break;
  140. }
  141. new_hashmask = (new_divisor - 1);
  142. #if RT_CACHE_DEBUG >= 2
  143. printk(KERN_DEBUG "fn_rehash_zone: hash for zone %d grows from %d\n",
  144. fz->fz_order, old_divisor);
  145. #endif
  146. ht = fz_hash_alloc(new_divisor);
  147. if (ht) {
  148. write_lock_bh(&fib_hash_lock);
  149. old_ht = fz->fz_hash;
  150. fz->fz_hash = ht;
  151. fz->fz_hashmask = new_hashmask;
  152. fz->fz_divisor = new_divisor;
  153. fn_rebuild_zone(fz, old_ht, old_divisor);
  154. fib_hash_genid++;
  155. write_unlock_bh(&fib_hash_lock);
  156. fz_hash_free(old_ht, old_divisor);
  157. }
  158. }
  159. static inline void fn_free_node(struct fib_node * f)
  160. {
  161. kmem_cache_free(fn_hash_kmem, f);
  162. }
  163. static inline void fn_free_alias(struct fib_alias *fa)
  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. 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 void
  236. fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
  237. {
  238. int order, last_idx;
  239. struct hlist_node *node;
  240. struct fib_node *f;
  241. struct fib_info *fi = NULL;
  242. struct fib_info *last_resort;
  243. struct fn_hash *t = (struct fn_hash*)tb->tb_data;
  244. struct fn_zone *fz = t->fn_zones[0];
  245. if (fz == NULL)
  246. return;
  247. last_idx = -1;
  248. last_resort = NULL;
  249. order = -1;
  250. read_lock(&fib_hash_lock);
  251. hlist_for_each_entry(f, node, &fz->fz_hash[0], fn_hash) {
  252. struct fib_alias *fa;
  253. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  254. struct fib_info *next_fi = fa->fa_info;
  255. if (fa->fa_scope != res->scope ||
  256. fa->fa_type != RTN_UNICAST)
  257. continue;
  258. if (next_fi->fib_priority > res->fi->fib_priority)
  259. break;
  260. if (!next_fi->fib_nh[0].nh_gw ||
  261. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  262. continue;
  263. fa->fa_state |= FA_S_ACCESSED;
  264. if (fi == NULL) {
  265. if (next_fi != res->fi)
  266. break;
  267. } else if (!fib_detect_death(fi, order, &last_resort,
  268. &last_idx, tb->tb_default)) {
  269. fib_result_assign(res, fi);
  270. tb->tb_default = order;
  271. goto out;
  272. }
  273. fi = next_fi;
  274. order++;
  275. }
  276. }
  277. if (order <= 0 || fi == NULL) {
  278. tb->tb_default = -1;
  279. goto out;
  280. }
  281. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  282. tb->tb_default)) {
  283. fib_result_assign(res, fi);
  284. tb->tb_default = order;
  285. goto out;
  286. }
  287. if (last_idx >= 0)
  288. fib_result_assign(res, last_resort);
  289. tb->tb_default = last_idx;
  290. out:
  291. read_unlock(&fib_hash_lock);
  292. }
  293. /* Insert node F to FZ. */
  294. static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
  295. {
  296. struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  297. hlist_add_head(&f->fn_hash, head);
  298. }
  299. /* Return the node in FZ matching KEY. */
  300. static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
  301. {
  302. struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
  303. struct hlist_node *node;
  304. struct fib_node *f;
  305. hlist_for_each_entry(f, node, head, fn_hash) {
  306. if (f->fn_key == key)
  307. return f;
  308. }
  309. return NULL;
  310. }
  311. static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
  312. {
  313. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  314. struct fib_node *new_f, *f;
  315. struct fib_alias *fa, *new_fa;
  316. struct fn_zone *fz;
  317. struct fib_info *fi;
  318. u8 tos = cfg->fc_tos;
  319. __be32 key;
  320. int err;
  321. if (cfg->fc_dst_len > 32)
  322. return -EINVAL;
  323. fz = table->fn_zones[cfg->fc_dst_len];
  324. if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
  325. return -ENOBUFS;
  326. key = 0;
  327. if (cfg->fc_dst) {
  328. if (cfg->fc_dst & ~FZ_MASK(fz))
  329. return -EINVAL;
  330. key = fz_key(cfg->fc_dst, fz);
  331. }
  332. fi = fib_create_info(cfg);
  333. if (IS_ERR(fi))
  334. return PTR_ERR(fi);
  335. if (fz->fz_nent > (fz->fz_divisor<<1) &&
  336. fz->fz_divisor < FZ_MAX_DIVISOR &&
  337. (cfg->fc_dst_len == 32 ||
  338. (1 << cfg->fc_dst_len) > fz->fz_divisor))
  339. fn_rehash_zone(fz);
  340. f = fib_find_node(fz, key);
  341. if (!f)
  342. fa = NULL;
  343. else
  344. fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
  345. /* Now fa, if non-NULL, points to the first fib alias
  346. * with the same keys [prefix,tos,priority], if such key already
  347. * exists or to the node before which we will insert new one.
  348. *
  349. * If fa is NULL, we will need to allocate a new one and
  350. * insert to the head of f.
  351. *
  352. * If f is NULL, no fib node matched the destination key
  353. * and we need to allocate a new one of those as well.
  354. */
  355. if (fa && fa->fa_tos == tos &&
  356. fa->fa_info->fib_priority == fi->fib_priority) {
  357. struct fib_alias *fa_orig;
  358. err = -EEXIST;
  359. if (cfg->fc_nlflags & NLM_F_EXCL)
  360. goto out;
  361. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  362. struct fib_info *fi_drop;
  363. u8 state;
  364. if (fi->fib_treeref > 1)
  365. goto out;
  366. write_lock_bh(&fib_hash_lock);
  367. fi_drop = fa->fa_info;
  368. fa->fa_info = fi;
  369. fa->fa_type = cfg->fc_type;
  370. fa->fa_scope = cfg->fc_scope;
  371. state = fa->fa_state;
  372. fa->fa_state &= ~FA_S_ACCESSED;
  373. fib_hash_genid++;
  374. write_unlock_bh(&fib_hash_lock);
  375. fib_release_info(fi_drop);
  376. if (state & FA_S_ACCESSED)
  377. rt_cache_flush(-1);
  378. rtmsg_fib(RTM_NEWROUTE, key, fa, cfg->fc_dst_len, tb->tb_id,
  379. &cfg->fc_nlinfo, NLM_F_REPLACE);
  380. return 0;
  381. }
  382. /* Error if we find a perfect match which
  383. * uses the same scope, type, and nexthop
  384. * information.
  385. */
  386. fa_orig = fa;
  387. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  388. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  389. if (fa->fa_tos != tos)
  390. break;
  391. if (fa->fa_info->fib_priority != fi->fib_priority)
  392. break;
  393. if (fa->fa_type == cfg->fc_type &&
  394. fa->fa_scope == cfg->fc_scope &&
  395. fa->fa_info == fi)
  396. goto out;
  397. }
  398. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  399. fa = fa_orig;
  400. }
  401. err = -ENOENT;
  402. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  403. goto out;
  404. err = -ENOBUFS;
  405. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  406. if (new_fa == NULL)
  407. goto out;
  408. new_f = NULL;
  409. if (!f) {
  410. new_f = kmem_cache_alloc(fn_hash_kmem, GFP_KERNEL);
  411. if (new_f == NULL)
  412. goto out_free_new_fa;
  413. INIT_HLIST_NODE(&new_f->fn_hash);
  414. INIT_LIST_HEAD(&new_f->fn_alias);
  415. new_f->fn_key = key;
  416. f = new_f;
  417. }
  418. new_fa->fa_info = fi;
  419. new_fa->fa_tos = tos;
  420. new_fa->fa_type = cfg->fc_type;
  421. new_fa->fa_scope = cfg->fc_scope;
  422. new_fa->fa_state = 0;
  423. /*
  424. * Insert new entry to the list.
  425. */
  426. write_lock_bh(&fib_hash_lock);
  427. if (new_f)
  428. fib_insert_node(fz, new_f);
  429. list_add_tail(&new_fa->fa_list,
  430. (fa ? &fa->fa_list : &f->fn_alias));
  431. fib_hash_genid++;
  432. write_unlock_bh(&fib_hash_lock);
  433. if (new_f)
  434. fz->fz_nent++;
  435. rt_cache_flush(-1);
  436. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
  437. &cfg->fc_nlinfo, 0);
  438. return 0;
  439. out_free_new_fa:
  440. kmem_cache_free(fn_alias_kmem, new_fa);
  441. out:
  442. fib_release_info(fi);
  443. return err;
  444. }
  445. static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
  446. {
  447. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  448. struct fib_node *f;
  449. struct fib_alias *fa, *fa_to_delete;
  450. struct fn_zone *fz;
  451. __be32 key;
  452. if (cfg->fc_dst_len > 32)
  453. return -EINVAL;
  454. if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
  455. return -ESRCH;
  456. key = 0;
  457. if (cfg->fc_dst) {
  458. if (cfg->fc_dst & ~FZ_MASK(fz))
  459. return -EINVAL;
  460. key = fz_key(cfg->fc_dst, fz);
  461. }
  462. f = fib_find_node(fz, key);
  463. if (!f)
  464. fa = NULL;
  465. else
  466. fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
  467. if (!fa)
  468. return -ESRCH;
  469. fa_to_delete = NULL;
  470. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  471. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  472. struct fib_info *fi = fa->fa_info;
  473. if (fa->fa_tos != cfg->fc_tos)
  474. break;
  475. if ((!cfg->fc_type ||
  476. fa->fa_type == cfg->fc_type) &&
  477. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  478. fa->fa_scope == cfg->fc_scope) &&
  479. (!cfg->fc_protocol ||
  480. fi->fib_protocol == cfg->fc_protocol) &&
  481. fib_nh_match(cfg, fi) == 0) {
  482. fa_to_delete = fa;
  483. break;
  484. }
  485. }
  486. if (fa_to_delete) {
  487. int kill_fn;
  488. fa = fa_to_delete;
  489. rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
  490. tb->tb_id, &cfg->fc_nlinfo, 0);
  491. kill_fn = 0;
  492. write_lock_bh(&fib_hash_lock);
  493. list_del(&fa->fa_list);
  494. if (list_empty(&f->fn_alias)) {
  495. hlist_del(&f->fn_hash);
  496. kill_fn = 1;
  497. }
  498. fib_hash_genid++;
  499. write_unlock_bh(&fib_hash_lock);
  500. if (fa->fa_state & FA_S_ACCESSED)
  501. rt_cache_flush(-1);
  502. fn_free_alias(fa);
  503. if (kill_fn) {
  504. fn_free_node(f);
  505. fz->fz_nent--;
  506. }
  507. return 0;
  508. }
  509. return -ESRCH;
  510. }
  511. static int fn_flush_list(struct fn_zone *fz, int idx)
  512. {
  513. struct hlist_head *head = &fz->fz_hash[idx];
  514. struct hlist_node *node, *n;
  515. struct fib_node *f;
  516. int found = 0;
  517. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  518. struct fib_alias *fa, *fa_node;
  519. int kill_f;
  520. kill_f = 0;
  521. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  522. struct fib_info *fi = fa->fa_info;
  523. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  524. write_lock_bh(&fib_hash_lock);
  525. list_del(&fa->fa_list);
  526. if (list_empty(&f->fn_alias)) {
  527. hlist_del(&f->fn_hash);
  528. kill_f = 1;
  529. }
  530. fib_hash_genid++;
  531. write_unlock_bh(&fib_hash_lock);
  532. fn_free_alias(fa);
  533. found++;
  534. }
  535. }
  536. if (kill_f) {
  537. fn_free_node(f);
  538. fz->fz_nent--;
  539. }
  540. }
  541. return found;
  542. }
  543. static int fn_hash_flush(struct fib_table *tb)
  544. {
  545. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  546. struct fn_zone *fz;
  547. int found = 0;
  548. for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
  549. int i;
  550. for (i = fz->fz_divisor - 1; i >= 0; i--)
  551. found += fn_flush_list(fz, i);
  552. }
  553. return found;
  554. }
  555. static inline int
  556. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  557. struct fib_table *tb,
  558. struct fn_zone *fz,
  559. struct hlist_head *head)
  560. {
  561. struct hlist_node *node;
  562. struct fib_node *f;
  563. int i, s_i;
  564. s_i = cb->args[4];
  565. i = 0;
  566. hlist_for_each_entry(f, node, head, fn_hash) {
  567. struct fib_alias *fa;
  568. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  569. if (i < s_i)
  570. goto next;
  571. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  572. cb->nlh->nlmsg_seq,
  573. RTM_NEWROUTE,
  574. tb->tb_id,
  575. fa->fa_type,
  576. fa->fa_scope,
  577. f->fn_key,
  578. fz->fz_order,
  579. fa->fa_tos,
  580. fa->fa_info,
  581. NLM_F_MULTI) < 0) {
  582. cb->args[4] = i;
  583. return -1;
  584. }
  585. next:
  586. i++;
  587. }
  588. }
  589. cb->args[4] = i;
  590. return skb->len;
  591. }
  592. static inline int
  593. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  594. struct fib_table *tb,
  595. struct fn_zone *fz)
  596. {
  597. int h, s_h;
  598. if (fz->fz_hash == NULL)
  599. return skb->len;
  600. s_h = cb->args[3];
  601. for (h = s_h; h < fz->fz_divisor; h++) {
  602. if (hlist_empty(&fz->fz_hash[h]))
  603. continue;
  604. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h]) < 0) {
  605. cb->args[3] = h;
  606. return -1;
  607. }
  608. memset(&cb->args[4], 0,
  609. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  610. }
  611. cb->args[3] = h;
  612. return skb->len;
  613. }
  614. static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
  615. {
  616. int m, s_m;
  617. struct fn_zone *fz;
  618. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  619. s_m = cb->args[2];
  620. read_lock(&fib_hash_lock);
  621. for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
  622. if (m < s_m) continue;
  623. if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  624. cb->args[2] = m;
  625. read_unlock(&fib_hash_lock);
  626. return -1;
  627. }
  628. memset(&cb->args[3], 0,
  629. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  630. }
  631. read_unlock(&fib_hash_lock);
  632. cb->args[2] = m;
  633. return skb->len;
  634. }
  635. void __init fib_hash_init(void)
  636. {
  637. fn_hash_kmem = kmem_cache_create("ip_fib_hash", sizeof(struct fib_node),
  638. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  639. fn_alias_kmem = kmem_cache_create("ip_fib_alias", sizeof(struct fib_alias),
  640. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  641. }
  642. struct fib_table *fib_hash_table(u32 id)
  643. {
  644. struct fib_table *tb;
  645. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
  646. GFP_KERNEL);
  647. if (tb == NULL)
  648. return NULL;
  649. tb->tb_id = id;
  650. tb->tb_default = -1;
  651. tb->tb_lookup = fn_hash_lookup;
  652. tb->tb_insert = fn_hash_insert;
  653. tb->tb_delete = fn_hash_delete;
  654. tb->tb_flush = fn_hash_flush;
  655. tb->tb_select_default = fn_hash_select_default;
  656. tb->tb_dump = fn_hash_dump;
  657. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  658. return tb;
  659. }
  660. /* ------------------------------------------------------------------------ */
  661. #ifdef CONFIG_PROC_FS
  662. struct fib_iter_state {
  663. struct seq_net_private p;
  664. struct fn_zone *zone;
  665. int bucket;
  666. struct hlist_head *hash_head;
  667. struct fib_node *fn;
  668. struct fib_alias *fa;
  669. loff_t pos;
  670. unsigned int genid;
  671. int valid;
  672. };
  673. static struct fib_alias *fib_get_first(struct seq_file *seq)
  674. {
  675. struct fib_iter_state *iter = seq->private;
  676. struct fib_table *main_table;
  677. struct fn_hash *table;
  678. main_table = fib_get_table(iter->p.net, RT_TABLE_MAIN);
  679. table = (struct fn_hash *)main_table->tb_data;
  680. iter->bucket = 0;
  681. iter->hash_head = NULL;
  682. iter->fn = NULL;
  683. iter->fa = NULL;
  684. iter->pos = 0;
  685. iter->genid = fib_hash_genid;
  686. iter->valid = 1;
  687. for (iter->zone = table->fn_zone_list; iter->zone;
  688. iter->zone = iter->zone->fz_next) {
  689. int maxslot;
  690. if (!iter->zone->fz_nent)
  691. continue;
  692. iter->hash_head = iter->zone->fz_hash;
  693. maxslot = iter->zone->fz_divisor;
  694. for (iter->bucket = 0; iter->bucket < maxslot;
  695. ++iter->bucket, ++iter->hash_head) {
  696. struct hlist_node *node;
  697. struct fib_node *fn;
  698. hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
  699. struct fib_alias *fa;
  700. list_for_each_entry(fa,&fn->fn_alias,fa_list) {
  701. iter->fn = fn;
  702. iter->fa = fa;
  703. goto out;
  704. }
  705. }
  706. }
  707. }
  708. out:
  709. return iter->fa;
  710. }
  711. static struct fib_alias *fib_get_next(struct seq_file *seq)
  712. {
  713. struct fib_iter_state *iter = seq->private;
  714. struct fib_node *fn;
  715. struct fib_alias *fa;
  716. /* Advance FA, if any. */
  717. fn = iter->fn;
  718. fa = iter->fa;
  719. if (fa) {
  720. BUG_ON(!fn);
  721. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  722. iter->fa = fa;
  723. goto out;
  724. }
  725. }
  726. fa = iter->fa = NULL;
  727. /* Advance FN. */
  728. if (fn) {
  729. struct hlist_node *node = &fn->fn_hash;
  730. hlist_for_each_entry_continue(fn, node, fn_hash) {
  731. iter->fn = fn;
  732. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  733. iter->fa = fa;
  734. goto out;
  735. }
  736. }
  737. }
  738. fn = iter->fn = NULL;
  739. /* Advance hash chain. */
  740. if (!iter->zone)
  741. goto out;
  742. for (;;) {
  743. struct hlist_node *node;
  744. int maxslot;
  745. maxslot = iter->zone->fz_divisor;
  746. while (++iter->bucket < maxslot) {
  747. iter->hash_head++;
  748. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  749. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  750. iter->fn = fn;
  751. iter->fa = fa;
  752. goto out;
  753. }
  754. }
  755. }
  756. iter->zone = iter->zone->fz_next;
  757. if (!iter->zone)
  758. goto out;
  759. iter->bucket = 0;
  760. iter->hash_head = iter->zone->fz_hash;
  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. out:
  770. iter->pos++;
  771. return fa;
  772. }
  773. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  774. {
  775. struct fib_iter_state *iter = seq->private;
  776. struct fib_alias *fa;
  777. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  778. fa = iter->fa;
  779. pos -= iter->pos;
  780. } else
  781. fa = fib_get_first(seq);
  782. if (fa)
  783. while (pos && (fa = fib_get_next(seq)))
  784. --pos;
  785. return pos ? NULL : fa;
  786. }
  787. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  788. __acquires(fib_hash_lock)
  789. {
  790. struct fib_iter_state *iter = seq->private;
  791. void *v = NULL;
  792. read_lock(&fib_hash_lock);
  793. if (fib_get_table(iter->p.net, RT_TABLE_MAIN))
  794. v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  795. return v;
  796. }
  797. static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  798. {
  799. ++*pos;
  800. return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
  801. }
  802. static void fib_seq_stop(struct seq_file *seq, void *v)
  803. __releases(fib_hash_lock)
  804. {
  805. read_unlock(&fib_hash_lock);
  806. }
  807. static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
  808. {
  809. static const unsigned type2flags[RTN_MAX + 1] = {
  810. [7] = RTF_REJECT, [8] = RTF_REJECT,
  811. };
  812. unsigned flags = type2flags[type];
  813. if (fi && fi->fib_nh->nh_gw)
  814. flags |= RTF_GATEWAY;
  815. if (mask == htonl(0xFFFFFFFF))
  816. flags |= RTF_HOST;
  817. flags |= RTF_UP;
  818. return flags;
  819. }
  820. /*
  821. * This outputs /proc/net/route.
  822. *
  823. * It always works in backward compatibility mode.
  824. * The format of the file is not supposed to be changed.
  825. */
  826. static int fib_seq_show(struct seq_file *seq, void *v)
  827. {
  828. struct fib_iter_state *iter;
  829. char bf[128];
  830. __be32 prefix, mask;
  831. unsigned flags;
  832. struct fib_node *f;
  833. struct fib_alias *fa;
  834. struct fib_info *fi;
  835. if (v == SEQ_START_TOKEN) {
  836. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  837. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  838. "\tWindow\tIRTT");
  839. goto out;
  840. }
  841. iter = seq->private;
  842. f = iter->fn;
  843. fa = iter->fa;
  844. fi = fa->fa_info;
  845. prefix = f->fn_key;
  846. mask = FZ_MASK(iter->zone);
  847. flags = fib_flag_trans(fa->fa_type, mask, fi);
  848. if (fi)
  849. snprintf(bf, sizeof(bf),
  850. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  851. fi->fib_dev ? fi->fib_dev->name : "*", prefix,
  852. fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
  853. mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
  854. fi->fib_window,
  855. fi->fib_rtt >> 3);
  856. else
  857. snprintf(bf, sizeof(bf),
  858. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  859. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
  860. seq_printf(seq, "%-127s\n", bf);
  861. out:
  862. return 0;
  863. }
  864. static const struct seq_operations fib_seq_ops = {
  865. .start = fib_seq_start,
  866. .next = fib_seq_next,
  867. .stop = fib_seq_stop,
  868. .show = fib_seq_show,
  869. };
  870. static int fib_seq_open(struct inode *inode, struct file *file)
  871. {
  872. return seq_open_net(inode, file, &fib_seq_ops,
  873. sizeof(struct fib_iter_state));
  874. }
  875. static const struct file_operations fib_seq_fops = {
  876. .owner = THIS_MODULE,
  877. .open = fib_seq_open,
  878. .read = seq_read,
  879. .llseek = seq_lseek,
  880. .release = seq_release_net,
  881. };
  882. int __net_init fib_proc_init(struct net *net)
  883. {
  884. if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_seq_fops))
  885. return -ENOMEM;
  886. return 0;
  887. }
  888. void __net_exit fib_proc_exit(struct net *net)
  889. {
  890. proc_net_remove(net, "route");
  891. }
  892. #endif /* CONFIG_PROC_FS */