blob: aa673a6a7e4328d03e89527ed3daaccd69c39084 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * ip6_flowlabel.c IPv6 flowlabel manager.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 */
7
Randy Dunlap4fc268d2006-01-11 12:17:47 -08008#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/errno.h>
10#include <linux/types.h>
11#include <linux/socket.h>
12#include <linux/net.h>
13#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/proc_fs.h>
16#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040018#include <linux/export.h>
Eric W. Biederman4f82f452012-05-24 10:37:59 -060019#include <linux/pid_namespace.h>
Willem de Bruijn59c820b2019-07-07 05:34:45 -040020#include <linux/jump_label_ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020022#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/sock.h>
24
25#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/rawv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/transp_v6.h>
28
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
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 */
Florent Fourcot53b47102013-11-07 17:53:13 +010034#define FL_MAX_LINGER 150 /* Maximal linger timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
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
43static atomic_t fl_size = ATOMIC_INIT(0);
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000044static struct ip6_flowlabel __rcu *fl_ht[FL_HASH_MASK+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Kees Cook24ed9602017-08-28 11:28:21 -070046static void ip6_fl_gc(struct timer_list *unused);
Kees Cook1d27e3e2017-10-04 16:27:04 -070047static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* FL hash table lock: it protects only of GC */
50
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000051static DEFINE_SPINLOCK(ip6_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* Big socket sock */
54
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +000055static DEFINE_SPINLOCK(ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Willem de Bruijn59c820b2019-07-07 05:34:45 -040057DEFINE_STATIC_KEY_DEFERRED_FALSE(ipv6_flowlabel_exclusive, HZ);
58EXPORT_SYMBOL(ipv6_flowlabel_exclusive);
59
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000060#define for_each_fl_rcu(hash, fl) \
Amerigo Wang6a98dcf2013-02-07 15:52:40 +000061 for (fl = rcu_dereference_bh(fl_ht[(hash)]); \
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000062 fl != NULL; \
Amerigo Wang6a98dcf2013-02-07 15:52:40 +000063 fl = rcu_dereference_bh(fl->next))
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000064#define for_each_fl_continue_rcu(fl) \
Amerigo Wang6a98dcf2013-02-07 15:52:40 +000065 for (fl = rcu_dereference_bh(fl->next); \
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000066 fl != NULL; \
Amerigo Wang6a98dcf2013-02-07 15:52:40 +000067 fl = rcu_dereference_bh(fl->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +000069#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
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070074static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct ip6_flowlabel *fl;
77
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000078 for_each_fl_rcu(FL_HASH(label), fl) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -080079 if (fl->label == label && net_eq(fl->fl_net, net))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return fl;
81 }
82 return NULL;
83}
84
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070085static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct ip6_flowlabel *fl;
88
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000089 rcu_read_lock_bh();
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070090 fl = __fl_lookup(net, label);
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +000091 if (fl && !atomic_inc_not_zero(&fl->users))
92 fl = NULL;
93 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return fl;
95}
96
Willem de Bruijn59c820b2019-07-07 05:34:45 -040097static 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
Eric Dumazet6c0afef2019-04-27 16:49:06 -0700104static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static void fl_free(struct ip6_flowlabel *fl)
116{
Willem de Bruijn59c820b2019-07-07 05:34:45 -0400117 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static void fl_release(struct ip6_flowlabel *fl)
127{
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000128 spin_lock_bh(&ip6_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
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 }
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000145 spin_unlock_bh(&ip6_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Kees Cook24ed9602017-08-28 11:28:21 -0700148static void ip6_fl_gc(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int i;
151 unsigned long now = jiffies;
152 unsigned long sched = 0;
153
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000154 spin_lock(&ip6_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Ian Morris67ba4152014-08-24 21:53:10 +0100156 for (i = 0; i <= FL_HASH_MASK; i++) {
Eric Dumazet7f0e44a2013-03-07 04:20:32 +0000157 struct ip6_flowlabel *fl;
158 struct ip6_flowlabel __rcu **flp;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 flp = &fl_ht[i];
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000161 while ((fl = rcu_dereference_protected(*flp,
162 lockdep_is_held(&ip6_fl_lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 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) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700183 mod_timer(&ip6_fl_gc_timer, sched);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000185 spin_unlock(&ip6_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000188static void __net_exit ip6_fl_purge(struct net *net)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700189{
190 int i;
191
Jan Stancek4762fb92015-02-11 14:06:23 +0100192 spin_lock_bh(&ip6_fl_lock);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700193 for (i = 0; i <= FL_HASH_MASK; i++) {
Eric Dumazet7f0e44a2013-03-07 04:20:32 +0000194 struct ip6_flowlabel *fl;
195 struct ip6_flowlabel __rcu **flp;
196
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700197 flp = &fl_ht[i];
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000198 while ((fl = rcu_dereference_protected(*flp,
199 lockdep_is_held(&ip6_fl_lock))) != NULL) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800200 if (net_eq(fl->fl_net, net) &&
201 atomic_read(&fl->users) == 0) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700202 *flp = fl->next;
203 fl_free(fl);
204 atomic_dec(&fl_size);
205 continue;
206 }
207 flp = &fl->next;
208 }
209 }
Jan Stancek4762fb92015-02-11 14:06:23 +0100210 spin_unlock_bh(&ip6_fl_lock);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700211}
212
213static struct ip6_flowlabel *fl_intern(struct net *net,
214 struct ip6_flowlabel *fl, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700216 struct ip6_flowlabel *lfl;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 fl->label = label & IPV6_FLOWLABEL_MASK;
219
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000220 spin_lock_bh(&ip6_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (label == 0) {
222 for (;;) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500223 fl->label = htonl(prandom_u32())&IPV6_FLOWLABEL_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (fl->label) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700225 lfl = __fl_lookup(net, fl->label);
Ian Morris63159f22015-03-29 14:00:04 +0100226 if (!lfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
228 }
229 }
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700230 } 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 */
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700239 lfl = __fl_lookup(net, fl->label);
Ian Morris53b24b82015-03-29 14:00:05 +0100240 if (lfl) {
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700241 atomic_inc(&lfl->users);
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000242 spin_unlock_bh(&ip6_fl_lock);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700243 return lfl;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 fl->lastuse = jiffies;
248 fl->next = fl_ht[FL_HASH(fl->label)];
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000249 rcu_assign_pointer(fl_ht[FL_HASH(fl->label)], fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 atomic_inc(&fl_size);
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000251 spin_unlock_bh(&ip6_fl_lock);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700252 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255
256
257/* Socket flowlabel lists */
258
Willem de Bruijn59c820b2019-07-07 05:34:45 -0400259struct ip6_flowlabel *__fl6_sock_lookup(struct sock *sk, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct ipv6_fl_socklist *sfl;
262 struct ipv6_pinfo *np = inet6_sk(sk);
263
264 label &= IPV6_FLOWLABEL_MASK;
265
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000266 rcu_read_lock_bh();
267 for_each_sk_fl_rcu(np, sfl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct ip6_flowlabel *fl = sfl->fl;
Eric Dumazet65a3c492019-06-06 14:32:34 -0700269
270 if (fl->label == label && atomic_inc_not_zero(&fl->users)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 fl->lastuse = jiffies;
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000272 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return fl;
274 }
275 }
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000276 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return NULL;
278}
Willem de Bruijn59c820b2019-07-07 05:34:45 -0400279EXPORT_SYMBOL_GPL(__fl6_sock_lookup);
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281void fl6_free_socklist(struct sock *sk)
282{
283 struct ipv6_pinfo *np = inet6_sk(sk);
284 struct ipv6_fl_socklist *sfl;
285
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000286 if (!rcu_access_pointer(np->ipv6_fl_list))
YOSHIFUJI Hideaki / 吉藤英明f256dc52013-01-30 09:26:42 +0000287 return;
288
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000289 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);
YOSHIFUJI Hideaki / 吉藤英明f256dc52013-01-30 09:26:42 +0000294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 fl_release(sfl->fl);
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000296 kfree_rcu(sfl, rcu);
297
298 spin_lock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000300 spin_unlock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
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
Ian Morris67ba4152014-08-24 21:53:10 +0100312struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
313 struct ip6_flowlabel *fl,
314 struct ipv6_txoptions *fopt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Ian Morris67ba4152014-08-24 21:53:10 +0100316 struct ipv6_txoptions *fl_opt = fl->opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900317
Ian Morris63159f22015-03-29 14:00:04 +0100318 if (!fopt || fopt->opt_flen == 0)
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900319 return fl_opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900320
Ian Morris53b24b82015-03-29 14:00:05 +0100321 if (fl_opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 opt_space->hopopt = fl_opt->hopopt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900323 opt_space->dst0opt = fl_opt->dst0opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 opt_space->opt_flen = fopt->opt_flen;
Eric Dumazet864e2a12017-10-21 12:26:23 -0700336 opt_space->tot_len = fopt->tot_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return opt_space;
338}
Chris Elstona495f832012-04-29 21:48:53 +0000339EXPORT_SYMBOL_GPL(fl6_merge_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341static 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
350static 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;
Florent Fourcot394055f2013-11-07 17:53:14 +0100358
359 spin_lock_bh(&ip6_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 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;
Florent Fourcot394055f2013-11-07 17:53:14 +0100367 spin_unlock_bh(&ip6_fl_lock);
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
372static struct ip6_flowlabel *
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000373fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
Christoph Hellwig86298282020-07-23 08:09:01 +0200374 sockptr_t optval, int optlen, int *err_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
David S. Miller684de402009-02-06 00:49:55 -0800376 struct ip6_flowlabel *fl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int olen;
378 int addr_type;
379 int err;
380
David S. Miller684de402009-02-06 00:49:55 -0800381 olen = optlen - CMSG_ALIGN(sizeof(*freq));
382 err = -EINVAL;
383 if (olen > 64 * 1024)
384 goto done;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 err = -ENOMEM;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800387 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100388 if (!fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (olen > 0) {
392 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500393 struct flowi6 flowi6;
Wei Wang26879da2016-05-02 21:40:07 -0700394 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 err = -ENOMEM;
397 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100398 if (!fl->opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto done;
400
401 memset(fl->opt, 0, sizeof(*fl->opt));
402 fl->opt->tot_len = sizeof(*fl->opt) + olen;
403 err = -EFAULT;
Christoph Hellwigd3c48152020-07-28 18:38:35 +0200404 if (copy_from_sockptr_offset(fl->opt + 1, optval,
405 CMSG_ALIGN(sizeof(*freq)), olen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto done;
407
408 msg.msg_controllen = olen;
Ian Morris67ba4152014-08-24 21:53:10 +0100409 msg.msg_control = (void *)(fl->opt+1);
David S. Miller4c9483b2011-03-12 16:22:43 -0500410 memset(&flowi6, 0, sizeof(flowi6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Wei Wang26879da2016-05-02 21:40:07 -0700412 ipc6.opt = fl->opt;
Willem de Bruijn5fdaa882018-07-06 10:12:57 -0400413 err = ip6_datagram_send_ctl(net, sk, &msg, &flowi6, &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (err)
415 goto done;
416 err = -EINVAL;
417 if (fl->opt->opt_flen)
418 goto done;
419 if (fl->opt->opt_nflen == 0) {
420 kfree(fl->opt);
421 fl->opt = NULL;
422 }
423 }
424
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500425 fl->fl_net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 fl->expires = jiffies;
427 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
428 if (err)
429 goto done;
430 fl->share = freq->flr_share;
431 addr_type = ipv6_addr_type(&freq->flr_dst);
Joe Perches35700212009-11-24 14:52:52 -0800432 if ((addr_type & IPV6_ADDR_MAPPED) ||
433 addr_type == IPV6_ADDR_ANY) {
James Morrisc6817e42006-10-30 18:56:06 -0800434 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 goto done;
James Morrisc6817e42006-10-30 18:56:06 -0800436 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000437 fl->dst = freq->flr_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 atomic_set(&fl->users, 1);
439 switch (fl->share) {
440 case IPV6_FL_S_EXCL:
441 case IPV6_FL_S_ANY:
442 break;
443 case IPV6_FL_S_PROCESS:
Eric W. Biederman4f82f452012-05-24 10:37:59 -0600444 fl->owner.pid = get_task_pid(current, PIDTYPE_PID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446 case IPV6_FL_S_USER:
Eric W. Biederman4f82f452012-05-24 10:37:59 -0600447 fl->owner.uid = current_euid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449 default:
450 err = -EINVAL;
451 goto done;
452 }
Eric Dumazetd44e3fa2019-07-10 06:40:11 -0700453 if (fl_shared_exclusive(fl) || fl->opt)
454 static_branch_deferred_inc(&ipv6_flowlabel_exclusive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return fl;
456
457done:
Eric Dumazetd44e3fa2019-07-10 06:40:11 -0700458 if (fl) {
459 kfree(fl->opt);
460 kfree(fl);
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *err_p = err;
463 return NULL;
464}
465
466static int mem_check(struct sock *sk)
467{
468 struct ipv6_pinfo *np = inet6_sk(sk);
469 struct ipv6_fl_socklist *sfl;
470 int room = FL_MAX_SIZE - atomic_read(&fl_size);
471 int count = 0;
472
473 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
474 return 0;
475
Hannes Frederic Sowaf8c31c82013-11-08 19:26:21 +0100476 rcu_read_lock_bh();
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000477 for_each_sk_fl_rcu(np, sfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 count++;
Hannes Frederic Sowaf8c31c82013-11-08 19:26:21 +0100479 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (room <= 0 ||
482 ((count >= FL_MAX_PER_SOCK ||
Joe Perches35700212009-11-24 14:52:52 -0800483 (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) &&
484 !capable(CAP_NET_ADMIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -ENOBUFS;
486
487 return 0;
488}
489
Pavel Emelyanov04028042007-10-18 05:14:58 -0700490static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
491 struct ip6_flowlabel *fl)
492{
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000493 spin_lock_bh(&ip6_sk_fl_lock);
Pavel Emelyanov04028042007-10-18 05:14:58 -0700494 sfl->fl = fl;
495 sfl->next = np->ipv6_fl_list;
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000496 rcu_assign_pointer(np->ipv6_fl_list, sfl);
497 spin_unlock_bh(&ip6_sk_fl_lock);
Pavel Emelyanov04028042007-10-18 05:14:58 -0700498}
499
Florent Fourcot46e5f402014-01-17 17:15:04 +0100500int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
501 int flags)
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +0100502{
503 struct ipv6_pinfo *np = inet6_sk(sk);
504 struct ipv6_fl_socklist *sfl;
505
Florent Fourcot46e5f402014-01-17 17:15:04 +0100506 if (flags & IPV6_FL_F_REMOTE) {
507 freq->flr_label = np->rcv_flowinfo & IPV6_FLOWLABEL_MASK;
508 return 0;
509 }
510
Florent Fourcotdf3687f2014-01-17 17:15:03 +0100511 if (np->repflow) {
512 freq->flr_label = np->flow_label;
513 return 0;
514 }
515
Florent Fourcot3fdfa5f2013-11-07 17:53:12 +0100516 rcu_read_lock_bh();
517
518 for_each_sk_fl_rcu(np, sfl) {
519 if (sfl->fl->label == (np->flow_label & IPV6_FLOWLABEL_MASK)) {
520 spin_lock_bh(&ip6_fl_lock);
521 freq->flr_label = sfl->fl->label;
522 freq->flr_dst = sfl->fl->dst;
523 freq->flr_share = sfl->fl->share;
524 freq->flr_expires = (sfl->fl->expires - jiffies) / HZ;
525 freq->flr_linger = sfl->fl->linger / HZ;
526
527 spin_unlock_bh(&ip6_fl_lock);
528 rcu_read_unlock_bh();
529 return 0;
530 }
531 }
532 rcu_read_unlock_bh();
533
534 return -ENOENT;
535}
536
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200537#define socklist_dereference(__sflp) \
538 rcu_dereference_protected(__sflp, lockdep_is_held(&ip6_sk_fl_lock))
539
540static int ipv6_flowlabel_put(struct sock *sk, struct in6_flowlabel_req *freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazet7f0e44a2013-03-07 04:20:32 +0000543 struct ipv6_fl_socklist __rcu **sflp;
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200544 struct ipv6_fl_socklist *sfl;
545
546 if (freq->flr_flags & IPV6_FL_F_REFLECT) {
547 if (sk->sk_protocol != IPPROTO_TCP)
548 return -ENOPROTOOPT;
549 if (!np->repflow)
550 return -ESRCH;
551 np->flow_label = 0;
552 np->repflow = 0;
553 return 0;
554 }
555
556 spin_lock_bh(&ip6_sk_fl_lock);
557 for (sflp = &np->ipv6_fl_list;
558 (sfl = socklist_dereference(*sflp)) != NULL;
559 sflp = &sfl->next) {
560 if (sfl->fl->label == freq->flr_label)
561 goto found;
562 }
563 spin_unlock_bh(&ip6_sk_fl_lock);
564 return -ESRCH;
565found:
566 if (freq->flr_label == (np->flow_label & IPV6_FLOWLABEL_MASK))
567 np->flow_label &= ~IPV6_FLOWLABEL_MASK;
568 *sflp = sfl->next;
569 spin_unlock_bh(&ip6_sk_fl_lock);
570 fl_release(sfl->fl);
571 kfree_rcu(sfl, rcu);
572 return 0;
573}
574
575static int ipv6_flowlabel_renew(struct sock *sk, struct in6_flowlabel_req *freq)
576{
577 struct ipv6_pinfo *np = inet6_sk(sk);
578 struct net *net = sock_net(sk);
579 struct ipv6_fl_socklist *sfl;
580 int err;
581
582 rcu_read_lock_bh();
583 for_each_sk_fl_rcu(np, sfl) {
584 if (sfl->fl->label == freq->flr_label) {
585 err = fl6_renew(sfl->fl, freq->flr_linger,
586 freq->flr_expires);
587 rcu_read_unlock_bh();
588 return err;
589 }
590 }
591 rcu_read_unlock_bh();
592
593 if (freq->flr_share == IPV6_FL_S_NONE &&
594 ns_capable(net->user_ns, CAP_NET_ADMIN)) {
595 struct ip6_flowlabel *fl = fl_lookup(net, freq->flr_label);
596
597 if (fl) {
598 err = fl6_renew(fl, freq->flr_linger,
599 freq->flr_expires);
600 fl_release(fl);
601 return err;
602 }
603 }
604 return -ESRCH;
605}
606
607static int ipv6_flowlabel_get(struct sock *sk, struct in6_flowlabel_req *freq,
Christoph Hellwig86298282020-07-23 08:09:01 +0200608 sockptr_t optval, int optlen)
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200609{
610 struct ipv6_fl_socklist *sfl, *sfl1 = NULL;
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700611 struct ip6_flowlabel *fl, *fl1 = NULL;
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200612 struct ipv6_pinfo *np = inet6_sk(sk);
613 struct net *net = sock_net(sk);
Linus Torvalds47ec5302020-08-05 20:13:21 -0700614 int err;
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700615
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200616 if (freq->flr_flags & IPV6_FL_F_REFLECT) {
617 if (net->ipv6.sysctl.flowlabel_consistency) {
618 net_info_ratelimited("Can not set IPV6_FL_F_REFLECT if flowlabel_consistency sysctl is enable\n");
619 return -EPERM;
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200622 if (sk->sk_protocol != IPPROTO_TCP)
623 return -ENOPROTOOPT;
624 np->repflow = 1;
625 return 0;
626 }
627
628 if (freq->flr_label & ~IPV6_FLOWLABEL_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -EINVAL;
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200630 if (net->ipv6.sysctl.flowlabel_state_ranges &&
631 (freq->flr_label & IPV6_FLOWLABEL_STATELESS_FLAG))
632 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200634 fl = fl_create(net, sk, freq, optval, optlen, &err);
635 if (!fl)
636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200638 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200640 if (freq->flr_label) {
641 err = -EEXIST;
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000642 rcu_read_lock_bh();
643 for_each_sk_fl_rcu(np, sfl) {
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200644 if (sfl->fl->label == freq->flr_label) {
645 if (freq->flr_flags & IPV6_FL_F_EXCL) {
646 rcu_read_unlock_bh();
647 goto done;
648 }
649 fl1 = sfl->fl;
650 if (!atomic_inc_not_zero(&fl1->users))
651 fl1 = NULL;
652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 }
YOSHIFUJI Hideaki / 吉藤英明18367682013-01-30 09:27:52 +0000655 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200657 if (!fl1)
658 fl1 = fl_lookup(net, freq->flr_label);
659 if (fl1) {
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700660recheck:
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200661 err = -EEXIST;
662 if (freq->flr_flags&IPV6_FL_F_EXCL)
663 goto release;
664 err = -EPERM;
665 if (fl1->share == IPV6_FL_S_EXCL ||
666 fl1->share != fl->share ||
667 ((fl1->share == IPV6_FL_S_PROCESS) &&
668 (fl1->owner.pid != fl->owner.pid)) ||
669 ((fl1->share == IPV6_FL_S_USER) &&
670 !uid_eq(fl1->owner.uid, fl->owner.uid)))
671 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200673 err = -ENOMEM;
674 if (!sfl1)
675 goto release;
676 if (fl->linger > fl1->linger)
677 fl1->linger = fl->linger;
678 if ((long)(fl->expires - fl1->expires) > 0)
679 fl1->expires = fl->expires;
680 fl_link(np, sfl1, fl1);
681 fl_free(fl);
682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684release:
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200685 fl_release(fl1);
686 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200688 }
689 err = -ENOENT;
690 if (!(freq->flr_flags & IPV6_FL_F_CREATE))
691 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200693 err = -ENOMEM;
694 if (!sfl1)
695 goto done;
Ian Morrise5d08d72014-11-23 21:28:43 +0000696
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200697 err = mem_check(sk);
698 if (err != 0)
699 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200701 fl1 = fl_intern(net, fl, freq->flr_label);
702 if (fl1)
703 goto recheck;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200705 if (!freq->flr_label) {
Christoph Hellwigd3c48152020-07-28 18:38:35 +0200706 size_t offset = offsetof(struct in6_flowlabel_req, flr_label);
707
708 if (copy_to_sockptr_offset(optval, offset, &fl->label,
709 sizeof(fl->label))) {
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200710 /* Intentionally ignore fault. */
David S. Miller6c94d362005-05-29 20:28:01 -0700711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200714 fl_link(np, sfl1, fl);
715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716done:
717 fl_free(fl);
718 kfree(sfl1);
719 return err;
720}
721
Christoph Hellwig86298282020-07-23 08:09:01 +0200722int ipv6_flowlabel_opt(struct sock *sk, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct in6_flowlabel_req freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (optlen < sizeof(freq))
727 return -EINVAL;
Christoph Hellwig86298282020-07-23 08:09:01 +0200728 if (copy_from_sockptr(&freq, optval, sizeof(freq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EFAULT;
730
731 switch (freq.flr_action) {
732 case IPV6_FL_A_PUT:
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200733 return ipv6_flowlabel_put(sk, &freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 case IPV6_FL_A_RENEW:
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200735 return ipv6_flowlabel_renew(sk, &freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case IPV6_FL_A_GET:
Christoph Hellwigff6a4cf2020-07-23 08:09:00 +0200737 return ipv6_flowlabel_get(sk, &freq, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 default:
739 return -EINVAL;
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743#ifdef CONFIG_PROC_FS
744
745struct ip6fl_iter_state {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700746 struct seq_net_private p;
Eric W. Biederman4f82f452012-05-24 10:37:59 -0600747 struct pid_namespace *pid_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int bucket;
749};
750
751#define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
752
753static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
754{
755 struct ip6_flowlabel *fl = NULL;
756 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700757 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000760 for_each_fl_rcu(state->bucket, fl) {
761 if (net_eq(fl->fl_net, net))
762 goto out;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000765 fl = NULL;
766out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return fl;
768}
769
770static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
771{
772 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700773 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000775 for_each_fl_continue_rcu(fl) {
776 if (net_eq(fl->fl_net, net))
777 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000779
780try_again:
781 if (++state->bucket <= FL_HASH_MASK) {
782 for_each_fl_rcu(state->bucket, fl) {
783 if (net_eq(fl->fl_net, net))
784 goto out;
785 }
786 goto try_again;
787 }
788 fl = NULL;
789
790out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return fl;
792}
793
794static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
795{
796 struct ip6_flowlabel *fl = ip6fl_get_first(seq);
797 if (fl)
798 while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
799 --pos;
800 return pos ? NULL : fl;
801}
802
803static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000804 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Christoph Hellwigad089782018-04-11 10:01:30 +0200806 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
807
Alexey Gladkov9d78ede2020-05-18 20:07:38 +0200808 state->pid_ns = proc_pid_ns(file_inode(seq->file)->i_sb);
Christoph Hellwigad089782018-04-11 10:01:30 +0200809
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000810 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
812}
813
814static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
815{
816 struct ip6_flowlabel *fl;
817
818 if (v == SEQ_START_TOKEN)
819 fl = ip6fl_get_first(seq);
820 else
821 fl = ip6fl_get_next(seq, v);
822 ++*pos;
823 return fl;
824}
825
826static void ip6fl_seq_stop(struct seq_file *seq, void *v)
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000827 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
YOSHIFUJI Hideaki / 吉藤英明d3aedd52013-01-30 09:27:47 +0000829 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
James Morris1b7c2db2006-10-31 00:43:44 -0800832static int ip6fl_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Eric W. Biederman4f82f452012-05-24 10:37:59 -0600834 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Florent Fourcot869ba982014-11-04 23:01:00 +0100835 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -0800836 seq_puts(seq, "Label S Owner Users Linger Expires Dst Opt\n");
Florent Fourcot869ba982014-11-04 23:01:00 +0100837 } else {
James Morris1b7c2db2006-10-31 00:43:44 -0800838 struct ip6_flowlabel *fl = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700840 "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000841 (unsigned int)ntohl(fl->label),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 fl->share,
Eric W. Biederman4f82f452012-05-24 10:37:59 -0600843 ((fl->share == IPV6_FL_S_PROCESS) ?
844 pid_nr_ns(fl->owner.pid, state->pid_ns) :
845 ((fl->share == IPV6_FL_S_USER) ?
846 from_kuid_munged(seq_user_ns(seq), fl->owner.uid) :
847 0)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 atomic_read(&fl->users),
849 fl->linger/HZ,
850 (long)(fl->expires - jiffies)/HZ,
Harvey Harrisonb0711952008-10-28 16:05:40 -0700851 &fl->dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 fl->opt ? fl->opt->opt_nflen : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return 0;
855}
856
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700857static const struct seq_operations ip6fl_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 .start = ip6fl_seq_start,
859 .next = ip6fl_seq_next,
860 .stop = ip6fl_seq_stop,
861 .show = ip6fl_seq_show,
862};
863
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000864static int __net_init ip6_flowlabel_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Christoph Hellwigc3506372018-04-10 19:42:55 +0200866 if (!proc_create_net("ip6_flowlabel", 0444, net->proc_net,
867 &ip6fl_seq_ops, sizeof(struct ip6fl_iter_state)))
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800868 return -ENOMEM;
869 return 0;
870}
871
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000872static void __net_exit ip6_flowlabel_proc_fini(struct net *net)
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800873{
Gao fengece31ff2013-02-18 01:34:56 +0000874 remove_proc_entry("ip6_flowlabel", net->proc_net);
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800875}
876#else
877static inline int ip6_flowlabel_proc_init(struct net *net)
878{
879 return 0;
880}
881static inline void ip6_flowlabel_proc_fini(struct net *net)
882{
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884#endif
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800885
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000886static void __net_exit ip6_flowlabel_net_exit(struct net *net)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700887{
888 ip6_fl_purge(net);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700889 ip6_flowlabel_proc_fini(net);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700890}
891
892static struct pernet_operations ip6_flowlabel_net_ops = {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700893 .init = ip6_flowlabel_proc_init,
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700894 .exit = ip6_flowlabel_net_exit,
895};
896
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800897int ip6_flowlabel_init(void)
898{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700899 return register_pernet_subsys(&ip6_flowlabel_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902void ip6_flowlabel_cleanup(void)
903{
Willem de Bruijn59c820b2019-07-07 05:34:45 -0400904 static_key_deferred_flush(&ipv6_flowlabel_exclusive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 del_timer(&ip6_fl_gc_timer);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700906 unregister_pernet_subsys(&ip6_flowlabel_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}