1 /* $NetBSD: pf_osfp.c,v 1.9 2009/07/28 18:15:26 minskim Exp $ */ 2 /* $OpenBSD: pf_osfp.c,v 1.12 2006/12/13 18:14:10 itojun Exp $ */ 3 4 /* 5 * Copyright (c) 2003 Mike Frantzen <frantzen@w4g.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <sys/cdefs.h> 22 __KERNEL_RCSID(0, "$NetBSD: pf_osfp.c,v 1.9 2009/07/28 18:15:26 minskim Exp $"); 23 24 #ifdef _KERNEL_OPT 25 #include "opt_inet.h" 26 #endif 27 28 #include <sys/param.h> 29 #include <sys/socket.h> 30 #ifdef _KERNEL 31 # include <sys/systm.h> 32 #endif /* _KERNEL */ 33 #include <sys/mbuf.h> 34 35 #include <netinet/in.h> 36 #include <netinet/in_systm.h> 37 #include <netinet/ip.h> 38 #include <netinet/tcp.h> 39 40 #include <net/if.h> 41 #include <net/pfvar.h> 42 43 #include <netinet/ip6.h> 44 #ifdef _KERNEL 45 #include <netinet6/in6_var.h> 46 #endif 47 48 49 #ifdef _KERNEL 50 # define DPFPRINTF(format, x...) \ 51 if (pf_status.debug >= PF_DEBUG_NOISY) \ 52 printf(format , ##x) 53 typedef struct pool pool_t; 54 55 #else 56 /* Userland equivalents so we can lend code to tcpdump et al. */ 57 58 # include <arpa/inet.h> 59 # include <errno.h> 60 # include <stdio.h> 61 # include <stdlib.h> 62 # include <string.h> 63 # include <netdb.h> 64 # define pool_t int 65 # define pool_get(pool, flags) malloc(*(pool)) 66 # define pool_put(pool, item) free(item) 67 # define pool_init(pool, size, a, ao, f, m, p) (*(pool)) = (size) 68 69 # ifdef PFDEBUG 70 # include <sys/stdarg.h> 71 # define DPFPRINTF(format, x...) fprintf(stderr, format , ##x) 72 # else 73 # define DPFPRINTF(format, x...) ((void)0) 74 # endif /* PFDEBUG */ 75 #endif /* _KERNEL */ 76 77 78 SLIST_HEAD(pf_osfp_list, pf_os_fingerprint) pf_osfp_list; 79 pool_t pf_osfp_entry_pl; 80 pool_t pf_osfp_pl; 81 82 struct pf_os_fingerprint *pf_osfp_find(struct pf_osfp_list *, 83 struct pf_os_fingerprint *, u_int8_t); 84 struct pf_os_fingerprint *pf_osfp_find_exact(struct pf_osfp_list *, 85 struct pf_os_fingerprint *); 86 void pf_osfp_insert(struct pf_osfp_list *, 87 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, struct mbuf *m, int off, 97 const struct tcphdr *tcp) 98 { 99 struct ip *ip; 100 struct ip6_hdr *ip6; 101 char hdr[60]; 102 103 if ((pd->af != PF_INET && pd->af != PF_INET6) || 104 pd->proto != IPPROTO_TCP || (tcp->th_off << 2) < sizeof(*tcp)) 105 return (NULL); 106 107 if (pd->af == PF_INET) { 108 ip = mtod(m, struct ip *); 109 ip6 = (struct ip6_hdr *)NULL; 110 } else { 111 ip = (struct ip *)NULL; 112 ip6 = mtod(m, struct ip6_hdr *); 113 } 114 if (!pf_pull_hdr(m, off, hdr, tcp->th_off << 2, NULL, NULL, 115 pd->af)) return (NULL); 116 117 return (pf_osfp_fingerprint_hdr(ip, ip6, (struct tcphdr *)hdr)); 118 } 119 #endif /* _KERNEL */ 120 121 struct pf_osfp_enlist * 122 pf_osfp_fingerprint_hdr(const struct ip *ip, const struct ip6_hdr *ip6, const struct tcphdr *tcp) 123 { 124 struct pf_os_fingerprint fp, *fpresult; 125 int cnt, optlen = 0; 126 const u_int8_t *optp; 127 #ifdef _KERNEL 128 char srcname[128]; 129 #else 130 char srcname[NI_MAXHOST]; 131 #endif 132 133 if ((tcp->th_flags & (TH_SYN|TH_ACK)) != TH_SYN) 134 return (NULL); 135 if (ip) { 136 if ((ip->ip_off & htons(IP_OFFMASK)) != 0) 137 return (NULL); 138 } 139 140 memset(&fp, 0, sizeof(fp)); 141 142 if (ip) { 143 #ifndef _KERNEL 144 struct sockaddr_in sin; 145 #endif 146 147 fp.fp_psize = ntohs(ip->ip_len); 148 fp.fp_ttl = ip->ip_ttl; 149 if (ip->ip_off & htons(IP_DF)) 150 fp.fp_flags |= PF_OSFP_DF; 151 #ifdef _KERNEL 152 strlcpy(srcname, inet_ntoa(ip->ip_src), sizeof(srcname)); 153 #else 154 memset(&sin, 0, sizeof(sin)); 155 sin.sin_family = AF_INET; 156 sin.sin_len = sizeof(struct sockaddr_in); 157 sin.sin_addr = ip->ip_src; 158 (void)getnameinfo((struct sockaddr *)&sin, 159 sizeof(struct sockaddr_in), srcname, sizeof(srcname), 160 NULL, 0, NI_NUMERICHOST); 161 #endif 162 } 163 #ifdef INET6 164 else if (ip6) { 165 #ifndef _KERNEL 166 struct sockaddr_in6 sin6; 167 #endif 168 169 /* jumbo payload? */ 170 fp.fp_psize = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen); 171 fp.fp_ttl = ip6->ip6_hlim; 172 fp.fp_flags |= PF_OSFP_DF; 173 fp.fp_flags |= PF_OSFP_INET6; 174 #ifdef _KERNEL 175 strlcpy(srcname, 176 ip6_sprintf((const struct in6_addr *)&ip6->ip6_src), 177 sizeof(srcname)); 178 #else 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 187 } 188 #endif 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 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 NTOHS(fp.fp_wscale); 225 fp.fp_tcpopts = (fp.fp_tcpopts << 226 PF_OSFP_TCPOPT_BITS) | 227 PF_OSFP_TCPOPT_WSCALE; 228 break; 229 case TCPOPT_SACK_PERMITTED: 230 fp.fp_tcpopts = (fp.fp_tcpopts << 231 PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_SACK; 232 break; 233 case TCPOPT_TIMESTAMP: 234 if (optlen >= TCPOLEN_TIMESTAMP) { 235 u_int32_t ts; 236 memcpy(&ts, &optp[2], sizeof(ts)); 237 if (ts == 0) 238 fp.fp_flags |= PF_OSFP_TS0; 239 240 } 241 fp.fp_tcpopts = (fp.fp_tcpopts << 242 PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_TS; 243 break; 244 default: 245 return (NULL); 246 } 247 } 248 optlen = MAX(optlen, 1); /* paranoia */ 249 } 250 251 DPFPRINTF("fingerprinted %s:%d %d:%d:%d:%d:%llx (%d) " 252 "(TS=%s,M=%s%d,W=%s%d)\n", 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(&pf_osfp_list, &fp, 265 PF_OSFP_MAXTTL_OFFSET))) 266 return (&fpresult->fp_oses); 267 return (NULL); 268 } 269 270 /* Match a fingerprint ID against a list of OSes */ 271 int 272 pf_osfp_match(struct pf_osfp_enlist *list, pf_osfp_t os) 273 { 274 struct pf_osfp_entry *entry; 275 int os_class, os_version, os_subtype; 276 int en_class, en_version, en_subtype; 277 278 if (os == PF_OSFP_ANY) 279 return (1); 280 if (list == NULL) { 281 DPFPRINTF("osfp no match against %x\n", os); 282 return (os == PF_OSFP_UNKNOWN); 283 } 284 PF_OSFP_UNPACK(os, os_class, os_version, os_subtype); 285 SLIST_FOREACH(entry, list, fp_entry) { 286 PF_OSFP_UNPACK(entry->fp_os, en_class, en_version, en_subtype); 287 if ((os_class == PF_OSFP_ANY || en_class == os_class) && 288 (os_version == PF_OSFP_ANY || en_version == os_version) && 289 (os_subtype == PF_OSFP_ANY || en_subtype == os_subtype)) { 290 DPFPRINTF("osfp matched %s %s %s %x==%x\n", 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("fingerprint 0x%x didn't match\n", os); 297 return (0); 298 } 299 300 /* Initialize the OS fingerprint system */ 301 void 302 pf_osfp_initialize(void) 303 { 304 #ifdef __NetBSD__ 305 pool_init(&pf_osfp_entry_pl, sizeof(struct pf_osfp_entry), 0, 0, 0, 306 "pfosfpen", &pool_allocator_nointr, IPL_NONE); 307 pool_init(&pf_osfp_pl, sizeof(struct pf_os_fingerprint), 0, 0, 0, 308 "pfosfp", &pool_allocator_nointr, IPL_NONE); 309 #else 310 pool_init(&pf_osfp_entry_pl, sizeof(struct pf_osfp_entry), 0, 0, 0, 311 "pfosfpen", &pool_allocator_nointr); 312 pool_init(&pf_osfp_pl, sizeof(struct pf_os_fingerprint), 0, 0, 0, 313 "pfosfp", &pool_allocator_nointr); 314 #endif /* !__NetBSD__ */ 315 SLIST_INIT(&pf_osfp_list); 316 } 317 318 /* Flush the fingerprint list */ 319 void 320 pf_osfp_flush(void) 321 { 322 struct pf_os_fingerprint *fp; 323 struct pf_osfp_entry *entry; 324 325 while ((fp = SLIST_FIRST(&pf_osfp_list))) { 326 SLIST_REMOVE_HEAD(&pf_osfp_list, fp_next); 327 while ((entry = SLIST_FIRST(&fp->fp_oses))) { 328 SLIST_REMOVE_HEAD(&fp->fp_oses, fp_entry); 329 pool_put(&pf_osfp_entry_pl, entry); 330 } 331 pool_put(&pf_osfp_pl, fp); 332 } 333 } 334 335 336 /* Add a fingerprint */ 337 int 338 pf_osfp_add(struct pf_osfp_ioctl *fpioc) 339 { 340 struct pf_os_fingerprint *fp, fpadd; 341 struct pf_osfp_entry *entry; 342 343 memset(&fpadd, 0, sizeof(fpadd)); 344 fpadd.fp_tcpopts = fpioc->fp_tcpopts; 345 fpadd.fp_wsize = fpioc->fp_wsize; 346 fpadd.fp_psize = fpioc->fp_psize; 347 fpadd.fp_mss = fpioc->fp_mss; 348 fpadd.fp_flags = fpioc->fp_flags; 349 fpadd.fp_optcnt = fpioc->fp_optcnt; 350 fpadd.fp_wscale = fpioc->fp_wscale; 351 fpadd.fp_ttl = fpioc->fp_ttl; 352 353 DPFPRINTF("adding osfp %s %s %s = %s%d:%d:%d:%s%d:0x%llx %d " 354 "(TS=%s,M=%s%d,W=%s%d) %x\n", 355 fpioc->fp_os.fp_class_nm, fpioc->fp_os.fp_version_nm, 356 fpioc->fp_os.fp_subtype_nm, 357 (fpadd.fp_flags & PF_OSFP_WSIZE_MOD) ? "%" : 358 (fpadd.fp_flags & PF_OSFP_WSIZE_MSS) ? "S" : 359 (fpadd.fp_flags & PF_OSFP_WSIZE_MTU) ? "T" : 360 (fpadd.fp_flags & PF_OSFP_WSIZE_DC) ? "*" : "", 361 fpadd.fp_wsize, 362 fpadd.fp_ttl, 363 (fpadd.fp_flags & PF_OSFP_DF) ? 1 : 0, 364 (fpadd.fp_flags & PF_OSFP_PSIZE_MOD) ? "%" : 365 (fpadd.fp_flags & PF_OSFP_PSIZE_DC) ? "*" : "", 366 fpadd.fp_psize, 367 (long long int)fpadd.fp_tcpopts, fpadd.fp_optcnt, 368 (fpadd.fp_flags & PF_OSFP_TS0) ? "0" : "", 369 (fpadd.fp_flags & PF_OSFP_MSS_MOD) ? "%" : 370 (fpadd.fp_flags & PF_OSFP_MSS_DC) ? "*" : "", 371 fpadd.fp_mss, 372 (fpadd.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" : 373 (fpadd.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "", 374 fpadd.fp_wscale, 375 fpioc->fp_os.fp_os); 376 377 378 if ((fp = pf_osfp_find_exact(&pf_osfp_list, &fpadd))) { 379 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) { 380 if (PF_OSFP_ENTRY_EQ(entry, &fpioc->fp_os)) 381 return (EEXIST); 382 } 383 if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL) 384 return (ENOMEM); 385 } else { 386 if ((fp = pool_get(&pf_osfp_pl, PR_NOWAIT)) == NULL) 387 return (ENOMEM); 388 memset(fp, 0, sizeof(*fp)); 389 fp->fp_tcpopts = fpioc->fp_tcpopts; 390 fp->fp_wsize = fpioc->fp_wsize; 391 fp->fp_psize = fpioc->fp_psize; 392 fp->fp_mss = fpioc->fp_mss; 393 fp->fp_flags = fpioc->fp_flags; 394 fp->fp_optcnt = fpioc->fp_optcnt; 395 fp->fp_wscale = fpioc->fp_wscale; 396 fp->fp_ttl = fpioc->fp_ttl; 397 SLIST_INIT(&fp->fp_oses); 398 if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL) { 399 pool_put(&pf_osfp_pl, fp); 400 return (ENOMEM); 401 } 402 pf_osfp_insert(&pf_osfp_list, fp); 403 } 404 memcpy(entry, &fpioc->fp_os, sizeof(*entry)); 405 406 /* Make sure the strings are NUL terminated */ 407 entry->fp_class_nm[sizeof(entry->fp_class_nm)-1] = '\0'; 408 entry->fp_version_nm[sizeof(entry->fp_version_nm)-1] = '\0'; 409 entry->fp_subtype_nm[sizeof(entry->fp_subtype_nm)-1] = '\0'; 410 411 SLIST_INSERT_HEAD(&fp->fp_oses, entry, fp_entry); 412 413 #ifdef PFDEBUG 414 if ((fp = pf_osfp_validate())) 415 printf("Invalid fingerprint list\n"); 416 #endif /* PFDEBUG */ 417 return (0); 418 } 419 420 421 /* Find a fingerprint in the list */ 422 struct pf_os_fingerprint * 423 pf_osfp_find(struct pf_osfp_list *list, struct pf_os_fingerprint *find, 424 u_int8_t ttldiff) 425 { 426 struct pf_os_fingerprint *f; 427 428 #define MATCH_INT(_MOD, _DC, _field) \ 429 if ((f->fp_flags & _DC) == 0) { \ 430 if ((f->fp_flags & _MOD) == 0) { \ 431 if (f->_field != find->_field) \ 432 continue; \ 433 } else { \ 434 if (f->_field == 0 || find->_field % f->_field) \ 435 continue; \ 436 } \ 437 } 438 439 SLIST_FOREACH(f, list, fp_next) { 440 if (f->fp_tcpopts != find->fp_tcpopts || 441 f->fp_optcnt != find->fp_optcnt || 442 f->fp_ttl < find->fp_ttl || 443 f->fp_ttl - find->fp_ttl > ttldiff || 444 (f->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)) != 445 (find->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0))) 446 continue; 447 448 MATCH_INT(PF_OSFP_PSIZE_MOD, PF_OSFP_PSIZE_DC, fp_psize) 449 MATCH_INT(PF_OSFP_MSS_MOD, PF_OSFP_MSS_DC, fp_mss) 450 MATCH_INT(PF_OSFP_WSCALE_MOD, PF_OSFP_WSCALE_DC, fp_wscale) 451 if ((f->fp_flags & PF_OSFP_WSIZE_DC) == 0) { 452 if (f->fp_flags & PF_OSFP_WSIZE_MSS) { 453 if (find->fp_mss == 0) 454 continue; 455 456 /* Some "smart" NAT devices and DSL routers will tweak the MSS size and 457 * will set it to whatever is suitable for the link type. 458 */ 459 #define SMART_MSS 1460 460 if ((find->fp_wsize % find->fp_mss || 461 find->fp_wsize / find->fp_mss != 462 f->fp_wsize) && 463 (find->fp_wsize % SMART_MSS || 464 find->fp_wsize / SMART_MSS != 465 f->fp_wsize)) 466 continue; 467 } else if (f->fp_flags & PF_OSFP_WSIZE_MTU) { 468 if (find->fp_mss == 0) 469 continue; 470 471 #define MTUOFF (sizeof(struct ip) + sizeof(struct tcphdr)) 472 #define SMART_MTU (SMART_MSS + MTUOFF) 473 if ((find->fp_wsize % (find->fp_mss + MTUOFF) || 474 find->fp_wsize / (find->fp_mss + MTUOFF) != 475 f->fp_wsize) && 476 (find->fp_wsize % SMART_MTU || 477 find->fp_wsize / SMART_MTU != 478 f->fp_wsize)) 479 continue; 480 } else if (f->fp_flags & PF_OSFP_WSIZE_MOD) { 481 if (f->fp_wsize == 0 || find->fp_wsize % 482 f->fp_wsize) 483 continue; 484 } else { 485 if (f->fp_wsize != find->fp_wsize) 486 continue; 487 } 488 } 489 return (f); 490 } 491 492 return (NULL); 493 } 494 495 /* Find an exact fingerprint in the list */ 496 struct pf_os_fingerprint * 497 pf_osfp_find_exact(struct pf_osfp_list *list, struct pf_os_fingerprint *find) 498 { 499 struct pf_os_fingerprint *f; 500 501 SLIST_FOREACH(f, list, fp_next) { 502 if (f->fp_tcpopts == find->fp_tcpopts && 503 f->fp_wsize == find->fp_wsize && 504 f->fp_psize == find->fp_psize && 505 f->fp_mss == find->fp_mss && 506 f->fp_flags == find->fp_flags && 507 f->fp_optcnt == find->fp_optcnt && 508 f->fp_wscale == find->fp_wscale && 509 f->fp_ttl == find->fp_ttl) 510 return (f); 511 } 512 513 return (NULL); 514 } 515 516 /* Insert a fingerprint into the list */ 517 void 518 pf_osfp_insert(struct pf_osfp_list *list, struct pf_os_fingerprint *ins) 519 { 520 struct pf_os_fingerprint *f, *prev = NULL; 521 522 /* XXX need to go semi tree based. can key on tcp options */ 523 524 SLIST_FOREACH(f, list, fp_next) 525 prev = f; 526 if (prev) 527 SLIST_INSERT_AFTER(prev, ins, fp_next); 528 else 529 SLIST_INSERT_HEAD(list, ins, fp_next); 530 } 531 532 /* Fill a fingerprint by its number (from an ioctl) */ 533 int 534 pf_osfp_get(struct pf_osfp_ioctl *fpioc) 535 { 536 struct pf_os_fingerprint *fp; 537 struct pf_osfp_entry *entry; 538 int num = fpioc->fp_getnum; 539 int i = 0; 540 541 542 memset(fpioc, 0, sizeof(*fpioc)); 543 SLIST_FOREACH(fp, &pf_osfp_list, fp_next) { 544 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) { 545 if (i++ == num) { 546 fpioc->fp_mss = fp->fp_mss; 547 fpioc->fp_wsize = fp->fp_wsize; 548 fpioc->fp_flags = fp->fp_flags; 549 fpioc->fp_psize = fp->fp_psize; 550 fpioc->fp_ttl = fp->fp_ttl; 551 fpioc->fp_wscale = fp->fp_wscale; 552 fpioc->fp_getnum = num; 553 memcpy(&fpioc->fp_os, entry, 554 sizeof(fpioc->fp_os)); 555 return (0); 556 } 557 } 558 } 559 560 return (EBUSY); 561 } 562 563 564 /* Validate that each signature is reachable */ 565 struct pf_os_fingerprint * 566 pf_osfp_validate(void) 567 { 568 struct pf_os_fingerprint *f, *f2, find; 569 570 SLIST_FOREACH(f, &pf_osfp_list, fp_next) { 571 memcpy(&find, f, sizeof(find)); 572 573 /* We do a few MSS/th_win percolations to make things unique */ 574 if (find.fp_mss == 0) 575 find.fp_mss = 128; 576 if (f->fp_flags & PF_OSFP_WSIZE_MSS) 577 find.fp_wsize *= find.fp_mss, 1; 578 else if (f->fp_flags & PF_OSFP_WSIZE_MTU) 579 find.fp_wsize *= (find.fp_mss + 40); 580 else if (f->fp_flags & PF_OSFP_WSIZE_MOD) 581 find.fp_wsize *= 2; 582 if (f != (f2 = pf_osfp_find(&pf_osfp_list, &find, 0))) { 583 if (f2) 584 printf("Found \"%s %s %s\" instead of " 585 "\"%s %s %s\"\n", 586 SLIST_FIRST(&f2->fp_oses)->fp_class_nm, 587 SLIST_FIRST(&f2->fp_oses)->fp_version_nm, 588 SLIST_FIRST(&f2->fp_oses)->fp_subtype_nm, 589 SLIST_FIRST(&f->fp_oses)->fp_class_nm, 590 SLIST_FIRST(&f->fp_oses)->fp_version_nm, 591 SLIST_FIRST(&f->fp_oses)->fp_subtype_nm); 592 else 593 printf("Couldn't find \"%s %s %s\"\n", 594 SLIST_FIRST(&f->fp_oses)->fp_class_nm, 595 SLIST_FIRST(&f->fp_oses)->fp_version_nm, 596 SLIST_FIRST(&f->fp_oses)->fp_subtype_nm); 597 return (f); 598 } 599 } 600 return (NULL); 601 } 602