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