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