ip6_fib.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /*
  2. * Linux INET6 implementation
  3. * Forwarding Information Database
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. /*
  14. * Changes:
  15. * Yuji SEKIYA @USAGI: Support default route on router node;
  16. * remove ip6_null_entry from the top of
  17. * routing table.
  18. * Ville Nuorvala: Fixed routing subtrees.
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/net.h>
  23. #include <linux/route.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/in6.h>
  26. #include <linux/init.h>
  27. #include <linux/list.h>
  28. #ifdef CONFIG_PROC_FS
  29. #include <linux/proc_fs.h>
  30. #endif
  31. #include <net/ipv6.h>
  32. #include <net/ndisc.h>
  33. #include <net/addrconf.h>
  34. #include <net/ip6_fib.h>
  35. #include <net/ip6_route.h>
  36. #define RT6_DEBUG 2
  37. #if RT6_DEBUG >= 3
  38. #define RT6_TRACE(x...) printk(KERN_DEBUG x)
  39. #else
  40. #define RT6_TRACE(x...) do { ; } while (0)
  41. #endif
  42. static struct kmem_cache * fib6_node_kmem __read_mostly;
  43. enum fib_walk_state_t
  44. {
  45. #ifdef CONFIG_IPV6_SUBTREES
  46. FWS_S,
  47. #endif
  48. FWS_L,
  49. FWS_R,
  50. FWS_C,
  51. FWS_U
  52. };
  53. struct fib6_cleaner_t
  54. {
  55. struct fib6_walker_t w;
  56. struct net *net;
  57. int (*func)(struct rt6_info *, void *arg);
  58. void *arg;
  59. };
  60. static DEFINE_RWLOCK(fib6_walker_lock);
  61. #ifdef CONFIG_IPV6_SUBTREES
  62. #define FWS_INIT FWS_S
  63. #else
  64. #define FWS_INIT FWS_L
  65. #endif
  66. static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
  67. struct rt6_info *rt);
  68. static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
  69. static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
  70. static int fib6_walk(struct fib6_walker_t *w);
  71. static int fib6_walk_continue(struct fib6_walker_t *w);
  72. /*
  73. * A routing update causes an increase of the serial number on the
  74. * affected subtree. This allows for cached routes to be asynchronously
  75. * tested when modifications are made to the destination cache as a
  76. * result of redirects, path MTU changes, etc.
  77. */
  78. static __u32 rt_sernum;
  79. static void fib6_gc_timer_cb(unsigned long arg);
  80. static struct fib6_walker_t fib6_walker_list = {
  81. .prev = &fib6_walker_list,
  82. .next = &fib6_walker_list,
  83. };
  84. #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
  85. static inline void fib6_walker_link(struct fib6_walker_t *w)
  86. {
  87. write_lock_bh(&fib6_walker_lock);
  88. w->next = fib6_walker_list.next;
  89. w->prev = &fib6_walker_list;
  90. w->next->prev = w;
  91. w->prev->next = w;
  92. write_unlock_bh(&fib6_walker_lock);
  93. }
  94. static inline void fib6_walker_unlink(struct fib6_walker_t *w)
  95. {
  96. write_lock_bh(&fib6_walker_lock);
  97. w->next->prev = w->prev;
  98. w->prev->next = w->next;
  99. w->prev = w->next = w;
  100. write_unlock_bh(&fib6_walker_lock);
  101. }
  102. static __inline__ u32 fib6_new_sernum(void)
  103. {
  104. u32 n = ++rt_sernum;
  105. if ((__s32)n <= 0)
  106. rt_sernum = n = 1;
  107. return n;
  108. }
  109. /*
  110. * Auxiliary address test functions for the radix tree.
  111. *
  112. * These assume a 32bit processor (although it will work on
  113. * 64bit processors)
  114. */
  115. /*
  116. * test bit
  117. */
  118. static __inline__ __be32 addr_bit_set(void *token, int fn_bit)
  119. {
  120. __be32 *addr = token;
  121. return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
  122. }
  123. static __inline__ struct fib6_node * node_alloc(void)
  124. {
  125. struct fib6_node *fn;
  126. fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
  127. return fn;
  128. }
  129. static __inline__ void node_free(struct fib6_node * fn)
  130. {
  131. kmem_cache_free(fib6_node_kmem, fn);
  132. }
  133. static __inline__ void rt6_release(struct rt6_info *rt)
  134. {
  135. if (atomic_dec_and_test(&rt->rt6i_ref))
  136. dst_free(&rt->u.dst);
  137. }
  138. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  139. #define FIB_TABLE_HASHSZ 256
  140. #else
  141. #define FIB_TABLE_HASHSZ 1
  142. #endif
  143. static void fib6_link_table(struct net *net, struct fib6_table *tb)
  144. {
  145. unsigned int h;
  146. /*
  147. * Initialize table lock at a single place to give lockdep a key,
  148. * tables aren't visible prior to being linked to the list.
  149. */
  150. rwlock_init(&tb->tb6_lock);
  151. h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1);
  152. /*
  153. * No protection necessary, this is the only list mutatation
  154. * operation, tables never disappear once they exist.
  155. */
  156. hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
  157. }
  158. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  159. static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
  160. {
  161. struct fib6_table *table;
  162. table = kzalloc(sizeof(*table), GFP_ATOMIC);
  163. if (table != NULL) {
  164. table->tb6_id = id;
  165. table->tb6_root.leaf = net->ipv6.ip6_null_entry;
  166. table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  167. }
  168. return table;
  169. }
  170. struct fib6_table *fib6_new_table(struct net *net, u32 id)
  171. {
  172. struct fib6_table *tb;
  173. if (id == 0)
  174. id = RT6_TABLE_MAIN;
  175. tb = fib6_get_table(net, id);
  176. if (tb)
  177. return tb;
  178. tb = fib6_alloc_table(net, id);
  179. if (tb != NULL)
  180. fib6_link_table(net, tb);
  181. return tb;
  182. }
  183. struct fib6_table *fib6_get_table(struct net *net, u32 id)
  184. {
  185. struct fib6_table *tb;
  186. struct hlist_head *head;
  187. struct hlist_node *node;
  188. unsigned int h;
  189. if (id == 0)
  190. id = RT6_TABLE_MAIN;
  191. h = id & (FIB_TABLE_HASHSZ - 1);
  192. rcu_read_lock();
  193. head = &net->ipv6.fib_table_hash[h];
  194. hlist_for_each_entry_rcu(tb, node, head, tb6_hlist) {
  195. if (tb->tb6_id == id) {
  196. rcu_read_unlock();
  197. return tb;
  198. }
  199. }
  200. rcu_read_unlock();
  201. return NULL;
  202. }
  203. static void fib6_tables_init(struct net *net)
  204. {
  205. fib6_link_table(net, net->ipv6.fib6_main_tbl);
  206. fib6_link_table(net, net->ipv6.fib6_local_tbl);
  207. }
  208. #else
  209. struct fib6_table *fib6_new_table(struct net *net, u32 id)
  210. {
  211. return fib6_get_table(net, id);
  212. }
  213. struct fib6_table *fib6_get_table(struct net *net, u32 id)
  214. {
  215. return net->ipv6.fib6_main_tbl;
  216. }
  217. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi *fl,
  218. int flags, pol_lookup_t lookup)
  219. {
  220. return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl, flags);
  221. }
  222. static void fib6_tables_init(struct net *net)
  223. {
  224. fib6_link_table(net, net->ipv6.fib6_main_tbl);
  225. }
  226. #endif
  227. static int fib6_dump_node(struct fib6_walker_t *w)
  228. {
  229. int res;
  230. struct rt6_info *rt;
  231. for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
  232. res = rt6_dump_route(rt, w->args);
  233. if (res < 0) {
  234. /* Frame is full, suspend walking */
  235. w->leaf = rt;
  236. return 1;
  237. }
  238. BUG_TRAP(res!=0);
  239. }
  240. w->leaf = NULL;
  241. return 0;
  242. }
  243. static void fib6_dump_end(struct netlink_callback *cb)
  244. {
  245. struct fib6_walker_t *w = (void*)cb->args[2];
  246. if (w) {
  247. cb->args[2] = 0;
  248. kfree(w);
  249. }
  250. cb->done = (void*)cb->args[3];
  251. cb->args[1] = 3;
  252. }
  253. static int fib6_dump_done(struct netlink_callback *cb)
  254. {
  255. fib6_dump_end(cb);
  256. return cb->done ? cb->done(cb) : 0;
  257. }
  258. static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
  259. struct netlink_callback *cb)
  260. {
  261. struct fib6_walker_t *w;
  262. int res;
  263. w = (void *)cb->args[2];
  264. w->root = &table->tb6_root;
  265. if (cb->args[4] == 0) {
  266. read_lock_bh(&table->tb6_lock);
  267. res = fib6_walk(w);
  268. read_unlock_bh(&table->tb6_lock);
  269. if (res > 0)
  270. cb->args[4] = 1;
  271. } else {
  272. read_lock_bh(&table->tb6_lock);
  273. res = fib6_walk_continue(w);
  274. read_unlock_bh(&table->tb6_lock);
  275. if (res != 0) {
  276. if (res < 0)
  277. fib6_walker_unlink(w);
  278. goto end;
  279. }
  280. fib6_walker_unlink(w);
  281. cb->args[4] = 0;
  282. }
  283. end:
  284. return res;
  285. }
  286. static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  287. {
  288. struct net *net = sock_net(skb->sk);
  289. unsigned int h, s_h;
  290. unsigned int e = 0, s_e;
  291. struct rt6_rtnl_dump_arg arg;
  292. struct fib6_walker_t *w;
  293. struct fib6_table *tb;
  294. struct hlist_node *node;
  295. struct hlist_head *head;
  296. int res = 0;
  297. s_h = cb->args[0];
  298. s_e = cb->args[1];
  299. w = (void *)cb->args[2];
  300. if (w == NULL) {
  301. /* New dump:
  302. *
  303. * 1. hook callback destructor.
  304. */
  305. cb->args[3] = (long)cb->done;
  306. cb->done = fib6_dump_done;
  307. /*
  308. * 2. allocate and initialize walker.
  309. */
  310. w = kzalloc(sizeof(*w), GFP_ATOMIC);
  311. if (w == NULL)
  312. return -ENOMEM;
  313. w->func = fib6_dump_node;
  314. cb->args[2] = (long)w;
  315. }
  316. arg.skb = skb;
  317. arg.cb = cb;
  318. w->args = &arg;
  319. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  320. e = 0;
  321. head = &net->ipv6.fib_table_hash[h];
  322. hlist_for_each_entry(tb, node, head, tb6_hlist) {
  323. if (e < s_e)
  324. goto next;
  325. res = fib6_dump_table(tb, skb, cb);
  326. if (res != 0)
  327. goto out;
  328. next:
  329. e++;
  330. }
  331. }
  332. out:
  333. cb->args[1] = e;
  334. cb->args[0] = h;
  335. res = res < 0 ? res : skb->len;
  336. if (res <= 0)
  337. fib6_dump_end(cb);
  338. return res;
  339. }
  340. /*
  341. * Routing Table
  342. *
  343. * return the appropriate node for a routing tree "add" operation
  344. * by either creating and inserting or by returning an existing
  345. * node.
  346. */
  347. static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
  348. int addrlen, int plen,
  349. int offset)
  350. {
  351. struct fib6_node *fn, *in, *ln;
  352. struct fib6_node *pn = NULL;
  353. struct rt6key *key;
  354. int bit;
  355. __be32 dir = 0;
  356. __u32 sernum = fib6_new_sernum();
  357. RT6_TRACE("fib6_add_1\n");
  358. /* insert node in tree */
  359. fn = root;
  360. do {
  361. key = (struct rt6key *)((u8 *)fn->leaf + offset);
  362. /*
  363. * Prefix match
  364. */
  365. if (plen < fn->fn_bit ||
  366. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  367. goto insert_above;
  368. /*
  369. * Exact match ?
  370. */
  371. if (plen == fn->fn_bit) {
  372. /* clean up an intermediate node */
  373. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  374. rt6_release(fn->leaf);
  375. fn->leaf = NULL;
  376. }
  377. fn->fn_sernum = sernum;
  378. return fn;
  379. }
  380. /*
  381. * We have more bits to go
  382. */
  383. /* Try to walk down on tree. */
  384. fn->fn_sernum = sernum;
  385. dir = addr_bit_set(addr, fn->fn_bit);
  386. pn = fn;
  387. fn = dir ? fn->right: fn->left;
  388. } while (fn);
  389. /*
  390. * We walked to the bottom of tree.
  391. * Create new leaf node without children.
  392. */
  393. ln = node_alloc();
  394. if (ln == NULL)
  395. return NULL;
  396. ln->fn_bit = plen;
  397. ln->parent = pn;
  398. ln->fn_sernum = sernum;
  399. if (dir)
  400. pn->right = ln;
  401. else
  402. pn->left = ln;
  403. return ln;
  404. insert_above:
  405. /*
  406. * split since we don't have a common prefix anymore or
  407. * we have a less significant route.
  408. * we've to insert an intermediate node on the list
  409. * this new node will point to the one we need to create
  410. * and the current
  411. */
  412. pn = fn->parent;
  413. /* find 1st bit in difference between the 2 addrs.
  414. See comment in __ipv6_addr_diff: bit may be an invalid value,
  415. but if it is >= plen, the value is ignored in any case.
  416. */
  417. bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
  418. /*
  419. * (intermediate)[in]
  420. * / \
  421. * (new leaf node)[ln] (old node)[fn]
  422. */
  423. if (plen > bit) {
  424. in = node_alloc();
  425. ln = node_alloc();
  426. if (in == NULL || ln == NULL) {
  427. if (in)
  428. node_free(in);
  429. if (ln)
  430. node_free(ln);
  431. return NULL;
  432. }
  433. /*
  434. * new intermediate node.
  435. * RTN_RTINFO will
  436. * be off since that an address that chooses one of
  437. * the branches would not match less specific routes
  438. * in the other branch
  439. */
  440. in->fn_bit = bit;
  441. in->parent = pn;
  442. in->leaf = fn->leaf;
  443. atomic_inc(&in->leaf->rt6i_ref);
  444. in->fn_sernum = sernum;
  445. /* update parent pointer */
  446. if (dir)
  447. pn->right = in;
  448. else
  449. pn->left = in;
  450. ln->fn_bit = plen;
  451. ln->parent = in;
  452. fn->parent = in;
  453. ln->fn_sernum = sernum;
  454. if (addr_bit_set(addr, bit)) {
  455. in->right = ln;
  456. in->left = fn;
  457. } else {
  458. in->left = ln;
  459. in->right = fn;
  460. }
  461. } else { /* plen <= bit */
  462. /*
  463. * (new leaf node)[ln]
  464. * / \
  465. * (old node)[fn] NULL
  466. */
  467. ln = node_alloc();
  468. if (ln == NULL)
  469. return NULL;
  470. ln->fn_bit = plen;
  471. ln->parent = pn;
  472. ln->fn_sernum = sernum;
  473. if (dir)
  474. pn->right = ln;
  475. else
  476. pn->left = ln;
  477. if (addr_bit_set(&key->addr, plen))
  478. ln->right = fn;
  479. else
  480. ln->left = fn;
  481. fn->parent = ln;
  482. }
  483. return ln;
  484. }
  485. /*
  486. * Insert routing information in a node.
  487. */
  488. static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
  489. struct nl_info *info)
  490. {
  491. struct rt6_info *iter = NULL;
  492. struct rt6_info **ins;
  493. ins = &fn->leaf;
  494. for (iter = fn->leaf; iter; iter=iter->u.dst.rt6_next) {
  495. /*
  496. * Search for duplicates
  497. */
  498. if (iter->rt6i_metric == rt->rt6i_metric) {
  499. /*
  500. * Same priority level
  501. */
  502. if (iter->rt6i_dev == rt->rt6i_dev &&
  503. iter->rt6i_idev == rt->rt6i_idev &&
  504. ipv6_addr_equal(&iter->rt6i_gateway,
  505. &rt->rt6i_gateway)) {
  506. if (!(iter->rt6i_flags&RTF_EXPIRES))
  507. return -EEXIST;
  508. iter->rt6i_expires = rt->rt6i_expires;
  509. if (!(rt->rt6i_flags&RTF_EXPIRES)) {
  510. iter->rt6i_flags &= ~RTF_EXPIRES;
  511. iter->rt6i_expires = 0;
  512. }
  513. return -EEXIST;
  514. }
  515. }
  516. if (iter->rt6i_metric > rt->rt6i_metric)
  517. break;
  518. ins = &iter->u.dst.rt6_next;
  519. }
  520. /* Reset round-robin state, if necessary */
  521. if (ins == &fn->leaf)
  522. fn->rr_ptr = NULL;
  523. /*
  524. * insert node
  525. */
  526. rt->u.dst.rt6_next = iter;
  527. *ins = rt;
  528. rt->rt6i_node = fn;
  529. atomic_inc(&rt->rt6i_ref);
  530. inet6_rt_notify(RTM_NEWROUTE, rt, info);
  531. info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
  532. if ((fn->fn_flags & RTN_RTINFO) == 0) {
  533. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  534. fn->fn_flags |= RTN_RTINFO;
  535. }
  536. return 0;
  537. }
  538. static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
  539. {
  540. if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
  541. (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
  542. mod_timer(&net->ipv6.ip6_fib_timer,
  543. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  544. }
  545. void fib6_force_start_gc(struct net *net)
  546. {
  547. if (!timer_pending(&net->ipv6.ip6_fib_timer))
  548. mod_timer(&net->ipv6.ip6_fib_timer,
  549. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  550. }
  551. /*
  552. * Add routing information to the routing tree.
  553. * <destination addr>/<source addr>
  554. * with source addr info in sub-trees
  555. */
  556. int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
  557. {
  558. struct fib6_node *fn, *pn = NULL;
  559. int err = -ENOMEM;
  560. fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
  561. rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
  562. if (fn == NULL)
  563. goto out;
  564. pn = fn;
  565. #ifdef CONFIG_IPV6_SUBTREES
  566. if (rt->rt6i_src.plen) {
  567. struct fib6_node *sn;
  568. if (fn->subtree == NULL) {
  569. struct fib6_node *sfn;
  570. /*
  571. * Create subtree.
  572. *
  573. * fn[main tree]
  574. * |
  575. * sfn[subtree root]
  576. * \
  577. * sn[new leaf node]
  578. */
  579. /* Create subtree root node */
  580. sfn = node_alloc();
  581. if (sfn == NULL)
  582. goto st_failure;
  583. sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
  584. atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
  585. sfn->fn_flags = RTN_ROOT;
  586. sfn->fn_sernum = fib6_new_sernum();
  587. /* Now add the first leaf node to new subtree */
  588. sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
  589. sizeof(struct in6_addr), rt->rt6i_src.plen,
  590. offsetof(struct rt6_info, rt6i_src));
  591. if (sn == NULL) {
  592. /* If it is failed, discard just allocated
  593. root, and then (in st_failure) stale node
  594. in main tree.
  595. */
  596. node_free(sfn);
  597. goto st_failure;
  598. }
  599. /* Now link new subtree to main tree */
  600. sfn->parent = fn;
  601. fn->subtree = sfn;
  602. } else {
  603. sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
  604. sizeof(struct in6_addr), rt->rt6i_src.plen,
  605. offsetof(struct rt6_info, rt6i_src));
  606. if (sn == NULL)
  607. goto st_failure;
  608. }
  609. if (fn->leaf == NULL) {
  610. fn->leaf = rt;
  611. atomic_inc(&rt->rt6i_ref);
  612. }
  613. fn = sn;
  614. }
  615. #endif
  616. err = fib6_add_rt2node(fn, rt, info);
  617. if (err == 0) {
  618. fib6_start_gc(info->nl_net, rt);
  619. if (!(rt->rt6i_flags&RTF_CACHE))
  620. fib6_prune_clones(info->nl_net, pn, rt);
  621. }
  622. out:
  623. if (err) {
  624. #ifdef CONFIG_IPV6_SUBTREES
  625. /*
  626. * If fib6_add_1 has cleared the old leaf pointer in the
  627. * super-tree leaf node we have to find a new one for it.
  628. */
  629. if (pn != fn && pn->leaf == rt) {
  630. pn->leaf = NULL;
  631. atomic_dec(&rt->rt6i_ref);
  632. }
  633. if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
  634. pn->leaf = fib6_find_prefix(info->nl_net, pn);
  635. #if RT6_DEBUG >= 2
  636. if (!pn->leaf) {
  637. BUG_TRAP(pn->leaf != NULL);
  638. pn->leaf = info->nl_net->ipv6.ip6_null_entry;
  639. }
  640. #endif
  641. atomic_inc(&pn->leaf->rt6i_ref);
  642. }
  643. #endif
  644. dst_free(&rt->u.dst);
  645. }
  646. return err;
  647. #ifdef CONFIG_IPV6_SUBTREES
  648. /* Subtree creation failed, probably main tree node
  649. is orphan. If it is, shoot it.
  650. */
  651. st_failure:
  652. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  653. fib6_repair_tree(info->nl_net, fn);
  654. dst_free(&rt->u.dst);
  655. return err;
  656. #endif
  657. }
  658. /*
  659. * Routing tree lookup
  660. *
  661. */
  662. struct lookup_args {
  663. int offset; /* key offset on rt6_info */
  664. struct in6_addr *addr; /* search key */
  665. };
  666. static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
  667. struct lookup_args *args)
  668. {
  669. struct fib6_node *fn;
  670. __be32 dir;
  671. if (unlikely(args->offset == 0))
  672. return NULL;
  673. /*
  674. * Descend on a tree
  675. */
  676. fn = root;
  677. for (;;) {
  678. struct fib6_node *next;
  679. dir = addr_bit_set(args->addr, fn->fn_bit);
  680. next = dir ? fn->right : fn->left;
  681. if (next) {
  682. fn = next;
  683. continue;
  684. }
  685. break;
  686. }
  687. while(fn) {
  688. if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
  689. struct rt6key *key;
  690. key = (struct rt6key *) ((u8 *) fn->leaf +
  691. args->offset);
  692. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  693. #ifdef CONFIG_IPV6_SUBTREES
  694. if (fn->subtree)
  695. fn = fib6_lookup_1(fn->subtree, args + 1);
  696. #endif
  697. if (!fn || fn->fn_flags & RTN_RTINFO)
  698. return fn;
  699. }
  700. }
  701. if (fn->fn_flags & RTN_ROOT)
  702. break;
  703. fn = fn->parent;
  704. }
  705. return NULL;
  706. }
  707. struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
  708. struct in6_addr *saddr)
  709. {
  710. struct fib6_node *fn;
  711. struct lookup_args args[] = {
  712. {
  713. .offset = offsetof(struct rt6_info, rt6i_dst),
  714. .addr = daddr,
  715. },
  716. #ifdef CONFIG_IPV6_SUBTREES
  717. {
  718. .offset = offsetof(struct rt6_info, rt6i_src),
  719. .addr = saddr,
  720. },
  721. #endif
  722. {
  723. .offset = 0, /* sentinel */
  724. }
  725. };
  726. fn = fib6_lookup_1(root, daddr ? args : args + 1);
  727. if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
  728. fn = root;
  729. return fn;
  730. }
  731. /*
  732. * Get node with specified destination prefix (and source prefix,
  733. * if subtrees are used)
  734. */
  735. static struct fib6_node * fib6_locate_1(struct fib6_node *root,
  736. struct in6_addr *addr,
  737. int plen, int offset)
  738. {
  739. struct fib6_node *fn;
  740. for (fn = root; fn ; ) {
  741. struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
  742. /*
  743. * Prefix match
  744. */
  745. if (plen < fn->fn_bit ||
  746. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  747. return NULL;
  748. if (plen == fn->fn_bit)
  749. return fn;
  750. /*
  751. * We have more bits to go
  752. */
  753. if (addr_bit_set(addr, fn->fn_bit))
  754. fn = fn->right;
  755. else
  756. fn = fn->left;
  757. }
  758. return NULL;
  759. }
  760. struct fib6_node * fib6_locate(struct fib6_node *root,
  761. struct in6_addr *daddr, int dst_len,
  762. struct in6_addr *saddr, int src_len)
  763. {
  764. struct fib6_node *fn;
  765. fn = fib6_locate_1(root, daddr, dst_len,
  766. offsetof(struct rt6_info, rt6i_dst));
  767. #ifdef CONFIG_IPV6_SUBTREES
  768. if (src_len) {
  769. BUG_TRAP(saddr!=NULL);
  770. if (fn && fn->subtree)
  771. fn = fib6_locate_1(fn->subtree, saddr, src_len,
  772. offsetof(struct rt6_info, rt6i_src));
  773. }
  774. #endif
  775. if (fn && fn->fn_flags&RTN_RTINFO)
  776. return fn;
  777. return NULL;
  778. }
  779. /*
  780. * Deletion
  781. *
  782. */
  783. static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
  784. {
  785. if (fn->fn_flags&RTN_ROOT)
  786. return net->ipv6.ip6_null_entry;
  787. while(fn) {
  788. if(fn->left)
  789. return fn->left->leaf;
  790. if(fn->right)
  791. return fn->right->leaf;
  792. fn = FIB6_SUBTREE(fn);
  793. }
  794. return NULL;
  795. }
  796. /*
  797. * Called to trim the tree of intermediate nodes when possible. "fn"
  798. * is the node we want to try and remove.
  799. */
  800. static struct fib6_node *fib6_repair_tree(struct net *net,
  801. struct fib6_node *fn)
  802. {
  803. int children;
  804. int nstate;
  805. struct fib6_node *child, *pn;
  806. struct fib6_walker_t *w;
  807. int iter = 0;
  808. for (;;) {
  809. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  810. iter++;
  811. BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
  812. BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
  813. BUG_TRAP(fn->leaf==NULL);
  814. children = 0;
  815. child = NULL;
  816. if (fn->right) child = fn->right, children |= 1;
  817. if (fn->left) child = fn->left, children |= 2;
  818. if (children == 3 || FIB6_SUBTREE(fn)
  819. #ifdef CONFIG_IPV6_SUBTREES
  820. /* Subtree root (i.e. fn) may have one child */
  821. || (children && fn->fn_flags&RTN_ROOT)
  822. #endif
  823. ) {
  824. fn->leaf = fib6_find_prefix(net, fn);
  825. #if RT6_DEBUG >= 2
  826. if (fn->leaf==NULL) {
  827. BUG_TRAP(fn->leaf);
  828. fn->leaf = net->ipv6.ip6_null_entry;
  829. }
  830. #endif
  831. atomic_inc(&fn->leaf->rt6i_ref);
  832. return fn->parent;
  833. }
  834. pn = fn->parent;
  835. #ifdef CONFIG_IPV6_SUBTREES
  836. if (FIB6_SUBTREE(pn) == fn) {
  837. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  838. FIB6_SUBTREE(pn) = NULL;
  839. nstate = FWS_L;
  840. } else {
  841. BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
  842. #endif
  843. if (pn->right == fn) pn->right = child;
  844. else if (pn->left == fn) pn->left = child;
  845. #if RT6_DEBUG >= 2
  846. else BUG_TRAP(0);
  847. #endif
  848. if (child)
  849. child->parent = pn;
  850. nstate = FWS_R;
  851. #ifdef CONFIG_IPV6_SUBTREES
  852. }
  853. #endif
  854. read_lock(&fib6_walker_lock);
  855. FOR_WALKERS(w) {
  856. if (child == NULL) {
  857. if (w->root == fn) {
  858. w->root = w->node = NULL;
  859. RT6_TRACE("W %p adjusted by delroot 1\n", w);
  860. } else if (w->node == fn) {
  861. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  862. w->node = pn;
  863. w->state = nstate;
  864. }
  865. } else {
  866. if (w->root == fn) {
  867. w->root = child;
  868. RT6_TRACE("W %p adjusted by delroot 2\n", w);
  869. }
  870. if (w->node == fn) {
  871. w->node = child;
  872. if (children&2) {
  873. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  874. w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
  875. } else {
  876. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  877. w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
  878. }
  879. }
  880. }
  881. }
  882. read_unlock(&fib6_walker_lock);
  883. node_free(fn);
  884. if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn))
  885. return pn;
  886. rt6_release(pn->leaf);
  887. pn->leaf = NULL;
  888. fn = pn;
  889. }
  890. }
  891. static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
  892. struct nl_info *info)
  893. {
  894. struct fib6_walker_t *w;
  895. struct rt6_info *rt = *rtp;
  896. struct net *net = info->nl_net;
  897. RT6_TRACE("fib6_del_route\n");
  898. /* Unlink it */
  899. *rtp = rt->u.dst.rt6_next;
  900. rt->rt6i_node = NULL;
  901. net->ipv6.rt6_stats->fib_rt_entries--;
  902. net->ipv6.rt6_stats->fib_discarded_routes++;
  903. /* Reset round-robin state, if necessary */
  904. if (fn->rr_ptr == rt)
  905. fn->rr_ptr = NULL;
  906. /* Adjust walkers */
  907. read_lock(&fib6_walker_lock);
  908. FOR_WALKERS(w) {
  909. if (w->state == FWS_C && w->leaf == rt) {
  910. RT6_TRACE("walker %p adjusted by delroute\n", w);
  911. w->leaf = rt->u.dst.rt6_next;
  912. if (w->leaf == NULL)
  913. w->state = FWS_U;
  914. }
  915. }
  916. read_unlock(&fib6_walker_lock);
  917. rt->u.dst.rt6_next = NULL;
  918. /* If it was last route, expunge its radix tree node */
  919. if (fn->leaf == NULL) {
  920. fn->fn_flags &= ~RTN_RTINFO;
  921. net->ipv6.rt6_stats->fib_route_nodes--;
  922. fn = fib6_repair_tree(net, fn);
  923. }
  924. if (atomic_read(&rt->rt6i_ref) != 1) {
  925. /* This route is used as dummy address holder in some split
  926. * nodes. It is not leaked, but it still holds other resources,
  927. * which must be released in time. So, scan ascendant nodes
  928. * and replace dummy references to this route with references
  929. * to still alive ones.
  930. */
  931. while (fn) {
  932. if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
  933. fn->leaf = fib6_find_prefix(net, fn);
  934. atomic_inc(&fn->leaf->rt6i_ref);
  935. rt6_release(rt);
  936. }
  937. fn = fn->parent;
  938. }
  939. /* No more references are possible at this point. */
  940. BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
  941. }
  942. inet6_rt_notify(RTM_DELROUTE, rt, info);
  943. rt6_release(rt);
  944. }
  945. int fib6_del(struct rt6_info *rt, struct nl_info *info)
  946. {
  947. struct net *net = info->nl_net;
  948. struct fib6_node *fn = rt->rt6i_node;
  949. struct rt6_info **rtp;
  950. #if RT6_DEBUG >= 2
  951. if (rt->u.dst.obsolete>0) {
  952. BUG_TRAP(fn==NULL);
  953. return -ENOENT;
  954. }
  955. #endif
  956. if (fn == NULL || rt == net->ipv6.ip6_null_entry)
  957. return -ENOENT;
  958. BUG_TRAP(fn->fn_flags&RTN_RTINFO);
  959. if (!(rt->rt6i_flags&RTF_CACHE)) {
  960. struct fib6_node *pn = fn;
  961. #ifdef CONFIG_IPV6_SUBTREES
  962. /* clones of this route might be in another subtree */
  963. if (rt->rt6i_src.plen) {
  964. while (!(pn->fn_flags&RTN_ROOT))
  965. pn = pn->parent;
  966. pn = pn->parent;
  967. }
  968. #endif
  969. fib6_prune_clones(info->nl_net, pn, rt);
  970. }
  971. /*
  972. * Walk the leaf entries looking for ourself
  973. */
  974. for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.dst.rt6_next) {
  975. if (*rtp == rt) {
  976. fib6_del_route(fn, rtp, info);
  977. return 0;
  978. }
  979. }
  980. return -ENOENT;
  981. }
  982. /*
  983. * Tree traversal function.
  984. *
  985. * Certainly, it is not interrupt safe.
  986. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  987. * It means, that we can modify tree during walking
  988. * and use this function for garbage collection, clone pruning,
  989. * cleaning tree when a device goes down etc. etc.
  990. *
  991. * It guarantees that every node will be traversed,
  992. * and that it will be traversed only once.
  993. *
  994. * Callback function w->func may return:
  995. * 0 -> continue walking.
  996. * positive value -> walking is suspended (used by tree dumps,
  997. * and probably by gc, if it will be split to several slices)
  998. * negative value -> terminate walking.
  999. *
  1000. * The function itself returns:
  1001. * 0 -> walk is complete.
  1002. * >0 -> walk is incomplete (i.e. suspended)
  1003. * <0 -> walk is terminated by an error.
  1004. */
  1005. static int fib6_walk_continue(struct fib6_walker_t *w)
  1006. {
  1007. struct fib6_node *fn, *pn;
  1008. for (;;) {
  1009. fn = w->node;
  1010. if (fn == NULL)
  1011. return 0;
  1012. if (w->prune && fn != w->root &&
  1013. fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
  1014. w->state = FWS_C;
  1015. w->leaf = fn->leaf;
  1016. }
  1017. switch (w->state) {
  1018. #ifdef CONFIG_IPV6_SUBTREES
  1019. case FWS_S:
  1020. if (FIB6_SUBTREE(fn)) {
  1021. w->node = FIB6_SUBTREE(fn);
  1022. continue;
  1023. }
  1024. w->state = FWS_L;
  1025. #endif
  1026. case FWS_L:
  1027. if (fn->left) {
  1028. w->node = fn->left;
  1029. w->state = FWS_INIT;
  1030. continue;
  1031. }
  1032. w->state = FWS_R;
  1033. case FWS_R:
  1034. if (fn->right) {
  1035. w->node = fn->right;
  1036. w->state = FWS_INIT;
  1037. continue;
  1038. }
  1039. w->state = FWS_C;
  1040. w->leaf = fn->leaf;
  1041. case FWS_C:
  1042. if (w->leaf && fn->fn_flags&RTN_RTINFO) {
  1043. int err = w->func(w);
  1044. if (err)
  1045. return err;
  1046. continue;
  1047. }
  1048. w->state = FWS_U;
  1049. case FWS_U:
  1050. if (fn == w->root)
  1051. return 0;
  1052. pn = fn->parent;
  1053. w->node = pn;
  1054. #ifdef CONFIG_IPV6_SUBTREES
  1055. if (FIB6_SUBTREE(pn) == fn) {
  1056. BUG_TRAP(fn->fn_flags&RTN_ROOT);
  1057. w->state = FWS_L;
  1058. continue;
  1059. }
  1060. #endif
  1061. if (pn->left == fn) {
  1062. w->state = FWS_R;
  1063. continue;
  1064. }
  1065. if (pn->right == fn) {
  1066. w->state = FWS_C;
  1067. w->leaf = w->node->leaf;
  1068. continue;
  1069. }
  1070. #if RT6_DEBUG >= 2
  1071. BUG_TRAP(0);
  1072. #endif
  1073. }
  1074. }
  1075. }
  1076. static int fib6_walk(struct fib6_walker_t *w)
  1077. {
  1078. int res;
  1079. w->state = FWS_INIT;
  1080. w->node = w->root;
  1081. fib6_walker_link(w);
  1082. res = fib6_walk_continue(w);
  1083. if (res <= 0)
  1084. fib6_walker_unlink(w);
  1085. return res;
  1086. }
  1087. static int fib6_clean_node(struct fib6_walker_t *w)
  1088. {
  1089. int res;
  1090. struct rt6_info *rt;
  1091. struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
  1092. struct nl_info info = {
  1093. .nl_net = c->net,
  1094. };
  1095. for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
  1096. res = c->func(rt, c->arg);
  1097. if (res < 0) {
  1098. w->leaf = rt;
  1099. res = fib6_del(rt, &info);
  1100. if (res) {
  1101. #if RT6_DEBUG >= 2
  1102. printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
  1103. #endif
  1104. continue;
  1105. }
  1106. return 0;
  1107. }
  1108. BUG_TRAP(res==0);
  1109. }
  1110. w->leaf = rt;
  1111. return 0;
  1112. }
  1113. /*
  1114. * Convenient frontend to tree walker.
  1115. *
  1116. * func is called on each route.
  1117. * It may return -1 -> delete this route.
  1118. * 0 -> continue walking
  1119. *
  1120. * prune==1 -> only immediate children of node (certainly,
  1121. * ignoring pure split nodes) will be scanned.
  1122. */
  1123. static void fib6_clean_tree(struct net *net, struct fib6_node *root,
  1124. int (*func)(struct rt6_info *, void *arg),
  1125. int prune, void *arg)
  1126. {
  1127. struct fib6_cleaner_t c;
  1128. c.w.root = root;
  1129. c.w.func = fib6_clean_node;
  1130. c.w.prune = prune;
  1131. c.func = func;
  1132. c.arg = arg;
  1133. c.net = net;
  1134. fib6_walk(&c.w);
  1135. }
  1136. void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
  1137. int prune, void *arg)
  1138. {
  1139. struct fib6_table *table;
  1140. struct hlist_node *node;
  1141. struct hlist_head *head;
  1142. unsigned int h;
  1143. rcu_read_lock();
  1144. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  1145. head = &net->ipv6.fib_table_hash[h];
  1146. hlist_for_each_entry_rcu(table, node, head, tb6_hlist) {
  1147. write_lock_bh(&table->tb6_lock);
  1148. fib6_clean_tree(net, &table->tb6_root,
  1149. func, prune, arg);
  1150. write_unlock_bh(&table->tb6_lock);
  1151. }
  1152. }
  1153. rcu_read_unlock();
  1154. }
  1155. static int fib6_prune_clone(struct rt6_info *rt, void *arg)
  1156. {
  1157. if (rt->rt6i_flags & RTF_CACHE) {
  1158. RT6_TRACE("pruning clone %p\n", rt);
  1159. return -1;
  1160. }
  1161. return 0;
  1162. }
  1163. static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
  1164. struct rt6_info *rt)
  1165. {
  1166. fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
  1167. }
  1168. /*
  1169. * Garbage collection
  1170. */
  1171. static struct fib6_gc_args
  1172. {
  1173. int timeout;
  1174. int more;
  1175. } gc_args;
  1176. static int fib6_age(struct rt6_info *rt, void *arg)
  1177. {
  1178. unsigned long now = jiffies;
  1179. /*
  1180. * check addrconf expiration here.
  1181. * Routes are expired even if they are in use.
  1182. *
  1183. * Also age clones. Note, that clones are aged out
  1184. * only if they are not in use now.
  1185. */
  1186. if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
  1187. if (time_after(now, rt->rt6i_expires)) {
  1188. RT6_TRACE("expiring %p\n", rt);
  1189. return -1;
  1190. }
  1191. gc_args.more++;
  1192. } else if (rt->rt6i_flags & RTF_CACHE) {
  1193. if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
  1194. time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
  1195. RT6_TRACE("aging clone %p\n", rt);
  1196. return -1;
  1197. } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
  1198. (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
  1199. RT6_TRACE("purging route %p via non-router but gateway\n",
  1200. rt);
  1201. return -1;
  1202. }
  1203. gc_args.more++;
  1204. }
  1205. return 0;
  1206. }
  1207. static DEFINE_SPINLOCK(fib6_gc_lock);
  1208. void fib6_run_gc(unsigned long expires, struct net *net)
  1209. {
  1210. if (expires != ~0UL) {
  1211. spin_lock_bh(&fib6_gc_lock);
  1212. gc_args.timeout = expires ? (int)expires :
  1213. net->ipv6.sysctl.ip6_rt_gc_interval;
  1214. } else {
  1215. if (!spin_trylock_bh(&fib6_gc_lock)) {
  1216. mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
  1217. return;
  1218. }
  1219. gc_args.timeout = net->ipv6.sysctl.ip6_rt_gc_interval;
  1220. }
  1221. gc_args.more = icmp6_dst_gc();
  1222. fib6_clean_all(net, fib6_age, 0, NULL);
  1223. if (gc_args.more)
  1224. mod_timer(&net->ipv6.ip6_fib_timer,
  1225. round_jiffies(jiffies
  1226. + net->ipv6.sysctl.ip6_rt_gc_interval));
  1227. else
  1228. del_timer(&net->ipv6.ip6_fib_timer);
  1229. spin_unlock_bh(&fib6_gc_lock);
  1230. }
  1231. static void fib6_gc_timer_cb(unsigned long arg)
  1232. {
  1233. fib6_run_gc(0, (struct net *)arg);
  1234. }
  1235. static int fib6_net_init(struct net *net)
  1236. {
  1237. setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
  1238. net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
  1239. if (!net->ipv6.rt6_stats)
  1240. goto out_timer;
  1241. net->ipv6.fib_table_hash = kcalloc(FIB_TABLE_HASHSZ,
  1242. sizeof(*net->ipv6.fib_table_hash),
  1243. GFP_KERNEL);
  1244. if (!net->ipv6.fib_table_hash)
  1245. goto out_rt6_stats;
  1246. net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
  1247. GFP_KERNEL);
  1248. if (!net->ipv6.fib6_main_tbl)
  1249. goto out_fib_table_hash;
  1250. net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
  1251. net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1252. net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
  1253. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1254. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1255. net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
  1256. GFP_KERNEL);
  1257. if (!net->ipv6.fib6_local_tbl)
  1258. goto out_fib6_main_tbl;
  1259. net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
  1260. net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
  1261. net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
  1262. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1263. #endif
  1264. fib6_tables_init(net);
  1265. return 0;
  1266. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1267. out_fib6_main_tbl:
  1268. kfree(net->ipv6.fib6_main_tbl);
  1269. #endif
  1270. out_fib_table_hash:
  1271. kfree(net->ipv6.fib_table_hash);
  1272. out_rt6_stats:
  1273. kfree(net->ipv6.rt6_stats);
  1274. out_timer:
  1275. return -ENOMEM;
  1276. }
  1277. static void fib6_net_exit(struct net *net)
  1278. {
  1279. rt6_ifdown(net, NULL);
  1280. del_timer_sync(&net->ipv6.ip6_fib_timer);
  1281. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1282. kfree(net->ipv6.fib6_local_tbl);
  1283. #endif
  1284. kfree(net->ipv6.fib6_main_tbl);
  1285. kfree(net->ipv6.fib_table_hash);
  1286. kfree(net->ipv6.rt6_stats);
  1287. }
  1288. static struct pernet_operations fib6_net_ops = {
  1289. .init = fib6_net_init,
  1290. .exit = fib6_net_exit,
  1291. };
  1292. int __init fib6_init(void)
  1293. {
  1294. int ret = -ENOMEM;
  1295. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1296. sizeof(struct fib6_node),
  1297. 0, SLAB_HWCACHE_ALIGN,
  1298. NULL);
  1299. if (!fib6_node_kmem)
  1300. goto out;
  1301. ret = register_pernet_subsys(&fib6_net_ops);
  1302. if (ret)
  1303. goto out_kmem_cache_create;
  1304. ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib);
  1305. if (ret)
  1306. goto out_unregister_subsys;
  1307. out:
  1308. return ret;
  1309. out_unregister_subsys:
  1310. unregister_pernet_subsys(&fib6_net_ops);
  1311. out_kmem_cache_create:
  1312. kmem_cache_destroy(fib6_node_kmem);
  1313. goto out;
  1314. }
  1315. void fib6_gc_cleanup(void)
  1316. {
  1317. unregister_pernet_subsys(&fib6_net_ops);
  1318. kmem_cache_destroy(fib6_node_kmem);
  1319. }