| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * INET		An implementation of the TCP/IP protocol suite for the LINUX | 
					
						
							|  |  |  |  *		operating system.  INET is implemented using the BSD Socket | 
					
						
							|  |  |  |  *		interface as the means of communication with the user level. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *		Generic INET6 transport hashtables | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  |  * Authors:	Lotsa people, from code originally in tcp, generalised here | 
					
						
							|  |  |  |  * 		by Arnaldo Carvalho de Melo <acme@mandriva.com> | 
					
						
							| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  |  * | 
					
						
							|  |  |  |  *	This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  *      modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  *      as published by the Free Software Foundation; either version | 
					
						
							|  |  |  |  *      2 of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | #include <linux/random.h>
 | 
					
						
							| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <net/inet_connection_sock.h>
 | 
					
						
							|  |  |  | #include <net/inet_hashtables.h>
 | 
					
						
							|  |  |  | #include <net/inet6_hashtables.h>
 | 
					
						
							| 
									
										
										
										
											2011-08-03 20:50:44 -07:00
										 |  |  | #include <net/secure_seq.h>
 | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | #include <net/ip.h>
 | 
					
						
							| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-04 03:46:54 +00:00
										 |  |  | int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw) | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-03-22 16:50:58 -07:00
										 |  |  | 	struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; | 
					
						
							| 
									
										
										
										
											2009-12-04 03:46:54 +00:00
										 |  |  | 	int twrefcnt = 0; | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-25 21:43:18 -07:00
										 |  |  | 	WARN_ON(!sk_unhashed(sk)); | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (sk->sk_state == TCP_LISTEN) { | 
					
						
							| 
									
										
										
										
											2008-11-20 00:40:07 -08:00
										 |  |  | 		struct inet_listen_hashbucket *ilb; | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 00:40:07 -08:00
										 |  |  | 		ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)]; | 
					
						
							|  |  |  | 		spin_lock(&ilb->lock); | 
					
						
							| 
									
										
										
										
											2008-11-23 17:22:55 -08:00
										 |  |  | 		__sk_nulls_add_node_rcu(sk, &ilb->head); | 
					
						
							| 
									
										
										
										
											2008-11-20 00:40:07 -08:00
										 |  |  | 		spin_unlock(&ilb->lock); | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		unsigned int hash; | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 		struct hlist_nulls_head *list; | 
					
						
							| 
									
										
										
										
											2008-11-20 20:39:09 -08:00
										 |  |  | 		spinlock_t *lock; | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 		sk->sk_hash = hash = inet6_sk_ehashfn(sk); | 
					
						
							| 
									
										
										
										
											2007-11-07 02:40:20 -08:00
										 |  |  | 		list = &inet_ehash_bucket(hashinfo, hash)->chain; | 
					
						
							|  |  |  | 		lock = inet_ehash_lockp(hashinfo, hash); | 
					
						
							| 
									
										
										
										
											2008-11-20 20:39:09 -08:00
										 |  |  | 		spin_lock(lock); | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 		__sk_nulls_add_node_rcu(sk, list); | 
					
						
							| 
									
										
										
										
											2009-12-04 03:46:54 +00:00
										 |  |  | 		if (tw) { | 
					
						
							|  |  |  | 			WARN_ON(sk->sk_hash != tw->tw_hash); | 
					
						
							|  |  |  | 			twrefcnt = inet_twsk_unhash(tw); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-11-20 20:39:09 -08:00
										 |  |  | 		spin_unlock(lock); | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-31 19:41:46 -07:00
										 |  |  | 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); | 
					
						
							| 
									
										
										
										
											2009-12-04 03:46:54 +00:00
										 |  |  | 	return twrefcnt; | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL(__inet6_hash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so | 
					
						
							|  |  |  |  * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The sockhash lock must be held as a reader here. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-01-31 05:07:21 -08:00
										 |  |  | struct sock *__inet6_lookup_established(struct net *net, | 
					
						
							|  |  |  | 					struct inet_hashinfo *hashinfo, | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 					   const struct in6_addr *saddr, | 
					
						
							| 
									
										
										
										
											2006-11-08 00:20:00 -08:00
										 |  |  | 					   const __be16 sport, | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 					   const struct in6_addr *daddr, | 
					
						
							|  |  |  | 					   const u16 hnum, | 
					
						
							|  |  |  | 					   const int dif) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sock *sk; | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	const struct hlist_nulls_node *node; | 
					
						
							| 
									
										
										
										
											2006-09-27 18:43:07 -07:00
										 |  |  | 	const __portpair ports = INET_COMBINED_PORTS(sport, hnum); | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 	/* Optimize here for direct hit, only listening connections can
 | 
					
						
							|  |  |  | 	 * have wildcards anyways. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2008-06-16 17:13:48 -07:00
										 |  |  | 	unsigned int hash = inet6_ehashfn(net, daddr, hnum, saddr, sport); | 
					
						
							| 
									
										
										
										
											2009-10-09 00:16:19 +00:00
										 |  |  | 	unsigned int slot = hash & hashinfo->ehash_mask; | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	struct inet_ehash_bucket *head = &hashinfo->ehash[slot]; | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	rcu_read_lock(); | 
					
						
							|  |  |  | begin: | 
					
						
							|  |  |  | 	sk_nulls_for_each_rcu(sk, node, &head->chain) { | 
					
						
							| 
									
										
										
										
											2012-11-30 09:49:27 +00:00
										 |  |  | 		if (sk->sk_hash != hash) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		if (likely(INET6_MATCH(sk, net, saddr, daddr, ports, dif))) { | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 			if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) | 
					
						
							|  |  |  | 				goto begintw; | 
					
						
							| 
									
										
										
										
											2012-11-30 09:49:27 +00:00
										 |  |  | 			if (unlikely(!INET6_MATCH(sk, net, saddr, daddr, | 
					
						
							|  |  |  | 						  ports, dif))) { | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 				sock_put(sk); | 
					
						
							|  |  |  | 				goto begin; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	if (get_nulls_value(node) != slot) | 
					
						
							|  |  |  | 		goto begin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | begintw: | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 	/* Must check for a TIME_WAIT'er before going to listener hash. */ | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	sk_nulls_for_each_rcu(sk, node, &head->twchain) { | 
					
						
							| 
									
										
										
										
											2012-11-30 09:49:27 +00:00
										 |  |  | 		if (sk->sk_hash != hash) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		if (likely(INET6_TW_MATCH(sk, net, saddr, daddr, | 
					
						
							|  |  |  | 					  ports, dif))) { | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 			if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) { | 
					
						
							|  |  |  | 				sk = NULL; | 
					
						
							|  |  |  | 				goto out; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2012-11-30 09:49:27 +00:00
										 |  |  | 			if (unlikely(!INET6_TW_MATCH(sk, net, saddr, daddr, | 
					
						
							|  |  |  | 						     ports, dif))) { | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 				sock_put(sk); | 
					
						
							|  |  |  | 				goto begintw; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			goto out; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	if (get_nulls_value(node) != slot) | 
					
						
							|  |  |  | 		goto begintw; | 
					
						
							|  |  |  | 	sk = NULL; | 
					
						
							|  |  |  | out: | 
					
						
							|  |  |  | 	rcu_read_unlock(); | 
					
						
							| 
									
										
										
										
											2006-04-09 22:48:59 -07:00
										 |  |  | 	return sk; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL(__inet6_lookup_established); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-17 00:09:38 +01:00
										 |  |  | static inline int compute_score(struct sock *sk, struct net *net, | 
					
						
							| 
									
										
										
										
											2008-11-23 17:22:55 -08:00
										 |  |  | 				const unsigned short hnum, | 
					
						
							|  |  |  | 				const struct in6_addr *daddr, | 
					
						
							|  |  |  | 				const int dif) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int score = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 06:30:45 +00:00
										 |  |  | 	if (net_eq(sock_net(sk), net) && inet_sk(sk)->inet_num == hnum && | 
					
						
							| 
									
										
										
										
											2008-11-23 17:22:55 -08:00
										 |  |  | 	    sk->sk_family == PF_INET6) { | 
					
						
							|  |  |  | 		const struct ipv6_pinfo *np = inet6_sk(sk); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		score = 1; | 
					
						
							|  |  |  | 		if (!ipv6_addr_any(&np->rcv_saddr)) { | 
					
						
							|  |  |  | 			if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) | 
					
						
							|  |  |  | 				return -1; | 
					
						
							|  |  |  | 			score++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (sk->sk_bound_dev_if) { | 
					
						
							|  |  |  | 			if (sk->sk_bound_dev_if != dif) | 
					
						
							|  |  |  | 				return -1; | 
					
						
							|  |  |  | 			score++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return score; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-31 05:07:21 -08:00
										 |  |  | struct sock *inet6_lookup_listener(struct net *net, | 
					
						
							| 
									
										
										
										
											2013-01-22 09:50:39 +00:00
										 |  |  | 		struct inet_hashinfo *hashinfo, const struct in6_addr *saddr, | 
					
						
							|  |  |  | 		const __be16 sport, const struct in6_addr *daddr, | 
					
						
							| 
									
										
										
										
											2008-01-31 05:07:21 -08:00
										 |  |  | 		const unsigned short hnum, const int dif) | 
					
						
							| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct sock *sk; | 
					
						
							| 
									
										
										
										
											2008-11-23 17:22:55 -08:00
										 |  |  | 	const struct hlist_nulls_node *node; | 
					
						
							|  |  |  | 	struct sock *result; | 
					
						
							| 
									
										
										
										
											2013-01-22 09:50:39 +00:00
										 |  |  | 	int score, hiscore, matches = 0, reuseport = 0; | 
					
						
							|  |  |  | 	u32 phash = 0; | 
					
						
							| 
									
										
										
										
											2008-11-23 17:22:55 -08:00
										 |  |  | 	unsigned int hash = inet_lhashfn(net, hnum); | 
					
						
							|  |  |  | 	struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rcu_read_lock(); | 
					
						
							|  |  |  | begin: | 
					
						
							|  |  |  | 	result = NULL; | 
					
						
							| 
									
										
										
										
											2013-01-22 09:50:39 +00:00
										 |  |  | 	hiscore = 0; | 
					
						
							| 
									
										
										
										
											2008-11-23 17:22:55 -08:00
										 |  |  | 	sk_nulls_for_each(sk, node, &ilb->head) { | 
					
						
							|  |  |  | 		score = compute_score(sk, net, hnum, daddr, dif); | 
					
						
							|  |  |  | 		if (score > hiscore) { | 
					
						
							|  |  |  | 			hiscore = score; | 
					
						
							|  |  |  | 			result = sk; | 
					
						
							| 
									
										
										
										
											2013-01-22 09:50:39 +00:00
										 |  |  | 			reuseport = sk->sk_reuseport; | 
					
						
							|  |  |  | 			if (reuseport) { | 
					
						
							|  |  |  | 				phash = inet6_ehashfn(net, daddr, hnum, | 
					
						
							|  |  |  | 						      saddr, sport); | 
					
						
							|  |  |  | 				matches = 1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else if (score == hiscore && reuseport) { | 
					
						
							|  |  |  | 			matches++; | 
					
						
							|  |  |  | 			if (((u64)phash * matches) >> 32 == 0) | 
					
						
							|  |  |  | 				result = sk; | 
					
						
							|  |  |  | 			phash = next_pseudo_random32(phash); | 
					
						
							| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-23 17:22:55 -08:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * if the nulls value we got at the end of this lookup is | 
					
						
							|  |  |  | 	 * not the expected one, we must restart lookup. | 
					
						
							|  |  |  | 	 * We probably met an item that was moved to another chain. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	if (get_nulls_value(node) != hash + LISTENING_NULLS_BASE) | 
					
						
							|  |  |  | 		goto begin; | 
					
						
							|  |  |  | 	if (result) { | 
					
						
							|  |  |  | 		if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt))) | 
					
						
							|  |  |  | 			result = NULL; | 
					
						
							|  |  |  | 		else if (unlikely(compute_score(result, net, hnum, daddr, | 
					
						
							|  |  |  | 				  dif) < hiscore)) { | 
					
						
							|  |  |  | 			sock_put(result); | 
					
						
							|  |  |  | 			goto begin; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	rcu_read_unlock(); | 
					
						
							| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(inet6_lookup_listener); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-31 05:07:21 -08:00
										 |  |  | struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo, | 
					
						
							| 
									
										
										
										
											2006-11-08 00:20:00 -08:00
										 |  |  | 			  const struct in6_addr *saddr, const __be16 sport, | 
					
						
							|  |  |  | 			  const struct in6_addr *daddr, const __be16 dport, | 
					
						
							| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  | 			  const int dif) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sock *sk; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	local_bh_disable(); | 
					
						
							| 
									
										
										
										
											2008-01-31 05:07:21 -08:00
										 |  |  | 	sk = __inet6_lookup(net, hashinfo, saddr, sport, daddr, ntohs(dport), dif); | 
					
						
							| 
									
										
										
										
											2005-08-12 09:26:18 -03:00
										 |  |  | 	local_bh_enable(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sk; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(inet6_lookup); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int __inet6_check_established(struct inet_timewait_death_row *death_row, | 
					
						
							|  |  |  | 				     struct sock *sk, const __u16 lport, | 
					
						
							|  |  |  | 				     struct inet_timewait_sock **twp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct inet_hashinfo *hinfo = death_row->hashinfo; | 
					
						
							| 
									
										
										
										
											2006-03-13 14:26:12 -08:00
										 |  |  | 	struct inet_sock *inet = inet_sk(sk); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 	const struct ipv6_pinfo *np = inet6_sk(sk); | 
					
						
							|  |  |  | 	const struct in6_addr *daddr = &np->rcv_saddr; | 
					
						
							|  |  |  | 	const struct in6_addr *saddr = &np->daddr; | 
					
						
							|  |  |  | 	const int dif = sk->sk_bound_dev_if; | 
					
						
							| 
									
										
										
										
											2009-10-15 06:30:45 +00:00
										 |  |  | 	const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport); | 
					
						
							| 
									
										
										
										
											2008-06-16 17:13:48 -07:00
										 |  |  | 	struct net *net = sock_net(sk); | 
					
						
							|  |  |  | 	const unsigned int hash = inet6_ehashfn(net, daddr, lport, saddr, | 
					
						
							| 
									
										
										
										
											2009-10-15 06:30:45 +00:00
										 |  |  | 						inet->inet_dport); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 	struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash); | 
					
						
							| 
									
										
										
										
											2008-11-20 20:39:09 -08:00
										 |  |  | 	spinlock_t *lock = inet_ehash_lockp(hinfo, hash); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 	struct sock *sk2; | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	const struct hlist_nulls_node *node; | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 	struct inet_timewait_sock *tw; | 
					
						
							| 
									
										
										
										
											2009-12-02 22:31:19 +00:00
										 |  |  | 	int twrefcnt = 0; | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 20:39:09 -08:00
										 |  |  | 	spin_lock(lock); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Check TIME-WAIT sockets first. */ | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	sk_nulls_for_each(sk2, node, &head->twchain) { | 
					
						
							| 
									
										
										
										
											2012-11-30 09:49:27 +00:00
										 |  |  | 		if (sk2->sk_hash != hash) | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-30 09:49:27 +00:00
										 |  |  | 		if (likely(INET6_TW_MATCH(sk2, net, saddr, daddr, | 
					
						
							|  |  |  | 					  ports, dif))) { | 
					
						
							|  |  |  | 			tw = inet_twsk(sk2); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 			if (twsk_unique(sk, sk2, twp)) | 
					
						
							|  |  |  | 				goto unique; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				goto not_unique; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	tw = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* And established part... */ | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	sk_nulls_for_each(sk2, node, &head->chain) { | 
					
						
							| 
									
										
										
										
											2012-11-30 09:49:27 +00:00
										 |  |  | 		if (sk2->sk_hash != hash) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		if (likely(INET6_MATCH(sk2, net, saddr, daddr, ports, dif))) | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 			goto not_unique; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | unique: | 
					
						
							| 
									
										
										
										
											2006-03-13 14:26:12 -08:00
										 |  |  | 	/* Must record num and sport now. Otherwise we will see
 | 
					
						
							|  |  |  | 	 * in hash table socket with a funny identity. */ | 
					
						
							| 
									
										
										
										
											2009-10-15 06:30:45 +00:00
										 |  |  | 	inet->inet_num = lport; | 
					
						
							|  |  |  | 	inet->inet_sport = htons(lport); | 
					
						
							| 
									
										
										
										
											2009-12-02 22:31:19 +00:00
										 |  |  | 	sk->sk_hash = hash; | 
					
						
							| 
									
										
										
										
											2008-07-25 21:43:18 -07:00
										 |  |  | 	WARN_ON(!sk_unhashed(sk)); | 
					
						
							| 
									
										
										
										
											2008-11-16 19:40:17 -08:00
										 |  |  | 	__sk_nulls_add_node_rcu(sk, &head->chain); | 
					
						
							| 
									
										
										
										
											2009-12-02 22:31:19 +00:00
										 |  |  | 	if (tw) { | 
					
						
							|  |  |  | 		twrefcnt = inet_twsk_unhash(tw); | 
					
						
							|  |  |  | 		NET_INC_STATS_BH(net, LINUX_MIB_TIMEWAITRECYCLED); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-20 20:39:09 -08:00
										 |  |  | 	spin_unlock(lock); | 
					
						
							| 
									
										
										
										
											2009-12-02 22:31:19 +00:00
										 |  |  | 	if (twrefcnt) | 
					
						
							|  |  |  | 		inet_twsk_put(tw); | 
					
						
							| 
									
										
										
										
											2008-03-31 19:41:46 -07:00
										 |  |  | 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-02 22:31:19 +00:00
										 |  |  | 	if (twp) { | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 		*twp = tw; | 
					
						
							| 
									
										
										
										
											2009-12-02 22:31:19 +00:00
										 |  |  | 	} else if (tw) { | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 		/* Silly. Should hash-dance instead... */ | 
					
						
							|  |  |  | 		inet_twsk_deschedule(tw, death_row); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		inet_twsk_put(tw); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | not_unique: | 
					
						
							| 
									
										
										
										
											2008-11-20 20:39:09 -08:00
										 |  |  | 	spin_unlock(lock); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | 	return -EADDRNOTAVAIL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline u32 inet6_sk_port_offset(const struct sock *sk) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const struct inet_sock *inet = inet_sk(sk); | 
					
						
							|  |  |  | 	const struct ipv6_pinfo *np = inet6_sk(sk); | 
					
						
							|  |  |  | 	return secure_ipv6_port_ephemeral(np->rcv_saddr.s6_addr32, | 
					
						
							|  |  |  | 					  np->daddr.s6_addr32, | 
					
						
							| 
									
										
										
										
											2009-10-15 06:30:45 +00:00
										 |  |  | 					  inet->inet_dport); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int inet6_hash_connect(struct inet_timewait_death_row *death_row, | 
					
						
							|  |  |  | 		       struct sock *sk) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-02-05 03:14:44 -08:00
										 |  |  | 	return __inet_hash_connect(death_row, sk, inet6_sk_port_offset(sk), | 
					
						
							| 
									
										
										
										
											2008-01-31 05:04:45 -08:00
										 |  |  | 			__inet6_check_established, __inet6_hash); | 
					
						
							| 
									
										
										
										
											2005-12-13 23:25:44 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(inet6_hash_connect); |