blob: e1cf810140b02d142d97c3a32f52122d28ce0714 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Implementation of the Transmission Control Protocol(TCP).
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * IPv4 specific functions
9 *
10 *
11 * code split from:
12 * linux/ipv4/tcp.c
13 * linux/ipv4/tcp_input.c
14 * linux/ipv4/tcp_output.c
15 *
16 * See tcp.c for author information
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
24/*
25 * Changes:
26 * David S. Miller : New socket lookup architecture.
27 * This code is dedicated to John Dyson.
28 * David S. Miller : Change semantics of established hash,
29 * half is devoted to TIME_WAIT sockets
30 * and the rest go in the other half.
31 * Andi Kleen : Add support for syncookies and fixed
32 * some bugs: ip options weren't passed to
33 * the TCP layer, missed a check for an
34 * ACK bit.
35 * Andi Kleen : Implemented fast path mtu discovery.
36 * Fixed many serious bugs in the
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -070037 * request_sock handling and moved
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * most of it into the af independent code.
39 * Added tail drop and some other bugfixes.
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -080040 * Added new listen semantics.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * Mike McLagan : Routing by source
42 * Juan Jose Ciarlante: ip_dynaddr bits
43 * Andi Kleen: various fixes.
44 * Vitaly E. Lavrov : Transparent proxy revived after year
45 * coma.
46 * Andi Kleen : Fix new listen.
47 * Andi Kleen : Fix accept error reporting.
48 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
49 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
50 * a single port at the same time.
51 */
52
Joe Perchesafd465032012-03-12 07:03:32 +000053#define pr_fmt(fmt) "TCP: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Herbert Xueb4dea52008-12-29 23:04:08 -080055#include <linux/bottom_half.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/fcntl.h>
58#include <linux/module.h>
59#include <linux/random.h>
60#include <linux/cache.h>
61#include <linux/jhash.h>
62#include <linux/init.h>
63#include <linux/times.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090064#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020066#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <net/icmp.h>
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -070068#include <net/inet_hashtables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <net/tcp.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030070#include <net/transp_v6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <net/ipv6.h>
72#include <net/inet_common.h>
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -080073#include <net/timewait_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <net/xfrm.h>
David S. Miller6e5714e2011-08-03 20:50:44 -070075#include <net/secure_seq.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +030076#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#include <linux/inet.h>
79#include <linux/ipv6.h>
80#include <linux/stddef.h>
81#include <linux/proc_fs.h>
82#include <linux/seq_file.h>
83
Herbert Xucf80e0e2016-01-24 21:20:23 +080084#include <crypto/hash.h>
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080085#include <linux/scatterlist.h>
86
Brian Haleyab32ea52006-09-22 14:15:41 -070087int sysctl_tcp_tw_reuse __read_mostly;
88int sysctl_tcp_low_latency __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080090#ifdef CONFIG_TCP_MD5SIG
Eric Dumazeta915da9b2012-01-31 05:18:33 +000091static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
Eric Dumazet318cf7a2011-10-24 02:46:04 -040092 __be32 daddr, __be32 saddr, const struct tcphdr *th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080093#endif
94
Eric Dumazet5caea4e2008-11-20 00:40:07 -080095struct inet_hashinfo tcp_hashinfo;
Eric Dumazet4bc2f182010-07-09 21:22:10 +000096EXPORT_SYMBOL(tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Octavian Purdila936b8bd2014-06-25 17:09:57 +030098static __u32 tcp_v4_init_sequence(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700100 return secure_tcp_sequence_number(ip_hdr(skb)->daddr,
101 ip_hdr(skb)->saddr,
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700102 tcp_hdr(skb)->dest,
103 tcp_hdr(skb)->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800106int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
107{
108 const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
109 struct tcp_sock *tp = tcp_sk(sk);
110
111 /* With PAWS, it is safe from the viewpoint
112 of data integrity. Even without PAWS it is safe provided sequence
113 spaces do not overlap i.e. at data rates <= 80Mbit/sec.
114
115 Actually, the idea is close to VJ's one, only timestamp cache is
116 held not per host, but per port pair and TW bucket is used as state
117 holder.
118
119 If TW bucket has been already destroyed we fall back to VJ's scheme
120 and use initial timestamp retrieved from peer table.
121 */
122 if (tcptw->tw_ts_recent_stamp &&
Ian Morris51456b22015-04-03 09:17:26 +0100123 (!twp || (sysctl_tcp_tw_reuse &&
James Morris9d729f72007-03-04 16:12:44 -0800124 get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800125 tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
126 if (tp->write_seq == 0)
127 tp->write_seq = 1;
128 tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
129 tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
130 sock_hold(sktw);
131 return 1;
132 }
133
134 return 0;
135}
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800136EXPORT_SYMBOL_GPL(tcp_twsk_unique);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* This will initiate an outgoing connection. */
139int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
140{
David S. Miller2d7192d2011-04-26 13:28:44 -0700141 struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct inet_sock *inet = inet_sk(sk);
143 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerdca8b082011-02-24 13:38:12 -0800144 __be16 orig_sport, orig_dport;
Al Virobada8ad2006-09-26 21:27:15 -0700145 __be32 daddr, nexthop;
David S. Millerda905bd2011-05-06 16:11:19 -0700146 struct flowi4 *fl4;
David S. Miller2d7192d2011-04-26 13:28:44 -0700147 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int err;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000149 struct ip_options_rcu *inet_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (addr_len < sizeof(struct sockaddr_in))
152 return -EINVAL;
153
154 if (usin->sin_family != AF_INET)
155 return -EAFNOSUPPORT;
156
157 nexthop = daddr = usin->sin_addr.s_addr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000158 inet_opt = rcu_dereference_protected(inet->inet_opt,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200159 lockdep_sock_is_held(sk));
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000160 if (inet_opt && inet_opt->opt.srr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!daddr)
162 return -EINVAL;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000163 nexthop = inet_opt->opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
David S. Millerdca8b082011-02-24 13:38:12 -0800166 orig_sport = inet->inet_sport;
167 orig_dport = usin->sin_port;
David S. Millerda905bd2011-05-06 16:11:19 -0700168 fl4 = &inet->cork.fl.u.ip4;
169 rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
David S. Millerb23dd4f2011-03-02 14:31:35 -0800170 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
171 IPPROTO_TCP,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +0200172 orig_sport, orig_dport, sk);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800173 if (IS_ERR(rt)) {
174 err = PTR_ERR(rt);
175 if (err == -ENETUNREACH)
Eric Dumazetf1d8cba2013-11-28 09:51:22 -0800176 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800177 return err;
Wei Dong584bdf82007-05-31 22:49:28 -0700178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
181 ip_rt_put(rt);
182 return -ENETUNREACH;
183 }
184
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000185 if (!inet_opt || !inet_opt->opt.srr)
David S. Millerda905bd2011-05-06 16:11:19 -0700186 daddr = fl4->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000188 if (!inet->inet_saddr)
David S. Millerda905bd2011-05-06 16:11:19 -0700189 inet->inet_saddr = fl4->saddr;
Eric Dumazetd1e559d2015-03-18 14:05:35 -0700190 sk_rcv_saddr_set(sk, inet->inet_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000192 if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* Reset inherited state */
194 tp->rx_opt.ts_recent = 0;
195 tp->rx_opt.ts_recent_stamp = 0;
Pavel Emelyanovee995282012-04-19 03:40:39 +0000196 if (likely(!tp->repair))
197 tp->write_seq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -0700200 if (tcp_death_row.sysctl_tw_recycle &&
David S. Miller81166dd2012-07-10 03:14:24 -0700201 !tp->rx_opt.ts_recent_stamp && fl4->daddr == daddr)
202 tcp_fetch_timewait_stamp(sk, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000204 inet->inet_dport = usin->sin_port;
Eric Dumazetd1e559d2015-03-18 14:05:35 -0700205 sk_daddr_set(sk, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800207 inet_csk(sk)->icsk_ext_hdr_len = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000208 if (inet_opt)
209 inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000211 tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Socket identity is still unknown (sport may be zero).
214 * However we set state to SYN-SENT and not releasing socket
215 * lock select source port, enter ourselves into the hash tables and
216 * complete initialization after this.
217 */
218 tcp_set_state(sk, TCP_SYN_SENT);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800219 err = inet_hash_connect(&tcp_death_row, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (err)
221 goto failure;
222
Tom Herbert877d1f62015-07-28 16:02:05 -0700223 sk_set_txhash(sk);
Sathya Perla9e7ceb02014-10-22 21:42:01 +0530224
David S. Millerda905bd2011-05-06 16:11:19 -0700225 rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
David S. Millerb23dd4f2011-03-02 14:31:35 -0800226 inet->inet_sport, inet->inet_dport, sk);
227 if (IS_ERR(rt)) {
228 err = PTR_ERR(rt);
229 rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto failure;
David S. Millerb23dd4f2011-03-02 14:31:35 -0800231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* OK, now commit destination to socket. */
Herbert Xubcd76112006-06-30 13:36:35 -0700233 sk->sk_gso_type = SKB_GSO_TCPV4;
Changli Gaod8d1f302010-06-10 23:31:35 -0700234 sk_setup_caps(sk, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Pavel Emelyanovee995282012-04-19 03:40:39 +0000236 if (!tp->write_seq && likely(!tp->repair))
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000237 tp->write_seq = secure_tcp_sequence_number(inet->inet_saddr,
238 inet->inet_daddr,
239 inet->inet_sport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 usin->sin_port);
241
Eric Dumazeta67a32d2019-11-01 10:32:19 -0700242 inet->inet_id = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Andrey Vagin2b916472012-11-22 01:13:58 +0000244 err = tcp_connect(sk);
Pavel Emelyanovee995282012-04-19 03:40:39 +0000245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 rt = NULL;
247 if (err)
248 goto failure;
249
250 return 0;
251
252failure:
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200253 /*
254 * This unhashes the socket and releases the local port,
255 * if necessary.
256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 tcp_set_state(sk, TCP_CLOSE);
258 ip_rt_put(rt);
259 sk->sk_route_caps = 0;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000260 inet->inet_dport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return err;
262}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000263EXPORT_SYMBOL(tcp_v4_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
Eric Dumazet563d34d2012-07-23 09:48:52 +0200266 * This routine reacts to ICMP_FRAG_NEEDED mtu indications as defined in RFC1191.
267 * It can be called through tcp_release_cb() if socket was owned by user
268 * at the time tcp_v4_err() was called to handle ICMP message.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Neal Cardwell4fab9072014-08-14 12:40:05 -0400270void tcp_v4_mtu_reduced(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet07753bc2017-03-03 14:08:21 -0800273 struct dst_entry *dst;
274 u32 mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Eric Dumazet07753bc2017-03-03 14:08:21 -0800276 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
277 return;
278 mtu = tcp_sk(sk)->mtu_info;
David S. Miller80d0a692012-07-16 03:28:06 -0700279 dst = inet_csk_update_pmtu(sk, mtu);
280 if (!dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* Something is about to be wrong... Remember soft error
284 * for the case, if this connection will not able to recover.
285 */
286 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
287 sk->sk_err_soft = EMSGSIZE;
288
289 mtu = dst_mtu(dst);
290
291 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
Hannes Frederic Sowa482fc602013-11-05 02:24:17 +0100292 ip_sk_accept_pmtu(sk) &&
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800293 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 tcp_sync_mss(sk, mtu);
295
296 /* Resend the TCP packet because it's
297 * clear that the old packet has been
298 * dropped. This is the new "fast" path mtu
299 * discovery.
300 */
301 tcp_simple_retransmit(sk);
302 } /* else let the usual retransmit timer handle it */
303}
Neal Cardwell4fab9072014-08-14 12:40:05 -0400304EXPORT_SYMBOL(tcp_v4_mtu_reduced);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David S. Miller55be7a92012-07-11 21:27:49 -0700306static void do_redirect(struct sk_buff *skb, struct sock *sk)
307{
308 struct dst_entry *dst = __sk_dst_check(sk, 0);
309
David S. Miller1ed5c482012-07-12 00:41:25 -0700310 if (dst)
David S. Miller6700c272012-07-17 03:29:28 -0700311 dst->ops->redirect(dst, sk, skb);
David S. Miller55be7a92012-07-11 21:27:49 -0700312}
313
Eric Dumazet26e37362015-03-22 10:22:22 -0700314
315/* handle ICMP messages on TCP_NEW_SYN_RECV request sockets */
Eric Dumazet9cf74902016-02-02 19:31:12 -0800316void tcp_req_err(struct sock *sk, u32 seq, bool abort)
Eric Dumazet26e37362015-03-22 10:22:22 -0700317{
318 struct request_sock *req = inet_reqsk(sk);
319 struct net *net = sock_net(sk);
320
321 /* ICMPs are not backlogged, hence we cannot get
322 * an established socket here.
323 */
Eric Dumazet26e37362015-03-22 10:22:22 -0700324 if (seq != tcp_rsk(req)->snt_isn) {
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700325 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
Eric Dumazet9cf74902016-02-02 19:31:12 -0800326 } else if (abort) {
Eric Dumazet26e37362015-03-22 10:22:22 -0700327 /*
328 * Still in SYN_RECV, just remove it silently.
329 * There is no good way to pass the error to the newly
330 * created socket, and POSIX does not want network
331 * errors returned from accept().
332 */
Fan Duc6973662015-03-23 15:00:41 -0700333 inet_csk_reqsk_queue_drop(req->rsk_listener, req);
Eric Dumazet9caad862016-04-01 08:52:20 -0700334 tcp_listendrop(req->rsk_listener);
Eric Dumazet26e37362015-03-22 10:22:22 -0700335 }
Eric Dumazetef84d8c2015-10-14 11:16:26 -0700336 reqsk_put(req);
Eric Dumazet26e37362015-03-22 10:22:22 -0700337}
338EXPORT_SYMBOL(tcp_req_err);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
341 * This routine is called by the ICMP module when it gets some
342 * sort of error condition. If err < 0 then the socket should
343 * be closed and the error returned to the user. If err > 0
344 * it's just the icmp type << 8 | icmp code. After adjustment
345 * header points to the first 8 bytes of the tcp header. We need
346 * to find the appropriate port.
347 *
348 * The locking strategy used here is very "optimistic". When
349 * someone else accesses the socket the ICMP is just dropped
350 * and for some paths there is no check at all.
351 * A more general error queue to queue errors for later handling
352 * is probably better.
353 *
354 */
355
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000356void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000358 const struct iphdr *iph = (const struct iphdr *)icmp_skb->data;
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000359 struct tcphdr *th = (struct tcphdr *)(icmp_skb->data + (iph->ihl << 2));
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000360 struct inet_connection_sock *icsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct tcp_sock *tp;
362 struct inet_sock *inet;
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000363 const int type = icmp_hdr(icmp_skb)->type;
364 const int code = icmp_hdr(icmp_skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct sock *sk;
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000366 struct sk_buff *skb;
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700367 struct request_sock *fastopen;
368 __u32 seq, snd_una;
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000369 __u32 remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int err;
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000371 struct net *net = dev_net(icmp_skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Eric Dumazet26e37362015-03-22 10:22:22 -0700373 sk = __inet_lookup_established(net, &tcp_hashinfo, iph->daddr,
374 th->dest, iph->saddr, ntohs(th->source),
375 inet_iif(icmp_skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!sk) {
Eric Dumazet5d3848b2016-04-27 16:44:29 -0700377 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return;
379 }
380 if (sk->sk_state == TCP_TIME_WAIT) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -0700381 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return;
383 }
Eric Dumazet26e37362015-03-22 10:22:22 -0700384 seq = ntohl(th->seq);
385 if (sk->sk_state == TCP_NEW_SYN_RECV)
Eric Dumazet9cf74902016-02-02 19:31:12 -0800386 return tcp_req_err(sk, seq,
387 type == ICMP_PARAMETERPROB ||
388 type == ICMP_TIME_EXCEEDED ||
389 (type == ICMP_DEST_UNREACH &&
390 (code == ICMP_NET_UNREACH ||
391 code == ICMP_HOST_UNREACH)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 bh_lock_sock(sk);
394 /* If too many ICMPs get dropped on busy
395 * servers this needs to be solved differently.
Eric Dumazet563d34d2012-07-23 09:48:52 +0200396 * We do take care of PMTU discovery (RFC1191) special case :
397 * we can receive locally generated ICMP messages while socket is held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Eric Dumazetb74aa932013-01-19 16:10:37 +0000399 if (sock_owned_by_user(sk)) {
400 if (!(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED))
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700401 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
Eric Dumazetb74aa932013-01-19 16:10:37 +0000402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (sk->sk_state == TCP_CLOSE)
404 goto out;
405
stephen hemminger97e3ecd12010-03-18 11:27:32 +0000406 if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700407 __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
stephen hemminger97e3ecd12010-03-18 11:27:32 +0000408 goto out;
409 }
410
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000411 icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 tp = tcp_sk(sk);
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700413 /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
414 fastopen = tp->fastopen_rsk;
415 snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (sk->sk_state != TCP_LISTEN &&
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700417 !between(seq, snd_una, tp->snd_nxt)) {
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700418 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 goto out;
420 }
421
422 switch (type) {
David S. Miller55be7a92012-07-11 21:27:49 -0700423 case ICMP_REDIRECT:
Jon Maxwell98933eb2017-03-10 16:40:33 +1100424 if (!sock_owned_by_user(sk))
425 do_redirect(icmp_skb, sk);
David S. Miller55be7a92012-07-11 21:27:49 -0700426 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 case ICMP_SOURCE_QUENCH:
428 /* Just silently ignore these. */
429 goto out;
430 case ICMP_PARAMETERPROB:
431 err = EPROTO;
432 break;
433 case ICMP_DEST_UNREACH:
434 if (code > NR_ICMP_UNREACH)
435 goto out;
436
437 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
Eric Dumazet0d4f0602013-03-18 07:01:28 +0000438 /* We are not interested in TCP_LISTEN and open_requests
439 * (SYN-ACKs send out by Linux are always <576bytes so
440 * they should go through unfragmented).
441 */
442 if (sk->sk_state == TCP_LISTEN)
443 goto out;
444
Eric Dumazet563d34d2012-07-23 09:48:52 +0200445 tp->mtu_info = info;
Eric Dumazet144d56e2012-08-20 00:22:46 +0000446 if (!sock_owned_by_user(sk)) {
Eric Dumazet563d34d2012-07-23 09:48:52 +0200447 tcp_v4_mtu_reduced(sk);
Eric Dumazet144d56e2012-08-20 00:22:46 +0000448 } else {
449 if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED, &tp->tsq_flags))
450 sock_hold(sk);
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto out;
453 }
454
455 err = icmp_err_convert[code].errno;
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000456 /* check if icmp_skb allows revert of backoff
457 * (see draft-zimmermann-tcp-lcd) */
458 if (code != ICMP_NET_UNREACH && code != ICMP_HOST_UNREACH)
459 break;
460 if (seq != tp->snd_una || !icsk->icsk_retransmits ||
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700461 !icsk->icsk_backoff || fastopen)
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000462 break;
463
David S. Miller8f49c272010-11-12 13:35:00 -0800464 if (sock_owned_by_user(sk))
465 break;
466
Eric Dumazet209d8d22019-02-15 13:36:21 -0800467 skb = tcp_write_queue_head(sk);
468 if (WARN_ON_ONCE(!skb))
469 break;
470
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000471 icsk->icsk_backoff--;
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -0700472 icsk->icsk_rto = tp->srtt_us ? __tcp_set_rto(tp) :
473 TCP_TIMEOUT_INIT;
474 icsk->icsk_rto = inet_csk_rto_backoff(icsk, TCP_RTO_MAX);
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000475
Eric Dumazet7faee5c2014-09-05 15:33:33 -0700476 remaining = icsk->icsk_rto -
477 min(icsk->icsk_rto,
478 tcp_time_stamp - tcp_skb_timestamp(skb));
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000479
480 if (remaining) {
481 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
482 remaining, TCP_RTO_MAX);
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000483 } else {
484 /* RTO revert clocked out retransmission.
485 * Will retransmit now */
486 tcp_retransmit_timer(sk);
487 }
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 case ICMP_TIME_EXCEEDED:
491 err = EHOSTUNREACH;
492 break;
493 default:
494 goto out;
495 }
496
497 switch (sk->sk_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 case TCP_SYN_SENT:
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700499 case TCP_SYN_RECV:
500 /* Only in fast or simultaneous open. If a fast open socket is
501 * is already accepted it is treated as a connected one below.
502 */
Ian Morris51456b22015-04-03 09:17:26 +0100503 if (fastopen && !fastopen->sk)
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700504 break;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!sock_owned_by_user(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 sk->sk_err = err;
508
509 sk->sk_error_report(sk);
510
511 tcp_done(sk);
512 } else {
513 sk->sk_err_soft = err;
514 }
515 goto out;
516 }
517
518 /* If we've already connected we will keep trying
519 * until we time out, or the user gives up.
520 *
521 * rfc1122 4.2.3.9 allows to consider as hard errors
522 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
523 * but it is obsoleted by pmtu discovery).
524 *
525 * Note, that in modern internet, where routing is unreliable
526 * and in each dark corner broken firewalls sit, sending random
527 * errors ordered by their masters even this two messages finally lose
528 * their original sense (even Linux sends invalid PORT_UNREACHs)
529 *
530 * Now we are in compliance with RFCs.
531 * --ANK (980905)
532 */
533
534 inet = inet_sk(sk);
535 if (!sock_owned_by_user(sk) && inet->recverr) {
536 sk->sk_err = err;
537 sk->sk_error_report(sk);
538 } else { /* Only an error on timeout */
539 sk->sk_err_soft = err;
540 }
541
542out:
543 bh_unlock_sock(sk);
544 sock_put(sk);
545}
546
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000547void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700549 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Patrick McHardy84fa7932006-08-29 16:44:56 -0700551 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Herbert Xu419f9f82010-04-11 02:15:53 +0000552 th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700553 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800554 skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 } else {
Herbert Xu419f9f82010-04-11 02:15:53 +0000556 th->check = tcp_v4_check(skb->len, saddr, daddr,
Joe Perches07f07572008-11-19 15:44:53 -0800557 csum_partial(th,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 th->doff << 2,
559 skb->csum));
560 }
561}
562
Herbert Xu419f9f82010-04-11 02:15:53 +0000563/* This routine computes an IPv4 TCP checksum. */
Herbert Xubb296242010-04-11 02:15:55 +0000564void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
Herbert Xu419f9f82010-04-11 02:15:53 +0000565{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400566 const struct inet_sock *inet = inet_sk(sk);
Herbert Xu419f9f82010-04-11 02:15:53 +0000567
568 __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr);
569}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000570EXPORT_SYMBOL(tcp_v4_send_check);
Herbert Xu419f9f82010-04-11 02:15:53 +0000571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
573 * This routine will send an RST to the other tcp.
574 *
575 * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
576 * for reset.
577 * Answer: if a packet caused RST, it is not for a socket
578 * existing in our system, if it is matched to a socket,
579 * it is just duplicate segment or bug in other side's TCP.
580 * So that we build reply only basing on parameters
581 * arrived with segment.
582 * Exception: precedence violation. We do not implement it in any case.
583 */
584
Eric Dumazeta00e7442015-09-29 07:42:39 -0700585static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400587 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800588 struct {
589 struct tcphdr th;
590#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800591 __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800592#endif
593 } rep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800595#ifdef CONFIG_TCP_MD5SIG
Florian Westphale46787f2015-12-21 21:29:25 +0100596 struct tcp_md5sig_key *key = NULL;
Shawn Lu658ddaa2012-01-31 22:35:48 +0000597 const __u8 *hash_location = NULL;
598 unsigned char newhash[16];
599 int genhash;
600 struct sock *sk1 = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800601#endif
Pavel Emelyanova86b1e32008-07-16 20:20:58 -0700602 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* Never send a reset in response to a reset. */
605 if (th->rst)
606 return;
607
Eric Dumazetc3658e82014-11-25 07:40:04 -0800608 /* If sk not NULL, it means we did a successful lookup and incoming
609 * route had to be correct. prequeue might have dropped our dst.
610 */
611 if (!sk && skb_rtable(skb)->rt_type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return;
613
614 /* Swap the send and the receive. */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800615 memset(&rep, 0, sizeof(rep));
616 rep.th.dest = th->source;
617 rep.th.source = th->dest;
618 rep.th.doff = sizeof(struct tcphdr) / 4;
619 rep.th.rst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (th->ack) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800622 rep.th.seq = th->ack_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800624 rep.th.ack = 1;
625 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
626 skb->len - (th->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200629 memset(&arg, 0, sizeof(arg));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800630 arg.iov[0].iov_base = (unsigned char *)&rep;
631 arg.iov[0].iov_len = sizeof(rep.th);
632
Eric Dumazet0f85fea2014-12-09 09:56:08 -0800633 net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800634#ifdef CONFIG_TCP_MD5SIG
Eric Dumazet3b24d852016-04-01 08:52:17 -0700635 rcu_read_lock();
Shawn Lu658ddaa2012-01-31 22:35:48 +0000636 hash_location = tcp_parse_md5sig_option(th);
Florian Westphal271c3b92015-12-21 21:29:26 +0100637 if (sk && sk_fullsock(sk)) {
Florian Westphale46787f2015-12-21 21:29:25 +0100638 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)
639 &ip_hdr(skb)->saddr, AF_INET);
640 } else if (hash_location) {
Shawn Lu658ddaa2012-01-31 22:35:48 +0000641 /*
642 * active side is lost. Try to find listening socket through
643 * source port, and then find md5 key through listening socket.
644 * we are not loose security here:
645 * Incoming packet is checked with md5 hash with finding key,
646 * no RST generated if md5 hash doesn't match.
647 */
Craig Galleka5836362016-02-10 11:50:38 -0500648 sk1 = __inet_lookup_listener(net, &tcp_hashinfo, NULL, 0,
649 ip_hdr(skb)->saddr,
Tom Herbertda5e3632013-01-22 09:50:24 +0000650 th->source, ip_hdr(skb)->daddr,
Shawn Lu658ddaa2012-01-31 22:35:48 +0000651 ntohs(th->source), inet_iif(skb));
652 /* don't send rst if it can't find key */
653 if (!sk1)
Eric Dumazet3b24d852016-04-01 08:52:17 -0700654 goto out;
655
Shawn Lu658ddaa2012-01-31 22:35:48 +0000656 key = tcp_md5_do_lookup(sk1, (union tcp_md5_addr *)
657 &ip_hdr(skb)->saddr, AF_INET);
658 if (!key)
Eric Dumazet3b24d852016-04-01 08:52:17 -0700659 goto out;
660
Shawn Lu658ddaa2012-01-31 22:35:48 +0000661
Eric Dumazet39f8e582015-03-24 15:58:55 -0700662 genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
Shawn Lu658ddaa2012-01-31 22:35:48 +0000663 if (genhash || memcmp(hash_location, newhash, 16) != 0)
Eric Dumazet3b24d852016-04-01 08:52:17 -0700664 goto out;
665
Shawn Lu658ddaa2012-01-31 22:35:48 +0000666 }
667
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800668 if (key) {
669 rep.opt[0] = htonl((TCPOPT_NOP << 24) |
670 (TCPOPT_NOP << 16) |
671 (TCPOPT_MD5SIG << 8) |
672 TCPOLEN_MD5SIG);
673 /* Update length and the length the header thinks exists */
674 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
675 rep.th.doff = arg.iov[0].iov_len / 4;
676
Adam Langley49a72df2008-07-19 00:01:42 -0700677 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
Ilpo Järvinen78e645cb2008-10-09 14:37:47 -0700678 key, ip_hdr(skb)->saddr,
679 ip_hdr(skb)->daddr, &rep.th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800680 }
681#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700682 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
683 ip_hdr(skb)->saddr, /* XXX */
Ilpo Järvinen52cd5752008-10-08 11:34:06 -0700684 arg.iov[0].iov_len, IPPROTO_TCP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
Florian Westphal271c3b92015-12-21 21:29:26 +0100686 arg.flags = (sk && inet_sk_transparent(sk)) ? IP_REPLY_ARG_NOSRCCHECK : 0;
687
Shawn Lue2446ea2012-02-04 12:38:09 +0000688 /* When socket is gone, all binding information is lost.
Alexey Kuznetsov4c675252012-10-12 04:34:17 +0000689 * routing might fail in this case. No choice here, if we choose to force
690 * input interface, we will misroute in case of asymmetric route.
Shawn Lue2446ea2012-02-04 12:38:09 +0000691 */
Alexey Kuznetsov4c675252012-10-12 04:34:17 +0000692 if (sk)
693 arg.bound_dev_if = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Florian Westphal271c3b92015-12-21 21:29:26 +0100695 BUILD_BUG_ON(offsetof(struct sock, sk_bound_dev_if) !=
696 offsetof(struct inet_timewait_sock, tw_bound_dev_if));
697
Eric Dumazet66b13d92011-10-24 03:06:21 -0400698 arg.tos = ip_hdr(skb)->tos;
Eric Dumazet47dcc202016-05-06 09:46:18 -0700699 local_bh_disable();
Eric Dumazetbdbbb852015-01-29 21:35:05 -0800700 ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
701 skb, &TCP_SKB_CB(skb)->header.h4.opt,
Eric Dumazet24a2d432014-09-27 09:50:55 -0700702 ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
703 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Eric Dumazet90bbcc62016-04-27 16:44:32 -0700705 __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
706 __TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
Eric Dumazet47dcc202016-05-06 09:46:18 -0700707 local_bh_enable();
Shawn Lu658ddaa2012-01-31 22:35:48 +0000708
709#ifdef CONFIG_TCP_MD5SIG
Eric Dumazet3b24d852016-04-01 08:52:17 -0700710out:
711 rcu_read_unlock();
Shawn Lu658ddaa2012-01-31 22:35:48 +0000712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715/* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
716 outside socket context is ugly, certainly. What can I do?
717 */
718
Eric Dumazete62a1232016-01-21 08:02:54 -0800719static void tcp_v4_send_ack(struct net *net,
720 struct sk_buff *skb, u32 seq, u32 ack,
Andrey Vaginee684b62013-02-11 05:50:19 +0000721 u32 win, u32 tsval, u32 tsecr, int oif,
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700722 struct tcp_md5sig_key *key,
Eric Dumazet66b13d92011-10-24 03:06:21 -0400723 int reply_flags, u8 tos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400725 const struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct {
727 struct tcphdr th;
Al Viro714e85b2006-11-14 20:51:49 -0800728 __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800729#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800730 + (TCPOLEN_MD5SIG_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800731#endif
732 ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 } rep;
734 struct ip_reply_arg arg;
735
736 memset(&rep.th, 0, sizeof(struct tcphdr));
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200737 memset(&arg, 0, sizeof(arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 arg.iov[0].iov_base = (unsigned char *)&rep;
740 arg.iov[0].iov_len = sizeof(rep.th);
Andrey Vaginee684b62013-02-11 05:50:19 +0000741 if (tsecr) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800742 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
743 (TCPOPT_TIMESTAMP << 8) |
744 TCPOLEN_TIMESTAMP);
Andrey Vaginee684b62013-02-11 05:50:19 +0000745 rep.opt[1] = htonl(tsval);
746 rep.opt[2] = htonl(tsecr);
Craig Schlentercb48cfe2007-01-09 00:11:15 -0800747 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 /* Swap the send and the receive. */
751 rep.th.dest = th->source;
752 rep.th.source = th->dest;
753 rep.th.doff = arg.iov[0].iov_len / 4;
754 rep.th.seq = htonl(seq);
755 rep.th.ack_seq = htonl(ack);
756 rep.th.ack = 1;
757 rep.th.window = htons(win);
758
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800759#ifdef CONFIG_TCP_MD5SIG
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800760 if (key) {
Andrey Vaginee684b62013-02-11 05:50:19 +0000761 int offset = (tsecr) ? 3 : 0;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800762
763 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
764 (TCPOPT_NOP << 16) |
765 (TCPOPT_MD5SIG << 8) |
766 TCPOLEN_MD5SIG);
767 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
768 rep.th.doff = arg.iov[0].iov_len/4;
769
Adam Langley49a72df2008-07-19 00:01:42 -0700770 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset],
Adam Langley90b7e112008-07-31 20:49:48 -0700771 key, ip_hdr(skb)->saddr,
772 ip_hdr(skb)->daddr, &rep.th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800773 }
774#endif
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700775 arg.flags = reply_flags;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700776 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
777 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 arg.iov[0].iov_len, IPPROTO_TCP, 0);
779 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900780 if (oif)
781 arg.bound_dev_if = oif;
Eric Dumazet66b13d92011-10-24 03:06:21 -0400782 arg.tos = tos;
Eric Dumazet47dcc202016-05-06 09:46:18 -0700783 local_bh_disable();
Eric Dumazetbdbbb852015-01-29 21:35:05 -0800784 ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
785 skb, &TCP_SKB_CB(skb)->header.h4.opt,
Eric Dumazet24a2d432014-09-27 09:50:55 -0700786 ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
787 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Eric Dumazet90bbcc62016-04-27 16:44:32 -0700789 __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
Eric Dumazet47dcc202016-05-06 09:46:18 -0700790 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
794{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700795 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800796 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Eric Dumazete62a1232016-01-21 08:02:54 -0800798 tcp_v4_send_ack(sock_net(sk), skb,
799 tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200800 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
Andrey Vaginee684b62013-02-11 05:50:19 +0000801 tcp_time_stamp + tcptw->tw_ts_offset,
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900802 tcptw->tw_ts_recent,
803 tw->tw_bound_dev_if,
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700804 tcp_twsk_md5_key(tcptw),
Eric Dumazet66b13d92011-10-24 03:06:21 -0400805 tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0,
806 tw->tw_tos
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900807 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700809 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Eric Dumazeta00e7442015-09-29 07:42:39 -0700812static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200813 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jerry Chu168a8f52012-08-31 12:29:13 +0000815 /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV
816 * sk->sk_state == TCP_SYN_RECV -> for Fast Open.
817 */
Eric Dumazete62a1232016-01-21 08:02:54 -0800818 u32 seq = (sk->sk_state == TCP_LISTEN) ? tcp_rsk(req)->snt_isn + 1 :
819 tcp_sk(sk)->snd_nxt;
820
Eric Dumazet20a2b492016-08-22 11:31:10 -0700821 /* RFC 7323 2.3
822 * The window field (SEG.WND) of every outgoing segment, with the
823 * exception of <SYN> segments, MUST be right-shifted by
824 * Rcv.Wind.Shift bits:
825 */
Eric Dumazete62a1232016-01-21 08:02:54 -0800826 tcp_v4_send_ack(sock_net(sk), skb, seq,
Eric Dumazet20a2b492016-08-22 11:31:10 -0700827 tcp_rsk(req)->rcv_nxt,
828 req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale,
Andrey Vaginee684b62013-02-11 05:50:19 +0000829 tcp_time_stamp,
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900830 req->ts_recent,
831 0,
Christoph Paasch7887a702017-12-11 00:05:46 -0800832 tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&ip_hdr(skb)->saddr,
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000833 AF_INET),
Eric Dumazet66b13d92011-10-24 03:06:21 -0400834 inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0,
835 ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/*
Kris Katterjohn9bf1d832008-02-17 22:29:19 -0800839 * Send a SYN-ACK after having received a SYN.
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700840 * This still operates on a request_sock only, not on a big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * socket.
842 */
Eric Dumazet0f935dbe2015-09-25 07:39:21 -0700843static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
Octavian Purdilad6274bd2014-06-25 17:09:58 +0300844 struct flowi *fl,
Octavian Purdila72659ec2010-01-17 19:09:39 -0800845 struct request_sock *req,
Eric Dumazetca6fb062015-10-02 11:43:35 -0700846 struct tcp_fastopen_cookie *foc,
Eric Dumazetb3d05142016-04-13 22:05:39 -0700847 enum tcp_synack_type synack_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700849 const struct inet_request_sock *ireq = inet_rsk(req);
David S. Miller6bd023f2011-05-18 18:32:03 -0400850 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int err = -1;
Weilong Chend41db5a2013-12-23 14:37:28 +0800852 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* First, grab a route. */
David S. Millerba3f7f02012-07-17 14:02:46 -0700855 if (!dst && (dst = inet_csk_route_req(sk, &fl4, req)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800856 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Eric Dumazetb3d05142016-04-13 22:05:39 -0700858 skb = tcp_make_synack(sk, dst, req, foc, synack_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (skb) {
Eric Dumazet634fb9792013-10-09 15:21:29 -0700861 __tcp_v4_send_check(skb, ireq->ir_loc_addr, ireq->ir_rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Eric Dumazet6b2f36b2018-10-02 12:35:05 -0700863 rcu_read_lock();
Eric Dumazet634fb9792013-10-09 15:21:29 -0700864 err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
865 ireq->ir_rmt_addr,
Eric Dumazet6b2f36b2018-10-02 12:35:05 -0700866 rcu_dereference(ireq->ireq_opt));
867 rcu_read_unlock();
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200868 err = net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return err;
872}
873
874/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700875 * IPv4 request_sock destructor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700877static void tcp_v4_reqsk_destructor(struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Eric Dumazet2ffd2612017-10-20 09:04:13 -0700879 kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800882#ifdef CONFIG_TCP_MD5SIG
883/*
884 * RFC2385 MD5 checksumming requires a mapping of
885 * IP address->MD5 Key.
886 * We need to maintain these in the sk structure.
887 */
888
889/* Find the Key structure for an address. */
Eric Dumazetb83e3de2015-09-25 07:39:15 -0700890struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk,
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000891 const union tcp_md5_addr *addr,
892 int family)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800893{
Eric Dumazetfd3a1542015-03-24 15:58:56 -0700894 const struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000895 struct tcp_md5sig_key *key;
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000896 unsigned int size = sizeof(struct in_addr);
Eric Dumazetfd3a1542015-03-24 15:58:56 -0700897 const struct tcp_md5sig_info *md5sig;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800898
Eric Dumazeta8afca02012-01-31 18:45:40 +0000899 /* caller either holds rcu_read_lock() or socket lock */
900 md5sig = rcu_dereference_check(tp->md5sig_info,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200901 lockdep_sock_is_held(sk));
Eric Dumazeta8afca02012-01-31 18:45:40 +0000902 if (!md5sig)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800903 return NULL;
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000904#if IS_ENABLED(CONFIG_IPV6)
905 if (family == AF_INET6)
906 size = sizeof(struct in6_addr);
907#endif
Sasha Levinb67bfe02013-02-27 17:06:00 -0800908 hlist_for_each_entry_rcu(key, &md5sig->head, node) {
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000909 if (key->family != family)
910 continue;
911 if (!memcmp(&key->addr, addr, size))
912 return key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800913 }
914 return NULL;
915}
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000916EXPORT_SYMBOL(tcp_md5_do_lookup);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800917
Eric Dumazetb83e3de2015-09-25 07:39:15 -0700918struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
Eric Dumazetfd3a1542015-03-24 15:58:56 -0700919 const struct sock *addr_sk)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800920{
Eric Dumazetb52e6922015-04-09 14:36:42 -0700921 const union tcp_md5_addr *addr;
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000922
Eric Dumazetb52e6922015-04-09 14:36:42 -0700923 addr = (const union tcp_md5_addr *)&addr_sk->sk_daddr;
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000924 return tcp_md5_do_lookup(sk, addr, AF_INET);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800925}
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800926EXPORT_SYMBOL(tcp_v4_md5_lookup);
927
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800928/* This can be called on a newly created socket, from other files */
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000929int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
930 int family, const u8 *newkey, u8 newkeylen, gfp_t gfp)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800931{
932 /* Add Key to the list */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700933 struct tcp_md5sig_key *key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800934 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000935 struct tcp_md5sig_info *md5sig;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800936
Aydin Arikc0353c72013-06-14 18:56:31 +1200937 key = tcp_md5_do_lookup(sk, addr, family);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800938 if (key) {
939 /* Pre-existing entry - just update that one. */
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000940 memcpy(key->key, newkey, newkeylen);
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700941 key->keylen = newkeylen;
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000942 return 0;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800943 }
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000944
Eric Dumazeta8afca02012-01-31 18:45:40 +0000945 md5sig = rcu_dereference_protected(tp->md5sig_info,
Hannes Frederic Sowa1e1d04e2016-04-05 17:10:15 +0200946 lockdep_sock_is_held(sk));
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000947 if (!md5sig) {
948 md5sig = kmalloc(sizeof(*md5sig), gfp);
949 if (!md5sig)
950 return -ENOMEM;
951
952 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
953 INIT_HLIST_HEAD(&md5sig->head);
Eric Dumazeta8afca02012-01-31 18:45:40 +0000954 rcu_assign_pointer(tp->md5sig_info, md5sig);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000955 }
956
Eric Dumazet5f3d9cb2012-01-31 10:56:48 +0000957 key = sock_kmalloc(sk, sizeof(*key), gfp);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000958 if (!key)
959 return -ENOMEM;
Eric Dumazet71cea172013-05-20 06:52:26 +0000960 if (!tcp_alloc_md5sig_pool()) {
Eric Dumazet5f3d9cb2012-01-31 10:56:48 +0000961 sock_kfree_s(sk, key, sizeof(*key));
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000962 return -ENOMEM;
963 }
964
965 memcpy(key->key, newkey, newkeylen);
966 key->keylen = newkeylen;
967 key->family = family;
968 memcpy(&key->addr, addr,
969 (family == AF_INET6) ? sizeof(struct in6_addr) :
970 sizeof(struct in_addr));
971 hlist_add_head_rcu(&key->node, &md5sig->head);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800972 return 0;
973}
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000974EXPORT_SYMBOL(tcp_md5_do_add);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800975
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000976int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int family)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800977{
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000978 struct tcp_md5sig_key *key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800979
Aydin Arikc0353c72013-06-14 18:56:31 +1200980 key = tcp_md5_do_lookup(sk, addr, family);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000981 if (!key)
982 return -ENOENT;
983 hlist_del_rcu(&key->node);
Eric Dumazet5f3d9cb2012-01-31 10:56:48 +0000984 atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000985 kfree_rcu(key, rcu);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000986 return 0;
987}
988EXPORT_SYMBOL(tcp_md5_do_del);
989
stephen hemmingere0683e702012-10-26 14:31:40 +0000990static void tcp_clear_md5_list(struct sock *sk)
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000991{
992 struct tcp_sock *tp = tcp_sk(sk);
993 struct tcp_md5sig_key *key;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800994 struct hlist_node *n;
Eric Dumazeta8afca02012-01-31 18:45:40 +0000995 struct tcp_md5sig_info *md5sig;
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000996
Eric Dumazeta8afca02012-01-31 18:45:40 +0000997 md5sig = rcu_dereference_protected(tp->md5sig_info, 1);
998
Sasha Levinb67bfe02013-02-27 17:06:00 -0800999 hlist_for_each_entry_safe(key, n, &md5sig->head, node) {
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001000 hlist_del_rcu(&key->node);
Eric Dumazet5f3d9cb2012-01-31 10:56:48 +00001001 atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001002 kfree_rcu(key, rcu);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001003 }
1004}
1005
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001006static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
1007 int optlen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001008{
1009 struct tcp_md5sig cmd;
1010 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001011
1012 if (optlen < sizeof(cmd))
1013 return -EINVAL;
1014
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001015 if (copy_from_user(&cmd, optval, sizeof(cmd)))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001016 return -EFAULT;
1017
1018 if (sin->sin_family != AF_INET)
1019 return -EINVAL;
1020
Dmitry Popov64a124e2014-08-03 22:45:19 +04001021 if (!cmd.tcpm_keylen)
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001022 return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
1023 AF_INET);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001024
1025 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
1026 return -EINVAL;
1027
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001028 return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
1029 AF_INET, cmd.tcpm_key, cmd.tcpm_keylen,
1030 GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001031}
1032
Eric Dumazet19689e32016-06-27 18:51:53 +02001033static int tcp_v4_md5_hash_headers(struct tcp_md5sig_pool *hp,
1034 __be32 daddr, __be32 saddr,
1035 const struct tcphdr *th, int nbytes)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001036{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001037 struct tcp4_pseudohdr *bp;
Adam Langley49a72df2008-07-19 00:01:42 -07001038 struct scatterlist sg;
Eric Dumazet19689e32016-06-27 18:51:53 +02001039 struct tcphdr *_th;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001040
Eric Dumazet19689e32016-06-27 18:51:53 +02001041 bp = hp->scratch;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001042 bp->saddr = saddr;
1043 bp->daddr = daddr;
1044 bp->pad = 0;
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001045 bp->protocol = IPPROTO_TCP;
Adam Langley49a72df2008-07-19 00:01:42 -07001046 bp->len = cpu_to_be16(nbytes);
David S. Millerc7da57a2007-10-26 00:41:21 -07001047
Eric Dumazet19689e32016-06-27 18:51:53 +02001048 _th = (struct tcphdr *)(bp + 1);
1049 memcpy(_th, th, sizeof(*th));
1050 _th->check = 0;
1051
1052 sg_init_one(&sg, bp, sizeof(*bp) + sizeof(*th));
1053 ahash_request_set_crypt(hp->md5_req, &sg, NULL,
1054 sizeof(*bp) + sizeof(*th));
Herbert Xucf80e0e2016-01-24 21:20:23 +08001055 return crypto_ahash_update(hp->md5_req);
Adam Langley49a72df2008-07-19 00:01:42 -07001056}
1057
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001058static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
Eric Dumazet318cf7a2011-10-24 02:46:04 -04001059 __be32 daddr, __be32 saddr, const struct tcphdr *th)
Adam Langley49a72df2008-07-19 00:01:42 -07001060{
1061 struct tcp_md5sig_pool *hp;
Herbert Xucf80e0e2016-01-24 21:20:23 +08001062 struct ahash_request *req;
Adam Langley49a72df2008-07-19 00:01:42 -07001063
1064 hp = tcp_get_md5sig_pool();
1065 if (!hp)
1066 goto clear_hash_noput;
Herbert Xucf80e0e2016-01-24 21:20:23 +08001067 req = hp->md5_req;
Adam Langley49a72df2008-07-19 00:01:42 -07001068
Herbert Xucf80e0e2016-01-24 21:20:23 +08001069 if (crypto_ahash_init(req))
Adam Langley49a72df2008-07-19 00:01:42 -07001070 goto clear_hash;
Eric Dumazet19689e32016-06-27 18:51:53 +02001071 if (tcp_v4_md5_hash_headers(hp, daddr, saddr, th, th->doff << 2))
Adam Langley49a72df2008-07-19 00:01:42 -07001072 goto clear_hash;
1073 if (tcp_md5_hash_key(hp, key))
1074 goto clear_hash;
Herbert Xucf80e0e2016-01-24 21:20:23 +08001075 ahash_request_set_crypt(req, NULL, md5_hash, 0);
1076 if (crypto_ahash_final(req))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001077 goto clear_hash;
1078
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001079 tcp_put_md5sig_pool();
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001080 return 0;
Adam Langley49a72df2008-07-19 00:01:42 -07001081
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001082clear_hash:
1083 tcp_put_md5sig_pool();
1084clear_hash_noput:
1085 memset(md5_hash, 0, 16);
Adam Langley49a72df2008-07-19 00:01:42 -07001086 return 1;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001087}
1088
Eric Dumazet39f8e582015-03-24 15:58:55 -07001089int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
1090 const struct sock *sk,
Eric Dumazet318cf7a2011-10-24 02:46:04 -04001091 const struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001092{
Adam Langley49a72df2008-07-19 00:01:42 -07001093 struct tcp_md5sig_pool *hp;
Herbert Xucf80e0e2016-01-24 21:20:23 +08001094 struct ahash_request *req;
Eric Dumazet318cf7a2011-10-24 02:46:04 -04001095 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001096 __be32 saddr, daddr;
1097
Eric Dumazet39f8e582015-03-24 15:58:55 -07001098 if (sk) { /* valid for establish/request sockets */
1099 saddr = sk->sk_rcv_saddr;
1100 daddr = sk->sk_daddr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001101 } else {
Adam Langley49a72df2008-07-19 00:01:42 -07001102 const struct iphdr *iph = ip_hdr(skb);
1103 saddr = iph->saddr;
1104 daddr = iph->daddr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001105 }
Adam Langley49a72df2008-07-19 00:01:42 -07001106
1107 hp = tcp_get_md5sig_pool();
1108 if (!hp)
1109 goto clear_hash_noput;
Herbert Xucf80e0e2016-01-24 21:20:23 +08001110 req = hp->md5_req;
Adam Langley49a72df2008-07-19 00:01:42 -07001111
Herbert Xucf80e0e2016-01-24 21:20:23 +08001112 if (crypto_ahash_init(req))
Adam Langley49a72df2008-07-19 00:01:42 -07001113 goto clear_hash;
1114
Eric Dumazet19689e32016-06-27 18:51:53 +02001115 if (tcp_v4_md5_hash_headers(hp, daddr, saddr, th, skb->len))
Adam Langley49a72df2008-07-19 00:01:42 -07001116 goto clear_hash;
1117 if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
1118 goto clear_hash;
1119 if (tcp_md5_hash_key(hp, key))
1120 goto clear_hash;
Herbert Xucf80e0e2016-01-24 21:20:23 +08001121 ahash_request_set_crypt(req, NULL, md5_hash, 0);
1122 if (crypto_ahash_final(req))
Adam Langley49a72df2008-07-19 00:01:42 -07001123 goto clear_hash;
1124
1125 tcp_put_md5sig_pool();
1126 return 0;
1127
1128clear_hash:
1129 tcp_put_md5sig_pool();
1130clear_hash_noput:
1131 memset(md5_hash, 0, 16);
1132 return 1;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001133}
Adam Langley49a72df2008-07-19 00:01:42 -07001134EXPORT_SYMBOL(tcp_v4_md5_hash_skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001135
Eric Dumazetba8e2752015-10-02 11:43:28 -07001136#endif
1137
Eric Dumazetff74e232015-03-24 15:58:54 -07001138/* Called with rcu_read_lock() */
Eric Dumazetba8e2752015-10-02 11:43:28 -07001139static bool tcp_v4_inbound_md5_hash(const struct sock *sk,
Eric Dumazetff74e232015-03-24 15:58:54 -07001140 const struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001141{
Eric Dumazetba8e2752015-10-02 11:43:28 -07001142#ifdef CONFIG_TCP_MD5SIG
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001143 /*
1144 * This gets called for each TCP segment that arrives
1145 * so we want to be efficient.
1146 * We have 3 drop cases:
1147 * o No MD5 hash and one expected.
1148 * o MD5 hash and we're not expecting one.
1149 * o MD5 hash and its wrong.
1150 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001151 const __u8 *hash_location = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001152 struct tcp_md5sig_key *hash_expected;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001153 const struct iphdr *iph = ip_hdr(skb);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001154 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001155 int genhash;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001156 unsigned char newhash[16];
1157
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001158 hash_expected = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&iph->saddr,
1159 AF_INET);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09001160 hash_location = tcp_parse_md5sig_option(th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001161
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001162 /* We've parsed the options - do we have a hash? */
1163 if (!hash_expected && !hash_location)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001164 return false;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001165
1166 if (hash_expected && !hash_location) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07001167 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001168 return true;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001169 }
1170
1171 if (!hash_expected && hash_location) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07001172 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001173 return true;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001174 }
1175
1176 /* Okay, so this is hash_expected and hash_location -
1177 * so we need to calculate the checksum.
1178 */
Adam Langley49a72df2008-07-19 00:01:42 -07001179 genhash = tcp_v4_md5_hash_skb(newhash,
1180 hash_expected,
Eric Dumazet39f8e582015-03-24 15:58:55 -07001181 NULL, skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001182
1183 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
Eric Dumazet72145a62016-08-24 09:01:23 -07001184 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE);
Joe Perchese87cc472012-05-13 21:56:26 +00001185 net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n",
1186 &iph->saddr, ntohs(th->source),
1187 &iph->daddr, ntohs(th->dest),
1188 genhash ? " tcp_v4_calc_md5_hash failed"
1189 : "");
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001190 return true;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001191 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001192 return false;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001193#endif
Eric Dumazetba8e2752015-10-02 11:43:28 -07001194 return false;
1195}
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001196
Eric Dumazetb40cf182015-09-25 07:39:08 -07001197static void tcp_v4_init_req(struct request_sock *req,
1198 const struct sock *sk_listener,
Octavian Purdila16bea702014-06-25 17:09:53 +03001199 struct sk_buff *skb)
1200{
1201 struct inet_request_sock *ireq = inet_rsk(req);
1202
Eric Dumazet08d2cc3b2015-03-18 14:05:38 -07001203 sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
1204 sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
Eric Dumazet2ffd2612017-10-20 09:04:13 -07001205 RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(skb));
Octavian Purdila16bea702014-06-25 17:09:53 +03001206}
1207
Eric Dumazetf9646292015-09-29 07:42:50 -07001208static struct dst_entry *tcp_v4_route_req(const struct sock *sk,
1209 struct flowi *fl,
Octavian Purdilad94e0412014-06-25 17:09:55 +03001210 const struct request_sock *req,
1211 bool *strict)
1212{
1213 struct dst_entry *dst = inet_csk_route_req(sk, &fl->u.ip4, req);
1214
1215 if (strict) {
1216 if (fl->u.ip4.daddr == inet_rsk(req)->ir_rmt_addr)
1217 *strict = true;
1218 else
1219 *strict = false;
1220 }
1221
1222 return dst;
1223}
1224
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001225struct request_sock_ops tcp_request_sock_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .family = PF_INET,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001227 .obj_size = sizeof(struct tcp_request_sock),
Octavian Purdila5db92c92014-06-25 17:09:59 +03001228 .rtx_syn_ack = tcp_rtx_synack,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001229 .send_ack = tcp_v4_reqsk_send_ack,
1230 .destructor = tcp_v4_reqsk_destructor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 .send_reset = tcp_v4_send_reset,
stephen hemminger688d1942014-08-29 23:32:05 -07001232 .syn_ack_timeout = tcp_syn_ack_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233};
1234
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001235static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
Octavian Purdila2aec4a22014-06-25 17:10:00 +03001236 .mss_clamp = TCP_MSS_DEFAULT,
Octavian Purdila16bea702014-06-25 17:09:53 +03001237#ifdef CONFIG_TCP_MD5SIG
Eric Dumazetfd3a1542015-03-24 15:58:56 -07001238 .req_md5_lookup = tcp_v4_md5_lookup,
John Dykstrae3afe7b2009-07-16 05:04:51 +00001239 .calc_md5_hash = tcp_v4_md5_hash_skb,
Andrew Mortonb6332e62006-11-30 19:16:28 -08001240#endif
Octavian Purdila16bea702014-06-25 17:09:53 +03001241 .init_req = tcp_v4_init_req,
Octavian Purdilafb7b37a2014-06-25 17:09:54 +03001242#ifdef CONFIG_SYN_COOKIES
1243 .cookie_init_seq = cookie_v4_init_sequence,
1244#endif
Octavian Purdilad94e0412014-06-25 17:09:55 +03001245 .route_req = tcp_v4_route_req,
Octavian Purdila936b8bd2014-06-25 17:09:57 +03001246 .init_seq = tcp_v4_init_sequence,
Octavian Purdilad6274bd2014-06-25 17:09:58 +03001247 .send_synack = tcp_v4_send_synack,
Octavian Purdila16bea702014-06-25 17:09:53 +03001248};
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
1251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /* Never answer to SYNs send to broadcast or multicast */
Eric Dumazet511c3f92009-06-02 05:14:27 +00001253 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto drop;
1255
Octavian Purdila1fb6f152014-06-25 17:10:02 +03001256 return tcp_conn_request(&tcp_request_sock_ops,
1257 &tcp_request_sock_ipv4_ops, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259drop:
Eric Dumazet9caad862016-04-01 08:52:20 -07001260 tcp_listendrop(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001263EXPORT_SYMBOL(tcp_v4_conn_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265
1266/*
1267 * The three way handshake has completed - we got a valid synack -
1268 * now create the new socket.
1269 */
Eric Dumazet0c271712015-09-29 07:42:48 -07001270struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001271 struct request_sock *req,
Eric Dumazet5e0724d2015-10-22 08:20:46 -07001272 struct dst_entry *dst,
1273 struct request_sock *req_unhash,
1274 bool *own_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001276 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct inet_sock *newinet;
1278 struct tcp_sock *newtp;
1279 struct sock *newsk;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001280#ifdef CONFIG_TCP_MD5SIG
1281 struct tcp_md5sig_key *key;
1282#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001283 struct ip_options_rcu *inet_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (sk_acceptq_is_full(sk))
1286 goto exit_overflow;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 newsk = tcp_create_openreq_child(sk, req, skb);
1289 if (!newsk)
Balazs Scheidler093d2822010-10-21 13:06:43 +02001290 goto exit_nonewsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Herbert Xubcd76112006-06-30 13:36:35 -07001292 newsk->sk_gso_type = SKB_GSO_TCPV4;
Neal Cardwellfae6ef82012-08-19 03:30:38 +00001293 inet_sk_rx_dst_set(newsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 newtp = tcp_sk(newsk);
1296 newinet = inet_sk(newsk);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001297 ireq = inet_rsk(req);
Eric Dumazetd1e559d2015-03-18 14:05:35 -07001298 sk_daddr_set(newsk, ireq->ir_rmt_addr);
1299 sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
David Ahern6dd9a142015-12-16 13:20:44 -08001300 newsk->sk_bound_dev_if = ireq->ir_iif;
Eric Dumazet2ffd2612017-10-20 09:04:13 -07001301 newinet->inet_saddr = ireq->ir_loc_addr;
1302 inet_opt = rcu_dereference(ireq->ireq_opt);
1303 RCU_INIT_POINTER(newinet->inet_opt, inet_opt);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001304 newinet->mc_index = inet_iif(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001305 newinet->mc_ttl = ip_hdr(skb)->ttl;
Jiri Benc4c507d22012-02-09 09:35:49 +00001306 newinet->rcv_tos = ip_hdr(skb)->tos;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001307 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001308 if (inet_opt)
1309 inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
Eric Dumazeta67a32d2019-11-01 10:32:19 -07001310 newinet->inet_id = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Eric Dumazetdfd25ff2012-03-10 09:20:21 +00001312 if (!dst) {
1313 dst = inet_csk_route_child_sock(sk, newsk, req);
1314 if (!dst)
1315 goto put_and_exit;
1316 } else {
1317 /* syncookie case : see end of cookie_v4_check() */
1318 }
David S. Miller0e734412011-05-08 15:28:03 -07001319 sk_setup_caps(newsk, dst);
1320
Daniel Borkmann81164412015-01-05 23:57:48 +01001321 tcp_ca_openreq_child(newsk, dst);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 tcp_sync_mss(newsk, dst_mtu(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08001324 newtp->advmss = dst_metric_advmss(dst);
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07001325 if (tcp_sk(sk)->rx_opt.user_mss &&
1326 tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1327 newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 tcp_initialize_rcv_mss(newsk);
1330
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001331#ifdef CONFIG_TCP_MD5SIG
1332 /* Copy over the MD5 key from the original socket */
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001333 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr,
1334 AF_INET);
Ian Morris00db4122015-04-03 09:17:27 +01001335 if (key) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001336 /*
1337 * We're using one, so create a matching key
1338 * on the newsk structure. If we fail to get
1339 * memory, then we end up not copying the key
1340 * across. Shucks.
1341 */
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001342 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newinet->inet_daddr,
1343 AF_INET, key->key, key->keylen, GFP_ATOMIC);
Eric Dumazeta4654192010-05-16 00:36:33 -07001344 sk_nocaps_add(newsk, NETIF_F_GSO_MASK);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001345 }
1346#endif
1347
David S. Miller0e734412011-05-08 15:28:03 -07001348 if (__inet_inherit_port(sk, newsk) < 0)
1349 goto put_and_exit;
Eric Dumazet5e0724d2015-10-22 08:20:46 -07001350 *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
Eric Dumazet2ffd2612017-10-20 09:04:13 -07001351 if (likely(*own_req)) {
Eric Dumazet49a496c2015-11-05 12:50:19 -08001352 tcp_move_syn(newtp, req);
Eric Dumazet2ffd2612017-10-20 09:04:13 -07001353 ireq->ireq_opt = NULL;
1354 } else {
1355 newinet->inet_opt = NULL;
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return newsk;
1358
1359exit_overflow:
Eric Dumazetc10d9312016-04-29 14:16:47 -07001360 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
Balazs Scheidler093d2822010-10-21 13:06:43 +02001361exit_nonewsk:
1362 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363exit:
Eric Dumazet9caad862016-04-01 08:52:20 -07001364 tcp_listendrop(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return NULL;
David S. Miller0e734412011-05-08 15:28:03 -07001366put_and_exit:
Eric Dumazet2ffd2612017-10-20 09:04:13 -07001367 newinet->inet_opt = NULL;
Christoph Paasche337e242012-12-14 04:07:58 +00001368 inet_csk_prepare_forced_close(newsk);
1369 tcp_done(newsk);
David S. Miller0e734412011-05-08 15:28:03 -07001370 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001372EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Eric Dumazet079096f2015-10-02 11:43:32 -07001374static struct sock *tcp_v4_cookie_check(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376#ifdef CONFIG_SYN_COOKIES
Eric Dumazet079096f2015-10-02 11:43:32 -07001377 const struct tcphdr *th = tcp_hdr(skb);
1378
Florian Westphalaf9b4732010-06-03 00:43:44 +00001379 if (!th->syn)
Cong Wang461b74c2014-10-15 14:33:22 -07001380 sk = cookie_v4_check(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381#endif
1382 return sk;
1383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/* The socket must have it's spinlock held when we get
Eric Dumazete994b2f2015-10-02 11:43:39 -07001386 * here, unless it is a TCP_LISTEN socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 *
1388 * We have a potential double-lock case here, so even when
1389 * doing backlog processing we use the BH locking scheme.
1390 * This is because we cannot sleep with the original spinlock
1391 * held.
1392 */
1393int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
1394{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001395 struct sock *rsk;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
Eric Dumazet404e0a82012-07-29 23:20:37 +00001398 struct dst_entry *dst = sk->sk_rx_dst;
1399
Tom Herbertbdeab992011-08-14 19:45:55 +00001400 sock_rps_save_rxhash(sk, skb);
Eric Dumazet3d973792014-11-11 05:54:27 -08001401 sk_mark_napi_id(sk, skb);
Eric Dumazet404e0a82012-07-29 23:20:37 +00001402 if (dst) {
Eric Dumazet505fbcf2012-07-27 06:23:40 +00001403 if (inet_sk(sk)->rx_dst_ifindex != skb->skb_iif ||
Ian Morris51456b22015-04-03 09:17:26 +01001404 !dst->ops->check(dst, 0)) {
David S. Miller92101b32012-07-23 16:29:00 -07001405 dst_release(dst);
1406 sk->sk_rx_dst = NULL;
1407 }
1408 }
Vijay Subramanianc995ae22013-09-03 12:23:22 -07001409 tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return 0;
1411 }
1412
Eric Dumazet12e25e12015-06-03 23:49:21 -07001413 if (tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 goto csum_err;
1415
1416 if (sk->sk_state == TCP_LISTEN) {
Eric Dumazet079096f2015-10-02 11:43:32 -07001417 struct sock *nsk = tcp_v4_cookie_check(sk, skb);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!nsk)
1420 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (nsk != sk) {
Tom Herbertbdeab992011-08-14 19:45:55 +00001422 sock_rps_save_rxhash(nsk, skb);
Eric Dumazet38cb5242015-10-02 11:43:26 -07001423 sk_mark_napi_id(nsk, skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001424 if (tcp_child_process(sk, nsk, skb)) {
1425 rsk = nsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
1429 }
Eric Dumazetca551582010-06-03 09:03:58 +00001430 } else
Tom Herbertbdeab992011-08-14 19:45:55 +00001431 sock_rps_save_rxhash(sk, skb);
Eric Dumazetca551582010-06-03 09:03:58 +00001432
Eric Dumazet72ab4a82015-09-29 07:42:41 -07001433 if (tcp_rcv_state_process(sk, skb)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001434 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return 0;
1438
1439reset:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001440 tcp_v4_send_reset(rsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441discard:
1442 kfree_skb(skb);
1443 /* Be careful here. If this function gets more complicated and
1444 * gcc suffers from register pressure on the x86, sk (in %ebx)
1445 * might be destroyed here. This current version compiles correctly,
1446 * but you have been warned.
1447 */
1448 return 0;
1449
1450csum_err:
Eric Dumazetc10d9312016-04-29 14:16:47 -07001451 TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
1452 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 goto discard;
1454}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001455EXPORT_SYMBOL(tcp_v4_do_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
David S. Miller160eb5a2012-06-27 22:01:22 -07001457void tcp_v4_early_demux(struct sk_buff *skb)
David S. Miller41063e92012-06-19 21:22:05 -07001458{
David S. Miller41063e92012-06-19 21:22:05 -07001459 const struct iphdr *iph;
1460 const struct tcphdr *th;
1461 struct sock *sk;
David S. Miller41063e92012-06-19 21:22:05 -07001462
David S. Miller41063e92012-06-19 21:22:05 -07001463 if (skb->pkt_type != PACKET_HOST)
David S. Miller160eb5a2012-06-27 22:01:22 -07001464 return;
David S. Miller41063e92012-06-19 21:22:05 -07001465
Eric Dumazet45f00f92012-10-22 21:42:47 +00001466 if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr)))
David S. Miller160eb5a2012-06-27 22:01:22 -07001467 return;
David S. Miller41063e92012-06-19 21:22:05 -07001468
1469 iph = ip_hdr(skb);
Eric Dumazet45f00f92012-10-22 21:42:47 +00001470 th = tcp_hdr(skb);
David S. Miller41063e92012-06-19 21:22:05 -07001471
1472 if (th->doff < sizeof(struct tcphdr) / 4)
David S. Miller160eb5a2012-06-27 22:01:22 -07001473 return;
David S. Miller41063e92012-06-19 21:22:05 -07001474
Eric Dumazet45f00f92012-10-22 21:42:47 +00001475 sk = __inet_lookup_established(dev_net(skb->dev), &tcp_hashinfo,
David S. Miller41063e92012-06-19 21:22:05 -07001476 iph->saddr, th->source,
Vijay Subramanian7011d082012-06-23 17:38:10 +00001477 iph->daddr, ntohs(th->dest),
Eric Dumazet9cb429d2012-07-24 01:19:31 +00001478 skb->skb_iif);
David S. Miller41063e92012-06-19 21:22:05 -07001479 if (sk) {
1480 skb->sk = sk;
1481 skb->destructor = sock_edemux;
Eric Dumazetf7e4eb02015-03-15 21:12:13 -07001482 if (sk_fullsock(sk)) {
Michal Kubečekd0c294c2015-03-23 15:14:00 +01001483 struct dst_entry *dst = READ_ONCE(sk->sk_rx_dst);
Eric Dumazet505fbcf2012-07-27 06:23:40 +00001484
David S. Miller41063e92012-06-19 21:22:05 -07001485 if (dst)
1486 dst = dst_check(dst, 0);
David S. Miller92101b32012-07-23 16:29:00 -07001487 if (dst &&
Eric Dumazet505fbcf2012-07-27 06:23:40 +00001488 inet_sk(sk)->rx_dst_ifindex == skb->skb_iif)
David S. Miller92101b32012-07-23 16:29:00 -07001489 skb_dst_set_noref(skb, dst);
David S. Miller41063e92012-06-19 21:22:05 -07001490 }
1491 }
David S. Miller41063e92012-06-19 21:22:05 -07001492}
1493
Eric Dumazetb2fb4f52013-03-06 12:58:01 +00001494/* Packet is added to VJ-style prequeue for processing in process
1495 * context, if a reader task is waiting. Apparently, this exciting
1496 * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
1497 * failed somewhere. Latency? Burstiness? Well, at least now we will
1498 * see, why it failed. 8)8) --ANK
1499 *
1500 */
1501bool tcp_prequeue(struct sock *sk, struct sk_buff *skb)
1502{
1503 struct tcp_sock *tp = tcp_sk(sk);
1504
1505 if (sysctl_tcp_low_latency || !tp->ucopy.task)
1506 return false;
1507
1508 if (skb->len <= tcp_hdrlen(skb) &&
1509 skb_queue_len(&tp->ucopy.prequeue) == 0)
1510 return false;
1511
Eric Dumazetca777ef2014-09-08 08:06:07 -07001512 /* Before escaping RCU protected region, we need to take care of skb
1513 * dst. Prequeue is only enabled for established sockets.
1514 * For such sockets, we might need the skb dst only to set sk->sk_rx_dst
1515 * Instead of doing full sk_rx_dst validity here, let's perform
1516 * an optimistic check.
1517 */
1518 if (likely(sk->sk_rx_dst))
1519 skb_dst_drop(skb);
1520 else
Eric Dumazet5037e9e2015-12-14 14:08:53 -08001521 skb_dst_force_safe(skb);
Eric Dumazetca777ef2014-09-08 08:06:07 -07001522
Eric Dumazetb2fb4f52013-03-06 12:58:01 +00001523 __skb_queue_tail(&tp->ucopy.prequeue, skb);
1524 tp->ucopy.memory += skb->truesize;
Eric Dumazet0cef6a42016-04-27 10:12:25 -07001525 if (skb_queue_len(&tp->ucopy.prequeue) >= 32 ||
1526 tp->ucopy.memory + atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) {
Eric Dumazetb2fb4f52013-03-06 12:58:01 +00001527 struct sk_buff *skb1;
1528
1529 BUG_ON(sock_owned_by_user(sk));
Eric Dumazet0cef6a42016-04-27 10:12:25 -07001530 __NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPPREQUEUEDROPPED,
1531 skb_queue_len(&tp->ucopy.prequeue));
Eric Dumazetb2fb4f52013-03-06 12:58:01 +00001532
Eric Dumazet0cef6a42016-04-27 10:12:25 -07001533 while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL)
Eric Dumazetb2fb4f52013-03-06 12:58:01 +00001534 sk_backlog_rcv(sk, skb1);
Eric Dumazetb2fb4f52013-03-06 12:58:01 +00001535
1536 tp->ucopy.memory = 0;
1537 } else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
1538 wake_up_interruptible_sync_poll(sk_sleep(sk),
1539 POLLIN | POLLRDNORM | POLLRDBAND);
1540 if (!inet_csk_ack_scheduled(sk))
1541 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
1542 (3 * tcp_rto_min(sk)) / 4,
1543 TCP_RTO_MAX);
1544 }
1545 return true;
1546}
1547EXPORT_SYMBOL(tcp_prequeue);
1548
Eric Dumazetc9c33212016-08-27 07:37:54 -07001549bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
1550{
1551 u32 limit = sk->sk_rcvbuf + sk->sk_sndbuf;
1552
1553 /* Only socket owner can try to collapse/prune rx queues
1554 * to reduce memory overhead, so add a little headroom here.
1555 * Few sockets backlog are possibly concurrently non empty.
1556 */
1557 limit += 64*1024;
1558
1559 /* In case all data was pulled from skb frags (in __pskb_pull_tail()),
1560 * we can fix skb->truesize to its real value to avoid future drops.
1561 * This is valid because skb is not yet charged to the socket.
1562 * It has been noticed pure SACK packets were sometimes dropped
1563 * (if cooked by drivers without copybreak feature).
1564 */
1565 if (!skb->data_len)
1566 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
1567
1568 if (unlikely(sk_add_backlog(sk, skb, limit))) {
1569 bh_unlock_sock(sk);
1570 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPBACKLOGDROP);
1571 return true;
1572 }
1573 return false;
1574}
1575EXPORT_SYMBOL(tcp_add_backlog);
1576
Eric Dumazetac6e7802016-11-10 13:12:35 -08001577int tcp_filter(struct sock *sk, struct sk_buff *skb)
1578{
1579 struct tcphdr *th = (struct tcphdr *)skb->data;
1580 unsigned int eaten = skb->len;
1581 int err;
1582
1583 err = sk_filter_trim_cap(sk, skb, th->doff * 4);
1584 if (!err) {
1585 eaten -= skb->len;
1586 TCP_SKB_CB(skb)->end_seq -= eaten;
1587 }
1588 return err;
1589}
1590EXPORT_SYMBOL(tcp_filter);
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592/*
1593 * From tcp_input.c
1594 */
1595
1596int tcp_v4_rcv(struct sk_buff *skb)
1597{
Eric Dumazet3b24d852016-04-01 08:52:17 -07001598 struct net *net = dev_net(skb->dev);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001599 const struct iphdr *iph;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001600 const struct tcphdr *th;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001601 bool refcounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 struct sock *sk;
1603 int ret;
1604
1605 if (skb->pkt_type != PACKET_HOST)
1606 goto discard_it;
1607
1608 /* Count it even if it's bad */
Eric Dumazet90bbcc62016-04-27 16:44:32 -07001609 __TCP_INC_STATS(net, TCP_MIB_INSEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1612 goto discard_it;
1613
Eric Dumazetea1627c2016-05-13 09:16:40 -07001614 th = (const struct tcphdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Eric Dumazetea1627c2016-05-13 09:16:40 -07001616 if (unlikely(th->doff < sizeof(struct tcphdr) / 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto bad_packet;
1618 if (!pskb_may_pull(skb, th->doff * 4))
1619 goto discard_it;
1620
1621 /* An explanation is required here, I think.
1622 * Packet length and doff are validated by header prediction,
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001623 * provided case of th->doff==0 is eliminated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * So, we defer the checks. */
Tom Herberted70fcf2014-05-02 16:29:38 -07001625
1626 if (skb_checksum_init(skb, IPPROTO_TCP, inet_compute_pseudo))
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001627 goto csum_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Eric Dumazetea1627c2016-05-13 09:16:40 -07001629 th = (const struct tcphdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001630 iph = ip_hdr(skb);
Eric Dumazet971f10e2014-09-27 09:50:57 -07001631 /* This is tricky : We move IPCB at its correct location into TCP_SKB_CB()
1632 * barrier() makes sure compiler wont play fool^Waliasing games.
1633 */
1634 memmove(&TCP_SKB_CB(skb)->header.h4, IPCB(skb),
1635 sizeof(struct inet_skb_parm));
1636 barrier();
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1639 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1640 skb->len - th->doff * 4);
1641 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
Eric Dumazete11ecdd2014-09-15 04:19:51 -07001642 TCP_SKB_CB(skb)->tcp_flags = tcp_flag_byte(th);
Eric Dumazet04317da2014-09-05 15:33:32 -07001643 TCP_SKB_CB(skb)->tcp_tw_isn = 0;
Eric Dumazetb82d1bb2011-09-27 02:20:08 -04001644 TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 TCP_SKB_CB(skb)->sacked = 0;
1646
Eric Dumazet4bdc3d62015-10-13 17:12:54 -07001647lookup:
Craig Galleka5836362016-02-10 11:50:38 -05001648 sk = __inet_lookup_skb(&tcp_hashinfo, skb, __tcp_hdrlen(th), th->source,
Eric Dumazet3b24d852016-04-01 08:52:17 -07001649 th->dest, &refcounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (!sk)
1651 goto no_tcp_socket;
1652
Eric Dumazetbb134d52010-03-09 05:55:56 +00001653process:
1654 if (sk->sk_state == TCP_TIME_WAIT)
1655 goto do_time_wait;
1656
Eric Dumazet079096f2015-10-02 11:43:32 -07001657 if (sk->sk_state == TCP_NEW_SYN_RECV) {
1658 struct request_sock *req = inet_reqsk(sk);
Eric Dumazet77166822016-02-18 05:39:18 -08001659 struct sock *nsk;
Eric Dumazet079096f2015-10-02 11:43:32 -07001660
1661 sk = req->rsk_listener;
Eric Dumazet72923552016-02-11 22:50:29 -08001662 if (unlikely(tcp_v4_inbound_md5_hash(sk, skb))) {
Eric Dumazete65c3322016-08-24 08:50:24 -07001663 sk_drops_add(sk, skb);
Eric Dumazet72923552016-02-11 22:50:29 -08001664 reqsk_put(req);
1665 goto discard_it;
1666 }
Frank van der Linden6caca342018-06-12 23:09:37 +00001667 if (tcp_checksum_complete(skb)) {
1668 reqsk_put(req);
1669 goto csum_error;
1670 }
Eric Dumazet77166822016-02-18 05:39:18 -08001671 if (unlikely(sk->sk_state != TCP_LISTEN)) {
Eric Dumazetf03f2e12015-10-14 11:16:27 -07001672 inet_csk_reqsk_queue_drop_and_put(sk, req);
Eric Dumazet4bdc3d62015-10-13 17:12:54 -07001673 goto lookup;
1674 }
Eric Dumazet3b24d852016-04-01 08:52:17 -07001675 /* We own a reference on the listener, increase it again
1676 * as we might lose it too soon.
1677 */
Eric Dumazet77166822016-02-18 05:39:18 -08001678 sock_hold(sk);
Eric Dumazet3b24d852016-04-01 08:52:17 -07001679 refcounted = true;
Eric Dumazet77166822016-02-18 05:39:18 -08001680 nsk = tcp_check_req(sk, skb, req, false);
Eric Dumazet079096f2015-10-02 11:43:32 -07001681 if (!nsk) {
1682 reqsk_put(req);
Eric Dumazet77166822016-02-18 05:39:18 -08001683 goto discard_and_relse;
Eric Dumazet079096f2015-10-02 11:43:32 -07001684 }
1685 if (nsk == sk) {
Eric Dumazet079096f2015-10-02 11:43:32 -07001686 reqsk_put(req);
1687 } else if (tcp_child_process(sk, nsk, skb)) {
1688 tcp_v4_send_reset(nsk, skb);
Eric Dumazet77166822016-02-18 05:39:18 -08001689 goto discard_and_relse;
Eric Dumazet079096f2015-10-02 11:43:32 -07001690 } else {
Eric Dumazet77166822016-02-18 05:39:18 -08001691 sock_put(sk);
Eric Dumazet079096f2015-10-02 11:43:32 -07001692 return 0;
1693 }
1694 }
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001695 if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07001696 __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001697 goto discard_and_relse;
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001698 }
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1701 goto discard_and_relse;
Dmitry Popov9ea88a12014-08-07 02:38:22 +04001702
Dmitry Popov9ea88a12014-08-07 02:38:22 +04001703 if (tcp_v4_inbound_md5_hash(sk, skb))
1704 goto discard_and_relse;
Dmitry Popov9ea88a12014-08-07 02:38:22 +04001705
Patrick McHardyb59c2702006-01-06 23:06:10 -08001706 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Eric Dumazetac6e7802016-11-10 13:12:35 -08001708 if (tcp_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 goto discard_and_relse;
Eric Dumazetac6e7802016-11-10 13:12:35 -08001710 th = (const struct tcphdr *)skb->data;
1711 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 skb->dev = NULL;
1714
Eric Dumazete994b2f2015-10-02 11:43:39 -07001715 if (sk->sk_state == TCP_LISTEN) {
1716 ret = tcp_v4_do_rcv(sk, skb);
1717 goto put_and_return;
1718 }
1719
1720 sk_incoming_cpu_update(sk);
1721
Ingo Molnarc6366182006-07-03 00:25:13 -07001722 bh_lock_sock_nested(sk);
Martin KaFai Laua44d6ea2016-03-14 10:52:15 -07001723 tcp_segs_in(tcp_sk(sk), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 ret = 0;
1725 if (!sock_owned_by_user(sk)) {
Dan Williams7bced392013-12-30 12:37:29 -08001726 if (!tcp_prequeue(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ret = tcp_v4_do_rcv(sk, skb);
Eric Dumazetc9c33212016-08-27 07:37:54 -07001728 } else if (tcp_add_backlog(sk, skb)) {
Zhu Yi6b03a532010-03-04 18:01:41 +00001729 goto discard_and_relse;
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 bh_unlock_sock(sk);
1732
Eric Dumazete994b2f2015-10-02 11:43:39 -07001733put_and_return:
Eric Dumazet3b24d852016-04-01 08:52:17 -07001734 if (refcounted)
1735 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 return ret;
1738
1739no_tcp_socket:
1740 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1741 goto discard_it;
1742
Eric Dumazet12e25e12015-06-03 23:49:21 -07001743 if (tcp_checksum_complete(skb)) {
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001744csum_error:
Eric Dumazet90bbcc62016-04-27 16:44:32 -07001745 __TCP_INC_STATS(net, TCP_MIB_CSUMERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746bad_packet:
Eric Dumazet90bbcc62016-04-27 16:44:32 -07001747 __TCP_INC_STATS(net, TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001749 tcp_v4_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751
1752discard_it:
1753 /* Discard frame. */
1754 kfree_skb(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757discard_and_relse:
Eric Dumazet532182c2016-04-01 08:52:19 -07001758 sk_drops_add(sk, skb);
Eric Dumazet3b24d852016-04-01 08:52:17 -07001759 if (refcounted)
1760 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto discard_it;
1762
1763do_time_wait:
1764 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001765 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto discard_it;
1767 }
1768
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001769 if (tcp_checksum_complete(skb)) {
1770 inet_twsk_put(inet_twsk(sk));
1771 goto csum_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001773 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 case TCP_TW_SYN: {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001775 struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
Craig Galleka5836362016-02-10 11:50:38 -05001776 &tcp_hashinfo, skb,
1777 __tcp_hdrlen(th),
Tom Herbertda5e3632013-01-22 09:50:24 +00001778 iph->saddr, th->source,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001779 iph->daddr, th->dest,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001780 inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (sk2) {
Eric Dumazetdbe7faa2015-07-08 14:28:30 -07001782 inet_twsk_deschedule_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 sk = sk2;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001784 refcounted = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto process;
1786 }
1787 /* Fall through to ACK */
1788 }
1789 case TCP_TW_ACK:
1790 tcp_v4_timewait_ack(sk, skb);
1791 break;
1792 case TCP_TW_RST:
Florian Westphal271c3b92015-12-21 21:29:26 +01001793 tcp_v4_send_reset(sk, skb);
1794 inet_twsk_deschedule_put(inet_twsk(sk));
1795 goto discard_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 case TCP_TW_SUCCESS:;
1797 }
1798 goto discard_it;
1799}
1800
David S. Millerccb7c412010-12-01 18:09:13 -08001801static struct timewait_sock_ops tcp_timewait_sock_ops = {
1802 .twsk_obj_size = sizeof(struct tcp_timewait_sock),
1803 .twsk_unique = tcp_twsk_unique,
1804 .twsk_destructor= tcp_twsk_destructor,
David S. Millerccb7c412010-12-01 18:09:13 -08001805};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Eric Dumazet63d02d12012-08-09 14:11:00 +00001807void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
Eric Dumazet5d299f32012-08-06 05:09:33 +00001808{
1809 struct dst_entry *dst = skb_dst(skb);
1810
Eric Dumazet5037e9e2015-12-14 14:08:53 -08001811 if (dst && dst_hold_safe(dst)) {
Eric Dumazetca777ef2014-09-08 08:06:07 -07001812 sk->sk_rx_dst = dst;
1813 inet_sk(sk)->rx_dst_ifindex = skb->skb_iif;
1814 }
Eric Dumazet5d299f32012-08-06 05:09:33 +00001815}
Eric Dumazet63d02d12012-08-09 14:11:00 +00001816EXPORT_SYMBOL(inet_sk_rx_dst_set);
Eric Dumazet5d299f32012-08-06 05:09:33 +00001817
Stephen Hemminger3b401a82009-09-01 19:25:04 +00001818const struct inet_connection_sock_af_ops ipv4_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001819 .queue_xmit = ip_queue_xmit,
1820 .send_check = tcp_v4_send_check,
1821 .rebuild_header = inet_sk_rebuild_header,
Eric Dumazet5d299f32012-08-06 05:09:33 +00001822 .sk_rx_dst_set = inet_sk_rx_dst_set,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001823 .conn_request = tcp_v4_conn_request,
1824 .syn_recv_sock = tcp_v4_syn_recv_sock,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001825 .net_header_len = sizeof(struct iphdr),
1826 .setsockopt = ip_setsockopt,
1827 .getsockopt = ip_getsockopt,
1828 .addr2sockaddr = inet_csk_addr2sockaddr,
1829 .sockaddr_len = sizeof(struct sockaddr_in),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001830 .bind_conflict = inet_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001831#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001832 .compat_setsockopt = compat_ip_setsockopt,
1833 .compat_getsockopt = compat_ip_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001834#endif
Neal Cardwell4fab9072014-08-14 12:40:05 -04001835 .mtu_reduced = tcp_v4_mtu_reduced,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836};
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001837EXPORT_SYMBOL(ipv4_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001839#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001840static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001841 .md5_lookup = tcp_v4_md5_lookup,
Adam Langley49a72df2008-07-19 00:01:42 -07001842 .calc_md5_hash = tcp_v4_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001843 .md5_parse = tcp_v4_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001844};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001845#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847/* NOTE: A lot of things set to zero explicitly by call to
1848 * sk_alloc() so need not be done here.
1849 */
1850static int tcp_v4_init_sock(struct sock *sk)
1851{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001852 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Neal Cardwell900f65d2012-04-19 09:55:21 +00001854 tcp_init_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001856 icsk->icsk_af_ops = &ipv4_specific;
Neal Cardwell900f65d2012-04-19 09:55:21 +00001857
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001858#ifdef CONFIG_TCP_MD5SIG
David S. Millerac807fa2012-04-23 03:21:58 -04001859 tcp_sk(sk)->af_specific = &tcp_sock_ipv4_specific;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return 0;
1863}
1864
Brian Haley7d06b2e2008-06-14 17:04:49 -07001865void tcp_v4_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 struct tcp_sock *tp = tcp_sk(sk);
1868
1869 tcp_clear_xmit_timers(sk);
1870
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001871 tcp_cleanup_congestion_control(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /* Cleanup up the write buffer. */
David S. Millerfe067e82007-03-07 12:12:44 -08001874 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /* Cleans up our, hopefully empty, out_of_order_queue. */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07001877 skb_rbtree_purge(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001879#ifdef CONFIG_TCP_MD5SIG
1880 /* Clean up the MD5 key list, if any */
1881 if (tp->md5sig_info) {
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001882 tcp_clear_md5_list(sk);
Eric Dumazeta8afca02012-01-31 18:45:40 +00001883 kfree_rcu(tp->md5sig_info, rcu);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001884 tp->md5sig_info = NULL;
1885 }
1886#endif
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* Clean prequeue, it must be empty really */
1889 __skb_queue_purge(&tp->ucopy.prequeue);
1890
1891 /* Clean up a referenced TCP bind bucket. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001892 if (inet_csk(sk)->icsk_bind_hash)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001893 inet_put_port(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Ian Morris00db4122015-04-03 09:17:27 +01001895 BUG_ON(tp->fastopen_rsk);
William Allen Simpson435cf552009-12-02 18:17:05 +00001896
Yuchung Chengcf60af02012-07-19 06:43:09 +00001897 /* If socket is aborted during connect operation */
1898 tcp_free_fastopen_req(tp);
Eric Dumazetcd8ae852015-05-03 21:34:46 -07001899 tcp_saved_syn_free(tp);
Yuchung Chengcf60af02012-07-19 06:43:09 +00001900
Eric Dumazet777c6ae2016-05-04 15:27:29 -07001901 local_bh_disable();
Glauber Costa180d8cd2011-12-11 21:47:02 +00001902 sk_sockets_allocated_dec(sk);
Eric Dumazet777c6ae2016-05-04 15:27:29 -07001903 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905EXPORT_SYMBOL(tcp_v4_destroy_sock);
1906
1907#ifdef CONFIG_PROC_FS
1908/* Proc filesystem TCP sock list dumping. */
1909
Tom Herberta8b690f2010-06-07 00:43:42 -07001910/*
1911 * Get next listener socket follow cur. If cur is NULL, get first socket
1912 * starting from bucket given in st->bucket; when st->bucket is zero the
1913 * very first socket in the hash table is returned.
1914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915static void *listening_get_next(struct seq_file *seq, void *cur)
1916{
Jianjun Kong5799de02008-11-03 02:49:10 -08001917 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001918 struct net *net = seq_file_net(seq);
Eric Dumazet3b24d852016-04-01 08:52:17 -07001919 struct inet_listen_hashbucket *ilb;
Eric Dumazet792365b2019-12-13 18:20:41 -08001920 struct hlist_nulls_node *node;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001921 struct sock *sk = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 if (!sk) {
Eric Dumazet3b24d852016-04-01 08:52:17 -07001924get_head:
Tom Herberta8b690f2010-06-07 00:43:42 -07001925 ilb = &tcp_hashinfo.listening_hash[st->bucket];
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001926 spin_lock_bh(&ilb->lock);
Eric Dumazet792365b2019-12-13 18:20:41 -08001927 sk = sk_nulls_head(&ilb->nulls_head);
Tom Herberta8b690f2010-06-07 00:43:42 -07001928 st->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 goto get_sk;
1930 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001931 ilb = &tcp_hashinfo.listening_hash[st->bucket];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 ++st->num;
Tom Herberta8b690f2010-06-07 00:43:42 -07001933 ++st->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Eric Dumazet792365b2019-12-13 18:20:41 -08001935 sk = sk_nulls_next(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936get_sk:
Eric Dumazet792365b2019-12-13 18:20:41 -08001937 sk_nulls_for_each_from(sk, node) {
Pavel Emelyanov8475ef92010-11-22 03:26:12 +00001938 if (!net_eq(sock_net(sk), net))
1939 continue;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001940 if (sk->sk_family == st->family)
1941 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001943 spin_unlock_bh(&ilb->lock);
Tom Herberta8b690f2010-06-07 00:43:42 -07001944 st->offset = 0;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001945 if (++st->bucket < INET_LHTABLE_SIZE)
1946 goto get_head;
1947 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
1950static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
1951{
Tom Herberta8b690f2010-06-07 00:43:42 -07001952 struct tcp_iter_state *st = seq->private;
1953 void *rc;
1954
1955 st->bucket = 0;
1956 st->offset = 0;
1957 rc = listening_get_next(seq, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 while (rc && *pos) {
1960 rc = listening_get_next(seq, rc);
1961 --*pos;
1962 }
1963 return rc;
1964}
1965
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001966static inline bool empty_bucket(const struct tcp_iter_state *st)
Andi Kleen6eac5602008-08-28 01:08:02 -07001967{
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001968 return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain);
Andi Kleen6eac5602008-08-28 01:08:02 -07001969}
1970
Tom Herberta8b690f2010-06-07 00:43:42 -07001971/*
1972 * Get first established socket starting from bucket given in st->bucket.
1973 * If st->bucket is zero, the very first socket in the hash is returned.
1974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975static void *established_get_first(struct seq_file *seq)
1976{
Jianjun Kong5799de02008-11-03 02:49:10 -08001977 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001978 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 void *rc = NULL;
1980
Tom Herberta8b690f2010-06-07 00:43:42 -07001981 st->offset = 0;
1982 for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001984 struct hlist_nulls_node *node;
Eric Dumazet9db66bd2008-11-20 20:39:09 -08001985 spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Andi Kleen6eac5602008-08-28 01:08:02 -07001987 /* Lockless fast path for the common case of empty buckets */
1988 if (empty_bucket(st))
1989 continue;
1990
Eric Dumazet9db66bd2008-11-20 20:39:09 -08001991 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001992 sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07001993 if (sk->sk_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001994 !net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 continue;
1996 }
1997 rc = sk;
1998 goto out;
1999 }
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002000 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002out:
2003 return rc;
2004}
2005
2006static void *established_get_next(struct seq_file *seq, void *cur)
2007{
2008 struct sock *sk = cur;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002009 struct hlist_nulls_node *node;
Jianjun Kong5799de02008-11-03 02:49:10 -08002010 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07002011 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 ++st->num;
Tom Herberta8b690f2010-06-07 00:43:42 -07002014 ++st->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002016 sk = sk_nulls_next(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002018 sk_nulls_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002019 if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002020 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002023 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
2024 ++st->bucket;
2025 return established_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028static void *established_get_idx(struct seq_file *seq, loff_t pos)
2029{
Tom Herberta8b690f2010-06-07 00:43:42 -07002030 struct tcp_iter_state *st = seq->private;
2031 void *rc;
2032
2033 st->bucket = 0;
2034 rc = established_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 while (rc && pos) {
2037 rc = established_get_next(seq, rc);
2038 --pos;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return rc;
2041}
2042
2043static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
2044{
2045 void *rc;
Jianjun Kong5799de02008-11-03 02:49:10 -08002046 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 st->state = TCP_SEQ_STATE_LISTENING;
2049 rc = listening_get_idx(seq, &pos);
2050
2051 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 st->state = TCP_SEQ_STATE_ESTABLISHED;
2053 rc = established_get_idx(seq, pos);
2054 }
2055
2056 return rc;
2057}
2058
Tom Herberta8b690f2010-06-07 00:43:42 -07002059static void *tcp_seek_last_pos(struct seq_file *seq)
2060{
2061 struct tcp_iter_state *st = seq->private;
2062 int offset = st->offset;
2063 int orig_num = st->num;
2064 void *rc = NULL;
2065
2066 switch (st->state) {
Tom Herberta8b690f2010-06-07 00:43:42 -07002067 case TCP_SEQ_STATE_LISTENING:
2068 if (st->bucket >= INET_LHTABLE_SIZE)
2069 break;
2070 st->state = TCP_SEQ_STATE_LISTENING;
2071 rc = listening_get_next(seq, NULL);
2072 while (offset-- && rc)
2073 rc = listening_get_next(seq, rc);
2074 if (rc)
2075 break;
2076 st->bucket = 0;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002077 st->state = TCP_SEQ_STATE_ESTABLISHED;
Tom Herberta8b690f2010-06-07 00:43:42 -07002078 /* Fallthrough */
2079 case TCP_SEQ_STATE_ESTABLISHED:
Tom Herberta8b690f2010-06-07 00:43:42 -07002080 if (st->bucket > tcp_hashinfo.ehash_mask)
2081 break;
2082 rc = established_get_first(seq);
2083 while (offset-- && rc)
2084 rc = established_get_next(seq, rc);
2085 }
2086
2087 st->num = orig_num;
2088
2089 return rc;
2090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
2093{
Jianjun Kong5799de02008-11-03 02:49:10 -08002094 struct tcp_iter_state *st = seq->private;
Tom Herberta8b690f2010-06-07 00:43:42 -07002095 void *rc;
2096
2097 if (*pos && *pos == st->last_pos) {
2098 rc = tcp_seek_last_pos(seq);
2099 if (rc)
2100 goto out;
2101 }
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 st->state = TCP_SEQ_STATE_LISTENING;
2104 st->num = 0;
Tom Herberta8b690f2010-06-07 00:43:42 -07002105 st->bucket = 0;
2106 st->offset = 0;
2107 rc = *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2108
2109out:
2110 st->last_pos = *pos;
2111 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2115{
Tom Herberta8b690f2010-06-07 00:43:42 -07002116 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 void *rc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 if (v == SEQ_START_TOKEN) {
2120 rc = tcp_get_idx(seq, 0);
2121 goto out;
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 switch (st->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 case TCP_SEQ_STATE_LISTENING:
2126 rc = listening_get_next(seq, v);
2127 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 st->state = TCP_SEQ_STATE_ESTABLISHED;
Tom Herberta8b690f2010-06-07 00:43:42 -07002129 st->bucket = 0;
2130 st->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 rc = established_get_first(seq);
2132 }
2133 break;
2134 case TCP_SEQ_STATE_ESTABLISHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 rc = established_get_next(seq, v);
2136 break;
2137 }
2138out:
2139 ++*pos;
Tom Herberta8b690f2010-06-07 00:43:42 -07002140 st->last_pos = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return rc;
2142}
2143
2144static void tcp_seq_stop(struct seq_file *seq, void *v)
2145{
Jianjun Kong5799de02008-11-03 02:49:10 -08002146 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 switch (st->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 case TCP_SEQ_STATE_LISTENING:
2150 if (v != SEQ_START_TOKEN)
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002151 spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 case TCP_SEQ_STATE_ESTABLISHED:
2154 if (v)
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002155 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 break;
2157 }
2158}
2159
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002160int tcp_seq_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Al Virod9dda782013-03-31 18:16:14 -04002162 struct tcp_seq_afinfo *afinfo = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 struct tcp_iter_state *s;
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002164 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002166 err = seq_open_net(inode, file, &afinfo->seq_ops,
2167 sizeof(struct tcp_iter_state));
2168 if (err < 0)
2169 return err;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002170
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002171 s = ((struct seq_file *)file->private_data)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 s->family = afinfo->family;
stephen hemminger688d1942014-08-29 23:32:05 -07002173 s->last_pos = 0;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002174 return 0;
2175}
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002176EXPORT_SYMBOL(tcp_seq_open);
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002177
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002178int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 int rc = 0;
2181 struct proc_dir_entry *p;
2182
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002183 afinfo->seq_ops.start = tcp_seq_start;
2184 afinfo->seq_ops.next = tcp_seq_next;
2185 afinfo->seq_ops.stop = tcp_seq_stop;
2186
Denis V. Lunev84841c32008-05-02 04:10:08 -07002187 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002188 afinfo->seq_fops, afinfo);
Denis V. Lunev84841c32008-05-02 04:10:08 -07002189 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 rc = -ENOMEM;
2191 return rc;
2192}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002193EXPORT_SYMBOL(tcp_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002195void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Gao fengece31ff2013-02-18 01:34:56 +00002197 remove_proc_entry(afinfo->name, net->proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002199EXPORT_SYMBOL(tcp_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Eric Dumazetd4f06872015-03-12 16:44:09 -07002201static void get_openreq4(const struct request_sock *req,
Eric Dumazetaa3a0c82015-10-02 11:43:30 -07002202 struct seq_file *f, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002204 const struct inet_request_sock *ireq = inet_rsk(req);
Eric Dumazetfa76ce732015-03-19 19:04:20 -07002205 long delta = req->rsk_timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002207 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002208 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 i,
Eric Dumazet634fb9792013-10-09 15:21:29 -07002210 ireq->ir_loc_addr,
Eric Dumazetd4f06872015-03-12 16:44:09 -07002211 ireq->ir_num,
Eric Dumazet634fb9792013-10-09 15:21:29 -07002212 ireq->ir_rmt_addr,
2213 ntohs(ireq->ir_rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 TCP_SYN_RECV,
2215 0, 0, /* could print option size, but that is af dependent. */
2216 1, /* timers active (only the expire timer) */
Eric Dumazeta399a802012-08-08 21:13:53 +00002217 jiffies_delta_to_clock_t(delta),
Eric Dumazete6c022a2012-10-27 23:16:46 +00002218 req->num_timeout,
Eric Dumazetaa3a0c82015-10-02 11:43:30 -07002219 from_kuid_munged(seq_user_ns(f),
2220 sock_i_uid(req->rsk_listener)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 0, /* non standard timer */
2222 0, /* open_requests have no inode */
Eric Dumazetd4f06872015-03-12 16:44:09 -07002223 0,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002224 req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Tetsuo Handa652586d2013-11-14 14:31:57 -08002227static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 int timer_active;
2230 unsigned long timer_expires;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002231 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002232 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002233 const struct inet_sock *inet = inet_sk(sk);
Eric Dumazet0536fcc2015-09-29 07:42:52 -07002234 const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00002235 __be32 dest = inet->inet_daddr;
2236 __be32 src = inet->inet_rcv_saddr;
2237 __u16 destp = ntohs(inet->inet_dport);
2238 __u16 srcp = ntohs(inet->inet_sport);
Eric Dumazet49d09002009-12-03 16:06:13 -08002239 int rx_queue;
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002240 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002242 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
2243 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
2244 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002246 timer_expires = icsk->icsk_timeout;
2247 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002249 timer_expires = icsk->icsk_timeout;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002250 } else if (timer_pending(&sk->sk_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 timer_active = 2;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002252 timer_expires = sk->sk_timer.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 } else {
2254 timer_active = 0;
2255 timer_expires = jiffies;
2256 }
2257
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002258 state = sk_state_load(sk);
2259 if (state == TCP_LISTEN)
Eric Dumazet49d09002009-12-03 16:06:13 -08002260 rx_queue = sk->sk_ack_backlog;
2261 else
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002262 /* Because we don't lock the socket,
2263 * we might find a transient negative value.
Eric Dumazet49d09002009-12-03 16:06:13 -08002264 */
2265 rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
2266
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002267 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
Tetsuo Handa652586d2013-11-14 14:31:57 -08002268 "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d",
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002269 i, src, srcp, dest, destp, state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07002270 tp->write_seq - tp->snd_una,
Eric Dumazet49d09002009-12-03 16:06:13 -08002271 rx_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 timer_active,
Eric Dumazeta399a802012-08-08 21:13:53 +00002273 jiffies_delta_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002274 icsk->icsk_retransmits,
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06002275 from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002276 icsk->icsk_probes_out,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002277 sock_i_ino(sk),
2278 atomic_read(&sk->sk_refcnt), sk,
Stephen Hemminger7be87352008-06-27 20:00:19 -07002279 jiffies_to_clock_t(icsk->icsk_rto),
2280 jiffies_to_clock_t(icsk->icsk_ack.ato),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002281 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 tp->snd_cwnd,
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002283 state == TCP_LISTEN ?
2284 fastopenq->max_qlen :
Tetsuo Handa652586d2013-11-14 14:31:57 -08002285 (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002288static void get_timewait4_sock(const struct inet_timewait_sock *tw,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002289 struct seq_file *f, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Eric Dumazet789f5582015-04-12 18:51:09 -07002291 long delta = tw->tw_timer.expires - jiffies;
Al Viro23f33c22006-09-27 18:43:50 -07002292 __be32 dest, src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 __u16 destp, srcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 dest = tw->tw_daddr;
2296 src = tw->tw_rcv_saddr;
2297 destp = ntohs(tw->tw_dport);
2298 srcp = ntohs(tw->tw_sport);
2299
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002300 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002301 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
Eric Dumazeta399a802012-08-08 21:13:53 +00002303 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002304 atomic_read(&tw->tw_refcnt), tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
2307#define TMPSZ 150
2308
2309static int tcp4_seq_show(struct seq_file *seq, void *v)
2310{
Jianjun Kong5799de02008-11-03 02:49:10 -08002311 struct tcp_iter_state *st;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002312 struct sock *sk = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Tetsuo Handa652586d2013-11-14 14:31:57 -08002314 seq_setwidth(seq, TMPSZ - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (v == SEQ_START_TOKEN) {
Tetsuo Handa652586d2013-11-14 14:31:57 -08002316 seq_puts(seq, " sl local_address rem_address st tx_queue "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 "rx_queue tr tm->when retrnsmt uid timeout "
2318 "inode");
2319 goto out;
2320 }
2321 st = seq->private;
2322
Eric Dumazet079096f2015-10-02 11:43:32 -07002323 if (sk->sk_state == TCP_TIME_WAIT)
2324 get_timewait4_sock(v, seq, st->num);
2325 else if (sk->sk_state == TCP_NEW_SYN_RECV)
Eric Dumazetaa3a0c82015-10-02 11:43:30 -07002326 get_openreq4(v, seq, st->num);
Eric Dumazet079096f2015-10-02 11:43:32 -07002327 else
2328 get_tcp4_sock(v, seq, st->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329out:
Tetsuo Handa652586d2013-11-14 14:31:57 -08002330 seq_pad(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return 0;
2332}
2333
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002334static const struct file_operations tcp_afinfo_seq_fops = {
2335 .owner = THIS_MODULE,
2336 .open = tcp_seq_open,
2337 .read = seq_read,
2338 .llseek = seq_lseek,
2339 .release = seq_release_net
2340};
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342static struct tcp_seq_afinfo tcp4_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 .name = "tcp",
2344 .family = AF_INET,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002345 .seq_fops = &tcp_afinfo_seq_fops,
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002346 .seq_ops = {
2347 .show = tcp4_seq_show,
2348 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349};
2350
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002351static int __net_init tcp4_proc_init_net(struct net *net)
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002352{
2353 return tcp_proc_register(net, &tcp4_seq_afinfo);
2354}
2355
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002356static void __net_exit tcp4_proc_exit_net(struct net *net)
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002357{
2358 tcp_proc_unregister(net, &tcp4_seq_afinfo);
2359}
2360
2361static struct pernet_operations tcp4_net_ops = {
2362 .init = tcp4_proc_init_net,
2363 .exit = tcp4_proc_exit_net,
2364};
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366int __init tcp4_proc_init(void)
2367{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002368 return register_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
2371void tcp4_proc_exit(void)
2372{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002373 unregister_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375#endif /* CONFIG_PROC_FS */
2376
2377struct proto tcp_prot = {
2378 .name = "TCP",
2379 .owner = THIS_MODULE,
2380 .close = tcp_close,
2381 .connect = tcp_v4_connect,
2382 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002383 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 .ioctl = tcp_ioctl,
2385 .init = tcp_v4_init_sock,
2386 .destroy = tcp_v4_destroy_sock,
2387 .shutdown = tcp_shutdown,
2388 .setsockopt = tcp_setsockopt,
2389 .getsockopt = tcp_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 .recvmsg = tcp_recvmsg,
Changli Gao7ba42912010-07-10 20:41:55 +00002391 .sendmsg = tcp_sendmsg,
2392 .sendpage = tcp_sendpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 .backlog_rcv = tcp_v4_do_rcv,
Eric Dumazet46d3cea2012-07-11 05:50:31 +00002394 .release_cb = tcp_release_cb,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08002395 .hash = inet_hash,
2396 .unhash = inet_unhash,
2397 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 .enter_memory_pressure = tcp_enter_memory_pressure,
Eric Dumazetc9bee3b72013-07-22 20:27:07 -07002399 .stream_memory_free = tcp_stream_memory_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 .sockets_allocated = &tcp_sockets_allocated,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07002401 .orphan_count = &tcp_orphan_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 .memory_allocated = &tcp_memory_allocated,
2403 .memory_pressure = &tcp_memory_pressure,
Eric W. Biedermana4fe34b2013-10-19 16:25:36 -07002404 .sysctl_mem = sysctl_tcp_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 .sysctl_wmem = sysctl_tcp_wmem,
2406 .sysctl_rmem = sysctl_tcp_rmem,
2407 .max_header = MAX_TCP_HEADER,
2408 .obj_size = sizeof(struct tcp_sock),
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002409 .slab_flags = SLAB_DESTROY_BY_RCU,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002410 .twsk_prot = &tcp_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002411 .rsk_prot = &tcp_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07002412 .h.hashinfo = &tcp_hashinfo,
Changli Gao7ba42912010-07-10 20:41:55 +00002413 .no_autobind = true,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002414#ifdef CONFIG_COMPAT
2415 .compat_setsockopt = compat_tcp_setsockopt,
2416 .compat_getsockopt = compat_tcp_getsockopt,
2417#endif
Lorenzo Colittic1e64e22015-12-16 12:30:05 +09002418 .diag_destroy = tcp_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419};
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002420EXPORT_SYMBOL(tcp_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Denis V. Lunev046ee902008-04-03 14:31:33 -07002422static void __net_exit tcp_sk_exit(struct net *net)
2423{
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002424 int cpu;
2425
2426 for_each_possible_cpu(cpu)
2427 inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.tcp_sk, cpu));
2428 free_percpu(net->ipv4.tcp_sk);
2429}
2430
2431static int __net_init tcp_sk_init(struct net *net)
2432{
2433 int res, cpu;
2434
2435 net->ipv4.tcp_sk = alloc_percpu(struct sock *);
2436 if (!net->ipv4.tcp_sk)
2437 return -ENOMEM;
2438
2439 for_each_possible_cpu(cpu) {
2440 struct sock *sk;
2441
2442 res = inet_ctl_sock_create(&sk, PF_INET, SOCK_RAW,
2443 IPPROTO_TCP, net);
2444 if (res)
2445 goto fail;
Eric Dumazeta9d65322016-04-01 08:52:21 -07002446 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
Eric Dumazete801b692018-08-22 13:30:45 -07002447
2448 /* Please enforce IP_DF and IPID==0 for RST and
2449 * ACK sent in SYN-RECV and TIME-WAIT state.
2450 */
2451 inet_sk(sk)->pmtudisc = IP_PMTUDISC_DO;
2452
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002453 *per_cpu_ptr(net->ipv4.tcp_sk, cpu) = sk;
2454 }
Daniel Borkmann49213552015-05-19 21:04:22 +02002455
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002456 net->ipv4.sysctl_tcp_ecn = 2;
Daniel Borkmann49213552015-05-19 21:04:22 +02002457 net->ipv4.sysctl_tcp_ecn_fallback = 1;
2458
Fan Dub0f9ca52015-02-10 09:53:16 +08002459 net->ipv4.sysctl_tcp_base_mss = TCP_BASE_MSS;
Eric Dumazet8e39cbc2019-06-15 17:44:24 -07002460 net->ipv4.sysctl_tcp_min_snd_mss = TCP_MIN_SND_MSS;
Fan Du6b58e0a2015-03-06 11:18:23 +08002461 net->ipv4.sysctl_tcp_probe_threshold = TCP_PROBE_THRESHOLD;
Fan Du05cbc0d2015-03-06 11:18:24 +08002462 net->ipv4.sysctl_tcp_probe_interval = TCP_PROBE_INTERVAL;
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002463
Nikolay Borisov13b287e2016-01-07 16:38:43 +02002464 net->ipv4.sysctl_tcp_keepalive_time = TCP_KEEPALIVE_TIME;
Nikolay Borisov9bd68612016-01-07 16:38:44 +02002465 net->ipv4.sysctl_tcp_keepalive_probes = TCP_KEEPALIVE_PROBES;
Nikolay Borisovb840d152016-01-07 16:38:45 +02002466 net->ipv4.sysctl_tcp_keepalive_intvl = TCP_KEEPALIVE_INTVL;
Nikolay Borisov13b287e2016-01-07 16:38:43 +02002467
Nikolay Borisov6fa25162016-02-03 09:46:49 +02002468 net->ipv4.sysctl_tcp_syn_retries = TCP_SYN_RETRIES;
Nikolay Borisov7c083ec2016-02-03 09:46:50 +02002469 net->ipv4.sysctl_tcp_synack_retries = TCP_SYNACK_RETRIES;
David S. Miller0aca7372016-02-08 04:24:33 -05002470 net->ipv4.sysctl_tcp_syncookies = 1;
Nikolay Borisov1043e252016-02-03 09:46:52 +02002471 net->ipv4.sysctl_tcp_reordering = TCP_FASTRETRANS_THRESH;
Nikolay Borisovae5c3f42016-02-03 09:46:53 +02002472 net->ipv4.sysctl_tcp_retries1 = TCP_RETR1;
Nikolay Borisovc6214a92016-02-03 09:46:54 +02002473 net->ipv4.sysctl_tcp_retries2 = TCP_RETR2;
Nikolay Borisovc402d9b2016-02-03 09:46:55 +02002474 net->ipv4.sysctl_tcp_orphan_retries = 0;
Nikolay Borisov1e579ca2016-02-03 09:46:56 +02002475 net->ipv4.sysctl_tcp_fin_timeout = TCP_FIN_TIMEOUT;
Nikolay Borisov4979f2d2016-02-03 09:46:57 +02002476 net->ipv4.sysctl_tcp_notsent_lowat = UINT_MAX;
Nikolay Borisov12ed8242016-02-03 09:46:51 +02002477
Daniel Borkmann49213552015-05-19 21:04:22 +02002478 return 0;
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002479fail:
2480 tcp_sk_exit(net);
2481
2482 return res;
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002483}
2484
2485static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
2486{
2487 inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET);
Denis V. Lunev046ee902008-04-03 14:31:33 -07002488}
2489
2490static struct pernet_operations __net_initdata tcp_sk_ops = {
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002491 .init = tcp_sk_init,
2492 .exit = tcp_sk_exit,
2493 .exit_batch = tcp_sk_exit_batch,
Denis V. Lunev046ee902008-04-03 14:31:33 -07002494};
2495
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08002496void __init tcp_v4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002498 inet_hashinfo_init(&tcp_hashinfo);
Eric W. Biederman6a1b3052009-02-22 00:10:18 -08002499 if (register_pernet_subsys(&tcp_sk_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 panic("Failed to create the TCP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}