blob: 05ac17aec505e5fea96cdd7c57492185a9f97b23 [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 Dumazetc720c7e2009-10-15 06:30:45 +0000242 inet->inet_id = tp->write_seq ^ jiffies;
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
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000467 icsk->icsk_backoff--;
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -0700468 icsk->icsk_rto = tp->srtt_us ? __tcp_set_rto(tp) :
469 TCP_TIMEOUT_INIT;
470 icsk->icsk_rto = inet_csk_rto_backoff(icsk, TCP_RTO_MAX);
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000471
472 skb = tcp_write_queue_head(sk);
473 BUG_ON(!skb);
474
Eric Dumazet7faee5c2014-09-05 15:33:33 -0700475 remaining = icsk->icsk_rto -
476 min(icsk->icsk_rto,
477 tcp_time_stamp - tcp_skb_timestamp(skb));
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000478
479 if (remaining) {
480 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
481 remaining, TCP_RTO_MAX);
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000482 } else {
483 /* RTO revert clocked out retransmission.
484 * Will retransmit now */
485 tcp_retransmit_timer(sk);
486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489 case ICMP_TIME_EXCEEDED:
490 err = EHOSTUNREACH;
491 break;
492 default:
493 goto out;
494 }
495
496 switch (sk->sk_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 case TCP_SYN_SENT:
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700498 case TCP_SYN_RECV:
499 /* Only in fast or simultaneous open. If a fast open socket is
500 * is already accepted it is treated as a connected one below.
501 */
Ian Morris51456b22015-04-03 09:17:26 +0100502 if (fastopen && !fastopen->sk)
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700503 break;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!sock_owned_by_user(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 sk->sk_err = err;
507
508 sk->sk_error_report(sk);
509
510 tcp_done(sk);
511 } else {
512 sk->sk_err_soft = err;
513 }
514 goto out;
515 }
516
517 /* If we've already connected we will keep trying
518 * until we time out, or the user gives up.
519 *
520 * rfc1122 4.2.3.9 allows to consider as hard errors
521 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
522 * but it is obsoleted by pmtu discovery).
523 *
524 * Note, that in modern internet, where routing is unreliable
525 * and in each dark corner broken firewalls sit, sending random
526 * errors ordered by their masters even this two messages finally lose
527 * their original sense (even Linux sends invalid PORT_UNREACHs)
528 *
529 * Now we are in compliance with RFCs.
530 * --ANK (980905)
531 */
532
533 inet = inet_sk(sk);
534 if (!sock_owned_by_user(sk) && inet->recverr) {
535 sk->sk_err = err;
536 sk->sk_error_report(sk);
537 } else { /* Only an error on timeout */
538 sk->sk_err_soft = err;
539 }
540
541out:
542 bh_unlock_sock(sk);
543 sock_put(sk);
544}
545
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000546void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700548 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Patrick McHardy84fa7932006-08-29 16:44:56 -0700550 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Herbert Xu419f9f82010-04-11 02:15:53 +0000551 th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700552 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800553 skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else {
Herbert Xu419f9f82010-04-11 02:15:53 +0000555 th->check = tcp_v4_check(skb->len, saddr, daddr,
Joe Perches07f07572008-11-19 15:44:53 -0800556 csum_partial(th,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 th->doff << 2,
558 skb->csum));
559 }
560}
561
Herbert Xu419f9f82010-04-11 02:15:53 +0000562/* This routine computes an IPv4 TCP checksum. */
Herbert Xubb296242010-04-11 02:15:55 +0000563void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
Herbert Xu419f9f82010-04-11 02:15:53 +0000564{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400565 const struct inet_sock *inet = inet_sk(sk);
Herbert Xu419f9f82010-04-11 02:15:53 +0000566
567 __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr);
568}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000569EXPORT_SYMBOL(tcp_v4_send_check);
Herbert Xu419f9f82010-04-11 02:15:53 +0000570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/*
572 * This routine will send an RST to the other tcp.
573 *
574 * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
575 * for reset.
576 * Answer: if a packet caused RST, it is not for a socket
577 * existing in our system, if it is matched to a socket,
578 * it is just duplicate segment or bug in other side's TCP.
579 * So that we build reply only basing on parameters
580 * arrived with segment.
581 * Exception: precedence violation. We do not implement it in any case.
582 */
583
Eric Dumazeta00e7442015-09-29 07:42:39 -0700584static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400586 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800587 struct {
588 struct tcphdr th;
589#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800590 __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800591#endif
592 } rep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800594#ifdef CONFIG_TCP_MD5SIG
Florian Westphale46787f2015-12-21 21:29:25 +0100595 struct tcp_md5sig_key *key = NULL;
Shawn Lu658ddaa2012-01-31 22:35:48 +0000596 const __u8 *hash_location = NULL;
597 unsigned char newhash[16];
598 int genhash;
599 struct sock *sk1 = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800600#endif
Pavel Emelyanova86b1e32008-07-16 20:20:58 -0700601 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Never send a reset in response to a reset. */
604 if (th->rst)
605 return;
606
Eric Dumazetc3658e82014-11-25 07:40:04 -0800607 /* If sk not NULL, it means we did a successful lookup and incoming
608 * route had to be correct. prequeue might have dropped our dst.
609 */
610 if (!sk && skb_rtable(skb)->rt_type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return;
612
613 /* Swap the send and the receive. */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800614 memset(&rep, 0, sizeof(rep));
615 rep.th.dest = th->source;
616 rep.th.source = th->dest;
617 rep.th.doff = sizeof(struct tcphdr) / 4;
618 rep.th.rst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (th->ack) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800621 rep.th.seq = th->ack_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800623 rep.th.ack = 1;
624 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
625 skb->len - (th->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200628 memset(&arg, 0, sizeof(arg));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800629 arg.iov[0].iov_base = (unsigned char *)&rep;
630 arg.iov[0].iov_len = sizeof(rep.th);
631
Eric Dumazet0f85fea2014-12-09 09:56:08 -0800632 net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800633#ifdef CONFIG_TCP_MD5SIG
Eric Dumazet3b24d852016-04-01 08:52:17 -0700634 rcu_read_lock();
Shawn Lu658ddaa2012-01-31 22:35:48 +0000635 hash_location = tcp_parse_md5sig_option(th);
Florian Westphal271c3b92015-12-21 21:29:26 +0100636 if (sk && sk_fullsock(sk)) {
Florian Westphale46787f2015-12-21 21:29:25 +0100637 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)
638 &ip_hdr(skb)->saddr, AF_INET);
639 } else if (hash_location) {
Shawn Lu658ddaa2012-01-31 22:35:48 +0000640 /*
641 * active side is lost. Try to find listening socket through
642 * source port, and then find md5 key through listening socket.
643 * we are not loose security here:
644 * Incoming packet is checked with md5 hash with finding key,
645 * no RST generated if md5 hash doesn't match.
646 */
Craig Galleka5836362016-02-10 11:50:38 -0500647 sk1 = __inet_lookup_listener(net, &tcp_hashinfo, NULL, 0,
648 ip_hdr(skb)->saddr,
Tom Herbertda5e3632013-01-22 09:50:24 +0000649 th->source, ip_hdr(skb)->daddr,
Shawn Lu658ddaa2012-01-31 22:35:48 +0000650 ntohs(th->source), inet_iif(skb));
651 /* don't send rst if it can't find key */
652 if (!sk1)
Eric Dumazet3b24d852016-04-01 08:52:17 -0700653 goto out;
654
Shawn Lu658ddaa2012-01-31 22:35:48 +0000655 key = tcp_md5_do_lookup(sk1, (union tcp_md5_addr *)
656 &ip_hdr(skb)->saddr, AF_INET);
657 if (!key)
Eric Dumazet3b24d852016-04-01 08:52:17 -0700658 goto out;
659
Shawn Lu658ddaa2012-01-31 22:35:48 +0000660
Eric Dumazet39f8e582015-03-24 15:58:55 -0700661 genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
Shawn Lu658ddaa2012-01-31 22:35:48 +0000662 if (genhash || memcmp(hash_location, newhash, 16) != 0)
Eric Dumazet3b24d852016-04-01 08:52:17 -0700663 goto out;
664
Shawn Lu658ddaa2012-01-31 22:35:48 +0000665 }
666
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800667 if (key) {
668 rep.opt[0] = htonl((TCPOPT_NOP << 24) |
669 (TCPOPT_NOP << 16) |
670 (TCPOPT_MD5SIG << 8) |
671 TCPOLEN_MD5SIG);
672 /* Update length and the length the header thinks exists */
673 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
674 rep.th.doff = arg.iov[0].iov_len / 4;
675
Adam Langley49a72df2008-07-19 00:01:42 -0700676 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
Ilpo Järvinen78e645cb2008-10-09 14:37:47 -0700677 key, ip_hdr(skb)->saddr,
678 ip_hdr(skb)->daddr, &rep.th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800679 }
680#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700681 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
682 ip_hdr(skb)->saddr, /* XXX */
Ilpo Järvinen52cd5752008-10-08 11:34:06 -0700683 arg.iov[0].iov_len, IPPROTO_TCP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
Florian Westphal271c3b92015-12-21 21:29:26 +0100685 arg.flags = (sk && inet_sk_transparent(sk)) ? IP_REPLY_ARG_NOSRCCHECK : 0;
686
Shawn Lue2446ea2012-02-04 12:38:09 +0000687 /* When socket is gone, all binding information is lost.
Alexey Kuznetsov4c675252012-10-12 04:34:17 +0000688 * routing might fail in this case. No choice here, if we choose to force
689 * input interface, we will misroute in case of asymmetric route.
Shawn Lue2446ea2012-02-04 12:38:09 +0000690 */
Alexey Kuznetsov4c675252012-10-12 04:34:17 +0000691 if (sk)
692 arg.bound_dev_if = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Florian Westphal271c3b92015-12-21 21:29:26 +0100694 BUILD_BUG_ON(offsetof(struct sock, sk_bound_dev_if) !=
695 offsetof(struct inet_timewait_sock, tw_bound_dev_if));
696
Eric Dumazet66b13d92011-10-24 03:06:21 -0400697 arg.tos = ip_hdr(skb)->tos;
Lorenzo Colitti50442922016-11-04 02:23:43 +0900698 arg.uid = sock_net_uid(net, sk && sk_fullsock(sk) ? sk : NULL);
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
Lorenzo Colitti50442922016-11-04 02:23:43 +0900719static void tcp_v4_send_ack(const struct sock *sk,
Eric Dumazete62a1232016-01-21 08:02:54 -0800720 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;
Lorenzo Colitti50442922016-11-04 02:23:43 +0900734 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct ip_reply_arg arg;
736
737 memset(&rep.th, 0, sizeof(struct tcphdr));
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200738 memset(&arg, 0, sizeof(arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 arg.iov[0].iov_base = (unsigned char *)&rep;
741 arg.iov[0].iov_len = sizeof(rep.th);
Andrey Vaginee684b62013-02-11 05:50:19 +0000742 if (tsecr) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800743 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
744 (TCPOPT_TIMESTAMP << 8) |
745 TCPOLEN_TIMESTAMP);
Andrey Vaginee684b62013-02-11 05:50:19 +0000746 rep.opt[1] = htonl(tsval);
747 rep.opt[2] = htonl(tsecr);
Craig Schlentercb48cfe2007-01-09 00:11:15 -0800748 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
751 /* Swap the send and the receive. */
752 rep.th.dest = th->source;
753 rep.th.source = th->dest;
754 rep.th.doff = arg.iov[0].iov_len / 4;
755 rep.th.seq = htonl(seq);
756 rep.th.ack_seq = htonl(ack);
757 rep.th.ack = 1;
758 rep.th.window = htons(win);
759
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800760#ifdef CONFIG_TCP_MD5SIG
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800761 if (key) {
Andrey Vaginee684b62013-02-11 05:50:19 +0000762 int offset = (tsecr) ? 3 : 0;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800763
764 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
765 (TCPOPT_NOP << 16) |
766 (TCPOPT_MD5SIG << 8) |
767 TCPOLEN_MD5SIG);
768 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
769 rep.th.doff = arg.iov[0].iov_len/4;
770
Adam Langley49a72df2008-07-19 00:01:42 -0700771 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset],
Adam Langley90b7e112008-07-31 20:49:48 -0700772 key, ip_hdr(skb)->saddr,
773 ip_hdr(skb)->daddr, &rep.th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800774 }
775#endif
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700776 arg.flags = reply_flags;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700777 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
778 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 arg.iov[0].iov_len, IPPROTO_TCP, 0);
780 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900781 if (oif)
782 arg.bound_dev_if = oif;
Eric Dumazet66b13d92011-10-24 03:06:21 -0400783 arg.tos = tos;
Lorenzo Colitti50442922016-11-04 02:23:43 +0900784 arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
Eric Dumazet47dcc202016-05-06 09:46:18 -0700785 local_bh_disable();
Eric Dumazetbdbbb852015-01-29 21:35:05 -0800786 ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
787 skb, &TCP_SKB_CB(skb)->header.h4.opt,
Eric Dumazet24a2d432014-09-27 09:50:55 -0700788 ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
789 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Eric Dumazet90bbcc62016-04-27 16:44:32 -0700791 __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
Eric Dumazet47dcc202016-05-06 09:46:18 -0700792 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
796{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700797 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800798 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Lorenzo Colitti50442922016-11-04 02:23:43 +0900800 tcp_v4_send_ack(sk, skb,
Eric Dumazete62a1232016-01-21 08:02:54 -0800801 tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200802 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
Andrey Vaginee684b62013-02-11 05:50:19 +0000803 tcp_time_stamp + tcptw->tw_ts_offset,
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900804 tcptw->tw_ts_recent,
805 tw->tw_bound_dev_if,
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700806 tcp_twsk_md5_key(tcptw),
Eric Dumazet66b13d92011-10-24 03:06:21 -0400807 tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0,
808 tw->tw_tos
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900809 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700811 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Eric Dumazeta00e7442015-09-29 07:42:39 -0700814static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200815 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Jerry Chu168a8f52012-08-31 12:29:13 +0000817 /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV
818 * sk->sk_state == TCP_SYN_RECV -> for Fast Open.
819 */
Eric Dumazete62a1232016-01-21 08:02:54 -0800820 u32 seq = (sk->sk_state == TCP_LISTEN) ? tcp_rsk(req)->snt_isn + 1 :
821 tcp_sk(sk)->snd_nxt;
822
Eric Dumazet20a2b492016-08-22 11:31:10 -0700823 /* RFC 7323 2.3
824 * The window field (SEG.WND) of every outgoing segment, with the
825 * exception of <SYN> segments, MUST be right-shifted by
826 * Rcv.Wind.Shift bits:
827 */
Lorenzo Colitti50442922016-11-04 02:23:43 +0900828 tcp_v4_send_ack(sk, skb, seq,
Eric Dumazet20a2b492016-08-22 11:31:10 -0700829 tcp_rsk(req)->rcv_nxt,
830 req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale,
Andrey Vaginee684b62013-02-11 05:50:19 +0000831 tcp_time_stamp,
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900832 req->ts_recent,
833 0,
Christoph Paasch7887a702017-12-11 00:05:46 -0800834 tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&ip_hdr(skb)->saddr,
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000835 AF_INET),
Eric Dumazet66b13d92011-10-24 03:06:21 -0400836 inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0,
837 ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/*
Kris Katterjohn9bf1d832008-02-17 22:29:19 -0800841 * Send a SYN-ACK after having received a SYN.
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700842 * This still operates on a request_sock only, not on a big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * socket.
844 */
Eric Dumazet0f935dbe2015-09-25 07:39:21 -0700845static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
Octavian Purdilad6274bd2014-06-25 17:09:58 +0300846 struct flowi *fl,
Octavian Purdila72659ec2010-01-17 19:09:39 -0800847 struct request_sock *req,
Eric Dumazetca6fb062015-10-02 11:43:35 -0700848 struct tcp_fastopen_cookie *foc,
Eric Dumazetb3d05142016-04-13 22:05:39 -0700849 enum tcp_synack_type synack_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700851 const struct inet_request_sock *ireq = inet_rsk(req);
David S. Miller6bd023f2011-05-18 18:32:03 -0400852 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int err = -1;
Weilong Chend41db5a2013-12-23 14:37:28 +0800854 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* First, grab a route. */
David S. Millerba3f7f02012-07-17 14:02:46 -0700857 if (!dst && (dst = inet_csk_route_req(sk, &fl4, req)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800858 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Eric Dumazetb3d05142016-04-13 22:05:39 -0700860 skb = tcp_make_synack(sk, dst, req, foc, synack_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (skb) {
Eric Dumazet634fb9792013-10-09 15:21:29 -0700863 __tcp_v4_send_check(skb, ireq->ir_loc_addr, ireq->ir_rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Eric Dumazet634fb9792013-10-09 15:21:29 -0700865 err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
866 ireq->ir_rmt_addr,
Eric Dumazet2af59c62017-10-24 08:20:31 -0700867 ireq_opt_deref(ireq));
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 Dumazetc720c7e2009-10-15 06:30:45 +00001310 newinet->inet_id = newtp->write_seq ^ jiffies;
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 }
Eric Dumazet77166822016-02-18 05:39:18 -08001667 if (unlikely(sk->sk_state != TCP_LISTEN)) {
Eric Dumazetf03f2e12015-10-14 11:16:27 -07001668 inet_csk_reqsk_queue_drop_and_put(sk, req);
Eric Dumazet4bdc3d62015-10-13 17:12:54 -07001669 goto lookup;
1670 }
Eric Dumazet3b24d852016-04-01 08:52:17 -07001671 /* We own a reference on the listener, increase it again
1672 * as we might lose it too soon.
1673 */
Eric Dumazet77166822016-02-18 05:39:18 -08001674 sock_hold(sk);
Eric Dumazet3b24d852016-04-01 08:52:17 -07001675 refcounted = true;
Eric Dumazet77166822016-02-18 05:39:18 -08001676 nsk = tcp_check_req(sk, skb, req, false);
Eric Dumazet079096f2015-10-02 11:43:32 -07001677 if (!nsk) {
1678 reqsk_put(req);
Eric Dumazet77166822016-02-18 05:39:18 -08001679 goto discard_and_relse;
Eric Dumazet079096f2015-10-02 11:43:32 -07001680 }
1681 if (nsk == sk) {
Eric Dumazet079096f2015-10-02 11:43:32 -07001682 reqsk_put(req);
1683 } else if (tcp_child_process(sk, nsk, skb)) {
1684 tcp_v4_send_reset(nsk, skb);
Eric Dumazet77166822016-02-18 05:39:18 -08001685 goto discard_and_relse;
Eric Dumazet079096f2015-10-02 11:43:32 -07001686 } else {
Eric Dumazet77166822016-02-18 05:39:18 -08001687 sock_put(sk);
Eric Dumazet079096f2015-10-02 11:43:32 -07001688 return 0;
1689 }
1690 }
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001691 if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07001692 __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001693 goto discard_and_relse;
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001694 }
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1697 goto discard_and_relse;
Dmitry Popov9ea88a12014-08-07 02:38:22 +04001698
Dmitry Popov9ea88a12014-08-07 02:38:22 +04001699 if (tcp_v4_inbound_md5_hash(sk, skb))
1700 goto discard_and_relse;
Dmitry Popov9ea88a12014-08-07 02:38:22 +04001701
Patrick McHardyb59c2702006-01-06 23:06:10 -08001702 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Eric Dumazetac6e7802016-11-10 13:12:35 -08001704 if (tcp_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto discard_and_relse;
Eric Dumazetac6e7802016-11-10 13:12:35 -08001706 th = (const struct tcphdr *)skb->data;
1707 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 skb->dev = NULL;
1710
Eric Dumazete994b2f2015-10-02 11:43:39 -07001711 if (sk->sk_state == TCP_LISTEN) {
1712 ret = tcp_v4_do_rcv(sk, skb);
1713 goto put_and_return;
1714 }
1715
1716 sk_incoming_cpu_update(sk);
1717
Ingo Molnarc6366182006-07-03 00:25:13 -07001718 bh_lock_sock_nested(sk);
Martin KaFai Laua44d6ea2016-03-14 10:52:15 -07001719 tcp_segs_in(tcp_sk(sk), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 ret = 0;
1721 if (!sock_owned_by_user(sk)) {
Dan Williams7bced392013-12-30 12:37:29 -08001722 if (!tcp_prequeue(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 ret = tcp_v4_do_rcv(sk, skb);
Eric Dumazetc9c33212016-08-27 07:37:54 -07001724 } else if (tcp_add_backlog(sk, skb)) {
Zhu Yi6b03a532010-03-04 18:01:41 +00001725 goto discard_and_relse;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 bh_unlock_sock(sk);
1728
Eric Dumazete994b2f2015-10-02 11:43:39 -07001729put_and_return:
Eric Dumazet3b24d852016-04-01 08:52:17 -07001730 if (refcounted)
1731 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 return ret;
1734
1735no_tcp_socket:
1736 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1737 goto discard_it;
1738
Eric Dumazet12e25e12015-06-03 23:49:21 -07001739 if (tcp_checksum_complete(skb)) {
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001740csum_error:
Eric Dumazet90bbcc62016-04-27 16:44:32 -07001741 __TCP_INC_STATS(net, TCP_MIB_CSUMERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742bad_packet:
Eric Dumazet90bbcc62016-04-27 16:44:32 -07001743 __TCP_INC_STATS(net, TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001745 tcp_v4_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747
1748discard_it:
1749 /* Discard frame. */
1750 kfree_skb(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753discard_and_relse:
Eric Dumazet532182c2016-04-01 08:52:19 -07001754 sk_drops_add(sk, skb);
Eric Dumazet3b24d852016-04-01 08:52:17 -07001755 if (refcounted)
1756 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 goto discard_it;
1758
1759do_time_wait:
1760 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001761 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto discard_it;
1763 }
1764
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001765 if (tcp_checksum_complete(skb)) {
1766 inet_twsk_put(inet_twsk(sk));
1767 goto csum_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001769 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 case TCP_TW_SYN: {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001771 struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
Craig Galleka5836362016-02-10 11:50:38 -05001772 &tcp_hashinfo, skb,
1773 __tcp_hdrlen(th),
Tom Herbertda5e3632013-01-22 09:50:24 +00001774 iph->saddr, th->source,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001775 iph->daddr, th->dest,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001776 inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (sk2) {
Eric Dumazetdbe7faa2015-07-08 14:28:30 -07001778 inet_twsk_deschedule_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 sk = sk2;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001780 refcounted = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 goto process;
1782 }
1783 /* Fall through to ACK */
1784 }
1785 case TCP_TW_ACK:
1786 tcp_v4_timewait_ack(sk, skb);
1787 break;
1788 case TCP_TW_RST:
Florian Westphal271c3b92015-12-21 21:29:26 +01001789 tcp_v4_send_reset(sk, skb);
1790 inet_twsk_deschedule_put(inet_twsk(sk));
1791 goto discard_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 case TCP_TW_SUCCESS:;
1793 }
1794 goto discard_it;
1795}
1796
David S. Millerccb7c412010-12-01 18:09:13 -08001797static struct timewait_sock_ops tcp_timewait_sock_ops = {
1798 .twsk_obj_size = sizeof(struct tcp_timewait_sock),
1799 .twsk_unique = tcp_twsk_unique,
1800 .twsk_destructor= tcp_twsk_destructor,
David S. Millerccb7c412010-12-01 18:09:13 -08001801};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Eric Dumazet63d02d12012-08-09 14:11:00 +00001803void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
Eric Dumazet5d299f32012-08-06 05:09:33 +00001804{
1805 struct dst_entry *dst = skb_dst(skb);
1806
Eric Dumazet5037e9e2015-12-14 14:08:53 -08001807 if (dst && dst_hold_safe(dst)) {
Eric Dumazetca777ef2014-09-08 08:06:07 -07001808 sk->sk_rx_dst = dst;
1809 inet_sk(sk)->rx_dst_ifindex = skb->skb_iif;
1810 }
Eric Dumazet5d299f32012-08-06 05:09:33 +00001811}
Eric Dumazet63d02d12012-08-09 14:11:00 +00001812EXPORT_SYMBOL(inet_sk_rx_dst_set);
Eric Dumazet5d299f32012-08-06 05:09:33 +00001813
Stephen Hemminger3b401a82009-09-01 19:25:04 +00001814const struct inet_connection_sock_af_ops ipv4_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001815 .queue_xmit = ip_queue_xmit,
1816 .send_check = tcp_v4_send_check,
1817 .rebuild_header = inet_sk_rebuild_header,
Eric Dumazet5d299f32012-08-06 05:09:33 +00001818 .sk_rx_dst_set = inet_sk_rx_dst_set,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001819 .conn_request = tcp_v4_conn_request,
1820 .syn_recv_sock = tcp_v4_syn_recv_sock,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001821 .net_header_len = sizeof(struct iphdr),
1822 .setsockopt = ip_setsockopt,
1823 .getsockopt = ip_getsockopt,
1824 .addr2sockaddr = inet_csk_addr2sockaddr,
1825 .sockaddr_len = sizeof(struct sockaddr_in),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001826 .bind_conflict = inet_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001827#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001828 .compat_setsockopt = compat_ip_setsockopt,
1829 .compat_getsockopt = compat_ip_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001830#endif
Neal Cardwell4fab9072014-08-14 12:40:05 -04001831 .mtu_reduced = tcp_v4_mtu_reduced,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832};
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001833EXPORT_SYMBOL(ipv4_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001835#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001836static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001837 .md5_lookup = tcp_v4_md5_lookup,
Adam Langley49a72df2008-07-19 00:01:42 -07001838 .calc_md5_hash = tcp_v4_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001839 .md5_parse = tcp_v4_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001840};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001841#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843/* NOTE: A lot of things set to zero explicitly by call to
1844 * sk_alloc() so need not be done here.
1845 */
1846static int tcp_v4_init_sock(struct sock *sk)
1847{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001848 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Neal Cardwell900f65d2012-04-19 09:55:21 +00001850 tcp_init_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001852 icsk->icsk_af_ops = &ipv4_specific;
Neal Cardwell900f65d2012-04-19 09:55:21 +00001853
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001854#ifdef CONFIG_TCP_MD5SIG
David S. Millerac807fa2012-04-23 03:21:58 -04001855 tcp_sk(sk)->af_specific = &tcp_sock_ipv4_specific;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001856#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return 0;
1859}
1860
Brian Haley7d06b2e2008-06-14 17:04:49 -07001861void tcp_v4_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct tcp_sock *tp = tcp_sk(sk);
1864
1865 tcp_clear_xmit_timers(sk);
1866
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001867 tcp_cleanup_congestion_control(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* Cleanup up the write buffer. */
David S. Millerfe067e82007-03-07 12:12:44 -08001870 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 /* Cleans up our, hopefully empty, out_of_order_queue. */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07001873 skb_rbtree_purge(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001875#ifdef CONFIG_TCP_MD5SIG
1876 /* Clean up the MD5 key list, if any */
1877 if (tp->md5sig_info) {
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001878 tcp_clear_md5_list(sk);
Eric Dumazeta8afca02012-01-31 18:45:40 +00001879 kfree_rcu(tp->md5sig_info, rcu);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001880 tp->md5sig_info = NULL;
1881 }
1882#endif
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* Clean prequeue, it must be empty really */
1885 __skb_queue_purge(&tp->ucopy.prequeue);
1886
1887 /* Clean up a referenced TCP bind bucket. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001888 if (inet_csk(sk)->icsk_bind_hash)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001889 inet_put_port(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Ian Morris00db4122015-04-03 09:17:27 +01001891 BUG_ON(tp->fastopen_rsk);
William Allen Simpson435cf552009-12-02 18:17:05 +00001892
Yuchung Chengcf60af02012-07-19 06:43:09 +00001893 /* If socket is aborted during connect operation */
1894 tcp_free_fastopen_req(tp);
Eric Dumazetcd8ae852015-05-03 21:34:46 -07001895 tcp_saved_syn_free(tp);
Yuchung Chengcf60af02012-07-19 06:43:09 +00001896
Eric Dumazet777c6ae2016-05-04 15:27:29 -07001897 local_bh_disable();
Glauber Costa180d8cd2011-12-11 21:47:02 +00001898 sk_sockets_allocated_dec(sk);
Eric Dumazet777c6ae2016-05-04 15:27:29 -07001899 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901EXPORT_SYMBOL(tcp_v4_destroy_sock);
1902
1903#ifdef CONFIG_PROC_FS
1904/* Proc filesystem TCP sock list dumping. */
1905
Tom Herberta8b690f2010-06-07 00:43:42 -07001906/*
1907 * Get next listener socket follow cur. If cur is NULL, get first socket
1908 * starting from bucket given in st->bucket; when st->bucket is zero the
1909 * very first socket in the hash table is returned.
1910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911static void *listening_get_next(struct seq_file *seq, void *cur)
1912{
Jianjun Kong5799de02008-11-03 02:49:10 -08001913 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001914 struct net *net = seq_file_net(seq);
Eric Dumazet3b24d852016-04-01 08:52:17 -07001915 struct inet_listen_hashbucket *ilb;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001916 struct sock *sk = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 if (!sk) {
Eric Dumazet3b24d852016-04-01 08:52:17 -07001919get_head:
Tom Herberta8b690f2010-06-07 00:43:42 -07001920 ilb = &tcp_hashinfo.listening_hash[st->bucket];
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001921 spin_lock_bh(&ilb->lock);
Eric Dumazet3b24d852016-04-01 08:52:17 -07001922 sk = sk_head(&ilb->head);
Tom Herberta8b690f2010-06-07 00:43:42 -07001923 st->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 goto get_sk;
1925 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001926 ilb = &tcp_hashinfo.listening_hash[st->bucket];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 ++st->num;
Tom Herberta8b690f2010-06-07 00:43:42 -07001928 ++st->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Eric Dumazet3b24d852016-04-01 08:52:17 -07001930 sk = sk_next(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931get_sk:
Eric Dumazet3b24d852016-04-01 08:52:17 -07001932 sk_for_each_from(sk) {
Pavel Emelyanov8475ef92010-11-22 03:26:12 +00001933 if (!net_eq(sock_net(sk), net))
1934 continue;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001935 if (sk->sk_family == st->family)
1936 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001938 spin_unlock_bh(&ilb->lock);
Tom Herberta8b690f2010-06-07 00:43:42 -07001939 st->offset = 0;
Eric Dumazet3b24d852016-04-01 08:52:17 -07001940 if (++st->bucket < INET_LHTABLE_SIZE)
1941 goto get_head;
1942 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
1946{
Tom Herberta8b690f2010-06-07 00:43:42 -07001947 struct tcp_iter_state *st = seq->private;
1948 void *rc;
1949
1950 st->bucket = 0;
1951 st->offset = 0;
1952 rc = listening_get_next(seq, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 while (rc && *pos) {
1955 rc = listening_get_next(seq, rc);
1956 --*pos;
1957 }
1958 return rc;
1959}
1960
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001961static inline bool empty_bucket(const struct tcp_iter_state *st)
Andi Kleen6eac5602008-08-28 01:08:02 -07001962{
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001963 return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain);
Andi Kleen6eac5602008-08-28 01:08:02 -07001964}
1965
Tom Herberta8b690f2010-06-07 00:43:42 -07001966/*
1967 * Get first established socket starting from bucket given in st->bucket.
1968 * If st->bucket is zero, the very first socket in the hash is returned.
1969 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970static void *established_get_first(struct seq_file *seq)
1971{
Jianjun Kong5799de02008-11-03 02:49:10 -08001972 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001973 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 void *rc = NULL;
1975
Tom Herberta8b690f2010-06-07 00:43:42 -07001976 st->offset = 0;
1977 for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001979 struct hlist_nulls_node *node;
Eric Dumazet9db66bd2008-11-20 20:39:09 -08001980 spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Andi Kleen6eac5602008-08-28 01:08:02 -07001982 /* Lockless fast path for the common case of empty buckets */
1983 if (empty_bucket(st))
1984 continue;
1985
Eric Dumazet9db66bd2008-11-20 20:39:09 -08001986 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001987 sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07001988 if (sk->sk_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001989 !net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 continue;
1991 }
1992 rc = sk;
1993 goto out;
1994 }
Eric Dumazet9db66bd2008-11-20 20:39:09 -08001995 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997out:
1998 return rc;
1999}
2000
2001static void *established_get_next(struct seq_file *seq, void *cur)
2002{
2003 struct sock *sk = cur;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002004 struct hlist_nulls_node *node;
Jianjun Kong5799de02008-11-03 02:49:10 -08002005 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07002006 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 ++st->num;
Tom Herberta8b690f2010-06-07 00:43:42 -07002009 ++st->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002011 sk = sk_nulls_next(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002013 sk_nulls_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002014 if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002015 return sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002018 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
2019 ++st->bucket;
2020 return established_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023static void *established_get_idx(struct seq_file *seq, loff_t pos)
2024{
Tom Herberta8b690f2010-06-07 00:43:42 -07002025 struct tcp_iter_state *st = seq->private;
2026 void *rc;
2027
2028 st->bucket = 0;
2029 rc = established_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 while (rc && pos) {
2032 rc = established_get_next(seq, rc);
2033 --pos;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return rc;
2036}
2037
2038static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
2039{
2040 void *rc;
Jianjun Kong5799de02008-11-03 02:49:10 -08002041 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 st->state = TCP_SEQ_STATE_LISTENING;
2044 rc = listening_get_idx(seq, &pos);
2045
2046 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 st->state = TCP_SEQ_STATE_ESTABLISHED;
2048 rc = established_get_idx(seq, pos);
2049 }
2050
2051 return rc;
2052}
2053
Tom Herberta8b690f2010-06-07 00:43:42 -07002054static void *tcp_seek_last_pos(struct seq_file *seq)
2055{
2056 struct tcp_iter_state *st = seq->private;
2057 int offset = st->offset;
2058 int orig_num = st->num;
2059 void *rc = NULL;
2060
2061 switch (st->state) {
Tom Herberta8b690f2010-06-07 00:43:42 -07002062 case TCP_SEQ_STATE_LISTENING:
2063 if (st->bucket >= INET_LHTABLE_SIZE)
2064 break;
2065 st->state = TCP_SEQ_STATE_LISTENING;
2066 rc = listening_get_next(seq, NULL);
2067 while (offset-- && rc)
2068 rc = listening_get_next(seq, rc);
2069 if (rc)
2070 break;
2071 st->bucket = 0;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002072 st->state = TCP_SEQ_STATE_ESTABLISHED;
Tom Herberta8b690f2010-06-07 00:43:42 -07002073 /* Fallthrough */
2074 case TCP_SEQ_STATE_ESTABLISHED:
Tom Herberta8b690f2010-06-07 00:43:42 -07002075 if (st->bucket > tcp_hashinfo.ehash_mask)
2076 break;
2077 rc = established_get_first(seq);
2078 while (offset-- && rc)
2079 rc = established_get_next(seq, rc);
2080 }
2081
2082 st->num = orig_num;
2083
2084 return rc;
2085}
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
2088{
Jianjun Kong5799de02008-11-03 02:49:10 -08002089 struct tcp_iter_state *st = seq->private;
Tom Herberta8b690f2010-06-07 00:43:42 -07002090 void *rc;
2091
2092 if (*pos && *pos == st->last_pos) {
2093 rc = tcp_seek_last_pos(seq);
2094 if (rc)
2095 goto out;
2096 }
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 st->state = TCP_SEQ_STATE_LISTENING;
2099 st->num = 0;
Tom Herberta8b690f2010-06-07 00:43:42 -07002100 st->bucket = 0;
2101 st->offset = 0;
2102 rc = *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2103
2104out:
2105 st->last_pos = *pos;
2106 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
2109static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2110{
Tom Herberta8b690f2010-06-07 00:43:42 -07002111 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 void *rc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 if (v == SEQ_START_TOKEN) {
2115 rc = tcp_get_idx(seq, 0);
2116 goto out;
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 switch (st->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 case TCP_SEQ_STATE_LISTENING:
2121 rc = listening_get_next(seq, v);
2122 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 st->state = TCP_SEQ_STATE_ESTABLISHED;
Tom Herberta8b690f2010-06-07 00:43:42 -07002124 st->bucket = 0;
2125 st->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 rc = established_get_first(seq);
2127 }
2128 break;
2129 case TCP_SEQ_STATE_ESTABLISHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 rc = established_get_next(seq, v);
2131 break;
2132 }
2133out:
2134 ++*pos;
Tom Herberta8b690f2010-06-07 00:43:42 -07002135 st->last_pos = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return rc;
2137}
2138
2139static void tcp_seq_stop(struct seq_file *seq, void *v)
2140{
Jianjun Kong5799de02008-11-03 02:49:10 -08002141 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 switch (st->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 case TCP_SEQ_STATE_LISTENING:
2145 if (v != SEQ_START_TOKEN)
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002146 spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 case TCP_SEQ_STATE_ESTABLISHED:
2149 if (v)
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002150 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 break;
2152 }
2153}
2154
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002155int tcp_seq_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Al Virod9dda782013-03-31 18:16:14 -04002157 struct tcp_seq_afinfo *afinfo = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct tcp_iter_state *s;
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002159 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002161 err = seq_open_net(inode, file, &afinfo->seq_ops,
2162 sizeof(struct tcp_iter_state));
2163 if (err < 0)
2164 return err;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002165
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002166 s = ((struct seq_file *)file->private_data)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 s->family = afinfo->family;
stephen hemminger688d1942014-08-29 23:32:05 -07002168 s->last_pos = 0;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002169 return 0;
2170}
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002171EXPORT_SYMBOL(tcp_seq_open);
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002172
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002173int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 int rc = 0;
2176 struct proc_dir_entry *p;
2177
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002178 afinfo->seq_ops.start = tcp_seq_start;
2179 afinfo->seq_ops.next = tcp_seq_next;
2180 afinfo->seq_ops.stop = tcp_seq_stop;
2181
Denis V. Lunev84841c32008-05-02 04:10:08 -07002182 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002183 afinfo->seq_fops, afinfo);
Denis V. Lunev84841c32008-05-02 04:10:08 -07002184 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 rc = -ENOMEM;
2186 return rc;
2187}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002188EXPORT_SYMBOL(tcp_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002190void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Gao fengece31ff2013-02-18 01:34:56 +00002192 remove_proc_entry(afinfo->name, net->proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002194EXPORT_SYMBOL(tcp_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Eric Dumazetd4f06872015-03-12 16:44:09 -07002196static void get_openreq4(const struct request_sock *req,
Eric Dumazetaa3a0c82015-10-02 11:43:30 -07002197 struct seq_file *f, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002199 const struct inet_request_sock *ireq = inet_rsk(req);
Eric Dumazetfa76ce732015-03-19 19:04:20 -07002200 long delta = req->rsk_timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002202 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002203 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 i,
Eric Dumazet634fb9792013-10-09 15:21:29 -07002205 ireq->ir_loc_addr,
Eric Dumazetd4f06872015-03-12 16:44:09 -07002206 ireq->ir_num,
Eric Dumazet634fb9792013-10-09 15:21:29 -07002207 ireq->ir_rmt_addr,
2208 ntohs(ireq->ir_rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 TCP_SYN_RECV,
2210 0, 0, /* could print option size, but that is af dependent. */
2211 1, /* timers active (only the expire timer) */
Eric Dumazeta399a802012-08-08 21:13:53 +00002212 jiffies_delta_to_clock_t(delta),
Eric Dumazete6c022a2012-10-27 23:16:46 +00002213 req->num_timeout,
Eric Dumazetaa3a0c82015-10-02 11:43:30 -07002214 from_kuid_munged(seq_user_ns(f),
2215 sock_i_uid(req->rsk_listener)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 0, /* non standard timer */
2217 0, /* open_requests have no inode */
Eric Dumazetd4f06872015-03-12 16:44:09 -07002218 0,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002219 req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
Tetsuo Handa652586d2013-11-14 14:31:57 -08002222static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 int timer_active;
2225 unsigned long timer_expires;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002226 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002227 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002228 const struct inet_sock *inet = inet_sk(sk);
Eric Dumazet0536fcc2015-09-29 07:42:52 -07002229 const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00002230 __be32 dest = inet->inet_daddr;
2231 __be32 src = inet->inet_rcv_saddr;
2232 __u16 destp = ntohs(inet->inet_dport);
2233 __u16 srcp = ntohs(inet->inet_sport);
Subash Abhinov Kasiviswanathanf65f7a42015-06-05 13:23:01 -06002234 __u8 seq_state = sk->sk_state;
Eric Dumazet49d09002009-12-03 16:06:13 -08002235 int rx_queue;
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002236 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002238 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
2239 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
2240 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002242 timer_expires = icsk->icsk_timeout;
2243 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002245 timer_expires = icsk->icsk_timeout;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002246 } else if (timer_pending(&sk->sk_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 timer_active = 2;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002248 timer_expires = sk->sk_timer.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 } else {
2250 timer_active = 0;
2251 timer_expires = jiffies;
2252 }
2253
Subash Abhinov Kasiviswanathanf65f7a42015-06-05 13:23:01 -06002254 if (inet->transparent)
2255 seq_state |= 0x80;
2256
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002257 state = sk_state_load(sk);
2258 if (state == TCP_LISTEN)
Eric Dumazet49d09002009-12-03 16:06:13 -08002259 rx_queue = sk->sk_ack_backlog;
2260 else
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002261 /* Because we don't lock the socket,
2262 * we might find a transient negative value.
Eric Dumazet49d09002009-12-03 16:06:13 -08002263 */
2264 rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
2265
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002266 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
Tetsuo Handa652586d2013-11-14 14:31:57 -08002267 "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d",
Subash Abhinov Kasiviswanathanf65f7a42015-06-05 13:23:01 -06002268 i, src, srcp, dest, destp, seq_state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07002269 tp->write_seq - tp->snd_una,
Eric Dumazet49d09002009-12-03 16:06:13 -08002270 rx_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 timer_active,
Eric Dumazeta399a802012-08-08 21:13:53 +00002272 jiffies_delta_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002273 icsk->icsk_retransmits,
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06002274 from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002275 icsk->icsk_probes_out,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002276 sock_i_ino(sk),
2277 atomic_read(&sk->sk_refcnt), sk,
Stephen Hemminger7be87352008-06-27 20:00:19 -07002278 jiffies_to_clock_t(icsk->icsk_rto),
2279 jiffies_to_clock_t(icsk->icsk_ack.ato),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002280 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 tp->snd_cwnd,
Eric Dumazet00fd38d2015-11-12 08:43:18 -08002282 state == TCP_LISTEN ?
2283 fastopenq->max_qlen :
Tetsuo Handa652586d2013-11-14 14:31:57 -08002284 (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002287static void get_timewait4_sock(const struct inet_timewait_sock *tw,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002288 struct seq_file *f, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Eric Dumazet789f5582015-04-12 18:51:09 -07002290 long delta = tw->tw_timer.expires - jiffies;
Al Viro23f33c22006-09-27 18:43:50 -07002291 __be32 dest, src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 __u16 destp, srcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 dest = tw->tw_daddr;
2295 src = tw->tw_rcv_saddr;
2296 destp = ntohs(tw->tw_dport);
2297 srcp = ntohs(tw->tw_sport);
2298
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002299 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
Tetsuo Handa652586d2013-11-14 14:31:57 -08002300 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
Eric Dumazeta399a802012-08-08 21:13:53 +00002302 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
Tetsuo Handa652586d2013-11-14 14:31:57 -08002303 atomic_read(&tw->tw_refcnt), tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306#define TMPSZ 150
2307
2308static int tcp4_seq_show(struct seq_file *seq, void *v)
2309{
Jianjun Kong5799de02008-11-03 02:49:10 -08002310 struct tcp_iter_state *st;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07002311 struct sock *sk = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Tetsuo Handa652586d2013-11-14 14:31:57 -08002313 seq_setwidth(seq, TMPSZ - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (v == SEQ_START_TOKEN) {
Tetsuo Handa652586d2013-11-14 14:31:57 -08002315 seq_puts(seq, " sl local_address rem_address st tx_queue "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 "rx_queue tr tm->when retrnsmt uid timeout "
2317 "inode");
2318 goto out;
2319 }
2320 st = seq->private;
2321
Eric Dumazet079096f2015-10-02 11:43:32 -07002322 if (sk->sk_state == TCP_TIME_WAIT)
2323 get_timewait4_sock(v, seq, st->num);
2324 else if (sk->sk_state == TCP_NEW_SYN_RECV)
Eric Dumazetaa3a0c82015-10-02 11:43:30 -07002325 get_openreq4(v, seq, st->num);
Eric Dumazet079096f2015-10-02 11:43:32 -07002326 else
2327 get_tcp4_sock(v, seq, st->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328out:
Tetsuo Handa652586d2013-11-14 14:31:57 -08002329 seq_pad(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return 0;
2331}
2332
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002333static const struct file_operations tcp_afinfo_seq_fops = {
2334 .owner = THIS_MODULE,
2335 .open = tcp_seq_open,
2336 .read = seq_read,
2337 .llseek = seq_lseek,
2338 .release = seq_release_net
2339};
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341static struct tcp_seq_afinfo tcp4_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 .name = "tcp",
2343 .family = AF_INET,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002344 .seq_fops = &tcp_afinfo_seq_fops,
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002345 .seq_ops = {
2346 .show = tcp4_seq_show,
2347 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348};
2349
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002350static int __net_init tcp4_proc_init_net(struct net *net)
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002351{
2352 return tcp_proc_register(net, &tcp4_seq_afinfo);
2353}
2354
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002355static void __net_exit tcp4_proc_exit_net(struct net *net)
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002356{
2357 tcp_proc_unregister(net, &tcp4_seq_afinfo);
2358}
2359
2360static struct pernet_operations tcp4_net_ops = {
2361 .init = tcp4_proc_init_net,
2362 .exit = tcp4_proc_exit_net,
2363};
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365int __init tcp4_proc_init(void)
2366{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002367 return register_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
2370void tcp4_proc_exit(void)
2371{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002372 unregister_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374#endif /* CONFIG_PROC_FS */
2375
2376struct proto tcp_prot = {
2377 .name = "TCP",
2378 .owner = THIS_MODULE,
2379 .close = tcp_close,
2380 .connect = tcp_v4_connect,
2381 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002382 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 .ioctl = tcp_ioctl,
2384 .init = tcp_v4_init_sock,
2385 .destroy = tcp_v4_destroy_sock,
2386 .shutdown = tcp_shutdown,
2387 .setsockopt = tcp_setsockopt,
2388 .getsockopt = tcp_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 .recvmsg = tcp_recvmsg,
Changli Gao7ba42912010-07-10 20:41:55 +00002390 .sendmsg = tcp_sendmsg,
2391 .sendpage = tcp_sendpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 .backlog_rcv = tcp_v4_do_rcv,
Eric Dumazet46d3cea2012-07-11 05:50:31 +00002393 .release_cb = tcp_release_cb,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08002394 .hash = inet_hash,
2395 .unhash = inet_unhash,
2396 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 .enter_memory_pressure = tcp_enter_memory_pressure,
Eric Dumazetc9bee3b72013-07-22 20:27:07 -07002398 .stream_memory_free = tcp_stream_memory_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 .sockets_allocated = &tcp_sockets_allocated,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07002400 .orphan_count = &tcp_orphan_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 .memory_allocated = &tcp_memory_allocated,
2402 .memory_pressure = &tcp_memory_pressure,
Eric W. Biedermana4fe34b2013-10-19 16:25:36 -07002403 .sysctl_mem = sysctl_tcp_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 .sysctl_wmem = sysctl_tcp_wmem,
2405 .sysctl_rmem = sysctl_tcp_rmem,
2406 .max_header = MAX_TCP_HEADER,
2407 .obj_size = sizeof(struct tcp_sock),
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002408 .slab_flags = SLAB_DESTROY_BY_RCU,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002409 .twsk_prot = &tcp_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002410 .rsk_prot = &tcp_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07002411 .h.hashinfo = &tcp_hashinfo,
Changli Gao7ba42912010-07-10 20:41:55 +00002412 .no_autobind = true,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002413#ifdef CONFIG_COMPAT
2414 .compat_setsockopt = compat_tcp_setsockopt,
2415 .compat_getsockopt = compat_tcp_getsockopt,
2416#endif
Lorenzo Colittic1e64e22015-12-16 12:30:05 +09002417 .diag_destroy = tcp_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418};
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002419EXPORT_SYMBOL(tcp_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Denis V. Lunev046ee902008-04-03 14:31:33 -07002421static void __net_exit tcp_sk_exit(struct net *net)
2422{
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002423 int cpu;
2424
2425 for_each_possible_cpu(cpu)
2426 inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.tcp_sk, cpu));
2427 free_percpu(net->ipv4.tcp_sk);
2428}
2429
2430static int __net_init tcp_sk_init(struct net *net)
2431{
2432 int res, cpu;
2433
2434 net->ipv4.tcp_sk = alloc_percpu(struct sock *);
2435 if (!net->ipv4.tcp_sk)
2436 return -ENOMEM;
2437
2438 for_each_possible_cpu(cpu) {
2439 struct sock *sk;
2440
2441 res = inet_ctl_sock_create(&sk, PF_INET, SOCK_RAW,
2442 IPPROTO_TCP, net);
2443 if (res)
2444 goto fail;
Eric Dumazeta9d65322016-04-01 08:52:21 -07002445 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002446 *per_cpu_ptr(net->ipv4.tcp_sk, cpu) = sk;
2447 }
Daniel Borkmann49213552015-05-19 21:04:22 +02002448
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002449 net->ipv4.sysctl_tcp_ecn = 2;
Daniel Borkmann49213552015-05-19 21:04:22 +02002450 net->ipv4.sysctl_tcp_ecn_fallback = 1;
2451
Fan Dub0f9ca52015-02-10 09:53:16 +08002452 net->ipv4.sysctl_tcp_base_mss = TCP_BASE_MSS;
Fan Du6b58e0a2015-03-06 11:18:23 +08002453 net->ipv4.sysctl_tcp_probe_threshold = TCP_PROBE_THRESHOLD;
Fan Du05cbc0d2015-03-06 11:18:24 +08002454 net->ipv4.sysctl_tcp_probe_interval = TCP_PROBE_INTERVAL;
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002455
Nikolay Borisov13b287e2016-01-07 16:38:43 +02002456 net->ipv4.sysctl_tcp_keepalive_time = TCP_KEEPALIVE_TIME;
Nikolay Borisov9bd68612016-01-07 16:38:44 +02002457 net->ipv4.sysctl_tcp_keepalive_probes = TCP_KEEPALIVE_PROBES;
Nikolay Borisovb840d152016-01-07 16:38:45 +02002458 net->ipv4.sysctl_tcp_keepalive_intvl = TCP_KEEPALIVE_INTVL;
Nikolay Borisov13b287e2016-01-07 16:38:43 +02002459
Nikolay Borisov6fa25162016-02-03 09:46:49 +02002460 net->ipv4.sysctl_tcp_syn_retries = TCP_SYN_RETRIES;
Nikolay Borisov7c083ec2016-02-03 09:46:50 +02002461 net->ipv4.sysctl_tcp_synack_retries = TCP_SYNACK_RETRIES;
David S. Miller0aca7372016-02-08 04:24:33 -05002462 net->ipv4.sysctl_tcp_syncookies = 1;
Nikolay Borisov1043e252016-02-03 09:46:52 +02002463 net->ipv4.sysctl_tcp_reordering = TCP_FASTRETRANS_THRESH;
Nikolay Borisovae5c3f42016-02-03 09:46:53 +02002464 net->ipv4.sysctl_tcp_retries1 = TCP_RETR1;
Nikolay Borisovc6214a92016-02-03 09:46:54 +02002465 net->ipv4.sysctl_tcp_retries2 = TCP_RETR2;
Nikolay Borisovc402d9b2016-02-03 09:46:55 +02002466 net->ipv4.sysctl_tcp_orphan_retries = 0;
Nikolay Borisov1e579ca2016-02-03 09:46:56 +02002467 net->ipv4.sysctl_tcp_fin_timeout = TCP_FIN_TIMEOUT;
Nikolay Borisov4979f2d2016-02-03 09:46:57 +02002468 net->ipv4.sysctl_tcp_notsent_lowat = UINT_MAX;
Nikolay Borisov12ed8242016-02-03 09:46:51 +02002469
Daniel Borkmann49213552015-05-19 21:04:22 +02002470 return 0;
Eric Dumazetbdbbb852015-01-29 21:35:05 -08002471fail:
2472 tcp_sk_exit(net);
2473
2474 return res;
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002475}
2476
2477static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
2478{
2479 inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET);
Denis V. Lunev046ee902008-04-03 14:31:33 -07002480}
2481
2482static struct pernet_operations __net_initdata tcp_sk_ops = {
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002483 .init = tcp_sk_init,
2484 .exit = tcp_sk_exit,
2485 .exit_batch = tcp_sk_exit_batch,
Denis V. Lunev046ee902008-04-03 14:31:33 -07002486};
2487
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08002488void __init tcp_v4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002490 inet_hashinfo_init(&tcp_hashinfo);
Eric W. Biederman6a1b3052009-02-22 00:10:18 -08002491 if (register_pernet_subsys(&tcp_sk_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 panic("Failed to create the TCP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}