Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
[linux-2.6-block.git] / net / ipv6 / ip6_flowlabel.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      ip6_flowlabel.c         IPv6 flowlabel manager.
4  *
5  *      Authors:        Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  */
7
8 #include <linux/capability.h>
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
12 #include <linux/net.h>
13 #include <linux/netdevice.h>
14 #include <linux/in6.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/slab.h>
18 #include <linux/export.h>
19 #include <linux/pid_namespace.h>
20 #include <linux/jump_label_ratelimit.h>
21
22 #include <net/net_namespace.h>
23 #include <net/sock.h>
24
25 #include <net/ipv6.h>
26 #include <net/rawv6.h>
27 #include <net/transp_v6.h>
28
29 #include <linux/uaccess.h>
30
31 #define FL_MIN_LINGER   6       /* Minimal linger. It is set to 6sec specified
32                                    in old IPv6 RFC. Well, it was reasonable value.
33                                  */
34 #define FL_MAX_LINGER   150     /* Maximal linger timeout */
35
36 /* FL hash table */
37
38 #define FL_MAX_PER_SOCK 32
39 #define FL_MAX_SIZE     4096
40 #define FL_HASH_MASK    255
41 #define FL_HASH(l)      (ntohl(l)&FL_HASH_MASK)
42
43 static atomic_t fl_size = ATOMIC_INIT(0);
44 static struct ip6_flowlabel __rcu *fl_ht[FL_HASH_MASK+1];
45
46 static void ip6_fl_gc(struct timer_list *unused);
47 static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc);
48
49 /* FL hash table lock: it protects only of GC */
50
51 static DEFINE_SPINLOCK(ip6_fl_lock);
52
53 /* Big socket sock */
54
55 static DEFINE_SPINLOCK(ip6_sk_fl_lock);
56
57 DEFINE_STATIC_KEY_DEFERRED_FALSE(ipv6_flowlabel_exclusive, HZ);
58 EXPORT_SYMBOL(ipv6_flowlabel_exclusive);
59
60 #define for_each_fl_rcu(hash, fl)                               \
61         for (fl = rcu_dereference_bh(fl_ht[(hash)]);            \
62              fl != NULL;                                        \
63              fl = rcu_dereference_bh(fl->next))
64 #define for_each_fl_continue_rcu(fl)                            \
65         for (fl = rcu_dereference_bh(fl->next);                 \
66              fl != NULL;                                        \
67              fl = rcu_dereference_bh(fl->next))
68
69 #define for_each_sk_fl_rcu(np, sfl)                             \
70         for (sfl = rcu_dereference_bh(np->ipv6_fl_list);        \
71              sfl != NULL;                                       \
72              sfl = rcu_dereference_bh(sfl->next))
73
74 static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label)
75 {
76         struct ip6_flowlabel *fl;
77
78         for_each_fl_rcu(FL_HASH(label), fl) {
79                 if (fl->label == label && net_eq(fl->fl_net, net))
80                         return fl;
81         }
82         return NULL;
83 }
84
85 static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label)
86 {
87         struct ip6_flowlabel *fl;
88
89         rcu_read_lock_bh();
90         fl = __fl_lookup(net, label);
91         if (fl && !atomic_inc_not_zero(&fl->users))
92                 fl = NULL;
93         rcu_read_unlock_bh();
94         return fl;
95 }
96
97 static bool fl_shared_exclusive(struct ip6_flowlabel *fl)
98 {
99         return fl->share == IPV6_FL_S_EXCL ||
100                fl->share == IPV6_FL_S_PROCESS ||
101                fl->share == IPV6_FL_S_USER;
102 }
103
104 static void fl_free_rcu(struct rcu_head *head)
105 {
106         struct ip6_flowlabel *fl = container_of(head, struct ip6_flowlabel, rcu);
107
108         if (fl->share == IPV6_FL_S_PROCESS)
109                 put_pid(fl->owner.pid);
110         kfree(fl->opt);
111         kfree(fl);
112 }
113
114
115 static void fl_free(struct ip6_flowlabel *fl)
116 {
117         if (!fl)
118                 return;
119
120         if (fl_shared_exclusive(fl) || fl->opt)
121                 static_branch_slow_dec_deferred(&ipv6_flowlabel_exclusive);
122
123         call_rcu(&fl->rcu, fl_free_rcu);
124 }
125
126 static void fl_release(struct ip6_flowlabel *fl)
127 {
128         spin_lock_bh(&ip6_fl_lock);
129
130         fl->lastuse = jiffies;
131         if (atomic_dec_and_test(&fl->users)) {
132                 unsigned long ttd = fl->lastuse + fl->linger;
133                 if (time_after(ttd, fl->expires))
134                         fl->expires = ttd;
135                 ttd = fl->expires;
136                 if (fl->opt && fl->share == IPV6_FL_S_EXCL) {
137                         struct ipv6_txoptions *opt = fl->opt;
138                         fl->opt = NULL;
139                         kfree(opt);
140                 }
141                 if (!timer_pending(&ip6_fl_gc_timer) ||
142                     time_after(ip6_fl_gc_timer.expires, ttd))
143                         mod_timer(&ip6_fl_gc_timer, ttd);
144         }
145         spin_unlock_bh(&ip6_fl_lock);
146 }
147
148 static void ip6_fl_gc(struct timer_list *unused)
149 {
150         int i;
151         unsigned long now = jiffies;
152         unsigned long sched = 0;
153
154         spin_lock(&ip6_fl_lock);
155
156         for (i = 0; i <= FL_HASH_MASK; i++) {
157                 struct ip6_flowlabel *fl;
158                 struct ip6_flowlabel __rcu **flp;
159
160                 flp = &fl_ht[i];
161                 while ((fl = rcu_dereference_protected(*flp,
162                                                        lockdep_is_held(&ip6_fl_lock))) != NULL) {
163                         if (atomic_read(&fl->users) == 0) {
164                                 unsigned long ttd = fl->lastuse + fl->linger;
165                                 if (time_after(ttd, fl->expires))
166                                         fl->expires = ttd;
167                                 ttd = fl->expires;
168                                 if (time_after_eq(now, ttd)) {
169                                         *flp = fl->next;
170                                         fl_free(fl);
171                                         atomic_dec(&fl_size);
172                                         continue;
173                                 }
174                                 if (!sched || time_before(ttd, sched))
175                                         sched = ttd;
176                         }
177                         flp = &fl->next;
178                 }
179         }
180         if (!sched && atomic_read(&fl_size))
181                 sched = now + FL_MAX_LINGER;
182         if (sched) {
183                 mod_timer(&ip6_fl_gc_timer, sched);
184         }
185         spin_unlock(&ip6_fl_lock);
186 }
187
188 static void __net_exit ip6_fl_purge(struct net *net)
189 {
190         int i;
191
192         spin_lock_bh(&ip6_fl_lock);
193         for (i = 0; i <= FL_HASH_MASK; i++) {
194                 struct ip6_flowlabel *fl;
195                 struct ip6_flowlabel __rcu **flp;
196
197                 flp = &fl_ht[i];
198                 while ((fl = rcu_dereference_protected(*flp,
199                                                        lockdep_is_held(&ip6_fl_lock))) != NULL) {
200                         if (net_eq(fl->fl_net, net) &&
201                             atomic_read(&fl->users) == 0) {
202                                 *flp = fl->next;
203                                 fl_free(fl);
204                                 atomic_dec(&fl_size);
205                                 continue;
206                         }
207                         flp = &fl->next;
208                 }
209         }
210         spin_unlock_bh(&ip6_fl_lock);
211 }
212
213 static struct ip6_flowlabel *fl_intern(struct net *net,
214                                        struct ip6_flowlabel *fl, __be32 label)
215 {
216         struct ip6_flowlabel *lfl;
217
218         fl->label = label & IPV6_FLOWLABEL_MASK;
219
220         spin_lock_bh(&ip6_fl_lock);
221         if (label == 0) {
222                 for (;;) {
223                         fl->label = htonl(prandom_u32())&IPV6_FLOWLABEL_MASK;
224                         if (fl->label) {
225                                 lfl = __fl_lookup(net, fl->label);
226                                 if (!lfl)
227                                         break;
228                         }
229                 }
230         } else {
231                 /*
232                  * we dropper the ip6_fl_lock, so this entry could reappear
233                  * and we need to recheck with it.
234                  *
235                  * OTOH no need to search the active socket first, like it is
236                  * done in ipv6_flowlabel_opt - sock is locked, so new entry
237                  * with the same label can only appear on another sock
238                  */
239                 lfl = __fl_lookup(net, fl->label);
240                 if (lfl) {
241                         atomic_inc(&lfl->users);
242                         spin_unlock_bh(&ip6_fl_lock);
243                         return lfl;
244                 }
245         }
246
247         fl->lastuse = jiffies;
248         fl->next = fl_ht[FL_HASH(fl->label)];
249         rcu_assign_pointer(fl_ht[FL_HASH(fl->label)], fl);
250         atomic_inc(&fl_size);
251         spin_unlock_bh(&ip6_fl_lock);
252         return NULL;
253 }
254
255
256
257 /* Socket flowlabel lists */
258
259 struct ip6_flowlabel *__fl6_sock_lookup(struct sock *sk, __be32 label)
260 {
261         struct ipv6_fl_socklist *sfl;
262         struct ipv6_pinfo *np = inet6_sk(sk);
263
264         label &= IPV6_FLOWLABEL_MASK;
265
266         rcu_read_lock_bh();
267         for_each_sk_fl_rcu(np, sfl) {
268                 struct ip6_flowlabel *fl = sfl->fl;
269
270                 if (fl->label == label && atomic_inc_not_zero(&fl->users)) {
271                         fl->lastuse = jiffies;
272                         rcu_read_unlock_bh();
273                         return fl;
274                 }
275         }
276         rcu_read_unlock_bh();
277         return NULL;
278 }
279 EXPORT_SYMBOL_GPL(__fl6_sock_lookup);
280
281 void fl6_free_socklist(struct sock *sk)
282 {
283         struct ipv6_pinfo *np = inet6_sk(sk);
284         struct ipv6_fl_socklist *sfl;
285
286         if (!rcu_access_pointer(np->ipv6_fl_list))
287                 return;
288
289         spin_lock_bh(&ip6_sk_fl_lock);
290         while ((sfl = rcu_dereference_protected(np->ipv6_fl_list,
291                                                 lockdep_is_held(&ip6_sk_fl_lock))) != NULL) {
292                 np->ipv6_fl_list = sfl->next;
293                 spin_unlock_bh(&ip6_sk_fl_lock);
294
295                 fl_release(sfl->fl);
296                 kfree_rcu(sfl, rcu);
297
298                 spin_lock_bh(&ip6_sk_fl_lock);
299         }
300         spin_unlock_bh(&ip6_sk_fl_lock);
301 }
302
303 /* Service routines */
304
305
306 /*
307    It is the only difficult place. flowlabel enforces equal headers
308    before and including routing header, however user may supply options
309    following rthdr.
310  */
311
312 struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
313                                          struct ip6_flowlabel *fl,
314                                          struct ipv6_txoptions *fopt)
315 {
316         struct ipv6_txoptions *fl_opt = fl->opt;
317
318         if (!fopt || fopt->opt_flen == 0)
319                 return fl_opt;
320
321         if (fl_opt) {
322                 opt_space->hopopt = fl_opt->hopopt;
323                 opt_space->dst0opt = fl_opt->dst0opt;
324                 opt_space->srcrt = fl_opt->srcrt;
325                 opt_space->opt_nflen = fl_opt->opt_nflen;
326         } else {
327                 if (fopt->opt_nflen == 0)
328                         return fopt;
329                 opt_space->hopopt = NULL;
330                 opt_space->dst0opt = NULL;
331                 opt_space->srcrt = NULL;
332                 opt_space->opt_nflen = 0;
333         }
334         opt_space->dst1opt = fopt->dst1opt;
335         opt_space->opt_flen = fopt->opt_flen;
336         opt_space->tot_len = fopt->tot_len;
337         return opt_space;
338 }
339 EXPORT_SYMBOL_GPL(fl6_merge_options);
340
341 static unsigned long check_linger(unsigned long ttl)
342 {
343         if (ttl < FL_MIN_LINGER)
344                 return FL_MIN_LINGER*HZ;
345         if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
346                 return 0;
347         return ttl*HZ;
348 }
349
350 static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
351 {
352         linger = check_linger(linger);
353         if (!linger)
354                 return -EPERM;
355         expires = check_linger(expires);
356         if (!expires)
357                 return -EPERM;
358
359         spin_lock_bh(&ip6_fl_lock);
360         fl->lastuse = jiffies;
361         if (time_before(fl->linger, linger))
362                 fl->linger = linger;
363         if (time_before(expires, fl->linger))
364                 expires = fl->linger;
365         if (time_before(fl->expires, fl->lastuse + expires))
366                 fl->expires = fl->lastuse + expires;
367         spin_unlock_bh(&ip6_fl_lock);
368
369         return 0;
370 }
371
372 static struct ip6_flowlabel *
373 fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
374           char __user *optval, int optlen, int *err_p)
375 {
376         struct ip6_flowlabel *fl = NULL;
377         int olen;
378         int addr_type;
379         int err;
380
381         olen = optlen - CMSG_ALIGN(sizeof(*freq));
382         err = -EINVAL;
383         if (olen > 64 * 1024)
384                 goto done;
385
386         err = -ENOMEM;
387         fl = kzalloc(sizeof(*fl), GFP_KERNEL);
388         if (!fl)
389                 goto done;
390
391         if (olen > 0) {
392                 struct msghdr msg;
393                 struct flowi6 flowi6;
394                 struct ipcm6_cookie ipc6;
395
396                 err = -ENOMEM;
397                 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
398                 if (!fl->opt)
399                         goto done;
400
401                 memset(fl->opt, 0, sizeof(*fl->opt));
402                 fl->opt->tot_len = sizeof(*fl->opt) + olen;
403                 err = -EFAULT;
404                 if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
405                         goto done;
406
407                 msg.msg_controllen = olen;
408                 msg.msg_control = (void *)(fl->opt+1);
409                 memset(&flowi6, 0, sizeof(flowi6));
410
411                 ipc6.opt = fl->opt;
412                 err = ip6_datagram_send_ctl(net, sk, &msg, &flowi6, &ipc6);
413                 if (err)
414                         goto done;
415                 err = -EINVAL;
416                 if (fl->opt->opt_flen)
417                         goto done;
418                 if (fl->opt->opt_nflen == 0) {
419                         kfree(fl->opt);
420                         fl->opt = NULL;
421                 }
422         }
423
424         fl->fl_net = net;
425         fl->expires = jiffies;
426         err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
427         if (err)
428                 goto done;
429         fl->share = freq->flr_share;
430         addr_type = ipv6_addr_type(&freq->flr_dst);
431         if ((addr_type & IPV6_ADDR_MAPPED) ||
432             addr_type == IPV6_ADDR_ANY) {
433                 err = -EINVAL;
434                 goto done;
435         }
436         fl->dst = freq->flr_dst;
437         atomic_set(&fl->users, 1);
438         if (fl_shared_exclusive(fl) || fl->opt)
439                 static_branch_deferred_inc(&ipv6_flowlabel_exclusive);
440         switch (fl->share) {
441         case IPV6_FL_S_EXCL:
442         case IPV6_FL_S_ANY:
443                 break;
444         case IPV6_FL_S_PROCESS:
445                 fl->owner.pid = get_task_pid(current, PIDTYPE_PID);
446                 break;
447         case IPV6_FL_S_USER:
448                 fl->owner.uid = current_euid();
449                 break;
450         default:
451                 err = -EINVAL;
452                 goto done;
453         }
454         return fl;
455
456 done:
457         fl_free(fl);
458         *err_p = err;
459         return NULL;
460 }
461
462 static int mem_check(struct sock *sk)
463 {
464         struct ipv6_pinfo *np = inet6_sk(sk);
465         struct ipv6_fl_socklist *sfl;
466         int room = FL_MAX_SIZE - atomic_read(&fl_size);
467         int count = 0;
468
469         if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
470                 return 0;
471
472         rcu_read_lock_bh();
473         for_each_sk_fl_rcu(np, sfl)
474                 count++;
475         rcu_read_unlock_bh();
476
477         if (room <= 0 ||
478             ((count >= FL_MAX_PER_SOCK ||
479               (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) &&
480              !capable(CAP_NET_ADMIN)))
481                 return -ENOBUFS;
482
483         return 0;
484 }
485
486 static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
487                 struct ip6_flowlabel *fl)
488 {
489         spin_lock_bh(&ip6_sk_fl_lock);
490         sfl->fl = fl;
491         sfl->next = np->ipv6_fl_list;
492         rcu_assign_pointer(np->ipv6_fl_list, sfl);
493         spin_unlock_bh(&ip6_sk_fl_lock);
494 }
495
496 int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
497                            int flags)
498 {
499         struct ipv6_pinfo *np = inet6_sk(sk);
500         struct ipv6_fl_socklist *sfl;
501
502         if (flags & IPV6_FL_F_REMOTE) {
503                 freq->flr_label = np->rcv_flowinfo & IPV6_FLOWLABEL_MASK;
504                 return 0;
505         }
506
507         if (np->repflow) {
508                 freq->flr_label = np->flow_label;
509                 return 0;
510         }
511
512         rcu_read_lock_bh();
513
514         for_each_sk_fl_rcu(np, sfl) {
515                 if (sfl->fl->label == (np->flow_label & IPV6_FLOWLABEL_MASK)) {
516                         spin_lock_bh(&ip6_fl_lock);
517                         freq->flr_label = sfl->fl->label;
518                         freq->flr_dst = sfl->fl->dst;
519                         freq->flr_share = sfl->fl->share;
520                         freq->flr_expires = (sfl->fl->expires - jiffies) / HZ;
521                         freq->flr_linger = sfl->fl->linger / HZ;
522
523                         spin_unlock_bh(&ip6_fl_lock);
524                         rcu_read_unlock_bh();
525                         return 0;
526                 }
527         }
528         rcu_read_unlock_bh();
529
530         return -ENOENT;
531 }
532
533 int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
534 {
535         int uninitialized_var(err);
536         struct net *net = sock_net(sk);
537         struct ipv6_pinfo *np = inet6_sk(sk);
538         struct in6_flowlabel_req freq;
539         struct ipv6_fl_socklist *sfl1 = NULL;
540         struct ipv6_fl_socklist *sfl;
541         struct ipv6_fl_socklist __rcu **sflp;
542         struct ip6_flowlabel *fl, *fl1 = NULL;
543
544
545         if (optlen < sizeof(freq))
546                 return -EINVAL;
547
548         if (copy_from_user(&freq, optval, sizeof(freq)))
549                 return -EFAULT;
550
551         switch (freq.flr_action) {
552         case IPV6_FL_A_PUT:
553                 if (freq.flr_flags & IPV6_FL_F_REFLECT) {
554                         if (sk->sk_protocol != IPPROTO_TCP)
555                                 return -ENOPROTOOPT;
556                         if (!np->repflow)
557                                 return -ESRCH;
558                         np->flow_label = 0;
559                         np->repflow = 0;
560                         return 0;
561                 }
562                 spin_lock_bh(&ip6_sk_fl_lock);
563                 for (sflp = &np->ipv6_fl_list;
564                      (sfl = rcu_dereference_protected(*sflp,
565                                                       lockdep_is_held(&ip6_sk_fl_lock))) != NULL;
566                      sflp = &sfl->next) {
567                         if (sfl->fl->label == freq.flr_label) {
568                                 if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
569                                         np->flow_label &= ~IPV6_FLOWLABEL_MASK;
570                                 *sflp = sfl->next;
571                                 spin_unlock_bh(&ip6_sk_fl_lock);
572                                 fl_release(sfl->fl);
573                                 kfree_rcu(sfl, rcu);
574                                 return 0;
575                         }
576                 }
577                 spin_unlock_bh(&ip6_sk_fl_lock);
578                 return -ESRCH;
579
580         case IPV6_FL_A_RENEW:
581                 rcu_read_lock_bh();
582                 for_each_sk_fl_rcu(np, sfl) {
583                         if (sfl->fl->label == freq.flr_label) {
584                                 err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
585                                 rcu_read_unlock_bh();
586                                 return err;
587                         }
588                 }
589                 rcu_read_unlock_bh();
590
591                 if (freq.flr_share == IPV6_FL_S_NONE &&
592                     ns_capable(net->user_ns, CAP_NET_ADMIN)) {
593                         fl = fl_lookup(net, freq.flr_label);
594                         if (fl) {
595                                 err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
596                                 fl_release(fl);
597                                 return err;
598                         }
599                 }
600                 return -ESRCH;
601
602         case IPV6_FL_A_GET:
603                 if (freq.flr_flags & IPV6_FL_F_REFLECT) {
604                         struct net *net = sock_net(sk);
605                         if (net->ipv6.sysctl.flowlabel_consistency) {
606                                 net_info_ratelimited("Can not set IPV6_FL_F_REFLECT if flowlabel_consistency sysctl is enable\n");
607                                 return -EPERM;
608                         }
609
610                         if (sk->sk_protocol != IPPROTO_TCP)
611                                 return -ENOPROTOOPT;
612
613                         np->repflow = 1;
614                         return 0;
615                 }
616
617                 if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
618                         return -EINVAL;
619
620                 if (net->ipv6.sysctl.flowlabel_state_ranges &&
621                     (freq.flr_label & IPV6_FLOWLABEL_STATELESS_FLAG))
622                         return -ERANGE;
623
624                 fl = fl_create(net, sk, &freq, optval, optlen, &err);
625                 if (!fl)
626                         return err;
627                 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
628
629                 if (freq.flr_label) {
630                         err = -EEXIST;
631                         rcu_read_lock_bh();
632                         for_each_sk_fl_rcu(np, sfl) {
633                                 if (sfl->fl->label == freq.flr_label) {
634                                         if (freq.flr_flags&IPV6_FL_F_EXCL) {
635                                                 rcu_read_unlock_bh();
636                                                 goto done;
637                                         }
638                                         fl1 = sfl->fl;
639                                         if (!atomic_inc_not_zero(&fl1->users))
640                                                 fl1 = NULL;
641                                         break;
642                                 }
643                         }
644                         rcu_read_unlock_bh();
645
646                         if (!fl1)
647                                 fl1 = fl_lookup(net, freq.flr_label);
648                         if (fl1) {
649 recheck:
650                                 err = -EEXIST;
651                                 if (freq.flr_flags&IPV6_FL_F_EXCL)
652                                         goto release;
653                                 err = -EPERM;
654                                 if (fl1->share == IPV6_FL_S_EXCL ||
655                                     fl1->share != fl->share ||
656                                     ((fl1->share == IPV6_FL_S_PROCESS) &&
657                                      (fl1->owner.pid != fl->owner.pid)) ||
658                                     ((fl1->share == IPV6_FL_S_USER) &&
659                                      !uid_eq(fl1->owner.uid, fl->owner.uid)))
660                                         goto release;
661
662                                 err = -ENOMEM;
663                                 if (!sfl1)
664                                         goto release;
665                                 if (fl->linger > fl1->linger)
666                                         fl1->linger = fl->linger;
667                                 if ((long)(fl->expires - fl1->expires) > 0)
668                                         fl1->expires = fl->expires;
669                                 fl_link(np, sfl1, fl1);
670                                 fl_free(fl);
671                                 return 0;
672
673 release:
674                                 fl_release(fl1);
675                                 goto done;
676                         }
677                 }
678                 err = -ENOENT;
679                 if (!(freq.flr_flags&IPV6_FL_F_CREATE))
680                         goto done;
681
682                 err = -ENOMEM;
683                 if (!sfl1)
684                         goto done;
685
686                 err = mem_check(sk);
687                 if (err != 0)
688                         goto done;
689
690                 fl1 = fl_intern(net, fl, freq.flr_label);
691                 if (fl1)
692                         goto recheck;
693
694                 if (!freq.flr_label) {
695                         if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
696                                          &fl->label, sizeof(fl->label))) {
697                                 /* Intentionally ignore fault. */
698                         }
699                 }
700
701                 fl_link(np, sfl1, fl);
702                 return 0;
703
704         default:
705                 return -EINVAL;
706         }
707
708 done:
709         fl_free(fl);
710         kfree(sfl1);
711         return err;
712 }
713
714 #ifdef CONFIG_PROC_FS
715
716 struct ip6fl_iter_state {
717         struct seq_net_private p;
718         struct pid_namespace *pid_ns;
719         int bucket;
720 };
721
722 #define ip6fl_seq_private(seq)  ((struct ip6fl_iter_state *)(seq)->private)
723
724 static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
725 {
726         struct ip6_flowlabel *fl = NULL;
727         struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
728         struct net *net = seq_file_net(seq);
729
730         for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
731                 for_each_fl_rcu(state->bucket, fl) {
732                         if (net_eq(fl->fl_net, net))
733                                 goto out;
734                 }
735         }
736         fl = NULL;
737 out:
738         return fl;
739 }
740
741 static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
742 {
743         struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
744         struct net *net = seq_file_net(seq);
745
746         for_each_fl_continue_rcu(fl) {
747                 if (net_eq(fl->fl_net, net))
748                         goto out;
749         }
750
751 try_again:
752         if (++state->bucket <= FL_HASH_MASK) {
753                 for_each_fl_rcu(state->bucket, fl) {
754                         if (net_eq(fl->fl_net, net))
755                                 goto out;
756                 }
757                 goto try_again;
758         }
759         fl = NULL;
760
761 out:
762         return fl;
763 }
764
765 static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
766 {
767         struct ip6_flowlabel *fl = ip6fl_get_first(seq);
768         if (fl)
769                 while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
770                         --pos;
771         return pos ? NULL : fl;
772 }
773
774 static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
775         __acquires(RCU)
776 {
777         struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
778
779         state->pid_ns = proc_pid_ns(file_inode(seq->file));
780
781         rcu_read_lock_bh();
782         return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
783 }
784
785 static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
786 {
787         struct ip6_flowlabel *fl;
788
789         if (v == SEQ_START_TOKEN)
790                 fl = ip6fl_get_first(seq);
791         else
792                 fl = ip6fl_get_next(seq, v);
793         ++*pos;
794         return fl;
795 }
796
797 static void ip6fl_seq_stop(struct seq_file *seq, void *v)
798         __releases(RCU)
799 {
800         rcu_read_unlock_bh();
801 }
802
803 static int ip6fl_seq_show(struct seq_file *seq, void *v)
804 {
805         struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
806         if (v == SEQ_START_TOKEN) {
807                 seq_puts(seq, "Label S Owner  Users  Linger Expires  Dst                              Opt\n");
808         } else {
809                 struct ip6_flowlabel *fl = v;
810                 seq_printf(seq,
811                            "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
812                            (unsigned int)ntohl(fl->label),
813                            fl->share,
814                            ((fl->share == IPV6_FL_S_PROCESS) ?
815                             pid_nr_ns(fl->owner.pid, state->pid_ns) :
816                             ((fl->share == IPV6_FL_S_USER) ?
817                              from_kuid_munged(seq_user_ns(seq), fl->owner.uid) :
818                              0)),
819                            atomic_read(&fl->users),
820                            fl->linger/HZ,
821                            (long)(fl->expires - jiffies)/HZ,
822                            &fl->dst,
823                            fl->opt ? fl->opt->opt_nflen : 0);
824         }
825         return 0;
826 }
827
828 static const struct seq_operations ip6fl_seq_ops = {
829         .start  =       ip6fl_seq_start,
830         .next   =       ip6fl_seq_next,
831         .stop   =       ip6fl_seq_stop,
832         .show   =       ip6fl_seq_show,
833 };
834
835 static int __net_init ip6_flowlabel_proc_init(struct net *net)
836 {
837         if (!proc_create_net("ip6_flowlabel", 0444, net->proc_net,
838                         &ip6fl_seq_ops, sizeof(struct ip6fl_iter_state)))
839                 return -ENOMEM;
840         return 0;
841 }
842
843 static void __net_exit ip6_flowlabel_proc_fini(struct net *net)
844 {
845         remove_proc_entry("ip6_flowlabel", net->proc_net);
846 }
847 #else
848 static inline int ip6_flowlabel_proc_init(struct net *net)
849 {
850         return 0;
851 }
852 static inline void ip6_flowlabel_proc_fini(struct net *net)
853 {
854 }
855 #endif
856
857 static void __net_exit ip6_flowlabel_net_exit(struct net *net)
858 {
859         ip6_fl_purge(net);
860         ip6_flowlabel_proc_fini(net);
861 }
862
863 static struct pernet_operations ip6_flowlabel_net_ops = {
864         .init = ip6_flowlabel_proc_init,
865         .exit = ip6_flowlabel_net_exit,
866 };
867
868 int ip6_flowlabel_init(void)
869 {
870         return register_pernet_subsys(&ip6_flowlabel_net_ops);
871 }
872
873 void ip6_flowlabel_cleanup(void)
874 {
875         static_key_deferred_flush(&ipv6_flowlabel_exclusive);
876         del_timer(&ip6_fl_gc_timer);
877         unregister_pernet_subsys(&ip6_flowlabel_net_ops);
878 }