1 /*- 2 * SPDX-License-Identifier: ISC 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 * $OpenBSD: pf_osfp.c,v 1.14 2008/06/12 18:17:01 henning Exp $ 19 */ 20 21 #include <sys/cdefs.h> 22 __FBSDID("$FreeBSD$"); 23 24 #include "opt_inet6.h" 25 26 #include <sys/param.h> 27 #include <sys/kernel.h> 28 #include <sys/lock.h> 29 #include <sys/mbuf.h> 30 #include <sys/rwlock.h> 31 #include <sys/socket.h> 32 33 #include <netinet/in.h> 34 #include <netinet/ip.h> 35 #include <netinet/tcp.h> 36 37 #include <net/if.h> 38 #include <net/vnet.h> 39 #include <net/pfvar.h> 40 41 #ifdef INET6 42 #include <netinet/ip6.h> 43 #endif 44 45 static MALLOC_DEFINE(M_PFOSFP, "pf_osfp", "pf(4) operating system fingerprints"); 46 #define DPFPRINTF(format, x...) \ 47 if (V_pf_status.debug >= PF_DEBUG_NOISY) \ 48 printf(format , ##x) 49 50 SLIST_HEAD(pf_osfp_list, pf_os_fingerprint); 51 static VNET_DEFINE(struct pf_osfp_list, pf_osfp_list) = 52 SLIST_HEAD_INITIALIZER(); 53 #define V_pf_osfp_list VNET(pf_osfp_list) 54 55 static struct pf_osfp_enlist *pf_osfp_fingerprint_hdr(const struct ip *, 56 const struct ip6_hdr *, 57 const struct tcphdr *); 58 static struct pf_os_fingerprint *pf_osfp_find(struct pf_osfp_list *, 59 struct pf_os_fingerprint *, u_int8_t); 60 static struct pf_os_fingerprint *pf_osfp_find_exact(struct pf_osfp_list *, 61 struct pf_os_fingerprint *); 62 static void pf_osfp_insert(struct pf_osfp_list *, 63 struct pf_os_fingerprint *); 64 #ifdef PFDEBUG 65 static struct pf_os_fingerprint *pf_osfp_validate(void); 66 #endif 67 68 /* 69 * Passively fingerprint the OS of the host (IPv4 TCP SYN packets only) 70 * Returns the list of possible OSes. 71 */ 72 struct pf_osfp_enlist * 73 pf_osfp_fingerprint(struct pf_pdesc *pd, struct mbuf *m, int off, 74 const struct tcphdr *tcp) 75 { 76 struct ip *ip; 77 struct ip6_hdr *ip6; 78 char hdr[60]; 79 80 if ((pd->af != PF_INET && pd->af != PF_INET6) || 81 pd->proto != IPPROTO_TCP || (tcp->th_off << 2) < sizeof(*tcp)) 82 return (NULL); 83 84 if (pd->af == PF_INET) { 85 ip = mtod(m, struct ip *); 86 ip6 = (struct ip6_hdr *)NULL; 87 } else { 88 ip = (struct ip *)NULL; 89 ip6 = mtod(m, struct ip6_hdr *); 90 } 91 if (!pf_pull_hdr(m, off, hdr, tcp->th_off << 2, NULL, NULL, 92 pd->af)) return (NULL); 93 94 return (pf_osfp_fingerprint_hdr(ip, ip6, (struct tcphdr *)hdr)); 95 } 96 97 static struct pf_osfp_enlist * 98 pf_osfp_fingerprint_hdr(const struct ip *ip, const struct ip6_hdr *ip6, const struct tcphdr *tcp) 99 { 100 struct pf_os_fingerprint fp, *fpresult; 101 int cnt, optlen = 0; 102 const u_int8_t *optp; 103 #ifdef INET6 104 char srcname[INET6_ADDRSTRLEN]; 105 #else 106 char srcname[INET_ADDRSTRLEN]; 107 #endif 108 109 if ((tcp->th_flags & (TH_SYN|TH_ACK)) != TH_SYN) 110 return (NULL); 111 if (ip) { 112 if ((ip->ip_off & htons(IP_OFFMASK)) != 0) 113 return (NULL); 114 } 115 116 memset(&fp, 0, sizeof(fp)); 117 118 if (ip) { 119 fp.fp_psize = ntohs(ip->ip_len); 120 fp.fp_ttl = ip->ip_ttl; 121 if (ip->ip_off & htons(IP_DF)) 122 fp.fp_flags |= PF_OSFP_DF; 123 inet_ntoa_r(ip->ip_src, srcname); 124 } 125 #ifdef INET6 126 else if (ip6) { 127 /* jumbo payload? */ 128 fp.fp_psize = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen); 129 fp.fp_ttl = ip6->ip6_hlim; 130 fp.fp_flags |= PF_OSFP_DF; 131 fp.fp_flags |= PF_OSFP_INET6; 132 ip6_sprintf(srcname, (const struct in6_addr *)&ip6->ip6_src); 133 } 134 #endif 135 else 136 return (NULL); 137 fp.fp_wsize = ntohs(tcp->th_win); 138 139 140 cnt = (tcp->th_off << 2) - sizeof(*tcp); 141 optp = (const u_int8_t *)((const char *)tcp + sizeof(*tcp)); 142 for (; cnt > 0; cnt -= optlen, optp += optlen) { 143 if (*optp == TCPOPT_EOL) 144 break; 145 146 fp.fp_optcnt++; 147 if (*optp == TCPOPT_NOP) { 148 fp.fp_tcpopts = (fp.fp_tcpopts << PF_OSFP_TCPOPT_BITS) | 149 PF_OSFP_TCPOPT_NOP; 150 optlen = 1; 151 } else { 152 if (cnt < 2) 153 return (NULL); 154 optlen = optp[1]; 155 if (optlen > cnt || optlen < 2) 156 return (NULL); 157 switch (*optp) { 158 case TCPOPT_MAXSEG: 159 if (optlen >= TCPOLEN_MAXSEG) 160 memcpy(&fp.fp_mss, &optp[2], 161 sizeof(fp.fp_mss)); 162 fp.fp_tcpopts = (fp.fp_tcpopts << 163 PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_MSS; 164 NTOHS(fp.fp_mss); 165 break; 166 case TCPOPT_WINDOW: 167 if (optlen >= TCPOLEN_WINDOW) 168 memcpy(&fp.fp_wscale, &optp[2], 169 sizeof(fp.fp_wscale)); 170 NTOHS(fp.fp_wscale); 171 fp.fp_tcpopts = (fp.fp_tcpopts << 172 PF_OSFP_TCPOPT_BITS) | 173 PF_OSFP_TCPOPT_WSCALE; 174 break; 175 case TCPOPT_SACK_PERMITTED: 176 fp.fp_tcpopts = (fp.fp_tcpopts << 177 PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_SACK; 178 break; 179 case TCPOPT_TIMESTAMP: 180 if (optlen >= TCPOLEN_TIMESTAMP) { 181 u_int32_t ts; 182 memcpy(&ts, &optp[2], sizeof(ts)); 183 if (ts == 0) 184 fp.fp_flags |= PF_OSFP_TS0; 185 186 } 187 fp.fp_tcpopts = (fp.fp_tcpopts << 188 PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_TS; 189 break; 190 default: 191 return (NULL); 192 } 193 } 194 optlen = MAX(optlen, 1); /* paranoia */ 195 } 196 197 DPFPRINTF("fingerprinted %s:%d %d:%d:%d:%d:%llx (%d) " 198 "(TS=%s,M=%s%d,W=%s%d)\n", 199 srcname, ntohs(tcp->th_sport), 200 fp.fp_wsize, fp.fp_ttl, (fp.fp_flags & PF_OSFP_DF) != 0, 201 fp.fp_psize, (long long int)fp.fp_tcpopts, fp.fp_optcnt, 202 (fp.fp_flags & PF_OSFP_TS0) ? "0" : "", 203 (fp.fp_flags & PF_OSFP_MSS_MOD) ? "%" : 204 (fp.fp_flags & PF_OSFP_MSS_DC) ? "*" : "", 205 fp.fp_mss, 206 (fp.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" : 207 (fp.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "", 208 fp.fp_wscale); 209 210 if ((fpresult = pf_osfp_find(&V_pf_osfp_list, &fp, 211 PF_OSFP_MAXTTL_OFFSET))) 212 return (&fpresult->fp_oses); 213 return (NULL); 214 } 215 216 /* Match a fingerprint ID against a list of OSes */ 217 int 218 pf_osfp_match(struct pf_osfp_enlist *list, pf_osfp_t os) 219 { 220 struct pf_osfp_entry *entry; 221 int os_class, os_version, os_subtype; 222 int en_class, en_version, en_subtype; 223 224 if (os == PF_OSFP_ANY) 225 return (1); 226 if (list == NULL) { 227 DPFPRINTF("osfp no match against %x\n", os); 228 return (os == PF_OSFP_UNKNOWN); 229 } 230 PF_OSFP_UNPACK(os, os_class, os_version, os_subtype); 231 SLIST_FOREACH(entry, list, fp_entry) { 232 PF_OSFP_UNPACK(entry->fp_os, en_class, en_version, en_subtype); 233 if ((os_class == PF_OSFP_ANY || en_class == os_class) && 234 (os_version == PF_OSFP_ANY || en_version == os_version) && 235 (os_subtype == PF_OSFP_ANY || en_subtype == os_subtype)) { 236 DPFPRINTF("osfp matched %s %s %s %x==%x\n", 237 entry->fp_class_nm, entry->fp_version_nm, 238 entry->fp_subtype_nm, os, entry->fp_os); 239 return (1); 240 } 241 } 242 DPFPRINTF("fingerprint 0x%x didn't match\n", os); 243 return (0); 244 } 245 246 /* Flush the fingerprint list */ 247 void 248 pf_osfp_flush(void) 249 { 250 struct pf_os_fingerprint *fp; 251 struct pf_osfp_entry *entry; 252 253 while ((fp = SLIST_FIRST(&V_pf_osfp_list))) { 254 SLIST_REMOVE_HEAD(&V_pf_osfp_list, fp_next); 255 while ((entry = SLIST_FIRST(&fp->fp_oses))) { 256 SLIST_REMOVE_HEAD(&fp->fp_oses, fp_entry); 257 free(entry, M_PFOSFP); 258 } 259 free(fp, M_PFOSFP); 260 } 261 } 262 263 264 /* Add a fingerprint */ 265 int 266 pf_osfp_add(struct pf_osfp_ioctl *fpioc) 267 { 268 struct pf_os_fingerprint *fp, fpadd; 269 struct pf_osfp_entry *entry; 270 271 PF_RULES_WASSERT(); 272 273 memset(&fpadd, 0, sizeof(fpadd)); 274 fpadd.fp_tcpopts = fpioc->fp_tcpopts; 275 fpadd.fp_wsize = fpioc->fp_wsize; 276 fpadd.fp_psize = fpioc->fp_psize; 277 fpadd.fp_mss = fpioc->fp_mss; 278 fpadd.fp_flags = fpioc->fp_flags; 279 fpadd.fp_optcnt = fpioc->fp_optcnt; 280 fpadd.fp_wscale = fpioc->fp_wscale; 281 fpadd.fp_ttl = fpioc->fp_ttl; 282 283 #if 0 /* XXX RYAN wants to fix logging */ 284 DPFPRINTF("adding osfp %s %s %s = %s%d:%d:%d:%s%d:0x%llx %d " 285 "(TS=%s,M=%s%d,W=%s%d) %x\n", 286 fpioc->fp_os.fp_class_nm, fpioc->fp_os.fp_version_nm, 287 fpioc->fp_os.fp_subtype_nm, 288 (fpadd.fp_flags & PF_OSFP_WSIZE_MOD) ? "%" : 289 (fpadd.fp_flags & PF_OSFP_WSIZE_MSS) ? "S" : 290 (fpadd.fp_flags & PF_OSFP_WSIZE_MTU) ? "T" : 291 (fpadd.fp_flags & PF_OSFP_WSIZE_DC) ? "*" : "", 292 fpadd.fp_wsize, 293 fpadd.fp_ttl, 294 (fpadd.fp_flags & PF_OSFP_DF) ? 1 : 0, 295 (fpadd.fp_flags & PF_OSFP_PSIZE_MOD) ? "%" : 296 (fpadd.fp_flags & PF_OSFP_PSIZE_DC) ? "*" : "", 297 fpadd.fp_psize, 298 (long long int)fpadd.fp_tcpopts, fpadd.fp_optcnt, 299 (fpadd.fp_flags & PF_OSFP_TS0) ? "0" : "", 300 (fpadd.fp_flags & PF_OSFP_MSS_MOD) ? "%" : 301 (fpadd.fp_flags & PF_OSFP_MSS_DC) ? "*" : "", 302 fpadd.fp_mss, 303 (fpadd.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" : 304 (fpadd.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "", 305 fpadd.fp_wscale, 306 fpioc->fp_os.fp_os); 307 #endif 308 309 if ((fp = pf_osfp_find_exact(&V_pf_osfp_list, &fpadd))) { 310 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) { 311 if (PF_OSFP_ENTRY_EQ(entry, &fpioc->fp_os)) 312 return (EEXIST); 313 } 314 if ((entry = malloc(sizeof(*entry), M_PFOSFP, M_NOWAIT)) 315 == NULL) 316 return (ENOMEM); 317 } else { 318 if ((fp = malloc(sizeof(*fp), M_PFOSFP, M_ZERO | M_NOWAIT)) 319 == NULL) 320 return (ENOMEM); 321 fp->fp_tcpopts = fpioc->fp_tcpopts; 322 fp->fp_wsize = fpioc->fp_wsize; 323 fp->fp_psize = fpioc->fp_psize; 324 fp->fp_mss = fpioc->fp_mss; 325 fp->fp_flags = fpioc->fp_flags; 326 fp->fp_optcnt = fpioc->fp_optcnt; 327 fp->fp_wscale = fpioc->fp_wscale; 328 fp->fp_ttl = fpioc->fp_ttl; 329 SLIST_INIT(&fp->fp_oses); 330 if ((entry = malloc(sizeof(*entry), M_PFOSFP, M_NOWAIT)) 331 == NULL) { 332 free(fp, M_PFOSFP); 333 return (ENOMEM); 334 } 335 pf_osfp_insert(&V_pf_osfp_list, fp); 336 } 337 memcpy(entry, &fpioc->fp_os, sizeof(*entry)); 338 339 /* Make sure the strings are NUL terminated */ 340 entry->fp_class_nm[sizeof(entry->fp_class_nm)-1] = '\0'; 341 entry->fp_version_nm[sizeof(entry->fp_version_nm)-1] = '\0'; 342 entry->fp_subtype_nm[sizeof(entry->fp_subtype_nm)-1] = '\0'; 343 344 SLIST_INSERT_HEAD(&fp->fp_oses, entry, fp_entry); 345 346 #ifdef PFDEBUG 347 if ((fp = pf_osfp_validate())) 348 printf("Invalid fingerprint list\n"); 349 #endif /* PFDEBUG */ 350 return (0); 351 } 352 353 354 /* Find a fingerprint in the list */ 355 static struct pf_os_fingerprint * 356 pf_osfp_find(struct pf_osfp_list *list, struct pf_os_fingerprint *find, 357 u_int8_t ttldiff) 358 { 359 struct pf_os_fingerprint *f; 360 361 #define MATCH_INT(_MOD, _DC, _field) \ 362 if ((f->fp_flags & _DC) == 0) { \ 363 if ((f->fp_flags & _MOD) == 0) { \ 364 if (f->_field != find->_field) \ 365 continue; \ 366 } else { \ 367 if (f->_field == 0 || find->_field % f->_field) \ 368 continue; \ 369 } \ 370 } 371 372 SLIST_FOREACH(f, list, fp_next) { 373 if (f->fp_tcpopts != find->fp_tcpopts || 374 f->fp_optcnt != find->fp_optcnt || 375 f->fp_ttl < find->fp_ttl || 376 f->fp_ttl - find->fp_ttl > ttldiff || 377 (f->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)) != 378 (find->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0))) 379 continue; 380 381 MATCH_INT(PF_OSFP_PSIZE_MOD, PF_OSFP_PSIZE_DC, fp_psize) 382 MATCH_INT(PF_OSFP_MSS_MOD, PF_OSFP_MSS_DC, fp_mss) 383 MATCH_INT(PF_OSFP_WSCALE_MOD, PF_OSFP_WSCALE_DC, fp_wscale) 384 if ((f->fp_flags & PF_OSFP_WSIZE_DC) == 0) { 385 if (f->fp_flags & PF_OSFP_WSIZE_MSS) { 386 if (find->fp_mss == 0) 387 continue; 388 389 /* 390 * Some "smart" NAT devices and DSL routers will tweak the MSS size and 391 * will set it to whatever is suitable for the link type. 392 */ 393 #define SMART_MSS 1460 394 if ((find->fp_wsize % find->fp_mss || 395 find->fp_wsize / find->fp_mss != 396 f->fp_wsize) && 397 (find->fp_wsize % SMART_MSS || 398 find->fp_wsize / SMART_MSS != 399 f->fp_wsize)) 400 continue; 401 } else if (f->fp_flags & PF_OSFP_WSIZE_MTU) { 402 if (find->fp_mss == 0) 403 continue; 404 405 #define MTUOFF (sizeof(struct ip) + sizeof(struct tcphdr)) 406 #define SMART_MTU (SMART_MSS + MTUOFF) 407 if ((find->fp_wsize % (find->fp_mss + MTUOFF) || 408 find->fp_wsize / (find->fp_mss + MTUOFF) != 409 f->fp_wsize) && 410 (find->fp_wsize % SMART_MTU || 411 find->fp_wsize / SMART_MTU != 412 f->fp_wsize)) 413 continue; 414 } else if (f->fp_flags & PF_OSFP_WSIZE_MOD) { 415 if (f->fp_wsize == 0 || find->fp_wsize % 416 f->fp_wsize) 417 continue; 418 } else { 419 if (f->fp_wsize != find->fp_wsize) 420 continue; 421 } 422 } 423 return (f); 424 } 425 426 return (NULL); 427 } 428 429 /* Find an exact fingerprint in the list */ 430 static struct pf_os_fingerprint * 431 pf_osfp_find_exact(struct pf_osfp_list *list, struct pf_os_fingerprint *find) 432 { 433 struct pf_os_fingerprint *f; 434 435 SLIST_FOREACH(f, list, fp_next) { 436 if (f->fp_tcpopts == find->fp_tcpopts && 437 f->fp_wsize == find->fp_wsize && 438 f->fp_psize == find->fp_psize && 439 f->fp_mss == find->fp_mss && 440 f->fp_flags == find->fp_flags && 441 f->fp_optcnt == find->fp_optcnt && 442 f->fp_wscale == find->fp_wscale && 443 f->fp_ttl == find->fp_ttl) 444 return (f); 445 } 446 447 return (NULL); 448 } 449 450 /* Insert a fingerprint into the list */ 451 static void 452 pf_osfp_insert(struct pf_osfp_list *list, struct pf_os_fingerprint *ins) 453 { 454 struct pf_os_fingerprint *f, *prev = NULL; 455 456 /* XXX need to go semi tree based. can key on tcp options */ 457 458 SLIST_FOREACH(f, list, fp_next) 459 prev = f; 460 if (prev) 461 SLIST_INSERT_AFTER(prev, ins, fp_next); 462 else 463 SLIST_INSERT_HEAD(list, ins, fp_next); 464 } 465 466 /* Fill a fingerprint by its number (from an ioctl) */ 467 int 468 pf_osfp_get(struct pf_osfp_ioctl *fpioc) 469 { 470 struct pf_os_fingerprint *fp; 471 struct pf_osfp_entry *entry; 472 int num = fpioc->fp_getnum; 473 int i = 0; 474 475 476 memset(fpioc, 0, sizeof(*fpioc)); 477 SLIST_FOREACH(fp, &V_pf_osfp_list, fp_next) { 478 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) { 479 if (i++ == num) { 480 fpioc->fp_mss = fp->fp_mss; 481 fpioc->fp_wsize = fp->fp_wsize; 482 fpioc->fp_flags = fp->fp_flags; 483 fpioc->fp_psize = fp->fp_psize; 484 fpioc->fp_ttl = fp->fp_ttl; 485 fpioc->fp_wscale = fp->fp_wscale; 486 fpioc->fp_getnum = num; 487 memcpy(&fpioc->fp_os, entry, 488 sizeof(fpioc->fp_os)); 489 return (0); 490 } 491 } 492 } 493 494 return (EBUSY); 495 } 496 497 498 #ifdef PFDEBUG 499 /* Validate that each signature is reachable */ 500 static struct pf_os_fingerprint * 501 pf_osfp_validate(void) 502 { 503 struct pf_os_fingerprint *f, *f2, find; 504 505 SLIST_FOREACH(f, &V_pf_osfp_list, fp_next) { 506 memcpy(&find, f, sizeof(find)); 507 508 /* We do a few MSS/th_win percolations to make things unique */ 509 if (find.fp_mss == 0) 510 find.fp_mss = 128; 511 if (f->fp_flags & PF_OSFP_WSIZE_MSS) 512 find.fp_wsize *= find.fp_mss; 513 else if (f->fp_flags & PF_OSFP_WSIZE_MTU) 514 find.fp_wsize *= (find.fp_mss + 40); 515 else if (f->fp_flags & PF_OSFP_WSIZE_MOD) 516 find.fp_wsize *= 2; 517 if (f != (f2 = pf_osfp_find(&V_pf_osfp_list, &find, 0))) { 518 if (f2) 519 printf("Found \"%s %s %s\" instead of " 520 "\"%s %s %s\"\n", 521 SLIST_FIRST(&f2->fp_oses)->fp_class_nm, 522 SLIST_FIRST(&f2->fp_oses)->fp_version_nm, 523 SLIST_FIRST(&f2->fp_oses)->fp_subtype_nm, 524 SLIST_FIRST(&f->fp_oses)->fp_class_nm, 525 SLIST_FIRST(&f->fp_oses)->fp_version_nm, 526 SLIST_FIRST(&f->fp_oses)->fp_subtype_nm); 527 else 528 printf("Couldn't find \"%s %s %s\"\n", 529 SLIST_FIRST(&f->fp_oses)->fp_class_nm, 530 SLIST_FIRST(&f->fp_oses)->fp_version_nm, 531 SLIST_FIRST(&f->fp_oses)->fp_subtype_nm); 532 return (f); 533 } 534 } 535 return (NULL); 536 } 537 #endif /* PFDEBUG */ 538