xref: /freebsd/sys/netpfil/pf/pf_syncookies.c (revision d59a76183470685bdf0b88013d2baad1f04f030f)
1 /*	$OpenBSD: pf_syncookies.c,v 1.7 2018/09/10 15:54:28 henning Exp $ */
2 
3 /* Copyright (c) 2016,2017 Henning Brauer <henning@openbsd.org>
4  * Copyright (c) 2016 Alexandr Nedvedicky <sashan@openbsd.org>
5  *
6  * syncookie parts based on FreeBSD sys/netinet/tcp_syncache.c
7  *
8  * Copyright (c) 2001 McAfee, Inc.
9  * Copyright (c) 2006,2013 Andre Oppermann, Internet Business Solutions AG
10  * All rights reserved.
11  *
12  * This software was developed for the FreeBSD Project by Jonathan Lemon
13  * and McAfee Research, the Security Research Division of McAfee, Inc. under
14  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
15  * DARPA CHATS research program. [2001 McAfee, Inc.]
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*
40  * when we're under synflood, we use syncookies to prevent state table
41  * exhaustion. Trigger for the synflood mode is the number of half-open
42  * connections in the state table.
43  * We leave synflood mode when the number of half-open states - including
44  * in-flight syncookies - drops far enough again
45  */
46 
47 /*
48  * syncookie enabled Initial Sequence Number:
49  *  24 bit MAC
50  *   3 bit WSCALE index
51  *   3 bit MSS index
52  *   1 bit SACK permitted
53  *   1 bit odd/even secret
54  *
55  * References:
56  *  RFC4987 TCP SYN Flooding Attacks and Common Mitigations
57  *  http://cr.yp.to/syncookies.html    (overview)
58  *  http://cr.yp.to/syncookies/archive (details)
59  */
60 
61 //#include "pflog.h"
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/mbuf.h>
66 #include <sys/filio.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/kernel.h>
70 #include <sys/time.h>
71 #include <sys/proc.h>
72 #include <sys/rwlock.h>
73 #include <sys/syslog.h>
74 
75 #include <crypto/siphash/siphash.h>
76 
77 #include <net/if.h>
78 #include <net/if_var.h>
79 #include <net/if_types.h>
80 #include <net/route.h>
81 
82 #include <netinet/in.h>
83 #include <netinet/in_pcb.h>
84 #include <netinet/ip.h>
85 #include <netinet/tcp.h>
86 #include <netinet/tcp_var.h>
87 
88 #include <net/pfvar.h>
89 #include <netpfil/pf/pf_nv.h>
90 
91 #define	DPFPRINTF(n, x)	if (V_pf_status.debug >= (n)) printf x
92 
93 union pf_syncookie {
94 	uint8_t		cookie;
95 	struct {
96 		uint8_t	oddeven:1,
97 			sack_ok:1,
98 			wscale_idx:3,
99 			mss_idx:3;
100 	} flags;
101 };
102 
103 #define	PF_SYNCOOKIE_SECRET_SIZE	SIPHASH_KEY_LENGTH
104 #define	PF_SYNCOOKIE_SECRET_LIFETIME	15 /* seconds */
105 
106 /* Protected by PF_RULES_xLOCK. */
107 struct pf_syncookie_status {
108 	struct callout	keytimeout;
109 	uint8_t		oddeven;
110 	uint8_t		key[2][SIPHASH_KEY_LENGTH];
111 	uint32_t	hiwat;	/* absolute; # of states */
112 	uint32_t	lowat;
113 };
114 VNET_DEFINE_STATIC(struct pf_syncookie_status, pf_syncookie_status);
115 #define V_pf_syncookie_status	VNET(pf_syncookie_status)
116 
117 static int	pf_syncookies_setmode(u_int8_t);
118 void		pf_syncookie_rotate(void *);
119 void		pf_syncookie_newkey(void);
120 uint32_t	pf_syncookie_mac(struct pf_pdesc *, union pf_syncookie,
121 		    uint32_t);
122 uint32_t	pf_syncookie_generate(struct mbuf *m, int off, struct pf_pdesc *,
123 		    uint16_t);
124 
125 void
126 pf_syncookies_init(void)
127 {
128 	callout_init(&V_pf_syncookie_status.keytimeout, 1);
129 	PF_RULES_WLOCK();
130 
131 	V_pf_syncookie_status.hiwat = PF_SYNCOOKIES_HIWATPCT *
132 	    V_pf_limits[PF_LIMIT_STATES].limit / 100;
133 	V_pf_syncookie_status.lowat = PF_SYNCOOKIES_LOWATPCT *
134 	    V_pf_limits[PF_LIMIT_STATES].limit / 100;
135 	pf_syncookies_setmode(PF_SYNCOOKIES_ADAPTIVE);
136 
137 	PF_RULES_WUNLOCK();
138 }
139 
140 void
141 pf_syncookies_cleanup(void)
142 {
143 	callout_stop(&V_pf_syncookie_status.keytimeout);
144 }
145 
146 int
147 pf_get_syncookies(struct pfioc_nv *nv)
148 {
149 	nvlist_t	*nvl = NULL;
150 	void		*nvlpacked = NULL;
151 	int		 error;
152 
153 #define ERROUT(x)	ERROUT_FUNCTION(errout, x)
154 
155 	nvl = nvlist_create(0);
156 	if (nvl == NULL)
157 		ERROUT(ENOMEM);
158 
159 	nvlist_add_bool(nvl, "enabled",
160 	    V_pf_status.syncookies_mode != PF_SYNCOOKIES_NEVER);
161 	nvlist_add_bool(nvl, "adaptive",
162 	    V_pf_status.syncookies_mode == PF_SYNCOOKIES_ADAPTIVE);
163 	nvlist_add_number(nvl, "highwater", V_pf_syncookie_status.hiwat);
164 	nvlist_add_number(nvl, "lowwater", V_pf_syncookie_status.lowat);
165 	nvlist_add_number(nvl, "halfopen_states",
166 	    atomic_load_32(&V_pf_status.states_halfopen));
167 
168 	nvlpacked = nvlist_pack(nvl, &nv->len);
169 	if (nvlpacked == NULL)
170 		ERROUT(ENOMEM);
171 
172 	if (nv->size == 0) {
173 		ERROUT(0);
174 	} else if (nv->size < nv->len) {
175 		ERROUT(ENOSPC);
176 	}
177 
178 	error = copyout(nvlpacked, nv->data, nv->len);
179 
180 #undef ERROUT
181 errout:
182 	nvlist_destroy(nvl);
183 	free(nvlpacked, M_NVLIST);
184 
185 	return (error);
186 }
187 
188 int
189 pf_set_syncookies(struct pfioc_nv *nv)
190 {
191 	nvlist_t	*nvl = NULL;
192 	void		*nvlpacked = NULL;
193 	int		 error;
194 	bool		 enabled, adaptive;
195 	uint32_t	 hiwat, lowat;
196 	uint8_t		 newmode;
197 
198 #define ERROUT(x)	ERROUT_FUNCTION(errout, x)
199 
200 	if (nv->len > pf_ioctl_maxcount)
201 		return (ENOMEM);
202 
203 	nvlpacked = malloc(nv->len, M_NVLIST, M_WAITOK);
204 	error = copyin(nv->data, nvlpacked, nv->len);
205 	if (error)
206 		ERROUT(error);
207 
208 	nvl = nvlist_unpack(nvlpacked, nv->len, 0);
209 	if (nvl == NULL)
210 		ERROUT(EBADMSG);
211 
212 	if (! nvlist_exists_bool(nvl, "enabled")
213 	    || ! nvlist_exists_bool(nvl, "adaptive"))
214 		ERROUT(EBADMSG);
215 
216 	enabled = nvlist_get_bool(nvl, "enabled");
217 	adaptive = nvlist_get_bool(nvl, "adaptive");
218 	PFNV_CHK(pf_nvuint32_opt(nvl, "highwater", &hiwat,
219 	    V_pf_syncookie_status.hiwat));
220 	PFNV_CHK(pf_nvuint32_opt(nvl, "lowwater", &lowat,
221 	    V_pf_syncookie_status.lowat));
222 
223 	if (lowat >= hiwat)
224 		ERROUT(EINVAL);
225 
226 	newmode = PF_SYNCOOKIES_NEVER;
227 	if (enabled)
228 		newmode = adaptive ? PF_SYNCOOKIES_ADAPTIVE : PF_SYNCOOKIES_ALWAYS;
229 
230 	PF_RULES_WLOCK();
231 	error = pf_syncookies_setmode(newmode);
232 
233 	V_pf_syncookie_status.lowat = lowat;
234 	V_pf_syncookie_status.hiwat = hiwat;
235 
236 	PF_RULES_WUNLOCK();
237 
238 #undef ERROUT
239 errout:
240 	nvlist_destroy(nvl);
241 	free(nvlpacked, M_NVLIST);
242 
243 	return (error);
244 }
245 
246 static int
247 pf_syncookies_setmode(u_int8_t mode)
248 {
249 	if (mode > PF_SYNCOOKIES_MODE_MAX)
250 		return (EINVAL);
251 
252 	if (V_pf_status.syncookies_mode == mode)
253 		return (0);
254 
255 	V_pf_status.syncookies_mode = mode;
256 	if (V_pf_status.syncookies_mode == PF_SYNCOOKIES_ALWAYS) {
257 		pf_syncookie_newkey();
258 		V_pf_status.syncookies_active = true;
259 	}
260 	return (0);
261 }
262 
263 int
264 pf_synflood_check(struct pf_pdesc *pd)
265 {
266 	MPASS(pd->proto == IPPROTO_TCP);
267 	PF_RULES_RASSERT();
268 
269 	if (pd->pf_mtag && (pd->pf_mtag->flags & PF_MTAG_FLAG_SYNCOOKIE_RECREATED))
270 		return (0);
271 
272 	if (V_pf_status.syncookies_mode != PF_SYNCOOKIES_ADAPTIVE)
273 		return (V_pf_status.syncookies_mode);
274 
275 	if (!V_pf_status.syncookies_active &&
276 	    atomic_load_32(&V_pf_status.states_halfopen) >
277 	    V_pf_syncookie_status.hiwat) {
278 		/* We'd want to 'pf_syncookie_newkey()' here, but that requires
279 		 * the rules write lock, which we can't get with the read lock
280 		 * held. */
281 		callout_reset(&V_pf_syncookie_status.keytimeout, 0,
282 		    pf_syncookie_rotate, curvnet);
283 		V_pf_status.syncookies_active = true;
284 		DPFPRINTF(LOG_WARNING,
285 		    ("synflood detected, enabling syncookies\n"));
286 		// XXXTODO V_pf_status.lcounters[LCNT_SYNFLOODS]++;
287 	}
288 
289 	return (V_pf_status.syncookies_active);
290 }
291 
292 void
293 pf_syncookie_send(struct mbuf *m, int off, struct pf_pdesc *pd)
294 {
295 	uint16_t	mss;
296 	uint32_t	iss;
297 
298 	mss = max(V_tcp_mssdflt, pf_get_mss(m, off, pd->hdr.tcp.th_off, pd->af));
299 	iss = pf_syncookie_generate(m, off, pd, mss);
300 	pf_send_tcp(NULL, pd->af, pd->dst, pd->src, *pd->dport, *pd->sport,
301 	    iss, ntohl(pd->hdr.tcp.th_seq) + 1, TH_SYN|TH_ACK, 0, mss,
302 	    0, true, 0, 0, pd->act.rtableid);
303 	counter_u64_add(V_pf_status.lcounters[KLCNT_SYNCOOKIES_SENT], 1);
304 	/* XXX Maybe only in adaptive mode? */
305 	atomic_add_64(&V_pf_status.syncookies_inflight[V_pf_syncookie_status.oddeven],
306 	    1);
307 }
308 
309 bool
310 pf_syncookie_check(struct pf_pdesc *pd)
311 {
312 	uint32_t		 hash, ack, seq;
313 	union pf_syncookie	 cookie;
314 
315 	MPASS(pd->proto == IPPROTO_TCP);
316 	PF_RULES_RASSERT();
317 
318 	seq = ntohl(pd->hdr.tcp.th_seq) - 1;
319 	ack = ntohl(pd->hdr.tcp.th_ack) - 1;
320 	cookie.cookie = (ack & 0xff) ^ (ack >> 24);
321 
322 	/* we don't know oddeven before setting the cookie (union) */
323 	if (atomic_load_64(&V_pf_status.syncookies_inflight[cookie.flags.oddeven])
324 	    == 0)
325 		return (0);
326 
327 	hash = pf_syncookie_mac(pd, cookie, seq);
328 	if ((ack & ~0xff) != (hash & ~0xff))
329 		return (false);
330 
331 	return (true);
332 }
333 
334 uint8_t
335 pf_syncookie_validate(struct pf_pdesc *pd)
336 {
337 	uint32_t		 ack;
338 	union pf_syncookie	 cookie;
339 
340 	if (! pf_syncookie_check(pd))
341 		return (0);
342 
343 	ack = ntohl(pd->hdr.tcp.th_ack) - 1;
344 	cookie.cookie = (ack & 0xff) ^ (ack >> 24);
345 
346 	counter_u64_add(V_pf_status.lcounters[KLCNT_SYNCOOKIES_VALID], 1);
347 	atomic_add_64(&V_pf_status.syncookies_inflight[cookie.flags.oddeven], -1);
348 
349 	return (1);
350 }
351 
352 /*
353  * all following functions private
354  */
355 void
356 pf_syncookie_rotate(void *arg)
357 {
358 	CURVNET_SET((struct vnet *)arg);
359 
360 	/* do we want to disable syncookies? */
361 	if (V_pf_status.syncookies_active &&
362 	    ((V_pf_status.syncookies_mode == PF_SYNCOOKIES_ADAPTIVE &&
363 	    (atomic_load_32(&V_pf_status.states_halfopen) +
364 	    atomic_load_64(&V_pf_status.syncookies_inflight[0]) +
365 	    atomic_load_64(&V_pf_status.syncookies_inflight[1])) <
366 	    V_pf_syncookie_status.lowat) ||
367 	    V_pf_status.syncookies_mode == PF_SYNCOOKIES_NEVER)
368 			) {
369 		V_pf_status.syncookies_active = false;
370 		DPFPRINTF(PF_DEBUG_MISC, ("syncookies disabled\n"));
371 	}
372 
373 	/* nothing in flight any more? delete keys and return */
374 	if (!V_pf_status.syncookies_active &&
375 	    atomic_load_64(&V_pf_status.syncookies_inflight[0]) == 0 &&
376 	    atomic_load_64(&V_pf_status.syncookies_inflight[1]) == 0) {
377 		memset(V_pf_syncookie_status.key[0], 0,
378 		    PF_SYNCOOKIE_SECRET_SIZE);
379 		memset(V_pf_syncookie_status.key[1], 0,
380 		    PF_SYNCOOKIE_SECRET_SIZE);
381 		CURVNET_RESTORE();
382 		return;
383 	}
384 
385 	PF_RULES_WLOCK();
386 	/* new key, including timeout */
387 	pf_syncookie_newkey();
388 	PF_RULES_WUNLOCK();
389 
390 	CURVNET_RESTORE();
391 }
392 
393 void
394 pf_syncookie_newkey(void)
395 {
396 	PF_RULES_WASSERT();
397 
398 	MPASS(V_pf_syncookie_status.oddeven < 2);
399 	V_pf_syncookie_status.oddeven = (V_pf_syncookie_status.oddeven + 1) & 0x1;
400 	atomic_store_64(&V_pf_status.syncookies_inflight[V_pf_syncookie_status.oddeven], 0);
401 	arc4random_buf(V_pf_syncookie_status.key[V_pf_syncookie_status.oddeven],
402 	    PF_SYNCOOKIE_SECRET_SIZE);
403 	callout_reset(&V_pf_syncookie_status.keytimeout,
404 	    PF_SYNCOOKIE_SECRET_LIFETIME * hz, pf_syncookie_rotate, curvnet);
405 }
406 
407 /*
408  * Distribution and probability of certain MSS values.  Those in between are
409  * rounded down to the next lower one.
410  * [An Analysis of TCP Maximum Segment Sizes, S. Alcock and R. Nelson, 2011]
411  *   .2%  .3%   5%    7%    7%    20%   15%   45%
412  */
413 static int pf_syncookie_msstab[] =
414     { 216, 536, 1200, 1360, 1400, 1440, 1452, 1460 };
415 
416 /*
417  * Distribution and probability of certain WSCALE values.
418  * The absence of the WSCALE option is encoded with index zero.
419  * [WSCALE values histograms, Allman, 2012]
420  *                                  X 10 10 35  5  6 14 10%   by host
421  *                                  X 11  4  5  5 18 49  3%   by connections
422  */
423 static int pf_syncookie_wstab[] = { 0, 0, 1, 2, 4, 6, 7, 8 };
424 
425 uint32_t
426 pf_syncookie_mac(struct pf_pdesc *pd, union pf_syncookie cookie, uint32_t seq)
427 {
428 	SIPHASH_CTX	ctx;
429 	uint32_t	siphash[2];
430 
431 	PF_RULES_RASSERT();
432 	MPASS(pd->proto == IPPROTO_TCP);
433 
434 	SipHash24_Init(&ctx);
435 	SipHash_SetKey(&ctx, V_pf_syncookie_status.key[cookie.flags.oddeven]);
436 
437 	switch (pd->af) {
438 	case AF_INET:
439 		SipHash_Update(&ctx, pd->src, sizeof(pd->src->v4));
440 		SipHash_Update(&ctx, pd->dst, sizeof(pd->dst->v4));
441 		break;
442 	case AF_INET6:
443 		SipHash_Update(&ctx, pd->src, sizeof(pd->src->v6));
444 		SipHash_Update(&ctx, pd->dst, sizeof(pd->dst->v6));
445 		break;
446 	default:
447 		panic("unknown address family");
448 	}
449 
450 	SipHash_Update(&ctx, pd->sport, sizeof(*pd->sport));
451 	SipHash_Update(&ctx, pd->dport, sizeof(*pd->dport));
452 	SipHash_Update(&ctx, &seq, sizeof(seq));
453 	SipHash_Update(&ctx, &cookie, sizeof(cookie));
454 	SipHash_Final((uint8_t *)&siphash, &ctx);
455 
456 	return (siphash[0] ^ siphash[1]);
457 }
458 
459 uint32_t
460 pf_syncookie_generate(struct mbuf *m, int off, struct pf_pdesc *pd,
461     uint16_t mss)
462 {
463 	uint8_t			 i, wscale;
464 	uint32_t		 iss, hash;
465 	union pf_syncookie	 cookie;
466 
467 	PF_RULES_RASSERT();
468 
469 	cookie.cookie = 0;
470 
471 	/* map MSS */
472 	for (i = nitems(pf_syncookie_msstab) - 1;
473 	    pf_syncookie_msstab[i] > mss && i > 0; i--)
474 		/* nada */;
475 	cookie.flags.mss_idx = i;
476 
477 	/* map WSCALE */
478 	wscale = pf_get_wscale(m, off, pd->hdr.tcp.th_off, pd->af);
479 	for (i = nitems(pf_syncookie_wstab) - 1;
480 	    pf_syncookie_wstab[i] > wscale && i > 0; i--)
481 		/* nada */;
482 	cookie.flags.wscale_idx = i;
483 	cookie.flags.sack_ok = 0;	/* XXX */
484 
485 	cookie.flags.oddeven = V_pf_syncookie_status.oddeven;
486 	hash = pf_syncookie_mac(pd, cookie, ntohl(pd->hdr.tcp.th_seq));
487 
488 	/*
489 	 * Put the flags into the hash and XOR them to get better ISS number
490 	 * variance.  This doesn't enhance the cryptographic strength and is
491 	 * done to prevent the 8 cookie bits from showing up directly on the
492 	 * wire.
493 	 */
494 	iss = hash & ~0xff;
495 	iss |= cookie.cookie ^ (hash >> 24);
496 
497 	return (iss);
498 }
499 
500 struct mbuf *
501 pf_syncookie_recreate_syn(uint8_t ttl, int off, struct pf_pdesc *pd)
502 {
503 	uint8_t			 wscale;
504 	uint16_t		 mss;
505 	uint32_t		 ack, seq;
506 	union pf_syncookie	 cookie;
507 
508 	seq = ntohl(pd->hdr.tcp.th_seq) - 1;
509 	ack = ntohl(pd->hdr.tcp.th_ack) - 1;
510 	cookie.cookie = (ack & 0xff) ^ (ack >> 24);
511 
512 	if (cookie.flags.mss_idx >= nitems(pf_syncookie_msstab) ||
513 	    cookie.flags.wscale_idx >= nitems(pf_syncookie_wstab))
514 		return (NULL);
515 
516 	mss = pf_syncookie_msstab[cookie.flags.mss_idx];
517 	wscale = pf_syncookie_wstab[cookie.flags.wscale_idx];
518 
519 	return (pf_build_tcp(NULL, pd->af, pd->src, pd->dst, *pd->sport,
520 	    *pd->dport, seq, 0, TH_SYN, wscale, mss, ttl, false, 0,
521 	    PF_MTAG_FLAG_SYNCOOKIE_RECREATED, pd->act.rtableid));
522 }
523