fib_hash.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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("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. 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. fz->fz_order = z;
  186. fz->fz_mask = inet_make_mask(z);
  187. /* Find the first not empty zone with more specific mask */
  188. for (i=z+1; i<=32; i++)
  189. if (table->fn_zones[i])
  190. break;
  191. write_lock_bh(&fib_hash_lock);
  192. if (i>32) {
  193. /* No more specific masks, we are the first. */
  194. fz->fz_next = table->fn_zone_list;
  195. table->fn_zone_list = fz;
  196. } else {
  197. fz->fz_next = table->fn_zones[i]->fz_next;
  198. table->fn_zones[i]->fz_next = fz;
  199. }
  200. table->fn_zones[z] = fz;
  201. fib_hash_genid++;
  202. write_unlock_bh(&fib_hash_lock);
  203. return fz;
  204. }
  205. static int
  206. fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
  207. {
  208. int err;
  209. struct fn_zone *fz;
  210. struct fn_hash *t = (struct fn_hash*)tb->tb_data;
  211. read_lock(&fib_hash_lock);
  212. for (fz = t->fn_zone_list; fz; fz = fz->fz_next) {
  213. struct hlist_head *head;
  214. struct hlist_node *node;
  215. struct fib_node *f;
  216. __be32 k = fz_key(flp->fl4_dst, fz);
  217. head = &fz->fz_hash[fn_hash(k, fz)];
  218. hlist_for_each_entry(f, node, head, fn_hash) {
  219. if (f->fn_key != k)
  220. continue;
  221. err = fib_semantic_match(&f->fn_alias,
  222. flp, res,
  223. f->fn_key, fz->fz_mask,
  224. fz->fz_order);
  225. if (err <= 0)
  226. goto out;
  227. }
  228. }
  229. err = 1;
  230. out:
  231. read_unlock(&fib_hash_lock);
  232. return err;
  233. }
  234. static int fn_hash_last_dflt=-1;
  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, fn_hash_last_dflt)) {
  269. if (res->fi)
  270. fib_info_put(res->fi);
  271. res->fi = fi;
  272. atomic_inc(&fi->fib_clntref);
  273. fn_hash_last_dflt = order;
  274. goto out;
  275. }
  276. fi = next_fi;
  277. order++;
  278. }
  279. }
  280. if (order <= 0 || fi == NULL) {
  281. fn_hash_last_dflt = -1;
  282. goto out;
  283. }
  284. if (!fib_detect_death(fi, order, &last_resort, &last_idx, fn_hash_last_dflt)) {
  285. if (res->fi)
  286. fib_info_put(res->fi);
  287. res->fi = fi;
  288. atomic_inc(&fi->fib_clntref);
  289. fn_hash_last_dflt = order;
  290. goto out;
  291. }
  292. if (last_idx >= 0) {
  293. if (res->fi)
  294. fib_info_put(res->fi);
  295. res->fi = last_resort;
  296. if (last_resort)
  297. atomic_inc(&last_resort->fib_clntref);
  298. }
  299. fn_hash_last_dflt = last_idx;
  300. out:
  301. read_unlock(&fib_hash_lock);
  302. }
  303. /* Insert node F to FZ. */
  304. static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
  305. {
  306. struct hlist_head *head = &fz->fz_hash[fn_hash(f->fn_key, fz)];
  307. hlist_add_head(&f->fn_hash, head);
  308. }
  309. /* Return the node in FZ matching KEY. */
  310. static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
  311. {
  312. struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
  313. struct hlist_node *node;
  314. struct fib_node *f;
  315. hlist_for_each_entry(f, node, head, fn_hash) {
  316. if (f->fn_key == key)
  317. return f;
  318. }
  319. return NULL;
  320. }
  321. static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
  322. {
  323. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  324. struct fib_node *new_f, *f;
  325. struct fib_alias *fa, *new_fa;
  326. struct fn_zone *fz;
  327. struct fib_info *fi;
  328. u8 tos = cfg->fc_tos;
  329. __be32 key;
  330. int err;
  331. if (cfg->fc_dst_len > 32)
  332. return -EINVAL;
  333. fz = table->fn_zones[cfg->fc_dst_len];
  334. if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
  335. return -ENOBUFS;
  336. key = 0;
  337. if (cfg->fc_dst) {
  338. if (cfg->fc_dst & ~FZ_MASK(fz))
  339. return -EINVAL;
  340. key = fz_key(cfg->fc_dst, fz);
  341. }
  342. fi = fib_create_info(cfg);
  343. if (IS_ERR(fi))
  344. return PTR_ERR(fi);
  345. if (fz->fz_nent > (fz->fz_divisor<<1) &&
  346. fz->fz_divisor < FZ_MAX_DIVISOR &&
  347. (cfg->fc_dst_len == 32 ||
  348. (1 << cfg->fc_dst_len) > fz->fz_divisor))
  349. fn_rehash_zone(fz);
  350. f = fib_find_node(fz, key);
  351. if (!f)
  352. fa = NULL;
  353. else
  354. fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
  355. /* Now fa, if non-NULL, points to the first fib alias
  356. * with the same keys [prefix,tos,priority], if such key already
  357. * exists or to the node before which we will insert new one.
  358. *
  359. * If fa is NULL, we will need to allocate a new one and
  360. * insert to the head of f.
  361. *
  362. * If f is NULL, no fib node matched the destination key
  363. * and we need to allocate a new one of those as well.
  364. */
  365. if (fa && fa->fa_tos == tos &&
  366. fa->fa_info->fib_priority == fi->fib_priority) {
  367. struct fib_alias *fa_orig;
  368. err = -EEXIST;
  369. if (cfg->fc_nlflags & NLM_F_EXCL)
  370. goto out;
  371. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  372. struct fib_info *fi_drop;
  373. u8 state;
  374. if (fi->fib_treeref > 1)
  375. goto out;
  376. write_lock_bh(&fib_hash_lock);
  377. fi_drop = fa->fa_info;
  378. fa->fa_info = fi;
  379. fa->fa_type = cfg->fc_type;
  380. fa->fa_scope = cfg->fc_scope;
  381. state = fa->fa_state;
  382. fa->fa_state &= ~FA_S_ACCESSED;
  383. fib_hash_genid++;
  384. write_unlock_bh(&fib_hash_lock);
  385. fib_release_info(fi_drop);
  386. if (state & FA_S_ACCESSED)
  387. rt_cache_flush(-1);
  388. rtmsg_fib(RTM_NEWROUTE, key, fa, cfg->fc_dst_len, tb->tb_id,
  389. &cfg->fc_nlinfo, NLM_F_REPLACE);
  390. return 0;
  391. }
  392. /* Error if we find a perfect match which
  393. * uses the same scope, type, and nexthop
  394. * information.
  395. */
  396. fa_orig = fa;
  397. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  398. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  399. if (fa->fa_tos != tos)
  400. break;
  401. if (fa->fa_info->fib_priority != fi->fib_priority)
  402. break;
  403. if (fa->fa_type == cfg->fc_type &&
  404. fa->fa_scope == cfg->fc_scope &&
  405. fa->fa_info == fi)
  406. goto out;
  407. }
  408. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  409. fa = fa_orig;
  410. }
  411. err = -ENOENT;
  412. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  413. goto out;
  414. err = -ENOBUFS;
  415. new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  416. if (new_fa == NULL)
  417. goto out;
  418. new_f = NULL;
  419. if (!f) {
  420. new_f = kmem_cache_alloc(fn_hash_kmem, GFP_KERNEL);
  421. if (new_f == NULL)
  422. goto out_free_new_fa;
  423. INIT_HLIST_NODE(&new_f->fn_hash);
  424. INIT_LIST_HEAD(&new_f->fn_alias);
  425. new_f->fn_key = key;
  426. f = new_f;
  427. }
  428. new_fa->fa_info = fi;
  429. new_fa->fa_tos = tos;
  430. new_fa->fa_type = cfg->fc_type;
  431. new_fa->fa_scope = cfg->fc_scope;
  432. new_fa->fa_state = 0;
  433. /*
  434. * Insert new entry to the list.
  435. */
  436. write_lock_bh(&fib_hash_lock);
  437. if (new_f)
  438. fib_insert_node(fz, new_f);
  439. list_add_tail(&new_fa->fa_list,
  440. (fa ? &fa->fa_list : &f->fn_alias));
  441. fib_hash_genid++;
  442. write_unlock_bh(&fib_hash_lock);
  443. if (new_f)
  444. fz->fz_nent++;
  445. rt_cache_flush(-1);
  446. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
  447. &cfg->fc_nlinfo, 0);
  448. return 0;
  449. out_free_new_fa:
  450. kmem_cache_free(fn_alias_kmem, new_fa);
  451. out:
  452. fib_release_info(fi);
  453. return err;
  454. }
  455. static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
  456. {
  457. struct fn_hash *table = (struct fn_hash*)tb->tb_data;
  458. struct fib_node *f;
  459. struct fib_alias *fa, *fa_to_delete;
  460. struct fn_zone *fz;
  461. __be32 key;
  462. if (cfg->fc_dst_len > 32)
  463. return -EINVAL;
  464. if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
  465. return -ESRCH;
  466. key = 0;
  467. if (cfg->fc_dst) {
  468. if (cfg->fc_dst & ~FZ_MASK(fz))
  469. return -EINVAL;
  470. key = fz_key(cfg->fc_dst, fz);
  471. }
  472. f = fib_find_node(fz, key);
  473. if (!f)
  474. fa = NULL;
  475. else
  476. fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
  477. if (!fa)
  478. return -ESRCH;
  479. fa_to_delete = NULL;
  480. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  481. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  482. struct fib_info *fi = fa->fa_info;
  483. if (fa->fa_tos != cfg->fc_tos)
  484. break;
  485. if ((!cfg->fc_type ||
  486. fa->fa_type == cfg->fc_type) &&
  487. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  488. fa->fa_scope == cfg->fc_scope) &&
  489. (!cfg->fc_protocol ||
  490. fi->fib_protocol == cfg->fc_protocol) &&
  491. fib_nh_match(cfg, fi) == 0) {
  492. fa_to_delete = fa;
  493. break;
  494. }
  495. }
  496. if (fa_to_delete) {
  497. int kill_fn;
  498. fa = fa_to_delete;
  499. rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
  500. tb->tb_id, &cfg->fc_nlinfo, 0);
  501. kill_fn = 0;
  502. write_lock_bh(&fib_hash_lock);
  503. list_del(&fa->fa_list);
  504. if (list_empty(&f->fn_alias)) {
  505. hlist_del(&f->fn_hash);
  506. kill_fn = 1;
  507. }
  508. fib_hash_genid++;
  509. write_unlock_bh(&fib_hash_lock);
  510. if (fa->fa_state & FA_S_ACCESSED)
  511. rt_cache_flush(-1);
  512. fn_free_alias(fa);
  513. if (kill_fn) {
  514. fn_free_node(f);
  515. fz->fz_nent--;
  516. }
  517. return 0;
  518. }
  519. return -ESRCH;
  520. }
  521. static int fn_flush_list(struct fn_zone *fz, int idx)
  522. {
  523. struct hlist_head *head = &fz->fz_hash[idx];
  524. struct hlist_node *node, *n;
  525. struct fib_node *f;
  526. int found = 0;
  527. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  528. struct fib_alias *fa, *fa_node;
  529. int kill_f;
  530. kill_f = 0;
  531. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  532. struct fib_info *fi = fa->fa_info;
  533. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  534. write_lock_bh(&fib_hash_lock);
  535. list_del(&fa->fa_list);
  536. if (list_empty(&f->fn_alias)) {
  537. hlist_del(&f->fn_hash);
  538. kill_f = 1;
  539. }
  540. fib_hash_genid++;
  541. write_unlock_bh(&fib_hash_lock);
  542. fn_free_alias(fa);
  543. found++;
  544. }
  545. }
  546. if (kill_f) {
  547. fn_free_node(f);
  548. fz->fz_nent--;
  549. }
  550. }
  551. return found;
  552. }
  553. static int fn_hash_flush(struct fib_table *tb)
  554. {
  555. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  556. struct fn_zone *fz;
  557. int found = 0;
  558. for (fz = table->fn_zone_list; fz; fz = fz->fz_next) {
  559. int i;
  560. for (i = fz->fz_divisor - 1; i >= 0; i--)
  561. found += fn_flush_list(fz, i);
  562. }
  563. return found;
  564. }
  565. static inline int
  566. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  567. struct fib_table *tb,
  568. struct fn_zone *fz,
  569. struct hlist_head *head)
  570. {
  571. struct hlist_node *node;
  572. struct fib_node *f;
  573. int i, s_i;
  574. s_i = cb->args[4];
  575. i = 0;
  576. hlist_for_each_entry(f, node, head, fn_hash) {
  577. struct fib_alias *fa;
  578. list_for_each_entry(fa, &f->fn_alias, fa_list) {
  579. if (i < s_i)
  580. goto next;
  581. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  582. cb->nlh->nlmsg_seq,
  583. RTM_NEWROUTE,
  584. tb->tb_id,
  585. fa->fa_type,
  586. fa->fa_scope,
  587. f->fn_key,
  588. fz->fz_order,
  589. fa->fa_tos,
  590. fa->fa_info,
  591. NLM_F_MULTI) < 0) {
  592. cb->args[4] = i;
  593. return -1;
  594. }
  595. next:
  596. i++;
  597. }
  598. }
  599. cb->args[4] = i;
  600. return skb->len;
  601. }
  602. static inline int
  603. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  604. struct fib_table *tb,
  605. struct fn_zone *fz)
  606. {
  607. int h, s_h;
  608. if (fz->fz_hash == NULL)
  609. return skb->len;
  610. s_h = cb->args[3];
  611. for (h = s_h; h < fz->fz_divisor; h++) {
  612. if (hlist_empty(&fz->fz_hash[h]))
  613. continue;
  614. if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h]) < 0) {
  615. cb->args[3] = h;
  616. return -1;
  617. }
  618. memset(&cb->args[4], 0,
  619. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  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 (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  634. cb->args[2] = m;
  635. read_unlock(&fib_hash_lock);
  636. return -1;
  637. }
  638. memset(&cb->args[3], 0,
  639. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  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);
  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);
  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 fib_table *main_table = fib_get_table(RT_TABLE_MAIN);
  692. struct fn_hash *table = (struct fn_hash *)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 (fib_get_table(RT_TABLE_MAIN))
  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. return seq_open_private(file, &fib_seq_ops,
  883. sizeof(struct fib_iter_state));
  884. }
  885. static const struct file_operations fib_seq_fops = {
  886. .owner = THIS_MODULE,
  887. .open = fib_seq_open,
  888. .read = seq_read,
  889. .llseek = seq_lseek,
  890. .release = seq_release_private,
  891. };
  892. int __init fib_proc_init(void)
  893. {
  894. if (!proc_net_fops_create(&init_net, "route", S_IRUGO, &fib_seq_fops))
  895. return -ENOMEM;
  896. return 0;
  897. }
  898. void __init fib_proc_exit(void)
  899. {
  900. proc_net_remove(&init_net, "route");
  901. }
  902. #endif /* CONFIG_PROC_FS */