blob: 7a0062cb4ed05a189c7ca775f35221097532dabe [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>
Chris Leech1a2449a2006-05-23 18:05:53 -070075#include <net/netdma.h>
David S. Miller6e5714e2011-08-03 20:50:44 -070076#include <net/secure_seq.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000077#include <net/tcp_memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#include <linux/inet.h>
80#include <linux/ipv6.h>
81#include <linux/stddef.h>
82#include <linux/proc_fs.h>
83#include <linux/seq_file.h>
84
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080085#include <linux/crypto.h>
86#include <linux/scatterlist.h>
87
Brian Haleyab32ea52006-09-22 14:15:41 -070088int sysctl_tcp_tw_reuse __read_mostly;
89int sysctl_tcp_low_latency __read_mostly;
Eric Dumazet4bc2f182010-07-09 21:22:10 +000090EXPORT_SYMBOL(sysctl_tcp_low_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080093#ifdef CONFIG_TCP_MD5SIG
Eric Dumazeta915da9b2012-01-31 05:18:33 +000094static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
Eric Dumazet318cf7a2011-10-24 02:46:04 -040095 __be32 daddr, __be32 saddr, const struct tcphdr *th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080096#endif
97
Eric Dumazet5caea4e2008-11-20 00:40:07 -080098struct inet_hashinfo tcp_hashinfo;
Eric Dumazet4bc2f182010-07-09 21:22:10 +000099EXPORT_SYMBOL(tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400101static inline __u32 tcp_v4_init_sequence(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700103 return secure_tcp_sequence_number(ip_hdr(skb)->daddr,
104 ip_hdr(skb)->saddr,
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700105 tcp_hdr(skb)->dest,
106 tcp_hdr(skb)->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800109int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
110{
111 const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
112 struct tcp_sock *tp = tcp_sk(sk);
113
114 /* With PAWS, it is safe from the viewpoint
115 of data integrity. Even without PAWS it is safe provided sequence
116 spaces do not overlap i.e. at data rates <= 80Mbit/sec.
117
118 Actually, the idea is close to VJ's one, only timestamp cache is
119 held not per host, but per port pair and TW bucket is used as state
120 holder.
121
122 If TW bucket has been already destroyed we fall back to VJ's scheme
123 and use initial timestamp retrieved from peer table.
124 */
125 if (tcptw->tw_ts_recent_stamp &&
126 (twp == NULL || (sysctl_tcp_tw_reuse &&
James Morris9d729f72007-03-04 16:12:44 -0800127 get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800128 tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
129 if (tp->write_seq == 0)
130 tp->write_seq = 1;
131 tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
132 tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
133 sock_hold(sktw);
134 return 1;
135 }
136
137 return 0;
138}
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800139EXPORT_SYMBOL_GPL(tcp_twsk_unique);
140
Pavel Emelyanovee995282012-04-19 03:40:39 +0000141static int tcp_repair_connect(struct sock *sk)
142{
143 tcp_connect_init(sk);
144 tcp_finish_connect(sk, NULL);
145
146 return 0;
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* This will initiate an outgoing connection. */
150int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
151{
David S. Miller2d7192d2011-04-26 13:28:44 -0700152 struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct inet_sock *inet = inet_sk(sk);
154 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerdca8b082011-02-24 13:38:12 -0800155 __be16 orig_sport, orig_dport;
Al Virobada8ad2006-09-26 21:27:15 -0700156 __be32 daddr, nexthop;
David S. Millerda905bd2011-05-06 16:11:19 -0700157 struct flowi4 *fl4;
David S. Miller2d7192d2011-04-26 13:28:44 -0700158 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int err;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000160 struct ip_options_rcu *inet_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (addr_len < sizeof(struct sockaddr_in))
163 return -EINVAL;
164
165 if (usin->sin_family != AF_INET)
166 return -EAFNOSUPPORT;
167
168 nexthop = daddr = usin->sin_addr.s_addr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000169 inet_opt = rcu_dereference_protected(inet->inet_opt,
170 sock_owned_by_user(sk));
171 if (inet_opt && inet_opt->opt.srr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (!daddr)
173 return -EINVAL;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000174 nexthop = inet_opt->opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
David S. Millerdca8b082011-02-24 13:38:12 -0800177 orig_sport = inet->inet_sport;
178 orig_dport = usin->sin_port;
David S. Millerda905bd2011-05-06 16:11:19 -0700179 fl4 = &inet->cork.fl.u.ip4;
180 rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
David S. Millerb23dd4f2011-03-02 14:31:35 -0800181 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
182 IPPROTO_TCP,
183 orig_sport, orig_dport, sk, true);
184 if (IS_ERR(rt)) {
185 err = PTR_ERR(rt);
186 if (err == -ENETUNREACH)
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700187 IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800188 return err;
Wei Dong584bdf82007-05-31 22:49:28 -0700189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
192 ip_rt_put(rt);
193 return -ENETUNREACH;
194 }
195
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000196 if (!inet_opt || !inet_opt->opt.srr)
David S. Millerda905bd2011-05-06 16:11:19 -0700197 daddr = fl4->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000199 if (!inet->inet_saddr)
David S. Millerda905bd2011-05-06 16:11:19 -0700200 inet->inet_saddr = fl4->saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000201 inet->inet_rcv_saddr = inet->inet_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000203 if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Reset inherited state */
205 tp->rx_opt.ts_recent = 0;
206 tp->rx_opt.ts_recent_stamp = 0;
Pavel Emelyanovee995282012-04-19 03:40:39 +0000207 if (likely(!tp->repair))
208 tp->write_seq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -0700211 if (tcp_death_row.sysctl_tw_recycle &&
David S. Miller81166dd2012-07-10 03:14:24 -0700212 !tp->rx_opt.ts_recent_stamp && fl4->daddr == daddr)
213 tcp_fetch_timewait_stamp(sk, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000215 inet->inet_dport = usin->sin_port;
216 inet->inet_daddr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800218 inet_csk(sk)->icsk_ext_hdr_len = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000219 if (inet_opt)
220 inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000222 tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Socket identity is still unknown (sport may be zero).
225 * However we set state to SYN-SENT and not releasing socket
226 * lock select source port, enter ourselves into the hash tables and
227 * complete initialization after this.
228 */
229 tcp_set_state(sk, TCP_SYN_SENT);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800230 err = inet_hash_connect(&tcp_death_row, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (err)
232 goto failure;
233
David S. Millerda905bd2011-05-06 16:11:19 -0700234 rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
David S. Millerb23dd4f2011-03-02 14:31:35 -0800235 inet->inet_sport, inet->inet_dport, sk);
236 if (IS_ERR(rt)) {
237 err = PTR_ERR(rt);
238 rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto failure;
David S. Millerb23dd4f2011-03-02 14:31:35 -0800240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* OK, now commit destination to socket. */
Herbert Xubcd76112006-06-30 13:36:35 -0700242 sk->sk_gso_type = SKB_GSO_TCPV4;
Changli Gaod8d1f302010-06-10 23:31:35 -0700243 sk_setup_caps(sk, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Pavel Emelyanovee995282012-04-19 03:40:39 +0000245 if (!tp->write_seq && likely(!tp->repair))
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000246 tp->write_seq = secure_tcp_sequence_number(inet->inet_saddr,
247 inet->inet_daddr,
248 inet->inet_sport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 usin->sin_port);
250
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000251 inet->inet_id = tp->write_seq ^ jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Pavel Emelyanovee995282012-04-19 03:40:39 +0000253 if (likely(!tp->repair))
254 err = tcp_connect(sk);
255 else
256 err = tcp_repair_connect(sk);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 rt = NULL;
259 if (err)
260 goto failure;
261
262 return 0;
263
264failure:
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200265 /*
266 * This unhashes the socket and releases the local port,
267 * if necessary.
268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 tcp_set_state(sk, TCP_CLOSE);
270 ip_rt_put(rt);
271 sk->sk_route_caps = 0;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000272 inet->inet_dport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return err;
274}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000275EXPORT_SYMBOL(tcp_v4_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * This routine does path mtu discovery as defined in RFC1191.
279 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000280static void do_pmtu_discovery(struct sock *sk, const struct iphdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct dst_entry *dst;
283 struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
286 * send out by Linux are always <576bytes so they should go through
287 * unfragmented).
288 */
289 if (sk->sk_state == TCP_LISTEN)
290 return;
291
292 /* We don't check in the destentry if pmtu discovery is forbidden
293 * on this route. We just assume that no packet_to_big packets
294 * are send back when pmtu discovery is not active.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900295 * There is a small race when the user changes this flag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * route, but I think that's acceptable.
297 */
298 if ((dst = __sk_dst_check(sk, 0)) == NULL)
299 return;
300
301 dst->ops->update_pmtu(dst, mtu);
302
303 /* Something is about to be wrong... Remember soft error
304 * for the case, if this connection will not able to recover.
305 */
306 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
307 sk->sk_err_soft = EMSGSIZE;
308
309 mtu = dst_mtu(dst);
310
311 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800312 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 tcp_sync_mss(sk, mtu);
314
315 /* Resend the TCP packet because it's
316 * clear that the old packet has been
317 * dropped. This is the new "fast" path mtu
318 * discovery.
319 */
320 tcp_simple_retransmit(sk);
321 } /* else let the usual retransmit timer handle it */
322}
323
David S. Miller55be7a92012-07-11 21:27:49 -0700324static void do_redirect(struct sk_buff *skb, struct sock *sk)
325{
326 struct dst_entry *dst = __sk_dst_check(sk, 0);
327
David S. Miller1ed5c482012-07-12 00:41:25 -0700328 if (dst)
David S. Miller55be7a92012-07-11 21:27:49 -0700329 dst->ops->redirect(dst, skb);
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * This routine is called by the ICMP module when it gets some
334 * sort of error condition. If err < 0 then the socket should
335 * be closed and the error returned to the user. If err > 0
336 * it's just the icmp type << 8 | icmp code. After adjustment
337 * header points to the first 8 bytes of the tcp header. We need
338 * to find the appropriate port.
339 *
340 * The locking strategy used here is very "optimistic". When
341 * someone else accesses the socket the ICMP is just dropped
342 * and for some paths there is no check at all.
343 * A more general error queue to queue errors for later handling
344 * is probably better.
345 *
346 */
347
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000348void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000350 const struct iphdr *iph = (const struct iphdr *)icmp_skb->data;
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000351 struct tcphdr *th = (struct tcphdr *)(icmp_skb->data + (iph->ihl << 2));
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000352 struct inet_connection_sock *icsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct tcp_sock *tp;
354 struct inet_sock *inet;
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000355 const int type = icmp_hdr(icmp_skb)->type;
356 const int code = icmp_hdr(icmp_skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct sock *sk;
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000358 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 __u32 seq;
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000360 __u32 remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int err;
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000362 struct net *net = dev_net(icmp_skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000364 if (icmp_skb->len < (iph->ihl << 2) + 8) {
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -0700365 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return;
367 }
368
Pavel Emelyanovfd54d712008-07-14 23:01:40 -0700369 sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest,
Damian Lukowski4d1a2d92009-08-26 00:16:27 +0000370 iph->saddr, th->source, inet_iif(icmp_skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (!sk) {
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -0700372 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return;
374 }
375 if (sk->sk_state == TCP_TIME_WAIT) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -0700376 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return;
378 }
379
380 bh_lock_sock(sk);
381 /* If too many ICMPs get dropped on busy
382 * servers this needs to be solved differently.
383 */
384 if (sock_owned_by_user(sk))
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700385 NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (sk->sk_state == TCP_CLOSE)
388 goto out;
389
stephen hemminger97e3ecd12010-03-18 11:27:32 +0000390 if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
391 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
392 goto out;
393 }
394
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000395 icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 tp = tcp_sk(sk);
397 seq = ntohl(th->seq);
398 if (sk->sk_state != TCP_LISTEN &&
399 !between(seq, tp->snd_una, tp->snd_nxt)) {
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700400 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto out;
402 }
403
404 switch (type) {
David S. Miller55be7a92012-07-11 21:27:49 -0700405 case ICMP_REDIRECT:
406 do_redirect(icmp_skb, sk);
407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 case ICMP_SOURCE_QUENCH:
409 /* Just silently ignore these. */
410 goto out;
411 case ICMP_PARAMETERPROB:
412 err = EPROTO;
413 break;
414 case ICMP_DEST_UNREACH:
415 if (code > NR_ICMP_UNREACH)
416 goto out;
417
418 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
419 if (!sock_owned_by_user(sk))
420 do_pmtu_discovery(sk, iph, info);
421 goto out;
422 }
423
424 err = icmp_err_convert[code].errno;
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000425 /* check if icmp_skb allows revert of backoff
426 * (see draft-zimmermann-tcp-lcd) */
427 if (code != ICMP_NET_UNREACH && code != ICMP_HOST_UNREACH)
428 break;
429 if (seq != tp->snd_una || !icsk->icsk_retransmits ||
430 !icsk->icsk_backoff)
431 break;
432
David S. Miller8f49c272010-11-12 13:35:00 -0800433 if (sock_owned_by_user(sk))
434 break;
435
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000436 icsk->icsk_backoff--;
Jerry Chu9ad7c042011-06-08 11:08:38 +0000437 inet_csk(sk)->icsk_rto = (tp->srtt ? __tcp_set_rto(tp) :
438 TCP_TIMEOUT_INIT) << icsk->icsk_backoff;
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000439 tcp_bound_rto(sk);
440
441 skb = tcp_write_queue_head(sk);
442 BUG_ON(!skb);
443
444 remaining = icsk->icsk_rto - min(icsk->icsk_rto,
445 tcp_time_stamp - TCP_SKB_CB(skb)->when);
446
447 if (remaining) {
448 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
449 remaining, TCP_RTO_MAX);
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000450 } else {
451 /* RTO revert clocked out retransmission.
452 * Will retransmit now */
453 tcp_retransmit_timer(sk);
454 }
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457 case ICMP_TIME_EXCEEDED:
458 err = EHOSTUNREACH;
459 break;
460 default:
461 goto out;
462 }
463
464 switch (sk->sk_state) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700465 struct request_sock *req, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 case TCP_LISTEN:
467 if (sock_owned_by_user(sk))
468 goto out;
469
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700470 req = inet_csk_search_req(sk, &prev, th->dest,
471 iph->daddr, iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!req)
473 goto out;
474
475 /* ICMPs are not backlogged, hence we cannot get
476 an established socket here.
477 */
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700478 WARN_ON(req->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700480 if (seq != tcp_rsk(req)->snt_isn) {
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700481 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto out;
483 }
484
485 /*
486 * Still in SYN_RECV, just remove it silently.
487 * There is no good way to pass the error to the newly
488 * created socket, and POSIX does not want network
489 * errors returned from accept().
490 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700491 inet_csk_reqsk_queue_drop(sk, req, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto out;
493
494 case TCP_SYN_SENT:
495 case TCP_SYN_RECV: /* Cannot happen.
496 It can f.e. if SYNs crossed.
497 */
498 if (!sock_owned_by_user(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 sk->sk_err = err;
500
501 sk->sk_error_report(sk);
502
503 tcp_done(sk);
504 } else {
505 sk->sk_err_soft = err;
506 }
507 goto out;
508 }
509
510 /* If we've already connected we will keep trying
511 * until we time out, or the user gives up.
512 *
513 * rfc1122 4.2.3.9 allows to consider as hard errors
514 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
515 * but it is obsoleted by pmtu discovery).
516 *
517 * Note, that in modern internet, where routing is unreliable
518 * and in each dark corner broken firewalls sit, sending random
519 * errors ordered by their masters even this two messages finally lose
520 * their original sense (even Linux sends invalid PORT_UNREACHs)
521 *
522 * Now we are in compliance with RFCs.
523 * --ANK (980905)
524 */
525
526 inet = inet_sk(sk);
527 if (!sock_owned_by_user(sk) && inet->recverr) {
528 sk->sk_err = err;
529 sk->sk_error_report(sk);
530 } else { /* Only an error on timeout */
531 sk->sk_err_soft = err;
532 }
533
534out:
535 bh_unlock_sock(sk);
536 sock_put(sk);
537}
538
Herbert Xu419f9f82010-04-11 02:15:53 +0000539static void __tcp_v4_send_check(struct sk_buff *skb,
540 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700542 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Patrick McHardy84fa7932006-08-29 16:44:56 -0700544 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Herbert Xu419f9f82010-04-11 02:15:53 +0000545 th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700546 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800547 skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 } else {
Herbert Xu419f9f82010-04-11 02:15:53 +0000549 th->check = tcp_v4_check(skb->len, saddr, daddr,
Joe Perches07f07572008-11-19 15:44:53 -0800550 csum_partial(th,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 th->doff << 2,
552 skb->csum));
553 }
554}
555
Herbert Xu419f9f82010-04-11 02:15:53 +0000556/* This routine computes an IPv4 TCP checksum. */
Herbert Xubb296242010-04-11 02:15:55 +0000557void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
Herbert Xu419f9f82010-04-11 02:15:53 +0000558{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400559 const struct inet_sock *inet = inet_sk(sk);
Herbert Xu419f9f82010-04-11 02:15:53 +0000560
561 __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr);
562}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000563EXPORT_SYMBOL(tcp_v4_send_check);
Herbert Xu419f9f82010-04-11 02:15:53 +0000564
Herbert Xua430a432006-07-08 13:34:56 -0700565int tcp_v4_gso_send_check(struct sk_buff *skb)
566{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700567 const struct iphdr *iph;
Herbert Xua430a432006-07-08 13:34:56 -0700568 struct tcphdr *th;
569
570 if (!pskb_may_pull(skb, sizeof(*th)))
571 return -EINVAL;
572
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700573 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700574 th = tcp_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -0700575
576 th->check = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700577 skb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xu419f9f82010-04-11 02:15:53 +0000578 __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
Herbert Xua430a432006-07-08 13:34:56 -0700579 return 0;
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
583 * This routine will send an RST to the other tcp.
584 *
585 * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
586 * for reset.
587 * Answer: if a packet caused RST, it is not for a socket
588 * existing in our system, if it is matched to a socket,
589 * it is just duplicate segment or bug in other side's TCP.
590 * So that we build reply only basing on parameters
591 * arrived with segment.
592 * Exception: precedence violation. We do not implement it in any case.
593 */
594
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800595static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400597 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800598 struct {
599 struct tcphdr th;
600#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800601 __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800602#endif
603 } rep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800605#ifdef CONFIG_TCP_MD5SIG
606 struct tcp_md5sig_key *key;
Shawn Lu658ddaa2012-01-31 22:35:48 +0000607 const __u8 *hash_location = NULL;
608 unsigned char newhash[16];
609 int genhash;
610 struct sock *sk1 = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800611#endif
Pavel Emelyanova86b1e32008-07-16 20:20:58 -0700612 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* Never send a reset in response to a reset. */
615 if (th->rst)
616 return;
617
Eric Dumazet511c3f92009-06-02 05:14:27 +0000618 if (skb_rtable(skb)->rt_type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return;
620
621 /* Swap the send and the receive. */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800622 memset(&rep, 0, sizeof(rep));
623 rep.th.dest = th->source;
624 rep.th.source = th->dest;
625 rep.th.doff = sizeof(struct tcphdr) / 4;
626 rep.th.rst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (th->ack) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800629 rep.th.seq = th->ack_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800631 rep.th.ack = 1;
632 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
633 skb->len - (th->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200636 memset(&arg, 0, sizeof(arg));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800637 arg.iov[0].iov_base = (unsigned char *)&rep;
638 arg.iov[0].iov_len = sizeof(rep.th);
639
640#ifdef CONFIG_TCP_MD5SIG
Shawn Lu658ddaa2012-01-31 22:35:48 +0000641 hash_location = tcp_parse_md5sig_option(th);
642 if (!sk && hash_location) {
643 /*
644 * active side is lost. Try to find listening socket through
645 * source port, and then find md5 key through listening socket.
646 * we are not loose security here:
647 * Incoming packet is checked with md5 hash with finding key,
648 * no RST generated if md5 hash doesn't match.
649 */
650 sk1 = __inet_lookup_listener(dev_net(skb_dst(skb)->dev),
651 &tcp_hashinfo, ip_hdr(skb)->daddr,
652 ntohs(th->source), inet_iif(skb));
653 /* don't send rst if it can't find key */
654 if (!sk1)
655 return;
656 rcu_read_lock();
657 key = tcp_md5_do_lookup(sk1, (union tcp_md5_addr *)
658 &ip_hdr(skb)->saddr, AF_INET);
659 if (!key)
660 goto release_sk1;
661
662 genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, NULL, skb);
663 if (genhash || memcmp(hash_location, newhash, 16) != 0)
664 goto release_sk1;
665 } else {
666 key = sk ? tcp_md5_do_lookup(sk, (union tcp_md5_addr *)
667 &ip_hdr(skb)->saddr,
668 AF_INET) : NULL;
669 }
670
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800671 if (key) {
672 rep.opt[0] = htonl((TCPOPT_NOP << 24) |
673 (TCPOPT_NOP << 16) |
674 (TCPOPT_MD5SIG << 8) |
675 TCPOLEN_MD5SIG);
676 /* Update length and the length the header thinks exists */
677 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
678 rep.th.doff = arg.iov[0].iov_len / 4;
679
Adam Langley49a72df2008-07-19 00:01:42 -0700680 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
Ilpo Järvinen78e645cb2008-10-09 14:37:47 -0700681 key, ip_hdr(skb)->saddr,
682 ip_hdr(skb)->daddr, &rep.th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800683 }
684#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700685 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
686 ip_hdr(skb)->saddr, /* XXX */
Ilpo Järvinen52cd5752008-10-08 11:34:06 -0700687 arg.iov[0].iov_len, IPPROTO_TCP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700689 arg.flags = (sk && inet_sk(sk)->transparent) ? IP_REPLY_ARG_NOSRCCHECK : 0;
Shawn Lue2446ea2012-02-04 12:38:09 +0000690 /* When socket is gone, all binding information is lost.
691 * routing might fail in this case. using iif for oif to
692 * make sure we can deliver it
693 */
694 arg.bound_dev_if = sk ? sk->sk_bound_dev_if : inet_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric Dumazetadf30902009-06-02 05:19:30 +0000696 net = dev_net(skb_dst(skb)->dev);
Eric Dumazet66b13d92011-10-24 03:06:21 -0400697 arg.tos = ip_hdr(skb)->tos;
David S. Miller70e73412012-06-28 03:21:41 -0700698 ip_send_unicast_reply(net->ipv4.tcp_sock, skb, ip_hdr(skb)->saddr,
699 ip_hdr(skb)->daddr, &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Pavel Emelyanov63231bd2008-07-16 20:22:25 -0700701 TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
702 TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
Shawn Lu658ddaa2012-01-31 22:35:48 +0000703
704#ifdef CONFIG_TCP_MD5SIG
705release_sk1:
706 if (sk1) {
707 rcu_read_unlock();
708 sock_put(sk1);
709 }
710#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713/* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
714 outside socket context is ugly, certainly. What can I do?
715 */
716
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900717static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
718 u32 win, u32 ts, int oif,
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700719 struct tcp_md5sig_key *key,
Eric Dumazet66b13d92011-10-24 03:06:21 -0400720 int reply_flags, u8 tos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400722 const struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct {
724 struct tcphdr th;
Al Viro714e85b2006-11-14 20:51:49 -0800725 __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800726#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800727 + (TCPOLEN_MD5SIG_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800728#endif
729 ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } rep;
731 struct ip_reply_arg arg;
Eric Dumazetadf30902009-06-02 05:19:30 +0000732 struct net *net = dev_net(skb_dst(skb)->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 memset(&rep.th, 0, sizeof(struct tcphdr));
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200735 memset(&arg, 0, sizeof(arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 arg.iov[0].iov_base = (unsigned char *)&rep;
738 arg.iov[0].iov_len = sizeof(rep.th);
739 if (ts) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800740 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
741 (TCPOPT_TIMESTAMP << 8) |
742 TCPOLEN_TIMESTAMP);
743 rep.opt[1] = htonl(tcp_time_stamp);
744 rep.opt[2] = htonl(ts);
Craig Schlentercb48cfe2007-01-09 00:11:15 -0800745 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
748 /* Swap the send and the receive. */
749 rep.th.dest = th->source;
750 rep.th.source = th->dest;
751 rep.th.doff = arg.iov[0].iov_len / 4;
752 rep.th.seq = htonl(seq);
753 rep.th.ack_seq = htonl(ack);
754 rep.th.ack = 1;
755 rep.th.window = htons(win);
756
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800757#ifdef CONFIG_TCP_MD5SIG
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800758 if (key) {
759 int offset = (ts) ? 3 : 0;
760
761 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
762 (TCPOPT_NOP << 16) |
763 (TCPOPT_MD5SIG << 8) |
764 TCPOLEN_MD5SIG);
765 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
766 rep.th.doff = arg.iov[0].iov_len/4;
767
Adam Langley49a72df2008-07-19 00:01:42 -0700768 tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset],
Adam Langley90b7e112008-07-31 20:49:48 -0700769 key, ip_hdr(skb)->saddr,
770 ip_hdr(skb)->daddr, &rep.th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800771 }
772#endif
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700773 arg.flags = reply_flags;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700774 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
775 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 arg.iov[0].iov_len, IPPROTO_TCP, 0);
777 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900778 if (oif)
779 arg.bound_dev_if = oif;
Eric Dumazet66b13d92011-10-24 03:06:21 -0400780 arg.tos = tos;
David S. Miller70e73412012-06-28 03:21:41 -0700781 ip_send_unicast_reply(net->ipv4.tcp_sock, skb, ip_hdr(skb)->saddr,
782 ip_hdr(skb)->daddr, &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Pavel Emelyanov63231bd2008-07-16 20:22:25 -0700784 TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
788{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700789 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800790 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900792 tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200793 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900794 tcptw->tw_ts_recent,
795 tw->tw_bound_dev_if,
KOVACS Krisztian88ef4a52008-10-01 07:41:00 -0700796 tcp_twsk_md5_key(tcptw),
Eric Dumazet66b13d92011-10-24 03:06:21 -0400797 tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0,
798 tw->tw_tos
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900799 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700801 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Gui Jianfeng6edafaa2008-08-06 23:50:04 -0700804static void tcp_v4_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200805 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900807 tcp_v4_send_ack(skb, tcp_rsk(req)->snt_isn + 1,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800808 tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900809 req->ts_recent,
810 0,
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000811 tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&ip_hdr(skb)->daddr,
812 AF_INET),
Eric Dumazet66b13d92011-10-24 03:06:21 -0400813 inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0,
814 ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/*
Kris Katterjohn9bf1d832008-02-17 22:29:19 -0800818 * Send a SYN-ACK after having received a SYN.
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700819 * This still operates on a request_sock only, not on a big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * socket.
821 */
Octavian Purdila72659ec2010-01-17 19:09:39 -0800822static int tcp_v4_send_synack(struct sock *sk, struct dst_entry *dst,
823 struct request_sock *req,
Eric Dumazetfff32692012-06-01 01:47:50 +0000824 struct request_values *rvp,
Eric Dumazet7586ece2012-06-20 05:02:19 +0000825 u16 queue_mapping,
826 bool nocache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700828 const struct inet_request_sock *ireq = inet_rsk(req);
David S. Miller6bd023f2011-05-18 18:32:03 -0400829 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int err = -1;
831 struct sk_buff * skb;
832
833 /* First, grab a route. */
Eric Dumazet7586ece2012-06-20 05:02:19 +0000834 if (!dst && (dst = inet_csk_route_req(sk, &fl4, req, nocache)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800835 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
William Allen Simpsone6b4d112009-12-02 18:07:39 +0000837 skb = tcp_make_synack(sk, dst, req, rvp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (skb) {
Herbert Xu419f9f82010-04-11 02:15:53 +0000840 __tcp_v4_send_check(skb, ireq->loc_addr, ireq->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Eric Dumazetfff32692012-06-01 01:47:50 +0000842 skb_set_queue_mapping(skb, queue_mapping);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700843 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
844 ireq->rmt_addr,
845 ireq->opt);
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200846 err = net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return err;
850}
851
Octavian Purdila72659ec2010-01-17 19:09:39 -0800852static int tcp_v4_rtx_synack(struct sock *sk, struct request_sock *req,
William Allen Simpsone6b4d112009-12-02 18:07:39 +0000853 struct request_values *rvp)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800854{
Octavian Purdila72659ec2010-01-17 19:09:39 -0800855 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
Eric Dumazet7586ece2012-06-20 05:02:19 +0000856 return tcp_v4_send_synack(sk, NULL, req, rvp, 0, false);
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700860 * IPv4 request_sock destructor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700862static void tcp_v4_reqsk_destructor(struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Jesper Juhla51482b2005-11-08 09:41:34 -0800864 kfree(inet_rsk(req)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Eric Dumazet946cedc2011-08-30 03:21:44 +0000867/*
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000868 * Return true if a syncookie should be sent
Eric Dumazet946cedc2011-08-30 03:21:44 +0000869 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000870bool tcp_syn_flood_action(struct sock *sk,
Eric Dumazet946cedc2011-08-30 03:21:44 +0000871 const struct sk_buff *skb,
872 const char *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Eric Dumazet946cedc2011-08-30 03:21:44 +0000874 const char *msg = "Dropping request";
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000875 bool want_cookie = false;
Eric Dumazet946cedc2011-08-30 03:21:44 +0000876 struct listen_sock *lopt;
877
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Florian Westphal2a1d4bd2010-06-03 00:43:12 +0000880#ifdef CONFIG_SYN_COOKIES
Eric Dumazet946cedc2011-08-30 03:21:44 +0000881 if (sysctl_tcp_syncookies) {
Florian Westphal2a1d4bd2010-06-03 00:43:12 +0000882 msg = "Sending cookies";
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000883 want_cookie = true;
Eric Dumazet946cedc2011-08-30 03:21:44 +0000884 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
885 } else
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200886#endif
Eric Dumazet946cedc2011-08-30 03:21:44 +0000887 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
Florian Westphal2a1d4bd2010-06-03 00:43:12 +0000888
Eric Dumazet946cedc2011-08-30 03:21:44 +0000889 lopt = inet_csk(sk)->icsk_accept_queue.listen_opt;
890 if (!lopt->synflood_warned) {
891 lopt->synflood_warned = 1;
Joe Perchesafd465032012-03-12 07:03:32 +0000892 pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n",
Eric Dumazet946cedc2011-08-30 03:21:44 +0000893 proto, ntohs(tcp_hdr(skb)->dest), msg);
894 }
895 return want_cookie;
Florian Westphal2a1d4bd2010-06-03 00:43:12 +0000896}
Eric Dumazet946cedc2011-08-30 03:21:44 +0000897EXPORT_SYMBOL(tcp_syn_flood_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700900 * Save and compile IPv4 options into the request_sock if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000902static struct ip_options_rcu *tcp_v4_save_options(struct sock *sk,
903 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000905 const struct ip_options *opt = &(IPCB(skb)->opt);
906 struct ip_options_rcu *dopt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (opt && opt->optlen) {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000909 int opt_size = sizeof(*dopt) + opt->optlen;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 dopt = kmalloc(opt_size, GFP_ATOMIC);
912 if (dopt) {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000913 if (ip_options_echo(&dopt->opt, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 kfree(dopt);
915 dopt = NULL;
916 }
917 }
918 }
919 return dopt;
920}
921
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800922#ifdef CONFIG_TCP_MD5SIG
923/*
924 * RFC2385 MD5 checksumming requires a mapping of
925 * IP address->MD5 Key.
926 * We need to maintain these in the sk structure.
927 */
928
929/* Find the Key structure for an address. */
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000930struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
931 const union tcp_md5_addr *addr,
932 int family)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800933{
934 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000935 struct tcp_md5sig_key *key;
936 struct hlist_node *pos;
937 unsigned int size = sizeof(struct in_addr);
Eric Dumazeta8afca02012-01-31 18:45:40 +0000938 struct tcp_md5sig_info *md5sig;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800939
Eric Dumazeta8afca02012-01-31 18:45:40 +0000940 /* caller either holds rcu_read_lock() or socket lock */
941 md5sig = rcu_dereference_check(tp->md5sig_info,
Eric Dumazetb4fb05e2012-03-07 04:45:43 +0000942 sock_owned_by_user(sk) ||
943 lockdep_is_held(&sk->sk_lock.slock));
Eric Dumazeta8afca02012-01-31 18:45:40 +0000944 if (!md5sig)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800945 return NULL;
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000946#if IS_ENABLED(CONFIG_IPV6)
947 if (family == AF_INET6)
948 size = sizeof(struct in6_addr);
949#endif
Eric Dumazeta8afca02012-01-31 18:45:40 +0000950 hlist_for_each_entry_rcu(key, pos, &md5sig->head, node) {
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000951 if (key->family != family)
952 continue;
953 if (!memcmp(&key->addr, addr, size))
954 return key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800955 }
956 return NULL;
957}
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000958EXPORT_SYMBOL(tcp_md5_do_lookup);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800959
960struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
961 struct sock *addr_sk)
962{
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000963 union tcp_md5_addr *addr;
964
965 addr = (union tcp_md5_addr *)&inet_sk(addr_sk)->inet_daddr;
966 return tcp_md5_do_lookup(sk, addr, AF_INET);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800967}
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800968EXPORT_SYMBOL(tcp_v4_md5_lookup);
969
Adrian Bunkf5b99bc2006-11-30 17:22:29 -0800970static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
971 struct request_sock *req)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800972{
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000973 union tcp_md5_addr *addr;
974
975 addr = (union tcp_md5_addr *)&inet_rsk(req)->rmt_addr;
976 return tcp_md5_do_lookup(sk, addr, AF_INET);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800977}
978
979/* This can be called on a newly created socket, from other files */
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000980int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
981 int family, const u8 *newkey, u8 newkeylen, gfp_t gfp)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800982{
983 /* Add Key to the list */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700984 struct tcp_md5sig_key *key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800985 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000986 struct tcp_md5sig_info *md5sig;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800987
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000988 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&addr, AF_INET);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800989 if (key) {
990 /* Pre-existing entry - just update that one. */
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000991 memcpy(key->key, newkey, newkeylen);
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700992 key->keylen = newkeylen;
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000993 return 0;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800994 }
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000995
Eric Dumazeta8afca02012-01-31 18:45:40 +0000996 md5sig = rcu_dereference_protected(tp->md5sig_info,
997 sock_owned_by_user(sk));
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000998 if (!md5sig) {
999 md5sig = kmalloc(sizeof(*md5sig), gfp);
1000 if (!md5sig)
1001 return -ENOMEM;
1002
1003 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
1004 INIT_HLIST_HEAD(&md5sig->head);
Eric Dumazeta8afca02012-01-31 18:45:40 +00001005 rcu_assign_pointer(tp->md5sig_info, md5sig);
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001006 }
1007
Eric Dumazet5f3d9cb2012-01-31 10:56:48 +00001008 key = sock_kmalloc(sk, sizeof(*key), gfp);
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001009 if (!key)
1010 return -ENOMEM;
1011 if (hlist_empty(&md5sig->head) && !tcp_alloc_md5sig_pool(sk)) {
Eric Dumazet5f3d9cb2012-01-31 10:56:48 +00001012 sock_kfree_s(sk, key, sizeof(*key));
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001013 return -ENOMEM;
1014 }
1015
1016 memcpy(key->key, newkey, newkeylen);
1017 key->keylen = newkeylen;
1018 key->family = family;
1019 memcpy(&key->addr, addr,
1020 (family == AF_INET6) ? sizeof(struct in6_addr) :
1021 sizeof(struct in_addr));
1022 hlist_add_head_rcu(&key->node, &md5sig->head);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001023 return 0;
1024}
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001025EXPORT_SYMBOL(tcp_md5_do_add);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001026
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001027int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int family)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001028{
1029 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001030 struct tcp_md5sig_key *key;
Eric Dumazeta8afca02012-01-31 18:45:40 +00001031 struct tcp_md5sig_info *md5sig;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001032
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001033 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&addr, AF_INET);
1034 if (!key)
1035 return -ENOENT;
1036 hlist_del_rcu(&key->node);
Eric Dumazet5f3d9cb2012-01-31 10:56:48 +00001037 atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001038 kfree_rcu(key, rcu);
Eric Dumazeta8afca02012-01-31 18:45:40 +00001039 md5sig = rcu_dereference_protected(tp->md5sig_info,
1040 sock_owned_by_user(sk));
1041 if (hlist_empty(&md5sig->head))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001042 tcp_free_md5sig_pool();
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001043 return 0;
1044}
1045EXPORT_SYMBOL(tcp_md5_do_del);
1046
1047void tcp_clear_md5_list(struct sock *sk)
1048{
1049 struct tcp_sock *tp = tcp_sk(sk);
1050 struct tcp_md5sig_key *key;
1051 struct hlist_node *pos, *n;
Eric Dumazeta8afca02012-01-31 18:45:40 +00001052 struct tcp_md5sig_info *md5sig;
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001053
Eric Dumazeta8afca02012-01-31 18:45:40 +00001054 md5sig = rcu_dereference_protected(tp->md5sig_info, 1);
1055
1056 if (!hlist_empty(&md5sig->head))
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001057 tcp_free_md5sig_pool();
Eric Dumazeta8afca02012-01-31 18:45:40 +00001058 hlist_for_each_entry_safe(key, pos, n, &md5sig->head, node) {
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001059 hlist_del_rcu(&key->node);
Eric Dumazet5f3d9cb2012-01-31 10:56:48 +00001060 atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001061 kfree_rcu(key, rcu);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001062 }
1063}
1064
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001065static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
1066 int optlen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001067{
1068 struct tcp_md5sig cmd;
1069 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001070
1071 if (optlen < sizeof(cmd))
1072 return -EINVAL;
1073
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001074 if (copy_from_user(&cmd, optval, sizeof(cmd)))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001075 return -EFAULT;
1076
1077 if (sin->sin_family != AF_INET)
1078 return -EINVAL;
1079
Eric Dumazeta8afca02012-01-31 18:45:40 +00001080 if (!cmd.tcpm_key || !cmd.tcpm_keylen)
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001081 return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
1082 AF_INET);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001083
1084 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
1085 return -EINVAL;
1086
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001087 return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
1088 AF_INET, cmd.tcpm_key, cmd.tcpm_keylen,
1089 GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001090}
1091
Adam Langley49a72df2008-07-19 00:01:42 -07001092static int tcp_v4_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
1093 __be32 daddr, __be32 saddr, int nbytes)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001094{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001095 struct tcp4_pseudohdr *bp;
Adam Langley49a72df2008-07-19 00:01:42 -07001096 struct scatterlist sg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001097
1098 bp = &hp->md5_blk.ip4;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001099
1100 /*
Adam Langley49a72df2008-07-19 00:01:42 -07001101 * 1. the TCP pseudo-header (in the order: source IP address,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001102 * destination IP address, zero-padded protocol number, and
1103 * segment length)
1104 */
1105 bp->saddr = saddr;
1106 bp->daddr = daddr;
1107 bp->pad = 0;
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001108 bp->protocol = IPPROTO_TCP;
Adam Langley49a72df2008-07-19 00:01:42 -07001109 bp->len = cpu_to_be16(nbytes);
David S. Millerc7da57a2007-10-26 00:41:21 -07001110
Adam Langley49a72df2008-07-19 00:01:42 -07001111 sg_init_one(&sg, bp, sizeof(*bp));
1112 return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
1113}
1114
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001115static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
Eric Dumazet318cf7a2011-10-24 02:46:04 -04001116 __be32 daddr, __be32 saddr, const struct tcphdr *th)
Adam Langley49a72df2008-07-19 00:01:42 -07001117{
1118 struct tcp_md5sig_pool *hp;
1119 struct hash_desc *desc;
1120
1121 hp = tcp_get_md5sig_pool();
1122 if (!hp)
1123 goto clear_hash_noput;
1124 desc = &hp->md5_desc;
1125
1126 if (crypto_hash_init(desc))
1127 goto clear_hash;
1128 if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
1129 goto clear_hash;
1130 if (tcp_md5_hash_header(hp, th))
1131 goto clear_hash;
1132 if (tcp_md5_hash_key(hp, key))
1133 goto clear_hash;
1134 if (crypto_hash_final(desc, md5_hash))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001135 goto clear_hash;
1136
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001137 tcp_put_md5sig_pool();
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001138 return 0;
Adam Langley49a72df2008-07-19 00:01:42 -07001139
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001140clear_hash:
1141 tcp_put_md5sig_pool();
1142clear_hash_noput:
1143 memset(md5_hash, 0, 16);
Adam Langley49a72df2008-07-19 00:01:42 -07001144 return 1;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001145}
1146
Adam Langley49a72df2008-07-19 00:01:42 -07001147int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
Eric Dumazet318cf7a2011-10-24 02:46:04 -04001148 const struct sock *sk, const struct request_sock *req,
1149 const struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001150{
Adam Langley49a72df2008-07-19 00:01:42 -07001151 struct tcp_md5sig_pool *hp;
1152 struct hash_desc *desc;
Eric Dumazet318cf7a2011-10-24 02:46:04 -04001153 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001154 __be32 saddr, daddr;
1155
1156 if (sk) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001157 saddr = inet_sk(sk)->inet_saddr;
1158 daddr = inet_sk(sk)->inet_daddr;
Adam Langley49a72df2008-07-19 00:01:42 -07001159 } else if (req) {
1160 saddr = inet_rsk(req)->loc_addr;
1161 daddr = inet_rsk(req)->rmt_addr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001162 } else {
Adam Langley49a72df2008-07-19 00:01:42 -07001163 const struct iphdr *iph = ip_hdr(skb);
1164 saddr = iph->saddr;
1165 daddr = iph->daddr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001166 }
Adam Langley49a72df2008-07-19 00:01:42 -07001167
1168 hp = tcp_get_md5sig_pool();
1169 if (!hp)
1170 goto clear_hash_noput;
1171 desc = &hp->md5_desc;
1172
1173 if (crypto_hash_init(desc))
1174 goto clear_hash;
1175
1176 if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
1177 goto clear_hash;
1178 if (tcp_md5_hash_header(hp, th))
1179 goto clear_hash;
1180 if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
1181 goto clear_hash;
1182 if (tcp_md5_hash_key(hp, key))
1183 goto clear_hash;
1184 if (crypto_hash_final(desc, md5_hash))
1185 goto clear_hash;
1186
1187 tcp_put_md5sig_pool();
1188 return 0;
1189
1190clear_hash:
1191 tcp_put_md5sig_pool();
1192clear_hash_noput:
1193 memset(md5_hash, 0, 16);
1194 return 1;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001195}
Adam Langley49a72df2008-07-19 00:01:42 -07001196EXPORT_SYMBOL(tcp_v4_md5_hash_skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001197
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001198static bool tcp_v4_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001199{
1200 /*
1201 * This gets called for each TCP segment that arrives
1202 * so we want to be efficient.
1203 * We have 3 drop cases:
1204 * o No MD5 hash and one expected.
1205 * o MD5 hash and we're not expecting one.
1206 * o MD5 hash and its wrong.
1207 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001208 const __u8 *hash_location = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001209 struct tcp_md5sig_key *hash_expected;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001210 const struct iphdr *iph = ip_hdr(skb);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001211 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001212 int genhash;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001213 unsigned char newhash[16];
1214
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001215 hash_expected = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&iph->saddr,
1216 AF_INET);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09001217 hash_location = tcp_parse_md5sig_option(th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001218
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001219 /* We've parsed the options - do we have a hash? */
1220 if (!hash_expected && !hash_location)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001221 return false;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001222
1223 if (hash_expected && !hash_location) {
David S. Miller785957d2008-07-30 03:03:15 -07001224 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001225 return true;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001226 }
1227
1228 if (!hash_expected && hash_location) {
David S. Miller785957d2008-07-30 03:03:15 -07001229 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001230 return true;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001231 }
1232
1233 /* Okay, so this is hash_expected and hash_location -
1234 * so we need to calculate the checksum.
1235 */
Adam Langley49a72df2008-07-19 00:01:42 -07001236 genhash = tcp_v4_md5_hash_skb(newhash,
1237 hash_expected,
1238 NULL, NULL, skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001239
1240 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001241 net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n",
1242 &iph->saddr, ntohs(th->source),
1243 &iph->daddr, ntohs(th->dest),
1244 genhash ? " tcp_v4_calc_md5_hash failed"
1245 : "");
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001246 return true;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001247 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001248 return false;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001249}
1250
1251#endif
1252
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001253struct request_sock_ops tcp_request_sock_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 .family = PF_INET,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001255 .obj_size = sizeof(struct tcp_request_sock),
Octavian Purdila72659ec2010-01-17 19:09:39 -08001256 .rtx_syn_ack = tcp_v4_rtx_synack,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001257 .send_ack = tcp_v4_reqsk_send_ack,
1258 .destructor = tcp_v4_reqsk_destructor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 .send_reset = tcp_v4_send_reset,
Octavian Purdila72659ec2010-01-17 19:09:39 -08001260 .syn_ack_timeout = tcp_syn_ack_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261};
1262
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001263#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001264static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001265 .md5_lookup = tcp_v4_reqsk_md5_lookup,
John Dykstrae3afe7b2009-07-16 05:04:51 +00001266 .calc_md5_hash = tcp_v4_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001267};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001268#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
1271{
William Allen Simpson4957faade2009-12-02 18:25:27 +00001272 struct tcp_extend_values tmp_ext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct tcp_options_received tmp_opt;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001274 const u8 *hash_location;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001275 struct request_sock *req;
William Allen Simpsone6b4d112009-12-02 18:07:39 +00001276 struct inet_request_sock *ireq;
William Allen Simpson4957faade2009-12-02 18:25:27 +00001277 struct tcp_sock *tp = tcp_sk(sk);
William Allen Simpsone6b4d112009-12-02 18:07:39 +00001278 struct dst_entry *dst = NULL;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001279 __be32 saddr = ip_hdr(skb)->saddr;
1280 __be32 daddr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 __u32 isn = TCP_SKB_CB(skb)->when;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001282 bool want_cookie = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /* Never answer to SYNs send to broadcast or multicast */
Eric Dumazet511c3f92009-06-02 05:14:27 +00001285 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 goto drop;
1287
1288 /* TW buckets are converted to open requests without
1289 * limitations, they conserve resources and peer is
1290 * evidently real one.
1291 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001292 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
Eric Dumazet946cedc2011-08-30 03:21:44 +00001293 want_cookie = tcp_syn_flood_action(sk, skb, "TCP");
1294 if (!want_cookie)
1295 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
1298 /* Accept backlog is full. If we have already queued enough
1299 * of warm entries in syn queue, drop request. It is better than
1300 * clogging syn queue with openreqs with exponentially increasing
1301 * timeout.
1302 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001303 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 goto drop;
1305
Arnaldo Carvalho de Meloce4a7d02008-06-10 12:39:35 -07001306 req = inet_reqsk_alloc(&tcp_request_sock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (!req)
1308 goto drop;
1309
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001310#ifdef CONFIG_TCP_MD5SIG
1311 tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
1312#endif
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 tcp_clear_options(&tmp_opt);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +00001315 tmp_opt.mss_clamp = TCP_MSS_DEFAULT;
William Allen Simpson4957faade2009-12-02 18:25:27 +00001316 tmp_opt.user_mss = tp->rx_opt.user_mss;
David S. Millerbb5b7c12009-12-15 20:56:42 -08001317 tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
William Allen Simpson4957faade2009-12-02 18:25:27 +00001319 if (tmp_opt.cookie_plus > 0 &&
1320 tmp_opt.saw_tstamp &&
1321 !tp->rx_opt.cookie_out_never &&
1322 (sysctl_tcp_cookie_size > 0 ||
1323 (tp->cookie_values != NULL &&
1324 tp->cookie_values->cookie_desired > 0))) {
1325 u8 *c;
1326 u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
1327 int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
1328
1329 if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
1330 goto drop_and_release;
1331
1332 /* Secret recipe starts with IP addresses */
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001333 *mess++ ^= (__force u32)daddr;
1334 *mess++ ^= (__force u32)saddr;
William Allen Simpson4957faade2009-12-02 18:25:27 +00001335
1336 /* plus variable length Initiator Cookie */
1337 c = (u8 *)mess;
1338 while (l-- > 0)
1339 *c++ ^= *hash_location++;
1340
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001341 want_cookie = false; /* not our kind of cookie */
William Allen Simpson4957faade2009-12-02 18:25:27 +00001342 tmp_ext.cookie_out_never = 0; /* false */
1343 tmp_ext.cookie_plus = tmp_opt.cookie_plus;
1344 } else if (!tp->rx_opt.cookie_in_always) {
1345 /* redundant indications, but ensure initialization. */
1346 tmp_ext.cookie_out_never = 1; /* true */
1347 tmp_ext.cookie_plus = 0;
1348 } else {
1349 goto drop_and_release;
1350 }
1351 tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Florian Westphal4dfc2812008-04-10 03:12:40 -07001353 if (want_cookie && !tmp_opt.saw_tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 tcp_clear_options(&tmp_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 tcp_openreq_init(req, &tmp_opt, skb);
1358
David S. Millerbb5b7c12009-12-15 20:56:42 -08001359 ireq = inet_rsk(req);
1360 ireq->loc_addr = daddr;
1361 ireq->rmt_addr = saddr;
1362 ireq->no_srccheck = inet_sk(sk)->transparent;
1363 ireq->opt = tcp_v4_save_options(sk, skb);
1364
Paul Moore284904a2009-03-27 17:10:28 -04001365 if (security_inet_conn_request(sk, skb, req))
David S. Millerbb5b7c12009-12-15 20:56:42 -08001366 goto drop_and_free;
Paul Moore284904a2009-03-27 17:10:28 -04001367
Florian Westphal172d69e2010-06-21 11:48:45 +00001368 if (!want_cookie || tmp_opt.tstamp_ok)
Eric Dumazetbd14b1b2012-05-04 05:14:02 +00001369 TCP_ECN_create_request(req, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (want_cookie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 isn = cookie_v4_init_sequence(sk, skb, &req->mss);
Florian Westphal172d69e2010-06-21 11:48:45 +00001373 req->cookie_ts = tmp_opt.tstamp_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 } else if (!isn) {
David S. Miller6bd023f2011-05-18 18:32:03 -04001375 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* VJ's idea. We save last timestamp seen
1378 * from the destination in peer table, when entering
1379 * state TIME-WAIT, and check against it before
1380 * accepting new connection request.
1381 *
1382 * If "isn" is not zero, this request hit alive
1383 * timewait bucket, so that all the necessary checks
1384 * are made in the function processing timewait state.
1385 */
1386 if (tmp_opt.saw_tstamp &&
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07001387 tcp_death_row.sysctl_tw_recycle &&
Eric Dumazet7586ece2012-06-20 05:02:19 +00001388 (dst = inet_csk_route_req(sk, &fl4, req, want_cookie)) != NULL &&
David S. Miller81166dd2012-07-10 03:14:24 -07001389 fl4.daddr == saddr) {
1390 if (!tcp_peer_is_proven(req, dst, true)) {
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001391 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001392 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394 }
1395 /* Kill the following clause, if you dislike this way. */
1396 else if (!sysctl_tcp_syncookies &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001397 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 (sysctl_max_syn_backlog >> 2)) &&
David S. Miller81166dd2012-07-10 03:14:24 -07001399 !tcp_peer_is_proven(req, dst, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* Without syncookies last quarter of
1401 * backlog is filled with destinations,
1402 * proven to be alive.
1403 * It means that we continue to communicate
1404 * to destinations, already remembered
1405 * to the moment of synflood.
1406 */
Joe Perchesafd465032012-03-12 07:03:32 +00001407 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("drop open request from %pI4/%u\n"),
Harvey Harrison673d57e2008-10-31 00:53:57 -07001408 &saddr, ntohs(tcp_hdr(skb)->source));
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001409 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
Gerrit Renkera94f7232006-11-10 14:06:49 -08001412 isn = tcp_v4_init_sequence(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001414 tcp_rsk(req)->snt_isn = isn;
Jerry Chu9ad7c042011-06-08 11:08:38 +00001415 tcp_rsk(req)->snt_synack = tcp_time_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Octavian Purdila72659ec2010-01-17 19:09:39 -08001417 if (tcp_v4_send_synack(sk, dst, req,
Eric Dumazetfff32692012-06-01 01:47:50 +00001418 (struct request_values *)&tmp_ext,
Eric Dumazet7586ece2012-06-20 05:02:19 +00001419 skb_get_queue_mapping(skb),
1420 want_cookie) ||
William Allen Simpson4957faade2009-12-02 18:25:27 +00001421 want_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 goto drop_and_free;
1423
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001424 inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
1426
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001427drop_and_release:
1428 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429drop_and_free:
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001430 reqsk_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return 0;
1433}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001434EXPORT_SYMBOL(tcp_v4_conn_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436
1437/*
1438 * The three way handshake has completed - we got a valid synack -
1439 * now create the new socket.
1440 */
1441struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001442 struct request_sock *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct dst_entry *dst)
1444{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001445 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 struct inet_sock *newinet;
1447 struct tcp_sock *newtp;
1448 struct sock *newsk;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001449#ifdef CONFIG_TCP_MD5SIG
1450 struct tcp_md5sig_key *key;
1451#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001452 struct ip_options_rcu *inet_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (sk_acceptq_is_full(sk))
1455 goto exit_overflow;
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 newsk = tcp_create_openreq_child(sk, req, skb);
1458 if (!newsk)
Balazs Scheidler093d2822010-10-21 13:06:43 +02001459 goto exit_nonewsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Herbert Xubcd76112006-06-30 13:36:35 -07001461 newsk->sk_gso_type = SKB_GSO_TCPV4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 newtp = tcp_sk(newsk);
1464 newinet = inet_sk(newsk);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001465 ireq = inet_rsk(req);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001466 newinet->inet_daddr = ireq->rmt_addr;
1467 newinet->inet_rcv_saddr = ireq->loc_addr;
1468 newinet->inet_saddr = ireq->loc_addr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001469 inet_opt = ireq->opt;
1470 rcu_assign_pointer(newinet->inet_opt, inet_opt);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001471 ireq->opt = NULL;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001472 newinet->mc_index = inet_iif(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001473 newinet->mc_ttl = ip_hdr(skb)->ttl;
Jiri Benc4c507d22012-02-09 09:35:49 +00001474 newinet->rcv_tos = ip_hdr(skb)->tos;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001475 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001476 if (inet_opt)
1477 inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001478 newinet->inet_id = newtp->write_seq ^ jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Eric Dumazetdfd25ff2012-03-10 09:20:21 +00001480 if (!dst) {
1481 dst = inet_csk_route_child_sock(sk, newsk, req);
1482 if (!dst)
1483 goto put_and_exit;
1484 } else {
1485 /* syncookie case : see end of cookie_v4_check() */
1486 }
David S. Miller0e734412011-05-08 15:28:03 -07001487 sk_setup_caps(newsk, dst);
1488
John Heffner5d424d52006-03-20 17:53:41 -08001489 tcp_mtup_init(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 tcp_sync_mss(newsk, dst_mtu(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08001491 newtp->advmss = dst_metric_advmss(dst);
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07001492 if (tcp_sk(sk)->rx_opt.user_mss &&
1493 tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1494 newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 tcp_initialize_rcv_mss(newsk);
Jerry Chu9ad7c042011-06-08 11:08:38 +00001497 if (tcp_rsk(req)->snt_synack)
1498 tcp_valid_rtt_meas(newsk,
1499 tcp_time_stamp - tcp_rsk(req)->snt_synack);
1500 newtp->total_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001502#ifdef CONFIG_TCP_MD5SIG
1503 /* Copy over the MD5 key from the original socket */
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001504 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr,
1505 AF_INET);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001506 if (key != NULL) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001507 /*
1508 * We're using one, so create a matching key
1509 * on the newsk structure. If we fail to get
1510 * memory, then we end up not copying the key
1511 * across. Shucks.
1512 */
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001513 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newinet->inet_daddr,
1514 AF_INET, key->key, key->keylen, GFP_ATOMIC);
Eric Dumazeta4654192010-05-16 00:36:33 -07001515 sk_nocaps_add(newsk, NETIF_F_GSO_MASK);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001516 }
1517#endif
1518
David S. Miller0e734412011-05-08 15:28:03 -07001519 if (__inet_inherit_port(sk, newsk) < 0)
1520 goto put_and_exit;
Eric Dumazet9327f702009-12-04 03:46:54 +00001521 __inet_hash_nolisten(newsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 return newsk;
1524
1525exit_overflow:
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001526 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
Balazs Scheidler093d2822010-10-21 13:06:43 +02001527exit_nonewsk:
1528 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529exit:
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001530 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return NULL;
David S. Miller0e734412011-05-08 15:28:03 -07001532put_and_exit:
Eric Dumazet709e8692011-11-14 10:56:56 +00001533 tcp_clear_xmit_timers(newsk);
Eric Dumazetd8a6e652011-11-30 01:02:41 +00001534 tcp_cleanup_congestion_control(newsk);
Eric Dumazet918eb392011-11-02 12:42:56 +00001535 bh_unlock_sock(newsk);
David S. Miller0e734412011-05-08 15:28:03 -07001536 sock_put(newsk);
1537 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001539EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
1542{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001543 struct tcphdr *th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001544 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct sock *nsk;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001546 struct request_sock **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /* Find possible connection requests. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001548 struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
1549 iph->saddr, iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (req)
1551 return tcp_check_req(sk, skb, req, prev);
1552
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001553 nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001554 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 if (nsk) {
1557 if (nsk->sk_state != TCP_TIME_WAIT) {
1558 bh_lock_sock(nsk);
1559 return nsk;
1560 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001561 inet_twsk_put(inet_twsk(nsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return NULL;
1563 }
1564
1565#ifdef CONFIG_SYN_COOKIES
Florian Westphalaf9b4732010-06-03 00:43:44 +00001566 if (!th->syn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
1568#endif
1569 return sk;
1570}
1571
Al Virob51655b2006-11-14 21:40:42 -08001572static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001574 const struct iphdr *iph = ip_hdr(skb);
1575
Patrick McHardy84fa7932006-08-29 16:44:56 -07001576 if (skb->ip_summed == CHECKSUM_COMPLETE) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001577 if (!tcp_v4_check(skb->len, iph->saddr,
1578 iph->daddr, skb->csum)) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001579 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return 0;
Herbert Xufb286bb2005-11-10 13:01:24 -08001581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001583
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001584 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
Herbert Xufb286bb2005-11-10 13:01:24 -08001585 skb->len, IPPROTO_TCP, 0);
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (skb->len <= 76) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001588 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 return 0;
1591}
1592
1593
1594/* The socket must have it's spinlock held when we get
1595 * here.
1596 *
1597 * We have a potential double-lock case here, so even when
1598 * doing backlog processing we use the BH locking scheme.
1599 * This is because we cannot sleep with the original spinlock
1600 * held.
1601 */
1602int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
1603{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001604 struct sock *rsk;
1605#ifdef CONFIG_TCP_MD5SIG
1606 /*
1607 * We really want to reject the packet as early as possible
1608 * if:
1609 * o We're expecting an MD5'd packet and this is no MD5 tcp option
1610 * o There is an MD5 option and we're not expecting one
1611 */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001612 if (tcp_v4_inbound_md5_hash(sk, skb))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001613 goto discard;
1614#endif
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
Tom Herbertbdeab992011-08-14 19:45:55 +00001617 sock_rps_save_rxhash(sk, skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001618 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001619 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return 0;
1623 }
1624
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001625 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 goto csum_err;
1627
1628 if (sk->sk_state == TCP_LISTEN) {
1629 struct sock *nsk = tcp_v4_hnd_req(sk, skb);
1630 if (!nsk)
1631 goto discard;
1632
1633 if (nsk != sk) {
Tom Herbertbdeab992011-08-14 19:45:55 +00001634 sock_rps_save_rxhash(nsk, skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001635 if (tcp_child_process(sk, nsk, skb)) {
1636 rsk = nsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return 0;
1640 }
Eric Dumazetca551582010-06-03 09:03:58 +00001641 } else
Tom Herbertbdeab992011-08-14 19:45:55 +00001642 sock_rps_save_rxhash(sk, skb);
Eric Dumazetca551582010-06-03 09:03:58 +00001643
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001644 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001645 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return 0;
1649
1650reset:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001651 tcp_v4_send_reset(rsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652discard:
1653 kfree_skb(skb);
1654 /* Be careful here. If this function gets more complicated and
1655 * gcc suffers from register pressure on the x86, sk (in %ebx)
1656 * might be destroyed here. This current version compiles correctly,
1657 * but you have been warned.
1658 */
1659 return 0;
1660
1661csum_err:
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001662 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto discard;
1664}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001665EXPORT_SYMBOL(tcp_v4_do_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
David S. Miller160eb5a2012-06-27 22:01:22 -07001667void tcp_v4_early_demux(struct sk_buff *skb)
David S. Miller41063e92012-06-19 21:22:05 -07001668{
1669 struct net *net = dev_net(skb->dev);
1670 const struct iphdr *iph;
1671 const struct tcphdr *th;
David S. Millerfd62e092012-06-21 14:58:10 -07001672 struct net_device *dev;
David S. Miller41063e92012-06-19 21:22:05 -07001673 struct sock *sk;
David S. Miller41063e92012-06-19 21:22:05 -07001674
David S. Miller41063e92012-06-19 21:22:05 -07001675 if (skb->pkt_type != PACKET_HOST)
David S. Miller160eb5a2012-06-27 22:01:22 -07001676 return;
David S. Miller41063e92012-06-19 21:22:05 -07001677
1678 if (!pskb_may_pull(skb, ip_hdrlen(skb) + sizeof(struct tcphdr)))
David S. Miller160eb5a2012-06-27 22:01:22 -07001679 return;
David S. Miller41063e92012-06-19 21:22:05 -07001680
1681 iph = ip_hdr(skb);
1682 th = (struct tcphdr *) ((char *)iph + ip_hdrlen(skb));
1683
1684 if (th->doff < sizeof(struct tcphdr) / 4)
David S. Miller160eb5a2012-06-27 22:01:22 -07001685 return;
David S. Miller41063e92012-06-19 21:22:05 -07001686
1687 if (!pskb_may_pull(skb, ip_hdrlen(skb) + th->doff * 4))
David S. Miller160eb5a2012-06-27 22:01:22 -07001688 return;
David S. Miller41063e92012-06-19 21:22:05 -07001689
David S. Millerfd62e092012-06-21 14:58:10 -07001690 dev = skb->dev;
David S. Miller41063e92012-06-19 21:22:05 -07001691 sk = __inet_lookup_established(net, &tcp_hashinfo,
1692 iph->saddr, th->source,
Vijay Subramanian7011d082012-06-23 17:38:10 +00001693 iph->daddr, ntohs(th->dest),
David S. Millerfd62e092012-06-21 14:58:10 -07001694 dev->ifindex);
David S. Miller41063e92012-06-19 21:22:05 -07001695 if (sk) {
1696 skb->sk = sk;
1697 skb->destructor = sock_edemux;
1698 if (sk->sk_state != TCP_TIME_WAIT) {
1699 struct dst_entry *dst = sk->sk_rx_dst;
1700 if (dst)
1701 dst = dst_check(dst, 0);
1702 if (dst) {
David S. Millerfd62e092012-06-21 14:58:10 -07001703 struct rtable *rt = (struct rtable *) dst;
1704
David S. Miller160eb5a2012-06-27 22:01:22 -07001705 if (rt->rt_iif == dev->ifindex)
David S. Millerfd62e092012-06-21 14:58:10 -07001706 skb_dst_set_noref(skb, dst);
David S. Miller41063e92012-06-19 21:22:05 -07001707 }
1708 }
1709 }
David S. Miller41063e92012-06-19 21:22:05 -07001710}
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712/*
1713 * From tcp_input.c
1714 */
1715
1716int tcp_v4_rcv(struct sk_buff *skb)
1717{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001718 const struct iphdr *iph;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001719 const struct tcphdr *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 struct sock *sk;
1721 int ret;
Pavel Emelyanova86b1e32008-07-16 20:20:58 -07001722 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 if (skb->pkt_type != PACKET_HOST)
1725 goto discard_it;
1726
1727 /* Count it even if it's bad */
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001728 TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1731 goto discard_it;
1732
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001733 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 if (th->doff < sizeof(struct tcphdr) / 4)
1736 goto bad_packet;
1737 if (!pskb_may_pull(skb, th->doff * 4))
1738 goto discard_it;
1739
1740 /* An explanation is required here, I think.
1741 * Packet length and doff are validated by header prediction,
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001742 * provided case of th->doff==0 is eliminated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 * So, we defer the checks. */
Herbert Xu60476372007-04-09 11:59:39 -07001744 if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto bad_packet;
1746
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001747 th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001748 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1750 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1751 skb->len - th->doff * 4);
1752 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1753 TCP_SKB_CB(skb)->when = 0;
Eric Dumazetb82d1bb2011-09-27 02:20:08 -04001754 TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 TCP_SKB_CB(skb)->sacked = 0;
1756
Arnaldo Carvalho de Melo9a1f27c2008-10-07 11:41:57 -07001757 sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (!sk)
1759 goto no_tcp_socket;
1760
Eric Dumazetbb134d52010-03-09 05:55:56 +00001761process:
1762 if (sk->sk_state == TCP_TIME_WAIT)
1763 goto do_time_wait;
1764
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001765 if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
1766 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001767 goto discard_and_relse;
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001768 }
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1771 goto discard_and_relse;
Patrick McHardyb59c2702006-01-06 23:06:10 -08001772 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001774 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 goto discard_and_relse;
1776
1777 skb->dev = NULL;
1778
Ingo Molnarc6366182006-07-03 00:25:13 -07001779 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ret = 0;
1781 if (!sock_owned_by_user(sk)) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001782#ifdef CONFIG_NET_DMA
1783 struct tcp_sock *tp = tcp_sk(sk);
1784 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
Dave Jianga2bd1142012-04-04 16:10:46 -07001785 tp->ucopy.dma_chan = net_dma_find_channel();
Chris Leech1a2449a2006-05-23 18:05:53 -07001786 if (tp->ucopy.dma_chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 ret = tcp_v4_do_rcv(sk, skb);
Chris Leech1a2449a2006-05-23 18:05:53 -07001788 else
1789#endif
1790 {
1791 if (!tcp_prequeue(sk, skb))
Shan Weiae8d7f82009-05-05 01:01:29 +00001792 ret = tcp_v4_do_rcv(sk, skb);
Chris Leech1a2449a2006-05-23 18:05:53 -07001793 }
Eric Dumazetda882c12012-04-22 23:38:54 +00001794 } else if (unlikely(sk_add_backlog(sk, skb,
1795 sk->sk_rcvbuf + sk->sk_sndbuf))) {
Zhu Yi6b03a532010-03-04 18:01:41 +00001796 bh_unlock_sock(sk);
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001797 NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
Zhu Yi6b03a532010-03-04 18:01:41 +00001798 goto discard_and_relse;
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 bh_unlock_sock(sk);
1801
1802 sock_put(sk);
1803
1804 return ret;
1805
1806no_tcp_socket:
1807 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1808 goto discard_it;
1809
1810 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1811bad_packet:
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001812 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001814 tcp_v4_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816
1817discard_it:
1818 /* Discard frame. */
1819 kfree_skb(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822discard_and_relse:
1823 sock_put(sk);
1824 goto discard_it;
1825
1826do_time_wait:
1827 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001828 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 goto discard_it;
1830 }
1831
1832 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001833 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001834 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 goto discard_it;
1836 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001837 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 case TCP_TW_SYN: {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001839 struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001840 &tcp_hashinfo,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001841 iph->daddr, th->dest,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001842 inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (sk2) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001844 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
1845 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 sk = sk2;
1847 goto process;
1848 }
1849 /* Fall through to ACK */
1850 }
1851 case TCP_TW_ACK:
1852 tcp_v4_timewait_ack(sk, skb);
1853 break;
1854 case TCP_TW_RST:
1855 goto no_tcp_socket;
1856 case TCP_TW_SUCCESS:;
1857 }
1858 goto discard_it;
1859}
1860
David S. Millerccb7c412010-12-01 18:09:13 -08001861static struct timewait_sock_ops tcp_timewait_sock_ops = {
1862 .twsk_obj_size = sizeof(struct tcp_timewait_sock),
1863 .twsk_unique = tcp_twsk_unique,
1864 .twsk_destructor= tcp_twsk_destructor,
David S. Millerccb7c412010-12-01 18:09:13 -08001865};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Stephen Hemminger3b401a82009-09-01 19:25:04 +00001867const struct inet_connection_sock_af_ops ipv4_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001868 .queue_xmit = ip_queue_xmit,
1869 .send_check = tcp_v4_send_check,
1870 .rebuild_header = inet_sk_rebuild_header,
1871 .conn_request = tcp_v4_conn_request,
1872 .syn_recv_sock = tcp_v4_syn_recv_sock,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001873 .net_header_len = sizeof(struct iphdr),
1874 .setsockopt = ip_setsockopt,
1875 .getsockopt = ip_getsockopt,
1876 .addr2sockaddr = inet_csk_addr2sockaddr,
1877 .sockaddr_len = sizeof(struct sockaddr_in),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001878 .bind_conflict = inet_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001879#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001880 .compat_setsockopt = compat_ip_setsockopt,
1881 .compat_getsockopt = compat_ip_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883};
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001884EXPORT_SYMBOL(ipv4_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001886#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001887static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001888 .md5_lookup = tcp_v4_md5_lookup,
Adam Langley49a72df2008-07-19 00:01:42 -07001889 .calc_md5_hash = tcp_v4_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001890 .md5_parse = tcp_v4_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001891};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001892#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894/* NOTE: A lot of things set to zero explicitly by call to
1895 * sk_alloc() so need not be done here.
1896 */
1897static int tcp_v4_init_sock(struct sock *sk)
1898{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001899 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Neal Cardwell900f65d2012-04-19 09:55:21 +00001901 tcp_init_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001903 icsk->icsk_af_ops = &ipv4_specific;
Neal Cardwell900f65d2012-04-19 09:55:21 +00001904
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001905#ifdef CONFIG_TCP_MD5SIG
David S. Millerac807fa2012-04-23 03:21:58 -04001906 tcp_sk(sk)->af_specific = &tcp_sock_ipv4_specific;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return 0;
1910}
1911
Brian Haley7d06b2e2008-06-14 17:04:49 -07001912void tcp_v4_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 struct tcp_sock *tp = tcp_sk(sk);
1915
1916 tcp_clear_xmit_timers(sk);
1917
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001918 tcp_cleanup_congestion_control(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /* Cleanup up the write buffer. */
David S. Millerfe067e82007-03-07 12:12:44 -08001921 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 /* Cleans up our, hopefully empty, out_of_order_queue. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001924 __skb_queue_purge(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001926#ifdef CONFIG_TCP_MD5SIG
1927 /* Clean up the MD5 key list, if any */
1928 if (tp->md5sig_info) {
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001929 tcp_clear_md5_list(sk);
Eric Dumazeta8afca02012-01-31 18:45:40 +00001930 kfree_rcu(tp->md5sig_info, rcu);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001931 tp->md5sig_info = NULL;
1932 }
1933#endif
1934
Chris Leech1a2449a2006-05-23 18:05:53 -07001935#ifdef CONFIG_NET_DMA
1936 /* Cleans up our sk_async_wait_queue */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001937 __skb_queue_purge(&sk->sk_async_wait_queue);
Chris Leech1a2449a2006-05-23 18:05:53 -07001938#endif
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* Clean prequeue, it must be empty really */
1941 __skb_queue_purge(&tp->ucopy.prequeue);
1942
1943 /* Clean up a referenced TCP bind bucket. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001944 if (inet_csk(sk)->icsk_bind_hash)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001945 inet_put_port(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /*
1948 * If sendmsg cached page exists, toss it.
1949 */
1950 if (sk->sk_sndmsg_page) {
1951 __free_page(sk->sk_sndmsg_page);
1952 sk->sk_sndmsg_page = NULL;
1953 }
1954
William Allen Simpson435cf552009-12-02 18:17:05 +00001955 /* TCP Cookie Transactions */
1956 if (tp->cookie_values != NULL) {
1957 kref_put(&tp->cookie_values->kref,
1958 tcp_cookie_values_release);
1959 tp->cookie_values = NULL;
1960 }
1961
Glauber Costa180d8cd2011-12-11 21:47:02 +00001962 sk_sockets_allocated_dec(sk);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001963 sock_release_memcg(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965EXPORT_SYMBOL(tcp_v4_destroy_sock);
1966
1967#ifdef CONFIG_PROC_FS
1968/* Proc filesystem TCP sock list dumping. */
1969
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001970static inline struct inet_timewait_sock *tw_head(struct hlist_nulls_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001972 return hlist_nulls_empty(head) ? NULL :
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001973 list_entry(head->first, struct inet_timewait_sock, tw_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001976static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001978 return !is_a_nulls(tw->tw_node.next) ?
1979 hlist_nulls_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Tom Herberta8b690f2010-06-07 00:43:42 -07001982/*
1983 * Get next listener socket follow cur. If cur is NULL, get first socket
1984 * starting from bucket given in st->bucket; when st->bucket is zero the
1985 * very first socket in the hash table is returned.
1986 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987static void *listening_get_next(struct seq_file *seq, void *cur)
1988{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001989 struct inet_connection_sock *icsk;
Eric Dumazetc25eb3b2008-11-23 17:22:55 -08001990 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct sock *sk = cur;
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001992 struct inet_listen_hashbucket *ilb;
Jianjun Kong5799de02008-11-03 02:49:10 -08001993 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001994 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 if (!sk) {
Tom Herberta8b690f2010-06-07 00:43:42 -07001997 ilb = &tcp_hashinfo.listening_hash[st->bucket];
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001998 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -08001999 sk = sk_nulls_head(&ilb->head);
Tom Herberta8b690f2010-06-07 00:43:42 -07002000 st->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 goto get_sk;
2002 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002003 ilb = &tcp_hashinfo.listening_hash[st->bucket];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 ++st->num;
Tom Herberta8b690f2010-06-07 00:43:42 -07002005 ++st->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 if (st->state == TCP_SEQ_STATE_OPENREQ) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002008 struct request_sock *req = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Eric Dumazet72a3eff2006-11-16 02:30:37 -08002010 icsk = inet_csk(st->syn_wait_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 req = req->dl_next;
2012 while (1) {
2013 while (req) {
Daniel Lezcanobdccc4c2008-07-19 00:15:13 -07002014 if (req->rsk_ops->family == st->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 cur = req;
2016 goto out;
2017 }
2018 req = req->dl_next;
2019 }
Eric Dumazet72a3eff2006-11-16 02:30:37 -08002020 if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 break;
2022get_req:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002023 req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Eric Dumazet1bde5ac2010-12-23 09:32:46 -08002025 sk = sk_nulls_next(st->syn_wait_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 st->state = TCP_SEQ_STATE_LISTENING;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002027 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 } else {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002029 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002030 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
2031 if (reqsk_queue_len(&icsk->icsk_accept_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 goto start_req;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002033 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Eric Dumazet1bde5ac2010-12-23 09:32:46 -08002034 sk = sk_nulls_next(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036get_sk:
Eric Dumazetc25eb3b2008-11-23 17:22:55 -08002037 sk_nulls_for_each_from(sk, node) {
Pavel Emelyanov8475ef92010-11-22 03:26:12 +00002038 if (!net_eq(sock_net(sk), net))
2039 continue;
2040 if (sk->sk_family == st->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 cur = sk;
2042 goto out;
2043 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002044 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002045 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
2046 if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047start_req:
2048 st->uid = sock_i_uid(sk);
2049 st->syn_wait_sk = sk;
2050 st->state = TCP_SEQ_STATE_OPENREQ;
2051 st->sbucket = 0;
2052 goto get_req;
2053 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002054 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002056 spin_unlock_bh(&ilb->lock);
Tom Herberta8b690f2010-06-07 00:43:42 -07002057 st->offset = 0;
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -07002058 if (++st->bucket < INET_LHTABLE_SIZE) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002059 ilb = &tcp_hashinfo.listening_hash[st->bucket];
2060 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -08002061 sk = sk_nulls_head(&ilb->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 goto get_sk;
2063 }
2064 cur = NULL;
2065out:
2066 return cur;
2067}
2068
2069static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
2070{
Tom Herberta8b690f2010-06-07 00:43:42 -07002071 struct tcp_iter_state *st = seq->private;
2072 void *rc;
2073
2074 st->bucket = 0;
2075 st->offset = 0;
2076 rc = listening_get_next(seq, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 while (rc && *pos) {
2079 rc = listening_get_next(seq, rc);
2080 --*pos;
2081 }
2082 return rc;
2083}
2084
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002085static inline bool empty_bucket(struct tcp_iter_state *st)
Andi Kleen6eac5602008-08-28 01:08:02 -07002086{
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002087 return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain) &&
2088 hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].twchain);
Andi Kleen6eac5602008-08-28 01:08:02 -07002089}
2090
Tom Herberta8b690f2010-06-07 00:43:42 -07002091/*
2092 * Get first established socket starting from bucket given in st->bucket.
2093 * If st->bucket is zero, the very first socket in the hash is returned.
2094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095static void *established_get_first(struct seq_file *seq)
2096{
Jianjun Kong5799de02008-11-03 02:49:10 -08002097 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07002098 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 void *rc = NULL;
2100
Tom Herberta8b690f2010-06-07 00:43:42 -07002101 st->offset = 0;
2102 for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002104 struct hlist_nulls_node *node;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002105 struct inet_timewait_sock *tw;
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002106 spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Andi Kleen6eac5602008-08-28 01:08:02 -07002108 /* Lockless fast path for the common case of empty buckets */
2109 if (empty_bucket(st))
2110 continue;
2111
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002112 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002113 sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002114 if (sk->sk_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002115 !net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 continue;
2117 }
2118 rc = sk;
2119 goto out;
2120 }
2121 st->state = TCP_SEQ_STATE_TIME_WAIT;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002122 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002123 &tcp_hashinfo.ehash[st->bucket].twchain) {
Pavel Emelyanov28518fc2008-03-21 15:52:00 -07002124 if (tw->tw_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002125 !net_eq(twsk_net(tw), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 continue;
2127 }
2128 rc = tw;
2129 goto out;
2130 }
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002131 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 st->state = TCP_SEQ_STATE_ESTABLISHED;
2133 }
2134out:
2135 return rc;
2136}
2137
2138static void *established_get_next(struct seq_file *seq, void *cur)
2139{
2140 struct sock *sk = cur;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002141 struct inet_timewait_sock *tw;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002142 struct hlist_nulls_node *node;
Jianjun Kong5799de02008-11-03 02:49:10 -08002143 struct tcp_iter_state *st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07002144 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 ++st->num;
Tom Herberta8b690f2010-06-07 00:43:42 -07002147 ++st->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
2150 tw = cur;
2151 tw = tw_next(tw);
2152get_tw:
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002153 while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 tw = tw_next(tw);
2155 }
2156 if (tw) {
2157 cur = tw;
2158 goto out;
2159 }
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002160 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 st->state = TCP_SEQ_STATE_ESTABLISHED;
2162
Andi Kleen6eac5602008-08-28 01:08:02 -07002163 /* Look for next non empty bucket */
Tom Herberta8b690f2010-06-07 00:43:42 -07002164 st->offset = 0;
Eric Dumazetf373b532009-10-09 00:16:19 +00002165 while (++st->bucket <= tcp_hashinfo.ehash_mask &&
Andi Kleen6eac5602008-08-28 01:08:02 -07002166 empty_bucket(st))
2167 ;
Eric Dumazetf373b532009-10-09 00:16:19 +00002168 if (st->bucket > tcp_hashinfo.ehash_mask)
Andi Kleen6eac5602008-08-28 01:08:02 -07002169 return NULL;
2170
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002171 spin_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002172 sk = sk_nulls_head(&tcp_hashinfo.ehash[st->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 } else
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002174 sk = sk_nulls_next(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002176 sk_nulls_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002177 if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 goto found;
2179 }
2180
2181 st->state = TCP_SEQ_STATE_TIME_WAIT;
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002182 tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 goto get_tw;
2184found:
2185 cur = sk;
2186out:
2187 return cur;
2188}
2189
2190static void *established_get_idx(struct seq_file *seq, loff_t pos)
2191{
Tom Herberta8b690f2010-06-07 00:43:42 -07002192 struct tcp_iter_state *st = seq->private;
2193 void *rc;
2194
2195 st->bucket = 0;
2196 rc = established_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 while (rc && pos) {
2199 rc = established_get_next(seq, rc);
2200 --pos;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return rc;
2203}
2204
2205static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
2206{
2207 void *rc;
Jianjun Kong5799de02008-11-03 02:49:10 -08002208 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 st->state = TCP_SEQ_STATE_LISTENING;
2211 rc = listening_get_idx(seq, &pos);
2212
2213 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 st->state = TCP_SEQ_STATE_ESTABLISHED;
2215 rc = established_get_idx(seq, pos);
2216 }
2217
2218 return rc;
2219}
2220
Tom Herberta8b690f2010-06-07 00:43:42 -07002221static void *tcp_seek_last_pos(struct seq_file *seq)
2222{
2223 struct tcp_iter_state *st = seq->private;
2224 int offset = st->offset;
2225 int orig_num = st->num;
2226 void *rc = NULL;
2227
2228 switch (st->state) {
2229 case TCP_SEQ_STATE_OPENREQ:
2230 case TCP_SEQ_STATE_LISTENING:
2231 if (st->bucket >= INET_LHTABLE_SIZE)
2232 break;
2233 st->state = TCP_SEQ_STATE_LISTENING;
2234 rc = listening_get_next(seq, NULL);
2235 while (offset-- && rc)
2236 rc = listening_get_next(seq, rc);
2237 if (rc)
2238 break;
2239 st->bucket = 0;
2240 /* Fallthrough */
2241 case TCP_SEQ_STATE_ESTABLISHED:
2242 case TCP_SEQ_STATE_TIME_WAIT:
2243 st->state = TCP_SEQ_STATE_ESTABLISHED;
2244 if (st->bucket > tcp_hashinfo.ehash_mask)
2245 break;
2246 rc = established_get_first(seq);
2247 while (offset-- && rc)
2248 rc = established_get_next(seq, rc);
2249 }
2250
2251 st->num = orig_num;
2252
2253 return rc;
2254}
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
2257{
Jianjun Kong5799de02008-11-03 02:49:10 -08002258 struct tcp_iter_state *st = seq->private;
Tom Herberta8b690f2010-06-07 00:43:42 -07002259 void *rc;
2260
2261 if (*pos && *pos == st->last_pos) {
2262 rc = tcp_seek_last_pos(seq);
2263 if (rc)
2264 goto out;
2265 }
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 st->state = TCP_SEQ_STATE_LISTENING;
2268 st->num = 0;
Tom Herberta8b690f2010-06-07 00:43:42 -07002269 st->bucket = 0;
2270 st->offset = 0;
2271 rc = *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2272
2273out:
2274 st->last_pos = *pos;
2275 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2279{
Tom Herberta8b690f2010-06-07 00:43:42 -07002280 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 void *rc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 if (v == SEQ_START_TOKEN) {
2284 rc = tcp_get_idx(seq, 0);
2285 goto out;
2286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 switch (st->state) {
2289 case TCP_SEQ_STATE_OPENREQ:
2290 case TCP_SEQ_STATE_LISTENING:
2291 rc = listening_get_next(seq, v);
2292 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 st->state = TCP_SEQ_STATE_ESTABLISHED;
Tom Herberta8b690f2010-06-07 00:43:42 -07002294 st->bucket = 0;
2295 st->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 rc = established_get_first(seq);
2297 }
2298 break;
2299 case TCP_SEQ_STATE_ESTABLISHED:
2300 case TCP_SEQ_STATE_TIME_WAIT:
2301 rc = established_get_next(seq, v);
2302 break;
2303 }
2304out:
2305 ++*pos;
Tom Herberta8b690f2010-06-07 00:43:42 -07002306 st->last_pos = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return rc;
2308}
2309
2310static void tcp_seq_stop(struct seq_file *seq, void *v)
2311{
Jianjun Kong5799de02008-11-03 02:49:10 -08002312 struct tcp_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 switch (st->state) {
2315 case TCP_SEQ_STATE_OPENREQ:
2316 if (v) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002317 struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
2318 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 case TCP_SEQ_STATE_LISTENING:
2321 if (v != SEQ_START_TOKEN)
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002322 spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 break;
2324 case TCP_SEQ_STATE_TIME_WAIT:
2325 case TCP_SEQ_STATE_ESTABLISHED:
2326 if (v)
Eric Dumazet9db66bd2008-11-20 20:39:09 -08002327 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 break;
2329 }
2330}
2331
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002332int tcp_seq_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 struct tcp_iter_state *s;
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002336 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002338 err = seq_open_net(inode, file, &afinfo->seq_ops,
2339 sizeof(struct tcp_iter_state));
2340 if (err < 0)
2341 return err;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002342
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002343 s = ((struct seq_file *)file->private_data)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 s->family = afinfo->family;
Tom Herberta8b690f2010-06-07 00:43:42 -07002345 s->last_pos = 0;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002346 return 0;
2347}
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002348EXPORT_SYMBOL(tcp_seq_open);
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002349
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002350int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 int rc = 0;
2353 struct proc_dir_entry *p;
2354
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002355 afinfo->seq_ops.start = tcp_seq_start;
2356 afinfo->seq_ops.next = tcp_seq_next;
2357 afinfo->seq_ops.stop = tcp_seq_stop;
2358
Denis V. Lunev84841c32008-05-02 04:10:08 -07002359 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002360 afinfo->seq_fops, afinfo);
Denis V. Lunev84841c32008-05-02 04:10:08 -07002361 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 rc = -ENOMEM;
2363 return rc;
2364}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002365EXPORT_SYMBOL(tcp_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002367void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002369 proc_net_remove(net, afinfo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002371EXPORT_SYMBOL(tcp_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002373static void get_openreq4(const struct sock *sk, const struct request_sock *req,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002374 struct seq_file *f, int i, int uid, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002376 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 int ttd = req->expires - jiffies;
2378
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002379 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002380 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %pK%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 i,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002382 ireq->loc_addr,
Eric Dumazetc720c7e2009-10-15 06:30:45 +00002383 ntohs(inet_sk(sk)->inet_sport),
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002384 ireq->rmt_addr,
2385 ntohs(ireq->rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 TCP_SYN_RECV,
2387 0, 0, /* could print option size, but that is af dependent. */
2388 1, /* timers active (only the expire timer) */
2389 jiffies_to_clock_t(ttd),
2390 req->retrans,
2391 uid,
2392 0, /* non standard timer */
2393 0, /* open_requests have no inode */
2394 atomic_read(&sk->sk_refcnt),
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002395 req,
2396 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002399static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 int timer_active;
2402 unsigned long timer_expires;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002403 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002404 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002405 const struct inet_sock *inet = inet_sk(sk);
Eric Dumazetc720c7e2009-10-15 06:30:45 +00002406 __be32 dest = inet->inet_daddr;
2407 __be32 src = inet->inet_rcv_saddr;
2408 __u16 destp = ntohs(inet->inet_dport);
2409 __u16 srcp = ntohs(inet->inet_sport);
Eric Dumazet49d09002009-12-03 16:06:13 -08002410 int rx_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002412 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002414 timer_expires = icsk->icsk_timeout;
2415 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002417 timer_expires = icsk->icsk_timeout;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002418 } else if (timer_pending(&sk->sk_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 timer_active = 2;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002420 timer_expires = sk->sk_timer.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 } else {
2422 timer_active = 0;
2423 timer_expires = jiffies;
2424 }
2425
Eric Dumazet49d09002009-12-03 16:06:13 -08002426 if (sk->sk_state == TCP_LISTEN)
2427 rx_queue = sk->sk_ack_backlog;
2428 else
2429 /*
2430 * because we dont lock socket, we might find a transient negative value
2431 */
2432 rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
2433
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002434 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002435 "%08X %5d %8d %lu %d %pK %lu %lu %u %u %d%n",
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002436 i, src, srcp, dest, destp, sk->sk_state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07002437 tp->write_seq - tp->snd_una,
Eric Dumazet49d09002009-12-03 16:06:13 -08002438 rx_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 timer_active,
2440 jiffies_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002441 icsk->icsk_retransmits,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002442 sock_i_uid(sk),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002443 icsk->icsk_probes_out,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002444 sock_i_ino(sk),
2445 atomic_read(&sk->sk_refcnt), sk,
Stephen Hemminger7be87352008-06-27 20:00:19 -07002446 jiffies_to_clock_t(icsk->icsk_rto),
2447 jiffies_to_clock_t(icsk->icsk_ack.ato),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002448 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 tp->snd_cwnd,
Ilpo Järvinen0b6a05c2009-09-15 01:30:10 -07002450 tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002451 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002454static void get_timewait4_sock(const struct inet_timewait_sock *tw,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002455 struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Al Viro23f33c22006-09-27 18:43:50 -07002457 __be32 dest, src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 __u16 destp, srcp;
2459 int ttd = tw->tw_ttd - jiffies;
2460
2461 if (ttd < 0)
2462 ttd = 0;
2463
2464 dest = tw->tw_daddr;
2465 src = tw->tw_rcv_saddr;
2466 destp = ntohs(tw->tw_dport);
2467 srcp = ntohs(tw->tw_sport);
2468
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002469 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002470 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
2472 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002473 atomic_read(&tw->tw_refcnt), tw, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
2476#define TMPSZ 150
2477
2478static int tcp4_seq_show(struct seq_file *seq, void *v)
2479{
Jianjun Kong5799de02008-11-03 02:49:10 -08002480 struct tcp_iter_state *st;
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002481 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 if (v == SEQ_START_TOKEN) {
2484 seq_printf(seq, "%-*s\n", TMPSZ - 1,
2485 " sl local_address rem_address st tx_queue "
2486 "rx_queue tr tm->when retrnsmt uid timeout "
2487 "inode");
2488 goto out;
2489 }
2490 st = seq->private;
2491
2492 switch (st->state) {
2493 case TCP_SEQ_STATE_LISTENING:
2494 case TCP_SEQ_STATE_ESTABLISHED:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002495 get_tcp4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 break;
2497 case TCP_SEQ_STATE_OPENREQ:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002498 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 break;
2500 case TCP_SEQ_STATE_TIME_WAIT:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002501 get_timewait4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 break;
2503 }
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002504 seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505out:
2506 return 0;
2507}
2508
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002509static const struct file_operations tcp_afinfo_seq_fops = {
2510 .owner = THIS_MODULE,
2511 .open = tcp_seq_open,
2512 .read = seq_read,
2513 .llseek = seq_lseek,
2514 .release = seq_release_net
2515};
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517static struct tcp_seq_afinfo tcp4_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 .name = "tcp",
2519 .family = AF_INET,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00002520 .seq_fops = &tcp_afinfo_seq_fops,
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002521 .seq_ops = {
2522 .show = tcp4_seq_show,
2523 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524};
2525
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002526static int __net_init tcp4_proc_init_net(struct net *net)
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002527{
2528 return tcp_proc_register(net, &tcp4_seq_afinfo);
2529}
2530
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002531static void __net_exit tcp4_proc_exit_net(struct net *net)
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002532{
2533 tcp_proc_unregister(net, &tcp4_seq_afinfo);
2534}
2535
2536static struct pernet_operations tcp4_net_ops = {
2537 .init = tcp4_proc_init_net,
2538 .exit = tcp4_proc_exit_net,
2539};
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541int __init tcp4_proc_init(void)
2542{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002543 return register_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
2546void tcp4_proc_exit(void)
2547{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002548 unregister_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550#endif /* CONFIG_PROC_FS */
2551
Herbert Xubf296b12008-12-15 23:43:36 -08002552struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2553{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002554 const struct iphdr *iph = skb_gro_network_header(skb);
Herbert Xubf296b12008-12-15 23:43:36 -08002555
2556 switch (skb->ip_summed) {
2557 case CHECKSUM_COMPLETE:
Herbert Xu86911732009-01-29 14:19:50 +00002558 if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
Herbert Xubf296b12008-12-15 23:43:36 -08002559 skb->csum)) {
2560 skb->ip_summed = CHECKSUM_UNNECESSARY;
2561 break;
2562 }
2563
2564 /* fall through */
2565 case CHECKSUM_NONE:
2566 NAPI_GRO_CB(skb)->flush = 1;
2567 return NULL;
2568 }
2569
2570 return tcp_gro_receive(head, skb);
2571}
Herbert Xubf296b12008-12-15 23:43:36 -08002572
2573int tcp4_gro_complete(struct sk_buff *skb)
2574{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002575 const struct iphdr *iph = ip_hdr(skb);
Herbert Xubf296b12008-12-15 23:43:36 -08002576 struct tcphdr *th = tcp_hdr(skb);
2577
2578 th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
2579 iph->saddr, iph->daddr, 0);
2580 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
2581
2582 return tcp_gro_complete(skb);
2583}
Herbert Xubf296b12008-12-15 23:43:36 -08002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585struct proto tcp_prot = {
2586 .name = "TCP",
2587 .owner = THIS_MODULE,
2588 .close = tcp_close,
2589 .connect = tcp_v4_connect,
2590 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002591 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 .ioctl = tcp_ioctl,
2593 .init = tcp_v4_init_sock,
2594 .destroy = tcp_v4_destroy_sock,
2595 .shutdown = tcp_shutdown,
2596 .setsockopt = tcp_setsockopt,
2597 .getsockopt = tcp_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 .recvmsg = tcp_recvmsg,
Changli Gao7ba42912010-07-10 20:41:55 +00002599 .sendmsg = tcp_sendmsg,
2600 .sendpage = tcp_sendpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 .backlog_rcv = tcp_v4_do_rcv,
Eric Dumazet46d3cea2012-07-11 05:50:31 +00002602 .release_cb = tcp_release_cb,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08002603 .hash = inet_hash,
2604 .unhash = inet_unhash,
2605 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 .enter_memory_pressure = tcp_enter_memory_pressure,
2607 .sockets_allocated = &tcp_sockets_allocated,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07002608 .orphan_count = &tcp_orphan_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 .memory_allocated = &tcp_memory_allocated,
2610 .memory_pressure = &tcp_memory_pressure,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 .sysctl_wmem = sysctl_tcp_wmem,
2612 .sysctl_rmem = sysctl_tcp_rmem,
2613 .max_header = MAX_TCP_HEADER,
2614 .obj_size = sizeof(struct tcp_sock),
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08002615 .slab_flags = SLAB_DESTROY_BY_RCU,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002616 .twsk_prot = &tcp_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002617 .rsk_prot = &tcp_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07002618 .h.hashinfo = &tcp_hashinfo,
Changli Gao7ba42912010-07-10 20:41:55 +00002619 .no_autobind = true,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002620#ifdef CONFIG_COMPAT
2621 .compat_setsockopt = compat_tcp_setsockopt,
2622 .compat_getsockopt = compat_tcp_getsockopt,
2623#endif
Glauber Costad1a4c0b2011-12-11 21:47:04 +00002624#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
2625 .init_cgroup = tcp_init_cgroup,
2626 .destroy_cgroup = tcp_destroy_cgroup,
2627 .proto_cgroup = tcp_proto_cgroup,
2628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629};
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002630EXPORT_SYMBOL(tcp_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Denis V. Lunev046ee902008-04-03 14:31:33 -07002632static int __net_init tcp_sk_init(struct net *net)
2633{
2634 return inet_ctl_sock_create(&net->ipv4.tcp_sock,
2635 PF_INET, SOCK_RAW, IPPROTO_TCP, net);
2636}
2637
2638static void __net_exit tcp_sk_exit(struct net *net)
2639{
2640 inet_ctl_sock_destroy(net->ipv4.tcp_sock);
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002641}
2642
2643static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
2644{
2645 inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET);
Denis V. Lunev046ee902008-04-03 14:31:33 -07002646}
2647
2648static struct pernet_operations __net_initdata tcp_sk_ops = {
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002649 .init = tcp_sk_init,
2650 .exit = tcp_sk_exit,
2651 .exit_batch = tcp_sk_exit_batch,
Denis V. Lunev046ee902008-04-03 14:31:33 -07002652};
2653
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08002654void __init tcp_v4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
Eric Dumazet5caea4e2008-11-20 00:40:07 -08002656 inet_hashinfo_init(&tcp_hashinfo);
Eric W. Biederman6a1b3052009-02-22 00:10:18 -08002657 if (register_pernet_subsys(&tcp_sk_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 panic("Failed to create the TCP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}