fib_hash.c 23 KB

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