ip6_fib.c 55 KB

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