blob: f2926ae1de57dee2821915c2e3f11b60e36927bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Implementation of the Transmission Control Protocol(TCP).
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * IPv4 specific functions
9 *
10 *
11 * code split from:
12 * linux/ipv4/tcp.c
13 * linux/ipv4/tcp_input.c
14 * linux/ipv4/tcp_output.c
15 *
16 * See tcp.c for author information
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
24/*
25 * Changes:
26 * David S. Miller : New socket lookup architecture.
27 * This code is dedicated to John Dyson.
28 * David S. Miller : Change semantics of established hash,
29 * half is devoted to TIME_WAIT sockets
30 * and the rest go in the other half.
31 * Andi Kleen : Add support for syncookies and fixed
32 * some bugs: ip options weren't passed to
33 * the TCP layer, missed a check for an
34 * ACK bit.
35 * Andi Kleen : Implemented fast path mtu discovery.
36 * Fixed many serious bugs in the
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -070037 * request_sock handling and moved
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * most of it into the af independent code.
39 * Added tail drop and some other bugfixes.
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -080040 * Added new listen semantics.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * Mike McLagan : Routing by source
42 * Juan Jose Ciarlante: ip_dynaddr bits
43 * Andi Kleen: various fixes.
44 * Vitaly E. Lavrov : Transparent proxy revived after year
45 * coma.
46 * Andi Kleen : Fix new listen.
47 * Andi Kleen : Fix accept error reporting.
48 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
49 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
50 * a single port at the same time.
51 */
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <linux/types.h>
55#include <linux/fcntl.h>
56#include <linux/module.h>
57#include <linux/random.h>
58#include <linux/cache.h>
59#include <linux/jhash.h>
60#include <linux/init.h>
61#include <linux/times.h>
62
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020063#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/icmp.h>
Arnaldo Carvalho de Melo304a1612005-08-09 19:59:20 -070065#include <net/inet_hashtables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/tcp.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030067#include <net/transp_v6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/ipv6.h>
69#include <net/inet_common.h>
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -080070#include <net/timewait_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <net/xfrm.h>
Chris Leech1a2449a2006-05-23 18:05:53 -070072#include <net/netdma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#include <linux/inet.h>
75#include <linux/ipv6.h>
76#include <linux/stddef.h>
77#include <linux/proc_fs.h>
78#include <linux/seq_file.h>
79
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080080#include <linux/crypto.h>
81#include <linux/scatterlist.h>
82
Brian Haleyab32ea52006-09-22 14:15:41 -070083int sysctl_tcp_tw_reuse __read_mostly;
84int sysctl_tcp_low_latency __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Check TCP sequence numbers in ICMP packets. */
87#define ICMP_MIN_LENGTH 8
88
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -080089void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080091#ifdef CONFIG_TCP_MD5SIG
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020092static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk,
93 __be32 addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080094static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -020095 __be32 saddr, __be32 daddr,
96 struct tcphdr *th, int protocol,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -080097 unsigned int tcplen);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080098#endif
99
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700100struct inet_hashinfo __cacheline_aligned tcp_hashinfo = {
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200101 .lhash_lock = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock),
102 .lhash_users = ATOMIC_INIT(0),
103 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Gerrit Renkera94f7232006-11-10 14:06:49 -0800106static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700108 return secure_tcp_sequence_number(ip_hdr(skb)->daddr,
109 ip_hdr(skb)->saddr,
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700110 tcp_hdr(skb)->dest,
111 tcp_hdr(skb)->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800114int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
115{
116 const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
117 struct tcp_sock *tp = tcp_sk(sk);
118
119 /* With PAWS, it is safe from the viewpoint
120 of data integrity. Even without PAWS it is safe provided sequence
121 spaces do not overlap i.e. at data rates <= 80Mbit/sec.
122
123 Actually, the idea is close to VJ's one, only timestamp cache is
124 held not per host, but per port pair and TW bucket is used as state
125 holder.
126
127 If TW bucket has been already destroyed we fall back to VJ's scheme
128 and use initial timestamp retrieved from peer table.
129 */
130 if (tcptw->tw_ts_recent_stamp &&
131 (twp == NULL || (sysctl_tcp_tw_reuse &&
James Morris9d729f72007-03-04 16:12:44 -0800132 get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800133 tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
134 if (tp->write_seq == 0)
135 tp->write_seq = 1;
136 tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
137 tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
138 sock_hold(sktw);
139 return 1;
140 }
141
142 return 0;
143}
144
145EXPORT_SYMBOL_GPL(tcp_twsk_unique);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* This will initiate an outgoing connection. */
148int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
149{
150 struct inet_sock *inet = inet_sk(sk);
151 struct tcp_sock *tp = tcp_sk(sk);
152 struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
153 struct rtable *rt;
Al Virobada8ad2006-09-26 21:27:15 -0700154 __be32 daddr, nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int tmp;
156 int err;
157
158 if (addr_len < sizeof(struct sockaddr_in))
159 return -EINVAL;
160
161 if (usin->sin_family != AF_INET)
162 return -EAFNOSUPPORT;
163
164 nexthop = daddr = usin->sin_addr.s_addr;
165 if (inet->opt && inet->opt->srr) {
166 if (!daddr)
167 return -EINVAL;
168 nexthop = inet->opt->faddr;
169 }
170
171 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
172 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
173 IPPROTO_TCP,
David S. Miller8eb90862007-02-08 02:09:21 -0800174 inet->sport, usin->sin_port, sk, 1);
Wei Dong584bdf82007-05-31 22:49:28 -0700175 if (tmp < 0) {
176 if (tmp == -ENETUNREACH)
177 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return tmp;
Wei Dong584bdf82007-05-31 22:49:28 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
182 ip_rt_put(rt);
183 return -ENETUNREACH;
184 }
185
186 if (!inet->opt || !inet->opt->srr)
187 daddr = rt->rt_dst;
188
189 if (!inet->saddr)
190 inet->saddr = rt->rt_src;
191 inet->rcv_saddr = inet->saddr;
192
193 if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) {
194 /* Reset inherited state */
195 tp->rx_opt.ts_recent = 0;
196 tp->rx_opt.ts_recent_stamp = 0;
197 tp->write_seq = 0;
198 }
199
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -0700200 if (tcp_death_row.sysctl_tw_recycle &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
202 struct inet_peer *peer = rt_get_peer(rt);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200203 /*
204 * VJ's idea. We save last timestamp seen from
205 * the destination in peer table, when entering state
206 * TIME-WAIT * and initialize rx_opt.ts_recent from it,
207 * when trying new connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200209 if (peer != NULL &&
James Morris9d729f72007-03-04 16:12:44 -0800210 peer->tcp_ts_stamp + TCP_PAWS_MSL >= get_seconds()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
212 tp->rx_opt.ts_recent = peer->tcp_ts;
213 }
214 }
215
216 inet->dport = usin->sin_port;
217 inet->daddr = daddr;
218
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800219 inet_csk(sk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (inet->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800221 inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 tp->rx_opt.mss_clamp = 536;
224
225 /* Socket identity is still unknown (sport may be zero).
226 * However we set state to SYN-SENT and not releasing socket
227 * lock select source port, enter ourselves into the hash tables and
228 * complete initialization after this.
229 */
230 tcp_set_state(sk, TCP_SYN_SENT);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800231 err = inet_hash_connect(&tcp_death_row, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (err)
233 goto failure;
234
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200235 err = ip_route_newports(&rt, IPPROTO_TCP,
236 inet->sport, inet->dport, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (err)
238 goto failure;
239
240 /* OK, now commit destination to socket. */
Herbert Xubcd76112006-06-30 13:36:35 -0700241 sk->sk_gso_type = SKB_GSO_TCPV4;
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -0700242 sk_setup_caps(sk, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (!tp->write_seq)
245 tp->write_seq = secure_tcp_sequence_number(inet->saddr,
246 inet->daddr,
247 inet->sport,
248 usin->sin_port);
249
250 inet->id = tp->write_seq ^ jiffies;
251
252 err = tcp_connect(sk);
253 rt = NULL;
254 if (err)
255 goto failure;
256
257 return 0;
258
259failure:
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200260 /*
261 * This unhashes the socket and releases the local port,
262 * if necessary.
263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 tcp_set_state(sk, TCP_CLOSE);
265 ip_rt_put(rt);
266 sk->sk_route_caps = 0;
267 inet->dport = 0;
268 return err;
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/*
272 * This routine does path mtu discovery as defined in RFC1191.
273 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800274static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct dst_entry *dst;
277 struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
280 * send out by Linux are always <576bytes so they should go through
281 * unfragmented).
282 */
283 if (sk->sk_state == TCP_LISTEN)
284 return;
285
286 /* We don't check in the destentry if pmtu discovery is forbidden
287 * on this route. We just assume that no packet_to_big packets
288 * are send back when pmtu discovery is not active.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900289 * There is a small race when the user changes this flag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * route, but I think that's acceptable.
291 */
292 if ((dst = __sk_dst_check(sk, 0)) == NULL)
293 return;
294
295 dst->ops->update_pmtu(dst, mtu);
296
297 /* Something is about to be wrong... Remember soft error
298 * for the case, if this connection will not able to recover.
299 */
300 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
301 sk->sk_err_soft = EMSGSIZE;
302
303 mtu = dst_mtu(dst);
304
305 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800306 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 tcp_sync_mss(sk, mtu);
308
309 /* Resend the TCP packet because it's
310 * clear that the old packet has been
311 * dropped. This is the new "fast" path mtu
312 * discovery.
313 */
314 tcp_simple_retransmit(sk);
315 } /* else let the usual retransmit timer handle it */
316}
317
318/*
319 * This routine is called by the ICMP module when it gets some
320 * sort of error condition. If err < 0 then the socket should
321 * be closed and the error returned to the user. If err > 0
322 * it's just the icmp type << 8 | icmp code. After adjustment
323 * header points to the first 8 bytes of the tcp header. We need
324 * to find the appropriate port.
325 *
326 * The locking strategy used here is very "optimistic". When
327 * someone else accesses the socket the ICMP is just dropped
328 * and for some paths there is no check at all.
329 * A more general error queue to queue errors for later handling
330 * is probably better.
331 *
332 */
333
334void tcp_v4_err(struct sk_buff *skb, u32 info)
335{
336 struct iphdr *iph = (struct iphdr *)skb->data;
337 struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
338 struct tcp_sock *tp;
339 struct inet_sock *inet;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300340 const int type = icmp_hdr(skb)->type;
341 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct sock *sk;
343 __u32 seq;
344 int err;
345
346 if (skb->len < (iph->ihl << 2) + 8) {
347 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
348 return;
349 }
350
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900351 sk = inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->daddr, th->dest,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -0800352 iph->saddr, th->source, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!sk) {
354 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
355 return;
356 }
357 if (sk->sk_state == TCP_TIME_WAIT) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -0700358 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return;
360 }
361
362 bh_lock_sock(sk);
363 /* If too many ICMPs get dropped on busy
364 * servers this needs to be solved differently.
365 */
366 if (sock_owned_by_user(sk))
367 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
368
369 if (sk->sk_state == TCP_CLOSE)
370 goto out;
371
372 tp = tcp_sk(sk);
373 seq = ntohl(th->seq);
374 if (sk->sk_state != TCP_LISTEN &&
375 !between(seq, tp->snd_una, tp->snd_nxt)) {
Eric Dumazet06ca7192006-10-20 00:22:25 -0700376 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto out;
378 }
379
380 switch (type) {
381 case ICMP_SOURCE_QUENCH:
382 /* Just silently ignore these. */
383 goto out;
384 case ICMP_PARAMETERPROB:
385 err = EPROTO;
386 break;
387 case ICMP_DEST_UNREACH:
388 if (code > NR_ICMP_UNREACH)
389 goto out;
390
391 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
392 if (!sock_owned_by_user(sk))
393 do_pmtu_discovery(sk, iph, info);
394 goto out;
395 }
396
397 err = icmp_err_convert[code].errno;
398 break;
399 case ICMP_TIME_EXCEEDED:
400 err = EHOSTUNREACH;
401 break;
402 default:
403 goto out;
404 }
405
406 switch (sk->sk_state) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700407 struct request_sock *req, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 case TCP_LISTEN:
409 if (sock_owned_by_user(sk))
410 goto out;
411
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700412 req = inet_csk_search_req(sk, &prev, th->dest,
413 iph->daddr, iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!req)
415 goto out;
416
417 /* ICMPs are not backlogged, hence we cannot get
418 an established socket here.
419 */
420 BUG_TRAP(!req->sk);
421
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700422 if (seq != tcp_rsk(req)->snt_isn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
424 goto out;
425 }
426
427 /*
428 * Still in SYN_RECV, just remove it silently.
429 * There is no good way to pass the error to the newly
430 * created socket, and POSIX does not want network
431 * errors returned from accept().
432 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700433 inet_csk_reqsk_queue_drop(sk, req, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 goto out;
435
436 case TCP_SYN_SENT:
437 case TCP_SYN_RECV: /* Cannot happen.
438 It can f.e. if SYNs crossed.
439 */
440 if (!sock_owned_by_user(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 sk->sk_err = err;
442
443 sk->sk_error_report(sk);
444
445 tcp_done(sk);
446 } else {
447 sk->sk_err_soft = err;
448 }
449 goto out;
450 }
451
452 /* If we've already connected we will keep trying
453 * until we time out, or the user gives up.
454 *
455 * rfc1122 4.2.3.9 allows to consider as hard errors
456 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
457 * but it is obsoleted by pmtu discovery).
458 *
459 * Note, that in modern internet, where routing is unreliable
460 * and in each dark corner broken firewalls sit, sending random
461 * errors ordered by their masters even this two messages finally lose
462 * their original sense (even Linux sends invalid PORT_UNREACHs)
463 *
464 * Now we are in compliance with RFCs.
465 * --ANK (980905)
466 */
467
468 inet = inet_sk(sk);
469 if (!sock_owned_by_user(sk) && inet->recverr) {
470 sk->sk_err = err;
471 sk->sk_error_report(sk);
472 } else { /* Only an error on timeout */
473 sk->sk_err_soft = err;
474 }
475
476out:
477 bh_unlock_sock(sk);
478 sock_put(sk);
479}
480
481/* This routine computes an IPv4 TCP checksum. */
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -0800482void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700485 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Patrick McHardy84fa7932006-08-29 16:44:56 -0700487 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800488 th->check = ~tcp_v4_check(len, inet->saddr,
489 inet->daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700490 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800491 skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800493 th->check = tcp_v4_check(len, inet->saddr, inet->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 csum_partial((char *)th,
495 th->doff << 2,
496 skb->csum));
497 }
498}
499
Herbert Xua430a432006-07-08 13:34:56 -0700500int tcp_v4_gso_send_check(struct sk_buff *skb)
501{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700502 const struct iphdr *iph;
Herbert Xua430a432006-07-08 13:34:56 -0700503 struct tcphdr *th;
504
505 if (!pskb_may_pull(skb, sizeof(*th)))
506 return -EINVAL;
507
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700508 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700509 th = tcp_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -0700510
511 th->check = 0;
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800512 th->check = ~tcp_v4_check(skb->len, iph->saddr, iph->daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700513 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800514 skb->csum_offset = offsetof(struct tcphdr, check);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700515 skb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xua430a432006-07-08 13:34:56 -0700516 return 0;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/*
520 * This routine will send an RST to the other tcp.
521 *
522 * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
523 * for reset.
524 * Answer: if a packet caused RST, it is not for a socket
525 * existing in our system, if it is matched to a socket,
526 * it is just duplicate segment or bug in other side's TCP.
527 * So that we build reply only basing on parameters
528 * arrived with segment.
529 * Exception: precedence violation. We do not implement it in any case.
530 */
531
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800532static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700534 struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800535 struct {
536 struct tcphdr th;
537#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800538 __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800539#endif
540 } rep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800542#ifdef CONFIG_TCP_MD5SIG
543 struct tcp_md5sig_key *key;
544#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /* Never send a reset in response to a reset. */
547 if (th->rst)
548 return;
549
Eric Dumazetee6b9672008-03-05 18:30:47 -0800550 if (skb->rtable->rt_type != RTN_LOCAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return;
552
553 /* Swap the send and the receive. */
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800554 memset(&rep, 0, sizeof(rep));
555 rep.th.dest = th->source;
556 rep.th.source = th->dest;
557 rep.th.doff = sizeof(struct tcphdr) / 4;
558 rep.th.rst = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (th->ack) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800561 rep.th.seq = th->ack_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800563 rep.th.ack = 1;
564 rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
565 skb->len - (th->doff << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200568 memset(&arg, 0, sizeof(arg));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800569 arg.iov[0].iov_base = (unsigned char *)&rep;
570 arg.iov[0].iov_len = sizeof(rep.th);
571
572#ifdef CONFIG_TCP_MD5SIG
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700573 key = sk ? tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr) : NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800574 if (key) {
575 rep.opt[0] = htonl((TCPOPT_NOP << 24) |
576 (TCPOPT_NOP << 16) |
577 (TCPOPT_MD5SIG << 8) |
578 TCPOLEN_MD5SIG);
579 /* Update length and the length the header thinks exists */
580 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
581 rep.th.doff = arg.iov[0].iov_len / 4;
582
583 tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[1],
584 key,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700585 ip_hdr(skb)->daddr,
586 ip_hdr(skb)->saddr,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800587 &rep.th, IPPROTO_TCP,
588 arg.iov[0].iov_len);
589 }
590#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700591 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
592 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 sizeof(struct tcphdr), IPPROTO_TCP, 0);
594 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
595
Denis V. Lunev7feb49c2008-04-03 14:32:00 -0700596 ip_send_reply(dev_net(skb->dst->dev)->ipv4.tcp_sock, skb,
597 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
600 TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
601}
602
603/* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
604 outside socket context is ugly, certainly. What can I do?
605 */
606
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800607static void tcp_v4_send_ack(struct tcp_timewait_sock *twsk,
608 struct sk_buff *skb, u32 seq, u32 ack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 u32 win, u32 ts)
610{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700611 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct {
613 struct tcphdr th;
Al Viro714e85b2006-11-14 20:51:49 -0800614 __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800615#ifdef CONFIG_TCP_MD5SIG
Al Viro714e85b2006-11-14 20:51:49 -0800616 + (TCPOLEN_MD5SIG_ALIGNED >> 2)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800617#endif
618 ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 } rep;
620 struct ip_reply_arg arg;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800621#ifdef CONFIG_TCP_MD5SIG
622 struct tcp_md5sig_key *key;
623 struct tcp_md5sig_key tw_key;
624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 memset(&rep.th, 0, sizeof(struct tcphdr));
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200627 memset(&arg, 0, sizeof(arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 arg.iov[0].iov_base = (unsigned char *)&rep;
630 arg.iov[0].iov_len = sizeof(rep.th);
631 if (ts) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800632 rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
633 (TCPOPT_TIMESTAMP << 8) |
634 TCPOLEN_TIMESTAMP);
635 rep.opt[1] = htonl(tcp_time_stamp);
636 rep.opt[2] = htonl(ts);
Craig Schlentercb48cfe2007-01-09 00:11:15 -0800637 arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 /* Swap the send and the receive. */
641 rep.th.dest = th->source;
642 rep.th.source = th->dest;
643 rep.th.doff = arg.iov[0].iov_len / 4;
644 rep.th.seq = htonl(seq);
645 rep.th.ack_seq = htonl(ack);
646 rep.th.ack = 1;
647 rep.th.window = htons(win);
648
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800649#ifdef CONFIG_TCP_MD5SIG
650 /*
651 * The SKB holds an imcoming packet, but may not have a valid ->sk
652 * pointer. This is especially the case when we're dealing with a
653 * TIME_WAIT ack, because the sk structure is long gone, and only
654 * the tcp_timewait_sock remains. So the md5 key is stashed in that
655 * structure, and we use it in preference. I believe that (twsk ||
656 * skb->sk) holds true, but we program defensively.
657 */
658 if (!twsk && skb->sk) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700659 key = tcp_v4_md5_do_lookup(skb->sk, ip_hdr(skb)->daddr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800660 } else if (twsk && twsk->tw_md5_keylen) {
661 tw_key.key = twsk->tw_md5_key;
662 tw_key.keylen = twsk->tw_md5_keylen;
663 key = &tw_key;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200664 } else
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800665 key = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800666
667 if (key) {
668 int offset = (ts) ? 3 : 0;
669
670 rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
671 (TCPOPT_NOP << 16) |
672 (TCPOPT_MD5SIG << 8) |
673 TCPOLEN_MD5SIG);
674 arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
675 rep.th.doff = arg.iov[0].iov_len/4;
676
677 tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[offset],
678 key,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700679 ip_hdr(skb)->daddr,
680 ip_hdr(skb)->saddr,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800681 &rep.th, IPPROTO_TCP,
682 arg.iov[0].iov_len);
683 }
684#endif
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700685 arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
686 ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 arg.iov[0].iov_len, IPPROTO_TCP, 0);
688 arg.csumoffset = offsetof(struct tcphdr, check) / 2;
Patrick McHardyf0e48db2007-06-04 21:32:46 -0700689 if (twsk)
690 arg.bound_dev_if = twsk->tw_sk.tw_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Denis V. Lunev7feb49c2008-04-03 14:32:00 -0700692 ip_send_reply(dev_net(skb->dev)->ipv4.tcp_sock, skb,
693 &arg, arg.iov[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
696}
697
698static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
699{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700700 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800701 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800703 tcp_v4_send_ack(tcptw, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200704 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
705 tcptw->tw_ts_recent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700707 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200710static void tcp_v4_reqsk_send_ack(struct sk_buff *skb,
711 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800713 tcp_v4_send_ack(NULL, skb, tcp_rsk(req)->snt_isn + 1,
714 tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 req->ts_recent);
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718/*
Kris Katterjohn9bf1d832008-02-17 22:29:19 -0800719 * Send a SYN-ACK after having received a SYN.
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700720 * This still operates on a request_sock only, not on a big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * socket.
722 */
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800723static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
724 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700726 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int err = -1;
728 struct sk_buff * skb;
729
730 /* First, grab a route. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700731 if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800732 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 skb = tcp_make_synack(sk, dst, req);
735
736 if (skb) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700737 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Frederik Deweerdtba7808e2007-02-04 20:15:27 -0800739 th->check = tcp_v4_check(skb->len,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700740 ireq->loc_addr,
741 ireq->rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 csum_partial((char *)th, skb->len,
743 skb->csum));
744
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700745 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
746 ireq->rmt_addr,
747 ireq->opt);
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200748 err = net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 dst_release(dst);
752 return err;
753}
754
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800755static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req)
756{
757 return __tcp_v4_send_synack(sk, req, NULL);
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700761 * IPv4 request_sock destructor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700763static void tcp_v4_reqsk_destructor(struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Jesper Juhla51482b2005-11-08 09:41:34 -0800765 kfree(inet_rsk(req)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200768#ifdef CONFIG_SYN_COOKIES
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800769static void syn_flood_warning(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 static unsigned long warntime;
772
773 if (time_after(jiffies, (warntime + HZ * 60))) {
774 warntime = jiffies;
775 printk(KERN_INFO
776 "possible SYN flooding on port %d. Sending cookies.\n",
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700777 ntohs(tcp_hdr(skb)->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779}
Arnaldo Carvalho de Melo80e40da2006-01-04 01:58:06 -0200780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782/*
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700783 * Save and compile IPv4 options into the request_sock if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800785static struct ip_options *tcp_v4_save_options(struct sock *sk,
786 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct ip_options *opt = &(IPCB(skb)->opt);
789 struct ip_options *dopt = NULL;
790
791 if (opt && opt->optlen) {
792 int opt_size = optlength(opt);
793 dopt = kmalloc(opt_size, GFP_ATOMIC);
794 if (dopt) {
795 if (ip_options_echo(dopt, skb)) {
796 kfree(dopt);
797 dopt = NULL;
798 }
799 }
800 }
801 return dopt;
802}
803
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800804#ifdef CONFIG_TCP_MD5SIG
805/*
806 * RFC2385 MD5 checksumming requires a mapping of
807 * IP address->MD5 Key.
808 * We need to maintain these in the sk structure.
809 */
810
811/* Find the Key structure for an address. */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200812static struct tcp_md5sig_key *
813 tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800814{
815 struct tcp_sock *tp = tcp_sk(sk);
816 int i;
817
818 if (!tp->md5sig_info || !tp->md5sig_info->entries4)
819 return NULL;
820 for (i = 0; i < tp->md5sig_info->entries4; i++) {
821 if (tp->md5sig_info->keys4[i].addr == addr)
David S. Millerf8ab18d2007-09-28 15:18:35 -0700822 return &tp->md5sig_info->keys4[i].base;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800823 }
824 return NULL;
825}
826
827struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
828 struct sock *addr_sk)
829{
830 return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->daddr);
831}
832
833EXPORT_SYMBOL(tcp_v4_md5_lookup);
834
Adrian Bunkf5b99bc2006-11-30 17:22:29 -0800835static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
836 struct request_sock *req)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800837{
838 return tcp_v4_md5_do_lookup(sk, inet_rsk(req)->rmt_addr);
839}
840
841/* This can be called on a newly created socket, from other files */
842int tcp_v4_md5_do_add(struct sock *sk, __be32 addr,
843 u8 *newkey, u8 newkeylen)
844{
845 /* Add Key to the list */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700846 struct tcp_md5sig_key *key;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800847 struct tcp_sock *tp = tcp_sk(sk);
848 struct tcp4_md5sig_key *keys;
849
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700850 key = tcp_v4_md5_do_lookup(sk, addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800851 if (key) {
852 /* Pre-existing entry - just update that one. */
Matthias M. Dellwegb0a713e2007-10-29 20:55:27 -0700853 kfree(key->key);
854 key->key = newkey;
855 key->keylen = newkeylen;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800856 } else {
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200857 struct tcp_md5sig_info *md5sig;
858
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800859 if (!tp->md5sig_info) {
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200860 tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info),
861 GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800862 if (!tp->md5sig_info) {
863 kfree(newkey);
864 return -ENOMEM;
865 }
David S. Miller3d7dbea2007-06-12 14:36:42 -0700866 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800867 }
868 if (tcp_alloc_md5sig_pool() == NULL) {
869 kfree(newkey);
870 return -ENOMEM;
871 }
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200872 md5sig = tp->md5sig_info;
873
874 if (md5sig->alloced4 == md5sig->entries4) {
875 keys = kmalloc((sizeof(*keys) *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900876 (md5sig->entries4 + 1)), GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800877 if (!keys) {
878 kfree(newkey);
879 tcp_free_md5sig_pool();
880 return -ENOMEM;
881 }
882
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200883 if (md5sig->entries4)
884 memcpy(keys, md5sig->keys4,
885 sizeof(*keys) * md5sig->entries4);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800886
887 /* Free old key list, and reference new one */
YOSHIFUJI Hideakia80cc202007-11-20 17:30:06 -0800888 kfree(md5sig->keys4);
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200889 md5sig->keys4 = keys;
890 md5sig->alloced4++;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800891 }
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200892 md5sig->entries4++;
David S. Millerf8ab18d2007-09-28 15:18:35 -0700893 md5sig->keys4[md5sig->entries4 - 1].addr = addr;
894 md5sig->keys4[md5sig->entries4 - 1].base.key = newkey;
895 md5sig->keys4[md5sig->entries4 - 1].base.keylen = newkeylen;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800896 }
897 return 0;
898}
899
900EXPORT_SYMBOL(tcp_v4_md5_do_add);
901
902static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk,
903 u8 *newkey, u8 newkeylen)
904{
905 return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->daddr,
906 newkey, newkeylen);
907}
908
909int tcp_v4_md5_do_del(struct sock *sk, __be32 addr)
910{
911 struct tcp_sock *tp = tcp_sk(sk);
912 int i;
913
914 for (i = 0; i < tp->md5sig_info->entries4; i++) {
915 if (tp->md5sig_info->keys4[i].addr == addr) {
916 /* Free the key */
David S. Millerf8ab18d2007-09-28 15:18:35 -0700917 kfree(tp->md5sig_info->keys4[i].base.key);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800918 tp->md5sig_info->entries4--;
919
920 if (tp->md5sig_info->entries4 == 0) {
921 kfree(tp->md5sig_info->keys4);
922 tp->md5sig_info->keys4 = NULL;
Leigh Brown8228a18d2006-12-17 17:12:30 -0800923 tp->md5sig_info->alloced4 = 0;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200924 } else if (tp->md5sig_info->entries4 != i) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800925 /* Need to do some manipulation */
YOSHIFUJI Hideaki354faf02007-11-20 17:30:31 -0800926 memmove(&tp->md5sig_info->keys4[i],
927 &tp->md5sig_info->keys4[i+1],
928 (tp->md5sig_info->entries4 - i) *
929 sizeof(struct tcp4_md5sig_key));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800930 }
931 tcp_free_md5sig_pool();
932 return 0;
933 }
934 }
935 return -ENOENT;
936}
937
938EXPORT_SYMBOL(tcp_v4_md5_do_del);
939
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200940static void tcp_v4_clear_md5_list(struct sock *sk)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800941{
942 struct tcp_sock *tp = tcp_sk(sk);
943
944 /* Free each key, then the set of key keys,
945 * the crypto element, and then decrement our
946 * hold on the last resort crypto.
947 */
948 if (tp->md5sig_info->entries4) {
949 int i;
950 for (i = 0; i < tp->md5sig_info->entries4; i++)
David S. Millerf8ab18d2007-09-28 15:18:35 -0700951 kfree(tp->md5sig_info->keys4[i].base.key);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800952 tp->md5sig_info->entries4 = 0;
953 tcp_free_md5sig_pool();
954 }
955 if (tp->md5sig_info->keys4) {
956 kfree(tp->md5sig_info->keys4);
957 tp->md5sig_info->keys4 = NULL;
958 tp->md5sig_info->alloced4 = 0;
959 }
960}
961
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200962static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
963 int optlen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800964{
965 struct tcp_md5sig cmd;
966 struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
967 u8 *newkey;
968
969 if (optlen < sizeof(cmd))
970 return -EINVAL;
971
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200972 if (copy_from_user(&cmd, optval, sizeof(cmd)))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800973 return -EFAULT;
974
975 if (sin->sin_family != AF_INET)
976 return -EINVAL;
977
978 if (!cmd.tcpm_key || !cmd.tcpm_keylen) {
979 if (!tcp_sk(sk)->md5sig_info)
980 return -ENOENT;
981 return tcp_v4_md5_do_del(sk, sin->sin_addr.s_addr);
982 }
983
984 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
985 return -EINVAL;
986
987 if (!tcp_sk(sk)->md5sig_info) {
988 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -0200989 struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800990
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800991 if (!p)
992 return -EINVAL;
993
994 tp->md5sig_info = p;
David S. Miller3d7dbea2007-06-12 14:36:42 -0700995 sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800996 }
997
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -0200998 newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800999 if (!newkey)
1000 return -ENOMEM;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001001 return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
1002 newkey, cmd.tcpm_keylen);
1003}
1004
1005static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
1006 __be32 saddr, __be32 daddr,
1007 struct tcphdr *th, int protocol,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -08001008 unsigned int tcplen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001009{
1010 struct scatterlist sg[4];
1011 __u16 data_len;
1012 int block = 0;
Al Viro8e5200f2006-11-20 18:06:37 -08001013 __sum16 old_checksum;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001014 struct tcp_md5sig_pool *hp;
1015 struct tcp4_pseudohdr *bp;
1016 struct hash_desc *desc;
1017 int err;
1018 unsigned int nbytes = 0;
1019
1020 /*
1021 * Okay, so RFC2385 is turned on for this connection,
1022 * so we need to generate the MD5 hash for the packet now.
1023 */
1024
1025 hp = tcp_get_md5sig_pool();
1026 if (!hp)
1027 goto clear_hash_noput;
1028
1029 bp = &hp->md5_blk.ip4;
1030 desc = &hp->md5_desc;
1031
1032 /*
1033 * 1. the TCP pseudo-header (in the order: source IP address,
1034 * destination IP address, zero-padded protocol number, and
1035 * segment length)
1036 */
1037 bp->saddr = saddr;
1038 bp->daddr = daddr;
1039 bp->pad = 0;
1040 bp->protocol = protocol;
1041 bp->len = htons(tcplen);
David S. Millerc7da57a2007-10-26 00:41:21 -07001042
1043 sg_init_table(sg, 4);
1044
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001045 sg_set_buf(&sg[block++], bp, sizeof(*bp));
1046 nbytes += sizeof(*bp);
1047
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001048 /* 2. the TCP header, excluding options, and assuming a
1049 * checksum of zero/
1050 */
1051 old_checksum = th->check;
1052 th->check = 0;
1053 sg_set_buf(&sg[block++], th, sizeof(struct tcphdr));
1054 nbytes += sizeof(struct tcphdr);
David S. Miller08dd1a52006-11-30 16:35:01 -08001055
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001056 /* 3. the TCP segment data (if any) */
1057 data_len = tcplen - (th->doff << 2);
1058 if (data_len > 0) {
1059 unsigned char *data = (unsigned char *)th + (th->doff << 2);
1060 sg_set_buf(&sg[block++], data, data_len);
1061 nbytes += data_len;
1062 }
1063
1064 /* 4. an independently-specified key or password, known to both
1065 * TCPs and presumably connection-specific
1066 */
1067 sg_set_buf(&sg[block++], key->key, key->keylen);
1068 nbytes += key->keylen;
1069
Jens Axboec46f2332007-10-31 12:06:37 +01001070 sg_mark_end(&sg[block - 1]);
David S. Millerc7da57a2007-10-26 00:41:21 -07001071
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001072 /* Now store the Hash into the packet */
1073 err = crypto_hash_init(desc);
1074 if (err)
1075 goto clear_hash;
1076 err = crypto_hash_update(desc, sg, nbytes);
1077 if (err)
1078 goto clear_hash;
1079 err = crypto_hash_final(desc, md5_hash);
1080 if (err)
1081 goto clear_hash;
1082
1083 /* Reset header, and free up the crypto */
1084 tcp_put_md5sig_pool();
1085 th->check = old_checksum;
1086
1087out:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001088 return 0;
1089clear_hash:
1090 tcp_put_md5sig_pool();
1091clear_hash_noput:
1092 memset(md5_hash, 0, 16);
1093 goto out;
1094}
1095
1096int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
1097 struct sock *sk,
1098 struct dst_entry *dst,
1099 struct request_sock *req,
1100 struct tcphdr *th, int protocol,
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -08001101 unsigned int tcplen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001102{
1103 __be32 saddr, daddr;
1104
1105 if (sk) {
1106 saddr = inet_sk(sk)->saddr;
1107 daddr = inet_sk(sk)->daddr;
1108 } else {
1109 struct rtable *rt = (struct rtable *)dst;
1110 BUG_ON(!rt);
1111 saddr = rt->rt_src;
1112 daddr = rt->rt_dst;
1113 }
1114 return tcp_v4_do_calc_md5_hash(md5_hash, key,
1115 saddr, daddr,
1116 th, protocol, tcplen);
1117}
1118
1119EXPORT_SYMBOL(tcp_v4_calc_md5_hash);
1120
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001121static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001122{
1123 /*
1124 * This gets called for each TCP segment that arrives
1125 * so we want to be efficient.
1126 * We have 3 drop cases:
1127 * o No MD5 hash and one expected.
1128 * o MD5 hash and we're not expecting one.
1129 * o MD5 hash and its wrong.
1130 */
1131 __u8 *hash_location = NULL;
1132 struct tcp_md5sig_key *hash_expected;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001133 const struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001134 struct tcphdr *th = tcp_hdr(skb);
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001135 int length = (th->doff << 2) - sizeof(struct tcphdr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001136 int genhash;
1137 unsigned char *ptr;
1138 unsigned char newhash[16];
1139
1140 hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
1141
1142 /*
1143 * If the TCP option length is less than the TCP_MD5SIG
1144 * option length, then we can shortcut
1145 */
1146 if (length < TCPOLEN_MD5SIG) {
1147 if (hash_expected)
1148 return 1;
1149 else
1150 return 0;
1151 }
1152
1153 /* Okay, we can't shortcut - we have to grub through the options */
1154 ptr = (unsigned char *)(th + 1);
1155 while (length > 0) {
1156 int opcode = *ptr++;
1157 int opsize;
1158
1159 switch (opcode) {
1160 case TCPOPT_EOL:
1161 goto done_opts;
1162 case TCPOPT_NOP:
1163 length--;
1164 continue;
1165 default:
1166 opsize = *ptr++;
1167 if (opsize < 2)
1168 goto done_opts;
1169 if (opsize > length)
1170 goto done_opts;
1171
1172 if (opcode == TCPOPT_MD5SIG) {
1173 hash_location = ptr;
1174 goto done_opts;
1175 }
1176 }
1177 ptr += opsize-2;
1178 length -= opsize;
1179 }
1180done_opts:
1181 /* We've parsed the options - do we have a hash? */
1182 if (!hash_expected && !hash_location)
1183 return 0;
1184
1185 if (hash_expected && !hash_location) {
Leigh Browna9fc00c2006-12-17 17:13:10 -08001186 LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found "
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001187 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001188 NIPQUAD(iph->saddr), ntohs(th->source),
1189 NIPQUAD(iph->daddr), ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001190 return 1;
1191 }
1192
1193 if (!hash_expected && hash_location) {
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001194 LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found "
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001195 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001196 NIPQUAD(iph->saddr), ntohs(th->source),
1197 NIPQUAD(iph->daddr), ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001198 return 1;
1199 }
1200
1201 /* Okay, so this is hash_expected and hash_location -
1202 * so we need to calculate the checksum.
1203 */
1204 genhash = tcp_v4_do_calc_md5_hash(newhash,
1205 hash_expected,
1206 iph->saddr, iph->daddr,
1207 th, sk->sk_protocol,
1208 skb->len);
1209
1210 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
1211 if (net_ratelimit()) {
1212 printk(KERN_INFO "MD5 Hash failed for "
1213 "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n",
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001214 NIPQUAD(iph->saddr), ntohs(th->source),
1215 NIPQUAD(iph->daddr), ntohs(th->dest),
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001216 genhash ? " tcp_v4_calc_md5_hash failed" : "");
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001217 }
1218 return 1;
1219 }
1220 return 0;
1221}
1222
1223#endif
1224
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001225struct request_sock_ops tcp_request_sock_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .family = PF_INET,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001227 .obj_size = sizeof(struct tcp_request_sock),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 .rtx_syn_ack = tcp_v4_send_synack,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001229 .send_ack = tcp_v4_reqsk_send_ack,
1230 .destructor = tcp_v4_reqsk_destructor,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 .send_reset = tcp_v4_send_reset,
1232};
1233
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001234#ifdef CONFIG_TCP_MD5SIG
Andrew Mortonb6332e62006-11-30 19:16:28 -08001235static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001236 .md5_lookup = tcp_v4_reqsk_md5_lookup,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001237};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001238#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001239
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001240static struct timewait_sock_ops tcp_timewait_sock_ops = {
1241 .twsk_obj_size = sizeof(struct tcp_timewait_sock),
1242 .twsk_unique = tcp_twsk_unique,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001243 .twsk_destructor= tcp_twsk_destructor,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001244};
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
1247{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001248 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct tcp_options_received tmp_opt;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001250 struct request_sock *req;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001251 __be32 saddr = ip_hdr(skb)->saddr;
1252 __be32 daddr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 __u32 isn = TCP_SKB_CB(skb)->when;
1254 struct dst_entry *dst = NULL;
1255#ifdef CONFIG_SYN_COOKIES
1256 int want_cookie = 0;
1257#else
1258#define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
1259#endif
1260
1261 /* Never answer to SYNs send to broadcast or multicast */
Eric Dumazetee6b9672008-03-05 18:30:47 -08001262 if (skb->rtable->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 goto drop;
1264
1265 /* TW buckets are converted to open requests without
1266 * limitations, they conserve resources and peer is
1267 * evidently real one.
1268 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001269 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270#ifdef CONFIG_SYN_COOKIES
1271 if (sysctl_tcp_syncookies) {
1272 want_cookie = 1;
1273 } else
1274#endif
1275 goto drop;
1276 }
1277
1278 /* Accept backlog is full. If we have already queued enough
1279 * of warm entries in syn queue, drop request. It is better than
1280 * clogging syn queue with openreqs with exponentially increasing
1281 * timeout.
1282 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001283 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto drop;
1285
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001286 req = reqsk_alloc(&tcp_request_sock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (!req)
1288 goto drop;
1289
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001290#ifdef CONFIG_TCP_MD5SIG
1291 tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
1292#endif
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 tcp_clear_options(&tmp_opt);
1295 tmp_opt.mss_clamp = 536;
1296 tmp_opt.user_mss = tcp_sk(sk)->rx_opt.user_mss;
1297
1298 tcp_parse_options(skb, &tmp_opt, 0);
1299
Florian Westphal4dfc2812008-04-10 03:12:40 -07001300 if (want_cookie && !tmp_opt.saw_tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 tcp_clear_options(&tmp_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
1304 /* Some OSes (unknown ones, but I see them on web server, which
1305 * contains information interesting only for windows'
1306 * users) do not send their stamp in SYN. It is easy case.
1307 * We simply do not advertise TS support.
1308 */
1309 tmp_opt.saw_tstamp = 0;
1310 tmp_opt.tstamp_ok = 0;
1311 }
1312 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1313
1314 tcp_openreq_init(req, &tmp_opt, skb);
1315
Venkat Yekkirala4237c752006-07-24 23:32:50 -07001316 if (security_inet_conn_request(sk, skb, req))
1317 goto drop_and_free;
1318
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001319 ireq = inet_rsk(req);
1320 ireq->loc_addr = daddr;
1321 ireq->rmt_addr = saddr;
1322 ireq->opt = tcp_v4_save_options(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!want_cookie)
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001324 TCP_ECN_create_request(req, tcp_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (want_cookie) {
1327#ifdef CONFIG_SYN_COOKIES
1328 syn_flood_warning(skb);
Florian Westphal4dfc2812008-04-10 03:12:40 -07001329 req->cookie_ts = tmp_opt.tstamp_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#endif
1331 isn = cookie_v4_init_sequence(sk, skb, &req->mss);
1332 } else if (!isn) {
1333 struct inet_peer *peer = NULL;
1334
1335 /* VJ's idea. We save last timestamp seen
1336 * from the destination in peer table, when entering
1337 * state TIME-WAIT, and check against it before
1338 * accepting new connection request.
1339 *
1340 * If "isn" is not zero, this request hit alive
1341 * timewait bucket, so that all the necessary checks
1342 * are made in the function processing timewait state.
1343 */
1344 if (tmp_opt.saw_tstamp &&
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07001345 tcp_death_row.sysctl_tw_recycle &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001346 (dst = inet_csk_route_req(sk, req)) != NULL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
1348 peer->v4daddr == saddr) {
James Morris9d729f72007-03-04 16:12:44 -08001349 if (get_seconds() < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 (s32)(peer->tcp_ts - req->ts_recent) >
1351 TCP_PAWS_WINDOW) {
1352 NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001353 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 }
1356 /* Kill the following clause, if you dislike this way. */
1357 else if (!sysctl_tcp_syncookies &&
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001358 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 (sysctl_max_syn_backlog >> 2)) &&
1360 (!peer || !peer->tcp_ts_stamp) &&
1361 (!dst || !dst_metric(dst, RTAX_RTT))) {
1362 /* Without syncookies last quarter of
1363 * backlog is filled with destinations,
1364 * proven to be alive.
1365 * It means that we continue to communicate
1366 * to destinations, already remembered
1367 * to the moment of synflood.
1368 */
Patrick McHardy64ce2072005-08-09 20:50:53 -07001369 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open "
YOSHIFUJI Hideakia7d632b2008-04-14 04:09:00 -07001370 "request from " NIPQUAD_FMT "/%u\n",
Patrick McHardy64ce2072005-08-09 20:50:53 -07001371 NIPQUAD(saddr),
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001372 ntohs(tcp_hdr(skb)->source));
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001373 goto drop_and_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375
Gerrit Renkera94f7232006-11-10 14:06:49 -08001376 isn = tcp_v4_init_sequence(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001378 tcp_rsk(req)->snt_isn = isn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001380 if (__tcp_v4_send_synack(sk, req, dst) || want_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 goto drop_and_free;
1382
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001383 inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return 0;
1385
Denis V. Lunev7cd04fa2008-03-03 11:59:32 -08001386drop_and_release:
1387 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388drop_and_free:
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001389 reqsk_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return 0;
1392}
1393
1394
1395/*
1396 * The three way handshake has completed - we got a valid synack -
1397 * now create the new socket.
1398 */
1399struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001400 struct request_sock *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 struct dst_entry *dst)
1402{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001403 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct inet_sock *newinet;
1405 struct tcp_sock *newtp;
1406 struct sock *newsk;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001407#ifdef CONFIG_TCP_MD5SIG
1408 struct tcp_md5sig_key *key;
1409#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (sk_acceptq_is_full(sk))
1412 goto exit_overflow;
1413
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001414 if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 goto exit;
1416
1417 newsk = tcp_create_openreq_child(sk, req, skb);
1418 if (!newsk)
1419 goto exit;
1420
Herbert Xubcd76112006-06-30 13:36:35 -07001421 newsk->sk_gso_type = SKB_GSO_TCPV4;
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -07001422 sk_setup_caps(newsk, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 newtp = tcp_sk(newsk);
1425 newinet = inet_sk(newsk);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001426 ireq = inet_rsk(req);
1427 newinet->daddr = ireq->rmt_addr;
1428 newinet->rcv_saddr = ireq->loc_addr;
1429 newinet->saddr = ireq->loc_addr;
1430 newinet->opt = ireq->opt;
1431 ireq->opt = NULL;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001432 newinet->mc_index = inet_iif(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001433 newinet->mc_ttl = ip_hdr(skb)->ttl;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001434 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (newinet->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001436 inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 newinet->id = newtp->write_seq ^ jiffies;
1438
John Heffner5d424d52006-03-20 17:53:41 -08001439 tcp_mtup_init(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 tcp_sync_mss(newsk, dst_mtu(dst));
1441 newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
1442 tcp_initialize_rcv_mss(newsk);
1443
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001444#ifdef CONFIG_TCP_MD5SIG
1445 /* Copy over the MD5 key from the original socket */
1446 if ((key = tcp_v4_md5_do_lookup(sk, newinet->daddr)) != NULL) {
1447 /*
1448 * We're using one, so create a matching key
1449 * on the newsk structure. If we fail to get
1450 * memory, then we end up not copying the key
1451 * across. Shucks.
1452 */
Arnaldo Carvalho de Melof6685932006-11-17 11:06:01 -02001453 char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1454 if (newkey != NULL)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001455 tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr,
1456 newkey, key->keylen);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001457 }
1458#endif
1459
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001460 __inet_hash_nolisten(newsk);
1461 __inet_inherit_port(sk, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 return newsk;
1464
1465exit_overflow:
1466 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
1467exit:
1468 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
1469 dst_release(dst);
1470 return NULL;
1471}
1472
1473static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
1474{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001475 struct tcphdr *th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001476 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 struct sock *nsk;
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001478 struct request_sock **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /* Find possible connection requests. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001480 struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
1481 iph->saddr, iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (req)
1483 return tcp_check_req(sk, skb, req, prev);
1484
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001485 nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001486 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 if (nsk) {
1489 if (nsk->sk_state != TCP_TIME_WAIT) {
1490 bh_lock_sock(nsk);
1491 return nsk;
1492 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001493 inet_twsk_put(inet_twsk(nsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return NULL;
1495 }
1496
1497#ifdef CONFIG_SYN_COOKIES
1498 if (!th->rst && !th->syn && th->ack)
1499 sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
1500#endif
1501 return sk;
1502}
1503
Al Virob51655b2006-11-14 21:40:42 -08001504static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001506 const struct iphdr *iph = ip_hdr(skb);
1507
Patrick McHardy84fa7932006-08-29 16:44:56 -07001508 if (skb->ip_summed == CHECKSUM_COMPLETE) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001509 if (!tcp_v4_check(skb->len, iph->saddr,
1510 iph->daddr, skb->csum)) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001511 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 0;
Herbert Xufb286bb2005-11-10 13:01:24 -08001513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001515
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001516 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
Herbert Xufb286bb2005-11-10 13:01:24 -08001517 skb->len, IPPROTO_TCP, 0);
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (skb->len <= 76) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001520 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522 return 0;
1523}
1524
1525
1526/* The socket must have it's spinlock held when we get
1527 * here.
1528 *
1529 * We have a potential double-lock case here, so even when
1530 * doing backlog processing we use the BH locking scheme.
1531 * This is because we cannot sleep with the original spinlock
1532 * held.
1533 */
1534int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
1535{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001536 struct sock *rsk;
1537#ifdef CONFIG_TCP_MD5SIG
1538 /*
1539 * We really want to reject the packet as early as possible
1540 * if:
1541 * o We're expecting an MD5'd packet and this is no MD5 tcp option
1542 * o There is an MD5 option and we're not expecting one
1543 */
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02001544 if (tcp_v4_inbound_md5_hash(sk, skb))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001545 goto discard;
1546#endif
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1549 TCP_CHECK_TIMER(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001550 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001551 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 TCP_CHECK_TIMER(sk);
1555 return 0;
1556 }
1557
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001558 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 goto csum_err;
1560
1561 if (sk->sk_state == TCP_LISTEN) {
1562 struct sock *nsk = tcp_v4_hnd_req(sk, skb);
1563 if (!nsk)
1564 goto discard;
1565
1566 if (nsk != sk) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001567 if (tcp_child_process(sk, nsk, skb)) {
1568 rsk = nsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572 }
1573 }
1574
1575 TCP_CHECK_TIMER(sk);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001576 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001577 rsk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto reset;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 TCP_CHECK_TIMER(sk);
1581 return 0;
1582
1583reset:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001584 tcp_v4_send_reset(rsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585discard:
1586 kfree_skb(skb);
1587 /* Be careful here. If this function gets more complicated and
1588 * gcc suffers from register pressure on the x86, sk (in %ebx)
1589 * might be destroyed here. This current version compiles correctly,
1590 * but you have been warned.
1591 */
1592 return 0;
1593
1594csum_err:
1595 TCP_INC_STATS_BH(TCP_MIB_INERRS);
1596 goto discard;
1597}
1598
1599/*
1600 * From tcp_input.c
1601 */
1602
1603int tcp_v4_rcv(struct sk_buff *skb)
1604{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001605 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct tcphdr *th;
1607 struct sock *sk;
1608 int ret;
1609
1610 if (skb->pkt_type != PACKET_HOST)
1611 goto discard_it;
1612
1613 /* Count it even if it's bad */
1614 TCP_INC_STATS_BH(TCP_MIB_INSEGS);
1615
1616 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1617 goto discard_it;
1618
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001619 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 if (th->doff < sizeof(struct tcphdr) / 4)
1622 goto bad_packet;
1623 if (!pskb_may_pull(skb, th->doff * 4))
1624 goto discard_it;
1625
1626 /* An explanation is required here, I think.
1627 * Packet length and doff are validated by header prediction,
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001628 * provided case of th->doff==0 is eliminated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 * So, we defer the checks. */
Herbert Xu60476372007-04-09 11:59:39 -07001630 if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto bad_packet;
1632
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001633 th = tcp_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001634 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1636 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1637 skb->len - th->doff * 4);
1638 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1639 TCP_SKB_CB(skb)->when = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001640 TCP_SKB_CB(skb)->flags = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 TCP_SKB_CB(skb)->sacked = 0;
1642
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001643 sk = __inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->saddr,
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001644 th->source, iph->daddr, th->dest, inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (!sk)
1646 goto no_tcp_socket;
1647
1648process:
1649 if (sk->sk_state == TCP_TIME_WAIT)
1650 goto do_time_wait;
1651
1652 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1653 goto discard_and_relse;
Patrick McHardyb59c2702006-01-06 23:06:10 -08001654 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001656 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto discard_and_relse;
1658
1659 skb->dev = NULL;
1660
Ingo Molnarc6366182006-07-03 00:25:13 -07001661 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 ret = 0;
1663 if (!sock_owned_by_user(sk)) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001664#ifdef CONFIG_NET_DMA
1665 struct tcp_sock *tp = tcp_sk(sk);
1666 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1667 tp->ucopy.dma_chan = get_softnet_dma();
1668 if (tp->ucopy.dma_chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 ret = tcp_v4_do_rcv(sk, skb);
Chris Leech1a2449a2006-05-23 18:05:53 -07001670 else
1671#endif
1672 {
1673 if (!tcp_prequeue(sk, skb))
1674 ret = tcp_v4_do_rcv(sk, skb);
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 } else
1677 sk_add_backlog(sk, skb);
1678 bh_unlock_sock(sk);
1679
1680 sock_put(sk);
1681
1682 return ret;
1683
1684no_tcp_socket:
1685 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1686 goto discard_it;
1687
1688 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1689bad_packet:
1690 TCP_INC_STATS_BH(TCP_MIB_INERRS);
1691 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001692 tcp_v4_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694
1695discard_it:
1696 /* Discard frame. */
1697 kfree_skb(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700discard_and_relse:
1701 sock_put(sk);
1702 goto discard_it;
1703
1704do_time_wait:
1705 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001706 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 goto discard_it;
1708 }
1709
1710 if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
1711 TCP_INC_STATS_BH(TCP_MIB_INERRS);
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001712 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 goto discard_it;
1714 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001715 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 case TCP_TW_SYN: {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001717 struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
Pavel Emelyanovc67499c2008-01-31 05:06:40 -08001718 &tcp_hashinfo,
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001719 iph->daddr, th->dest,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001720 inet_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (sk2) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001722 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
1723 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 sk = sk2;
1725 goto process;
1726 }
1727 /* Fall through to ACK */
1728 }
1729 case TCP_TW_ACK:
1730 tcp_v4_timewait_ack(sk, skb);
1731 break;
1732 case TCP_TW_RST:
1733 goto no_tcp_socket;
1734 case TCP_TW_SUCCESS:;
1735 }
1736 goto discard_it;
1737}
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739/* VJ's idea. Save last timestamp seen from this destination
1740 * and hold it at least for normal timewait interval to use for duplicate
1741 * segment detection in subsequent connections, before they enter synchronized
1742 * state.
1743 */
1744
1745int tcp_v4_remember_stamp(struct sock *sk)
1746{
1747 struct inet_sock *inet = inet_sk(sk);
1748 struct tcp_sock *tp = tcp_sk(sk);
1749 struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
1750 struct inet_peer *peer = NULL;
1751 int release_it = 0;
1752
1753 if (!rt || rt->rt_dst != inet->daddr) {
1754 peer = inet_getpeer(inet->daddr, 1);
1755 release_it = 1;
1756 } else {
1757 if (!rt->peer)
1758 rt_bind_peer(rt, 1);
1759 peer = rt->peer;
1760 }
1761
1762 if (peer) {
1763 if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
James Morris9d729f72007-03-04 16:12:44 -08001764 (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
1766 peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
1767 peer->tcp_ts = tp->rx_opt.ts_recent;
1768 }
1769 if (release_it)
1770 inet_putpeer(peer);
1771 return 1;
1772 }
1773
1774 return 0;
1775}
1776
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001777int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001779 struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 if (peer) {
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001782 const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
1783
1784 if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
James Morris9d729f72007-03-04 16:12:44 -08001785 (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001786 peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) {
1787 peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp;
1788 peer->tcp_ts = tcptw->tw_ts_recent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 inet_putpeer(peer);
1791 return 1;
1792 }
1793
1794 return 0;
1795}
1796
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001797struct inet_connection_sock_af_ops ipv4_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001798 .queue_xmit = ip_queue_xmit,
1799 .send_check = tcp_v4_send_check,
1800 .rebuild_header = inet_sk_rebuild_header,
1801 .conn_request = tcp_v4_conn_request,
1802 .syn_recv_sock = tcp_v4_syn_recv_sock,
1803 .remember_stamp = tcp_v4_remember_stamp,
1804 .net_header_len = sizeof(struct iphdr),
1805 .setsockopt = ip_setsockopt,
1806 .getsockopt = ip_getsockopt,
1807 .addr2sockaddr = inet_csk_addr2sockaddr,
1808 .sockaddr_len = sizeof(struct sockaddr_in),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001809 .bind_conflict = inet_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001810#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001811 .compat_setsockopt = compat_ip_setsockopt,
1812 .compat_getsockopt = compat_ip_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001813#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814};
1815
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001816#ifdef CONFIG_TCP_MD5SIG
Andrew Mortonb6332e62006-11-30 19:16:28 -08001817static struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001818 .md5_lookup = tcp_v4_md5_lookup,
1819 .calc_md5_hash = tcp_v4_calc_md5_hash,
1820 .md5_add = tcp_v4_md5_add_func,
1821 .md5_parse = tcp_v4_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001822};
Andrew Mortonb6332e62006-11-30 19:16:28 -08001823#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825/* NOTE: A lot of things set to zero explicitly by call to
1826 * sk_alloc() so need not be done here.
1827 */
1828static int tcp_v4_init_sock(struct sock *sk)
1829{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001830 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct tcp_sock *tp = tcp_sk(sk);
1832
1833 skb_queue_head_init(&tp->out_of_order_queue);
1834 tcp_init_xmit_timers(sk);
1835 tcp_prequeue_init(tp);
1836
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001837 icsk->icsk_rto = TCP_TIMEOUT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 tp->mdev = TCP_TIMEOUT_INIT;
1839
1840 /* So many TCP implementations out there (incorrectly) count the
1841 * initial SYN frame in their delayed-ACK and congestion control
1842 * algorithms that we must have the following bandaid to talk
1843 * efficiently to them. -DaveM
1844 */
1845 tp->snd_cwnd = 2;
1846
1847 /* See draft-stevens-tcpca-spec-01 for discussion of the
1848 * initialization of these values.
1849 */
1850 tp->snd_ssthresh = 0x7fffffff; /* Infinity */
1851 tp->snd_cwnd_clamp = ~0;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001852 tp->mss_cache = 536;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 tp->reordering = sysctl_tcp_reordering;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001855 icsk->icsk_ca_ops = &tcp_init_congestion_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 sk->sk_state = TCP_CLOSE;
1858
1859 sk->sk_write_space = sk_stream_write_space;
1860 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1861
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001862 icsk->icsk_af_ops = &ipv4_specific;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001863 icsk->icsk_sync_mss = tcp_sync_mss;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001864#ifdef CONFIG_TCP_MD5SIG
1865 tp->af_specific = &tcp_sock_ipv4_specific;
1866#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 sk->sk_sndbuf = sysctl_tcp_wmem[1];
1869 sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1870
1871 atomic_inc(&tcp_sockets_allocated);
1872
1873 return 0;
1874}
1875
1876int tcp_v4_destroy_sock(struct sock *sk)
1877{
1878 struct tcp_sock *tp = tcp_sk(sk);
1879
1880 tcp_clear_xmit_timers(sk);
1881
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001882 tcp_cleanup_congestion_control(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* Cleanup up the write buffer. */
David S. Millerfe067e82007-03-07 12:12:44 -08001885 tcp_write_queue_purge(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /* Cleans up our, hopefully empty, out_of_order_queue. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001888 __skb_queue_purge(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001890#ifdef CONFIG_TCP_MD5SIG
1891 /* Clean up the MD5 key list, if any */
1892 if (tp->md5sig_info) {
1893 tcp_v4_clear_md5_list(sk);
1894 kfree(tp->md5sig_info);
1895 tp->md5sig_info = NULL;
1896 }
1897#endif
1898
Chris Leech1a2449a2006-05-23 18:05:53 -07001899#ifdef CONFIG_NET_DMA
1900 /* Cleans up our sk_async_wait_queue */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001901 __skb_queue_purge(&sk->sk_async_wait_queue);
Chris Leech1a2449a2006-05-23 18:05:53 -07001902#endif
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 /* Clean prequeue, it must be empty really */
1905 __skb_queue_purge(&tp->ucopy.prequeue);
1906
1907 /* Clean up a referenced TCP bind bucket. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001908 if (inet_csk(sk)->icsk_bind_hash)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001909 inet_put_port(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /*
1912 * If sendmsg cached page exists, toss it.
1913 */
1914 if (sk->sk_sndmsg_page) {
1915 __free_page(sk->sk_sndmsg_page);
1916 sk->sk_sndmsg_page = NULL;
1917 }
1918
Patrick McManusec3c0982008-03-21 16:33:01 -07001919 if (tp->defer_tcp_accept.request) {
1920 reqsk_free(tp->defer_tcp_accept.request);
1921 sock_put(tp->defer_tcp_accept.listen_sk);
1922 sock_put(sk);
1923 tp->defer_tcp_accept.listen_sk = NULL;
1924 tp->defer_tcp_accept.request = NULL;
1925 }
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 atomic_dec(&tcp_sockets_allocated);
1928
1929 return 0;
1930}
1931
1932EXPORT_SYMBOL(tcp_v4_destroy_sock);
1933
1934#ifdef CONFIG_PROC_FS
1935/* Proc filesystem TCP sock list dumping. */
1936
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001937static inline struct inet_timewait_sock *tw_head(struct hlist_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
1939 return hlist_empty(head) ? NULL :
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001940 list_entry(head->first, struct inet_timewait_sock, tw_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001943static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 return tw->tw_node.next ?
1946 hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
1947}
1948
1949static void *listening_get_next(struct seq_file *seq, void *cur)
1950{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001951 struct inet_connection_sock *icsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct hlist_node *node;
1953 struct sock *sk = cur;
1954 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07001955 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 if (!sk) {
1958 st->bucket = 0;
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07001959 sk = sk_head(&tcp_hashinfo.listening_hash[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto get_sk;
1961 }
1962
1963 ++st->num;
1964
1965 if (st->state == TCP_SEQ_STATE_OPENREQ) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001966 struct request_sock *req = cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001968 icsk = inet_csk(st->syn_wait_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 req = req->dl_next;
1970 while (1) {
1971 while (req) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07001972 if (req->rsk_ops->family == st->family &&
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001973 net_eq(sock_net(req->sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 cur = req;
1975 goto out;
1976 }
1977 req = req->dl_next;
1978 }
Eric Dumazet72a3eff2006-11-16 02:30:37 -08001979 if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
1981get_req:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001982 req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 sk = sk_next(st->syn_wait_sk);
1985 st->state = TCP_SEQ_STATE_LISTENING;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001986 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 } else {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001988 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001989 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1990 if (reqsk_queue_len(&icsk->icsk_accept_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 goto start_req;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001992 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 sk = sk_next(sk);
1994 }
1995get_sk:
1996 sk_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001997 if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 cur = sk;
1999 goto out;
2000 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002001 icsk = inet_csk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002002 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
2003 if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004start_req:
2005 st->uid = sock_i_uid(sk);
2006 st->syn_wait_sk = sk;
2007 st->state = TCP_SEQ_STATE_OPENREQ;
2008 st->sbucket = 0;
2009 goto get_req;
2010 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002011 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -07002013 if (++st->bucket < INET_LHTABLE_SIZE) {
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002014 sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 goto get_sk;
2016 }
2017 cur = NULL;
2018out:
2019 return cur;
2020}
2021
2022static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
2023{
2024 void *rc = listening_get_next(seq, NULL);
2025
2026 while (rc && *pos) {
2027 rc = listening_get_next(seq, rc);
2028 --*pos;
2029 }
2030 return rc;
2031}
2032
2033static void *established_get_first(struct seq_file *seq)
2034{
2035 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07002036 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 void *rc = NULL;
2038
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002039 for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct sock *sk;
2041 struct hlist_node *node;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002042 struct inet_timewait_sock *tw;
Eric Dumazet230140c2007-11-07 02:40:20 -08002043 rwlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Eric Dumazet230140c2007-11-07 02:40:20 -08002045 read_lock_bh(lock);
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002046 sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002047 if (sk->sk_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002048 !net_eq(sock_net(sk), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 continue;
2050 }
2051 rc = sk;
2052 goto out;
2053 }
2054 st->state = TCP_SEQ_STATE_TIME_WAIT;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002055 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002056 &tcp_hashinfo.ehash[st->bucket].twchain) {
Pavel Emelyanov28518fc2008-03-21 15:52:00 -07002057 if (tw->tw_family != st->family ||
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002058 !net_eq(twsk_net(tw), net)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 continue;
2060 }
2061 rc = tw;
2062 goto out;
2063 }
Eric Dumazet230140c2007-11-07 02:40:20 -08002064 read_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 st->state = TCP_SEQ_STATE_ESTABLISHED;
2066 }
2067out:
2068 return rc;
2069}
2070
2071static void *established_get_next(struct seq_file *seq, void *cur)
2072{
2073 struct sock *sk = cur;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07002074 struct inet_timewait_sock *tw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct hlist_node *node;
2076 struct tcp_iter_state* st = seq->private;
Denis V. Luneva4146b12008-04-13 22:11:14 -07002077 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 ++st->num;
2080
2081 if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
2082 tw = cur;
2083 tw = tw_next(tw);
2084get_tw:
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002085 while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 tw = tw_next(tw);
2087 }
2088 if (tw) {
2089 cur = tw;
2090 goto out;
2091 }
Eric Dumazet230140c2007-11-07 02:40:20 -08002092 read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 st->state = TCP_SEQ_STATE_ESTABLISHED;
2094
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002095 if (++st->bucket < tcp_hashinfo.ehash_size) {
Eric Dumazet230140c2007-11-07 02:40:20 -08002096 read_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -07002097 sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 } else {
2099 cur = NULL;
2100 goto out;
2101 }
2102 } else
2103 sk = sk_next(sk);
2104
2105 sk_for_each_from(sk, node) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002106 if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto found;
2108 }
2109
2110 st->state = TCP_SEQ_STATE_TIME_WAIT;
Eric Dumazetdbca9b2752007-02-08 14:16:46 -08002111 tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 goto get_tw;
2113found:
2114 cur = sk;
2115out:
2116 return cur;
2117}
2118
2119static void *established_get_idx(struct seq_file *seq, loff_t pos)
2120{
2121 void *rc = established_get_first(seq);
2122
2123 while (rc && pos) {
2124 rc = established_get_next(seq, rc);
2125 --pos;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return rc;
2128}
2129
2130static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
2131{
2132 void *rc;
2133 struct tcp_iter_state* st = seq->private;
2134
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002135 inet_listen_lock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 st->state = TCP_SEQ_STATE_LISTENING;
2137 rc = listening_get_idx(seq, &pos);
2138
2139 if (!rc) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002140 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 st->state = TCP_SEQ_STATE_ESTABLISHED;
2142 rc = established_get_idx(seq, pos);
2143 }
2144
2145 return rc;
2146}
2147
2148static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
2149{
2150 struct tcp_iter_state* st = seq->private;
2151 st->state = TCP_SEQ_STATE_LISTENING;
2152 st->num = 0;
2153 return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2154}
2155
2156static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2157{
2158 void *rc = NULL;
2159 struct tcp_iter_state* st;
2160
2161 if (v == SEQ_START_TOKEN) {
2162 rc = tcp_get_idx(seq, 0);
2163 goto out;
2164 }
2165 st = seq->private;
2166
2167 switch (st->state) {
2168 case TCP_SEQ_STATE_OPENREQ:
2169 case TCP_SEQ_STATE_LISTENING:
2170 rc = listening_get_next(seq, v);
2171 if (!rc) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002172 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 st->state = TCP_SEQ_STATE_ESTABLISHED;
2174 rc = established_get_first(seq);
2175 }
2176 break;
2177 case TCP_SEQ_STATE_ESTABLISHED:
2178 case TCP_SEQ_STATE_TIME_WAIT:
2179 rc = established_get_next(seq, v);
2180 break;
2181 }
2182out:
2183 ++*pos;
2184 return rc;
2185}
2186
2187static void tcp_seq_stop(struct seq_file *seq, void *v)
2188{
2189 struct tcp_iter_state* st = seq->private;
2190
2191 switch (st->state) {
2192 case TCP_SEQ_STATE_OPENREQ:
2193 if (v) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002194 struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
2195 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197 case TCP_SEQ_STATE_LISTENING:
2198 if (v != SEQ_START_TOKEN)
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -07002199 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 break;
2201 case TCP_SEQ_STATE_TIME_WAIT:
2202 case TCP_SEQ_STATE_ESTABLISHED:
2203 if (v)
Eric Dumazet230140c2007-11-07 02:40:20 -08002204 read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 break;
2206 }
2207}
2208
2209static int tcp_seq_open(struct inode *inode, struct file *file)
2210{
2211 struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct tcp_iter_state *s;
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002213 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002215 err = seq_open_net(inode, file, &afinfo->seq_ops,
2216 sizeof(struct tcp_iter_state));
2217 if (err < 0)
2218 return err;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002219
Denis V. Lunev52d6f3f2008-04-13 22:12:41 -07002220 s = ((struct seq_file *)file->private_data)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 s->family = afinfo->family;
Daniel Lezcanof40c8172008-03-21 04:13:54 -07002222 return 0;
2223}
2224
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002225int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
2227 int rc = 0;
2228 struct proc_dir_entry *p;
2229
Denis V. Lunev68fcadd2008-04-13 22:13:30 -07002230 afinfo->seq_fops.open = tcp_seq_open;
2231 afinfo->seq_fops.read = seq_read;
2232 afinfo->seq_fops.llseek = seq_lseek;
2233 afinfo->seq_fops.release = seq_release_net;
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002234
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002235 afinfo->seq_ops.start = tcp_seq_start;
2236 afinfo->seq_ops.next = tcp_seq_next;
2237 afinfo->seq_ops.stop = tcp_seq_stop;
2238
Denis V. Lunev84841c32008-05-02 04:10:08 -07002239 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
2240 &afinfo->seq_fops, afinfo);
2241 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 rc = -ENOMEM;
2243 return rc;
2244}
2245
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002246void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07002248 proc_net_remove(net, afinfo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002251static void get_openreq4(struct sock *sk, struct request_sock *req,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002252 struct seq_file *f, int i, int uid, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002254 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int ttd = req->expires - jiffies;
2256
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002257 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2258 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 i,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002260 ireq->loc_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 ntohs(inet_sk(sk)->sport),
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002262 ireq->rmt_addr,
2263 ntohs(ireq->rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 TCP_SYN_RECV,
2265 0, 0, /* could print option size, but that is af dependent. */
2266 1, /* timers active (only the expire timer) */
2267 jiffies_to_clock_t(ttd),
2268 req->retrans,
2269 uid,
2270 0, /* non standard timer */
2271 0, /* open_requests have no inode */
2272 atomic_read(&sk->sk_refcnt),
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002273 req,
2274 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002277static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 int timer_active;
2280 unsigned long timer_expires;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002281 struct tcp_sock *tp = tcp_sk(sk);
2282 const struct inet_connection_sock *icsk = inet_csk(sk);
2283 struct inet_sock *inet = inet_sk(sk);
Al Viro714e85b2006-11-14 20:51:49 -08002284 __be32 dest = inet->daddr;
2285 __be32 src = inet->rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 __u16 destp = ntohs(inet->dport);
2287 __u16 srcp = ntohs(inet->sport);
2288
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002289 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002291 timer_expires = icsk->icsk_timeout;
2292 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002294 timer_expires = icsk->icsk_timeout;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002295 } else if (timer_pending(&sk->sk_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 timer_active = 2;
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002297 timer_expires = sk->sk_timer.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 } else {
2299 timer_active = 0;
2300 timer_expires = jiffies;
2301 }
2302
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002303 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
2304 "%08X %5d %8d %lu %d %p %u %u %u %u %d%n",
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002305 i, src, srcp, dest, destp, sk->sk_state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07002306 tp->write_seq - tp->snd_una,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002307 sk->sk_state == TCP_LISTEN ? sk->sk_ack_backlog :
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002308 (tp->rcv_nxt - tp->copied_seq),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 timer_active,
2310 jiffies_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002311 icsk->icsk_retransmits,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002312 sock_i_uid(sk),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002313 icsk->icsk_probes_out,
Ilpo Järvinencf4c6bf2007-02-22 01:13:58 -08002314 sock_i_ino(sk),
2315 atomic_read(&sk->sk_refcnt), sk,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002316 icsk->icsk_rto,
2317 icsk->icsk_ack.ato,
2318 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 tp->snd_cwnd,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002320 tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh,
2321 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Arnaldo Carvalho de Melo71742592006-11-17 10:57:30 -02002324static void get_timewait4_sock(struct inet_timewait_sock *tw,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002325 struct seq_file *f, int i, int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Al Viro23f33c22006-09-27 18:43:50 -07002327 __be32 dest, src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 __u16 destp, srcp;
2329 int ttd = tw->tw_ttd - jiffies;
2330
2331 if (ttd < 0)
2332 ttd = 0;
2333
2334 dest = tw->tw_daddr;
2335 src = tw->tw_rcv_saddr;
2336 destp = ntohs(tw->tw_dport);
2337 srcp = ntohs(tw->tw_sport);
2338
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002339 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2340 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
2342 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002343 atomic_read(&tw->tw_refcnt), tw, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
2346#define TMPSZ 150
2347
2348static int tcp4_seq_show(struct seq_file *seq, void *v)
2349{
2350 struct tcp_iter_state* st;
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002351 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 if (v == SEQ_START_TOKEN) {
2354 seq_printf(seq, "%-*s\n", TMPSZ - 1,
2355 " sl local_address rem_address st tx_queue "
2356 "rx_queue tr tm->when retrnsmt uid timeout "
2357 "inode");
2358 goto out;
2359 }
2360 st = seq->private;
2361
2362 switch (st->state) {
2363 case TCP_SEQ_STATE_LISTENING:
2364 case TCP_SEQ_STATE_ESTABLISHED:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002365 get_tcp4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 break;
2367 case TCP_SEQ_STATE_OPENREQ:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002368 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 break;
2370 case TCP_SEQ_STATE_TIME_WAIT:
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002371 get_timewait4_sock(v, seq, st->num, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 break;
2373 }
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07002374 seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375out:
2376 return 0;
2377}
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379static struct tcp_seq_afinfo tcp4_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 .name = "tcp",
2381 .family = AF_INET,
Denis V. Lunev5f4472c2008-04-13 22:13:53 -07002382 .seq_fops = {
2383 .owner = THIS_MODULE,
2384 },
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07002385 .seq_ops = {
2386 .show = tcp4_seq_show,
2387 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388};
2389
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002390static int tcp4_proc_init_net(struct net *net)
2391{
2392 return tcp_proc_register(net, &tcp4_seq_afinfo);
2393}
2394
2395static void tcp4_proc_exit_net(struct net *net)
2396{
2397 tcp_proc_unregister(net, &tcp4_seq_afinfo);
2398}
2399
2400static struct pernet_operations tcp4_net_ops = {
2401 .init = tcp4_proc_init_net,
2402 .exit = tcp4_proc_exit_net,
2403};
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405int __init tcp4_proc_init(void)
2406{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002407 return register_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410void tcp4_proc_exit(void)
2411{
Pavel Emelyanov757764f2008-03-24 14:56:02 -07002412 unregister_pernet_subsys(&tcp4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414#endif /* CONFIG_PROC_FS */
2415
2416struct proto tcp_prot = {
2417 .name = "TCP",
2418 .owner = THIS_MODULE,
2419 .close = tcp_close,
2420 .connect = tcp_v4_connect,
2421 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002422 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 .ioctl = tcp_ioctl,
2424 .init = tcp_v4_init_sock,
2425 .destroy = tcp_v4_destroy_sock,
2426 .shutdown = tcp_shutdown,
2427 .setsockopt = tcp_setsockopt,
2428 .getsockopt = tcp_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 .recvmsg = tcp_recvmsg,
2430 .backlog_rcv = tcp_v4_do_rcv,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08002431 .hash = inet_hash,
2432 .unhash = inet_unhash,
2433 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 .enter_memory_pressure = tcp_enter_memory_pressure,
2435 .sockets_allocated = &tcp_sockets_allocated,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07002436 .orphan_count = &tcp_orphan_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 .memory_allocated = &tcp_memory_allocated,
2438 .memory_pressure = &tcp_memory_pressure,
2439 .sysctl_mem = sysctl_tcp_mem,
2440 .sysctl_wmem = sysctl_tcp_wmem,
2441 .sysctl_rmem = sysctl_tcp_rmem,
2442 .max_header = MAX_TCP_HEADER,
2443 .obj_size = sizeof(struct tcp_sock),
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08002444 .twsk_prot = &tcp_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07002445 .rsk_prot = &tcp_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07002446 .h.hashinfo = &tcp_hashinfo,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08002447#ifdef CONFIG_COMPAT
2448 .compat_setsockopt = compat_tcp_setsockopt,
2449 .compat_getsockopt = compat_tcp_getsockopt,
2450#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451};
2452
Denis V. Lunev046ee902008-04-03 14:31:33 -07002453
2454static int __net_init tcp_sk_init(struct net *net)
2455{
2456 return inet_ctl_sock_create(&net->ipv4.tcp_sock,
2457 PF_INET, SOCK_RAW, IPPROTO_TCP, net);
2458}
2459
2460static void __net_exit tcp_sk_exit(struct net *net)
2461{
2462 inet_ctl_sock_destroy(net->ipv4.tcp_sock);
2463}
2464
2465static struct pernet_operations __net_initdata tcp_sk_ops = {
2466 .init = tcp_sk_init,
2467 .exit = tcp_sk_exit,
2468};
2469
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08002470void __init tcp_v4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
Denis V. Lunev046ee902008-04-03 14:31:33 -07002472 if (register_pernet_device(&tcp_sk_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 panic("Failed to create the TCP control socket.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
2476EXPORT_SYMBOL(ipv4_specific);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477EXPORT_SYMBOL(tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478EXPORT_SYMBOL(tcp_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479EXPORT_SYMBOL(tcp_v4_conn_request);
2480EXPORT_SYMBOL(tcp_v4_connect);
2481EXPORT_SYMBOL(tcp_v4_do_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482EXPORT_SYMBOL(tcp_v4_remember_stamp);
2483EXPORT_SYMBOL(tcp_v4_send_check);
2484EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
2485
2486#ifdef CONFIG_PROC_FS
2487EXPORT_SYMBOL(tcp_proc_register);
2488EXPORT_SYMBOL(tcp_proc_unregister);
2489#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490EXPORT_SYMBOL(sysctl_tcp_low_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491