1/*-
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2003 Mike Frantzen <frantzen@w4g.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 *	$OpenBSD: pf_osfp.c,v 1.14 2008/06/12 18:17:01 henning Exp $
19 */
20
21#include <sys/cdefs.h>
22#include "opt_inet6.h"
23
24#include <sys/param.h>
25#include <sys/kernel.h>
26#include <sys/lock.h>
27#include <sys/mbuf.h>
28#include <sys/socket.h>
29
30#include <netinet/in.h>
31#include <netinet/ip.h>
32#include <netinet/tcp.h>
33
34#include <net/if.h>
35#include <net/vnet.h>
36#include <net/pfvar.h>
37
38#ifdef INET6
39#include <netinet/ip6.h>
40#endif
41
42static MALLOC_DEFINE(M_PFOSFP, "pf_osfp", "pf(4) operating system fingerprints");
43#define	DPFPRINTF(format, x...)		\
44	if (V_pf_status.debug >= PF_DEBUG_NOISY)	\
45		printf(format , ##x)
46
47SLIST_HEAD(pf_osfp_list, pf_os_fingerprint);
48VNET_DEFINE_STATIC(struct pf_osfp_list,	pf_osfp_list) =
49	SLIST_HEAD_INITIALIZER();
50#define	V_pf_osfp_list			VNET(pf_osfp_list)
51
52static struct pf_osfp_enlist	*pf_osfp_fingerprint_hdr(const struct ip *,
53				    const struct ip6_hdr *,
54				    const struct tcphdr *);
55static struct pf_os_fingerprint	*pf_osfp_find(struct pf_osfp_list *,
56				    struct pf_os_fingerprint *, u_int8_t);
57static struct pf_os_fingerprint	*pf_osfp_find_exact(struct pf_osfp_list *,
58				    struct pf_os_fingerprint *);
59static void			 pf_osfp_insert(struct pf_osfp_list *,
60				    struct pf_os_fingerprint *);
61#ifdef PFDEBUG
62static struct pf_os_fingerprint	*pf_osfp_validate(void);
63#endif
64
65/*
66 * Passively fingerprint the OS of the host (IPv4 TCP SYN packets only)
67 * Returns the list of possible OSes.
68 */
69struct pf_osfp_enlist *
70pf_osfp_fingerprint(struct pf_pdesc *pd, struct mbuf *m, int off,
71    const struct tcphdr *tcp)
72{
73	struct ip *ip;
74	struct ip6_hdr *ip6;
75	char hdr[60];
76
77	if ((pd->af != PF_INET && pd->af != PF_INET6) ||
78	    pd->proto != IPPROTO_TCP || (tcp->th_off << 2) < sizeof(*tcp))
79		return (NULL);
80
81	if (pd->af == PF_INET) {
82		ip = mtod(m, struct ip *);
83		ip6 = (struct ip6_hdr *)NULL;
84	} else {
85		ip = (struct ip *)NULL;
86		ip6 = mtod(m, struct ip6_hdr *);
87	}
88	if (!pf_pull_hdr(m, off, hdr, tcp->th_off << 2, NULL, NULL,
89	    pd->af)) return (NULL);
90
91	return (pf_osfp_fingerprint_hdr(ip, ip6, (struct tcphdr *)hdr));
92}
93
94static struct pf_osfp_enlist *
95pf_osfp_fingerprint_hdr(const struct ip *ip, const struct ip6_hdr *ip6, const struct tcphdr *tcp)
96{
97	struct pf_os_fingerprint fp, *fpresult;
98	int cnt, optlen = 0;
99	const u_int8_t *optp;
100#ifdef INET6
101	char srcname[INET6_ADDRSTRLEN];
102#else
103	char srcname[INET_ADDRSTRLEN];
104#endif
105
106	if ((tcp->th_flags & (TH_SYN|TH_ACK)) != TH_SYN)
107		return (NULL);
108	if (ip) {
109		if ((ip->ip_off & htons(IP_OFFMASK)) != 0)
110			return (NULL);
111	}
112
113	memset(&fp, 0, sizeof(fp));
114
115	if (ip) {
116		fp.fp_psize = ntohs(ip->ip_len);
117		fp.fp_ttl = ip->ip_ttl;
118		if (ip->ip_off & htons(IP_DF))
119			fp.fp_flags |= PF_OSFP_DF;
120		inet_ntoa_r(ip->ip_src, srcname);
121	}
122#ifdef INET6
123	else if (ip6) {
124		/* jumbo payload? */
125		fp.fp_psize = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
126		fp.fp_ttl = ip6->ip6_hlim;
127		fp.fp_flags |= PF_OSFP_DF;
128		fp.fp_flags |= PF_OSFP_INET6;
129		ip6_sprintf(srcname, (const struct in6_addr *)&ip6->ip6_src);
130	}
131#endif
132	else
133		return (NULL);
134	fp.fp_wsize = ntohs(tcp->th_win);
135
136	cnt = (tcp->th_off << 2) - sizeof(*tcp);
137	optp = (const u_int8_t *)((const char *)tcp + sizeof(*tcp));
138	for (; cnt > 0; cnt -= optlen, optp += optlen) {
139		if (*optp == TCPOPT_EOL)
140			break;
141
142		fp.fp_optcnt++;
143		if (*optp == TCPOPT_NOP) {
144			fp.fp_tcpopts = (fp.fp_tcpopts << PF_OSFP_TCPOPT_BITS) |
145			    PF_OSFP_TCPOPT_NOP;
146			optlen = 1;
147		} else {
148			if (cnt < 2)
149				return (NULL);
150			optlen = optp[1];
151			if (optlen > cnt || optlen < 2)
152				return (NULL);
153			switch (*optp) {
154			case TCPOPT_MAXSEG:
155				if (optlen >= TCPOLEN_MAXSEG)
156					memcpy(&fp.fp_mss, &optp[2],
157					    sizeof(fp.fp_mss));
158				fp.fp_tcpopts = (fp.fp_tcpopts <<
159				    PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_MSS;
160				NTOHS(fp.fp_mss);
161				break;
162			case TCPOPT_WINDOW:
163				if (optlen >= TCPOLEN_WINDOW)
164					memcpy(&fp.fp_wscale, &optp[2],
165					    sizeof(fp.fp_wscale));
166				NTOHS(fp.fp_wscale);
167				fp.fp_tcpopts = (fp.fp_tcpopts <<
168				    PF_OSFP_TCPOPT_BITS) |
169				    PF_OSFP_TCPOPT_WSCALE;
170				break;
171			case TCPOPT_SACK_PERMITTED:
172				fp.fp_tcpopts = (fp.fp_tcpopts <<
173				    PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_SACK;
174				break;
175			case TCPOPT_TIMESTAMP:
176				if (optlen >= TCPOLEN_TIMESTAMP) {
177					u_int32_t ts;
178					memcpy(&ts, &optp[2], sizeof(ts));
179					if (ts == 0)
180						fp.fp_flags |= PF_OSFP_TS0;
181				}
182				fp.fp_tcpopts = (fp.fp_tcpopts <<
183				    PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_TS;
184				break;
185			default:
186				return (NULL);
187			}
188		}
189		optlen = MAX(optlen, 1);	/* paranoia */
190	}
191
192	DPFPRINTF("fingerprinted %s:%d  %d:%d:%d:%d:%llx (%d) "
193	    "(TS=%s,M=%s%d,W=%s%d)\n",
194	    srcname, ntohs(tcp->th_sport),
195	    fp.fp_wsize, fp.fp_ttl, (fp.fp_flags & PF_OSFP_DF) != 0,
196	    fp.fp_psize, (long long int)fp.fp_tcpopts, fp.fp_optcnt,
197	    (fp.fp_flags & PF_OSFP_TS0) ? "0" : "",
198	    (fp.fp_flags & PF_OSFP_MSS_MOD) ? "%" :
199	    (fp.fp_flags & PF_OSFP_MSS_DC) ? "*" : "",
200	    fp.fp_mss,
201	    (fp.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" :
202	    (fp.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "",
203	    fp.fp_wscale);
204
205	if ((fpresult = pf_osfp_find(&V_pf_osfp_list, &fp,
206	    PF_OSFP_MAXTTL_OFFSET)))
207		return (&fpresult->fp_oses);
208	return (NULL);
209}
210
211/* Match a fingerprint ID against a list of OSes */
212int
213pf_osfp_match(struct pf_osfp_enlist *list, pf_osfp_t os)
214{
215	struct pf_osfp_entry *entry;
216	int os_class, os_version, os_subtype;
217	int en_class, en_version, en_subtype;
218
219	if (os == PF_OSFP_ANY)
220		return (1);
221	if (list == NULL) {
222		DPFPRINTF("osfp no match against %x\n", os);
223		return (os == PF_OSFP_UNKNOWN);
224	}
225	PF_OSFP_UNPACK(os, os_class, os_version, os_subtype);
226	SLIST_FOREACH(entry, list, fp_entry) {
227		PF_OSFP_UNPACK(entry->fp_os, en_class, en_version, en_subtype);
228		if ((os_class == PF_OSFP_ANY || en_class == os_class) &&
229		    (os_version == PF_OSFP_ANY || en_version == os_version) &&
230		    (os_subtype == PF_OSFP_ANY || en_subtype == os_subtype)) {
231			DPFPRINTF("osfp matched %s %s %s  %x==%x\n",
232			    entry->fp_class_nm, entry->fp_version_nm,
233			    entry->fp_subtype_nm, os, entry->fp_os);
234			return (1);
235		}
236	}
237	DPFPRINTF("fingerprint 0x%x didn't match\n", os);
238	return (0);
239}
240
241/* Flush the fingerprint list */
242void
243pf_osfp_flush(void)
244{
245	struct pf_os_fingerprint *fp;
246	struct pf_osfp_entry *entry;
247
248	while ((fp = SLIST_FIRST(&V_pf_osfp_list))) {
249		SLIST_REMOVE_HEAD(&V_pf_osfp_list, fp_next);
250		while ((entry = SLIST_FIRST(&fp->fp_oses))) {
251			SLIST_REMOVE_HEAD(&fp->fp_oses, fp_entry);
252			free(entry, M_PFOSFP);
253		}
254		free(fp, M_PFOSFP);
255	}
256}
257
258/* Add a fingerprint */
259int
260pf_osfp_add(struct pf_osfp_ioctl *fpioc)
261{
262	struct pf_os_fingerprint *fp, fpadd;
263	struct pf_osfp_entry *entry;
264
265	PF_RULES_WASSERT();
266
267	memset(&fpadd, 0, sizeof(fpadd));
268	fpadd.fp_tcpopts = fpioc->fp_tcpopts;
269	fpadd.fp_wsize = fpioc->fp_wsize;
270	fpadd.fp_psize = fpioc->fp_psize;
271	fpadd.fp_mss = fpioc->fp_mss;
272	fpadd.fp_flags = fpioc->fp_flags;
273	fpadd.fp_optcnt = fpioc->fp_optcnt;
274	fpadd.fp_wscale = fpioc->fp_wscale;
275	fpadd.fp_ttl = fpioc->fp_ttl;
276
277#if 0	/* XXX RYAN wants to fix logging */
278	DPFPRINTF("adding osfp %s %s %s = %s%d:%d:%d:%s%d:0x%llx %d "
279	    "(TS=%s,M=%s%d,W=%s%d) %x\n",
280	    fpioc->fp_os.fp_class_nm, fpioc->fp_os.fp_version_nm,
281	    fpioc->fp_os.fp_subtype_nm,
282	    (fpadd.fp_flags & PF_OSFP_WSIZE_MOD) ? "%" :
283	    (fpadd.fp_flags & PF_OSFP_WSIZE_MSS) ? "S" :
284	    (fpadd.fp_flags & PF_OSFP_WSIZE_MTU) ? "T" :
285	    (fpadd.fp_flags & PF_OSFP_WSIZE_DC) ? "*" : "",
286	    fpadd.fp_wsize,
287	    fpadd.fp_ttl,
288	    (fpadd.fp_flags & PF_OSFP_DF) ? 1 : 0,
289	    (fpadd.fp_flags & PF_OSFP_PSIZE_MOD) ? "%" :
290	    (fpadd.fp_flags & PF_OSFP_PSIZE_DC) ? "*" : "",
291	    fpadd.fp_psize,
292	    (long long int)fpadd.fp_tcpopts, fpadd.fp_optcnt,
293	    (fpadd.fp_flags & PF_OSFP_TS0) ? "0" : "",
294	    (fpadd.fp_flags & PF_OSFP_MSS_MOD) ? "%" :
295	    (fpadd.fp_flags & PF_OSFP_MSS_DC) ? "*" : "",
296	    fpadd.fp_mss,
297	    (fpadd.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" :
298	    (fpadd.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "",
299	    fpadd.fp_wscale,
300	    fpioc->fp_os.fp_os);
301#endif
302
303	if ((fp = pf_osfp_find_exact(&V_pf_osfp_list, &fpadd))) {
304		 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
305			if (PF_OSFP_ENTRY_EQ(entry, &fpioc->fp_os))
306				return (EEXIST);
307		}
308		if ((entry = malloc(sizeof(*entry), M_PFOSFP, M_NOWAIT))
309		    == NULL)
310			return (ENOMEM);
311	} else {
312		if ((fp = malloc(sizeof(*fp), M_PFOSFP, M_ZERO | M_NOWAIT))
313		    == NULL)
314			return (ENOMEM);
315		fp->fp_tcpopts = fpioc->fp_tcpopts;
316		fp->fp_wsize = fpioc->fp_wsize;
317		fp->fp_psize = fpioc->fp_psize;
318		fp->fp_mss = fpioc->fp_mss;
319		fp->fp_flags = fpioc->fp_flags;
320		fp->fp_optcnt = fpioc->fp_optcnt;
321		fp->fp_wscale = fpioc->fp_wscale;
322		fp->fp_ttl = fpioc->fp_ttl;
323		SLIST_INIT(&fp->fp_oses);
324		if ((entry = malloc(sizeof(*entry), M_PFOSFP, M_NOWAIT))
325		    == NULL) {
326			free(fp, M_PFOSFP);
327			return (ENOMEM);
328		}
329		pf_osfp_insert(&V_pf_osfp_list, fp);
330	}
331	memcpy(entry, &fpioc->fp_os, sizeof(*entry));
332
333	/* Make sure the strings are NUL terminated */
334	entry->fp_class_nm[sizeof(entry->fp_class_nm)-1] = '\0';
335	entry->fp_version_nm[sizeof(entry->fp_version_nm)-1] = '\0';
336	entry->fp_subtype_nm[sizeof(entry->fp_subtype_nm)-1] = '\0';
337
338	SLIST_INSERT_HEAD(&fp->fp_oses, entry, fp_entry);
339
340#ifdef PFDEBUG
341	if ((fp = pf_osfp_validate()))
342		printf("Invalid fingerprint list\n");
343#endif /* PFDEBUG */
344	return (0);
345}
346
347/* Find a fingerprint in the list */
348static struct pf_os_fingerprint *
349pf_osfp_find(struct pf_osfp_list *list, struct pf_os_fingerprint *find,
350    u_int8_t ttldiff)
351{
352	struct pf_os_fingerprint *f;
353
354#define	MATCH_INT(_MOD, _DC, _field)					\
355	if ((f->fp_flags & _DC) == 0) {					\
356		if ((f->fp_flags & _MOD) == 0) {			\
357			if (f->_field != find->_field)			\
358				continue;				\
359		} else {						\
360			if (f->_field == 0 || find->_field % f->_field)	\
361				continue;				\
362		}							\
363	}
364
365	SLIST_FOREACH(f, list, fp_next) {
366		if (f->fp_tcpopts != find->fp_tcpopts ||
367		    f->fp_optcnt != find->fp_optcnt ||
368		    f->fp_ttl < find->fp_ttl ||
369		    f->fp_ttl - find->fp_ttl > ttldiff ||
370		    (f->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)) !=
371		    (find->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)))
372			continue;
373
374		MATCH_INT(PF_OSFP_PSIZE_MOD, PF_OSFP_PSIZE_DC, fp_psize)
375		MATCH_INT(PF_OSFP_MSS_MOD, PF_OSFP_MSS_DC, fp_mss)
376		MATCH_INT(PF_OSFP_WSCALE_MOD, PF_OSFP_WSCALE_DC, fp_wscale)
377		if ((f->fp_flags & PF_OSFP_WSIZE_DC) == 0) {
378			if (f->fp_flags & PF_OSFP_WSIZE_MSS) {
379				if (find->fp_mss == 0)
380					continue;
381
382/*
383 * Some "smart" NAT devices and DSL routers will tweak the MSS size and
384 * will set it to whatever is suitable for the link type.
385 */
386#define	SMART_MSS	1460
387				if ((find->fp_wsize % find->fp_mss ||
388				    find->fp_wsize / find->fp_mss !=
389				    f->fp_wsize) &&
390				    (find->fp_wsize % SMART_MSS ||
391				    find->fp_wsize / SMART_MSS !=
392				    f->fp_wsize))
393					continue;
394			} else if (f->fp_flags & PF_OSFP_WSIZE_MTU) {
395				if (find->fp_mss == 0)
396					continue;
397
398#define	MTUOFF		(sizeof(struct ip) + sizeof(struct tcphdr))
399#define	SMART_MTU	(SMART_MSS + MTUOFF)
400				if ((find->fp_wsize % (find->fp_mss + MTUOFF) ||
401				    find->fp_wsize / (find->fp_mss + MTUOFF) !=
402				    f->fp_wsize) &&
403				    (find->fp_wsize % SMART_MTU ||
404				    find->fp_wsize / SMART_MTU !=
405				    f->fp_wsize))
406					continue;
407			} else if (f->fp_flags & PF_OSFP_WSIZE_MOD) {
408				if (f->fp_wsize == 0 || find->fp_wsize %
409				    f->fp_wsize)
410					continue;
411			} else {
412				if (f->fp_wsize != find->fp_wsize)
413					continue;
414			}
415		}
416		return (f);
417	}
418
419	return (NULL);
420}
421
422/* Find an exact fingerprint in the list */
423static struct pf_os_fingerprint *
424pf_osfp_find_exact(struct pf_osfp_list *list, struct pf_os_fingerprint *find)
425{
426	struct pf_os_fingerprint *f;
427
428	SLIST_FOREACH(f, list, fp_next) {
429		if (f->fp_tcpopts == find->fp_tcpopts &&
430		    f->fp_wsize == find->fp_wsize &&
431		    f->fp_psize == find->fp_psize &&
432		    f->fp_mss == find->fp_mss &&
433		    f->fp_flags == find->fp_flags &&
434		    f->fp_optcnt == find->fp_optcnt &&
435		    f->fp_wscale == find->fp_wscale &&
436		    f->fp_ttl == find->fp_ttl)
437			return (f);
438	}
439
440	return (NULL);
441}
442
443/* Insert a fingerprint into the list */
444static void
445pf_osfp_insert(struct pf_osfp_list *list, struct pf_os_fingerprint *ins)
446{
447	struct pf_os_fingerprint *f, *prev = NULL;
448
449	/* XXX need to go semi tree based.  can key on tcp options */
450
451	SLIST_FOREACH(f, list, fp_next)
452		prev = f;
453	if (prev)
454		SLIST_INSERT_AFTER(prev, ins, fp_next);
455	else
456		SLIST_INSERT_HEAD(list, ins, fp_next);
457}
458
459/* Fill a fingerprint by its number (from an ioctl) */
460int
461pf_osfp_get(struct pf_osfp_ioctl *fpioc)
462{
463	struct pf_os_fingerprint *fp;
464	struct pf_osfp_entry *entry;
465	int num = fpioc->fp_getnum;
466	int i = 0;
467
468	memset(fpioc, 0, sizeof(*fpioc));
469	SLIST_FOREACH(fp, &V_pf_osfp_list, fp_next) {
470		SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
471			if (i++ == num) {
472				fpioc->fp_mss = fp->fp_mss;
473				fpioc->fp_wsize = fp->fp_wsize;
474				fpioc->fp_flags = fp->fp_flags;
475				fpioc->fp_psize = fp->fp_psize;
476				fpioc->fp_ttl = fp->fp_ttl;
477				fpioc->fp_wscale = fp->fp_wscale;
478				fpioc->fp_getnum = num;
479				memcpy(&fpioc->fp_os, entry,
480				    sizeof(fpioc->fp_os));
481				return (0);
482			}
483		}
484	}
485
486	return (EBUSY);
487}
488
489#ifdef PFDEBUG
490/* Validate that each signature is reachable */
491static struct pf_os_fingerprint *
492pf_osfp_validate(void)
493{
494	struct pf_os_fingerprint *f, *f2, find;
495
496	SLIST_FOREACH(f, &V_pf_osfp_list, fp_next) {
497		memcpy(&find, f, sizeof(find));
498
499		/* We do a few MSS/th_win percolations to make things unique */
500		if (find.fp_mss == 0)
501			find.fp_mss = 128;
502		if (f->fp_flags & PF_OSFP_WSIZE_MSS)
503			find.fp_wsize *= find.fp_mss;
504		else if (f->fp_flags & PF_OSFP_WSIZE_MTU)
505			find.fp_wsize *= (find.fp_mss + 40);
506		else if (f->fp_flags & PF_OSFP_WSIZE_MOD)
507			find.fp_wsize *= 2;
508		if (f != (f2 = pf_osfp_find(&V_pf_osfp_list, &find, 0))) {
509			if (f2)
510				printf("Found \"%s %s %s\" instead of "
511				    "\"%s %s %s\"\n",
512				    SLIST_FIRST(&f2->fp_oses)->fp_class_nm,
513				    SLIST_FIRST(&f2->fp_oses)->fp_version_nm,
514				    SLIST_FIRST(&f2->fp_oses)->fp_subtype_nm,
515				    SLIST_FIRST(&f->fp_oses)->fp_class_nm,
516				    SLIST_FIRST(&f->fp_oses)->fp_version_nm,
517				    SLIST_FIRST(&f->fp_oses)->fp_subtype_nm);
518			else
519				printf("Couldn't find \"%s %s %s\"\n",
520				    SLIST_FIRST(&f->fp_oses)->fp_class_nm,
521				    SLIST_FIRST(&f->fp_oses)->fp_version_nm,
522				    SLIST_FIRST(&f->fp_oses)->fp_subtype_nm);
523			return (f);
524		}
525	}
526	return (NULL);
527}
528#endif /* PFDEBUG */
529