ip6_fib.c 55 KB

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