1 /* $OpenBSD: pf_osfp.c,v 1.3 2003/08/27 18:23:36 frantzen 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/param.h> 21 #include <sys/socket.h> 22 #ifdef _KERNEL 23 # include <sys/systm.h> 24 #endif /* _KERNEL */ 25 #include <sys/mbuf.h> 26 27 #include <netinet/in.h> 28 #include <netinet/in_systm.h> 29 #include <netinet/ip.h> 30 #include <netinet/tcp.h> 31 32 #include <net/if.h> 33 #include <net/pfvar.h> 34 35 #ifdef INET6 36 #include <netinet/ip6.h> 37 #endif /* INET6 */ 38 39 40 #ifdef _KERNEL 41 # define DPFPRINTF(format, x...) \ 42 if (pf_status.debug >= PF_DEBUG_NOISY) \ 43 printf(format , ##x) 44 typedef struct pool pool_t; 45 46 #else 47 /* Userland equivalents so we can lend code to tcpdump et al. */ 48 49 # include <arpa/inet.h> 50 # include <errno.h> 51 # include <stdio.h> 52 # include <stdlib.h> 53 # define pool_t int 54 # define pool_get(pool, flags) malloc(*(pool)) 55 # define pool_put(pool, item) free(item) 56 # define pool_init(pool, size, a, ao, f, m, p) (*(pool)) = (size) 57 58 # ifdef PFDEBUG 59 # include <stdarg.h> 60 # define DPFPRINTF(format, x...) fprintf(stderr, format , ##x) 61 # else 62 # define DPFPRINTF(format, x...) ((void)0) 63 # endif /* PFDEBUG */ 64 #endif /* _KERNEL */ 65 66 67 SLIST_HEAD(pf_osfp_list, pf_os_fingerprint) pf_osfp_list; 68 pool_t pf_osfp_entry_pl; 69 pool_t pf_osfp_pl; 70 71 struct pf_os_fingerprint *pf_osfp_find(struct pf_osfp_list *, 72 struct pf_os_fingerprint *, u_int8_t); 73 struct pf_os_fingerprint *pf_osfp_find_exact(struct pf_osfp_list *, 74 struct pf_os_fingerprint *); 75 void pf_osfp_insert(struct pf_osfp_list *, 76 struct pf_os_fingerprint *); 77 78 79 #ifdef _KERNEL 80 /* 81 * Passively fingerprint the OS of the host (IPv4 TCP SYN packets only) 82 * Returns the list of possible OSes. 83 */ 84 struct pf_osfp_enlist * 85 pf_osfp_fingerprint(struct pf_pdesc *pd, struct mbuf *m, int off, 86 const struct tcphdr *tcp) 87 { 88 struct ip *ip; 89 char hdr[60]; 90 91 /* XXX don't have a fingerprint database for IPv6 :-( */ 92 if (pd->af != PF_INET || pd->proto != IPPROTO_TCP || (tcp->th_off << 2) 93 < sizeof(*tcp)) 94 return (NULL); 95 96 ip = mtod(m, struct ip *); 97 if (!pf_pull_hdr(m, off, hdr, tcp->th_off << 2, NULL, NULL, pd->af)) 98 return (NULL); 99 100 return (pf_osfp_fingerprint_hdr(ip, (struct tcphdr *)hdr)); 101 } 102 #endif /* _KERNEL */ 103 104 struct pf_osfp_enlist * 105 pf_osfp_fingerprint_hdr(const struct ip *ip, const struct tcphdr *tcp) 106 { 107 struct pf_os_fingerprint fp, *fpresult; 108 int cnt, optlen = 0; 109 u_int8_t *optp; 110 111 if ((tcp->th_flags & (TH_SYN|TH_ACK)) != TH_SYN || (ip->ip_off & 112 htons(IP_OFFMASK))) 113 return (NULL); 114 115 memset(&fp, 0, sizeof(fp)); 116 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 fp.fp_wsize = ntohs(tcp->th_win); 122 123 124 cnt = (tcp->th_off << 2) - sizeof(*tcp); 125 optp = (caddr_t)tcp + sizeof(*tcp); 126 for (; cnt > 0; cnt -= optlen, optp += optlen) { 127 if (*optp == TCPOPT_EOL) 128 break; 129 130 fp.fp_optcnt++; 131 if (*optp == TCPOPT_NOP) { 132 fp.fp_tcpopts = (fp.fp_tcpopts << PF_OSFP_TCPOPT_BITS) | 133 PF_OSFP_TCPOPT_NOP; 134 optlen = 1; 135 } else { 136 if (cnt < 2) 137 return (NULL); 138 optlen = optp[1]; 139 if (optlen > cnt || optlen < 2) 140 return (NULL); 141 switch (*optp) { 142 case TCPOPT_MAXSEG: 143 if (optlen >= TCPOLEN_MAXSEG) 144 memcpy(&fp.fp_mss, &optp[2], 145 sizeof(fp.fp_mss)); 146 fp.fp_tcpopts = (fp.fp_tcpopts << 147 PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_MSS; 148 NTOHS(fp.fp_mss); 149 break; 150 case TCPOPT_WINDOW: 151 if (optlen >= TCPOLEN_WINDOW) 152 memcpy(&fp.fp_wscale, &optp[2], 153 sizeof(fp.fp_wscale)); 154 NTOHS(fp.fp_wscale); 155 fp.fp_tcpopts = (fp.fp_tcpopts << 156 PF_OSFP_TCPOPT_BITS) | 157 PF_OSFP_TCPOPT_WSCALE; 158 break; 159 case TCPOPT_SACK_PERMITTED: 160 fp.fp_tcpopts = (fp.fp_tcpopts << 161 PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_SACK; 162 break; 163 case TCPOPT_TIMESTAMP: 164 if (optlen >= TCPOLEN_TIMESTAMP) { 165 u_int32_t ts; 166 memcpy(&ts, &optp[2], sizeof(ts)); 167 if (ts == 0) 168 fp.fp_flags |= PF_OSFP_TS0; 169 170 } 171 fp.fp_tcpopts = (fp.fp_tcpopts << 172 PF_OSFP_TCPOPT_BITS) | PF_OSFP_TCPOPT_TS; 173 break; 174 default: 175 return (NULL); 176 } 177 } 178 optlen = MAX(optlen, 1); /* paranoia */ 179 } 180 181 DPFPRINTF("fingerprinted %s:%d %d:%d:%d:%d:%llx (%d) " 182 "(TS=%s,M=%s%d,W=%s%d)\n", 183 inet_ntoa(ip->ip_src), ntohs(tcp->th_sport), 184 fp.fp_wsize, fp.fp_ttl, (fp.fp_flags & PF_OSFP_DF) != 0, 185 fp.fp_psize, (long long int)fp.fp_tcpopts, fp.fp_optcnt, 186 (fp.fp_flags & PF_OSFP_TS0) ? "0" : "", 187 (fp.fp_flags & PF_OSFP_MSS_MOD) ? "%" : 188 (fp.fp_flags & PF_OSFP_MSS_DC) ? "*" : "", 189 fp.fp_mss, 190 (fp.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" : 191 (fp.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "", 192 fp.fp_wscale); 193 194 if ((fpresult = pf_osfp_find(&pf_osfp_list, &fp, 195 PF_OSFP_MAXTTL_OFFSET))) 196 return (&fpresult->fp_oses); 197 return (NULL); 198 } 199 200 /* Match a fingerprint ID against a list of OSes */ 201 int 202 pf_osfp_match(struct pf_osfp_enlist *list, pf_osfp_t os) 203 { 204 struct pf_osfp_entry *entry; 205 int os_class, os_version, os_subtype; 206 int en_class, en_version, en_subtype; 207 208 if (os == PF_OSFP_ANY) 209 return (1); 210 if (list == NULL) { 211 DPFPRINTF("osfp no match against %x\n", os); 212 return (os == PF_OSFP_UNKNOWN); 213 } 214 PF_OSFP_UNPACK(os, os_class, os_version, os_subtype); 215 SLIST_FOREACH(entry, list, fp_entry) { 216 PF_OSFP_UNPACK(entry->fp_os, en_class, en_version, en_subtype); 217 if ((os_class == PF_OSFP_ANY || en_class == os_class) && 218 (os_version == PF_OSFP_ANY || en_version == os_version) && 219 (os_subtype == PF_OSFP_ANY || en_subtype == os_subtype)) { 220 DPFPRINTF("osfp matched %s %s %s %x==%x\n", 221 entry->fp_class_nm, entry->fp_version_nm, 222 entry->fp_subtype_nm, os, entry->fp_os); 223 return (1); 224 } 225 } 226 DPFPRINTF("fingerprint 0x%x didn't match\n", os); 227 return (0); 228 } 229 230 /* Initialize the OS fingerprint system */ 231 void 232 pf_osfp_initialize(void) 233 { 234 pool_init(&pf_osfp_entry_pl, sizeof(struct pf_osfp_entry), 0, 0, 0, 235 "pfosfpen", NULL); 236 pool_init(&pf_osfp_pl, sizeof(struct pf_os_fingerprint), 0, 0, 0, 237 "pfosfp", NULL); 238 SLIST_INIT(&pf_osfp_list); 239 } 240 241 /* Flush the fingerprint list */ 242 void 243 pf_osfp_flush(void) 244 { 245 struct pf_os_fingerprint *fp; 246 struct pf_osfp_entry *entry; 247 248 while ((fp = SLIST_FIRST(&pf_osfp_list))) { 249 SLIST_REMOVE_HEAD(&pf_osfp_list, fp_next); 250 while ((entry = SLIST_FIRST(&fp->fp_oses))) { 251 SLIST_REMOVE_HEAD(&fp->fp_oses, fp_entry); 252 pool_put(&pf_osfp_entry_pl, entry); 253 } 254 pool_put(&pf_osfp_pl, fp); 255 } 256 } 257 258 259 /* Add a fingerprint */ 260 int 261 pf_osfp_add(struct pf_osfp_ioctl *fpioc) 262 { 263 struct pf_os_fingerprint *fp, fpadd; 264 struct pf_osfp_entry *entry; 265 266 memset(&fpadd, 0, sizeof(fpadd)); 267 fpadd.fp_tcpopts = fpioc->fp_tcpopts; 268 fpadd.fp_wsize = fpioc->fp_wsize; 269 fpadd.fp_psize = fpioc->fp_psize; 270 fpadd.fp_mss = fpioc->fp_mss; 271 fpadd.fp_flags = fpioc->fp_flags; 272 fpadd.fp_optcnt = fpioc->fp_optcnt; 273 fpadd.fp_wscale = fpioc->fp_wscale; 274 fpadd.fp_ttl = fpioc->fp_ttl; 275 276 DPFPRINTF("adding osfp %s %s %s = %s%d:%d:%d:%s%d:0x%llx %d " 277 "(TS=%s,M=%s%d,W=%s%d) %x\n", 278 fpioc->fp_os.fp_class_nm, fpioc->fp_os.fp_version_nm, 279 fpioc->fp_os.fp_subtype_nm, 280 (fpadd.fp_flags & PF_OSFP_WSIZE_MOD) ? "%" : 281 (fpadd.fp_flags & PF_OSFP_WSIZE_MSS) ? "S" : 282 (fpadd.fp_flags & PF_OSFP_WSIZE_MTU) ? "T" : 283 (fpadd.fp_flags & PF_OSFP_WSIZE_DC) ? "*" : "", 284 fpadd.fp_wsize, 285 fpadd.fp_ttl, 286 (fpadd.fp_flags & PF_OSFP_DF) ? 1 : 0, 287 (fpadd.fp_flags & PF_OSFP_PSIZE_MOD) ? "%" : 288 (fpadd.fp_flags & PF_OSFP_PSIZE_DC) ? "*" : "", 289 fpadd.fp_psize, 290 (long long int)fpadd.fp_tcpopts, fpadd.fp_optcnt, 291 (fpadd.fp_flags & PF_OSFP_TS0) ? "0" : "", 292 (fpadd.fp_flags & PF_OSFP_MSS_MOD) ? "%" : 293 (fpadd.fp_flags & PF_OSFP_MSS_DC) ? "*" : "", 294 fpadd.fp_mss, 295 (fpadd.fp_flags & PF_OSFP_WSCALE_MOD) ? "%" : 296 (fpadd.fp_flags & PF_OSFP_WSCALE_DC) ? "*" : "", 297 fpadd.fp_wscale, 298 fpioc->fp_os.fp_os); 299 300 301 if ((fp = pf_osfp_find_exact(&pf_osfp_list, &fpadd))) { 302 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) { 303 if (PF_OSFP_ENTRY_EQ(entry, &fpioc->fp_os)) 304 return (EEXIST); 305 } 306 if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL) 307 return (ENOMEM); 308 } else { 309 if ((fp = pool_get(&pf_osfp_pl, PR_NOWAIT)) == NULL) 310 return (ENOMEM); 311 memset(fp, 0, sizeof(*fp)); 312 fp->fp_tcpopts = fpioc->fp_tcpopts; 313 fp->fp_wsize = fpioc->fp_wsize; 314 fp->fp_psize = fpioc->fp_psize; 315 fp->fp_mss = fpioc->fp_mss; 316 fp->fp_flags = fpioc->fp_flags; 317 fp->fp_optcnt = fpioc->fp_optcnt; 318 fp->fp_wscale = fpioc->fp_wscale; 319 fp->fp_ttl = fpioc->fp_ttl; 320 SLIST_INIT(&fp->fp_oses); 321 if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL) { 322 pool_put(&pf_osfp_pl, fp); 323 return (ENOMEM); 324 } 325 pf_osfp_insert(&pf_osfp_list, fp); 326 } 327 memcpy(entry, &fpioc->fp_os, sizeof(*entry)); 328 329 /* Make sure the strings are NUL terminated */ 330 entry->fp_class_nm[sizeof(entry->fp_class_nm)-1] = '\0'; 331 entry->fp_version_nm[sizeof(entry->fp_version_nm)-1] = '\0'; 332 entry->fp_subtype_nm[sizeof(entry->fp_subtype_nm)-1] = '\0'; 333 334 SLIST_INSERT_HEAD(&fp->fp_oses, entry, fp_entry); 335 336 #ifdef PFDEBUG 337 if ((fp = pf_osfp_validate())) 338 printf("Invalid fingerprint list\n"); 339 #endif /* PFDEBUG */ 340 return (0); 341 } 342 343 344 /* Find a fingerprint in the list */ 345 struct pf_os_fingerprint * 346 pf_osfp_find(struct pf_osfp_list *list, struct pf_os_fingerprint *find, 347 u_int8_t ttldiff) 348 { 349 struct pf_os_fingerprint *f; 350 351 #define MATCH_INT(_MOD, _DC, _field) \ 352 if ((f->fp_flags & _DC) == 0) { \ 353 if ((f->fp_flags & _MOD) == 0) { \ 354 if (f->_field != find->_field) \ 355 continue; \ 356 } else { \ 357 if (f->_field == 0 || find->_field % f->_field) \ 358 continue; \ 359 } \ 360 } 361 362 SLIST_FOREACH(f, list, fp_next) { 363 if (f->fp_tcpopts != find->fp_tcpopts || 364 f->fp_optcnt != find->fp_optcnt || 365 f->fp_ttl < find->fp_ttl || 366 f->fp_ttl - find->fp_ttl > ttldiff || 367 (f->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)) != 368 (find->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0))) 369 continue; 370 371 MATCH_INT(PF_OSFP_PSIZE_MOD, PF_OSFP_PSIZE_DC, fp_psize) 372 MATCH_INT(PF_OSFP_MSS_MOD, PF_OSFP_MSS_DC, fp_mss) 373 MATCH_INT(PF_OSFP_WSCALE_MOD, PF_OSFP_WSCALE_DC, fp_wscale) 374 if ((f->fp_flags & PF_OSFP_WSIZE_DC) == 0) { 375 if (f->fp_flags & PF_OSFP_WSIZE_MSS) { 376 if (find->fp_mss == 0) 377 continue; 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 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 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, &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 /* Validate that each signature is reachable */ 488 struct pf_os_fingerprint * 489 pf_osfp_validate(void) 490 { 491 struct pf_os_fingerprint *f, *f2, find; 492 493 SLIST_FOREACH(f, &pf_osfp_list, fp_next) { 494 memcpy(&find, f, sizeof(find)); 495 496 /* We do a few MSS/th_win percolations to make things unique */ 497 if (find.fp_mss == 0) 498 find.fp_mss = 128; 499 if (f->fp_flags & PF_OSFP_WSIZE_MSS) 500 find.fp_wsize *= find.fp_mss, 1; 501 else if (f->fp_flags & PF_OSFP_WSIZE_MTU) 502 find.fp_wsize *= (find.fp_mss + 40); 503 else if (f->fp_flags & PF_OSFP_WSIZE_MOD) 504 find.fp_wsize *= 2; 505 if (f != (f2 = pf_osfp_find(&pf_osfp_list, &find, 0))) { 506 if (f2) 507 printf("Found \"%s %s %s\" instead of " 508 "\"%s %s %s\"\n", 509 SLIST_FIRST(&f2->fp_oses)->fp_class_nm, 510 SLIST_FIRST(&f2->fp_oses)->fp_version_nm, 511 SLIST_FIRST(&f2->fp_oses)->fp_subtype_nm, 512 SLIST_FIRST(&f->fp_oses)->fp_class_nm, 513 SLIST_FIRST(&f->fp_oses)->fp_version_nm, 514 SLIST_FIRST(&f->fp_oses)->fp_subtype_nm); 515 else 516 printf("Couldn't find \"%s %s %s\"\n", 517 SLIST_FIRST(&f->fp_oses)->fp_class_nm, 518 SLIST_FIRST(&f->fp_oses)->fp_version_nm, 519 SLIST_FIRST(&f->fp_oses)->fp_subtype_nm); 520 return (f); 521 } 522 } 523 return (NULL); 524 } 525