ip6_fib.c 66 KB

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