ip6_fib.c 56 KB

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