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