pf_osfp.c revision 1.2 1 /* $NetBSD: pf_osfp.c,v 1.2 2004/06/22 14:17:08 itojun Exp $ */
2 /* $OpenBSD: pf_osfp.c,v 1.9 2004/01/04 20:08:42 pvalchev 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", NULL);
242 pool_init(&pf_osfp_pl, sizeof(struct pf_os_fingerprint), 0, 0, 0,
243 "pfosfp", NULL);
244 SLIST_INIT(&pf_osfp_list);
245 }
246
247 /* Flush the fingerprint list */
248 void
249 pf_osfp_flush(void)
250 {
251 struct pf_os_fingerprint *fp;
252 struct pf_osfp_entry *entry;
253
254 while ((fp = SLIST_FIRST(&pf_osfp_list))) {
255 SLIST_REMOVE_HEAD(&pf_osfp_list, fp_next);
256 while ((entry = SLIST_FIRST(&fp->fp_oses))) {
257 SLIST_REMOVE_HEAD(&fp->fp_oses, fp_entry);
258 pool_put(&pf_osfp_entry_pl, entry);
259 }
260 pool_put(&pf_osfp_pl, fp);
261 }
262 }
263
264
265 /* Add a fingerprint */
266 int
267 pf_osfp_add(struct pf_osfp_ioctl *fpioc)
268 {
269 struct pf_os_fingerprint *fp, fpadd;
270 struct pf_osfp_entry *entry;
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 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
306
307 if ((fp = pf_osfp_find_exact(&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 = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL)
313 return (ENOMEM);
314 } else {
315 if ((fp = pool_get(&pf_osfp_pl, PR_NOWAIT)) == NULL)
316 return (ENOMEM);
317 memset(fp, 0, sizeof(*fp));
318 fp->fp_tcpopts = fpioc->fp_tcpopts;
319 fp->fp_wsize = fpioc->fp_wsize;
320 fp->fp_psize = fpioc->fp_psize;
321 fp->fp_mss = fpioc->fp_mss;
322 fp->fp_flags = fpioc->fp_flags;
323 fp->fp_optcnt = fpioc->fp_optcnt;
324 fp->fp_wscale = fpioc->fp_wscale;
325 fp->fp_ttl = fpioc->fp_ttl;
326 SLIST_INIT(&fp->fp_oses);
327 if ((entry = pool_get(&pf_osfp_entry_pl, PR_NOWAIT)) == NULL) {
328 pool_put(&pf_osfp_pl, fp);
329 return (ENOMEM);
330 }
331 pf_osfp_insert(&pf_osfp_list, fp);
332 }
333 memcpy(entry, &fpioc->fp_os, sizeof(*entry));
334
335 /* Make sure the strings are NUL terminated */
336 entry->fp_class_nm[sizeof(entry->fp_class_nm)-1] = '\0';
337 entry->fp_version_nm[sizeof(entry->fp_version_nm)-1] = '\0';
338 entry->fp_subtype_nm[sizeof(entry->fp_subtype_nm)-1] = '\0';
339
340 SLIST_INSERT_HEAD(&fp->fp_oses, entry, fp_entry);
341
342 #ifdef PFDEBUG
343 if ((fp = pf_osfp_validate()))
344 printf("Invalid fingerprint list\n");
345 #endif /* PFDEBUG */
346 return (0);
347 }
348
349
350 /* Find a fingerprint in the list */
351 struct pf_os_fingerprint *
352 pf_osfp_find(struct pf_osfp_list *list, struct pf_os_fingerprint *find,
353 u_int8_t ttldiff)
354 {
355 struct pf_os_fingerprint *f;
356
357 #define MATCH_INT(_MOD, _DC, _field) \
358 if ((f->fp_flags & _DC) == 0) { \
359 if ((f->fp_flags & _MOD) == 0) { \
360 if (f->_field != find->_field) \
361 continue; \
362 } else { \
363 if (f->_field == 0 || find->_field % f->_field) \
364 continue; \
365 } \
366 }
367
368 SLIST_FOREACH(f, list, fp_next) {
369 if (f->fp_tcpopts != find->fp_tcpopts ||
370 f->fp_optcnt != find->fp_optcnt ||
371 f->fp_ttl < find->fp_ttl ||
372 f->fp_ttl - find->fp_ttl > ttldiff ||
373 (f->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)) !=
374 (find->fp_flags & (PF_OSFP_DF|PF_OSFP_TS0)))
375 continue;
376
377 MATCH_INT(PF_OSFP_PSIZE_MOD, PF_OSFP_PSIZE_DC, fp_psize)
378 MATCH_INT(PF_OSFP_MSS_MOD, PF_OSFP_MSS_DC, fp_mss)
379 MATCH_INT(PF_OSFP_WSCALE_MOD, PF_OSFP_WSCALE_DC, fp_wscale)
380 if ((f->fp_flags & PF_OSFP_WSIZE_DC) == 0) {
381 if (f->fp_flags & PF_OSFP_WSIZE_MSS) {
382 if (find->fp_mss == 0)
383 continue;
384
385 /* Some "smart" NAT devices and DSL routers will tweak the MSS size and
386 * will set it to whatever is suitable for the link type.
387 */
388 #define SMART_MSS 1460
389 if ((find->fp_wsize % find->fp_mss ||
390 find->fp_wsize / find->fp_mss !=
391 f->fp_wsize) &&
392 (find->fp_wsize % SMART_MSS ||
393 find->fp_wsize / SMART_MSS !=
394 f->fp_wsize))
395 continue;
396 } else if (f->fp_flags & PF_OSFP_WSIZE_MTU) {
397 if (find->fp_mss == 0)
398 continue;
399
400 #define MTUOFF (sizeof(struct ip) + sizeof(struct tcphdr))
401 #define SMART_MTU (SMART_MSS + MTUOFF)
402 if ((find->fp_wsize % (find->fp_mss + MTUOFF) ||
403 find->fp_wsize / (find->fp_mss + MTUOFF) !=
404 f->fp_wsize) &&
405 (find->fp_wsize % SMART_MTU ||
406 find->fp_wsize / SMART_MTU !=
407 f->fp_wsize))
408 continue;
409 } else if (f->fp_flags & PF_OSFP_WSIZE_MOD) {
410 if (f->fp_wsize == 0 || find->fp_wsize %
411 f->fp_wsize)
412 continue;
413 } else {
414 if (f->fp_wsize != find->fp_wsize)
415 continue;
416 }
417 }
418 return (f);
419 }
420
421 return (NULL);
422 }
423
424 /* Find an exact fingerprint in the list */
425 struct pf_os_fingerprint *
426 pf_osfp_find_exact(struct pf_osfp_list *list, struct pf_os_fingerprint *find)
427 {
428 struct pf_os_fingerprint *f;
429
430 SLIST_FOREACH(f, list, fp_next) {
431 if (f->fp_tcpopts == find->fp_tcpopts &&
432 f->fp_wsize == find->fp_wsize &&
433 f->fp_psize == find->fp_psize &&
434 f->fp_mss == find->fp_mss &&
435 f->fp_flags == find->fp_flags &&
436 f->fp_optcnt == find->fp_optcnt &&
437 f->fp_wscale == find->fp_wscale &&
438 f->fp_ttl == find->fp_ttl)
439 return (f);
440 }
441
442 return (NULL);
443 }
444
445 /* Insert a fingerprint into the list */
446 void
447 pf_osfp_insert(struct pf_osfp_list *list, struct pf_os_fingerprint *ins)
448 {
449 struct pf_os_fingerprint *f, *prev = NULL;
450
451 /* XXX need to go semi tree based. can key on tcp options */
452
453 SLIST_FOREACH(f, list, fp_next)
454 prev = f;
455 if (prev)
456 SLIST_INSERT_AFTER(prev, ins, fp_next);
457 else
458 SLIST_INSERT_HEAD(list, ins, fp_next);
459 }
460
461 /* Fill a fingerprint by its number (from an ioctl) */
462 int
463 pf_osfp_get(struct pf_osfp_ioctl *fpioc)
464 {
465 struct pf_os_fingerprint *fp;
466 struct pf_osfp_entry *entry;
467 int num = fpioc->fp_getnum;
468 int i = 0;
469
470
471 memset(fpioc, 0, sizeof(*fpioc));
472 SLIST_FOREACH(fp, &pf_osfp_list, fp_next) {
473 SLIST_FOREACH(entry, &fp->fp_oses, fp_entry) {
474 if (i++ == num) {
475 fpioc->fp_mss = fp->fp_mss;
476 fpioc->fp_wsize = fp->fp_wsize;
477 fpioc->fp_flags = fp->fp_flags;
478 fpioc->fp_psize = fp->fp_psize;
479 fpioc->fp_ttl = fp->fp_ttl;
480 fpioc->fp_wscale = fp->fp_wscale;
481 fpioc->fp_getnum = num;
482 memcpy(&fpioc->fp_os, entry,
483 sizeof(fpioc->fp_os));
484 return (0);
485 }
486 }
487 }
488
489 return (EBUSY);
490 }
491
492
493 /* Validate that each signature is reachable */
494 struct pf_os_fingerprint *
495 pf_osfp_validate(void)
496 {
497 struct pf_os_fingerprint *f, *f2, find;
498
499 SLIST_FOREACH(f, &pf_osfp_list, fp_next) {
500 memcpy(&find, f, sizeof(find));
501
502 /* We do a few MSS/th_win percolations to make things unique */
503 if (find.fp_mss == 0)
504 find.fp_mss = 128;
505 if (f->fp_flags & PF_OSFP_WSIZE_MSS)
506 find.fp_wsize *= find.fp_mss, 1;
507 else if (f->fp_flags & PF_OSFP_WSIZE_MTU)
508 find.fp_wsize *= (find.fp_mss + 40);
509 else if (f->fp_flags & PF_OSFP_WSIZE_MOD)
510 find.fp_wsize *= 2;
511 if (f != (f2 = pf_osfp_find(&pf_osfp_list, &find, 0))) {
512 if (f2)
513 printf("Found \"%s %s %s\" instead of "
514 "\"%s %s %s\"\n",
515 SLIST_FIRST(&f2->fp_oses)->fp_class_nm,
516 SLIST_FIRST(&f2->fp_oses)->fp_version_nm,
517 SLIST_FIRST(&f2->fp_oses)->fp_subtype_nm,
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 else
522 printf("Couldn't find \"%s %s %s\"\n",
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 return (f);
527 }
528 }
529 return (NULL);
530 }
531