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