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