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