fib_hash.c 23 KB

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