blob: ffe869ac1bcf15b51abd801b3b3b4533da0b530e [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080089#ifdef CONFIG_TCP_MD5SIG
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020090static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk,
91 __be32 addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080092static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020093 __be32 saddr, __be32 daddr,
94 struct tcphdr *th, int protocol,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -080095 unsigned int tcplen);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080096#endif
97
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -070098struct inet_hashinfo __cacheline_aligned tcp_hashinfo = {
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020099 .lhash_lock = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock),
100 .lhash_users = ATOMIC_INIT(0),
101 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Gerrit Renkera94f7232006-11-10 14:06:49 -0800104static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700106 return secure_tcp_sequence_number(ip_hdr(skb)->daddr,
107 ip_hdr(skb)->saddr,
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700108 tcp_hdr(skb)->dest,
109 tcp_hdr(skb)->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800112int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
113{
114 const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
115 struct tcp_sock *tp = tcp_sk(sk);
116
117 /* With PAWS, it is safe from the viewpoint
118 of data integrity. Even without PAWS it is safe provided sequence
119 spaces do not overlap i.e. at data rates <= 80Mbit/sec.
120
121 Actually, the idea is close to VJ's one, only timestamp cache is
122 held not per host, but per port pair and TW bucket is used as state
123 holder.
124
125 If TW bucket has been already destroyed we fall back to VJ's scheme
126 and use initial timestamp retrieved from peer table.
127 */
128 if (tcptw->tw_ts_recent_stamp &&
129 (twp == NULL || (sysctl_tcp_tw_reuse &&
James Morris9d729f72007-03-04 16:12:44 -0800130 get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800131 tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
132 if (tp->write_seq == 0)
133 tp->write_seq = 1;
134 tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
135 tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
136 sock_hold(sktw);
137 return 1;
138 }
139
140 return 0;
141}
142
143EXPORT_SYMBOL_GPL(tcp_twsk_unique);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* This will initiate an outgoing connection. */
146int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
147{
148 struct inet_sock *inet = inet_sk(sk);
149 struct tcp_sock *tp = tcp_sk(sk);
150 struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
151 struct rtable *rt;
Al Virobada8ad2006-09-26 21:27:15 -0700152 __be32 daddr, nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int tmp;
154 int err;
155
156 if (addr_len < sizeof(struct sockaddr_in))
157 return -EINVAL;
158
159 if (usin->sin_family != AF_INET)
160 return -EAFNOSUPPORT;
161
162 nexthop = daddr = usin->sin_addr.s_addr;
163 if (inet->opt && inet->opt->srr) {
164 if (!daddr)
165 return -EINVAL;
166 nexthop = inet->opt->faddr;
167 }
168
169 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
170 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
171 IPPROTO_TCP,
David S. Miller8eb90862007-02-08 02:09:21 -0800172 inet->sport, usin->sin_port, sk, 1);
Wei Dong584bdf82007-05-31 22:49:28 -0700173 if (tmp < 0) {
174 if (tmp == -ENETUNREACH)
175 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return tmp;
Wei Dong584bdf82007-05-31 22:49:28 -0700177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
180 ip_rt_put(rt);
181 return -ENETUNREACH;
182 }
183
184 if (!inet->opt || !inet->opt->srr)
185 daddr = rt->rt_dst;
186
187 if (!inet->saddr)
188 inet->saddr = rt->rt_src;
189 inet->rcv_saddr = inet->saddr;
190
191 if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) {
192 /* Reset inherited state */
193 tp->rx_opt.ts_recent = 0;
194 tp->rx_opt.ts_recent_stamp = 0;
195 tp->write_seq = 0;
196 }
197
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -0700198 if (tcp_death_row.sysctl_tw_recycle &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
200 struct inet_peer *peer = rt_get_peer(rt);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200201 /*
202 * VJ's idea. We save last timestamp seen from
203 * the destination in peer table, when entering state
204 * TIME-WAIT * and initialize rx_opt.ts_recent from it,
205 * when trying new connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200207 if (peer != NULL &&
James Morris9d729f72007-03-04 16:12:44 -0800208 peer->tcp_ts_stamp + TCP_PAWS_MSL >= get_seconds()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
210 tp->rx_opt.ts_recent = peer->tcp_ts;
211 }
212 }
213
214 inet->dport = usin->sin_port;
215 inet->daddr = daddr;
216
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800217 inet_csk(sk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (inet->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800219 inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 tp->rx_opt.mss_clamp = 536;
222
223 /* Socket identity is still unknown (sport may be zero).
224 * However we set state to SYN-SENT and not releasing socket
225 * lock select source port, enter ourselves into the hash tables and
226 * complete initialization after this.
227 */
228 tcp_set_state(sk, TCP_SYN_SENT);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800229 err = inet_hash_connect(&tcp_death_row, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (err)
231 goto failure;
232
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200233 err = ip_route_newports(&rt, IPPROTO_TCP,
234 inet->sport, inet->dport, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (err)
236 goto failure;
237
238 /* OK, now commit destination to socket. */
Herbert Xubcd76112006-06-30 13:36:35 -0700239 sk->sk_gso_type = SKB_GSO_TCPV4;
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -0700240 sk_setup_caps(sk, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 if (!tp->write_seq)
243 tp->write_seq = secure_tcp_sequence_number(inet->saddr,
244 inet->daddr,
245 inet->sport,
246 usin->sin_port);
247
248 inet->id = tp->write_seq ^ jiffies;
249
250 err = tcp_connect(sk);
251 rt = NULL;
252 if (err)
253 goto failure;
254
255 return 0;
256
257failure:
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200258 /*
259 * This unhashes the socket and releases the local port,
260 * if necessary.
261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 tcp_set_state(sk, TCP_CLOSE);
263 ip_rt_put(rt);
264 sk->sk_route_caps = 0;
265 inet->dport = 0;
266 return err;
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
270 * This routine does path mtu discovery as defined in RFC1191.
271 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800272static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct dst_entry *dst;
275 struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
278 * send out by Linux are always <576bytes so they should go through
279 * unfragmented).
280 */
281 if (sk->sk_state == TCP_LISTEN)
282 return;
283
284 /* We don't check in the destentry if pmtu discovery is forbidden
285 * on this route. We just assume that no packet_to_big packets
286 * are send back when pmtu discovery is not active.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900287 * There is a small race when the user changes this flag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * route, but I think that's acceptable.
289 */
290 if ((dst = __sk_dst_check(sk, 0)) == NULL)
291 return;
292
293 dst->ops->update_pmtu(dst, mtu);
294
295 /* Something is about to be wrong... Remember soft error
296 * for the case, if this connection will not able to recover.
297 */
298 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
299 sk->sk_err_soft = EMSGSIZE;
300
301 mtu = dst_mtu(dst);
302
303 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800304 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 tcp_sync_mss(sk, mtu);
306
307 /* Resend the TCP packet because it's
308 * clear that the old packet has been
309 * dropped. This is the new "fast" path mtu
310 * discovery.
311 */
312 tcp_simple_retransmit(sk);
313 } /* else let the usual retransmit timer handle it */
314}
315
316/*
317 * This routine is called by the ICMP module when it gets some
318 * sort of error condition. If err < 0 then the socket should
319 * be closed and the error returned to the user. If err > 0
320 * it's just the icmp type << 8 | icmp code. After adjustment
321 * header points to the first 8 bytes of the tcp header. We need
322 * to find the appropriate port.
323 *
324 * The locking strategy used here is very "optimistic". When
325 * someone else accesses the socket the ICMP is just dropped
326 * and for some paths there is no check at all.
327 * A more general error queue to queue errors for later handling
328 * is probably better.
329 *
330 */
331
332void tcp_v4_err(struct sk_buff *skb, u32 info)
333{
334 struct iphdr *iph = (struct iphdr *)skb->data;
335 struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
336 struct tcp_sock *tp;
337 struct inet_sock *inet;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300338 const int type = icmp_hdr(skb)->type;
339 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct sock *sk;
341 __u32 seq;
342 int err;
343
344 if (skb->len < (iph->ihl << 2) + 8) {
345 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
346 return;
347 }
348
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900349 sk = inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->daddr, th->dest,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -0800350 iph->saddr, th->source, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!sk) {
352 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
353 return;
354 }
355 if (sk->sk_state == TCP_TIME_WAIT) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -0700356 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return;
358 }
359
360 bh_lock_sock(sk);
361 /* If too many ICMPs get dropped on busy
362 * servers this needs to be solved differently.
363 */
364 if (sock_owned_by_user(sk))
365 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
366
367 if (sk->sk_state == TCP_CLOSE)
368 goto out;
369
370 tp = tcp_sk(sk);
371 seq = ntohl(th->seq);
372 if (sk->sk_state != TCP_LISTEN &&
373 !between(seq, tp->snd_una, tp->snd_nxt)) {
Eric Dumazet06ca7192006-10-20 00:22:25 -0700374 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 goto out;
376 }
377
378 switch (type) {
379 case ICMP_SOURCE_QUENCH:
380 /* Just silently ignore these. */
381 goto out;
382 case ICMP_PARAMETERPROB:
383 err = EPROTO;
384 break;
385 case ICMP_DEST_UNREACH:
386 if (code > NR_ICMP_UNREACH)
387 goto out;
388
389 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
390 if (!sock_owned_by_user(sk))
391 do_pmtu_discovery(sk, iph, info);
392 goto out;
393 }
394
395 err = icmp_err_convert[code].errno;
396 break;
397 case ICMP_TIME_EXCEEDED:
398 err = EHOSTUNREACH;
399 break;
400 default:
401 goto out;
402 }
403
404 switch (sk->sk_state) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700405 struct request_sock *req, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 case TCP_LISTEN:
407 if (sock_owned_by_user(sk))
408 goto out;
409
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700410 req = inet_csk_search_req(sk, &prev, th->dest,
411 iph->daddr, iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!req)
413 goto out;
414
415 /* ICMPs are not backlogged, hence we cannot get
416 an established socket here.
417 */
418 BUG_TRAP(!req->sk);
419
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700420 if (seq != tcp_rsk(req)->snt_isn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
422 goto out;
423 }
424
425 /*
426 * Still in SYN_RECV, just remove it silently.
427 * There is no good way to pass the error to the newly
428 * created socket, and POSIX does not want network
429 * errors returned from accept().
430 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700431 inet_csk_reqsk_queue_drop(sk, req, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto out;
433
434 case TCP_SYN_SENT:
435 case TCP_SYN_RECV: /* Cannot happen.
436 It can f.e. if SYNs crossed.
437 */
438 if (!sock_owned_by_user(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 sk->sk_err = err;
440
441 sk->sk_error_report(sk);
442
443 tcp_done(sk);
444 } else {
445 sk->sk_err_soft = err;
446 }
447 goto out;
448 }
449
450 /* If we've already connected we will keep trying
451 * until we time out, or the user gives up.
452 *
453 * rfc1122 4.2.3.9 allows to consider as hard errors
454 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
455 * but it is obsoleted by pmtu discovery).
456 *
457 * Note, that in modern internet, where routing is unreliable
458 * and in each dark corner broken firewalls sit, sending random
459 * errors ordered by their masters even this two messages finally lose
460 * their original sense (even Linux sends invalid PORT_UNREACHs)
461 *
462 * Now we are in compliance with RFCs.
463 * --ANK (980905)
464 */
465
466 inet = inet_sk(sk);
467 if (!sock_owned_by_user(sk) && inet->recverr) {
468 sk->sk_err = err;
469 sk->sk_error_report(sk);
470 } else { /* Only an error on timeout */
471 sk->sk_err_soft = err;
472 }
473
474out:
475 bh_unlock_sock(sk);
476 sock_put(sk);
477}
478
479/* This routine computes an IPv4 TCP checksum. */
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -0800480void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700483 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Patrick McHardy84fa7932006-08-29 16:44:56 -0700485 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800486 th->check = ~tcp_v4_check(len, inet->saddr,
487 inet->daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700488 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800489 skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 } else {
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800491 th->check = tcp_v4_check(len, inet->saddr, inet->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 csum_partial((char *)th,
493 th->doff << 2,
494 skb->csum));
495 }
496}
497
Herbert Xua430a432006-07-08 13:34:56 -0700498int tcp_v4_gso_send_check(struct sk_buff *skb)
499{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700500 const struct iphdr *iph;
Herbert Xua430a432006-07-08 13:34:56 -0700501 struct tcphdr *th;
502
503 if (!pskb_may_pull(skb, sizeof(*th)))
504 return -EINVAL;
505
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700506 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700507 th = tcp_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -0700508
509 th->check = 0;
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800510 th->check = ~tcp_v4_check(skb->len, iph->saddr, iph->daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700511 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800512 skb->csum_offset = offsetof(struct tcphdr, check);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700513 skb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xua430a432006-07-08 13:34:56 -0700514 return 0;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/*
518 * This routine will send an RST to the other tcp.
519 *
520 * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
521 * for reset.
522 * Answer: if a packet caused RST, it is not for a socket
523 * existing in our system, if it is matched to a socket,
524 * it is just duplicate segment or bug in other side's TCP.
525 * So that we build reply only basing on parameters
526 * arrived with segment.
527 * Exception: precedence violation. We do not implement it in any case.
528 */
529
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800530static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700532 struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800533 struct {
534 struct tcphdr th;
535#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800536 __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800537#endif
538 } rep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800540#ifdef CONFIG_TCP_MD5SIG
541 struct tcp_md5sig_key *key;
542#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* Never send a reset in response to a reset. */
545 if (th->rst)
546 return;
547
Eric Dumazetee6b9672008-03-05 18:30:47 -0800548 if (skb->rtable->rt_type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return;
550
551 /* Swap the send and the receive. */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800552 memset(&rep, 0, sizeof(rep));
553 rep.th.dest = th->source;
554 rep.th.source = th->dest;
555 rep.th.doff = sizeof(struct tcphdr) / 4;
556 rep.th.rst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (th->ack) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800559 rep.th.seq = th->ack_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800561 rep.th.ack = 1;
562 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
563 skb->len - (th->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200566 memset(&arg, 0, sizeof(arg));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800567 arg.iov[0].iov_base = (unsigned char *)&rep;
568 arg.iov[0].iov_len = sizeof(rep.th);
569
570#ifdef CONFIG_TCP_MD5SIG
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700571 key = sk ? tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr) : NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800572 if (key) {
573 rep.opt[0] = htonl((TCPOPT_NOP << 24) |
574 (TCPOPT_NOP << 16) |
575 (TCPOPT_MD5SIG << 8) |
576 TCPOLEN_MD5SIG);
577 /* Update length and the length the header thinks exists */
578 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
579 rep.th.doff = arg.iov[0].iov_len / 4;
580
581 tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[1],
582 key,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700583 ip_hdr(skb)->daddr,
584 ip_hdr(skb)->saddr,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800585 &rep.th, IPPROTO_TCP,
586 arg.iov[0].iov_len);
587 }
588#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700589 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
590 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 sizeof(struct tcphdr), IPPROTO_TCP, 0);
592 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
593
Denis V. Lunev7feb49c2008-04-03 14:32:00 -0700594 ip_send_reply(dev_net(skb->dst->dev)->ipv4.tcp_sock, skb,
595 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
598 TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
599}
600
601/* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
602 outside socket context is ugly, certainly. What can I do?
603 */
604
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800605static void tcp_v4_send_ack(struct tcp_timewait_sock *twsk,
606 struct sk_buff *skb, u32 seq, u32 ack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 u32 win, u32 ts)
608{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700609 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct {
611 struct tcphdr th;
Al Viro714e85b2006-11-14 20:51:49 -0800612 __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800613#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800614 + (TCPOLEN_MD5SIG_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800615#endif
616 ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 } rep;
618 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800619#ifdef CONFIG_TCP_MD5SIG
620 struct tcp_md5sig_key *key;
621 struct tcp_md5sig_key tw_key;
622#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 memset(&rep.th, 0, sizeof(struct tcphdr));
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200625 memset(&arg, 0, sizeof(arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 arg.iov[0].iov_base = (unsigned char *)&rep;
628 arg.iov[0].iov_len = sizeof(rep.th);
629 if (ts) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800630 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
631 (TCPOPT_TIMESTAMP << 8) |
632 TCPOLEN_TIMESTAMP);
633 rep.opt[1] = htonl(tcp_time_stamp);
634 rep.opt[2] = htonl(ts);
Craig Schlentercb48cfe2007-01-09 00:11:15 -0800635 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
638 /* Swap the send and the receive. */
639 rep.th.dest = th->source;
640 rep.th.source = th->dest;
641 rep.th.doff = arg.iov[0].iov_len / 4;
642 rep.th.seq = htonl(seq);
643 rep.th.ack_seq = htonl(ack);
644 rep.th.ack = 1;
645 rep.th.window = htons(win);
646
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800647#ifdef CONFIG_TCP_MD5SIG
648 /*
649 * The SKB holds an imcoming packet, but may not have a valid ->sk
650 * pointer. This is especially the case when we're dealing with a
651 * TIME_WAIT ack, because the sk structure is long gone, and only
652 * the tcp_timewait_sock remains. So the md5 key is stashed in that
653 * structure, and we use it in preference. I believe that (twsk ||
654 * skb->sk) holds true, but we program defensively.
655 */
656 if (!twsk && skb->sk) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700657 key = tcp_v4_md5_do_lookup(skb->sk, ip_hdr(skb)->daddr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800658 } else if (twsk && twsk->tw_md5_keylen) {
659 tw_key.key = twsk->tw_md5_key;
660 tw_key.keylen = twsk->tw_md5_keylen;
661 key = &tw_key;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200662 } else
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800663 key = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800664
665 if (key) {
666 int offset = (ts) ? 3 : 0;
667
668 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
669 (TCPOPT_NOP << 16) |
670 (TCPOPT_MD5SIG << 8) |
671 TCPOLEN_MD5SIG);
672 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
673 rep.th.doff = arg.iov[0].iov_len/4;
674
675 tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[offset],
676 key,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700677 ip_hdr(skb)->daddr,
678 ip_hdr(skb)->saddr,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800679 &rep.th, IPPROTO_TCP,
680 arg.iov[0].iov_len);
681 }
682#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700683 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
684 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 arg.iov[0].iov_len, IPPROTO_TCP, 0);
686 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
Patrick McHardyf0e48db2007-06-04 21:32:46 -0700687 if (twsk)
688 arg.bound_dev_if = twsk->tw_sk.tw_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Denis V. Lunev7feb49c2008-04-03 14:32:00 -0700690 ip_send_reply(dev_net(skb->dev)->ipv4.tcp_sock, skb,
691 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
694}
695
696static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
697{
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700698 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800699 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800701 tcp_v4_send_ack(tcptw, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200702 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
703 tcptw->tw_ts_recent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700705 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200708static void tcp_v4_reqsk_send_ack(struct sk_buff *skb,
709 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800711 tcp_v4_send_ack(NULL, skb, tcp_rsk(req)->snt_isn + 1,
712 tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 req->ts_recent);
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/*
Kris Katterjohn9bf1d832008-02-17 22:29:19 -0800717 * Send a SYN-ACK after having received a SYN.
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700718 * This still operates on a request_sock only, not on a big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * socket.
720 */
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800721static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
722 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700724 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int err = -1;
726 struct sk_buff * skb;
727
728 /* First, grab a route. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700729 if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800730 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 skb = tcp_make_synack(sk, dst, req);
733
734 if (skb) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700735 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800737 th->check = tcp_v4_check(skb->len,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700738 ireq->loc_addr,
739 ireq->rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 csum_partial((char *)th, skb->len,
741 skb->csum));
742
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700743 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
744 ireq->rmt_addr,
745 ireq->opt);
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200746 err = net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 dst_release(dst);
750 return err;
751}
752
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800753static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req)
754{
755 return __tcp_v4_send_synack(sk, req, NULL);
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700759 * IPv4 request_sock destructor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700761static void tcp_v4_reqsk_destructor(struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Jesper Juhla51482b2005-11-08 09:41:34 -0800763 kfree(inet_rsk(req)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200766#ifdef CONFIG_SYN_COOKIES
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800767static void syn_flood_warning(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 static unsigned long warntime;
770
771 if (time_after(jiffies, (warntime + HZ * 60))) {
772 warntime = jiffies;
773 printk(KERN_INFO
774 "possible SYN flooding on port %d. Sending cookies.\n",
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700775 ntohs(tcp_hdr(skb)->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777}
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200778#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700781 * Save and compile IPv4 options into the request_sock if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800783static struct ip_options *tcp_v4_save_options(struct sock *sk,
784 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct ip_options *opt = &(IPCB(skb)->opt);
787 struct ip_options *dopt = NULL;
788
789 if (opt && opt->optlen) {
790 int opt_size = optlength(opt);
791 dopt = kmalloc(opt_size, GFP_ATOMIC);
792 if (dopt) {
793 if (ip_options_echo(dopt, skb)) {
794 kfree(dopt);
795 dopt = NULL;
796 }
797 }
798 }
799 return dopt;
800}
801
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800802#ifdef CONFIG_TCP_MD5SIG
803/*
804 * RFC2385 MD5 checksumming requires a mapping of
805 * IP address->MD5 Key.
806 * We need to maintain these in the sk structure.
807 */
808
809/* Find the Key structure for an address. */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200810static struct tcp_md5sig_key *
811 tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800812{
813 struct tcp_sock *tp = tcp_sk(sk);
814 int i;
815
816 if (!tp->md5sig_info || !tp->md5sig_info->entries4)
817 return NULL;
818 for (i = 0; i < tp->md5sig_info->entries4; i++) {
819 if (tp->md5sig_info->keys4[i].addr == addr)
David S. Millerf8ab18d2007-09-28 15:18:35 -0700820 return &tp->md5sig_info->keys4[i].base;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800821 }
822 return NULL;
823}
824
825struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
826 struct sock *addr_sk)
827{
828 return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->daddr);
829}
830
831EXPORT_SYMBOL(tcp_v4_md5_lookup);
832
Adrian Bunkf5b99bc2006-11-30 17:22:29 -0800833static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
834 struct request_sock *req)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800835{
836 return tcp_v4_md5_do_lookup(sk, inet_rsk(req)->rmt_addr);
837}
838
839/* This can be called on a newly created socket, from other files */
840int tcp_v4_md5_do_add(struct sock *sk, __be32 addr,
841 u8 *newkey, u8 newkeylen)
842{
843 /* Add Key to the list */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700844 struct tcp_md5sig_key *key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800845 struct tcp_sock *tp = tcp_sk(sk);
846 struct tcp4_md5sig_key *keys;
847
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700848 key = tcp_v4_md5_do_lookup(sk, addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800849 if (key) {
850 /* Pre-existing entry - just update that one. */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700851 kfree(key->key);
852 key->key = newkey;
853 key->keylen = newkeylen;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800854 } else {
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200855 struct tcp_md5sig_info *md5sig;
856
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800857 if (!tp->md5sig_info) {
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200858 tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info),
859 GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800860 if (!tp->md5sig_info) {
861 kfree(newkey);
862 return -ENOMEM;
863 }
David S. Miller3d7dbea2007-06-12 14:36:42 -0700864 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800865 }
866 if (tcp_alloc_md5sig_pool() == NULL) {
867 kfree(newkey);
868 return -ENOMEM;
869 }
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200870 md5sig = tp->md5sig_info;
871
872 if (md5sig->alloced4 == md5sig->entries4) {
873 keys = kmalloc((sizeof(*keys) *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900874 (md5sig->entries4 + 1)), GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800875 if (!keys) {
876 kfree(newkey);
877 tcp_free_md5sig_pool();
878 return -ENOMEM;
879 }
880
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200881 if (md5sig->entries4)
882 memcpy(keys, md5sig->keys4,
883 sizeof(*keys) * md5sig->entries4);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800884
885 /* Free old key list, and reference new one */
YOSHIFUJI Hideakia80cc202007-11-20 17:30:06 -0800886 kfree(md5sig->keys4);
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200887 md5sig->keys4 = keys;
888 md5sig->alloced4++;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800889 }
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200890 md5sig->entries4++;
David S. Millerf8ab18d2007-09-28 15:18:35 -0700891 md5sig->keys4[md5sig->entries4 - 1].addr = addr;
892 md5sig->keys4[md5sig->entries4 - 1].base.key = newkey;
893 md5sig->keys4[md5sig->entries4 - 1].base.keylen = newkeylen;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800894 }
895 return 0;
896}
897
898EXPORT_SYMBOL(tcp_v4_md5_do_add);
899
900static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk,
901 u8 *newkey, u8 newkeylen)
902{
903 return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->daddr,
904 newkey, newkeylen);
905}
906
907int tcp_v4_md5_do_del(struct sock *sk, __be32 addr)
908{
909 struct tcp_sock *tp = tcp_sk(sk);
910 int i;
911
912 for (i = 0; i < tp->md5sig_info->entries4; i++) {
913 if (tp->md5sig_info->keys4[i].addr == addr) {
914 /* Free the key */
David S. Millerf8ab18d2007-09-28 15:18:35 -0700915 kfree(tp->md5sig_info->keys4[i].base.key);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800916 tp->md5sig_info->entries4--;
917
918 if (tp->md5sig_info->entries4 == 0) {
919 kfree(tp->md5sig_info->keys4);
920 tp->md5sig_info->keys4 = NULL;
Leigh Brown8228a18d2006-12-17 17:12:30 -0800921 tp->md5sig_info->alloced4 = 0;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200922 } else if (tp->md5sig_info->entries4 != i) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800923 /* Need to do some manipulation */
YOSHIFUJI Hideaki354faf02007-11-20 17:30:31 -0800924 memmove(&tp->md5sig_info->keys4[i],
925 &tp->md5sig_info->keys4[i+1],
926 (tp->md5sig_info->entries4 - i) *
927 sizeof(struct tcp4_md5sig_key));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800928 }
929 tcp_free_md5sig_pool();
930 return 0;
931 }
932 }
933 return -ENOENT;
934}
935
936EXPORT_SYMBOL(tcp_v4_md5_do_del);
937
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200938static void tcp_v4_clear_md5_list(struct sock *sk)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800939{
940 struct tcp_sock *tp = tcp_sk(sk);
941
942 /* Free each key, then the set of key keys,
943 * the crypto element, and then decrement our
944 * hold on the last resort crypto.
945 */
946 if (tp->md5sig_info->entries4) {
947 int i;
948 for (i = 0; i < tp->md5sig_info->entries4; i++)
David S. Millerf8ab18d2007-09-28 15:18:35 -0700949 kfree(tp->md5sig_info->keys4[i].base.key);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800950 tp->md5sig_info->entries4 = 0;
951 tcp_free_md5sig_pool();
952 }
953 if (tp->md5sig_info->keys4) {
954 kfree(tp->md5sig_info->keys4);
955 tp->md5sig_info->keys4 = NULL;
956 tp->md5sig_info->alloced4 = 0;
957 }
958}
959
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200960static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
961 int optlen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800962{
963 struct tcp_md5sig cmd;
964 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
965 u8 *newkey;
966
967 if (optlen < sizeof(cmd))
968 return -EINVAL;
969
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200970 if (copy_from_user(&cmd, optval, sizeof(cmd)))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800971 return -EFAULT;
972
973 if (sin->sin_family != AF_INET)
974 return -EINVAL;
975
976 if (!cmd.tcpm_key || !cmd.tcpm_keylen) {
977 if (!tcp_sk(sk)->md5sig_info)
978 return -ENOENT;
979 return tcp_v4_md5_do_del(sk, sin->sin_addr.s_addr);
980 }
981
982 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
983 return -EINVAL;
984
985 if (!tcp_sk(sk)->md5sig_info) {
986 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200987 struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800988
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800989 if (!p)
990 return -EINVAL;
991
992 tp->md5sig_info = p;
David S. Miller3d7dbea2007-06-12 14:36:42 -0700993 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800994 }
995
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200996 newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800997 if (!newkey)
998 return -ENOMEM;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800999 return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
1000 newkey, cmd.tcpm_keylen);
1001}
1002
1003static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
1004 __be32 saddr, __be32 daddr,
1005 struct tcphdr *th, int protocol,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -08001006 unsigned int tcplen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001007{
1008 struct scatterlist sg[4];
1009 __u16 data_len;
1010 int block = 0;
Al Viro8e5200f2006-11-20 18:06:37 -08001011 __sum16 old_checksum;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001012 struct tcp_md5sig_pool *hp;
1013 struct tcp4_pseudohdr *bp;
1014 struct hash_desc *desc;
1015 int err;
1016 unsigned int nbytes = 0;
1017
1018 /*
1019 * Okay, so RFC2385 is turned on for this connection,
1020 * so we need to generate the MD5 hash for the packet now.
1021 */
1022
1023 hp = tcp_get_md5sig_pool();
1024 if (!hp)
1025 goto clear_hash_noput;
1026
1027 bp = &hp->md5_blk.ip4;
1028 desc = &hp->md5_desc;
1029
1030 /*
1031 * 1. the TCP pseudo-header (in the order: source IP address,
1032 * destination IP address, zero-padded protocol number, and
1033 * segment length)
1034 */
1035 bp->saddr = saddr;
1036 bp->daddr = daddr;
1037 bp->pad = 0;
1038 bp->protocol = protocol;
1039 bp->len = htons(tcplen);
David S. Millerc7da57a2007-10-26 00:41:21 -07001040
1041 sg_init_table(sg, 4);
1042
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001043 sg_set_buf(&sg[block++], bp, sizeof(*bp));
1044 nbytes += sizeof(*bp);
1045
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001046 /* 2. the TCP header, excluding options, and assuming a
1047 * checksum of zero/
1048 */
1049 old_checksum = th->check;
1050 th->check = 0;
1051 sg_set_buf(&sg[block++], th, sizeof(struct tcphdr));
1052 nbytes += sizeof(struct tcphdr);
David S. Miller08dd1a52006-11-30 16:35:01 -08001053
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001054 /* 3. the TCP segment data (if any) */
1055 data_len = tcplen - (th->doff << 2);
1056 if (data_len > 0) {
1057 unsigned char *data = (unsigned char *)th + (th->doff << 2);
1058 sg_set_buf(&sg[block++], data, data_len);
1059 nbytes += data_len;
1060 }
1061
1062 /* 4. an independently-specified key or password, known to both
1063 * TCPs and presumably connection-specific
1064 */
1065 sg_set_buf(&sg[block++], key->key, key->keylen);
1066 nbytes += key->keylen;
1067
Jens Axboec46f2332007-10-31 12:06:37 +01001068 sg_mark_end(&sg[block - 1]);
David S. Millerc7da57a2007-10-26 00:41:21 -07001069
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001070 /* Now store the Hash into the packet */
1071 err = crypto_hash_init(desc);
1072 if (err)
1073 goto clear_hash;
1074 err = crypto_hash_update(desc, sg, nbytes);
1075 if (err)
1076 goto clear_hash;
1077 err = crypto_hash_final(desc, md5_hash);
1078 if (err)
1079 goto clear_hash;
1080
1081 /* Reset header, and free up the crypto */
1082 tcp_put_md5sig_pool();
1083 th->check = old_checksum;
1084
1085out:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001086 return 0;
1087clear_hash:
1088 tcp_put_md5sig_pool();
1089clear_hash_noput:
1090 memset(md5_hash, 0, 16);
1091 goto out;
1092}
1093
1094int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
1095 struct sock *sk,
1096 struct dst_entry *dst,
1097 struct request_sock *req,
1098 struct tcphdr *th, int protocol,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -08001099 unsigned int tcplen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001100{
1101 __be32 saddr, daddr;
1102
1103 if (sk) {
1104 saddr = inet_sk(sk)->saddr;
1105 daddr = inet_sk(sk)->daddr;
1106 } else {
1107 struct rtable *rt = (struct rtable *)dst;
1108 BUG_ON(!rt);
1109 saddr = rt->rt_src;
1110 daddr = rt->rt_dst;
1111 }
1112 return tcp_v4_do_calc_md5_hash(md5_hash, key,
1113 saddr, daddr,
1114 th, protocol, tcplen);
1115}
1116
1117EXPORT_SYMBOL(tcp_v4_calc_md5_hash);
1118
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001119static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001120{
1121 /*
1122 * This gets called for each TCP segment that arrives
1123 * so we want to be efficient.
1124 * We have 3 drop cases:
1125 * o No MD5 hash and one expected.
1126 * o MD5 hash and we're not expecting one.
1127 * o MD5 hash and its wrong.
1128 */
1129 __u8 *hash_location = NULL;
1130 struct tcp_md5sig_key *hash_expected;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001131 const struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001132 struct tcphdr *th = tcp_hdr(skb);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001133 int length = (th->doff << 2) - sizeof(struct tcphdr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001134 int genhash;
1135 unsigned char *ptr;
1136 unsigned char newhash[16];
1137
1138 hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
1139
1140 /*
1141 * If the TCP option length is less than the TCP_MD5SIG
1142 * option length, then we can shortcut
1143 */
1144 if (length < TCPOLEN_MD5SIG) {
1145 if (hash_expected)
1146 return 1;
1147 else
1148 return 0;
1149 }
1150
1151 /* Okay, we can't shortcut - we have to grub through the options */
1152 ptr = (unsigned char *)(th + 1);
1153 while (length > 0) {
1154 int opcode = *ptr++;
1155 int opsize;
1156
1157 switch (opcode) {
1158 case TCPOPT_EOL:
1159 goto done_opts;
1160 case TCPOPT_NOP:
1161 length--;
1162 continue;
1163 default:
1164 opsize = *ptr++;
1165 if (opsize < 2)
1166 goto done_opts;
1167 if (opsize > length)
1168 goto done_opts;
1169
1170 if (opcode == TCPOPT_MD5SIG) {
1171 hash_location = ptr;
1172 goto done_opts;
1173 }
1174 }
1175 ptr += opsize-2;
1176 length -= opsize;
1177 }
1178done_opts:
1179 /* We've parsed the options - do we have a hash? */
1180 if (!hash_expected && !hash_location)
1181 return 0;
1182
1183 if (hash_expected && !hash_location) {
Leigh Browna9fc00c2006-12-17 17:13:10 -08001184 LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found "
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001185 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001186 NIPQUAD(iph->saddr), ntohs(th->source),
1187 NIPQUAD(iph->daddr), ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001188 return 1;
1189 }
1190
1191 if (!hash_expected && hash_location) {
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001192 LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found "
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001193 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001194 NIPQUAD(iph->saddr), ntohs(th->source),
1195 NIPQUAD(iph->daddr), ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001196 return 1;
1197 }
1198
1199 /* Okay, so this is hash_expected and hash_location -
1200 * so we need to calculate the checksum.
1201 */
1202 genhash = tcp_v4_do_calc_md5_hash(newhash,
1203 hash_expected,
1204 iph->saddr, iph->daddr,
1205 th, sk->sk_protocol,
1206 skb->len);
1207
1208 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
1209 if (net_ratelimit()) {
1210 printk(KERN_INFO "MD5 Hash failed for "
1211 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001212 NIPQUAD(iph->saddr), ntohs(th->source),
1213 NIPQUAD(iph->daddr), ntohs(th->dest),
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001214 genhash ? " tcp_v4_calc_md5_hash failed" : "");
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001215 }
1216 return 1;
1217 }
1218 return 0;
1219}
1220
1221#endif
1222
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001223struct request_sock_ops tcp_request_sock_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 .family = PF_INET,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001225 .obj_size = sizeof(struct tcp_request_sock),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .rtx_syn_ack = tcp_v4_send_synack,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001227 .send_ack = tcp_v4_reqsk_send_ack,
1228 .destructor = tcp_v4_reqsk_destructor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 .send_reset = tcp_v4_send_reset,
1230};
1231
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001232#ifdef CONFIG_TCP_MD5SIG
Andrew Mortonb6332e62006-11-30 19:16:28 -08001233static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001234 .md5_lookup = tcp_v4_reqsk_md5_lookup,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001235};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001236#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001237
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001238static struct timewait_sock_ops tcp_timewait_sock_ops = {
1239 .twsk_obj_size = sizeof(struct tcp_timewait_sock),
1240 .twsk_unique = tcp_twsk_unique,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001241 .twsk_destructor= tcp_twsk_destructor,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001242};
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
1245{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001246 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct tcp_options_received tmp_opt;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001248 struct request_sock *req;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001249 __be32 saddr = ip_hdr(skb)->saddr;
1250 __be32 daddr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 __u32 isn = TCP_SKB_CB(skb)->when;
1252 struct dst_entry *dst = NULL;
1253#ifdef CONFIG_SYN_COOKIES
1254 int want_cookie = 0;
1255#else
1256#define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
1257#endif
1258
1259 /* Never answer to SYNs send to broadcast or multicast */
Eric Dumazetee6b9672008-03-05 18:30:47 -08001260 if (skb->rtable->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 goto drop;
1262
1263 /* TW buckets are converted to open requests without
1264 * limitations, they conserve resources and peer is
1265 * evidently real one.
1266 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001267 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#ifdef CONFIG_SYN_COOKIES
1269 if (sysctl_tcp_syncookies) {
1270 want_cookie = 1;
1271 } else
1272#endif
1273 goto drop;
1274 }
1275
1276 /* Accept backlog is full. If we have already queued enough
1277 * of warm entries in syn queue, drop request. It is better than
1278 * clogging syn queue with openreqs with exponentially increasing
1279 * timeout.
1280 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001281 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto drop;
1283
Arnaldo Carvalho de Meloce4a7d02008-06-10 12:39:35 -07001284 req = inet_reqsk_alloc(&tcp_request_sock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (!req)
1286 goto drop;
1287
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001288#ifdef CONFIG_TCP_MD5SIG
1289 tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
1290#endif
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 tcp_clear_options(&tmp_opt);
1293 tmp_opt.mss_clamp = 536;
1294 tmp_opt.user_mss = tcp_sk(sk)->rx_opt.user_mss;
1295
1296 tcp_parse_options(skb, &tmp_opt, 0);
1297
Florian Westphal4dfc2812008-04-10 03:12:40 -07001298 if (want_cookie && !tmp_opt.saw_tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 tcp_clear_options(&tmp_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
1302 /* Some OSes (unknown ones, but I see them on web server, which
1303 * contains information interesting only for windows'
1304 * users) do not send their stamp in SYN. It is easy case.
1305 * We simply do not advertise TS support.
1306 */
1307 tmp_opt.saw_tstamp = 0;
1308 tmp_opt.tstamp_ok = 0;
1309 }
1310 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1311
1312 tcp_openreq_init(req, &tmp_opt, skb);
1313
Venkat Yekkirala4237c752006-07-24 23:32:50 -07001314 if (security_inet_conn_request(sk, skb, req))
1315 goto drop_and_free;
1316
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001317 ireq = inet_rsk(req);
1318 ireq->loc_addr = daddr;
1319 ireq->rmt_addr = saddr;
1320 ireq->opt = tcp_v4_save_options(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (!want_cookie)
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001322 TCP_ECN_create_request(req, tcp_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 if (want_cookie) {
1325#ifdef CONFIG_SYN_COOKIES
1326 syn_flood_warning(skb);
Florian Westphal4dfc2812008-04-10 03:12:40 -07001327 req->cookie_ts = tmp_opt.tstamp_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328#endif
1329 isn = cookie_v4_init_sequence(sk, skb, &req->mss);
1330 } else if (!isn) {
1331 struct inet_peer *peer = NULL;
1332
1333 /* VJ's idea. We save last timestamp seen
1334 * from the destination in peer table, when entering
1335 * state TIME-WAIT, and check against it before
1336 * accepting new connection request.
1337 *
1338 * If "isn" is not zero, this request hit alive
1339 * timewait bucket, so that all the necessary checks
1340 * are made in the function processing timewait state.
1341 */
1342 if (tmp_opt.saw_tstamp &&
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07001343 tcp_death_row.sysctl_tw_recycle &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001344 (dst = inet_csk_route_req(sk, req)) != NULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
1346 peer->v4daddr == saddr) {
James Morris9d729f72007-03-04 16:12:44 -08001347 if (get_seconds() < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 (s32)(peer->tcp_ts - req->ts_recent) >
1349 TCP_PAWS_WINDOW) {
1350 NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001351 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 }
1354 /* Kill the following clause, if you dislike this way. */
1355 else if (!sysctl_tcp_syncookies &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001356 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 (sysctl_max_syn_backlog >> 2)) &&
1358 (!peer || !peer->tcp_ts_stamp) &&
1359 (!dst || !dst_metric(dst, RTAX_RTT))) {
1360 /* Without syncookies last quarter of
1361 * backlog is filled with destinations,
1362 * proven to be alive.
1363 * It means that we continue to communicate
1364 * to destinations, already remembered
1365 * to the moment of synflood.
1366 */
Patrick McHardy64ce2072005-08-09 20:50:53 -07001367 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open "
YOSHIFUJI Hideakia7d632b2008-04-14 04:09:00 -07001368 "request from " NIPQUAD_FMT "/%u\n",
Patrick McHardy64ce2072005-08-09 20:50:53 -07001369 NIPQUAD(saddr),
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001370 ntohs(tcp_hdr(skb)->source));
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001371 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
Gerrit Renkera94f7232006-11-10 14:06:49 -08001374 isn = tcp_v4_init_sequence(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001376 tcp_rsk(req)->snt_isn = isn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001378 if (__tcp_v4_send_synack(sk, req, dst) || want_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto drop_and_free;
1380
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001381 inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
1383
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001384drop_and_release:
1385 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386drop_and_free:
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001387 reqsk_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return 0;
1390}
1391
1392
1393/*
1394 * The three way handshake has completed - we got a valid synack -
1395 * now create the new socket.
1396 */
1397struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001398 struct request_sock *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 struct dst_entry *dst)
1400{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001401 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct inet_sock *newinet;
1403 struct tcp_sock *newtp;
1404 struct sock *newsk;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001405#ifdef CONFIG_TCP_MD5SIG
1406 struct tcp_md5sig_key *key;
1407#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 if (sk_acceptq_is_full(sk))
1410 goto exit_overflow;
1411
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001412 if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 goto exit;
1414
1415 newsk = tcp_create_openreq_child(sk, req, skb);
1416 if (!newsk)
1417 goto exit;
1418
Herbert Xubcd76112006-06-30 13:36:35 -07001419 newsk->sk_gso_type = SKB_GSO_TCPV4;
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -07001420 sk_setup_caps(newsk, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 newtp = tcp_sk(newsk);
1423 newinet = inet_sk(newsk);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001424 ireq = inet_rsk(req);
1425 newinet->daddr = ireq->rmt_addr;
1426 newinet->rcv_saddr = ireq->loc_addr;
1427 newinet->saddr = ireq->loc_addr;
1428 newinet->opt = ireq->opt;
1429 ireq->opt = NULL;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001430 newinet->mc_index = inet_iif(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001431 newinet->mc_ttl = ip_hdr(skb)->ttl;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001432 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (newinet->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001434 inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 newinet->id = newtp->write_seq ^ jiffies;
1436
John Heffner5d424d52006-03-20 17:53:41 -08001437 tcp_mtup_init(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 tcp_sync_mss(newsk, dst_mtu(dst));
1439 newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
1440 tcp_initialize_rcv_mss(newsk);
1441
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001442#ifdef CONFIG_TCP_MD5SIG
1443 /* Copy over the MD5 key from the original socket */
1444 if ((key = tcp_v4_md5_do_lookup(sk, newinet->daddr)) != NULL) {
1445 /*
1446 * We're using one, so create a matching key
1447 * on the newsk structure. If we fail to get
1448 * memory, then we end up not copying the key
1449 * across. Shucks.
1450 */
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -02001451 char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1452 if (newkey != NULL)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001453 tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr,
1454 newkey, key->keylen);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001455 }
1456#endif
1457
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001458 __inet_hash_nolisten(newsk);
1459 __inet_inherit_port(sk, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 return newsk;
1462
1463exit_overflow:
1464 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
1465exit:
1466 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
1467 dst_release(dst);
1468 return NULL;
1469}
1470
1471static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
1472{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001473 struct tcphdr *th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001474 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 struct sock *nsk;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001476 struct request_sock **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 /* Find possible connection requests. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001478 struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
1479 iph->saddr, iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (req)
1481 return tcp_check_req(sk, skb, req, prev);
1482
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001483 nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001484 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (nsk) {
1487 if (nsk->sk_state != TCP_TIME_WAIT) {
1488 bh_lock_sock(nsk);
1489 return nsk;
1490 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001491 inet_twsk_put(inet_twsk(nsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return NULL;
1493 }
1494
1495#ifdef CONFIG_SYN_COOKIES
1496 if (!th->rst && !th->syn && th->ack)
1497 sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
1498#endif
1499 return sk;
1500}
1501
Al Virob51655b2006-11-14 21:40:42 -08001502static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001504 const struct iphdr *iph = ip_hdr(skb);
1505
Patrick McHardy84fa7932006-08-29 16:44:56 -07001506 if (skb->ip_summed == CHECKSUM_COMPLETE) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001507 if (!tcp_v4_check(skb->len, iph->saddr,
1508 iph->daddr, skb->csum)) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001509 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
Herbert Xufb286bb2005-11-10 13:01:24 -08001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001513
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001514 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
Herbert Xufb286bb2005-11-10 13:01:24 -08001515 skb->len, IPPROTO_TCP, 0);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (skb->len <= 76) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001518 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520 return 0;
1521}
1522
1523
1524/* The socket must have it's spinlock held when we get
1525 * here.
1526 *
1527 * We have a potential double-lock case here, so even when
1528 * doing backlog processing we use the BH locking scheme.
1529 * This is because we cannot sleep with the original spinlock
1530 * held.
1531 */
1532int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
1533{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001534 struct sock *rsk;
1535#ifdef CONFIG_TCP_MD5SIG
1536 /*
1537 * We really want to reject the packet as early as possible
1538 * if:
1539 * o We're expecting an MD5'd packet and this is no MD5 tcp option
1540 * o There is an MD5 option and we're not expecting one
1541 */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001542 if (tcp_v4_inbound_md5_hash(sk, skb))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001543 goto discard;
1544#endif
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1547 TCP_CHECK_TIMER(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001548 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001549 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 TCP_CHECK_TIMER(sk);
1553 return 0;
1554 }
1555
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001556 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto csum_err;
1558
1559 if (sk->sk_state == TCP_LISTEN) {
1560 struct sock *nsk = tcp_v4_hnd_req(sk, skb);
1561 if (!nsk)
1562 goto discard;
1563
1564 if (nsk != sk) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001565 if (tcp_child_process(sk, nsk, skb)) {
1566 rsk = nsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return 0;
1570 }
1571 }
1572
1573 TCP_CHECK_TIMER(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001574 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001575 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 TCP_CHECK_TIMER(sk);
1579 return 0;
1580
1581reset:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001582 tcp_v4_send_reset(rsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583discard:
1584 kfree_skb(skb);
1585 /* Be careful here. If this function gets more complicated and
1586 * gcc suffers from register pressure on the x86, sk (in %ebx)
1587 * might be destroyed here. This current version compiles correctly,
1588 * but you have been warned.
1589 */
1590 return 0;
1591
1592csum_err:
1593 TCP_INC_STATS_BH(TCP_MIB_INERRS);
1594 goto discard;
1595}
1596
1597/*
1598 * From tcp_input.c
1599 */
1600
1601int tcp_v4_rcv(struct sk_buff *skb)
1602{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001603 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 struct tcphdr *th;
1605 struct sock *sk;
1606 int ret;
1607
1608 if (skb->pkt_type != PACKET_HOST)
1609 goto discard_it;
1610
1611 /* Count it even if it's bad */
1612 TCP_INC_STATS_BH(TCP_MIB_INSEGS);
1613
1614 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1615 goto discard_it;
1616
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001617 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 if (th->doff < sizeof(struct tcphdr) / 4)
1620 goto bad_packet;
1621 if (!pskb_may_pull(skb, th->doff * 4))
1622 goto discard_it;
1623
1624 /* An explanation is required here, I think.
1625 * Packet length and doff are validated by header prediction,
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001626 * provided case of th->doff==0 is eliminated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * So, we defer the checks. */
Herbert Xu60476372007-04-09 11:59:39 -07001628 if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 goto bad_packet;
1630
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001631 th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001632 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1634 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1635 skb->len - th->doff * 4);
1636 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1637 TCP_SKB_CB(skb)->when = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001638 TCP_SKB_CB(skb)->flags = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 TCP_SKB_CB(skb)->sacked = 0;
1640
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001641 sk = __inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001642 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (!sk)
1644 goto no_tcp_socket;
1645
1646process:
1647 if (sk->sk_state == TCP_TIME_WAIT)
1648 goto do_time_wait;
1649
1650 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1651 goto discard_and_relse;
Patrick McHardyb59c2702006-01-06 23:06:10 -08001652 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001654 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto discard_and_relse;
1656
1657 skb->dev = NULL;
1658
Ingo Molnarc6366182006-07-03 00:25:13 -07001659 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 ret = 0;
1661 if (!sock_owned_by_user(sk)) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001662#ifdef CONFIG_NET_DMA
1663 struct tcp_sock *tp = tcp_sk(sk);
1664 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1665 tp->ucopy.dma_chan = get_softnet_dma();
1666 if (tp->ucopy.dma_chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 ret = tcp_v4_do_rcv(sk, skb);
Chris Leech1a2449a2006-05-23 18:05:53 -07001668 else
1669#endif
1670 {
1671 if (!tcp_prequeue(sk, skb))
1672 ret = tcp_v4_do_rcv(sk, skb);
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 } else
1675 sk_add_backlog(sk, skb);
1676 bh_unlock_sock(sk);
1677
1678 sock_put(sk);
1679
1680 return ret;
1681
1682no_tcp_socket:
1683 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1684 goto discard_it;
1685
1686 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1687bad_packet:
1688 TCP_INC_STATS_BH(TCP_MIB_INERRS);
1689 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001690 tcp_v4_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
1693discard_it:
1694 /* Discard frame. */
1695 kfree_skb(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698discard_and_relse:
1699 sock_put(sk);
1700 goto discard_it;
1701
1702do_time_wait:
1703 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001704 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto discard_it;
1706 }
1707
1708 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1709 TCP_INC_STATS_BH(TCP_MIB_INERRS);
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001710 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto discard_it;
1712 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001713 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 case TCP_TW_SYN: {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001715 struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001716 &tcp_hashinfo,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001717 iph->daddr, th->dest,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001718 inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (sk2) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001720 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
1721 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 sk = sk2;
1723 goto process;
1724 }
1725 /* Fall through to ACK */
1726 }
1727 case TCP_TW_ACK:
1728 tcp_v4_timewait_ack(sk, skb);
1729 break;
1730 case TCP_TW_RST:
1731 goto no_tcp_socket;
1732 case TCP_TW_SUCCESS:;
1733 }
1734 goto discard_it;
1735}
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737/* VJ's idea. Save last timestamp seen from this destination
1738 * and hold it at least for normal timewait interval to use for duplicate
1739 * segment detection in subsequent connections, before they enter synchronized
1740 * state.
1741 */
1742
1743int tcp_v4_remember_stamp(struct sock *sk)
1744{
1745 struct inet_sock *inet = inet_sk(sk);
1746 struct tcp_sock *tp = tcp_sk(sk);
1747 struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
1748 struct inet_peer *peer = NULL;
1749 int release_it = 0;
1750
1751 if (!rt || rt->rt_dst != inet->daddr) {
1752 peer = inet_getpeer(inet->daddr, 1);
1753 release_it = 1;
1754 } else {
1755 if (!rt->peer)
1756 rt_bind_peer(rt, 1);
1757 peer = rt->peer;
1758 }
1759
1760 if (peer) {
1761 if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
James Morris9d729f72007-03-04 16:12:44 -08001762 (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
1764 peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
1765 peer->tcp_ts = tp->rx_opt.ts_recent;
1766 }
1767 if (release_it)
1768 inet_putpeer(peer);
1769 return 1;
1770 }
1771
1772 return 0;
1773}
1774
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001775int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001777 struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 if (peer) {
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001780 const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
1781
1782 if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
James Morris9d729f72007-03-04 16:12:44 -08001783 (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001784 peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) {
1785 peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp;
1786 peer->tcp_ts = tcptw->tw_ts_recent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 inet_putpeer(peer);
1789 return 1;
1790 }
1791
1792 return 0;
1793}
1794
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001795struct inet_connection_sock_af_ops ipv4_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001796 .queue_xmit = ip_queue_xmit,
1797 .send_check = tcp_v4_send_check,
1798 .rebuild_header = inet_sk_rebuild_header,
1799 .conn_request = tcp_v4_conn_request,
1800 .syn_recv_sock = tcp_v4_syn_recv_sock,
1801 .remember_stamp = tcp_v4_remember_stamp,
1802 .net_header_len = sizeof(struct iphdr),
1803 .setsockopt = ip_setsockopt,
1804 .getsockopt = ip_getsockopt,
1805 .addr2sockaddr = inet_csk_addr2sockaddr,
1806 .sockaddr_len = sizeof(struct sockaddr_in),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001807 .bind_conflict = inet_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001808#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001809 .compat_setsockopt = compat_ip_setsockopt,
1810 .compat_getsockopt = compat_ip_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001811#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812};
1813
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001814#ifdef CONFIG_TCP_MD5SIG
Andrew Mortonb6332e62006-11-30 19:16:28 -08001815static struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001816 .md5_lookup = tcp_v4_md5_lookup,
1817 .calc_md5_hash = tcp_v4_calc_md5_hash,
1818 .md5_add = tcp_v4_md5_add_func,
1819 .md5_parse = tcp_v4_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001820};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001821#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/* NOTE: A lot of things set to zero explicitly by call to
1824 * sk_alloc() so need not be done here.
1825 */
1826static int tcp_v4_init_sock(struct sock *sk)
1827{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001828 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct tcp_sock *tp = tcp_sk(sk);
1830
1831 skb_queue_head_init(&tp->out_of_order_queue);
1832 tcp_init_xmit_timers(sk);
1833 tcp_prequeue_init(tp);
1834
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001835 icsk->icsk_rto = TCP_TIMEOUT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 tp->mdev = TCP_TIMEOUT_INIT;
1837
1838 /* So many TCP implementations out there (incorrectly) count the
1839 * initial SYN frame in their delayed-ACK and congestion control
1840 * algorithms that we must have the following bandaid to talk
1841 * efficiently to them. -DaveM
1842 */
1843 tp->snd_cwnd = 2;
1844
1845 /* See draft-stevens-tcpca-spec-01 for discussion of the
1846 * initialization of these values.
1847 */
1848 tp->snd_ssthresh = 0x7fffffff; /* Infinity */
1849 tp->snd_cwnd_clamp = ~0;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001850 tp->mss_cache = 536;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 tp->reordering = sysctl_tcp_reordering;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001853 icsk->icsk_ca_ops = &tcp_init_congestion_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 sk->sk_state = TCP_CLOSE;
1856
1857 sk->sk_write_space = sk_stream_write_space;
1858 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1859
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001860 icsk->icsk_af_ops = &ipv4_specific;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001861 icsk->icsk_sync_mss = tcp_sync_mss;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001862#ifdef CONFIG_TCP_MD5SIG
1863 tp->af_specific = &tcp_sock_ipv4_specific;
1864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 sk->sk_sndbuf = sysctl_tcp_wmem[1];
1867 sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1868
1869 atomic_inc(&tcp_sockets_allocated);
1870
1871 return 0;
1872}
1873
1874int tcp_v4_destroy_sock(struct sock *sk)
1875{
1876 struct tcp_sock *tp = tcp_sk(sk);
1877
1878 tcp_clear_xmit_timers(sk);
1879
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001880 tcp_cleanup_congestion_control(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /* Cleanup up the write buffer. */
David S. Millerfe067e82007-03-07 12:12:44 -08001883 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 /* Cleans up our, hopefully empty, out_of_order_queue. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001886 __skb_queue_purge(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001888#ifdef CONFIG_TCP_MD5SIG
1889 /* Clean up the MD5 key list, if any */
1890 if (tp->md5sig_info) {
1891 tcp_v4_clear_md5_list(sk);
1892 kfree(tp->md5sig_info);
1893 tp->md5sig_info = NULL;
1894 }
1895#endif
1896
Chris Leech1a2449a2006-05-23 18:05:53 -07001897#ifdef CONFIG_NET_DMA
1898 /* Cleans up our sk_async_wait_queue */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001899 __skb_queue_purge(&sk->sk_async_wait_queue);
Chris Leech1a2449a2006-05-23 18:05:53 -07001900#endif
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* Clean prequeue, it must be empty really */
1903 __skb_queue_purge(&tp->ucopy.prequeue);
1904
1905 /* Clean up a referenced TCP bind bucket. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001906 if (inet_csk(sk)->icsk_bind_hash)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001907 inet_put_port(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /*
1910 * If sendmsg cached page exists, toss it.
1911 */
1912 if (sk->sk_sndmsg_page) {
1913 __free_page(sk->sk_sndmsg_page);
1914 sk->sk_sndmsg_page = NULL;
1915 }
1916
1917 atomic_dec(&tcp_sockets_allocated);
1918
1919 return 0;
1920}
1921
1922EXPORT_SYMBOL(tcp_v4_destroy_sock);
1923
1924#ifdef CONFIG_PROC_FS
1925/* Proc filesystem TCP sock list dumping. */
1926
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001927static inline struct inet_timewait_sock *tw_head(struct hlist_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 return hlist_empty(head) ? NULL :
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001930 list_entry(head->first, struct inet_timewait_sock, tw_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07001933static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 return tw->tw_node.next ?
1936 hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
1937}
1938
1939static void *listening_get_next(struct seq_file *seq, void *cur)
1940{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001941 struct inet_connection_sock *icsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 struct hlist_node *node;
1943 struct sock *sk = cur;
1944 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001945 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 if (!sk) {
1948 st->bucket = 0;
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001949 sk = sk_head(&tcp_hashinfo.listening_hash[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 goto get_sk;
1951 }
1952
1953 ++st->num;
1954
1955 if (st->state == TCP_SEQ_STATE_OPENREQ) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001956 struct request_sock *req = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001958 icsk = inet_csk(st->syn_wait_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 req = req->dl_next;
1960 while (1) {
1961 while (req) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07001962 if (req->rsk_ops->family == st->family &&
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001963 net_eq(sock_net(req->sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 cur = req;
1965 goto out;
1966 }
1967 req = req->dl_next;
1968 }
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001969 if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 break;
1971get_req:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001972 req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 sk = sk_next(st->syn_wait_sk);
1975 st->state = TCP_SEQ_STATE_LISTENING;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001976 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 } else {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001978 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001979 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1980 if (reqsk_queue_len(&icsk->icsk_accept_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 goto start_req;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001982 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 sk = sk_next(sk);
1984 }
1985get_sk:
1986 sk_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001987 if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 cur = sk;
1989 goto out;
1990 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001991 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001992 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1993 if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994start_req:
1995 st->uid = sock_i_uid(sk);
1996 st->syn_wait_sk = sk;
1997 st->state = TCP_SEQ_STATE_OPENREQ;
1998 st->sbucket = 0;
1999 goto get_req;
2000 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002001 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -07002003 if (++st->bucket < INET_LHTABLE_SIZE) {
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002004 sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 goto get_sk;
2006 }
2007 cur = NULL;
2008out:
2009 return cur;
2010}
2011
2012static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
2013{
2014 void *rc = listening_get_next(seq, NULL);
2015
2016 while (rc && *pos) {
2017 rc = listening_get_next(seq, rc);
2018 --*pos;
2019 }
2020 return rc;
2021}
2022
2023static void *established_get_first(struct seq_file *seq)
2024{
2025 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07002026 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 void *rc = NULL;
2028
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002029 for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 struct sock *sk;
2031 struct hlist_node *node;
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002032 struct inet_timewait_sock *tw;
Eric Dumazet230140c2007-11-07 02:40:20 -08002033 rwlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Eric Dumazet230140c2007-11-07 02:40:20 -08002035 read_lock_bh(lock);
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002036 sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002037 if (sk->sk_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002038 !net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 continue;
2040 }
2041 rc = sk;
2042 goto out;
2043 }
2044 st->state = TCP_SEQ_STATE_TIME_WAIT;
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002045 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002046 &tcp_hashinfo.ehash[st->bucket].twchain) {
Pavel Emelyanov28518fc2008-03-21 15:52:00 -07002047 if (tw->tw_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002048 !net_eq(twsk_net(tw), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 continue;
2050 }
2051 rc = tw;
2052 goto out;
2053 }
Eric Dumazet230140c2007-11-07 02:40:20 -08002054 read_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 st->state = TCP_SEQ_STATE_ESTABLISHED;
2056 }
2057out:
2058 return rc;
2059}
2060
2061static void *established_get_next(struct seq_file *seq, void *cur)
2062{
2063 struct sock *sk = cur;
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -07002064 struct inet_timewait_sock *tw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct hlist_node *node;
2066 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07002067 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 ++st->num;
2070
2071 if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
2072 tw = cur;
2073 tw = tw_next(tw);
2074get_tw:
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002075 while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 tw = tw_next(tw);
2077 }
2078 if (tw) {
2079 cur = tw;
2080 goto out;
2081 }
Eric Dumazet230140c2007-11-07 02:40:20 -08002082 read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 st->state = TCP_SEQ_STATE_ESTABLISHED;
2084
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002085 if (++st->bucket < tcp_hashinfo.ehash_size) {
Eric Dumazet230140c2007-11-07 02:40:20 -08002086 read_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002087 sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 } else {
2089 cur = NULL;
2090 goto out;
2091 }
2092 } else
2093 sk = sk_next(sk);
2094
2095 sk_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002096 if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto found;
2098 }
2099
2100 st->state = TCP_SEQ_STATE_TIME_WAIT;
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002101 tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 goto get_tw;
2103found:
2104 cur = sk;
2105out:
2106 return cur;
2107}
2108
2109static void *established_get_idx(struct seq_file *seq, loff_t pos)
2110{
2111 void *rc = established_get_first(seq);
2112
2113 while (rc && pos) {
2114 rc = established_get_next(seq, rc);
2115 --pos;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return rc;
2118}
2119
2120static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
2121{
2122 void *rc;
2123 struct tcp_iter_state* st = seq->private;
2124
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002125 inet_listen_lock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 st->state = TCP_SEQ_STATE_LISTENING;
2127 rc = listening_get_idx(seq, &pos);
2128
2129 if (!rc) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002130 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 st->state = TCP_SEQ_STATE_ESTABLISHED;
2132 rc = established_get_idx(seq, pos);
2133 }
2134
2135 return rc;
2136}
2137
2138static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
2139{
2140 struct tcp_iter_state* st = seq->private;
2141 st->state = TCP_SEQ_STATE_LISTENING;
2142 st->num = 0;
2143 return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2144}
2145
2146static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2147{
2148 void *rc = NULL;
2149 struct tcp_iter_state* st;
2150
2151 if (v == SEQ_START_TOKEN) {
2152 rc = tcp_get_idx(seq, 0);
2153 goto out;
2154 }
2155 st = seq->private;
2156
2157 switch (st->state) {
2158 case TCP_SEQ_STATE_OPENREQ:
2159 case TCP_SEQ_STATE_LISTENING:
2160 rc = listening_get_next(seq, v);
2161 if (!rc) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002162 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 st->state = TCP_SEQ_STATE_ESTABLISHED;
2164 rc = established_get_first(seq);
2165 }
2166 break;
2167 case TCP_SEQ_STATE_ESTABLISHED:
2168 case TCP_SEQ_STATE_TIME_WAIT:
2169 rc = established_get_next(seq, v);
2170 break;
2171 }
2172out:
2173 ++*pos;
2174 return rc;
2175}
2176
2177static void tcp_seq_stop(struct seq_file *seq, void *v)
2178{
2179 struct tcp_iter_state* st = seq->private;
2180
2181 switch (st->state) {
2182 case TCP_SEQ_STATE_OPENREQ:
2183 if (v) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002184 struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
2185 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187 case TCP_SEQ_STATE_LISTENING:
2188 if (v != SEQ_START_TOKEN)
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002189 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 break;
2191 case TCP_SEQ_STATE_TIME_WAIT:
2192 case TCP_SEQ_STATE_ESTABLISHED:
2193 if (v)
Eric Dumazet230140c2007-11-07 02:40:20 -08002194 read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 break;
2196 }
2197}
2198
2199static int tcp_seq_open(struct inode *inode, struct file *file)
2200{
2201 struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct tcp_iter_state *s;
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002203 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002205 err = seq_open_net(inode, file, &afinfo->seq_ops,
2206 sizeof(struct tcp_iter_state));
2207 if (err < 0)
2208 return err;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002209
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002210 s = ((struct seq_file *)file->private_data)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 s->family = afinfo->family;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002212 return 0;
2213}
2214
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002215int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 int rc = 0;
2218 struct proc_dir_entry *p;
2219
Denis V. Lunev68fcadd2008-04-13 22:13:30 -07002220 afinfo->seq_fops.open = tcp_seq_open;
2221 afinfo->seq_fops.read = seq_read;
2222 afinfo->seq_fops.llseek = seq_lseek;
2223 afinfo->seq_fops.release = seq_release_net;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002224
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002225 afinfo->seq_ops.start = tcp_seq_start;
2226 afinfo->seq_ops.next = tcp_seq_next;
2227 afinfo->seq_ops.stop = tcp_seq_stop;
2228
Denis V. Lunev84841c32008-05-02 04:10:08 -07002229 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
2230 &afinfo->seq_fops, afinfo);
2231 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 rc = -ENOMEM;
2233 return rc;
2234}
2235
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002236void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002238 proc_net_remove(net, afinfo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002241static void get_openreq4(struct sock *sk, struct request_sock *req,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002242 struct seq_file *f, int i, int uid, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002244 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 int ttd = req->expires - jiffies;
2246
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002247 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2248 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 i,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002250 ireq->loc_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 ntohs(inet_sk(sk)->sport),
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002252 ireq->rmt_addr,
2253 ntohs(ireq->rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 TCP_SYN_RECV,
2255 0, 0, /* could print option size, but that is af dependent. */
2256 1, /* timers active (only the expire timer) */
2257 jiffies_to_clock_t(ttd),
2258 req->retrans,
2259 uid,
2260 0, /* non standard timer */
2261 0, /* open_requests have no inode */
2262 atomic_read(&sk->sk_refcnt),
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002263 req,
2264 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002267static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 int timer_active;
2270 unsigned long timer_expires;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002271 struct tcp_sock *tp = tcp_sk(sk);
2272 const struct inet_connection_sock *icsk = inet_csk(sk);
2273 struct inet_sock *inet = inet_sk(sk);
Al Viro714e85b2006-11-14 20:51:49 -08002274 __be32 dest = inet->daddr;
2275 __be32 src = inet->rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 __u16 destp = ntohs(inet->dport);
2277 __u16 srcp = ntohs(inet->sport);
2278
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002279 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002281 timer_expires = icsk->icsk_timeout;
2282 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002284 timer_expires = icsk->icsk_timeout;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002285 } else if (timer_pending(&sk->sk_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 timer_active = 2;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002287 timer_expires = sk->sk_timer.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 } else {
2289 timer_active = 0;
2290 timer_expires = jiffies;
2291 }
2292
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002293 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
Stephen Hemminger7be87352008-06-27 20:00:19 -07002294 "%08X %5d %8d %lu %d %p %lu %lu %u %u %d%n",
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002295 i, src, srcp, dest, destp, sk->sk_state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07002296 tp->write_seq - tp->snd_una,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002297 sk->sk_state == TCP_LISTEN ? sk->sk_ack_backlog :
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002298 (tp->rcv_nxt - tp->copied_seq),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 timer_active,
2300 jiffies_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002301 icsk->icsk_retransmits,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002302 sock_i_uid(sk),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002303 icsk->icsk_probes_out,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002304 sock_i_ino(sk),
2305 atomic_read(&sk->sk_refcnt), sk,
Stephen Hemminger7be87352008-06-27 20:00:19 -07002306 jiffies_to_clock_t(icsk->icsk_rto),
2307 jiffies_to_clock_t(icsk->icsk_ack.ato),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002308 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 tp->snd_cwnd,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002310 tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh,
2311 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002314static void get_timewait4_sock(struct inet_timewait_sock *tw,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002315 struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Al Viro23f33c22006-09-27 18:43:50 -07002317 __be32 dest, src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 __u16 destp, srcp;
2319 int ttd = tw->tw_ttd - jiffies;
2320
2321 if (ttd < 0)
2322 ttd = 0;
2323
2324 dest = tw->tw_daddr;
2325 src = tw->tw_rcv_saddr;
2326 destp = ntohs(tw->tw_dport);
2327 srcp = ntohs(tw->tw_sport);
2328
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002329 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2330 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
2332 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002333 atomic_read(&tw->tw_refcnt), tw, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
2336#define TMPSZ 150
2337
2338static int tcp4_seq_show(struct seq_file *seq, void *v)
2339{
2340 struct tcp_iter_state* st;
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002341 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 if (v == SEQ_START_TOKEN) {
2344 seq_printf(seq, "%-*s\n", TMPSZ - 1,
2345 " sl local_address rem_address st tx_queue "
2346 "rx_queue tr tm->when retrnsmt uid timeout "
2347 "inode");
2348 goto out;
2349 }
2350 st = seq->private;
2351
2352 switch (st->state) {
2353 case TCP_SEQ_STATE_LISTENING:
2354 case TCP_SEQ_STATE_ESTABLISHED:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002355 get_tcp4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 break;
2357 case TCP_SEQ_STATE_OPENREQ:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002358 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 break;
2360 case TCP_SEQ_STATE_TIME_WAIT:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002361 get_timewait4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 break;
2363 }
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002364 seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365out:
2366 return 0;
2367}
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369static struct tcp_seq_afinfo tcp4_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 .name = "tcp",
2371 .family = AF_INET,
Denis V. Lunev5f4472c2008-04-13 22:13:53 -07002372 .seq_fops = {
2373 .owner = THIS_MODULE,
2374 },
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002375 .seq_ops = {
2376 .show = tcp4_seq_show,
2377 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378};
2379
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002380static int tcp4_proc_init_net(struct net *net)
2381{
2382 return tcp_proc_register(net, &tcp4_seq_afinfo);
2383}
2384
2385static void tcp4_proc_exit_net(struct net *net)
2386{
2387 tcp_proc_unregister(net, &tcp4_seq_afinfo);
2388}
2389
2390static struct pernet_operations tcp4_net_ops = {
2391 .init = tcp4_proc_init_net,
2392 .exit = tcp4_proc_exit_net,
2393};
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395int __init tcp4_proc_init(void)
2396{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002397 return register_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
2400void tcp4_proc_exit(void)
2401{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002402 unregister_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404#endif /* CONFIG_PROC_FS */
2405
2406struct proto tcp_prot = {
2407 .name = "TCP",
2408 .owner = THIS_MODULE,
2409 .close = tcp_close,
2410 .connect = tcp_v4_connect,
2411 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002412 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 .ioctl = tcp_ioctl,
2414 .init = tcp_v4_init_sock,
2415 .destroy = tcp_v4_destroy_sock,
2416 .shutdown = tcp_shutdown,
2417 .setsockopt = tcp_setsockopt,
2418 .getsockopt = tcp_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 .recvmsg = tcp_recvmsg,
2420 .backlog_rcv = tcp_v4_do_rcv,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08002421 .hash = inet_hash,
2422 .unhash = inet_unhash,
2423 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 .enter_memory_pressure = tcp_enter_memory_pressure,
2425 .sockets_allocated = &tcp_sockets_allocated,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07002426 .orphan_count = &tcp_orphan_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 .memory_allocated = &tcp_memory_allocated,
2428 .memory_pressure = &tcp_memory_pressure,
2429 .sysctl_mem = sysctl_tcp_mem,
2430 .sysctl_wmem = sysctl_tcp_wmem,
2431 .sysctl_rmem = sysctl_tcp_rmem,
2432 .max_header = MAX_TCP_HEADER,
2433 .obj_size = sizeof(struct tcp_sock),
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002434 .twsk_prot = &tcp_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002435 .rsk_prot = &tcp_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07002436 .h.hashinfo = &tcp_hashinfo,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002437#ifdef CONFIG_COMPAT
2438 .compat_setsockopt = compat_tcp_setsockopt,
2439 .compat_getsockopt = compat_tcp_getsockopt,
2440#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441};
2442
Denis V. Lunev046ee902008-04-03 14:31:33 -07002443
2444static int __net_init tcp_sk_init(struct net *net)
2445{
2446 return inet_ctl_sock_create(&net->ipv4.tcp_sock,
2447 PF_INET, SOCK_RAW, IPPROTO_TCP, net);
2448}
2449
2450static void __net_exit tcp_sk_exit(struct net *net)
2451{
2452 inet_ctl_sock_destroy(net->ipv4.tcp_sock);
2453}
2454
2455static struct pernet_operations __net_initdata tcp_sk_ops = {
2456 .init = tcp_sk_init,
2457 .exit = tcp_sk_exit,
2458};
2459
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08002460void __init tcp_v4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Denis V. Lunev046ee902008-04-03 14:31:33 -07002462 if (register_pernet_device(&tcp_sk_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 panic("Failed to create the TCP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
2466EXPORT_SYMBOL(ipv4_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467EXPORT_SYMBOL(tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468EXPORT_SYMBOL(tcp_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469EXPORT_SYMBOL(tcp_v4_conn_request);
2470EXPORT_SYMBOL(tcp_v4_connect);
2471EXPORT_SYMBOL(tcp_v4_do_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472EXPORT_SYMBOL(tcp_v4_remember_stamp);
2473EXPORT_SYMBOL(tcp_v4_send_check);
2474EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
2475
2476#ifdef CONFIG_PROC_FS
2477EXPORT_SYMBOL(tcp_proc_register);
2478EXPORT_SYMBOL(tcp_proc_unregister);
2479#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480EXPORT_SYMBOL(sysctl_tcp_low_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481