blob: 14e23216eb28c7332fc67afe6a855ee9a98936a4 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020025#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sock.h>
27
28#include <net/ipv6.h>
29#include <net/ndisc.h>
30#include <net/protocol.h>
31#include <net/ip6_route.h>
32#include <net/addrconf.h>
33#include <net/rawv6.h>
34#include <net/icmp.h>
35#include <net/transp_v6.h>
36
37#include <asm/uaccess.h>
38
39#define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
40 in old IPv6 RFC. Well, it was reasonable value.
41 */
42#define FL_MAX_LINGER 60 /* Maximal linger timeout */
43
44/* FL hash table */
45
46#define FL_MAX_PER_SOCK 32
47#define FL_MAX_SIZE 4096
48#define FL_HASH_MASK 255
49#define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
50
51static atomic_t fl_size = ATOMIC_INIT(0);
52static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1];
53
54static void ip6_fl_gc(unsigned long dummy);
Ingo Molnar8d06afa2005-09-09 13:10:40 -070055static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* FL hash table lock: it protects only of GC */
58
59static DEFINE_RWLOCK(ip6_fl_lock);
60
61/* Big socket sock */
62
63static DEFINE_RWLOCK(ip6_sk_fl_lock);
64
65
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070066static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct ip6_flowlabel *fl;
69
70 for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -080071 if (fl->label == label && net_eq(fl->fl_net, net))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return fl;
73 }
74 return NULL;
75}
76
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070077static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct ip6_flowlabel *fl;
80
81 read_lock_bh(&ip6_fl_lock);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070082 fl = __fl_lookup(net, label);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (fl)
84 atomic_inc(&fl->users);
85 read_unlock_bh(&ip6_fl_lock);
86 return fl;
87}
88
89
90static void fl_free(struct ip6_flowlabel *fl)
91{
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070092 if (fl) {
93 release_net(fl->fl_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 kfree(fl->opt);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 kfree(fl);
97}
98
99static void fl_release(struct ip6_flowlabel *fl)
100{
101 write_lock_bh(&ip6_fl_lock);
102
103 fl->lastuse = jiffies;
104 if (atomic_dec_and_test(&fl->users)) {
105 unsigned long ttd = fl->lastuse + fl->linger;
106 if (time_after(ttd, fl->expires))
107 fl->expires = ttd;
108 ttd = fl->expires;
109 if (fl->opt && fl->share == IPV6_FL_S_EXCL) {
110 struct ipv6_txoptions *opt = fl->opt;
111 fl->opt = NULL;
112 kfree(opt);
113 }
114 if (!timer_pending(&ip6_fl_gc_timer) ||
115 time_after(ip6_fl_gc_timer.expires, ttd))
116 mod_timer(&ip6_fl_gc_timer, ttd);
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 write_unlock_bh(&ip6_fl_lock);
119}
120
121static void ip6_fl_gc(unsigned long dummy)
122{
123 int i;
124 unsigned long now = jiffies;
125 unsigned long sched = 0;
126
127 write_lock(&ip6_fl_lock);
128
129 for (i=0; i<=FL_HASH_MASK; i++) {
130 struct ip6_flowlabel *fl, **flp;
131 flp = &fl_ht[i];
132 while ((fl=*flp) != NULL) {
133 if (atomic_read(&fl->users) == 0) {
134 unsigned long ttd = fl->lastuse + fl->linger;
135 if (time_after(ttd, fl->expires))
136 fl->expires = ttd;
137 ttd = fl->expires;
138 if (time_after_eq(now, ttd)) {
139 *flp = fl->next;
140 fl_free(fl);
141 atomic_dec(&fl_size);
142 continue;
143 }
144 if (!sched || time_before(ttd, sched))
145 sched = ttd;
146 }
147 flp = &fl->next;
148 }
149 }
150 if (!sched && atomic_read(&fl_size))
151 sched = now + FL_MAX_LINGER;
152 if (sched) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700153 mod_timer(&ip6_fl_gc_timer, sched);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 write_unlock(&ip6_fl_lock);
156}
157
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000158static void __net_exit ip6_fl_purge(struct net *net)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700159{
160 int i;
161
162 write_lock(&ip6_fl_lock);
163 for (i = 0; i <= FL_HASH_MASK; i++) {
164 struct ip6_flowlabel *fl, **flp;
165 flp = &fl_ht[i];
166 while ((fl = *flp) != NULL) {
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800167 if (net_eq(fl->fl_net, net) &&
168 atomic_read(&fl->users) == 0) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700169 *flp = fl->next;
170 fl_free(fl);
171 atomic_dec(&fl_size);
172 continue;
173 }
174 flp = &fl->next;
175 }
176 }
177 write_unlock(&ip6_fl_lock);
178}
179
180static struct ip6_flowlabel *fl_intern(struct net *net,
181 struct ip6_flowlabel *fl, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700183 struct ip6_flowlabel *lfl;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 fl->label = label & IPV6_FLOWLABEL_MASK;
186
187 write_lock_bh(&ip6_fl_lock);
188 if (label == 0) {
189 for (;;) {
190 fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK;
191 if (fl->label) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700192 lfl = __fl_lookup(net, fl->label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (lfl == NULL)
194 break;
195 }
196 }
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700197 } else {
198 /*
199 * we dropper the ip6_fl_lock, so this entry could reappear
200 * and we need to recheck with it.
201 *
202 * OTOH no need to search the active socket first, like it is
203 * done in ipv6_flowlabel_opt - sock is locked, so new entry
204 * with the same label can only appear on another sock
205 */
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700206 lfl = __fl_lookup(net, fl->label);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700207 if (lfl != NULL) {
208 atomic_inc(&lfl->users);
209 write_unlock_bh(&ip6_fl_lock);
210 return lfl;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 fl->lastuse = jiffies;
215 fl->next = fl_ht[FL_HASH(fl->label)];
216 fl_ht[FL_HASH(fl->label)] = fl;
217 atomic_inc(&fl_size);
218 write_unlock_bh(&ip6_fl_lock);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700219 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222
223
224/* Socket flowlabel lists */
225
Al Viro90bcaf72006-11-08 00:25:17 -0800226struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct ipv6_fl_socklist *sfl;
229 struct ipv6_pinfo *np = inet6_sk(sk);
230
231 label &= IPV6_FLOWLABEL_MASK;
232
Pavel Emelyanovbd0bf572007-10-18 05:15:57 -0700233 read_lock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) {
235 struct ip6_flowlabel *fl = sfl->fl;
236 if (fl->label == label) {
237 fl->lastuse = jiffies;
238 atomic_inc(&fl->users);
Pavel Emelyanov52f095e2007-10-18 05:38:48 -0700239 read_unlock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return fl;
241 }
242 }
Pavel Emelyanovbd0bf572007-10-18 05:15:57 -0700243 read_unlock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return NULL;
245}
246
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800247EXPORT_SYMBOL_GPL(fl6_sock_lookup);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249void fl6_free_socklist(struct sock *sk)
250{
251 struct ipv6_pinfo *np = inet6_sk(sk);
252 struct ipv6_fl_socklist *sfl;
253
254 while ((sfl = np->ipv6_fl_list) != NULL) {
255 np->ipv6_fl_list = sfl->next;
256 fl_release(sfl->fl);
257 kfree(sfl);
258 }
259}
260
261/* Service routines */
262
263
264/*
265 It is the only difficult place. flowlabel enforces equal headers
266 before and including routing header, however user may supply options
267 following rthdr.
268 */
269
270struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
271 struct ip6_flowlabel * fl,
272 struct ipv6_txoptions * fopt)
273{
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900274 struct ipv6_txoptions * fl_opt = fl->opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900275
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900276 if (fopt == NULL || fopt->opt_flen == 0)
277 return fl_opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (fl_opt != NULL) {
280 opt_space->hopopt = fl_opt->hopopt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900281 opt_space->dst0opt = fl_opt->dst0opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 opt_space->srcrt = fl_opt->srcrt;
283 opt_space->opt_nflen = fl_opt->opt_nflen;
284 } else {
285 if (fopt->opt_nflen == 0)
286 return fopt;
287 opt_space->hopopt = NULL;
288 opt_space->dst0opt = NULL;
289 opt_space->srcrt = NULL;
290 opt_space->opt_nflen = 0;
291 }
292 opt_space->dst1opt = fopt->dst1opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 opt_space->opt_flen = fopt->opt_flen;
294 return opt_space;
295}
296
297static unsigned long check_linger(unsigned long ttl)
298{
299 if (ttl < FL_MIN_LINGER)
300 return FL_MIN_LINGER*HZ;
301 if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
302 return 0;
303 return ttl*HZ;
304}
305
306static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
307{
308 linger = check_linger(linger);
309 if (!linger)
310 return -EPERM;
311 expires = check_linger(expires);
312 if (!expires)
313 return -EPERM;
314 fl->lastuse = jiffies;
315 if (time_before(fl->linger, linger))
316 fl->linger = linger;
317 if (time_before(expires, fl->linger))
318 expires = fl->linger;
319 if (time_before(fl->expires, fl->lastuse + expires))
320 fl->expires = fl->lastuse + expires;
321 return 0;
322}
323
324static struct ip6_flowlabel *
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700325fl_create(struct net *net, struct in6_flowlabel_req *freq, char __user *optval,
326 int optlen, int *err_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
David S. Miller684de402009-02-06 00:49:55 -0800328 struct ip6_flowlabel *fl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int olen;
330 int addr_type;
331 int err;
332
David S. Miller684de402009-02-06 00:49:55 -0800333 olen = optlen - CMSG_ALIGN(sizeof(*freq));
334 err = -EINVAL;
335 if (olen > 64 * 1024)
336 goto done;
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 err = -ENOMEM;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800339 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (fl == NULL)
341 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (olen > 0) {
344 struct msghdr msg;
345 struct flowi flowi;
346 int junk;
347
348 err = -ENOMEM;
349 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
350 if (fl->opt == NULL)
351 goto done;
352
353 memset(fl->opt, 0, sizeof(*fl->opt));
354 fl->opt->tot_len = sizeof(*fl->opt) + olen;
355 err = -EFAULT;
356 if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
357 goto done;
358
359 msg.msg_controllen = olen;
360 msg.msg_control = (void*)(fl->opt+1);
361 flowi.oif = 0;
362
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900363 err = datagram_send_ctl(net, &msg, &flowi, fl->opt, &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (err)
365 goto done;
366 err = -EINVAL;
367 if (fl->opt->opt_flen)
368 goto done;
369 if (fl->opt->opt_nflen == 0) {
370 kfree(fl->opt);
371 fl->opt = NULL;
372 }
373 }
374
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700375 fl->fl_net = hold_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 fl->expires = jiffies;
377 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
378 if (err)
379 goto done;
380 fl->share = freq->flr_share;
381 addr_type = ipv6_addr_type(&freq->flr_dst);
Joe Perches35700212009-11-24 14:52:52 -0800382 if ((addr_type & IPV6_ADDR_MAPPED) ||
383 addr_type == IPV6_ADDR_ANY) {
James Morrisc6817e42006-10-30 18:56:06 -0800384 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto done;
James Morrisc6817e42006-10-30 18:56:06 -0800386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ipv6_addr_copy(&fl->dst, &freq->flr_dst);
388 atomic_set(&fl->users, 1);
389 switch (fl->share) {
390 case IPV6_FL_S_EXCL:
391 case IPV6_FL_S_ANY:
392 break;
393 case IPV6_FL_S_PROCESS:
394 fl->owner = current->pid;
395 break;
396 case IPV6_FL_S_USER:
David Howellsf82b35902008-11-14 10:39:07 +1100397 fl->owner = current_euid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 break;
399 default:
400 err = -EINVAL;
401 goto done;
402 }
403 return fl;
404
405done:
406 fl_free(fl);
407 *err_p = err;
408 return NULL;
409}
410
411static int mem_check(struct sock *sk)
412{
413 struct ipv6_pinfo *np = inet6_sk(sk);
414 struct ipv6_fl_socklist *sfl;
415 int room = FL_MAX_SIZE - atomic_read(&fl_size);
416 int count = 0;
417
418 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
419 return 0;
420
421 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next)
422 count++;
423
424 if (room <= 0 ||
425 ((count >= FL_MAX_PER_SOCK ||
Joe Perches35700212009-11-24 14:52:52 -0800426 (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) &&
427 !capable(CAP_NET_ADMIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -ENOBUFS;
429
430 return 0;
431}
432
433static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
434{
435 if (h1 == h2)
436 return 0;
437 if (h1 == NULL || h2 == NULL)
438 return 1;
439 if (h1->hdrlen != h2->hdrlen)
440 return 1;
441 return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
442}
443
444static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
445{
446 if (o1 == o2)
447 return 0;
448 if (o1 == NULL || o2 == NULL)
449 return 1;
450 if (o1->opt_nflen != o2->opt_nflen)
451 return 1;
452 if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
453 return 1;
454 if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
455 return 1;
456 if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
457 return 1;
458 return 0;
459}
460
Pavel Emelyanov04028042007-10-18 05:14:58 -0700461static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
462 struct ip6_flowlabel *fl)
463{
464 write_lock_bh(&ip6_sk_fl_lock);
465 sfl->fl = fl;
466 sfl->next = np->ipv6_fl_list;
467 np->ipv6_fl_list = sfl;
468 write_unlock_bh(&ip6_sk_fl_lock);
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
472{
Ingo Molnar55205d42008-11-25 16:50:30 -0800473 int uninitialized_var(err);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700474 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct ipv6_pinfo *np = inet6_sk(sk);
476 struct in6_flowlabel_req freq;
477 struct ipv6_fl_socklist *sfl1=NULL;
478 struct ipv6_fl_socklist *sfl, **sflp;
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700479 struct ip6_flowlabel *fl, *fl1 = NULL;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (optlen < sizeof(freq))
483 return -EINVAL;
484
485 if (copy_from_user(&freq, optval, sizeof(freq)))
486 return -EFAULT;
487
488 switch (freq.flr_action) {
489 case IPV6_FL_A_PUT:
490 write_lock_bh(&ip6_sk_fl_lock);
491 for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
492 if (sfl->fl->label == freq.flr_label) {
493 if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
494 np->flow_label &= ~IPV6_FLOWLABEL_MASK;
495 *sflp = sfl->next;
496 write_unlock_bh(&ip6_sk_fl_lock);
497 fl_release(sfl->fl);
498 kfree(sfl);
499 return 0;
500 }
501 }
502 write_unlock_bh(&ip6_sk_fl_lock);
503 return -ESRCH;
504
505 case IPV6_FL_A_RENEW:
506 read_lock_bh(&ip6_sk_fl_lock);
507 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
508 if (sfl->fl->label == freq.flr_label) {
509 err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
510 read_unlock_bh(&ip6_sk_fl_lock);
511 return err;
512 }
513 }
514 read_unlock_bh(&ip6_sk_fl_lock);
515
516 if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700517 fl = fl_lookup(net, freq.flr_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (fl) {
519 err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
520 fl_release(fl);
521 return err;
522 }
523 }
524 return -ESRCH;
525
526 case IPV6_FL_A_GET:
527 if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
528 return -EINVAL;
529
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700530 fl = fl_create(net, &freq, optval, optlen, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (fl == NULL)
532 return err;
533 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
534
535 if (freq.flr_label) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 err = -EEXIST;
537 read_lock_bh(&ip6_sk_fl_lock);
538 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
539 if (sfl->fl->label == freq.flr_label) {
540 if (freq.flr_flags&IPV6_FL_F_EXCL) {
541 read_unlock_bh(&ip6_sk_fl_lock);
542 goto done;
543 }
544 fl1 = sfl->fl;
Yan Zheng4ea6a802005-10-24 19:55:23 +0800545 atomic_inc(&fl1->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
547 }
548 }
549 read_unlock_bh(&ip6_sk_fl_lock);
550
551 if (fl1 == NULL)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700552 fl1 = fl_lookup(net, freq.flr_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (fl1) {
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700554recheck:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 err = -EEXIST;
556 if (freq.flr_flags&IPV6_FL_F_EXCL)
557 goto release;
558 err = -EPERM;
559 if (fl1->share == IPV6_FL_S_EXCL ||
560 fl1->share != fl->share ||
561 fl1->owner != fl->owner)
562 goto release;
563
564 err = -EINVAL;
565 if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
566 ipv6_opt_cmp(fl1->opt, fl->opt))
567 goto release;
568
569 err = -ENOMEM;
570 if (sfl1 == NULL)
571 goto release;
572 if (fl->linger > fl1->linger)
573 fl1->linger = fl->linger;
574 if ((long)(fl->expires - fl1->expires) > 0)
575 fl1->expires = fl->expires;
Pavel Emelyanov04028042007-10-18 05:14:58 -0700576 fl_link(np, sfl1, fl1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 fl_free(fl);
578 return 0;
579
580release:
581 fl_release(fl1);
582 goto done;
583 }
584 }
585 err = -ENOENT;
586 if (!(freq.flr_flags&IPV6_FL_F_CREATE))
587 goto done;
588
589 err = -ENOMEM;
590 if (sfl1 == NULL || (err = mem_check(sk)) != 0)
591 goto done;
592
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700593 fl1 = fl_intern(net, fl, freq.flr_label);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700594 if (fl1 != NULL)
595 goto recheck;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
David S. Miller6c94d362005-05-29 20:28:01 -0700597 if (!freq.flr_label) {
598 if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
599 &fl->label, sizeof(fl->label))) {
600 /* Intentionally ignore fault. */
601 }
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Pavel Emelyanov04028042007-10-18 05:14:58 -0700604 fl_link(np, sfl1, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606
607 default:
608 return -EINVAL;
609 }
610
611done:
612 fl_free(fl);
613 kfree(sfl1);
614 return err;
615}
616
617#ifdef CONFIG_PROC_FS
618
619struct ip6fl_iter_state {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700620 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int bucket;
622};
623
624#define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
625
626static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
627{
628 struct ip6_flowlabel *fl = NULL;
629 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700630 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700633 fl = fl_ht[state->bucket];
634
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800635 while (fl && !net_eq(fl->fl_net, net))
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700636 fl = fl->next;
637 if (fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 return fl;
641}
642
643static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
644{
645 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700646 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 fl = fl->next;
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700649try_again:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800650 while (fl && !net_eq(fl->fl_net, net))
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700651 fl = fl->next;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 while (!fl) {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700654 if (++state->bucket <= FL_HASH_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 fl = fl_ht[state->bucket];
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700656 goto try_again;
657 } else
James Morrisbcd62072006-10-30 15:08:42 -0800658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 return fl;
661}
662
663static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
664{
665 struct ip6_flowlabel *fl = ip6fl_get_first(seq);
666 if (fl)
667 while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
668 --pos;
669 return pos ? NULL : fl;
670}
671
672static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800673 __acquires(ip6_fl_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 read_lock_bh(&ip6_fl_lock);
676 return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
677}
678
679static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
680{
681 struct ip6_flowlabel *fl;
682
683 if (v == SEQ_START_TOKEN)
684 fl = ip6fl_get_first(seq);
685 else
686 fl = ip6fl_get_next(seq, v);
687 ++*pos;
688 return fl;
689}
690
691static void ip6fl_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800692 __releases(ip6_fl_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 read_unlock_bh(&ip6_fl_lock);
695}
696
James Morris1b7c2db2006-10-31 00:43:44 -0800697static int ip6fl_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
James Morris1b7c2db2006-10-31 00:43:44 -0800699 if (v == SEQ_START_TOKEN)
700 seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
701 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
702 else {
703 struct ip6_flowlabel *fl = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700705 "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 (unsigned)ntohl(fl->label),
707 fl->share,
708 (unsigned)fl->owner,
709 atomic_read(&fl->users),
710 fl->linger/HZ,
711 (long)(fl->expires - jiffies)/HZ,
Harvey Harrisonb0711952008-10-28 16:05:40 -0700712 &fl->dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 fl->opt ? fl->opt->opt_nflen : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return 0;
716}
717
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700718static const struct seq_operations ip6fl_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 .start = ip6fl_seq_start,
720 .next = ip6fl_seq_next,
721 .stop = ip6fl_seq_stop,
722 .show = ip6fl_seq_show,
723};
724
725static int ip6fl_seq_open(struct inode *inode, struct file *file)
726{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700727 return seq_open_net(inode, file, &ip6fl_seq_ops,
728 sizeof(struct ip6fl_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Arjan van de Ven9a321442007-02-12 00:55:35 -0800731static const struct file_operations ip6fl_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 .owner = THIS_MODULE,
733 .open = ip6fl_seq_open,
734 .read = seq_read,
735 .llseek = seq_lseek,
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700736 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000739static int __net_init ip6_flowlabel_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700741 if (!proc_net_fops_create(net, "ip6_flowlabel",
742 S_IRUGO, &ip6fl_seq_fops))
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800743 return -ENOMEM;
744 return 0;
745}
746
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000747static void __net_exit ip6_flowlabel_proc_fini(struct net *net)
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800748{
749 proc_net_remove(net, "ip6_flowlabel");
750}
751#else
752static inline int ip6_flowlabel_proc_init(struct net *net)
753{
754 return 0;
755}
756static inline void ip6_flowlabel_proc_fini(struct net *net)
757{
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759#endif
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800760
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000761static void __net_exit ip6_flowlabel_net_exit(struct net *net)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700762{
763 ip6_fl_purge(net);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700764 ip6_flowlabel_proc_fini(net);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700765}
766
767static struct pernet_operations ip6_flowlabel_net_ops = {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700768 .init = ip6_flowlabel_proc_init,
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700769 .exit = ip6_flowlabel_net_exit,
770};
771
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800772int ip6_flowlabel_init(void)
773{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700774 return register_pernet_subsys(&ip6_flowlabel_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777void ip6_flowlabel_cleanup(void)
778{
779 del_timer(&ip6_fl_gc_timer);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700780 unregister_pernet_subsys(&ip6_flowlabel_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}