blob: 9772fbd8a3f5b5c3ce36f1715153f21673ea5acd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip6_flowlabel.c IPv6 flowlabel manager.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
Randy Dunlap4fc268d2006-01-11 12:17:47 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/types.h>
15#include <linux/socket.h>
16#include <linux/net.h>
17#include <linux/netdevice.h>
18#include <linux/if_arp.h>
19#include <linux/in6.h>
20#include <linux/route.h>
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020026#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/sock.h>
28
29#include <net/ipv6.h>
30#include <net/ndisc.h>
31#include <net/protocol.h>
32#include <net/ip6_route.h>
33#include <net/addrconf.h>
34#include <net/rawv6.h>
35#include <net/icmp.h>
36#include <net/transp_v6.h>
37
38#include <asm/uaccess.h>
39
40#define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
41 in old IPv6 RFC. Well, it was reasonable value.
42 */
43#define FL_MAX_LINGER 60 /* Maximal linger timeout */
44
45/* FL hash table */
46
47#define FL_MAX_PER_SOCK 32
48#define FL_MAX_SIZE 4096
49#define FL_HASH_MASK 255
50#define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
51
52static atomic_t fl_size = ATOMIC_INIT(0);
53static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1];
54
55static void ip6_fl_gc(unsigned long dummy);
Ingo Molnar8d06afa2005-09-09 13:10:40 -070056static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/* FL hash table lock: it protects only of GC */
59
60static DEFINE_RWLOCK(ip6_fl_lock);
61
62/* Big socket sock */
63
64static DEFINE_RWLOCK(ip6_sk_fl_lock);
65
66
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070067static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct ip6_flowlabel *fl;
70
71 for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -080072 if (fl->label == label && net_eq(fl->fl_net, net))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return fl;
74 }
75 return NULL;
76}
77
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070078static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct ip6_flowlabel *fl;
81
82 read_lock_bh(&ip6_fl_lock);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070083 fl = __fl_lookup(net, label);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (fl)
85 atomic_inc(&fl->users);
86 read_unlock_bh(&ip6_fl_lock);
87 return fl;
88}
89
90
91static void fl_free(struct ip6_flowlabel *fl)
92{
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070093 if (fl) {
94 release_net(fl->fl_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 kfree(fl->opt);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 kfree(fl);
98}
99
100static void fl_release(struct ip6_flowlabel *fl)
101{
102 write_lock_bh(&ip6_fl_lock);
103
104 fl->lastuse = jiffies;
105 if (atomic_dec_and_test(&fl->users)) {
106 unsigned long ttd = fl->lastuse + fl->linger;
107 if (time_after(ttd, fl->expires))
108 fl->expires = ttd;
109 ttd = fl->expires;
110 if (fl->opt && fl->share == IPV6_FL_S_EXCL) {
111 struct ipv6_txoptions *opt = fl->opt;
112 fl->opt = NULL;
113 kfree(opt);
114 }
115 if (!timer_pending(&ip6_fl_gc_timer) ||
116 time_after(ip6_fl_gc_timer.expires, ttd))
117 mod_timer(&ip6_fl_gc_timer, ttd);
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 write_unlock_bh(&ip6_fl_lock);
120}
121
122static void ip6_fl_gc(unsigned long dummy)
123{
124 int i;
125 unsigned long now = jiffies;
126 unsigned long sched = 0;
127
128 write_lock(&ip6_fl_lock);
129
130 for (i=0; i<=FL_HASH_MASK; i++) {
131 struct ip6_flowlabel *fl, **flp;
132 flp = &fl_ht[i];
133 while ((fl=*flp) != NULL) {
134 if (atomic_read(&fl->users) == 0) {
135 unsigned long ttd = fl->lastuse + fl->linger;
136 if (time_after(ttd, fl->expires))
137 fl->expires = ttd;
138 ttd = fl->expires;
139 if (time_after_eq(now, ttd)) {
140 *flp = fl->next;
141 fl_free(fl);
142 atomic_dec(&fl_size);
143 continue;
144 }
145 if (!sched || time_before(ttd, sched))
146 sched = ttd;
147 }
148 flp = &fl->next;
149 }
150 }
151 if (!sched && atomic_read(&fl_size))
152 sched = now + FL_MAX_LINGER;
153 if (sched) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700154 mod_timer(&ip6_fl_gc_timer, sched);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 write_unlock(&ip6_fl_lock);
157}
158
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000159static void __net_exit ip6_fl_purge(struct net *net)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700160{
161 int i;
162
163 write_lock(&ip6_fl_lock);
164 for (i = 0; i <= FL_HASH_MASK; i++) {
165 struct ip6_flowlabel *fl, **flp;
166 flp = &fl_ht[i];
167 while ((fl = *flp) != NULL) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800168 if (net_eq(fl->fl_net, net) &&
169 atomic_read(&fl->users) == 0) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700170 *flp = fl->next;
171 fl_free(fl);
172 atomic_dec(&fl_size);
173 continue;
174 }
175 flp = &fl->next;
176 }
177 }
178 write_unlock(&ip6_fl_lock);
179}
180
181static struct ip6_flowlabel *fl_intern(struct net *net,
182 struct ip6_flowlabel *fl, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700184 struct ip6_flowlabel *lfl;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 fl->label = label & IPV6_FLOWLABEL_MASK;
187
188 write_lock_bh(&ip6_fl_lock);
189 if (label == 0) {
190 for (;;) {
191 fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK;
192 if (fl->label) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700193 lfl = __fl_lookup(net, fl->label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (lfl == NULL)
195 break;
196 }
197 }
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700198 } else {
199 /*
200 * we dropper the ip6_fl_lock, so this entry could reappear
201 * and we need to recheck with it.
202 *
203 * OTOH no need to search the active socket first, like it is
204 * done in ipv6_flowlabel_opt - sock is locked, so new entry
205 * with the same label can only appear on another sock
206 */
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700207 lfl = __fl_lookup(net, fl->label);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700208 if (lfl != NULL) {
209 atomic_inc(&lfl->users);
210 write_unlock_bh(&ip6_fl_lock);
211 return lfl;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
215 fl->lastuse = jiffies;
216 fl->next = fl_ht[FL_HASH(fl->label)];
217 fl_ht[FL_HASH(fl->label)] = fl;
218 atomic_inc(&fl_size);
219 write_unlock_bh(&ip6_fl_lock);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700220 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223
224
225/* Socket flowlabel lists */
226
Al Viro90bcaf72006-11-08 00:25:17 -0800227struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct ipv6_fl_socklist *sfl;
230 struct ipv6_pinfo *np = inet6_sk(sk);
231
232 label &= IPV6_FLOWLABEL_MASK;
233
Pavel Emelyanovbd0bf572007-10-18 05:15:57 -0700234 read_lock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) {
236 struct ip6_flowlabel *fl = sfl->fl;
237 if (fl->label == label) {
238 fl->lastuse = jiffies;
239 atomic_inc(&fl->users);
Pavel Emelyanov52f095e2007-10-18 05:38:48 -0700240 read_unlock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return fl;
242 }
243 }
Pavel Emelyanovbd0bf572007-10-18 05:15:57 -0700244 read_unlock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return NULL;
246}
247
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800248EXPORT_SYMBOL_GPL(fl6_sock_lookup);
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250void fl6_free_socklist(struct sock *sk)
251{
252 struct ipv6_pinfo *np = inet6_sk(sk);
253 struct ipv6_fl_socklist *sfl;
254
255 while ((sfl = np->ipv6_fl_list) != NULL) {
256 np->ipv6_fl_list = sfl->next;
257 fl_release(sfl->fl);
258 kfree(sfl);
259 }
260}
261
262/* Service routines */
263
264
265/*
266 It is the only difficult place. flowlabel enforces equal headers
267 before and including routing header, however user may supply options
268 following rthdr.
269 */
270
271struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
272 struct ip6_flowlabel * fl,
273 struct ipv6_txoptions * fopt)
274{
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900275 struct ipv6_txoptions * fl_opt = fl->opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900276
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900277 if (fopt == NULL || fopt->opt_flen == 0)
278 return fl_opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (fl_opt != NULL) {
281 opt_space->hopopt = fl_opt->hopopt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900282 opt_space->dst0opt = fl_opt->dst0opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 opt_space->srcrt = fl_opt->srcrt;
284 opt_space->opt_nflen = fl_opt->opt_nflen;
285 } else {
286 if (fopt->opt_nflen == 0)
287 return fopt;
288 opt_space->hopopt = NULL;
289 opt_space->dst0opt = NULL;
290 opt_space->srcrt = NULL;
291 opt_space->opt_nflen = 0;
292 }
293 opt_space->dst1opt = fopt->dst1opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 opt_space->opt_flen = fopt->opt_flen;
295 return opt_space;
296}
Chris Elstona495f832012-04-29 21:48:53 +0000297EXPORT_SYMBOL_GPL(fl6_merge_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299static unsigned long check_linger(unsigned long ttl)
300{
301 if (ttl < FL_MIN_LINGER)
302 return FL_MIN_LINGER*HZ;
303 if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
304 return 0;
305 return ttl*HZ;
306}
307
308static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
309{
310 linger = check_linger(linger);
311 if (!linger)
312 return -EPERM;
313 expires = check_linger(expires);
314 if (!expires)
315 return -EPERM;
316 fl->lastuse = jiffies;
317 if (time_before(fl->linger, linger))
318 fl->linger = linger;
319 if (time_before(expires, fl->linger))
320 expires = fl->linger;
321 if (time_before(fl->expires, fl->lastuse + expires))
322 fl->expires = fl->lastuse + expires;
323 return 0;
324}
325
326static struct ip6_flowlabel *
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000327fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
328 char __user *optval, int optlen, int *err_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
David S. Miller684de402009-02-06 00:49:55 -0800330 struct ip6_flowlabel *fl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int olen;
332 int addr_type;
333 int err;
334
David S. Miller684de402009-02-06 00:49:55 -0800335 olen = optlen - CMSG_ALIGN(sizeof(*freq));
336 err = -EINVAL;
337 if (olen > 64 * 1024)
338 goto done;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 err = -ENOMEM;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800341 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (fl == NULL)
343 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (olen > 0) {
346 struct msghdr msg;
David S. Miller4c9483b2011-03-12 16:22:43 -0500347 struct flowi6 flowi6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int junk;
349
350 err = -ENOMEM;
351 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
352 if (fl->opt == NULL)
353 goto done;
354
355 memset(fl->opt, 0, sizeof(*fl->opt));
356 fl->opt->tot_len = sizeof(*fl->opt) + olen;
357 err = -EFAULT;
358 if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
359 goto done;
360
361 msg.msg_controllen = olen;
362 msg.msg_control = (void*)(fl->opt+1);
David S. Miller4c9483b2011-03-12 16:22:43 -0500363 memset(&flowi6, 0, sizeof(flowi6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000365 err = datagram_send_ctl(net, sk, &msg, &flowi6, fl->opt, &junk,
Brian Haley13b52cd2010-04-23 11:26:08 +0000366 &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (err)
368 goto done;
369 err = -EINVAL;
370 if (fl->opt->opt_flen)
371 goto done;
372 if (fl->opt->opt_nflen == 0) {
373 kfree(fl->opt);
374 fl->opt = NULL;
375 }
376 }
377
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700378 fl->fl_net = hold_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 fl->expires = jiffies;
380 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
381 if (err)
382 goto done;
383 fl->share = freq->flr_share;
384 addr_type = ipv6_addr_type(&freq->flr_dst);
Joe Perches35700212009-11-24 14:52:52 -0800385 if ((addr_type & IPV6_ADDR_MAPPED) ||
386 addr_type == IPV6_ADDR_ANY) {
James Morrisc6817e42006-10-30 18:56:06 -0800387 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 goto done;
James Morrisc6817e42006-10-30 18:56:06 -0800389 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000390 fl->dst = freq->flr_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 atomic_set(&fl->users, 1);
392 switch (fl->share) {
393 case IPV6_FL_S_EXCL:
394 case IPV6_FL_S_ANY:
395 break;
396 case IPV6_FL_S_PROCESS:
397 fl->owner = current->pid;
398 break;
399 case IPV6_FL_S_USER:
David Howellsf82b35902008-11-14 10:39:07 +1100400 fl->owner = current_euid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
402 default:
403 err = -EINVAL;
404 goto done;
405 }
406 return fl;
407
408done:
409 fl_free(fl);
410 *err_p = err;
411 return NULL;
412}
413
414static int mem_check(struct sock *sk)
415{
416 struct ipv6_pinfo *np = inet6_sk(sk);
417 struct ipv6_fl_socklist *sfl;
418 int room = FL_MAX_SIZE - atomic_read(&fl_size);
419 int count = 0;
420
421 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
422 return 0;
423
424 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next)
425 count++;
426
427 if (room <= 0 ||
428 ((count >= FL_MAX_PER_SOCK ||
Joe Perches35700212009-11-24 14:52:52 -0800429 (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) &&
430 !capable(CAP_NET_ADMIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -ENOBUFS;
432
433 return 0;
434}
435
Eric Dumazeta50feda2012-05-18 18:57:34 +0000436static bool ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 if (h1 == h2)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000439 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (h1 == NULL || h2 == NULL)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000441 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (h1->hdrlen != h2->hdrlen)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000443 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
445}
446
Eric Dumazeta50feda2012-05-18 18:57:34 +0000447static bool ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 if (o1 == o2)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000450 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (o1 == NULL || o2 == NULL)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000452 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (o1->opt_nflen != o2->opt_nflen)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000454 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000456 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000458 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000460 return true;
461 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Pavel Emelyanov04028042007-10-18 05:14:58 -0700464static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
465 struct ip6_flowlabel *fl)
466{
467 write_lock_bh(&ip6_sk_fl_lock);
468 sfl->fl = fl;
469 sfl->next = np->ipv6_fl_list;
470 np->ipv6_fl_list = sfl;
471 write_unlock_bh(&ip6_sk_fl_lock);
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
475{
Ingo Molnar55205d42008-11-25 16:50:30 -0800476 int uninitialized_var(err);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700477 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct ipv6_pinfo *np = inet6_sk(sk);
479 struct in6_flowlabel_req freq;
480 struct ipv6_fl_socklist *sfl1=NULL;
481 struct ipv6_fl_socklist *sfl, **sflp;
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700482 struct ip6_flowlabel *fl, *fl1 = NULL;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (optlen < sizeof(freq))
486 return -EINVAL;
487
488 if (copy_from_user(&freq, optval, sizeof(freq)))
489 return -EFAULT;
490
491 switch (freq.flr_action) {
492 case IPV6_FL_A_PUT:
493 write_lock_bh(&ip6_sk_fl_lock);
494 for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
495 if (sfl->fl->label == freq.flr_label) {
496 if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
497 np->flow_label &= ~IPV6_FLOWLABEL_MASK;
498 *sflp = sfl->next;
499 write_unlock_bh(&ip6_sk_fl_lock);
500 fl_release(sfl->fl);
501 kfree(sfl);
502 return 0;
503 }
504 }
505 write_unlock_bh(&ip6_sk_fl_lock);
506 return -ESRCH;
507
508 case IPV6_FL_A_RENEW:
509 read_lock_bh(&ip6_sk_fl_lock);
510 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
511 if (sfl->fl->label == freq.flr_label) {
512 err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
513 read_unlock_bh(&ip6_sk_fl_lock);
514 return err;
515 }
516 }
517 read_unlock_bh(&ip6_sk_fl_lock);
518
519 if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700520 fl = fl_lookup(net, freq.flr_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (fl) {
522 err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
523 fl_release(fl);
524 return err;
525 }
526 }
527 return -ESRCH;
528
529 case IPV6_FL_A_GET:
530 if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
531 return -EINVAL;
532
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000533 fl = fl_create(net, sk, &freq, optval, optlen, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (fl == NULL)
535 return err;
536 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
537
538 if (freq.flr_label) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 err = -EEXIST;
540 read_lock_bh(&ip6_sk_fl_lock);
541 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
542 if (sfl->fl->label == freq.flr_label) {
543 if (freq.flr_flags&IPV6_FL_F_EXCL) {
544 read_unlock_bh(&ip6_sk_fl_lock);
545 goto done;
546 }
547 fl1 = sfl->fl;
Yan Zheng4ea6a802005-10-24 19:55:23 +0800548 atomic_inc(&fl1->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550 }
551 }
552 read_unlock_bh(&ip6_sk_fl_lock);
553
554 if (fl1 == NULL)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700555 fl1 = fl_lookup(net, freq.flr_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (fl1) {
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700557recheck:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 err = -EEXIST;
559 if (freq.flr_flags&IPV6_FL_F_EXCL)
560 goto release;
561 err = -EPERM;
562 if (fl1->share == IPV6_FL_S_EXCL ||
563 fl1->share != fl->share ||
564 fl1->owner != fl->owner)
565 goto release;
566
567 err = -EINVAL;
568 if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
569 ipv6_opt_cmp(fl1->opt, fl->opt))
570 goto release;
571
572 err = -ENOMEM;
573 if (sfl1 == NULL)
574 goto release;
575 if (fl->linger > fl1->linger)
576 fl1->linger = fl->linger;
577 if ((long)(fl->expires - fl1->expires) > 0)
578 fl1->expires = fl->expires;
Pavel Emelyanov04028042007-10-18 05:14:58 -0700579 fl_link(np, sfl1, fl1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 fl_free(fl);
581 return 0;
582
583release:
584 fl_release(fl1);
585 goto done;
586 }
587 }
588 err = -ENOENT;
589 if (!(freq.flr_flags&IPV6_FL_F_CREATE))
590 goto done;
591
592 err = -ENOMEM;
593 if (sfl1 == NULL || (err = mem_check(sk)) != 0)
594 goto done;
595
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700596 fl1 = fl_intern(net, fl, freq.flr_label);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700597 if (fl1 != NULL)
598 goto recheck;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
David S. Miller6c94d362005-05-29 20:28:01 -0700600 if (!freq.flr_label) {
601 if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
602 &fl->label, sizeof(fl->label))) {
603 /* Intentionally ignore fault. */
604 }
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Pavel Emelyanov04028042007-10-18 05:14:58 -0700607 fl_link(np, sfl1, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 0;
609
610 default:
611 return -EINVAL;
612 }
613
614done:
615 fl_free(fl);
616 kfree(sfl1);
617 return err;
618}
619
620#ifdef CONFIG_PROC_FS
621
622struct ip6fl_iter_state {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700623 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int bucket;
625};
626
627#define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
628
629static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
630{
631 struct ip6_flowlabel *fl = NULL;
632 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700633 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700636 fl = fl_ht[state->bucket];
637
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800638 while (fl && !net_eq(fl->fl_net, net))
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700639 fl = fl->next;
640 if (fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 return fl;
644}
645
646static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
647{
648 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700649 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 fl = fl->next;
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700652try_again:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800653 while (fl && !net_eq(fl->fl_net, net))
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700654 fl = fl->next;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 while (!fl) {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700657 if (++state->bucket <= FL_HASH_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 fl = fl_ht[state->bucket];
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700659 goto try_again;
660 } else
James Morrisbcd62072006-10-30 15:08:42 -0800661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 return fl;
664}
665
666static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
667{
668 struct ip6_flowlabel *fl = ip6fl_get_first(seq);
669 if (fl)
670 while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
671 --pos;
672 return pos ? NULL : fl;
673}
674
675static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800676 __acquires(ip6_fl_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 read_lock_bh(&ip6_fl_lock);
679 return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
680}
681
682static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
683{
684 struct ip6_flowlabel *fl;
685
686 if (v == SEQ_START_TOKEN)
687 fl = ip6fl_get_first(seq);
688 else
689 fl = ip6fl_get_next(seq, v);
690 ++*pos;
691 return fl;
692}
693
694static void ip6fl_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800695 __releases(ip6_fl_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 read_unlock_bh(&ip6_fl_lock);
698}
699
James Morris1b7c2db2006-10-31 00:43:44 -0800700static int ip6fl_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
James Morris1b7c2db2006-10-31 00:43:44 -0800702 if (v == SEQ_START_TOKEN)
703 seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
704 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
705 else {
706 struct ip6_flowlabel *fl = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700708 "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000709 (unsigned int)ntohl(fl->label),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 fl->share,
Eric Dumazet95c96172012-04-15 05:58:06 +0000711 (int)fl->owner,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 atomic_read(&fl->users),
713 fl->linger/HZ,
714 (long)(fl->expires - jiffies)/HZ,
Harvey Harrisonb0711952008-10-28 16:05:40 -0700715 &fl->dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 fl->opt ? fl->opt->opt_nflen : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719}
720
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700721static const struct seq_operations ip6fl_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 .start = ip6fl_seq_start,
723 .next = ip6fl_seq_next,
724 .stop = ip6fl_seq_stop,
725 .show = ip6fl_seq_show,
726};
727
728static int ip6fl_seq_open(struct inode *inode, struct file *file)
729{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700730 return seq_open_net(inode, file, &ip6fl_seq_ops,
731 sizeof(struct ip6fl_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Arjan van de Ven9a321442007-02-12 00:55:35 -0800734static const struct file_operations ip6fl_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 .owner = THIS_MODULE,
736 .open = ip6fl_seq_open,
737 .read = seq_read,
738 .llseek = seq_lseek,
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700739 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000742static int __net_init ip6_flowlabel_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700744 if (!proc_net_fops_create(net, "ip6_flowlabel",
745 S_IRUGO, &ip6fl_seq_fops))
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800746 return -ENOMEM;
747 return 0;
748}
749
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000750static void __net_exit ip6_flowlabel_proc_fini(struct net *net)
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800751{
752 proc_net_remove(net, "ip6_flowlabel");
753}
754#else
755static inline int ip6_flowlabel_proc_init(struct net *net)
756{
757 return 0;
758}
759static inline void ip6_flowlabel_proc_fini(struct net *net)
760{
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800761}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762#endif
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800763
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000764static void __net_exit ip6_flowlabel_net_exit(struct net *net)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700765{
766 ip6_fl_purge(net);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700767 ip6_flowlabel_proc_fini(net);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700768}
769
770static struct pernet_operations ip6_flowlabel_net_ops = {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700771 .init = ip6_flowlabel_proc_init,
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700772 .exit = ip6_flowlabel_net_exit,
773};
774
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800775int ip6_flowlabel_init(void)
776{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700777 return register_pernet_subsys(&ip6_flowlabel_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780void ip6_flowlabel_cleanup(void)
781{
782 del_timer(&ip6_fl_gc_timer);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700783 unregister_pernet_subsys(&ip6_flowlabel_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}