fib_hash.c 25 KB

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