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