ip6_fib.c 64 KB

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