ip6_fib.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343
  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(unsigned long arg);
  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. {
  296. struct fib6_entry_notifier_info info = {
  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. }
  539. return fn;
  540. }
  541. /*
  542. * We have more bits to go
  543. */
  544. /* Try to walk down on tree. */
  545. dir = addr_bit_set(addr, fn->fn_bit);
  546. pn = fn;
  547. fn = dir ?
  548. rcu_dereference_protected(fn->right,
  549. lockdep_is_held(&table->tb6_lock)) :
  550. rcu_dereference_protected(fn->left,
  551. lockdep_is_held(&table->tb6_lock));
  552. } while (fn);
  553. if (!allow_create) {
  554. /* We should not create new node because
  555. * NLM_F_REPLACE was specified without NLM_F_CREATE
  556. * I assume it is safe to require NLM_F_CREATE when
  557. * REPLACE flag is used! Later we may want to remove the
  558. * check for replace_required, because according
  559. * to netlink specification, NLM_F_CREATE
  560. * MUST be specified if new route is created.
  561. * That would keep IPv6 consistent with IPv4
  562. */
  563. if (replace_required) {
  564. NL_SET_ERR_MSG(extack,
  565. "Can not replace route - no match found");
  566. pr_warn("Can't replace route, no match found\n");
  567. return ERR_PTR(-ENOENT);
  568. }
  569. pr_warn("NLM_F_CREATE should be set when creating new route\n");
  570. }
  571. /*
  572. * We walked to the bottom of tree.
  573. * Create new leaf node without children.
  574. */
  575. ln = node_alloc(net);
  576. if (!ln)
  577. return ERR_PTR(-ENOMEM);
  578. ln->fn_bit = plen;
  579. RCU_INIT_POINTER(ln->parent, pn);
  580. if (dir)
  581. rcu_assign_pointer(pn->right, ln);
  582. else
  583. rcu_assign_pointer(pn->left, ln);
  584. return ln;
  585. insert_above:
  586. /*
  587. * split since we don't have a common prefix anymore or
  588. * we have a less significant route.
  589. * we've to insert an intermediate node on the list
  590. * this new node will point to the one we need to create
  591. * and the current
  592. */
  593. pn = rcu_dereference_protected(fn->parent,
  594. lockdep_is_held(&table->tb6_lock));
  595. /* find 1st bit in difference between the 2 addrs.
  596. See comment in __ipv6_addr_diff: bit may be an invalid value,
  597. but if it is >= plen, the value is ignored in any case.
  598. */
  599. bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
  600. /*
  601. * (intermediate)[in]
  602. * / \
  603. * (new leaf node)[ln] (old node)[fn]
  604. */
  605. if (plen > bit) {
  606. in = node_alloc(net);
  607. ln = node_alloc(net);
  608. if (!in || !ln) {
  609. if (in)
  610. node_free_immediate(net, in);
  611. if (ln)
  612. node_free_immediate(net, ln);
  613. return ERR_PTR(-ENOMEM);
  614. }
  615. /*
  616. * new intermediate node.
  617. * RTN_RTINFO will
  618. * be off since that an address that chooses one of
  619. * the branches would not match less specific routes
  620. * in the other branch
  621. */
  622. in->fn_bit = bit;
  623. RCU_INIT_POINTER(in->parent, pn);
  624. in->leaf = fn->leaf;
  625. atomic_inc(&rcu_dereference_protected(in->leaf,
  626. lockdep_is_held(&table->tb6_lock))->rt6i_ref);
  627. /* update parent pointer */
  628. if (dir)
  629. rcu_assign_pointer(pn->right, in);
  630. else
  631. rcu_assign_pointer(pn->left, in);
  632. ln->fn_bit = plen;
  633. RCU_INIT_POINTER(ln->parent, in);
  634. rcu_assign_pointer(fn->parent, in);
  635. if (addr_bit_set(addr, bit)) {
  636. rcu_assign_pointer(in->right, ln);
  637. rcu_assign_pointer(in->left, fn);
  638. } else {
  639. rcu_assign_pointer(in->left, ln);
  640. rcu_assign_pointer(in->right, fn);
  641. }
  642. } else { /* plen <= bit */
  643. /*
  644. * (new leaf node)[ln]
  645. * / \
  646. * (old node)[fn] NULL
  647. */
  648. ln = node_alloc(net);
  649. if (!ln)
  650. return ERR_PTR(-ENOMEM);
  651. ln->fn_bit = plen;
  652. RCU_INIT_POINTER(ln->parent, pn);
  653. if (addr_bit_set(&key->addr, plen))
  654. RCU_INIT_POINTER(ln->right, fn);
  655. else
  656. RCU_INIT_POINTER(ln->left, fn);
  657. rcu_assign_pointer(fn->parent, ln);
  658. if (dir)
  659. rcu_assign_pointer(pn->right, ln);
  660. else
  661. rcu_assign_pointer(pn->left, ln);
  662. }
  663. return ln;
  664. }
  665. static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
  666. {
  667. return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
  668. RTF_GATEWAY;
  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. {
  726. struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
  727. lockdep_is_held(&rt->rt6i_table->tb6_lock));
  728. struct rt6_info *iter = NULL;
  729. struct rt6_info __rcu **ins;
  730. struct rt6_info __rcu **fallback_ins = NULL;
  731. int replace = (info->nlh &&
  732. (info->nlh->nlmsg_flags & NLM_F_REPLACE));
  733. int add = (!info->nlh ||
  734. (info->nlh->nlmsg_flags & NLM_F_CREATE));
  735. int found = 0;
  736. bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
  737. u16 nlflags = NLM_F_EXCL;
  738. int err;
  739. if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
  740. nlflags |= NLM_F_APPEND;
  741. ins = &fn->leaf;
  742. for (iter = leaf; iter;
  743. iter = rcu_dereference_protected(iter->dst.rt6_next,
  744. lockdep_is_held(&rt->rt6i_table->tb6_lock))) {
  745. /*
  746. * Search for duplicates
  747. */
  748. if (iter->rt6i_metric == rt->rt6i_metric) {
  749. /*
  750. * Same priority level
  751. */
  752. if (info->nlh &&
  753. (info->nlh->nlmsg_flags & NLM_F_EXCL))
  754. return -EEXIST;
  755. nlflags &= ~NLM_F_EXCL;
  756. if (replace) {
  757. if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
  758. found++;
  759. break;
  760. }
  761. if (rt_can_ecmp)
  762. fallback_ins = fallback_ins ?: ins;
  763. goto next_iter;
  764. }
  765. if (rt6_duplicate_nexthop(iter, rt)) {
  766. if (rt->rt6i_nsiblings)
  767. rt->rt6i_nsiblings = 0;
  768. if (!(iter->rt6i_flags & RTF_EXPIRES))
  769. return -EEXIST;
  770. if (!(rt->rt6i_flags & RTF_EXPIRES))
  771. rt6_clean_expires(iter);
  772. else
  773. rt6_set_expires(iter, rt->dst.expires);
  774. iter->rt6i_pmtu = rt->rt6i_pmtu;
  775. return -EEXIST;
  776. }
  777. /* If we have the same destination and the same metric,
  778. * but not the same gateway, then the route we try to
  779. * add is sibling to this route, increment our counter
  780. * of siblings, and later we will add our route to the
  781. * list.
  782. * Only static routes (which don't have flag
  783. * RTF_EXPIRES) are used for ECMPv6.
  784. *
  785. * To avoid long list, we only had siblings if the
  786. * route have a gateway.
  787. */
  788. if (rt_can_ecmp &&
  789. rt6_qualify_for_ecmp(iter))
  790. rt->rt6i_nsiblings++;
  791. }
  792. if (iter->rt6i_metric > rt->rt6i_metric)
  793. break;
  794. next_iter:
  795. ins = &iter->dst.rt6_next;
  796. }
  797. if (fallback_ins && !found) {
  798. /* No ECMP-able route found, replace first non-ECMP one */
  799. ins = fallback_ins;
  800. iter = rcu_dereference_protected(*ins,
  801. lockdep_is_held(&rt->rt6i_table->tb6_lock));
  802. found++;
  803. }
  804. /* Reset round-robin state, if necessary */
  805. if (ins == &fn->leaf)
  806. fn->rr_ptr = NULL;
  807. /* Link this route to others same route. */
  808. if (rt->rt6i_nsiblings) {
  809. unsigned int rt6i_nsiblings;
  810. struct rt6_info *sibling, *temp_sibling;
  811. /* Find the first route that have the same metric */
  812. sibling = leaf;
  813. while (sibling) {
  814. if (sibling->rt6i_metric == rt->rt6i_metric &&
  815. rt6_qualify_for_ecmp(sibling)) {
  816. list_add_tail(&rt->rt6i_siblings,
  817. &sibling->rt6i_siblings);
  818. break;
  819. }
  820. sibling = rcu_dereference_protected(sibling->dst.rt6_next,
  821. lockdep_is_held(&rt->rt6i_table->tb6_lock));
  822. }
  823. /* For each sibling in the list, increment the counter of
  824. * siblings. BUG() if counters does not match, list of siblings
  825. * is broken!
  826. */
  827. rt6i_nsiblings = 0;
  828. list_for_each_entry_safe(sibling, temp_sibling,
  829. &rt->rt6i_siblings, rt6i_siblings) {
  830. sibling->rt6i_nsiblings++;
  831. BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
  832. rt6i_nsiblings++;
  833. }
  834. BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
  835. }
  836. /*
  837. * insert node
  838. */
  839. if (!replace) {
  840. if (!add)
  841. pr_warn("NLM_F_CREATE should be set when creating new route\n");
  842. add:
  843. nlflags |= NLM_F_CREATE;
  844. err = fib6_commit_metrics(&rt->dst, mxc);
  845. if (err)
  846. return err;
  847. rcu_assign_pointer(rt->dst.rt6_next, iter);
  848. atomic_inc(&rt->rt6i_ref);
  849. rcu_assign_pointer(rt->rt6i_node, fn);
  850. rcu_assign_pointer(*ins, rt);
  851. call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
  852. rt);
  853. if (!info->skip_notify)
  854. inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
  855. info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
  856. if (!(fn->fn_flags & RTN_RTINFO)) {
  857. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  858. fn->fn_flags |= RTN_RTINFO;
  859. }
  860. } else {
  861. int nsiblings;
  862. if (!found) {
  863. if (add)
  864. goto add;
  865. pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
  866. return -ENOENT;
  867. }
  868. err = fib6_commit_metrics(&rt->dst, mxc);
  869. if (err)
  870. return err;
  871. atomic_inc(&rt->rt6i_ref);
  872. rcu_assign_pointer(rt->rt6i_node, fn);
  873. rt->dst.rt6_next = iter->dst.rt6_next;
  874. rcu_assign_pointer(*ins, rt);
  875. call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
  876. rt);
  877. if (!info->skip_notify)
  878. inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
  879. if (!(fn->fn_flags & RTN_RTINFO)) {
  880. info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
  881. fn->fn_flags |= RTN_RTINFO;
  882. }
  883. nsiblings = iter->rt6i_nsiblings;
  884. iter->rt6i_node = NULL;
  885. fib6_purge_rt(iter, fn, info->nl_net);
  886. if (rcu_access_pointer(fn->rr_ptr) == iter)
  887. fn->rr_ptr = NULL;
  888. rt6_release(iter);
  889. if (nsiblings) {
  890. /* Replacing an ECMP route, remove all siblings */
  891. ins = &rt->dst.rt6_next;
  892. iter = rcu_dereference_protected(*ins,
  893. lockdep_is_held(&rt->rt6i_table->tb6_lock));
  894. while (iter) {
  895. if (iter->rt6i_metric > rt->rt6i_metric)
  896. break;
  897. if (rt6_qualify_for_ecmp(iter)) {
  898. *ins = iter->dst.rt6_next;
  899. iter->rt6i_node = NULL;
  900. fib6_purge_rt(iter, fn, info->nl_net);
  901. if (rcu_access_pointer(fn->rr_ptr) == iter)
  902. fn->rr_ptr = NULL;
  903. rt6_release(iter);
  904. nsiblings--;
  905. info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
  906. } else {
  907. ins = &iter->dst.rt6_next;
  908. }
  909. iter = rcu_dereference_protected(*ins,
  910. lockdep_is_held(&rt->rt6i_table->tb6_lock));
  911. }
  912. WARN_ON(nsiblings != 0);
  913. }
  914. }
  915. return 0;
  916. }
  917. static void fib6_start_gc(struct net *net, struct rt6_info *rt)
  918. {
  919. if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
  920. (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
  921. mod_timer(&net->ipv6.ip6_fib_timer,
  922. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  923. }
  924. void fib6_force_start_gc(struct net *net)
  925. {
  926. if (!timer_pending(&net->ipv6.ip6_fib_timer))
  927. mod_timer(&net->ipv6.ip6_fib_timer,
  928. jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
  929. }
  930. static void fib6_update_sernum_upto_root(struct rt6_info *rt,
  931. int sernum)
  932. {
  933. struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
  934. lockdep_is_held(&rt->rt6i_table->tb6_lock));
  935. /* paired with smp_rmb() in rt6_get_cookie_safe() */
  936. smp_wmb();
  937. while (fn) {
  938. fn->fn_sernum = sernum;
  939. fn = rcu_dereference_protected(fn->parent,
  940. lockdep_is_held(&rt->rt6i_table->tb6_lock));
  941. }
  942. }
  943. /*
  944. * Add routing information to the routing tree.
  945. * <destination addr>/<source addr>
  946. * with source addr info in sub-trees
  947. * Need to own table->tb6_lock
  948. */
  949. int fib6_add(struct fib6_node *root, struct rt6_info *rt,
  950. struct nl_info *info, struct mx6_config *mxc,
  951. struct netlink_ext_ack *extack)
  952. {
  953. struct fib6_table *table = rt->rt6i_table;
  954. struct fib6_node *fn, *pn = NULL;
  955. int err = -ENOMEM;
  956. int allow_create = 1;
  957. int replace_required = 0;
  958. int sernum = fib6_new_sernum(info->nl_net);
  959. if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
  960. return -EINVAL;
  961. if (WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE))
  962. return -EINVAL;
  963. if (info->nlh) {
  964. if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
  965. allow_create = 0;
  966. if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
  967. replace_required = 1;
  968. }
  969. if (!allow_create && !replace_required)
  970. pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
  971. fn = fib6_add_1(info->nl_net, table, root,
  972. &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
  973. offsetof(struct rt6_info, rt6i_dst), allow_create,
  974. replace_required, extack);
  975. if (IS_ERR(fn)) {
  976. err = PTR_ERR(fn);
  977. fn = NULL;
  978. goto out;
  979. }
  980. pn = fn;
  981. #ifdef CONFIG_IPV6_SUBTREES
  982. if (rt->rt6i_src.plen) {
  983. struct fib6_node *sn;
  984. if (!rcu_access_pointer(fn->subtree)) {
  985. struct fib6_node *sfn;
  986. /*
  987. * Create subtree.
  988. *
  989. * fn[main tree]
  990. * |
  991. * sfn[subtree root]
  992. * \
  993. * sn[new leaf node]
  994. */
  995. /* Create subtree root node */
  996. sfn = node_alloc(info->nl_net);
  997. if (!sfn)
  998. goto failure;
  999. atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
  1000. rcu_assign_pointer(sfn->leaf,
  1001. info->nl_net->ipv6.ip6_null_entry);
  1002. sfn->fn_flags = RTN_ROOT;
  1003. /* Now add the first leaf node to new subtree */
  1004. sn = fib6_add_1(info->nl_net, table, sfn,
  1005. &rt->rt6i_src.addr, rt->rt6i_src.plen,
  1006. offsetof(struct rt6_info, rt6i_src),
  1007. allow_create, replace_required, extack);
  1008. if (IS_ERR(sn)) {
  1009. /* If it is failed, discard just allocated
  1010. root, and then (in failure) stale node
  1011. in main tree.
  1012. */
  1013. node_free_immediate(info->nl_net, sfn);
  1014. err = PTR_ERR(sn);
  1015. goto failure;
  1016. }
  1017. /* Now link new subtree to main tree */
  1018. rcu_assign_pointer(sfn->parent, fn);
  1019. rcu_assign_pointer(fn->subtree, sfn);
  1020. } else {
  1021. sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
  1022. &rt->rt6i_src.addr, rt->rt6i_src.plen,
  1023. offsetof(struct rt6_info, rt6i_src),
  1024. allow_create, replace_required, extack);
  1025. if (IS_ERR(sn)) {
  1026. err = PTR_ERR(sn);
  1027. goto failure;
  1028. }
  1029. }
  1030. if (!rcu_access_pointer(fn->leaf)) {
  1031. atomic_inc(&rt->rt6i_ref);
  1032. rcu_assign_pointer(fn->leaf, rt);
  1033. }
  1034. fn = sn;
  1035. }
  1036. #endif
  1037. err = fib6_add_rt2node(fn, rt, info, mxc);
  1038. if (!err) {
  1039. fib6_update_sernum_upto_root(rt, sernum);
  1040. fib6_start_gc(info->nl_net, rt);
  1041. }
  1042. out:
  1043. if (err) {
  1044. #ifdef CONFIG_IPV6_SUBTREES
  1045. /*
  1046. * If fib6_add_1 has cleared the old leaf pointer in the
  1047. * super-tree leaf node we have to find a new one for it.
  1048. */
  1049. struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
  1050. lockdep_is_held(&table->tb6_lock));
  1051. if (pn != fn && pn_leaf == rt) {
  1052. pn_leaf = NULL;
  1053. RCU_INIT_POINTER(pn->leaf, NULL);
  1054. atomic_dec(&rt->rt6i_ref);
  1055. }
  1056. if (pn != fn && !pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
  1057. pn_leaf = fib6_find_prefix(info->nl_net, table, pn);
  1058. #if RT6_DEBUG >= 2
  1059. if (!pn_leaf) {
  1060. WARN_ON(!pn_leaf);
  1061. pn_leaf = info->nl_net->ipv6.ip6_null_entry;
  1062. }
  1063. #endif
  1064. atomic_inc(&pn_leaf->rt6i_ref);
  1065. rcu_assign_pointer(pn->leaf, pn_leaf);
  1066. }
  1067. #endif
  1068. goto failure;
  1069. }
  1070. return err;
  1071. failure:
  1072. /* fn->leaf could be NULL if fn is an intermediate node and we
  1073. * failed to add the new route to it in both subtree creation
  1074. * failure and fib6_add_rt2node() failure case.
  1075. * In both cases, fib6_repair_tree() should be called to fix
  1076. * fn->leaf.
  1077. */
  1078. if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
  1079. fib6_repair_tree(info->nl_net, table, fn);
  1080. /* Always release dst as dst->__refcnt is guaranteed
  1081. * to be taken before entering this function
  1082. */
  1083. dst_release_immediate(&rt->dst);
  1084. return err;
  1085. }
  1086. /*
  1087. * Routing tree lookup
  1088. *
  1089. */
  1090. struct lookup_args {
  1091. int offset; /* key offset on rt6_info */
  1092. const struct in6_addr *addr; /* search key */
  1093. };
  1094. static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
  1095. struct lookup_args *args)
  1096. {
  1097. struct fib6_node *fn;
  1098. __be32 dir;
  1099. if (unlikely(args->offset == 0))
  1100. return NULL;
  1101. /*
  1102. * Descend on a tree
  1103. */
  1104. fn = root;
  1105. for (;;) {
  1106. struct fib6_node *next;
  1107. dir = addr_bit_set(args->addr, fn->fn_bit);
  1108. next = dir ? rcu_dereference(fn->right) :
  1109. rcu_dereference(fn->left);
  1110. if (next) {
  1111. fn = next;
  1112. continue;
  1113. }
  1114. break;
  1115. }
  1116. while (fn) {
  1117. struct fib6_node *subtree = FIB6_SUBTREE(fn);
  1118. if (subtree || fn->fn_flags & RTN_RTINFO) {
  1119. struct rt6_info *leaf = rcu_dereference(fn->leaf);
  1120. struct rt6key *key;
  1121. if (!leaf)
  1122. goto backtrack;
  1123. key = (struct rt6key *) ((u8 *)leaf + args->offset);
  1124. if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
  1125. #ifdef CONFIG_IPV6_SUBTREES
  1126. if (subtree) {
  1127. struct fib6_node *sfn;
  1128. sfn = fib6_lookup_1(subtree, args + 1);
  1129. if (!sfn)
  1130. goto backtrack;
  1131. fn = sfn;
  1132. }
  1133. #endif
  1134. if (fn->fn_flags & RTN_RTINFO)
  1135. return fn;
  1136. }
  1137. }
  1138. backtrack:
  1139. if (fn->fn_flags & RTN_ROOT)
  1140. break;
  1141. fn = rcu_dereference(fn->parent);
  1142. }
  1143. return NULL;
  1144. }
  1145. /* called with rcu_read_lock() held
  1146. */
  1147. struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
  1148. const struct in6_addr *saddr)
  1149. {
  1150. struct fib6_node *fn;
  1151. struct lookup_args args[] = {
  1152. {
  1153. .offset = offsetof(struct rt6_info, rt6i_dst),
  1154. .addr = daddr,
  1155. },
  1156. #ifdef CONFIG_IPV6_SUBTREES
  1157. {
  1158. .offset = offsetof(struct rt6_info, rt6i_src),
  1159. .addr = saddr,
  1160. },
  1161. #endif
  1162. {
  1163. .offset = 0, /* sentinel */
  1164. }
  1165. };
  1166. fn = fib6_lookup_1(root, daddr ? args : args + 1);
  1167. if (!fn || fn->fn_flags & RTN_TL_ROOT)
  1168. fn = root;
  1169. return fn;
  1170. }
  1171. /*
  1172. * Get node with specified destination prefix (and source prefix,
  1173. * if subtrees are used)
  1174. * exact_match == true means we try to find fn with exact match of
  1175. * the passed in prefix addr
  1176. * exact_match == false means we try to find fn with longest prefix
  1177. * match of the passed in prefix addr. This is useful for finding fn
  1178. * for cached route as it will be stored in the exception table under
  1179. * the node with longest prefix length.
  1180. */
  1181. static struct fib6_node *fib6_locate_1(struct fib6_node *root,
  1182. const struct in6_addr *addr,
  1183. int plen, int offset,
  1184. bool exact_match)
  1185. {
  1186. struct fib6_node *fn, *prev = NULL;
  1187. for (fn = root; fn ; ) {
  1188. struct rt6_info *leaf = rcu_dereference(fn->leaf);
  1189. struct rt6key *key;
  1190. /* This node is being deleted */
  1191. if (!leaf) {
  1192. if (plen <= fn->fn_bit)
  1193. goto out;
  1194. else
  1195. goto next;
  1196. }
  1197. key = (struct rt6key *)((u8 *)leaf + offset);
  1198. /*
  1199. * Prefix match
  1200. */
  1201. if (plen < fn->fn_bit ||
  1202. !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
  1203. goto out;
  1204. if (plen == fn->fn_bit)
  1205. return fn;
  1206. prev = fn;
  1207. next:
  1208. /*
  1209. * We have more bits to go
  1210. */
  1211. if (addr_bit_set(addr, fn->fn_bit))
  1212. fn = rcu_dereference(fn->right);
  1213. else
  1214. fn = rcu_dereference(fn->left);
  1215. }
  1216. out:
  1217. if (exact_match)
  1218. return NULL;
  1219. else
  1220. return prev;
  1221. }
  1222. struct fib6_node *fib6_locate(struct fib6_node *root,
  1223. const struct in6_addr *daddr, int dst_len,
  1224. const struct in6_addr *saddr, int src_len,
  1225. bool exact_match)
  1226. {
  1227. struct fib6_node *fn;
  1228. fn = fib6_locate_1(root, daddr, dst_len,
  1229. offsetof(struct rt6_info, rt6i_dst),
  1230. exact_match);
  1231. #ifdef CONFIG_IPV6_SUBTREES
  1232. if (src_len) {
  1233. struct fib6_node *subtree = FIB6_SUBTREE(fn);
  1234. WARN_ON(saddr == NULL);
  1235. if (fn && subtree)
  1236. fn = fib6_locate_1(subtree, saddr, src_len,
  1237. offsetof(struct rt6_info, rt6i_src),
  1238. exact_match);
  1239. }
  1240. #endif
  1241. if (fn && fn->fn_flags & RTN_RTINFO)
  1242. return fn;
  1243. return NULL;
  1244. }
  1245. /*
  1246. * Deletion
  1247. *
  1248. */
  1249. static struct rt6_info *fib6_find_prefix(struct net *net,
  1250. struct fib6_table *table,
  1251. struct fib6_node *fn)
  1252. {
  1253. struct fib6_node *child_left, *child_right;
  1254. if (fn->fn_flags & RTN_ROOT)
  1255. return net->ipv6.ip6_null_entry;
  1256. while (fn) {
  1257. child_left = rcu_dereference_protected(fn->left,
  1258. lockdep_is_held(&table->tb6_lock));
  1259. child_right = rcu_dereference_protected(fn->right,
  1260. lockdep_is_held(&table->tb6_lock));
  1261. if (child_left)
  1262. return rcu_dereference_protected(child_left->leaf,
  1263. lockdep_is_held(&table->tb6_lock));
  1264. if (child_right)
  1265. return rcu_dereference_protected(child_right->leaf,
  1266. lockdep_is_held(&table->tb6_lock));
  1267. fn = FIB6_SUBTREE(fn);
  1268. }
  1269. return NULL;
  1270. }
  1271. /*
  1272. * Called to trim the tree of intermediate nodes when possible. "fn"
  1273. * is the node we want to try and remove.
  1274. * Need to own table->tb6_lock
  1275. */
  1276. static struct fib6_node *fib6_repair_tree(struct net *net,
  1277. struct fib6_table *table,
  1278. struct fib6_node *fn)
  1279. {
  1280. int children;
  1281. int nstate;
  1282. struct fib6_node *child;
  1283. struct fib6_walker *w;
  1284. int iter = 0;
  1285. for (;;) {
  1286. struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
  1287. lockdep_is_held(&table->tb6_lock));
  1288. struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
  1289. lockdep_is_held(&table->tb6_lock));
  1290. struct fib6_node *pn = rcu_dereference_protected(fn->parent,
  1291. lockdep_is_held(&table->tb6_lock));
  1292. struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
  1293. lockdep_is_held(&table->tb6_lock));
  1294. struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
  1295. lockdep_is_held(&table->tb6_lock));
  1296. struct rt6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
  1297. lockdep_is_held(&table->tb6_lock));
  1298. struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
  1299. lockdep_is_held(&table->tb6_lock));
  1300. struct rt6_info *new_fn_leaf;
  1301. RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
  1302. iter++;
  1303. WARN_ON(fn->fn_flags & RTN_RTINFO);
  1304. WARN_ON(fn->fn_flags & RTN_TL_ROOT);
  1305. WARN_ON(fn_leaf);
  1306. children = 0;
  1307. child = NULL;
  1308. if (fn_r)
  1309. child = fn_r, children |= 1;
  1310. if (fn_l)
  1311. child = fn_l, children |= 2;
  1312. if (children == 3 || FIB6_SUBTREE(fn)
  1313. #ifdef CONFIG_IPV6_SUBTREES
  1314. /* Subtree root (i.e. fn) may have one child */
  1315. || (children && fn->fn_flags & RTN_ROOT)
  1316. #endif
  1317. ) {
  1318. new_fn_leaf = fib6_find_prefix(net, table, fn);
  1319. #if RT6_DEBUG >= 2
  1320. if (!new_fn_leaf) {
  1321. WARN_ON(!new_fn_leaf);
  1322. new_fn_leaf = net->ipv6.ip6_null_entry;
  1323. }
  1324. #endif
  1325. atomic_inc(&new_fn_leaf->rt6i_ref);
  1326. rcu_assign_pointer(fn->leaf, new_fn_leaf);
  1327. return pn;
  1328. }
  1329. #ifdef CONFIG_IPV6_SUBTREES
  1330. if (FIB6_SUBTREE(pn) == fn) {
  1331. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  1332. RCU_INIT_POINTER(pn->subtree, NULL);
  1333. nstate = FWS_L;
  1334. } else {
  1335. WARN_ON(fn->fn_flags & RTN_ROOT);
  1336. #endif
  1337. if (pn_r == fn)
  1338. rcu_assign_pointer(pn->right, child);
  1339. else if (pn_l == fn)
  1340. rcu_assign_pointer(pn->left, child);
  1341. #if RT6_DEBUG >= 2
  1342. else
  1343. WARN_ON(1);
  1344. #endif
  1345. if (child)
  1346. rcu_assign_pointer(child->parent, pn);
  1347. nstate = FWS_R;
  1348. #ifdef CONFIG_IPV6_SUBTREES
  1349. }
  1350. #endif
  1351. read_lock(&net->ipv6.fib6_walker_lock);
  1352. FOR_WALKERS(net, w) {
  1353. if (!child) {
  1354. if (w->node == fn) {
  1355. RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
  1356. w->node = pn;
  1357. w->state = nstate;
  1358. }
  1359. } else {
  1360. if (w->node == fn) {
  1361. w->node = child;
  1362. if (children&2) {
  1363. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  1364. w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
  1365. } else {
  1366. RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
  1367. w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
  1368. }
  1369. }
  1370. }
  1371. }
  1372. read_unlock(&net->ipv6.fib6_walker_lock);
  1373. node_free(net, fn);
  1374. if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
  1375. return pn;
  1376. RCU_INIT_POINTER(pn->leaf, NULL);
  1377. rt6_release(pn_leaf);
  1378. fn = pn;
  1379. }
  1380. }
  1381. static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
  1382. struct rt6_info __rcu **rtp, struct nl_info *info)
  1383. {
  1384. struct fib6_walker *w;
  1385. struct rt6_info *rt = rcu_dereference_protected(*rtp,
  1386. lockdep_is_held(&table->tb6_lock));
  1387. struct net *net = info->nl_net;
  1388. RT6_TRACE("fib6_del_route\n");
  1389. WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE);
  1390. /* Unlink it */
  1391. *rtp = rt->dst.rt6_next;
  1392. rt->rt6i_node = NULL;
  1393. net->ipv6.rt6_stats->fib_rt_entries--;
  1394. net->ipv6.rt6_stats->fib_discarded_routes++;
  1395. /* Flush all cached dst in exception table */
  1396. rt6_flush_exceptions(rt);
  1397. /* Reset round-robin state, if necessary */
  1398. if (rcu_access_pointer(fn->rr_ptr) == rt)
  1399. fn->rr_ptr = NULL;
  1400. /* Remove this entry from other siblings */
  1401. if (rt->rt6i_nsiblings) {
  1402. struct rt6_info *sibling, *next_sibling;
  1403. list_for_each_entry_safe(sibling, next_sibling,
  1404. &rt->rt6i_siblings, rt6i_siblings)
  1405. sibling->rt6i_nsiblings--;
  1406. rt->rt6i_nsiblings = 0;
  1407. list_del_init(&rt->rt6i_siblings);
  1408. }
  1409. /* Adjust walkers */
  1410. read_lock(&net->ipv6.fib6_walker_lock);
  1411. FOR_WALKERS(net, w) {
  1412. if (w->state == FWS_C && w->leaf == rt) {
  1413. RT6_TRACE("walker %p adjusted by delroute\n", w);
  1414. w->leaf = rcu_dereference_protected(rt->dst.rt6_next,
  1415. lockdep_is_held(&table->tb6_lock));
  1416. if (!w->leaf)
  1417. w->state = FWS_U;
  1418. }
  1419. }
  1420. read_unlock(&net->ipv6.fib6_walker_lock);
  1421. /* If it was last route, expunge its radix tree node */
  1422. if (!rcu_access_pointer(fn->leaf)) {
  1423. fn->fn_flags &= ~RTN_RTINFO;
  1424. net->ipv6.rt6_stats->fib_route_nodes--;
  1425. fn = fib6_repair_tree(net, table, fn);
  1426. }
  1427. fib6_purge_rt(rt, fn, net);
  1428. call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt);
  1429. if (!info->skip_notify)
  1430. inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
  1431. rt6_release(rt);
  1432. }
  1433. /* Need to own table->tb6_lock */
  1434. int fib6_del(struct rt6_info *rt, struct nl_info *info)
  1435. {
  1436. struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
  1437. lockdep_is_held(&rt->rt6i_table->tb6_lock));
  1438. struct fib6_table *table = rt->rt6i_table;
  1439. struct net *net = info->nl_net;
  1440. struct rt6_info __rcu **rtp;
  1441. struct rt6_info __rcu **rtp_next;
  1442. #if RT6_DEBUG >= 2
  1443. if (rt->dst.obsolete > 0) {
  1444. WARN_ON(fn);
  1445. return -ENOENT;
  1446. }
  1447. #endif
  1448. if (!fn || rt == net->ipv6.ip6_null_entry)
  1449. return -ENOENT;
  1450. WARN_ON(!(fn->fn_flags & RTN_RTINFO));
  1451. /* remove cached dst from exception table */
  1452. if (rt->rt6i_flags & RTF_CACHE)
  1453. return rt6_remove_exception_rt(rt);
  1454. /*
  1455. * Walk the leaf entries looking for ourself
  1456. */
  1457. for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
  1458. struct rt6_info *cur = rcu_dereference_protected(*rtp,
  1459. lockdep_is_held(&table->tb6_lock));
  1460. if (rt == cur) {
  1461. fib6_del_route(table, fn, rtp, info);
  1462. return 0;
  1463. }
  1464. rtp_next = &cur->dst.rt6_next;
  1465. }
  1466. return -ENOENT;
  1467. }
  1468. /*
  1469. * Tree traversal function.
  1470. *
  1471. * Certainly, it is not interrupt safe.
  1472. * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
  1473. * It means, that we can modify tree during walking
  1474. * and use this function for garbage collection, clone pruning,
  1475. * cleaning tree when a device goes down etc. etc.
  1476. *
  1477. * It guarantees that every node will be traversed,
  1478. * and that it will be traversed only once.
  1479. *
  1480. * Callback function w->func may return:
  1481. * 0 -> continue walking.
  1482. * positive value -> walking is suspended (used by tree dumps,
  1483. * and probably by gc, if it will be split to several slices)
  1484. * negative value -> terminate walking.
  1485. *
  1486. * The function itself returns:
  1487. * 0 -> walk is complete.
  1488. * >0 -> walk is incomplete (i.e. suspended)
  1489. * <0 -> walk is terminated by an error.
  1490. *
  1491. * This function is called with tb6_lock held.
  1492. */
  1493. static int fib6_walk_continue(struct fib6_walker *w)
  1494. {
  1495. struct fib6_node *fn, *pn, *left, *right;
  1496. /* w->root should always be table->tb6_root */
  1497. WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
  1498. for (;;) {
  1499. fn = w->node;
  1500. if (!fn)
  1501. return 0;
  1502. switch (w->state) {
  1503. #ifdef CONFIG_IPV6_SUBTREES
  1504. case FWS_S:
  1505. if (FIB6_SUBTREE(fn)) {
  1506. w->node = FIB6_SUBTREE(fn);
  1507. continue;
  1508. }
  1509. w->state = FWS_L;
  1510. #endif
  1511. case FWS_L:
  1512. left = rcu_dereference_protected(fn->left, 1);
  1513. if (left) {
  1514. w->node = left;
  1515. w->state = FWS_INIT;
  1516. continue;
  1517. }
  1518. w->state = FWS_R;
  1519. case FWS_R:
  1520. right = rcu_dereference_protected(fn->right, 1);
  1521. if (right) {
  1522. w->node = right;
  1523. w->state = FWS_INIT;
  1524. continue;
  1525. }
  1526. w->state = FWS_C;
  1527. w->leaf = rcu_dereference_protected(fn->leaf, 1);
  1528. case FWS_C:
  1529. if (w->leaf && fn->fn_flags & RTN_RTINFO) {
  1530. int err;
  1531. if (w->skip) {
  1532. w->skip--;
  1533. goto skip;
  1534. }
  1535. err = w->func(w);
  1536. if (err)
  1537. return err;
  1538. w->count++;
  1539. continue;
  1540. }
  1541. skip:
  1542. w->state = FWS_U;
  1543. case FWS_U:
  1544. if (fn == w->root)
  1545. return 0;
  1546. pn = rcu_dereference_protected(fn->parent, 1);
  1547. left = rcu_dereference_protected(pn->left, 1);
  1548. right = rcu_dereference_protected(pn->right, 1);
  1549. w->node = pn;
  1550. #ifdef CONFIG_IPV6_SUBTREES
  1551. if (FIB6_SUBTREE(pn) == fn) {
  1552. WARN_ON(!(fn->fn_flags & RTN_ROOT));
  1553. w->state = FWS_L;
  1554. continue;
  1555. }
  1556. #endif
  1557. if (left == fn) {
  1558. w->state = FWS_R;
  1559. continue;
  1560. }
  1561. if (right == fn) {
  1562. w->state = FWS_C;
  1563. w->leaf = rcu_dereference_protected(w->node->leaf, 1);
  1564. continue;
  1565. }
  1566. #if RT6_DEBUG >= 2
  1567. WARN_ON(1);
  1568. #endif
  1569. }
  1570. }
  1571. }
  1572. static int fib6_walk(struct net *net, struct fib6_walker *w)
  1573. {
  1574. int res;
  1575. w->state = FWS_INIT;
  1576. w->node = w->root;
  1577. fib6_walker_link(net, w);
  1578. res = fib6_walk_continue(w);
  1579. if (res <= 0)
  1580. fib6_walker_unlink(net, w);
  1581. return res;
  1582. }
  1583. static int fib6_clean_node(struct fib6_walker *w)
  1584. {
  1585. int res;
  1586. struct rt6_info *rt;
  1587. struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
  1588. struct nl_info info = {
  1589. .nl_net = c->net,
  1590. };
  1591. if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
  1592. w->node->fn_sernum != c->sernum)
  1593. w->node->fn_sernum = c->sernum;
  1594. if (!c->func) {
  1595. WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
  1596. w->leaf = NULL;
  1597. return 0;
  1598. }
  1599. for_each_fib6_walker_rt(w) {
  1600. res = c->func(rt, c->arg);
  1601. if (res < 0) {
  1602. w->leaf = rt;
  1603. res = fib6_del(rt, &info);
  1604. if (res) {
  1605. #if RT6_DEBUG >= 2
  1606. pr_debug("%s: del failed: rt=%p@%p err=%d\n",
  1607. __func__, rt,
  1608. rcu_access_pointer(rt->rt6i_node),
  1609. res);
  1610. #endif
  1611. continue;
  1612. }
  1613. return 0;
  1614. }
  1615. WARN_ON(res != 0);
  1616. }
  1617. w->leaf = rt;
  1618. return 0;
  1619. }
  1620. /*
  1621. * Convenient frontend to tree walker.
  1622. *
  1623. * func is called on each route.
  1624. * It may return -1 -> delete this route.
  1625. * 0 -> continue walking
  1626. */
  1627. static void fib6_clean_tree(struct net *net, struct fib6_node *root,
  1628. int (*func)(struct rt6_info *, void *arg),
  1629. int sernum, void *arg)
  1630. {
  1631. struct fib6_cleaner c;
  1632. c.w.root = root;
  1633. c.w.func = fib6_clean_node;
  1634. c.w.count = 0;
  1635. c.w.skip = 0;
  1636. c.func = func;
  1637. c.sernum = sernum;
  1638. c.arg = arg;
  1639. c.net = net;
  1640. fib6_walk(net, &c.w);
  1641. }
  1642. static void __fib6_clean_all(struct net *net,
  1643. int (*func)(struct rt6_info *, void *),
  1644. int sernum, void *arg)
  1645. {
  1646. struct fib6_table *table;
  1647. struct hlist_head *head;
  1648. unsigned int h;
  1649. rcu_read_lock();
  1650. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  1651. head = &net->ipv6.fib_table_hash[h];
  1652. hlist_for_each_entry_rcu(table, head, tb6_hlist) {
  1653. spin_lock_bh(&table->tb6_lock);
  1654. fib6_clean_tree(net, &table->tb6_root,
  1655. func, sernum, arg);
  1656. spin_unlock_bh(&table->tb6_lock);
  1657. }
  1658. }
  1659. rcu_read_unlock();
  1660. }
  1661. void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
  1662. void *arg)
  1663. {
  1664. __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
  1665. }
  1666. static void fib6_flush_trees(struct net *net)
  1667. {
  1668. int new_sernum = fib6_new_sernum(net);
  1669. __fib6_clean_all(net, NULL, new_sernum, NULL);
  1670. }
  1671. /*
  1672. * Garbage collection
  1673. */
  1674. static int fib6_age(struct rt6_info *rt, void *arg)
  1675. {
  1676. struct fib6_gc_args *gc_args = arg;
  1677. unsigned long now = jiffies;
  1678. /*
  1679. * check addrconf expiration here.
  1680. * Routes are expired even if they are in use.
  1681. */
  1682. if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
  1683. if (time_after(now, rt->dst.expires)) {
  1684. RT6_TRACE("expiring %p\n", rt);
  1685. return -1;
  1686. }
  1687. gc_args->more++;
  1688. }
  1689. /* Also age clones in the exception table.
  1690. * Note, that clones are aged out
  1691. * only if they are not in use now.
  1692. */
  1693. rt6_age_exceptions(rt, gc_args, now);
  1694. return 0;
  1695. }
  1696. void fib6_run_gc(unsigned long expires, struct net *net, bool force)
  1697. {
  1698. struct fib6_gc_args gc_args;
  1699. unsigned long now;
  1700. if (force) {
  1701. spin_lock_bh(&net->ipv6.fib6_gc_lock);
  1702. } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
  1703. mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
  1704. return;
  1705. }
  1706. gc_args.timeout = expires ? (int)expires :
  1707. net->ipv6.sysctl.ip6_rt_gc_interval;
  1708. gc_args.more = 0;
  1709. fib6_clean_all(net, fib6_age, &gc_args);
  1710. now = jiffies;
  1711. net->ipv6.ip6_rt_last_gc = now;
  1712. if (gc_args.more)
  1713. mod_timer(&net->ipv6.ip6_fib_timer,
  1714. round_jiffies(now
  1715. + net->ipv6.sysctl.ip6_rt_gc_interval));
  1716. else
  1717. del_timer(&net->ipv6.ip6_fib_timer);
  1718. spin_unlock_bh(&net->ipv6.fib6_gc_lock);
  1719. }
  1720. static void fib6_gc_timer_cb(unsigned long arg)
  1721. {
  1722. fib6_run_gc(0, (struct net *)arg, true);
  1723. }
  1724. static int __net_init fib6_net_init(struct net *net)
  1725. {
  1726. size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
  1727. int err;
  1728. err = fib6_notifier_init(net);
  1729. if (err)
  1730. return err;
  1731. spin_lock_init(&net->ipv6.fib6_gc_lock);
  1732. rwlock_init(&net->ipv6.fib6_walker_lock);
  1733. INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
  1734. setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
  1735. net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
  1736. if (!net->ipv6.rt6_stats)
  1737. goto out_timer;
  1738. /* Avoid false sharing : Use at least a full cache line */
  1739. size = max_t(size_t, size, L1_CACHE_BYTES);
  1740. net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1741. if (!net->ipv6.fib_table_hash)
  1742. goto out_rt6_stats;
  1743. net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
  1744. GFP_KERNEL);
  1745. if (!net->ipv6.fib6_main_tbl)
  1746. goto out_fib_table_hash;
  1747. net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
  1748. rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
  1749. net->ipv6.ip6_null_entry);
  1750. net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
  1751. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1752. inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
  1753. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1754. net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
  1755. GFP_KERNEL);
  1756. if (!net->ipv6.fib6_local_tbl)
  1757. goto out_fib6_main_tbl;
  1758. net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
  1759. rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
  1760. net->ipv6.ip6_null_entry);
  1761. net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
  1762. RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
  1763. inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
  1764. #endif
  1765. fib6_tables_init(net);
  1766. return 0;
  1767. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  1768. out_fib6_main_tbl:
  1769. kfree(net->ipv6.fib6_main_tbl);
  1770. #endif
  1771. out_fib_table_hash:
  1772. kfree(net->ipv6.fib_table_hash);
  1773. out_rt6_stats:
  1774. kfree(net->ipv6.rt6_stats);
  1775. out_timer:
  1776. fib6_notifier_exit(net);
  1777. return -ENOMEM;
  1778. }
  1779. static void fib6_net_exit(struct net *net)
  1780. {
  1781. unsigned int i;
  1782. rt6_ifdown(net, NULL);
  1783. del_timer_sync(&net->ipv6.ip6_fib_timer);
  1784. for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
  1785. struct hlist_head *head = &net->ipv6.fib_table_hash[i];
  1786. struct hlist_node *tmp;
  1787. struct fib6_table *tb;
  1788. hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
  1789. hlist_del(&tb->tb6_hlist);
  1790. fib6_free_table(tb);
  1791. }
  1792. }
  1793. kfree(net->ipv6.fib_table_hash);
  1794. kfree(net->ipv6.rt6_stats);
  1795. fib6_notifier_exit(net);
  1796. }
  1797. static struct pernet_operations fib6_net_ops = {
  1798. .init = fib6_net_init,
  1799. .exit = fib6_net_exit,
  1800. };
  1801. int __init fib6_init(void)
  1802. {
  1803. int ret = -ENOMEM;
  1804. fib6_node_kmem = kmem_cache_create("fib6_nodes",
  1805. sizeof(struct fib6_node),
  1806. 0, SLAB_HWCACHE_ALIGN,
  1807. NULL);
  1808. if (!fib6_node_kmem)
  1809. goto out;
  1810. ret = register_pernet_subsys(&fib6_net_ops);
  1811. if (ret)
  1812. goto out_kmem_cache_create;
  1813. ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
  1814. 0);
  1815. if (ret)
  1816. goto out_unregister_subsys;
  1817. __fib6_flush_trees = fib6_flush_trees;
  1818. out:
  1819. return ret;
  1820. out_unregister_subsys:
  1821. unregister_pernet_subsys(&fib6_net_ops);
  1822. out_kmem_cache_create:
  1823. kmem_cache_destroy(fib6_node_kmem);
  1824. goto out;
  1825. }
  1826. void fib6_gc_cleanup(void)
  1827. {
  1828. unregister_pernet_subsys(&fib6_net_ops);
  1829. kmem_cache_destroy(fib6_node_kmem);
  1830. }
  1831. #ifdef CONFIG_PROC_FS
  1832. struct ipv6_route_iter {
  1833. struct seq_net_private p;
  1834. struct fib6_walker w;
  1835. loff_t skip;
  1836. struct fib6_table *tbl;
  1837. int sernum;
  1838. };
  1839. static int ipv6_route_seq_show(struct seq_file *seq, void *v)
  1840. {
  1841. struct rt6_info *rt = v;
  1842. struct ipv6_route_iter *iter = seq->private;
  1843. seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
  1844. #ifdef CONFIG_IPV6_SUBTREES
  1845. seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
  1846. #else
  1847. seq_puts(seq, "00000000000000000000000000000000 00 ");
  1848. #endif
  1849. if (rt->rt6i_flags & RTF_GATEWAY)
  1850. seq_printf(seq, "%pi6", &rt->rt6i_gateway);
  1851. else
  1852. seq_puts(seq, "00000000000000000000000000000000");
  1853. seq_printf(seq, " %08x %08x %08x %08x %8s\n",
  1854. rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
  1855. rt->dst.__use, rt->rt6i_flags,
  1856. rt->dst.dev ? rt->dst.dev->name : "");
  1857. iter->w.leaf = NULL;
  1858. return 0;
  1859. }
  1860. static int ipv6_route_yield(struct fib6_walker *w)
  1861. {
  1862. struct ipv6_route_iter *iter = w->args;
  1863. if (!iter->skip)
  1864. return 1;
  1865. do {
  1866. iter->w.leaf = rcu_dereference_protected(
  1867. iter->w.leaf->dst.rt6_next,
  1868. lockdep_is_held(&iter->tbl->tb6_lock));
  1869. iter->skip--;
  1870. if (!iter->skip && iter->w.leaf)
  1871. return 1;
  1872. } while (iter->w.leaf);
  1873. return 0;
  1874. }
  1875. static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
  1876. struct net *net)
  1877. {
  1878. memset(&iter->w, 0, sizeof(iter->w));
  1879. iter->w.func = ipv6_route_yield;
  1880. iter->w.root = &iter->tbl->tb6_root;
  1881. iter->w.state = FWS_INIT;
  1882. iter->w.node = iter->w.root;
  1883. iter->w.args = iter;
  1884. iter->sernum = iter->w.root->fn_sernum;
  1885. INIT_LIST_HEAD(&iter->w.lh);
  1886. fib6_walker_link(net, &iter->w);
  1887. }
  1888. static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
  1889. struct net *net)
  1890. {
  1891. unsigned int h;
  1892. struct hlist_node *node;
  1893. if (tbl) {
  1894. h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
  1895. node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
  1896. } else {
  1897. h = 0;
  1898. node = NULL;
  1899. }
  1900. while (!node && h < FIB6_TABLE_HASHSZ) {
  1901. node = rcu_dereference_bh(
  1902. hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
  1903. }
  1904. return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
  1905. }
  1906. static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
  1907. {
  1908. if (iter->sernum != iter->w.root->fn_sernum) {
  1909. iter->sernum = iter->w.root->fn_sernum;
  1910. iter->w.state = FWS_INIT;
  1911. iter->w.node = iter->w.root;
  1912. WARN_ON(iter->w.skip);
  1913. iter->w.skip = iter->w.count;
  1914. }
  1915. }
  1916. static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1917. {
  1918. int r;
  1919. struct rt6_info *n;
  1920. struct net *net = seq_file_net(seq);
  1921. struct ipv6_route_iter *iter = seq->private;
  1922. if (!v)
  1923. goto iter_table;
  1924. n = rcu_dereference(((struct rt6_info *)v)->dst.rt6_next);
  1925. if (n) {
  1926. ++*pos;
  1927. return n;
  1928. }
  1929. iter_table:
  1930. ipv6_route_check_sernum(iter);
  1931. spin_lock_bh(&iter->tbl->tb6_lock);
  1932. r = fib6_walk_continue(&iter->w);
  1933. spin_unlock_bh(&iter->tbl->tb6_lock);
  1934. if (r > 0) {
  1935. if (v)
  1936. ++*pos;
  1937. return iter->w.leaf;
  1938. } else if (r < 0) {
  1939. fib6_walker_unlink(net, &iter->w);
  1940. return NULL;
  1941. }
  1942. fib6_walker_unlink(net, &iter->w);
  1943. iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
  1944. if (!iter->tbl)
  1945. return NULL;
  1946. ipv6_route_seq_setup_walk(iter, net);
  1947. goto iter_table;
  1948. }
  1949. static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
  1950. __acquires(RCU_BH)
  1951. {
  1952. struct net *net = seq_file_net(seq);
  1953. struct ipv6_route_iter *iter = seq->private;
  1954. rcu_read_lock_bh();
  1955. iter->tbl = ipv6_route_seq_next_table(NULL, net);
  1956. iter->skip = *pos;
  1957. if (iter->tbl) {
  1958. ipv6_route_seq_setup_walk(iter, net);
  1959. return ipv6_route_seq_next(seq, NULL, pos);
  1960. } else {
  1961. return NULL;
  1962. }
  1963. }
  1964. static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
  1965. {
  1966. struct fib6_walker *w = &iter->w;
  1967. return w->node && !(w->state == FWS_U && w->node == w->root);
  1968. }
  1969. static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
  1970. __releases(RCU_BH)
  1971. {
  1972. struct net *net = seq_file_net(seq);
  1973. struct ipv6_route_iter *iter = seq->private;
  1974. if (ipv6_route_iter_active(iter))
  1975. fib6_walker_unlink(net, &iter->w);
  1976. rcu_read_unlock_bh();
  1977. }
  1978. static const struct seq_operations ipv6_route_seq_ops = {
  1979. .start = ipv6_route_seq_start,
  1980. .next = ipv6_route_seq_next,
  1981. .stop = ipv6_route_seq_stop,
  1982. .show = ipv6_route_seq_show
  1983. };
  1984. int ipv6_route_open(struct inode *inode, struct file *file)
  1985. {
  1986. return seq_open_net(inode, file, &ipv6_route_seq_ops,
  1987. sizeof(struct ipv6_route_iter));
  1988. }
  1989. #endif /* CONFIG_PROC_FS */