ip6_fib.c 54 KB

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