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