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