xref: /linux/net/ipv4/syncookies.c (revision aa8223c7bb0b05183e1737881ed21827aa5b9e73)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Syncookies implementation for the Linux kernel
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1997 Andi Kleen
51da177e4SLinus Torvalds  *  Based on ideas by D.J.Bernstein and Eric Schenk.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
91da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
101da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  $Id: syncookies.c,v 1.18 2002/02/01 22:01:04 davem Exp $
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  Missing: IPv6 support.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/tcp.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/random.h>
201da177e4SLinus Torvalds #include <linux/cryptohash.h>
211da177e4SLinus Torvalds #include <linux/kernel.h>
221da177e4SLinus Torvalds #include <net/tcp.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static __u32 syncookie_secret[2][16-3+SHA_DIGEST_WORDS];
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds static __init int init_syncookies(void)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
311da177e4SLinus Torvalds 	return 0;
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds module_init(init_syncookies);
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define COOKIEBITS 24	/* Upper bits store count */
361da177e4SLinus Torvalds #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
371da177e4SLinus Torvalds 
38714e85beSAl Viro static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
391da177e4SLinus Torvalds 		       u32 count, int c)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	__u32 tmp[16 + 5 + SHA_WORKSPACE_WORDS];
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	memcpy(tmp + 3, syncookie_secret[c], sizeof(syncookie_secret[c]));
44714e85beSAl Viro 	tmp[0] = (__force u32)saddr;
45714e85beSAl Viro 	tmp[1] = (__force u32)daddr;
46714e85beSAl Viro 	tmp[2] = ((__force u32)sport << 16) + (__force u32)dport;
471da177e4SLinus Torvalds 	tmp[3] = count;
481da177e4SLinus Torvalds 	sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	return tmp[17];
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
53714e85beSAl Viro static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
54714e85beSAl Viro 				   __be16 dport, __u32 sseq, __u32 count,
551da177e4SLinus Torvalds 				   __u32 data)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	/*
581da177e4SLinus Torvalds 	 * Compute the secure sequence number.
591da177e4SLinus Torvalds 	 * The output should be:
601da177e4SLinus Torvalds 	 *   HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
611da177e4SLinus Torvalds 	 *      + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
621da177e4SLinus Torvalds 	 * Where sseq is their sequence number and count increases every
631da177e4SLinus Torvalds 	 * minute by 1.
641da177e4SLinus Torvalds 	 * As an extra hack, we add a small "data" value that encodes the
651da177e4SLinus Torvalds 	 * MSS into the second hash value.
661da177e4SLinus Torvalds 	 */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
691da177e4SLinus Torvalds 		sseq + (count << COOKIEBITS) +
701da177e4SLinus Torvalds 		((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
711da177e4SLinus Torvalds 		 & COOKIEMASK));
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  * This retrieves the small "data" value from the syncookie.
761da177e4SLinus Torvalds  * If the syncookie is bad, the data returned will be out of
771da177e4SLinus Torvalds  * range.  This must be checked by the caller.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  * The count value used to generate the cookie must be within
801da177e4SLinus Torvalds  * "maxdiff" if the current (passed-in) "count".  The return value
811da177e4SLinus Torvalds  * is (__u32)-1 if this test fails.
821da177e4SLinus Torvalds  */
83714e85beSAl Viro static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
84714e85beSAl Viro 				  __be16 sport, __be16 dport, __u32 sseq,
851da177e4SLinus Torvalds 				  __u32 count, __u32 maxdiff)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	__u32 diff;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	/* Strip away the layers from the cookie */
901da177e4SLinus Torvalds 	cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	/* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
931da177e4SLinus Torvalds 	diff = (count - (cookie >> COOKIEBITS)) & ((__u32) - 1 >> COOKIEBITS);
941da177e4SLinus Torvalds 	if (diff >= maxdiff)
951da177e4SLinus Torvalds 		return (__u32)-1;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	return (cookie -
981da177e4SLinus Torvalds 		cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
991da177e4SLinus Torvalds 		& COOKIEMASK;	/* Leaving the data behind */
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
1031da177e4SLinus Torvalds  * This table has to be sorted and terminated with (__u16)-1.
1041da177e4SLinus Torvalds  * XXX generate a better table.
1051da177e4SLinus Torvalds  * Unresolved Issues: HIPPI with a 64k MSS is not well supported.
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds static __u16 const msstab[] = {
1081da177e4SLinus Torvalds 	64 - 1,
1091da177e4SLinus Torvalds 	256 - 1,
1101da177e4SLinus Torvalds 	512 - 1,
1111da177e4SLinus Torvalds 	536 - 1,
1121da177e4SLinus Torvalds 	1024 - 1,
1131da177e4SLinus Torvalds 	1440 - 1,
1141da177e4SLinus Torvalds 	1460 - 1,
1151da177e4SLinus Torvalds 	4312 - 1,
1161da177e4SLinus Torvalds 	(__u16)-1
1171da177e4SLinus Torvalds };
1181da177e4SLinus Torvalds /* The number doesn't include the -1 terminator */
1191da177e4SLinus Torvalds #define NUM_MSS (ARRAY_SIZE(msstab) - 1)
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds  * Generate a syncookie.  mssp points to the mss, which is returned
1231da177e4SLinus Torvalds  * rounded down to the value encoded in the cookie.
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
128*aa8223c7SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
129*aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
1301da177e4SLinus Torvalds 	int mssind;
1311da177e4SLinus Torvalds 	const __u16 mss = *mssp;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	tp->last_synq_overflow = jiffies;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/* XXX sort msstab[] by probability?  Binary search? */
1361da177e4SLinus Torvalds 	for (mssind = 0; mss > msstab[mssind + 1]; mssind++)
1371da177e4SLinus Torvalds 		;
1381da177e4SLinus Torvalds 	*mssp = msstab[mssind] + 1;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESSENT);
1411da177e4SLinus Torvalds 
142*aa8223c7SArnaldo Carvalho de Melo 	return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
143*aa8223c7SArnaldo Carvalho de Melo 				     th->source, th->dest, ntohl(th->seq),
1441da177e4SLinus Torvalds 				     jiffies / (HZ * 60), mssind);
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /*
1481da177e4SLinus Torvalds  * This (misnamed) value is the age of syncookie which is permitted.
1491da177e4SLinus Torvalds  * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
1501da177e4SLinus Torvalds  * sysctl_tcp_retries1. It's a rather complicated formula (exponential
1511da177e4SLinus Torvalds  * backoff) to compute at runtime so it's currently hardcoded here.
1521da177e4SLinus Torvalds  */
1531da177e4SLinus Torvalds #define COUNTER_TRIES 4
1541da177e4SLinus Torvalds /*
1551da177e4SLinus Torvalds  * Check if a ack sequence number is a valid syncookie.
1561da177e4SLinus Torvalds  * Return the decoded mss if it is, or 0 if not.
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds static inline int cookie_check(struct sk_buff *skb, __u32 cookie)
1591da177e4SLinus Torvalds {
160*aa8223c7SArnaldo Carvalho de Melo 	const struct iphdr *iph = ip_hdr(skb);
161*aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
162*aa8223c7SArnaldo Carvalho de Melo 	__u32 seq = ntohl(th->seq) - 1;
163*aa8223c7SArnaldo Carvalho de Melo 	__u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
164*aa8223c7SArnaldo Carvalho de Melo 					    th->source, th->dest, seq,
165*aa8223c7SArnaldo Carvalho de Melo 					    jiffies / (HZ * 60),
166*aa8223c7SArnaldo Carvalho de Melo 					    COUNTER_TRIES);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	return mssind < NUM_MSS ? msstab[mssind] + 1 : 0;
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
17260236fddSArnaldo Carvalho de Melo 					   struct request_sock *req,
1731da177e4SLinus Torvalds 					   struct dst_entry *dst)
1741da177e4SLinus Torvalds {
1758292a17aSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
1761da177e4SLinus Torvalds 	struct sock *child;
1771da177e4SLinus Torvalds 
1788292a17aSArnaldo Carvalho de Melo 	child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
1791da177e4SLinus Torvalds 	if (child)
180463c84b9SArnaldo Carvalho de Melo 		inet_csk_reqsk_queue_add(sk, req, child);
1811da177e4SLinus Torvalds 	else
18260236fddSArnaldo Carvalho de Melo 		reqsk_free(req);
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	return child;
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
1881da177e4SLinus Torvalds 			     struct ip_options *opt)
1891da177e4SLinus Torvalds {
1902e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq;
1912e6599cbSArnaldo Carvalho de Melo 	struct tcp_request_sock *treq;
1921da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
193*aa8223c7SArnaldo Carvalho de Melo 	const struct tcphdr *th = tcp_hdr(skb);
194*aa8223c7SArnaldo Carvalho de Melo 	__u32 cookie = ntohl(th->ack_seq) - 1;
1951da177e4SLinus Torvalds 	struct sock *ret = sk;
19660236fddSArnaldo Carvalho de Melo 	struct request_sock *req;
1971da177e4SLinus Torvalds 	int mss;
1981da177e4SLinus Torvalds 	struct rtable *rt;
1991da177e4SLinus Torvalds 	__u8 rcv_wscale;
2001da177e4SLinus Torvalds 
201*aa8223c7SArnaldo Carvalho de Melo 	if (!sysctl_tcp_syncookies || !th->ack)
2021da177e4SLinus Torvalds 		goto out;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	if (time_after(jiffies, tp->last_synq_overflow + TCP_TIMEOUT_INIT) ||
2051da177e4SLinus Torvalds 	    (mss = cookie_check(skb, cookie)) == 0) {
2061da177e4SLinus Torvalds 		NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESFAILED);
2071da177e4SLinus Torvalds 		goto out;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESRECV);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	ret = NULL;
21360236fddSArnaldo Carvalho de Melo 	req = reqsk_alloc(&tcp_request_sock_ops); /* for safety */
2141da177e4SLinus Torvalds 	if (!req)
2151da177e4SLinus Torvalds 		goto out;
2161da177e4SLinus Torvalds 
2174237c75cSVenkat Yekkirala 	if (security_inet_conn_request(sk, skb, req)) {
2184237c75cSVenkat Yekkirala 		reqsk_free(req);
2194237c75cSVenkat Yekkirala 		goto out;
2204237c75cSVenkat Yekkirala 	}
2212e6599cbSArnaldo Carvalho de Melo 	ireq = inet_rsk(req);
2222e6599cbSArnaldo Carvalho de Melo 	treq = tcp_rsk(req);
223*aa8223c7SArnaldo Carvalho de Melo 	treq->rcv_isn		= ntohl(th->seq) - 1;
2242e6599cbSArnaldo Carvalho de Melo 	treq->snt_isn		= cookie;
2251da177e4SLinus Torvalds 	req->mss		= mss;
226*aa8223c7SArnaldo Carvalho de Melo 	ireq->rmt_port		= th->source;
227eddc9ec5SArnaldo Carvalho de Melo 	ireq->loc_addr		= ip_hdr(skb)->daddr;
228eddc9ec5SArnaldo Carvalho de Melo 	ireq->rmt_addr		= ip_hdr(skb)->saddr;
2292e6599cbSArnaldo Carvalho de Melo 	ireq->opt		= NULL;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	/* We throwed the options of the initial SYN away, so we hope
2321da177e4SLinus Torvalds 	 * the ACK carries the same options again (see RFC1122 4.2.3.8)
2331da177e4SLinus Torvalds 	 */
2341da177e4SLinus Torvalds 	if (opt && opt->optlen) {
2351da177e4SLinus Torvalds 		int opt_size = sizeof(struct ip_options) + opt->optlen;
2361da177e4SLinus Torvalds 
2372e6599cbSArnaldo Carvalho de Melo 		ireq->opt = kmalloc(opt_size, GFP_ATOMIC);
2382e6599cbSArnaldo Carvalho de Melo 		if (ireq->opt != NULL && ip_options_echo(ireq->opt, skb)) {
2392e6599cbSArnaldo Carvalho de Melo 			kfree(ireq->opt);
2402e6599cbSArnaldo Carvalho de Melo 			ireq->opt = NULL;
2411da177e4SLinus Torvalds 		}
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 
2442e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = ireq->rcv_wscale = ireq->tstamp_ok = 0;
2452e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok	 = ireq->sack_ok = 0;
2461da177e4SLinus Torvalds 	req->expires	= 0UL;
2471da177e4SLinus Torvalds 	req->retrans	= 0;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	/*
2501da177e4SLinus Torvalds 	 * We need to lookup the route here to get at the correct
2511da177e4SLinus Torvalds 	 * window size. We should better make sure that the window size
2521da177e4SLinus Torvalds 	 * hasn't changed since we received the original syn, but I see
2531da177e4SLinus Torvalds 	 * no easy way to do this.
2541da177e4SLinus Torvalds 	 */
2551da177e4SLinus Torvalds 	{
2561da177e4SLinus Torvalds 		struct flowi fl = { .nl_u = { .ip4_u =
2571da177e4SLinus Torvalds 					      { .daddr = ((opt && opt->srr) ?
2581da177e4SLinus Torvalds 							  opt->faddr :
2592e6599cbSArnaldo Carvalho de Melo 							  ireq->rmt_addr),
2602e6599cbSArnaldo Carvalho de Melo 						.saddr = ireq->loc_addr,
2611da177e4SLinus Torvalds 						.tos = RT_CONN_FLAGS(sk) } },
2621da177e4SLinus Torvalds 				    .proto = IPPROTO_TCP,
2631da177e4SLinus Torvalds 				    .uli_u = { .ports =
264*aa8223c7SArnaldo Carvalho de Melo 					       { .sport = th->dest,
265*aa8223c7SArnaldo Carvalho de Melo 						 .dport = th->source } } };
2664237c75cSVenkat Yekkirala 		security_req_classify_flow(req, &fl);
2671da177e4SLinus Torvalds 		if (ip_route_output_key(&rt, &fl)) {
26860236fddSArnaldo Carvalho de Melo 			reqsk_free(req);
2691da177e4SLinus Torvalds 			goto out;
2701da177e4SLinus Torvalds 		}
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/* Try to redo what tcp_v4_send_synack did. */
2741da177e4SLinus Torvalds 	req->window_clamp = dst_metric(&rt->u.dst, RTAX_WINDOW);
2751da177e4SLinus Torvalds 	tcp_select_initial_window(tcp_full_space(sk), req->mss,
2761da177e4SLinus Torvalds 				  &req->rcv_wnd, &req->window_clamp,
2771da177e4SLinus Torvalds 				  0, &rcv_wscale);
2781da177e4SLinus Torvalds 	/* BTW win scale with syncookies is 0 by definition */
2792e6599cbSArnaldo Carvalho de Melo 	ireq->rcv_wscale  = rcv_wscale;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	ret = get_cookie_sock(sk, skb, req, &rt->u.dst);
2821da177e4SLinus Torvalds out:	return ret;
2831da177e4SLinus Torvalds }
284