fib_hash.c 24 KB

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