xref: /linux/net/ipv4/tcp_ipv4.c (revision bb5b7c11263dbbe78253cd05945a6bf8f55add8e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Implementation of the Transmission Control Protocol(TCP).
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *		IPv4 specific functions
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *		code split from:
121da177e4SLinus Torvalds  *		linux/ipv4/tcp.c
131da177e4SLinus Torvalds  *		linux/ipv4/tcp_input.c
141da177e4SLinus Torvalds  *		linux/ipv4/tcp_output.c
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *		See tcp.c for author information
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
191da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
201da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
211da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds /*
251da177e4SLinus Torvalds  * Changes:
261da177e4SLinus Torvalds  *		David S. Miller	:	New socket lookup architecture.
271da177e4SLinus Torvalds  *					This code is dedicated to John Dyson.
281da177e4SLinus Torvalds  *		David S. Miller :	Change semantics of established hash,
291da177e4SLinus Torvalds  *					half is devoted to TIME_WAIT sockets
301da177e4SLinus Torvalds  *					and the rest go in the other half.
311da177e4SLinus Torvalds  *		Andi Kleen :		Add support for syncookies and fixed
321da177e4SLinus Torvalds  *					some bugs: ip options weren't passed to
331da177e4SLinus Torvalds  *					the TCP layer, missed a check for an
341da177e4SLinus Torvalds  *					ACK bit.
351da177e4SLinus Torvalds  *		Andi Kleen :		Implemented fast path mtu discovery.
361da177e4SLinus Torvalds  *	     				Fixed many serious bugs in the
3760236fddSArnaldo Carvalho de Melo  *					request_sock handling and moved
381da177e4SLinus Torvalds  *					most of it into the af independent code.
391da177e4SLinus Torvalds  *					Added tail drop and some other bugfixes.
40caa20d9aSStephen Hemminger  *					Added new listen semantics.
411da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
421da177e4SLinus Torvalds  *	Juan Jose Ciarlante:		ip_dynaddr bits
431da177e4SLinus Torvalds  *		Andi Kleen:		various fixes.
441da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year
451da177e4SLinus Torvalds  *					coma.
461da177e4SLinus Torvalds  *	Andi Kleen		:	Fix new listen.
471da177e4SLinus Torvalds  *	Andi Kleen		:	Fix accept error reporting.
481da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
491da177e4SLinus Torvalds  *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
501da177e4SLinus Torvalds  *					a single port at the same time.
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 
54eb4dea58SHerbert Xu #include <linux/bottom_half.h>
551da177e4SLinus Torvalds #include <linux/types.h>
561da177e4SLinus Torvalds #include <linux/fcntl.h>
571da177e4SLinus Torvalds #include <linux/module.h>
581da177e4SLinus Torvalds #include <linux/random.h>
591da177e4SLinus Torvalds #include <linux/cache.h>
601da177e4SLinus Torvalds #include <linux/jhash.h>
611da177e4SLinus Torvalds #include <linux/init.h>
621da177e4SLinus Torvalds #include <linux/times.h>
631da177e4SLinus Torvalds 
64457c4cbcSEric W. Biederman #include <net/net_namespace.h>
651da177e4SLinus Torvalds #include <net/icmp.h>
66304a1618SArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
671da177e4SLinus Torvalds #include <net/tcp.h>
6820380731SArnaldo Carvalho de Melo #include <net/transp_v6.h>
691da177e4SLinus Torvalds #include <net/ipv6.h>
701da177e4SLinus Torvalds #include <net/inet_common.h>
716d6ee43eSArnaldo Carvalho de Melo #include <net/timewait_sock.h>
721da177e4SLinus Torvalds #include <net/xfrm.h>
731a2449a8SChris Leech #include <net/netdma.h>
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds #include <linux/inet.h>
761da177e4SLinus Torvalds #include <linux/ipv6.h>
771da177e4SLinus Torvalds #include <linux/stddef.h>
781da177e4SLinus Torvalds #include <linux/proc_fs.h>
791da177e4SLinus Torvalds #include <linux/seq_file.h>
801da177e4SLinus Torvalds 
81cfb6eeb4SYOSHIFUJI Hideaki #include <linux/crypto.h>
82cfb6eeb4SYOSHIFUJI Hideaki #include <linux/scatterlist.h>
83cfb6eeb4SYOSHIFUJI Hideaki 
84ab32ea5dSBrian Haley int sysctl_tcp_tw_reuse __read_mostly;
85ab32ea5dSBrian Haley int sysctl_tcp_low_latency __read_mostly;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 
88cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
897174259eSArnaldo Carvalho de Melo static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk,
907174259eSArnaldo Carvalho de Melo 						   __be32 addr);
9149a72dfbSAdam Langley static int tcp_v4_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
9249a72dfbSAdam Langley 			       __be32 daddr, __be32 saddr, struct tcphdr *th);
939501f972SYOSHIFUJI Hideaki #else
949501f972SYOSHIFUJI Hideaki static inline
959501f972SYOSHIFUJI Hideaki struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
969501f972SYOSHIFUJI Hideaki {
979501f972SYOSHIFUJI Hideaki 	return NULL;
989501f972SYOSHIFUJI Hideaki }
99cfb6eeb4SYOSHIFUJI Hideaki #endif
100cfb6eeb4SYOSHIFUJI Hideaki 
1015caea4eaSEric Dumazet struct inet_hashinfo tcp_hashinfo;
1021da177e4SLinus Torvalds 
103a94f723dSGerrit Renker static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
1041da177e4SLinus Torvalds {
105eddc9ec5SArnaldo Carvalho de Melo 	return secure_tcp_sequence_number(ip_hdr(skb)->daddr,
106eddc9ec5SArnaldo Carvalho de Melo 					  ip_hdr(skb)->saddr,
107aa8223c7SArnaldo Carvalho de Melo 					  tcp_hdr(skb)->dest,
108aa8223c7SArnaldo Carvalho de Melo 					  tcp_hdr(skb)->source);
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1116d6ee43eSArnaldo Carvalho de Melo int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
1126d6ee43eSArnaldo Carvalho de Melo {
1136d6ee43eSArnaldo Carvalho de Melo 	const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
1146d6ee43eSArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
1156d6ee43eSArnaldo Carvalho de Melo 
1166d6ee43eSArnaldo Carvalho de Melo 	/* With PAWS, it is safe from the viewpoint
1176d6ee43eSArnaldo Carvalho de Melo 	   of data integrity. Even without PAWS it is safe provided sequence
1186d6ee43eSArnaldo Carvalho de Melo 	   spaces do not overlap i.e. at data rates <= 80Mbit/sec.
1196d6ee43eSArnaldo Carvalho de Melo 
1206d6ee43eSArnaldo Carvalho de Melo 	   Actually, the idea is close to VJ's one, only timestamp cache is
1216d6ee43eSArnaldo Carvalho de Melo 	   held not per host, but per port pair and TW bucket is used as state
1226d6ee43eSArnaldo Carvalho de Melo 	   holder.
1236d6ee43eSArnaldo Carvalho de Melo 
1246d6ee43eSArnaldo Carvalho de Melo 	   If TW bucket has been already destroyed we fall back to VJ's scheme
1256d6ee43eSArnaldo Carvalho de Melo 	   and use initial timestamp retrieved from peer table.
1266d6ee43eSArnaldo Carvalho de Melo 	 */
1276d6ee43eSArnaldo Carvalho de Melo 	if (tcptw->tw_ts_recent_stamp &&
1286d6ee43eSArnaldo Carvalho de Melo 	    (twp == NULL || (sysctl_tcp_tw_reuse &&
1299d729f72SJames Morris 			     get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
1306d6ee43eSArnaldo Carvalho de Melo 		tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
1316d6ee43eSArnaldo Carvalho de Melo 		if (tp->write_seq == 0)
1326d6ee43eSArnaldo Carvalho de Melo 			tp->write_seq = 1;
1336d6ee43eSArnaldo Carvalho de Melo 		tp->rx_opt.ts_recent	   = tcptw->tw_ts_recent;
1346d6ee43eSArnaldo Carvalho de Melo 		tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
1356d6ee43eSArnaldo Carvalho de Melo 		sock_hold(sktw);
1366d6ee43eSArnaldo Carvalho de Melo 		return 1;
1376d6ee43eSArnaldo Carvalho de Melo 	}
1386d6ee43eSArnaldo Carvalho de Melo 
1396d6ee43eSArnaldo Carvalho de Melo 	return 0;
1406d6ee43eSArnaldo Carvalho de Melo }
1416d6ee43eSArnaldo Carvalho de Melo 
1426d6ee43eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(tcp_twsk_unique);
1436d6ee43eSArnaldo Carvalho de Melo 
1441da177e4SLinus Torvalds /* This will initiate an outgoing connection. */
1451da177e4SLinus Torvalds int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1481da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
1491da177e4SLinus Torvalds 	struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
1501da177e4SLinus Torvalds 	struct rtable *rt;
151bada8adcSAl Viro 	__be32 daddr, nexthop;
1521da177e4SLinus Torvalds 	int tmp;
1531da177e4SLinus Torvalds 	int err;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	if (addr_len < sizeof(struct sockaddr_in))
1561da177e4SLinus Torvalds 		return -EINVAL;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (usin->sin_family != AF_INET)
1591da177e4SLinus Torvalds 		return -EAFNOSUPPORT;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	nexthop = daddr = usin->sin_addr.s_addr;
1621da177e4SLinus Torvalds 	if (inet->opt && inet->opt->srr) {
1631da177e4SLinus Torvalds 		if (!daddr)
1641da177e4SLinus Torvalds 			return -EINVAL;
1651da177e4SLinus Torvalds 		nexthop = inet->opt->faddr;
1661da177e4SLinus Torvalds 	}
1671da177e4SLinus Torvalds 
168c720c7e8SEric Dumazet 	tmp = ip_route_connect(&rt, nexthop, inet->inet_saddr,
1691da177e4SLinus Torvalds 			       RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
1701da177e4SLinus Torvalds 			       IPPROTO_TCP,
171c720c7e8SEric Dumazet 			       inet->inet_sport, usin->sin_port, sk, 1);
172584bdf8cSWei Dong 	if (tmp < 0) {
173584bdf8cSWei Dong 		if (tmp == -ENETUNREACH)
1747c73a6faSPavel Emelyanov 			IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
1751da177e4SLinus Torvalds 		return tmp;
176584bdf8cSWei Dong 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
1791da177e4SLinus Torvalds 		ip_rt_put(rt);
1801da177e4SLinus Torvalds 		return -ENETUNREACH;
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	if (!inet->opt || !inet->opt->srr)
1841da177e4SLinus Torvalds 		daddr = rt->rt_dst;
1851da177e4SLinus Torvalds 
186c720c7e8SEric Dumazet 	if (!inet->inet_saddr)
187c720c7e8SEric Dumazet 		inet->inet_saddr = rt->rt_src;
188c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = inet->inet_saddr;
1891da177e4SLinus Torvalds 
190c720c7e8SEric Dumazet 	if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
1911da177e4SLinus Torvalds 		/* Reset inherited state */
1921da177e4SLinus Torvalds 		tp->rx_opt.ts_recent	   = 0;
1931da177e4SLinus Torvalds 		tp->rx_opt.ts_recent_stamp = 0;
1941da177e4SLinus Torvalds 		tp->write_seq		   = 0;
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 
197295ff7edSArnaldo Carvalho de Melo 	if (tcp_death_row.sysctl_tw_recycle &&
1981da177e4SLinus Torvalds 	    !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
1991da177e4SLinus Torvalds 		struct inet_peer *peer = rt_get_peer(rt);
2007174259eSArnaldo Carvalho de Melo 		/*
2017174259eSArnaldo Carvalho de Melo 		 * VJ's idea. We save last timestamp seen from
2027174259eSArnaldo Carvalho de Melo 		 * the destination in peer table, when entering state
2037174259eSArnaldo Carvalho de Melo 		 * TIME-WAIT * and initialize rx_opt.ts_recent from it,
2047174259eSArnaldo Carvalho de Melo 		 * when trying new connection.
2051da177e4SLinus Torvalds 		 */
2067174259eSArnaldo Carvalho de Melo 		if (peer != NULL &&
2072c1409a0SEric Dumazet 		    (u32)get_seconds() - peer->tcp_ts_stamp <= TCP_PAWS_MSL) {
2081da177e4SLinus Torvalds 			tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
2091da177e4SLinus Torvalds 			tp->rx_opt.ts_recent = peer->tcp_ts;
2101da177e4SLinus Torvalds 		}
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
213c720c7e8SEric Dumazet 	inet->inet_dport = usin->sin_port;
214c720c7e8SEric Dumazet 	inet->inet_daddr = daddr;
2151da177e4SLinus Torvalds 
216d83d8461SArnaldo Carvalho de Melo 	inet_csk(sk)->icsk_ext_hdr_len = 0;
2171da177e4SLinus Torvalds 	if (inet->opt)
218d83d8461SArnaldo Carvalho de Melo 		inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
2191da177e4SLinus Torvalds 
220bee7ca9eSWilliam Allen Simpson 	tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/* Socket identity is still unknown (sport may be zero).
2231da177e4SLinus Torvalds 	 * However we set state to SYN-SENT and not releasing socket
2241da177e4SLinus Torvalds 	 * lock select source port, enter ourselves into the hash tables and
2251da177e4SLinus Torvalds 	 * complete initialization after this.
2261da177e4SLinus Torvalds 	 */
2271da177e4SLinus Torvalds 	tcp_set_state(sk, TCP_SYN_SENT);
228a7f5e7f1SArnaldo Carvalho de Melo 	err = inet_hash_connect(&tcp_death_row, sk);
2291da177e4SLinus Torvalds 	if (err)
2301da177e4SLinus Torvalds 		goto failure;
2311da177e4SLinus Torvalds 
2327174259eSArnaldo Carvalho de Melo 	err = ip_route_newports(&rt, IPPROTO_TCP,
233c720c7e8SEric Dumazet 				inet->inet_sport, inet->inet_dport, sk);
2341da177e4SLinus Torvalds 	if (err)
2351da177e4SLinus Torvalds 		goto failure;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	/* OK, now commit destination to socket.  */
238bcd76111SHerbert Xu 	sk->sk_gso_type = SKB_GSO_TCPV4;
2396cbb0df7SArnaldo Carvalho de Melo 	sk_setup_caps(sk, &rt->u.dst);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	if (!tp->write_seq)
242c720c7e8SEric Dumazet 		tp->write_seq = secure_tcp_sequence_number(inet->inet_saddr,
243c720c7e8SEric Dumazet 							   inet->inet_daddr,
244c720c7e8SEric Dumazet 							   inet->inet_sport,
2451da177e4SLinus Torvalds 							   usin->sin_port);
2461da177e4SLinus Torvalds 
247c720c7e8SEric Dumazet 	inet->inet_id = tp->write_seq ^ jiffies;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	err = tcp_connect(sk);
2501da177e4SLinus Torvalds 	rt = NULL;
2511da177e4SLinus Torvalds 	if (err)
2521da177e4SLinus Torvalds 		goto failure;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	return 0;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds failure:
2577174259eSArnaldo Carvalho de Melo 	/*
2587174259eSArnaldo Carvalho de Melo 	 * This unhashes the socket and releases the local port,
2597174259eSArnaldo Carvalho de Melo 	 * if necessary.
2607174259eSArnaldo Carvalho de Melo 	 */
2611da177e4SLinus Torvalds 	tcp_set_state(sk, TCP_CLOSE);
2621da177e4SLinus Torvalds 	ip_rt_put(rt);
2631da177e4SLinus Torvalds 	sk->sk_route_caps = 0;
264c720c7e8SEric Dumazet 	inet->inet_dport = 0;
2651da177e4SLinus Torvalds 	return err;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds /*
2691da177e4SLinus Torvalds  * This routine does path mtu discovery as defined in RFC1191.
2701da177e4SLinus Torvalds  */
27140efc6faSStephen Hemminger static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	struct dst_entry *dst;
2741da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
2771da177e4SLinus Torvalds 	 * send out by Linux are always <576bytes so they should go through
2781da177e4SLinus Torvalds 	 * unfragmented).
2791da177e4SLinus Torvalds 	 */
2801da177e4SLinus Torvalds 	if (sk->sk_state == TCP_LISTEN)
2811da177e4SLinus Torvalds 		return;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* We don't check in the destentry if pmtu discovery is forbidden
2841da177e4SLinus Torvalds 	 * on this route. We just assume that no packet_to_big packets
2851da177e4SLinus Torvalds 	 * are send back when pmtu discovery is not active.
2861da177e4SLinus Torvalds 	 * There is a small race when the user changes this flag in the
2871da177e4SLinus Torvalds 	 * route, but I think that's acceptable.
2881da177e4SLinus Torvalds 	 */
2891da177e4SLinus Torvalds 	if ((dst = __sk_dst_check(sk, 0)) == NULL)
2901da177e4SLinus Torvalds 		return;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	dst->ops->update_pmtu(dst, mtu);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	/* Something is about to be wrong... Remember soft error
2951da177e4SLinus Torvalds 	 * for the case, if this connection will not able to recover.
2961da177e4SLinus Torvalds 	 */
2971da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
2981da177e4SLinus Torvalds 		sk->sk_err_soft = EMSGSIZE;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	mtu = dst_mtu(dst);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (inet->pmtudisc != IP_PMTUDISC_DONT &&
303d83d8461SArnaldo Carvalho de Melo 	    inet_csk(sk)->icsk_pmtu_cookie > mtu) {
3041da177e4SLinus Torvalds 		tcp_sync_mss(sk, mtu);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 		/* Resend the TCP packet because it's
3071da177e4SLinus Torvalds 		 * clear that the old packet has been
3081da177e4SLinus Torvalds 		 * dropped. This is the new "fast" path mtu
3091da177e4SLinus Torvalds 		 * discovery.
3101da177e4SLinus Torvalds 		 */
3111da177e4SLinus Torvalds 		tcp_simple_retransmit(sk);
3121da177e4SLinus Torvalds 	} /* else let the usual retransmit timer handle it */
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds /*
3161da177e4SLinus Torvalds  * This routine is called by the ICMP module when it gets some
3171da177e4SLinus Torvalds  * sort of error condition.  If err < 0 then the socket should
3181da177e4SLinus Torvalds  * be closed and the error returned to the user.  If err > 0
3191da177e4SLinus Torvalds  * it's just the icmp type << 8 | icmp code.  After adjustment
3201da177e4SLinus Torvalds  * header points to the first 8 bytes of the tcp header.  We need
3211da177e4SLinus Torvalds  * to find the appropriate port.
3221da177e4SLinus Torvalds  *
3231da177e4SLinus Torvalds  * The locking strategy used here is very "optimistic". When
3241da177e4SLinus Torvalds  * someone else accesses the socket the ICMP is just dropped
3251da177e4SLinus Torvalds  * and for some paths there is no check at all.
3261da177e4SLinus Torvalds  * A more general error queue to queue errors for later handling
3271da177e4SLinus Torvalds  * is probably better.
3281da177e4SLinus Torvalds  *
3291da177e4SLinus Torvalds  */
3301da177e4SLinus Torvalds 
3314d1a2d9eSDamian Lukowski void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
3321da177e4SLinus Torvalds {
3334d1a2d9eSDamian Lukowski 	struct iphdr *iph = (struct iphdr *)icmp_skb->data;
3344d1a2d9eSDamian Lukowski 	struct tcphdr *th = (struct tcphdr *)(icmp_skb->data + (iph->ihl << 2));
335f1ecd5d9SDamian Lukowski 	struct inet_connection_sock *icsk;
3361da177e4SLinus Torvalds 	struct tcp_sock *tp;
3371da177e4SLinus Torvalds 	struct inet_sock *inet;
3384d1a2d9eSDamian Lukowski 	const int type = icmp_hdr(icmp_skb)->type;
3394d1a2d9eSDamian Lukowski 	const int code = icmp_hdr(icmp_skb)->code;
3401da177e4SLinus Torvalds 	struct sock *sk;
341f1ecd5d9SDamian Lukowski 	struct sk_buff *skb;
3421da177e4SLinus Torvalds 	__u32 seq;
343f1ecd5d9SDamian Lukowski 	__u32 remaining;
3441da177e4SLinus Torvalds 	int err;
3454d1a2d9eSDamian Lukowski 	struct net *net = dev_net(icmp_skb->dev);
3461da177e4SLinus Torvalds 
3474d1a2d9eSDamian Lukowski 	if (icmp_skb->len < (iph->ihl << 2) + 8) {
348dcfc23caSPavel Emelyanov 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
3491da177e4SLinus Torvalds 		return;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 
352fd54d716SPavel Emelyanov 	sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest,
3534d1a2d9eSDamian Lukowski 			iph->saddr, th->source, inet_iif(icmp_skb));
3541da177e4SLinus Torvalds 	if (!sk) {
355dcfc23caSPavel Emelyanov 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
3561da177e4SLinus Torvalds 		return;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 	if (sk->sk_state == TCP_TIME_WAIT) {
3599469c7b4SYOSHIFUJI Hideaki 		inet_twsk_put(inet_twsk(sk));
3601da177e4SLinus Torvalds 		return;
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	bh_lock_sock(sk);
3641da177e4SLinus Torvalds 	/* If too many ICMPs get dropped on busy
3651da177e4SLinus Torvalds 	 * servers this needs to be solved differently.
3661da177e4SLinus Torvalds 	 */
3671da177e4SLinus Torvalds 	if (sock_owned_by_user(sk))
368de0744afSPavel Emelyanov 		NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	if (sk->sk_state == TCP_CLOSE)
3711da177e4SLinus Torvalds 		goto out;
3721da177e4SLinus Torvalds 
373f1ecd5d9SDamian Lukowski 	icsk = inet_csk(sk);
3741da177e4SLinus Torvalds 	tp = tcp_sk(sk);
3751da177e4SLinus Torvalds 	seq = ntohl(th->seq);
3761da177e4SLinus Torvalds 	if (sk->sk_state != TCP_LISTEN &&
3771da177e4SLinus Torvalds 	    !between(seq, tp->snd_una, tp->snd_nxt)) {
378de0744afSPavel Emelyanov 		NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
3791da177e4SLinus Torvalds 		goto out;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	switch (type) {
3831da177e4SLinus Torvalds 	case ICMP_SOURCE_QUENCH:
3841da177e4SLinus Torvalds 		/* Just silently ignore these. */
3851da177e4SLinus Torvalds 		goto out;
3861da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
3871da177e4SLinus Torvalds 		err = EPROTO;
3881da177e4SLinus Torvalds 		break;
3891da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
3901da177e4SLinus Torvalds 		if (code > NR_ICMP_UNREACH)
3911da177e4SLinus Torvalds 			goto out;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
3941da177e4SLinus Torvalds 			if (!sock_owned_by_user(sk))
3951da177e4SLinus Torvalds 				do_pmtu_discovery(sk, iph, info);
3961da177e4SLinus Torvalds 			goto out;
3971da177e4SLinus Torvalds 		}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 		err = icmp_err_convert[code].errno;
400f1ecd5d9SDamian Lukowski 		/* check if icmp_skb allows revert of backoff
401f1ecd5d9SDamian Lukowski 		 * (see draft-zimmermann-tcp-lcd) */
402f1ecd5d9SDamian Lukowski 		if (code != ICMP_NET_UNREACH && code != ICMP_HOST_UNREACH)
403f1ecd5d9SDamian Lukowski 			break;
404f1ecd5d9SDamian Lukowski 		if (seq != tp->snd_una  || !icsk->icsk_retransmits ||
405f1ecd5d9SDamian Lukowski 		    !icsk->icsk_backoff)
406f1ecd5d9SDamian Lukowski 			break;
407f1ecd5d9SDamian Lukowski 
408f1ecd5d9SDamian Lukowski 		icsk->icsk_backoff--;
409f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = __tcp_set_rto(tp) <<
410f1ecd5d9SDamian Lukowski 					 icsk->icsk_backoff;
411f1ecd5d9SDamian Lukowski 		tcp_bound_rto(sk);
412f1ecd5d9SDamian Lukowski 
413f1ecd5d9SDamian Lukowski 		skb = tcp_write_queue_head(sk);
414f1ecd5d9SDamian Lukowski 		BUG_ON(!skb);
415f1ecd5d9SDamian Lukowski 
416f1ecd5d9SDamian Lukowski 		remaining = icsk->icsk_rto - min(icsk->icsk_rto,
417f1ecd5d9SDamian Lukowski 				tcp_time_stamp - TCP_SKB_CB(skb)->when);
418f1ecd5d9SDamian Lukowski 
419f1ecd5d9SDamian Lukowski 		if (remaining) {
420f1ecd5d9SDamian Lukowski 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
421f1ecd5d9SDamian Lukowski 						  remaining, TCP_RTO_MAX);
422f1ecd5d9SDamian Lukowski 		} else if (sock_owned_by_user(sk)) {
423f1ecd5d9SDamian Lukowski 			/* RTO revert clocked out retransmission,
424f1ecd5d9SDamian Lukowski 			 * but socket is locked. Will defer. */
425f1ecd5d9SDamian Lukowski 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
426f1ecd5d9SDamian Lukowski 						  HZ/20, TCP_RTO_MAX);
427f1ecd5d9SDamian Lukowski 		} else {
428f1ecd5d9SDamian Lukowski 			/* RTO revert clocked out retransmission.
429f1ecd5d9SDamian Lukowski 			 * Will retransmit now */
430f1ecd5d9SDamian Lukowski 			tcp_retransmit_timer(sk);
431f1ecd5d9SDamian Lukowski 		}
432f1ecd5d9SDamian Lukowski 
4331da177e4SLinus Torvalds 		break;
4341da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
4351da177e4SLinus Torvalds 		err = EHOSTUNREACH;
4361da177e4SLinus Torvalds 		break;
4371da177e4SLinus Torvalds 	default:
4381da177e4SLinus Torvalds 		goto out;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	switch (sk->sk_state) {
44260236fddSArnaldo Carvalho de Melo 		struct request_sock *req, **prev;
4431da177e4SLinus Torvalds 	case TCP_LISTEN:
4441da177e4SLinus Torvalds 		if (sock_owned_by_user(sk))
4451da177e4SLinus Torvalds 			goto out;
4461da177e4SLinus Torvalds 
447463c84b9SArnaldo Carvalho de Melo 		req = inet_csk_search_req(sk, &prev, th->dest,
4481da177e4SLinus Torvalds 					  iph->daddr, iph->saddr);
4491da177e4SLinus Torvalds 		if (!req)
4501da177e4SLinus Torvalds 			goto out;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 		/* ICMPs are not backlogged, hence we cannot get
4531da177e4SLinus Torvalds 		   an established socket here.
4541da177e4SLinus Torvalds 		 */
455547b792cSIlpo Järvinen 		WARN_ON(req->sk);
4561da177e4SLinus Torvalds 
4572e6599cbSArnaldo Carvalho de Melo 		if (seq != tcp_rsk(req)->snt_isn) {
458de0744afSPavel Emelyanov 			NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
4591da177e4SLinus Torvalds 			goto out;
4601da177e4SLinus Torvalds 		}
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 		/*
4631da177e4SLinus Torvalds 		 * Still in SYN_RECV, just remove it silently.
4641da177e4SLinus Torvalds 		 * There is no good way to pass the error to the newly
4651da177e4SLinus Torvalds 		 * created socket, and POSIX does not want network
4661da177e4SLinus Torvalds 		 * errors returned from accept().
4671da177e4SLinus Torvalds 		 */
468463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_drop(sk, req, prev);
4691da177e4SLinus Torvalds 		goto out;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	case TCP_SYN_SENT:
4721da177e4SLinus Torvalds 	case TCP_SYN_RECV:  /* Cannot happen.
4731da177e4SLinus Torvalds 			       It can f.e. if SYNs crossed.
4741da177e4SLinus Torvalds 			     */
4751da177e4SLinus Torvalds 		if (!sock_owned_by_user(sk)) {
4761da177e4SLinus Torvalds 			sk->sk_err = err;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 			sk->sk_error_report(sk);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 			tcp_done(sk);
4811da177e4SLinus Torvalds 		} else {
4821da177e4SLinus Torvalds 			sk->sk_err_soft = err;
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 		goto out;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/* If we've already connected we will keep trying
4881da177e4SLinus Torvalds 	 * until we time out, or the user gives up.
4891da177e4SLinus Torvalds 	 *
4901da177e4SLinus Torvalds 	 * rfc1122 4.2.3.9 allows to consider as hard errors
4911da177e4SLinus Torvalds 	 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
4921da177e4SLinus Torvalds 	 * but it is obsoleted by pmtu discovery).
4931da177e4SLinus Torvalds 	 *
4941da177e4SLinus Torvalds 	 * Note, that in modern internet, where routing is unreliable
4951da177e4SLinus Torvalds 	 * and in each dark corner broken firewalls sit, sending random
4961da177e4SLinus Torvalds 	 * errors ordered by their masters even this two messages finally lose
4971da177e4SLinus Torvalds 	 * their original sense (even Linux sends invalid PORT_UNREACHs)
4981da177e4SLinus Torvalds 	 *
4991da177e4SLinus Torvalds 	 * Now we are in compliance with RFCs.
5001da177e4SLinus Torvalds 	 *							--ANK (980905)
5011da177e4SLinus Torvalds 	 */
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	inet = inet_sk(sk);
5041da177e4SLinus Torvalds 	if (!sock_owned_by_user(sk) && inet->recverr) {
5051da177e4SLinus Torvalds 		sk->sk_err = err;
5061da177e4SLinus Torvalds 		sk->sk_error_report(sk);
5071da177e4SLinus Torvalds 	} else	{ /* Only an error on timeout */
5081da177e4SLinus Torvalds 		sk->sk_err_soft = err;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds out:
5121da177e4SLinus Torvalds 	bh_unlock_sock(sk);
5131da177e4SLinus Torvalds 	sock_put(sk);
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /* This routine computes an IPv4 TCP checksum. */
5178292a17aSArnaldo Carvalho de Melo void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
520aa8223c7SArnaldo Carvalho de Melo 	struct tcphdr *th = tcp_hdr(skb);
5211da177e4SLinus Torvalds 
52284fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
523c720c7e8SEric Dumazet 		th->check = ~tcp_v4_check(len, inet->inet_saddr,
524c720c7e8SEric Dumazet 					  inet->inet_daddr, 0);
525663ead3bSHerbert Xu 		skb->csum_start = skb_transport_header(skb) - skb->head;
526ff1dcadbSAl Viro 		skb->csum_offset = offsetof(struct tcphdr, check);
5271da177e4SLinus Torvalds 	} else {
528c720c7e8SEric Dumazet 		th->check = tcp_v4_check(len, inet->inet_saddr,
529c720c7e8SEric Dumazet 					 inet->inet_daddr,
53007f0757aSJoe Perches 					 csum_partial(th,
5311da177e4SLinus Torvalds 						      th->doff << 2,
5321da177e4SLinus Torvalds 						      skb->csum));
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
536a430a43dSHerbert Xu int tcp_v4_gso_send_check(struct sk_buff *skb)
537a430a43dSHerbert Xu {
538eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph;
539a430a43dSHerbert Xu 	struct tcphdr *th;
540a430a43dSHerbert Xu 
541a430a43dSHerbert Xu 	if (!pskb_may_pull(skb, sizeof(*th)))
542a430a43dSHerbert Xu 		return -EINVAL;
543a430a43dSHerbert Xu 
544eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
545aa8223c7SArnaldo Carvalho de Melo 	th = tcp_hdr(skb);
546a430a43dSHerbert Xu 
547a430a43dSHerbert Xu 	th->check = 0;
548ba7808eaSFrederik Deweerdt 	th->check = ~tcp_v4_check(skb->len, iph->saddr, iph->daddr, 0);
549663ead3bSHerbert Xu 	skb->csum_start = skb_transport_header(skb) - skb->head;
550ff1dcadbSAl Viro 	skb->csum_offset = offsetof(struct tcphdr, check);
55184fa7933SPatrick McHardy 	skb->ip_summed = CHECKSUM_PARTIAL;
552a430a43dSHerbert Xu 	return 0;
553a430a43dSHerbert Xu }
554a430a43dSHerbert Xu 
5551da177e4SLinus Torvalds /*
5561da177e4SLinus Torvalds  *	This routine will send an RST to the other tcp.
5571da177e4SLinus Torvalds  *
5581da177e4SLinus Torvalds  *	Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
5591da177e4SLinus Torvalds  *		      for reset.
5601da177e4SLinus Torvalds  *	Answer: if a packet caused RST, it is not for a socket
5611da177e4SLinus Torvalds  *		existing in our system, if it is matched to a socket,
5621da177e4SLinus Torvalds  *		it is just duplicate segment or bug in other side's TCP.
5631da177e4SLinus Torvalds  *		So that we build reply only basing on parameters
5641da177e4SLinus Torvalds  *		arrived with segment.
5651da177e4SLinus Torvalds  *	Exception: precedence violation. We do not implement it in any case.
5661da177e4SLinus Torvalds  */
5671da177e4SLinus Torvalds 
568cfb6eeb4SYOSHIFUJI Hideaki static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
5691da177e4SLinus Torvalds {
570aa8223c7SArnaldo Carvalho de Melo 	struct tcphdr *th = tcp_hdr(skb);
571cfb6eeb4SYOSHIFUJI Hideaki 	struct {
572cfb6eeb4SYOSHIFUJI Hideaki 		struct tcphdr th;
573cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
574714e85beSAl Viro 		__be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
575cfb6eeb4SYOSHIFUJI Hideaki #endif
576cfb6eeb4SYOSHIFUJI Hideaki 	} rep;
5771da177e4SLinus Torvalds 	struct ip_reply_arg arg;
578cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
579cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key *key;
580cfb6eeb4SYOSHIFUJI Hideaki #endif
581a86b1e30SPavel Emelyanov 	struct net *net;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	/* Never send a reset in response to a reset. */
5841da177e4SLinus Torvalds 	if (th->rst)
5851da177e4SLinus Torvalds 		return;
5861da177e4SLinus Torvalds 
587511c3f92SEric Dumazet 	if (skb_rtable(skb)->rt_type != RTN_LOCAL)
5881da177e4SLinus Torvalds 		return;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/* Swap the send and the receive. */
591cfb6eeb4SYOSHIFUJI Hideaki 	memset(&rep, 0, sizeof(rep));
592cfb6eeb4SYOSHIFUJI Hideaki 	rep.th.dest   = th->source;
593cfb6eeb4SYOSHIFUJI Hideaki 	rep.th.source = th->dest;
594cfb6eeb4SYOSHIFUJI Hideaki 	rep.th.doff   = sizeof(struct tcphdr) / 4;
595cfb6eeb4SYOSHIFUJI Hideaki 	rep.th.rst    = 1;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	if (th->ack) {
598cfb6eeb4SYOSHIFUJI Hideaki 		rep.th.seq = th->ack_seq;
5991da177e4SLinus Torvalds 	} else {
600cfb6eeb4SYOSHIFUJI Hideaki 		rep.th.ack = 1;
601cfb6eeb4SYOSHIFUJI Hideaki 		rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
6021da177e4SLinus Torvalds 				       skb->len - (th->doff << 2));
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6057174259eSArnaldo Carvalho de Melo 	memset(&arg, 0, sizeof(arg));
606cfb6eeb4SYOSHIFUJI Hideaki 	arg.iov[0].iov_base = (unsigned char *)&rep;
607cfb6eeb4SYOSHIFUJI Hideaki 	arg.iov[0].iov_len  = sizeof(rep.th);
608cfb6eeb4SYOSHIFUJI Hideaki 
609cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
610eddc9ec5SArnaldo Carvalho de Melo 	key = sk ? tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr) : NULL;
611cfb6eeb4SYOSHIFUJI Hideaki 	if (key) {
612cfb6eeb4SYOSHIFUJI Hideaki 		rep.opt[0] = htonl((TCPOPT_NOP << 24) |
613cfb6eeb4SYOSHIFUJI Hideaki 				   (TCPOPT_NOP << 16) |
614cfb6eeb4SYOSHIFUJI Hideaki 				   (TCPOPT_MD5SIG << 8) |
615cfb6eeb4SYOSHIFUJI Hideaki 				   TCPOLEN_MD5SIG);
616cfb6eeb4SYOSHIFUJI Hideaki 		/* Update length and the length the header thinks exists */
617cfb6eeb4SYOSHIFUJI Hideaki 		arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
618cfb6eeb4SYOSHIFUJI Hideaki 		rep.th.doff = arg.iov[0].iov_len / 4;
619cfb6eeb4SYOSHIFUJI Hideaki 
62049a72dfbSAdam Langley 		tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
62178e645cbSIlpo Järvinen 				     key, ip_hdr(skb)->saddr,
62278e645cbSIlpo Järvinen 				     ip_hdr(skb)->daddr, &rep.th);
623cfb6eeb4SYOSHIFUJI Hideaki 	}
624cfb6eeb4SYOSHIFUJI Hideaki #endif
625eddc9ec5SArnaldo Carvalho de Melo 	arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
626eddc9ec5SArnaldo Carvalho de Melo 				      ip_hdr(skb)->saddr, /* XXX */
62752cd5750SIlpo Järvinen 				      arg.iov[0].iov_len, IPPROTO_TCP, 0);
6281da177e4SLinus Torvalds 	arg.csumoffset = offsetof(struct tcphdr, check) / 2;
62988ef4a5aSKOVACS Krisztian 	arg.flags = (sk && inet_sk(sk)->transparent) ? IP_REPLY_ARG_NOSRCCHECK : 0;
6301da177e4SLinus Torvalds 
631adf30907SEric Dumazet 	net = dev_net(skb_dst(skb)->dev);
632a86b1e30SPavel Emelyanov 	ip_send_reply(net->ipv4.tcp_sock, skb,
6337feb49c8SDenis V. Lunev 		      &arg, arg.iov[0].iov_len);
6341da177e4SLinus Torvalds 
63563231bddSPavel Emelyanov 	TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
63663231bddSPavel Emelyanov 	TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
6401da177e4SLinus Torvalds    outside socket context is ugly, certainly. What can I do?
6411da177e4SLinus Torvalds  */
6421da177e4SLinus Torvalds 
6439501f972SYOSHIFUJI Hideaki static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
6449501f972SYOSHIFUJI Hideaki 			    u32 win, u32 ts, int oif,
64588ef4a5aSKOVACS Krisztian 			    struct tcp_md5sig_key *key,
64688ef4a5aSKOVACS Krisztian 			    int reply_flags)
6471da177e4SLinus Torvalds {
648aa8223c7SArnaldo Carvalho de Melo 	struct tcphdr *th = tcp_hdr(skb);
6491da177e4SLinus Torvalds 	struct {
6501da177e4SLinus Torvalds 		struct tcphdr th;
651714e85beSAl Viro 		__be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
652cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
653cfb6eeb4SYOSHIFUJI Hideaki 			   + (TCPOLEN_MD5SIG_ALIGNED >> 2)
654cfb6eeb4SYOSHIFUJI Hideaki #endif
655cfb6eeb4SYOSHIFUJI Hideaki 			];
6561da177e4SLinus Torvalds 	} rep;
6571da177e4SLinus Torvalds 	struct ip_reply_arg arg;
658adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	memset(&rep.th, 0, sizeof(struct tcphdr));
6617174259eSArnaldo Carvalho de Melo 	memset(&arg, 0, sizeof(arg));
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	arg.iov[0].iov_base = (unsigned char *)&rep;
6641da177e4SLinus Torvalds 	arg.iov[0].iov_len  = sizeof(rep.th);
6651da177e4SLinus Torvalds 	if (ts) {
666cfb6eeb4SYOSHIFUJI Hideaki 		rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
6671da177e4SLinus Torvalds 				   (TCPOPT_TIMESTAMP << 8) |
6681da177e4SLinus Torvalds 				   TCPOLEN_TIMESTAMP);
669cfb6eeb4SYOSHIFUJI Hideaki 		rep.opt[1] = htonl(tcp_time_stamp);
670cfb6eeb4SYOSHIFUJI Hideaki 		rep.opt[2] = htonl(ts);
671cb48cfe8SCraig Schlenter 		arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
6721da177e4SLinus Torvalds 	}
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	/* Swap the send and the receive. */
6751da177e4SLinus Torvalds 	rep.th.dest    = th->source;
6761da177e4SLinus Torvalds 	rep.th.source  = th->dest;
6771da177e4SLinus Torvalds 	rep.th.doff    = arg.iov[0].iov_len / 4;
6781da177e4SLinus Torvalds 	rep.th.seq     = htonl(seq);
6791da177e4SLinus Torvalds 	rep.th.ack_seq = htonl(ack);
6801da177e4SLinus Torvalds 	rep.th.ack     = 1;
6811da177e4SLinus Torvalds 	rep.th.window  = htons(win);
6821da177e4SLinus Torvalds 
683cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
684cfb6eeb4SYOSHIFUJI Hideaki 	if (key) {
685cfb6eeb4SYOSHIFUJI Hideaki 		int offset = (ts) ? 3 : 0;
686cfb6eeb4SYOSHIFUJI Hideaki 
687cfb6eeb4SYOSHIFUJI Hideaki 		rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
688cfb6eeb4SYOSHIFUJI Hideaki 					  (TCPOPT_NOP << 16) |
689cfb6eeb4SYOSHIFUJI Hideaki 					  (TCPOPT_MD5SIG << 8) |
690cfb6eeb4SYOSHIFUJI Hideaki 					  TCPOLEN_MD5SIG);
691cfb6eeb4SYOSHIFUJI Hideaki 		arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
692cfb6eeb4SYOSHIFUJI Hideaki 		rep.th.doff = arg.iov[0].iov_len/4;
693cfb6eeb4SYOSHIFUJI Hideaki 
69449a72dfbSAdam Langley 		tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset],
69590b7e112SAdam Langley 				    key, ip_hdr(skb)->saddr,
69690b7e112SAdam Langley 				    ip_hdr(skb)->daddr, &rep.th);
697cfb6eeb4SYOSHIFUJI Hideaki 	}
698cfb6eeb4SYOSHIFUJI Hideaki #endif
69988ef4a5aSKOVACS Krisztian 	arg.flags = reply_flags;
700eddc9ec5SArnaldo Carvalho de Melo 	arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
701eddc9ec5SArnaldo Carvalho de Melo 				      ip_hdr(skb)->saddr, /* XXX */
7021da177e4SLinus Torvalds 				      arg.iov[0].iov_len, IPPROTO_TCP, 0);
7031da177e4SLinus Torvalds 	arg.csumoffset = offsetof(struct tcphdr, check) / 2;
7049501f972SYOSHIFUJI Hideaki 	if (oif)
7059501f972SYOSHIFUJI Hideaki 		arg.bound_dev_if = oif;
7061da177e4SLinus Torvalds 
707a86b1e30SPavel Emelyanov 	ip_send_reply(net->ipv4.tcp_sock, skb,
7087feb49c8SDenis V. Lunev 		      &arg, arg.iov[0].iov_len);
7091da177e4SLinus Torvalds 
71063231bddSPavel Emelyanov 	TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
7141da177e4SLinus Torvalds {
7158feaf0c0SArnaldo Carvalho de Melo 	struct inet_timewait_sock *tw = inet_twsk(sk);
716cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
7171da177e4SLinus Torvalds 
7189501f972SYOSHIFUJI Hideaki 	tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
7197174259eSArnaldo Carvalho de Melo 			tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
7209501f972SYOSHIFUJI Hideaki 			tcptw->tw_ts_recent,
7219501f972SYOSHIFUJI Hideaki 			tw->tw_bound_dev_if,
72288ef4a5aSKOVACS Krisztian 			tcp_twsk_md5_key(tcptw),
72388ef4a5aSKOVACS Krisztian 			tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0
7249501f972SYOSHIFUJI Hideaki 			);
7251da177e4SLinus Torvalds 
7268feaf0c0SArnaldo Carvalho de Melo 	inet_twsk_put(tw);
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7296edafaafSGui Jianfeng static void tcp_v4_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
7307174259eSArnaldo Carvalho de Melo 				  struct request_sock *req)
7311da177e4SLinus Torvalds {
7329501f972SYOSHIFUJI Hideaki 	tcp_v4_send_ack(skb, tcp_rsk(req)->snt_isn + 1,
733cfb6eeb4SYOSHIFUJI Hideaki 			tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
7349501f972SYOSHIFUJI Hideaki 			req->ts_recent,
7359501f972SYOSHIFUJI Hideaki 			0,
73688ef4a5aSKOVACS Krisztian 			tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr),
73788ef4a5aSKOVACS Krisztian 			inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0);
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /*
7419bf1d83eSKris Katterjohn  *	Send a SYN-ACK after having received a SYN.
74260236fddSArnaldo Carvalho de Melo  *	This still operates on a request_sock only, not on a big
7431da177e4SLinus Torvalds  *	socket.
7441da177e4SLinus Torvalds  */
745e6b4d113SWilliam Allen Simpson static int __tcp_v4_send_synack(struct sock *sk, struct dst_entry *dst,
746e6b4d113SWilliam Allen Simpson 				struct request_sock *req,
747e6b4d113SWilliam Allen Simpson 				struct request_values *rvp)
7481da177e4SLinus Torvalds {
7492e6599cbSArnaldo Carvalho de Melo 	const struct inet_request_sock *ireq = inet_rsk(req);
7501da177e4SLinus Torvalds 	int err = -1;
7511da177e4SLinus Torvalds 	struct sk_buff * skb;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	/* First, grab a route. */
754463c84b9SArnaldo Carvalho de Melo 	if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
755fd80eb94SDenis V. Lunev 		return -1;
7561da177e4SLinus Torvalds 
757e6b4d113SWilliam Allen Simpson 	skb = tcp_make_synack(sk, dst, req, rvp);
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	if (skb) {
760aa8223c7SArnaldo Carvalho de Melo 		struct tcphdr *th = tcp_hdr(skb);
7611da177e4SLinus Torvalds 
762ba7808eaSFrederik Deweerdt 		th->check = tcp_v4_check(skb->len,
7632e6599cbSArnaldo Carvalho de Melo 					 ireq->loc_addr,
7642e6599cbSArnaldo Carvalho de Melo 					 ireq->rmt_addr,
76507f0757aSJoe Perches 					 csum_partial(th, skb->len,
7661da177e4SLinus Torvalds 						      skb->csum));
7671da177e4SLinus Torvalds 
7682e6599cbSArnaldo Carvalho de Melo 		err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
7692e6599cbSArnaldo Carvalho de Melo 					    ireq->rmt_addr,
7702e6599cbSArnaldo Carvalho de Melo 					    ireq->opt);
771b9df3cb8SGerrit Renker 		err = net_xmit_eval(err);
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	dst_release(dst);
7751da177e4SLinus Torvalds 	return err;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
778e6b4d113SWilliam Allen Simpson static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
779e6b4d113SWilliam Allen Simpson 			      struct request_values *rvp)
780fd80eb94SDenis V. Lunev {
781e6b4d113SWilliam Allen Simpson 	return __tcp_v4_send_synack(sk, NULL, req, rvp);
782fd80eb94SDenis V. Lunev }
783fd80eb94SDenis V. Lunev 
7841da177e4SLinus Torvalds /*
78560236fddSArnaldo Carvalho de Melo  *	IPv4 request_sock destructor.
7861da177e4SLinus Torvalds  */
78760236fddSArnaldo Carvalho de Melo static void tcp_v4_reqsk_destructor(struct request_sock *req)
7881da177e4SLinus Torvalds {
7892e6599cbSArnaldo Carvalho de Melo 	kfree(inet_rsk(req)->opt);
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
79280e40daaSArnaldo Carvalho de Melo #ifdef CONFIG_SYN_COOKIES
79340efc6faSStephen Hemminger static void syn_flood_warning(struct sk_buff *skb)
7941da177e4SLinus Torvalds {
7951da177e4SLinus Torvalds 	static unsigned long warntime;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	if (time_after(jiffies, (warntime + HZ * 60))) {
7981da177e4SLinus Torvalds 		warntime = jiffies;
7991da177e4SLinus Torvalds 		printk(KERN_INFO
8001da177e4SLinus Torvalds 		       "possible SYN flooding on port %d. Sending cookies.\n",
801aa8223c7SArnaldo Carvalho de Melo 		       ntohs(tcp_hdr(skb)->dest));
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds }
80480e40daaSArnaldo Carvalho de Melo #endif
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds /*
80760236fddSArnaldo Carvalho de Melo  * Save and compile IPv4 options into the request_sock if needed.
8081da177e4SLinus Torvalds  */
80940efc6faSStephen Hemminger static struct ip_options *tcp_v4_save_options(struct sock *sk,
8101da177e4SLinus Torvalds 					      struct sk_buff *skb)
8111da177e4SLinus Torvalds {
8121da177e4SLinus Torvalds 	struct ip_options *opt = &(IPCB(skb)->opt);
8131da177e4SLinus Torvalds 	struct ip_options *dopt = NULL;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	if (opt && opt->optlen) {
8161da177e4SLinus Torvalds 		int opt_size = optlength(opt);
8171da177e4SLinus Torvalds 		dopt = kmalloc(opt_size, GFP_ATOMIC);
8181da177e4SLinus Torvalds 		if (dopt) {
8191da177e4SLinus Torvalds 			if (ip_options_echo(dopt, skb)) {
8201da177e4SLinus Torvalds 				kfree(dopt);
8211da177e4SLinus Torvalds 				dopt = NULL;
8221da177e4SLinus Torvalds 			}
8231da177e4SLinus Torvalds 		}
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds 	return dopt;
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
828cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
829cfb6eeb4SYOSHIFUJI Hideaki /*
830cfb6eeb4SYOSHIFUJI Hideaki  * RFC2385 MD5 checksumming requires a mapping of
831cfb6eeb4SYOSHIFUJI Hideaki  * IP address->MD5 Key.
832cfb6eeb4SYOSHIFUJI Hideaki  * We need to maintain these in the sk structure.
833cfb6eeb4SYOSHIFUJI Hideaki  */
834cfb6eeb4SYOSHIFUJI Hideaki 
835cfb6eeb4SYOSHIFUJI Hideaki /* Find the Key structure for an address.  */
8367174259eSArnaldo Carvalho de Melo static struct tcp_md5sig_key *
8377174259eSArnaldo Carvalho de Melo 			tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
838cfb6eeb4SYOSHIFUJI Hideaki {
839cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_sock *tp = tcp_sk(sk);
840cfb6eeb4SYOSHIFUJI Hideaki 	int i;
841cfb6eeb4SYOSHIFUJI Hideaki 
842cfb6eeb4SYOSHIFUJI Hideaki 	if (!tp->md5sig_info || !tp->md5sig_info->entries4)
843cfb6eeb4SYOSHIFUJI Hideaki 		return NULL;
844cfb6eeb4SYOSHIFUJI Hideaki 	for (i = 0; i < tp->md5sig_info->entries4; i++) {
845cfb6eeb4SYOSHIFUJI Hideaki 		if (tp->md5sig_info->keys4[i].addr == addr)
846f8ab18d2SDavid S. Miller 			return &tp->md5sig_info->keys4[i].base;
847cfb6eeb4SYOSHIFUJI Hideaki 	}
848cfb6eeb4SYOSHIFUJI Hideaki 	return NULL;
849cfb6eeb4SYOSHIFUJI Hideaki }
850cfb6eeb4SYOSHIFUJI Hideaki 
851cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
852cfb6eeb4SYOSHIFUJI Hideaki 					 struct sock *addr_sk)
853cfb6eeb4SYOSHIFUJI Hideaki {
854c720c7e8SEric Dumazet 	return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->inet_daddr);
855cfb6eeb4SYOSHIFUJI Hideaki }
856cfb6eeb4SYOSHIFUJI Hideaki 
857cfb6eeb4SYOSHIFUJI Hideaki EXPORT_SYMBOL(tcp_v4_md5_lookup);
858cfb6eeb4SYOSHIFUJI Hideaki 
859f5b99bcdSAdrian Bunk static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
860cfb6eeb4SYOSHIFUJI Hideaki 						      struct request_sock *req)
861cfb6eeb4SYOSHIFUJI Hideaki {
862cfb6eeb4SYOSHIFUJI Hideaki 	return tcp_v4_md5_do_lookup(sk, inet_rsk(req)->rmt_addr);
863cfb6eeb4SYOSHIFUJI Hideaki }
864cfb6eeb4SYOSHIFUJI Hideaki 
865cfb6eeb4SYOSHIFUJI Hideaki /* This can be called on a newly created socket, from other files */
866cfb6eeb4SYOSHIFUJI Hideaki int tcp_v4_md5_do_add(struct sock *sk, __be32 addr,
867cfb6eeb4SYOSHIFUJI Hideaki 		      u8 *newkey, u8 newkeylen)
868cfb6eeb4SYOSHIFUJI Hideaki {
869cfb6eeb4SYOSHIFUJI Hideaki 	/* Add Key to the list */
870b0a713e9SMatthias M. Dellweg 	struct tcp_md5sig_key *key;
871cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_sock *tp = tcp_sk(sk);
872cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_md5sig_key *keys;
873cfb6eeb4SYOSHIFUJI Hideaki 
874b0a713e9SMatthias M. Dellweg 	key = tcp_v4_md5_do_lookup(sk, addr);
875cfb6eeb4SYOSHIFUJI Hideaki 	if (key) {
876cfb6eeb4SYOSHIFUJI Hideaki 		/* Pre-existing entry - just update that one. */
877b0a713e9SMatthias M. Dellweg 		kfree(key->key);
878b0a713e9SMatthias M. Dellweg 		key->key = newkey;
879b0a713e9SMatthias M. Dellweg 		key->keylen = newkeylen;
880cfb6eeb4SYOSHIFUJI Hideaki 	} else {
881f6685938SArnaldo Carvalho de Melo 		struct tcp_md5sig_info *md5sig;
882f6685938SArnaldo Carvalho de Melo 
883cfb6eeb4SYOSHIFUJI Hideaki 		if (!tp->md5sig_info) {
884f6685938SArnaldo Carvalho de Melo 			tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info),
885f6685938SArnaldo Carvalho de Melo 						  GFP_ATOMIC);
886cfb6eeb4SYOSHIFUJI Hideaki 			if (!tp->md5sig_info) {
887cfb6eeb4SYOSHIFUJI Hideaki 				kfree(newkey);
888cfb6eeb4SYOSHIFUJI Hideaki 				return -ENOMEM;
889cfb6eeb4SYOSHIFUJI Hideaki 			}
8903d7dbeacSDavid S. Miller 			sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
891cfb6eeb4SYOSHIFUJI Hideaki 		}
892aa133076SWu Fengguang 		if (tcp_alloc_md5sig_pool(sk) == NULL) {
893cfb6eeb4SYOSHIFUJI Hideaki 			kfree(newkey);
894cfb6eeb4SYOSHIFUJI Hideaki 			return -ENOMEM;
895cfb6eeb4SYOSHIFUJI Hideaki 		}
896f6685938SArnaldo Carvalho de Melo 		md5sig = tp->md5sig_info;
897f6685938SArnaldo Carvalho de Melo 
898f6685938SArnaldo Carvalho de Melo 		if (md5sig->alloced4 == md5sig->entries4) {
899f6685938SArnaldo Carvalho de Melo 			keys = kmalloc((sizeof(*keys) *
900f6685938SArnaldo Carvalho de Melo 					(md5sig->entries4 + 1)), GFP_ATOMIC);
901cfb6eeb4SYOSHIFUJI Hideaki 			if (!keys) {
902cfb6eeb4SYOSHIFUJI Hideaki 				kfree(newkey);
903cfb6eeb4SYOSHIFUJI Hideaki 				tcp_free_md5sig_pool();
904cfb6eeb4SYOSHIFUJI Hideaki 				return -ENOMEM;
905cfb6eeb4SYOSHIFUJI Hideaki 			}
906cfb6eeb4SYOSHIFUJI Hideaki 
907f6685938SArnaldo Carvalho de Melo 			if (md5sig->entries4)
908f6685938SArnaldo Carvalho de Melo 				memcpy(keys, md5sig->keys4,
909f6685938SArnaldo Carvalho de Melo 				       sizeof(*keys) * md5sig->entries4);
910cfb6eeb4SYOSHIFUJI Hideaki 
911cfb6eeb4SYOSHIFUJI Hideaki 			/* Free old key list, and reference new one */
912f6685938SArnaldo Carvalho de Melo 			kfree(md5sig->keys4);
913f6685938SArnaldo Carvalho de Melo 			md5sig->keys4 = keys;
914f6685938SArnaldo Carvalho de Melo 			md5sig->alloced4++;
915cfb6eeb4SYOSHIFUJI Hideaki 		}
916f6685938SArnaldo Carvalho de Melo 		md5sig->entries4++;
917f6685938SArnaldo Carvalho de Melo 		md5sig->keys4[md5sig->entries4 - 1].addr        = addr;
918f8ab18d2SDavid S. Miller 		md5sig->keys4[md5sig->entries4 - 1].base.key    = newkey;
919f8ab18d2SDavid S. Miller 		md5sig->keys4[md5sig->entries4 - 1].base.keylen = newkeylen;
920cfb6eeb4SYOSHIFUJI Hideaki 	}
921cfb6eeb4SYOSHIFUJI Hideaki 	return 0;
922cfb6eeb4SYOSHIFUJI Hideaki }
923cfb6eeb4SYOSHIFUJI Hideaki 
924cfb6eeb4SYOSHIFUJI Hideaki EXPORT_SYMBOL(tcp_v4_md5_do_add);
925cfb6eeb4SYOSHIFUJI Hideaki 
926cfb6eeb4SYOSHIFUJI Hideaki static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk,
927cfb6eeb4SYOSHIFUJI Hideaki 			       u8 *newkey, u8 newkeylen)
928cfb6eeb4SYOSHIFUJI Hideaki {
929c720c7e8SEric Dumazet 	return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->inet_daddr,
930cfb6eeb4SYOSHIFUJI Hideaki 				 newkey, newkeylen);
931cfb6eeb4SYOSHIFUJI Hideaki }
932cfb6eeb4SYOSHIFUJI Hideaki 
933cfb6eeb4SYOSHIFUJI Hideaki int tcp_v4_md5_do_del(struct sock *sk, __be32 addr)
934cfb6eeb4SYOSHIFUJI Hideaki {
935cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_sock *tp = tcp_sk(sk);
936cfb6eeb4SYOSHIFUJI Hideaki 	int i;
937cfb6eeb4SYOSHIFUJI Hideaki 
938cfb6eeb4SYOSHIFUJI Hideaki 	for (i = 0; i < tp->md5sig_info->entries4; i++) {
939cfb6eeb4SYOSHIFUJI Hideaki 		if (tp->md5sig_info->keys4[i].addr == addr) {
940cfb6eeb4SYOSHIFUJI Hideaki 			/* Free the key */
941f8ab18d2SDavid S. Miller 			kfree(tp->md5sig_info->keys4[i].base.key);
942cfb6eeb4SYOSHIFUJI Hideaki 			tp->md5sig_info->entries4--;
943cfb6eeb4SYOSHIFUJI Hideaki 
944cfb6eeb4SYOSHIFUJI Hideaki 			if (tp->md5sig_info->entries4 == 0) {
945cfb6eeb4SYOSHIFUJI Hideaki 				kfree(tp->md5sig_info->keys4);
946cfb6eeb4SYOSHIFUJI Hideaki 				tp->md5sig_info->keys4 = NULL;
9478228a18dSLeigh Brown 				tp->md5sig_info->alloced4 = 0;
9487174259eSArnaldo Carvalho de Melo 			} else if (tp->md5sig_info->entries4 != i) {
949cfb6eeb4SYOSHIFUJI Hideaki 				/* Need to do some manipulation */
950354faf09SYOSHIFUJI Hideaki 				memmove(&tp->md5sig_info->keys4[i],
951cfb6eeb4SYOSHIFUJI Hideaki 					&tp->md5sig_info->keys4[i+1],
9527174259eSArnaldo Carvalho de Melo 					(tp->md5sig_info->entries4 - i) *
9537174259eSArnaldo Carvalho de Melo 					 sizeof(struct tcp4_md5sig_key));
954cfb6eeb4SYOSHIFUJI Hideaki 			}
955cfb6eeb4SYOSHIFUJI Hideaki 			tcp_free_md5sig_pool();
956cfb6eeb4SYOSHIFUJI Hideaki 			return 0;
957cfb6eeb4SYOSHIFUJI Hideaki 		}
958cfb6eeb4SYOSHIFUJI Hideaki 	}
959cfb6eeb4SYOSHIFUJI Hideaki 	return -ENOENT;
960cfb6eeb4SYOSHIFUJI Hideaki }
961cfb6eeb4SYOSHIFUJI Hideaki 
962cfb6eeb4SYOSHIFUJI Hideaki EXPORT_SYMBOL(tcp_v4_md5_do_del);
963cfb6eeb4SYOSHIFUJI Hideaki 
964cfb6eeb4SYOSHIFUJI Hideaki static void tcp_v4_clear_md5_list(struct sock *sk)
965cfb6eeb4SYOSHIFUJI Hideaki {
966cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_sock *tp = tcp_sk(sk);
967cfb6eeb4SYOSHIFUJI Hideaki 
968cfb6eeb4SYOSHIFUJI Hideaki 	/* Free each key, then the set of key keys,
969cfb6eeb4SYOSHIFUJI Hideaki 	 * the crypto element, and then decrement our
970cfb6eeb4SYOSHIFUJI Hideaki 	 * hold on the last resort crypto.
971cfb6eeb4SYOSHIFUJI Hideaki 	 */
972cfb6eeb4SYOSHIFUJI Hideaki 	if (tp->md5sig_info->entries4) {
973cfb6eeb4SYOSHIFUJI Hideaki 		int i;
974cfb6eeb4SYOSHIFUJI Hideaki 		for (i = 0; i < tp->md5sig_info->entries4; i++)
975f8ab18d2SDavid S. Miller 			kfree(tp->md5sig_info->keys4[i].base.key);
976cfb6eeb4SYOSHIFUJI Hideaki 		tp->md5sig_info->entries4 = 0;
977cfb6eeb4SYOSHIFUJI Hideaki 		tcp_free_md5sig_pool();
978cfb6eeb4SYOSHIFUJI Hideaki 	}
979cfb6eeb4SYOSHIFUJI Hideaki 	if (tp->md5sig_info->keys4) {
980cfb6eeb4SYOSHIFUJI Hideaki 		kfree(tp->md5sig_info->keys4);
981cfb6eeb4SYOSHIFUJI Hideaki 		tp->md5sig_info->keys4 = NULL;
982cfb6eeb4SYOSHIFUJI Hideaki 		tp->md5sig_info->alloced4  = 0;
983cfb6eeb4SYOSHIFUJI Hideaki 	}
984cfb6eeb4SYOSHIFUJI Hideaki }
985cfb6eeb4SYOSHIFUJI Hideaki 
986cfb6eeb4SYOSHIFUJI Hideaki static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
987cfb6eeb4SYOSHIFUJI Hideaki 				 int optlen)
988cfb6eeb4SYOSHIFUJI Hideaki {
989cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig cmd;
990cfb6eeb4SYOSHIFUJI Hideaki 	struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
991cfb6eeb4SYOSHIFUJI Hideaki 	u8 *newkey;
992cfb6eeb4SYOSHIFUJI Hideaki 
993cfb6eeb4SYOSHIFUJI Hideaki 	if (optlen < sizeof(cmd))
994cfb6eeb4SYOSHIFUJI Hideaki 		return -EINVAL;
995cfb6eeb4SYOSHIFUJI Hideaki 
996cfb6eeb4SYOSHIFUJI Hideaki 	if (copy_from_user(&cmd, optval, sizeof(cmd)))
997cfb6eeb4SYOSHIFUJI Hideaki 		return -EFAULT;
998cfb6eeb4SYOSHIFUJI Hideaki 
999cfb6eeb4SYOSHIFUJI Hideaki 	if (sin->sin_family != AF_INET)
1000cfb6eeb4SYOSHIFUJI Hideaki 		return -EINVAL;
1001cfb6eeb4SYOSHIFUJI Hideaki 
1002cfb6eeb4SYOSHIFUJI Hideaki 	if (!cmd.tcpm_key || !cmd.tcpm_keylen) {
1003cfb6eeb4SYOSHIFUJI Hideaki 		if (!tcp_sk(sk)->md5sig_info)
1004cfb6eeb4SYOSHIFUJI Hideaki 			return -ENOENT;
1005cfb6eeb4SYOSHIFUJI Hideaki 		return tcp_v4_md5_do_del(sk, sin->sin_addr.s_addr);
1006cfb6eeb4SYOSHIFUJI Hideaki 	}
1007cfb6eeb4SYOSHIFUJI Hideaki 
1008cfb6eeb4SYOSHIFUJI Hideaki 	if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
1009cfb6eeb4SYOSHIFUJI Hideaki 		return -EINVAL;
1010cfb6eeb4SYOSHIFUJI Hideaki 
1011cfb6eeb4SYOSHIFUJI Hideaki 	if (!tcp_sk(sk)->md5sig_info) {
1012cfb6eeb4SYOSHIFUJI Hideaki 		struct tcp_sock *tp = tcp_sk(sk);
1013aa133076SWu Fengguang 		struct tcp_md5sig_info *p;
1014cfb6eeb4SYOSHIFUJI Hideaki 
1015aa133076SWu Fengguang 		p = kzalloc(sizeof(*p), sk->sk_allocation);
1016cfb6eeb4SYOSHIFUJI Hideaki 		if (!p)
1017cfb6eeb4SYOSHIFUJI Hideaki 			return -EINVAL;
1018cfb6eeb4SYOSHIFUJI Hideaki 
1019cfb6eeb4SYOSHIFUJI Hideaki 		tp->md5sig_info = p;
10203d7dbeacSDavid S. Miller 		sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
1021cfb6eeb4SYOSHIFUJI Hideaki 	}
1022cfb6eeb4SYOSHIFUJI Hideaki 
1023aa133076SWu Fengguang 	newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, sk->sk_allocation);
1024cfb6eeb4SYOSHIFUJI Hideaki 	if (!newkey)
1025cfb6eeb4SYOSHIFUJI Hideaki 		return -ENOMEM;
1026cfb6eeb4SYOSHIFUJI Hideaki 	return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
1027cfb6eeb4SYOSHIFUJI Hideaki 				 newkey, cmd.tcpm_keylen);
1028cfb6eeb4SYOSHIFUJI Hideaki }
1029cfb6eeb4SYOSHIFUJI Hideaki 
103049a72dfbSAdam Langley static int tcp_v4_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
103149a72dfbSAdam Langley 					__be32 daddr, __be32 saddr, int nbytes)
1032cfb6eeb4SYOSHIFUJI Hideaki {
1033cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr *bp;
103449a72dfbSAdam Langley 	struct scatterlist sg;
1035cfb6eeb4SYOSHIFUJI Hideaki 
1036cfb6eeb4SYOSHIFUJI Hideaki 	bp = &hp->md5_blk.ip4;
1037cfb6eeb4SYOSHIFUJI Hideaki 
1038cfb6eeb4SYOSHIFUJI Hideaki 	/*
103949a72dfbSAdam Langley 	 * 1. the TCP pseudo-header (in the order: source IP address,
1040cfb6eeb4SYOSHIFUJI Hideaki 	 * destination IP address, zero-padded protocol number, and
1041cfb6eeb4SYOSHIFUJI Hideaki 	 * segment length)
1042cfb6eeb4SYOSHIFUJI Hideaki 	 */
1043cfb6eeb4SYOSHIFUJI Hideaki 	bp->saddr = saddr;
1044cfb6eeb4SYOSHIFUJI Hideaki 	bp->daddr = daddr;
1045cfb6eeb4SYOSHIFUJI Hideaki 	bp->pad = 0;
1046076fb722SYOSHIFUJI Hideaki 	bp->protocol = IPPROTO_TCP;
104749a72dfbSAdam Langley 	bp->len = cpu_to_be16(nbytes);
1048c7da57a1SDavid S. Miller 
104949a72dfbSAdam Langley 	sg_init_one(&sg, bp, sizeof(*bp));
105049a72dfbSAdam Langley 	return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
105149a72dfbSAdam Langley }
105249a72dfbSAdam Langley 
105349a72dfbSAdam Langley static int tcp_v4_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
105449a72dfbSAdam Langley 			       __be32 daddr, __be32 saddr, struct tcphdr *th)
105549a72dfbSAdam Langley {
105649a72dfbSAdam Langley 	struct tcp_md5sig_pool *hp;
105749a72dfbSAdam Langley 	struct hash_desc *desc;
105849a72dfbSAdam Langley 
105949a72dfbSAdam Langley 	hp = tcp_get_md5sig_pool();
106049a72dfbSAdam Langley 	if (!hp)
106149a72dfbSAdam Langley 		goto clear_hash_noput;
106249a72dfbSAdam Langley 	desc = &hp->md5_desc;
106349a72dfbSAdam Langley 
106449a72dfbSAdam Langley 	if (crypto_hash_init(desc))
106549a72dfbSAdam Langley 		goto clear_hash;
106649a72dfbSAdam Langley 	if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
106749a72dfbSAdam Langley 		goto clear_hash;
106849a72dfbSAdam Langley 	if (tcp_md5_hash_header(hp, th))
106949a72dfbSAdam Langley 		goto clear_hash;
107049a72dfbSAdam Langley 	if (tcp_md5_hash_key(hp, key))
107149a72dfbSAdam Langley 		goto clear_hash;
107249a72dfbSAdam Langley 	if (crypto_hash_final(desc, md5_hash))
1073cfb6eeb4SYOSHIFUJI Hideaki 		goto clear_hash;
1074cfb6eeb4SYOSHIFUJI Hideaki 
1075cfb6eeb4SYOSHIFUJI Hideaki 	tcp_put_md5sig_pool();
1076cfb6eeb4SYOSHIFUJI Hideaki 	return 0;
107749a72dfbSAdam Langley 
1078cfb6eeb4SYOSHIFUJI Hideaki clear_hash:
1079cfb6eeb4SYOSHIFUJI Hideaki 	tcp_put_md5sig_pool();
1080cfb6eeb4SYOSHIFUJI Hideaki clear_hash_noput:
1081cfb6eeb4SYOSHIFUJI Hideaki 	memset(md5_hash, 0, 16);
108249a72dfbSAdam Langley 	return 1;
1083cfb6eeb4SYOSHIFUJI Hideaki }
1084cfb6eeb4SYOSHIFUJI Hideaki 
108549a72dfbSAdam Langley int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
108649a72dfbSAdam Langley 			struct sock *sk, struct request_sock *req,
108749a72dfbSAdam Langley 			struct sk_buff *skb)
1088cfb6eeb4SYOSHIFUJI Hideaki {
108949a72dfbSAdam Langley 	struct tcp_md5sig_pool *hp;
109049a72dfbSAdam Langley 	struct hash_desc *desc;
109149a72dfbSAdam Langley 	struct tcphdr *th = tcp_hdr(skb);
1092cfb6eeb4SYOSHIFUJI Hideaki 	__be32 saddr, daddr;
1093cfb6eeb4SYOSHIFUJI Hideaki 
1094cfb6eeb4SYOSHIFUJI Hideaki 	if (sk) {
1095c720c7e8SEric Dumazet 		saddr = inet_sk(sk)->inet_saddr;
1096c720c7e8SEric Dumazet 		daddr = inet_sk(sk)->inet_daddr;
109749a72dfbSAdam Langley 	} else if (req) {
109849a72dfbSAdam Langley 		saddr = inet_rsk(req)->loc_addr;
109949a72dfbSAdam Langley 		daddr = inet_rsk(req)->rmt_addr;
1100cfb6eeb4SYOSHIFUJI Hideaki 	} else {
110149a72dfbSAdam Langley 		const struct iphdr *iph = ip_hdr(skb);
110249a72dfbSAdam Langley 		saddr = iph->saddr;
110349a72dfbSAdam Langley 		daddr = iph->daddr;
1104cfb6eeb4SYOSHIFUJI Hideaki 	}
1105cfb6eeb4SYOSHIFUJI Hideaki 
110649a72dfbSAdam Langley 	hp = tcp_get_md5sig_pool();
110749a72dfbSAdam Langley 	if (!hp)
110849a72dfbSAdam Langley 		goto clear_hash_noput;
110949a72dfbSAdam Langley 	desc = &hp->md5_desc;
111049a72dfbSAdam Langley 
111149a72dfbSAdam Langley 	if (crypto_hash_init(desc))
111249a72dfbSAdam Langley 		goto clear_hash;
111349a72dfbSAdam Langley 
111449a72dfbSAdam Langley 	if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
111549a72dfbSAdam Langley 		goto clear_hash;
111649a72dfbSAdam Langley 	if (tcp_md5_hash_header(hp, th))
111749a72dfbSAdam Langley 		goto clear_hash;
111849a72dfbSAdam Langley 	if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
111949a72dfbSAdam Langley 		goto clear_hash;
112049a72dfbSAdam Langley 	if (tcp_md5_hash_key(hp, key))
112149a72dfbSAdam Langley 		goto clear_hash;
112249a72dfbSAdam Langley 	if (crypto_hash_final(desc, md5_hash))
112349a72dfbSAdam Langley 		goto clear_hash;
112449a72dfbSAdam Langley 
112549a72dfbSAdam Langley 	tcp_put_md5sig_pool();
112649a72dfbSAdam Langley 	return 0;
112749a72dfbSAdam Langley 
112849a72dfbSAdam Langley clear_hash:
112949a72dfbSAdam Langley 	tcp_put_md5sig_pool();
113049a72dfbSAdam Langley clear_hash_noput:
113149a72dfbSAdam Langley 	memset(md5_hash, 0, 16);
113249a72dfbSAdam Langley 	return 1;
113349a72dfbSAdam Langley }
113449a72dfbSAdam Langley 
113549a72dfbSAdam Langley EXPORT_SYMBOL(tcp_v4_md5_hash_skb);
1136cfb6eeb4SYOSHIFUJI Hideaki 
1137cfb6eeb4SYOSHIFUJI Hideaki static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
1138cfb6eeb4SYOSHIFUJI Hideaki {
1139cfb6eeb4SYOSHIFUJI Hideaki 	/*
1140cfb6eeb4SYOSHIFUJI Hideaki 	 * This gets called for each TCP segment that arrives
1141cfb6eeb4SYOSHIFUJI Hideaki 	 * so we want to be efficient.
1142cfb6eeb4SYOSHIFUJI Hideaki 	 * We have 3 drop cases:
1143cfb6eeb4SYOSHIFUJI Hideaki 	 * o No MD5 hash and one expected.
1144cfb6eeb4SYOSHIFUJI Hideaki 	 * o MD5 hash and we're not expecting one.
1145cfb6eeb4SYOSHIFUJI Hideaki 	 * o MD5 hash and its wrong.
1146cfb6eeb4SYOSHIFUJI Hideaki 	 */
1147cfb6eeb4SYOSHIFUJI Hideaki 	__u8 *hash_location = NULL;
1148cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key *hash_expected;
1149eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
1150aa8223c7SArnaldo Carvalho de Melo 	struct tcphdr *th = tcp_hdr(skb);
1151cfb6eeb4SYOSHIFUJI Hideaki 	int genhash;
1152cfb6eeb4SYOSHIFUJI Hideaki 	unsigned char newhash[16];
1153cfb6eeb4SYOSHIFUJI Hideaki 
1154cfb6eeb4SYOSHIFUJI Hideaki 	hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
11557d5d5525SYOSHIFUJI Hideaki 	hash_location = tcp_parse_md5sig_option(th);
1156cfb6eeb4SYOSHIFUJI Hideaki 
1157cfb6eeb4SYOSHIFUJI Hideaki 	/* We've parsed the options - do we have a hash? */
1158cfb6eeb4SYOSHIFUJI Hideaki 	if (!hash_expected && !hash_location)
1159cfb6eeb4SYOSHIFUJI Hideaki 		return 0;
1160cfb6eeb4SYOSHIFUJI Hideaki 
1161cfb6eeb4SYOSHIFUJI Hideaki 	if (hash_expected && !hash_location) {
1162785957d3SDavid S. Miller 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
1163cfb6eeb4SYOSHIFUJI Hideaki 		return 1;
1164cfb6eeb4SYOSHIFUJI Hideaki 	}
1165cfb6eeb4SYOSHIFUJI Hideaki 
1166cfb6eeb4SYOSHIFUJI Hideaki 	if (!hash_expected && hash_location) {
1167785957d3SDavid S. Miller 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
1168cfb6eeb4SYOSHIFUJI Hideaki 		return 1;
1169cfb6eeb4SYOSHIFUJI Hideaki 	}
1170cfb6eeb4SYOSHIFUJI Hideaki 
1171cfb6eeb4SYOSHIFUJI Hideaki 	/* Okay, so this is hash_expected and hash_location -
1172cfb6eeb4SYOSHIFUJI Hideaki 	 * so we need to calculate the checksum.
1173cfb6eeb4SYOSHIFUJI Hideaki 	 */
117449a72dfbSAdam Langley 	genhash = tcp_v4_md5_hash_skb(newhash,
1175cfb6eeb4SYOSHIFUJI Hideaki 				      hash_expected,
117649a72dfbSAdam Langley 				      NULL, NULL, skb);
1177cfb6eeb4SYOSHIFUJI Hideaki 
1178cfb6eeb4SYOSHIFUJI Hideaki 	if (genhash || memcmp(hash_location, newhash, 16) != 0) {
1179cfb6eeb4SYOSHIFUJI Hideaki 		if (net_ratelimit()) {
1180673d57e7SHarvey Harrison 			printk(KERN_INFO "MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n",
1181673d57e7SHarvey Harrison 			       &iph->saddr, ntohs(th->source),
1182673d57e7SHarvey Harrison 			       &iph->daddr, ntohs(th->dest),
1183cfb6eeb4SYOSHIFUJI Hideaki 			       genhash ? " tcp_v4_calc_md5_hash failed" : "");
1184cfb6eeb4SYOSHIFUJI Hideaki 		}
1185cfb6eeb4SYOSHIFUJI Hideaki 		return 1;
1186cfb6eeb4SYOSHIFUJI Hideaki 	}
1187cfb6eeb4SYOSHIFUJI Hideaki 	return 0;
1188cfb6eeb4SYOSHIFUJI Hideaki }
1189cfb6eeb4SYOSHIFUJI Hideaki 
1190cfb6eeb4SYOSHIFUJI Hideaki #endif
1191cfb6eeb4SYOSHIFUJI Hideaki 
119272a3effaSEric Dumazet struct request_sock_ops tcp_request_sock_ops __read_mostly = {
11931da177e4SLinus Torvalds 	.family		=	PF_INET,
11942e6599cbSArnaldo Carvalho de Melo 	.obj_size	=	sizeof(struct tcp_request_sock),
11951da177e4SLinus Torvalds 	.rtx_syn_ack	=	tcp_v4_send_synack,
119660236fddSArnaldo Carvalho de Melo 	.send_ack	=	tcp_v4_reqsk_send_ack,
119760236fddSArnaldo Carvalho de Melo 	.destructor	=	tcp_v4_reqsk_destructor,
11981da177e4SLinus Torvalds 	.send_reset	=	tcp_v4_send_reset,
11991da177e4SLinus Torvalds };
12001da177e4SLinus Torvalds 
1201cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1202b2e4b3deSStephen Hemminger static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
1203cfb6eeb4SYOSHIFUJI Hideaki 	.md5_lookup	=	tcp_v4_reqsk_md5_lookup,
1204e3afe7b7SJohn Dykstra 	.calc_md5_hash	=	tcp_v4_md5_hash_skb,
1205cfb6eeb4SYOSHIFUJI Hideaki };
1206b6332e6cSAndrew Morton #endif
1207cfb6eeb4SYOSHIFUJI Hideaki 
12086d6ee43eSArnaldo Carvalho de Melo static struct timewait_sock_ops tcp_timewait_sock_ops = {
12096d6ee43eSArnaldo Carvalho de Melo 	.twsk_obj_size	= sizeof(struct tcp_timewait_sock),
12106d6ee43eSArnaldo Carvalho de Melo 	.twsk_unique	= tcp_twsk_unique,
1211cfb6eeb4SYOSHIFUJI Hideaki 	.twsk_destructor= tcp_twsk_destructor,
12126d6ee43eSArnaldo Carvalho de Melo };
12136d6ee43eSArnaldo Carvalho de Melo 
12141da177e4SLinus Torvalds int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
12151da177e4SLinus Torvalds {
12164957faadSWilliam Allen Simpson 	struct tcp_extend_values tmp_ext;
12171da177e4SLinus Torvalds 	struct tcp_options_received tmp_opt;
12184957faadSWilliam Allen Simpson 	u8 *hash_location;
121960236fddSArnaldo Carvalho de Melo 	struct request_sock *req;
1220e6b4d113SWilliam Allen Simpson 	struct inet_request_sock *ireq;
12214957faadSWilliam Allen Simpson 	struct tcp_sock *tp = tcp_sk(sk);
1222e6b4d113SWilliam Allen Simpson 	struct dst_entry *dst = NULL;
1223eddc9ec5SArnaldo Carvalho de Melo 	__be32 saddr = ip_hdr(skb)->saddr;
1224eddc9ec5SArnaldo Carvalho de Melo 	__be32 daddr = ip_hdr(skb)->daddr;
12251da177e4SLinus Torvalds 	__u32 isn = TCP_SKB_CB(skb)->when;
12261da177e4SLinus Torvalds #ifdef CONFIG_SYN_COOKIES
12271da177e4SLinus Torvalds 	int want_cookie = 0;
12281da177e4SLinus Torvalds #else
12291da177e4SLinus Torvalds #define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
12301da177e4SLinus Torvalds #endif
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	/* Never answer to SYNs send to broadcast or multicast */
1233511c3f92SEric Dumazet 	if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
12341da177e4SLinus Torvalds 		goto drop;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	/* TW buckets are converted to open requests without
12371da177e4SLinus Torvalds 	 * limitations, they conserve resources and peer is
12381da177e4SLinus Torvalds 	 * evidently real one.
12391da177e4SLinus Torvalds 	 */
1240463c84b9SArnaldo Carvalho de Melo 	if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
12411da177e4SLinus Torvalds #ifdef CONFIG_SYN_COOKIES
12421da177e4SLinus Torvalds 		if (sysctl_tcp_syncookies) {
12431da177e4SLinus Torvalds 			want_cookie = 1;
12441da177e4SLinus Torvalds 		} else
12451da177e4SLinus Torvalds #endif
12461da177e4SLinus Torvalds 		goto drop;
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	/* Accept backlog is full. If we have already queued enough
12501da177e4SLinus Torvalds 	 * of warm entries in syn queue, drop request. It is better than
12511da177e4SLinus Torvalds 	 * clogging syn queue with openreqs with exponentially increasing
12521da177e4SLinus Torvalds 	 * timeout.
12531da177e4SLinus Torvalds 	 */
1254463c84b9SArnaldo Carvalho de Melo 	if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
12551da177e4SLinus Torvalds 		goto drop;
12561da177e4SLinus Torvalds 
1257ce4a7d0dSArnaldo Carvalho de Melo 	req = inet_reqsk_alloc(&tcp_request_sock_ops);
12581da177e4SLinus Torvalds 	if (!req)
12591da177e4SLinus Torvalds 		goto drop;
12601da177e4SLinus Torvalds 
1261cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1262cfb6eeb4SYOSHIFUJI Hideaki 	tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
1263cfb6eeb4SYOSHIFUJI Hideaki #endif
1264cfb6eeb4SYOSHIFUJI Hideaki 
12651da177e4SLinus Torvalds 	tcp_clear_options(&tmp_opt);
1266bee7ca9eSWilliam Allen Simpson 	tmp_opt.mss_clamp = TCP_MSS_DEFAULT;
12674957faadSWilliam Allen Simpson 	tmp_opt.user_mss  = tp->rx_opt.user_mss;
1268*bb5b7c11SDavid S. Miller 	tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
12691da177e4SLinus Torvalds 
12704957faadSWilliam Allen Simpson 	if (tmp_opt.cookie_plus > 0 &&
12714957faadSWilliam Allen Simpson 	    tmp_opt.saw_tstamp &&
12724957faadSWilliam Allen Simpson 	    !tp->rx_opt.cookie_out_never &&
12734957faadSWilliam Allen Simpson 	    (sysctl_tcp_cookie_size > 0 ||
12744957faadSWilliam Allen Simpson 	     (tp->cookie_values != NULL &&
12754957faadSWilliam Allen Simpson 	      tp->cookie_values->cookie_desired > 0))) {
12764957faadSWilliam Allen Simpson 		u8 *c;
12774957faadSWilliam Allen Simpson 		u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
12784957faadSWilliam Allen Simpson 		int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
12794957faadSWilliam Allen Simpson 
12804957faadSWilliam Allen Simpson 		if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
12814957faadSWilliam Allen Simpson 			goto drop_and_release;
12824957faadSWilliam Allen Simpson 
12834957faadSWilliam Allen Simpson 		/* Secret recipe starts with IP addresses */
12844957faadSWilliam Allen Simpson 		*mess++ ^= daddr;
12854957faadSWilliam Allen Simpson 		*mess++ ^= saddr;
12864957faadSWilliam Allen Simpson 
12874957faadSWilliam Allen Simpson 		/* plus variable length Initiator Cookie */
12884957faadSWilliam Allen Simpson 		c = (u8 *)mess;
12894957faadSWilliam Allen Simpson 		while (l-- > 0)
12904957faadSWilliam Allen Simpson 			*c++ ^= *hash_location++;
12914957faadSWilliam Allen Simpson 
12924957faadSWilliam Allen Simpson #ifdef CONFIG_SYN_COOKIES
12934957faadSWilliam Allen Simpson 		want_cookie = 0;	/* not our kind of cookie */
12944957faadSWilliam Allen Simpson #endif
12954957faadSWilliam Allen Simpson 		tmp_ext.cookie_out_never = 0; /* false */
12964957faadSWilliam Allen Simpson 		tmp_ext.cookie_plus = tmp_opt.cookie_plus;
12974957faadSWilliam Allen Simpson 	} else if (!tp->rx_opt.cookie_in_always) {
12984957faadSWilliam Allen Simpson 		/* redundant indications, but ensure initialization. */
12994957faadSWilliam Allen Simpson 		tmp_ext.cookie_out_never = 1; /* true */
13004957faadSWilliam Allen Simpson 		tmp_ext.cookie_plus = 0;
13014957faadSWilliam Allen Simpson 	} else {
13024957faadSWilliam Allen Simpson 		goto drop_and_release;
13034957faadSWilliam Allen Simpson 	}
13044957faadSWilliam Allen Simpson 	tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
13051da177e4SLinus Torvalds 
13064dfc2817SFlorian Westphal 	if (want_cookie && !tmp_opt.saw_tstamp)
13071da177e4SLinus Torvalds 		tcp_clear_options(&tmp_opt);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
13101da177e4SLinus Torvalds 	tcp_openreq_init(req, &tmp_opt, skb);
13111da177e4SLinus Torvalds 
1312*bb5b7c11SDavid S. Miller 	ireq = inet_rsk(req);
1313*bb5b7c11SDavid S. Miller 	ireq->loc_addr = daddr;
1314*bb5b7c11SDavid S. Miller 	ireq->rmt_addr = saddr;
1315*bb5b7c11SDavid S. Miller 	ireq->no_srccheck = inet_sk(sk)->transparent;
1316*bb5b7c11SDavid S. Miller 	ireq->opt = tcp_v4_save_options(sk, skb);
1317*bb5b7c11SDavid S. Miller 
1318284904aaSPaul Moore 	if (security_inet_conn_request(sk, skb, req))
1319*bb5b7c11SDavid S. Miller 		goto drop_and_free;
1320284904aaSPaul Moore 
13211da177e4SLinus Torvalds 	if (!want_cookie)
1322aa8223c7SArnaldo Carvalho de Melo 		TCP_ECN_create_request(req, tcp_hdr(skb));
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	if (want_cookie) {
13251da177e4SLinus Torvalds #ifdef CONFIG_SYN_COOKIES
13261da177e4SLinus Torvalds 		syn_flood_warning(skb);
13274dfc2817SFlorian Westphal 		req->cookie_ts = tmp_opt.tstamp_ok;
13281da177e4SLinus Torvalds #endif
13291da177e4SLinus Torvalds 		isn = cookie_v4_init_sequence(sk, skb, &req->mss);
13301da177e4SLinus Torvalds 	} else if (!isn) {
13311da177e4SLinus Torvalds 		struct inet_peer *peer = NULL;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 		/* VJ's idea. We save last timestamp seen
13341da177e4SLinus Torvalds 		 * from the destination in peer table, when entering
13351da177e4SLinus Torvalds 		 * state TIME-WAIT, and check against it before
13361da177e4SLinus Torvalds 		 * accepting new connection request.
13371da177e4SLinus Torvalds 		 *
13381da177e4SLinus Torvalds 		 * If "isn" is not zero, this request hit alive
13391da177e4SLinus Torvalds 		 * timewait bucket, so that all the necessary checks
13401da177e4SLinus Torvalds 		 * are made in the function processing timewait state.
13411da177e4SLinus Torvalds 		 */
13421da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp &&
1343295ff7edSArnaldo Carvalho de Melo 		    tcp_death_row.sysctl_tw_recycle &&
1344*bb5b7c11SDavid S. Miller 		    (dst = inet_csk_route_req(sk, req)) != NULL &&
13451da177e4SLinus Torvalds 		    (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
13461da177e4SLinus Torvalds 		    peer->v4daddr == saddr) {
13472c1409a0SEric Dumazet 			if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
13481da177e4SLinus Torvalds 			    (s32)(peer->tcp_ts - req->ts_recent) >
13491da177e4SLinus Torvalds 							TCP_PAWS_WINDOW) {
1350de0744afSPavel Emelyanov 				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
13517cd04fa7SDenis V. Lunev 				goto drop_and_release;
13521da177e4SLinus Torvalds 			}
13531da177e4SLinus Torvalds 		}
13541da177e4SLinus Torvalds 		/* Kill the following clause, if you dislike this way. */
13551da177e4SLinus Torvalds 		else if (!sysctl_tcp_syncookies &&
1356463c84b9SArnaldo Carvalho de Melo 			 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
13571da177e4SLinus Torvalds 			  (sysctl_max_syn_backlog >> 2)) &&
13581da177e4SLinus Torvalds 			 (!peer || !peer->tcp_ts_stamp) &&
13591da177e4SLinus Torvalds 			 (!dst || !dst_metric(dst, RTAX_RTT))) {
13601da177e4SLinus Torvalds 			/* Without syncookies last quarter of
13611da177e4SLinus Torvalds 			 * backlog is filled with destinations,
13621da177e4SLinus Torvalds 			 * proven to be alive.
13631da177e4SLinus Torvalds 			 * It means that we continue to communicate
13641da177e4SLinus Torvalds 			 * to destinations, already remembered
13651da177e4SLinus Torvalds 			 * to the moment of synflood.
13661da177e4SLinus Torvalds 			 */
1367673d57e7SHarvey Harrison 			LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI4/%u\n",
1368673d57e7SHarvey Harrison 				       &saddr, ntohs(tcp_hdr(skb)->source));
13697cd04fa7SDenis V. Lunev 			goto drop_and_release;
13701da177e4SLinus Torvalds 		}
13711da177e4SLinus Torvalds 
1372a94f723dSGerrit Renker 		isn = tcp_v4_init_sequence(skb);
13731da177e4SLinus Torvalds 	}
13742e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->snt_isn = isn;
13751da177e4SLinus Torvalds 
13764957faadSWilliam Allen Simpson 	if (__tcp_v4_send_synack(sk, dst, req,
13774957faadSWilliam Allen Simpson 				 (struct request_values *)&tmp_ext) ||
13784957faadSWilliam Allen Simpson 	    want_cookie)
13791da177e4SLinus Torvalds 		goto drop_and_free;
13801da177e4SLinus Torvalds 
13813f421baaSArnaldo Carvalho de Melo 	inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
13821da177e4SLinus Torvalds 	return 0;
13831da177e4SLinus Torvalds 
13847cd04fa7SDenis V. Lunev drop_and_release:
13857cd04fa7SDenis V. Lunev 	dst_release(dst);
13861da177e4SLinus Torvalds drop_and_free:
138760236fddSArnaldo Carvalho de Melo 	reqsk_free(req);
13881da177e4SLinus Torvalds drop:
13891da177e4SLinus Torvalds 	return 0;
13901da177e4SLinus Torvalds }
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds /*
13941da177e4SLinus Torvalds  * The three way handshake has completed - we got a valid synack -
13951da177e4SLinus Torvalds  * now create the new socket.
13961da177e4SLinus Torvalds  */
13971da177e4SLinus Torvalds struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
139860236fddSArnaldo Carvalho de Melo 				  struct request_sock *req,
13991da177e4SLinus Torvalds 				  struct dst_entry *dst)
14001da177e4SLinus Torvalds {
14012e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq;
14021da177e4SLinus Torvalds 	struct inet_sock *newinet;
14031da177e4SLinus Torvalds 	struct tcp_sock *newtp;
14041da177e4SLinus Torvalds 	struct sock *newsk;
1405cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1406cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key *key;
1407cfb6eeb4SYOSHIFUJI Hideaki #endif
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	if (sk_acceptq_is_full(sk))
14101da177e4SLinus Torvalds 		goto exit_overflow;
14111da177e4SLinus Torvalds 
1412463c84b9SArnaldo Carvalho de Melo 	if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
14131da177e4SLinus Torvalds 		goto exit;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	newsk = tcp_create_openreq_child(sk, req, skb);
14161da177e4SLinus Torvalds 	if (!newsk)
14171da177e4SLinus Torvalds 		goto exit;
14181da177e4SLinus Torvalds 
1419bcd76111SHerbert Xu 	newsk->sk_gso_type = SKB_GSO_TCPV4;
14206cbb0df7SArnaldo Carvalho de Melo 	sk_setup_caps(newsk, dst);
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	newtp		      = tcp_sk(newsk);
14231da177e4SLinus Torvalds 	newinet		      = inet_sk(newsk);
14242e6599cbSArnaldo Carvalho de Melo 	ireq		      = inet_rsk(req);
1425c720c7e8SEric Dumazet 	newinet->inet_daddr   = ireq->rmt_addr;
1426c720c7e8SEric Dumazet 	newinet->inet_rcv_saddr = ireq->loc_addr;
1427c720c7e8SEric Dumazet 	newinet->inet_saddr	      = ireq->loc_addr;
14282e6599cbSArnaldo Carvalho de Melo 	newinet->opt	      = ireq->opt;
14292e6599cbSArnaldo Carvalho de Melo 	ireq->opt	      = NULL;
1430463c84b9SArnaldo Carvalho de Melo 	newinet->mc_index     = inet_iif(skb);
1431eddc9ec5SArnaldo Carvalho de Melo 	newinet->mc_ttl	      = ip_hdr(skb)->ttl;
1432d83d8461SArnaldo Carvalho de Melo 	inet_csk(newsk)->icsk_ext_hdr_len = 0;
14331da177e4SLinus Torvalds 	if (newinet->opt)
1434d83d8461SArnaldo Carvalho de Melo 		inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
1435c720c7e8SEric Dumazet 	newinet->inet_id = newtp->write_seq ^ jiffies;
14361da177e4SLinus Torvalds 
14375d424d5aSJohn Heffner 	tcp_mtup_init(newsk);
14381da177e4SLinus Torvalds 	tcp_sync_mss(newsk, dst_mtu(dst));
14391da177e4SLinus Torvalds 	newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
1440f5fff5dcSTom Quetchenbach 	if (tcp_sk(sk)->rx_opt.user_mss &&
1441f5fff5dcSTom Quetchenbach 	    tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1442f5fff5dcSTom Quetchenbach 		newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1443f5fff5dcSTom Quetchenbach 
14441da177e4SLinus Torvalds 	tcp_initialize_rcv_mss(newsk);
14451da177e4SLinus Torvalds 
1446cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1447cfb6eeb4SYOSHIFUJI Hideaki 	/* Copy over the MD5 key from the original socket */
1448c720c7e8SEric Dumazet 	key = tcp_v4_md5_do_lookup(sk, newinet->inet_daddr);
1449c720c7e8SEric Dumazet 	if (key != NULL) {
1450cfb6eeb4SYOSHIFUJI Hideaki 		/*
1451cfb6eeb4SYOSHIFUJI Hideaki 		 * We're using one, so create a matching key
1452cfb6eeb4SYOSHIFUJI Hideaki 		 * on the newsk structure. If we fail to get
1453cfb6eeb4SYOSHIFUJI Hideaki 		 * memory, then we end up not copying the key
1454cfb6eeb4SYOSHIFUJI Hideaki 		 * across. Shucks.
1455cfb6eeb4SYOSHIFUJI Hideaki 		 */
1456f6685938SArnaldo Carvalho de Melo 		char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1457f6685938SArnaldo Carvalho de Melo 		if (newkey != NULL)
1458c720c7e8SEric Dumazet 			tcp_v4_md5_do_add(newsk, newinet->inet_daddr,
1459cfb6eeb4SYOSHIFUJI Hideaki 					  newkey, key->keylen);
146049a72dfbSAdam Langley 		newsk->sk_route_caps &= ~NETIF_F_GSO_MASK;
1461cfb6eeb4SYOSHIFUJI Hideaki 	}
1462cfb6eeb4SYOSHIFUJI Hideaki #endif
1463cfb6eeb4SYOSHIFUJI Hideaki 
14649327f705SEric Dumazet 	__inet_hash_nolisten(newsk, NULL);
1465ab1e0a13SArnaldo Carvalho de Melo 	__inet_inherit_port(sk, newsk);
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	return newsk;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds exit_overflow:
1470de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
14711da177e4SLinus Torvalds exit:
1472de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
14731da177e4SLinus Torvalds 	dst_release(dst);
14741da177e4SLinus Torvalds 	return NULL;
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
14781da177e4SLinus Torvalds {
1479aa8223c7SArnaldo Carvalho de Melo 	struct tcphdr *th = tcp_hdr(skb);
1480eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
14811da177e4SLinus Torvalds 	struct sock *nsk;
148260236fddSArnaldo Carvalho de Melo 	struct request_sock **prev;
14831da177e4SLinus Torvalds 	/* Find possible connection requests. */
1484463c84b9SArnaldo Carvalho de Melo 	struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
14851da177e4SLinus Torvalds 						       iph->saddr, iph->daddr);
14861da177e4SLinus Torvalds 	if (req)
14871da177e4SLinus Torvalds 		return tcp_check_req(sk, skb, req, prev);
14881da177e4SLinus Torvalds 
14893b1e0a65SYOSHIFUJI Hideaki 	nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
1490c67499c0SPavel Emelyanov 			th->source, iph->daddr, th->dest, inet_iif(skb));
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	if (nsk) {
14931da177e4SLinus Torvalds 		if (nsk->sk_state != TCP_TIME_WAIT) {
14941da177e4SLinus Torvalds 			bh_lock_sock(nsk);
14951da177e4SLinus Torvalds 			return nsk;
14961da177e4SLinus Torvalds 		}
14979469c7b4SYOSHIFUJI Hideaki 		inet_twsk_put(inet_twsk(nsk));
14981da177e4SLinus Torvalds 		return NULL;
14991da177e4SLinus Torvalds 	}
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds #ifdef CONFIG_SYN_COOKIES
15021da177e4SLinus Torvalds 	if (!th->rst && !th->syn && th->ack)
15031da177e4SLinus Torvalds 		sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
15041da177e4SLinus Torvalds #endif
15051da177e4SLinus Torvalds 	return sk;
15061da177e4SLinus Torvalds }
15071da177e4SLinus Torvalds 
1508b51655b9SAl Viro static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
15091da177e4SLinus Torvalds {
1510eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
1511eddc9ec5SArnaldo Carvalho de Melo 
151284fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
1513eddc9ec5SArnaldo Carvalho de Melo 		if (!tcp_v4_check(skb->len, iph->saddr,
1514eddc9ec5SArnaldo Carvalho de Melo 				  iph->daddr, skb->csum)) {
15151da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1516fb286bb2SHerbert Xu 			return 0;
1517fb286bb2SHerbert Xu 		}
1518fb286bb2SHerbert Xu 	}
1519fb286bb2SHerbert Xu 
1520eddc9ec5SArnaldo Carvalho de Melo 	skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
1521fb286bb2SHerbert Xu 				       skb->len, IPPROTO_TCP, 0);
1522fb286bb2SHerbert Xu 
1523fb286bb2SHerbert Xu 	if (skb->len <= 76) {
1524fb286bb2SHerbert Xu 		return __skb_checksum_complete(skb);
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 	return 0;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds /* The socket must have it's spinlock held when we get
15311da177e4SLinus Torvalds  * here.
15321da177e4SLinus Torvalds  *
15331da177e4SLinus Torvalds  * We have a potential double-lock case here, so even when
15341da177e4SLinus Torvalds  * doing backlog processing we use the BH locking scheme.
15351da177e4SLinus Torvalds  * This is because we cannot sleep with the original spinlock
15361da177e4SLinus Torvalds  * held.
15371da177e4SLinus Torvalds  */
15381da177e4SLinus Torvalds int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
15391da177e4SLinus Torvalds {
1540cfb6eeb4SYOSHIFUJI Hideaki 	struct sock *rsk;
1541cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1542cfb6eeb4SYOSHIFUJI Hideaki 	/*
1543cfb6eeb4SYOSHIFUJI Hideaki 	 * We really want to reject the packet as early as possible
1544cfb6eeb4SYOSHIFUJI Hideaki 	 * if:
1545cfb6eeb4SYOSHIFUJI Hideaki 	 *  o We're expecting an MD5'd packet and this is no MD5 tcp option
1546cfb6eeb4SYOSHIFUJI Hideaki 	 *  o There is an MD5 option and we're not expecting one
1547cfb6eeb4SYOSHIFUJI Hideaki 	 */
1548cfb6eeb4SYOSHIFUJI Hideaki 	if (tcp_v4_inbound_md5_hash(sk, skb))
1549cfb6eeb4SYOSHIFUJI Hideaki 		goto discard;
1550cfb6eeb4SYOSHIFUJI Hideaki #endif
1551cfb6eeb4SYOSHIFUJI Hideaki 
15521da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
15531da177e4SLinus Torvalds 		TCP_CHECK_TIMER(sk);
1554aa8223c7SArnaldo Carvalho de Melo 		if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
1555cfb6eeb4SYOSHIFUJI Hideaki 			rsk = sk;
15561da177e4SLinus Torvalds 			goto reset;
1557cfb6eeb4SYOSHIFUJI Hideaki 		}
15581da177e4SLinus Torvalds 		TCP_CHECK_TIMER(sk);
15591da177e4SLinus Torvalds 		return 0;
15601da177e4SLinus Torvalds 	}
15611da177e4SLinus Torvalds 
1562ab6a5bb6SArnaldo Carvalho de Melo 	if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
15631da177e4SLinus Torvalds 		goto csum_err;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	if (sk->sk_state == TCP_LISTEN) {
15661da177e4SLinus Torvalds 		struct sock *nsk = tcp_v4_hnd_req(sk, skb);
15671da177e4SLinus Torvalds 		if (!nsk)
15681da177e4SLinus Torvalds 			goto discard;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 		if (nsk != sk) {
1571cfb6eeb4SYOSHIFUJI Hideaki 			if (tcp_child_process(sk, nsk, skb)) {
1572cfb6eeb4SYOSHIFUJI Hideaki 				rsk = nsk;
15731da177e4SLinus Torvalds 				goto reset;
1574cfb6eeb4SYOSHIFUJI Hideaki 			}
15751da177e4SLinus Torvalds 			return 0;
15761da177e4SLinus Torvalds 		}
15771da177e4SLinus Torvalds 	}
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	TCP_CHECK_TIMER(sk);
1580aa8223c7SArnaldo Carvalho de Melo 	if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
1581cfb6eeb4SYOSHIFUJI Hideaki 		rsk = sk;
15821da177e4SLinus Torvalds 		goto reset;
1583cfb6eeb4SYOSHIFUJI Hideaki 	}
15841da177e4SLinus Torvalds 	TCP_CHECK_TIMER(sk);
15851da177e4SLinus Torvalds 	return 0;
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds reset:
1588cfb6eeb4SYOSHIFUJI Hideaki 	tcp_v4_send_reset(rsk, skb);
15891da177e4SLinus Torvalds discard:
15901da177e4SLinus Torvalds 	kfree_skb(skb);
15911da177e4SLinus Torvalds 	/* Be careful here. If this function gets more complicated and
15921da177e4SLinus Torvalds 	 * gcc suffers from register pressure on the x86, sk (in %ebx)
15931da177e4SLinus Torvalds 	 * might be destroyed here. This current version compiles correctly,
15941da177e4SLinus Torvalds 	 * but you have been warned.
15951da177e4SLinus Torvalds 	 */
15961da177e4SLinus Torvalds 	return 0;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds csum_err:
159963231bddSPavel Emelyanov 	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
16001da177e4SLinus Torvalds 	goto discard;
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds /*
16041da177e4SLinus Torvalds  *	From tcp_input.c
16051da177e4SLinus Torvalds  */
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds int tcp_v4_rcv(struct sk_buff *skb)
16081da177e4SLinus Torvalds {
1609eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph;
16101da177e4SLinus Torvalds 	struct tcphdr *th;
16111da177e4SLinus Torvalds 	struct sock *sk;
16121da177e4SLinus Torvalds 	int ret;
1613a86b1e30SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	if (skb->pkt_type != PACKET_HOST)
16161da177e4SLinus Torvalds 		goto discard_it;
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	/* Count it even if it's bad */
161963231bddSPavel Emelyanov 	TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
16221da177e4SLinus Torvalds 		goto discard_it;
16231da177e4SLinus Torvalds 
1624aa8223c7SArnaldo Carvalho de Melo 	th = tcp_hdr(skb);
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	if (th->doff < sizeof(struct tcphdr) / 4)
16271da177e4SLinus Torvalds 		goto bad_packet;
16281da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, th->doff * 4))
16291da177e4SLinus Torvalds 		goto discard_it;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	/* An explanation is required here, I think.
16321da177e4SLinus Torvalds 	 * Packet length and doff are validated by header prediction,
1633caa20d9aSStephen Hemminger 	 * provided case of th->doff==0 is eliminated.
16341da177e4SLinus Torvalds 	 * So, we defer the checks. */
163560476372SHerbert Xu 	if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
16361da177e4SLinus Torvalds 		goto bad_packet;
16371da177e4SLinus Torvalds 
1638aa8223c7SArnaldo Carvalho de Melo 	th = tcp_hdr(skb);
1639eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
16401da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->seq = ntohl(th->seq);
16411da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
16421da177e4SLinus Torvalds 				    skb->len - th->doff * 4);
16431da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
16441da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->when	 = 0;
1645eddc9ec5SArnaldo Carvalho de Melo 	TCP_SKB_CB(skb)->flags	 = iph->tos;
16461da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->sacked	 = 0;
16471da177e4SLinus Torvalds 
16489a1f27c4SArnaldo Carvalho de Melo 	sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
16491da177e4SLinus Torvalds 	if (!sk)
16501da177e4SLinus Torvalds 		goto no_tcp_socket;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds process:
16531da177e4SLinus Torvalds 	if (sk->sk_state == TCP_TIME_WAIT)
16541da177e4SLinus Torvalds 		goto do_time_wait;
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
16571da177e4SLinus Torvalds 		goto discard_and_relse;
1658b59c2701SPatrick McHardy 	nf_reset(skb);
16591da177e4SLinus Torvalds 
1660fda9ef5dSDmitry Mishin 	if (sk_filter(sk, skb))
16611da177e4SLinus Torvalds 		goto discard_and_relse;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	skb->dev = NULL;
16641da177e4SLinus Torvalds 
1665c6366184SIngo Molnar 	bh_lock_sock_nested(sk);
16661da177e4SLinus Torvalds 	ret = 0;
16671da177e4SLinus Torvalds 	if (!sock_owned_by_user(sk)) {
16681a2449a8SChris Leech #ifdef CONFIG_NET_DMA
16691a2449a8SChris Leech 		struct tcp_sock *tp = tcp_sk(sk);
16701a2449a8SChris Leech 		if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1671f67b4599SDan Williams 			tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
16721a2449a8SChris Leech 		if (tp->ucopy.dma_chan)
16731a2449a8SChris Leech 			ret = tcp_v4_do_rcv(sk, skb);
16741a2449a8SChris Leech 		else
16751a2449a8SChris Leech #endif
16761a2449a8SChris Leech 		{
16771da177e4SLinus Torvalds 			if (!tcp_prequeue(sk, skb))
16781da177e4SLinus Torvalds 				ret = tcp_v4_do_rcv(sk, skb);
16791a2449a8SChris Leech 		}
16801da177e4SLinus Torvalds 	} else
16811da177e4SLinus Torvalds 		sk_add_backlog(sk, skb);
16821da177e4SLinus Torvalds 	bh_unlock_sock(sk);
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	sock_put(sk);
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	return ret;
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds no_tcp_socket:
16891da177e4SLinus Torvalds 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
16901da177e4SLinus Torvalds 		goto discard_it;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
16931da177e4SLinus Torvalds bad_packet:
169463231bddSPavel Emelyanov 		TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
16951da177e4SLinus Torvalds 	} else {
1696cfb6eeb4SYOSHIFUJI Hideaki 		tcp_v4_send_reset(NULL, skb);
16971da177e4SLinus Torvalds 	}
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds discard_it:
17001da177e4SLinus Torvalds 	/* Discard frame. */
17011da177e4SLinus Torvalds 	kfree_skb(skb);
17021da177e4SLinus Torvalds 	return 0;
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds discard_and_relse:
17051da177e4SLinus Torvalds 	sock_put(sk);
17061da177e4SLinus Torvalds 	goto discard_it;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds do_time_wait:
17091da177e4SLinus Torvalds 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
17109469c7b4SYOSHIFUJI Hideaki 		inet_twsk_put(inet_twsk(sk));
17111da177e4SLinus Torvalds 		goto discard_it;
17121da177e4SLinus Torvalds 	}
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
171563231bddSPavel Emelyanov 		TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
17169469c7b4SYOSHIFUJI Hideaki 		inet_twsk_put(inet_twsk(sk));
17171da177e4SLinus Torvalds 		goto discard_it;
17181da177e4SLinus Torvalds 	}
17199469c7b4SYOSHIFUJI Hideaki 	switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
17201da177e4SLinus Torvalds 	case TCP_TW_SYN: {
1721c346dca1SYOSHIFUJI Hideaki 		struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
1722c67499c0SPavel Emelyanov 							&tcp_hashinfo,
1723eddc9ec5SArnaldo Carvalho de Melo 							iph->daddr, th->dest,
1724463c84b9SArnaldo Carvalho de Melo 							inet_iif(skb));
17251da177e4SLinus Torvalds 		if (sk2) {
17269469c7b4SYOSHIFUJI Hideaki 			inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
17279469c7b4SYOSHIFUJI Hideaki 			inet_twsk_put(inet_twsk(sk));
17281da177e4SLinus Torvalds 			sk = sk2;
17291da177e4SLinus Torvalds 			goto process;
17301da177e4SLinus Torvalds 		}
17311da177e4SLinus Torvalds 		/* Fall through to ACK */
17321da177e4SLinus Torvalds 	}
17331da177e4SLinus Torvalds 	case TCP_TW_ACK:
17341da177e4SLinus Torvalds 		tcp_v4_timewait_ack(sk, skb);
17351da177e4SLinus Torvalds 		break;
17361da177e4SLinus Torvalds 	case TCP_TW_RST:
17371da177e4SLinus Torvalds 		goto no_tcp_socket;
17381da177e4SLinus Torvalds 	case TCP_TW_SUCCESS:;
17391da177e4SLinus Torvalds 	}
17401da177e4SLinus Torvalds 	goto discard_it;
17411da177e4SLinus Torvalds }
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds /* VJ's idea. Save last timestamp seen from this destination
17441da177e4SLinus Torvalds  * and hold it at least for normal timewait interval to use for duplicate
17451da177e4SLinus Torvalds  * segment detection in subsequent connections, before they enter synchronized
17461da177e4SLinus Torvalds  * state.
17471da177e4SLinus Torvalds  */
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds int tcp_v4_remember_stamp(struct sock *sk)
17501da177e4SLinus Torvalds {
17511da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
17521da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
17531da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
17541da177e4SLinus Torvalds 	struct inet_peer *peer = NULL;
17551da177e4SLinus Torvalds 	int release_it = 0;
17561da177e4SLinus Torvalds 
1757c720c7e8SEric Dumazet 	if (!rt || rt->rt_dst != inet->inet_daddr) {
1758c720c7e8SEric Dumazet 		peer = inet_getpeer(inet->inet_daddr, 1);
17591da177e4SLinus Torvalds 		release_it = 1;
17601da177e4SLinus Torvalds 	} else {
17611da177e4SLinus Torvalds 		if (!rt->peer)
17621da177e4SLinus Torvalds 			rt_bind_peer(rt, 1);
17631da177e4SLinus Torvalds 		peer = rt->peer;
17641da177e4SLinus Torvalds 	}
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	if (peer) {
17671da177e4SLinus Torvalds 		if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
17682c1409a0SEric Dumazet 		    ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
17692c1409a0SEric Dumazet 		     peer->tcp_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
17702c1409a0SEric Dumazet 			peer->tcp_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
17711da177e4SLinus Torvalds 			peer->tcp_ts = tp->rx_opt.ts_recent;
17721da177e4SLinus Torvalds 		}
17731da177e4SLinus Torvalds 		if (release_it)
17741da177e4SLinus Torvalds 			inet_putpeer(peer);
17751da177e4SLinus Torvalds 		return 1;
17761da177e4SLinus Torvalds 	}
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	return 0;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
17818feaf0c0SArnaldo Carvalho de Melo int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
17821da177e4SLinus Torvalds {
17838feaf0c0SArnaldo Carvalho de Melo 	struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	if (peer) {
17868feaf0c0SArnaldo Carvalho de Melo 		const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
17878feaf0c0SArnaldo Carvalho de Melo 
17888feaf0c0SArnaldo Carvalho de Melo 		if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
17892c1409a0SEric Dumazet 		    ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
17902c1409a0SEric Dumazet 		     peer->tcp_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
17912c1409a0SEric Dumazet 			peer->tcp_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
17928feaf0c0SArnaldo Carvalho de Melo 			peer->tcp_ts	   = tcptw->tw_ts_recent;
17931da177e4SLinus Torvalds 		}
17941da177e4SLinus Torvalds 		inet_putpeer(peer);
17951da177e4SLinus Torvalds 		return 1;
17961da177e4SLinus Torvalds 	}
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	return 0;
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds 
18013b401a81SStephen Hemminger const struct inet_connection_sock_af_ops ipv4_specific = {
18021da177e4SLinus Torvalds 	.queue_xmit	   = ip_queue_xmit,
18031da177e4SLinus Torvalds 	.send_check	   = tcp_v4_send_check,
180432519f11SArnaldo Carvalho de Melo 	.rebuild_header	   = inet_sk_rebuild_header,
18051da177e4SLinus Torvalds 	.conn_request	   = tcp_v4_conn_request,
18061da177e4SLinus Torvalds 	.syn_recv_sock	   = tcp_v4_syn_recv_sock,
18071da177e4SLinus Torvalds 	.remember_stamp	   = tcp_v4_remember_stamp,
18081da177e4SLinus Torvalds 	.net_header_len	   = sizeof(struct iphdr),
18091da177e4SLinus Torvalds 	.setsockopt	   = ip_setsockopt,
18101da177e4SLinus Torvalds 	.getsockopt	   = ip_getsockopt,
1811543d9cfeSArnaldo Carvalho de Melo 	.addr2sockaddr	   = inet_csk_addr2sockaddr,
1812543d9cfeSArnaldo Carvalho de Melo 	.sockaddr_len	   = sizeof(struct sockaddr_in),
1813ab1e0a13SArnaldo Carvalho de Melo 	.bind_conflict	   = inet_csk_bind_conflict,
18143fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
18153fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_ip_setsockopt,
18163fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_ip_getsockopt,
18173fdadf7dSDmitry Mishin #endif
18181da177e4SLinus Torvalds };
18191da177e4SLinus Torvalds 
1820cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1821b2e4b3deSStephen Hemminger static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
1822cfb6eeb4SYOSHIFUJI Hideaki 	.md5_lookup		= tcp_v4_md5_lookup,
182349a72dfbSAdam Langley 	.calc_md5_hash		= tcp_v4_md5_hash_skb,
1824cfb6eeb4SYOSHIFUJI Hideaki 	.md5_add		= tcp_v4_md5_add_func,
1825cfb6eeb4SYOSHIFUJI Hideaki 	.md5_parse		= tcp_v4_parse_md5_keys,
1826cfb6eeb4SYOSHIFUJI Hideaki };
1827b6332e6cSAndrew Morton #endif
1828cfb6eeb4SYOSHIFUJI Hideaki 
18291da177e4SLinus Torvalds /* NOTE: A lot of things set to zero explicitly by call to
18301da177e4SLinus Torvalds  *       sk_alloc() so need not be done here.
18311da177e4SLinus Torvalds  */
18321da177e4SLinus Torvalds static int tcp_v4_init_sock(struct sock *sk)
18331da177e4SLinus Torvalds {
18346687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
18351da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	skb_queue_head_init(&tp->out_of_order_queue);
18381da177e4SLinus Torvalds 	tcp_init_xmit_timers(sk);
18391da177e4SLinus Torvalds 	tcp_prequeue_init(tp);
18401da177e4SLinus Torvalds 
18416687e988SArnaldo Carvalho de Melo 	icsk->icsk_rto = TCP_TIMEOUT_INIT;
18421da177e4SLinus Torvalds 	tp->mdev = TCP_TIMEOUT_INIT;
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	/* So many TCP implementations out there (incorrectly) count the
18451da177e4SLinus Torvalds 	 * initial SYN frame in their delayed-ACK and congestion control
18461da177e4SLinus Torvalds 	 * algorithms that we must have the following bandaid to talk
18471da177e4SLinus Torvalds 	 * efficiently to them.  -DaveM
18481da177e4SLinus Torvalds 	 */
18491da177e4SLinus Torvalds 	tp->snd_cwnd = 2;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	/* See draft-stevens-tcpca-spec-01 for discussion of the
18521da177e4SLinus Torvalds 	 * initialization of these values.
18531da177e4SLinus Torvalds 	 */
18540b6a05c1SIlpo Järvinen 	tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
18551da177e4SLinus Torvalds 	tp->snd_cwnd_clamp = ~0;
1856bee7ca9eSWilliam Allen Simpson 	tp->mss_cache = TCP_MSS_DEFAULT;
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	tp->reordering = sysctl_tcp_reordering;
18596687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_ops = &tcp_init_congestion_ops;
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	sk->sk_write_space = sk_stream_write_space;
18641da177e4SLinus Torvalds 	sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
18651da177e4SLinus Torvalds 
18668292a17aSArnaldo Carvalho de Melo 	icsk->icsk_af_ops = &ipv4_specific;
1867d83d8461SArnaldo Carvalho de Melo 	icsk->icsk_sync_mss = tcp_sync_mss;
1868cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1869cfb6eeb4SYOSHIFUJI Hideaki 	tp->af_specific = &tcp_sock_ipv4_specific;
1870cfb6eeb4SYOSHIFUJI Hideaki #endif
18711da177e4SLinus Torvalds 
1872435cf559SWilliam Allen Simpson 	/* TCP Cookie Transactions */
1873435cf559SWilliam Allen Simpson 	if (sysctl_tcp_cookie_size > 0) {
1874435cf559SWilliam Allen Simpson 		/* Default, cookies without s_data_payload. */
1875435cf559SWilliam Allen Simpson 		tp->cookie_values =
1876435cf559SWilliam Allen Simpson 			kzalloc(sizeof(*tp->cookie_values),
1877435cf559SWilliam Allen Simpson 				sk->sk_allocation);
1878435cf559SWilliam Allen Simpson 		if (tp->cookie_values != NULL)
1879435cf559SWilliam Allen Simpson 			kref_init(&tp->cookie_values->kref);
1880435cf559SWilliam Allen Simpson 	}
1881435cf559SWilliam Allen Simpson 	/* Presumed zeroed, in order of appearance:
1882435cf559SWilliam Allen Simpson 	 *	cookie_in_always, cookie_out_never,
1883435cf559SWilliam Allen Simpson 	 *	s_data_constant, s_data_in, s_data_out
1884435cf559SWilliam Allen Simpson 	 */
18851da177e4SLinus Torvalds 	sk->sk_sndbuf = sysctl_tcp_wmem[1];
18861da177e4SLinus Torvalds 	sk->sk_rcvbuf = sysctl_tcp_rmem[1];
18871da177e4SLinus Torvalds 
1888eb4dea58SHerbert Xu 	local_bh_disable();
18891748376bSEric Dumazet 	percpu_counter_inc(&tcp_sockets_allocated);
1890eb4dea58SHerbert Xu 	local_bh_enable();
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds 	return 0;
18931da177e4SLinus Torvalds }
18941da177e4SLinus Torvalds 
18957d06b2e0SBrian Haley void tcp_v4_destroy_sock(struct sock *sk)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	tcp_clear_xmit_timers(sk);
19001da177e4SLinus Torvalds 
19016687e988SArnaldo Carvalho de Melo 	tcp_cleanup_congestion_control(sk);
1902317a76f9SStephen Hemminger 
19031da177e4SLinus Torvalds 	/* Cleanup up the write buffer. */
1904fe067e8aSDavid S. Miller 	tcp_write_queue_purge(sk);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	/* Cleans up our, hopefully empty, out_of_order_queue. */
19071da177e4SLinus Torvalds 	__skb_queue_purge(&tp->out_of_order_queue);
19081da177e4SLinus Torvalds 
1909cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1910cfb6eeb4SYOSHIFUJI Hideaki 	/* Clean up the MD5 key list, if any */
1911cfb6eeb4SYOSHIFUJI Hideaki 	if (tp->md5sig_info) {
1912cfb6eeb4SYOSHIFUJI Hideaki 		tcp_v4_clear_md5_list(sk);
1913cfb6eeb4SYOSHIFUJI Hideaki 		kfree(tp->md5sig_info);
1914cfb6eeb4SYOSHIFUJI Hideaki 		tp->md5sig_info = NULL;
1915cfb6eeb4SYOSHIFUJI Hideaki 	}
1916cfb6eeb4SYOSHIFUJI Hideaki #endif
1917cfb6eeb4SYOSHIFUJI Hideaki 
19181a2449a8SChris Leech #ifdef CONFIG_NET_DMA
19191a2449a8SChris Leech 	/* Cleans up our sk_async_wait_queue */
19201a2449a8SChris Leech 	__skb_queue_purge(&sk->sk_async_wait_queue);
19211a2449a8SChris Leech #endif
19221a2449a8SChris Leech 
19231da177e4SLinus Torvalds 	/* Clean prequeue, it must be empty really */
19241da177e4SLinus Torvalds 	__skb_queue_purge(&tp->ucopy.prequeue);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	/* Clean up a referenced TCP bind bucket. */
1927463c84b9SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_bind_hash)
1928ab1e0a13SArnaldo Carvalho de Melo 		inet_put_port(sk);
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	/*
19311da177e4SLinus Torvalds 	 * If sendmsg cached page exists, toss it.
19321da177e4SLinus Torvalds 	 */
19331da177e4SLinus Torvalds 	if (sk->sk_sndmsg_page) {
19341da177e4SLinus Torvalds 		__free_page(sk->sk_sndmsg_page);
19351da177e4SLinus Torvalds 		sk->sk_sndmsg_page = NULL;
19361da177e4SLinus Torvalds 	}
19371da177e4SLinus Torvalds 
1938435cf559SWilliam Allen Simpson 	/* TCP Cookie Transactions */
1939435cf559SWilliam Allen Simpson 	if (tp->cookie_values != NULL) {
1940435cf559SWilliam Allen Simpson 		kref_put(&tp->cookie_values->kref,
1941435cf559SWilliam Allen Simpson 			 tcp_cookie_values_release);
1942435cf559SWilliam Allen Simpson 		tp->cookie_values = NULL;
1943435cf559SWilliam Allen Simpson 	}
1944435cf559SWilliam Allen Simpson 
19451748376bSEric Dumazet 	percpu_counter_dec(&tcp_sockets_allocated);
19461da177e4SLinus Torvalds }
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_destroy_sock);
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
19511da177e4SLinus Torvalds /* Proc filesystem TCP sock list dumping. */
19521da177e4SLinus Torvalds 
19533ab5aee7SEric Dumazet static inline struct inet_timewait_sock *tw_head(struct hlist_nulls_head *head)
19541da177e4SLinus Torvalds {
19553ab5aee7SEric Dumazet 	return hlist_nulls_empty(head) ? NULL :
19568feaf0c0SArnaldo Carvalho de Melo 		list_entry(head->first, struct inet_timewait_sock, tw_node);
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
19598feaf0c0SArnaldo Carvalho de Melo static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
19601da177e4SLinus Torvalds {
19613ab5aee7SEric Dumazet 	return !is_a_nulls(tw->tw_node.next) ?
19623ab5aee7SEric Dumazet 		hlist_nulls_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
19631da177e4SLinus Torvalds }
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds static void *listening_get_next(struct seq_file *seq, void *cur)
19661da177e4SLinus Torvalds {
1967463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk;
1968c25eb3bfSEric Dumazet 	struct hlist_nulls_node *node;
19691da177e4SLinus Torvalds 	struct sock *sk = cur;
19705caea4eaSEric Dumazet 	struct inet_listen_hashbucket *ilb;
19711da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
1972a4146b1bSDenis V. Lunev 	struct net *net = seq_file_net(seq);
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	if (!sk) {
19751da177e4SLinus Torvalds 		st->bucket = 0;
19765caea4eaSEric Dumazet 		ilb = &tcp_hashinfo.listening_hash[0];
19775caea4eaSEric Dumazet 		spin_lock_bh(&ilb->lock);
1978c25eb3bfSEric Dumazet 		sk = sk_nulls_head(&ilb->head);
19791da177e4SLinus Torvalds 		goto get_sk;
19801da177e4SLinus Torvalds 	}
19815caea4eaSEric Dumazet 	ilb = &tcp_hashinfo.listening_hash[st->bucket];
19821da177e4SLinus Torvalds 	++st->num;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	if (st->state == TCP_SEQ_STATE_OPENREQ) {
198560236fddSArnaldo Carvalho de Melo 		struct request_sock *req = cur;
19861da177e4SLinus Torvalds 
1987463c84b9SArnaldo Carvalho de Melo 		icsk = inet_csk(st->syn_wait_sk);
19881da177e4SLinus Torvalds 		req = req->dl_next;
19891da177e4SLinus Torvalds 		while (1) {
19901da177e4SLinus Torvalds 			while (req) {
1991bdccc4caSDaniel Lezcano 				if (req->rsk_ops->family == st->family) {
19921da177e4SLinus Torvalds 					cur = req;
19931da177e4SLinus Torvalds 					goto out;
19941da177e4SLinus Torvalds 				}
19951da177e4SLinus Torvalds 				req = req->dl_next;
19961da177e4SLinus Torvalds 			}
199772a3effaSEric Dumazet 			if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
19981da177e4SLinus Torvalds 				break;
19991da177e4SLinus Torvalds get_req:
2000463c84b9SArnaldo Carvalho de Melo 			req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
20011da177e4SLinus Torvalds 		}
20021da177e4SLinus Torvalds 		sk	  = sk_next(st->syn_wait_sk);
20031da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_LISTENING;
2004463c84b9SArnaldo Carvalho de Melo 		read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
20051da177e4SLinus Torvalds 	} else {
2006463c84b9SArnaldo Carvalho de Melo 		icsk = inet_csk(sk);
2007463c84b9SArnaldo Carvalho de Melo 		read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
2008463c84b9SArnaldo Carvalho de Melo 		if (reqsk_queue_len(&icsk->icsk_accept_queue))
20091da177e4SLinus Torvalds 			goto start_req;
2010463c84b9SArnaldo Carvalho de Melo 		read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
20111da177e4SLinus Torvalds 		sk = sk_next(sk);
20121da177e4SLinus Torvalds 	}
20131da177e4SLinus Torvalds get_sk:
2014c25eb3bfSEric Dumazet 	sk_nulls_for_each_from(sk, node) {
2015878628fbSYOSHIFUJI Hideaki 		if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) {
20161da177e4SLinus Torvalds 			cur = sk;
20171da177e4SLinus Torvalds 			goto out;
20181da177e4SLinus Torvalds 		}
2019463c84b9SArnaldo Carvalho de Melo 		icsk = inet_csk(sk);
2020463c84b9SArnaldo Carvalho de Melo 		read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
2021463c84b9SArnaldo Carvalho de Melo 		if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
20221da177e4SLinus Torvalds start_req:
20231da177e4SLinus Torvalds 			st->uid		= sock_i_uid(sk);
20241da177e4SLinus Torvalds 			st->syn_wait_sk = sk;
20251da177e4SLinus Torvalds 			st->state	= TCP_SEQ_STATE_OPENREQ;
20261da177e4SLinus Torvalds 			st->sbucket	= 0;
20271da177e4SLinus Torvalds 			goto get_req;
20281da177e4SLinus Torvalds 		}
2029463c84b9SArnaldo Carvalho de Melo 		read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
20301da177e4SLinus Torvalds 	}
20315caea4eaSEric Dumazet 	spin_unlock_bh(&ilb->lock);
20320f7ff927SArnaldo Carvalho de Melo 	if (++st->bucket < INET_LHTABLE_SIZE) {
20335caea4eaSEric Dumazet 		ilb = &tcp_hashinfo.listening_hash[st->bucket];
20345caea4eaSEric Dumazet 		spin_lock_bh(&ilb->lock);
2035c25eb3bfSEric Dumazet 		sk = sk_nulls_head(&ilb->head);
20361da177e4SLinus Torvalds 		goto get_sk;
20371da177e4SLinus Torvalds 	}
20381da177e4SLinus Torvalds 	cur = NULL;
20391da177e4SLinus Torvalds out:
20401da177e4SLinus Torvalds 	return cur;
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
20441da177e4SLinus Torvalds {
20451da177e4SLinus Torvalds 	void *rc = listening_get_next(seq, NULL);
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	while (rc && *pos) {
20481da177e4SLinus Torvalds 		rc = listening_get_next(seq, rc);
20491da177e4SLinus Torvalds 		--*pos;
20501da177e4SLinus Torvalds 	}
20511da177e4SLinus Torvalds 	return rc;
20521da177e4SLinus Torvalds }
20531da177e4SLinus Torvalds 
20546eac5604SAndi Kleen static inline int empty_bucket(struct tcp_iter_state *st)
20556eac5604SAndi Kleen {
20563ab5aee7SEric Dumazet 	return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain) &&
20573ab5aee7SEric Dumazet 		hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].twchain);
20586eac5604SAndi Kleen }
20596eac5604SAndi Kleen 
20601da177e4SLinus Torvalds static void *established_get_first(struct seq_file *seq)
20611da177e4SLinus Torvalds {
20621da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
2063a4146b1bSDenis V. Lunev 	struct net *net = seq_file_net(seq);
20641da177e4SLinus Torvalds 	void *rc = NULL;
20651da177e4SLinus Torvalds 
2066f373b53bSEric Dumazet 	for (st->bucket = 0; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
20671da177e4SLinus Torvalds 		struct sock *sk;
20683ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
20698feaf0c0SArnaldo Carvalho de Melo 		struct inet_timewait_sock *tw;
20709db66bdcSEric Dumazet 		spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
20711da177e4SLinus Torvalds 
20726eac5604SAndi Kleen 		/* Lockless fast path for the common case of empty buckets */
20736eac5604SAndi Kleen 		if (empty_bucket(st))
20746eac5604SAndi Kleen 			continue;
20756eac5604SAndi Kleen 
20769db66bdcSEric Dumazet 		spin_lock_bh(lock);
20773ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
2078f40c8174SDaniel Lezcano 			if (sk->sk_family != st->family ||
2079878628fbSYOSHIFUJI Hideaki 			    !net_eq(sock_net(sk), net)) {
20801da177e4SLinus Torvalds 				continue;
20811da177e4SLinus Torvalds 			}
20821da177e4SLinus Torvalds 			rc = sk;
20831da177e4SLinus Torvalds 			goto out;
20841da177e4SLinus Torvalds 		}
20851da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_TIME_WAIT;
20868feaf0c0SArnaldo Carvalho de Melo 		inet_twsk_for_each(tw, node,
2087dbca9b27SEric Dumazet 				   &tcp_hashinfo.ehash[st->bucket].twchain) {
208828518fc1SPavel Emelyanov 			if (tw->tw_family != st->family ||
2089878628fbSYOSHIFUJI Hideaki 			    !net_eq(twsk_net(tw), net)) {
20901da177e4SLinus Torvalds 				continue;
20911da177e4SLinus Torvalds 			}
20921da177e4SLinus Torvalds 			rc = tw;
20931da177e4SLinus Torvalds 			goto out;
20941da177e4SLinus Torvalds 		}
20959db66bdcSEric Dumazet 		spin_unlock_bh(lock);
20961da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_ESTABLISHED;
20971da177e4SLinus Torvalds 	}
20981da177e4SLinus Torvalds out:
20991da177e4SLinus Torvalds 	return rc;
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds static void *established_get_next(struct seq_file *seq, void *cur)
21031da177e4SLinus Torvalds {
21041da177e4SLinus Torvalds 	struct sock *sk = cur;
21058feaf0c0SArnaldo Carvalho de Melo 	struct inet_timewait_sock *tw;
21063ab5aee7SEric Dumazet 	struct hlist_nulls_node *node;
21071da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
2108a4146b1bSDenis V. Lunev 	struct net *net = seq_file_net(seq);
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	++st->num;
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
21131da177e4SLinus Torvalds 		tw = cur;
21141da177e4SLinus Torvalds 		tw = tw_next(tw);
21151da177e4SLinus Torvalds get_tw:
2116878628fbSYOSHIFUJI Hideaki 		while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
21171da177e4SLinus Torvalds 			tw = tw_next(tw);
21181da177e4SLinus Torvalds 		}
21191da177e4SLinus Torvalds 		if (tw) {
21201da177e4SLinus Torvalds 			cur = tw;
21211da177e4SLinus Torvalds 			goto out;
21221da177e4SLinus Torvalds 		}
21239db66bdcSEric Dumazet 		spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
21241da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_ESTABLISHED;
21251da177e4SLinus Torvalds 
21266eac5604SAndi Kleen 		/* Look for next non empty bucket */
2127f373b53bSEric Dumazet 		while (++st->bucket <= tcp_hashinfo.ehash_mask &&
21286eac5604SAndi Kleen 				empty_bucket(st))
21296eac5604SAndi Kleen 			;
2130f373b53bSEric Dumazet 		if (st->bucket > tcp_hashinfo.ehash_mask)
21316eac5604SAndi Kleen 			return NULL;
21326eac5604SAndi Kleen 
21339db66bdcSEric Dumazet 		spin_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
21343ab5aee7SEric Dumazet 		sk = sk_nulls_head(&tcp_hashinfo.ehash[st->bucket].chain);
21351da177e4SLinus Torvalds 	} else
21363ab5aee7SEric Dumazet 		sk = sk_nulls_next(sk);
21371da177e4SLinus Torvalds 
21383ab5aee7SEric Dumazet 	sk_nulls_for_each_from(sk, node) {
2139878628fbSYOSHIFUJI Hideaki 		if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
21401da177e4SLinus Torvalds 			goto found;
21411da177e4SLinus Torvalds 	}
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	st->state = TCP_SEQ_STATE_TIME_WAIT;
2144dbca9b27SEric Dumazet 	tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
21451da177e4SLinus Torvalds 	goto get_tw;
21461da177e4SLinus Torvalds found:
21471da177e4SLinus Torvalds 	cur = sk;
21481da177e4SLinus Torvalds out:
21491da177e4SLinus Torvalds 	return cur;
21501da177e4SLinus Torvalds }
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds static void *established_get_idx(struct seq_file *seq, loff_t pos)
21531da177e4SLinus Torvalds {
21541da177e4SLinus Torvalds 	void *rc = established_get_first(seq);
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	while (rc && pos) {
21571da177e4SLinus Torvalds 		rc = established_get_next(seq, rc);
21581da177e4SLinus Torvalds 		--pos;
21591da177e4SLinus Torvalds 	}
21601da177e4SLinus Torvalds 	return rc;
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
21641da177e4SLinus Torvalds {
21651da177e4SLinus Torvalds 	void *rc;
21661da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	st->state = TCP_SEQ_STATE_LISTENING;
21691da177e4SLinus Torvalds 	rc	  = listening_get_idx(seq, &pos);
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	if (!rc) {
21721da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_ESTABLISHED;
21731da177e4SLinus Torvalds 		rc	  = established_get_idx(seq, pos);
21741da177e4SLinus Torvalds 	}
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	return rc;
21771da177e4SLinus Torvalds }
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
21801da177e4SLinus Torvalds {
21811da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
21821da177e4SLinus Torvalds 	st->state = TCP_SEQ_STATE_LISTENING;
21831da177e4SLinus Torvalds 	st->num = 0;
21841da177e4SLinus Torvalds 	return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
21851da177e4SLinus Torvalds }
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
21881da177e4SLinus Torvalds {
21891da177e4SLinus Torvalds 	void *rc = NULL;
21901da177e4SLinus Torvalds 	struct tcp_iter_state *st;
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
21931da177e4SLinus Torvalds 		rc = tcp_get_idx(seq, 0);
21941da177e4SLinus Torvalds 		goto out;
21951da177e4SLinus Torvalds 	}
21961da177e4SLinus Torvalds 	st = seq->private;
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 	switch (st->state) {
21991da177e4SLinus Torvalds 	case TCP_SEQ_STATE_OPENREQ:
22001da177e4SLinus Torvalds 	case TCP_SEQ_STATE_LISTENING:
22011da177e4SLinus Torvalds 		rc = listening_get_next(seq, v);
22021da177e4SLinus Torvalds 		if (!rc) {
22031da177e4SLinus Torvalds 			st->state = TCP_SEQ_STATE_ESTABLISHED;
22041da177e4SLinus Torvalds 			rc	  = established_get_first(seq);
22051da177e4SLinus Torvalds 		}
22061da177e4SLinus Torvalds 		break;
22071da177e4SLinus Torvalds 	case TCP_SEQ_STATE_ESTABLISHED:
22081da177e4SLinus Torvalds 	case TCP_SEQ_STATE_TIME_WAIT:
22091da177e4SLinus Torvalds 		rc = established_get_next(seq, v);
22101da177e4SLinus Torvalds 		break;
22111da177e4SLinus Torvalds 	}
22121da177e4SLinus Torvalds out:
22131da177e4SLinus Torvalds 	++*pos;
22141da177e4SLinus Torvalds 	return rc;
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds static void tcp_seq_stop(struct seq_file *seq, void *v)
22181da177e4SLinus Torvalds {
22191da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	switch (st->state) {
22221da177e4SLinus Torvalds 	case TCP_SEQ_STATE_OPENREQ:
22231da177e4SLinus Torvalds 		if (v) {
2224463c84b9SArnaldo Carvalho de Melo 			struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
2225463c84b9SArnaldo Carvalho de Melo 			read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
22261da177e4SLinus Torvalds 		}
22271da177e4SLinus Torvalds 	case TCP_SEQ_STATE_LISTENING:
22281da177e4SLinus Torvalds 		if (v != SEQ_START_TOKEN)
22295caea4eaSEric Dumazet 			spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
22301da177e4SLinus Torvalds 		break;
22311da177e4SLinus Torvalds 	case TCP_SEQ_STATE_TIME_WAIT:
22321da177e4SLinus Torvalds 	case TCP_SEQ_STATE_ESTABLISHED:
22331da177e4SLinus Torvalds 		if (v)
22349db66bdcSEric Dumazet 			spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
22351da177e4SLinus Torvalds 		break;
22361da177e4SLinus Torvalds 	}
22371da177e4SLinus Torvalds }
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds static int tcp_seq_open(struct inode *inode, struct file *file)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
22421da177e4SLinus Torvalds 	struct tcp_iter_state *s;
224352d6f3f1SDenis V. Lunev 	int err;
22441da177e4SLinus Torvalds 
224552d6f3f1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
224652d6f3f1SDenis V. Lunev 			  sizeof(struct tcp_iter_state));
224752d6f3f1SDenis V. Lunev 	if (err < 0)
224852d6f3f1SDenis V. Lunev 		return err;
2249f40c8174SDaniel Lezcano 
225052d6f3f1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
22511da177e4SLinus Torvalds 	s->family		= afinfo->family;
2252f40c8174SDaniel Lezcano 	return 0;
2253f40c8174SDaniel Lezcano }
2254f40c8174SDaniel Lezcano 
22556f8b13bcSDaniel Lezcano int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	int rc = 0;
22581da177e4SLinus Torvalds 	struct proc_dir_entry *p;
22591da177e4SLinus Torvalds 
226068fcadd1SDenis V. Lunev 	afinfo->seq_fops.open		= tcp_seq_open;
226168fcadd1SDenis V. Lunev 	afinfo->seq_fops.read		= seq_read;
226268fcadd1SDenis V. Lunev 	afinfo->seq_fops.llseek		= seq_lseek;
226368fcadd1SDenis V. Lunev 	afinfo->seq_fops.release	= seq_release_net;
22641da177e4SLinus Torvalds 
22659427c4b3SDenis V. Lunev 	afinfo->seq_ops.start		= tcp_seq_start;
22669427c4b3SDenis V. Lunev 	afinfo->seq_ops.next		= tcp_seq_next;
22679427c4b3SDenis V. Lunev 	afinfo->seq_ops.stop		= tcp_seq_stop;
22689427c4b3SDenis V. Lunev 
226984841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
227084841c3cSDenis V. Lunev 			     &afinfo->seq_fops, afinfo);
227184841c3cSDenis V. Lunev 	if (!p)
22721da177e4SLinus Torvalds 		rc = -ENOMEM;
22731da177e4SLinus Torvalds 	return rc;
22741da177e4SLinus Torvalds }
22751da177e4SLinus Torvalds 
22766f8b13bcSDaniel Lezcano void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
22771da177e4SLinus Torvalds {
22786f8b13bcSDaniel Lezcano 	proc_net_remove(net, afinfo->name);
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds 
228160236fddSArnaldo Carvalho de Melo static void get_openreq4(struct sock *sk, struct request_sock *req,
22825e659e4cSPavel Emelyanov 			 struct seq_file *f, int i, int uid, int *len)
22831da177e4SLinus Torvalds {
22842e6599cbSArnaldo Carvalho de Melo 	const struct inet_request_sock *ireq = inet_rsk(req);
22851da177e4SLinus Torvalds 	int ttd = req->expires - jiffies;
22861da177e4SLinus Torvalds 
22875e659e4cSPavel Emelyanov 	seq_printf(f, "%4d: %08X:%04X %08X:%04X"
22885e659e4cSPavel Emelyanov 		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n",
22891da177e4SLinus Torvalds 		i,
22902e6599cbSArnaldo Carvalho de Melo 		ireq->loc_addr,
2291c720c7e8SEric Dumazet 		ntohs(inet_sk(sk)->inet_sport),
22922e6599cbSArnaldo Carvalho de Melo 		ireq->rmt_addr,
22932e6599cbSArnaldo Carvalho de Melo 		ntohs(ireq->rmt_port),
22941da177e4SLinus Torvalds 		TCP_SYN_RECV,
22951da177e4SLinus Torvalds 		0, 0, /* could print option size, but that is af dependent. */
22961da177e4SLinus Torvalds 		1,    /* timers active (only the expire timer) */
22971da177e4SLinus Torvalds 		jiffies_to_clock_t(ttd),
22981da177e4SLinus Torvalds 		req->retrans,
22991da177e4SLinus Torvalds 		uid,
23001da177e4SLinus Torvalds 		0,  /* non standard timer */
23011da177e4SLinus Torvalds 		0, /* open_requests have no inode */
23021da177e4SLinus Torvalds 		atomic_read(&sk->sk_refcnt),
23035e659e4cSPavel Emelyanov 		req,
23045e659e4cSPavel Emelyanov 		len);
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
23075e659e4cSPavel Emelyanov static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
23081da177e4SLinus Torvalds {
23091da177e4SLinus Torvalds 	int timer_active;
23101da177e4SLinus Torvalds 	unsigned long timer_expires;
2311cf4c6bf8SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
2312cf4c6bf8SIlpo Järvinen 	const struct inet_connection_sock *icsk = inet_csk(sk);
2313cf4c6bf8SIlpo Järvinen 	struct inet_sock *inet = inet_sk(sk);
2314c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2315c720c7e8SEric Dumazet 	__be32 src = inet->inet_rcv_saddr;
2316c720c7e8SEric Dumazet 	__u16 destp = ntohs(inet->inet_dport);
2317c720c7e8SEric Dumazet 	__u16 srcp = ntohs(inet->inet_sport);
231849d09007SEric Dumazet 	int rx_queue;
23191da177e4SLinus Torvalds 
2320463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
23211da177e4SLinus Torvalds 		timer_active	= 1;
2322463c84b9SArnaldo Carvalho de Melo 		timer_expires	= icsk->icsk_timeout;
2323463c84b9SArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
23241da177e4SLinus Torvalds 		timer_active	= 4;
2325463c84b9SArnaldo Carvalho de Melo 		timer_expires	= icsk->icsk_timeout;
2326cf4c6bf8SIlpo Järvinen 	} else if (timer_pending(&sk->sk_timer)) {
23271da177e4SLinus Torvalds 		timer_active	= 2;
2328cf4c6bf8SIlpo Järvinen 		timer_expires	= sk->sk_timer.expires;
23291da177e4SLinus Torvalds 	} else {
23301da177e4SLinus Torvalds 		timer_active	= 0;
23311da177e4SLinus Torvalds 		timer_expires = jiffies;
23321da177e4SLinus Torvalds 	}
23331da177e4SLinus Torvalds 
233449d09007SEric Dumazet 	if (sk->sk_state == TCP_LISTEN)
233549d09007SEric Dumazet 		rx_queue = sk->sk_ack_backlog;
233649d09007SEric Dumazet 	else
233749d09007SEric Dumazet 		/*
233849d09007SEric Dumazet 		 * because we dont lock socket, we might find a transient negative value
233949d09007SEric Dumazet 		 */
234049d09007SEric Dumazet 		rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
234149d09007SEric Dumazet 
23425e659e4cSPavel Emelyanov 	seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
23437be87351SStephen Hemminger 			"%08X %5d %8d %lu %d %p %lu %lu %u %u %d%n",
2344cf4c6bf8SIlpo Järvinen 		i, src, srcp, dest, destp, sk->sk_state,
234547da8ee6SSridhar Samudrala 		tp->write_seq - tp->snd_una,
234649d09007SEric Dumazet 		rx_queue,
23471da177e4SLinus Torvalds 		timer_active,
23481da177e4SLinus Torvalds 		jiffies_to_clock_t(timer_expires - jiffies),
2349463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_retransmits,
2350cf4c6bf8SIlpo Järvinen 		sock_i_uid(sk),
23516687e988SArnaldo Carvalho de Melo 		icsk->icsk_probes_out,
2352cf4c6bf8SIlpo Järvinen 		sock_i_ino(sk),
2353cf4c6bf8SIlpo Järvinen 		atomic_read(&sk->sk_refcnt), sk,
23547be87351SStephen Hemminger 		jiffies_to_clock_t(icsk->icsk_rto),
23557be87351SStephen Hemminger 		jiffies_to_clock_t(icsk->icsk_ack.ato),
2356463c84b9SArnaldo Carvalho de Melo 		(icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
23571da177e4SLinus Torvalds 		tp->snd_cwnd,
23580b6a05c1SIlpo Järvinen 		tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh,
23595e659e4cSPavel Emelyanov 		len);
23601da177e4SLinus Torvalds }
23611da177e4SLinus Torvalds 
23627174259eSArnaldo Carvalho de Melo static void get_timewait4_sock(struct inet_timewait_sock *tw,
23635e659e4cSPavel Emelyanov 			       struct seq_file *f, int i, int *len)
23641da177e4SLinus Torvalds {
236523f33c2dSAl Viro 	__be32 dest, src;
23661da177e4SLinus Torvalds 	__u16 destp, srcp;
23671da177e4SLinus Torvalds 	int ttd = tw->tw_ttd - jiffies;
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	if (ttd < 0)
23701da177e4SLinus Torvalds 		ttd = 0;
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	dest  = tw->tw_daddr;
23731da177e4SLinus Torvalds 	src   = tw->tw_rcv_saddr;
23741da177e4SLinus Torvalds 	destp = ntohs(tw->tw_dport);
23751da177e4SLinus Torvalds 	srcp  = ntohs(tw->tw_sport);
23761da177e4SLinus Torvalds 
23775e659e4cSPavel Emelyanov 	seq_printf(f, "%4d: %08X:%04X %08X:%04X"
23785e659e4cSPavel Emelyanov 		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n",
23791da177e4SLinus Torvalds 		i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
23801da177e4SLinus Torvalds 		3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
23815e659e4cSPavel Emelyanov 		atomic_read(&tw->tw_refcnt), tw, len);
23821da177e4SLinus Torvalds }
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds #define TMPSZ 150
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds static int tcp4_seq_show(struct seq_file *seq, void *v)
23871da177e4SLinus Torvalds {
23881da177e4SLinus Torvalds 	struct tcp_iter_state *st;
23895e659e4cSPavel Emelyanov 	int len;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
23921da177e4SLinus Torvalds 		seq_printf(seq, "%-*s\n", TMPSZ - 1,
23931da177e4SLinus Torvalds 			   "  sl  local_address rem_address   st tx_queue "
23941da177e4SLinus Torvalds 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
23951da177e4SLinus Torvalds 			   "inode");
23961da177e4SLinus Torvalds 		goto out;
23971da177e4SLinus Torvalds 	}
23981da177e4SLinus Torvalds 	st = seq->private;
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 	switch (st->state) {
24011da177e4SLinus Torvalds 	case TCP_SEQ_STATE_LISTENING:
24021da177e4SLinus Torvalds 	case TCP_SEQ_STATE_ESTABLISHED:
24035e659e4cSPavel Emelyanov 		get_tcp4_sock(v, seq, st->num, &len);
24041da177e4SLinus Torvalds 		break;
24051da177e4SLinus Torvalds 	case TCP_SEQ_STATE_OPENREQ:
24065e659e4cSPavel Emelyanov 		get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
24071da177e4SLinus Torvalds 		break;
24081da177e4SLinus Torvalds 	case TCP_SEQ_STATE_TIME_WAIT:
24095e659e4cSPavel Emelyanov 		get_timewait4_sock(v, seq, st->num, &len);
24101da177e4SLinus Torvalds 		break;
24111da177e4SLinus Torvalds 	}
24125e659e4cSPavel Emelyanov 	seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
24131da177e4SLinus Torvalds out:
24141da177e4SLinus Torvalds 	return 0;
24151da177e4SLinus Torvalds }
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds static struct tcp_seq_afinfo tcp4_seq_afinfo = {
24181da177e4SLinus Torvalds 	.name		= "tcp",
24191da177e4SLinus Torvalds 	.family		= AF_INET,
24205f4472c5SDenis V. Lunev 	.seq_fops	= {
24215f4472c5SDenis V. Lunev 		.owner		= THIS_MODULE,
24225f4472c5SDenis V. Lunev 	},
24239427c4b3SDenis V. Lunev 	.seq_ops	= {
24249427c4b3SDenis V. Lunev 		.show		= tcp4_seq_show,
24259427c4b3SDenis V. Lunev 	},
24261da177e4SLinus Torvalds };
24271da177e4SLinus Torvalds 
2428757764f6SPavel Emelyanov static int tcp4_proc_init_net(struct net *net)
2429757764f6SPavel Emelyanov {
2430757764f6SPavel Emelyanov 	return tcp_proc_register(net, &tcp4_seq_afinfo);
2431757764f6SPavel Emelyanov }
2432757764f6SPavel Emelyanov 
2433757764f6SPavel Emelyanov static void tcp4_proc_exit_net(struct net *net)
2434757764f6SPavel Emelyanov {
2435757764f6SPavel Emelyanov 	tcp_proc_unregister(net, &tcp4_seq_afinfo);
2436757764f6SPavel Emelyanov }
2437757764f6SPavel Emelyanov 
2438757764f6SPavel Emelyanov static struct pernet_operations tcp4_net_ops = {
2439757764f6SPavel Emelyanov 	.init = tcp4_proc_init_net,
2440757764f6SPavel Emelyanov 	.exit = tcp4_proc_exit_net,
2441757764f6SPavel Emelyanov };
2442757764f6SPavel Emelyanov 
24431da177e4SLinus Torvalds int __init tcp4_proc_init(void)
24441da177e4SLinus Torvalds {
2445757764f6SPavel Emelyanov 	return register_pernet_subsys(&tcp4_net_ops);
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds void tcp4_proc_exit(void)
24491da177e4SLinus Torvalds {
2450757764f6SPavel Emelyanov 	unregister_pernet_subsys(&tcp4_net_ops);
24511da177e4SLinus Torvalds }
24521da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
24531da177e4SLinus Torvalds 
2454bf296b12SHerbert Xu struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2455bf296b12SHerbert Xu {
245636e7b1b8SHerbert Xu 	struct iphdr *iph = skb_gro_network_header(skb);
2457bf296b12SHerbert Xu 
2458bf296b12SHerbert Xu 	switch (skb->ip_summed) {
2459bf296b12SHerbert Xu 	case CHECKSUM_COMPLETE:
246086911732SHerbert Xu 		if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
2461bf296b12SHerbert Xu 				  skb->csum)) {
2462bf296b12SHerbert Xu 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2463bf296b12SHerbert Xu 			break;
2464bf296b12SHerbert Xu 		}
2465bf296b12SHerbert Xu 
2466bf296b12SHerbert Xu 		/* fall through */
2467bf296b12SHerbert Xu 	case CHECKSUM_NONE:
2468bf296b12SHerbert Xu 		NAPI_GRO_CB(skb)->flush = 1;
2469bf296b12SHerbert Xu 		return NULL;
2470bf296b12SHerbert Xu 	}
2471bf296b12SHerbert Xu 
2472bf296b12SHerbert Xu 	return tcp_gro_receive(head, skb);
2473bf296b12SHerbert Xu }
2474bf296b12SHerbert Xu EXPORT_SYMBOL(tcp4_gro_receive);
2475bf296b12SHerbert Xu 
2476bf296b12SHerbert Xu int tcp4_gro_complete(struct sk_buff *skb)
2477bf296b12SHerbert Xu {
2478bf296b12SHerbert Xu 	struct iphdr *iph = ip_hdr(skb);
2479bf296b12SHerbert Xu 	struct tcphdr *th = tcp_hdr(skb);
2480bf296b12SHerbert Xu 
2481bf296b12SHerbert Xu 	th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
2482bf296b12SHerbert Xu 				  iph->saddr, iph->daddr, 0);
2483bf296b12SHerbert Xu 	skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
2484bf296b12SHerbert Xu 
2485bf296b12SHerbert Xu 	return tcp_gro_complete(skb);
2486bf296b12SHerbert Xu }
2487bf296b12SHerbert Xu EXPORT_SYMBOL(tcp4_gro_complete);
2488bf296b12SHerbert Xu 
24891da177e4SLinus Torvalds struct proto tcp_prot = {
24901da177e4SLinus Torvalds 	.name			= "TCP",
24911da177e4SLinus Torvalds 	.owner			= THIS_MODULE,
24921da177e4SLinus Torvalds 	.close			= tcp_close,
24931da177e4SLinus Torvalds 	.connect		= tcp_v4_connect,
24941da177e4SLinus Torvalds 	.disconnect		= tcp_disconnect,
2495463c84b9SArnaldo Carvalho de Melo 	.accept			= inet_csk_accept,
24961da177e4SLinus Torvalds 	.ioctl			= tcp_ioctl,
24971da177e4SLinus Torvalds 	.init			= tcp_v4_init_sock,
24981da177e4SLinus Torvalds 	.destroy		= tcp_v4_destroy_sock,
24991da177e4SLinus Torvalds 	.shutdown		= tcp_shutdown,
25001da177e4SLinus Torvalds 	.setsockopt		= tcp_setsockopt,
25011da177e4SLinus Torvalds 	.getsockopt		= tcp_getsockopt,
25021da177e4SLinus Torvalds 	.recvmsg		= tcp_recvmsg,
25031da177e4SLinus Torvalds 	.backlog_rcv		= tcp_v4_do_rcv,
2504ab1e0a13SArnaldo Carvalho de Melo 	.hash			= inet_hash,
2505ab1e0a13SArnaldo Carvalho de Melo 	.unhash			= inet_unhash,
2506ab1e0a13SArnaldo Carvalho de Melo 	.get_port		= inet_csk_get_port,
25071da177e4SLinus Torvalds 	.enter_memory_pressure	= tcp_enter_memory_pressure,
25081da177e4SLinus Torvalds 	.sockets_allocated	= &tcp_sockets_allocated,
25090a5578cfSArnaldo Carvalho de Melo 	.orphan_count		= &tcp_orphan_count,
25101da177e4SLinus Torvalds 	.memory_allocated	= &tcp_memory_allocated,
25111da177e4SLinus Torvalds 	.memory_pressure	= &tcp_memory_pressure,
25121da177e4SLinus Torvalds 	.sysctl_mem		= sysctl_tcp_mem,
25131da177e4SLinus Torvalds 	.sysctl_wmem		= sysctl_tcp_wmem,
25141da177e4SLinus Torvalds 	.sysctl_rmem		= sysctl_tcp_rmem,
25151da177e4SLinus Torvalds 	.max_header		= MAX_TCP_HEADER,
25161da177e4SLinus Torvalds 	.obj_size		= sizeof(struct tcp_sock),
25173ab5aee7SEric Dumazet 	.slab_flags		= SLAB_DESTROY_BY_RCU,
25186d6ee43eSArnaldo Carvalho de Melo 	.twsk_prot		= &tcp_timewait_sock_ops,
251960236fddSArnaldo Carvalho de Melo 	.rsk_prot		= &tcp_request_sock_ops,
252039d8cda7SPavel Emelyanov 	.h.hashinfo		= &tcp_hashinfo,
2521543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
2522543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt	= compat_tcp_setsockopt,
2523543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt	= compat_tcp_getsockopt,
2524543d9cfeSArnaldo Carvalho de Melo #endif
25251da177e4SLinus Torvalds };
25261da177e4SLinus Torvalds 
2527046ee902SDenis V. Lunev 
2528046ee902SDenis V. Lunev static int __net_init tcp_sk_init(struct net *net)
2529046ee902SDenis V. Lunev {
2530046ee902SDenis V. Lunev 	return inet_ctl_sock_create(&net->ipv4.tcp_sock,
2531046ee902SDenis V. Lunev 				    PF_INET, SOCK_RAW, IPPROTO_TCP, net);
2532046ee902SDenis V. Lunev }
2533046ee902SDenis V. Lunev 
2534046ee902SDenis V. Lunev static void __net_exit tcp_sk_exit(struct net *net)
2535046ee902SDenis V. Lunev {
2536046ee902SDenis V. Lunev 	inet_ctl_sock_destroy(net->ipv4.tcp_sock);
2537b099ce26SEric W. Biederman }
2538b099ce26SEric W. Biederman 
2539b099ce26SEric W. Biederman static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
2540b099ce26SEric W. Biederman {
2541b099ce26SEric W. Biederman 	inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET);
2542046ee902SDenis V. Lunev }
2543046ee902SDenis V. Lunev 
2544046ee902SDenis V. Lunev static struct pernet_operations __net_initdata tcp_sk_ops = {
2545046ee902SDenis V. Lunev        .init	   = tcp_sk_init,
2546046ee902SDenis V. Lunev        .exit	   = tcp_sk_exit,
2547b099ce26SEric W. Biederman        .exit_batch = tcp_sk_exit_batch,
2548046ee902SDenis V. Lunev };
2549046ee902SDenis V. Lunev 
25509b0f976fSDenis V. Lunev void __init tcp_v4_init(void)
25511da177e4SLinus Torvalds {
25525caea4eaSEric Dumazet 	inet_hashinfo_init(&tcp_hashinfo);
25536a1b3054SEric W. Biederman 	if (register_pernet_subsys(&tcp_sk_ops))
25541da177e4SLinus Torvalds 		panic("Failed to create the TCP control socket.\n");
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds EXPORT_SYMBOL(ipv4_specific);
25581da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_hashinfo);
25591da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_prot);
25601da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_conn_request);
25611da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_connect);
25621da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_do_rcv);
25631da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_remember_stamp);
25641da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_send_check);
25651da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
25681da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_proc_register);
25691da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_proc_unregister);
25701da177e4SLinus Torvalds #endif
25711da177e4SLinus Torvalds EXPORT_SYMBOL(sysctl_tcp_low_latency);
25721da177e4SLinus Torvalds 
2573