ip6_fib.c 56 KB

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