blob: 7712578bdc66c7f177b32fe8baf0bfac8b9d7d85 [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>
23
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020024#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <net/sock.h>
26
27#include <net/ipv6.h>
28#include <net/ndisc.h>
29#include <net/protocol.h>
30#include <net/ip6_route.h>
31#include <net/addrconf.h>
32#include <net/rawv6.h>
33#include <net/icmp.h>
34#include <net/transp_v6.h>
35
36#include <asm/uaccess.h>
37
38#define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
39 in old IPv6 RFC. Well, it was reasonable value.
40 */
41#define FL_MAX_LINGER 60 /* Maximal linger timeout */
42
43/* FL hash table */
44
45#define FL_MAX_PER_SOCK 32
46#define FL_MAX_SIZE 4096
47#define FL_HASH_MASK 255
48#define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
49
50static atomic_t fl_size = ATOMIC_INIT(0);
51static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1];
52
53static void ip6_fl_gc(unsigned long dummy);
Ingo Molnar8d06afa2005-09-09 13:10:40 -070054static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* FL hash table lock: it protects only of GC */
57
58static DEFINE_RWLOCK(ip6_fl_lock);
59
60/* Big socket sock */
61
62static DEFINE_RWLOCK(ip6_sk_fl_lock);
63
64
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070065static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 struct ip6_flowlabel *fl;
68
69 for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070070 if (fl->label == label && fl->fl_net == net)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return fl;
72 }
73 return NULL;
74}
75
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070076static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 struct ip6_flowlabel *fl;
79
80 read_lock_bh(&ip6_fl_lock);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070081 fl = __fl_lookup(net, label);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (fl)
83 atomic_inc(&fl->users);
84 read_unlock_bh(&ip6_fl_lock);
85 return fl;
86}
87
88
89static void fl_free(struct ip6_flowlabel *fl)
90{
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070091 if (fl) {
92 release_net(fl->fl_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 kfree(fl->opt);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -070094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 kfree(fl);
96}
97
98static void fl_release(struct ip6_flowlabel *fl)
99{
100 write_lock_bh(&ip6_fl_lock);
101
102 fl->lastuse = jiffies;
103 if (atomic_dec_and_test(&fl->users)) {
104 unsigned long ttd = fl->lastuse + fl->linger;
105 if (time_after(ttd, fl->expires))
106 fl->expires = ttd;
107 ttd = fl->expires;
108 if (fl->opt && fl->share == IPV6_FL_S_EXCL) {
109 struct ipv6_txoptions *opt = fl->opt;
110 fl->opt = NULL;
111 kfree(opt);
112 }
113 if (!timer_pending(&ip6_fl_gc_timer) ||
114 time_after(ip6_fl_gc_timer.expires, ttd))
115 mod_timer(&ip6_fl_gc_timer, ttd);
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 write_unlock_bh(&ip6_fl_lock);
118}
119
120static void ip6_fl_gc(unsigned long dummy)
121{
122 int i;
123 unsigned long now = jiffies;
124 unsigned long sched = 0;
125
126 write_lock(&ip6_fl_lock);
127
128 for (i=0; i<=FL_HASH_MASK; i++) {
129 struct ip6_flowlabel *fl, **flp;
130 flp = &fl_ht[i];
131 while ((fl=*flp) != NULL) {
132 if (atomic_read(&fl->users) == 0) {
133 unsigned long ttd = fl->lastuse + fl->linger;
134 if (time_after(ttd, fl->expires))
135 fl->expires = ttd;
136 ttd = fl->expires;
137 if (time_after_eq(now, ttd)) {
138 *flp = fl->next;
139 fl_free(fl);
140 atomic_dec(&fl_size);
141 continue;
142 }
143 if (!sched || time_before(ttd, sched))
144 sched = ttd;
145 }
146 flp = &fl->next;
147 }
148 }
149 if (!sched && atomic_read(&fl_size))
150 sched = now + FL_MAX_LINGER;
151 if (sched) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700152 mod_timer(&ip6_fl_gc_timer, sched);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154 write_unlock(&ip6_fl_lock);
155}
156
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700157static void ip6_fl_purge(struct net *net)
158{
159 int i;
160
161 write_lock(&ip6_fl_lock);
162 for (i = 0; i <= FL_HASH_MASK; i++) {
163 struct ip6_flowlabel *fl, **flp;
164 flp = &fl_ht[i];
165 while ((fl = *flp) != NULL) {
166 if (fl->fl_net == net && atomic_read(&fl->users) == 0) {
167 *flp = fl->next;
168 fl_free(fl);
169 atomic_dec(&fl_size);
170 continue;
171 }
172 flp = &fl->next;
173 }
174 }
175 write_unlock(&ip6_fl_lock);
176}
177
178static struct ip6_flowlabel *fl_intern(struct net *net,
179 struct ip6_flowlabel *fl, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700181 struct ip6_flowlabel *lfl;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 fl->label = label & IPV6_FLOWLABEL_MASK;
184
185 write_lock_bh(&ip6_fl_lock);
186 if (label == 0) {
187 for (;;) {
188 fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK;
189 if (fl->label) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700190 lfl = __fl_lookup(net, fl->label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (lfl == NULL)
192 break;
193 }
194 }
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700195 } else {
196 /*
197 * we dropper the ip6_fl_lock, so this entry could reappear
198 * and we need to recheck with it.
199 *
200 * OTOH no need to search the active socket first, like it is
201 * done in ipv6_flowlabel_opt - sock is locked, so new entry
202 * with the same label can only appear on another sock
203 */
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700204 lfl = __fl_lookup(net, fl->label);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700205 if (lfl != NULL) {
206 atomic_inc(&lfl->users);
207 write_unlock_bh(&ip6_fl_lock);
208 return lfl;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 fl->lastuse = jiffies;
213 fl->next = fl_ht[FL_HASH(fl->label)];
214 fl_ht[FL_HASH(fl->label)] = fl;
215 atomic_inc(&fl_size);
216 write_unlock_bh(&ip6_fl_lock);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700217 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220
221
222/* Socket flowlabel lists */
223
Al Viro90bcaf72006-11-08 00:25:17 -0800224struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, __be32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct ipv6_fl_socklist *sfl;
227 struct ipv6_pinfo *np = inet6_sk(sk);
228
229 label &= IPV6_FLOWLABEL_MASK;
230
Pavel Emelyanovbd0bf572007-10-18 05:15:57 -0700231 read_lock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) {
233 struct ip6_flowlabel *fl = sfl->fl;
234 if (fl->label == label) {
235 fl->lastuse = jiffies;
236 atomic_inc(&fl->users);
Pavel Emelyanov52f095e2007-10-18 05:38:48 -0700237 read_unlock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return fl;
239 }
240 }
Pavel Emelyanovbd0bf572007-10-18 05:15:57 -0700241 read_unlock_bh(&ip6_sk_fl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return NULL;
243}
244
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800245EXPORT_SYMBOL_GPL(fl6_sock_lookup);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247void fl6_free_socklist(struct sock *sk)
248{
249 struct ipv6_pinfo *np = inet6_sk(sk);
250 struct ipv6_fl_socklist *sfl;
251
252 while ((sfl = np->ipv6_fl_list) != NULL) {
253 np->ipv6_fl_list = sfl->next;
254 fl_release(sfl->fl);
255 kfree(sfl);
256 }
257}
258
259/* Service routines */
260
261
262/*
263 It is the only difficult place. flowlabel enforces equal headers
264 before and including routing header, however user may supply options
265 following rthdr.
266 */
267
268struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space,
269 struct ip6_flowlabel * fl,
270 struct ipv6_txoptions * fopt)
271{
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900272 struct ipv6_txoptions * fl_opt = fl->opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900273
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900274 if (fopt == NULL || fopt->opt_flen == 0)
275 return fl_opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (fl_opt != NULL) {
278 opt_space->hopopt = fl_opt->hopopt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900279 opt_space->dst0opt = fl_opt->dst0opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 opt_space->srcrt = fl_opt->srcrt;
281 opt_space->opt_nflen = fl_opt->opt_nflen;
282 } else {
283 if (fopt->opt_nflen == 0)
284 return fopt;
285 opt_space->hopopt = NULL;
286 opt_space->dst0opt = NULL;
287 opt_space->srcrt = NULL;
288 opt_space->opt_nflen = 0;
289 }
290 opt_space->dst1opt = fopt->dst1opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 opt_space->opt_flen = fopt->opt_flen;
292 return opt_space;
293}
294
295static unsigned long check_linger(unsigned long ttl)
296{
297 if (ttl < FL_MIN_LINGER)
298 return FL_MIN_LINGER*HZ;
299 if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN))
300 return 0;
301 return ttl*HZ;
302}
303
304static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires)
305{
306 linger = check_linger(linger);
307 if (!linger)
308 return -EPERM;
309 expires = check_linger(expires);
310 if (!expires)
311 return -EPERM;
312 fl->lastuse = jiffies;
313 if (time_before(fl->linger, linger))
314 fl->linger = linger;
315 if (time_before(expires, fl->linger))
316 expires = fl->linger;
317 if (time_before(fl->expires, fl->lastuse + expires))
318 fl->expires = fl->lastuse + expires;
319 return 0;
320}
321
322static struct ip6_flowlabel *
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700323fl_create(struct net *net, struct in6_flowlabel_req *freq, char __user *optval,
324 int optlen, int *err_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
David S. Miller684de402009-02-06 00:49:55 -0800326 struct ip6_flowlabel *fl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int olen;
328 int addr_type;
329 int err;
330
David S. Miller684de402009-02-06 00:49:55 -0800331 olen = optlen - CMSG_ALIGN(sizeof(*freq));
332 err = -EINVAL;
333 if (olen > 64 * 1024)
334 goto done;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 err = -ENOMEM;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800337 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (fl == NULL)
339 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (olen > 0) {
342 struct msghdr msg;
343 struct flowi flowi;
344 int junk;
345
346 err = -ENOMEM;
347 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
348 if (fl->opt == NULL)
349 goto done;
350
351 memset(fl->opt, 0, sizeof(*fl->opt));
352 fl->opt->tot_len = sizeof(*fl->opt) + olen;
353 err = -EFAULT;
354 if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen))
355 goto done;
356
357 msg.msg_controllen = olen;
358 msg.msg_control = (void*)(fl->opt+1);
359 flowi.oif = 0;
360
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900361 err = datagram_send_ctl(net, &msg, &flowi, fl->opt, &junk, &junk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (err)
363 goto done;
364 err = -EINVAL;
365 if (fl->opt->opt_flen)
366 goto done;
367 if (fl->opt->opt_nflen == 0) {
368 kfree(fl->opt);
369 fl->opt = NULL;
370 }
371 }
372
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700373 fl->fl_net = hold_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 fl->expires = jiffies;
375 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
376 if (err)
377 goto done;
378 fl->share = freq->flr_share;
379 addr_type = ipv6_addr_type(&freq->flr_dst);
380 if ((addr_type&IPV6_ADDR_MAPPED)
James Morrisc6817e42006-10-30 18:56:06 -0800381 || addr_type == IPV6_ADDR_ANY) {
382 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto done;
James Morrisc6817e42006-10-30 18:56:06 -0800384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ipv6_addr_copy(&fl->dst, &freq->flr_dst);
386 atomic_set(&fl->users, 1);
387 switch (fl->share) {
388 case IPV6_FL_S_EXCL:
389 case IPV6_FL_S_ANY:
390 break;
391 case IPV6_FL_S_PROCESS:
392 fl->owner = current->pid;
393 break;
394 case IPV6_FL_S_USER:
David Howellsf82b35902008-11-14 10:39:07 +1100395 fl->owner = current_euid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
397 default:
398 err = -EINVAL;
399 goto done;
400 }
401 return fl;
402
403done:
404 fl_free(fl);
405 *err_p = err;
406 return NULL;
407}
408
409static int mem_check(struct sock *sk)
410{
411 struct ipv6_pinfo *np = inet6_sk(sk);
412 struct ipv6_fl_socklist *sfl;
413 int room = FL_MAX_SIZE - atomic_read(&fl_size);
414 int count = 0;
415
416 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
417 return 0;
418
419 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next)
420 count++;
421
422 if (room <= 0 ||
423 ((count >= FL_MAX_PER_SOCK ||
424 (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4)
425 && !capable(CAP_NET_ADMIN)))
426 return -ENOBUFS;
427
428 return 0;
429}
430
431static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
432{
433 if (h1 == h2)
434 return 0;
435 if (h1 == NULL || h2 == NULL)
436 return 1;
437 if (h1->hdrlen != h2->hdrlen)
438 return 1;
439 return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
440}
441
442static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
443{
444 if (o1 == o2)
445 return 0;
446 if (o1 == NULL || o2 == NULL)
447 return 1;
448 if (o1->opt_nflen != o2->opt_nflen)
449 return 1;
450 if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
451 return 1;
452 if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
453 return 1;
454 if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
455 return 1;
456 return 0;
457}
458
Pavel Emelyanov04028042007-10-18 05:14:58 -0700459static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
460 struct ip6_flowlabel *fl)
461{
462 write_lock_bh(&ip6_sk_fl_lock);
463 sfl->fl = fl;
464 sfl->next = np->ipv6_fl_list;
465 np->ipv6_fl_list = sfl;
466 write_unlock_bh(&ip6_sk_fl_lock);
467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
470{
Ingo Molnar55205d42008-11-25 16:50:30 -0800471 int uninitialized_var(err);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700472 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct ipv6_pinfo *np = inet6_sk(sk);
474 struct in6_flowlabel_req freq;
475 struct ipv6_fl_socklist *sfl1=NULL;
476 struct ipv6_fl_socklist *sfl, **sflp;
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700477 struct ip6_flowlabel *fl, *fl1 = NULL;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (optlen < sizeof(freq))
481 return -EINVAL;
482
483 if (copy_from_user(&freq, optval, sizeof(freq)))
484 return -EFAULT;
485
486 switch (freq.flr_action) {
487 case IPV6_FL_A_PUT:
488 write_lock_bh(&ip6_sk_fl_lock);
489 for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) {
490 if (sfl->fl->label == freq.flr_label) {
491 if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK))
492 np->flow_label &= ~IPV6_FLOWLABEL_MASK;
493 *sflp = sfl->next;
494 write_unlock_bh(&ip6_sk_fl_lock);
495 fl_release(sfl->fl);
496 kfree(sfl);
497 return 0;
498 }
499 }
500 write_unlock_bh(&ip6_sk_fl_lock);
501 return -ESRCH;
502
503 case IPV6_FL_A_RENEW:
504 read_lock_bh(&ip6_sk_fl_lock);
505 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
506 if (sfl->fl->label == freq.flr_label) {
507 err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires);
508 read_unlock_bh(&ip6_sk_fl_lock);
509 return err;
510 }
511 }
512 read_unlock_bh(&ip6_sk_fl_lock);
513
514 if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) {
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700515 fl = fl_lookup(net, freq.flr_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (fl) {
517 err = fl6_renew(fl, freq.flr_linger, freq.flr_expires);
518 fl_release(fl);
519 return err;
520 }
521 }
522 return -ESRCH;
523
524 case IPV6_FL_A_GET:
525 if (freq.flr_label & ~IPV6_FLOWLABEL_MASK)
526 return -EINVAL;
527
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700528 fl = fl_create(net, &freq, optval, optlen, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (fl == NULL)
530 return err;
531 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
532
533 if (freq.flr_label) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 err = -EEXIST;
535 read_lock_bh(&ip6_sk_fl_lock);
536 for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) {
537 if (sfl->fl->label == freq.flr_label) {
538 if (freq.flr_flags&IPV6_FL_F_EXCL) {
539 read_unlock_bh(&ip6_sk_fl_lock);
540 goto done;
541 }
542 fl1 = sfl->fl;
Yan Zheng4ea6a802005-10-24 19:55:23 +0800543 atomic_inc(&fl1->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 }
546 }
547 read_unlock_bh(&ip6_sk_fl_lock);
548
549 if (fl1 == NULL)
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700550 fl1 = fl_lookup(net, freq.flr_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (fl1) {
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700552recheck:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 err = -EEXIST;
554 if (freq.flr_flags&IPV6_FL_F_EXCL)
555 goto release;
556 err = -EPERM;
557 if (fl1->share == IPV6_FL_S_EXCL ||
558 fl1->share != fl->share ||
559 fl1->owner != fl->owner)
560 goto release;
561
562 err = -EINVAL;
563 if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
564 ipv6_opt_cmp(fl1->opt, fl->opt))
565 goto release;
566
567 err = -ENOMEM;
568 if (sfl1 == NULL)
569 goto release;
570 if (fl->linger > fl1->linger)
571 fl1->linger = fl->linger;
572 if ((long)(fl->expires - fl1->expires) > 0)
573 fl1->expires = fl->expires;
Pavel Emelyanov04028042007-10-18 05:14:58 -0700574 fl_link(np, sfl1, fl1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 fl_free(fl);
576 return 0;
577
578release:
579 fl_release(fl1);
580 goto done;
581 }
582 }
583 err = -ENOENT;
584 if (!(freq.flr_flags&IPV6_FL_F_CREATE))
585 goto done;
586
587 err = -ENOMEM;
588 if (sfl1 == NULL || (err = mem_check(sk)) != 0)
589 goto done;
590
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700591 fl1 = fl_intern(net, fl, freq.flr_label);
Pavel Emelyanov78c2e502007-10-18 05:18:56 -0700592 if (fl1 != NULL)
593 goto recheck;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
David S. Miller6c94d362005-05-29 20:28:01 -0700595 if (!freq.flr_label) {
596 if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label,
597 &fl->label, sizeof(fl->label))) {
598 /* Intentionally ignore fault. */
599 }
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Pavel Emelyanov04028042007-10-18 05:14:58 -0700602 fl_link(np, sfl1, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604
605 default:
606 return -EINVAL;
607 }
608
609done:
610 fl_free(fl);
611 kfree(sfl1);
612 return err;
613}
614
615#ifdef CONFIG_PROC_FS
616
617struct ip6fl_iter_state {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700618 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int bucket;
620};
621
622#define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private)
623
624static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq)
625{
626 struct ip6_flowlabel *fl = NULL;
627 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700628 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700631 fl = fl_ht[state->bucket];
632
633 while (fl && fl->fl_net != net)
634 fl = fl->next;
635 if (fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 return fl;
639}
640
641static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl)
642{
643 struct ip6fl_iter_state *state = ip6fl_seq_private(seq);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700644 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 fl = fl->next;
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700647try_again:
648 while (fl && fl->fl_net != net)
649 fl = fl->next;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 while (!fl) {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700652 if (++state->bucket <= FL_HASH_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 fl = fl_ht[state->bucket];
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700654 goto try_again;
655 } else
James Morrisbcd62072006-10-30 15:08:42 -0800656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 return fl;
659}
660
661static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos)
662{
663 struct ip6_flowlabel *fl = ip6fl_get_first(seq);
664 if (fl)
665 while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL)
666 --pos;
667 return pos ? NULL : fl;
668}
669
670static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800671 __acquires(ip6_fl_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 read_lock_bh(&ip6_fl_lock);
674 return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
675}
676
677static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos)
678{
679 struct ip6_flowlabel *fl;
680
681 if (v == SEQ_START_TOKEN)
682 fl = ip6fl_get_first(seq);
683 else
684 fl = ip6fl_get_next(seq, v);
685 ++*pos;
686 return fl;
687}
688
689static void ip6fl_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800690 __releases(ip6_fl_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 read_unlock_bh(&ip6_fl_lock);
693}
694
James Morris1b7c2db2006-10-31 00:43:44 -0800695static int ip6fl_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
James Morris1b7c2db2006-10-31 00:43:44 -0800697 if (v == SEQ_START_TOKEN)
698 seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n",
699 "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt");
700 else {
701 struct ip6_flowlabel *fl = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700703 "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 (unsigned)ntohl(fl->label),
705 fl->share,
706 (unsigned)fl->owner,
707 atomic_read(&fl->users),
708 fl->linger/HZ,
709 (long)(fl->expires - jiffies)/HZ,
Harvey Harrisonb0711952008-10-28 16:05:40 -0700710 &fl->dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 fl->opt ? fl->opt->opt_nflen : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714}
715
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700716static const struct seq_operations ip6fl_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 .start = ip6fl_seq_start,
718 .next = ip6fl_seq_next,
719 .stop = ip6fl_seq_stop,
720 .show = ip6fl_seq_show,
721};
722
723static int ip6fl_seq_open(struct inode *inode, struct file *file)
724{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700725 return seq_open_net(inode, file, &ip6fl_seq_ops,
726 sizeof(struct ip6fl_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Arjan van de Ven9a321442007-02-12 00:55:35 -0800729static const struct file_operations ip6fl_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 .owner = THIS_MODULE,
731 .open = ip6fl_seq_open,
732 .read = seq_read,
733 .llseek = seq_lseek,
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700734 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800737static int ip6_flowlabel_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700739 if (!proc_net_fops_create(net, "ip6_flowlabel",
740 S_IRUGO, &ip6fl_seq_fops))
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800741 return -ENOMEM;
742 return 0;
743}
744
745static void ip6_flowlabel_proc_fini(struct net *net)
746{
747 proc_net_remove(net, "ip6_flowlabel");
748}
749#else
750static inline int ip6_flowlabel_proc_init(struct net *net)
751{
752 return 0;
753}
754static inline void ip6_flowlabel_proc_fini(struct net *net)
755{
756 return ;
757}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#endif
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800759
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700760static inline void ip6_flowlabel_net_exit(struct net *net)
761{
762 ip6_fl_purge(net);
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700763 ip6_flowlabel_proc_fini(net);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700764}
765
766static struct pernet_operations ip6_flowlabel_net_ops = {
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700767 .init = ip6_flowlabel_proc_init,
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700768 .exit = ip6_flowlabel_net_exit,
769};
770
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800771int ip6_flowlabel_init(void)
772{
Benjamin Thery5983a3d2008-03-26 16:53:30 -0700773 return register_pernet_subsys(&ip6_flowlabel_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776void ip6_flowlabel_cleanup(void)
777{
778 del_timer(&ip6_fl_gc_timer);
Benjamin Thery60e8fbc2008-03-26 16:53:08 -0700779 unregister_pernet_subsys(&ip6_flowlabel_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}