Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 12 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 24 | #include <linux/export.h> |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 25 | #include <linux/pid_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 27 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <net/sock.h> |
| 29 | |
| 30 | #include <net/ipv6.h> |
| 31 | #include <net/ndisc.h> |
| 32 | #include <net/protocol.h> |
| 33 | #include <net/ip6_route.h> |
| 34 | #include <net/addrconf.h> |
| 35 | #include <net/rawv6.h> |
| 36 | #include <net/icmp.h> |
| 37 | #include <net/transp_v6.h> |
| 38 | |
| 39 | #include <asm/uaccess.h> |
| 40 | |
| 41 | #define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified |
| 42 | in old IPv6 RFC. Well, it was reasonable value. |
| 43 | */ |
| 44 | #define FL_MAX_LINGER 60 /* Maximal linger timeout */ |
| 45 | |
| 46 | /* FL hash table */ |
| 47 | |
| 48 | #define FL_MAX_PER_SOCK 32 |
| 49 | #define FL_MAX_SIZE 4096 |
| 50 | #define FL_HASH_MASK 255 |
| 51 | #define FL_HASH(l) (ntohl(l)&FL_HASH_MASK) |
| 52 | |
| 53 | static atomic_t fl_size = ATOMIC_INIT(0); |
| 54 | static struct ip6_flowlabel *fl_ht[FL_HASH_MASK+1]; |
| 55 | |
| 56 | static void ip6_fl_gc(unsigned long dummy); |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 57 | static DEFINE_TIMER(ip6_fl_gc_timer, ip6_fl_gc, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /* FL hash table lock: it protects only of GC */ |
| 60 | |
| 61 | static DEFINE_RWLOCK(ip6_fl_lock); |
| 62 | |
| 63 | /* Big socket sock */ |
| 64 | |
| 65 | static DEFINE_RWLOCK(ip6_sk_fl_lock); |
| 66 | |
| 67 | |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 68 | static inline struct ip6_flowlabel *__fl_lookup(struct net *net, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | struct ip6_flowlabel *fl; |
| 71 | |
| 72 | for (fl=fl_ht[FL_HASH(label)]; fl; fl = fl->next) { |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 73 | if (fl->label == label && net_eq(fl->fl_net, net)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | return fl; |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 79 | static struct ip6_flowlabel *fl_lookup(struct net *net, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | struct ip6_flowlabel *fl; |
| 82 | |
| 83 | read_lock_bh(&ip6_fl_lock); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 84 | fl = __fl_lookup(net, label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (fl) |
| 86 | atomic_inc(&fl->users); |
| 87 | read_unlock_bh(&ip6_fl_lock); |
| 88 | return fl; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | static void fl_free(struct ip6_flowlabel *fl) |
| 93 | { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 94 | if (fl) { |
Dan Carpenter | 898132a | 2012-08-16 16:15:02 +0300 | [diff] [blame] | 95 | if (fl->share == IPV6_FL_S_PROCESS) |
| 96 | put_pid(fl->owner.pid); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 97 | release_net(fl->fl_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | kfree(fl->opt); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | kfree(fl); |
| 101 | } |
| 102 | |
| 103 | static void fl_release(struct ip6_flowlabel *fl) |
| 104 | { |
| 105 | write_lock_bh(&ip6_fl_lock); |
| 106 | |
| 107 | fl->lastuse = jiffies; |
| 108 | if (atomic_dec_and_test(&fl->users)) { |
| 109 | unsigned long ttd = fl->lastuse + fl->linger; |
| 110 | if (time_after(ttd, fl->expires)) |
| 111 | fl->expires = ttd; |
| 112 | ttd = fl->expires; |
| 113 | if (fl->opt && fl->share == IPV6_FL_S_EXCL) { |
| 114 | struct ipv6_txoptions *opt = fl->opt; |
| 115 | fl->opt = NULL; |
| 116 | kfree(opt); |
| 117 | } |
| 118 | if (!timer_pending(&ip6_fl_gc_timer) || |
| 119 | time_after(ip6_fl_gc_timer.expires, ttd)) |
| 120 | mod_timer(&ip6_fl_gc_timer, ttd); |
| 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | write_unlock_bh(&ip6_fl_lock); |
| 123 | } |
| 124 | |
| 125 | static void ip6_fl_gc(unsigned long dummy) |
| 126 | { |
| 127 | int i; |
| 128 | unsigned long now = jiffies; |
| 129 | unsigned long sched = 0; |
| 130 | |
| 131 | write_lock(&ip6_fl_lock); |
| 132 | |
| 133 | for (i=0; i<=FL_HASH_MASK; i++) { |
| 134 | struct ip6_flowlabel *fl, **flp; |
| 135 | flp = &fl_ht[i]; |
| 136 | while ((fl=*flp) != NULL) { |
| 137 | if (atomic_read(&fl->users) == 0) { |
| 138 | unsigned long ttd = fl->lastuse + fl->linger; |
| 139 | if (time_after(ttd, fl->expires)) |
| 140 | fl->expires = ttd; |
| 141 | ttd = fl->expires; |
| 142 | if (time_after_eq(now, ttd)) { |
| 143 | *flp = fl->next; |
| 144 | fl_free(fl); |
| 145 | atomic_dec(&fl_size); |
| 146 | continue; |
| 147 | } |
| 148 | if (!sched || time_before(ttd, sched)) |
| 149 | sched = ttd; |
| 150 | } |
| 151 | flp = &fl->next; |
| 152 | } |
| 153 | } |
| 154 | if (!sched && atomic_read(&fl_size)) |
| 155 | sched = now + FL_MAX_LINGER; |
| 156 | if (sched) { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 157 | mod_timer(&ip6_fl_gc_timer, sched); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | write_unlock(&ip6_fl_lock); |
| 160 | } |
| 161 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 162 | static void __net_exit ip6_fl_purge(struct net *net) |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 163 | { |
| 164 | int i; |
| 165 | |
| 166 | write_lock(&ip6_fl_lock); |
| 167 | for (i = 0; i <= FL_HASH_MASK; i++) { |
| 168 | struct ip6_flowlabel *fl, **flp; |
| 169 | flp = &fl_ht[i]; |
| 170 | while ((fl = *flp) != NULL) { |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 171 | if (net_eq(fl->fl_net, net) && |
| 172 | atomic_read(&fl->users) == 0) { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 173 | *flp = fl->next; |
| 174 | fl_free(fl); |
| 175 | atomic_dec(&fl_size); |
| 176 | continue; |
| 177 | } |
| 178 | flp = &fl->next; |
| 179 | } |
| 180 | } |
| 181 | write_unlock(&ip6_fl_lock); |
| 182 | } |
| 183 | |
| 184 | static struct ip6_flowlabel *fl_intern(struct net *net, |
| 185 | struct ip6_flowlabel *fl, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 187 | struct ip6_flowlabel *lfl; |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | fl->label = label & IPV6_FLOWLABEL_MASK; |
| 190 | |
| 191 | write_lock_bh(&ip6_fl_lock); |
| 192 | if (label == 0) { |
| 193 | for (;;) { |
| 194 | fl->label = htonl(net_random())&IPV6_FLOWLABEL_MASK; |
| 195 | if (fl->label) { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 196 | lfl = __fl_lookup(net, fl->label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (lfl == NULL) |
| 198 | break; |
| 199 | } |
| 200 | } |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 201 | } else { |
| 202 | /* |
| 203 | * we dropper the ip6_fl_lock, so this entry could reappear |
| 204 | * and we need to recheck with it. |
| 205 | * |
| 206 | * OTOH no need to search the active socket first, like it is |
| 207 | * done in ipv6_flowlabel_opt - sock is locked, so new entry |
| 208 | * with the same label can only appear on another sock |
| 209 | */ |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 210 | lfl = __fl_lookup(net, fl->label); |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 211 | if (lfl != NULL) { |
| 212 | atomic_inc(&lfl->users); |
| 213 | write_unlock_bh(&ip6_fl_lock); |
| 214 | return lfl; |
| 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | fl->lastuse = jiffies; |
| 219 | fl->next = fl_ht[FL_HASH(fl->label)]; |
| 220 | fl_ht[FL_HASH(fl->label)] = fl; |
| 221 | atomic_inc(&fl_size); |
| 222 | write_unlock_bh(&ip6_fl_lock); |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 223 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
| 227 | |
| 228 | /* Socket flowlabel lists */ |
| 229 | |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 230 | struct ip6_flowlabel * fl6_sock_lookup(struct sock *sk, __be32 label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
| 232 | struct ipv6_fl_socklist *sfl; |
| 233 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 234 | |
| 235 | label &= IPV6_FLOWLABEL_MASK; |
| 236 | |
Pavel Emelyanov | bd0bf57 | 2007-10-18 05:15:57 -0700 | [diff] [blame] | 237 | read_lock_bh(&ip6_sk_fl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | for (sfl=np->ipv6_fl_list; sfl; sfl = sfl->next) { |
| 239 | struct ip6_flowlabel *fl = sfl->fl; |
| 240 | if (fl->label == label) { |
| 241 | fl->lastuse = jiffies; |
| 242 | atomic_inc(&fl->users); |
Pavel Emelyanov | 52f095e | 2007-10-18 05:38:48 -0700 | [diff] [blame] | 243 | read_unlock_bh(&ip6_sk_fl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return fl; |
| 245 | } |
| 246 | } |
Pavel Emelyanov | bd0bf57 | 2007-10-18 05:15:57 -0700 | [diff] [blame] | 247 | read_unlock_bh(&ip6_sk_fl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | return NULL; |
| 249 | } |
| 250 | |
Arnaldo Carvalho de Melo | 3cf3dc6 | 2005-12-13 23:23:20 -0800 | [diff] [blame] | 251 | EXPORT_SYMBOL_GPL(fl6_sock_lookup); |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | void fl6_free_socklist(struct sock *sk) |
| 254 | { |
| 255 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 256 | struct ipv6_fl_socklist *sfl; |
| 257 | |
YOSHIFUJI Hideaki / 吉藤英明 | f256dc5 | 2013-01-30 09:26:42 +0000 | [diff] [blame^] | 258 | if (!np->ipv6_fl_list) |
| 259 | return; |
| 260 | |
| 261 | write_lock_bh(&ipv6_sk_fl_lock); |
| 262 | sfl = np->ipv6_fl_list; |
| 263 | np->ipv6_fl_list = NULL; |
| 264 | write_unlock_bh(&ipv6_sk_fl_lock); |
| 265 | |
| 266 | while (sfl) { |
| 267 | struct ipv6_fl_socklist *next = sfl->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | fl_release(sfl->fl); |
| 269 | kfree(sfl); |
YOSHIFUJI Hideaki / 吉藤英明 | f256dc5 | 2013-01-30 09:26:42 +0000 | [diff] [blame^] | 270 | sfl = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | /* Service routines */ |
| 275 | |
| 276 | |
| 277 | /* |
| 278 | It is the only difficult place. flowlabel enforces equal headers |
| 279 | before and including routing header, however user may supply options |
| 280 | following rthdr. |
| 281 | */ |
| 282 | |
| 283 | struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions * opt_space, |
| 284 | struct ip6_flowlabel * fl, |
| 285 | struct ipv6_txoptions * fopt) |
| 286 | { |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 287 | struct ipv6_txoptions * fl_opt = fl->opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 288 | |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 289 | if (fopt == NULL || fopt->opt_flen == 0) |
| 290 | return fl_opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (fl_opt != NULL) { |
| 293 | opt_space->hopopt = fl_opt->hopopt; |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 294 | opt_space->dst0opt = fl_opt->dst0opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | opt_space->srcrt = fl_opt->srcrt; |
| 296 | opt_space->opt_nflen = fl_opt->opt_nflen; |
| 297 | } else { |
| 298 | if (fopt->opt_nflen == 0) |
| 299 | return fopt; |
| 300 | opt_space->hopopt = NULL; |
| 301 | opt_space->dst0opt = NULL; |
| 302 | opt_space->srcrt = NULL; |
| 303 | opt_space->opt_nflen = 0; |
| 304 | } |
| 305 | opt_space->dst1opt = fopt->dst1opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | opt_space->opt_flen = fopt->opt_flen; |
| 307 | return opt_space; |
| 308 | } |
Chris Elston | a495f83 | 2012-04-29 21:48:53 +0000 | [diff] [blame] | 309 | EXPORT_SYMBOL_GPL(fl6_merge_options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | static unsigned long check_linger(unsigned long ttl) |
| 312 | { |
| 313 | if (ttl < FL_MIN_LINGER) |
| 314 | return FL_MIN_LINGER*HZ; |
| 315 | if (ttl > FL_MAX_LINGER && !capable(CAP_NET_ADMIN)) |
| 316 | return 0; |
| 317 | return ttl*HZ; |
| 318 | } |
| 319 | |
| 320 | static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned long expires) |
| 321 | { |
| 322 | linger = check_linger(linger); |
| 323 | if (!linger) |
| 324 | return -EPERM; |
| 325 | expires = check_linger(expires); |
| 326 | if (!expires) |
| 327 | return -EPERM; |
| 328 | fl->lastuse = jiffies; |
| 329 | if (time_before(fl->linger, linger)) |
| 330 | fl->linger = linger; |
| 331 | if (time_before(expires, fl->linger)) |
| 332 | expires = fl->linger; |
| 333 | if (time_before(fl->expires, fl->lastuse + expires)) |
| 334 | fl->expires = fl->lastuse + expires; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static struct ip6_flowlabel * |
Maciej Żenczykowski | ec0506d | 2011-08-28 12:35:31 +0000 | [diff] [blame] | 339 | fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq, |
| 340 | char __user *optval, int optlen, int *err_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
David S. Miller | 684de40 | 2009-02-06 00:49:55 -0800 | [diff] [blame] | 342 | struct ip6_flowlabel *fl = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | int olen; |
| 344 | int addr_type; |
| 345 | int err; |
| 346 | |
David S. Miller | 684de40 | 2009-02-06 00:49:55 -0800 | [diff] [blame] | 347 | olen = optlen - CMSG_ALIGN(sizeof(*freq)); |
| 348 | err = -EINVAL; |
| 349 | if (olen > 64 * 1024) |
| 350 | goto done; |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | err = -ENOMEM; |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 353 | fl = kzalloc(sizeof(*fl), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | if (fl == NULL) |
| 355 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | if (olen > 0) { |
| 358 | struct msghdr msg; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 359 | struct flowi6 flowi6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | int junk; |
| 361 | |
| 362 | err = -ENOMEM; |
| 363 | fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL); |
| 364 | if (fl->opt == NULL) |
| 365 | goto done; |
| 366 | |
| 367 | memset(fl->opt, 0, sizeof(*fl->opt)); |
| 368 | fl->opt->tot_len = sizeof(*fl->opt) + olen; |
| 369 | err = -EFAULT; |
| 370 | if (copy_from_user(fl->opt+1, optval+CMSG_ALIGN(sizeof(*freq)), olen)) |
| 371 | goto done; |
| 372 | |
| 373 | msg.msg_controllen = olen; |
| 374 | msg.msg_control = (void*)(fl->opt+1); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 375 | memset(&flowi6, 0, sizeof(flowi6)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Maciej Żenczykowski | ec0506d | 2011-08-28 12:35:31 +0000 | [diff] [blame] | 377 | err = datagram_send_ctl(net, sk, &msg, &flowi6, fl->opt, &junk, |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame] | 378 | &junk, &junk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (err) |
| 380 | goto done; |
| 381 | err = -EINVAL; |
| 382 | if (fl->opt->opt_flen) |
| 383 | goto done; |
| 384 | if (fl->opt->opt_nflen == 0) { |
| 385 | kfree(fl->opt); |
| 386 | fl->opt = NULL; |
| 387 | } |
| 388 | } |
| 389 | |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 390 | fl->fl_net = hold_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | fl->expires = jiffies; |
| 392 | err = fl6_renew(fl, freq->flr_linger, freq->flr_expires); |
| 393 | if (err) |
| 394 | goto done; |
| 395 | fl->share = freq->flr_share; |
| 396 | addr_type = ipv6_addr_type(&freq->flr_dst); |
Joe Perches | 3570021 | 2009-11-24 14:52:52 -0800 | [diff] [blame] | 397 | if ((addr_type & IPV6_ADDR_MAPPED) || |
| 398 | addr_type == IPV6_ADDR_ANY) { |
James Morris | c6817e4 | 2006-10-30 18:56:06 -0800 | [diff] [blame] | 399 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | goto done; |
James Morris | c6817e4 | 2006-10-30 18:56:06 -0800 | [diff] [blame] | 401 | } |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 402 | fl->dst = freq->flr_dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | atomic_set(&fl->users, 1); |
| 404 | switch (fl->share) { |
| 405 | case IPV6_FL_S_EXCL: |
| 406 | case IPV6_FL_S_ANY: |
| 407 | break; |
| 408 | case IPV6_FL_S_PROCESS: |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 409 | fl->owner.pid = get_task_pid(current, PIDTYPE_PID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | break; |
| 411 | case IPV6_FL_S_USER: |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 412 | fl->owner.uid = current_euid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | break; |
| 414 | default: |
| 415 | err = -EINVAL; |
| 416 | goto done; |
| 417 | } |
| 418 | return fl; |
| 419 | |
| 420 | done: |
| 421 | fl_free(fl); |
| 422 | *err_p = err; |
| 423 | return NULL; |
| 424 | } |
| 425 | |
| 426 | static int mem_check(struct sock *sk) |
| 427 | { |
| 428 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 429 | struct ipv6_fl_socklist *sfl; |
| 430 | int room = FL_MAX_SIZE - atomic_read(&fl_size); |
| 431 | int count = 0; |
| 432 | |
| 433 | if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK) |
| 434 | return 0; |
| 435 | |
| 436 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) |
| 437 | count++; |
| 438 | |
| 439 | if (room <= 0 || |
| 440 | ((count >= FL_MAX_PER_SOCK || |
Joe Perches | 3570021 | 2009-11-24 14:52:52 -0800 | [diff] [blame] | 441 | (count > 0 && room < FL_MAX_SIZE/2) || room < FL_MAX_SIZE/4) && |
| 442 | !capable(CAP_NET_ADMIN))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | return -ENOBUFS; |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 448 | static bool ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | if (h1 == h2) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 451 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (h1 == NULL || h2 == NULL) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 453 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (h1->hdrlen != h2->hdrlen) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 455 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1)); |
| 457 | } |
| 458 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 459 | static bool ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
| 461 | if (o1 == o2) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 462 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if (o1 == NULL || o2 == NULL) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 464 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (o1->opt_nflen != o2->opt_nflen) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 466 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 468 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 470 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt)) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 472 | return true; |
| 473 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Pavel Emelyanov | 0402804 | 2007-10-18 05:14:58 -0700 | [diff] [blame] | 476 | static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl, |
| 477 | struct ip6_flowlabel *fl) |
| 478 | { |
| 479 | write_lock_bh(&ip6_sk_fl_lock); |
| 480 | sfl->fl = fl; |
| 481 | sfl->next = np->ipv6_fl_list; |
| 482 | np->ipv6_fl_list = sfl; |
| 483 | write_unlock_bh(&ip6_sk_fl_lock); |
| 484 | } |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen) |
| 487 | { |
Ingo Molnar | 55205d4 | 2008-11-25 16:50:30 -0800 | [diff] [blame] | 488 | int uninitialized_var(err); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 489 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 491 | struct in6_flowlabel_req freq; |
| 492 | struct ipv6_fl_socklist *sfl1=NULL; |
| 493 | struct ipv6_fl_socklist *sfl, **sflp; |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 494 | struct ip6_flowlabel *fl, *fl1 = NULL; |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | if (optlen < sizeof(freq)) |
| 498 | return -EINVAL; |
| 499 | |
| 500 | if (copy_from_user(&freq, optval, sizeof(freq))) |
| 501 | return -EFAULT; |
| 502 | |
| 503 | switch (freq.flr_action) { |
| 504 | case IPV6_FL_A_PUT: |
| 505 | write_lock_bh(&ip6_sk_fl_lock); |
| 506 | for (sflp = &np->ipv6_fl_list; (sfl=*sflp)!=NULL; sflp = &sfl->next) { |
| 507 | if (sfl->fl->label == freq.flr_label) { |
| 508 | if (freq.flr_label == (np->flow_label&IPV6_FLOWLABEL_MASK)) |
| 509 | np->flow_label &= ~IPV6_FLOWLABEL_MASK; |
| 510 | *sflp = sfl->next; |
| 511 | write_unlock_bh(&ip6_sk_fl_lock); |
| 512 | fl_release(sfl->fl); |
| 513 | kfree(sfl); |
| 514 | return 0; |
| 515 | } |
| 516 | } |
| 517 | write_unlock_bh(&ip6_sk_fl_lock); |
| 518 | return -ESRCH; |
| 519 | |
| 520 | case IPV6_FL_A_RENEW: |
| 521 | read_lock_bh(&ip6_sk_fl_lock); |
| 522 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { |
| 523 | if (sfl->fl->label == freq.flr_label) { |
| 524 | err = fl6_renew(sfl->fl, freq.flr_linger, freq.flr_expires); |
| 525 | read_unlock_bh(&ip6_sk_fl_lock); |
| 526 | return err; |
| 527 | } |
| 528 | } |
| 529 | read_unlock_bh(&ip6_sk_fl_lock); |
| 530 | |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 531 | if (freq.flr_share == IPV6_FL_S_NONE && |
| 532 | ns_capable(net->user_ns, CAP_NET_ADMIN)) { |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 533 | fl = fl_lookup(net, freq.flr_label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (fl) { |
| 535 | err = fl6_renew(fl, freq.flr_linger, freq.flr_expires); |
| 536 | fl_release(fl); |
| 537 | return err; |
| 538 | } |
| 539 | } |
| 540 | return -ESRCH; |
| 541 | |
| 542 | case IPV6_FL_A_GET: |
| 543 | if (freq.flr_label & ~IPV6_FLOWLABEL_MASK) |
| 544 | return -EINVAL; |
| 545 | |
Maciej Żenczykowski | ec0506d | 2011-08-28 12:35:31 +0000 | [diff] [blame] | 546 | fl = fl_create(net, sk, &freq, optval, optlen, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (fl == NULL) |
| 548 | return err; |
| 549 | sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL); |
| 550 | |
| 551 | if (freq.flr_label) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | err = -EEXIST; |
| 553 | read_lock_bh(&ip6_sk_fl_lock); |
| 554 | for (sfl = np->ipv6_fl_list; sfl; sfl = sfl->next) { |
| 555 | if (sfl->fl->label == freq.flr_label) { |
| 556 | if (freq.flr_flags&IPV6_FL_F_EXCL) { |
| 557 | read_unlock_bh(&ip6_sk_fl_lock); |
| 558 | goto done; |
| 559 | } |
| 560 | fl1 = sfl->fl; |
Yan Zheng | 4ea6a80 | 2005-10-24 19:55:23 +0800 | [diff] [blame] | 561 | atomic_inc(&fl1->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | break; |
| 563 | } |
| 564 | } |
| 565 | read_unlock_bh(&ip6_sk_fl_lock); |
| 566 | |
| 567 | if (fl1 == NULL) |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 568 | fl1 = fl_lookup(net, freq.flr_label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | if (fl1) { |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 570 | recheck: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | err = -EEXIST; |
| 572 | if (freq.flr_flags&IPV6_FL_F_EXCL) |
| 573 | goto release; |
| 574 | err = -EPERM; |
| 575 | if (fl1->share == IPV6_FL_S_EXCL || |
| 576 | fl1->share != fl->share || |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 577 | ((fl1->share == IPV6_FL_S_PROCESS) && |
| 578 | (fl1->owner.pid == fl->owner.pid)) || |
| 579 | ((fl1->share == IPV6_FL_S_USER) && |
| 580 | uid_eq(fl1->owner.uid, fl->owner.uid))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | goto release; |
| 582 | |
| 583 | err = -EINVAL; |
| 584 | if (!ipv6_addr_equal(&fl1->dst, &fl->dst) || |
| 585 | ipv6_opt_cmp(fl1->opt, fl->opt)) |
| 586 | goto release; |
| 587 | |
| 588 | err = -ENOMEM; |
| 589 | if (sfl1 == NULL) |
| 590 | goto release; |
| 591 | if (fl->linger > fl1->linger) |
| 592 | fl1->linger = fl->linger; |
| 593 | if ((long)(fl->expires - fl1->expires) > 0) |
| 594 | fl1->expires = fl->expires; |
Pavel Emelyanov | 0402804 | 2007-10-18 05:14:58 -0700 | [diff] [blame] | 595 | fl_link(np, sfl1, fl1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | fl_free(fl); |
| 597 | return 0; |
| 598 | |
| 599 | release: |
| 600 | fl_release(fl1); |
| 601 | goto done; |
| 602 | } |
| 603 | } |
| 604 | err = -ENOENT; |
| 605 | if (!(freq.flr_flags&IPV6_FL_F_CREATE)) |
| 606 | goto done; |
| 607 | |
| 608 | err = -ENOMEM; |
| 609 | if (sfl1 == NULL || (err = mem_check(sk)) != 0) |
| 610 | goto done; |
| 611 | |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 612 | fl1 = fl_intern(net, fl, freq.flr_label); |
Pavel Emelyanov | 78c2e50 | 2007-10-18 05:18:56 -0700 | [diff] [blame] | 613 | if (fl1 != NULL) |
| 614 | goto recheck; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
David S. Miller | 6c94d36 | 2005-05-29 20:28:01 -0700 | [diff] [blame] | 616 | if (!freq.flr_label) { |
| 617 | if (copy_to_user(&((struct in6_flowlabel_req __user *) optval)->flr_label, |
| 618 | &fl->label, sizeof(fl->label))) { |
| 619 | /* Intentionally ignore fault. */ |
| 620 | } |
| 621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Pavel Emelyanov | 0402804 | 2007-10-18 05:14:58 -0700 | [diff] [blame] | 623 | fl_link(np, sfl1, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | return 0; |
| 625 | |
| 626 | default: |
| 627 | return -EINVAL; |
| 628 | } |
| 629 | |
| 630 | done: |
| 631 | fl_free(fl); |
| 632 | kfree(sfl1); |
| 633 | return err; |
| 634 | } |
| 635 | |
| 636 | #ifdef CONFIG_PROC_FS |
| 637 | |
| 638 | struct ip6fl_iter_state { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 639 | struct seq_net_private p; |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 640 | struct pid_namespace *pid_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | int bucket; |
| 642 | }; |
| 643 | |
| 644 | #define ip6fl_seq_private(seq) ((struct ip6fl_iter_state *)(seq)->private) |
| 645 | |
| 646 | static struct ip6_flowlabel *ip6fl_get_first(struct seq_file *seq) |
| 647 | { |
| 648 | struct ip6_flowlabel *fl = NULL; |
| 649 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 650 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
| 652 | for (state->bucket = 0; state->bucket <= FL_HASH_MASK; ++state->bucket) { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 653 | fl = fl_ht[state->bucket]; |
| 654 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 655 | while (fl && !net_eq(fl->fl_net, net)) |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 656 | fl = fl->next; |
| 657 | if (fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
| 660 | return fl; |
| 661 | } |
| 662 | |
| 663 | static struct ip6_flowlabel *ip6fl_get_next(struct seq_file *seq, struct ip6_flowlabel *fl) |
| 664 | { |
| 665 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 666 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
| 668 | fl = fl->next; |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 669 | try_again: |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 670 | while (fl && !net_eq(fl->fl_net, net)) |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 671 | fl = fl->next; |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | while (!fl) { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 674 | if (++state->bucket <= FL_HASH_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | fl = fl_ht[state->bucket]; |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 676 | goto try_again; |
| 677 | } else |
James Morris | bcd6207 | 2006-10-30 15:08:42 -0800 | [diff] [blame] | 678 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | return fl; |
| 681 | } |
| 682 | |
| 683 | static struct ip6_flowlabel *ip6fl_get_idx(struct seq_file *seq, loff_t pos) |
| 684 | { |
| 685 | struct ip6_flowlabel *fl = ip6fl_get_first(seq); |
| 686 | if (fl) |
| 687 | while (pos && (fl = ip6fl_get_next(seq, fl)) != NULL) |
| 688 | --pos; |
| 689 | return pos ? NULL : fl; |
| 690 | } |
| 691 | |
| 692 | static void *ip6fl_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 693 | __acquires(ip6_fl_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
| 695 | read_lock_bh(&ip6_fl_lock); |
| 696 | return *pos ? ip6fl_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 697 | } |
| 698 | |
| 699 | static void *ip6fl_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 700 | { |
| 701 | struct ip6_flowlabel *fl; |
| 702 | |
| 703 | if (v == SEQ_START_TOKEN) |
| 704 | fl = ip6fl_get_first(seq); |
| 705 | else |
| 706 | fl = ip6fl_get_next(seq, v); |
| 707 | ++*pos; |
| 708 | return fl; |
| 709 | } |
| 710 | |
| 711 | static void ip6fl_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 712 | __releases(ip6_fl_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { |
| 714 | read_unlock_bh(&ip6_fl_lock); |
| 715 | } |
| 716 | |
James Morris | 1b7c2db | 2006-10-31 00:43:44 -0800 | [diff] [blame] | 717 | static int ip6fl_seq_show(struct seq_file *seq, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 719 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); |
James Morris | 1b7c2db | 2006-10-31 00:43:44 -0800 | [diff] [blame] | 720 | if (v == SEQ_START_TOKEN) |
| 721 | seq_printf(seq, "%-5s %-1s %-6s %-6s %-6s %-8s %-32s %s\n", |
| 722 | "Label", "S", "Owner", "Users", "Linger", "Expires", "Dst", "Opt"); |
| 723 | else { |
| 724 | struct ip6_flowlabel *fl = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | seq_printf(seq, |
Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 726 | "%05X %-1d %-6d %-6d %-6ld %-8ld %pi6 %-4d\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 727 | (unsigned int)ntohl(fl->label), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | fl->share, |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 729 | ((fl->share == IPV6_FL_S_PROCESS) ? |
| 730 | pid_nr_ns(fl->owner.pid, state->pid_ns) : |
| 731 | ((fl->share == IPV6_FL_S_USER) ? |
| 732 | from_kuid_munged(seq_user_ns(seq), fl->owner.uid) : |
| 733 | 0)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | atomic_read(&fl->users), |
| 735 | fl->linger/HZ, |
| 736 | (long)(fl->expires - jiffies)/HZ, |
Harvey Harrison | b071195 | 2008-10-28 16:05:40 -0700 | [diff] [blame] | 737 | &fl->dst, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | fl->opt ? fl->opt->opt_nflen : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 743 | static const struct seq_operations ip6fl_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | .start = ip6fl_seq_start, |
| 745 | .next = ip6fl_seq_next, |
| 746 | .stop = ip6fl_seq_stop, |
| 747 | .show = ip6fl_seq_show, |
| 748 | }; |
| 749 | |
| 750 | static int ip6fl_seq_open(struct inode *inode, struct file *file) |
| 751 | { |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 752 | struct seq_file *seq; |
| 753 | struct ip6fl_iter_state *state; |
| 754 | int err; |
| 755 | |
| 756 | err = seq_open_net(inode, file, &ip6fl_seq_ops, |
| 757 | sizeof(struct ip6fl_iter_state)); |
| 758 | |
| 759 | if (!err) { |
| 760 | seq = file->private_data; |
| 761 | state = ip6fl_seq_private(seq); |
| 762 | rcu_read_lock(); |
| 763 | state->pid_ns = get_pid_ns(task_active_pid_ns(current)); |
| 764 | rcu_read_unlock(); |
| 765 | } |
| 766 | return err; |
| 767 | } |
| 768 | |
| 769 | static int ip6fl_seq_release(struct inode *inode, struct file *file) |
| 770 | { |
| 771 | struct seq_file *seq = file->private_data; |
| 772 | struct ip6fl_iter_state *state = ip6fl_seq_private(seq); |
| 773 | put_pid_ns(state->pid_ns); |
| 774 | return seq_release_net(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 777 | static const struct file_operations ip6fl_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | .owner = THIS_MODULE, |
| 779 | .open = ip6fl_seq_open, |
| 780 | .read = seq_read, |
| 781 | .llseek = seq_lseek, |
Eric W. Biederman | 4f82f45 | 2012-05-24 10:37:59 -0600 | [diff] [blame] | 782 | .release = ip6fl_seq_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 785 | static int __net_init ip6_flowlabel_proc_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 787 | if (!proc_net_fops_create(net, "ip6_flowlabel", |
| 788 | S_IRUGO, &ip6fl_seq_fops)) |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 789 | return -ENOMEM; |
| 790 | return 0; |
| 791 | } |
| 792 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 793 | static void __net_exit ip6_flowlabel_proc_fini(struct net *net) |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 794 | { |
| 795 | proc_net_remove(net, "ip6_flowlabel"); |
| 796 | } |
| 797 | #else |
| 798 | static inline int ip6_flowlabel_proc_init(struct net *net) |
| 799 | { |
| 800 | return 0; |
| 801 | } |
| 802 | static inline void ip6_flowlabel_proc_fini(struct net *net) |
| 803 | { |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | #endif |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 806 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 807 | static void __net_exit ip6_flowlabel_net_exit(struct net *net) |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 808 | { |
| 809 | ip6_fl_purge(net); |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 810 | ip6_flowlabel_proc_fini(net); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static struct pernet_operations ip6_flowlabel_net_ops = { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 814 | .init = ip6_flowlabel_proc_init, |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 815 | .exit = ip6_flowlabel_net_exit, |
| 816 | }; |
| 817 | |
Daniel Lezcano | 0a3e78a | 2007-12-11 02:23:18 -0800 | [diff] [blame] | 818 | int ip6_flowlabel_init(void) |
| 819 | { |
Benjamin Thery | 5983a3d | 2008-03-26 16:53:30 -0700 | [diff] [blame] | 820 | return register_pernet_subsys(&ip6_flowlabel_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | void ip6_flowlabel_cleanup(void) |
| 824 | { |
| 825 | del_timer(&ip6_fl_gc_timer); |
Benjamin Thery | 60e8fbc | 2008-03-26 16:53:08 -0700 | [diff] [blame] | 826 | unregister_pernet_subsys(&ip6_flowlabel_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |