fib_hash.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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/sched.h>
  23. #include <linux/mm.h>
  24. #include <linux/string.h>
  25. #include <linux/socket.h>
  26. #include <linux/sockios.h>
  27. #include <linux/errno.h>
  28. #include <linux/in.h>
  29. #include <linux/inet.h>
  30. #include <linux/inetdevice.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/netlink.h>
  36. #include <linux/init.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 kmem_cache_t *fn_hash_kmem __read_mostly;
  45. static kmem_cache_t *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. return 0;
  389. }
  390. /* Error if we find a perfect match which
  391. * uses the same scope, type, and nexthop
  392. * information.
  393. */
  394. fa_orig = fa;
  395. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  396. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  397. if (fa->fa_tos != tos)
  398. break;
  399. if (fa->fa_info->fib_priority != fi->fib_priority)
  400. break;
  401. if (fa->fa_type == cfg->fc_type &&
  402. fa->fa_scope == cfg->fc_scope &&
  403. fa->fa_info == fi)
  404. goto out;
  405. }
  406. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  407. fa = fa_orig;
  408. }
  409. err = -ENOENT;
  410. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  411. goto out;
  412. err = -ENOBUFS;
  413. new_fa = kmem_cache_alloc(fn_alias_kmem, SLAB_KERNEL);
  414. if (new_fa == NULL)
  415. goto out;
  416. new_f = NULL;
  417. if (!f) {
  418. new_f = kmem_cache_alloc(fn_hash_kmem, SLAB_KERNEL);
  419. if (new_f == NULL)
  420. goto out_free_new_fa;
  421. INIT_HLIST_NODE(&new_f->fn_hash);
  422. INIT_LIST_HEAD(&new_f->fn_alias);
  423. new_f->fn_key = key;
  424. f = new_f;
  425. }
  426. new_fa->fa_info = fi;
  427. new_fa->fa_tos = tos;
  428. new_fa->fa_type = cfg->fc_type;
  429. new_fa->fa_scope = cfg->fc_scope;
  430. new_fa->fa_state = 0;
  431. /*
  432. * Insert new entry to the list.
  433. */
  434. write_lock_bh(&fib_hash_lock);
  435. if (new_f)
  436. fib_insert_node(fz, new_f);
  437. list_add_tail(&new_fa->fa_list,
  438. (fa ? &fa->fa_list : &f->fn_alias));
  439. fib_hash_genid++;
  440. write_unlock_bh(&fib_hash_lock);
  441. if (new_f)
  442. fz->fz_nent++;
  443. rt_cache_flush(-1);
  444. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
  445. &cfg->fc_nlinfo);
  446. return 0;
  447. out_free_new_fa:
  448. kmem_cache_free(fn_alias_kmem, new_fa);
  449. out:
  450. fib_release_info(fi);
  451. return err;
  452. }
  453. static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
  454. {
  455. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  456. struct fib_node *f;
  457. struct fib_alias *fa, *fa_to_delete;
  458. struct fn_zone *fz;
  459. __be32 key;
  460. if (cfg->fc_dst_len > 32)
  461. return -EINVAL;
  462. if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
  463. return -ESRCH;
  464. key = 0;
  465. if (cfg->fc_dst) {
  466. if (cfg->fc_dst & ~FZ_MASK(fz))
  467. return -EINVAL;
  468. key = fz_key(cfg->fc_dst, fz);
  469. }
  470. f = fib_find_node(fz, key);
  471. if (!f)
  472. fa = NULL;
  473. else
  474. fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
  475. if (!fa)
  476. return -ESRCH;
  477. fa_to_delete = NULL;
  478. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  479. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  480. struct fib_info *fi = fa->fa_info;
  481. if (fa->fa_tos != cfg->fc_tos)
  482. break;
  483. if ((!cfg->fc_type ||
  484. fa->fa_type == cfg->fc_type) &&
  485. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  486. fa->fa_scope == cfg->fc_scope) &&
  487. (!cfg->fc_protocol ||
  488. fi->fib_protocol == cfg->fc_protocol) &&
  489. fib_nh_match(cfg, fi) == 0) {
  490. fa_to_delete = fa;
  491. break;
  492. }
  493. }
  494. if (fa_to_delete) {
  495. int kill_fn;
  496. fa = fa_to_delete;
  497. rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
  498. tb->tb_id, &cfg->fc_nlinfo);
  499. kill_fn = 0;
  500. write_lock_bh(&fib_hash_lock);
  501. list_del(&fa->fa_list);
  502. if (list_empty(&f->fn_alias)) {
  503. hlist_del(&f->fn_hash);
  504. kill_fn = 1;
  505. }
  506. fib_hash_genid++;
  507. write_unlock_bh(&fib_hash_lock);
  508. if (fa->fa_state & FA_S_ACCESSED)
  509. rt_cache_flush(-1);
  510. fn_free_alias(fa);
  511. if (kill_fn) {
  512. fn_free_node(f);
  513. fz->fz_nent--;
  514. }
  515. return 0;
  516. }
  517. return -ESRCH;
  518. }
  519. static int fn_flush_list(struct fn_zone *fz, int idx)
  520. {
  521. struct hlist_head *head = &fz->fz_hash[idx];
  522. struct hlist_node *node, *n;
  523. struct fib_node *f;
  524. int found = 0;
  525. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  526. struct fib_alias *fa, *fa_node;
  527. int kill_f;
  528. kill_f = 0;
  529. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  530. struct fib_info *fi = fa->fa_info;
  531. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  532. write_lock_bh(&fib_hash_lock);
  533. list_del(&fa->fa_list);
  534. if (list_empty(&f->fn_alias)) {
  535. hlist_del(&f->fn_hash);
  536. kill_f = 1;
  537. }
  538. fib_hash_genid++;
  539. write_unlock_bh(&fib_hash_lock);
  540. fn_free_alias(fa);
  541. found++;
  542. }
  543. }
  544. if (kill_f) {
  545. fn_free_node(f);
  546. fz->fz_nent--;
  547. }
  548. }
  549. return found;
  550. }
  551. static int fn_hash_flush(struct fib_table *tb)
  552. {
  553. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  554. struct fn_zone *fz;
  555. int found = 0;
  556. for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
  557. int i;
  558. for (i = fz->fz_divisor - 1; i >= 0; i--)
  559. found += fn_flush_list(fz, i);
  560. }
  561. return found;
  562. }
  563. static inline int
  564. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  565. struct fib_table *tb,
  566. struct fn_zone *fz,
  567. struct hlist_head *head)
  568. {
  569. struct hlist_node *node;
  570. struct fib_node *f;
  571. int i, s_i;
  572. s_i = cb->args[4];
  573. i = 0;
  574. hlist_for_each_entry(f, node, head, fn_hash) {
  575. struct fib_alias *fa;
  576. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  577. if (i < s_i)
  578. goto next;
  579. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  580. cb->nlh->nlmsg_seq,
  581. RTM_NEWROUTE,
  582. tb->tb_id,
  583. fa->fa_type,
  584. fa->fa_scope,
  585. f->fn_key,
  586. fz->fz_order,
  587. fa->fa_tos,
  588. fa->fa_info,
  589. NLM_F_MULTI) < 0) {
  590. cb->args[4] = i;
  591. return -1;
  592. }
  593. next:
  594. i++;
  595. }
  596. }
  597. cb->args[4] = i;
  598. return skb->len;
  599. }
  600. static inline int
  601. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  602. struct fib_table *tb,
  603. struct fn_zone *fz)
  604. {
  605. int h, s_h;
  606. s_h = cb->args[3];
  607. for (h=0; h < fz->fz_divisor; h++) {
  608. if (h < s_h) continue;
  609. if (h > s_h)
  610. memset(&cb->args[4], 0,
  611. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  612. if (fz->fz_hash == NULL ||
  613. hlist_empty(&fz->fz_hash[h]))
  614. continue;
  615. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
  616. cb->args[3] = h;
  617. return -1;
  618. }
  619. }
  620. cb->args[3] = h;
  621. return skb->len;
  622. }
  623. static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlink_callback *cb)
  624. {
  625. int m, s_m;
  626. struct fn_zone *fz;
  627. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  628. s_m = cb->args[2];
  629. read_lock(&fib_hash_lock);
  630. for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_next, m++) {
  631. if (m < s_m) continue;
  632. if (m > s_m)
  633. memset(&cb->args[3], 0,
  634. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  635. if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  636. cb->args[2] = m;
  637. read_unlock(&fib_hash_lock);
  638. return -1;
  639. }
  640. }
  641. read_unlock(&fib_hash_lock);
  642. cb->args[2] = m;
  643. return skb->len;
  644. }
  645. #ifdef CONFIG_IP_MULTIPLE_TABLES
  646. struct fib_table * fib_hash_init(u32 id)
  647. #else
  648. struct fib_table * __init fib_hash_init(u32 id)
  649. #endif
  650. {
  651. struct fib_table *tb;
  652. if (fn_hash_kmem == NULL)
  653. fn_hash_kmem = kmem_cache_create("ip_fib_hash",
  654. sizeof(struct fib_node),
  655. 0, SLAB_HWCACHE_ALIGN,
  656. NULL, NULL);
  657. if (fn_alias_kmem == NULL)
  658. fn_alias_kmem = kmem_cache_create("ip_fib_alias",
  659. sizeof(struct fib_alias),
  660. 0, SLAB_HWCACHE_ALIGN,
  661. NULL, NULL);
  662. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
  663. GFP_KERNEL);
  664. if (tb == NULL)
  665. return NULL;
  666. tb->tb_id = id;
  667. tb->tb_lookup = fn_hash_lookup;
  668. tb->tb_insert = fn_hash_insert;
  669. tb->tb_delete = fn_hash_delete;
  670. tb->tb_flush = fn_hash_flush;
  671. tb->tb_select_default = fn_hash_select_default;
  672. tb->tb_dump = fn_hash_dump;
  673. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  674. return tb;
  675. }
  676. /* ------------------------------------------------------------------------ */
  677. #ifdef CONFIG_PROC_FS
  678. struct fib_iter_state {
  679. struct fn_zone *zone;
  680. int bucket;
  681. struct hlist_head *hash_head;
  682. struct fib_node *fn;
  683. struct fib_alias *fa;
  684. loff_t pos;
  685. unsigned int genid;
  686. int valid;
  687. };
  688. static struct fib_alias *fib_get_first(struct seq_file *seq)
  689. {
  690. struct fib_iter_state *iter = seq->private;
  691. struct fn_hash *table = (struct fn_hash *) ip_fib_main_table->tb_data;
  692. iter->bucket = 0;
  693. iter->hash_head = NULL;
  694. iter->fn = NULL;
  695. iter->fa = NULL;
  696. iter->pos = 0;
  697. iter->genid = fib_hash_genid;
  698. iter->valid = 1;
  699. for (iter->zone = table->fn_zone_list; iter->zone;
  700. iter->zone = iter->zone->fz_next) {
  701. int maxslot;
  702. if (!iter->zone->fz_nent)
  703. continue;
  704. iter->hash_head = iter->zone->fz_hash;
  705. maxslot = iter->zone->fz_divisor;
  706. for (iter->bucket = 0; iter->bucket < maxslot;
  707. ++iter->bucket, ++iter->hash_head) {
  708. struct hlist_node *node;
  709. struct fib_node *fn;
  710. hlist_for_each_entry(fn,node,iter->hash_head,fn_hash) {
  711. struct fib_alias *fa;
  712. list_for_each_entry(fa,&fn->fn_alias,fa_list) {
  713. iter->fn = fn;
  714. iter->fa = fa;
  715. goto out;
  716. }
  717. }
  718. }
  719. }
  720. out:
  721. return iter->fa;
  722. }
  723. static struct fib_alias *fib_get_next(struct seq_file *seq)
  724. {
  725. struct fib_iter_state *iter = seq->private;
  726. struct fib_node *fn;
  727. struct fib_alias *fa;
  728. /* Advance FA, if any. */
  729. fn = iter->fn;
  730. fa = iter->fa;
  731. if (fa) {
  732. BUG_ON(!fn);
  733. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  734. iter->fa = fa;
  735. goto out;
  736. }
  737. }
  738. fa = iter->fa = NULL;
  739. /* Advance FN. */
  740. if (fn) {
  741. struct hlist_node *node = &fn->fn_hash;
  742. hlist_for_each_entry_continue(fn, node, fn_hash) {
  743. iter->fn = fn;
  744. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  745. iter->fa = fa;
  746. goto out;
  747. }
  748. }
  749. }
  750. fn = iter->fn = NULL;
  751. /* Advance hash chain. */
  752. if (!iter->zone)
  753. goto out;
  754. for (;;) {
  755. struct hlist_node *node;
  756. int maxslot;
  757. maxslot = iter->zone->fz_divisor;
  758. while (++iter->bucket < maxslot) {
  759. iter->hash_head++;
  760. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  761. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  762. iter->fn = fn;
  763. iter->fa = fa;
  764. goto out;
  765. }
  766. }
  767. }
  768. iter->zone = iter->zone->fz_next;
  769. if (!iter->zone)
  770. goto out;
  771. iter->bucket = 0;
  772. iter->hash_head = iter->zone->fz_hash;
  773. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  774. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  775. iter->fn = fn;
  776. iter->fa = fa;
  777. goto out;
  778. }
  779. }
  780. }
  781. out:
  782. iter->pos++;
  783. return fa;
  784. }
  785. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  786. {
  787. struct fib_iter_state *iter = seq->private;
  788. struct fib_alias *fa;
  789. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  790. fa = iter->fa;
  791. pos -= iter->pos;
  792. } else
  793. fa = fib_get_first(seq);
  794. if (fa)
  795. while (pos && (fa = fib_get_next(seq)))
  796. --pos;
  797. return pos ? NULL : fa;
  798. }
  799. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  800. {
  801. void *v = NULL;
  802. read_lock(&fib_hash_lock);
  803. if (ip_fib_main_table)
  804. v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  805. return v;
  806. }
  807. static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  808. {
  809. ++*pos;
  810. return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
  811. }
  812. static void fib_seq_stop(struct seq_file *seq, void *v)
  813. {
  814. read_unlock(&fib_hash_lock);
  815. }
  816. static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
  817. {
  818. static const unsigned type2flags[RTN_MAX + 1] = {
  819. [7] = RTF_REJECT, [8] = RTF_REJECT,
  820. };
  821. unsigned flags = type2flags[type];
  822. if (fi && fi->fib_nh->nh_gw)
  823. flags |= RTF_GATEWAY;
  824. if (mask == htonl(0xFFFFFFFF))
  825. flags |= RTF_HOST;
  826. flags |= RTF_UP;
  827. return flags;
  828. }
  829. /*
  830. * This outputs /proc/net/route.
  831. *
  832. * It always works in backward compatibility mode.
  833. * The format of the file is not supposed to be changed.
  834. */
  835. static int fib_seq_show(struct seq_file *seq, void *v)
  836. {
  837. struct fib_iter_state *iter;
  838. char bf[128];
  839. __be32 prefix, mask;
  840. unsigned flags;
  841. struct fib_node *f;
  842. struct fib_alias *fa;
  843. struct fib_info *fi;
  844. if (v == SEQ_START_TOKEN) {
  845. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  846. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  847. "\tWindow\tIRTT");
  848. goto out;
  849. }
  850. iter = seq->private;
  851. f = iter->fn;
  852. fa = iter->fa;
  853. fi = fa->fa_info;
  854. prefix = f->fn_key;
  855. mask = FZ_MASK(iter->zone);
  856. flags = fib_flag_trans(fa->fa_type, mask, fi);
  857. if (fi)
  858. snprintf(bf, sizeof(bf),
  859. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  860. fi->fib_dev ? fi->fib_dev->name : "*", prefix,
  861. fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
  862. mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
  863. fi->fib_window,
  864. fi->fib_rtt >> 3);
  865. else
  866. snprintf(bf, sizeof(bf),
  867. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
  868. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0);
  869. seq_printf(seq, "%-127s\n", bf);
  870. out:
  871. return 0;
  872. }
  873. static struct seq_operations fib_seq_ops = {
  874. .start = fib_seq_start,
  875. .next = fib_seq_next,
  876. .stop = fib_seq_stop,
  877. .show = fib_seq_show,
  878. };
  879. static int fib_seq_open(struct inode *inode, struct file *file)
  880. {
  881. struct seq_file *seq;
  882. int rc = -ENOMEM;
  883. struct fib_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  884. if (!s)
  885. goto out;
  886. rc = seq_open(file, &fib_seq_ops);
  887. if (rc)
  888. goto out_kfree;
  889. seq = file->private_data;
  890. seq->private = s;
  891. out:
  892. return rc;
  893. out_kfree:
  894. kfree(s);
  895. goto out;
  896. }
  897. static struct file_operations fib_seq_fops = {
  898. .owner = THIS_MODULE,
  899. .open = fib_seq_open,
  900. .read = seq_read,
  901. .llseek = seq_lseek,
  902. .release = seq_release_private,
  903. };
  904. int __init fib_proc_init(void)
  905. {
  906. if (!proc_net_fops_create("route", S_IRUGO, &fib_seq_fops))
  907. return -ENOMEM;
  908. return 0;
  909. }
  910. void __init fib_proc_exit(void)
  911. {
  912. proc_net_remove("route");
  913. }
  914. #endif /* CONFIG_PROC_FS */