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