fib_hash.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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(tb, &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. /* Insert node F to FZ. */
  256. static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
  257. {
  258. struct hlist_head *head = rtnl_dereference(fz->fz_hash) + fn_hash(f->fn_key, fz);
  259. hlist_add_head_rcu(&f->fn_hash, head);
  260. }
  261. /* Return the node in FZ matching KEY. */
  262. static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
  263. {
  264. struct hlist_head *head = rtnl_dereference(fz->fz_hash) + fn_hash(key, fz);
  265. struct hlist_node *node;
  266. struct fib_node *f;
  267. hlist_for_each_entry_rcu(f, node, head, fn_hash) {
  268. if (f->fn_key == key)
  269. return f;
  270. }
  271. return NULL;
  272. }
  273. static struct fib_alias *fib_fast_alloc(struct fib_node *f)
  274. {
  275. struct fib_alias *fa = &f->fn_embedded_alias;
  276. if (fa->fa_info != NULL)
  277. fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
  278. return fa;
  279. }
  280. /* Caller must hold RTNL. */
  281. int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
  282. {
  283. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  284. struct fib_node *new_f = NULL;
  285. struct fib_node *f;
  286. struct fib_alias *fa, *new_fa;
  287. struct fn_zone *fz;
  288. struct fib_info *fi;
  289. u8 tos = cfg->fc_tos;
  290. __be32 key;
  291. int err;
  292. if (cfg->fc_dst_len > 32)
  293. return -EINVAL;
  294. fz = table->fn_zones[cfg->fc_dst_len];
  295. if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
  296. return -ENOBUFS;
  297. key = 0;
  298. if (cfg->fc_dst) {
  299. if (cfg->fc_dst & ~FZ_MASK(fz))
  300. return -EINVAL;
  301. key = fz_key(cfg->fc_dst, fz);
  302. }
  303. fi = fib_create_info(cfg);
  304. if (IS_ERR(fi))
  305. return PTR_ERR(fi);
  306. if (fz->fz_nent > (fz->fz_divisor<<1) &&
  307. fz->fz_divisor < FZ_MAX_DIVISOR &&
  308. (cfg->fc_dst_len == 32 ||
  309. (1 << cfg->fc_dst_len) > fz->fz_divisor))
  310. fn_rehash_zone(fz);
  311. f = fib_find_node(fz, key);
  312. if (!f)
  313. fa = NULL;
  314. else
  315. fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
  316. /* Now fa, if non-NULL, points to the first fib alias
  317. * with the same keys [prefix,tos,priority], if such key already
  318. * exists or to the node before which we will insert new one.
  319. *
  320. * If fa is NULL, we will need to allocate a new one and
  321. * insert to the head of f.
  322. *
  323. * If f is NULL, no fib node matched the destination key
  324. * and we need to allocate a new one of those as well.
  325. */
  326. if (fa && fa->fa_tos == tos &&
  327. fa->fa_info->fib_priority == fi->fib_priority) {
  328. struct fib_alias *fa_first, *fa_match;
  329. err = -EEXIST;
  330. if (cfg->fc_nlflags & NLM_F_EXCL)
  331. goto out;
  332. /* We have 2 goals:
  333. * 1. Find exact match for type, scope, fib_info to avoid
  334. * duplicate routes
  335. * 2. Find next 'fa' (or head), NLM_F_APPEND inserts before it
  336. */
  337. fa_match = NULL;
  338. fa_first = fa;
  339. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  340. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  341. if (fa->fa_tos != tos)
  342. break;
  343. if (fa->fa_info->fib_priority != fi->fib_priority)
  344. break;
  345. if (fa->fa_type == cfg->fc_type &&
  346. fa->fa_scope == cfg->fc_scope &&
  347. fa->fa_info == fi) {
  348. fa_match = fa;
  349. break;
  350. }
  351. }
  352. if (cfg->fc_nlflags & NLM_F_REPLACE) {
  353. u8 state;
  354. fa = fa_first;
  355. if (fa_match) {
  356. if (fa == fa_match)
  357. err = 0;
  358. goto out;
  359. }
  360. err = -ENOBUFS;
  361. new_fa = fib_fast_alloc(f);
  362. if (new_fa == NULL)
  363. goto out;
  364. new_fa->fa_tos = fa->fa_tos;
  365. new_fa->fa_info = fi;
  366. new_fa->fa_type = cfg->fc_type;
  367. new_fa->fa_scope = cfg->fc_scope;
  368. state = fa->fa_state;
  369. new_fa->fa_state = state & ~FA_S_ACCESSED;
  370. fib_hash_genid++;
  371. list_replace_rcu(&fa->fa_list, &new_fa->fa_list);
  372. fn_free_alias(fa, f);
  373. if (state & FA_S_ACCESSED)
  374. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  375. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len,
  376. tb->tb_id, &cfg->fc_nlinfo, NLM_F_REPLACE);
  377. return 0;
  378. }
  379. /* Error if we find a perfect match which
  380. * uses the same scope, type, and nexthop
  381. * information.
  382. */
  383. if (fa_match)
  384. goto out;
  385. if (!(cfg->fc_nlflags & NLM_F_APPEND))
  386. fa = fa_first;
  387. }
  388. err = -ENOENT;
  389. if (!(cfg->fc_nlflags & NLM_F_CREATE))
  390. goto out;
  391. err = -ENOBUFS;
  392. if (!f) {
  393. new_f = kmem_cache_zalloc(fn_hash_kmem, GFP_KERNEL);
  394. if (new_f == NULL)
  395. goto out;
  396. INIT_HLIST_NODE(&new_f->fn_hash);
  397. INIT_LIST_HEAD(&new_f->fn_alias);
  398. new_f->fn_key = key;
  399. f = new_f;
  400. }
  401. new_fa = fib_fast_alloc(f);
  402. if (new_fa == NULL)
  403. goto out;
  404. new_fa->fa_info = fi;
  405. new_fa->fa_tos = tos;
  406. new_fa->fa_type = cfg->fc_type;
  407. new_fa->fa_scope = cfg->fc_scope;
  408. new_fa->fa_state = 0;
  409. /*
  410. * Insert new entry to the list.
  411. */
  412. if (new_f)
  413. fib_insert_node(fz, new_f);
  414. list_add_tail_rcu(&new_fa->fa_list,
  415. (fa ? &fa->fa_list : &f->fn_alias));
  416. fib_hash_genid++;
  417. if (new_f)
  418. fz->fz_nent++;
  419. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  420. rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
  421. &cfg->fc_nlinfo, 0);
  422. return 0;
  423. out:
  424. if (new_f)
  425. kmem_cache_free(fn_hash_kmem, new_f);
  426. fib_release_info(fi);
  427. return err;
  428. }
  429. int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
  430. {
  431. struct fn_hash *table = (struct fn_hash *)tb->tb_data;
  432. struct fib_node *f;
  433. struct fib_alias *fa, *fa_to_delete;
  434. struct fn_zone *fz;
  435. __be32 key;
  436. if (cfg->fc_dst_len > 32)
  437. return -EINVAL;
  438. if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
  439. return -ESRCH;
  440. key = 0;
  441. if (cfg->fc_dst) {
  442. if (cfg->fc_dst & ~FZ_MASK(fz))
  443. return -EINVAL;
  444. key = fz_key(cfg->fc_dst, fz);
  445. }
  446. f = fib_find_node(fz, key);
  447. if (!f)
  448. fa = NULL;
  449. else
  450. fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
  451. if (!fa)
  452. return -ESRCH;
  453. fa_to_delete = NULL;
  454. fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list);
  455. list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
  456. struct fib_info *fi = fa->fa_info;
  457. if (fa->fa_tos != cfg->fc_tos)
  458. break;
  459. if ((!cfg->fc_type ||
  460. fa->fa_type == cfg->fc_type) &&
  461. (cfg->fc_scope == RT_SCOPE_NOWHERE ||
  462. fa->fa_scope == cfg->fc_scope) &&
  463. (!cfg->fc_protocol ||
  464. fi->fib_protocol == cfg->fc_protocol) &&
  465. fib_nh_match(cfg, fi) == 0) {
  466. fa_to_delete = fa;
  467. break;
  468. }
  469. }
  470. if (fa_to_delete) {
  471. int kill_fn;
  472. fa = fa_to_delete;
  473. rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
  474. tb->tb_id, &cfg->fc_nlinfo, 0);
  475. kill_fn = 0;
  476. list_del_rcu(&fa->fa_list);
  477. if (list_empty(&f->fn_alias)) {
  478. hlist_del_rcu(&f->fn_hash);
  479. kill_fn = 1;
  480. }
  481. fib_hash_genid++;
  482. if (fa->fa_state & FA_S_ACCESSED)
  483. rt_cache_flush(cfg->fc_nlinfo.nl_net, -1);
  484. fn_free_alias(fa, f);
  485. if (kill_fn) {
  486. fn_free_node(f);
  487. fz->fz_nent--;
  488. }
  489. return 0;
  490. }
  491. return -ESRCH;
  492. }
  493. static int fn_flush_list(struct fn_zone *fz, int idx)
  494. {
  495. struct hlist_head *head = rtnl_dereference(fz->fz_hash) + idx;
  496. struct hlist_node *node, *n;
  497. struct fib_node *f;
  498. int found = 0;
  499. hlist_for_each_entry_safe(f, node, n, head, fn_hash) {
  500. struct fib_alias *fa, *fa_node;
  501. int kill_f;
  502. kill_f = 0;
  503. list_for_each_entry_safe(fa, fa_node, &f->fn_alias, fa_list) {
  504. struct fib_info *fi = fa->fa_info;
  505. if (fi && (fi->fib_flags&RTNH_F_DEAD)) {
  506. list_del_rcu(&fa->fa_list);
  507. if (list_empty(&f->fn_alias)) {
  508. hlist_del_rcu(&f->fn_hash);
  509. kill_f = 1;
  510. }
  511. fib_hash_genid++;
  512. fn_free_alias(fa, f);
  513. found++;
  514. }
  515. }
  516. if (kill_f) {
  517. fn_free_node(f);
  518. fz->fz_nent--;
  519. }
  520. }
  521. return found;
  522. }
  523. /* caller must hold RTNL. */
  524. int fib_table_flush(struct fib_table *tb)
  525. {
  526. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  527. struct fn_zone *fz;
  528. int found = 0;
  529. for (fz = rtnl_dereference(table->fn_zone_list);
  530. fz != NULL;
  531. fz = rtnl_dereference(fz->fz_next)) {
  532. int i;
  533. for (i = fz->fz_divisor - 1; i >= 0; i--)
  534. found += fn_flush_list(fz, i);
  535. }
  536. return found;
  537. }
  538. void fib_free_table(struct fib_table *tb)
  539. {
  540. struct fn_hash *table = (struct fn_hash *) tb->tb_data;
  541. struct fn_zone *fz, *next;
  542. next = table->fn_zone_list;
  543. while (next != NULL) {
  544. fz = next;
  545. next = fz->fz_next;
  546. if (fz->fz_hash != fz->fz_embedded_hash)
  547. fz_hash_free(fz->fz_hash, fz->fz_divisor);
  548. kfree(fz);
  549. }
  550. kfree(tb);
  551. }
  552. static inline int
  553. fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
  554. struct fib_table *tb,
  555. struct fn_zone *fz,
  556. struct hlist_head *head)
  557. {
  558. struct hlist_node *node;
  559. struct fib_node *f;
  560. int i, s_i;
  561. s_i = cb->args[4];
  562. i = 0;
  563. hlist_for_each_entry_rcu(f, node, head, fn_hash) {
  564. struct fib_alias *fa;
  565. list_for_each_entry_rcu(fa, &f->fn_alias, fa_list) {
  566. if (i < s_i)
  567. goto next;
  568. if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
  569. cb->nlh->nlmsg_seq,
  570. RTM_NEWROUTE,
  571. tb->tb_id,
  572. fa->fa_type,
  573. fa->fa_scope,
  574. f->fn_key,
  575. fz->fz_order,
  576. fa->fa_tos,
  577. fa->fa_info,
  578. NLM_F_MULTI) < 0) {
  579. cb->args[4] = i;
  580. return -1;
  581. }
  582. next:
  583. i++;
  584. }
  585. }
  586. cb->args[4] = i;
  587. return skb->len;
  588. }
  589. static inline int
  590. fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
  591. struct fib_table *tb,
  592. struct fn_zone *fz)
  593. {
  594. int h, s_h;
  595. struct hlist_head *head = rcu_dereference(fz->fz_hash);
  596. if (head == NULL)
  597. return skb->len;
  598. s_h = cb->args[3];
  599. for (h = s_h; h < fz->fz_divisor; h++) {
  600. if (hlist_empty(head + h))
  601. continue;
  602. if (fn_hash_dump_bucket(skb, cb, tb, fz, head + h) < 0) {
  603. cb->args[3] = h;
  604. return -1;
  605. }
  606. memset(&cb->args[4], 0,
  607. sizeof(cb->args) - 4*sizeof(cb->args[0]));
  608. }
  609. cb->args[3] = h;
  610. return skb->len;
  611. }
  612. int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
  613. struct netlink_callback *cb)
  614. {
  615. int m = 0, s_m;
  616. struct fn_zone *fz;
  617. struct fn_hash *table = (struct fn_hash *)tb->tb_data;
  618. s_m = cb->args[2];
  619. rcu_read_lock();
  620. for (fz = rcu_dereference(table->fn_zone_list);
  621. fz != NULL;
  622. fz = rcu_dereference(fz->fz_next), m++) {
  623. if (m < s_m)
  624. continue;
  625. if (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
  626. cb->args[2] = m;
  627. rcu_read_unlock();
  628. return -1;
  629. }
  630. memset(&cb->args[3], 0,
  631. sizeof(cb->args) - 3*sizeof(cb->args[0]));
  632. }
  633. rcu_read_unlock();
  634. cb->args[2] = m;
  635. return skb->len;
  636. }
  637. void __init fib_hash_init(void)
  638. {
  639. fn_hash_kmem = kmem_cache_create("ip_fib_hash", sizeof(struct fib_node),
  640. 0, SLAB_PANIC, NULL);
  641. fn_alias_kmem = kmem_cache_create("ip_fib_alias", sizeof(struct fib_alias),
  642. 0, SLAB_PANIC, NULL);
  643. }
  644. struct fib_table *fib_hash_table(u32 id)
  645. {
  646. struct fib_table *tb;
  647. tb = kmalloc(sizeof(struct fib_table) + sizeof(struct fn_hash),
  648. GFP_KERNEL);
  649. if (tb == NULL)
  650. return NULL;
  651. tb->tb_id = id;
  652. tb->tb_default = -1;
  653. memset(tb->tb_data, 0, sizeof(struct fn_hash));
  654. return tb;
  655. }
  656. /* ------------------------------------------------------------------------ */
  657. #ifdef CONFIG_PROC_FS
  658. struct fib_iter_state {
  659. struct seq_net_private p;
  660. struct fn_zone *zone;
  661. int bucket;
  662. struct hlist_head *hash_head;
  663. struct fib_node *fn;
  664. struct fib_alias *fa;
  665. loff_t pos;
  666. unsigned int genid;
  667. int valid;
  668. };
  669. static struct fib_alias *fib_get_first(struct seq_file *seq)
  670. {
  671. struct fib_iter_state *iter = seq->private;
  672. struct fib_table *main_table;
  673. struct fn_hash *table;
  674. main_table = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
  675. table = (struct fn_hash *)main_table->tb_data;
  676. iter->bucket = 0;
  677. iter->hash_head = NULL;
  678. iter->fn = NULL;
  679. iter->fa = NULL;
  680. iter->pos = 0;
  681. iter->genid = fib_hash_genid;
  682. iter->valid = 1;
  683. for (iter->zone = rcu_dereference(table->fn_zone_list);
  684. iter->zone != NULL;
  685. iter->zone = rcu_dereference(iter->zone->fz_next)) {
  686. int maxslot;
  687. if (!iter->zone->fz_nent)
  688. continue;
  689. iter->hash_head = rcu_dereference(iter->zone->fz_hash);
  690. maxslot = iter->zone->fz_divisor;
  691. for (iter->bucket = 0; iter->bucket < maxslot;
  692. ++iter->bucket, ++iter->hash_head) {
  693. struct hlist_node *node;
  694. struct fib_node *fn;
  695. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  696. struct fib_alias *fa;
  697. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  698. iter->fn = fn;
  699. iter->fa = fa;
  700. goto out;
  701. }
  702. }
  703. }
  704. }
  705. out:
  706. return iter->fa;
  707. }
  708. static struct fib_alias *fib_get_next(struct seq_file *seq)
  709. {
  710. struct fib_iter_state *iter = seq->private;
  711. struct fib_node *fn;
  712. struct fib_alias *fa;
  713. /* Advance FA, if any. */
  714. fn = iter->fn;
  715. fa = iter->fa;
  716. if (fa) {
  717. BUG_ON(!fn);
  718. list_for_each_entry_continue(fa, &fn->fn_alias, fa_list) {
  719. iter->fa = fa;
  720. goto out;
  721. }
  722. }
  723. fa = iter->fa = NULL;
  724. /* Advance FN. */
  725. if (fn) {
  726. struct hlist_node *node = &fn->fn_hash;
  727. hlist_for_each_entry_continue(fn, node, fn_hash) {
  728. iter->fn = fn;
  729. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  730. iter->fa = fa;
  731. goto out;
  732. }
  733. }
  734. }
  735. fn = iter->fn = NULL;
  736. /* Advance hash chain. */
  737. if (!iter->zone)
  738. goto out;
  739. for (;;) {
  740. struct hlist_node *node;
  741. int maxslot;
  742. maxslot = iter->zone->fz_divisor;
  743. while (++iter->bucket < maxslot) {
  744. iter->hash_head++;
  745. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  746. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  747. iter->fn = fn;
  748. iter->fa = fa;
  749. goto out;
  750. }
  751. }
  752. }
  753. iter->zone = rcu_dereference(iter->zone->fz_next);
  754. if (!iter->zone)
  755. goto out;
  756. iter->bucket = 0;
  757. iter->hash_head = rcu_dereference(iter->zone->fz_hash);
  758. hlist_for_each_entry(fn, node, iter->hash_head, fn_hash) {
  759. list_for_each_entry(fa, &fn->fn_alias, fa_list) {
  760. iter->fn = fn;
  761. iter->fa = fa;
  762. goto out;
  763. }
  764. }
  765. }
  766. out:
  767. iter->pos++;
  768. return fa;
  769. }
  770. static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
  771. {
  772. struct fib_iter_state *iter = seq->private;
  773. struct fib_alias *fa;
  774. if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
  775. fa = iter->fa;
  776. pos -= iter->pos;
  777. } else
  778. fa = fib_get_first(seq);
  779. if (fa)
  780. while (pos && (fa = fib_get_next(seq)))
  781. --pos;
  782. return pos ? NULL : fa;
  783. }
  784. static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
  785. __acquires(RCU)
  786. {
  787. void *v = NULL;
  788. rcu_read_lock();
  789. if (fib_get_table(seq_file_net(seq), RT_TABLE_MAIN))
  790. v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  791. return v;
  792. }
  793. static void *fib_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  794. {
  795. ++*pos;
  796. return v == SEQ_START_TOKEN ? fib_get_first(seq) : fib_get_next(seq);
  797. }
  798. static void fib_seq_stop(struct seq_file *seq, void *v)
  799. __releases(RCU)
  800. {
  801. rcu_read_unlock();
  802. }
  803. static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
  804. {
  805. static const unsigned type2flags[RTN_MAX + 1] = {
  806. [7] = RTF_REJECT,
  807. [8] = RTF_REJECT,
  808. };
  809. unsigned flags = type2flags[type];
  810. if (fi && fi->fib_nh->nh_gw)
  811. flags |= RTF_GATEWAY;
  812. if (mask == htonl(0xFFFFFFFF))
  813. flags |= RTF_HOST;
  814. flags |= RTF_UP;
  815. return flags;
  816. }
  817. /*
  818. * This outputs /proc/net/route.
  819. *
  820. * It always works in backward compatibility mode.
  821. * The format of the file is not supposed to be changed.
  822. */
  823. static int fib_seq_show(struct seq_file *seq, void *v)
  824. {
  825. struct fib_iter_state *iter;
  826. int len;
  827. __be32 prefix, mask;
  828. unsigned flags;
  829. struct fib_node *f;
  830. struct fib_alias *fa;
  831. struct fib_info *fi;
  832. if (v == SEQ_START_TOKEN) {
  833. seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
  834. "\tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU"
  835. "\tWindow\tIRTT");
  836. goto out;
  837. }
  838. iter = seq->private;
  839. f = iter->fn;
  840. fa = iter->fa;
  841. fi = fa->fa_info;
  842. prefix = f->fn_key;
  843. mask = FZ_MASK(iter->zone);
  844. flags = fib_flag_trans(fa->fa_type, mask, fi);
  845. if (fi)
  846. seq_printf(seq,
  847. "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
  848. fi->fib_dev ? fi->fib_dev->name : "*", prefix,
  849. fi->fib_nh->nh_gw, flags, 0, 0, fi->fib_priority,
  850. mask, (fi->fib_advmss ? fi->fib_advmss + 40 : 0),
  851. fi->fib_window,
  852. fi->fib_rtt >> 3, &len);
  853. else
  854. seq_printf(seq,
  855. "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u%n",
  856. prefix, 0, flags, 0, 0, 0, mask, 0, 0, 0, &len);
  857. seq_printf(seq, "%*s\n", 127 - len, "");
  858. out:
  859. return 0;
  860. }
  861. static const struct seq_operations fib_seq_ops = {
  862. .start = fib_seq_start,
  863. .next = fib_seq_next,
  864. .stop = fib_seq_stop,
  865. .show = fib_seq_show,
  866. };
  867. static int fib_seq_open(struct inode *inode, struct file *file)
  868. {
  869. return seq_open_net(inode, file, &fib_seq_ops,
  870. sizeof(struct fib_iter_state));
  871. }
  872. static const struct file_operations fib_seq_fops = {
  873. .owner = THIS_MODULE,
  874. .open = fib_seq_open,
  875. .read = seq_read,
  876. .llseek = seq_lseek,
  877. .release = seq_release_net,
  878. };
  879. int __net_init fib_proc_init(struct net *net)
  880. {
  881. if (!proc_net_fops_create(net, "route", S_IRUGO, &fib_seq_fops))
  882. return -ENOMEM;
  883. return 0;
  884. }
  885. void __net_exit fib_proc_exit(struct net *net)
  886. {
  887. proc_net_remove(net, "route");
  888. }
  889. #endif /* CONFIG_PROC_FS */