1 /* $OpenBSD: pfctl_osfp.c,v 1.15 2006/12/13 05:10:15 itojun Exp $ */
4 * Copyright (c) 2003 Mike Frantzen <frantzen@openbsd.org>
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.
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
24 #include <net/pf/pfvar.h>
26 #include <netinet/in_systm.h>
27 #include <netinet/ip.h>
28 #include <netinet/ip6.h>
37 #include "pfctl_parser.h"
41 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
44 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
49 # define DEBUG(fp, str, v...) \
50 fprintf(stderr, "%s:%s:%s " str "\n", (fp)->fp_os.fp_class_nm, \
51 (fp)->fp_os.fp_version_nm, (fp)->fp_os.fp_subtype_nm , ## v);
53 # define DEBUG(fp, str, v...) ((void)0)
58 LIST_HEAD(name_list
, name_entry
);
60 LIST_ENTRY(name_entry
) nm_entry
;
62 char nm_name
[PF_OSFP_LEN
];
64 struct name_list nm_sublist
;
67 struct name_list classes
= LIST_HEAD_INITIALIZER(&classes
);
69 int fingerprint_count
;
71 void add_fingerprint(int, int, struct pf_osfp_ioctl
*);
72 struct name_entry
*fingerprint_name_entry(struct name_list
*, char *);
73 void pfctl_flush_my_fingerprints(struct name_list
*);
74 char *get_field(char **, size_t *, int *);
75 int get_int(char **, size_t *, int *, int *, const char *,
76 int, int, const char *, int);
77 int get_str(char **, size_t *, char **, const char *, int,
79 int get_tcpopts(const char *, int, const char *,
80 pf_tcpopts_t
*, int *, int *, int *, int *, int *,
82 void import_fingerprint(struct pf_osfp_ioctl
*);
83 const char *print_ioctl(struct pf_osfp_ioctl
*);
84 void print_name_list(int, struct name_list
*, const char *);
85 void sort_name_list(int, struct name_list
*);
86 struct name_entry
*lookup_name_list(struct name_list
*, const char *);
88 /* Load fingerprints from a file */
90 pfctl_file_fingerprints(int dev
, int opts
, const char *fp_filename
)
96 int window
, w_mod
, ttl
, df
, psize
, p_mod
, mss
, mss_mod
, wscale
,
97 wscale_mod
, optcnt
, ts0
;
98 pf_tcpopts_t packed_tcpopts
;
99 char *class, *version
, *subtype
, *desc
, *tcpopts
;
100 struct pf_osfp_ioctl fp
;
102 pfctl_flush_my_fingerprints(&classes
);
104 if ((in
= pfctl_fopen(fp_filename
, "r")) == NULL
) {
105 warn("%s", fp_filename
);
108 class = version
= subtype
= desc
= tcpopts
= NULL
;
110 if ((opts
& PF_OPT_NOACTION
) == 0)
111 pfctl_clear_fingerprints(dev
, opts
);
113 while ((line
= fgetln(in
, &len
)) != NULL
) {
125 class = version
= subtype
= desc
= tcpopts
= NULL
;
126 memset(&fp
, 0, sizeof(fp
));
128 /* Chop off comment */
129 for (i
= 0; i
< len
; i
++)
130 if (line
[i
] == '#') {
134 /* Chop off whitespace */
135 while (len
> 0 && isspace(line
[len
- 1]))
137 while (len
> 0 && isspace(line
[0])) {
144 #define T_DC 0x01 /* Allow don't care */
145 #define T_MSS 0x02 /* Allow MSS multiple */
146 #define T_MTU 0x04 /* Allow MTU multiple */
147 #define T_MOD 0x08 /* Allow modulus */
149 #define GET_INT(v, mod, n, ty, mx) \
150 get_int(&line, &len, &v, mod, n, ty, mx, fp_filename, lineno)
151 #define GET_STR(v, n, mn) \
152 get_str(&line, &len, &v, n, mn, fp_filename, lineno)
154 if (GET_INT(window
, &w_mod
, "window size", T_DC
|T_MSS
|T_MTU
|
156 GET_INT(ttl
, NULL
, "ttl", 0, 0xff) ||
157 GET_INT(df
, NULL
, "don't fragment frag", 0, 1) ||
158 GET_INT(psize
, &p_mod
, "overall packet size", T_MOD
|T_DC
,
160 GET_STR(tcpopts
, "TCP Options", 1) ||
161 GET_STR(class, "OS class", 1) ||
162 GET_STR(version
, "OS version", 0) ||
163 GET_STR(subtype
, "OS subtype", 0) ||
164 GET_STR(desc
, "OS description", 2))
166 if (get_tcpopts(fp_filename
, lineno
, tcpopts
, &packed_tcpopts
,
167 &optcnt
, &mss
, &mss_mod
, &wscale
, &wscale_mod
, &ts0
))
170 fprintf(stderr
, "%s:%d excess field\n", fp_filename
,
177 fp
.fp_flags
|= PF_OSFP_DF
;
182 fp
.fp_flags
|= PF_OSFP_WSIZE_DC
;
185 fp
.fp_flags
|= PF_OSFP_WSIZE_MSS
;
188 fp
.fp_flags
|= PF_OSFP_WSIZE_MTU
;
191 fp
.fp_flags
|= PF_OSFP_WSIZE_MOD
;
194 fp
.fp_wsize
= window
;
198 fp
.fp_flags
|= PF_OSFP_PSIZE_DC
;
201 fp
.fp_flags
|= PF_OSFP_PSIZE_MOD
;
206 switch (wscale_mod
) {
208 fp
.fp_flags
|= PF_OSFP_WSCALE_DC
;
211 fp
.fp_flags
|= PF_OSFP_WSCALE_MOD
;
213 fp
.fp_wscale
= wscale
;
217 fp
.fp_flags
|= PF_OSFP_MSS_DC
;
220 fp
.fp_flags
|= PF_OSFP_MSS_MOD
;
225 fp
.fp_tcpopts
= packed_tcpopts
;
226 fp
.fp_optcnt
= optcnt
;
228 fp
.fp_flags
|= PF_OSFP_TS0
;
231 fp
.fp_os
.fp_enflags
|= PF_OSFP_GENERIC
;
233 fp
.fp_os
.fp_enflags
|= PF_OSFP_NODETAIL
;
235 if (class[0] == '@' || class[0] == '*')
236 strlcpy(fp
.fp_os
.fp_class_nm
, class + 1,
237 sizeof(fp
.fp_os
.fp_class_nm
));
239 strlcpy(fp
.fp_os
.fp_class_nm
, class,
240 sizeof(fp
.fp_os
.fp_class_nm
));
241 strlcpy(fp
.fp_os
.fp_version_nm
, version
,
242 sizeof(fp
.fp_os
.fp_version_nm
));
243 strlcpy(fp
.fp_os
.fp_subtype_nm
, subtype
,
244 sizeof(fp
.fp_os
.fp_subtype_nm
));
246 add_fingerprint(dev
, opts
, &fp
);
248 fp
.fp_flags
|= (PF_OSFP_DF
| PF_OSFP_INET6
);
249 fp
.fp_psize
+= sizeof(struct ip6_hdr
) - sizeof(struct ip
);
250 add_fingerprint(dev
, opts
, &fp
);
266 if (opts
& PF_OPT_VERBOSE2
)
267 printf("Loaded %d passive OS fingerprints\n",
272 /* flush the kernel's fingerprints */
274 pfctl_clear_fingerprints(int dev
, int opts __unused
)
276 if (ioctl(dev
, DIOCOSFPFLUSH
))
277 err(1, "DIOCOSFPFLUSH");
280 /* flush pfctl's view of the fingerprints */
282 pfctl_flush_my_fingerprints(struct name_list
*list
)
284 struct name_entry
*nm
;
286 while ((nm
= LIST_FIRST(list
)) != NULL
) {
287 LIST_REMOVE(nm
, nm_entry
);
288 pfctl_flush_my_fingerprints(&nm
->nm_sublist
);
291 fingerprint_count
= 0;
295 /* Fetch the active fingerprints from the kernel */
297 pfctl_load_fingerprints(int dev
, int opts __unused
)
299 struct pf_osfp_ioctl io
;
302 pfctl_flush_my_fingerprints(&classes
);
304 for (i
= 0; i
>= 0; i
++) {
305 memset(&io
, 0, sizeof(io
));
307 if (ioctl(dev
, DIOCOSFPGET
, &io
)) {
313 import_fingerprint(&io
);
318 /* List the fingerprints */
320 pfctl_show_fingerprints(int opts
)
322 if (LIST_FIRST(&classes
) != NULL
) {
323 if (opts
& PF_OPT_SHOWALL
) {
324 pfctl_print_title("OS FINGERPRINTS:");
325 printf("%u fingerprints loaded\n", fingerprint_count
);
327 printf("Class\tVersion\tSubtype(subversion)\n");
328 printf("-----\t-------\t-------------------\n");
329 sort_name_list(opts
, &classes
);
330 print_name_list(opts
, &classes
, "");
335 /* Lookup a fingerprint */
337 pfctl_get_fingerprint(const char *name
)
339 struct name_entry
*nm
, *class_nm
, *version_nm
, *subtype_nm
;
340 pf_osfp_t ret
= PF_OSFP_NOMATCH
;
341 int class, version
, subtype
;
342 int unp_class
, unp_version
, unp_subtype
;
343 int wr_len
, version_len
, subtype_len
;
346 if (strcasecmp(name
, "unknown") == 0)
347 return (PF_OSFP_UNKNOWN
);
349 /* Try most likely no version and no subtype */
350 if ((nm
= lookup_name_list(&classes
, name
))) {
352 version
= PF_OSFP_ANY
;
353 subtype
= PF_OSFP_ANY
;
357 /* Chop it up into class/version/subtype */
359 if ((wr_name
= strdup(name
)) == NULL
)
361 if ((ptr
= strchr(wr_name
, ' ')) == NULL
) {
363 return (PF_OSFP_NOMATCH
);
367 /* The class is easy to find since it is delimited by a space */
368 if ((class_nm
= lookup_name_list(&classes
, wr_name
)) == NULL
) {
370 return (PF_OSFP_NOMATCH
);
372 class = class_nm
->nm_num
;
375 if ((version_nm
= lookup_name_list(&class_nm
->nm_sublist
, ptr
)))
377 version
= version_nm
->nm_num
;
378 subtype
= PF_OSFP_ANY
;
385 * There must be a version and a subtype.
386 * We'll do some fuzzy matching to pick up things like:
387 * Linux 2.2.14 (version=2.2 subtype=14)
388 * FreeBSD 4.0-STABLE (version=4.0 subtype=STABLE)
389 * Windows 2000 SP2 (version=2000 subtype=SP2)
391 #define CONNECTOR(x) ((x) == '.' || (x) == ' ' || (x) == '\t' || (x) == '-')
392 wr_len
= strlen(ptr
);
393 LIST_FOREACH(version_nm
, &class_nm
->nm_sublist
, nm_entry
) {
394 version_len
= strlen(version_nm
->nm_name
);
395 if (wr_len
< version_len
+ 2 ||
396 !CONNECTOR(ptr
[version_len
]))
398 /* first part of the string must be version */
399 if (strncasecmp(ptr
, version_nm
->nm_name
,
403 LIST_FOREACH(subtype_nm
, &version_nm
->nm_sublist
,
405 subtype_len
= strlen(subtype_nm
->nm_name
);
406 if (wr_len
!= version_len
+ subtype_len
+ 1)
409 /* last part of the string must be subtype */
410 if (strcasecmp(&ptr
[version_len
+1],
411 subtype_nm
->nm_name
) != 0)
415 version
= version_nm
->nm_num
;
416 subtype
= subtype_nm
->nm_num
;
423 return (PF_OSFP_NOMATCH
);
427 PF_OSFP_PACK(ret
, class, version
, subtype
);
428 if (ret
!= PF_OSFP_NOMATCH
) {
429 PF_OSFP_UNPACK(ret
, unp_class
, unp_version
, unp_subtype
);
430 if (class != unp_class
) {
431 fprintf(stderr
, "warning: fingerprint table overflowed "
433 return (PF_OSFP_NOMATCH
);
435 if (version
!= unp_version
) {
436 fprintf(stderr
, "warning: fingerprint table overflowed "
438 return (PF_OSFP_NOMATCH
);
440 if (subtype
!= unp_subtype
) {
441 fprintf(stderr
, "warning: fingerprint table overflowed "
443 return (PF_OSFP_NOMATCH
);
446 if (ret
== PF_OSFP_ANY
) {
447 /* should never happen */
448 fprintf(stderr
, "warning: fingerprint packed to 'any'\n");
449 return (PF_OSFP_NOMATCH
);
455 /* Lookup a fingerprint name by ID */
457 pfctl_lookup_fingerprint(pf_osfp_t fp
, char *buf
, size_t len
)
459 int class, version
, subtype
;
460 struct name_list
*list
;
461 struct name_entry
*nm
;
463 char *class_name
, *version_name
, *subtype_name
;
464 class_name
= version_name
= subtype_name
= NULL
;
466 if (fp
== PF_OSFP_UNKNOWN
) {
467 strlcpy(buf
, "unknown", len
);
470 if (fp
== PF_OSFP_ANY
) {
471 strlcpy(buf
, "any", len
);
475 PF_OSFP_UNPACK(fp
, class, version
, subtype
);
476 if (class >= (1 << _FP_CLASS_BITS
) ||
477 version
>= (1 << _FP_VERSION_BITS
) ||
478 subtype
>= (1 << _FP_SUBTYPE_BITS
)) {
479 warnx("PF_OSFP_UNPACK(0x%x) failed!!", fp
);
480 strlcpy(buf
, "nomatch", len
);
484 LIST_FOREACH(nm
, &classes
, nm_entry
) {
485 if (nm
->nm_num
== class) {
486 class_name
= nm
->nm_name
;
487 if (version
== PF_OSFP_ANY
)
489 list
= &nm
->nm_sublist
;
490 LIST_FOREACH(nm
, list
, nm_entry
) {
491 if (nm
->nm_num
== version
) {
492 version_name
= nm
->nm_name
;
493 if (subtype
== PF_OSFP_ANY
)
495 list
= &nm
->nm_sublist
;
496 LIST_FOREACH(nm
, list
, nm_entry
) {
497 if (nm
->nm_num
== subtype
) {
502 } /* foreach subtype */
503 strlcpy(buf
, "nomatch", len
);
506 } /* foreach version */
507 strlcpy(buf
, "nomatch", len
);
510 } /* foreach class */
512 strlcpy(buf
, "nomatch", len
);
516 snprintf(buf
, len
, "%s", class_name
);
518 strlcat(buf
, " ", len
);
519 strlcat(buf
, version_name
, len
);
521 if (strchr(version_name
, ' '))
522 strlcat(buf
, " ", len
);
523 else if (strchr(version_name
, '.') &&
524 isdigit(*subtype_name
))
525 strlcat(buf
, ".", len
);
527 strlcat(buf
, " ", len
);
528 strlcat(buf
, subtype_name
, len
);
534 /* lookup a name in a list */
536 lookup_name_list(struct name_list
*list
, const char *name
)
538 struct name_entry
*nm
;
539 LIST_FOREACH(nm
, list
, nm_entry
)
540 if (strcasecmp(name
, nm
->nm_name
) == 0)
548 add_fingerprint(int dev
, int opts
, struct pf_osfp_ioctl
*fp
)
550 struct pf_osfp_ioctl fptmp
;
551 struct name_entry
*nm_class
, *nm_version
, *nm_subtype
;
552 int class, version
, subtype
;
554 /* We expand #-# or #.#-#.# version/subtypes into multiple fingerprints */
555 #define EXPAND(field) do { \
556 int _dot = -1, _start = -1, _end = -1, _i = 0; \
557 /* pick major version out of #.# */ \
558 if (isdigit(fp->field[_i]) && fp->field[_i+1] == '.') { \
559 _dot = fp->field[_i] - '0'; \
562 if (isdigit(fp->field[_i])) \
563 _start = fp->field[_i++] - '0'; \
566 if (isdigit(fp->field[_i])) \
567 _start = (_start * 10) + fp->field[_i++] - '0'; \
568 if (fp->field[_i++] != '-') \
570 if (isdigit(fp->field[_i]) && fp->field[_i+1] == '.' && \
571 fp->field[_i] - '0' == _dot) \
573 else if (_dot != -1) \
575 if (isdigit(fp->field[_i])) \
576 _end = fp->field[_i++] - '0'; \
579 if (isdigit(fp->field[_i])) \
580 _end = (_end * 10) + fp->field[_i++] - '0'; \
581 if (isdigit(fp->field[_i])) \
582 _end = (_end * 10) + fp->field[_i++] - '0'; \
583 if (fp->field[_i] != '\0') \
585 memcpy(&fptmp, fp, sizeof(fptmp)); \
586 for (;_start <= _end; _start++) { \
587 memset(fptmp.field, 0, sizeof(fptmp.field)); \
588 fptmp.fp_os.fp_enflags |= PF_OSFP_EXPANDED; \
590 snprintf(fptmp.field, sizeof(fptmp.field), \
593 snprintf(fptmp.field, sizeof(fptmp.field), \
594 "%d.%d", _dot, _start); \
595 add_fingerprint(dev, opts, &fptmp); \
599 /* We allow "#-#" as a version or subtype and we'll expand it */
600 EXPAND(fp_os
.fp_version_nm
);
601 EXPAND(fp_os
.fp_subtype_nm
);
603 if (strcasecmp(fp
->fp_os
.fp_class_nm
, "nomatch") == 0)
604 errx(1, "fingerprint class \"nomatch\" is reserved");
606 version
= PF_OSFP_ANY
;
607 subtype
= PF_OSFP_ANY
;
609 nm_class
= fingerprint_name_entry(&classes
, fp
->fp_os
.fp_class_nm
);
610 if (nm_class
->nm_num
== 0)
611 nm_class
->nm_num
= ++class_count
;
612 class = nm_class
->nm_num
;
614 nm_version
= fingerprint_name_entry(&nm_class
->nm_sublist
,
615 fp
->fp_os
.fp_version_nm
);
617 if (nm_version
->nm_num
== 0)
618 nm_version
->nm_num
= ++nm_class
->nm_sublist_num
;
619 version
= nm_version
->nm_num
;
620 nm_subtype
= fingerprint_name_entry(&nm_version
->nm_sublist
,
621 fp
->fp_os
.fp_subtype_nm
);
623 if (nm_subtype
->nm_num
== 0)
625 ++nm_version
->nm_sublist_num
;
626 subtype
= nm_subtype
->nm_num
;
631 DEBUG(fp
, "\tsignature %d:%d:%d %s", class, version
, subtype
,
634 PF_OSFP_PACK(fp
->fp_os
.fp_os
, class, version
, subtype
);
637 #ifdef FAKE_PF_KERNEL
638 /* Linked to the sys/net/pf_osfp.c. Call pf_osfp_add() */
639 if ((errno
= pf_osfp_add(fp
)))
641 if ((opts
& PF_OPT_NOACTION
) == 0 && ioctl(dev
, DIOCOSFPADD
, fp
))
642 #endif /* FAKE_PF_KERNEL */
644 if (errno
== EEXIST
) {
645 warn("Duplicate signature for %s %s %s",
646 fp
->fp_os
.fp_class_nm
,
647 fp
->fp_os
.fp_version_nm
,
648 fp
->fp_os
.fp_subtype_nm
);
651 err(1, "DIOCOSFPADD");
656 /* import a fingerprint from the kernel */
658 import_fingerprint(struct pf_osfp_ioctl
*fp
)
660 struct name_entry
*nm_class
, *nm_version
, *nm_subtype
;
661 int class, version
, subtype
;
663 PF_OSFP_UNPACK(fp
->fp_os
.fp_os
, class, version
, subtype
);
665 nm_class
= fingerprint_name_entry(&classes
, fp
->fp_os
.fp_class_nm
);
666 if (nm_class
->nm_num
== 0) {
667 nm_class
->nm_num
= class;
668 class_count
= MAX(class_count
, class);
671 nm_version
= fingerprint_name_entry(&nm_class
->nm_sublist
,
672 fp
->fp_os
.fp_version_nm
);
674 if (nm_version
->nm_num
== 0) {
675 nm_version
->nm_num
= version
;
676 nm_class
->nm_sublist_num
= MAX(nm_class
->nm_sublist_num
,
679 nm_subtype
= fingerprint_name_entry(&nm_version
->nm_sublist
,
680 fp
->fp_os
.fp_subtype_nm
);
682 if (nm_subtype
->nm_num
== 0) {
683 nm_subtype
->nm_num
= subtype
;
684 nm_version
->nm_sublist_num
=
685 MAX(nm_version
->nm_sublist_num
, subtype
);
692 DEBUG(fp
, "import signature %d:%d:%d", class, version
, subtype
);
695 /* Find an entry for a fingerprints class/version/subtype */
697 fingerprint_name_entry(struct name_list
*list
, char *name
)
699 struct name_entry
*nm_entry
;
701 if (name
== NULL
|| strlen(name
) == 0)
704 LIST_FOREACH(nm_entry
, list
, nm_entry
) {
705 if (strcasecmp(nm_entry
->nm_name
, name
) == 0) {
706 /* We'll move this to the front of the list later */
707 LIST_REMOVE(nm_entry
, nm_entry
);
711 if (nm_entry
== NULL
) {
712 nm_entry
= calloc(1, sizeof(*nm_entry
));
713 if (nm_entry
== NULL
)
715 LIST_INIT(&nm_entry
->nm_sublist
);
716 strlcpy(nm_entry
->nm_name
, name
, sizeof(nm_entry
->nm_name
));
718 LIST_INSERT_HEAD(list
, nm_entry
, nm_entry
);
724 print_name_list(int opts
, struct name_list
*nml
, const char *prefix
)
727 struct name_entry
*nm
;
729 LIST_FOREACH(nm
, nml
, nm_entry
) {
730 snprintf(newprefix
, sizeof(newprefix
), "%s%s\t", prefix
,
732 printf("%s\n", newprefix
);
733 print_name_list(opts
, &nm
->nm_sublist
, newprefix
);
738 sort_name_list(int opts
, struct name_list
*nml
)
740 struct name_list
new;
741 struct name_entry
*nm
, *nmsearch
, *nmlast
;
743 /* yes yes, it's a very slow sort. so sue me */
747 while ((nm
= LIST_FIRST(nml
)) != NULL
) {
748 LIST_REMOVE(nm
, nm_entry
);
750 LIST_FOREACH(nmsearch
, &new, nm_entry
) {
751 if (strcasecmp(nmsearch
->nm_name
, nm
->nm_name
) > 0) {
752 LIST_INSERT_BEFORE(nmsearch
, nm
, nm_entry
);
757 if (nmsearch
== NULL
) {
759 LIST_INSERT_AFTER(nmlast
, nm
, nm_entry
);
761 LIST_INSERT_HEAD(&new, nm
, nm_entry
);
764 sort_name_list(opts
, &nm
->nm_sublist
);
767 while ((nm
= LIST_FIRST(&new)) != NULL
) {
768 LIST_REMOVE(nm
, nm_entry
);
770 LIST_INSERT_HEAD(nml
, nm
, nm_entry
);
772 LIST_INSERT_AFTER(nmlast
, nm
, nm_entry
);
777 /* parse the next integer in a formatted config file line */
779 get_int(char **line
, size_t *len
, int *var
, int *mod
,
780 const char *name
, int flags
, int max
, const char *filename
, int lineno
)
790 field
= get_field(line
, len
, &fieldlen
);
794 fprintf(stderr
, "%s:%d empty %s\n", filename
, lineno
, name
);
799 if ((*field
== '%' || *field
== 'S' || *field
== 'T' || *field
== '*')
803 if (mod
&& (flags
& T_MSS
))
809 if (mod
&& (flags
& T_MTU
))
816 fprintf(stderr
, "%s:%d long '%c' %s\n",
817 filename
, lineno
, *field
, name
);
820 if (mod
&& (flags
& T_DC
)) {
825 if (mod
&& (flags
& T_MOD
))
828 fprintf(stderr
, "%s:%d modulus %s must have a "
829 "value\n", filename
, lineno
, name
);
834 if (mod
== NULL
|| *mod
== 0) {
835 fprintf(stderr
, "%s:%d does not allow %c' %s\n",
836 filename
, lineno
, *field
, name
);
842 for (; i
< fieldlen
; i
++) {
843 if (field
[i
] < '0' || field
[i
] > '9') {
844 fprintf(stderr
, "%s:%d non-digit character in %s\n",
845 filename
, lineno
, name
);
848 val
= val
* 10 + field
[i
] - '0';
850 fprintf(stderr
, "%s:%d %s overflowed\n", filename
,
857 fprintf(stderr
, "%s:%d %s value %ld > %d\n", filename
, lineno
,
866 /* parse the next string in a formatted config file line */
868 get_str(char **line
, size_t *len
, char **v
, const char *name
, int minlen
,
869 const char *filename
, int lineno
)
874 ptr
= get_field(line
, len
, &fieldlen
);
877 if (fieldlen
< minlen
) {
878 fprintf(stderr
, "%s:%d too short %s\n", filename
, lineno
, name
);
881 if ((*v
= malloc(fieldlen
+ 1)) == NULL
) {
885 memcpy(*v
, ptr
, fieldlen
);
886 (*v
)[fieldlen
] = '\0';
891 /* Parse out the TCP opts */
893 get_tcpopts(const char *filename
, int lineno
, const char *tcpopts
,
894 pf_tcpopts_t
*packed
, int *optcnt
, int *mss
, int *mss_mod
, int *wscale
,
895 int *wscale_mod
, int *ts0
)
906 if (strcmp(tcpopts
, ".") == 0)
909 for (i
= 0; tcpopts
[i
] && (size_t)*optcnt
< PF_OSFP_MAX_OPTS
;) {
910 switch ((opt
= toupper(tcpopts
[i
++]))) {
911 case 'N': /* FALLTHROUGH */
913 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
914 (opt
== 'N' ? PF_OSFP_TCPOPT_NOP
:
915 PF_OSFP_TCPOPT_SACK
);
917 case 'W': /* FALLTHROUGH */
919 int *this_mod
, *this;
923 this_mod
= wscale_mod
;
931 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
932 (opt
== 'W' ? PF_OSFP_TCPOPT_WSCALE
:
934 if (tcpopts
[i
] == '*' && (tcpopts
[i
+ 1] == '\0' ||
935 tcpopts
[i
+ 1] == ',')) {
941 if (tcpopts
[i
] == '%') {
946 if (!isdigit(tcpopts
[i
])) {
947 fprintf(stderr
, "%s:%d unknown "
948 "character '%c' in %c TCP opt\n",
949 filename
, lineno
, tcpopts
[i
], opt
);
952 *this = (*this * 10) + tcpopts
[i
++] - '0';
953 } while(tcpopts
[i
] != ',' && tcpopts
[i
] != '\0');
957 if (tcpopts
[i
] == '0') {
961 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
966 if (tcpopts
[i
] == '\0')
968 if (tcpopts
[i
] != ',') {
969 fprintf(stderr
, "%s:%d unknown option to %c TCP opt\n",
970 filename
, lineno
, opt
);
979 /* rip the next field ouf of a formatted config file line */
981 get_field(char **line
, size_t *len
, int *fieldlen
)
983 char *ret
, *ptr
= *line
;
987 while (plen
&& isspace(*ptr
)) {
994 for (; plen
> 0 && *ptr
!= ':'; plen
--, ptr
++)
1002 while (*fieldlen
&& isspace(ret
[*fieldlen
- 1]))
1009 print_ioctl(struct pf_osfp_ioctl
*fp
)
1011 static char buf
[1024];
1016 if (fp
->fp_flags
& PF_OSFP_WSIZE_DC
)
1017 strlcat(buf
, "*", sizeof(buf
));
1018 else if (fp
->fp_flags
& PF_OSFP_WSIZE_MSS
)
1019 strlcat(buf
, "S", sizeof(buf
));
1020 else if (fp
->fp_flags
& PF_OSFP_WSIZE_MTU
)
1021 strlcat(buf
, "T", sizeof(buf
));
1023 if (fp
->fp_flags
& PF_OSFP_WSIZE_MOD
)
1024 strlcat(buf
, "%", sizeof(buf
));
1025 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_wsize
);
1026 strlcat(buf
, tmp
, sizeof(buf
));
1028 strlcat(buf
, ":", sizeof(buf
));
1030 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_ttl
);
1031 strlcat(buf
, tmp
, sizeof(buf
));
1032 strlcat(buf
, ":", sizeof(buf
));
1034 if (fp
->fp_flags
& PF_OSFP_DF
)
1035 strlcat(buf
, "1", sizeof(buf
));
1037 strlcat(buf
, "0", sizeof(buf
));
1038 strlcat(buf
, ":", sizeof(buf
));
1040 if (fp
->fp_flags
& PF_OSFP_PSIZE_DC
)
1041 strlcat(buf
, "*", sizeof(buf
));
1043 if (fp
->fp_flags
& PF_OSFP_PSIZE_MOD
)
1044 strlcat(buf
, "%", sizeof(buf
));
1045 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_psize
);
1046 strlcat(buf
, tmp
, sizeof(buf
));
1048 strlcat(buf
, ":", sizeof(buf
));
1050 if (fp
->fp_optcnt
== 0)
1051 strlcat(buf
, ".", sizeof(buf
));
1052 for (i
= fp
->fp_optcnt
- 1; i
>= 0; i
--) {
1053 opt
= fp
->fp_tcpopts
>> (i
* PF_OSFP_TCPOPT_BITS
);
1054 opt
&= (1 << PF_OSFP_TCPOPT_BITS
) - 1;
1056 case PF_OSFP_TCPOPT_NOP
:
1057 strlcat(buf
, "N", sizeof(buf
));
1059 case PF_OSFP_TCPOPT_SACK
:
1060 strlcat(buf
, "S", sizeof(buf
));
1062 case PF_OSFP_TCPOPT_TS
:
1063 strlcat(buf
, "T", sizeof(buf
));
1064 if (fp
->fp_flags
& PF_OSFP_TS0
)
1065 strlcat(buf
, "0", sizeof(buf
));
1067 case PF_OSFP_TCPOPT_MSS
:
1068 strlcat(buf
, "M", sizeof(buf
));
1069 if (fp
->fp_flags
& PF_OSFP_MSS_DC
)
1070 strlcat(buf
, "*", sizeof(buf
));
1072 if (fp
->fp_flags
& PF_OSFP_MSS_MOD
)
1073 strlcat(buf
, "%", sizeof(buf
));
1074 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_mss
);
1075 strlcat(buf
, tmp
, sizeof(buf
));
1078 case PF_OSFP_TCPOPT_WSCALE
:
1079 strlcat(buf
, "W", sizeof(buf
));
1080 if (fp
->fp_flags
& PF_OSFP_WSCALE_DC
)
1081 strlcat(buf
, "*", sizeof(buf
));
1083 if (fp
->fp_flags
& PF_OSFP_WSCALE_MOD
)
1084 strlcat(buf
, "%", sizeof(buf
));
1085 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_wscale
);
1086 strlcat(buf
, tmp
, sizeof(buf
));
1092 strlcat(buf
, ",", sizeof(buf
));
1094 strlcat(buf
, ":", sizeof(buf
));
1096 strlcat(buf
, fp
->fp_os
.fp_class_nm
, sizeof(buf
));
1097 strlcat(buf
, ":", sizeof(buf
));
1098 strlcat(buf
, fp
->fp_os
.fp_version_nm
, sizeof(buf
));
1099 strlcat(buf
, ":", sizeof(buf
));
1100 strlcat(buf
, fp
->fp_os
.fp_subtype_nm
, sizeof(buf
));
1101 strlcat(buf
, ":", sizeof(buf
));
1103 snprintf(tmp
, sizeof(tmp
), "TcpOpts %d 0x%llx", fp
->fp_optcnt
,
1104 (long long int)fp
->fp_tcpopts
);
1105 strlcat(buf
, tmp
, sizeof(buf
));