ip6_fib.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403
  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. * Changes:
  14. * Yuji SEKIYA @USAGI: Support default route on router node;
  15. * remove ip6_null_entry from the top of
  16. * routing table.
  17. * Ville Nuorvala: Fixed routing subtrees.
  18. */
  19. #define pr_fmt(fmt) "IPv6: " fmt
  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. #include <linux/slab.h>
  29. #include <net/ipv6.h>
  30. #include <net/ndisc.h>
  31. #include <net/addrconf.h>
  32. #include <net/lwtunnel.h>
  33. #include <net/fib_notifier.h>
  34. #include <net/ip6_fib.h>
  35. #include <net/ip6_route.h>
  36. static struct kmem_cache *fib6_node_kmem __read_mostly;
  37. struct fib6_cleaner {
  38. struct fib6_walker w;
  39. struct net *net;
  40. int (*func)(struct fib6_info *, void *arg);
  41. int sernum;
  42. void *arg;
  43. };
  44. #ifdef CONFIG_IPV6_SUBTREES
  45. #define FWS_INIT FWS_S
  46. #else
  47. #define FWS_INIT FWS_L
  48. #endif
  49. static struct fib6_info *fib6_find_prefix(struct net *net,
  50. struct fib6_table *table,
  51. struct fib6_node *fn);
  52. static struct fib6_node *fib6_repair_tree(struct net *net,
  53. struct fib6_table *table,
  54. struct fib6_node *fn);
  55. static int fib6_walk(struct net *net, struct fib6_walker *w);
  56. static int fib6_walk_continue(struct fib6_walker *w);
  57. /*
  58. * A routing update causes an increase of the serial number on the
  59. * affected subtree. This allows for cached routes to be asynchronously
  60. * tested when modifications are made to the destination cache as a
  61. * result of redirects, path MTU changes, etc.
  62. */
  63. static void fib6_gc_timer_cb(struct timer_list *t);
  64. #define FOR_WALKERS(net, w) \
  65. list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
  66. static void fib6_walker_link(struct net *net, struct fib6_walker *w)
  67. {
  68. write_lock_bh(&net->ipv6.fib6_walker_lock);
  69. list_add(&w->lh, &net->ipv6.fib6_walkers);
  70. write_unlock_bh(&net->ipv6.fib6_walker_lock);
  71. }
  72. static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
  73. {
  74. write_lock_bh(&net->ipv6.fib6_walker_lock);
  75. list_del(&w->lh);
  76. write_unlock_bh(&net->ipv6.fib6_walker_lock);
  77. }
  78. static int fib6_new_sernum(struct net *net)
  79. {
  80. int new, old;
  81. do {
  82. old = atomic_read(&net->ipv6.fib6_sernum);
  83. new = old < INT_MAX ? old + 1 : 1;
  84. } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
  85. old, new) != old);
  86. return new;
  87. }
  88. enum {
  89. FIB6_NO_SERNUM_CHANGE = 0,
  90. };
  91. void fib6_update_sernum(struct net *net, struct fib6_info *f6i)
  92. {
  93. struct fib6_node *fn;
  94. fn = rcu_dereference_protected(f6i->fib6_node,
  95. lockdep_is_held(&f6i->fib6_table->tb6_lock));
  96. if (fn)
  97. fn->fn_sernum = fib6_new_sernum(net);
  98. }
  99. /*
  100. * Auxiliary address test functions for the radix tree.
  101. *
  102. * These assume a 32bit processor (although it will work on
  103. * 64bit processors)
  104. */
  105. /*
  106. * test bit
  107. */
  108. #if defined(__LITTLE_ENDIAN)
  109. # define BITOP_BE32_SWIZZLE (0x1F & ~7)
  110. #else
  111. # define BITOP_BE32_SWIZZLE 0
  112. #endif
  113. static __be32 addr_bit_set(const void *token, int fn_bit)
  114. {
  115. const __be32 *addr = token;
  116. /*
  117. * Here,
  118. * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
  119. * is optimized version of
  120. * htonl(1 << ((~fn_bit)&0x1F))
  121. * See include/asm-generic/bitops/le.h.
  122. */
  123. return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
  124. addr[fn_bit >> 5];
  125. }
  126. struct fib6_info *fib6_info_alloc(gfp_t gfp_flags)
  127. {
  128. struct fib6_info *f6i;
  129. f6i = kzalloc(sizeof(*f6i), gfp_flags);
  130. if (!f6i)
  131. return NULL;
  132. f6i->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
  133. if (!f6i->rt6i_pcpu) {
  134. kfree(f6i);
  135. return NULL;
  136. }
  137. INIT_LIST_HEAD(&f6i->fib6_siblings);
  138. f6i->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
  139. atomic_inc(&f6i->fib6_ref);
  140. return f6i;
  141. }
  142. void fib6_info_destroy(struct fib6_info *f6i)
  143. {
  144. struct rt6_exception_bucket *bucket;
  145. struct dst_metrics *m;
  146. WARN_ON(f6i->fib6_node);
  147. bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket, 1);
  148. if (bucket) {
  149. f6i->rt6i_exception_bucket = NULL;
  150. kfree(bucket);
  151. }
  152. if (f6i->rt6i_pcpu) {
  153. int cpu;
  154. for_each_possible_cpu(cpu) {
  155. struct rt6_info **ppcpu_rt;
  156. struct rt6_info *pcpu_rt;
  157. ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
  158. pcpu_rt = *ppcpu_rt;
  159. if (pcpu_rt) {
  160. dst_dev_put(&pcpu_rt->dst);
  161. dst_release(&pcpu_rt->dst);
  162. *ppcpu_rt = NULL;
  163. }
  164. }
  165. }
  166. if (f6i->fib6_nh.nh_dev)
  167. dev_put(f6i->fib6_nh.nh_dev);
  168. m = f6i->fib6_metrics;
  169. if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
  170. kfree(m);
  171. kfree(f6i);
  172. }
  173. EXPORT_SYMBOL_GPL(fib6_info_destroy);
  174. static struct fib6_node *node_alloc(struct net *net)
  175. {
  176. struct fib6_node *fn;
  177. fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
  178. if (fn)
  179. net->ipv6.rt6_stats->fib_nodes++;
  180. return fn;
  181. }
  182. static void node_free_immediate(struct net *net, struct fib6_node *fn)
  183. {
  184. kmem_cache_free(fib6_node_kmem, fn);
  185. net->ipv6.rt6_stats->fib_nodes--;
  186. }
  187. static void node_free_rcu(struct rcu_head *head)
  188. {
  189. struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
  190. kmem_cache_free(fib6_node_kmem, fn);
  191. }
  192. static void node_free(struct net *net, struct fib6_node *fn)
  193. {
  194. call_rcu(&fn->rcu, node_free_rcu);
  195. net->ipv6.rt6_stats->fib_nodes--;
  196. }
  197. static void fib6_free_table(struct fib6_table *table)
  198. {
  199. inetpeer_invalidate_tree(&table->tb6_peers);
  200. kfree(table);
  201. }
  202. static void fib6_link_table(struct net *net, struct fib6_table *tb)
  203. {
  204. unsigned int h;
  205. /*
  206. * Initialize table lock at a single place to give lockdep a key,
  207. * tables aren't visible prior to being linked to the list.
  208. */
  209. spin_lock_init(&tb->tb6_lock);
  210. h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
  211. /*
  212. * No protection necessary, this is the only list mutatation
  213. * operation, tables never disappear once they exist.
  214. */
  215. hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
  216. }
  217. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  218. static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
  219. {
  220. struct fib6_table *table;
  221. table = kzalloc(sizeof(*table), GFP_ATOMIC);
  222. if (table) {
  223. table->tb6_id = id;
  224. rcu_assign_pointer(table->tb6_root.leaf,
  225. net->ipv6.fib6_null_entry);
  226. table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  227. inet_peer_base_init(&table->tb6_peers);
  228. }
  229. return table;
  230. }
  231. struct fib6_table *fib6_new_table(struct net *net, u32 id)
  232. {
  233. struct fib6_table *tb;
  234. if (id == 0)
  235. id = RT6_TABLE_MAIN;
  236. tb = fib6_get_table(net, id);
  237. if (tb)
  238. return tb;
  239. tb = fib6_alloc_table(net, id);
  240. if (tb)
  241. fib6_link_table(net, tb);
  242. return tb;
  243. }
  244. EXPORT_SYMBOL_GPL(fib6_new_table);
  245. struct fib6_table *fib6_get_table(struct net *net, u32 id)
  246. {
  247. struct fib6_table *tb;
  248. struct hlist_head *head;
  249. unsigned int h;
  250. if (id == 0)
  251. id = RT6_TABLE_MAIN;
  252. h = id & (FIB6_TABLE_HASHSZ - 1);
  253. rcu_read_lock();
  254. head = &net->ipv6.fib_table_hash[h];
  255. hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
  256. if (tb->tb6_id == id) {
  257. rcu_read_unlock();
  258. return tb;
  259. }
  260. }
  261. rcu_read_unlock();
  262. return NULL;
  263. }
  264. EXPORT_SYMBOL_GPL(fib6_get_table);
  265. static void __net_init fib6_tables_init(struct net *net)
  266. {
  267. fib6_link_table(net, net->ipv6.fib6_main_tbl);
  268. fib6_link_table(net, net->ipv6.fib6_local_tbl);
  269. }
  270. #else
  271. struct fib6_table *fib6_new_table(struct net *net, u32 id)
  272. {
  273. return fib6_get_table(net, id);
  274. }
  275. struct fib6_table *fib6_get_table(struct net *net, u32 id)
  276. {
  277. return net->ipv6.fib6_main_tbl;
  278. }
  279. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
  280. const struct sk_buff *skb,
  281. int flags, pol_lookup_t lookup)
  282. {
  283. struct rt6_info *rt;
  284. rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
  285. if (rt->dst.error == -EAGAIN) {
  286. ip6_rt_put(rt);
  287. rt = net->ipv6.ip6_null_entry;
  288. dst_hold(&rt->dst);
  289. }
  290. return &rt->dst;
  291. }
  292. /* called with rcu lock held; no reference taken on fib6_info */
  293. struct fib6_info *fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
  294. int flags)
  295. {
  296. return fib6_table_lookup(net, net->ipv6.fib6_main_tbl, oif, fl6, flags);
  297. }
  298. static void __net_init fib6_tables_init(struct net *net)
  299. {
  300. fib6_link_table(net, net->ipv6.fib6_main_tbl);
  301. }
  302. #endif
  303. unsigned int fib6_tables_seq_read(struct net *net)
  304. {
  305. unsigned int h, fib_seq = 0;
  306. rcu_read_lock();
  307. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  308. struct hlist_head *head = &net->ipv6.fib_table_hash[h];
  309. struct fib6_table *tb;
  310. hlist_for_each_entry_rcu(tb, head, tb6_hlist)
  311. fib_seq += tb->fib_seq;
  312. }
  313. rcu_read_unlock();
  314. return fib_seq;
  315. }
  316. static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
  317. enum fib_event_type event_type,
  318. struct fib6_info *rt)
  319. {
  320. struct fib6_entry_notifier_info info = {
  321. .rt = rt,
  322. };
  323. return call_fib6_notifier(nb, net, event_type, &info.info);
  324. }
  325. static int call_fib6_entry_notifiers(struct net *net,
  326. enum fib_event_type event_type,
  327. struct fib6_info *rt,
  328. struct netlink_ext_ack *extack)
  329. {
  330. struct fib6_entry_notifier_info info = {
  331. .info.extack = extack,
  332. .rt = rt,
  333. };
  334. rt->fib6_table->fib_seq++;
  335. return call_fib6_notifiers(net, event_type, &info.info);
  336. }
  337. struct fib6_dump_arg {
  338. struct net *net;
  339. struct notifier_block *nb;
  340. };
  341. static void fib6_rt_dump(struct fib6_info *rt, struct fib6_dump_arg *arg)
  342. {
  343. if (rt == arg->net->ipv6.fib6_null_entry)
  344. return;
  345. call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
  346. }
  347. static int fib6_node_dump(struct fib6_walker *w)
  348. {
  349. struct fib6_info *rt;
  350. for_each_fib6_walker_rt(w)
  351. fib6_rt_dump(rt, w->args);
  352. w->leaf = NULL;
  353. return 0;
  354. }
  355. static void fib6_table_dump(struct net *net, struct fib6_table *tb,
  356. struct fib6_walker *w)
  357. {
  358. w->root = &tb->tb6_root;
  359. spin_lock_bh(&tb->tb6_lock);
  360. fib6_walk(net, w);
  361. spin_unlock_bh(&tb->tb6_lock);
  362. }
  363. /* Called with rcu_read_lock() */
  364. int fib6_tables_dump(struct net *net, struct notifier_block *nb)
  365. {
  366. struct fib6_dump_arg arg;
  367. struct fib6_walker *w;
  368. unsigned int h;
  369. w = kzalloc(sizeof(*w), GFP_ATOMIC);
  370. if (!w)
  371. return -ENOMEM;
  372. w->func = fib6_node_dump;
  373. arg.net = net;
  374. arg.nb = nb;
  375. w->args = &arg;
  376. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  377. struct hlist_head *head = &net->ipv6.fib_table_hash[h];
  378. struct fib6_table *tb;
  379. hlist_for_each_entry_rcu(tb, head, tb6_hlist)
  380. fib6_table_dump(net, tb, w);
  381. }
  382. kfree(w);
  383. return 0;
  384. }
  385. static int fib6_dump_node(struct fib6_walker *w)
  386. {
  387. int res;
  388. struct fib6_info *rt;
  389. for_each_fib6_walker_rt(w) {
  390. res = rt6_dump_route(rt, w->args);
  391. if (res < 0) {
  392. /* Frame is full, suspend walking */
  393. w->leaf = rt;
  394. return 1;
  395. }
  396. /* Multipath routes are dumped in one route with the
  397. * RTA_MULTIPATH attribute. Jump 'rt' to point to the
  398. * last sibling of this route (no need to dump the
  399. * sibling routes again)
  400. */
  401. if (rt->fib6_nsiblings)
  402. rt = list_last_entry(&rt->fib6_siblings,
  403. struct fib6_info,
  404. fib6_siblings);
  405. }
  406. w->leaf = NULL;
  407. return 0;
  408. }
  409. static void fib6_dump_end(struct netlink_callback *cb)
  410. {
  411. struct net *net = sock_net(cb->skb->sk);
  412. struct fib6_walker *w = (void *)cb->args[2];
  413. if (w) {
  414. if (cb->args[4]) {
  415. cb->args[4] = 0;
  416. fib6_walker_unlink(net, w);
  417. }
  418. cb->args[2] = 0;
  419. kfree(w);
  420. }
  421. cb->done = (void *)cb->args[3];
  422. cb->args[1] = 3;
  423. }
  424. static int fib6_dump_done(struct netlink_callback *cb)
  425. {
  426. fib6_dump_end(cb);
  427. return cb->done ? cb->done(cb) : 0;
  428. }
  429. static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
  430. struct netlink_callback *cb)
  431. {
  432. struct net *net = sock_net(skb->sk);
  433. struct fib6_walker *w;
  434. int res;
  435. w = (void *)cb->args[2];
  436. w->root = &table->tb6_root;
  437. if (cb->args[4] == 0) {
  438. w->count = 0;
  439. w->skip = 0;
  440. spin_lock_bh(&table->tb6_lock);
  441. res = fib6_walk(net, w);
  442. spin_unlock_bh(&table->tb6_lock);
  443. if (res > 0) {
  444. cb->args[4] = 1;
  445. cb->args[5] = w->root->fn_sernum;
  446. }
  447. } else {
  448. if (cb->args[5] != w->root->fn_sernum) {
  449. /* Begin at the root if the tree changed */
  450. cb->args[5] = w->root->fn_sernum;
  451. w->state = FWS_INIT;
  452. w->node = w->root;
  453. w->skip = w->count;
  454. } else
  455. w->skip = 0;
  456. spin_lock_bh(&table->tb6_lock);
  457. res = fib6_walk_continue(w);
  458. spin_unlock_bh(&table->tb6_lock);
  459. if (res <= 0) {
  460. fib6_walker_unlink(net, w);
  461. cb->args[4] = 0;
  462. }
  463. }
  464. return res;
  465. }
  466. static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  467. {
  468. struct net *net = sock_net(skb->sk);
  469. unsigned int h, s_h;
  470. unsigned int e = 0, s_e;
  471. struct rt6_rtnl_dump_arg arg;
  472. struct fib6_walker *w;
  473. struct fib6_table *tb;
  474. struct hlist_head *head;
  475. int res = 0;
  476. s_h = cb->args[0];
  477. s_e = cb->args[1];
  478. w = (void *)cb->args[2];
  479. if (!w) {
  480. /* New dump:
  481. *
  482. * 1. hook callback destructor.
  483. */
  484. cb->args[3] = (long)cb->done;
  485. cb->done = fib6_dump_done;
  486. /*
  487. * 2. allocate and initialize walker.
  488. */
  489. w = kzalloc(sizeof(*w), GFP_ATOMIC);
  490. if (!w)
  491. return -ENOMEM;
  492. w->func = fib6_dump_node;
  493. cb->args[2] = (long)w;
  494. }
  495. arg.skb = skb;
  496. arg.cb = cb;
  497. arg.net = net;
  498. w->args = &arg;
  499. rcu_read_lock();
  500. for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
  501. e = 0;
  502. head = &net->ipv6.fib_table_hash[h];
  503. hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
  504. if (e < s_e)
  505. goto next;
  506. res = fib6_dump_table(tb, skb, cb);
  507. if (res != 0)
  508. goto out;
  509. next:
  510. e++;
  511. }
  512. }
  513. out:
  514. rcu_read_unlock();
  515. cb->args[1] = e;
  516. cb->args[0] = h;
  517. res = res < 0 ? res : skb->len;
  518. if (res <= 0)
  519. fib6_dump_end(cb);
  520. return res;
  521. }
  522. void fib6_metric_set(struct fib6_info *f6i, int metric, u32 val)
  523. {
  524. if (!f6i)
  525. return;
  526. if (f6i->fib6_metrics == &dst_default_metrics) {
  527. struct dst_metrics *p = kzalloc(sizeof(*p), GFP_ATOMIC);
  528. if (!p)
  529. return;
  530. refcount_set(&p->refcnt, 1);
  531. f6i->fib6_metrics = p;
  532. }
  533. f6i->fib6_metrics->metrics[metric - 1] = val;
  534. }
  535. /*
  536. * Routing Table
  537. *
  538. * return the appropriate node for a routing tree "add" operation
  539. * by either creating and inserting or by returning an existing
  540. * node.
  541. */
  542. static struct fib6_node *fib6_add_1(struct net *net,
  543. struct fib6_table *table,
  544. struct fib6_node *root,
  545. struct in6_addr *addr, int plen,
  546. int offset, int allow_create,
  547. int replace_required,
  548. struct netlink_ext_ack *extack)
  549. {
  550. struct fib6_node *fn, *in, *ln;
  551. struct fib6_node *pn = NULL;
  552. struct rt6key *key;
  553. int bit;
  554. __be32 dir = 0;
  555. RT6_TRACE("fib6_add_1\n");
  556. /* insert node in tree */
  557. fn = root;
  558. do {
  559. struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
  560. lockdep_is_held(&table->tb6_lock));
  561. key = (struct rt6key *)((u8 *)leaf + offset);
  562. /*
  563. * Prefix match
  564. */
  565. if (plen < fn->fn_bit ||
  566. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
  567. if (!allow_create) {
  568. if (replace_required) {
  569. NL_SET_ERR_MSG(extack,
  570. "Can not replace route - no match found");
  571. pr_warn("Can't replace route, no match found\n");
  572. return ERR_PTR(-ENOENT);
  573. }
  574. pr_warn("NLM_F_CREATE should be set when creating new route\n");
  575. }
  576. goto insert_above;
  577. }
  578. /*
  579. * Exact match ?
  580. */
  581. if (plen == fn->fn_bit) {
  582. /* clean up an intermediate node */
  583. if (!(fn->fn_flags & RTN_RTINFO)) {
  584. RCU_INIT_POINTER(fn->leaf, NULL);
  585. fib6_info_release(leaf);
  586. /* remove null_entry in the root node */
  587. } else if (fn->fn_flags & RTN_TL_ROOT &&
  588. rcu_access_pointer(fn->leaf) ==
  589. net->ipv6.fib6_null_entry) {
  590. RCU_INIT_POINTER(fn->leaf, NULL);
  591. }
  592. return fn;
  593. }
  594. /*
  595. * We have more bits to go
  596. */
  597. /* Try to walk down on tree. */
  598. dir = addr_bit_set(addr, fn->fn_bit);
  599. pn = fn;
  600. fn = dir ?
  601. rcu_dereference_protected(fn->right,
  602. lockdep_is_held(&table->tb6_lock)) :
  603. rcu_dereference_protected(fn->left,
  604. lockdep_is_held(&table->tb6_lock));
  605. } while (fn);
  606. if (!allow_create) {
  607. /* We should not create new node because
  608. * NLM_F_REPLACE was specified without NLM_F_CREATE
  609. * I assume it is safe to require NLM_F_CREATE when
  610. * REPLACE flag is used! Later we may want to remove the
  611. * check for replace_required, because according
  612. * to netlink specification, NLM_F_CREATE
  613. * MUST be specified if new route is created.
  614. * That would keep IPv6 consistent with IPv4
  615. */
  616. if (replace_required) {
  617. NL_SET_ERR_MSG(extack,
  618. "Can not replace route - no match found");
  619. pr_warn("Can't replace route, no match found\n");
  620. return ERR_PTR(-ENOENT);
  621. }
  622. pr_warn("NLM_F_CREATE should be set when creating new route\n");
  623. }
  624. /*
  625. * We walked to the bottom of tree.
  626. * Create new leaf node without children.
  627. */
  628. ln = node_alloc(net);
  629. if (!ln)
  630. return ERR_PTR(-ENOMEM);
  631. ln->fn_bit = plen;
  632. RCU_INIT_POINTER(ln->parent, pn);
  633. if (dir)
  634. rcu_assign_pointer(pn->right, ln);
  635. else
  636. rcu_assign_pointer(pn->left, ln);
  637. return ln;
  638. insert_above:
  639. /*
  640. * split since we don't have a common prefix anymore or
  641. * we have a less significant route.
  642. * we've to insert an intermediate node on the list
  643. * this new node will point to the one we need to create
  644. * and the current
  645. */
  646. pn = rcu_dereference_protected(fn->parent,
  647. lockdep_is_held(&table->tb6_lock));
  648. /* find 1st bit in difference between the 2 addrs.
  649. See comment in __ipv6_addr_diff: bit may be an invalid value,
  650. but if it is >= plen, the value is ignored in any case.
  651. */
  652. bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
  653. /*
  654. * (intermediate)[in]
  655. * / \
  656. * (new leaf node)[ln] (old node)[fn]
  657. */
  658. if (plen > bit) {
  659. in = node_alloc(net);
  660. ln = node_alloc(net);
  661. if (!in || !ln) {
  662. if (in)
  663. node_free_immediate(net, in);
  664. if (ln)
  665. node_free_immediate(net, ln);
  666. return ERR_PTR(-ENOMEM);
  667. }
  668. /*
  669. * new intermediate node.
  670. * RTN_RTINFO will
  671. * be off since that an address that chooses one of
  672. * the branches would not match less specific routes
  673. * in the other branch
  674. */
  675. in->fn_bit = bit;
  676. RCU_INIT_POINTER(in->parent, pn);
  677. in->leaf = fn->leaf;
  678. atomic_inc(&rcu_dereference_protected(in->leaf,
  679. lockdep_is_held(&table->tb6_lock))->fib6_ref);
  680. /* update parent pointer */
  681. if (dir)
  682. rcu_assign_pointer(pn->right, in);
  683. else
  684. rcu_assign_pointer(pn->left, in);
  685. ln->fn_bit = plen;
  686. RCU_INIT_POINTER(ln->parent, in);
  687. rcu_assign_pointer(fn->parent, in);
  688. if (addr_bit_set(addr, bit)) {
  689. rcu_assign_pointer(in->right, ln);
  690. rcu_assign_pointer(in->left, fn);
  691. } else {
  692. rcu_assign_pointer(in->left, ln);
  693. rcu_assign_pointer(in->right, fn);
  694. }
  695. } else { /* plen <= bit */
  696. /*
  697. * (new leaf node)[ln]
  698. * / \
  699. * (old node)[fn] NULL
  700. */
  701. ln = node_alloc(net);
  702. if (!ln)
  703. return ERR_PTR(-ENOMEM);
  704. ln->fn_bit = plen;
  705. RCU_INIT_POINTER(ln->parent, pn);
  706. if (addr_bit_set(&key->addr, plen))
  707. RCU_INIT_POINTER(ln->right, fn);
  708. else
  709. RCU_INIT_POINTER(ln->left, fn);
  710. rcu_assign_pointer(fn->parent, ln);
  711. if (dir)
  712. rcu_assign_pointer(pn->right, ln);
  713. else
  714. rcu_assign_pointer(pn->left, ln);
  715. }
  716. return ln;
  717. }
  718. static void fib6_drop_pcpu_from(struct fib6_info *f6i,
  719. const struct fib6_table *table)
  720. {
  721. int cpu;
  722. /* release the reference to this fib entry from
  723. * all of its cached pcpu routes
  724. */
  725. for_each_possible_cpu(cpu) {
  726. struct rt6_info **ppcpu_rt;
  727. struct rt6_info *pcpu_rt;
  728. ppcpu_rt = per_cpu_ptr(f6i->rt6i_pcpu, cpu);
  729. pcpu_rt = *ppcpu_rt;
  730. if (pcpu_rt) {
  731. struct fib6_info *from;
  732. from = rcu_dereference_protected(pcpu_rt->from,
  733. lockdep_is_held(&table->tb6_lock));
  734. rcu_assign_pointer(pcpu_rt->from, NULL);
  735. fib6_info_release(from);
  736. }
  737. }
  738. }
  739. static void fib6_purge_rt(struct fib6_info *rt, struct fib6_node *fn,
  740. struct net *net)
  741. {
  742. struct fib6_table *table = rt->fib6_table;
  743. if (atomic_read(&rt->fib6_ref) != 1) {
  744. /* This route is used as dummy address holder in some split
  745. * nodes. It is not leaked, but it still holds other resources,
  746. * which must be released in time. So, scan ascendant nodes
  747. * and replace dummy references to this route with references
  748. * to still alive ones.
  749. */
  750. while (fn) {
  751. struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
  752. lockdep_is_held(&table->tb6_lock));
  753. struct fib6_info *new_leaf;
  754. if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
  755. new_leaf = fib6_find_prefix(net, table, fn);
  756. atomic_inc(&new_leaf->fib6_ref);
  757. rcu_assign_pointer(fn->leaf, new_leaf);
  758. fib6_info_release(rt);
  759. }
  760. fn = rcu_dereference_protected(fn->parent,
  761. lockdep_is_held(&table->tb6_lock));
  762. }
  763. if (rt->rt6i_pcpu)
  764. fib6_drop_pcpu_from(rt, table);
  765. }
  766. }
  767. /*
  768. * Insert routing information in a node.
  769. */
  770. static int fib6_add_rt2node(struct fib6_node *fn, struct fib6_info *rt,
  771. struct nl_info *info,
  772. struct netlink_ext_ack *extack)
  773. {
  774. struct fib6_info *leaf = rcu_dereference_protected(fn->leaf,
  775. lockdep_is_held(&rt->fib6_table->tb6_lock));
  776. enum fib_event_type event = FIB_EVENT_ENTRY_ADD;
  777. struct fib6_info *iter = NULL, *match = NULL;
  778. struct fib6_info __rcu **ins;
  779. int replace = (info->nlh &&
  780. (info->nlh->nlmsg_flags & NLM_F_REPLACE));
  781. int append = (info->nlh &&
  782. (info->nlh->nlmsg_flags & NLM_F_APPEND));
  783. int add = (!info->nlh ||
  784. (info->nlh->nlmsg_flags & NLM_F_CREATE));
  785. int found = 0;
  786. u16 nlflags = NLM_F_EXCL;
  787. int err;
  788. if (append)
  789. nlflags |= NLM_F_APPEND;
  790. ins = &fn->leaf;
  791. for (iter = leaf; iter;
  792. iter = rcu_dereference_protected(iter->fib6_next,
  793. lockdep_is_held(&rt->fib6_table->tb6_lock))) {
  794. /*
  795. * Search for duplicates
  796. */
  797. if (iter->fib6_metric == rt->fib6_metric) {
  798. /*
  799. * Same priority level
  800. */
  801. if (info->nlh &&
  802. (info->nlh->nlmsg_flags & NLM_F_EXCL))
  803. return -EEXIST;
  804. nlflags &= ~NLM_F_EXCL;
  805. if (replace) {
  806. found++;
  807. break;
  808. }
  809. if (rt6_duplicate_nexthop(iter, rt)) {
  810. if (rt->fib6_nsiblings)
  811. rt->fib6_nsiblings = 0;
  812. if (!(iter->fib6_flags & RTF_EXPIRES))
  813. return -EEXIST;
  814. if (!(rt->fib6_flags & RTF_EXPIRES))
  815. fib6_clean_expires(iter);
  816. else
  817. fib6_set_expires(iter, rt->expires);
  818. fib6_metric_set(iter, RTAX_MTU, rt->fib6_pmtu);
  819. return -EEXIST;
  820. }
  821. /* first route that matches */
  822. if (!match)
  823. match = iter;
  824. }
  825. if (iter->fib6_metric > rt->fib6_metric)
  826. break;
  827. ins = &iter->fib6_next;
  828. }
  829. /* Reset round-robin state, if necessary */
  830. if (ins == &fn->leaf)
  831. fn->rr_ptr = NULL;
  832. /* Link this route to others same route. */
  833. if (append && match) {
  834. struct fib6_info *sibling, *temp_sibling;
  835. if (rt->fib6_flags & RTF_REJECT) {
  836. NL_SET_ERR_MSG(extack,
  837. "Can not append a REJECT route");
  838. return -EINVAL;
  839. } else if (match->fib6_flags & RTF_REJECT) {
  840. NL_SET_ERR_MSG(extack,
  841. "Can not append to a REJECT route");
  842. return -EINVAL;
  843. }
  844. event = FIB_EVENT_ENTRY_APPEND;
  845. rt->fib6_nsiblings = match->fib6_nsiblings;
  846. list_add_tail(&rt->fib6_siblings, &match->fib6_siblings);
  847. match->fib6_nsiblings++;
  848. /* For each sibling in the list, increment the counter of
  849. * siblings. BUG() if counters does not match, list of siblings
  850. * is broken!
  851. */
  852. list_for_each_entry_safe(sibling, temp_sibling,
  853. &match->fib6_siblings, fib6_siblings) {
  854. sibling->fib6_nsiblings++;
  855. BUG_ON(sibling->fib6_nsiblings != match->fib6_nsiblings);
  856. }
  857. rt6_multipath_rebalance(match);
  858. }
  859. /*
  860. * insert node
  861. */
  862. if (!replace) {
  863. if (!add)
  864. pr_warn("NLM_F_CREATE should be set when creating new route\n");
  865. add:
  866. nlflags |= NLM_F_CREATE;
  867. err = call_fib6_entry_notifiers(info->nl_net, event, rt,
  868. extack);
  869. if (err)
  870. return err;
  871. rcu_assign_pointer(rt->fib6_next, iter);
  872. atomic_inc(&rt->fib6_ref);
  873. rcu_assign_pointer(rt->fib6_node, fn);
  874. rcu_assign_pointer(*ins, rt);
  875. if (!info->skip_notify)
  876. inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
  877. info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
  878. if (!(fn->fn_flags & RTN_RTINFO)) {
  879. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  880. fn->fn_flags |= RTN_RTINFO;
  881. }
  882. } else {
  883. struct fib6_info *tmp;
  884. if (!found) {
  885. if (add)
  886. goto add;
  887. pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
  888. return -ENOENT;
  889. }
  890. err = call_fib6_entry_notifiers(info->nl_net,
  891. FIB_EVENT_ENTRY_REPLACE,
  892. rt, extack);
  893. if (err)
  894. return err;
  895. /* if route being replaced has siblings, set tmp to
  896. * last one, otherwise tmp is current route. this is
  897. * used to set fib6_next for new route
  898. */
  899. if (iter->fib6_nsiblings)
  900. tmp = list_last_entry(&iter->fib6_siblings,
  901. struct fib6_info,
  902. fib6_siblings);
  903. else
  904. tmp = iter;
  905. /* insert new route */
  906. atomic_inc(&rt->fib6_ref);
  907. rcu_assign_pointer(rt->fib6_node, fn);
  908. rt->fib6_next = tmp->fib6_next;
  909. rcu_assign_pointer(*ins, rt);
  910. if (!info->skip_notify)
  911. inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
  912. if (!(fn->fn_flags & RTN_RTINFO)) {
  913. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  914. fn->fn_flags |= RTN_RTINFO;
  915. }
  916. /* delete old route */
  917. rt = iter;
  918. if (rt->fib6_nsiblings) {
  919. struct fib6_info *tmp;
  920. /* Replacing an ECMP route, remove all siblings */
  921. list_for_each_entry_safe(iter, tmp, &rt->fib6_siblings,
  922. fib6_siblings) {
  923. iter->fib6_node = NULL;
  924. fib6_purge_rt(iter, fn, info->nl_net);
  925. if (rcu_access_pointer(fn->rr_ptr) == iter)
  926. fn->rr_ptr = NULL;
  927. fib6_info_release(iter);
  928. rt->fib6_nsiblings--;
  929. info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
  930. }
  931. }
  932. WARN_ON(rt->fib6_nsiblings != 0);
  933. rt->fib6_node = NULL;
  934. fib6_purge_rt(rt, fn, info->nl_net);
  935. if (rcu_access_pointer(fn->rr_ptr) == rt)
  936. fn->rr_ptr = NULL;
  937. fib6_info_release(rt);
  938. }
  939. return 0;
  940. }
  941. static void fib6_start_gc(struct net *net, struct fib6_info *rt)
  942. {
  943. if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
  944. (rt->fib6_flags & RTF_EXPIRES))
  945. mod_timer(&net->ipv6.ip6_fib_timer,
  946. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  947. }
  948. void fib6_force_start_gc(struct net *net)
  949. {
  950. if (!timer_pending(&net->ipv6.ip6_fib_timer))
  951. mod_timer(&net->ipv6.ip6_fib_timer,
  952. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  953. }
  954. static void __fib6_update_sernum_upto_root(struct fib6_info *rt,
  955. int sernum)
  956. {
  957. struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
  958. lockdep_is_held(&rt->fib6_table->tb6_lock));
  959. /* paired with smp_rmb() in rt6_get_cookie_safe() */
  960. smp_wmb();
  961. while (fn) {
  962. fn->fn_sernum = sernum;
  963. fn = rcu_dereference_protected(fn->parent,
  964. lockdep_is_held(&rt->fib6_table->tb6_lock));
  965. }
  966. }
  967. void fib6_update_sernum_upto_root(struct net *net, struct fib6_info *rt)
  968. {
  969. __fib6_update_sernum_upto_root(rt, fib6_new_sernum(net));
  970. }
  971. /*
  972. * Add routing information to the routing tree.
  973. * <destination addr>/<source addr>
  974. * with source addr info in sub-trees
  975. * Need to own table->tb6_lock
  976. */
  977. int fib6_add(struct fib6_node *root, struct fib6_info *rt,
  978. struct nl_info *info, struct netlink_ext_ack *extack)
  979. {
  980. struct fib6_table *table = rt->fib6_table;
  981. struct fib6_node *fn, *pn = NULL;
  982. int err = -ENOMEM;
  983. int allow_create = 1;
  984. int replace_required = 0;
  985. int sernum = fib6_new_sernum(info->nl_net);
  986. if (info->nlh) {
  987. if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
  988. allow_create = 0;
  989. if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
  990. replace_required = 1;
  991. }
  992. if (!allow_create && !replace_required)
  993. pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
  994. fn = fib6_add_1(info->nl_net, table, root,
  995. &rt->fib6_dst.addr, rt->fib6_dst.plen,
  996. offsetof(struct fib6_info, fib6_dst), allow_create,
  997. replace_required, extack);
  998. if (IS_ERR(fn)) {
  999. err = PTR_ERR(fn);
  1000. fn = NULL;
  1001. goto out;
  1002. }
  1003. pn = fn;
  1004. #ifdef CONFIG_IPV6_SUBTREES
  1005. if (rt->fib6_src.plen) {
  1006. struct fib6_node *sn;
  1007. if (!rcu_access_pointer(fn->subtree)) {
  1008. struct fib6_node *sfn;
  1009. /*
  1010. * Create subtree.
  1011. *
  1012. * fn[main tree]
  1013. * |
  1014. * sfn[subtree root]
  1015. * \
  1016. * sn[new leaf node]
  1017. */
  1018. /* Create subtree root node */
  1019. sfn = node_alloc(info->nl_net);
  1020. if (!sfn)
  1021. goto failure;
  1022. atomic_inc(&info->nl_net->ipv6.fib6_null_entry->fib6_ref);
  1023. rcu_assign_pointer(sfn->leaf,
  1024. info->nl_net->ipv6.fib6_null_entry);
  1025. sfn->fn_flags = RTN_ROOT;
  1026. /* Now add the first leaf node to new subtree */
  1027. sn = fib6_add_1(info->nl_net, table, sfn,
  1028. &rt->fib6_src.addr, rt->fib6_src.plen,
  1029. offsetof(struct fib6_info, fib6_src),
  1030. allow_create, replace_required, extack);
  1031. if (IS_ERR(sn)) {
  1032. /* If it is failed, discard just allocated
  1033. root, and then (in failure) stale node
  1034. in main tree.
  1035. */
  1036. node_free_immediate(info->nl_net, sfn);
  1037. err = PTR_ERR(sn);
  1038. goto failure;
  1039. }
  1040. /* Now link new subtree to main tree */
  1041. rcu_assign_pointer(sfn->parent, fn);
  1042. rcu_assign_pointer(fn->subtree, sfn);
  1043. } else {
  1044. sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
  1045. &rt->fib6_src.addr, rt->fib6_src.plen,
  1046. offsetof(struct fib6_info, fib6_src),
  1047. allow_create, replace_required, extack);
  1048. if (IS_ERR(sn)) {
  1049. err = PTR_ERR(sn);
  1050. goto failure;
  1051. }
  1052. }
  1053. if (!rcu_access_pointer(fn->leaf)) {
  1054. if (fn->fn_flags & RTN_TL_ROOT) {
  1055. /* put back null_entry for root node */
  1056. rcu_assign_pointer(fn->leaf,
  1057. info->nl_net->ipv6.fib6_null_entry);
  1058. } else {
  1059. atomic_inc(&rt->fib6_ref);
  1060. rcu_assign_pointer(fn->leaf, rt);
  1061. }
  1062. }
  1063. fn = sn;
  1064. }
  1065. #endif
  1066. err = fib6_add_rt2node(fn, rt, info, extack);
  1067. if (!err) {
  1068. __fib6_update_sernum_upto_root(rt, sernum);
  1069. fib6_start_gc(info->nl_net, rt);
  1070. }
  1071. out:
  1072. if (err) {
  1073. #ifdef CONFIG_IPV6_SUBTREES
  1074. /*
  1075. * If fib6_add_1 has cleared the old leaf pointer in the
  1076. * super-tree leaf node we have to find a new one for it.
  1077. */
  1078. if (pn != fn) {
  1079. struct fib6_info *pn_leaf =
  1080. rcu_dereference_protected(pn->leaf,
  1081. lockdep_is_held(&table->tb6_lock));
  1082. if (pn_leaf == rt) {
  1083. pn_leaf = NULL;
  1084. RCU_INIT_POINTER(pn->leaf, NULL);
  1085. fib6_info_release(rt);
  1086. }
  1087. if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
  1088. pn_leaf = fib6_find_prefix(info->nl_net, table,
  1089. pn);
  1090. #if RT6_DEBUG >= 2
  1091. if (!pn_leaf) {
  1092. WARN_ON(!pn_leaf);
  1093. pn_leaf =
  1094. info->nl_net->ipv6.fib6_null_entry;
  1095. }
  1096. #endif
  1097. fib6_info_hold(pn_leaf);
  1098. rcu_assign_pointer(pn->leaf, pn_leaf);
  1099. }
  1100. }
  1101. #endif
  1102. goto failure;
  1103. }
  1104. return err;
  1105. failure:
  1106. /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
  1107. * 1. fn is an intermediate node and we failed to add the new
  1108. * route to it in both subtree creation failure and fib6_add_rt2node()
  1109. * failure case.
  1110. * 2. fn is the root node in the table and we fail to add the first
  1111. * default route to it.
  1112. */
  1113. if (fn &&
  1114. (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
  1115. (fn->fn_flags & RTN_TL_ROOT &&
  1116. !rcu_access_pointer(fn->leaf))))
  1117. fib6_repair_tree(info->nl_net, table, fn);
  1118. return err;
  1119. }
  1120. /*
  1121. * Routing tree lookup
  1122. *
  1123. */
  1124. struct lookup_args {
  1125. int offset; /* key offset on fib6_info */
  1126. const struct in6_addr *addr; /* search key */
  1127. };
  1128. static struct fib6_node *fib6_node_lookup_1(struct fib6_node *root,
  1129. struct lookup_args *args)
  1130. {
  1131. struct fib6_node *fn;
  1132. __be32 dir;
  1133. if (unlikely(args->offset == 0))
  1134. return NULL;
  1135. /*
  1136. * Descend on a tree
  1137. */
  1138. fn = root;
  1139. for (;;) {
  1140. struct fib6_node *next;
  1141. dir = addr_bit_set(args->addr, fn->fn_bit);
  1142. next = dir ? rcu_dereference(fn->right) :
  1143. rcu_dereference(fn->left);
  1144. if (next) {
  1145. fn = next;
  1146. continue;
  1147. }
  1148. break;
  1149. }
  1150. while (fn) {
  1151. struct fib6_node *subtree = FIB6_SUBTREE(fn);
  1152. if (subtree || fn->fn_flags & RTN_RTINFO) {
  1153. struct fib6_info *leaf = rcu_dereference(fn->leaf);
  1154. struct rt6key *key;
  1155. if (!leaf)
  1156. goto backtrack;
  1157. key = (struct rt6key *) ((u8 *)leaf + args->offset);
  1158. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  1159. #ifdef CONFIG_IPV6_SUBTREES
  1160. if (subtree) {
  1161. struct fib6_node *sfn;
  1162. sfn = fib6_node_lookup_1(subtree,
  1163. args + 1);
  1164. if (!sfn)
  1165. goto backtrack;
  1166. fn = sfn;
  1167. }
  1168. #endif
  1169. if (fn->fn_flags & RTN_RTINFO)
  1170. return fn;
  1171. }
  1172. }
  1173. backtrack:
  1174. if (fn->fn_flags & RTN_ROOT)
  1175. break;
  1176. fn = rcu_dereference(fn->parent);
  1177. }
  1178. return NULL;
  1179. }
  1180. /* called with rcu_read_lock() held
  1181. */
  1182. struct fib6_node *fib6_node_lookup(struct fib6_node *root,
  1183. const struct in6_addr *daddr,
  1184. const struct in6_addr *saddr)
  1185. {
  1186. struct fib6_node *fn;
  1187. struct lookup_args args[] = {
  1188. {
  1189. .offset = offsetof(struct fib6_info, fib6_dst),
  1190. .addr = daddr,
  1191. },
  1192. #ifdef CONFIG_IPV6_SUBTREES
  1193. {
  1194. .offset = offsetof(struct fib6_info, fib6_src),
  1195. .addr = saddr,
  1196. },
  1197. #endif
  1198. {
  1199. .offset = 0, /* sentinel */
  1200. }
  1201. };
  1202. fn = fib6_node_lookup_1(root, daddr ? args : args + 1);
  1203. if (!fn || fn->fn_flags & RTN_TL_ROOT)
  1204. fn = root;
  1205. return fn;
  1206. }
  1207. /*
  1208. * Get node with specified destination prefix (and source prefix,
  1209. * if subtrees are used)
  1210. * exact_match == true means we try to find fn with exact match of
  1211. * the passed in prefix addr
  1212. * exact_match == false means we try to find fn with longest prefix
  1213. * match of the passed in prefix addr. This is useful for finding fn
  1214. * for cached route as it will be stored in the exception table under
  1215. * the node with longest prefix length.
  1216. */
  1217. static struct fib6_node *fib6_locate_1(struct fib6_node *root,
  1218. const struct in6_addr *addr,
  1219. int plen, int offset,
  1220. bool exact_match)
  1221. {
  1222. struct fib6_node *fn, *prev = NULL;
  1223. for (fn = root; fn ; ) {
  1224. struct fib6_info *leaf = rcu_dereference(fn->leaf);
  1225. struct rt6key *key;
  1226. /* This node is being deleted */
  1227. if (!leaf) {
  1228. if (plen <= fn->fn_bit)
  1229. goto out;
  1230. else
  1231. goto next;
  1232. }
  1233. key = (struct rt6key *)((u8 *)leaf + offset);
  1234. /*
  1235. * Prefix match
  1236. */
  1237. if (plen < fn->fn_bit ||
  1238. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  1239. goto out;
  1240. if (plen == fn->fn_bit)
  1241. return fn;
  1242. prev = fn;
  1243. next:
  1244. /*
  1245. * We have more bits to go
  1246. */
  1247. if (addr_bit_set(addr, fn->fn_bit))
  1248. fn = rcu_dereference(fn->right);
  1249. else
  1250. fn = rcu_dereference(fn->left);
  1251. }
  1252. out:
  1253. if (exact_match)
  1254. return NULL;
  1255. else
  1256. return prev;
  1257. }
  1258. struct fib6_node *fib6_locate(struct fib6_node *root,
  1259. const struct in6_addr *daddr, int dst_len,
  1260. const struct in6_addr *saddr, int src_len,
  1261. bool exact_match)
  1262. {
  1263. struct fib6_node *fn;
  1264. fn = fib6_locate_1(root, daddr, dst_len,
  1265. offsetof(struct fib6_info, fib6_dst),
  1266. exact_match);
  1267. #ifdef CONFIG_IPV6_SUBTREES
  1268. if (src_len) {
  1269. WARN_ON(saddr == NULL);
  1270. if (fn) {
  1271. struct fib6_node *subtree = FIB6_SUBTREE(fn);
  1272. if (subtree) {
  1273. fn = fib6_locate_1(subtree, saddr, src_len,
  1274. offsetof(struct fib6_info, fib6_src),
  1275. exact_match);
  1276. }
  1277. }
  1278. }
  1279. #endif
  1280. if (fn && fn->fn_flags & RTN_RTINFO)
  1281. return fn;
  1282. return NULL;
  1283. }
  1284. /*
  1285. * Deletion
  1286. *
  1287. */
  1288. static struct fib6_info *fib6_find_prefix(struct net *net,
  1289. struct fib6_table *table,
  1290. struct fib6_node *fn)
  1291. {
  1292. struct fib6_node *child_left, *child_right;
  1293. if (fn->fn_flags & RTN_ROOT)
  1294. return net->ipv6.fib6_null_entry;
  1295. while (fn) {
  1296. child_left = rcu_dereference_protected(fn->left,
  1297. lockdep_is_held(&table->tb6_lock));
  1298. child_right = rcu_dereference_protected(fn->right,
  1299. lockdep_is_held(&table->tb6_lock));
  1300. if (child_left)
  1301. return rcu_dereference_protected(child_left->leaf,
  1302. lockdep_is_held(&table->tb6_lock));
  1303. if (child_right)
  1304. return rcu_dereference_protected(child_right->leaf,
  1305. lockdep_is_held(&table->tb6_lock));
  1306. fn = FIB6_SUBTREE(fn);
  1307. }
  1308. return NULL;
  1309. }
  1310. /*
  1311. * Called to trim the tree of intermediate nodes when possible. "fn"
  1312. * is the node we want to try and remove.
  1313. * Need to own table->tb6_lock
  1314. */
  1315. static struct fib6_node *fib6_repair_tree(struct net *net,
  1316. struct fib6_table *table,
  1317. struct fib6_node *fn)
  1318. {
  1319. int children;
  1320. int nstate;
  1321. struct fib6_node *child;
  1322. struct fib6_walker *w;
  1323. int iter = 0;
  1324. /* Set fn->leaf to null_entry for root node. */
  1325. if (fn->fn_flags & RTN_TL_ROOT) {
  1326. rcu_assign_pointer(fn->leaf, net->ipv6.fib6_null_entry);
  1327. return fn;
  1328. }
  1329. for (;;) {
  1330. struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
  1331. lockdep_is_held(&table->tb6_lock));
  1332. struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
  1333. lockdep_is_held(&table->tb6_lock));
  1334. struct fib6_node *pn = rcu_dereference_protected(fn->parent,
  1335. lockdep_is_held(&table->tb6_lock));
  1336. struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
  1337. lockdep_is_held(&table->tb6_lock));
  1338. struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
  1339. lockdep_is_held(&table->tb6_lock));
  1340. struct fib6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
  1341. lockdep_is_held(&table->tb6_lock));
  1342. struct fib6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
  1343. lockdep_is_held(&table->tb6_lock));
  1344. struct fib6_info *new_fn_leaf;
  1345. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  1346. iter++;
  1347. WARN_ON(fn->fn_flags & RTN_RTINFO);
  1348. WARN_ON(fn->fn_flags & RTN_TL_ROOT);
  1349. WARN_ON(fn_leaf);
  1350. children = 0;
  1351. child = NULL;
  1352. if (fn_r)
  1353. child = fn_r, children |= 1;
  1354. if (fn_l)
  1355. child = fn_l, children |= 2;
  1356. if (children == 3 || FIB6_SUBTREE(fn)
  1357. #ifdef CONFIG_IPV6_SUBTREES
  1358. /* Subtree root (i.e. fn) may have one child */
  1359. || (children && fn->fn_flags & RTN_ROOT)
  1360. #endif
  1361. ) {
  1362. new_fn_leaf = fib6_find_prefix(net, table, fn);
  1363. #if RT6_DEBUG >= 2
  1364. if (!new_fn_leaf) {
  1365. WARN_ON(!new_fn_leaf);
  1366. new_fn_leaf = net->ipv6.fib6_null_entry;
  1367. }
  1368. #endif
  1369. fib6_info_hold(new_fn_leaf);
  1370. rcu_assign_pointer(fn->leaf, new_fn_leaf);
  1371. return pn;
  1372. }
  1373. #ifdef CONFIG_IPV6_SUBTREES
  1374. if (FIB6_SUBTREE(pn) == fn) {
  1375. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  1376. RCU_INIT_POINTER(pn->subtree, NULL);
  1377. nstate = FWS_L;
  1378. } else {
  1379. WARN_ON(fn->fn_flags & RTN_ROOT);
  1380. #endif
  1381. if (pn_r == fn)
  1382. rcu_assign_pointer(pn->right, child);
  1383. else if (pn_l == fn)
  1384. rcu_assign_pointer(pn->left, child);
  1385. #if RT6_DEBUG >= 2
  1386. else
  1387. WARN_ON(1);
  1388. #endif
  1389. if (child)
  1390. rcu_assign_pointer(child->parent, pn);
  1391. nstate = FWS_R;
  1392. #ifdef CONFIG_IPV6_SUBTREES
  1393. }
  1394. #endif
  1395. read_lock(&net->ipv6.fib6_walker_lock);
  1396. FOR_WALKERS(net, w) {
  1397. if (!child) {
  1398. if (w->node == fn) {
  1399. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  1400. w->node = pn;
  1401. w->state = nstate;
  1402. }
  1403. } else {
  1404. if (w->node == fn) {
  1405. w->node = child;
  1406. if (children&2) {
  1407. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  1408. w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
  1409. } else {
  1410. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  1411. w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
  1412. }
  1413. }
  1414. }
  1415. }
  1416. read_unlock(&net->ipv6.fib6_walker_lock);
  1417. node_free(net, fn);
  1418. if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
  1419. return pn;
  1420. RCU_INIT_POINTER(pn->leaf, NULL);
  1421. fib6_info_release(pn_leaf);
  1422. fn = pn;
  1423. }
  1424. }
  1425. static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
  1426. struct fib6_info __rcu **rtp, struct nl_info *info)
  1427. {
  1428. struct fib6_walker *w;
  1429. struct fib6_info *rt = rcu_dereference_protected(*rtp,
  1430. lockdep_is_held(&table->tb6_lock));
  1431. struct net *net = info->nl_net;
  1432. RT6_TRACE("fib6_del_route\n");
  1433. /* Unlink it */
  1434. *rtp = rt->fib6_next;
  1435. rt->fib6_node = NULL;
  1436. net->ipv6.rt6_stats->fib_rt_entries--;
  1437. net->ipv6.rt6_stats->fib_discarded_routes++;
  1438. /* Flush all cached dst in exception table */
  1439. rt6_flush_exceptions(rt);
  1440. /* Reset round-robin state, if necessary */
  1441. if (rcu_access_pointer(fn->rr_ptr) == rt)
  1442. fn->rr_ptr = NULL;
  1443. /* Remove this entry from other siblings */
  1444. if (rt->fib6_nsiblings) {
  1445. struct fib6_info *sibling, *next_sibling;
  1446. list_for_each_entry_safe(sibling, next_sibling,
  1447. &rt->fib6_siblings, fib6_siblings)
  1448. sibling->fib6_nsiblings--;
  1449. rt->fib6_nsiblings = 0;
  1450. list_del_init(&rt->fib6_siblings);
  1451. rt6_multipath_rebalance(next_sibling);
  1452. }
  1453. /* Adjust walkers */
  1454. read_lock(&net->ipv6.fib6_walker_lock);
  1455. FOR_WALKERS(net, w) {
  1456. if (w->state == FWS_C && w->leaf == rt) {
  1457. RT6_TRACE("walker %p adjusted by delroute\n", w);
  1458. w->leaf = rcu_dereference_protected(rt->fib6_next,
  1459. lockdep_is_held(&table->tb6_lock));
  1460. if (!w->leaf)
  1461. w->state = FWS_U;
  1462. }
  1463. }
  1464. read_unlock(&net->ipv6.fib6_walker_lock);
  1465. /* If it was last route, call fib6_repair_tree() to:
  1466. * 1. For root node, put back null_entry as how the table was created.
  1467. * 2. For other nodes, expunge its radix tree node.
  1468. */
  1469. if (!rcu_access_pointer(fn->leaf)) {
  1470. if (!(fn->fn_flags & RTN_TL_ROOT)) {
  1471. fn->fn_flags &= ~RTN_RTINFO;
  1472. net->ipv6.rt6_stats->fib_route_nodes--;
  1473. }
  1474. fn = fib6_repair_tree(net, table, fn);
  1475. }
  1476. fib6_purge_rt(rt, fn, net);
  1477. call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
  1478. if (!info->skip_notify)
  1479. inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
  1480. fib6_info_release(rt);
  1481. }
  1482. /* Need to own table->tb6_lock */
  1483. int fib6_del(struct fib6_info *rt, struct nl_info *info)
  1484. {
  1485. struct fib6_node *fn = rcu_dereference_protected(rt->fib6_node,
  1486. lockdep_is_held(&rt->fib6_table->tb6_lock));
  1487. struct fib6_table *table = rt->fib6_table;
  1488. struct net *net = info->nl_net;
  1489. struct fib6_info __rcu **rtp;
  1490. struct fib6_info __rcu **rtp_next;
  1491. if (!fn || rt == net->ipv6.fib6_null_entry)
  1492. return -ENOENT;
  1493. WARN_ON(!(fn->fn_flags & RTN_RTINFO));
  1494. /*
  1495. * Walk the leaf entries looking for ourself
  1496. */
  1497. for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
  1498. struct fib6_info *cur = rcu_dereference_protected(*rtp,
  1499. lockdep_is_held(&table->tb6_lock));
  1500. if (rt == cur) {
  1501. fib6_del_route(table, fn, rtp, info);
  1502. return 0;
  1503. }
  1504. rtp_next = &cur->fib6_next;
  1505. }
  1506. return -ENOENT;
  1507. }
  1508. /*
  1509. * Tree traversal function.
  1510. *
  1511. * Certainly, it is not interrupt safe.
  1512. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  1513. * It means, that we can modify tree during walking
  1514. * and use this function for garbage collection, clone pruning,
  1515. * cleaning tree when a device goes down etc. etc.
  1516. *
  1517. * It guarantees that every node will be traversed,
  1518. * and that it will be traversed only once.
  1519. *
  1520. * Callback function w->func may return:
  1521. * 0 -> continue walking.
  1522. * positive value -> walking is suspended (used by tree dumps,
  1523. * and probably by gc, if it will be split to several slices)
  1524. * negative value -> terminate walking.
  1525. *
  1526. * The function itself returns:
  1527. * 0 -> walk is complete.
  1528. * >0 -> walk is incomplete (i.e. suspended)
  1529. * <0 -> walk is terminated by an error.
  1530. *
  1531. * This function is called with tb6_lock held.
  1532. */
  1533. static int fib6_walk_continue(struct fib6_walker *w)
  1534. {
  1535. struct fib6_node *fn, *pn, *left, *right;
  1536. /* w->root should always be table->tb6_root */
  1537. WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
  1538. for (;;) {
  1539. fn = w->node;
  1540. if (!fn)
  1541. return 0;
  1542. switch (w->state) {
  1543. #ifdef CONFIG_IPV6_SUBTREES
  1544. case FWS_S:
  1545. if (FIB6_SUBTREE(fn)) {
  1546. w->node = FIB6_SUBTREE(fn);
  1547. continue;
  1548. }
  1549. w->state = FWS_L;
  1550. #endif
  1551. /* fall through */
  1552. case FWS_L:
  1553. left = rcu_dereference_protected(fn->left, 1);
  1554. if (left) {
  1555. w->node = left;
  1556. w->state = FWS_INIT;
  1557. continue;
  1558. }
  1559. w->state = FWS_R;
  1560. /* fall through */
  1561. case FWS_R:
  1562. right = rcu_dereference_protected(fn->right, 1);
  1563. if (right) {
  1564. w->node = right;
  1565. w->state = FWS_INIT;
  1566. continue;
  1567. }
  1568. w->state = FWS_C;
  1569. w->leaf = rcu_dereference_protected(fn->leaf, 1);
  1570. /* fall through */
  1571. case FWS_C:
  1572. if (w->leaf && fn->fn_flags & RTN_RTINFO) {
  1573. int err;
  1574. if (w->skip) {
  1575. w->skip--;
  1576. goto skip;
  1577. }
  1578. err = w->func(w);
  1579. if (err)
  1580. return err;
  1581. w->count++;
  1582. continue;
  1583. }
  1584. skip:
  1585. w->state = FWS_U;
  1586. /* fall through */
  1587. case FWS_U:
  1588. if (fn == w->root)
  1589. return 0;
  1590. pn = rcu_dereference_protected(fn->parent, 1);
  1591. left = rcu_dereference_protected(pn->left, 1);
  1592. right = rcu_dereference_protected(pn->right, 1);
  1593. w->node = pn;
  1594. #ifdef CONFIG_IPV6_SUBTREES
  1595. if (FIB6_SUBTREE(pn) == fn) {
  1596. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  1597. w->state = FWS_L;
  1598. continue;
  1599. }
  1600. #endif
  1601. if (left == fn) {
  1602. w->state = FWS_R;
  1603. continue;
  1604. }
  1605. if (right == fn) {
  1606. w->state = FWS_C;
  1607. w->leaf = rcu_dereference_protected(w->node->leaf, 1);
  1608. continue;
  1609. }
  1610. #if RT6_DEBUG >= 2
  1611. WARN_ON(1);
  1612. #endif
  1613. }
  1614. }
  1615. }
  1616. static int fib6_walk(struct net *net, struct fib6_walker *w)
  1617. {
  1618. int res;
  1619. w->state = FWS_INIT;
  1620. w->node = w->root;
  1621. fib6_walker_link(net, w);
  1622. res = fib6_walk_continue(w);
  1623. if (res <= 0)
  1624. fib6_walker_unlink(net, w);
  1625. return res;
  1626. }
  1627. static int fib6_clean_node(struct fib6_walker *w)
  1628. {
  1629. int res;
  1630. struct fib6_info *rt;
  1631. struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
  1632. struct nl_info info = {
  1633. .nl_net = c->net,
  1634. };
  1635. if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
  1636. w->node->fn_sernum != c->sernum)
  1637. w->node->fn_sernum = c->sernum;
  1638. if (!c->func) {
  1639. WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
  1640. w->leaf = NULL;
  1641. return 0;
  1642. }
  1643. for_each_fib6_walker_rt(w) {
  1644. res = c->func(rt, c->arg);
  1645. if (res == -1) {
  1646. w->leaf = rt;
  1647. res = fib6_del(rt, &info);
  1648. if (res) {
  1649. #if RT6_DEBUG >= 2
  1650. pr_debug("%s: del failed: rt=%p@%p err=%d\n",
  1651. __func__, rt,
  1652. rcu_access_pointer(rt->fib6_node),
  1653. res);
  1654. #endif
  1655. continue;
  1656. }
  1657. return 0;
  1658. } else if (res == -2) {
  1659. if (WARN_ON(!rt->fib6_nsiblings))
  1660. continue;
  1661. rt = list_last_entry(&rt->fib6_siblings,
  1662. struct fib6_info, fib6_siblings);
  1663. continue;
  1664. }
  1665. WARN_ON(res != 0);
  1666. }
  1667. w->leaf = rt;
  1668. return 0;
  1669. }
  1670. /*
  1671. * Convenient frontend to tree walker.
  1672. *
  1673. * func is called on each route.
  1674. * It may return -2 -> skip multipath route.
  1675. * -1 -> delete this route.
  1676. * 0 -> continue walking
  1677. */
  1678. static void fib6_clean_tree(struct net *net, struct fib6_node *root,
  1679. int (*func)(struct fib6_info *, void *arg),
  1680. int sernum, void *arg)
  1681. {
  1682. struct fib6_cleaner c;
  1683. c.w.root = root;
  1684. c.w.func = fib6_clean_node;
  1685. c.w.count = 0;
  1686. c.w.skip = 0;
  1687. c.func = func;
  1688. c.sernum = sernum;
  1689. c.arg = arg;
  1690. c.net = net;
  1691. fib6_walk(net, &c.w);
  1692. }
  1693. static void __fib6_clean_all(struct net *net,
  1694. int (*func)(struct fib6_info *, void *),
  1695. int sernum, void *arg)
  1696. {
  1697. struct fib6_table *table;
  1698. struct hlist_head *head;
  1699. unsigned int h;
  1700. rcu_read_lock();
  1701. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  1702. head = &net->ipv6.fib_table_hash[h];
  1703. hlist_for_each_entry_rcu(table, head, tb6_hlist) {
  1704. spin_lock_bh(&table->tb6_lock);
  1705. fib6_clean_tree(net, &table->tb6_root,
  1706. func, sernum, arg);
  1707. spin_unlock_bh(&table->tb6_lock);
  1708. }
  1709. }
  1710. rcu_read_unlock();
  1711. }
  1712. void fib6_clean_all(struct net *net, int (*func)(struct fib6_info *, void *),
  1713. void *arg)
  1714. {
  1715. __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
  1716. }
  1717. static void fib6_flush_trees(struct net *net)
  1718. {
  1719. int new_sernum = fib6_new_sernum(net);
  1720. __fib6_clean_all(net, NULL, new_sernum, NULL);
  1721. }
  1722. /*
  1723. * Garbage collection
  1724. */
  1725. static int fib6_age(struct fib6_info *rt, void *arg)
  1726. {
  1727. struct fib6_gc_args *gc_args = arg;
  1728. unsigned long now = jiffies;
  1729. /*
  1730. * check addrconf expiration here.
  1731. * Routes are expired even if they are in use.
  1732. */
  1733. if (rt->fib6_flags & RTF_EXPIRES && rt->expires) {
  1734. if (time_after(now, rt->expires)) {
  1735. RT6_TRACE("expiring %p\n", rt);
  1736. return -1;
  1737. }
  1738. gc_args->more++;
  1739. }
  1740. /* Also age clones in the exception table.
  1741. * Note, that clones are aged out
  1742. * only if they are not in use now.
  1743. */
  1744. rt6_age_exceptions(rt, gc_args, now);
  1745. return 0;
  1746. }
  1747. void fib6_run_gc(unsigned long expires, struct net *net, bool force)
  1748. {
  1749. struct fib6_gc_args gc_args;
  1750. unsigned long now;
  1751. if (force) {
  1752. spin_lock_bh(&net->ipv6.fib6_gc_lock);
  1753. } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
  1754. mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
  1755. return;
  1756. }
  1757. gc_args.timeout = expires ? (int)expires :
  1758. net->ipv6.sysctl.ip6_rt_gc_interval;
  1759. gc_args.more = 0;
  1760. fib6_clean_all(net, fib6_age, &gc_args);
  1761. now = jiffies;
  1762. net->ipv6.ip6_rt_last_gc = now;
  1763. if (gc_args.more)
  1764. mod_timer(&net->ipv6.ip6_fib_timer,
  1765. round_jiffies(now
  1766. + net->ipv6.sysctl.ip6_rt_gc_interval));
  1767. else
  1768. del_timer(&net->ipv6.ip6_fib_timer);
  1769. spin_unlock_bh(&net->ipv6.fib6_gc_lock);
  1770. }
  1771. static void fib6_gc_timer_cb(struct timer_list *t)
  1772. {
  1773. struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
  1774. fib6_run_gc(0, arg, true);
  1775. }
  1776. static int __net_init fib6_net_init(struct net *net)
  1777. {
  1778. size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
  1779. int err;
  1780. err = fib6_notifier_init(net);
  1781. if (err)
  1782. return err;
  1783. spin_lock_init(&net->ipv6.fib6_gc_lock);
  1784. rwlock_init(&net->ipv6.fib6_walker_lock);
  1785. INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
  1786. timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
  1787. net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
  1788. if (!net->ipv6.rt6_stats)
  1789. goto out_timer;
  1790. /* Avoid false sharing : Use at least a full cache line */
  1791. size = max_t(size_t, size, L1_CACHE_BYTES);
  1792. net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1793. if (!net->ipv6.fib_table_hash)
  1794. goto out_rt6_stats;
  1795. net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
  1796. GFP_KERNEL);
  1797. if (!net->ipv6.fib6_main_tbl)
  1798. goto out_fib_table_hash;
  1799. net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
  1800. rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
  1801. net->ipv6.fib6_null_entry);
  1802. net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
  1803. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1804. inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
  1805. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1806. net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
  1807. GFP_KERNEL);
  1808. if (!net->ipv6.fib6_local_tbl)
  1809. goto out_fib6_main_tbl;
  1810. net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
  1811. rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
  1812. net->ipv6.fib6_null_entry);
  1813. net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
  1814. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1815. inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
  1816. #endif
  1817. fib6_tables_init(net);
  1818. return 0;
  1819. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1820. out_fib6_main_tbl:
  1821. kfree(net->ipv6.fib6_main_tbl);
  1822. #endif
  1823. out_fib_table_hash:
  1824. kfree(net->ipv6.fib_table_hash);
  1825. out_rt6_stats:
  1826. kfree(net->ipv6.rt6_stats);
  1827. out_timer:
  1828. fib6_notifier_exit(net);
  1829. return -ENOMEM;
  1830. }
  1831. static void fib6_net_exit(struct net *net)
  1832. {
  1833. unsigned int i;
  1834. del_timer_sync(&net->ipv6.ip6_fib_timer);
  1835. for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
  1836. struct hlist_head *head = &net->ipv6.fib_table_hash[i];
  1837. struct hlist_node *tmp;
  1838. struct fib6_table *tb;
  1839. hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
  1840. hlist_del(&tb->tb6_hlist);
  1841. fib6_free_table(tb);
  1842. }
  1843. }
  1844. kfree(net->ipv6.fib_table_hash);
  1845. kfree(net->ipv6.rt6_stats);
  1846. fib6_notifier_exit(net);
  1847. }
  1848. static struct pernet_operations fib6_net_ops = {
  1849. .init = fib6_net_init,
  1850. .exit = fib6_net_exit,
  1851. };
  1852. int __init fib6_init(void)
  1853. {
  1854. int ret = -ENOMEM;
  1855. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1856. sizeof(struct fib6_node),
  1857. 0, SLAB_HWCACHE_ALIGN,
  1858. NULL);
  1859. if (!fib6_node_kmem)
  1860. goto out;
  1861. ret = register_pernet_subsys(&fib6_net_ops);
  1862. if (ret)
  1863. goto out_kmem_cache_create;
  1864. ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, NULL,
  1865. inet6_dump_fib, 0);
  1866. if (ret)
  1867. goto out_unregister_subsys;
  1868. __fib6_flush_trees = fib6_flush_trees;
  1869. out:
  1870. return ret;
  1871. out_unregister_subsys:
  1872. unregister_pernet_subsys(&fib6_net_ops);
  1873. out_kmem_cache_create:
  1874. kmem_cache_destroy(fib6_node_kmem);
  1875. goto out;
  1876. }
  1877. void fib6_gc_cleanup(void)
  1878. {
  1879. unregister_pernet_subsys(&fib6_net_ops);
  1880. kmem_cache_destroy(fib6_node_kmem);
  1881. }
  1882. #ifdef CONFIG_PROC_FS
  1883. static int ipv6_route_seq_show(struct seq_file *seq, void *v)
  1884. {
  1885. struct fib6_info *rt = v;
  1886. struct ipv6_route_iter *iter = seq->private;
  1887. const struct net_device *dev;
  1888. seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
  1889. #ifdef CONFIG_IPV6_SUBTREES
  1890. seq_printf(seq, "%pi6 %02x ", &rt->fib6_src.addr, rt->fib6_src.plen);
  1891. #else
  1892. seq_puts(seq, "00000000000000000000000000000000 00 ");
  1893. #endif
  1894. if (rt->fib6_flags & RTF_GATEWAY)
  1895. seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
  1896. else
  1897. seq_puts(seq, "00000000000000000000000000000000");
  1898. dev = rt->fib6_nh.nh_dev;
  1899. seq_printf(seq, " %08x %08x %08x %08x %8s\n",
  1900. rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
  1901. rt->fib6_flags, dev ? dev->name : "");
  1902. iter->w.leaf = NULL;
  1903. return 0;
  1904. }
  1905. static int ipv6_route_yield(struct fib6_walker *w)
  1906. {
  1907. struct ipv6_route_iter *iter = w->args;
  1908. if (!iter->skip)
  1909. return 1;
  1910. do {
  1911. iter->w.leaf = rcu_dereference_protected(
  1912. iter->w.leaf->fib6_next,
  1913. lockdep_is_held(&iter->tbl->tb6_lock));
  1914. iter->skip--;
  1915. if (!iter->skip && iter->w.leaf)
  1916. return 1;
  1917. } while (iter->w.leaf);
  1918. return 0;
  1919. }
  1920. static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
  1921. struct net *net)
  1922. {
  1923. memset(&iter->w, 0, sizeof(iter->w));
  1924. iter->w.func = ipv6_route_yield;
  1925. iter->w.root = &iter->tbl->tb6_root;
  1926. iter->w.state = FWS_INIT;
  1927. iter->w.node = iter->w.root;
  1928. iter->w.args = iter;
  1929. iter->sernum = iter->w.root->fn_sernum;
  1930. INIT_LIST_HEAD(&iter->w.lh);
  1931. fib6_walker_link(net, &iter->w);
  1932. }
  1933. static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
  1934. struct net *net)
  1935. {
  1936. unsigned int h;
  1937. struct hlist_node *node;
  1938. if (tbl) {
  1939. h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
  1940. node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
  1941. } else {
  1942. h = 0;
  1943. node = NULL;
  1944. }
  1945. while (!node && h < FIB6_TABLE_HASHSZ) {
  1946. node = rcu_dereference_bh(
  1947. hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
  1948. }
  1949. return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
  1950. }
  1951. static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
  1952. {
  1953. if (iter->sernum != iter->w.root->fn_sernum) {
  1954. iter->sernum = iter->w.root->fn_sernum;
  1955. iter->w.state = FWS_INIT;
  1956. iter->w.node = iter->w.root;
  1957. WARN_ON(iter->w.skip);
  1958. iter->w.skip = iter->w.count;
  1959. }
  1960. }
  1961. static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1962. {
  1963. int r;
  1964. struct fib6_info *n;
  1965. struct net *net = seq_file_net(seq);
  1966. struct ipv6_route_iter *iter = seq->private;
  1967. if (!v)
  1968. goto iter_table;
  1969. n = rcu_dereference_bh(((struct fib6_info *)v)->fib6_next);
  1970. if (n) {
  1971. ++*pos;
  1972. return n;
  1973. }
  1974. iter_table:
  1975. ipv6_route_check_sernum(iter);
  1976. spin_lock_bh(&iter->tbl->tb6_lock);
  1977. r = fib6_walk_continue(&iter->w);
  1978. spin_unlock_bh(&iter->tbl->tb6_lock);
  1979. if (r > 0) {
  1980. if (v)
  1981. ++*pos;
  1982. return iter->w.leaf;
  1983. } else if (r < 0) {
  1984. fib6_walker_unlink(net, &iter->w);
  1985. return NULL;
  1986. }
  1987. fib6_walker_unlink(net, &iter->w);
  1988. iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
  1989. if (!iter->tbl)
  1990. return NULL;
  1991. ipv6_route_seq_setup_walk(iter, net);
  1992. goto iter_table;
  1993. }
  1994. static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
  1995. __acquires(RCU_BH)
  1996. {
  1997. struct net *net = seq_file_net(seq);
  1998. struct ipv6_route_iter *iter = seq->private;
  1999. rcu_read_lock_bh();
  2000. iter->tbl = ipv6_route_seq_next_table(NULL, net);
  2001. iter->skip = *pos;
  2002. if (iter->tbl) {
  2003. ipv6_route_seq_setup_walk(iter, net);
  2004. return ipv6_route_seq_next(seq, NULL, pos);
  2005. } else {
  2006. return NULL;
  2007. }
  2008. }
  2009. static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
  2010. {
  2011. struct fib6_walker *w = &iter->w;
  2012. return w->node && !(w->state == FWS_U && w->node == w->root);
  2013. }
  2014. static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
  2015. __releases(RCU_BH)
  2016. {
  2017. struct net *net = seq_file_net(seq);
  2018. struct ipv6_route_iter *iter = seq->private;
  2019. if (ipv6_route_iter_active(iter))
  2020. fib6_walker_unlink(net, &iter->w);
  2021. rcu_read_unlock_bh();
  2022. }
  2023. const struct seq_operations ipv6_route_seq_ops = {
  2024. .start = ipv6_route_seq_start,
  2025. .next = ipv6_route_seq_next,
  2026. .stop = ipv6_route_seq_stop,
  2027. .show = ipv6_route_seq_show
  2028. };
  2029. #endif /* CONFIG_PROC_FS */