xref: /openbsd-src/sys/net/pf_osfp.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: pf_osfp.c,v 1.28 2013/11/11 09:15:34 mpi 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/pool.h>
25 #endif /* _KERNEL */
26 #include <sys/mbuf.h>
27 #include <sys/syslog.h>
28 
29 #include <netinet/in.h>
30 #include <netinet/in_systm.h>
31 #include <netinet/ip.h>
32 #include <netinet/tcp.h>
33 
34 #include <net/if.h>
35 #include <net/pfvar.h>
36 
37 #include <netinet/ip6.h>
38 
39 
40 #ifdef _KERNEL
41 typedef struct pool pool_t;
42 
43 #else
44 /* Userland equivalents so we can lend code to tcpdump et al. */
45 
46 # include <arpa/inet.h>
47 # include <errno.h>
48 # include <stdio.h>
49 # include <stdlib.h>
50 # include <string.h>
51 # include <netdb.h>
52 # define pool_t			int
53 # define pool_get(pool, flags)	malloc(*(pool))
54 # define pool_put(pool, item)	free(item)
55 # define pool_init(pool, size, a, ao, f, m, p)	(*(pool)) = (size)
56 
57 # ifdef PFDEBUG
58 #  include <sys/stdarg.h>	/* for DPFPRINTF() */
59 # endif /* PFDEBUG */
60 
61 #endif /* _KERNEL */
62 
63 
64 SLIST_HEAD(pf_osfp_list, pf_os_fingerprint) pf_osfp_list;
65 pool_t pf_osfp_entry_pl;
66 pool_t pf_osfp_pl;
67 
68 struct pf_os_fingerprint	*pf_osfp_find(struct pf_osfp_list *,
69 				    struct pf_os_fingerprint *, u_int8_t);
70 struct pf_os_fingerprint	*pf_osfp_find_exact(struct pf_osfp_list *,
71 				    struct pf_os_fingerprint *);
72 void				 pf_osfp_insert(struct pf_osfp_list *,
73 				    struct pf_os_fingerprint *);
74 
75 
76 #ifdef _KERNEL
77 /*
78  * Passively fingerprint the OS of the host (IPv4 TCP SYN packets only)
79  * Returns the list of possible OSes.
80  */
81 struct pf_osfp_enlist *
82 pf_osfp_fingerprint(struct pf_pdesc *pd)
83 {
84 	struct tcphdr	*th = pd->hdr.tcp;
85 	struct ip	*ip = NULL;
86 	struct ip6_hdr	*ip6 = NULL;
87 	char		 hdr[60];
88 
89 	if (pd->proto != IPPROTO_TCP)
90 		return (NULL);
91 
92 	switch (pd->af) {
93 	case AF_INET:
94 		ip = mtod(pd->m, struct ip *);
95 		break;
96 	case AF_INET6:
97 		ip6 = mtod(pd->m, struct ip6_hdr *);
98 		break;
99 	}
100 	if (!pf_pull_hdr(pd->m, pd->off, hdr, th->th_off << 2, NULL, NULL,
101 	    pd->af))
102 		return (NULL);
103 
104 	return (pf_osfp_fingerprint_hdr(ip, ip6, (struct tcphdr *)hdr));
105 }
106 #endif /* _KERNEL */
107 
108 struct pf_osfp_enlist *
109 pf_osfp_fingerprint_hdr(const struct ip *ip, const struct ip6_hdr *ip6,
110     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 & htons(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 = ntohs(ip->ip_len);
136 		fp.fp_ttl = ip->ip_ttl;
137 		if (ip->ip_off & htons(IP_DF))
138 			fp.fp_flags |= PF_OSFP_DF;
139 #ifdef _KERNEL
140 		inet_ntop(AF_INET, &ip->ip_src, srcname, sizeof(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 		inet_ntop(AF_INET6, &ip6->ip6_src, srcname, sizeof(srcname));
164 #else
165 		memset(&sin6, 0, sizeof(sin6));
166 		sin6.sin6_family = AF_INET6;
167 		sin6.sin6_len = sizeof(struct sockaddr_in6);
168 		sin6.sin6_addr = ip6->ip6_src;
169 		(void)getnameinfo((struct sockaddr *)&sin6,
170 		    sizeof(struct sockaddr_in6), srcname, sizeof(srcname),
171 		    NULL, 0, NI_NUMERICHOST);
172 #endif
173 	}
174 #endif
175 	else
176 		return (NULL);
177 	fp.fp_wsize = ntohs(tcp->th_win);
178 
179 
180 	cnt = (tcp->th_off << 2) - sizeof(*tcp);
181 	optp = (const u_int8_t *)((const char *)tcp + sizeof(*tcp));
182 	for (; cnt > 0; cnt -= optlen, optp += optlen) {
183 		if (*optp == TCPOPT_EOL)
184 			break;
185 
186 		fp.fp_optcnt++;
187 		if (*optp == TCPOPT_NOP) {
188 			fp.fp_tcpopts = (fp.fp_tcpopts << PF_OSFP_TCPOPT_BITS) |
189 			    PF_OSFP_TCPOPT_NOP;
190 			optlen = 1;
191 		} else {
192 			if (cnt < 2)
193 				return (NULL);
194 			optlen = optp[1];
195 			if (optlen > cnt || optlen < 2)
196 				return (NULL);
197 			switch (*optp) {
198 			case TCPOPT_MAXSEG:
199 				if (optlen >= TCPOLEN_MAXSEG)
200 					memcpy(&fp.fp_mss, &optp[2],
201 					    sizeof(fp.fp_mss));
202 				fp.fp_tcpopts = (fp.fp_tcpopts <<
203 				    PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_MSS;
204 				NTOHS(fp.fp_mss);
205 				break;
206 			case TCPOPT_WINDOW:
207 				if (optlen >= TCPOLEN_WINDOW)
208 					memcpy(&fp.fp_wscale, &optp[2],
209 					    sizeof(fp.fp_wscale));
210 				fp.fp_tcpopts = (fp.fp_tcpopts <<
211 				    PF_OSFP_TCPOPT_BITS) |
212 				    PF_OSFP_TCPOPT_WSCALE;
213 				break;
214 			case TCPOPT_SACK_PERMITTED:
215 				fp.fp_tcpopts = (fp.fp_tcpopts <<
216 				    PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_SACK;
217 				break;
218 			case TCPOPT_TIMESTAMP:
219 				if (optlen >= TCPOLEN_TIMESTAMP) {
220 					u_int32_t ts;
221 					memcpy(&ts, &optp[2], sizeof(ts));
222 					if (ts == 0)
223 						fp.fp_flags |= PF_OSFP_TS0;
224 
225 				}
226 				fp.fp_tcpopts = (fp.fp_tcpopts <<
227 				    PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_TS;
228 				break;
229 			default:
230 				return (NULL);
231 			}
232 		}
233 		optlen = MAX(optlen, 1);	/* paranoia */
234 	}
235 
236 	DPFPRINTF(LOG_NOTICE,
237 	    "fingerprinted %s:%d  %d:%d:%d:%d:%llx (%d) "
238 	    "(TS=%s,M=%s%d,W=%s%d)",
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(LOG_NOTICE, "osfp no match against %x", 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(LOG_NOTICE,
277 			    "osfp matched %s %s %s  %x==%x",
278 			    entry->fp_class_nm, entry->fp_version_nm,
279 			    entry->fp_subtype_nm, os, entry->fp_os);
280 			return (1);
281 		}
282 	}
283 	DPFPRINTF(LOG_NOTICE, "fingerprint 0x%x didn't match", os);
284 	return (0);
285 }
286 
287 /* Initialize the OS fingerprint system */
288 void
289 pf_osfp_initialize(void)
290 {
291 	pool_init(&pf_osfp_entry_pl, sizeof(struct pf_osfp_entry), 0, 0, 0,
292 	    "pfosfpen", &pool_allocator_nointr);
293 	pool_init(&pf_osfp_pl, sizeof(struct pf_os_fingerprint), 0, 0, 0,
294 	    "pfosfp", &pool_allocator_nointr);
295 	SLIST_INIT(&pf_osfp_list);
296 }
297 
298 /* Flush the fingerprint list */
299 void
300 pf_osfp_flush(void)
301 {
302 	struct pf_os_fingerprint *fp;
303 	struct pf_osfp_entry *entry;
304 
305 	while ((fp = SLIST_FIRST(&pf_osfp_list))) {
306 		SLIST_REMOVE_HEAD(&pf_osfp_list, fp_next);
307 		while ((entry = SLIST_FIRST(&fp->fp_oses))) {
308 			SLIST_REMOVE_HEAD(&fp->fp_oses, fp_entry);
309 			pool_put(&pf_osfp_entry_pl, entry);
310 		}
311 		pool_put(&pf_osfp_pl, fp);
312 	}
313 }
314 
315 
316 /* Add a fingerprint */
317 int
318 pf_osfp_add(struct pf_osfp_ioctl *fpioc)
319 {
320 	struct pf_os_fingerprint *fp, fpadd;
321 	struct pf_osfp_entry *entry;
322 
323 	memset(&fpadd, 0, sizeof(fpadd));
324 	fpadd.fp_tcpopts = fpioc->fp_tcpopts;
325 	fpadd.fp_wsize = fpioc->fp_wsize;
326 	fpadd.fp_psize = fpioc->fp_psize;
327 	fpadd.fp_mss = fpioc->fp_mss;
328 	fpadd.fp_flags = fpioc->fp_flags;
329 	fpadd.fp_optcnt = fpioc->fp_optcnt;
330 	fpadd.fp_wscale = fpioc->fp_wscale;
331 	fpadd.fp_ttl = fpioc->fp_ttl;
332 
333 	DPFPRINTF(LOG_DEBUG,
334 	    "adding osfp %s %s %s = %s%d:%d:%d:%s%d:0x%llx %d "
335 	    "(TS=%s,M=%s%d,W=%s%d) %x",
336 	    fpioc->fp_os.fp_class_nm, fpioc->fp_os.fp_version_nm,
337 	    fpioc->fp_os.fp_subtype_nm,
338 	    (fpadd.fp_flags & PF_OSFP_WSIZE_MOD) ? "%" :
339 	    (fpadd.fp_flags & PF_OSFP_WSIZE_MSS) ? "S" :
340 	    (fpadd.fp_flags & PF_OSFP_WSIZE_MTU) ? "T" :
341 	    (fpadd.fp_flags & PF_OSFP_WSIZE_DC) ? "*" : "",
342 	    fpadd.fp_wsize,
343 	    fpadd.fp_ttl,
344 	    (fpadd.fp_flags & PF_OSFP_DF) ? 1 : 0,
345 	    (fpadd.fp_flags & PF_OSFP_PSIZE_MOD) ? "%" :
346 	    (fpadd.fp_flags & PF_OSFP_PSIZE_DC) ? "*" : "",
347 	    fpadd.fp_psize,
348 	    (long long int)fpadd.fp_tcpopts, fpadd.fp_optcnt,
349 	    (fpadd.fp_flags & PF_OSFP_TS0) ? "0" : "",
350 	    (fpadd.fp_flags & PF_OSFP_MSS_MOD) ? "%" :
351 	    (fpadd.fp_flags & PF_OSFP_MSS_DC) ? "*" : "",
352 	    fpadd.fp_mss,
353 	    (fpadd.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" :
354 	    (fpadd.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "",
355 	    fpadd.fp_wscale,
356 	    fpioc->fp_os.fp_os);
357 
358 	if ((fp = pf_osfp_find_exact(&pf_osfp_list, &fpadd))) {
359 		 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
360 			if (PF_OSFP_ENTRY_EQ(entry, &fpioc->fp_os))
361 				return (EEXIST);
362 		}
363 		if ((entry = pool_get(&pf_osfp_entry_pl,
364 		    PR_WAITOK|PR_LIMITFAIL)) == NULL)
365 			return (ENOMEM);
366 	} else {
367 		if ((fp = pool_get(&pf_osfp_pl,
368 		    PR_WAITOK|PR_ZERO|PR_LIMITFAIL)) == NULL)
369 			return (ENOMEM);
370 		fp->fp_tcpopts = fpioc->fp_tcpopts;
371 		fp->fp_wsize = fpioc->fp_wsize;
372 		fp->fp_psize = fpioc->fp_psize;
373 		fp->fp_mss = fpioc->fp_mss;
374 		fp->fp_flags = fpioc->fp_flags;
375 		fp->fp_optcnt = fpioc->fp_optcnt;
376 		fp->fp_wscale = fpioc->fp_wscale;
377 		fp->fp_ttl = fpioc->fp_ttl;
378 		SLIST_INIT(&fp->fp_oses);
379 		if ((entry = pool_get(&pf_osfp_entry_pl,
380 		    PR_WAITOK|PR_LIMITFAIL)) == NULL) {
381 			pool_put(&pf_osfp_pl, fp);
382 			return (ENOMEM);
383 		}
384 		pf_osfp_insert(&pf_osfp_list, fp);
385 	}
386 	memcpy(entry, &fpioc->fp_os, sizeof(*entry));
387 
388 	/* Make sure the strings are NUL terminated */
389 	entry->fp_class_nm[sizeof(entry->fp_class_nm)-1] = '\0';
390 	entry->fp_version_nm[sizeof(entry->fp_version_nm)-1] = '\0';
391 	entry->fp_subtype_nm[sizeof(entry->fp_subtype_nm)-1] = '\0';
392 
393 	SLIST_INSERT_HEAD(&fp->fp_oses, entry, fp_entry);
394 
395 #ifdef PFDEBUG
396 	if ((fp = pf_osfp_validate()))
397 		DPFPRINTF(LOG_NOTICE,
398 		    "Invalid fingerprint list");
399 #endif /* PFDEBUG */
400 	return (0);
401 }
402 
403 
404 /* Find a fingerprint in the list */
405 struct pf_os_fingerprint *
406 pf_osfp_find(struct pf_osfp_list *list, struct pf_os_fingerprint *find,
407     u_int8_t ttldiff)
408 {
409 	struct pf_os_fingerprint *f;
410 
411 #define MATCH_INT(_MOD, _DC, _field)					\
412 	if ((f->fp_flags & _DC) == 0) {					\
413 		if ((f->fp_flags & _MOD) == 0) {			\
414 			if (f->_field != find->_field)			\
415 				continue;				\
416 		} else {						\
417 			if (f->_field == 0 || find->_field % f->_field)	\
418 				continue;				\
419 		}							\
420 	}
421 
422 	SLIST_FOREACH(f, list, fp_next) {
423 		if (f->fp_tcpopts != find->fp_tcpopts ||
424 		    f->fp_optcnt != find->fp_optcnt ||
425 		    f->fp_ttl < find->fp_ttl ||
426 		    f->fp_ttl - find->fp_ttl > ttldiff ||
427 		    (f->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)) !=
428 		    (find->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)))
429 			continue;
430 
431 		MATCH_INT(PF_OSFP_PSIZE_MOD, PF_OSFP_PSIZE_DC, fp_psize)
432 		MATCH_INT(PF_OSFP_MSS_MOD, PF_OSFP_MSS_DC, fp_mss)
433 		MATCH_INT(PF_OSFP_WSCALE_MOD, PF_OSFP_WSCALE_DC, fp_wscale)
434 		if ((f->fp_flags & PF_OSFP_WSIZE_DC) == 0) {
435 			if (f->fp_flags & PF_OSFP_WSIZE_MSS) {
436 				if (find->fp_mss == 0)
437 					continue;
438 
439 /* Some "smart" NAT devices and DSL routers will tweak the MSS size and
440  * will set it to whatever is suitable for the link type.
441  */
442 #define SMART_MSS	1460
443 				if ((find->fp_wsize % find->fp_mss ||
444 				    find->fp_wsize / find->fp_mss !=
445 				    f->fp_wsize) &&
446 				    (find->fp_wsize % SMART_MSS ||
447 				    find->fp_wsize / SMART_MSS !=
448 				    f->fp_wsize))
449 					continue;
450 			} else if (f->fp_flags & PF_OSFP_WSIZE_MTU) {
451 				if (find->fp_mss == 0)
452 					continue;
453 
454 #define MTUOFF	(sizeof(struct ip) + sizeof(struct tcphdr))
455 #define SMART_MTU	(SMART_MSS + MTUOFF)
456 				if ((find->fp_wsize % (find->fp_mss + MTUOFF) ||
457 				    find->fp_wsize / (find->fp_mss + MTUOFF) !=
458 				    f->fp_wsize) &&
459 				    (find->fp_wsize % SMART_MTU ||
460 				    find->fp_wsize / SMART_MTU !=
461 				    f->fp_wsize))
462 					continue;
463 			} else if (f->fp_flags & PF_OSFP_WSIZE_MOD) {
464 				if (f->fp_wsize == 0 || find->fp_wsize %
465 				    f->fp_wsize)
466 					continue;
467 			} else {
468 				if (f->fp_wsize != find->fp_wsize)
469 					continue;
470 			}
471 		}
472 		return (f);
473 	}
474 
475 	return (NULL);
476 }
477 
478 /* Find an exact fingerprint in the list */
479 struct pf_os_fingerprint *
480 pf_osfp_find_exact(struct pf_osfp_list *list, struct pf_os_fingerprint *find)
481 {
482 	struct pf_os_fingerprint *f;
483 
484 	SLIST_FOREACH(f, list, fp_next) {
485 		if (f->fp_tcpopts == find->fp_tcpopts &&
486 		    f->fp_wsize == find->fp_wsize &&
487 		    f->fp_psize == find->fp_psize &&
488 		    f->fp_mss == find->fp_mss &&
489 		    f->fp_flags == find->fp_flags &&
490 		    f->fp_optcnt == find->fp_optcnt &&
491 		    f->fp_wscale == find->fp_wscale &&
492 		    f->fp_ttl == find->fp_ttl)
493 			return (f);
494 	}
495 
496 	return (NULL);
497 }
498 
499 /* Insert a fingerprint into the list */
500 void
501 pf_osfp_insert(struct pf_osfp_list *list, struct pf_os_fingerprint *ins)
502 {
503 	struct pf_os_fingerprint *f, *prev = NULL;
504 
505 	/* XXX need to go semi tree based.  can key on tcp options */
506 
507 	SLIST_FOREACH(f, list, fp_next)
508 		prev = f;
509 	if (prev)
510 		SLIST_INSERT_AFTER(prev, ins, fp_next);
511 	else
512 		SLIST_INSERT_HEAD(list, ins, fp_next);
513 }
514 
515 /* Fill a fingerprint by its number (from an ioctl) */
516 int
517 pf_osfp_get(struct pf_osfp_ioctl *fpioc)
518 {
519 	struct pf_os_fingerprint *fp;
520 	struct pf_osfp_entry *entry;
521 	int num = fpioc->fp_getnum;
522 	int i = 0;
523 
524 
525 	memset(fpioc, 0, sizeof(*fpioc));
526 	SLIST_FOREACH(fp, &pf_osfp_list, fp_next) {
527 		SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
528 			if (i++ == num) {
529 				fpioc->fp_mss = fp->fp_mss;
530 				fpioc->fp_wsize = fp->fp_wsize;
531 				fpioc->fp_flags = fp->fp_flags;
532 				fpioc->fp_psize = fp->fp_psize;
533 				fpioc->fp_ttl = fp->fp_ttl;
534 				fpioc->fp_wscale = fp->fp_wscale;
535 				fpioc->fp_getnum = num;
536 				memcpy(&fpioc->fp_os, entry,
537 				    sizeof(fpioc->fp_os));
538 				return (0);
539 			}
540 		}
541 	}
542 
543 	return (EBUSY);
544 }
545 
546 
547 /* Validate that each signature is reachable */
548 struct pf_os_fingerprint *
549 pf_osfp_validate(void)
550 {
551 	struct pf_os_fingerprint *f, *f2, find;
552 
553 	SLIST_FOREACH(f, &pf_osfp_list, fp_next) {
554 		memcpy(&find, f, sizeof(find));
555 
556 		/* We do a few MSS/th_win percolations to make things unique */
557 		if (find.fp_mss == 0)
558 			find.fp_mss = 128;
559 		if (f->fp_flags & PF_OSFP_WSIZE_MSS)
560 			find.fp_wsize *= find.fp_mss;
561 		else if (f->fp_flags & PF_OSFP_WSIZE_MTU)
562 			find.fp_wsize *= (find.fp_mss + 40);
563 		else if (f->fp_flags & PF_OSFP_WSIZE_MOD)
564 			find.fp_wsize *= 2;
565 		if (f != (f2 = pf_osfp_find(&pf_osfp_list, &find, 0))) {
566 			if (f2)
567 				DPFPRINTF(LOG_NOTICE,
568 				    "Found \"%s %s %s\" instead of "
569 				    "\"%s %s %s\"\n",
570 				    SLIST_FIRST(&f2->fp_oses)->fp_class_nm,
571 				    SLIST_FIRST(&f2->fp_oses)->fp_version_nm,
572 				    SLIST_FIRST(&f2->fp_oses)->fp_subtype_nm,
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 			else
577 				DPFPRINTF(LOG_NOTICE,
578 				    "Couldn't find \"%s %s %s\"\n",
579 				    SLIST_FIRST(&f->fp_oses)->fp_class_nm,
580 				    SLIST_FIRST(&f->fp_oses)->fp_version_nm,
581 				    SLIST_FIRST(&f->fp_oses)->fp_subtype_nm);
582 			return (f);
583 		}
584 	}
585 	return (NULL);
586 }
587