Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
12 | #include <linux/config.h> | |
13 | #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 | ||
24 | #include <net/sock.h> | |
25 | ||
26 | #include <net/ipv6.h> | |
27 | #include <net/ndisc.h> | |
28 | #include <net/protocol.h> | |
29 | #include <net/ip6_route.h> | |
30 | #include <net/addrconf.h> | |
31 | #include <net/rawv6.h> | |
32 | #include <net/icmp.h> | |
33 | #include <net/transp_v6.h> | |
34 | ||
35 | #include <asm/uaccess.h> | |
36 | ||
37 | #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified | |
38 | in old IPv6 RFC. Well, it was reasonable value. | |
39 | */ | |
40 | #define FL_MAX_LINGER 60 /* Maximal linger timeout */ | |
41 | ||
42 | /* FL hash table */ | |
43 | ||
44 | #define FL_MAX_PER_SOCK 32 | |
45 | #define FL_MAX_SIZE 4096 | |
46 | #define FL_HASH_MASK 255 | |
47 | #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK) | |
48 | ||
49 | static atomic_t fl_size = ATOMIC_INIT(0); | |
50 | static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1]; | |
51 | ||
52 | static void ip6_fl_gc(unsigned long dummy); | |
53 | static struct timer_list ip6_fl_gc_timer = TIMER_INITIALIZER(ip6_fl_gc, 0, 0); | |
54 | ||
55 | /* FL hash table lock: it protects only of GC */ | |
56 | ||
57 | static DEFINE_RWLOCK(ip6_fl_lock); | |
58 | ||
59 | /* Big socket sock */ | |
60 | ||
61 | static DEFINE_RWLOCK(ip6_sk_fl_lock); | |
62 | ||
63 | ||
64 | static __inline__ struct ip6_flowlabel * __fl_lookup(u32 label) | |
65 | { | |
66 | struct ip6_flowlabel *fl; | |
67 | ||
68 | for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) { | |
69 | if (fl->label == label) | |
70 | return fl; | |
71 | } | |
72 | return NULL; | |
73 | } | |
74 | ||
75 | static struct ip6_flowlabel * fl_lookup(u32 label) | |
76 | { | |
77 | struct ip6_flowlabel *fl; | |
78 | ||
79 | read_lock_bh(&ip6_fl_lock); | |
80 | fl = __fl_lookup(label); | |
81 | if (fl) | |
82 | atomic_inc(&fl->users); | |
83 | read_unlock_bh(&ip6_fl_lock); | |
84 | return fl; | |
85 | } | |
86 | ||
87 | ||
88 | static void fl_free(struct ip6_flowlabel *fl) | |
89 | { | |
90 | if (fl) | |
91 | kfree(fl->opt); | |
92 | kfree(fl); | |
93 | } | |
94 | ||
95 | static void fl_release(struct ip6_flowlabel *fl) | |
96 | { | |
97 | write_lock_bh(&ip6_fl_lock); | |
98 | ||
99 | fl->lastuse = jiffies; | |
100 | if (atomic_dec_and_test(&fl->users)) { | |
101 | unsigned long ttd = fl->lastuse + fl->linger; | |
102 | if (time_after(ttd, fl->expires)) | |
103 | fl->expires = ttd; | |
104 | ttd = fl->expires; | |
105 | if (fl->opt && fl->share == IPV6_FL_S_EXCL) { | |
106 | struct ipv6_txoptions *opt = fl->opt; | |
107 | fl->opt = NULL; | |
108 | kfree(opt); | |
109 | } | |
110 | if (!timer_pending(&ip6_fl_gc_timer) || | |
111 | time_after(ip6_fl_gc_timer.expires, ttd)) | |
112 | mod_timer(&ip6_fl_gc_timer, ttd); | |
113 | } | |
114 | ||
115 | write_unlock_bh(&ip6_fl_lock); | |
116 | } | |
117 | ||
118 | static void ip6_fl_gc(unsigned long dummy) | |
119 | { | |
120 | int i; | |
121 | unsigned long now = jiffies; | |
122 | unsigned long sched = 0; | |
123 | ||
124 | write_lock(&ip6_fl_lock); | |
125 | ||
126 | for (i=0; i<=FL_HASH_MASK; i++) { | |
127 | struct ip6_flowlabel *fl, **flp; | |
128 | flp = &fl_ht[i]; | |
129 | while ((fl=*flp) != NULL) { | |
130 | if (atomic_read(&fl->users) == 0) { | |
131 | unsigned long ttd = fl->lastuse + fl->linger; | |
132 | if (time_after(ttd, fl->expires)) | |
133 | fl->expires = ttd; | |
134 | ttd = fl->expires; | |
135 | if (time_after_eq(now, ttd)) { | |
136 | *flp = fl->next; | |
137 | fl_free(fl); | |
138 | atomic_dec(&fl_size); | |
139 | continue; | |
140 | } | |
141 | if (!sched || time_before(ttd, sched)) | |
142 | sched = ttd; | |
143 | } | |
144 | flp = &fl->next; | |
145 | } | |
146 | } | |
147 | if (!sched && atomic_read(&fl_size)) | |
148 | sched = now + FL_MAX_LINGER; | |
149 | if (sched) { | |
150 | ip6_fl_gc_timer.expires = sched; | |
151 | add_timer(&ip6_fl_gc_timer); | |
152 | } | |
153 | write_unlock(&ip6_fl_lock); | |
154 | } | |
155 | ||
156 | static int fl_intern(struct ip6_flowlabel *fl, __u32 label) | |
157 | { | |
158 | fl->label = label & IPV6_FLOWLABEL_MASK; | |
159 | ||
160 | write_lock_bh(&ip6_fl_lock); | |
161 | if (label == 0) { | |
162 | for (;;) { | |
163 | fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK; | |
164 | if (fl->label) { | |
165 | struct ip6_flowlabel *lfl; | |
166 | lfl = __fl_lookup(fl->label); | |
167 | if (lfl == NULL) | |
168 | break; | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | fl->lastuse = jiffies; | |
174 | fl->next = fl_ht[FL_HASH(fl->label)]; | |
175 | fl_ht[FL_HASH(fl->label)] = fl; | |
176 | atomic_inc(&fl_size); | |
177 | write_unlock_bh(&ip6_fl_lock); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | ||
182 | ||
183 | /* Socket flowlabel lists */ | |
184 | ||
185 | struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, u32 label) | |
186 | { | |
187 | struct ipv6_fl_socklist *sfl; | |
188 | struct ipv6_pinfo *np = inet6_sk(sk); | |
189 | ||
190 | label &= IPV6_FLOWLABEL_MASK; | |
191 | ||
192 | for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) { | |
193 | struct ip6_flowlabel *fl = sfl->fl; | |
194 | if (fl->label == label) { | |
195 | fl->lastuse = jiffies; | |
196 | atomic_inc(&fl->users); | |
197 | return fl; | |
198 | } | |
199 | } | |
200 | return NULL; | |
201 | } | |
202 | ||
203 | void fl6_free_socklist(struct sock *sk) | |
204 | { | |
205 | struct ipv6_pinfo *np = inet6_sk(sk); | |
206 | struct ipv6_fl_socklist *sfl; | |
207 | ||
208 | while ((sfl = np->ipv6_fl_list) != NULL) { | |
209 | np->ipv6_fl_list = sfl->next; | |
210 | fl_release(sfl->fl); | |
211 | kfree(sfl); | |
212 | } | |
213 | } | |
214 | ||
215 | /* Service routines */ | |
216 | ||
217 | ||
218 | /* | |
219 | It is the only difficult place. flowlabel enforces equal headers | |
220 | before and including routing header, however user may supply options | |
221 | following rthdr. | |
222 | */ | |
223 | ||
224 | struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space, | |
225 | struct ip6_flowlabel * fl, | |
226 | struct ipv6_txoptions * fopt) | |
227 | { | |
333fad53 | 228 | struct ipv6_txoptions * fl_opt = fl ? fl->opt : NULL; |
1da177e4 | 229 | |
333fad53 YH |
230 | if (fopt == NULL || fopt->opt_flen == 0) { |
231 | if (!fl_opt || !fl_opt->dst0opt || fl_opt->srcrt) | |
232 | return fl_opt; | |
233 | } | |
1da177e4 LT |
234 | |
235 | if (fl_opt != NULL) { | |
236 | opt_space->hopopt = fl_opt->hopopt; | |
333fad53 | 237 | opt_space->dst0opt = fl_opt->srcrt ? fl_opt->dst0opt : NULL; |
1da177e4 LT |
238 | opt_space->srcrt = fl_opt->srcrt; |
239 | opt_space->opt_nflen = fl_opt->opt_nflen; | |
333fad53 YH |
240 | if (fl_opt->dst0opt && !fl_opt->srcrt) |
241 | opt_space->opt_nflen -= ipv6_optlen(fl_opt->dst0opt); | |
1da177e4 LT |
242 | } else { |
243 | if (fopt->opt_nflen == 0) | |
244 | return fopt; | |
245 | opt_space->hopopt = NULL; | |
246 | opt_space->dst0opt = NULL; | |
247 | opt_space->srcrt = NULL; | |
248 | opt_space->opt_nflen = 0; | |
249 | } | |
250 | opt_space->dst1opt = fopt->dst1opt; | |
1da177e4 LT |
251 | opt_space->opt_flen = fopt->opt_flen; |
252 | return opt_space; | |
253 | } | |
254 | ||
255 | static unsigned long check_linger(unsigned long ttl) | |
256 | { | |
257 | if (ttl < FL_MIN_LINGER) | |
258 | return FL_MIN_LINGER*HZ; | |
259 | if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN)) | |
260 | return 0; | |
261 | return ttl*HZ; | |
262 | } | |
263 | ||
264 | static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires) | |
265 | { | |
266 | linger = check_linger(linger); | |
267 | if (!linger) | |
268 | return -EPERM; | |
269 | expires = check_linger(expires); | |
270 | if (!expires) | |
271 | return -EPERM; | |
272 | fl->lastuse = jiffies; | |
273 | if (time_before(fl->linger, linger)) | |
274 | fl->linger = linger; | |
275 | if (time_before(expires, fl->linger)) | |
276 | expires = fl->linger; | |
277 | if (time_before(fl->expires, fl->lastuse + expires)) | |
278 | fl->expires = fl->lastuse + expires; | |
279 | return 0; | |
280 | } | |
281 | ||
282 | static struct ip6_flowlabel * | |
283 | fl_create(struct in6_flowlabel_req *freq, char __user *optval, int optlen, int *err_p) | |
284 | { | |
285 | struct ip6_flowlabel *fl; | |
286 | int olen; | |
287 | int addr_type; | |
288 | int err; | |
289 | ||
290 | err = -ENOMEM; | |
291 | fl = kmalloc(sizeof(*fl), GFP_KERNEL); | |
292 | if (fl == NULL) | |
293 | goto done; | |
294 | memset(fl, 0, sizeof(*fl)); | |
295 | ||
296 | olen = optlen - CMSG_ALIGN(sizeof(*freq)); | |
297 | if (olen > 0) { | |
298 | struct msghdr msg; | |
299 | struct flowi flowi; | |
300 | int junk; | |
301 | ||
302 | err = -ENOMEM; | |
303 | fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL); | |
304 | if (fl->opt == NULL) | |
305 | goto done; | |
306 | ||
307 | memset(fl->opt, 0, sizeof(*fl->opt)); | |
308 | fl->opt->tot_len = sizeof(*fl->opt) + olen; | |
309 | err = -EFAULT; | |
310 | if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen)) | |
311 | goto done; | |
312 | ||
313 | msg.msg_controllen = olen; | |
314 | msg.msg_control = (void*)(fl->opt+1); | |
315 | flowi.oif = 0; | |
316 | ||
317 | err = datagram_send_ctl(&msg, &flowi, fl->opt, &junk); | |
318 | if (err) | |
319 | goto done; | |
320 | err = -EINVAL; | |
321 | if (fl->opt->opt_flen) | |
322 | goto done; | |
323 | if (fl->opt->opt_nflen == 0) { | |
324 | kfree(fl->opt); | |
325 | fl->opt = NULL; | |
326 | } | |
327 | } | |
328 | ||
329 | fl->expires = jiffies; | |
330 | err = fl6_renew(fl, freq->flr_linger, freq->flr_expires); | |
331 | if (err) | |
332 | goto done; | |
333 | fl->share = freq->flr_share; | |
334 | addr_type = ipv6_addr_type(&freq->flr_dst); | |
335 | if ((addr_type&IPV6_ADDR_MAPPED) | |
336 | || addr_type == IPV6_ADDR_ANY) | |
337 | goto done; | |
338 | ipv6_addr_copy(&fl->dst, &freq->flr_dst); | |
339 | atomic_set(&fl->users, 1); | |
340 | switch (fl->share) { | |
341 | case IPV6_FL_S_EXCL: | |
342 | case IPV6_FL_S_ANY: | |
343 | break; | |
344 | case IPV6_FL_S_PROCESS: | |
345 | fl->owner = current->pid; | |
346 | break; | |
347 | case IPV6_FL_S_USER: | |
348 | fl->owner = current->euid; | |
349 | break; | |
350 | default: | |
351 | err = -EINVAL; | |
352 | goto done; | |
353 | } | |
354 | return fl; | |
355 | ||
356 | done: | |
357 | fl_free(fl); | |
358 | *err_p = err; | |
359 | return NULL; | |
360 | } | |
361 | ||
362 | static int mem_check(struct sock *sk) | |
363 | { | |
364 | struct ipv6_pinfo *np = inet6_sk(sk); | |
365 | struct ipv6_fl_socklist *sfl; | |
366 | int room = FL_MAX_SIZE - atomic_read(&fl_size); | |
367 | int count = 0; | |
368 | ||
369 | if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK) | |
370 | return 0; | |
371 | ||
372 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) | |
373 | count++; | |
374 | ||
375 | if (room <= 0 || | |
376 | ((count >= FL_MAX_PER_SOCK || | |
377 | (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) | |
378 | && !capable(CAP_NET_ADMIN))) | |
379 | return -ENOBUFS; | |
380 | ||
381 | return 0; | |
382 | } | |
383 | ||
384 | static int ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2) | |
385 | { | |
386 | if (h1 == h2) | |
387 | return 0; | |
388 | if (h1 == NULL || h2 == NULL) | |
389 | return 1; | |
390 | if (h1->hdrlen != h2->hdrlen) | |
391 | return 1; | |
392 | return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1)); | |
393 | } | |
394 | ||
395 | static int ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2) | |
396 | { | |
397 | if (o1 == o2) | |
398 | return 0; | |
399 | if (o1 == NULL || o2 == NULL) | |
400 | return 1; | |
401 | if (o1->opt_nflen != o2->opt_nflen) | |
402 | return 1; | |
403 | if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt)) | |
404 | return 1; | |
405 | if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt)) | |
406 | return 1; | |
407 | if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt)) | |
408 | return 1; | |
409 | return 0; | |
410 | } | |
411 | ||
412 | int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen) | |
413 | { | |
414 | int err; | |
415 | struct ipv6_pinfo *np = inet6_sk(sk); | |
416 | struct in6_flowlabel_req freq; | |
417 | struct ipv6_fl_socklist *sfl1=NULL; | |
418 | struct ipv6_fl_socklist *sfl, **sflp; | |
419 | struct ip6_flowlabel *fl; | |
420 | ||
421 | if (optlen < sizeof(freq)) | |
422 | return -EINVAL; | |
423 | ||
424 | if (copy_from_user(&freq, optval, sizeof(freq))) | |
425 | return -EFAULT; | |
426 | ||
427 | switch (freq.flr_action) { | |
428 | case IPV6_FL_A_PUT: | |
429 | write_lock_bh(&ip6_sk_fl_lock); | |
430 | for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) { | |
431 | if (sfl->fl->label == freq.flr_label) { | |
432 | if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK)) | |
433 | np->flow_label &= ~IPV6_FLOWLABEL_MASK; | |
434 | *sflp = sfl->next; | |
435 | write_unlock_bh(&ip6_sk_fl_lock); | |
436 | fl_release(sfl->fl); | |
437 | kfree(sfl); | |
438 | return 0; | |
439 | } | |
440 | } | |
441 | write_unlock_bh(&ip6_sk_fl_lock); | |
442 | return -ESRCH; | |
443 | ||
444 | case IPV6_FL_A_RENEW: | |
445 | read_lock_bh(&ip6_sk_fl_lock); | |
446 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { | |
447 | if (sfl->fl->label == freq.flr_label) { | |
448 | err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires); | |
449 | read_unlock_bh(&ip6_sk_fl_lock); | |
450 | return err; | |
451 | } | |
452 | } | |
453 | read_unlock_bh(&ip6_sk_fl_lock); | |
454 | ||
455 | if (freq.flr_share == IPV6_FL_S_NONE && capable(CAP_NET_ADMIN)) { | |
456 | fl = fl_lookup(freq.flr_label); | |
457 | if (fl) { | |
458 | err = fl6_renew(fl, freq.flr_linger, freq.flr_expires); | |
459 | fl_release(fl); | |
460 | return err; | |
461 | } | |
462 | } | |
463 | return -ESRCH; | |
464 | ||
465 | case IPV6_FL_A_GET: | |
466 | if (freq.flr_label & ~IPV6_FLOWLABEL_MASK) | |
467 | return -EINVAL; | |
468 | ||
469 | fl = fl_create(&freq, optval, optlen, &err); | |
470 | if (fl == NULL) | |
471 | return err; | |
472 | sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL); | |
473 | ||
474 | if (freq.flr_label) { | |
475 | struct ip6_flowlabel *fl1 = NULL; | |
476 | ||
477 | err = -EEXIST; | |
478 | read_lock_bh(&ip6_sk_fl_lock); | |
479 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { | |
480 | if (sfl->fl->label == freq.flr_label) { | |
481 | if (freq.flr_flags&IPV6_FL_F_EXCL) { | |
482 | read_unlock_bh(&ip6_sk_fl_lock); | |
483 | goto done; | |
484 | } | |
485 | fl1 = sfl->fl; | |
486 | atomic_inc(&fl->users); | |
487 | break; | |
488 | } | |
489 | } | |
490 | read_unlock_bh(&ip6_sk_fl_lock); | |
491 | ||
492 | if (fl1 == NULL) | |
493 | fl1 = fl_lookup(freq.flr_label); | |
494 | if (fl1) { | |
495 | err = -EEXIST; | |
496 | if (freq.flr_flags&IPV6_FL_F_EXCL) | |
497 | goto release; | |
498 | err = -EPERM; | |
499 | if (fl1->share == IPV6_FL_S_EXCL || | |
500 | fl1->share != fl->share || | |
501 | fl1->owner != fl->owner) | |
502 | goto release; | |
503 | ||
504 | err = -EINVAL; | |
505 | if (!ipv6_addr_equal(&fl1->dst, &fl->dst) || | |
506 | ipv6_opt_cmp(fl1->opt, fl->opt)) | |
507 | goto release; | |
508 | ||
509 | err = -ENOMEM; | |
510 | if (sfl1 == NULL) | |
511 | goto release; | |
512 | if (fl->linger > fl1->linger) | |
513 | fl1->linger = fl->linger; | |
514 | if ((long)(fl->expires - fl1->expires) > 0) | |
515 | fl1->expires = fl->expires; | |
516 | write_lock_bh(&ip6_sk_fl_lock); | |
517 | sfl1->fl = fl1; | |
518 | sfl1->next = np->ipv6_fl_list; | |
519 | np->ipv6_fl_list = sfl1; | |
520 | write_unlock_bh(&ip6_sk_fl_lock); | |
521 | fl_free(fl); | |
522 | return 0; | |
523 | ||
524 | release: | |
525 | fl_release(fl1); | |
526 | goto done; | |
527 | } | |
528 | } | |
529 | err = -ENOENT; | |
530 | if (!(freq.flr_flags&IPV6_FL_F_CREATE)) | |
531 | goto done; | |
532 | ||
533 | err = -ENOMEM; | |
534 | if (sfl1 == NULL || (err = mem_check(sk)) != 0) | |
535 | goto done; | |
536 | ||
537 | err = fl_intern(fl, freq.flr_label); | |
538 | if (err) | |
539 | goto done; | |
540 | ||
6c94d361 DM |
541 | if (!freq.flr_label) { |
542 | if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label, | |
543 | &fl->label, sizeof(fl->label))) { | |
544 | /* Intentionally ignore fault. */ | |
545 | } | |
546 | } | |
1da177e4 LT |
547 | |
548 | sfl1->fl = fl; | |
549 | sfl1->next = np->ipv6_fl_list; | |
550 | np->ipv6_fl_list = sfl1; | |
551 | return 0; | |
552 | ||
553 | default: | |
554 | return -EINVAL; | |
555 | } | |
556 | ||
557 | done: | |
558 | fl_free(fl); | |
559 | kfree(sfl1); | |
560 | return err; | |
561 | } | |
562 | ||
563 | #ifdef CONFIG_PROC_FS | |
564 | ||
565 | struct ip6fl_iter_state { | |
566 | int bucket; | |
567 | }; | |
568 | ||
569 | #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private) | |
570 | ||
571 | static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq) | |
572 | { | |
573 | struct ip6_flowlabel *fl = NULL; | |
574 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); | |
575 | ||
576 | for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) { | |
577 | if (fl_ht[state->bucket]) { | |
578 | fl = fl_ht[state->bucket]; | |
579 | break; | |
580 | } | |
581 | } | |
582 | return fl; | |
583 | } | |
584 | ||
585 | static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl) | |
586 | { | |
587 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); | |
588 | ||
589 | fl = fl->next; | |
590 | while (!fl) { | |
591 | if (++state->bucket <= FL_HASH_MASK) | |
592 | fl = fl_ht[state->bucket]; | |
593 | } | |
594 | return fl; | |
595 | } | |
596 | ||
597 | static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos) | |
598 | { | |
599 | struct ip6_flowlabel *fl = ip6fl_get_first(seq); | |
600 | if (fl) | |
601 | while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL) | |
602 | --pos; | |
603 | return pos ? NULL : fl; | |
604 | } | |
605 | ||
606 | static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos) | |
607 | { | |
608 | read_lock_bh(&ip6_fl_lock); | |
609 | return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; | |
610 | } | |
611 | ||
612 | static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
613 | { | |
614 | struct ip6_flowlabel *fl; | |
615 | ||
616 | if (v == SEQ_START_TOKEN) | |
617 | fl = ip6fl_get_first(seq); | |
618 | else | |
619 | fl = ip6fl_get_next(seq, v); | |
620 | ++*pos; | |
621 | return fl; | |
622 | } | |
623 | ||
624 | static void ip6fl_seq_stop(struct seq_file *seq, void *v) | |
625 | { | |
626 | read_unlock_bh(&ip6_fl_lock); | |
627 | } | |
628 | ||
629 | static void ip6fl_fl_seq_show(struct seq_file *seq, struct ip6_flowlabel *fl) | |
630 | { | |
631 | while(fl) { | |
632 | seq_printf(seq, | |
633 | "%05X %-1d %-6d %-6d %-6ld %-8ld " | |
634 | "%02x%02x%02x%02x%02x%02x%02x%02x " | |
635 | "%-4d\n", | |
636 | (unsigned)ntohl(fl->label), | |
637 | fl->share, | |
638 | (unsigned)fl->owner, | |
639 | atomic_read(&fl->users), | |
640 | fl->linger/HZ, | |
641 | (long)(fl->expires - jiffies)/HZ, | |
642 | NIP6(fl->dst), | |
643 | fl->opt ? fl->opt->opt_nflen : 0); | |
644 | fl = fl->next; | |
645 | } | |
646 | } | |
647 | ||
648 | static int ip6fl_seq_show(struct seq_file *seq, void *v) | |
649 | { | |
650 | if (v == SEQ_START_TOKEN) | |
651 | seq_puts(seq, "Label S Owner Users Linger Expires " | |
652 | "Dst Opt\n"); | |
653 | else | |
654 | ip6fl_fl_seq_show(seq, v); | |
655 | return 0; | |
656 | } | |
657 | ||
658 | static struct seq_operations ip6fl_seq_ops = { | |
659 | .start = ip6fl_seq_start, | |
660 | .next = ip6fl_seq_next, | |
661 | .stop = ip6fl_seq_stop, | |
662 | .show = ip6fl_seq_show, | |
663 | }; | |
664 | ||
665 | static int ip6fl_seq_open(struct inode *inode, struct file *file) | |
666 | { | |
667 | struct seq_file *seq; | |
668 | int rc = -ENOMEM; | |
669 | struct ip6fl_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL); | |
670 | ||
671 | if (!s) | |
672 | goto out; | |
673 | ||
674 | rc = seq_open(file, &ip6fl_seq_ops); | |
675 | if (rc) | |
676 | goto out_kfree; | |
677 | ||
678 | seq = file->private_data; | |
679 | seq->private = s; | |
680 | memset(s, 0, sizeof(*s)); | |
681 | out: | |
682 | return rc; | |
683 | out_kfree: | |
684 | kfree(s); | |
685 | goto out; | |
686 | } | |
687 | ||
688 | static struct file_operations ip6fl_seq_fops = { | |
689 | .owner = THIS_MODULE, | |
690 | .open = ip6fl_seq_open, | |
691 | .read = seq_read, | |
692 | .llseek = seq_lseek, | |
693 | .release = seq_release_private, | |
694 | }; | |
695 | #endif | |
696 | ||
697 | ||
698 | void ip6_flowlabel_init(void) | |
699 | { | |
700 | #ifdef CONFIG_PROC_FS | |
701 | proc_net_fops_create("ip6_flowlabel", S_IRUGO, &ip6fl_seq_fops); | |
702 | #endif | |
703 | } | |
704 | ||
705 | void ip6_flowlabel_cleanup(void) | |
706 | { | |
707 | del_timer(&ip6_fl_gc_timer); | |
708 | #ifdef CONFIG_PROC_FS | |
709 | proc_net_remove("ip6_flowlabel"); | |
710 | #endif | |
711 | } |