ip6_fib.c 49 KB

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