blob: ca41b77f3f32c2e3b9500d9202c40b17c0d97ec5 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <linux/types.h>
55#include <linux/fcntl.h>
56#include <linux/module.h>
57#include <linux/random.h>
58#include <linux/cache.h>
59#include <linux/jhash.h>
60#include <linux/init.h>
61#include <linux/times.h>
62
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020063#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/icmp.h>
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -070065#include <net/inet_hashtables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/tcp.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030067#include <net/transp_v6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/ipv6.h>
69#include <net/inet_common.h>
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -080070#include <net/timewait_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <net/xfrm.h>
Chris Leech1a2449a2006-05-23 18:05:53 -070072#include <net/netdma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#include <linux/inet.h>
75#include <linux/ipv6.h>
76#include <linux/stddef.h>
77#include <linux/proc_fs.h>
78#include <linux/seq_file.h>
79
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080080#include <linux/crypto.h>
81#include <linux/scatterlist.h>
82
Brian Haleyab32ea52006-09-22 14:15:41 -070083int sysctl_tcp_tw_reuse __read_mostly;
84int sysctl_tcp_low_latency __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080087#ifdef CONFIG_TCP_MD5SIG
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020088static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk,
89 __be32 addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080090static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020091 __be32 saddr, __be32 daddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +090092 struct tcphdr *th, unsigned int tcplen);
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +090093#else
94static inline
95struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
96{
97 return NULL;
98}
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080099#endif
100
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700101struct inet_hashinfo __cacheline_aligned tcp_hashinfo = {
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200102 .lhash_lock = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock),
103 .lhash_users = ATOMIC_INIT(0),
104 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Gerrit Renkera94f7232006-11-10 14:06:49 -0800107static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700109 return secure_tcp_sequence_number(ip_hdr(skb)->daddr,
110 ip_hdr(skb)->saddr,
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700111 tcp_hdr(skb)->dest,
112 tcp_hdr(skb)->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800115int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
116{
117 const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
118 struct tcp_sock *tp = tcp_sk(sk);
119
120 /* With PAWS, it is safe from the viewpoint
121 of data integrity. Even without PAWS it is safe provided sequence
122 spaces do not overlap i.e. at data rates <= 80Mbit/sec.
123
124 Actually, the idea is close to VJ's one, only timestamp cache is
125 held not per host, but per port pair and TW bucket is used as state
126 holder.
127
128 If TW bucket has been already destroyed we fall back to VJ's scheme
129 and use initial timestamp retrieved from peer table.
130 */
131 if (tcptw->tw_ts_recent_stamp &&
132 (twp == NULL || (sysctl_tcp_tw_reuse &&
James Morris9d729f72007-03-04 16:12:44 -0800133 get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800134 tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
135 if (tp->write_seq == 0)
136 tp->write_seq = 1;
137 tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
138 tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
139 sock_hold(sktw);
140 return 1;
141 }
142
143 return 0;
144}
145
146EXPORT_SYMBOL_GPL(tcp_twsk_unique);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* This will initiate an outgoing connection. */
149int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
150{
151 struct inet_sock *inet = inet_sk(sk);
152 struct tcp_sock *tp = tcp_sk(sk);
153 struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
154 struct rtable *rt;
Al Virobada8ad2006-09-26 21:27:15 -0700155 __be32 daddr, nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 int tmp;
157 int err;
158
159 if (addr_len < sizeof(struct sockaddr_in))
160 return -EINVAL;
161
162 if (usin->sin_family != AF_INET)
163 return -EAFNOSUPPORT;
164
165 nexthop = daddr = usin->sin_addr.s_addr;
166 if (inet->opt && inet->opt->srr) {
167 if (!daddr)
168 return -EINVAL;
169 nexthop = inet->opt->faddr;
170 }
171
172 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
173 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
174 IPPROTO_TCP,
David S. Miller8eb90862007-02-08 02:09:21 -0800175 inet->sport, usin->sin_port, sk, 1);
Wei Dong584bdf82007-05-31 22:49:28 -0700176 if (tmp < 0) {
177 if (tmp == -ENETUNREACH)
178 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return tmp;
Wei Dong584bdf82007-05-31 22:49:28 -0700180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
183 ip_rt_put(rt);
184 return -ENETUNREACH;
185 }
186
187 if (!inet->opt || !inet->opt->srr)
188 daddr = rt->rt_dst;
189
190 if (!inet->saddr)
191 inet->saddr = rt->rt_src;
192 inet->rcv_saddr = inet->saddr;
193
194 if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) {
195 /* Reset inherited state */
196 tp->rx_opt.ts_recent = 0;
197 tp->rx_opt.ts_recent_stamp = 0;
198 tp->write_seq = 0;
199 }
200
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -0700201 if (tcp_death_row.sysctl_tw_recycle &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
203 struct inet_peer *peer = rt_get_peer(rt);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200204 /*
205 * VJ's idea. We save last timestamp seen from
206 * the destination in peer table, when entering state
207 * TIME-WAIT * and initialize rx_opt.ts_recent from it,
208 * when trying new connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200210 if (peer != NULL &&
James Morris9d729f72007-03-04 16:12:44 -0800211 peer->tcp_ts_stamp + TCP_PAWS_MSL >= get_seconds()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
213 tp->rx_opt.ts_recent = peer->tcp_ts;
214 }
215 }
216
217 inet->dport = usin->sin_port;
218 inet->daddr = daddr;
219
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800220 inet_csk(sk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (inet->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800222 inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 tp->rx_opt.mss_clamp = 536;
225
226 /* Socket identity is still unknown (sport may be zero).
227 * However we set state to SYN-SENT and not releasing socket
228 * lock select source port, enter ourselves into the hash tables and
229 * complete initialization after this.
230 */
231 tcp_set_state(sk, TCP_SYN_SENT);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800232 err = inet_hash_connect(&tcp_death_row, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (err)
234 goto failure;
235
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200236 err = ip_route_newports(&rt, IPPROTO_TCP,
237 inet->sport, inet->dport, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (err)
239 goto failure;
240
241 /* OK, now commit destination to socket. */
Herbert Xubcd76112006-06-30 13:36:35 -0700242 sk->sk_gso_type = SKB_GSO_TCPV4;
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -0700243 sk_setup_caps(sk, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 if (!tp->write_seq)
246 tp->write_seq = secure_tcp_sequence_number(inet->saddr,
247 inet->daddr,
248 inet->sport,
249 usin->sin_port);
250
251 inet->id = tp->write_seq ^ jiffies;
252
253 err = tcp_connect(sk);
254 rt = NULL;
255 if (err)
256 goto failure;
257
258 return 0;
259
260failure:
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200261 /*
262 * This unhashes the socket and releases the local port,
263 * if necessary.
264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 tcp_set_state(sk, TCP_CLOSE);
266 ip_rt_put(rt);
267 sk->sk_route_caps = 0;
268 inet->dport = 0;
269 return err;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
273 * This routine does path mtu discovery as defined in RFC1191.
274 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800275static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct dst_entry *dst;
278 struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
281 * send out by Linux are always <576bytes so they should go through
282 * unfragmented).
283 */
284 if (sk->sk_state == TCP_LISTEN)
285 return;
286
287 /* We don't check in the destentry if pmtu discovery is forbidden
288 * on this route. We just assume that no packet_to_big packets
289 * are send back when pmtu discovery is not active.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900290 * There is a small race when the user changes this flag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * route, but I think that's acceptable.
292 */
293 if ((dst = __sk_dst_check(sk, 0)) == NULL)
294 return;
295
296 dst->ops->update_pmtu(dst, mtu);
297
298 /* Something is about to be wrong... Remember soft error
299 * for the case, if this connection will not able to recover.
300 */
301 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
302 sk->sk_err_soft = EMSGSIZE;
303
304 mtu = dst_mtu(dst);
305
306 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800307 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 tcp_sync_mss(sk, mtu);
309
310 /* Resend the TCP packet because it's
311 * clear that the old packet has been
312 * dropped. This is the new "fast" path mtu
313 * discovery.
314 */
315 tcp_simple_retransmit(sk);
316 } /* else let the usual retransmit timer handle it */
317}
318
319/*
320 * This routine is called by the ICMP module when it gets some
321 * sort of error condition. If err < 0 then the socket should
322 * be closed and the error returned to the user. If err > 0
323 * it's just the icmp type << 8 | icmp code. After adjustment
324 * header points to the first 8 bytes of the tcp header. We need
325 * to find the appropriate port.
326 *
327 * The locking strategy used here is very "optimistic". When
328 * someone else accesses the socket the ICMP is just dropped
329 * and for some paths there is no check at all.
330 * A more general error queue to queue errors for later handling
331 * is probably better.
332 *
333 */
334
335void tcp_v4_err(struct sk_buff *skb, u32 info)
336{
337 struct iphdr *iph = (struct iphdr *)skb->data;
338 struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
339 struct tcp_sock *tp;
340 struct inet_sock *inet;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300341 const int type = icmp_hdr(skb)->type;
342 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct sock *sk;
344 __u32 seq;
345 int err;
Pavel Emelyanovfd54d712008-07-14 23:01:40 -0700346 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (skb->len < (iph->ihl << 2) + 8) {
349 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
350 return;
351 }
352
Pavel Emelyanovfd54d712008-07-14 23:01:40 -0700353 sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -0800354 iph->saddr, th->source, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!sk) {
356 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
357 return;
358 }
359 if (sk->sk_state == TCP_TIME_WAIT) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -0700360 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return;
362 }
363
364 bh_lock_sock(sk);
365 /* If too many ICMPs get dropped on busy
366 * servers this needs to be solved differently.
367 */
368 if (sock_owned_by_user(sk))
369 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
370
371 if (sk->sk_state == TCP_CLOSE)
372 goto out;
373
374 tp = tcp_sk(sk);
375 seq = ntohl(th->seq);
376 if (sk->sk_state != TCP_LISTEN &&
377 !between(seq, tp->snd_una, tp->snd_nxt)) {
Eric Dumazet06ca7192006-10-20 00:22:25 -0700378 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 goto out;
380 }
381
382 switch (type) {
383 case ICMP_SOURCE_QUENCH:
384 /* Just silently ignore these. */
385 goto out;
386 case ICMP_PARAMETERPROB:
387 err = EPROTO;
388 break;
389 case ICMP_DEST_UNREACH:
390 if (code > NR_ICMP_UNREACH)
391 goto out;
392
393 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
394 if (!sock_owned_by_user(sk))
395 do_pmtu_discovery(sk, iph, info);
396 goto out;
397 }
398
399 err = icmp_err_convert[code].errno;
400 break;
401 case ICMP_TIME_EXCEEDED:
402 err = EHOSTUNREACH;
403 break;
404 default:
405 goto out;
406 }
407
408 switch (sk->sk_state) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700409 struct request_sock *req, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 case TCP_LISTEN:
411 if (sock_owned_by_user(sk))
412 goto out;
413
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700414 req = inet_csk_search_req(sk, &prev, th->dest,
415 iph->daddr, iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!req)
417 goto out;
418
419 /* ICMPs are not backlogged, hence we cannot get
420 an established socket here.
421 */
422 BUG_TRAP(!req->sk);
423
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700424 if (seq != tcp_rsk(req)->snt_isn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
426 goto out;
427 }
428
429 /*
430 * Still in SYN_RECV, just remove it silently.
431 * There is no good way to pass the error to the newly
432 * created socket, and POSIX does not want network
433 * errors returned from accept().
434 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700435 inet_csk_reqsk_queue_drop(sk, req, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 goto out;
437
438 case TCP_SYN_SENT:
439 case TCP_SYN_RECV: /* Cannot happen.
440 It can f.e. if SYNs crossed.
441 */
442 if (!sock_owned_by_user(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sk->sk_err = err;
444
445 sk->sk_error_report(sk);
446
447 tcp_done(sk);
448 } else {
449 sk->sk_err_soft = err;
450 }
451 goto out;
452 }
453
454 /* If we've already connected we will keep trying
455 * until we time out, or the user gives up.
456 *
457 * rfc1122 4.2.3.9 allows to consider as hard errors
458 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
459 * but it is obsoleted by pmtu discovery).
460 *
461 * Note, that in modern internet, where routing is unreliable
462 * and in each dark corner broken firewalls sit, sending random
463 * errors ordered by their masters even this two messages finally lose
464 * their original sense (even Linux sends invalid PORT_UNREACHs)
465 *
466 * Now we are in compliance with RFCs.
467 * --ANK (980905)
468 */
469
470 inet = inet_sk(sk);
471 if (!sock_owned_by_user(sk) && inet->recverr) {
472 sk->sk_err = err;
473 sk->sk_error_report(sk);
474 } else { /* Only an error on timeout */
475 sk->sk_err_soft = err;
476 }
477
478out:
479 bh_unlock_sock(sk);
480 sock_put(sk);
481}
482
483/* This routine computes an IPv4 TCP checksum. */
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -0800484void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700487 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Patrick McHardy84fa7932006-08-29 16:44:56 -0700489 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800490 th->check = ~tcp_v4_check(len, inet->saddr,
491 inet->daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700492 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800493 skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else {
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800495 th->check = tcp_v4_check(len, inet->saddr, inet->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 csum_partial((char *)th,
497 th->doff << 2,
498 skb->csum));
499 }
500}
501
Herbert Xua430a432006-07-08 13:34:56 -0700502int tcp_v4_gso_send_check(struct sk_buff *skb)
503{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700504 const struct iphdr *iph;
Herbert Xua430a432006-07-08 13:34:56 -0700505 struct tcphdr *th;
506
507 if (!pskb_may_pull(skb, sizeof(*th)))
508 return -EINVAL;
509
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700510 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700511 th = tcp_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -0700512
513 th->check = 0;
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800514 th->check = ~tcp_v4_check(skb->len, iph->saddr, iph->daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700515 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800516 skb->csum_offset = offsetof(struct tcphdr, check);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700517 skb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xua430a432006-07-08 13:34:56 -0700518 return 0;
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/*
522 * This routine will send an RST to the other tcp.
523 *
524 * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
525 * for reset.
526 * Answer: if a packet caused RST, it is not for a socket
527 * existing in our system, if it is matched to a socket,
528 * it is just duplicate segment or bug in other side's TCP.
529 * So that we build reply only basing on parameters
530 * arrived with segment.
531 * Exception: precedence violation. We do not implement it in any case.
532 */
533
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800534static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700536 struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800537 struct {
538 struct tcphdr th;
539#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800540 __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800541#endif
542 } rep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800544#ifdef CONFIG_TCP_MD5SIG
545 struct tcp_md5sig_key *key;
546#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* Never send a reset in response to a reset. */
549 if (th->rst)
550 return;
551
Eric Dumazetee6b9672008-03-05 18:30:47 -0800552 if (skb->rtable->rt_type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return;
554
555 /* Swap the send and the receive. */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800556 memset(&rep, 0, sizeof(rep));
557 rep.th.dest = th->source;
558 rep.th.source = th->dest;
559 rep.th.doff = sizeof(struct tcphdr) / 4;
560 rep.th.rst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (th->ack) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800563 rep.th.seq = th->ack_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800565 rep.th.ack = 1;
566 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
567 skb->len - (th->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200570 memset(&arg, 0, sizeof(arg));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800571 arg.iov[0].iov_base = (unsigned char *)&rep;
572 arg.iov[0].iov_len = sizeof(rep.th);
573
574#ifdef CONFIG_TCP_MD5SIG
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700575 key = sk ? tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr) : NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800576 if (key) {
577 rep.opt[0] = htonl((TCPOPT_NOP << 24) |
578 (TCPOPT_NOP << 16) |
579 (TCPOPT_MD5SIG << 8) |
580 TCPOLEN_MD5SIG);
581 /* Update length and the length the header thinks exists */
582 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
583 rep.th.doff = arg.iov[0].iov_len / 4;
584
585 tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[1],
586 key,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700587 ip_hdr(skb)->daddr,
588 ip_hdr(skb)->saddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +0900589 &rep.th, arg.iov[0].iov_len);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800590 }
591#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700592 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
593 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 sizeof(struct tcphdr), IPPROTO_TCP, 0);
595 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
596
Denis V. Lunev7feb49c2008-04-03 14:32:00 -0700597 ip_send_reply(dev_net(skb->dst->dev)->ipv4.tcp_sock, skb,
598 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
601 TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
602}
603
604/* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
605 outside socket context is ugly, certainly. What can I do?
606 */
607
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900608static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
609 u32 win, u32 ts, int oif,
610 struct tcp_md5sig_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700612 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct {
614 struct tcphdr th;
Al Viro714e85b2006-11-14 20:51:49 -0800615 __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800616#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800617 + (TCPOLEN_MD5SIG_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800618#endif
619 ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 } rep;
621 struct ip_reply_arg arg;
622
623 memset(&rep.th, 0, sizeof(struct tcphdr));
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200624 memset(&arg, 0, sizeof(arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 arg.iov[0].iov_base = (unsigned char *)&rep;
627 arg.iov[0].iov_len = sizeof(rep.th);
628 if (ts) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800629 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
630 (TCPOPT_TIMESTAMP << 8) |
631 TCPOLEN_TIMESTAMP);
632 rep.opt[1] = htonl(tcp_time_stamp);
633 rep.opt[2] = htonl(ts);
Craig Schlentercb48cfe2007-01-09 00:11:15 -0800634 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 /* Swap the send and the receive. */
638 rep.th.dest = th->source;
639 rep.th.source = th->dest;
640 rep.th.doff = arg.iov[0].iov_len / 4;
641 rep.th.seq = htonl(seq);
642 rep.th.ack_seq = htonl(ack);
643 rep.th.ack = 1;
644 rep.th.window = htons(win);
645
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800646#ifdef CONFIG_TCP_MD5SIG
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800647 if (key) {
648 int offset = (ts) ? 3 : 0;
649
650 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
651 (TCPOPT_NOP << 16) |
652 (TCPOPT_MD5SIG << 8) |
653 TCPOLEN_MD5SIG);
654 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
655 rep.th.doff = arg.iov[0].iov_len/4;
656
657 tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[offset],
658 key,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700659 ip_hdr(skb)->daddr,
660 ip_hdr(skb)->saddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +0900661 &rep.th, arg.iov[0].iov_len);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800662 }
663#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700664 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
665 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 arg.iov[0].iov_len, IPPROTO_TCP, 0);
667 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900668 if (oif)
669 arg.bound_dev_if = oif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Denis V. Lunev7feb49c2008-04-03 14:32:00 -0700671 ip_send_reply(dev_net(skb->dev)->ipv4.tcp_sock, skb,
672 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
675}
676
677static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
678{
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700679 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800680 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900682 tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200683 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900684 tcptw->tw_ts_recent,
685 tw->tw_bound_dev_if,
686 tcp_twsk_md5_key(tcptw)
687 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700689 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200692static void tcp_v4_reqsk_send_ack(struct sk_buff *skb,
693 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900695 tcp_v4_send_ack(skb, tcp_rsk(req)->snt_isn + 1,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800696 tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900697 req->ts_recent,
698 0,
699 tcp_v4_md5_do_lookup(skb->sk, ip_hdr(skb)->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/*
Kris Katterjohn9bf1d832008-02-17 22:29:19 -0800703 * Send a SYN-ACK after having received a SYN.
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700704 * This still operates on a request_sock only, not on a big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * socket.
706 */
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800707static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
708 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700710 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int err = -1;
712 struct sk_buff * skb;
713
714 /* First, grab a route. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700715 if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800716 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 skb = tcp_make_synack(sk, dst, req);
719
720 if (skb) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700721 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800723 th->check = tcp_v4_check(skb->len,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700724 ireq->loc_addr,
725 ireq->rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 csum_partial((char *)th, skb->len,
727 skb->csum));
728
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700729 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
730 ireq->rmt_addr,
731 ireq->opt);
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200732 err = net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 dst_release(dst);
736 return err;
737}
738
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800739static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req)
740{
741 return __tcp_v4_send_synack(sk, req, NULL);
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700745 * IPv4 request_sock destructor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700747static void tcp_v4_reqsk_destructor(struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Jesper Juhla51482b2005-11-08 09:41:34 -0800749 kfree(inet_rsk(req)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200752#ifdef CONFIG_SYN_COOKIES
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800753static void syn_flood_warning(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 static unsigned long warntime;
756
757 if (time_after(jiffies, (warntime + HZ * 60))) {
758 warntime = jiffies;
759 printk(KERN_INFO
760 "possible SYN flooding on port %d. Sending cookies.\n",
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700761 ntohs(tcp_hdr(skb)->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763}
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200764#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700767 * Save and compile IPv4 options into the request_sock if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800769static struct ip_options *tcp_v4_save_options(struct sock *sk,
770 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct ip_options *opt = &(IPCB(skb)->opt);
773 struct ip_options *dopt = NULL;
774
775 if (opt && opt->optlen) {
776 int opt_size = optlength(opt);
777 dopt = kmalloc(opt_size, GFP_ATOMIC);
778 if (dopt) {
779 if (ip_options_echo(dopt, skb)) {
780 kfree(dopt);
781 dopt = NULL;
782 }
783 }
784 }
785 return dopt;
786}
787
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800788#ifdef CONFIG_TCP_MD5SIG
789/*
790 * RFC2385 MD5 checksumming requires a mapping of
791 * IP address->MD5 Key.
792 * We need to maintain these in the sk structure.
793 */
794
795/* Find the Key structure for an address. */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200796static struct tcp_md5sig_key *
797 tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800798{
799 struct tcp_sock *tp = tcp_sk(sk);
800 int i;
801
802 if (!tp->md5sig_info || !tp->md5sig_info->entries4)
803 return NULL;
804 for (i = 0; i < tp->md5sig_info->entries4; i++) {
805 if (tp->md5sig_info->keys4[i].addr == addr)
David S. Millerf8ab18d2007-09-28 15:18:35 -0700806 return &tp->md5sig_info->keys4[i].base;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800807 }
808 return NULL;
809}
810
811struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
812 struct sock *addr_sk)
813{
814 return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->daddr);
815}
816
817EXPORT_SYMBOL(tcp_v4_md5_lookup);
818
Adrian Bunkf5b99bc2006-11-30 17:22:29 -0800819static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
820 struct request_sock *req)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800821{
822 return tcp_v4_md5_do_lookup(sk, inet_rsk(req)->rmt_addr);
823}
824
825/* This can be called on a newly created socket, from other files */
826int tcp_v4_md5_do_add(struct sock *sk, __be32 addr,
827 u8 *newkey, u8 newkeylen)
828{
829 /* Add Key to the list */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700830 struct tcp_md5sig_key *key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800831 struct tcp_sock *tp = tcp_sk(sk);
832 struct tcp4_md5sig_key *keys;
833
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700834 key = tcp_v4_md5_do_lookup(sk, addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800835 if (key) {
836 /* Pre-existing entry - just update that one. */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700837 kfree(key->key);
838 key->key = newkey;
839 key->keylen = newkeylen;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800840 } else {
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200841 struct tcp_md5sig_info *md5sig;
842
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800843 if (!tp->md5sig_info) {
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200844 tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info),
845 GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800846 if (!tp->md5sig_info) {
847 kfree(newkey);
848 return -ENOMEM;
849 }
David S. Miller3d7dbea2007-06-12 14:36:42 -0700850 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800851 }
852 if (tcp_alloc_md5sig_pool() == NULL) {
853 kfree(newkey);
854 return -ENOMEM;
855 }
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200856 md5sig = tp->md5sig_info;
857
858 if (md5sig->alloced4 == md5sig->entries4) {
859 keys = kmalloc((sizeof(*keys) *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900860 (md5sig->entries4 + 1)), GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800861 if (!keys) {
862 kfree(newkey);
863 tcp_free_md5sig_pool();
864 return -ENOMEM;
865 }
866
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200867 if (md5sig->entries4)
868 memcpy(keys, md5sig->keys4,
869 sizeof(*keys) * md5sig->entries4);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800870
871 /* Free old key list, and reference new one */
YOSHIFUJI Hideakia80cc202007-11-20 17:30:06 -0800872 kfree(md5sig->keys4);
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200873 md5sig->keys4 = keys;
874 md5sig->alloced4++;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800875 }
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200876 md5sig->entries4++;
David S. Millerf8ab18d2007-09-28 15:18:35 -0700877 md5sig->keys4[md5sig->entries4 - 1].addr = addr;
878 md5sig->keys4[md5sig->entries4 - 1].base.key = newkey;
879 md5sig->keys4[md5sig->entries4 - 1].base.keylen = newkeylen;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800880 }
881 return 0;
882}
883
884EXPORT_SYMBOL(tcp_v4_md5_do_add);
885
886static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk,
887 u8 *newkey, u8 newkeylen)
888{
889 return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->daddr,
890 newkey, newkeylen);
891}
892
893int tcp_v4_md5_do_del(struct sock *sk, __be32 addr)
894{
895 struct tcp_sock *tp = tcp_sk(sk);
896 int i;
897
898 for (i = 0; i < tp->md5sig_info->entries4; i++) {
899 if (tp->md5sig_info->keys4[i].addr == addr) {
900 /* Free the key */
David S. Millerf8ab18d2007-09-28 15:18:35 -0700901 kfree(tp->md5sig_info->keys4[i].base.key);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800902 tp->md5sig_info->entries4--;
903
904 if (tp->md5sig_info->entries4 == 0) {
905 kfree(tp->md5sig_info->keys4);
906 tp->md5sig_info->keys4 = NULL;
Leigh Brown8228a18d2006-12-17 17:12:30 -0800907 tp->md5sig_info->alloced4 = 0;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200908 } else if (tp->md5sig_info->entries4 != i) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800909 /* Need to do some manipulation */
YOSHIFUJI Hideaki354faf02007-11-20 17:30:31 -0800910 memmove(&tp->md5sig_info->keys4[i],
911 &tp->md5sig_info->keys4[i+1],
912 (tp->md5sig_info->entries4 - i) *
913 sizeof(struct tcp4_md5sig_key));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800914 }
915 tcp_free_md5sig_pool();
916 return 0;
917 }
918 }
919 return -ENOENT;
920}
921
922EXPORT_SYMBOL(tcp_v4_md5_do_del);
923
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200924static void tcp_v4_clear_md5_list(struct sock *sk)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800925{
926 struct tcp_sock *tp = tcp_sk(sk);
927
928 /* Free each key, then the set of key keys,
929 * the crypto element, and then decrement our
930 * hold on the last resort crypto.
931 */
932 if (tp->md5sig_info->entries4) {
933 int i;
934 for (i = 0; i < tp->md5sig_info->entries4; i++)
David S. Millerf8ab18d2007-09-28 15:18:35 -0700935 kfree(tp->md5sig_info->keys4[i].base.key);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800936 tp->md5sig_info->entries4 = 0;
937 tcp_free_md5sig_pool();
938 }
939 if (tp->md5sig_info->keys4) {
940 kfree(tp->md5sig_info->keys4);
941 tp->md5sig_info->keys4 = NULL;
942 tp->md5sig_info->alloced4 = 0;
943 }
944}
945
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200946static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
947 int optlen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800948{
949 struct tcp_md5sig cmd;
950 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
951 u8 *newkey;
952
953 if (optlen < sizeof(cmd))
954 return -EINVAL;
955
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200956 if (copy_from_user(&cmd, optval, sizeof(cmd)))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800957 return -EFAULT;
958
959 if (sin->sin_family != AF_INET)
960 return -EINVAL;
961
962 if (!cmd.tcpm_key || !cmd.tcpm_keylen) {
963 if (!tcp_sk(sk)->md5sig_info)
964 return -ENOENT;
965 return tcp_v4_md5_do_del(sk, sin->sin_addr.s_addr);
966 }
967
968 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
969 return -EINVAL;
970
971 if (!tcp_sk(sk)->md5sig_info) {
972 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200973 struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800974
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800975 if (!p)
976 return -EINVAL;
977
978 tp->md5sig_info = p;
David S. Miller3d7dbea2007-06-12 14:36:42 -0700979 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800980 }
981
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200982 newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800983 if (!newkey)
984 return -ENOMEM;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800985 return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
986 newkey, cmd.tcpm_keylen);
987}
988
989static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
990 __be32 saddr, __be32 daddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +0900991 struct tcphdr *th,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -0800992 unsigned int tcplen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800993{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800994 struct tcp_md5sig_pool *hp;
995 struct tcp4_pseudohdr *bp;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800996 int err;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800997
998 /*
999 * Okay, so RFC2385 is turned on for this connection,
1000 * so we need to generate the MD5 hash for the packet now.
1001 */
1002
1003 hp = tcp_get_md5sig_pool();
1004 if (!hp)
1005 goto clear_hash_noput;
1006
1007 bp = &hp->md5_blk.ip4;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001008
1009 /*
YOSHIFUJI Hideaki8d26d762008-04-17 13:19:16 +09001010 * The TCP pseudo-header (in the order: source IP address,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001011 * destination IP address, zero-padded protocol number, and
1012 * segment length)
1013 */
1014 bp->saddr = saddr;
1015 bp->daddr = daddr;
1016 bp->pad = 0;
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001017 bp->protocol = IPPROTO_TCP;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001018 bp->len = htons(tcplen);
David S. Millerc7da57a2007-10-26 00:41:21 -07001019
YOSHIFUJI Hideaki8d26d762008-04-17 13:19:16 +09001020 err = tcp_calc_md5_hash(md5_hash, key, sizeof(*bp),
1021 th, tcplen, hp);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001022 if (err)
1023 goto clear_hash;
1024
YOSHIFUJI Hideaki8d26d762008-04-17 13:19:16 +09001025 /* Free up the crypto pool */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001026 tcp_put_md5sig_pool();
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001027out:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001028 return 0;
1029clear_hash:
1030 tcp_put_md5sig_pool();
1031clear_hash_noput:
1032 memset(md5_hash, 0, 16);
1033 goto out;
1034}
1035
1036int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
1037 struct sock *sk,
1038 struct dst_entry *dst,
1039 struct request_sock *req,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001040 struct tcphdr *th,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -08001041 unsigned int tcplen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001042{
1043 __be32 saddr, daddr;
1044
1045 if (sk) {
1046 saddr = inet_sk(sk)->saddr;
1047 daddr = inet_sk(sk)->daddr;
1048 } else {
1049 struct rtable *rt = (struct rtable *)dst;
1050 BUG_ON(!rt);
1051 saddr = rt->rt_src;
1052 daddr = rt->rt_dst;
1053 }
1054 return tcp_v4_do_calc_md5_hash(md5_hash, key,
1055 saddr, daddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001056 th, tcplen);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001057}
1058
1059EXPORT_SYMBOL(tcp_v4_calc_md5_hash);
1060
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001061static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001062{
1063 /*
1064 * This gets called for each TCP segment that arrives
1065 * so we want to be efficient.
1066 * We have 3 drop cases:
1067 * o No MD5 hash and one expected.
1068 * o MD5 hash and we're not expecting one.
1069 * o MD5 hash and its wrong.
1070 */
1071 __u8 *hash_location = NULL;
1072 struct tcp_md5sig_key *hash_expected;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001073 const struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001074 struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001075 int genhash;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001076 unsigned char newhash[16];
1077
1078 hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09001079 hash_location = tcp_parse_md5sig_option(th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001080
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001081 /* We've parsed the options - do we have a hash? */
1082 if (!hash_expected && !hash_location)
1083 return 0;
1084
1085 if (hash_expected && !hash_location) {
Leigh Browna9fc00c2006-12-17 17:13:10 -08001086 LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found "
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001087 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001088 NIPQUAD(iph->saddr), ntohs(th->source),
1089 NIPQUAD(iph->daddr), ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001090 return 1;
1091 }
1092
1093 if (!hash_expected && hash_location) {
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001094 LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found "
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001095 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001096 NIPQUAD(iph->saddr), ntohs(th->source),
1097 NIPQUAD(iph->daddr), ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001098 return 1;
1099 }
1100
1101 /* Okay, so this is hash_expected and hash_location -
1102 * so we need to calculate the checksum.
1103 */
1104 genhash = tcp_v4_do_calc_md5_hash(newhash,
1105 hash_expected,
1106 iph->saddr, iph->daddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001107 th, skb->len);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001108
1109 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
1110 if (net_ratelimit()) {
1111 printk(KERN_INFO "MD5 Hash failed for "
1112 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001113 NIPQUAD(iph->saddr), ntohs(th->source),
1114 NIPQUAD(iph->daddr), ntohs(th->dest),
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001115 genhash ? " tcp_v4_calc_md5_hash failed" : "");
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001116 }
1117 return 1;
1118 }
1119 return 0;
1120}
1121
1122#endif
1123
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001124struct request_sock_ops tcp_request_sock_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 .family = PF_INET,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001126 .obj_size = sizeof(struct tcp_request_sock),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 .rtx_syn_ack = tcp_v4_send_synack,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001128 .send_ack = tcp_v4_reqsk_send_ack,
1129 .destructor = tcp_v4_reqsk_destructor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 .send_reset = tcp_v4_send_reset,
1131};
1132
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001133#ifdef CONFIG_TCP_MD5SIG
Andrew Mortonb6332e62006-11-30 19:16:28 -08001134static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001135 .md5_lookup = tcp_v4_reqsk_md5_lookup,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001136};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001137#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001138
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001139static struct timewait_sock_ops tcp_timewait_sock_ops = {
1140 .twsk_obj_size = sizeof(struct tcp_timewait_sock),
1141 .twsk_unique = tcp_twsk_unique,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001142 .twsk_destructor= tcp_twsk_destructor,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001143};
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
1146{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001147 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct tcp_options_received tmp_opt;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001149 struct request_sock *req;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001150 __be32 saddr = ip_hdr(skb)->saddr;
1151 __be32 daddr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 __u32 isn = TCP_SKB_CB(skb)->when;
1153 struct dst_entry *dst = NULL;
1154#ifdef CONFIG_SYN_COOKIES
1155 int want_cookie = 0;
1156#else
1157#define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
1158#endif
1159
1160 /* Never answer to SYNs send to broadcast or multicast */
Eric Dumazetee6b9672008-03-05 18:30:47 -08001161 if (skb->rtable->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 goto drop;
1163
1164 /* TW buckets are converted to open requests without
1165 * limitations, they conserve resources and peer is
1166 * evidently real one.
1167 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001168 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169#ifdef CONFIG_SYN_COOKIES
1170 if (sysctl_tcp_syncookies) {
1171 want_cookie = 1;
1172 } else
1173#endif
1174 goto drop;
1175 }
1176
1177 /* Accept backlog is full. If we have already queued enough
1178 * of warm entries in syn queue, drop request. It is better than
1179 * clogging syn queue with openreqs with exponentially increasing
1180 * timeout.
1181 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001182 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 goto drop;
1184
Arnaldo Carvalho de Meloce4a7d02008-06-10 12:39:35 -07001185 req = inet_reqsk_alloc(&tcp_request_sock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (!req)
1187 goto drop;
1188
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001189#ifdef CONFIG_TCP_MD5SIG
1190 tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
1191#endif
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 tcp_clear_options(&tmp_opt);
1194 tmp_opt.mss_clamp = 536;
1195 tmp_opt.user_mss = tcp_sk(sk)->rx_opt.user_mss;
1196
1197 tcp_parse_options(skb, &tmp_opt, 0);
1198
Florian Westphal4dfc2812008-04-10 03:12:40 -07001199 if (want_cookie && !tmp_opt.saw_tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 tcp_clear_options(&tmp_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
1203 /* Some OSes (unknown ones, but I see them on web server, which
1204 * contains information interesting only for windows'
1205 * users) do not send their stamp in SYN. It is easy case.
1206 * We simply do not advertise TS support.
1207 */
1208 tmp_opt.saw_tstamp = 0;
1209 tmp_opt.tstamp_ok = 0;
1210 }
1211 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1212
1213 tcp_openreq_init(req, &tmp_opt, skb);
1214
Venkat Yekkirala4237c752006-07-24 23:32:50 -07001215 if (security_inet_conn_request(sk, skb, req))
1216 goto drop_and_free;
1217
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001218 ireq = inet_rsk(req);
1219 ireq->loc_addr = daddr;
1220 ireq->rmt_addr = saddr;
1221 ireq->opt = tcp_v4_save_options(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!want_cookie)
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001223 TCP_ECN_create_request(req, tcp_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (want_cookie) {
1226#ifdef CONFIG_SYN_COOKIES
1227 syn_flood_warning(skb);
Florian Westphal4dfc2812008-04-10 03:12:40 -07001228 req->cookie_ts = tmp_opt.tstamp_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229#endif
1230 isn = cookie_v4_init_sequence(sk, skb, &req->mss);
1231 } else if (!isn) {
1232 struct inet_peer *peer = NULL;
1233
1234 /* VJ's idea. We save last timestamp seen
1235 * from the destination in peer table, when entering
1236 * state TIME-WAIT, and check against it before
1237 * accepting new connection request.
1238 *
1239 * If "isn" is not zero, this request hit alive
1240 * timewait bucket, so that all the necessary checks
1241 * are made in the function processing timewait state.
1242 */
1243 if (tmp_opt.saw_tstamp &&
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07001244 tcp_death_row.sysctl_tw_recycle &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001245 (dst = inet_csk_route_req(sk, req)) != NULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
1247 peer->v4daddr == saddr) {
James Morris9d729f72007-03-04 16:12:44 -08001248 if (get_seconds() < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 (s32)(peer->tcp_ts - req->ts_recent) >
1250 TCP_PAWS_WINDOW) {
1251 NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001252 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 }
1255 /* Kill the following clause, if you dislike this way. */
1256 else if (!sysctl_tcp_syncookies &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001257 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 (sysctl_max_syn_backlog >> 2)) &&
1259 (!peer || !peer->tcp_ts_stamp) &&
1260 (!dst || !dst_metric(dst, RTAX_RTT))) {
1261 /* Without syncookies last quarter of
1262 * backlog is filled with destinations,
1263 * proven to be alive.
1264 * It means that we continue to communicate
1265 * to destinations, already remembered
1266 * to the moment of synflood.
1267 */
Patrick McHardy64ce2072005-08-09 20:50:53 -07001268 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open "
YOSHIFUJI Hideakia7d632b2008-04-14 04:09:00 -07001269 "request from " NIPQUAD_FMT "/%u\n",
Patrick McHardy64ce2072005-08-09 20:50:53 -07001270 NIPQUAD(saddr),
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001271 ntohs(tcp_hdr(skb)->source));
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001272 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
Gerrit Renkera94f7232006-11-10 14:06:49 -08001275 isn = tcp_v4_init_sequence(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001277 tcp_rsk(req)->snt_isn = isn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001279 if (__tcp_v4_send_synack(sk, req, dst) || want_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto drop_and_free;
1281
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001282 inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0;
1284
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001285drop_and_release:
1286 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287drop_and_free:
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001288 reqsk_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
1291}
1292
1293
1294/*
1295 * The three way handshake has completed - we got a valid synack -
1296 * now create the new socket.
1297 */
1298struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001299 struct request_sock *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct dst_entry *dst)
1301{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001302 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct inet_sock *newinet;
1304 struct tcp_sock *newtp;
1305 struct sock *newsk;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001306#ifdef CONFIG_TCP_MD5SIG
1307 struct tcp_md5sig_key *key;
1308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (sk_acceptq_is_full(sk))
1311 goto exit_overflow;
1312
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001313 if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 goto exit;
1315
1316 newsk = tcp_create_openreq_child(sk, req, skb);
1317 if (!newsk)
1318 goto exit;
1319
Herbert Xubcd76112006-06-30 13:36:35 -07001320 newsk->sk_gso_type = SKB_GSO_TCPV4;
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -07001321 sk_setup_caps(newsk, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 newtp = tcp_sk(newsk);
1324 newinet = inet_sk(newsk);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001325 ireq = inet_rsk(req);
1326 newinet->daddr = ireq->rmt_addr;
1327 newinet->rcv_saddr = ireq->loc_addr;
1328 newinet->saddr = ireq->loc_addr;
1329 newinet->opt = ireq->opt;
1330 ireq->opt = NULL;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001331 newinet->mc_index = inet_iif(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001332 newinet->mc_ttl = ip_hdr(skb)->ttl;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001333 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (newinet->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001335 inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 newinet->id = newtp->write_seq ^ jiffies;
1337
John Heffner5d424d52006-03-20 17:53:41 -08001338 tcp_mtup_init(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 tcp_sync_mss(newsk, dst_mtu(dst));
1340 newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
1341 tcp_initialize_rcv_mss(newsk);
1342
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001343#ifdef CONFIG_TCP_MD5SIG
1344 /* Copy over the MD5 key from the original socket */
1345 if ((key = tcp_v4_md5_do_lookup(sk, newinet->daddr)) != NULL) {
1346 /*
1347 * We're using one, so create a matching key
1348 * on the newsk structure. If we fail to get
1349 * memory, then we end up not copying the key
1350 * across. Shucks.
1351 */
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -02001352 char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1353 if (newkey != NULL)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001354 tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr,
1355 newkey, key->keylen);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001356 }
1357#endif
1358
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001359 __inet_hash_nolisten(newsk);
1360 __inet_inherit_port(sk, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 return newsk;
1363
1364exit_overflow:
1365 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
1366exit:
1367 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
1368 dst_release(dst);
1369 return NULL;
1370}
1371
1372static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
1373{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001374 struct tcphdr *th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001375 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct sock *nsk;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001377 struct request_sock **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* Find possible connection requests. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001379 struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
1380 iph->saddr, iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (req)
1382 return tcp_check_req(sk, skb, req, prev);
1383
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001384 nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001385 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 if (nsk) {
1388 if (nsk->sk_state != TCP_TIME_WAIT) {
1389 bh_lock_sock(nsk);
1390 return nsk;
1391 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001392 inet_twsk_put(inet_twsk(nsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return NULL;
1394 }
1395
1396#ifdef CONFIG_SYN_COOKIES
1397 if (!th->rst && !th->syn && th->ack)
1398 sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
1399#endif
1400 return sk;
1401}
1402
Al Virob51655b2006-11-14 21:40:42 -08001403static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001405 const struct iphdr *iph = ip_hdr(skb);
1406
Patrick McHardy84fa7932006-08-29 16:44:56 -07001407 if (skb->ip_summed == CHECKSUM_COMPLETE) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001408 if (!tcp_v4_check(skb->len, iph->saddr,
1409 iph->daddr, skb->csum)) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001410 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return 0;
Herbert Xufb286bb2005-11-10 13:01:24 -08001412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001414
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001415 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
Herbert Xufb286bb2005-11-10 13:01:24 -08001416 skb->len, IPPROTO_TCP, 0);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (skb->len <= 76) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001419 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 return 0;
1422}
1423
1424
1425/* The socket must have it's spinlock held when we get
1426 * here.
1427 *
1428 * We have a potential double-lock case here, so even when
1429 * doing backlog processing we use the BH locking scheme.
1430 * This is because we cannot sleep with the original spinlock
1431 * held.
1432 */
1433int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
1434{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001435 struct sock *rsk;
1436#ifdef CONFIG_TCP_MD5SIG
1437 /*
1438 * We really want to reject the packet as early as possible
1439 * if:
1440 * o We're expecting an MD5'd packet and this is no MD5 tcp option
1441 * o There is an MD5 option and we're not expecting one
1442 */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001443 if (tcp_v4_inbound_md5_hash(sk, skb))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001444 goto discard;
1445#endif
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1448 TCP_CHECK_TIMER(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001449 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001450 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 TCP_CHECK_TIMER(sk);
1454 return 0;
1455 }
1456
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001457 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto csum_err;
1459
1460 if (sk->sk_state == TCP_LISTEN) {
1461 struct sock *nsk = tcp_v4_hnd_req(sk, skb);
1462 if (!nsk)
1463 goto discard;
1464
1465 if (nsk != sk) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001466 if (tcp_child_process(sk, nsk, skb)) {
1467 rsk = nsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
1471 }
1472 }
1473
1474 TCP_CHECK_TIMER(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001475 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001476 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 TCP_CHECK_TIMER(sk);
1480 return 0;
1481
1482reset:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001483 tcp_v4_send_reset(rsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484discard:
1485 kfree_skb(skb);
1486 /* Be careful here. If this function gets more complicated and
1487 * gcc suffers from register pressure on the x86, sk (in %ebx)
1488 * might be destroyed here. This current version compiles correctly,
1489 * but you have been warned.
1490 */
1491 return 0;
1492
1493csum_err:
1494 TCP_INC_STATS_BH(TCP_MIB_INERRS);
1495 goto discard;
1496}
1497
1498/*
1499 * From tcp_input.c
1500 */
1501
1502int tcp_v4_rcv(struct sk_buff *skb)
1503{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001504 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 struct tcphdr *th;
1506 struct sock *sk;
1507 int ret;
1508
1509 if (skb->pkt_type != PACKET_HOST)
1510 goto discard_it;
1511
1512 /* Count it even if it's bad */
1513 TCP_INC_STATS_BH(TCP_MIB_INSEGS);
1514
1515 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1516 goto discard_it;
1517
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001518 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (th->doff < sizeof(struct tcphdr) / 4)
1521 goto bad_packet;
1522 if (!pskb_may_pull(skb, th->doff * 4))
1523 goto discard_it;
1524
1525 /* An explanation is required here, I think.
1526 * Packet length and doff are validated by header prediction,
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001527 * provided case of th->doff==0 is eliminated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 * So, we defer the checks. */
Herbert Xu60476372007-04-09 11:59:39 -07001529 if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 goto bad_packet;
1531
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001532 th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001533 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1535 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1536 skb->len - th->doff * 4);
1537 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1538 TCP_SKB_CB(skb)->when = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001539 TCP_SKB_CB(skb)->flags = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 TCP_SKB_CB(skb)->sacked = 0;
1541
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001542 sk = __inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001543 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (!sk)
1545 goto no_tcp_socket;
1546
1547process:
1548 if (sk->sk_state == TCP_TIME_WAIT)
1549 goto do_time_wait;
1550
1551 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1552 goto discard_and_relse;
Patrick McHardyb59c2702006-01-06 23:06:10 -08001553 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001555 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 goto discard_and_relse;
1557
1558 skb->dev = NULL;
1559
Ingo Molnarc6366182006-07-03 00:25:13 -07001560 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 ret = 0;
1562 if (!sock_owned_by_user(sk)) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001563#ifdef CONFIG_NET_DMA
1564 struct tcp_sock *tp = tcp_sk(sk);
1565 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1566 tp->ucopy.dma_chan = get_softnet_dma();
1567 if (tp->ucopy.dma_chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 ret = tcp_v4_do_rcv(sk, skb);
Chris Leech1a2449a2006-05-23 18:05:53 -07001569 else
1570#endif
1571 {
1572 if (!tcp_prequeue(sk, skb))
1573 ret = tcp_v4_do_rcv(sk, skb);
1574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 } else
1576 sk_add_backlog(sk, skb);
1577 bh_unlock_sock(sk);
1578
1579 sock_put(sk);
1580
1581 return ret;
1582
1583no_tcp_socket:
1584 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1585 goto discard_it;
1586
1587 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1588bad_packet:
1589 TCP_INC_STATS_BH(TCP_MIB_INERRS);
1590 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001591 tcp_v4_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
1594discard_it:
1595 /* Discard frame. */
1596 kfree_skb(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599discard_and_relse:
1600 sock_put(sk);
1601 goto discard_it;
1602
1603do_time_wait:
1604 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001605 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto discard_it;
1607 }
1608
1609 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1610 TCP_INC_STATS_BH(TCP_MIB_INERRS);
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001611 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 goto discard_it;
1613 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001614 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 case TCP_TW_SYN: {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001616 struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001617 &tcp_hashinfo,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001618 iph->daddr, th->dest,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001619 inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (sk2) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001621 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
1622 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 sk = sk2;
1624 goto process;
1625 }
1626 /* Fall through to ACK */
1627 }
1628 case TCP_TW_ACK:
1629 tcp_v4_timewait_ack(sk, skb);
1630 break;
1631 case TCP_TW_RST:
1632 goto no_tcp_socket;
1633 case TCP_TW_SUCCESS:;
1634 }
1635 goto discard_it;
1636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638/* VJ's idea. Save last timestamp seen from this destination
1639 * and hold it at least for normal timewait interval to use for duplicate
1640 * segment detection in subsequent connections, before they enter synchronized
1641 * state.
1642 */
1643
1644int tcp_v4_remember_stamp(struct sock *sk)
1645{
1646 struct inet_sock *inet = inet_sk(sk);
1647 struct tcp_sock *tp = tcp_sk(sk);
1648 struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
1649 struct inet_peer *peer = NULL;
1650 int release_it = 0;
1651
1652 if (!rt || rt->rt_dst != inet->daddr) {
1653 peer = inet_getpeer(inet->daddr, 1);
1654 release_it = 1;
1655 } else {
1656 if (!rt->peer)
1657 rt_bind_peer(rt, 1);
1658 peer = rt->peer;
1659 }
1660
1661 if (peer) {
1662 if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
James Morris9d729f72007-03-04 16:12:44 -08001663 (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
1665 peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
1666 peer->tcp_ts = tp->rx_opt.ts_recent;
1667 }
1668 if (release_it)
1669 inet_putpeer(peer);
1670 return 1;
1671 }
1672
1673 return 0;
1674}
1675
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001676int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001678 struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (peer) {
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001681 const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
1682
1683 if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
James Morris9d729f72007-03-04 16:12:44 -08001684 (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001685 peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) {
1686 peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp;
1687 peer->tcp_ts = tcptw->tw_ts_recent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689 inet_putpeer(peer);
1690 return 1;
1691 }
1692
1693 return 0;
1694}
1695
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001696struct inet_connection_sock_af_ops ipv4_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001697 .queue_xmit = ip_queue_xmit,
1698 .send_check = tcp_v4_send_check,
1699 .rebuild_header = inet_sk_rebuild_header,
1700 .conn_request = tcp_v4_conn_request,
1701 .syn_recv_sock = tcp_v4_syn_recv_sock,
1702 .remember_stamp = tcp_v4_remember_stamp,
1703 .net_header_len = sizeof(struct iphdr),
1704 .setsockopt = ip_setsockopt,
1705 .getsockopt = ip_getsockopt,
1706 .addr2sockaddr = inet_csk_addr2sockaddr,
1707 .sockaddr_len = sizeof(struct sockaddr_in),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001708 .bind_conflict = inet_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001709#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001710 .compat_setsockopt = compat_ip_setsockopt,
1711 .compat_getsockopt = compat_ip_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713};
1714
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001715#ifdef CONFIG_TCP_MD5SIG
Andrew Mortonb6332e62006-11-30 19:16:28 -08001716static struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001717 .md5_lookup = tcp_v4_md5_lookup,
1718 .calc_md5_hash = tcp_v4_calc_md5_hash,
1719 .md5_add = tcp_v4_md5_add_func,
1720 .md5_parse = tcp_v4_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001721};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001722#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724/* NOTE: A lot of things set to zero explicitly by call to
1725 * sk_alloc() so need not be done here.
1726 */
1727static int tcp_v4_init_sock(struct sock *sk)
1728{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001729 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct tcp_sock *tp = tcp_sk(sk);
1731
1732 skb_queue_head_init(&tp->out_of_order_queue);
1733 tcp_init_xmit_timers(sk);
1734 tcp_prequeue_init(tp);
1735
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001736 icsk->icsk_rto = TCP_TIMEOUT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 tp->mdev = TCP_TIMEOUT_INIT;
1738
1739 /* So many TCP implementations out there (incorrectly) count the
1740 * initial SYN frame in their delayed-ACK and congestion control
1741 * algorithms that we must have the following bandaid to talk
1742 * efficiently to them. -DaveM
1743 */
1744 tp->snd_cwnd = 2;
1745
1746 /* See draft-stevens-tcpca-spec-01 for discussion of the
1747 * initialization of these values.
1748 */
1749 tp->snd_ssthresh = 0x7fffffff; /* Infinity */
1750 tp->snd_cwnd_clamp = ~0;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001751 tp->mss_cache = 536;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 tp->reordering = sysctl_tcp_reordering;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001754 icsk->icsk_ca_ops = &tcp_init_congestion_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 sk->sk_state = TCP_CLOSE;
1757
1758 sk->sk_write_space = sk_stream_write_space;
1759 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1760
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001761 icsk->icsk_af_ops = &ipv4_specific;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001762 icsk->icsk_sync_mss = tcp_sync_mss;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001763#ifdef CONFIG_TCP_MD5SIG
1764 tp->af_specific = &tcp_sock_ipv4_specific;
1765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 sk->sk_sndbuf = sysctl_tcp_wmem[1];
1768 sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1769
1770 atomic_inc(&tcp_sockets_allocated);
1771
1772 return 0;
1773}
1774
Brian Haley7d06b2e2008-06-14 17:04:49 -07001775void tcp_v4_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct tcp_sock *tp = tcp_sk(sk);
1778
1779 tcp_clear_xmit_timers(sk);
1780
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001781 tcp_cleanup_congestion_control(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* Cleanup up the write buffer. */
David S. Millerfe067e82007-03-07 12:12:44 -08001784 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /* Cleans up our, hopefully empty, out_of_order_queue. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001787 __skb_queue_purge(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001789#ifdef CONFIG_TCP_MD5SIG
1790 /* Clean up the MD5 key list, if any */
1791 if (tp->md5sig_info) {
1792 tcp_v4_clear_md5_list(sk);
1793 kfree(tp->md5sig_info);
1794 tp->md5sig_info = NULL;
1795 }
1796#endif
1797
Chris Leech1a2449a2006-05-23 18:05:53 -07001798#ifdef CONFIG_NET_DMA
1799 /* Cleans up our sk_async_wait_queue */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001800 __skb_queue_purge(&sk->sk_async_wait_queue);
Chris Leech1a2449a2006-05-23 18:05:53 -07001801#endif
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /* Clean prequeue, it must be empty really */
1804 __skb_queue_purge(&tp->ucopy.prequeue);
1805
1806 /* Clean up a referenced TCP bind bucket. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001807 if (inet_csk(sk)->icsk_bind_hash)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001808 inet_put_port(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /*
1811 * If sendmsg cached page exists, toss it.
1812 */
1813 if (sk->sk_sndmsg_page) {
1814 __free_page(sk->sk_sndmsg_page);
1815 sk->sk_sndmsg_page = NULL;
1816 }
1817
1818 atomic_dec(&tcp_sockets_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
1821EXPORT_SYMBOL(tcp_v4_destroy_sock);
1822
1823#ifdef CONFIG_PROC_FS
1824/* Proc filesystem TCP sock list dumping. */
1825
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001826static inline struct inet_timewait_sock *tw_head(struct hlist_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 return hlist_empty(head) ? NULL :
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001829 list_entry(head->first, struct inet_timewait_sock, tw_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001832static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 return tw->tw_node.next ?
1835 hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
1836}
1837
1838static void *listening_get_next(struct seq_file *seq, void *cur)
1839{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001840 struct inet_connection_sock *icsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct hlist_node *node;
1842 struct sock *sk = cur;
1843 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001844 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 if (!sk) {
1847 st->bucket = 0;
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001848 sk = sk_head(&tcp_hashinfo.listening_hash[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto get_sk;
1850 }
1851
1852 ++st->num;
1853
1854 if (st->state == TCP_SEQ_STATE_OPENREQ) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001855 struct request_sock *req = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001857 icsk = inet_csk(st->syn_wait_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 req = req->dl_next;
1859 while (1) {
1860 while (req) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07001861 if (req->rsk_ops->family == st->family &&
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001862 net_eq(sock_net(req->sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 cur = req;
1864 goto out;
1865 }
1866 req = req->dl_next;
1867 }
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001868 if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 break;
1870get_req:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001871 req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 sk = sk_next(st->syn_wait_sk);
1874 st->state = TCP_SEQ_STATE_LISTENING;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001875 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 } else {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001877 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001878 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1879 if (reqsk_queue_len(&icsk->icsk_accept_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto start_req;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001881 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 sk = sk_next(sk);
1883 }
1884get_sk:
1885 sk_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001886 if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 cur = sk;
1888 goto out;
1889 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001890 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001891 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1892 if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893start_req:
1894 st->uid = sock_i_uid(sk);
1895 st->syn_wait_sk = sk;
1896 st->state = TCP_SEQ_STATE_OPENREQ;
1897 st->sbucket = 0;
1898 goto get_req;
1899 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001900 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -07001902 if (++st->bucket < INET_LHTABLE_SIZE) {
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001903 sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto get_sk;
1905 }
1906 cur = NULL;
1907out:
1908 return cur;
1909}
1910
1911static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
1912{
1913 void *rc = listening_get_next(seq, NULL);
1914
1915 while (rc && *pos) {
1916 rc = listening_get_next(seq, rc);
1917 --*pos;
1918 }
1919 return rc;
1920}
1921
1922static void *established_get_first(struct seq_file *seq)
1923{
1924 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001925 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 void *rc = NULL;
1927
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001928 for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 struct sock *sk;
1930 struct hlist_node *node;
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001931 struct inet_timewait_sock *tw;
Eric Dumazet230140c2007-11-07 02:40:20 -08001932 rwlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Eric Dumazet230140c2007-11-07 02:40:20 -08001934 read_lock_bh(lock);
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001935 sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07001936 if (sk->sk_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001937 !net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 continue;
1939 }
1940 rc = sk;
1941 goto out;
1942 }
1943 st->state = TCP_SEQ_STATE_TIME_WAIT;
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001944 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08001945 &tcp_hashinfo.ehash[st->bucket].twchain) {
Pavel Emelyanov28518fc2008-03-21 15:52:00 -07001946 if (tw->tw_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001947 !net_eq(twsk_net(tw), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 continue;
1949 }
1950 rc = tw;
1951 goto out;
1952 }
Eric Dumazet230140c2007-11-07 02:40:20 -08001953 read_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 st->state = TCP_SEQ_STATE_ESTABLISHED;
1955 }
1956out:
1957 return rc;
1958}
1959
1960static void *established_get_next(struct seq_file *seq, void *cur)
1961{
1962 struct sock *sk = cur;
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001963 struct inet_timewait_sock *tw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct hlist_node *node;
1965 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001966 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 ++st->num;
1969
1970 if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
1971 tw = cur;
1972 tw = tw_next(tw);
1973get_tw:
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001974 while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 tw = tw_next(tw);
1976 }
1977 if (tw) {
1978 cur = tw;
1979 goto out;
1980 }
Eric Dumazet230140c2007-11-07 02:40:20 -08001981 read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 st->state = TCP_SEQ_STATE_ESTABLISHED;
1983
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001984 if (++st->bucket < tcp_hashinfo.ehash_size) {
Eric Dumazet230140c2007-11-07 02:40:20 -08001985 read_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001986 sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 } else {
1988 cur = NULL;
1989 goto out;
1990 }
1991 } else
1992 sk = sk_next(sk);
1993
1994 sk_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001995 if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 goto found;
1997 }
1998
1999 st->state = TCP_SEQ_STATE_TIME_WAIT;
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002000 tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 goto get_tw;
2002found:
2003 cur = sk;
2004out:
2005 return cur;
2006}
2007
2008static void *established_get_idx(struct seq_file *seq, loff_t pos)
2009{
2010 void *rc = established_get_first(seq);
2011
2012 while (rc && pos) {
2013 rc = established_get_next(seq, rc);
2014 --pos;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return rc;
2017}
2018
2019static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
2020{
2021 void *rc;
2022 struct tcp_iter_state* st = seq->private;
2023
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002024 inet_listen_lock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 st->state = TCP_SEQ_STATE_LISTENING;
2026 rc = listening_get_idx(seq, &pos);
2027
2028 if (!rc) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002029 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 st->state = TCP_SEQ_STATE_ESTABLISHED;
2031 rc = established_get_idx(seq, pos);
2032 }
2033
2034 return rc;
2035}
2036
2037static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
2038{
2039 struct tcp_iter_state* st = seq->private;
2040 st->state = TCP_SEQ_STATE_LISTENING;
2041 st->num = 0;
2042 return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2043}
2044
2045static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2046{
2047 void *rc = NULL;
2048 struct tcp_iter_state* st;
2049
2050 if (v == SEQ_START_TOKEN) {
2051 rc = tcp_get_idx(seq, 0);
2052 goto out;
2053 }
2054 st = seq->private;
2055
2056 switch (st->state) {
2057 case TCP_SEQ_STATE_OPENREQ:
2058 case TCP_SEQ_STATE_LISTENING:
2059 rc = listening_get_next(seq, v);
2060 if (!rc) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002061 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 st->state = TCP_SEQ_STATE_ESTABLISHED;
2063 rc = established_get_first(seq);
2064 }
2065 break;
2066 case TCP_SEQ_STATE_ESTABLISHED:
2067 case TCP_SEQ_STATE_TIME_WAIT:
2068 rc = established_get_next(seq, v);
2069 break;
2070 }
2071out:
2072 ++*pos;
2073 return rc;
2074}
2075
2076static void tcp_seq_stop(struct seq_file *seq, void *v)
2077{
2078 struct tcp_iter_state* st = seq->private;
2079
2080 switch (st->state) {
2081 case TCP_SEQ_STATE_OPENREQ:
2082 if (v) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002083 struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
2084 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086 case TCP_SEQ_STATE_LISTENING:
2087 if (v != SEQ_START_TOKEN)
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002088 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 break;
2090 case TCP_SEQ_STATE_TIME_WAIT:
2091 case TCP_SEQ_STATE_ESTABLISHED:
2092 if (v)
Eric Dumazet230140c2007-11-07 02:40:20 -08002093 read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 break;
2095 }
2096}
2097
2098static int tcp_seq_open(struct inode *inode, struct file *file)
2099{
2100 struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct tcp_iter_state *s;
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002102 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002104 err = seq_open_net(inode, file, &afinfo->seq_ops,
2105 sizeof(struct tcp_iter_state));
2106 if (err < 0)
2107 return err;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002108
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002109 s = ((struct seq_file *)file->private_data)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 s->family = afinfo->family;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002111 return 0;
2112}
2113
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002114int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 int rc = 0;
2117 struct proc_dir_entry *p;
2118
Denis V. Lunev68fcadd2008-04-13 22:13:30 -07002119 afinfo->seq_fops.open = tcp_seq_open;
2120 afinfo->seq_fops.read = seq_read;
2121 afinfo->seq_fops.llseek = seq_lseek;
2122 afinfo->seq_fops.release = seq_release_net;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002123
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002124 afinfo->seq_ops.start = tcp_seq_start;
2125 afinfo->seq_ops.next = tcp_seq_next;
2126 afinfo->seq_ops.stop = tcp_seq_stop;
2127
Denis V. Lunev84841c32008-05-02 04:10:08 -07002128 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
2129 &afinfo->seq_fops, afinfo);
2130 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 rc = -ENOMEM;
2132 return rc;
2133}
2134
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002135void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002137 proc_net_remove(net, afinfo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002140static void get_openreq4(struct sock *sk, struct request_sock *req,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002141 struct seq_file *f, int i, int uid, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002143 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 int ttd = req->expires - jiffies;
2145
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002146 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2147 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 i,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002149 ireq->loc_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 ntohs(inet_sk(sk)->sport),
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002151 ireq->rmt_addr,
2152 ntohs(ireq->rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 TCP_SYN_RECV,
2154 0, 0, /* could print option size, but that is af dependent. */
2155 1, /* timers active (only the expire timer) */
2156 jiffies_to_clock_t(ttd),
2157 req->retrans,
2158 uid,
2159 0, /* non standard timer */
2160 0, /* open_requests have no inode */
2161 atomic_read(&sk->sk_refcnt),
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002162 req,
2163 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002166static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 int timer_active;
2169 unsigned long timer_expires;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002170 struct tcp_sock *tp = tcp_sk(sk);
2171 const struct inet_connection_sock *icsk = inet_csk(sk);
2172 struct inet_sock *inet = inet_sk(sk);
Al Viro714e85b2006-11-14 20:51:49 -08002173 __be32 dest = inet->daddr;
2174 __be32 src = inet->rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 __u16 destp = ntohs(inet->dport);
2176 __u16 srcp = ntohs(inet->sport);
2177
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002178 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002180 timer_expires = icsk->icsk_timeout;
2181 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002183 timer_expires = icsk->icsk_timeout;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002184 } else if (timer_pending(&sk->sk_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 timer_active = 2;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002186 timer_expires = sk->sk_timer.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 } else {
2188 timer_active = 0;
2189 timer_expires = jiffies;
2190 }
2191
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002192 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
Stephen Hemminger7be87352008-06-27 20:00:19 -07002193 "%08X %5d %8d %lu %d %p %lu %lu %u %u %d%n",
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002194 i, src, srcp, dest, destp, sk->sk_state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07002195 tp->write_seq - tp->snd_una,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002196 sk->sk_state == TCP_LISTEN ? sk->sk_ack_backlog :
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002197 (tp->rcv_nxt - tp->copied_seq),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 timer_active,
2199 jiffies_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002200 icsk->icsk_retransmits,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002201 sock_i_uid(sk),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002202 icsk->icsk_probes_out,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002203 sock_i_ino(sk),
2204 atomic_read(&sk->sk_refcnt), sk,
Stephen Hemminger7be87352008-06-27 20:00:19 -07002205 jiffies_to_clock_t(icsk->icsk_rto),
2206 jiffies_to_clock_t(icsk->icsk_ack.ato),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002207 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 tp->snd_cwnd,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002209 tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh,
2210 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002213static void get_timewait4_sock(struct inet_timewait_sock *tw,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002214 struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Al Viro23f33c22006-09-27 18:43:50 -07002216 __be32 dest, src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 __u16 destp, srcp;
2218 int ttd = tw->tw_ttd - jiffies;
2219
2220 if (ttd < 0)
2221 ttd = 0;
2222
2223 dest = tw->tw_daddr;
2224 src = tw->tw_rcv_saddr;
2225 destp = ntohs(tw->tw_dport);
2226 srcp = ntohs(tw->tw_sport);
2227
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002228 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2229 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
2231 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002232 atomic_read(&tw->tw_refcnt), tw, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
2235#define TMPSZ 150
2236
2237static int tcp4_seq_show(struct seq_file *seq, void *v)
2238{
2239 struct tcp_iter_state* st;
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002240 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 if (v == SEQ_START_TOKEN) {
2243 seq_printf(seq, "%-*s\n", TMPSZ - 1,
2244 " sl local_address rem_address st tx_queue "
2245 "rx_queue tr tm->when retrnsmt uid timeout "
2246 "inode");
2247 goto out;
2248 }
2249 st = seq->private;
2250
2251 switch (st->state) {
2252 case TCP_SEQ_STATE_LISTENING:
2253 case TCP_SEQ_STATE_ESTABLISHED:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002254 get_tcp4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 break;
2256 case TCP_SEQ_STATE_OPENREQ:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002257 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 break;
2259 case TCP_SEQ_STATE_TIME_WAIT:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002260 get_timewait4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 break;
2262 }
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002263 seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264out:
2265 return 0;
2266}
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268static struct tcp_seq_afinfo tcp4_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 .name = "tcp",
2270 .family = AF_INET,
Denis V. Lunev5f4472c2008-04-13 22:13:53 -07002271 .seq_fops = {
2272 .owner = THIS_MODULE,
2273 },
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002274 .seq_ops = {
2275 .show = tcp4_seq_show,
2276 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277};
2278
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002279static int tcp4_proc_init_net(struct net *net)
2280{
2281 return tcp_proc_register(net, &tcp4_seq_afinfo);
2282}
2283
2284static void tcp4_proc_exit_net(struct net *net)
2285{
2286 tcp_proc_unregister(net, &tcp4_seq_afinfo);
2287}
2288
2289static struct pernet_operations tcp4_net_ops = {
2290 .init = tcp4_proc_init_net,
2291 .exit = tcp4_proc_exit_net,
2292};
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294int __init tcp4_proc_init(void)
2295{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002296 return register_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
2299void tcp4_proc_exit(void)
2300{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002301 unregister_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303#endif /* CONFIG_PROC_FS */
2304
2305struct proto tcp_prot = {
2306 .name = "TCP",
2307 .owner = THIS_MODULE,
2308 .close = tcp_close,
2309 .connect = tcp_v4_connect,
2310 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002311 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 .ioctl = tcp_ioctl,
2313 .init = tcp_v4_init_sock,
2314 .destroy = tcp_v4_destroy_sock,
2315 .shutdown = tcp_shutdown,
2316 .setsockopt = tcp_setsockopt,
2317 .getsockopt = tcp_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 .recvmsg = tcp_recvmsg,
2319 .backlog_rcv = tcp_v4_do_rcv,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08002320 .hash = inet_hash,
2321 .unhash = inet_unhash,
2322 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 .enter_memory_pressure = tcp_enter_memory_pressure,
2324 .sockets_allocated = &tcp_sockets_allocated,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07002325 .orphan_count = &tcp_orphan_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 .memory_allocated = &tcp_memory_allocated,
2327 .memory_pressure = &tcp_memory_pressure,
2328 .sysctl_mem = sysctl_tcp_mem,
2329 .sysctl_wmem = sysctl_tcp_wmem,
2330 .sysctl_rmem = sysctl_tcp_rmem,
2331 .max_header = MAX_TCP_HEADER,
2332 .obj_size = sizeof(struct tcp_sock),
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002333 .twsk_prot = &tcp_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002334 .rsk_prot = &tcp_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07002335 .h.hashinfo = &tcp_hashinfo,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002336#ifdef CONFIG_COMPAT
2337 .compat_setsockopt = compat_tcp_setsockopt,
2338 .compat_getsockopt = compat_tcp_getsockopt,
2339#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340};
2341
Denis V. Lunev046ee902008-04-03 14:31:33 -07002342
2343static int __net_init tcp_sk_init(struct net *net)
2344{
2345 return inet_ctl_sock_create(&net->ipv4.tcp_sock,
2346 PF_INET, SOCK_RAW, IPPROTO_TCP, net);
2347}
2348
2349static void __net_exit tcp_sk_exit(struct net *net)
2350{
2351 inet_ctl_sock_destroy(net->ipv4.tcp_sock);
2352}
2353
2354static struct pernet_operations __net_initdata tcp_sk_ops = {
2355 .init = tcp_sk_init,
2356 .exit = tcp_sk_exit,
2357};
2358
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08002359void __init tcp_v4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Denis V. Lunev046ee902008-04-03 14:31:33 -07002361 if (register_pernet_device(&tcp_sk_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 panic("Failed to create the TCP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365EXPORT_SYMBOL(ipv4_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366EXPORT_SYMBOL(tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367EXPORT_SYMBOL(tcp_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368EXPORT_SYMBOL(tcp_v4_conn_request);
2369EXPORT_SYMBOL(tcp_v4_connect);
2370EXPORT_SYMBOL(tcp_v4_do_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371EXPORT_SYMBOL(tcp_v4_remember_stamp);
2372EXPORT_SYMBOL(tcp_v4_send_check);
2373EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
2374
2375#ifdef CONFIG_PROC_FS
2376EXPORT_SYMBOL(tcp_proc_register);
2377EXPORT_SYMBOL(tcp_proc_unregister);
2378#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379EXPORT_SYMBOL(sysctl_tcp_low_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380