xref: /dragonfly/sys/net/pf/pf_osfp.c (revision 63e667b8)
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 & htons(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 = ntohs(ip->ip_len);
140 		fp.fp_ttl = ip->ip_ttl;
141 		if (ip->ip_off & htons(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 				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 				NTOHS(fp.fp_wscale);
216 				fp.fp_tcpopts = (fp.fp_tcpopts <<
217 				    PF_OSFP_TCPOPT_BITS) |
218 				    PF_OSFP_TCPOPT_WSCALE;
219 				break;
220 			case TCPOPT_SACK_PERMITTED:
221 				fp.fp_tcpopts = (fp.fp_tcpopts <<
222 				    PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_SACK;
223 				break;
224 			case TCPOPT_TIMESTAMP:
225 				if (optlen >= TCPOLEN_TIMESTAMP) {
226 					u_int32_t ts;
227 					memcpy(&ts, &optp[2], sizeof(ts));
228 					if (ts == 0)
229 						fp.fp_flags |= PF_OSFP_TS0;
230 
231 				}
232 				fp.fp_tcpopts = (fp.fp_tcpopts <<
233 				    PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_TS;
234 				break;
235 			default:
236 				return (NULL);
237 			}
238 		}
239 		optlen = MAX(optlen, 1);	/* paranoia */
240 	}
241 
242 	DPFPRINTF("fingerprinted %s:%d  %d:%d:%d:%d:%llx (%d) "
243 	    "(TS=%s,M=%s%d,W=%s%d)\n",
244 	    srcname, ntohs(tcp->th_sport),
245 	    fp.fp_wsize, fp.fp_ttl, (fp.fp_flags & PF_OSFP_DF) != 0,
246 	    fp.fp_psize, (long long int)fp.fp_tcpopts, fp.fp_optcnt,
247 	    (fp.fp_flags & PF_OSFP_TS0) ? "0" : "",
248 	    (fp.fp_flags & PF_OSFP_MSS_MOD) ? "%" :
249 	    (fp.fp_flags & PF_OSFP_MSS_DC) ? "*" : "",
250 	    fp.fp_mss,
251 	    (fp.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" :
252 	    (fp.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "",
253 	    fp.fp_wscale);
254 
255 	if ((fpresult = pf_osfp_find(&pf_osfp_list, &fp,
256 	    PF_OSFP_MAXTTL_OFFSET)))
257 		return (&fpresult->fp_oses);
258 	return (NULL);
259 }
260 
261 /* Match a fingerprint ID against a list of OSes */
262 int
263 pf_osfp_match(struct pf_osfp_enlist *list, pf_osfp_t os)
264 {
265 	struct pf_osfp_entry *entry;
266 	int os_class, os_version, os_subtype;
267 	int en_class, en_version, en_subtype;
268 
269 	if (os == PF_OSFP_ANY)
270 		return (1);
271 	if (list == NULL) {
272 		DPFPRINTF("osfp no match against %x\n", os);
273 		return (os == PF_OSFP_UNKNOWN);
274 	}
275 	PF_OSFP_UNPACK(os, os_class, os_version, os_subtype);
276 	SLIST_FOREACH(entry, list, fp_entry) {
277 		PF_OSFP_UNPACK(entry->fp_os, en_class, en_version, en_subtype);
278 		if ((os_class == PF_OSFP_ANY || en_class == os_class) &&
279 		    (os_version == PF_OSFP_ANY || en_version == os_version) &&
280 		    (os_subtype == PF_OSFP_ANY || en_subtype == os_subtype)) {
281 			DPFPRINTF("osfp matched %s %s %s  %x==%x\n",
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("fingerprint 0x%x didn't match\n", os);
288 	return (0);
289 }
290 
291 /* Initialize the OS fingerprint system */
292 int
293 pf_osfp_initialize(void)
294 {
295 	int error = 0;
296 
297 #ifdef _KERNEL
298 	do {
299 		error = ENOMEM;
300 		pf_osfp_entry_pl = pf_osfp_pl = NULL;
301 		ZONE_CREATE(pf_osfp_entry_pl, struct pf_osfp_entry, "pfospfen");
302 		ZONE_CREATE(pf_osfp_pl, struct pf_os_fingerprint, "pfosfp");
303 		error = 0;
304 	} while(0);
305 #else
306 	pool_init(&pf_osfp_entry_pl, sizeof(struct pf_osfp_entry), 0, 0, 0,
307 	    "pfosfpen", NULL);
308 	pool_init(&pf_osfp_pl, sizeof(struct pf_os_fingerprint), 0, 0, 0,
309 	    "pfosfp", NULL);
310 #endif
311 	SLIST_INIT(&pf_osfp_list);
312 
313 	return (error);
314 }
315 
316 #ifdef _KERNEL
317 void
318 pf_osfp_cleanup(void)
319 {
320 	ZONE_DESTROY(pf_osfp_entry_pl);
321 	ZONE_DESTROY(pf_osfp_pl);
322 }
323 #endif
324 
325 /* Flush the fingerprint list */
326 void
327 pf_osfp_flush(void)
328 {
329 	struct pf_os_fingerprint *fp;
330 	struct pf_osfp_entry *entry;
331 
332 	while ((fp = SLIST_FIRST(&pf_osfp_list))) {
333 		SLIST_REMOVE_HEAD(&pf_osfp_list, fp_next);
334 		while ((entry = SLIST_FIRST(&fp->fp_oses))) {
335 			SLIST_REMOVE_HEAD(&fp->fp_oses, fp_entry);
336 			pool_put(&pf_osfp_entry_pl, entry);
337 		}
338 		pool_put(&pf_osfp_pl, fp);
339 	}
340 }
341 
342 
343 /* Add a fingerprint */
344 int
345 pf_osfp_add(struct pf_osfp_ioctl *fpioc)
346 {
347 	struct pf_os_fingerprint *fp, fpadd;
348 	struct pf_osfp_entry *entry;
349 
350 	memset(&fpadd, 0, sizeof(fpadd));
351 	fpadd.fp_tcpopts = fpioc->fp_tcpopts;
352 	fpadd.fp_wsize = fpioc->fp_wsize;
353 	fpadd.fp_psize = fpioc->fp_psize;
354 	fpadd.fp_mss = fpioc->fp_mss;
355 	fpadd.fp_flags = fpioc->fp_flags;
356 	fpadd.fp_optcnt = fpioc->fp_optcnt;
357 	fpadd.fp_wscale = fpioc->fp_wscale;
358 	fpadd.fp_ttl = fpioc->fp_ttl;
359 
360 	DPFPRINTF("adding osfp %s %s %s = %s%d:%d:%d:%s%d:0x%llx %d "
361 	    "(TS=%s,M=%s%d,W=%s%d) %x\n",
362 	    fpioc->fp_os.fp_class_nm, fpioc->fp_os.fp_version_nm,
363 	    fpioc->fp_os.fp_subtype_nm,
364 	    (fpadd.fp_flags & PF_OSFP_WSIZE_MOD) ? "%" :
365 	    (fpadd.fp_flags & PF_OSFP_WSIZE_MSS) ? "S" :
366 	    (fpadd.fp_flags & PF_OSFP_WSIZE_MTU) ? "T" :
367 	    (fpadd.fp_flags & PF_OSFP_WSIZE_DC) ? "*" : "",
368 	    fpadd.fp_wsize,
369 	    fpadd.fp_ttl,
370 	    (fpadd.fp_flags & PF_OSFP_DF) ? 1 : 0,
371 	    (fpadd.fp_flags & PF_OSFP_PSIZE_MOD) ? "%" :
372 	    (fpadd.fp_flags & PF_OSFP_PSIZE_DC) ? "*" : "",
373 	    fpadd.fp_psize,
374 	    (long long int)fpadd.fp_tcpopts, fpadd.fp_optcnt,
375 	    (fpadd.fp_flags & PF_OSFP_TS0) ? "0" : "",
376 	    (fpadd.fp_flags & PF_OSFP_MSS_MOD) ? "%" :
377 	    (fpadd.fp_flags & PF_OSFP_MSS_DC) ? "*" : "",
378 	    fpadd.fp_mss,
379 	    (fpadd.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" :
380 	    (fpadd.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "",
381 	    fpadd.fp_wscale,
382 	    fpioc->fp_os.fp_os);
383 
384 
385 	if ((fp = pf_osfp_find_exact(&pf_osfp_list, &fpadd))) {
386 		 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
387 			if (PF_OSFP_ENTRY_EQ(entry, &fpioc->fp_os))
388 				return (EEXIST);
389 		}
390 		if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL)
391 			return (ENOMEM);
392 	} else {
393 		if ((fp = pool_get(&pf_osfp_pl, PR_NOWAIT)) == NULL)
394 			return (ENOMEM);
395 		memset(fp, 0, sizeof(*fp));
396 		fp->fp_tcpopts = fpioc->fp_tcpopts;
397 		fp->fp_wsize = fpioc->fp_wsize;
398 		fp->fp_psize = fpioc->fp_psize;
399 		fp->fp_mss = fpioc->fp_mss;
400 		fp->fp_flags = fpioc->fp_flags;
401 		fp->fp_optcnt = fpioc->fp_optcnt;
402 		fp->fp_wscale = fpioc->fp_wscale;
403 		fp->fp_ttl = fpioc->fp_ttl;
404 		SLIST_INIT(&fp->fp_oses);
405 		if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL) {
406 			pool_put(&pf_osfp_pl, fp);
407 			return (ENOMEM);
408 		}
409 		pf_osfp_insert(&pf_osfp_list, fp);
410 	}
411 	memcpy(entry, &fpioc->fp_os, sizeof(*entry));
412 
413 	/* Make sure the strings are NUL terminated */
414 	entry->fp_class_nm[sizeof(entry->fp_class_nm)-1] = '\0';
415 	entry->fp_version_nm[sizeof(entry->fp_version_nm)-1] = '\0';
416 	entry->fp_subtype_nm[sizeof(entry->fp_subtype_nm)-1] = '\0';
417 
418 	SLIST_INSERT_HEAD(&fp->fp_oses, entry, fp_entry);
419 
420 #ifdef PFDEBUG
421 	if ((fp = pf_osfp_validate()))
422 		kprintf("Invalid fingerprint list\n");
423 #endif /* PFDEBUG */
424 	return (0);
425 }
426 
427 
428 /* Find a fingerprint in the list */
429 struct pf_os_fingerprint *
430 pf_osfp_find(struct pf_osfp_list *list, struct pf_os_fingerprint *find,
431     u_int8_t ttldiff)
432 {
433 	struct pf_os_fingerprint *f;
434 
435 #define MATCH_INT(_MOD, _DC, _field)					\
436 	if ((f->fp_flags & _DC) == 0) {					\
437 		if ((f->fp_flags & _MOD) == 0) {			\
438 			if (f->_field != find->_field)			\
439 				continue;				\
440 		} else {						\
441 			if (f->_field == 0 || find->_field % f->_field)	\
442 				continue;				\
443 		}							\
444 	}
445 
446 	SLIST_FOREACH(f, list, fp_next) {
447 		if (f->fp_tcpopts != find->fp_tcpopts ||
448 		    f->fp_optcnt != find->fp_optcnt ||
449 		    f->fp_ttl < find->fp_ttl ||
450 		    f->fp_ttl - find->fp_ttl > ttldiff ||
451 		    (f->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)) !=
452 		    (find->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)))
453 			continue;
454 
455 		MATCH_INT(PF_OSFP_PSIZE_MOD, PF_OSFP_PSIZE_DC, fp_psize)
456 		MATCH_INT(PF_OSFP_MSS_MOD, PF_OSFP_MSS_DC, fp_mss)
457 		MATCH_INT(PF_OSFP_WSCALE_MOD, PF_OSFP_WSCALE_DC, fp_wscale)
458 		if ((f->fp_flags & PF_OSFP_WSIZE_DC) == 0) {
459 			if (f->fp_flags & PF_OSFP_WSIZE_MSS) {
460 				if (find->fp_mss == 0)
461 					continue;
462 
463 /* Some "smart" NAT devices and DSL routers will tweak the MSS size and
464  * will set it to whatever is suitable for the link type.
465  */
466 #define SMART_MSS	1460
467 				if ((find->fp_wsize % find->fp_mss ||
468 				    find->fp_wsize / find->fp_mss !=
469 				    f->fp_wsize) &&
470 				    (find->fp_wsize % SMART_MSS ||
471 				    find->fp_wsize / SMART_MSS !=
472 				    f->fp_wsize))
473 					continue;
474 			} else if (f->fp_flags & PF_OSFP_WSIZE_MTU) {
475 				if (find->fp_mss == 0)
476 					continue;
477 
478 #define MTUOFF	(sizeof(struct ip) + sizeof(struct tcphdr))
479 #define SMART_MTU	(SMART_MSS + MTUOFF)
480 				if ((find->fp_wsize % (find->fp_mss + MTUOFF) ||
481 				    find->fp_wsize / (find->fp_mss + MTUOFF) !=
482 				    f->fp_wsize) &&
483 				    (find->fp_wsize % SMART_MTU ||
484 				    find->fp_wsize / SMART_MTU !=
485 				    f->fp_wsize))
486 					continue;
487 			} else if (f->fp_flags & PF_OSFP_WSIZE_MOD) {
488 				if (f->fp_wsize == 0 || find->fp_wsize %
489 				    f->fp_wsize)
490 					continue;
491 			} else {
492 				if (f->fp_wsize != find->fp_wsize)
493 					continue;
494 			}
495 		}
496 		return (f);
497 	}
498 
499 	return (NULL);
500 }
501 
502 /* Find an exact fingerprint in the list */
503 struct pf_os_fingerprint *
504 pf_osfp_find_exact(struct pf_osfp_list *list, struct pf_os_fingerprint *find)
505 {
506 	struct pf_os_fingerprint *f;
507 
508 	SLIST_FOREACH(f, list, fp_next) {
509 		if (f->fp_tcpopts == find->fp_tcpopts &&
510 		    f->fp_wsize == find->fp_wsize &&
511 		    f->fp_psize == find->fp_psize &&
512 		    f->fp_mss == find->fp_mss &&
513 		    f->fp_flags == find->fp_flags &&
514 		    f->fp_optcnt == find->fp_optcnt &&
515 		    f->fp_wscale == find->fp_wscale &&
516 		    f->fp_ttl == find->fp_ttl)
517 			return (f);
518 	}
519 
520 	return (NULL);
521 }
522 
523 /* Insert a fingerprint into the list */
524 void
525 pf_osfp_insert(struct pf_osfp_list *list, struct pf_os_fingerprint *ins)
526 {
527 	struct pf_os_fingerprint *f, *prev = NULL;
528 
529 	/* XXX need to go semi tree based.  can key on tcp options */
530 
531 	SLIST_FOREACH(f, list, fp_next)
532 		prev = f;
533 	if (prev)
534 		SLIST_INSERT_AFTER(prev, ins, fp_next);
535 	else
536 		SLIST_INSERT_HEAD(list, ins, fp_next);
537 }
538 
539 /* Fill a fingerprint by its number (from an ioctl) */
540 int
541 pf_osfp_get(struct pf_osfp_ioctl *fpioc)
542 {
543 	struct pf_os_fingerprint *fp;
544 	struct pf_osfp_entry *entry;
545 	int num = fpioc->fp_getnum;
546 	int i = 0;
547 
548 
549 	memset(fpioc, 0, sizeof(*fpioc));
550 	SLIST_FOREACH(fp, &pf_osfp_list, fp_next) {
551 		SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
552 			if (i++ == num) {
553 				fpioc->fp_mss = fp->fp_mss;
554 				fpioc->fp_wsize = fp->fp_wsize;
555 				fpioc->fp_flags = fp->fp_flags;
556 				fpioc->fp_psize = fp->fp_psize;
557 				fpioc->fp_ttl = fp->fp_ttl;
558 				fpioc->fp_wscale = fp->fp_wscale;
559 				fpioc->fp_getnum = num;
560 				memcpy(&fpioc->fp_os, entry,
561 				    sizeof(fpioc->fp_os));
562 				return (0);
563 			}
564 		}
565 	}
566 
567 	return (EBUSY);
568 }
569 
570 
571 /* Validate that each signature is reachable */
572 struct pf_os_fingerprint *
573 pf_osfp_validate(void)
574 {
575 	struct pf_os_fingerprint *f, *f2, find;
576 
577 	SLIST_FOREACH(f, &pf_osfp_list, fp_next) {
578 		memcpy(&find, f, sizeof(find));
579 
580 		/* We do a few MSS/th_win percolations to make things unique */
581 		if (find.fp_mss == 0)
582 			find.fp_mss = 128;
583 		if (f->fp_flags & PF_OSFP_WSIZE_MSS)
584 			find.fp_wsize *= find.fp_mss;
585 		else if (f->fp_flags & PF_OSFP_WSIZE_MTU)
586 			find.fp_wsize *= (find.fp_mss + 40);
587 		else if (f->fp_flags & PF_OSFP_WSIZE_MOD)
588 			find.fp_wsize *= 2;
589 		if (f != (f2 = pf_osfp_find(&pf_osfp_list, &find, 0))) {
590 			if (f2)
591 				kprintf("Found \"%s %s %s\" instead of "
592 				    "\"%s %s %s\"\n",
593 				    SLIST_FIRST(&f2->fp_oses)->fp_class_nm,
594 				    SLIST_FIRST(&f2->fp_oses)->fp_version_nm,
595 				    SLIST_FIRST(&f2->fp_oses)->fp_subtype_nm,
596 				    SLIST_FIRST(&f->fp_oses)->fp_class_nm,
597 				    SLIST_FIRST(&f->fp_oses)->fp_version_nm,
598 				    SLIST_FIRST(&f->fp_oses)->fp_subtype_nm);
599 			else
600 				kprintf("Couldn't find \"%s %s %s\"\n",
601 				    SLIST_FIRST(&f->fp_oses)->fp_class_nm,
602 				    SLIST_FIRST(&f->fp_oses)->fp_version_nm,
603 				    SLIST_FIRST(&f->fp_oses)->fp_subtype_nm);
604 			return (f);
605 		}
606 	}
607 	return (NULL);
608 }
609