xref: /linux/net/ipv4/tcp_ipv4.c (revision 4957faade11b3a278c3b3cade3411ddc20afa791)
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 {
1216*4957faadSWilliam Allen Simpson 	struct tcp_extend_values tmp_ext;
12171da177e4SLinus Torvalds 	struct tcp_options_received tmp_opt;
1218*4957faadSWilliam Allen Simpson 	u8 *hash_location;
121960236fddSArnaldo Carvalho de Melo 	struct request_sock *req;
1220e6b4d113SWilliam Allen Simpson 	struct inet_request_sock *ireq;
1221*4957faadSWilliam 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 
1265022c3f7dSGilad Ben-Yossef 	ireq = inet_rsk(req);
1266022c3f7dSGilad Ben-Yossef 	ireq->loc_addr = daddr;
1267022c3f7dSGilad Ben-Yossef 	ireq->rmt_addr = saddr;
1268022c3f7dSGilad Ben-Yossef 	ireq->no_srccheck = inet_sk(sk)->transparent;
1269022c3f7dSGilad Ben-Yossef 	ireq->opt = tcp_v4_save_options(sk, skb);
1270022c3f7dSGilad Ben-Yossef 
1271022c3f7dSGilad Ben-Yossef 	dst = inet_csk_route_req(sk, req);
1272022c3f7dSGilad Ben-Yossef 	if(!dst)
1273022c3f7dSGilad Ben-Yossef 		goto drop_and_free;
1274022c3f7dSGilad Ben-Yossef 
12751da177e4SLinus Torvalds 	tcp_clear_options(&tmp_opt);
1276bee7ca9eSWilliam Allen Simpson 	tmp_opt.mss_clamp = TCP_MSS_DEFAULT;
1277*4957faadSWilliam Allen Simpson 	tmp_opt.user_mss  = tp->rx_opt.user_mss;
1278*4957faadSWilliam Allen Simpson 	tcp_parse_options(skb, &tmp_opt, &hash_location, 0, dst);
12791da177e4SLinus Torvalds 
1280*4957faadSWilliam Allen Simpson 	if (tmp_opt.cookie_plus > 0 &&
1281*4957faadSWilliam Allen Simpson 	    tmp_opt.saw_tstamp &&
1282*4957faadSWilliam Allen Simpson 	    !tp->rx_opt.cookie_out_never &&
1283*4957faadSWilliam Allen Simpson 	    (sysctl_tcp_cookie_size > 0 ||
1284*4957faadSWilliam Allen Simpson 	     (tp->cookie_values != NULL &&
1285*4957faadSWilliam Allen Simpson 	      tp->cookie_values->cookie_desired > 0))) {
1286*4957faadSWilliam Allen Simpson 		u8 *c;
1287*4957faadSWilliam Allen Simpson 		u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
1288*4957faadSWilliam Allen Simpson 		int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
1289*4957faadSWilliam Allen Simpson 
1290*4957faadSWilliam Allen Simpson 		if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
1291*4957faadSWilliam Allen Simpson 			goto drop_and_release;
1292*4957faadSWilliam Allen Simpson 
1293*4957faadSWilliam Allen Simpson 		/* Secret recipe starts with IP addresses */
1294*4957faadSWilliam Allen Simpson 		*mess++ ^= daddr;
1295*4957faadSWilliam Allen Simpson 		*mess++ ^= saddr;
1296*4957faadSWilliam Allen Simpson 
1297*4957faadSWilliam Allen Simpson 		/* plus variable length Initiator Cookie */
1298*4957faadSWilliam Allen Simpson 		c = (u8 *)mess;
1299*4957faadSWilliam Allen Simpson 		while (l-- > 0)
1300*4957faadSWilliam Allen Simpson 			*c++ ^= *hash_location++;
1301*4957faadSWilliam Allen Simpson 
1302*4957faadSWilliam Allen Simpson #ifdef CONFIG_SYN_COOKIES
1303*4957faadSWilliam Allen Simpson 		want_cookie = 0;	/* not our kind of cookie */
1304*4957faadSWilliam Allen Simpson #endif
1305*4957faadSWilliam Allen Simpson 		tmp_ext.cookie_out_never = 0; /* false */
1306*4957faadSWilliam Allen Simpson 		tmp_ext.cookie_plus = tmp_opt.cookie_plus;
1307*4957faadSWilliam Allen Simpson 	} else if (!tp->rx_opt.cookie_in_always) {
1308*4957faadSWilliam Allen Simpson 		/* redundant indications, but ensure initialization. */
1309*4957faadSWilliam Allen Simpson 		tmp_ext.cookie_out_never = 1; /* true */
1310*4957faadSWilliam Allen Simpson 		tmp_ext.cookie_plus = 0;
1311*4957faadSWilliam Allen Simpson 	} else {
1312*4957faadSWilliam Allen Simpson 		goto drop_and_release;
1313*4957faadSWilliam Allen Simpson 	}
1314*4957faadSWilliam Allen Simpson 	tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
13151da177e4SLinus Torvalds 
13164dfc2817SFlorian Westphal 	if (want_cookie && !tmp_opt.saw_tstamp)
13171da177e4SLinus Torvalds 		tcp_clear_options(&tmp_opt);
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
13201da177e4SLinus Torvalds 	tcp_openreq_init(req, &tmp_opt, skb);
13211da177e4SLinus Torvalds 
1322284904aaSPaul Moore 	if (security_inet_conn_request(sk, skb, req))
1323022c3f7dSGilad Ben-Yossef 		goto drop_and_release;
1324284904aaSPaul Moore 
13251da177e4SLinus Torvalds 	if (!want_cookie)
1326aa8223c7SArnaldo Carvalho de Melo 		TCP_ECN_create_request(req, tcp_hdr(skb));
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	if (want_cookie) {
13291da177e4SLinus Torvalds #ifdef CONFIG_SYN_COOKIES
13301da177e4SLinus Torvalds 		syn_flood_warning(skb);
13314dfc2817SFlorian Westphal 		req->cookie_ts = tmp_opt.tstamp_ok;
13321da177e4SLinus Torvalds #endif
13331da177e4SLinus Torvalds 		isn = cookie_v4_init_sequence(sk, skb, &req->mss);
13341da177e4SLinus Torvalds 	} else if (!isn) {
13351da177e4SLinus Torvalds 		struct inet_peer *peer = NULL;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 		/* VJ's idea. We save last timestamp seen
13381da177e4SLinus Torvalds 		 * from the destination in peer table, when entering
13391da177e4SLinus Torvalds 		 * state TIME-WAIT, and check against it before
13401da177e4SLinus Torvalds 		 * accepting new connection request.
13411da177e4SLinus Torvalds 		 *
13421da177e4SLinus Torvalds 		 * If "isn" is not zero, this request hit alive
13431da177e4SLinus Torvalds 		 * timewait bucket, so that all the necessary checks
13441da177e4SLinus Torvalds 		 * are made in the function processing timewait state.
13451da177e4SLinus Torvalds 		 */
13461da177e4SLinus Torvalds 		if (tmp_opt.saw_tstamp &&
1347295ff7edSArnaldo Carvalho de Melo 		    tcp_death_row.sysctl_tw_recycle &&
13481da177e4SLinus Torvalds 		    (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
13491da177e4SLinus Torvalds 		    peer->v4daddr == saddr) {
13502c1409a0SEric Dumazet 			if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
13511da177e4SLinus Torvalds 			    (s32)(peer->tcp_ts - req->ts_recent) >
13521da177e4SLinus Torvalds 							TCP_PAWS_WINDOW) {
1353de0744afSPavel Emelyanov 				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
13547cd04fa7SDenis V. Lunev 				goto drop_and_release;
13551da177e4SLinus Torvalds 			}
13561da177e4SLinus Torvalds 		}
13571da177e4SLinus Torvalds 		/* Kill the following clause, if you dislike this way. */
13581da177e4SLinus Torvalds 		else if (!sysctl_tcp_syncookies &&
1359463c84b9SArnaldo Carvalho de Melo 			 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
13601da177e4SLinus Torvalds 			  (sysctl_max_syn_backlog >> 2)) &&
13611da177e4SLinus Torvalds 			 (!peer || !peer->tcp_ts_stamp) &&
13621da177e4SLinus Torvalds 			 (!dst || !dst_metric(dst, RTAX_RTT))) {
13631da177e4SLinus Torvalds 			/* Without syncookies last quarter of
13641da177e4SLinus Torvalds 			 * backlog is filled with destinations,
13651da177e4SLinus Torvalds 			 * proven to be alive.
13661da177e4SLinus Torvalds 			 * It means that we continue to communicate
13671da177e4SLinus Torvalds 			 * to destinations, already remembered
13681da177e4SLinus Torvalds 			 * to the moment of synflood.
13691da177e4SLinus Torvalds 			 */
1370673d57e7SHarvey Harrison 			LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI4/%u\n",
1371673d57e7SHarvey Harrison 				       &saddr, ntohs(tcp_hdr(skb)->source));
13727cd04fa7SDenis V. Lunev 			goto drop_and_release;
13731da177e4SLinus Torvalds 		}
13741da177e4SLinus Torvalds 
1375a94f723dSGerrit Renker 		isn = tcp_v4_init_sequence(skb);
13761da177e4SLinus Torvalds 	}
13772e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->snt_isn = isn;
13781da177e4SLinus Torvalds 
1379*4957faadSWilliam Allen Simpson 	if (__tcp_v4_send_synack(sk, dst, req,
1380*4957faadSWilliam Allen Simpson 				 (struct request_values *)&tmp_ext) ||
1381*4957faadSWilliam Allen Simpson 	    want_cookie)
13821da177e4SLinus Torvalds 		goto drop_and_free;
13831da177e4SLinus Torvalds 
13843f421baaSArnaldo Carvalho de Melo 	inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
13851da177e4SLinus Torvalds 	return 0;
13861da177e4SLinus Torvalds 
13877cd04fa7SDenis V. Lunev drop_and_release:
13887cd04fa7SDenis V. Lunev 	dst_release(dst);
13891da177e4SLinus Torvalds drop_and_free:
139060236fddSArnaldo Carvalho de Melo 	reqsk_free(req);
13911da177e4SLinus Torvalds drop:
13921da177e4SLinus Torvalds 	return 0;
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds /*
13971da177e4SLinus Torvalds  * The three way handshake has completed - we got a valid synack -
13981da177e4SLinus Torvalds  * now create the new socket.
13991da177e4SLinus Torvalds  */
14001da177e4SLinus Torvalds struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
140160236fddSArnaldo Carvalho de Melo 				  struct request_sock *req,
14021da177e4SLinus Torvalds 				  struct dst_entry *dst)
14031da177e4SLinus Torvalds {
14042e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq;
14051da177e4SLinus Torvalds 	struct inet_sock *newinet;
14061da177e4SLinus Torvalds 	struct tcp_sock *newtp;
14071da177e4SLinus Torvalds 	struct sock *newsk;
1408cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1409cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key *key;
1410cfb6eeb4SYOSHIFUJI Hideaki #endif
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	if (sk_acceptq_is_full(sk))
14131da177e4SLinus Torvalds 		goto exit_overflow;
14141da177e4SLinus Torvalds 
1415463c84b9SArnaldo Carvalho de Melo 	if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
14161da177e4SLinus Torvalds 		goto exit;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	newsk = tcp_create_openreq_child(sk, req, skb);
14191da177e4SLinus Torvalds 	if (!newsk)
14201da177e4SLinus Torvalds 		goto exit;
14211da177e4SLinus Torvalds 
1422bcd76111SHerbert Xu 	newsk->sk_gso_type = SKB_GSO_TCPV4;
14236cbb0df7SArnaldo Carvalho de Melo 	sk_setup_caps(newsk, dst);
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	newtp		      = tcp_sk(newsk);
14261da177e4SLinus Torvalds 	newinet		      = inet_sk(newsk);
14272e6599cbSArnaldo Carvalho de Melo 	ireq		      = inet_rsk(req);
1428c720c7e8SEric Dumazet 	newinet->inet_daddr   = ireq->rmt_addr;
1429c720c7e8SEric Dumazet 	newinet->inet_rcv_saddr = ireq->loc_addr;
1430c720c7e8SEric Dumazet 	newinet->inet_saddr	      = ireq->loc_addr;
14312e6599cbSArnaldo Carvalho de Melo 	newinet->opt	      = ireq->opt;
14322e6599cbSArnaldo Carvalho de Melo 	ireq->opt	      = NULL;
1433463c84b9SArnaldo Carvalho de Melo 	newinet->mc_index     = inet_iif(skb);
1434eddc9ec5SArnaldo Carvalho de Melo 	newinet->mc_ttl	      = ip_hdr(skb)->ttl;
1435d83d8461SArnaldo Carvalho de Melo 	inet_csk(newsk)->icsk_ext_hdr_len = 0;
14361da177e4SLinus Torvalds 	if (newinet->opt)
1437d83d8461SArnaldo Carvalho de Melo 		inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
1438c720c7e8SEric Dumazet 	newinet->inet_id = newtp->write_seq ^ jiffies;
14391da177e4SLinus Torvalds 
14405d424d5aSJohn Heffner 	tcp_mtup_init(newsk);
14411da177e4SLinus Torvalds 	tcp_sync_mss(newsk, dst_mtu(dst));
14421da177e4SLinus Torvalds 	newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
1443f5fff5dcSTom Quetchenbach 	if (tcp_sk(sk)->rx_opt.user_mss &&
1444f5fff5dcSTom Quetchenbach 	    tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1445f5fff5dcSTom Quetchenbach 		newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1446f5fff5dcSTom Quetchenbach 
14471da177e4SLinus Torvalds 	tcp_initialize_rcv_mss(newsk);
14481da177e4SLinus Torvalds 
1449cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1450cfb6eeb4SYOSHIFUJI Hideaki 	/* Copy over the MD5 key from the original socket */
1451c720c7e8SEric Dumazet 	key = tcp_v4_md5_do_lookup(sk, newinet->inet_daddr);
1452c720c7e8SEric Dumazet 	if (key != NULL) {
1453cfb6eeb4SYOSHIFUJI Hideaki 		/*
1454cfb6eeb4SYOSHIFUJI Hideaki 		 * We're using one, so create a matching key
1455cfb6eeb4SYOSHIFUJI Hideaki 		 * on the newsk structure. If we fail to get
1456cfb6eeb4SYOSHIFUJI Hideaki 		 * memory, then we end up not copying the key
1457cfb6eeb4SYOSHIFUJI Hideaki 		 * across. Shucks.
1458cfb6eeb4SYOSHIFUJI Hideaki 		 */
1459f6685938SArnaldo Carvalho de Melo 		char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1460f6685938SArnaldo Carvalho de Melo 		if (newkey != NULL)
1461c720c7e8SEric Dumazet 			tcp_v4_md5_do_add(newsk, newinet->inet_daddr,
1462cfb6eeb4SYOSHIFUJI Hideaki 					  newkey, key->keylen);
146349a72dfbSAdam Langley 		newsk->sk_route_caps &= ~NETIF_F_GSO_MASK;
1464cfb6eeb4SYOSHIFUJI Hideaki 	}
1465cfb6eeb4SYOSHIFUJI Hideaki #endif
1466cfb6eeb4SYOSHIFUJI Hideaki 
1467ab1e0a13SArnaldo Carvalho de Melo 	__inet_hash_nolisten(newsk);
1468ab1e0a13SArnaldo Carvalho de Melo 	__inet_inherit_port(sk, newsk);
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 	return newsk;
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds exit_overflow:
1473de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
14741da177e4SLinus Torvalds exit:
1475de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
14761da177e4SLinus Torvalds 	dst_release(dst);
14771da177e4SLinus Torvalds 	return NULL;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
14811da177e4SLinus Torvalds {
1482aa8223c7SArnaldo Carvalho de Melo 	struct tcphdr *th = tcp_hdr(skb);
1483eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
14841da177e4SLinus Torvalds 	struct sock *nsk;
148560236fddSArnaldo Carvalho de Melo 	struct request_sock **prev;
14861da177e4SLinus Torvalds 	/* Find possible connection requests. */
1487463c84b9SArnaldo Carvalho de Melo 	struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
14881da177e4SLinus Torvalds 						       iph->saddr, iph->daddr);
14891da177e4SLinus Torvalds 	if (req)
14901da177e4SLinus Torvalds 		return tcp_check_req(sk, skb, req, prev);
14911da177e4SLinus Torvalds 
14923b1e0a65SYOSHIFUJI Hideaki 	nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
1493c67499c0SPavel Emelyanov 			th->source, iph->daddr, th->dest, inet_iif(skb));
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	if (nsk) {
14961da177e4SLinus Torvalds 		if (nsk->sk_state != TCP_TIME_WAIT) {
14971da177e4SLinus Torvalds 			bh_lock_sock(nsk);
14981da177e4SLinus Torvalds 			return nsk;
14991da177e4SLinus Torvalds 		}
15009469c7b4SYOSHIFUJI Hideaki 		inet_twsk_put(inet_twsk(nsk));
15011da177e4SLinus Torvalds 		return NULL;
15021da177e4SLinus Torvalds 	}
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds #ifdef CONFIG_SYN_COOKIES
15051da177e4SLinus Torvalds 	if (!th->rst && !th->syn && th->ack)
15061da177e4SLinus Torvalds 		sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
15071da177e4SLinus Torvalds #endif
15081da177e4SLinus Torvalds 	return sk;
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
1511b51655b9SAl Viro static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
15121da177e4SLinus Torvalds {
1513eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
1514eddc9ec5SArnaldo Carvalho de Melo 
151584fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
1516eddc9ec5SArnaldo Carvalho de Melo 		if (!tcp_v4_check(skb->len, iph->saddr,
1517eddc9ec5SArnaldo Carvalho de Melo 				  iph->daddr, skb->csum)) {
15181da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1519fb286bb2SHerbert Xu 			return 0;
1520fb286bb2SHerbert Xu 		}
1521fb286bb2SHerbert Xu 	}
1522fb286bb2SHerbert Xu 
1523eddc9ec5SArnaldo Carvalho de Melo 	skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
1524fb286bb2SHerbert Xu 				       skb->len, IPPROTO_TCP, 0);
1525fb286bb2SHerbert Xu 
1526fb286bb2SHerbert Xu 	if (skb->len <= 76) {
1527fb286bb2SHerbert Xu 		return __skb_checksum_complete(skb);
15281da177e4SLinus Torvalds 	}
15291da177e4SLinus Torvalds 	return 0;
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds /* The socket must have it's spinlock held when we get
15341da177e4SLinus Torvalds  * here.
15351da177e4SLinus Torvalds  *
15361da177e4SLinus Torvalds  * We have a potential double-lock case here, so even when
15371da177e4SLinus Torvalds  * doing backlog processing we use the BH locking scheme.
15381da177e4SLinus Torvalds  * This is because we cannot sleep with the original spinlock
15391da177e4SLinus Torvalds  * held.
15401da177e4SLinus Torvalds  */
15411da177e4SLinus Torvalds int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
15421da177e4SLinus Torvalds {
1543cfb6eeb4SYOSHIFUJI Hideaki 	struct sock *rsk;
1544cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1545cfb6eeb4SYOSHIFUJI Hideaki 	/*
1546cfb6eeb4SYOSHIFUJI Hideaki 	 * We really want to reject the packet as early as possible
1547cfb6eeb4SYOSHIFUJI Hideaki 	 * if:
1548cfb6eeb4SYOSHIFUJI Hideaki 	 *  o We're expecting an MD5'd packet and this is no MD5 tcp option
1549cfb6eeb4SYOSHIFUJI Hideaki 	 *  o There is an MD5 option and we're not expecting one
1550cfb6eeb4SYOSHIFUJI Hideaki 	 */
1551cfb6eeb4SYOSHIFUJI Hideaki 	if (tcp_v4_inbound_md5_hash(sk, skb))
1552cfb6eeb4SYOSHIFUJI Hideaki 		goto discard;
1553cfb6eeb4SYOSHIFUJI Hideaki #endif
1554cfb6eeb4SYOSHIFUJI Hideaki 
15551da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
15561da177e4SLinus Torvalds 		TCP_CHECK_TIMER(sk);
1557aa8223c7SArnaldo Carvalho de Melo 		if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
1558cfb6eeb4SYOSHIFUJI Hideaki 			rsk = sk;
15591da177e4SLinus Torvalds 			goto reset;
1560cfb6eeb4SYOSHIFUJI Hideaki 		}
15611da177e4SLinus Torvalds 		TCP_CHECK_TIMER(sk);
15621da177e4SLinus Torvalds 		return 0;
15631da177e4SLinus Torvalds 	}
15641da177e4SLinus Torvalds 
1565ab6a5bb6SArnaldo Carvalho de Melo 	if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
15661da177e4SLinus Torvalds 		goto csum_err;
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 	if (sk->sk_state == TCP_LISTEN) {
15691da177e4SLinus Torvalds 		struct sock *nsk = tcp_v4_hnd_req(sk, skb);
15701da177e4SLinus Torvalds 		if (!nsk)
15711da177e4SLinus Torvalds 			goto discard;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 		if (nsk != sk) {
1574cfb6eeb4SYOSHIFUJI Hideaki 			if (tcp_child_process(sk, nsk, skb)) {
1575cfb6eeb4SYOSHIFUJI Hideaki 				rsk = nsk;
15761da177e4SLinus Torvalds 				goto reset;
1577cfb6eeb4SYOSHIFUJI Hideaki 			}
15781da177e4SLinus Torvalds 			return 0;
15791da177e4SLinus Torvalds 		}
15801da177e4SLinus Torvalds 	}
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	TCP_CHECK_TIMER(sk);
1583aa8223c7SArnaldo Carvalho de Melo 	if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
1584cfb6eeb4SYOSHIFUJI Hideaki 		rsk = sk;
15851da177e4SLinus Torvalds 		goto reset;
1586cfb6eeb4SYOSHIFUJI Hideaki 	}
15871da177e4SLinus Torvalds 	TCP_CHECK_TIMER(sk);
15881da177e4SLinus Torvalds 	return 0;
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds reset:
1591cfb6eeb4SYOSHIFUJI Hideaki 	tcp_v4_send_reset(rsk, skb);
15921da177e4SLinus Torvalds discard:
15931da177e4SLinus Torvalds 	kfree_skb(skb);
15941da177e4SLinus Torvalds 	/* Be careful here. If this function gets more complicated and
15951da177e4SLinus Torvalds 	 * gcc suffers from register pressure on the x86, sk (in %ebx)
15961da177e4SLinus Torvalds 	 * might be destroyed here. This current version compiles correctly,
15971da177e4SLinus Torvalds 	 * but you have been warned.
15981da177e4SLinus Torvalds 	 */
15991da177e4SLinus Torvalds 	return 0;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds csum_err:
160263231bddSPavel Emelyanov 	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
16031da177e4SLinus Torvalds 	goto discard;
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds /*
16071da177e4SLinus Torvalds  *	From tcp_input.c
16081da177e4SLinus Torvalds  */
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds int tcp_v4_rcv(struct sk_buff *skb)
16111da177e4SLinus Torvalds {
1612eddc9ec5SArnaldo Carvalho de Melo 	const struct iphdr *iph;
16131da177e4SLinus Torvalds 	struct tcphdr *th;
16141da177e4SLinus Torvalds 	struct sock *sk;
16151da177e4SLinus Torvalds 	int ret;
1616a86b1e30SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	if (skb->pkt_type != PACKET_HOST)
16191da177e4SLinus Torvalds 		goto discard_it;
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 	/* Count it even if it's bad */
162263231bddSPavel Emelyanov 	TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
16251da177e4SLinus Torvalds 		goto discard_it;
16261da177e4SLinus Torvalds 
1627aa8223c7SArnaldo Carvalho de Melo 	th = tcp_hdr(skb);
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	if (th->doff < sizeof(struct tcphdr) / 4)
16301da177e4SLinus Torvalds 		goto bad_packet;
16311da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, th->doff * 4))
16321da177e4SLinus Torvalds 		goto discard_it;
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	/* An explanation is required here, I think.
16351da177e4SLinus Torvalds 	 * Packet length and doff are validated by header prediction,
1636caa20d9aSStephen Hemminger 	 * provided case of th->doff==0 is eliminated.
16371da177e4SLinus Torvalds 	 * So, we defer the checks. */
163860476372SHerbert Xu 	if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
16391da177e4SLinus Torvalds 		goto bad_packet;
16401da177e4SLinus Torvalds 
1641aa8223c7SArnaldo Carvalho de Melo 	th = tcp_hdr(skb);
1642eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
16431da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->seq = ntohl(th->seq);
16441da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
16451da177e4SLinus Torvalds 				    skb->len - th->doff * 4);
16461da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
16471da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->when	 = 0;
1648eddc9ec5SArnaldo Carvalho de Melo 	TCP_SKB_CB(skb)->flags	 = iph->tos;
16491da177e4SLinus Torvalds 	TCP_SKB_CB(skb)->sacked	 = 0;
16501da177e4SLinus Torvalds 
16519a1f27c4SArnaldo Carvalho de Melo 	sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
16521da177e4SLinus Torvalds 	if (!sk)
16531da177e4SLinus Torvalds 		goto no_tcp_socket;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds process:
16561da177e4SLinus Torvalds 	if (sk->sk_state == TCP_TIME_WAIT)
16571da177e4SLinus Torvalds 		goto do_time_wait;
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
16601da177e4SLinus Torvalds 		goto discard_and_relse;
1661b59c2701SPatrick McHardy 	nf_reset(skb);
16621da177e4SLinus Torvalds 
1663fda9ef5dSDmitry Mishin 	if (sk_filter(sk, skb))
16641da177e4SLinus Torvalds 		goto discard_and_relse;
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	skb->dev = NULL;
16671da177e4SLinus Torvalds 
1668c6366184SIngo Molnar 	bh_lock_sock_nested(sk);
16691da177e4SLinus Torvalds 	ret = 0;
16701da177e4SLinus Torvalds 	if (!sock_owned_by_user(sk)) {
16711a2449a8SChris Leech #ifdef CONFIG_NET_DMA
16721a2449a8SChris Leech 		struct tcp_sock *tp = tcp_sk(sk);
16731a2449a8SChris Leech 		if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1674f67b4599SDan Williams 			tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
16751a2449a8SChris Leech 		if (tp->ucopy.dma_chan)
16761a2449a8SChris Leech 			ret = tcp_v4_do_rcv(sk, skb);
16771a2449a8SChris Leech 		else
16781a2449a8SChris Leech #endif
16791a2449a8SChris Leech 		{
16801da177e4SLinus Torvalds 			if (!tcp_prequeue(sk, skb))
16811da177e4SLinus Torvalds 				ret = tcp_v4_do_rcv(sk, skb);
16821a2449a8SChris Leech 		}
16831da177e4SLinus Torvalds 	} else
16841da177e4SLinus Torvalds 		sk_add_backlog(sk, skb);
16851da177e4SLinus Torvalds 	bh_unlock_sock(sk);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	sock_put(sk);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	return ret;
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds no_tcp_socket:
16921da177e4SLinus Torvalds 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
16931da177e4SLinus Torvalds 		goto discard_it;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
16961da177e4SLinus Torvalds bad_packet:
169763231bddSPavel Emelyanov 		TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
16981da177e4SLinus Torvalds 	} else {
1699cfb6eeb4SYOSHIFUJI Hideaki 		tcp_v4_send_reset(NULL, skb);
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds discard_it:
17031da177e4SLinus Torvalds 	/* Discard frame. */
17041da177e4SLinus Torvalds 	kfree_skb(skb);
17051da177e4SLinus Torvalds 	return 0;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds discard_and_relse:
17081da177e4SLinus Torvalds 	sock_put(sk);
17091da177e4SLinus Torvalds 	goto discard_it;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds do_time_wait:
17121da177e4SLinus Torvalds 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
17139469c7b4SYOSHIFUJI Hideaki 		inet_twsk_put(inet_twsk(sk));
17141da177e4SLinus Torvalds 		goto discard_it;
17151da177e4SLinus Torvalds 	}
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
171863231bddSPavel Emelyanov 		TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
17199469c7b4SYOSHIFUJI Hideaki 		inet_twsk_put(inet_twsk(sk));
17201da177e4SLinus Torvalds 		goto discard_it;
17211da177e4SLinus Torvalds 	}
17229469c7b4SYOSHIFUJI Hideaki 	switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
17231da177e4SLinus Torvalds 	case TCP_TW_SYN: {
1724c346dca1SYOSHIFUJI Hideaki 		struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
1725c67499c0SPavel Emelyanov 							&tcp_hashinfo,
1726eddc9ec5SArnaldo Carvalho de Melo 							iph->daddr, th->dest,
1727463c84b9SArnaldo Carvalho de Melo 							inet_iif(skb));
17281da177e4SLinus Torvalds 		if (sk2) {
17299469c7b4SYOSHIFUJI Hideaki 			inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
17309469c7b4SYOSHIFUJI Hideaki 			inet_twsk_put(inet_twsk(sk));
17311da177e4SLinus Torvalds 			sk = sk2;
17321da177e4SLinus Torvalds 			goto process;
17331da177e4SLinus Torvalds 		}
17341da177e4SLinus Torvalds 		/* Fall through to ACK */
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 	case TCP_TW_ACK:
17371da177e4SLinus Torvalds 		tcp_v4_timewait_ack(sk, skb);
17381da177e4SLinus Torvalds 		break;
17391da177e4SLinus Torvalds 	case TCP_TW_RST:
17401da177e4SLinus Torvalds 		goto no_tcp_socket;
17411da177e4SLinus Torvalds 	case TCP_TW_SUCCESS:;
17421da177e4SLinus Torvalds 	}
17431da177e4SLinus Torvalds 	goto discard_it;
17441da177e4SLinus Torvalds }
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds /* VJ's idea. Save last timestamp seen from this destination
17471da177e4SLinus Torvalds  * and hold it at least for normal timewait interval to use for duplicate
17481da177e4SLinus Torvalds  * segment detection in subsequent connections, before they enter synchronized
17491da177e4SLinus Torvalds  * state.
17501da177e4SLinus Torvalds  */
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds int tcp_v4_remember_stamp(struct sock *sk)
17531da177e4SLinus Torvalds {
17541da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
17551da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
17561da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
17571da177e4SLinus Torvalds 	struct inet_peer *peer = NULL;
17581da177e4SLinus Torvalds 	int release_it = 0;
17591da177e4SLinus Torvalds 
1760c720c7e8SEric Dumazet 	if (!rt || rt->rt_dst != inet->inet_daddr) {
1761c720c7e8SEric Dumazet 		peer = inet_getpeer(inet->inet_daddr, 1);
17621da177e4SLinus Torvalds 		release_it = 1;
17631da177e4SLinus Torvalds 	} else {
17641da177e4SLinus Torvalds 		if (!rt->peer)
17651da177e4SLinus Torvalds 			rt_bind_peer(rt, 1);
17661da177e4SLinus Torvalds 		peer = rt->peer;
17671da177e4SLinus Torvalds 	}
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	if (peer) {
17701da177e4SLinus Torvalds 		if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
17712c1409a0SEric Dumazet 		    ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
17722c1409a0SEric Dumazet 		     peer->tcp_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
17732c1409a0SEric Dumazet 			peer->tcp_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
17741da177e4SLinus Torvalds 			peer->tcp_ts = tp->rx_opt.ts_recent;
17751da177e4SLinus Torvalds 		}
17761da177e4SLinus Torvalds 		if (release_it)
17771da177e4SLinus Torvalds 			inet_putpeer(peer);
17781da177e4SLinus Torvalds 		return 1;
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	return 0;
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
17848feaf0c0SArnaldo Carvalho de Melo int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
17851da177e4SLinus Torvalds {
17868feaf0c0SArnaldo Carvalho de Melo 	struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	if (peer) {
17898feaf0c0SArnaldo Carvalho de Melo 		const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
17908feaf0c0SArnaldo Carvalho de Melo 
17918feaf0c0SArnaldo Carvalho de Melo 		if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
17922c1409a0SEric Dumazet 		    ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
17932c1409a0SEric Dumazet 		     peer->tcp_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
17942c1409a0SEric Dumazet 			peer->tcp_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
17958feaf0c0SArnaldo Carvalho de Melo 			peer->tcp_ts	   = tcptw->tw_ts_recent;
17961da177e4SLinus Torvalds 		}
17971da177e4SLinus Torvalds 		inet_putpeer(peer);
17981da177e4SLinus Torvalds 		return 1;
17991da177e4SLinus Torvalds 	}
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	return 0;
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds 
18043b401a81SStephen Hemminger const struct inet_connection_sock_af_ops ipv4_specific = {
18051da177e4SLinus Torvalds 	.queue_xmit	   = ip_queue_xmit,
18061da177e4SLinus Torvalds 	.send_check	   = tcp_v4_send_check,
180732519f11SArnaldo Carvalho de Melo 	.rebuild_header	   = inet_sk_rebuild_header,
18081da177e4SLinus Torvalds 	.conn_request	   = tcp_v4_conn_request,
18091da177e4SLinus Torvalds 	.syn_recv_sock	   = tcp_v4_syn_recv_sock,
18101da177e4SLinus Torvalds 	.remember_stamp	   = tcp_v4_remember_stamp,
18111da177e4SLinus Torvalds 	.net_header_len	   = sizeof(struct iphdr),
18121da177e4SLinus Torvalds 	.setsockopt	   = ip_setsockopt,
18131da177e4SLinus Torvalds 	.getsockopt	   = ip_getsockopt,
1814543d9cfeSArnaldo Carvalho de Melo 	.addr2sockaddr	   = inet_csk_addr2sockaddr,
1815543d9cfeSArnaldo Carvalho de Melo 	.sockaddr_len	   = sizeof(struct sockaddr_in),
1816ab1e0a13SArnaldo Carvalho de Melo 	.bind_conflict	   = inet_csk_bind_conflict,
18173fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
18183fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_ip_setsockopt,
18193fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_ip_getsockopt,
18203fdadf7dSDmitry Mishin #endif
18211da177e4SLinus Torvalds };
18221da177e4SLinus Torvalds 
1823cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1824b2e4b3deSStephen Hemminger static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
1825cfb6eeb4SYOSHIFUJI Hideaki 	.md5_lookup		= tcp_v4_md5_lookup,
182649a72dfbSAdam Langley 	.calc_md5_hash		= tcp_v4_md5_hash_skb,
1827cfb6eeb4SYOSHIFUJI Hideaki 	.md5_add		= tcp_v4_md5_add_func,
1828cfb6eeb4SYOSHIFUJI Hideaki 	.md5_parse		= tcp_v4_parse_md5_keys,
1829cfb6eeb4SYOSHIFUJI Hideaki };
1830b6332e6cSAndrew Morton #endif
1831cfb6eeb4SYOSHIFUJI Hideaki 
18321da177e4SLinus Torvalds /* NOTE: A lot of things set to zero explicitly by call to
18331da177e4SLinus Torvalds  *       sk_alloc() so need not be done here.
18341da177e4SLinus Torvalds  */
18351da177e4SLinus Torvalds static int tcp_v4_init_sock(struct sock *sk)
18361da177e4SLinus Torvalds {
18376687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
18381da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	skb_queue_head_init(&tp->out_of_order_queue);
18411da177e4SLinus Torvalds 	tcp_init_xmit_timers(sk);
18421da177e4SLinus Torvalds 	tcp_prequeue_init(tp);
18431da177e4SLinus Torvalds 
18446687e988SArnaldo Carvalho de Melo 	icsk->icsk_rto = TCP_TIMEOUT_INIT;
18451da177e4SLinus Torvalds 	tp->mdev = TCP_TIMEOUT_INIT;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	/* So many TCP implementations out there (incorrectly) count the
18481da177e4SLinus Torvalds 	 * initial SYN frame in their delayed-ACK and congestion control
18491da177e4SLinus Torvalds 	 * algorithms that we must have the following bandaid to talk
18501da177e4SLinus Torvalds 	 * efficiently to them.  -DaveM
18511da177e4SLinus Torvalds 	 */
18521da177e4SLinus Torvalds 	tp->snd_cwnd = 2;
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 	/* See draft-stevens-tcpca-spec-01 for discussion of the
18551da177e4SLinus Torvalds 	 * initialization of these values.
18561da177e4SLinus Torvalds 	 */
18570b6a05c1SIlpo Järvinen 	tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
18581da177e4SLinus Torvalds 	tp->snd_cwnd_clamp = ~0;
1859bee7ca9eSWilliam Allen Simpson 	tp->mss_cache = TCP_MSS_DEFAULT;
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	tp->reordering = sysctl_tcp_reordering;
18626687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_ops = &tcp_init_congestion_ops;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	sk->sk_write_space = sk_stream_write_space;
18671da177e4SLinus Torvalds 	sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
18681da177e4SLinus Torvalds 
18698292a17aSArnaldo Carvalho de Melo 	icsk->icsk_af_ops = &ipv4_specific;
1870d83d8461SArnaldo Carvalho de Melo 	icsk->icsk_sync_mss = tcp_sync_mss;
1871cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1872cfb6eeb4SYOSHIFUJI Hideaki 	tp->af_specific = &tcp_sock_ipv4_specific;
1873cfb6eeb4SYOSHIFUJI Hideaki #endif
18741da177e4SLinus Torvalds 
1875435cf559SWilliam Allen Simpson 	/* TCP Cookie Transactions */
1876435cf559SWilliam Allen Simpson 	if (sysctl_tcp_cookie_size > 0) {
1877435cf559SWilliam Allen Simpson 		/* Default, cookies without s_data_payload. */
1878435cf559SWilliam Allen Simpson 		tp->cookie_values =
1879435cf559SWilliam Allen Simpson 			kzalloc(sizeof(*tp->cookie_values),
1880435cf559SWilliam Allen Simpson 				sk->sk_allocation);
1881435cf559SWilliam Allen Simpson 		if (tp->cookie_values != NULL)
1882435cf559SWilliam Allen Simpson 			kref_init(&tp->cookie_values->kref);
1883435cf559SWilliam Allen Simpson 	}
1884435cf559SWilliam Allen Simpson 	/* Presumed zeroed, in order of appearance:
1885435cf559SWilliam Allen Simpson 	 *	cookie_in_always, cookie_out_never,
1886435cf559SWilliam Allen Simpson 	 *	s_data_constant, s_data_in, s_data_out
1887435cf559SWilliam Allen Simpson 	 */
18881da177e4SLinus Torvalds 	sk->sk_sndbuf = sysctl_tcp_wmem[1];
18891da177e4SLinus Torvalds 	sk->sk_rcvbuf = sysctl_tcp_rmem[1];
18901da177e4SLinus Torvalds 
1891eb4dea58SHerbert Xu 	local_bh_disable();
18921748376bSEric Dumazet 	percpu_counter_inc(&tcp_sockets_allocated);
1893eb4dea58SHerbert Xu 	local_bh_enable();
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	return 0;
18961da177e4SLinus Torvalds }
18971da177e4SLinus Torvalds 
18987d06b2e0SBrian Haley void tcp_v4_destroy_sock(struct sock *sk)
18991da177e4SLinus Torvalds {
19001da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	tcp_clear_xmit_timers(sk);
19031da177e4SLinus Torvalds 
19046687e988SArnaldo Carvalho de Melo 	tcp_cleanup_congestion_control(sk);
1905317a76f9SStephen Hemminger 
19061da177e4SLinus Torvalds 	/* Cleanup up the write buffer. */
1907fe067e8aSDavid S. Miller 	tcp_write_queue_purge(sk);
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	/* Cleans up our, hopefully empty, out_of_order_queue. */
19101da177e4SLinus Torvalds 	__skb_queue_purge(&tp->out_of_order_queue);
19111da177e4SLinus Torvalds 
1912cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1913cfb6eeb4SYOSHIFUJI Hideaki 	/* Clean up the MD5 key list, if any */
1914cfb6eeb4SYOSHIFUJI Hideaki 	if (tp->md5sig_info) {
1915cfb6eeb4SYOSHIFUJI Hideaki 		tcp_v4_clear_md5_list(sk);
1916cfb6eeb4SYOSHIFUJI Hideaki 		kfree(tp->md5sig_info);
1917cfb6eeb4SYOSHIFUJI Hideaki 		tp->md5sig_info = NULL;
1918cfb6eeb4SYOSHIFUJI Hideaki 	}
1919cfb6eeb4SYOSHIFUJI Hideaki #endif
1920cfb6eeb4SYOSHIFUJI Hideaki 
19211a2449a8SChris Leech #ifdef CONFIG_NET_DMA
19221a2449a8SChris Leech 	/* Cleans up our sk_async_wait_queue */
19231a2449a8SChris Leech 	__skb_queue_purge(&sk->sk_async_wait_queue);
19241a2449a8SChris Leech #endif
19251a2449a8SChris Leech 
19261da177e4SLinus Torvalds 	/* Clean prequeue, it must be empty really */
19271da177e4SLinus Torvalds 	__skb_queue_purge(&tp->ucopy.prequeue);
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 	/* Clean up a referenced TCP bind bucket. */
1930463c84b9SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_bind_hash)
1931ab1e0a13SArnaldo Carvalho de Melo 		inet_put_port(sk);
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	/*
19341da177e4SLinus Torvalds 	 * If sendmsg cached page exists, toss it.
19351da177e4SLinus Torvalds 	 */
19361da177e4SLinus Torvalds 	if (sk->sk_sndmsg_page) {
19371da177e4SLinus Torvalds 		__free_page(sk->sk_sndmsg_page);
19381da177e4SLinus Torvalds 		sk->sk_sndmsg_page = NULL;
19391da177e4SLinus Torvalds 	}
19401da177e4SLinus Torvalds 
1941435cf559SWilliam Allen Simpson 	/* TCP Cookie Transactions */
1942435cf559SWilliam Allen Simpson 	if (tp->cookie_values != NULL) {
1943435cf559SWilliam Allen Simpson 		kref_put(&tp->cookie_values->kref,
1944435cf559SWilliam Allen Simpson 			 tcp_cookie_values_release);
1945435cf559SWilliam Allen Simpson 		tp->cookie_values = NULL;
1946435cf559SWilliam Allen Simpson 	}
1947435cf559SWilliam Allen Simpson 
19481748376bSEric Dumazet 	percpu_counter_dec(&tcp_sockets_allocated);
19491da177e4SLinus Torvalds }
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_destroy_sock);
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
19541da177e4SLinus Torvalds /* Proc filesystem TCP sock list dumping. */
19551da177e4SLinus Torvalds 
19563ab5aee7SEric Dumazet static inline struct inet_timewait_sock *tw_head(struct hlist_nulls_head *head)
19571da177e4SLinus Torvalds {
19583ab5aee7SEric Dumazet 	return hlist_nulls_empty(head) ? NULL :
19598feaf0c0SArnaldo Carvalho de Melo 		list_entry(head->first, struct inet_timewait_sock, tw_node);
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds 
19628feaf0c0SArnaldo Carvalho de Melo static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
19631da177e4SLinus Torvalds {
19643ab5aee7SEric Dumazet 	return !is_a_nulls(tw->tw_node.next) ?
19653ab5aee7SEric Dumazet 		hlist_nulls_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds static void *listening_get_next(struct seq_file *seq, void *cur)
19691da177e4SLinus Torvalds {
1970463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk;
1971c25eb3bfSEric Dumazet 	struct hlist_nulls_node *node;
19721da177e4SLinus Torvalds 	struct sock *sk = cur;
19735caea4eaSEric Dumazet 	struct inet_listen_hashbucket *ilb;
19741da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
1975a4146b1bSDenis V. Lunev 	struct net *net = seq_file_net(seq);
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	if (!sk) {
19781da177e4SLinus Torvalds 		st->bucket = 0;
19795caea4eaSEric Dumazet 		ilb = &tcp_hashinfo.listening_hash[0];
19805caea4eaSEric Dumazet 		spin_lock_bh(&ilb->lock);
1981c25eb3bfSEric Dumazet 		sk = sk_nulls_head(&ilb->head);
19821da177e4SLinus Torvalds 		goto get_sk;
19831da177e4SLinus Torvalds 	}
19845caea4eaSEric Dumazet 	ilb = &tcp_hashinfo.listening_hash[st->bucket];
19851da177e4SLinus Torvalds 	++st->num;
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	if (st->state == TCP_SEQ_STATE_OPENREQ) {
198860236fddSArnaldo Carvalho de Melo 		struct request_sock *req = cur;
19891da177e4SLinus Torvalds 
1990463c84b9SArnaldo Carvalho de Melo 		icsk = inet_csk(st->syn_wait_sk);
19911da177e4SLinus Torvalds 		req = req->dl_next;
19921da177e4SLinus Torvalds 		while (1) {
19931da177e4SLinus Torvalds 			while (req) {
1994bdccc4caSDaniel Lezcano 				if (req->rsk_ops->family == st->family) {
19951da177e4SLinus Torvalds 					cur = req;
19961da177e4SLinus Torvalds 					goto out;
19971da177e4SLinus Torvalds 				}
19981da177e4SLinus Torvalds 				req = req->dl_next;
19991da177e4SLinus Torvalds 			}
200072a3effaSEric Dumazet 			if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
20011da177e4SLinus Torvalds 				break;
20021da177e4SLinus Torvalds get_req:
2003463c84b9SArnaldo Carvalho de Melo 			req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
20041da177e4SLinus Torvalds 		}
20051da177e4SLinus Torvalds 		sk	  = sk_next(st->syn_wait_sk);
20061da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_LISTENING;
2007463c84b9SArnaldo Carvalho de Melo 		read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
20081da177e4SLinus Torvalds 	} else {
2009463c84b9SArnaldo Carvalho de Melo 		icsk = inet_csk(sk);
2010463c84b9SArnaldo Carvalho de Melo 		read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
2011463c84b9SArnaldo Carvalho de Melo 		if (reqsk_queue_len(&icsk->icsk_accept_queue))
20121da177e4SLinus Torvalds 			goto start_req;
2013463c84b9SArnaldo Carvalho de Melo 		read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
20141da177e4SLinus Torvalds 		sk = sk_next(sk);
20151da177e4SLinus Torvalds 	}
20161da177e4SLinus Torvalds get_sk:
2017c25eb3bfSEric Dumazet 	sk_nulls_for_each_from(sk, node) {
2018878628fbSYOSHIFUJI Hideaki 		if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) {
20191da177e4SLinus Torvalds 			cur = sk;
20201da177e4SLinus Torvalds 			goto out;
20211da177e4SLinus Torvalds 		}
2022463c84b9SArnaldo Carvalho de Melo 		icsk = inet_csk(sk);
2023463c84b9SArnaldo Carvalho de Melo 		read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
2024463c84b9SArnaldo Carvalho de Melo 		if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
20251da177e4SLinus Torvalds start_req:
20261da177e4SLinus Torvalds 			st->uid		= sock_i_uid(sk);
20271da177e4SLinus Torvalds 			st->syn_wait_sk = sk;
20281da177e4SLinus Torvalds 			st->state	= TCP_SEQ_STATE_OPENREQ;
20291da177e4SLinus Torvalds 			st->sbucket	= 0;
20301da177e4SLinus Torvalds 			goto get_req;
20311da177e4SLinus Torvalds 		}
2032463c84b9SArnaldo Carvalho de Melo 		read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
20331da177e4SLinus Torvalds 	}
20345caea4eaSEric Dumazet 	spin_unlock_bh(&ilb->lock);
20350f7ff927SArnaldo Carvalho de Melo 	if (++st->bucket < INET_LHTABLE_SIZE) {
20365caea4eaSEric Dumazet 		ilb = &tcp_hashinfo.listening_hash[st->bucket];
20375caea4eaSEric Dumazet 		spin_lock_bh(&ilb->lock);
2038c25eb3bfSEric Dumazet 		sk = sk_nulls_head(&ilb->head);
20391da177e4SLinus Torvalds 		goto get_sk;
20401da177e4SLinus Torvalds 	}
20411da177e4SLinus Torvalds 	cur = NULL;
20421da177e4SLinus Torvalds out:
20431da177e4SLinus Torvalds 	return cur;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
20471da177e4SLinus Torvalds {
20481da177e4SLinus Torvalds 	void *rc = listening_get_next(seq, NULL);
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	while (rc && *pos) {
20511da177e4SLinus Torvalds 		rc = listening_get_next(seq, rc);
20521da177e4SLinus Torvalds 		--*pos;
20531da177e4SLinus Torvalds 	}
20541da177e4SLinus Torvalds 	return rc;
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
20576eac5604SAndi Kleen static inline int empty_bucket(struct tcp_iter_state *st)
20586eac5604SAndi Kleen {
20593ab5aee7SEric Dumazet 	return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain) &&
20603ab5aee7SEric Dumazet 		hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].twchain);
20616eac5604SAndi Kleen }
20626eac5604SAndi Kleen 
20631da177e4SLinus Torvalds static void *established_get_first(struct seq_file *seq)
20641da177e4SLinus Torvalds {
20651da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
2066a4146b1bSDenis V. Lunev 	struct net *net = seq_file_net(seq);
20671da177e4SLinus Torvalds 	void *rc = NULL;
20681da177e4SLinus Torvalds 
2069f373b53bSEric Dumazet 	for (st->bucket = 0; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
20701da177e4SLinus Torvalds 		struct sock *sk;
20713ab5aee7SEric Dumazet 		struct hlist_nulls_node *node;
20728feaf0c0SArnaldo Carvalho de Melo 		struct inet_timewait_sock *tw;
20739db66bdcSEric Dumazet 		spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
20741da177e4SLinus Torvalds 
20756eac5604SAndi Kleen 		/* Lockless fast path for the common case of empty buckets */
20766eac5604SAndi Kleen 		if (empty_bucket(st))
20776eac5604SAndi Kleen 			continue;
20786eac5604SAndi Kleen 
20799db66bdcSEric Dumazet 		spin_lock_bh(lock);
20803ab5aee7SEric Dumazet 		sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
2081f40c8174SDaniel Lezcano 			if (sk->sk_family != st->family ||
2082878628fbSYOSHIFUJI Hideaki 			    !net_eq(sock_net(sk), net)) {
20831da177e4SLinus Torvalds 				continue;
20841da177e4SLinus Torvalds 			}
20851da177e4SLinus Torvalds 			rc = sk;
20861da177e4SLinus Torvalds 			goto out;
20871da177e4SLinus Torvalds 		}
20881da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_TIME_WAIT;
20898feaf0c0SArnaldo Carvalho de Melo 		inet_twsk_for_each(tw, node,
2090dbca9b27SEric Dumazet 				   &tcp_hashinfo.ehash[st->bucket].twchain) {
209128518fc1SPavel Emelyanov 			if (tw->tw_family != st->family ||
2092878628fbSYOSHIFUJI Hideaki 			    !net_eq(twsk_net(tw), net)) {
20931da177e4SLinus Torvalds 				continue;
20941da177e4SLinus Torvalds 			}
20951da177e4SLinus Torvalds 			rc = tw;
20961da177e4SLinus Torvalds 			goto out;
20971da177e4SLinus Torvalds 		}
20989db66bdcSEric Dumazet 		spin_unlock_bh(lock);
20991da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_ESTABLISHED;
21001da177e4SLinus Torvalds 	}
21011da177e4SLinus Torvalds out:
21021da177e4SLinus Torvalds 	return rc;
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds static void *established_get_next(struct seq_file *seq, void *cur)
21061da177e4SLinus Torvalds {
21071da177e4SLinus Torvalds 	struct sock *sk = cur;
21088feaf0c0SArnaldo Carvalho de Melo 	struct inet_timewait_sock *tw;
21093ab5aee7SEric Dumazet 	struct hlist_nulls_node *node;
21101da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
2111a4146b1bSDenis V. Lunev 	struct net *net = seq_file_net(seq);
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 	++st->num;
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
21161da177e4SLinus Torvalds 		tw = cur;
21171da177e4SLinus Torvalds 		tw = tw_next(tw);
21181da177e4SLinus Torvalds get_tw:
2119878628fbSYOSHIFUJI Hideaki 		while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
21201da177e4SLinus Torvalds 			tw = tw_next(tw);
21211da177e4SLinus Torvalds 		}
21221da177e4SLinus Torvalds 		if (tw) {
21231da177e4SLinus Torvalds 			cur = tw;
21241da177e4SLinus Torvalds 			goto out;
21251da177e4SLinus Torvalds 		}
21269db66bdcSEric Dumazet 		spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
21271da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_ESTABLISHED;
21281da177e4SLinus Torvalds 
21296eac5604SAndi Kleen 		/* Look for next non empty bucket */
2130f373b53bSEric Dumazet 		while (++st->bucket <= tcp_hashinfo.ehash_mask &&
21316eac5604SAndi Kleen 				empty_bucket(st))
21326eac5604SAndi Kleen 			;
2133f373b53bSEric Dumazet 		if (st->bucket > tcp_hashinfo.ehash_mask)
21346eac5604SAndi Kleen 			return NULL;
21356eac5604SAndi Kleen 
21369db66bdcSEric Dumazet 		spin_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
21373ab5aee7SEric Dumazet 		sk = sk_nulls_head(&tcp_hashinfo.ehash[st->bucket].chain);
21381da177e4SLinus Torvalds 	} else
21393ab5aee7SEric Dumazet 		sk = sk_nulls_next(sk);
21401da177e4SLinus Torvalds 
21413ab5aee7SEric Dumazet 	sk_nulls_for_each_from(sk, node) {
2142878628fbSYOSHIFUJI Hideaki 		if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
21431da177e4SLinus Torvalds 			goto found;
21441da177e4SLinus Torvalds 	}
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	st->state = TCP_SEQ_STATE_TIME_WAIT;
2147dbca9b27SEric Dumazet 	tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
21481da177e4SLinus Torvalds 	goto get_tw;
21491da177e4SLinus Torvalds found:
21501da177e4SLinus Torvalds 	cur = sk;
21511da177e4SLinus Torvalds out:
21521da177e4SLinus Torvalds 	return cur;
21531da177e4SLinus Torvalds }
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds static void *established_get_idx(struct seq_file *seq, loff_t pos)
21561da177e4SLinus Torvalds {
21571da177e4SLinus Torvalds 	void *rc = established_get_first(seq);
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	while (rc && pos) {
21601da177e4SLinus Torvalds 		rc = established_get_next(seq, rc);
21611da177e4SLinus Torvalds 		--pos;
21621da177e4SLinus Torvalds 	}
21631da177e4SLinus Torvalds 	return rc;
21641da177e4SLinus Torvalds }
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
21671da177e4SLinus Torvalds {
21681da177e4SLinus Torvalds 	void *rc;
21691da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	st->state = TCP_SEQ_STATE_LISTENING;
21721da177e4SLinus Torvalds 	rc	  = listening_get_idx(seq, &pos);
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds 	if (!rc) {
21751da177e4SLinus Torvalds 		st->state = TCP_SEQ_STATE_ESTABLISHED;
21761da177e4SLinus Torvalds 		rc	  = established_get_idx(seq, pos);
21771da177e4SLinus Torvalds 	}
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	return rc;
21801da177e4SLinus Torvalds }
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
21831da177e4SLinus Torvalds {
21841da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
21851da177e4SLinus Torvalds 	st->state = TCP_SEQ_STATE_LISTENING;
21861da177e4SLinus Torvalds 	st->num = 0;
21871da177e4SLinus Torvalds 	return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
21911da177e4SLinus Torvalds {
21921da177e4SLinus Torvalds 	void *rc = NULL;
21931da177e4SLinus Torvalds 	struct tcp_iter_state *st;
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
21961da177e4SLinus Torvalds 		rc = tcp_get_idx(seq, 0);
21971da177e4SLinus Torvalds 		goto out;
21981da177e4SLinus Torvalds 	}
21991da177e4SLinus Torvalds 	st = seq->private;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	switch (st->state) {
22021da177e4SLinus Torvalds 	case TCP_SEQ_STATE_OPENREQ:
22031da177e4SLinus Torvalds 	case TCP_SEQ_STATE_LISTENING:
22041da177e4SLinus Torvalds 		rc = listening_get_next(seq, v);
22051da177e4SLinus Torvalds 		if (!rc) {
22061da177e4SLinus Torvalds 			st->state = TCP_SEQ_STATE_ESTABLISHED;
22071da177e4SLinus Torvalds 			rc	  = established_get_first(seq);
22081da177e4SLinus Torvalds 		}
22091da177e4SLinus Torvalds 		break;
22101da177e4SLinus Torvalds 	case TCP_SEQ_STATE_ESTABLISHED:
22111da177e4SLinus Torvalds 	case TCP_SEQ_STATE_TIME_WAIT:
22121da177e4SLinus Torvalds 		rc = established_get_next(seq, v);
22131da177e4SLinus Torvalds 		break;
22141da177e4SLinus Torvalds 	}
22151da177e4SLinus Torvalds out:
22161da177e4SLinus Torvalds 	++*pos;
22171da177e4SLinus Torvalds 	return rc;
22181da177e4SLinus Torvalds }
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds static void tcp_seq_stop(struct seq_file *seq, void *v)
22211da177e4SLinus Torvalds {
22221da177e4SLinus Torvalds 	struct tcp_iter_state *st = seq->private;
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	switch (st->state) {
22251da177e4SLinus Torvalds 	case TCP_SEQ_STATE_OPENREQ:
22261da177e4SLinus Torvalds 		if (v) {
2227463c84b9SArnaldo Carvalho de Melo 			struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
2228463c84b9SArnaldo Carvalho de Melo 			read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
22291da177e4SLinus Torvalds 		}
22301da177e4SLinus Torvalds 	case TCP_SEQ_STATE_LISTENING:
22311da177e4SLinus Torvalds 		if (v != SEQ_START_TOKEN)
22325caea4eaSEric Dumazet 			spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
22331da177e4SLinus Torvalds 		break;
22341da177e4SLinus Torvalds 	case TCP_SEQ_STATE_TIME_WAIT:
22351da177e4SLinus Torvalds 	case TCP_SEQ_STATE_ESTABLISHED:
22361da177e4SLinus Torvalds 		if (v)
22379db66bdcSEric Dumazet 			spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
22381da177e4SLinus Torvalds 		break;
22391da177e4SLinus Torvalds 	}
22401da177e4SLinus Torvalds }
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds static int tcp_seq_open(struct inode *inode, struct file *file)
22431da177e4SLinus Torvalds {
22441da177e4SLinus Torvalds 	struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
22451da177e4SLinus Torvalds 	struct tcp_iter_state *s;
224652d6f3f1SDenis V. Lunev 	int err;
22471da177e4SLinus Torvalds 
224852d6f3f1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
224952d6f3f1SDenis V. Lunev 			  sizeof(struct tcp_iter_state));
225052d6f3f1SDenis V. Lunev 	if (err < 0)
225152d6f3f1SDenis V. Lunev 		return err;
2252f40c8174SDaniel Lezcano 
225352d6f3f1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
22541da177e4SLinus Torvalds 	s->family		= afinfo->family;
2255f40c8174SDaniel Lezcano 	return 0;
2256f40c8174SDaniel Lezcano }
2257f40c8174SDaniel Lezcano 
22586f8b13bcSDaniel Lezcano int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
22591da177e4SLinus Torvalds {
22601da177e4SLinus Torvalds 	int rc = 0;
22611da177e4SLinus Torvalds 	struct proc_dir_entry *p;
22621da177e4SLinus Torvalds 
226368fcadd1SDenis V. Lunev 	afinfo->seq_fops.open		= tcp_seq_open;
226468fcadd1SDenis V. Lunev 	afinfo->seq_fops.read		= seq_read;
226568fcadd1SDenis V. Lunev 	afinfo->seq_fops.llseek		= seq_lseek;
226668fcadd1SDenis V. Lunev 	afinfo->seq_fops.release	= seq_release_net;
22671da177e4SLinus Torvalds 
22689427c4b3SDenis V. Lunev 	afinfo->seq_ops.start		= tcp_seq_start;
22699427c4b3SDenis V. Lunev 	afinfo->seq_ops.next		= tcp_seq_next;
22709427c4b3SDenis V. Lunev 	afinfo->seq_ops.stop		= tcp_seq_stop;
22719427c4b3SDenis V. Lunev 
227284841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
227384841c3cSDenis V. Lunev 			     &afinfo->seq_fops, afinfo);
227484841c3cSDenis V. Lunev 	if (!p)
22751da177e4SLinus Torvalds 		rc = -ENOMEM;
22761da177e4SLinus Torvalds 	return rc;
22771da177e4SLinus Torvalds }
22781da177e4SLinus Torvalds 
22796f8b13bcSDaniel Lezcano void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
22801da177e4SLinus Torvalds {
22816f8b13bcSDaniel Lezcano 	proc_net_remove(net, afinfo->name);
22821da177e4SLinus Torvalds }
22831da177e4SLinus Torvalds 
228460236fddSArnaldo Carvalho de Melo static void get_openreq4(struct sock *sk, struct request_sock *req,
22855e659e4cSPavel Emelyanov 			 struct seq_file *f, int i, int uid, int *len)
22861da177e4SLinus Torvalds {
22872e6599cbSArnaldo Carvalho de Melo 	const struct inet_request_sock *ireq = inet_rsk(req);
22881da177e4SLinus Torvalds 	int ttd = req->expires - jiffies;
22891da177e4SLinus Torvalds 
22905e659e4cSPavel Emelyanov 	seq_printf(f, "%4d: %08X:%04X %08X:%04X"
22915e659e4cSPavel Emelyanov 		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n",
22921da177e4SLinus Torvalds 		i,
22932e6599cbSArnaldo Carvalho de Melo 		ireq->loc_addr,
2294c720c7e8SEric Dumazet 		ntohs(inet_sk(sk)->inet_sport),
22952e6599cbSArnaldo Carvalho de Melo 		ireq->rmt_addr,
22962e6599cbSArnaldo Carvalho de Melo 		ntohs(ireq->rmt_port),
22971da177e4SLinus Torvalds 		TCP_SYN_RECV,
22981da177e4SLinus Torvalds 		0, 0, /* could print option size, but that is af dependent. */
22991da177e4SLinus Torvalds 		1,    /* timers active (only the expire timer) */
23001da177e4SLinus Torvalds 		jiffies_to_clock_t(ttd),
23011da177e4SLinus Torvalds 		req->retrans,
23021da177e4SLinus Torvalds 		uid,
23031da177e4SLinus Torvalds 		0,  /* non standard timer */
23041da177e4SLinus Torvalds 		0, /* open_requests have no inode */
23051da177e4SLinus Torvalds 		atomic_read(&sk->sk_refcnt),
23065e659e4cSPavel Emelyanov 		req,
23075e659e4cSPavel Emelyanov 		len);
23081da177e4SLinus Torvalds }
23091da177e4SLinus Torvalds 
23105e659e4cSPavel Emelyanov static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
23111da177e4SLinus Torvalds {
23121da177e4SLinus Torvalds 	int timer_active;
23131da177e4SLinus Torvalds 	unsigned long timer_expires;
2314cf4c6bf8SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
2315cf4c6bf8SIlpo Järvinen 	const struct inet_connection_sock *icsk = inet_csk(sk);
2316cf4c6bf8SIlpo Järvinen 	struct inet_sock *inet = inet_sk(sk);
2317c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2318c720c7e8SEric Dumazet 	__be32 src = inet->inet_rcv_saddr;
2319c720c7e8SEric Dumazet 	__u16 destp = ntohs(inet->inet_dport);
2320c720c7e8SEric Dumazet 	__u16 srcp = ntohs(inet->inet_sport);
23211da177e4SLinus Torvalds 
2322463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
23231da177e4SLinus Torvalds 		timer_active	= 1;
2324463c84b9SArnaldo Carvalho de Melo 		timer_expires	= icsk->icsk_timeout;
2325463c84b9SArnaldo Carvalho de Melo 	} else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
23261da177e4SLinus Torvalds 		timer_active	= 4;
2327463c84b9SArnaldo Carvalho de Melo 		timer_expires	= icsk->icsk_timeout;
2328cf4c6bf8SIlpo Järvinen 	} else if (timer_pending(&sk->sk_timer)) {
23291da177e4SLinus Torvalds 		timer_active	= 2;
2330cf4c6bf8SIlpo Järvinen 		timer_expires	= sk->sk_timer.expires;
23311da177e4SLinus Torvalds 	} else {
23321da177e4SLinus Torvalds 		timer_active	= 0;
23331da177e4SLinus Torvalds 		timer_expires = jiffies;
23341da177e4SLinus Torvalds 	}
23351da177e4SLinus Torvalds 
23365e659e4cSPavel Emelyanov 	seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
23377be87351SStephen Hemminger 			"%08X %5d %8d %lu %d %p %lu %lu %u %u %d%n",
2338cf4c6bf8SIlpo Järvinen 		i, src, srcp, dest, destp, sk->sk_state,
233947da8ee6SSridhar Samudrala 		tp->write_seq - tp->snd_una,
2340cf4c6bf8SIlpo Järvinen 		sk->sk_state == TCP_LISTEN ? sk->sk_ack_backlog :
23417174259eSArnaldo Carvalho de Melo 					     (tp->rcv_nxt - tp->copied_seq),
23421da177e4SLinus Torvalds 		timer_active,
23431da177e4SLinus Torvalds 		jiffies_to_clock_t(timer_expires - jiffies),
2344463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_retransmits,
2345cf4c6bf8SIlpo Järvinen 		sock_i_uid(sk),
23466687e988SArnaldo Carvalho de Melo 		icsk->icsk_probes_out,
2347cf4c6bf8SIlpo Järvinen 		sock_i_ino(sk),
2348cf4c6bf8SIlpo Järvinen 		atomic_read(&sk->sk_refcnt), sk,
23497be87351SStephen Hemminger 		jiffies_to_clock_t(icsk->icsk_rto),
23507be87351SStephen Hemminger 		jiffies_to_clock_t(icsk->icsk_ack.ato),
2351463c84b9SArnaldo Carvalho de Melo 		(icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
23521da177e4SLinus Torvalds 		tp->snd_cwnd,
23530b6a05c1SIlpo Järvinen 		tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh,
23545e659e4cSPavel Emelyanov 		len);
23551da177e4SLinus Torvalds }
23561da177e4SLinus Torvalds 
23577174259eSArnaldo Carvalho de Melo static void get_timewait4_sock(struct inet_timewait_sock *tw,
23585e659e4cSPavel Emelyanov 			       struct seq_file *f, int i, int *len)
23591da177e4SLinus Torvalds {
236023f33c2dSAl Viro 	__be32 dest, src;
23611da177e4SLinus Torvalds 	__u16 destp, srcp;
23621da177e4SLinus Torvalds 	int ttd = tw->tw_ttd - jiffies;
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 	if (ttd < 0)
23651da177e4SLinus Torvalds 		ttd = 0;
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds 	dest  = tw->tw_daddr;
23681da177e4SLinus Torvalds 	src   = tw->tw_rcv_saddr;
23691da177e4SLinus Torvalds 	destp = ntohs(tw->tw_dport);
23701da177e4SLinus Torvalds 	srcp  = ntohs(tw->tw_sport);
23711da177e4SLinus Torvalds 
23725e659e4cSPavel Emelyanov 	seq_printf(f, "%4d: %08X:%04X %08X:%04X"
23735e659e4cSPavel Emelyanov 		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n",
23741da177e4SLinus Torvalds 		i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
23751da177e4SLinus Torvalds 		3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
23765e659e4cSPavel Emelyanov 		atomic_read(&tw->tw_refcnt), tw, len);
23771da177e4SLinus Torvalds }
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds #define TMPSZ 150
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds static int tcp4_seq_show(struct seq_file *seq, void *v)
23821da177e4SLinus Torvalds {
23831da177e4SLinus Torvalds 	struct tcp_iter_state *st;
23845e659e4cSPavel Emelyanov 	int len;
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN) {
23871da177e4SLinus Torvalds 		seq_printf(seq, "%-*s\n", TMPSZ - 1,
23881da177e4SLinus Torvalds 			   "  sl  local_address rem_address   st tx_queue "
23891da177e4SLinus Torvalds 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
23901da177e4SLinus Torvalds 			   "inode");
23911da177e4SLinus Torvalds 		goto out;
23921da177e4SLinus Torvalds 	}
23931da177e4SLinus Torvalds 	st = seq->private;
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 	switch (st->state) {
23961da177e4SLinus Torvalds 	case TCP_SEQ_STATE_LISTENING:
23971da177e4SLinus Torvalds 	case TCP_SEQ_STATE_ESTABLISHED:
23985e659e4cSPavel Emelyanov 		get_tcp4_sock(v, seq, st->num, &len);
23991da177e4SLinus Torvalds 		break;
24001da177e4SLinus Torvalds 	case TCP_SEQ_STATE_OPENREQ:
24015e659e4cSPavel Emelyanov 		get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
24021da177e4SLinus Torvalds 		break;
24031da177e4SLinus Torvalds 	case TCP_SEQ_STATE_TIME_WAIT:
24045e659e4cSPavel Emelyanov 		get_timewait4_sock(v, seq, st->num, &len);
24051da177e4SLinus Torvalds 		break;
24061da177e4SLinus Torvalds 	}
24075e659e4cSPavel Emelyanov 	seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
24081da177e4SLinus Torvalds out:
24091da177e4SLinus Torvalds 	return 0;
24101da177e4SLinus Torvalds }
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds static struct tcp_seq_afinfo tcp4_seq_afinfo = {
24131da177e4SLinus Torvalds 	.name		= "tcp",
24141da177e4SLinus Torvalds 	.family		= AF_INET,
24155f4472c5SDenis V. Lunev 	.seq_fops	= {
24165f4472c5SDenis V. Lunev 		.owner		= THIS_MODULE,
24175f4472c5SDenis V. Lunev 	},
24189427c4b3SDenis V. Lunev 	.seq_ops	= {
24199427c4b3SDenis V. Lunev 		.show		= tcp4_seq_show,
24209427c4b3SDenis V. Lunev 	},
24211da177e4SLinus Torvalds };
24221da177e4SLinus Torvalds 
2423757764f6SPavel Emelyanov static int tcp4_proc_init_net(struct net *net)
2424757764f6SPavel Emelyanov {
2425757764f6SPavel Emelyanov 	return tcp_proc_register(net, &tcp4_seq_afinfo);
2426757764f6SPavel Emelyanov }
2427757764f6SPavel Emelyanov 
2428757764f6SPavel Emelyanov static void tcp4_proc_exit_net(struct net *net)
2429757764f6SPavel Emelyanov {
2430757764f6SPavel Emelyanov 	tcp_proc_unregister(net, &tcp4_seq_afinfo);
2431757764f6SPavel Emelyanov }
2432757764f6SPavel Emelyanov 
2433757764f6SPavel Emelyanov static struct pernet_operations tcp4_net_ops = {
2434757764f6SPavel Emelyanov 	.init = tcp4_proc_init_net,
2435757764f6SPavel Emelyanov 	.exit = tcp4_proc_exit_net,
2436757764f6SPavel Emelyanov };
2437757764f6SPavel Emelyanov 
24381da177e4SLinus Torvalds int __init tcp4_proc_init(void)
24391da177e4SLinus Torvalds {
2440757764f6SPavel Emelyanov 	return register_pernet_subsys(&tcp4_net_ops);
24411da177e4SLinus Torvalds }
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds void tcp4_proc_exit(void)
24441da177e4SLinus Torvalds {
2445757764f6SPavel Emelyanov 	unregister_pernet_subsys(&tcp4_net_ops);
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
24481da177e4SLinus Torvalds 
2449bf296b12SHerbert Xu struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2450bf296b12SHerbert Xu {
245136e7b1b8SHerbert Xu 	struct iphdr *iph = skb_gro_network_header(skb);
2452bf296b12SHerbert Xu 
2453bf296b12SHerbert Xu 	switch (skb->ip_summed) {
2454bf296b12SHerbert Xu 	case CHECKSUM_COMPLETE:
245586911732SHerbert Xu 		if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
2456bf296b12SHerbert Xu 				  skb->csum)) {
2457bf296b12SHerbert Xu 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2458bf296b12SHerbert Xu 			break;
2459bf296b12SHerbert Xu 		}
2460bf296b12SHerbert Xu 
2461bf296b12SHerbert Xu 		/* fall through */
2462bf296b12SHerbert Xu 	case CHECKSUM_NONE:
2463bf296b12SHerbert Xu 		NAPI_GRO_CB(skb)->flush = 1;
2464bf296b12SHerbert Xu 		return NULL;
2465bf296b12SHerbert Xu 	}
2466bf296b12SHerbert Xu 
2467bf296b12SHerbert Xu 	return tcp_gro_receive(head, skb);
2468bf296b12SHerbert Xu }
2469bf296b12SHerbert Xu EXPORT_SYMBOL(tcp4_gro_receive);
2470bf296b12SHerbert Xu 
2471bf296b12SHerbert Xu int tcp4_gro_complete(struct sk_buff *skb)
2472bf296b12SHerbert Xu {
2473bf296b12SHerbert Xu 	struct iphdr *iph = ip_hdr(skb);
2474bf296b12SHerbert Xu 	struct tcphdr *th = tcp_hdr(skb);
2475bf296b12SHerbert Xu 
2476bf296b12SHerbert Xu 	th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
2477bf296b12SHerbert Xu 				  iph->saddr, iph->daddr, 0);
2478bf296b12SHerbert Xu 	skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
2479bf296b12SHerbert Xu 
2480bf296b12SHerbert Xu 	return tcp_gro_complete(skb);
2481bf296b12SHerbert Xu }
2482bf296b12SHerbert Xu EXPORT_SYMBOL(tcp4_gro_complete);
2483bf296b12SHerbert Xu 
24841da177e4SLinus Torvalds struct proto tcp_prot = {
24851da177e4SLinus Torvalds 	.name			= "TCP",
24861da177e4SLinus Torvalds 	.owner			= THIS_MODULE,
24871da177e4SLinus Torvalds 	.close			= tcp_close,
24881da177e4SLinus Torvalds 	.connect		= tcp_v4_connect,
24891da177e4SLinus Torvalds 	.disconnect		= tcp_disconnect,
2490463c84b9SArnaldo Carvalho de Melo 	.accept			= inet_csk_accept,
24911da177e4SLinus Torvalds 	.ioctl			= tcp_ioctl,
24921da177e4SLinus Torvalds 	.init			= tcp_v4_init_sock,
24931da177e4SLinus Torvalds 	.destroy		= tcp_v4_destroy_sock,
24941da177e4SLinus Torvalds 	.shutdown		= tcp_shutdown,
24951da177e4SLinus Torvalds 	.setsockopt		= tcp_setsockopt,
24961da177e4SLinus Torvalds 	.getsockopt		= tcp_getsockopt,
24971da177e4SLinus Torvalds 	.recvmsg		= tcp_recvmsg,
24981da177e4SLinus Torvalds 	.backlog_rcv		= tcp_v4_do_rcv,
2499ab1e0a13SArnaldo Carvalho de Melo 	.hash			= inet_hash,
2500ab1e0a13SArnaldo Carvalho de Melo 	.unhash			= inet_unhash,
2501ab1e0a13SArnaldo Carvalho de Melo 	.get_port		= inet_csk_get_port,
25021da177e4SLinus Torvalds 	.enter_memory_pressure	= tcp_enter_memory_pressure,
25031da177e4SLinus Torvalds 	.sockets_allocated	= &tcp_sockets_allocated,
25040a5578cfSArnaldo Carvalho de Melo 	.orphan_count		= &tcp_orphan_count,
25051da177e4SLinus Torvalds 	.memory_allocated	= &tcp_memory_allocated,
25061da177e4SLinus Torvalds 	.memory_pressure	= &tcp_memory_pressure,
25071da177e4SLinus Torvalds 	.sysctl_mem		= sysctl_tcp_mem,
25081da177e4SLinus Torvalds 	.sysctl_wmem		= sysctl_tcp_wmem,
25091da177e4SLinus Torvalds 	.sysctl_rmem		= sysctl_tcp_rmem,
25101da177e4SLinus Torvalds 	.max_header		= MAX_TCP_HEADER,
25111da177e4SLinus Torvalds 	.obj_size		= sizeof(struct tcp_sock),
25123ab5aee7SEric Dumazet 	.slab_flags		= SLAB_DESTROY_BY_RCU,
25136d6ee43eSArnaldo Carvalho de Melo 	.twsk_prot		= &tcp_timewait_sock_ops,
251460236fddSArnaldo Carvalho de Melo 	.rsk_prot		= &tcp_request_sock_ops,
251539d8cda7SPavel Emelyanov 	.h.hashinfo		= &tcp_hashinfo,
2516543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
2517543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt	= compat_tcp_setsockopt,
2518543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt	= compat_tcp_getsockopt,
2519543d9cfeSArnaldo Carvalho de Melo #endif
25201da177e4SLinus Torvalds };
25211da177e4SLinus Torvalds 
2522046ee902SDenis V. Lunev 
2523046ee902SDenis V. Lunev static int __net_init tcp_sk_init(struct net *net)
2524046ee902SDenis V. Lunev {
2525046ee902SDenis V. Lunev 	return inet_ctl_sock_create(&net->ipv4.tcp_sock,
2526046ee902SDenis V. Lunev 				    PF_INET, SOCK_RAW, IPPROTO_TCP, net);
2527046ee902SDenis V. Lunev }
2528046ee902SDenis V. Lunev 
2529046ee902SDenis V. Lunev static void __net_exit tcp_sk_exit(struct net *net)
2530046ee902SDenis V. Lunev {
2531046ee902SDenis V. Lunev 	inet_ctl_sock_destroy(net->ipv4.tcp_sock);
2532d315492bSDaniel Lezcano 	inet_twsk_purge(net, &tcp_hashinfo, &tcp_death_row, AF_INET);
2533046ee902SDenis V. Lunev }
2534046ee902SDenis V. Lunev 
2535046ee902SDenis V. Lunev static struct pernet_operations __net_initdata tcp_sk_ops = {
2536046ee902SDenis V. Lunev        .init = tcp_sk_init,
2537046ee902SDenis V. Lunev        .exit = tcp_sk_exit,
2538046ee902SDenis V. Lunev };
2539046ee902SDenis V. Lunev 
25409b0f976fSDenis V. Lunev void __init tcp_v4_init(void)
25411da177e4SLinus Torvalds {
25425caea4eaSEric Dumazet 	inet_hashinfo_init(&tcp_hashinfo);
25436a1b3054SEric W. Biederman 	if (register_pernet_subsys(&tcp_sk_ops))
25441da177e4SLinus Torvalds 		panic("Failed to create the TCP control socket.\n");
25451da177e4SLinus Torvalds }
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds EXPORT_SYMBOL(ipv4_specific);
25481da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_hashinfo);
25491da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_prot);
25501da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_conn_request);
25511da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_connect);
25521da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_do_rcv);
25531da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_remember_stamp);
25541da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_send_check);
25551da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
25581da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_proc_register);
25591da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_proc_unregister);
25601da177e4SLinus Torvalds #endif
25611da177e4SLinus Torvalds EXPORT_SYMBOL(sysctl_tcp_low_latency);
25621da177e4SLinus Torvalds 
2563