tcp_ipv4.c 59.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		Implementation of the Transmission Control Protocol(TCP).
 *
 *		IPv4 specific functions
 *
 *
 *		code split from:
 *		linux/ipv4/tcp.c
 *		linux/ipv4/tcp_input.c
 *		linux/ipv4/tcp_output.c
 *
 *		See tcp.c for author information
 *
 *	This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

/*
 * Changes:
 *		David S. Miller	:	New socket lookup architecture.
 *					This code is dedicated to John Dyson.
 *		David S. Miller :	Change semantics of established hash,
 *					half is devoted to TIME_WAIT sockets
 *					and the rest go in the other half.
 *		Andi Kleen :		Add support for syncookies and fixed
 *					some bugs: ip options weren't passed to
 *					the TCP layer, missed a check for an
 *					ACK bit.
 *		Andi Kleen :		Implemented fast path mtu discovery.
 *	     				Fixed many serious bugs in the
37
 *					request_sock handling and moved
Linus Torvalds's avatar
Linus Torvalds committed
38 39
 *					most of it into the af independent code.
 *					Added tail drop and some other bugfixes.
Stephen Hemminger's avatar
Stephen Hemminger committed
40
 *					Added new listen semantics.
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 *		Mike McLagan	:	Routing by source
 *	Juan Jose Ciarlante:		ip_dynaddr bits
 *		Andi Kleen:		various fixes.
 *	Vitaly E. Lavrov	:	Transparent proxy revived after year
 *					coma.
 *	Andi Kleen		:	Fix new listen.
 *	Andi Kleen		:	Fix accept error reporting.
 *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
 *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
 *					a single port at the same time.
 */


#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/module.h>
#include <linux/random.h>
#include <linux/cache.h>
#include <linux/jhash.h>
#include <linux/init.h>
#include <linux/times.h>

63
#include <net/net_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
64
#include <net/icmp.h>
65
#include <net/inet_hashtables.h>
Linus Torvalds's avatar
Linus Torvalds committed
66
#include <net/tcp.h>
67
#include <net/transp_v6.h>
Linus Torvalds's avatar
Linus Torvalds committed
68 69
#include <net/ipv6.h>
#include <net/inet_common.h>
70
#include <net/timewait_sock.h>
Linus Torvalds's avatar
Linus Torvalds committed
71
#include <net/xfrm.h>
72
#include <net/netdma.h>
Linus Torvalds's avatar
Linus Torvalds committed
73 74 75 76 77 78 79

#include <linux/inet.h>
#include <linux/ipv6.h>
#include <linux/stddef.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

80 81 82
#include <linux/crypto.h>
#include <linux/scatterlist.h>

83 84
int sysctl_tcp_tw_reuse __read_mostly;
int sysctl_tcp_low_latency __read_mostly;
Linus Torvalds's avatar
Linus Torvalds committed
85 86 87 88

/* Check TCP sequence numbers in ICMP packets. */
#define ICMP_MIN_LENGTH 8

89
void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb);
Linus Torvalds's avatar
Linus Torvalds committed
90

91
#ifdef CONFIG_TCP_MD5SIG
92 93
static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk,
						   __be32 addr);
94
static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
95
				   __be32 saddr, __be32 daddr,
96
				   struct tcphdr *th, unsigned int tcplen);
97 98 99 100 101 102
#else
static inline
struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
{
	return NULL;
}
103 104
#endif

105
struct inet_hashinfo __cacheline_aligned tcp_hashinfo = {
106 107 108
	.lhash_lock  = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock),
	.lhash_users = ATOMIC_INIT(0),
	.lhash_wait  = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
Linus Torvalds's avatar
Linus Torvalds committed
109 110
};

111
static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
112
{
113 114
	return secure_tcp_sequence_number(ip_hdr(skb)->daddr,
					  ip_hdr(skb)->saddr,
115 116
					  tcp_hdr(skb)->dest,
					  tcp_hdr(skb)->source);
Linus Torvalds's avatar
Linus Torvalds committed
117 118
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
{
	const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
	struct tcp_sock *tp = tcp_sk(sk);

	/* With PAWS, it is safe from the viewpoint
	   of data integrity. Even without PAWS it is safe provided sequence
	   spaces do not overlap i.e. at data rates <= 80Mbit/sec.

	   Actually, the idea is close to VJ's one, only timestamp cache is
	   held not per host, but per port pair and TW bucket is used as state
	   holder.

	   If TW bucket has been already destroyed we fall back to VJ's scheme
	   and use initial timestamp retrieved from peer table.
	 */
	if (tcptw->tw_ts_recent_stamp &&
	    (twp == NULL || (sysctl_tcp_tw_reuse &&
137
			     get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
138 139 140 141 142 143 144 145 146 147 148 149 150 151
		tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
		if (tp->write_seq == 0)
			tp->write_seq = 1;
		tp->rx_opt.ts_recent	   = tcptw->tw_ts_recent;
		tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
		sock_hold(sktw);
		return 1;
	}

	return 0;
}

EXPORT_SYMBOL_GPL(tcp_twsk_unique);

Linus Torvalds's avatar
Linus Torvalds committed
152 153 154 155 156 157 158
/* This will initiate an outgoing connection. */
int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
{
	struct inet_sock *inet = inet_sk(sk);
	struct tcp_sock *tp = tcp_sk(sk);
	struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
	struct rtable *rt;
159
	__be32 daddr, nexthop;
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	int tmp;
	int err;

	if (addr_len < sizeof(struct sockaddr_in))
		return -EINVAL;

	if (usin->sin_family != AF_INET)
		return -EAFNOSUPPORT;

	nexthop = daddr = usin->sin_addr.s_addr;
	if (inet->opt && inet->opt->srr) {
		if (!daddr)
			return -EINVAL;
		nexthop = inet->opt->faddr;
	}

	tmp = ip_route_connect(&rt, nexthop, inet->saddr,
			       RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
			       IPPROTO_TCP,
179
			       inet->sport, usin->sin_port, sk, 1);
180 181 182
	if (tmp < 0) {
		if (tmp == -ENETUNREACH)
			IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds's avatar
Linus Torvalds committed
183
		return tmp;
184
	}
Linus Torvalds's avatar
Linus Torvalds committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

	if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
		ip_rt_put(rt);
		return -ENETUNREACH;
	}

	if (!inet->opt || !inet->opt->srr)
		daddr = rt->rt_dst;

	if (!inet->saddr)
		inet->saddr = rt->rt_src;
	inet->rcv_saddr = inet->saddr;

	if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) {
		/* Reset inherited state */
		tp->rx_opt.ts_recent	   = 0;
		tp->rx_opt.ts_recent_stamp = 0;
		tp->write_seq		   = 0;
	}

205
	if (tcp_death_row.sysctl_tw_recycle &&
Linus Torvalds's avatar
Linus Torvalds committed
206 207
	    !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
		struct inet_peer *peer = rt_get_peer(rt);
208 209 210 211 212
		/*
		 * VJ's idea. We save last timestamp seen from
		 * the destination in peer table, when entering state
		 * TIME-WAIT * and initialize rx_opt.ts_recent from it,
		 * when trying new connection.
Linus Torvalds's avatar
Linus Torvalds committed
213
		 */
214
		if (peer != NULL &&
215
		    peer->tcp_ts_stamp + TCP_PAWS_MSL >= get_seconds()) {
Linus Torvalds's avatar
Linus Torvalds committed
216 217 218 219 220 221 222 223
			tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
			tp->rx_opt.ts_recent = peer->tcp_ts;
		}
	}

	inet->dport = usin->sin_port;
	inet->daddr = daddr;

224
	inet_csk(sk)->icsk_ext_hdr_len = 0;
Linus Torvalds's avatar
Linus Torvalds committed
225
	if (inet->opt)
226
		inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
Linus Torvalds's avatar
Linus Torvalds committed
227 228 229 230 231 232 233 234 235

	tp->rx_opt.mss_clamp = 536;

	/* Socket identity is still unknown (sport may be zero).
	 * However we set state to SYN-SENT and not releasing socket
	 * lock select source port, enter ourselves into the hash tables and
	 * complete initialization after this.
	 */
	tcp_set_state(sk, TCP_SYN_SENT);
236
	err = inet_hash_connect(&tcp_death_row, sk);
Linus Torvalds's avatar
Linus Torvalds committed
237 238 239
	if (err)
		goto failure;

240 241
	err = ip_route_newports(&rt, IPPROTO_TCP,
				inet->sport, inet->dport, sk);
Linus Torvalds's avatar
Linus Torvalds committed
242 243 244 245
	if (err)
		goto failure;

	/* OK, now commit destination to socket.  */
246
	sk->sk_gso_type = SKB_GSO_TCPV4;
247
	sk_setup_caps(sk, &rt->u.dst);
Linus Torvalds's avatar
Linus Torvalds committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	if (!tp->write_seq)
		tp->write_seq = secure_tcp_sequence_number(inet->saddr,
							   inet->daddr,
							   inet->sport,
							   usin->sin_port);

	inet->id = tp->write_seq ^ jiffies;

	err = tcp_connect(sk);
	rt = NULL;
	if (err)
		goto failure;

	return 0;

failure:
265 266 267 268
	/*
	 * This unhashes the socket and releases the local port,
	 * if necessary.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273 274 275 276 277 278
	tcp_set_state(sk, TCP_CLOSE);
	ip_rt_put(rt);
	sk->sk_route_caps = 0;
	inet->dport = 0;
	return err;
}

/*
 * This routine does path mtu discovery as defined in RFC1191.
 */
Stephen Hemminger's avatar
Stephen Hemminger committed
279
static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu)
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293
{
	struct dst_entry *dst;
	struct inet_sock *inet = inet_sk(sk);

	/* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
	 * send out by Linux are always <576bytes so they should go through
	 * unfragmented).
	 */
	if (sk->sk_state == TCP_LISTEN)
		return;

	/* We don't check in the destentry if pmtu discovery is forbidden
	 * on this route. We just assume that no packet_to_big packets
	 * are send back when pmtu discovery is not active.
294
	 * There is a small race when the user changes this flag in the
Linus Torvalds's avatar
Linus Torvalds committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	 * route, but I think that's acceptable.
	 */
	if ((dst = __sk_dst_check(sk, 0)) == NULL)
		return;

	dst->ops->update_pmtu(dst, mtu);

	/* Something is about to be wrong... Remember soft error
	 * for the case, if this connection will not able to recover.
	 */
	if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
		sk->sk_err_soft = EMSGSIZE;

	mtu = dst_mtu(dst);

	if (inet->pmtudisc != IP_PMTUDISC_DONT &&
311
	    inet_csk(sk)->icsk_pmtu_cookie > mtu) {
Linus Torvalds's avatar
Linus Torvalds committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
		tcp_sync_mss(sk, mtu);

		/* Resend the TCP packet because it's
		 * clear that the old packet has been
		 * dropped. This is the new "fast" path mtu
		 * discovery.
		 */
		tcp_simple_retransmit(sk);
	} /* else let the usual retransmit timer handle it */
}

/*
 * This routine is called by the ICMP module when it gets some
 * sort of error condition.  If err < 0 then the socket should
 * be closed and the error returned to the user.  If err > 0
 * it's just the icmp type << 8 | icmp code.  After adjustment
 * header points to the first 8 bytes of the tcp header.  We need
 * to find the appropriate port.
 *
 * The locking strategy used here is very "optimistic". When
 * someone else accesses the socket the ICMP is just dropped
 * and for some paths there is no check at all.
 * A more general error queue to queue errors for later handling
 * is probably better.
 *
 */

void tcp_v4_err(struct sk_buff *skb, u32 info)
{
	struct iphdr *iph = (struct iphdr *)skb->data;
	struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
	struct tcp_sock *tp;
	struct inet_sock *inet;
345 346
	const int type = icmp_hdr(skb)->type;
	const int code = icmp_hdr(skb)->code;
Linus Torvalds's avatar
Linus Torvalds committed
347 348 349 350 351 352 353 354 355
	struct sock *sk;
	__u32 seq;
	int err;

	if (skb->len < (iph->ihl << 2) + 8) {
		ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
		return;
	}

356
	sk = inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->daddr, th->dest,
357
			iph->saddr, th->source, inet_iif(skb));
Linus Torvalds's avatar
Linus Torvalds committed
358 359 360 361 362
	if (!sk) {
		ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
		return;
	}
	if (sk->sk_state == TCP_TIME_WAIT) {
363
		inet_twsk_put(inet_twsk(sk));
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		return;
	}

	bh_lock_sock(sk);
	/* If too many ICMPs get dropped on busy
	 * servers this needs to be solved differently.
	 */
	if (sock_owned_by_user(sk))
		NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);

	if (sk->sk_state == TCP_CLOSE)
		goto out;

	tp = tcp_sk(sk);
	seq = ntohl(th->seq);
	if (sk->sk_state != TCP_LISTEN &&
	    !between(seq, tp->snd_una, tp->snd_nxt)) {
381
		NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds's avatar
Linus Torvalds committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		goto out;
	}

	switch (type) {
	case ICMP_SOURCE_QUENCH:
		/* Just silently ignore these. */
		goto out;
	case ICMP_PARAMETERPROB:
		err = EPROTO;
		break;
	case ICMP_DEST_UNREACH:
		if (code > NR_ICMP_UNREACH)
			goto out;

		if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
			if (!sock_owned_by_user(sk))
				do_pmtu_discovery(sk, iph, info);
			goto out;
		}

		err = icmp_err_convert[code].errno;
		break;
	case ICMP_TIME_EXCEEDED:
		err = EHOSTUNREACH;
		break;
	default:
		goto out;
	}

	switch (sk->sk_state) {
412
		struct request_sock *req, **prev;
Linus Torvalds's avatar
Linus Torvalds committed
413 414 415 416
	case TCP_LISTEN:
		if (sock_owned_by_user(sk))
			goto out;

417 418
		req = inet_csk_search_req(sk, &prev, th->dest,
					  iph->daddr, iph->saddr);
Linus Torvalds's avatar
Linus Torvalds committed
419 420 421 422 423 424 425 426
		if (!req)
			goto out;

		/* ICMPs are not backlogged, hence we cannot get
		   an established socket here.
		 */
		BUG_TRAP(!req->sk);

427
		if (seq != tcp_rsk(req)->snt_isn) {
Linus Torvalds's avatar
Linus Torvalds committed
428 429 430 431 432 433 434 435 436 437
			NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
			goto out;
		}

		/*
		 * Still in SYN_RECV, just remove it silently.
		 * There is no good way to pass the error to the newly
		 * created socket, and POSIX does not want network
		 * errors returned from accept().
		 */
438
		inet_csk_reqsk_queue_drop(sk, req, prev);
Linus Torvalds's avatar
Linus Torvalds committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
		goto out;

	case TCP_SYN_SENT:
	case TCP_SYN_RECV:  /* Cannot happen.
			       It can f.e. if SYNs crossed.
			     */
		if (!sock_owned_by_user(sk)) {
			sk->sk_err = err;

			sk->sk_error_report(sk);

			tcp_done(sk);
		} else {
			sk->sk_err_soft = err;
		}
		goto out;
	}

	/* If we've already connected we will keep trying
	 * until we time out, or the user gives up.
	 *
	 * rfc1122 4.2.3.9 allows to consider as hard errors
	 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
	 * but it is obsoleted by pmtu discovery).
	 *
	 * Note, that in modern internet, where routing is unreliable
	 * and in each dark corner broken firewalls sit, sending random
	 * errors ordered by their masters even this two messages finally lose
	 * their original sense (even Linux sends invalid PORT_UNREACHs)
	 *
	 * Now we are in compliance with RFCs.
	 *							--ANK (980905)
	 */

	inet = inet_sk(sk);
	if (!sock_owned_by_user(sk) && inet->recverr) {
		sk->sk_err = err;
		sk->sk_error_report(sk);
	} else	{ /* Only an error on timeout */
		sk->sk_err_soft = err;
	}

out:
	bh_unlock_sock(sk);
	sock_put(sk);
}

/* This routine computes an IPv4 TCP checksum. */
487
void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
488 489
{
	struct inet_sock *inet = inet_sk(sk);
490
	struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds's avatar
Linus Torvalds committed
491

492
	if (skb->ip_summed == CHECKSUM_PARTIAL) {
493 494
		th->check = ~tcp_v4_check(len, inet->saddr,
					  inet->daddr, 0);
495
		skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viro's avatar
Al Viro committed
496
		skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds's avatar
Linus Torvalds committed
497
	} else {
498
		th->check = tcp_v4_check(len, inet->saddr, inet->daddr,
Linus Torvalds's avatar
Linus Torvalds committed
499 500 501 502 503 504
					 csum_partial((char *)th,
						      th->doff << 2,
						      skb->csum));
	}
}

505 506
int tcp_v4_gso_send_check(struct sk_buff *skb)
{
507
	const struct iphdr *iph;
508 509 510 511 512
	struct tcphdr *th;

	if (!pskb_may_pull(skb, sizeof(*th)))
		return -EINVAL;

513
	iph = ip_hdr(skb);
514
	th = tcp_hdr(skb);
515 516

	th->check = 0;
517
	th->check = ~tcp_v4_check(skb->len, iph->saddr, iph->daddr, 0);
518
	skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viro's avatar
Al Viro committed
519
	skb->csum_offset = offsetof(struct tcphdr, check);
520
	skb->ip_summed = CHECKSUM_PARTIAL;
521 522 523
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
524 525 526 527 528 529 530 531 532 533 534 535 536
/*
 *	This routine will send an RST to the other tcp.
 *
 *	Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
 *		      for reset.
 *	Answer: if a packet caused RST, it is not for a socket
 *		existing in our system, if it is matched to a socket,
 *		it is just duplicate segment or bug in other side's TCP.
 *		So that we build reply only basing on parameters
 *		arrived with segment.
 *	Exception: precedence violation. We do not implement it in any case.
 */

537
static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
538
{
539
	struct tcphdr *th = tcp_hdr(skb);
540 541 542
	struct {
		struct tcphdr th;
#ifdef CONFIG_TCP_MD5SIG
543
		__be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
544 545
#endif
	} rep;
Linus Torvalds's avatar
Linus Torvalds committed
546
	struct ip_reply_arg arg;
547 548 549
#ifdef CONFIG_TCP_MD5SIG
	struct tcp_md5sig_key *key;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
550 551 552 553 554

	/* Never send a reset in response to a reset. */
	if (th->rst)
		return;

555
	if (skb->rtable->rt_type != RTN_LOCAL)
Linus Torvalds's avatar
Linus Torvalds committed
556 557 558
		return;

	/* Swap the send and the receive. */
559 560 561 562 563
	memset(&rep, 0, sizeof(rep));
	rep.th.dest   = th->source;
	rep.th.source = th->dest;
	rep.th.doff   = sizeof(struct tcphdr) / 4;
	rep.th.rst    = 1;
Linus Torvalds's avatar
Linus Torvalds committed
564 565

	if (th->ack) {
566
		rep.th.seq = th->ack_seq;
Linus Torvalds's avatar
Linus Torvalds committed
567
	} else {
568 569 570
		rep.th.ack = 1;
		rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
				       skb->len - (th->doff << 2));
Linus Torvalds's avatar
Linus Torvalds committed
571 572
	}

573
	memset(&arg, 0, sizeof(arg));
574 575 576 577
	arg.iov[0].iov_base = (unsigned char *)&rep;
	arg.iov[0].iov_len  = sizeof(rep.th);

#ifdef CONFIG_TCP_MD5SIG
578
	key = sk ? tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr) : NULL;
579 580 581 582 583 584 585 586 587 588 589
	if (key) {
		rep.opt[0] = htonl((TCPOPT_NOP << 24) |
				   (TCPOPT_NOP << 16) |
				   (TCPOPT_MD5SIG << 8) |
				   TCPOLEN_MD5SIG);
		/* Update length and the length the header thinks exists */
		arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
		rep.th.doff = arg.iov[0].iov_len / 4;

		tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[1],
					key,
590 591
					ip_hdr(skb)->daddr,
					ip_hdr(skb)->saddr,
592
					&rep.th, arg.iov[0].iov_len);
593 594
	}
#endif
595 596
	arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
				      ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds's avatar
Linus Torvalds committed
597 598 599
				      sizeof(struct tcphdr), IPPROTO_TCP, 0);
	arg.csumoffset = offsetof(struct tcphdr, check) / 2;

600 601
	ip_send_reply(dev_net(skb->dst->dev)->ipv4.tcp_sock, skb,
		      &arg, arg.iov[0].iov_len);
Linus Torvalds's avatar
Linus Torvalds committed
602 603 604 605 606 607 608 609 610

	TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
	TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
}

/* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
   outside socket context is ugly, certainly. What can I do?
 */

611 612 613
static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
			    u32 win, u32 ts, int oif,
			    struct tcp_md5sig_key *key)
Linus Torvalds's avatar
Linus Torvalds committed
614
{
615
	struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds's avatar
Linus Torvalds committed
616 617
	struct {
		struct tcphdr th;
618
		__be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
619
#ifdef CONFIG_TCP_MD5SIG
620
			   + (TCPOLEN_MD5SIG_ALIGNED >> 2)
621 622
#endif
			];
Linus Torvalds's avatar
Linus Torvalds committed
623 624 625 626
	} rep;
	struct ip_reply_arg arg;

	memset(&rep.th, 0, sizeof(struct tcphdr));
627
	memset(&arg, 0, sizeof(arg));
Linus Torvalds's avatar
Linus Torvalds committed
628 629 630 631

	arg.iov[0].iov_base = (unsigned char *)&rep;
	arg.iov[0].iov_len  = sizeof(rep.th);
	if (ts) {
632 633 634 635 636
		rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
				   (TCPOPT_TIMESTAMP << 8) |
				   TCPOLEN_TIMESTAMP);
		rep.opt[1] = htonl(tcp_time_stamp);
		rep.opt[2] = htonl(ts);
637
		arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
Linus Torvalds's avatar
Linus Torvalds committed
638 639 640 641 642 643 644 645 646 647 648
	}

	/* Swap the send and the receive. */
	rep.th.dest    = th->source;
	rep.th.source  = th->dest;
	rep.th.doff    = arg.iov[0].iov_len / 4;
	rep.th.seq     = htonl(seq);
	rep.th.ack_seq = htonl(ack);
	rep.th.ack     = 1;
	rep.th.window  = htons(win);

649 650 651 652 653 654 655 656 657 658 659 660 661
#ifdef CONFIG_TCP_MD5SIG
	if (key) {
		int offset = (ts) ? 3 : 0;

		rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
					  (TCPOPT_NOP << 16) |
					  (TCPOPT_MD5SIG << 8) |
					  TCPOLEN_MD5SIG);
		arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
		rep.th.doff = arg.iov[0].iov_len/4;

		tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[offset],
					key,
662 663
					ip_hdr(skb)->daddr,
					ip_hdr(skb)->saddr,
664
					&rep.th, arg.iov[0].iov_len);
665 666
	}
#endif
667 668
	arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
				      ip_hdr(skb)->saddr, /* XXX */
Linus Torvalds's avatar
Linus Torvalds committed
669 670
				      arg.iov[0].iov_len, IPPROTO_TCP, 0);
	arg.csumoffset = offsetof(struct tcphdr, check) / 2;
671 672
	if (oif)
		arg.bound_dev_if = oif;
Linus Torvalds's avatar
Linus Torvalds committed
673

674 675
	ip_send_reply(dev_net(skb->dev)->ipv4.tcp_sock, skb,
		      &arg, arg.iov[0].iov_len);
Linus Torvalds's avatar
Linus Torvalds committed
676 677 678 679 680 681

	TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
}

static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
{
682
	struct inet_timewait_sock *tw = inet_twsk(sk);
683
	struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds's avatar
Linus Torvalds committed
684

685
	tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
686
			tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
687 688 689 690
			tcptw->tw_ts_recent,
			tw->tw_bound_dev_if,
			tcp_twsk_md5_key(tcptw)
			);
Linus Torvalds's avatar
Linus Torvalds committed
691

692
	inet_twsk_put(tw);
Linus Torvalds's avatar
Linus Torvalds committed
693 694
}

695 696
static void tcp_v4_reqsk_send_ack(struct sk_buff *skb,
				  struct request_sock *req)
Linus Torvalds's avatar
Linus Torvalds committed
697
{
698
	tcp_v4_send_ack(skb, tcp_rsk(req)->snt_isn + 1,
699
			tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
700 701 702
			req->ts_recent,
			0,
			tcp_v4_md5_do_lookup(skb->sk, ip_hdr(skb)->daddr));
Linus Torvalds's avatar
Linus Torvalds committed
703 704 705
}

/*
706
 *	Send a SYN-ACK after having received a SYN.
707
 *	This still operates on a request_sock only, not on a big
Linus Torvalds's avatar
Linus Torvalds committed
708 709
 *	socket.
 */
710 711
static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
				struct dst_entry *dst)
Linus Torvalds's avatar
Linus Torvalds committed
712
{
713
	const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds's avatar
Linus Torvalds committed
714 715 716 717
	int err = -1;
	struct sk_buff * skb;

	/* First, grab a route. */
718
	if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
719
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
720 721 722 723

	skb = tcp_make_synack(sk, dst, req);

	if (skb) {
724
		struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds's avatar
Linus Torvalds committed
725

726
		th->check = tcp_v4_check(skb->len,
727 728
					 ireq->loc_addr,
					 ireq->rmt_addr,
Linus Torvalds's avatar
Linus Torvalds committed
729 730 731
					 csum_partial((char *)th, skb->len,
						      skb->csum));

732 733 734
		err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
					    ireq->rmt_addr,
					    ireq->opt);
735
		err = net_xmit_eval(err);
Linus Torvalds's avatar
Linus Torvalds committed
736 737 738 739 740 741
	}

	dst_release(dst);
	return err;
}

742 743 744 745 746
static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req)
{
	return __tcp_v4_send_synack(sk, req, NULL);
}

Linus Torvalds's avatar
Linus Torvalds committed
747
/*
748
 *	IPv4 request_sock destructor.
Linus Torvalds's avatar
Linus Torvalds committed
749
 */
750
static void tcp_v4_reqsk_destructor(struct request_sock *req)
Linus Torvalds's avatar
Linus Torvalds committed
751
{
Jesper Juhl's avatar
Jesper Juhl committed
752
	kfree(inet_rsk(req)->opt);
Linus Torvalds's avatar
Linus Torvalds committed
753 754
}

755
#ifdef CONFIG_SYN_COOKIES
Stephen Hemminger's avatar
Stephen Hemminger committed
756
static void syn_flood_warning(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
757 758 759 760 761 762 763
{
	static unsigned long warntime;

	if (time_after(jiffies, (warntime + HZ * 60))) {
		warntime = jiffies;
		printk(KERN_INFO
		       "possible SYN flooding on port %d. Sending cookies.\n",
764
		       ntohs(tcp_hdr(skb)->dest));
Linus Torvalds's avatar
Linus Torvalds committed
765 766
	}
}
767
#endif
Linus Torvalds's avatar
Linus Torvalds committed
768 769

/*
770
 * Save and compile IPv4 options into the request_sock if needed.
Linus Torvalds's avatar
Linus Torvalds committed
771
 */
Stephen Hemminger's avatar
Stephen Hemminger committed
772 773
static struct ip_options *tcp_v4_save_options(struct sock *sk,
					      struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
{
	struct ip_options *opt = &(IPCB(skb)->opt);
	struct ip_options *dopt = NULL;

	if (opt && opt->optlen) {
		int opt_size = optlength(opt);
		dopt = kmalloc(opt_size, GFP_ATOMIC);
		if (dopt) {
			if (ip_options_echo(dopt, skb)) {
				kfree(dopt);
				dopt = NULL;
			}
		}
	}
	return dopt;
}

791 792 793 794 795 796 797 798
#ifdef CONFIG_TCP_MD5SIG
/*
 * RFC2385 MD5 checksumming requires a mapping of
 * IP address->MD5 Key.
 * We need to maintain these in the sk structure.
 */

/* Find the Key structure for an address.  */
799 800
static struct tcp_md5sig_key *
			tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
801 802 803 804 805 806 807 808
{
	struct tcp_sock *tp = tcp_sk(sk);
	int i;

	if (!tp->md5sig_info || !tp->md5sig_info->entries4)
		return NULL;
	for (i = 0; i < tp->md5sig_info->entries4; i++) {
		if (tp->md5sig_info->keys4[i].addr == addr)
809
			return &tp->md5sig_info->keys4[i].base;
810 811 812 813 814 815 816 817 818 819 820 821
	}
	return NULL;
}

struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
					 struct sock *addr_sk)
{
	return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->daddr);
}

EXPORT_SYMBOL(tcp_v4_md5_lookup);

Adrian Bunk's avatar
Adrian Bunk committed
822 823
static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
						      struct request_sock *req)
824 825 826 827 828 829 830 831 832
{
	return tcp_v4_md5_do_lookup(sk, inet_rsk(req)->rmt_addr);
}

/* This can be called on a newly created socket, from other files */
int tcp_v4_md5_do_add(struct sock *sk, __be32 addr,
		      u8 *newkey, u8 newkeylen)
{
	/* Add Key to the list */
833
	struct tcp_md5sig_key *key;
834 835 836
	struct tcp_sock *tp = tcp_sk(sk);
	struct tcp4_md5sig_key *keys;

837
	key = tcp_v4_md5_do_lookup(sk, addr);
838 839
	if (key) {
		/* Pre-existing entry - just update that one. */
840 841 842
		kfree(key->key);
		key->key = newkey;
		key->keylen = newkeylen;
843
	} else {
844 845
		struct tcp_md5sig_info *md5sig;

846
		if (!tp->md5sig_info) {
847 848
			tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info),
						  GFP_ATOMIC);
849 850 851 852
			if (!tp->md5sig_info) {
				kfree(newkey);
				return -ENOMEM;
			}
853
			sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
854 855 856 857 858
		}
		if (tcp_alloc_md5sig_pool() == NULL) {
			kfree(newkey);
			return -ENOMEM;
		}
859 860 861 862
		md5sig = tp->md5sig_info;

		if (md5sig->alloced4 == md5sig->entries4) {
			keys = kmalloc((sizeof(*keys) *
863
					(md5sig->entries4 + 1)), GFP_ATOMIC);
864 865 866 867 868 869
			if (!keys) {
				kfree(newkey);
				tcp_free_md5sig_pool();
				return -ENOMEM;
			}

870 871 872
			if (md5sig->entries4)
				memcpy(keys, md5sig->keys4,
				       sizeof(*keys) * md5sig->entries4);
873 874

			/* Free old key list, and reference new one */
875
			kfree(md5sig->keys4);
876 877
			md5sig->keys4 = keys;
			md5sig->alloced4++;
878
		}
879
		md5sig->entries4++;
880 881 882
		md5sig->keys4[md5sig->entries4 - 1].addr        = addr;
		md5sig->keys4[md5sig->entries4 - 1].base.key    = newkey;
		md5sig->keys4[md5sig->entries4 - 1].base.keylen = newkeylen;
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
	}
	return 0;
}

EXPORT_SYMBOL(tcp_v4_md5_do_add);

static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk,
			       u8 *newkey, u8 newkeylen)
{
	return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->daddr,
				 newkey, newkeylen);
}

int tcp_v4_md5_do_del(struct sock *sk, __be32 addr)
{
	struct tcp_sock *tp = tcp_sk(sk);
	int i;

	for (i = 0; i < tp->md5sig_info->entries4; i++) {
		if (tp->md5sig_info->keys4[i].addr == addr) {
			/* Free the key */
904
			kfree(tp->md5sig_info->keys4[i].base.key);
905 906 907 908 909
			tp->md5sig_info->entries4--;

			if (tp->md5sig_info->entries4 == 0) {
				kfree(tp->md5sig_info->keys4);
				tp->md5sig_info->keys4 = NULL;
910
				tp->md5sig_info->alloced4 = 0;
911
			} else if (tp->md5sig_info->entries4 != i) {
912
				/* Need to do some manipulation */
913 914 915 916
				memmove(&tp->md5sig_info->keys4[i],
					&tp->md5sig_info->keys4[i+1],
					(tp->md5sig_info->entries4 - i) *
					 sizeof(struct tcp4_md5sig_key));
917 918 919 920 921 922 923 924 925 926
			}
			tcp_free_md5sig_pool();
			return 0;
		}
	}
	return -ENOENT;
}

EXPORT_SYMBOL(tcp_v4_md5_do_del);

927
static void tcp_v4_clear_md5_list(struct sock *sk)
928 929 930 931 932 933 934 935 936 937
{
	struct tcp_sock *tp = tcp_sk(sk);

	/* Free each key, then the set of key keys,
	 * the crypto element, and then decrement our
	 * hold on the last resort crypto.
	 */
	if (tp->md5sig_info->entries4) {
		int i;
		for (i = 0; i < tp->md5sig_info->entries4; i++)
938
			kfree(tp->md5sig_info->keys4[i].base.key);
939 940 941 942 943 944 945 946 947 948
		tp->md5sig_info->entries4 = 0;
		tcp_free_md5sig_pool();
	}
	if (tp->md5sig_info->keys4) {
		kfree(tp->md5sig_info->keys4);
		tp->md5sig_info->keys4 = NULL;
		tp->md5sig_info->alloced4  = 0;
	}
}

949 950
static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
				 int optlen)
951 952 953 954 955 956 957 958
{
	struct tcp_md5sig cmd;
	struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
	u8 *newkey;

	if (optlen < sizeof(cmd))
		return -EINVAL;

959
	if (copy_from_user(&cmd, optval, sizeof(cmd)))
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
		return -EFAULT;

	if (sin->sin_family != AF_INET)
		return -EINVAL;

	if (!cmd.tcpm_key || !cmd.tcpm_keylen) {
		if (!tcp_sk(sk)->md5sig_info)
			return -ENOENT;
		return tcp_v4_md5_do_del(sk, sin->sin_addr.s_addr);
	}

	if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
		return -EINVAL;

	if (!tcp_sk(sk)->md5sig_info) {
		struct tcp_sock *tp = tcp_sk(sk);
976
		struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
977 978 979 980 981

		if (!p)
			return -EINVAL;

		tp->md5sig_info = p;
982
		sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
983 984
	}

985
	newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
986 987 988 989 990 991 992 993
	if (!newkey)
		return -ENOMEM;
	return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
				 newkey, cmd.tcpm_keylen);
}

static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
				   __be32 saddr, __be32 daddr,
994
				   struct tcphdr *th,
995
				   unsigned int tcplen)
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
{
	struct tcp_md5sig_pool *hp;
	struct tcp4_pseudohdr *bp;
	int err;

	/*
	 * Okay, so RFC2385 is turned on for this connection,
	 * so we need to generate the MD5 hash for the packet now.
	 */

	hp = tcp_get_md5sig_pool();
	if (!hp)
		goto clear_hash_noput;

	bp = &hp->md5_blk.ip4;

	/*
1013
	 * The TCP pseudo-header (in the order: source IP address,
1014 1015 1016 1017 1018 1019
	 * destination IP address, zero-padded protocol number, and
	 * segment length)
	 */
	bp->saddr = saddr;
	bp->daddr = daddr;
	bp->pad = 0;
1020
	bp->protocol = IPPROTO_TCP;
1021
	bp->len = htons(tcplen);
1022

1023 1024
	err = tcp_calc_md5_hash(md5_hash, key, sizeof(*bp),
				th, tcplen, hp);
1025 1026 1027
	if (err)
		goto clear_hash;

1028
	/* Free up the crypto pool */
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	tcp_put_md5sig_pool();
out:
	return 0;
clear_hash:
	tcp_put_md5sig_pool();
clear_hash_noput:
	memset(md5_hash, 0, 16);
	goto out;
}

int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
			 struct sock *sk,
			 struct dst_entry *dst,
			 struct request_sock *req,
1043
			 struct tcphdr *th,
1044
			 unsigned int tcplen)
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
{
	__be32 saddr, daddr;

	if (sk) {
		saddr = inet_sk(sk)->saddr;
		daddr = inet_sk(sk)->daddr;
	} else {
		struct rtable *rt = (struct rtable *)dst;
		BUG_ON(!rt);
		saddr = rt->rt_src;
		daddr = rt->rt_dst;
	}
	return tcp_v4_do_calc_md5_hash(md5_hash, key,
				       saddr, daddr,
1059
				       th, tcplen);
1060 1061 1062 1063
}

EXPORT_SYMBOL(tcp_v4_calc_md5_hash);

1064
static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
{
	/*
	 * This gets called for each TCP segment that arrives
	 * so we want to be efficient.
	 * We have 3 drop cases:
	 * o No MD5 hash and one expected.
	 * o MD5 hash and we're not expecting one.
	 * o MD5 hash and its wrong.
	 */
	__u8 *hash_location = NULL;
	struct tcp_md5sig_key *hash_expected;
1076
	const struct iphdr *iph = ip_hdr(skb);
1077
	struct tcphdr *th = tcp_hdr(skb);
1078 1079 1080 1081
	int genhash;
	unsigned char newhash[16];

	hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
1082
	hash_location = tcp_parse_md5sig_option(th);
1083 1084 1085 1086 1087 1088

	/* We've parsed the options - do we have a hash? */
	if (!hash_expected && !hash_location)
		return 0;

	if (hash_expected && !hash_location) {
1089
		LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found "
1090
			       "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
1091 1092
			       NIPQUAD(iph->saddr), ntohs(th->source),
			       NIPQUAD(iph->daddr), ntohs(th->dest));
1093 1094 1095 1096
		return 1;
	}

	if (!hash_expected && hash_location) {
1097
		LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found "
1098
			       "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
1099 1100
			       NIPQUAD(iph->saddr), ntohs(th->source),
			       NIPQUAD(iph->daddr), ntohs(th->dest));
1101 1102 1103 1104 1105 1106 1107 1108 1109
		return 1;
	}

	/* Okay, so this is hash_expected and hash_location -
	 * so we need to calculate the checksum.
	 */
	genhash = tcp_v4_do_calc_md5_hash(newhash,
					  hash_expected,
					  iph->saddr, iph->daddr,
1110
					  th, skb->len);
1111 1112 1113 1114 1115

	if (genhash || memcmp(hash_location, newhash, 16) != 0) {
		if (net_ratelimit()) {
			printk(KERN_INFO "MD5 Hash failed for "
			       "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n",
1116 1117
			       NIPQUAD(iph->saddr), ntohs(th->source),
			       NIPQUAD(iph->daddr), ntohs(th->dest),
1118 1119 1120 1121 1122 1123 1124 1125 1126
			       genhash ? " tcp_v4_calc_md5_hash failed" : "");
		}
		return 1;
	}
	return 0;
}

#endif

1127
struct request_sock_ops tcp_request_sock_ops __read_mostly = {
Linus Torvalds's avatar
Linus Torvalds committed
1128
	.family		=	PF_INET,
1129
	.obj_size	=	sizeof(struct tcp_request_sock),
Linus Torvalds's avatar
Linus Torvalds committed
1130
	.rtx_syn_ack	=	tcp_v4_send_synack,
1131 1132
	.send_ack	=	tcp_v4_reqsk_send_ack,
	.destructor	=	tcp_v4_reqsk_destructor,
Linus Torvalds's avatar
Linus Torvalds committed
1133 1134 1135
	.send_reset	=	tcp_v4_send_reset,
};

1136
#ifdef CONFIG_TCP_MD5SIG
1137
static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
1138 1139
	.md5_lookup	=	tcp_v4_reqsk_md5_lookup,
};
1140
#endif
1141

1142 1143 1144
static struct timewait_sock_ops tcp_timewait_sock_ops = {
	.twsk_obj_size	= sizeof(struct tcp_timewait_sock),
	.twsk_unique	= tcp_twsk_unique,
1145
	.twsk_destructor= tcp_twsk_destructor,
1146 1147
};

Linus Torvalds's avatar
Linus Torvalds committed
1148 1149
int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
{
1150
	struct inet_request_sock *ireq;
Linus Torvalds's avatar
Linus Torvalds committed
1151
	struct tcp_options_received tmp_opt;
1152
	struct request_sock *req;
1153 1154
	__be32 saddr = ip_hdr(skb)