ip6_fib.c 55 KB

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