blob: e331cdbd09537e7cb5e02a4a76aeff31b73c3c8d [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 *
8 * Version: $Id: tcp_ipv4.c,v 1.240 2002/02/01 22:01:04 davem Exp $
9 *
10 * IPv4 specific functions
11 *
12 *
13 * code split from:
14 * linux/ipv4/tcp.c
15 * linux/ipv4/tcp_input.c
16 * linux/ipv4/tcp_output.c
17 *
18 * See tcp.c for author information
19 *
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version
23 * 2 of the License, or (at your option) any later version.
24 */
25
26/*
27 * Changes:
28 * David S. Miller : New socket lookup architecture.
29 * This code is dedicated to John Dyson.
30 * David S. Miller : Change semantics of established hash,
31 * half is devoted to TIME_WAIT sockets
32 * and the rest go in the other half.
33 * Andi Kleen : Add support for syncookies and fixed
34 * some bugs: ip options weren't passed to
35 * the TCP layer, missed a check for an
36 * ACK bit.
37 * Andi Kleen : Implemented fast path mtu discovery.
38 * Fixed many serious bugs in the
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -070039 * request_sock handling and moved
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * most of it into the af independent code.
41 * Added tail drop and some other bugfixes.
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -080042 * Added new listen semantics.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * Mike McLagan : Routing by source
44 * Juan Jose Ciarlante: ip_dynaddr bits
45 * Andi Kleen: various fixes.
46 * Vitaly E. Lavrov : Transparent proxy revived after year
47 * coma.
48 * Andi Kleen : Fix new listen.
49 * Andi Kleen : Fix accept error reporting.
50 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
51 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
52 * a single port at the same time.
53 */
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <linux/types.h>
57#include <linux/fcntl.h>
58#include <linux/module.h>
59#include <linux/random.h>
60#include <linux/cache.h>
61#include <linux/jhash.h>
62#include <linux/init.h>
63#include <linux/times.h>
64
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/icmp.h>
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -070067#include <net/inet_hashtables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/tcp.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030069#include <net/transp_v6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/ipv6.h>
71#include <net/inet_common.h>
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -080072#include <net/timewait_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/xfrm.h>
Chris Leech1a2449a2006-05-23 18:05:53 -070074#include <net/netdma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#include <linux/inet.h>
77#include <linux/ipv6.h>
78#include <linux/stddef.h>
79#include <linux/proc_fs.h>
80#include <linux/seq_file.h>
81
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080082#include <linux/crypto.h>
83#include <linux/scatterlist.h>
84
Brian Haleyab32ea52006-09-22 14:15:41 -070085int sysctl_tcp_tw_reuse __read_mostly;
86int sysctl_tcp_low_latency __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/* Check TCP sequence numbers in ICMP packets. */
89#define ICMP_MIN_LENGTH 8
90
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -080091void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080093#ifdef CONFIG_TCP_MD5SIG
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020094static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk,
95 __be32 addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080096static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020097 __be32 saddr, __be32 daddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +090098 struct tcphdr *th, unsigned int tcplen);
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;
346
347 if (skb->len < (iph->ihl << 2) + 8) {
348 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
349 return;
350 }
351
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900352 sk = inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->daddr, th->dest,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -0800353 iph->saddr, th->source, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!sk) {
355 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
356 return;
357 }
358 if (sk->sk_state == TCP_TIME_WAIT) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -0700359 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return;
361 }
362
363 bh_lock_sock(sk);
364 /* If too many ICMPs get dropped on busy
365 * servers this needs to be solved differently.
366 */
367 if (sock_owned_by_user(sk))
368 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
369
370 if (sk->sk_state == TCP_CLOSE)
371 goto out;
372
373 tp = tcp_sk(sk);
374 seq = ntohl(th->seq);
375 if (sk->sk_state != TCP_LISTEN &&
376 !between(seq, tp->snd_una, tp->snd_nxt)) {
Eric Dumazet06ca7192006-10-20 00:22:25 -0700377 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto out;
379 }
380
381 switch (type) {
382 case ICMP_SOURCE_QUENCH:
383 /* Just silently ignore these. */
384 goto out;
385 case ICMP_PARAMETERPROB:
386 err = EPROTO;
387 break;
388 case ICMP_DEST_UNREACH:
389 if (code > NR_ICMP_UNREACH)
390 goto out;
391
392 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
393 if (!sock_owned_by_user(sk))
394 do_pmtu_discovery(sk, iph, info);
395 goto out;
396 }
397
398 err = icmp_err_convert[code].errno;
399 break;
400 case ICMP_TIME_EXCEEDED:
401 err = EHOSTUNREACH;
402 break;
403 default:
404 goto out;
405 }
406
407 switch (sk->sk_state) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700408 struct request_sock *req, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 case TCP_LISTEN:
410 if (sock_owned_by_user(sk))
411 goto out;
412
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700413 req = inet_csk_search_req(sk, &prev, th->dest,
414 iph->daddr, iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!req)
416 goto out;
417
418 /* ICMPs are not backlogged, hence we cannot get
419 an established socket here.
420 */
421 BUG_TRAP(!req->sk);
422
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700423 if (seq != tcp_rsk(req)->snt_isn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
425 goto out;
426 }
427
428 /*
429 * Still in SYN_RECV, just remove it silently.
430 * There is no good way to pass the error to the newly
431 * created socket, and POSIX does not want network
432 * errors returned from accept().
433 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700434 inet_csk_reqsk_queue_drop(sk, req, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 goto out;
436
437 case TCP_SYN_SENT:
438 case TCP_SYN_RECV: /* Cannot happen.
439 It can f.e. if SYNs crossed.
440 */
441 if (!sock_owned_by_user(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 sk->sk_err = err;
443
444 sk->sk_error_report(sk);
445
446 tcp_done(sk);
447 } else {
448 sk->sk_err_soft = err;
449 }
450 goto out;
451 }
452
453 /* If we've already connected we will keep trying
454 * until we time out, or the user gives up.
455 *
456 * rfc1122 4.2.3.9 allows to consider as hard errors
457 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
458 * but it is obsoleted by pmtu discovery).
459 *
460 * Note, that in modern internet, where routing is unreliable
461 * and in each dark corner broken firewalls sit, sending random
462 * errors ordered by their masters even this two messages finally lose
463 * their original sense (even Linux sends invalid PORT_UNREACHs)
464 *
465 * Now we are in compliance with RFCs.
466 * --ANK (980905)
467 */
468
469 inet = inet_sk(sk);
470 if (!sock_owned_by_user(sk) && inet->recverr) {
471 sk->sk_err = err;
472 sk->sk_error_report(sk);
473 } else { /* Only an error on timeout */
474 sk->sk_err_soft = err;
475 }
476
477out:
478 bh_unlock_sock(sk);
479 sock_put(sk);
480}
481
482/* This routine computes an IPv4 TCP checksum. */
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -0800483void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700486 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Patrick McHardy84fa7932006-08-29 16:44:56 -0700488 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800489 th->check = ~tcp_v4_check(len, inet->saddr,
490 inet->daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700491 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800492 skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800494 th->check = tcp_v4_check(len, inet->saddr, inet->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 csum_partial((char *)th,
496 th->doff << 2,
497 skb->csum));
498 }
499}
500
Herbert Xua430a432006-07-08 13:34:56 -0700501int tcp_v4_gso_send_check(struct sk_buff *skb)
502{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700503 const struct iphdr *iph;
Herbert Xua430a432006-07-08 13:34:56 -0700504 struct tcphdr *th;
505
506 if (!pskb_may_pull(skb, sizeof(*th)))
507 return -EINVAL;
508
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700509 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700510 th = tcp_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -0700511
512 th->check = 0;
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800513 th->check = ~tcp_v4_check(skb->len, iph->saddr, iph->daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700514 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800515 skb->csum_offset = offsetof(struct tcphdr, check);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700516 skb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xua430a432006-07-08 13:34:56 -0700517 return 0;
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*
521 * This routine will send an RST to the other tcp.
522 *
523 * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
524 * for reset.
525 * Answer: if a packet caused RST, it is not for a socket
526 * existing in our system, if it is matched to a socket,
527 * it is just duplicate segment or bug in other side's TCP.
528 * So that we build reply only basing on parameters
529 * arrived with segment.
530 * Exception: precedence violation. We do not implement it in any case.
531 */
532
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800533static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700535 struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800536 struct {
537 struct tcphdr th;
538#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800539 __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800540#endif
541 } rep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800543#ifdef CONFIG_TCP_MD5SIG
544 struct tcp_md5sig_key *key;
545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* Never send a reset in response to a reset. */
548 if (th->rst)
549 return;
550
Eric Dumazetee6b9672008-03-05 18:30:47 -0800551 if (skb->rtable->rt_type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return;
553
554 /* Swap the send and the receive. */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800555 memset(&rep, 0, sizeof(rep));
556 rep.th.dest = th->source;
557 rep.th.source = th->dest;
558 rep.th.doff = sizeof(struct tcphdr) / 4;
559 rep.th.rst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (th->ack) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800562 rep.th.seq = th->ack_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800564 rep.th.ack = 1;
565 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
566 skb->len - (th->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200569 memset(&arg, 0, sizeof(arg));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800570 arg.iov[0].iov_base = (unsigned char *)&rep;
571 arg.iov[0].iov_len = sizeof(rep.th);
572
573#ifdef CONFIG_TCP_MD5SIG
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700574 key = sk ? tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr) : NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800575 if (key) {
576 rep.opt[0] = htonl((TCPOPT_NOP << 24) |
577 (TCPOPT_NOP << 16) |
578 (TCPOPT_MD5SIG << 8) |
579 TCPOLEN_MD5SIG);
580 /* Update length and the length the header thinks exists */
581 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
582 rep.th.doff = arg.iov[0].iov_len / 4;
583
584 tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[1],
585 key,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700586 ip_hdr(skb)->daddr,
587 ip_hdr(skb)->saddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +0900588 &rep.th, arg.iov[0].iov_len);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800589 }
590#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700591 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
592 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 sizeof(struct tcphdr), IPPROTO_TCP, 0);
594 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
595
Denis V. Lunev7feb49c2008-04-03 14:32:00 -0700596 ip_send_reply(dev_net(skb->dst->dev)->ipv4.tcp_sock, skb,
597 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
600 TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
601}
602
603/* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
604 outside socket context is ugly, certainly. What can I do?
605 */
606
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800607static void tcp_v4_send_ack(struct tcp_timewait_sock *twsk,
608 struct sk_buff *skb, u32 seq, u32 ack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 u32 win, u32 ts)
610{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700611 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct {
613 struct tcphdr th;
Al Viro714e85b2006-11-14 20:51:49 -0800614 __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800615#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800616 + (TCPOLEN_MD5SIG_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800617#endif
618 ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 } rep;
620 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800621#ifdef CONFIG_TCP_MD5SIG
622 struct tcp_md5sig_key *key;
623 struct tcp_md5sig_key tw_key;
624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 memset(&rep.th, 0, sizeof(struct tcphdr));
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200627 memset(&arg, 0, sizeof(arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 arg.iov[0].iov_base = (unsigned char *)&rep;
630 arg.iov[0].iov_len = sizeof(rep.th);
631 if (ts) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800632 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
633 (TCPOPT_TIMESTAMP << 8) |
634 TCPOLEN_TIMESTAMP);
635 rep.opt[1] = htonl(tcp_time_stamp);
636 rep.opt[2] = htonl(ts);
Craig Schlentercb48cfe2007-01-09 00:11:15 -0800637 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 /* Swap the send and the receive. */
641 rep.th.dest = th->source;
642 rep.th.source = th->dest;
643 rep.th.doff = arg.iov[0].iov_len / 4;
644 rep.th.seq = htonl(seq);
645 rep.th.ack_seq = htonl(ack);
646 rep.th.ack = 1;
647 rep.th.window = htons(win);
648
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800649#ifdef CONFIG_TCP_MD5SIG
650 /*
651 * The SKB holds an imcoming packet, but may not have a valid ->sk
652 * pointer. This is especially the case when we're dealing with a
653 * TIME_WAIT ack, because the sk structure is long gone, and only
654 * the tcp_timewait_sock remains. So the md5 key is stashed in that
655 * structure, and we use it in preference. I believe that (twsk ||
656 * skb->sk) holds true, but we program defensively.
657 */
658 if (!twsk && skb->sk) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700659 key = tcp_v4_md5_do_lookup(skb->sk, ip_hdr(skb)->daddr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800660 } else if (twsk && twsk->tw_md5_keylen) {
661 tw_key.key = twsk->tw_md5_key;
662 tw_key.keylen = twsk->tw_md5_keylen;
663 key = &tw_key;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200664 } else
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800665 key = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800666
667 if (key) {
668 int offset = (ts) ? 3 : 0;
669
670 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
671 (TCPOPT_NOP << 16) |
672 (TCPOPT_MD5SIG << 8) |
673 TCPOLEN_MD5SIG);
674 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
675 rep.th.doff = arg.iov[0].iov_len/4;
676
677 tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[offset],
678 key,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700679 ip_hdr(skb)->daddr,
680 ip_hdr(skb)->saddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +0900681 &rep.th, arg.iov[0].iov_len);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800682 }
683#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700684 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
685 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 arg.iov[0].iov_len, IPPROTO_TCP, 0);
687 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
Patrick McHardyf0e48db2007-06-04 21:32:46 -0700688 if (twsk)
689 arg.bound_dev_if = twsk->tw_sk.tw_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Denis V. Lunev7feb49c2008-04-03 14:32:00 -0700691 ip_send_reply(dev_net(skb->dev)->ipv4.tcp_sock, skb,
692 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
695}
696
697static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
698{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700699 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800700 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800702 tcp_v4_send_ack(tcptw, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200703 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
704 tcptw->tw_ts_recent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700706 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200709static void tcp_v4_reqsk_send_ack(struct sk_buff *skb,
710 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800712 tcp_v4_send_ack(NULL, skb, tcp_rsk(req)->snt_isn + 1,
713 tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 req->ts_recent);
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/*
Kris Katterjohn9bf1d832008-02-17 22:29:19 -0800718 * Send a SYN-ACK after having received a SYN.
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700719 * This still operates on a request_sock only, not on a big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * socket.
721 */
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800722static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
723 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700725 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int err = -1;
727 struct sk_buff * skb;
728
729 /* First, grab a route. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700730 if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800731 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 skb = tcp_make_synack(sk, dst, req);
734
735 if (skb) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700736 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800738 th->check = tcp_v4_check(skb->len,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700739 ireq->loc_addr,
740 ireq->rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 csum_partial((char *)th, skb->len,
742 skb->csum));
743
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700744 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
745 ireq->rmt_addr,
746 ireq->opt);
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200747 err = net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 dst_release(dst);
751 return err;
752}
753
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800754static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req)
755{
756 return __tcp_v4_send_synack(sk, req, NULL);
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700760 * IPv4 request_sock destructor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700762static void tcp_v4_reqsk_destructor(struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Jesper Juhla51482b2005-11-08 09:41:34 -0800764 kfree(inet_rsk(req)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200767#ifdef CONFIG_SYN_COOKIES
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800768static void syn_flood_warning(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 static unsigned long warntime;
771
772 if (time_after(jiffies, (warntime + HZ * 60))) {
773 warntime = jiffies;
774 printk(KERN_INFO
775 "possible SYN flooding on port %d. Sending cookies.\n",
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700776 ntohs(tcp_hdr(skb)->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778}
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200779#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700782 * Save and compile IPv4 options into the request_sock if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800784static struct ip_options *tcp_v4_save_options(struct sock *sk,
785 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct ip_options *opt = &(IPCB(skb)->opt);
788 struct ip_options *dopt = NULL;
789
790 if (opt && opt->optlen) {
791 int opt_size = optlength(opt);
792 dopt = kmalloc(opt_size, GFP_ATOMIC);
793 if (dopt) {
794 if (ip_options_echo(dopt, skb)) {
795 kfree(dopt);
796 dopt = NULL;
797 }
798 }
799 }
800 return dopt;
801}
802
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800803#ifdef CONFIG_TCP_MD5SIG
804/*
805 * RFC2385 MD5 checksumming requires a mapping of
806 * IP address->MD5 Key.
807 * We need to maintain these in the sk structure.
808 */
809
810/* Find the Key structure for an address. */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200811static struct tcp_md5sig_key *
812 tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800813{
814 struct tcp_sock *tp = tcp_sk(sk);
815 int i;
816
817 if (!tp->md5sig_info || !tp->md5sig_info->entries4)
818 return NULL;
819 for (i = 0; i < tp->md5sig_info->entries4; i++) {
820 if (tp->md5sig_info->keys4[i].addr == addr)
David S. Millerf8ab18d2007-09-28 15:18:35 -0700821 return &tp->md5sig_info->keys4[i].base;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800822 }
823 return NULL;
824}
825
826struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
827 struct sock *addr_sk)
828{
829 return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->daddr);
830}
831
832EXPORT_SYMBOL(tcp_v4_md5_lookup);
833
Adrian Bunkf5b99bc2006-11-30 17:22:29 -0800834static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
835 struct request_sock *req)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800836{
837 return tcp_v4_md5_do_lookup(sk, inet_rsk(req)->rmt_addr);
838}
839
840/* This can be called on a newly created socket, from other files */
841int tcp_v4_md5_do_add(struct sock *sk, __be32 addr,
842 u8 *newkey, u8 newkeylen)
843{
844 /* Add Key to the list */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700845 struct tcp_md5sig_key *key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800846 struct tcp_sock *tp = tcp_sk(sk);
847 struct tcp4_md5sig_key *keys;
848
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700849 key = tcp_v4_md5_do_lookup(sk, addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800850 if (key) {
851 /* Pre-existing entry - just update that one. */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700852 kfree(key->key);
853 key->key = newkey;
854 key->keylen = newkeylen;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800855 } else {
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200856 struct tcp_md5sig_info *md5sig;
857
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800858 if (!tp->md5sig_info) {
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200859 tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info),
860 GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800861 if (!tp->md5sig_info) {
862 kfree(newkey);
863 return -ENOMEM;
864 }
David S. Miller3d7dbea2007-06-12 14:36:42 -0700865 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800866 }
867 if (tcp_alloc_md5sig_pool() == NULL) {
868 kfree(newkey);
869 return -ENOMEM;
870 }
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200871 md5sig = tp->md5sig_info;
872
873 if (md5sig->alloced4 == md5sig->entries4) {
874 keys = kmalloc((sizeof(*keys) *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900875 (md5sig->entries4 + 1)), GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800876 if (!keys) {
877 kfree(newkey);
878 tcp_free_md5sig_pool();
879 return -ENOMEM;
880 }
881
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200882 if (md5sig->entries4)
883 memcpy(keys, md5sig->keys4,
884 sizeof(*keys) * md5sig->entries4);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800885
886 /* Free old key list, and reference new one */
YOSHIFUJI Hideakia80cc202007-11-20 17:30:06 -0800887 kfree(md5sig->keys4);
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200888 md5sig->keys4 = keys;
889 md5sig->alloced4++;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800890 }
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200891 md5sig->entries4++;
David S. Millerf8ab18d2007-09-28 15:18:35 -0700892 md5sig->keys4[md5sig->entries4 - 1].addr = addr;
893 md5sig->keys4[md5sig->entries4 - 1].base.key = newkey;
894 md5sig->keys4[md5sig->entries4 - 1].base.keylen = newkeylen;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800895 }
896 return 0;
897}
898
899EXPORT_SYMBOL(tcp_v4_md5_do_add);
900
901static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk,
902 u8 *newkey, u8 newkeylen)
903{
904 return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->daddr,
905 newkey, newkeylen);
906}
907
908int tcp_v4_md5_do_del(struct sock *sk, __be32 addr)
909{
910 struct tcp_sock *tp = tcp_sk(sk);
911 int i;
912
913 for (i = 0; i < tp->md5sig_info->entries4; i++) {
914 if (tp->md5sig_info->keys4[i].addr == addr) {
915 /* Free the key */
David S. Millerf8ab18d2007-09-28 15:18:35 -0700916 kfree(tp->md5sig_info->keys4[i].base.key);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800917 tp->md5sig_info->entries4--;
918
919 if (tp->md5sig_info->entries4 == 0) {
920 kfree(tp->md5sig_info->keys4);
921 tp->md5sig_info->keys4 = NULL;
Leigh Brown8228a18d2006-12-17 17:12:30 -0800922 tp->md5sig_info->alloced4 = 0;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200923 } else if (tp->md5sig_info->entries4 != i) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800924 /* Need to do some manipulation */
YOSHIFUJI Hideaki354faf02007-11-20 17:30:31 -0800925 memmove(&tp->md5sig_info->keys4[i],
926 &tp->md5sig_info->keys4[i+1],
927 (tp->md5sig_info->entries4 - i) *
928 sizeof(struct tcp4_md5sig_key));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800929 }
930 tcp_free_md5sig_pool();
931 return 0;
932 }
933 }
934 return -ENOENT;
935}
936
937EXPORT_SYMBOL(tcp_v4_md5_do_del);
938
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200939static void tcp_v4_clear_md5_list(struct sock *sk)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800940{
941 struct tcp_sock *tp = tcp_sk(sk);
942
943 /* Free each key, then the set of key keys,
944 * the crypto element, and then decrement our
945 * hold on the last resort crypto.
946 */
947 if (tp->md5sig_info->entries4) {
948 int i;
949 for (i = 0; i < tp->md5sig_info->entries4; i++)
David S. Millerf8ab18d2007-09-28 15:18:35 -0700950 kfree(tp->md5sig_info->keys4[i].base.key);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800951 tp->md5sig_info->entries4 = 0;
952 tcp_free_md5sig_pool();
953 }
954 if (tp->md5sig_info->keys4) {
955 kfree(tp->md5sig_info->keys4);
956 tp->md5sig_info->keys4 = NULL;
957 tp->md5sig_info->alloced4 = 0;
958 }
959}
960
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200961static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
962 int optlen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800963{
964 struct tcp_md5sig cmd;
965 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
966 u8 *newkey;
967
968 if (optlen < sizeof(cmd))
969 return -EINVAL;
970
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200971 if (copy_from_user(&cmd, optval, sizeof(cmd)))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800972 return -EFAULT;
973
974 if (sin->sin_family != AF_INET)
975 return -EINVAL;
976
977 if (!cmd.tcpm_key || !cmd.tcpm_keylen) {
978 if (!tcp_sk(sk)->md5sig_info)
979 return -ENOENT;
980 return tcp_v4_md5_do_del(sk, sin->sin_addr.s_addr);
981 }
982
983 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
984 return -EINVAL;
985
986 if (!tcp_sk(sk)->md5sig_info) {
987 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200988 struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800989
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800990 if (!p)
991 return -EINVAL;
992
993 tp->md5sig_info = p;
David S. Miller3d7dbea2007-06-12 14:36:42 -0700994 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800995 }
996
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200997 newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800998 if (!newkey)
999 return -ENOMEM;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001000 return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
1001 newkey, cmd.tcpm_keylen);
1002}
1003
1004static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
1005 __be32 saddr, __be32 daddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001006 struct tcphdr *th,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -08001007 unsigned int tcplen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001008{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001009 struct tcp_md5sig_pool *hp;
1010 struct tcp4_pseudohdr *bp;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001011 int err;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001012
1013 /*
1014 * Okay, so RFC2385 is turned on for this connection,
1015 * so we need to generate the MD5 hash for the packet now.
1016 */
1017
1018 hp = tcp_get_md5sig_pool();
1019 if (!hp)
1020 goto clear_hash_noput;
1021
1022 bp = &hp->md5_blk.ip4;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001023
1024 /*
YOSHIFUJI Hideaki8d26d762008-04-17 13:19:16 +09001025 * The TCP pseudo-header (in the order: source IP address,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001026 * destination IP address, zero-padded protocol number, and
1027 * segment length)
1028 */
1029 bp->saddr = saddr;
1030 bp->daddr = daddr;
1031 bp->pad = 0;
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001032 bp->protocol = IPPROTO_TCP;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001033 bp->len = htons(tcplen);
David S. Millerc7da57a2007-10-26 00:41:21 -07001034
YOSHIFUJI Hideaki8d26d762008-04-17 13:19:16 +09001035 err = tcp_calc_md5_hash(md5_hash, key, sizeof(*bp),
1036 th, tcplen, hp);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001037 if (err)
1038 goto clear_hash;
1039
YOSHIFUJI Hideaki8d26d762008-04-17 13:19:16 +09001040 /* Free up the crypto pool */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001041 tcp_put_md5sig_pool();
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001042out:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001043 return 0;
1044clear_hash:
1045 tcp_put_md5sig_pool();
1046clear_hash_noput:
1047 memset(md5_hash, 0, 16);
1048 goto out;
1049}
1050
1051int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
1052 struct sock *sk,
1053 struct dst_entry *dst,
1054 struct request_sock *req,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001055 struct tcphdr *th,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -08001056 unsigned int tcplen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001057{
1058 __be32 saddr, daddr;
1059
1060 if (sk) {
1061 saddr = inet_sk(sk)->saddr;
1062 daddr = inet_sk(sk)->daddr;
1063 } else {
1064 struct rtable *rt = (struct rtable *)dst;
1065 BUG_ON(!rt);
1066 saddr = rt->rt_src;
1067 daddr = rt->rt_dst;
1068 }
1069 return tcp_v4_do_calc_md5_hash(md5_hash, key,
1070 saddr, daddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001071 th, tcplen);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001072}
1073
1074EXPORT_SYMBOL(tcp_v4_calc_md5_hash);
1075
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001076static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001077{
1078 /*
1079 * This gets called for each TCP segment that arrives
1080 * so we want to be efficient.
1081 * We have 3 drop cases:
1082 * o No MD5 hash and one expected.
1083 * o MD5 hash and we're not expecting one.
1084 * o MD5 hash and its wrong.
1085 */
1086 __u8 *hash_location = NULL;
1087 struct tcp_md5sig_key *hash_expected;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001088 const struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001089 struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001090 int genhash;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001091 unsigned char newhash[16];
1092
1093 hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09001094 hash_location = tcp_parse_md5sig_option(th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001095
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001096 /* We've parsed the options - do we have a hash? */
1097 if (!hash_expected && !hash_location)
1098 return 0;
1099
1100 if (hash_expected && !hash_location) {
Leigh Browna9fc00c2006-12-17 17:13:10 -08001101 LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found "
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001102 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001103 NIPQUAD(iph->saddr), ntohs(th->source),
1104 NIPQUAD(iph->daddr), ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001105 return 1;
1106 }
1107
1108 if (!hash_expected && hash_location) {
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001109 LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found "
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001110 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001111 NIPQUAD(iph->saddr), ntohs(th->source),
1112 NIPQUAD(iph->daddr), ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001113 return 1;
1114 }
1115
1116 /* Okay, so this is hash_expected and hash_location -
1117 * so we need to calculate the checksum.
1118 */
1119 genhash = tcp_v4_do_calc_md5_hash(newhash,
1120 hash_expected,
1121 iph->saddr, iph->daddr,
YOSHIFUJI Hideaki076fb722008-04-17 12:48:12 +09001122 th, skb->len);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001123
1124 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
1125 if (net_ratelimit()) {
1126 printk(KERN_INFO "MD5 Hash failed for "
1127 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001128 NIPQUAD(iph->saddr), ntohs(th->source),
1129 NIPQUAD(iph->daddr), ntohs(th->dest),
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001130 genhash ? " tcp_v4_calc_md5_hash failed" : "");
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001131 }
1132 return 1;
1133 }
1134 return 0;
1135}
1136
1137#endif
1138
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001139struct request_sock_ops tcp_request_sock_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 .family = PF_INET,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001141 .obj_size = sizeof(struct tcp_request_sock),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 .rtx_syn_ack = tcp_v4_send_synack,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001143 .send_ack = tcp_v4_reqsk_send_ack,
1144 .destructor = tcp_v4_reqsk_destructor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 .send_reset = tcp_v4_send_reset,
1146};
1147
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001148#ifdef CONFIG_TCP_MD5SIG
Andrew Mortonb6332e62006-11-30 19:16:28 -08001149static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001150 .md5_lookup = tcp_v4_reqsk_md5_lookup,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001151};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001152#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001153
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001154static struct timewait_sock_ops tcp_timewait_sock_ops = {
1155 .twsk_obj_size = sizeof(struct tcp_timewait_sock),
1156 .twsk_unique = tcp_twsk_unique,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001157 .twsk_destructor= tcp_twsk_destructor,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001158};
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
1161{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001162 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct tcp_options_received tmp_opt;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001164 struct request_sock *req;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001165 __be32 saddr = ip_hdr(skb)->saddr;
1166 __be32 daddr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 __u32 isn = TCP_SKB_CB(skb)->when;
1168 struct dst_entry *dst = NULL;
1169#ifdef CONFIG_SYN_COOKIES
1170 int want_cookie = 0;
1171#else
1172#define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
1173#endif
1174
1175 /* Never answer to SYNs send to broadcast or multicast */
Eric Dumazetee6b9672008-03-05 18:30:47 -08001176 if (skb->rtable->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto drop;
1178
1179 /* TW buckets are converted to open requests without
1180 * limitations, they conserve resources and peer is
1181 * evidently real one.
1182 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001183 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184#ifdef CONFIG_SYN_COOKIES
1185 if (sysctl_tcp_syncookies) {
1186 want_cookie = 1;
1187 } else
1188#endif
1189 goto drop;
1190 }
1191
1192 /* Accept backlog is full. If we have already queued enough
1193 * of warm entries in syn queue, drop request. It is better than
1194 * clogging syn queue with openreqs with exponentially increasing
1195 * timeout.
1196 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001197 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 goto drop;
1199
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001200 req = reqsk_alloc(&tcp_request_sock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!req)
1202 goto drop;
1203
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001204#ifdef CONFIG_TCP_MD5SIG
1205 tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
1206#endif
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 tcp_clear_options(&tmp_opt);
1209 tmp_opt.mss_clamp = 536;
1210 tmp_opt.user_mss = tcp_sk(sk)->rx_opt.user_mss;
1211
1212 tcp_parse_options(skb, &tmp_opt, 0);
1213
Florian Westphal4dfc2812008-04-10 03:12:40 -07001214 if (want_cookie && !tmp_opt.saw_tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 tcp_clear_options(&tmp_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
1218 /* Some OSes (unknown ones, but I see them on web server, which
1219 * contains information interesting only for windows'
1220 * users) do not send their stamp in SYN. It is easy case.
1221 * We simply do not advertise TS support.
1222 */
1223 tmp_opt.saw_tstamp = 0;
1224 tmp_opt.tstamp_ok = 0;
1225 }
1226 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1227
1228 tcp_openreq_init(req, &tmp_opt, skb);
1229
Venkat Yekkirala4237c752006-07-24 23:32:50 -07001230 if (security_inet_conn_request(sk, skb, req))
1231 goto drop_and_free;
1232
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001233 ireq = inet_rsk(req);
1234 ireq->loc_addr = daddr;
1235 ireq->rmt_addr = saddr;
1236 ireq->opt = tcp_v4_save_options(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!want_cookie)
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001238 TCP_ECN_create_request(req, tcp_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (want_cookie) {
1241#ifdef CONFIG_SYN_COOKIES
1242 syn_flood_warning(skb);
Florian Westphal4dfc2812008-04-10 03:12:40 -07001243 req->cookie_ts = tmp_opt.tstamp_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244#endif
1245 isn = cookie_v4_init_sequence(sk, skb, &req->mss);
1246 } else if (!isn) {
1247 struct inet_peer *peer = NULL;
1248
1249 /* VJ's idea. We save last timestamp seen
1250 * from the destination in peer table, when entering
1251 * state TIME-WAIT, and check against it before
1252 * accepting new connection request.
1253 *
1254 * If "isn" is not zero, this request hit alive
1255 * timewait bucket, so that all the necessary checks
1256 * are made in the function processing timewait state.
1257 */
1258 if (tmp_opt.saw_tstamp &&
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07001259 tcp_death_row.sysctl_tw_recycle &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001260 (dst = inet_csk_route_req(sk, req)) != NULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
1262 peer->v4daddr == saddr) {
James Morris9d729f72007-03-04 16:12:44 -08001263 if (get_seconds() < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 (s32)(peer->tcp_ts - req->ts_recent) >
1265 TCP_PAWS_WINDOW) {
1266 NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001267 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 }
1270 /* Kill the following clause, if you dislike this way. */
1271 else if (!sysctl_tcp_syncookies &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001272 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 (sysctl_max_syn_backlog >> 2)) &&
1274 (!peer || !peer->tcp_ts_stamp) &&
1275 (!dst || !dst_metric(dst, RTAX_RTT))) {
1276 /* Without syncookies last quarter of
1277 * backlog is filled with destinations,
1278 * proven to be alive.
1279 * It means that we continue to communicate
1280 * to destinations, already remembered
1281 * to the moment of synflood.
1282 */
Patrick McHardy64ce2072005-08-09 20:50:53 -07001283 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open "
YOSHIFUJI Hideakia7d632b2008-04-14 04:09:00 -07001284 "request from " NIPQUAD_FMT "/%u\n",
Patrick McHardy64ce2072005-08-09 20:50:53 -07001285 NIPQUAD(saddr),
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001286 ntohs(tcp_hdr(skb)->source));
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001287 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Gerrit Renkera94f7232006-11-10 14:06:49 -08001290 isn = tcp_v4_init_sequence(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001292 tcp_rsk(req)->snt_isn = isn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001294 if (__tcp_v4_send_synack(sk, req, dst) || want_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto drop_and_free;
1296
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001297 inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return 0;
1299
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001300drop_and_release:
1301 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302drop_and_free:
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001303 reqsk_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return 0;
1306}
1307
1308
1309/*
1310 * The three way handshake has completed - we got a valid synack -
1311 * now create the new socket.
1312 */
1313struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001314 struct request_sock *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct dst_entry *dst)
1316{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001317 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct inet_sock *newinet;
1319 struct tcp_sock *newtp;
1320 struct sock *newsk;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001321#ifdef CONFIG_TCP_MD5SIG
1322 struct tcp_md5sig_key *key;
1323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (sk_acceptq_is_full(sk))
1326 goto exit_overflow;
1327
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001328 if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto exit;
1330
1331 newsk = tcp_create_openreq_child(sk, req, skb);
1332 if (!newsk)
1333 goto exit;
1334
Herbert Xubcd76112006-06-30 13:36:35 -07001335 newsk->sk_gso_type = SKB_GSO_TCPV4;
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -07001336 sk_setup_caps(newsk, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 newtp = tcp_sk(newsk);
1339 newinet = inet_sk(newsk);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001340 ireq = inet_rsk(req);
1341 newinet->daddr = ireq->rmt_addr;
1342 newinet->rcv_saddr = ireq->loc_addr;
1343 newinet->saddr = ireq->loc_addr;
1344 newinet->opt = ireq->opt;
1345 ireq->opt = NULL;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001346 newinet->mc_index = inet_iif(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001347 newinet->mc_ttl = ip_hdr(skb)->ttl;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001348 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (newinet->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001350 inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 newinet->id = newtp->write_seq ^ jiffies;
1352
John Heffner5d424d52006-03-20 17:53:41 -08001353 tcp_mtup_init(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 tcp_sync_mss(newsk, dst_mtu(dst));
1355 newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
1356 tcp_initialize_rcv_mss(newsk);
1357
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001358#ifdef CONFIG_TCP_MD5SIG
1359 /* Copy over the MD5 key from the original socket */
1360 if ((key = tcp_v4_md5_do_lookup(sk, newinet->daddr)) != NULL) {
1361 /*
1362 * We're using one, so create a matching key
1363 * on the newsk structure. If we fail to get
1364 * memory, then we end up not copying the key
1365 * across. Shucks.
1366 */
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -02001367 char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1368 if (newkey != NULL)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001369 tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr,
1370 newkey, key->keylen);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001371 }
1372#endif
1373
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001374 __inet_hash_nolisten(newsk);
1375 __inet_inherit_port(sk, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 return newsk;
1378
1379exit_overflow:
1380 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
1381exit:
1382 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
1383 dst_release(dst);
1384 return NULL;
1385}
1386
1387static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
1388{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001389 struct tcphdr *th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001390 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct sock *nsk;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001392 struct request_sock **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /* Find possible connection requests. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001394 struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
1395 iph->saddr, iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (req)
1397 return tcp_check_req(sk, skb, req, prev);
1398
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001399 nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001400 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if (nsk) {
1403 if (nsk->sk_state != TCP_TIME_WAIT) {
1404 bh_lock_sock(nsk);
1405 return nsk;
1406 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001407 inet_twsk_put(inet_twsk(nsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return NULL;
1409 }
1410
1411#ifdef CONFIG_SYN_COOKIES
1412 if (!th->rst && !th->syn && th->ack)
1413 sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
1414#endif
1415 return sk;
1416}
1417
Al Virob51655b2006-11-14 21:40:42 -08001418static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001420 const struct iphdr *iph = ip_hdr(skb);
1421
Patrick McHardy84fa7932006-08-29 16:44:56 -07001422 if (skb->ip_summed == CHECKSUM_COMPLETE) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001423 if (!tcp_v4_check(skb->len, iph->saddr,
1424 iph->daddr, skb->csum)) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001425 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return 0;
Herbert Xufb286bb2005-11-10 13:01:24 -08001427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001429
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001430 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
Herbert Xufb286bb2005-11-10 13:01:24 -08001431 skb->len, IPPROTO_TCP, 0);
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (skb->len <= 76) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001434 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 return 0;
1437}
1438
1439
1440/* The socket must have it's spinlock held when we get
1441 * here.
1442 *
1443 * We have a potential double-lock case here, so even when
1444 * doing backlog processing we use the BH locking scheme.
1445 * This is because we cannot sleep with the original spinlock
1446 * held.
1447 */
1448int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
1449{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001450 struct sock *rsk;
1451#ifdef CONFIG_TCP_MD5SIG
1452 /*
1453 * We really want to reject the packet as early as possible
1454 * if:
1455 * o We're expecting an MD5'd packet and this is no MD5 tcp option
1456 * o There is an MD5 option and we're not expecting one
1457 */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001458 if (tcp_v4_inbound_md5_hash(sk, skb))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001459 goto discard;
1460#endif
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1463 TCP_CHECK_TIMER(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001464 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001465 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 TCP_CHECK_TIMER(sk);
1469 return 0;
1470 }
1471
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001472 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 goto csum_err;
1474
1475 if (sk->sk_state == TCP_LISTEN) {
1476 struct sock *nsk = tcp_v4_hnd_req(sk, skb);
1477 if (!nsk)
1478 goto discard;
1479
1480 if (nsk != sk) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001481 if (tcp_child_process(sk, nsk, skb)) {
1482 rsk = nsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return 0;
1486 }
1487 }
1488
1489 TCP_CHECK_TIMER(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001490 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001491 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 TCP_CHECK_TIMER(sk);
1495 return 0;
1496
1497reset:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001498 tcp_v4_send_reset(rsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499discard:
1500 kfree_skb(skb);
1501 /* Be careful here. If this function gets more complicated and
1502 * gcc suffers from register pressure on the x86, sk (in %ebx)
1503 * might be destroyed here. This current version compiles correctly,
1504 * but you have been warned.
1505 */
1506 return 0;
1507
1508csum_err:
1509 TCP_INC_STATS_BH(TCP_MIB_INERRS);
1510 goto discard;
1511}
1512
1513/*
1514 * From tcp_input.c
1515 */
1516
1517int tcp_v4_rcv(struct sk_buff *skb)
1518{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001519 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 struct tcphdr *th;
1521 struct sock *sk;
1522 int ret;
1523
1524 if (skb->pkt_type != PACKET_HOST)
1525 goto discard_it;
1526
1527 /* Count it even if it's bad */
1528 TCP_INC_STATS_BH(TCP_MIB_INSEGS);
1529
1530 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1531 goto discard_it;
1532
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001533 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (th->doff < sizeof(struct tcphdr) / 4)
1536 goto bad_packet;
1537 if (!pskb_may_pull(skb, th->doff * 4))
1538 goto discard_it;
1539
1540 /* An explanation is required here, I think.
1541 * Packet length and doff are validated by header prediction,
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001542 * provided case of th->doff==0 is eliminated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * So, we defer the checks. */
Herbert Xu60476372007-04-09 11:59:39 -07001544 if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 goto bad_packet;
1546
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001547 th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001548 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1550 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1551 skb->len - th->doff * 4);
1552 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1553 TCP_SKB_CB(skb)->when = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001554 TCP_SKB_CB(skb)->flags = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 TCP_SKB_CB(skb)->sacked = 0;
1556
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001557 sk = __inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001558 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!sk)
1560 goto no_tcp_socket;
1561
1562process:
1563 if (sk->sk_state == TCP_TIME_WAIT)
1564 goto do_time_wait;
1565
1566 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1567 goto discard_and_relse;
Patrick McHardyb59c2702006-01-06 23:06:10 -08001568 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001570 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 goto discard_and_relse;
1572
1573 skb->dev = NULL;
1574
Ingo Molnarc6366182006-07-03 00:25:13 -07001575 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 ret = 0;
1577 if (!sock_owned_by_user(sk)) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001578#ifdef CONFIG_NET_DMA
1579 struct tcp_sock *tp = tcp_sk(sk);
1580 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1581 tp->ucopy.dma_chan = get_softnet_dma();
1582 if (tp->ucopy.dma_chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ret = tcp_v4_do_rcv(sk, skb);
Chris Leech1a2449a2006-05-23 18:05:53 -07001584 else
1585#endif
1586 {
1587 if (!tcp_prequeue(sk, skb))
1588 ret = tcp_v4_do_rcv(sk, skb);
1589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 } else
1591 sk_add_backlog(sk, skb);
1592 bh_unlock_sock(sk);
1593
1594 sock_put(sk);
1595
1596 return ret;
1597
1598no_tcp_socket:
1599 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1600 goto discard_it;
1601
1602 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1603bad_packet:
1604 TCP_INC_STATS_BH(TCP_MIB_INERRS);
1605 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001606 tcp_v4_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609discard_it:
1610 /* Discard frame. */
1611 kfree_skb(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614discard_and_relse:
1615 sock_put(sk);
1616 goto discard_it;
1617
1618do_time_wait:
1619 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001620 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 goto discard_it;
1622 }
1623
1624 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1625 TCP_INC_STATS_BH(TCP_MIB_INERRS);
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001626 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 goto discard_it;
1628 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001629 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 case TCP_TW_SYN: {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001631 struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001632 &tcp_hashinfo,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001633 iph->daddr, th->dest,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001634 inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (sk2) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001636 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
1637 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 sk = sk2;
1639 goto process;
1640 }
1641 /* Fall through to ACK */
1642 }
1643 case TCP_TW_ACK:
1644 tcp_v4_timewait_ack(sk, skb);
1645 break;
1646 case TCP_TW_RST:
1647 goto no_tcp_socket;
1648 case TCP_TW_SUCCESS:;
1649 }
1650 goto discard_it;
1651}
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653/* VJ's idea. Save last timestamp seen from this destination
1654 * and hold it at least for normal timewait interval to use for duplicate
1655 * segment detection in subsequent connections, before they enter synchronized
1656 * state.
1657 */
1658
1659int tcp_v4_remember_stamp(struct sock *sk)
1660{
1661 struct inet_sock *inet = inet_sk(sk);
1662 struct tcp_sock *tp = tcp_sk(sk);
1663 struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
1664 struct inet_peer *peer = NULL;
1665 int release_it = 0;
1666
1667 if (!rt || rt->rt_dst != inet->daddr) {
1668 peer = inet_getpeer(inet->daddr, 1);
1669 release_it = 1;
1670 } else {
1671 if (!rt->peer)
1672 rt_bind_peer(rt, 1);
1673 peer = rt->peer;
1674 }
1675
1676 if (peer) {
1677 if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
James Morris9d729f72007-03-04 16:12:44 -08001678 (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
1680 peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
1681 peer->tcp_ts = tp->rx_opt.ts_recent;
1682 }
1683 if (release_it)
1684 inet_putpeer(peer);
1685 return 1;
1686 }
1687
1688 return 0;
1689}
1690
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001691int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001693 struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 if (peer) {
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001696 const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
1697
1698 if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
James Morris9d729f72007-03-04 16:12:44 -08001699 (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001700 peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) {
1701 peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp;
1702 peer->tcp_ts = tcptw->tw_ts_recent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704 inet_putpeer(peer);
1705 return 1;
1706 }
1707
1708 return 0;
1709}
1710
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001711struct inet_connection_sock_af_ops ipv4_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001712 .queue_xmit = ip_queue_xmit,
1713 .send_check = tcp_v4_send_check,
1714 .rebuild_header = inet_sk_rebuild_header,
1715 .conn_request = tcp_v4_conn_request,
1716 .syn_recv_sock = tcp_v4_syn_recv_sock,
1717 .remember_stamp = tcp_v4_remember_stamp,
1718 .net_header_len = sizeof(struct iphdr),
1719 .setsockopt = ip_setsockopt,
1720 .getsockopt = ip_getsockopt,
1721 .addr2sockaddr = inet_csk_addr2sockaddr,
1722 .sockaddr_len = sizeof(struct sockaddr_in),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001723 .bind_conflict = inet_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001724#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001725 .compat_setsockopt = compat_ip_setsockopt,
1726 .compat_getsockopt = compat_ip_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001727#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728};
1729
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001730#ifdef CONFIG_TCP_MD5SIG
Andrew Mortonb6332e62006-11-30 19:16:28 -08001731static struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001732 .md5_lookup = tcp_v4_md5_lookup,
1733 .calc_md5_hash = tcp_v4_calc_md5_hash,
1734 .md5_add = tcp_v4_md5_add_func,
1735 .md5_parse = tcp_v4_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001736};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001737#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739/* NOTE: A lot of things set to zero explicitly by call to
1740 * sk_alloc() so need not be done here.
1741 */
1742static int tcp_v4_init_sock(struct sock *sk)
1743{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001744 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct tcp_sock *tp = tcp_sk(sk);
1746
1747 skb_queue_head_init(&tp->out_of_order_queue);
1748 tcp_init_xmit_timers(sk);
1749 tcp_prequeue_init(tp);
1750
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001751 icsk->icsk_rto = TCP_TIMEOUT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 tp->mdev = TCP_TIMEOUT_INIT;
1753
1754 /* So many TCP implementations out there (incorrectly) count the
1755 * initial SYN frame in their delayed-ACK and congestion control
1756 * algorithms that we must have the following bandaid to talk
1757 * efficiently to them. -DaveM
1758 */
1759 tp->snd_cwnd = 2;
1760
1761 /* See draft-stevens-tcpca-spec-01 for discussion of the
1762 * initialization of these values.
1763 */
1764 tp->snd_ssthresh = 0x7fffffff; /* Infinity */
1765 tp->snd_cwnd_clamp = ~0;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001766 tp->mss_cache = 536;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 tp->reordering = sysctl_tcp_reordering;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001769 icsk->icsk_ca_ops = &tcp_init_congestion_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 sk->sk_state = TCP_CLOSE;
1772
1773 sk->sk_write_space = sk_stream_write_space;
1774 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1775
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001776 icsk->icsk_af_ops = &ipv4_specific;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001777 icsk->icsk_sync_mss = tcp_sync_mss;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001778#ifdef CONFIG_TCP_MD5SIG
1779 tp->af_specific = &tcp_sock_ipv4_specific;
1780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 sk->sk_sndbuf = sysctl_tcp_wmem[1];
1783 sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1784
1785 atomic_inc(&tcp_sockets_allocated);
1786
1787 return 0;
1788}
1789
1790int tcp_v4_destroy_sock(struct sock *sk)
1791{
1792 struct tcp_sock *tp = tcp_sk(sk);
1793
1794 tcp_clear_xmit_timers(sk);
1795
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001796 tcp_cleanup_congestion_control(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /* Cleanup up the write buffer. */
David S. Millerfe067e82007-03-07 12:12:44 -08001799 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* Cleans up our, hopefully empty, out_of_order_queue. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001802 __skb_queue_purge(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001804#ifdef CONFIG_TCP_MD5SIG
1805 /* Clean up the MD5 key list, if any */
1806 if (tp->md5sig_info) {
1807 tcp_v4_clear_md5_list(sk);
1808 kfree(tp->md5sig_info);
1809 tp->md5sig_info = NULL;
1810 }
1811#endif
1812
Chris Leech1a2449a2006-05-23 18:05:53 -07001813#ifdef CONFIG_NET_DMA
1814 /* Cleans up our sk_async_wait_queue */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001815 __skb_queue_purge(&sk->sk_async_wait_queue);
Chris Leech1a2449a2006-05-23 18:05:53 -07001816#endif
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /* Clean prequeue, it must be empty really */
1819 __skb_queue_purge(&tp->ucopy.prequeue);
1820
1821 /* Clean up a referenced TCP bind bucket. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001822 if (inet_csk(sk)->icsk_bind_hash)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001823 inet_put_port(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /*
1826 * If sendmsg cached page exists, toss it.
1827 */
1828 if (sk->sk_sndmsg_page) {
1829 __free_page(sk->sk_sndmsg_page);
1830 sk->sk_sndmsg_page = NULL;
1831 }
1832
Patrick McManusec3c0982008-03-21 16:33:01 -07001833 if (tp->defer_tcp_accept.request) {
1834 reqsk_free(tp->defer_tcp_accept.request);
1835 sock_put(tp->defer_tcp_accept.listen_sk);
1836 sock_put(sk);
1837 tp->defer_tcp_accept.listen_sk = NULL;
1838 tp->defer_tcp_accept.request = NULL;
1839 }
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 atomic_dec(&tcp_sockets_allocated);
1842
1843 return 0;
1844}
1845
1846EXPORT_SYMBOL(tcp_v4_destroy_sock);
1847
1848#ifdef CONFIG_PROC_FS
1849/* Proc filesystem TCP sock list dumping. */
1850
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001851static inline struct inet_timewait_sock *tw_head(struct hlist_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 return hlist_empty(head) ? NULL :
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001854 list_entry(head->first, struct inet_timewait_sock, tw_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001857static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 return tw->tw_node.next ?
1860 hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
1861}
1862
1863static void *listening_get_next(struct seq_file *seq, void *cur)
1864{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001865 struct inet_connection_sock *icsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct hlist_node *node;
1867 struct sock *sk = cur;
1868 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001869 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 if (!sk) {
1872 st->bucket = 0;
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001873 sk = sk_head(&tcp_hashinfo.listening_hash[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto get_sk;
1875 }
1876
1877 ++st->num;
1878
1879 if (st->state == TCP_SEQ_STATE_OPENREQ) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001880 struct request_sock *req = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001882 icsk = inet_csk(st->syn_wait_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 req = req->dl_next;
1884 while (1) {
1885 while (req) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07001886 if (req->rsk_ops->family == st->family &&
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001887 net_eq(sock_net(req->sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 cur = req;
1889 goto out;
1890 }
1891 req = req->dl_next;
1892 }
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001893 if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895get_req:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001896 req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 sk = sk_next(st->syn_wait_sk);
1899 st->state = TCP_SEQ_STATE_LISTENING;
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 } else {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001902 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001903 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1904 if (reqsk_queue_len(&icsk->icsk_accept_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 goto start_req;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001906 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 sk = sk_next(sk);
1908 }
1909get_sk:
1910 sk_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001911 if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 cur = sk;
1913 goto out;
1914 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001915 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001916 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1917 if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918start_req:
1919 st->uid = sock_i_uid(sk);
1920 st->syn_wait_sk = sk;
1921 st->state = TCP_SEQ_STATE_OPENREQ;
1922 st->sbucket = 0;
1923 goto get_req;
1924 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001925 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -07001927 if (++st->bucket < INET_LHTABLE_SIZE) {
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001928 sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 goto get_sk;
1930 }
1931 cur = NULL;
1932out:
1933 return cur;
1934}
1935
1936static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
1937{
1938 void *rc = listening_get_next(seq, NULL);
1939
1940 while (rc && *pos) {
1941 rc = listening_get_next(seq, rc);
1942 --*pos;
1943 }
1944 return rc;
1945}
1946
1947static void *established_get_first(struct seq_file *seq)
1948{
1949 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001950 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 void *rc = NULL;
1952
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001953 for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct sock *sk;
1955 struct hlist_node *node;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001956 struct inet_timewait_sock *tw;
Eric Dumazet230140c2007-11-07 02:40:20 -08001957 rwlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Eric Dumazet230140c2007-11-07 02:40:20 -08001959 read_lock_bh(lock);
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001960 sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07001961 if (sk->sk_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001962 !net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 continue;
1964 }
1965 rc = sk;
1966 goto out;
1967 }
1968 st->state = TCP_SEQ_STATE_TIME_WAIT;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001969 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08001970 &tcp_hashinfo.ehash[st->bucket].twchain) {
Pavel Emelyanov28518fc2008-03-21 15:52:00 -07001971 if (tw->tw_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001972 !net_eq(twsk_net(tw), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 continue;
1974 }
1975 rc = tw;
1976 goto out;
1977 }
Eric Dumazet230140c2007-11-07 02:40:20 -08001978 read_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 st->state = TCP_SEQ_STATE_ESTABLISHED;
1980 }
1981out:
1982 return rc;
1983}
1984
1985static void *established_get_next(struct seq_file *seq, void *cur)
1986{
1987 struct sock *sk = cur;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001988 struct inet_timewait_sock *tw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct hlist_node *node;
1990 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001991 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 ++st->num;
1994
1995 if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
1996 tw = cur;
1997 tw = tw_next(tw);
1998get_tw:
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001999 while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 tw = tw_next(tw);
2001 }
2002 if (tw) {
2003 cur = tw;
2004 goto out;
2005 }
Eric Dumazet230140c2007-11-07 02:40:20 -08002006 read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 st->state = TCP_SEQ_STATE_ESTABLISHED;
2008
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002009 if (++st->bucket < tcp_hashinfo.ehash_size) {
Eric Dumazet230140c2007-11-07 02:40:20 -08002010 read_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002011 sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 } else {
2013 cur = NULL;
2014 goto out;
2015 }
2016 } else
2017 sk = sk_next(sk);
2018
2019 sk_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002020 if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 goto found;
2022 }
2023
2024 st->state = TCP_SEQ_STATE_TIME_WAIT;
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002025 tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 goto get_tw;
2027found:
2028 cur = sk;
2029out:
2030 return cur;
2031}
2032
2033static void *established_get_idx(struct seq_file *seq, loff_t pos)
2034{
2035 void *rc = established_get_first(seq);
2036
2037 while (rc && pos) {
2038 rc = established_get_next(seq, rc);
2039 --pos;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return rc;
2042}
2043
2044static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
2045{
2046 void *rc;
2047 struct tcp_iter_state* st = seq->private;
2048
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002049 inet_listen_lock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 st->state = TCP_SEQ_STATE_LISTENING;
2051 rc = listening_get_idx(seq, &pos);
2052
2053 if (!rc) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002054 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 st->state = TCP_SEQ_STATE_ESTABLISHED;
2056 rc = established_get_idx(seq, pos);
2057 }
2058
2059 return rc;
2060}
2061
2062static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
2063{
2064 struct tcp_iter_state* st = seq->private;
2065 st->state = TCP_SEQ_STATE_LISTENING;
2066 st->num = 0;
2067 return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2068}
2069
2070static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2071{
2072 void *rc = NULL;
2073 struct tcp_iter_state* st;
2074
2075 if (v == SEQ_START_TOKEN) {
2076 rc = tcp_get_idx(seq, 0);
2077 goto out;
2078 }
2079 st = seq->private;
2080
2081 switch (st->state) {
2082 case TCP_SEQ_STATE_OPENREQ:
2083 case TCP_SEQ_STATE_LISTENING:
2084 rc = listening_get_next(seq, v);
2085 if (!rc) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002086 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 st->state = TCP_SEQ_STATE_ESTABLISHED;
2088 rc = established_get_first(seq);
2089 }
2090 break;
2091 case TCP_SEQ_STATE_ESTABLISHED:
2092 case TCP_SEQ_STATE_TIME_WAIT:
2093 rc = established_get_next(seq, v);
2094 break;
2095 }
2096out:
2097 ++*pos;
2098 return rc;
2099}
2100
2101static void tcp_seq_stop(struct seq_file *seq, void *v)
2102{
2103 struct tcp_iter_state* st = seq->private;
2104
2105 switch (st->state) {
2106 case TCP_SEQ_STATE_OPENREQ:
2107 if (v) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002108 struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
2109 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111 case TCP_SEQ_STATE_LISTENING:
2112 if (v != SEQ_START_TOKEN)
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002113 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 break;
2115 case TCP_SEQ_STATE_TIME_WAIT:
2116 case TCP_SEQ_STATE_ESTABLISHED:
2117 if (v)
Eric Dumazet230140c2007-11-07 02:40:20 -08002118 read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 break;
2120 }
2121}
2122
2123static int tcp_seq_open(struct inode *inode, struct file *file)
2124{
2125 struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 struct tcp_iter_state *s;
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002127 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002129 err = seq_open_net(inode, file, &afinfo->seq_ops,
2130 sizeof(struct tcp_iter_state));
2131 if (err < 0)
2132 return err;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002133
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002134 s = ((struct seq_file *)file->private_data)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 s->family = afinfo->family;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002136 return 0;
2137}
2138
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002139int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 int rc = 0;
2142 struct proc_dir_entry *p;
2143
Denis V. Lunev68fcadd2008-04-13 22:13:30 -07002144 afinfo->seq_fops.open = tcp_seq_open;
2145 afinfo->seq_fops.read = seq_read;
2146 afinfo->seq_fops.llseek = seq_lseek;
2147 afinfo->seq_fops.release = seq_release_net;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002148
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002149 afinfo->seq_ops.start = tcp_seq_start;
2150 afinfo->seq_ops.next = tcp_seq_next;
2151 afinfo->seq_ops.stop = tcp_seq_stop;
2152
Denis V. Lunev84841c32008-05-02 04:10:08 -07002153 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
2154 &afinfo->seq_fops, afinfo);
2155 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 rc = -ENOMEM;
2157 return rc;
2158}
2159
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002160void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002162 proc_net_remove(net, afinfo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002165static void get_openreq4(struct sock *sk, struct request_sock *req,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002166 struct seq_file *f, int i, int uid, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002168 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int ttd = req->expires - jiffies;
2170
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002171 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2172 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 i,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002174 ireq->loc_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 ntohs(inet_sk(sk)->sport),
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002176 ireq->rmt_addr,
2177 ntohs(ireq->rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 TCP_SYN_RECV,
2179 0, 0, /* could print option size, but that is af dependent. */
2180 1, /* timers active (only the expire timer) */
2181 jiffies_to_clock_t(ttd),
2182 req->retrans,
2183 uid,
2184 0, /* non standard timer */
2185 0, /* open_requests have no inode */
2186 atomic_read(&sk->sk_refcnt),
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002187 req,
2188 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002191static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 int timer_active;
2194 unsigned long timer_expires;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002195 struct tcp_sock *tp = tcp_sk(sk);
2196 const struct inet_connection_sock *icsk = inet_csk(sk);
2197 struct inet_sock *inet = inet_sk(sk);
Al Viro714e85b2006-11-14 20:51:49 -08002198 __be32 dest = inet->daddr;
2199 __be32 src = inet->rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 __u16 destp = ntohs(inet->dport);
2201 __u16 srcp = ntohs(inet->sport);
2202
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002203 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002205 timer_expires = icsk->icsk_timeout;
2206 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002208 timer_expires = icsk->icsk_timeout;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002209 } else if (timer_pending(&sk->sk_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 timer_active = 2;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002211 timer_expires = sk->sk_timer.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 } else {
2213 timer_active = 0;
2214 timer_expires = jiffies;
2215 }
2216
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002217 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
2218 "%08X %5d %8d %lu %d %p %u %u %u %u %d%n",
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002219 i, src, srcp, dest, destp, sk->sk_state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07002220 tp->write_seq - tp->snd_una,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002221 sk->sk_state == TCP_LISTEN ? sk->sk_ack_backlog :
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002222 (tp->rcv_nxt - tp->copied_seq),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 timer_active,
2224 jiffies_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002225 icsk->icsk_retransmits,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002226 sock_i_uid(sk),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002227 icsk->icsk_probes_out,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002228 sock_i_ino(sk),
2229 atomic_read(&sk->sk_refcnt), sk,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002230 icsk->icsk_rto,
2231 icsk->icsk_ack.ato,
2232 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 tp->snd_cwnd,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002234 tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh,
2235 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002238static void get_timewait4_sock(struct inet_timewait_sock *tw,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002239 struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Al Viro23f33c22006-09-27 18:43:50 -07002241 __be32 dest, src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 __u16 destp, srcp;
2243 int ttd = tw->tw_ttd - jiffies;
2244
2245 if (ttd < 0)
2246 ttd = 0;
2247
2248 dest = tw->tw_daddr;
2249 src = tw->tw_rcv_saddr;
2250 destp = ntohs(tw->tw_dport);
2251 srcp = ntohs(tw->tw_sport);
2252
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002253 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2254 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
2256 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002257 atomic_read(&tw->tw_refcnt), tw, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
2260#define TMPSZ 150
2261
2262static int tcp4_seq_show(struct seq_file *seq, void *v)
2263{
2264 struct tcp_iter_state* st;
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002265 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 if (v == SEQ_START_TOKEN) {
2268 seq_printf(seq, "%-*s\n", TMPSZ - 1,
2269 " sl local_address rem_address st tx_queue "
2270 "rx_queue tr tm->when retrnsmt uid timeout "
2271 "inode");
2272 goto out;
2273 }
2274 st = seq->private;
2275
2276 switch (st->state) {
2277 case TCP_SEQ_STATE_LISTENING:
2278 case TCP_SEQ_STATE_ESTABLISHED:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002279 get_tcp4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 break;
2281 case TCP_SEQ_STATE_OPENREQ:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002282 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 break;
2284 case TCP_SEQ_STATE_TIME_WAIT:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002285 get_timewait4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 break;
2287 }
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002288 seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289out:
2290 return 0;
2291}
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293static struct tcp_seq_afinfo tcp4_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 .name = "tcp",
2295 .family = AF_INET,
Denis V. Lunev5f4472c2008-04-13 22:13:53 -07002296 .seq_fops = {
2297 .owner = THIS_MODULE,
2298 },
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002299 .seq_ops = {
2300 .show = tcp4_seq_show,
2301 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302};
2303
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002304static int tcp4_proc_init_net(struct net *net)
2305{
2306 return tcp_proc_register(net, &tcp4_seq_afinfo);
2307}
2308
2309static void tcp4_proc_exit_net(struct net *net)
2310{
2311 tcp_proc_unregister(net, &tcp4_seq_afinfo);
2312}
2313
2314static struct pernet_operations tcp4_net_ops = {
2315 .init = tcp4_proc_init_net,
2316 .exit = tcp4_proc_exit_net,
2317};
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319int __init tcp4_proc_init(void)
2320{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002321 return register_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
2324void tcp4_proc_exit(void)
2325{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002326 unregister_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328#endif /* CONFIG_PROC_FS */
2329
2330struct proto tcp_prot = {
2331 .name = "TCP",
2332 .owner = THIS_MODULE,
2333 .close = tcp_close,
2334 .connect = tcp_v4_connect,
2335 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002336 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 .ioctl = tcp_ioctl,
2338 .init = tcp_v4_init_sock,
2339 .destroy = tcp_v4_destroy_sock,
2340 .shutdown = tcp_shutdown,
2341 .setsockopt = tcp_setsockopt,
2342 .getsockopt = tcp_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 .recvmsg = tcp_recvmsg,
2344 .backlog_rcv = tcp_v4_do_rcv,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08002345 .hash = inet_hash,
2346 .unhash = inet_unhash,
2347 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 .enter_memory_pressure = tcp_enter_memory_pressure,
2349 .sockets_allocated = &tcp_sockets_allocated,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07002350 .orphan_count = &tcp_orphan_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 .memory_allocated = &tcp_memory_allocated,
2352 .memory_pressure = &tcp_memory_pressure,
2353 .sysctl_mem = sysctl_tcp_mem,
2354 .sysctl_wmem = sysctl_tcp_wmem,
2355 .sysctl_rmem = sysctl_tcp_rmem,
2356 .max_header = MAX_TCP_HEADER,
2357 .obj_size = sizeof(struct tcp_sock),
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002358 .twsk_prot = &tcp_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002359 .rsk_prot = &tcp_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07002360 .h.hashinfo = &tcp_hashinfo,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002361#ifdef CONFIG_COMPAT
2362 .compat_setsockopt = compat_tcp_setsockopt,
2363 .compat_getsockopt = compat_tcp_getsockopt,
2364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365};
2366
Denis V. Lunev046ee902008-04-03 14:31:33 -07002367
2368static int __net_init tcp_sk_init(struct net *net)
2369{
2370 return inet_ctl_sock_create(&net->ipv4.tcp_sock,
2371 PF_INET, SOCK_RAW, IPPROTO_TCP, net);
2372}
2373
2374static void __net_exit tcp_sk_exit(struct net *net)
2375{
2376 inet_ctl_sock_destroy(net->ipv4.tcp_sock);
2377}
2378
2379static struct pernet_operations __net_initdata tcp_sk_ops = {
2380 .init = tcp_sk_init,
2381 .exit = tcp_sk_exit,
2382};
2383
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08002384void __init tcp_v4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Denis V. Lunev046ee902008-04-03 14:31:33 -07002386 if (register_pernet_device(&tcp_sk_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 panic("Failed to create the TCP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
2390EXPORT_SYMBOL(ipv4_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391EXPORT_SYMBOL(tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392EXPORT_SYMBOL(tcp_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393EXPORT_SYMBOL(tcp_v4_conn_request);
2394EXPORT_SYMBOL(tcp_v4_connect);
2395EXPORT_SYMBOL(tcp_v4_do_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396EXPORT_SYMBOL(tcp_v4_remember_stamp);
2397EXPORT_SYMBOL(tcp_v4_send_check);
2398EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
2399
2400#ifdef CONFIG_PROC_FS
2401EXPORT_SYMBOL(tcp_proc_register);
2402EXPORT_SYMBOL(tcp_proc_unregister);
2403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404EXPORT_SYMBOL(sysctl_tcp_low_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405