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