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