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