ip6_fib.c 43 KB

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