ip6_fib.c 48 KB

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