1 /* $NetBSD: pf_osfp.c,v 1.8 2008/06/18 09:06:27 yamt Exp $ */
2 /* $OpenBSD: pf_osfp.c,v 1.12 2006/12/13 18:14:10 itojun Exp $ */
5 * Copyright (c) 2003 Mike Frantzen <frantzen@w4g.org>
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.
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.
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: pf_osfp.c,v 1.8 2008/06/18 09:06:27 yamt Exp $");
28 #include <sys/param.h>
29 #include <sys/socket.h>
31 # include <sys/systm.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/tcp.h>
41 #include <net/pfvar.h>
43 #include <netinet/ip6.h>
45 #include <netinet6/in6_var.h>
50 # define DPFPRINTF(format, x...) \
51 if (pf_status.debug >= PF_DEBUG_NOISY) \
53 typedef struct pool pool_t
;
56 /* Userland equivalents so we can lend code to tcpdump et al. */
58 # include <arpa/inet.h>
65 # define pool_get(pool, flags) malloc(*(pool))
66 # define pool_put(pool, item) free(item)
67 # define pool_init(pool, size, a, ao, f, m, p) (*(pool)) = (size)
70 # include <sys/stdarg.h>
71 # define DPFPRINTF(format, x...) fprintf(stderr, format , ##x)
73 # define DPFPRINTF(format, x...) ((void)0)
78 SLIST_HEAD(pf_osfp_list
, pf_os_fingerprint
) pf_osfp_list
;
79 pool_t pf_osfp_entry_pl
;
82 struct pf_os_fingerprint
*pf_osfp_find(struct pf_osfp_list
*,
83 struct pf_os_fingerprint
*, u_int8_t
);
84 struct pf_os_fingerprint
*pf_osfp_find_exact(struct pf_osfp_list
*,
85 struct pf_os_fingerprint
*);
86 void pf_osfp_insert(struct pf_osfp_list
*,
87 struct pf_os_fingerprint
*);
92 * Passively fingerprint the OS of the host (IPv4 TCP SYN packets only)
93 * Returns the list of possible OSes.
95 struct pf_osfp_enlist
*
96 pf_osfp_fingerprint(struct pf_pdesc
*pd
, struct mbuf
*m
, int off
,
97 const struct tcphdr
*tcp
)
103 if ((pd
->af
!= PF_INET
&& pd
->af
!= PF_INET6
) ||
104 pd
->proto
!= IPPROTO_TCP
|| (tcp
->th_off
<< 2) < sizeof(*tcp
))
107 if (pd
->af
== PF_INET
) {
108 ip
= mtod(m
, struct ip
*);
109 ip6
= (struct ip6_hdr
*)NULL
;
111 ip
= (struct ip
*)NULL
;
112 ip6
= mtod(m
, struct ip6_hdr
*);
114 if (!pf_pull_hdr(m
, off
, hdr
, tcp
->th_off
<< 2, NULL
, NULL
,
115 pd
->af
)) return (NULL
);
117 return (pf_osfp_fingerprint_hdr(ip
, ip6
, (struct tcphdr
*)hdr
));
121 struct pf_osfp_enlist
*
122 pf_osfp_fingerprint_hdr(const struct ip
*ip
, const struct ip6_hdr
*ip6
, const struct tcphdr
*tcp
)
124 struct pf_os_fingerprint fp
, *fpresult
;
126 const u_int8_t
*optp
;
130 char srcname
[NI_MAXHOST
];
133 if ((tcp
->th_flags
& (TH_SYN
|TH_ACK
)) != TH_SYN
)
136 if ((ip
->ip_off
& htons(IP_OFFMASK
)) != 0)
140 memset(&fp
, 0, sizeof(fp
));
144 struct sockaddr_in sin
;
147 fp
.fp_psize
= ntohs(ip
->ip_len
);
148 fp
.fp_ttl
= ip
->ip_ttl
;
149 if (ip
->ip_off
& htons(IP_DF
))
150 fp
.fp_flags
|= PF_OSFP_DF
;
152 strlcpy(srcname
, inet_ntoa(ip
->ip_src
), sizeof(srcname
));
154 memset(&sin
, 0, sizeof(sin
));
155 sin
.sin_family
= AF_INET
;
156 sin
.sin_len
= sizeof(struct sockaddr_in
);
157 sin
.sin_addr
= ip
->ip_src
;
158 (void)getnameinfo((struct sockaddr
*)&sin
,
159 sizeof(struct sockaddr_in
), srcname
, sizeof(srcname
),
160 NULL
, 0, NI_NUMERICHOST
);
166 struct sockaddr_in6 sin6
;
170 fp
.fp_psize
= sizeof(struct ip6_hdr
) + ntohs(ip6
->ip6_plen
);
171 fp
.fp_ttl
= ip6
->ip6_hlim
;
172 fp
.fp_flags
|= PF_OSFP_DF
;
173 fp
.fp_flags
|= PF_OSFP_INET6
;
176 ip6_sprintf((const struct in6_addr
*)&ip6
->ip6_src
),
179 memset(&sin6
, 0, sizeof(sin6
));
180 sin6
.sin6_family
= AF_INET6
;
181 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
182 sin6
.sin6_addr
= ip6
->ip6_src
;
183 (void)getnameinfo((struct sockaddr
*)&sin6
,
184 sizeof(struct sockaddr_in6
), srcname
, sizeof(srcname
),
185 NULL
, 0, NI_NUMERICHOST
);
191 fp
.fp_wsize
= ntohs(tcp
->th_win
);
194 cnt
= (tcp
->th_off
<< 2) - sizeof(*tcp
);
195 optp
= (const u_int8_t
*)((const char *)tcp
+ sizeof(*tcp
));
196 for (; cnt
> 0; cnt
-= optlen
, optp
+= optlen
) {
197 if (*optp
== TCPOPT_EOL
)
201 if (*optp
== TCPOPT_NOP
) {
202 fp
.fp_tcpopts
= (fp
.fp_tcpopts
<< PF_OSFP_TCPOPT_BITS
) |
209 if (optlen
> cnt
|| optlen
< 2)
213 if (optlen
>= TCPOLEN_MAXSEG
)
214 memcpy(&fp
.fp_mss
, &optp
[2],
216 fp
.fp_tcpopts
= (fp
.fp_tcpopts
<<
217 PF_OSFP_TCPOPT_BITS
) | PF_OSFP_TCPOPT_MSS
;
221 if (optlen
>= TCPOLEN_WINDOW
)
222 memcpy(&fp
.fp_wscale
, &optp
[2],
223 sizeof(fp
.fp_wscale
));
225 fp
.fp_tcpopts
= (fp
.fp_tcpopts
<<
226 PF_OSFP_TCPOPT_BITS
) |
227 PF_OSFP_TCPOPT_WSCALE
;
229 case TCPOPT_SACK_PERMITTED
:
230 fp
.fp_tcpopts
= (fp
.fp_tcpopts
<<
231 PF_OSFP_TCPOPT_BITS
) | PF_OSFP_TCPOPT_SACK
;
233 case TCPOPT_TIMESTAMP
:
234 if (optlen
>= TCPOLEN_TIMESTAMP
) {
236 memcpy(&ts
, &optp
[2], sizeof(ts
));
238 fp
.fp_flags
|= PF_OSFP_TS0
;
241 fp
.fp_tcpopts
= (fp
.fp_tcpopts
<<
242 PF_OSFP_TCPOPT_BITS
) | PF_OSFP_TCPOPT_TS
;
248 optlen
= MAX(optlen
, 1); /* paranoia */
251 DPFPRINTF("fingerprinted %s:%d %d:%d:%d:%d:%llx (%d) "
252 "(TS=%s,M=%s%d,W=%s%d)\n",
253 srcname
, ntohs(tcp
->th_sport
),
254 fp
.fp_wsize
, fp
.fp_ttl
, (fp
.fp_flags
& PF_OSFP_DF
) != 0,
255 fp
.fp_psize
, (long long int)fp
.fp_tcpopts
, fp
.fp_optcnt
,
256 (fp
.fp_flags
& PF_OSFP_TS0
) ? "0" : "",
257 (fp
.fp_flags
& PF_OSFP_MSS_MOD
) ? "%" :
258 (fp
.fp_flags
& PF_OSFP_MSS_DC
) ? "*" : "",
260 (fp
.fp_flags
& PF_OSFP_WSCALE_MOD
) ? "%" :
261 (fp
.fp_flags
& PF_OSFP_WSCALE_DC
) ? "*" : "",
264 if ((fpresult
= pf_osfp_find(&pf_osfp_list
, &fp
,
265 PF_OSFP_MAXTTL_OFFSET
)))
266 return (&fpresult
->fp_oses
);
270 /* Match a fingerprint ID against a list of OSes */
272 pf_osfp_match(struct pf_osfp_enlist
*list
, pf_osfp_t os
)
274 struct pf_osfp_entry
*entry
;
275 int os_class
, os_version
, os_subtype
;
276 int en_class
, en_version
, en_subtype
;
278 if (os
== PF_OSFP_ANY
)
281 DPFPRINTF("osfp no match against %x\n", os
);
282 return (os
== PF_OSFP_UNKNOWN
);
284 PF_OSFP_UNPACK(os
, os_class
, os_version
, os_subtype
);
285 SLIST_FOREACH(entry
, list
, fp_entry
) {
286 PF_OSFP_UNPACK(entry
->fp_os
, en_class
, en_version
, en_subtype
);
287 if ((os_class
== PF_OSFP_ANY
|| en_class
== os_class
) &&
288 (os_version
== PF_OSFP_ANY
|| en_version
== os_version
) &&
289 (os_subtype
== PF_OSFP_ANY
|| en_subtype
== os_subtype
)) {
290 DPFPRINTF("osfp matched %s %s %s %x==%x\n",
291 entry
->fp_class_nm
, entry
->fp_version_nm
,
292 entry
->fp_subtype_nm
, os
, entry
->fp_os
);
296 DPFPRINTF("fingerprint 0x%x didn't match\n", os
);
300 /* Initialize the OS fingerprint system */
302 pf_osfp_initialize(void)
305 pool_init(&pf_osfp_entry_pl
, sizeof(struct pf_osfp_entry
), 0, 0, 0,
306 "pfosfpen", &pool_allocator_nointr
, IPL_NONE
);
307 pool_init(&pf_osfp_pl
, sizeof(struct pf_os_fingerprint
), 0, 0, 0,
308 "pfosfp", &pool_allocator_nointr
, IPL_NONE
);
310 pool_init(&pf_osfp_entry_pl
, sizeof(struct pf_osfp_entry
), 0, 0, 0,
311 "pfosfpen", &pool_allocator_nointr
);
312 pool_init(&pf_osfp_pl
, sizeof(struct pf_os_fingerprint
), 0, 0, 0,
313 "pfosfp", &pool_allocator_nointr
);
314 #endif /* !__NetBSD__ */
315 SLIST_INIT(&pf_osfp_list
);
318 /* Flush the fingerprint list */
322 struct pf_os_fingerprint
*fp
;
323 struct pf_osfp_entry
*entry
;
325 while ((fp
= SLIST_FIRST(&pf_osfp_list
))) {
326 SLIST_REMOVE_HEAD(&pf_osfp_list
, fp_next
);
327 while ((entry
= SLIST_FIRST(&fp
->fp_oses
))) {
328 SLIST_REMOVE_HEAD(&fp
->fp_oses
, fp_entry
);
329 pool_put(&pf_osfp_entry_pl
, entry
);
331 pool_put(&pf_osfp_pl
, fp
);
336 /* Add a fingerprint */
338 pf_osfp_add(struct pf_osfp_ioctl
*fpioc
)
340 struct pf_os_fingerprint
*fp
, fpadd
;
341 struct pf_osfp_entry
*entry
;
343 memset(&fpadd
, 0, sizeof(fpadd
));
344 fpadd
.fp_tcpopts
= fpioc
->fp_tcpopts
;
345 fpadd
.fp_wsize
= fpioc
->fp_wsize
;
346 fpadd
.fp_psize
= fpioc
->fp_psize
;
347 fpadd
.fp_mss
= fpioc
->fp_mss
;
348 fpadd
.fp_flags
= fpioc
->fp_flags
;
349 fpadd
.fp_optcnt
= fpioc
->fp_optcnt
;
350 fpadd
.fp_wscale
= fpioc
->fp_wscale
;
351 fpadd
.fp_ttl
= fpioc
->fp_ttl
;
353 DPFPRINTF("adding osfp %s %s %s = %s%d:%d:%d:%s%d:0x%llx %d "
354 "(TS=%s,M=%s%d,W=%s%d) %x\n",
355 fpioc
->fp_os
.fp_class_nm
, fpioc
->fp_os
.fp_version_nm
,
356 fpioc
->fp_os
.fp_subtype_nm
,
357 (fpadd
.fp_flags
& PF_OSFP_WSIZE_MOD
) ? "%" :
358 (fpadd
.fp_flags
& PF_OSFP_WSIZE_MSS
) ? "S" :
359 (fpadd
.fp_flags
& PF_OSFP_WSIZE_MTU
) ? "T" :
360 (fpadd
.fp_flags
& PF_OSFP_WSIZE_DC
) ? "*" : "",
363 (fpadd
.fp_flags
& PF_OSFP_DF
) ? 1 : 0,
364 (fpadd
.fp_flags
& PF_OSFP_PSIZE_MOD
) ? "%" :
365 (fpadd
.fp_flags
& PF_OSFP_PSIZE_DC
) ? "*" : "",
367 (long long int)fpadd
.fp_tcpopts
, fpadd
.fp_optcnt
,
368 (fpadd
.fp_flags
& PF_OSFP_TS0
) ? "0" : "",
369 (fpadd
.fp_flags
& PF_OSFP_MSS_MOD
) ? "%" :
370 (fpadd
.fp_flags
& PF_OSFP_MSS_DC
) ? "*" : "",
372 (fpadd
.fp_flags
& PF_OSFP_WSCALE_MOD
) ? "%" :
373 (fpadd
.fp_flags
& PF_OSFP_WSCALE_DC
) ? "*" : "",
378 if ((fp
= pf_osfp_find_exact(&pf_osfp_list
, &fpadd
))) {
379 SLIST_FOREACH(entry
, &fp
->fp_oses
, fp_entry
) {
380 if (PF_OSFP_ENTRY_EQ(entry
, &fpioc
->fp_os
))
383 if ((entry
= pool_get(&pf_osfp_entry_pl
, PR_NOWAIT
)) == NULL
)
386 if ((fp
= pool_get(&pf_osfp_pl
, PR_NOWAIT
)) == NULL
)
388 memset(fp
, 0, sizeof(*fp
));
389 fp
->fp_tcpopts
= fpioc
->fp_tcpopts
;
390 fp
->fp_wsize
= fpioc
->fp_wsize
;
391 fp
->fp_psize
= fpioc
->fp_psize
;
392 fp
->fp_mss
= fpioc
->fp_mss
;
393 fp
->fp_flags
= fpioc
->fp_flags
;
394 fp
->fp_optcnt
= fpioc
->fp_optcnt
;
395 fp
->fp_wscale
= fpioc
->fp_wscale
;
396 fp
->fp_ttl
= fpioc
->fp_ttl
;
397 SLIST_INIT(&fp
->fp_oses
);
398 if ((entry
= pool_get(&pf_osfp_entry_pl
, PR_NOWAIT
)) == NULL
) {
399 pool_put(&pf_osfp_pl
, fp
);
402 pf_osfp_insert(&pf_osfp_list
, fp
);
404 memcpy(entry
, &fpioc
->fp_os
, sizeof(*entry
));
406 /* Make sure the strings are NUL terminated */
407 entry
->fp_class_nm
[sizeof(entry
->fp_class_nm
)-1] = '\0';
408 entry
->fp_version_nm
[sizeof(entry
->fp_version_nm
)-1] = '\0';
409 entry
->fp_subtype_nm
[sizeof(entry
->fp_subtype_nm
)-1] = '\0';
411 SLIST_INSERT_HEAD(&fp
->fp_oses
, entry
, fp_entry
);
414 if ((fp
= pf_osfp_validate()))
415 printf("Invalid fingerprint list\n");
421 /* Find a fingerprint in the list */
422 struct pf_os_fingerprint
*
423 pf_osfp_find(struct pf_osfp_list
*list
, struct pf_os_fingerprint
*find
,
426 struct pf_os_fingerprint
*f
;
428 #define MATCH_INT(_MOD, _DC, _field) \
429 if ((f->fp_flags & _DC) == 0) { \
430 if ((f->fp_flags & _MOD) == 0) { \
431 if (f->_field != find->_field) \
434 if (f->_field == 0 || find->_field % f->_field) \
439 SLIST_FOREACH(f
, list
, fp_next
) {
440 if (f
->fp_tcpopts
!= find
->fp_tcpopts
||
441 f
->fp_optcnt
!= find
->fp_optcnt
||
442 f
->fp_ttl
< find
->fp_ttl
||
443 f
->fp_ttl
- find
->fp_ttl
> ttldiff
||
444 (f
->fp_flags
& (PF_OSFP_DF
|PF_OSFP_TS0
)) !=
445 (find
->fp_flags
& (PF_OSFP_DF
|PF_OSFP_TS0
)))
448 MATCH_INT(PF_OSFP_PSIZE_MOD
, PF_OSFP_PSIZE_DC
, fp_psize
)
449 MATCH_INT(PF_OSFP_MSS_MOD
, PF_OSFP_MSS_DC
, fp_mss
)
450 MATCH_INT(PF_OSFP_WSCALE_MOD
, PF_OSFP_WSCALE_DC
, fp_wscale
)
451 if ((f
->fp_flags
& PF_OSFP_WSIZE_DC
) == 0) {
452 if (f
->fp_flags
& PF_OSFP_WSIZE_MSS
) {
453 if (find
->fp_mss
== 0)
456 /* Some "smart" NAT devices and DSL routers will tweak the MSS size and
457 * will set it to whatever is suitable for the link type.
459 #define SMART_MSS 1460
460 if ((find
->fp_wsize
% find
->fp_mss
||
461 find
->fp_wsize
/ find
->fp_mss
!=
463 (find
->fp_wsize
% SMART_MSS
||
464 find
->fp_wsize
/ SMART_MSS
!=
467 } else if (f
->fp_flags
& PF_OSFP_WSIZE_MTU
) {
468 if (find
->fp_mss
== 0)
471 #define MTUOFF (sizeof(struct ip) + sizeof(struct tcphdr))
472 #define SMART_MTU (SMART_MSS + MTUOFF)
473 if ((find
->fp_wsize
% (find
->fp_mss
+ MTUOFF
) ||
474 find
->fp_wsize
/ (find
->fp_mss
+ MTUOFF
) !=
476 (find
->fp_wsize
% SMART_MTU
||
477 find
->fp_wsize
/ SMART_MTU
!=
480 } else if (f
->fp_flags
& PF_OSFP_WSIZE_MOD
) {
481 if (f
->fp_wsize
== 0 || find
->fp_wsize
%
485 if (f
->fp_wsize
!= find
->fp_wsize
)
495 /* Find an exact fingerprint in the list */
496 struct pf_os_fingerprint
*
497 pf_osfp_find_exact(struct pf_osfp_list
*list
, struct pf_os_fingerprint
*find
)
499 struct pf_os_fingerprint
*f
;
501 SLIST_FOREACH(f
, list
, fp_next
) {
502 if (f
->fp_tcpopts
== find
->fp_tcpopts
&&
503 f
->fp_wsize
== find
->fp_wsize
&&
504 f
->fp_psize
== find
->fp_psize
&&
505 f
->fp_mss
== find
->fp_mss
&&
506 f
->fp_flags
== find
->fp_flags
&&
507 f
->fp_optcnt
== find
->fp_optcnt
&&
508 f
->fp_wscale
== find
->fp_wscale
&&
509 f
->fp_ttl
== find
->fp_ttl
)
516 /* Insert a fingerprint into the list */
518 pf_osfp_insert(struct pf_osfp_list
*list
, struct pf_os_fingerprint
*ins
)
520 struct pf_os_fingerprint
*f
, *prev
= NULL
;
522 /* XXX need to go semi tree based. can key on tcp options */
524 SLIST_FOREACH(f
, list
, fp_next
)
527 SLIST_INSERT_AFTER(prev
, ins
, fp_next
);
529 SLIST_INSERT_HEAD(list
, ins
, fp_next
);
532 /* Fill a fingerprint by its number (from an ioctl) */
534 pf_osfp_get(struct pf_osfp_ioctl
*fpioc
)
536 struct pf_os_fingerprint
*fp
;
537 struct pf_osfp_entry
*entry
;
538 int num
= fpioc
->fp_getnum
;
542 memset(fpioc
, 0, sizeof(*fpioc
));
543 SLIST_FOREACH(fp
, &pf_osfp_list
, fp_next
) {
544 SLIST_FOREACH(entry
, &fp
->fp_oses
, fp_entry
) {
546 fpioc
->fp_mss
= fp
->fp_mss
;
547 fpioc
->fp_wsize
= fp
->fp_wsize
;
548 fpioc
->fp_flags
= fp
->fp_flags
;
549 fpioc
->fp_psize
= fp
->fp_psize
;
550 fpioc
->fp_ttl
= fp
->fp_ttl
;
551 fpioc
->fp_wscale
= fp
->fp_wscale
;
552 fpioc
->fp_getnum
= num
;
553 memcpy(&fpioc
->fp_os
, entry
,
554 sizeof(fpioc
->fp_os
));
564 /* Validate that each signature is reachable */
565 struct pf_os_fingerprint
*
566 pf_osfp_validate(void)
568 struct pf_os_fingerprint
*f
, *f2
, find
;
570 SLIST_FOREACH(f
, &pf_osfp_list
, fp_next
) {
571 memcpy(&find
, f
, sizeof(find
));
573 /* We do a few MSS/th_win percolations to make things unique */
574 if (find
.fp_mss
== 0)
576 if (f
->fp_flags
& PF_OSFP_WSIZE_MSS
)
577 find
.fp_wsize
*= find
.fp_mss
, 1;
578 else if (f
->fp_flags
& PF_OSFP_WSIZE_MTU
)
579 find
.fp_wsize
*= (find
.fp_mss
+ 40);
580 else if (f
->fp_flags
& PF_OSFP_WSIZE_MOD
)
582 if (f
!= (f2
= pf_osfp_find(&pf_osfp_list
, &find
, 0))) {
584 printf("Found \"%s %s %s\" instead of "
586 SLIST_FIRST(&f2
->fp_oses
)->fp_class_nm
,
587 SLIST_FIRST(&f2
->fp_oses
)->fp_version_nm
,
588 SLIST_FIRST(&f2
->fp_oses
)->fp_subtype_nm
,
589 SLIST_FIRST(&f
->fp_oses
)->fp_class_nm
,
590 SLIST_FIRST(&f
->fp_oses
)->fp_version_nm
,
591 SLIST_FIRST(&f
->fp_oses
)->fp_subtype_nm
);
593 printf("Couldn't find \"%s %s %s\"\n",
594 SLIST_FIRST(&f
->fp_oses
)->fp_class_nm
,
595 SLIST_FIRST(&f
->fp_oses
)->fp_version_nm
,
596 SLIST_FIRST(&f
->fp_oses
)->fp_subtype_nm
);