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