1 /* $OpenBSD: pfctl_osfp.c,v 1.8 2004/02/27 10:42:00 henning Exp $ */
2 /* $DragonFly: src/usr.sbin/pfctl/pfctl_osfp.c,v 1.1 2004/09/21 21:25:28 joerg Exp $ */
5 * Copyright (c) 2003 Mike Frantzen <frantzen@openbsd.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.
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <net/pf/pfvar.h>
34 #include "pfctl_parser.h"
38 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
41 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
46 # define DEBUG(fp, str, v...) \
47 fprintf(stderr, "%s:%s:%s " str "\n", (fp)->fp_os.fp_class_nm, \
48 (fp)->fp_os.fp_version_nm, (fp)->fp_os.fp_subtype_nm , ## v);
50 # define DEBUG(fp, str, v...) ((void)0)
55 LIST_HEAD(name_list
, name_entry
);
57 LIST_ENTRY(name_entry
) nm_entry
;
59 char nm_name
[PF_OSFP_LEN
];
61 struct name_list nm_sublist
;
64 struct name_list classes
= LIST_HEAD_INITIALIZER(&classes
);
66 int fingerprint_count
;
68 void add_fingerprint(int, int, struct pf_osfp_ioctl
*);
69 struct name_entry
*fingerprint_name_entry(struct name_list
*, char *);
70 void pfctl_flush_my_fingerprints(struct name_list
*);
71 char *get_field(char **, size_t *, int *);
72 int get_int(char **, size_t *, int *, int *, const char *,
73 int, int, const char *, int);
74 int get_str(char **, size_t *, char **, const char *, int,
76 int get_tcpopts(const char *, int, const char *,
77 pf_tcpopts_t
*, int *, int *, int *, int *, int *,
79 void import_fingerprint(struct pf_osfp_ioctl
*);
80 const char *print_ioctl(struct pf_osfp_ioctl
*);
81 void print_name_list(int, struct name_list
*, const char *);
82 void sort_name_list(int, struct name_list
*);
83 struct name_entry
*lookup_name_list(struct name_list
*, const char *);
85 /* Load fingerprints from a file */
87 pfctl_file_fingerprints(int dev
, int opts
, const char *fp_filename
)
93 int window
, w_mod
, ttl
, df
, psize
, p_mod
, mss
, mss_mod
, wscale
,
94 wscale_mod
, optcnt
, ts0
;
95 pf_tcpopts_t packed_tcpopts
;
96 char *class, *version
, *subtype
, *desc
, *tcpopts
;
97 struct pf_osfp_ioctl fp
;
99 pfctl_flush_my_fingerprints(&classes
);
101 if ((in
= fopen(fp_filename
, "r")) == NULL
) {
102 warn("fopen(%s)", fp_filename
);
105 class = version
= subtype
= desc
= tcpopts
= NULL
;
107 if ((opts
& PF_OPT_NOACTION
) == 0)
108 pfctl_clear_fingerprints(dev
, opts
);
110 while ((line
= fgetln(in
, &len
)) != NULL
) {
122 class = version
= subtype
= desc
= tcpopts
= NULL
;
123 memset(&fp
, 0, sizeof(fp
));
125 /* Chop off comment */
126 for (i
= 0; i
< len
; i
++)
127 if (line
[i
] == '#') {
131 /* Chop off whitespace */
132 while (len
> 0 && isspace(line
[len
- 1]))
134 while (len
> 0 && isspace(line
[0])) {
141 #define T_DC 0x01 /* Allow don't care */
142 #define T_MSS 0x02 /* Allow MSS multiple */
143 #define T_MTU 0x04 /* Allow MTU multiple */
144 #define T_MOD 0x08 /* Allow modulus */
146 #define GET_INT(v, mod, n, ty, mx) \
147 get_int(&line, &len, &v, mod, n, ty, mx, fp_filename, lineno)
148 #define GET_STR(v, n, mn) \
149 get_str(&line, &len, &v, n, mn, fp_filename, lineno)
151 if (GET_INT(window
, &w_mod
, "window size", T_DC
|T_MSS
|T_MTU
|
153 GET_INT(ttl
, NULL
, "ttl", 0, 0xff) ||
154 GET_INT(df
, NULL
, "don't fragment frag", 0, 1) ||
155 GET_INT(psize
, &p_mod
, "overall packet size", T_MOD
|T_DC
,
157 GET_STR(tcpopts
, "TCP Options", 1) ||
158 GET_STR(class, "OS class", 1) ||
159 GET_STR(version
, "OS version", 0) ||
160 GET_STR(subtype
, "OS subtype", 0) ||
161 GET_STR(desc
, "OS description", 2))
163 if (get_tcpopts(fp_filename
, lineno
, tcpopts
, &packed_tcpopts
,
164 &optcnt
, &mss
, &mss_mod
, &wscale
, &wscale_mod
, &ts0
))
167 fprintf(stderr
, "%s:%d excess field\n", fp_filename
,
174 fp
.fp_flags
|= PF_OSFP_DF
;
179 fp
.fp_flags
|= PF_OSFP_WSIZE_DC
;
182 fp
.fp_flags
|= PF_OSFP_WSIZE_MSS
;
185 fp
.fp_flags
|= PF_OSFP_WSIZE_MTU
;
188 fp
.fp_flags
|= PF_OSFP_WSIZE_MOD
;
191 fp
.fp_wsize
= window
;
195 fp
.fp_flags
|= PF_OSFP_PSIZE_DC
;
198 fp
.fp_flags
|= PF_OSFP_PSIZE_MOD
;
203 switch (wscale_mod
) {
205 fp
.fp_flags
|= PF_OSFP_WSCALE_DC
;
208 fp
.fp_flags
|= PF_OSFP_WSCALE_MOD
;
210 fp
.fp_wscale
= wscale
;
214 fp
.fp_flags
|= PF_OSFP_MSS_DC
;
217 fp
.fp_flags
|= PF_OSFP_MSS_MOD
;
222 fp
.fp_tcpopts
= packed_tcpopts
;
223 fp
.fp_optcnt
= optcnt
;
225 fp
.fp_flags
|= PF_OSFP_TS0
;
228 fp
.fp_os
.fp_enflags
|= PF_OSFP_GENERIC
;
230 fp
.fp_os
.fp_enflags
|= PF_OSFP_NODETAIL
;
232 if (class[0] == '@' || class[0] == '*')
233 strlcpy(fp
.fp_os
.fp_class_nm
, class + 1,
234 sizeof(fp
.fp_os
.fp_class_nm
));
236 strlcpy(fp
.fp_os
.fp_class_nm
, class,
237 sizeof(fp
.fp_os
.fp_class_nm
));
238 strlcpy(fp
.fp_os
.fp_version_nm
, version
,
239 sizeof(fp
.fp_os
.fp_version_nm
));
240 strlcpy(fp
.fp_os
.fp_subtype_nm
, subtype
,
241 sizeof(fp
.fp_os
.fp_subtype_nm
));
243 add_fingerprint(dev
, opts
, &fp
);
257 if (opts
& PF_OPT_VERBOSE2
)
258 printf("Loaded %d passive OS fingerprints\n",
263 /* flush the kernel's fingerprints */
265 pfctl_clear_fingerprints(int dev
, int opts __unused
)
267 if (ioctl(dev
, DIOCOSFPFLUSH
))
268 err(1, "DIOCOSFPFLUSH");
271 /* flush pfctl's view of the fingerprints */
273 pfctl_flush_my_fingerprints(struct name_list
*list
)
275 struct name_entry
*nm
;
277 while ((nm
= LIST_FIRST(list
)) != NULL
) {
278 LIST_REMOVE(nm
, nm_entry
);
279 pfctl_flush_my_fingerprints(&nm
->nm_sublist
);
286 /* Fetch the active fingerprints from the kernel */
288 pfctl_load_fingerprints(int dev
, int opts __unused
)
290 struct pf_osfp_ioctl io
;
293 pfctl_flush_my_fingerprints(&classes
);
295 for (i
= 0; i
>= 0; i
++) {
296 memset(&io
, 0, sizeof(io
));
298 if (ioctl(dev
, DIOCOSFPGET
, &io
)) {
304 import_fingerprint(&io
);
309 /* List the fingerprints */
311 pfctl_show_fingerprints(int opts
)
313 if (LIST_FIRST(&classes
) != NULL
) {
314 if (opts
& PF_OPT_SHOWALL
) {
315 pfctl_print_title("OS FINGERPRINTS:");
316 printf("%u fingerprints loaded\n", fingerprint_count
);
318 printf("Class\tVersion\tSubtype(subversion)\n");
319 printf("-----\t-------\t-------------------\n");
320 sort_name_list(opts
, &classes
);
321 print_name_list(opts
, &classes
, "");
326 /* Lookup a fingerprint */
328 pfctl_get_fingerprint(const char *name
)
330 struct name_entry
*nm
, *class_nm
, *version_nm
, *subtype_nm
;
331 pf_osfp_t ret
= PF_OSFP_NOMATCH
;
332 int class, version
, subtype
;
333 int unp_class
, unp_version
, unp_subtype
;
334 int wr_len
, version_len
, subtype_len
;
337 if (strcasecmp(name
, "unknown") == 0)
338 return (PF_OSFP_UNKNOWN
);
340 /* Try most likely no version and no subtype */
341 if ((nm
= lookup_name_list(&classes
, name
))) {
343 version
= PF_OSFP_ANY
;
344 subtype
= PF_OSFP_ANY
;
348 /* Chop it up into class/version/subtype */
350 if ((wr_name
= strdup(name
)) == NULL
)
352 if ((ptr
= index(wr_name
, ' ')) == NULL
) {
354 return (PF_OSFP_NOMATCH
);
358 /* The class is easy to find since it is delimited by a space */
359 if ((class_nm
= lookup_name_list(&classes
, wr_name
)) == NULL
) {
361 return (PF_OSFP_NOMATCH
);
363 class = class_nm
->nm_num
;
366 if ((version_nm
= lookup_name_list(&class_nm
->nm_sublist
, ptr
)))
368 version
= version_nm
->nm_num
;
369 subtype
= PF_OSFP_ANY
;
376 * There must be a version and a subtype.
377 * We'll do some fuzzy matching to pick up things like:
378 * Linux 2.2.14 (version=2.2 subtype=14)
379 * FreeBSD 4.0-STABLE (version=4.0 subtype=STABLE)
380 * Windows 2000 SP2 (version=2000 subtype=SP2)
382 #define CONNECTOR(x) ((x) == '.' || (x) == ' ' || (x) == '\t' || (x) == '-')
383 wr_len
= strlen(ptr
);
384 LIST_FOREACH(version_nm
, &class_nm
->nm_sublist
, nm_entry
) {
385 version_len
= strlen(version_nm
->nm_name
);
386 if (wr_len
< version_len
+ 2 ||
387 !CONNECTOR(ptr
[version_len
]))
389 /* first part of the string must be version */
390 if (strncasecmp(ptr
, version_nm
->nm_name
,
394 LIST_FOREACH(subtype_nm
, &version_nm
->nm_sublist
,
396 subtype_len
= strlen(subtype_nm
->nm_name
);
397 if (wr_len
!= version_len
+ subtype_len
+ 1)
400 /* last part of the string must be subtype */
401 if (strcasecmp(&ptr
[version_len
+1],
402 subtype_nm
->nm_name
) != 0)
406 version
= version_nm
->nm_num
;
407 subtype
= subtype_nm
->nm_num
;
414 return (PF_OSFP_NOMATCH
);
418 PF_OSFP_PACK(ret
, class, version
, subtype
);
419 if (ret
!= PF_OSFP_NOMATCH
) {
420 PF_OSFP_UNPACK(ret
, unp_class
, unp_version
, unp_subtype
);
421 if (class != unp_class
) {
422 fprintf(stderr
, "warning: fingerprint table overflowed "
424 return (PF_OSFP_NOMATCH
);
426 if (version
!= unp_version
) {
427 fprintf(stderr
, "warning: fingerprint table overflowed "
429 return (PF_OSFP_NOMATCH
);
431 if (subtype
!= unp_subtype
) {
432 fprintf(stderr
, "warning: fingerprint table overflowed "
434 return (PF_OSFP_NOMATCH
);
437 if (ret
== PF_OSFP_ANY
) {
438 /* should never happen */
439 fprintf(stderr
, "warning: fingerprint packed to 'any'\n");
440 return (PF_OSFP_NOMATCH
);
446 /* Lookup a fingerprint name by ID */
448 pfctl_lookup_fingerprint(pf_osfp_t fp
, char *buf
, size_t len
)
450 int class, version
, subtype
;
451 struct name_list
*list
;
452 struct name_entry
*nm
;
454 char *class_name
, *version_name
, *subtype_name
;
455 class_name
= version_name
= subtype_name
= NULL
;
457 if (fp
== PF_OSFP_UNKNOWN
) {
458 strlcpy(buf
, "unknown", len
);
461 if (fp
== PF_OSFP_ANY
) {
462 strlcpy(buf
, "any", len
);
466 PF_OSFP_UNPACK(fp
, class, version
, subtype
);
467 if (class >= (1 << _FP_CLASS_BITS
) ||
468 version
>= (1 << _FP_VERSION_BITS
) ||
469 subtype
>= (1 << _FP_SUBTYPE_BITS
)) {
470 warnx("PF_OSFP_UNPACK(0x%x) failed!!", fp
);
471 strlcpy(buf
, "nomatch", len
);
475 LIST_FOREACH(nm
, &classes
, nm_entry
) {
476 if (nm
->nm_num
== class) {
477 class_name
= nm
->nm_name
;
478 if (version
== PF_OSFP_ANY
)
480 list
= &nm
->nm_sublist
;
481 LIST_FOREACH(nm
, list
, nm_entry
) {
482 if (nm
->nm_num
== version
) {
483 version_name
= nm
->nm_name
;
484 if (subtype
== PF_OSFP_ANY
)
486 list
= &nm
->nm_sublist
;
487 LIST_FOREACH(nm
, list
, nm_entry
) {
488 if (nm
->nm_num
== subtype
) {
493 } /* foreach subtype */
494 strlcpy(buf
, "nomatch", len
);
497 } /* foreach version */
498 strlcpy(buf
, "nomatch", len
);
501 } /* foreach class */
503 strlcpy(buf
, "nomatch", len
);
507 snprintf(buf
, len
, "%s", class_name
);
509 strlcat(buf
, " ", len
);
510 strlcat(buf
, version_name
, len
);
512 if (index(version_name
, ' '))
513 strlcat(buf
, " ", len
);
514 else if (index(version_name
, '.') &&
515 isdigit(*subtype_name
))
516 strlcat(buf
, ".", len
);
518 strlcat(buf
, " ", len
);
519 strlcat(buf
, subtype_name
, len
);
525 /* lookup a name in a list */
527 lookup_name_list(struct name_list
*list
, const char *name
)
529 struct name_entry
*nm
;
530 LIST_FOREACH(nm
, list
, nm_entry
)
531 if (strcasecmp(name
, nm
->nm_name
) == 0)
539 add_fingerprint(int dev
, int opts
, struct pf_osfp_ioctl
*fp
)
541 struct pf_osfp_ioctl fptmp
;
542 struct name_entry
*nm_class
, *nm_version
, *nm_subtype
;
543 int class, version
, subtype
;
545 /* We expand #-# or #.#-#.# version/subtypes into multiple fingerprints */
546 #define EXPAND(field) do { \
547 int _dot = -1, _start = -1, _end = -1, _i = 0; \
548 /* pick major version out of #.# */ \
549 if (isdigit(fp->field[_i]) && fp->field[_i+1] == '.') { \
550 _dot = fp->field[_i] - '0'; \
553 if (isdigit(fp->field[_i])) \
554 _start = fp->field[_i++] - '0'; \
557 if (isdigit(fp->field[_i])) \
558 _start = (_start * 10) + fp->field[_i++] - '0'; \
559 if (fp->field[_i++] != '-') \
561 if (isdigit(fp->field[_i]) && fp->field[_i+1] == '.' && \
562 fp->field[_i] - '0' == _dot) \
564 else if (_dot != -1) \
566 if (isdigit(fp->field[_i])) \
567 _end = fp->field[_i++] - '0'; \
570 if (isdigit(fp->field[_i])) \
571 _end = (_end * 10) + fp->field[_i++] - '0'; \
572 if (isdigit(fp->field[_i])) \
573 _end = (_end * 10) + fp->field[_i++] - '0'; \
574 if (fp->field[_i] != '\0') \
576 memcpy(&fptmp, fp, sizeof(fptmp)); \
577 for (;_start <= _end; _start++) { \
578 memset(fptmp.field, 0, sizeof(fptmp.field)); \
579 fptmp.fp_os.fp_enflags |= PF_OSFP_EXPANDED; \
581 snprintf(fptmp.field, sizeof(fptmp.field), \
584 snprintf(fptmp.field, sizeof(fptmp.field), \
585 "%d.%d", _dot, _start); \
586 add_fingerprint(dev, opts, &fptmp); \
590 /* We allow "#-#" as a version or subtype and we'll expand it */
591 EXPAND(fp_os
.fp_version_nm
);
592 EXPAND(fp_os
.fp_subtype_nm
);
594 if (strcasecmp(fp
->fp_os
.fp_class_nm
, "nomatch") == 0)
595 errx(1, "fingerprint class \"nomatch\" is reserved");
597 version
= PF_OSFP_ANY
;
598 subtype
= PF_OSFP_ANY
;
600 nm_class
= fingerprint_name_entry(&classes
, fp
->fp_os
.fp_class_nm
);
601 if (nm_class
->nm_num
== 0)
602 nm_class
->nm_num
= ++class_count
;
603 class = nm_class
->nm_num
;
605 nm_version
= fingerprint_name_entry(&nm_class
->nm_sublist
,
606 fp
->fp_os
.fp_version_nm
);
608 if (nm_version
->nm_num
== 0)
609 nm_version
->nm_num
= ++nm_class
->nm_sublist_num
;
610 version
= nm_version
->nm_num
;
611 nm_subtype
= fingerprint_name_entry(&nm_version
->nm_sublist
,
612 fp
->fp_os
.fp_subtype_nm
);
614 if (nm_subtype
->nm_num
== 0)
616 ++nm_version
->nm_sublist_num
;
617 subtype
= nm_subtype
->nm_num
;
622 DEBUG(fp
, "\tsignature %d:%d:%d %s", class, version
, subtype
,
625 PF_OSFP_PACK(fp
->fp_os
.fp_os
, class, version
, subtype
);
628 #ifdef FAKE_PF_KERNEL
629 /* Linked to the sys/net/pf/pf_osfp.c. Call pf_osfp_add() */
630 if ((errno
= pf_osfp_add(fp
)))
632 if ((opts
& PF_OPT_NOACTION
) == 0 && ioctl(dev
, DIOCOSFPADD
, fp
))
633 #endif /* FAKE_PF_KERNEL */
635 if (errno
== EEXIST
) {
636 warn("Duplicate signature for %s %s %s",
637 fp
->fp_os
.fp_class_nm
,
638 fp
->fp_os
.fp_version_nm
,
639 fp
->fp_os
.fp_subtype_nm
);
642 err(1, "DIOCOSFPADD");
647 /* import a fingerprint from the kernel */
649 import_fingerprint(struct pf_osfp_ioctl
*fp
)
651 struct name_entry
*nm_class
, *nm_version
, *nm_subtype
;
652 int class, version
, subtype
;
654 PF_OSFP_UNPACK(fp
->fp_os
.fp_os
, class, version
, subtype
);
656 nm_class
= fingerprint_name_entry(&classes
, fp
->fp_os
.fp_class_nm
);
657 if (nm_class
->nm_num
== 0) {
658 nm_class
->nm_num
= class;
659 class_count
= MAX(class_count
, class);
662 nm_version
= fingerprint_name_entry(&nm_class
->nm_sublist
,
663 fp
->fp_os
.fp_version_nm
);
665 if (nm_version
->nm_num
== 0) {
666 nm_version
->nm_num
= version
;
667 nm_class
->nm_sublist_num
= MAX(nm_class
->nm_sublist_num
,
670 nm_subtype
= fingerprint_name_entry(&nm_version
->nm_sublist
,
671 fp
->fp_os
.fp_subtype_nm
);
673 if (nm_subtype
->nm_num
== 0) {
674 nm_subtype
->nm_num
= subtype
;
675 nm_version
->nm_sublist_num
=
676 MAX(nm_version
->nm_sublist_num
, subtype
);
683 DEBUG(fp
, "import signature %d:%d:%d", class, version
, subtype
);
686 /* Find an entry for a fingerprints class/version/subtype */
688 fingerprint_name_entry(struct name_list
*list
, char *name
)
690 struct name_entry
*nm_entry
;
692 if (name
== NULL
|| strlen(name
) == 0)
695 LIST_FOREACH(nm_entry
, list
, nm_entry
) {
696 if (strcasecmp(nm_entry
->nm_name
, name
) == 0) {
697 /* We'll move this to the front of the list later */
698 LIST_REMOVE(nm_entry
, nm_entry
);
702 if (nm_entry
== NULL
) {
703 nm_entry
= calloc(1, sizeof(*nm_entry
));
704 if (nm_entry
== NULL
)
706 LIST_INIT(&nm_entry
->nm_sublist
);
707 strlcpy(nm_entry
->nm_name
, name
,
708 sizeof(nm_entry
->nm_name
));
710 LIST_INSERT_HEAD(list
, nm_entry
, nm_entry
);
716 print_name_list(int opts
, struct name_list
*nml
, const char *prefix
)
719 struct name_entry
*nm
;
721 LIST_FOREACH(nm
, nml
, nm_entry
) {
722 snprintf(newprefix
, sizeof(newprefix
), "%s%s\t", prefix
,
724 printf("%s\n", newprefix
);
725 print_name_list(opts
, &nm
->nm_sublist
, newprefix
);
730 sort_name_list(int opts
, struct name_list
*nml
)
732 struct name_list
new;
733 struct name_entry
*nm
, *nmsearch
, *nmlast
;
735 /* yes yes, it's a very slow sort. so sue me */
739 while ((nm
= LIST_FIRST(nml
)) != NULL
) {
740 LIST_REMOVE(nm
, nm_entry
);
742 LIST_FOREACH(nmsearch
, &new, nm_entry
) {
743 if (strcasecmp(nmsearch
->nm_name
, nm
->nm_name
) > 0) {
744 LIST_INSERT_BEFORE(nmsearch
, nm
, nm_entry
);
749 if (nmsearch
== NULL
) {
751 LIST_INSERT_AFTER(nmlast
, nm
, nm_entry
);
753 LIST_INSERT_HEAD(&new, nm
, nm_entry
);
756 sort_name_list(opts
, &nm
->nm_sublist
);
759 while ((nm
= LIST_FIRST(&new)) != NULL
) {
760 LIST_REMOVE(nm
, nm_entry
);
762 LIST_INSERT_HEAD(nml
, nm
, nm_entry
);
764 LIST_INSERT_AFTER(nmlast
, nm
, nm_entry
);
770 /* parse the next integer in a formatted config file line */
772 get_int(char **line
, size_t *len
, int *var
, int *mod
,
773 const char *name
, int flags
, int max
, const char *filename
, int lineno
)
783 field
= get_field(line
, len
, &fieldlen
);
787 fprintf(stderr
, "%s:%d empty %s\n", filename
, lineno
, name
);
792 if ((*field
== '%' || *field
== 'S' || *field
== 'T' || *field
== '*')
796 if (mod
&& (flags
& T_MSS
))
802 if (mod
&& (flags
& T_MTU
))
809 fprintf(stderr
, "%s:%d long '%c' %s\n",
810 filename
, lineno
, *field
, name
);
813 if (mod
&& (flags
& T_DC
)) {
818 if (mod
&& (flags
& T_MOD
))
821 fprintf(stderr
, "%s:%d modulus %s must have a "
822 "value\n", filename
, lineno
, name
);
827 if (mod
== NULL
|| *mod
== 0) {
828 fprintf(stderr
, "%s:%d does not allow %c' %s\n",
829 filename
, lineno
, *field
, name
);
835 for (; i
< fieldlen
; i
++) {
836 if (field
[i
] < '0' || field
[i
] > '9') {
837 fprintf(stderr
, "%s:%d non-digit character in %s\n",
838 filename
, lineno
, name
);
841 val
= val
* 10 + field
[i
] - '0';
843 fprintf(stderr
, "%s:%d %s overflowed\n", filename
,
850 fprintf(stderr
, "%s:%d %s value %ld > %d\n", filename
, lineno
,
859 /* parse the next string in a formatted config file line */
861 get_str(char **line
, size_t *len
, char **v
, const char *name
, int minlen
,
862 const char *filename
, int lineno
)
867 ptr
= get_field(line
, len
, &fieldlen
);
870 if (fieldlen
< minlen
) {
871 fprintf(stderr
, "%s:%d too short %s\n", filename
, lineno
, name
);
874 if ((*v
= malloc(fieldlen
+ 1)) == NULL
) {
878 memcpy(*v
, ptr
, fieldlen
);
879 (*v
)[fieldlen
] = '\0';
884 /* Parse out the TCP opts */
886 get_tcpopts(const char *filename
, int lineno
, const char *tcpopts
,
887 pf_tcpopts_t
*packed
, int *optcnt
, int *mss
, int *mss_mod
, int *wscale
,
888 int *wscale_mod
, int *ts0
)
899 if (strcmp(tcpopts
, ".") == 0)
902 for (i
= 0; tcpopts
[i
] && (size_t)*optcnt
< PF_OSFP_MAX_OPTS
;) {
903 switch ((opt
= toupper(tcpopts
[i
++]))) {
904 case 'N': /* FALLTHROUGH */
906 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
907 (opt
== 'N' ? PF_OSFP_TCPOPT_NOP
:
908 PF_OSFP_TCPOPT_SACK
);
910 case 'W': /* FALLTHROUGH */
912 int *this_mod
, *this;
916 this_mod
= wscale_mod
;
924 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
925 (opt
== 'W' ? PF_OSFP_TCPOPT_WSCALE
:
927 if (tcpopts
[i
] == '*' && (tcpopts
[i
+ 1] == '\0' ||
928 tcpopts
[i
+ 1] == ',')) {
934 if (tcpopts
[i
] == '%') {
939 if (!isdigit(tcpopts
[i
])) {
940 fprintf(stderr
, "%s:%d unknown "
941 "character '%c' in %c TCP opt\n",
942 filename
, lineno
, tcpopts
[i
], opt
);
945 *this = (*this * 10) + tcpopts
[i
++] - '0';
946 } while(tcpopts
[i
] != ',' && tcpopts
[i
] != '\0');
950 if (tcpopts
[i
] == '0') {
954 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
959 if (tcpopts
[i
] == '\0')
961 if (tcpopts
[i
] != ',') {
962 fprintf(stderr
, "%s:%d unknown option to %c TCP opt\n",
963 filename
, lineno
, opt
);
972 /* rip the next field ouf of a formatted config file line */
974 get_field(char **line
, size_t *len
, int *fieldlen
)
976 char *ret
, *ptr
= *line
;
980 while (plen
&& isspace(*ptr
)) {
987 for (; plen
> 0 && *ptr
!= ':'; plen
--, ptr
++)
995 while (*fieldlen
&& isspace(ret
[*fieldlen
- 1]))
1002 print_ioctl(struct pf_osfp_ioctl
*fp
)
1004 static char buf
[1024];
1009 if (fp
->fp_flags
& PF_OSFP_WSIZE_DC
)
1010 strlcat(buf
, "*", sizeof(buf
));
1011 else if (fp
->fp_flags
& PF_OSFP_WSIZE_MSS
)
1012 strlcat(buf
, "S", sizeof(buf
));
1013 else if (fp
->fp_flags
& PF_OSFP_WSIZE_MTU
)
1014 strlcat(buf
, "T", sizeof(buf
));
1016 if (fp
->fp_flags
& PF_OSFP_WSIZE_MOD
)
1017 strlcat(buf
, "%", sizeof(buf
));
1018 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_wsize
);
1019 strlcat(buf
, tmp
, sizeof(buf
));
1021 strlcat(buf
, ":", sizeof(buf
));
1023 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_ttl
);
1024 strlcat(buf
, tmp
, sizeof(buf
));
1025 strlcat(buf
, ":", sizeof(buf
));
1027 if (fp
->fp_flags
& PF_OSFP_DF
)
1028 strlcat(buf
, "1", sizeof(buf
));
1030 strlcat(buf
, "0", sizeof(buf
));
1031 strlcat(buf
, ":", sizeof(buf
));
1033 if (fp
->fp_flags
& PF_OSFP_PSIZE_DC
)
1034 strlcat(buf
, "*", sizeof(buf
));
1036 if (fp
->fp_flags
& PF_OSFP_PSIZE_MOD
)
1037 strlcat(buf
, "%", sizeof(buf
));
1038 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_psize
);
1039 strlcat(buf
, tmp
, sizeof(buf
));
1041 strlcat(buf
, ":", sizeof(buf
));
1043 if (fp
->fp_optcnt
== 0)
1044 strlcat(buf
, ".", sizeof(buf
));
1045 for (i
= fp
->fp_optcnt
- 1; i
>= 0; i
--) {
1046 opt
= fp
->fp_tcpopts
>> (i
* PF_OSFP_TCPOPT_BITS
);
1047 opt
&= (1 << PF_OSFP_TCPOPT_BITS
) - 1;
1049 case PF_OSFP_TCPOPT_NOP
:
1050 strlcat(buf
, "N", sizeof(buf
));
1052 case PF_OSFP_TCPOPT_SACK
:
1053 strlcat(buf
, "S", sizeof(buf
));
1055 case PF_OSFP_TCPOPT_TS
:
1056 strlcat(buf
, "T", sizeof(buf
));
1057 if (fp
->fp_flags
& PF_OSFP_TS0
)
1058 strlcat(buf
, "0", sizeof(buf
));
1060 case PF_OSFP_TCPOPT_MSS
:
1061 strlcat(buf
, "M", sizeof(buf
));
1062 if (fp
->fp_flags
& PF_OSFP_MSS_DC
)
1063 strlcat(buf
, "*", sizeof(buf
));
1065 if (fp
->fp_flags
& PF_OSFP_MSS_MOD
)
1066 strlcat(buf
, "%", sizeof(buf
));
1067 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_mss
);
1068 strlcat(buf
, tmp
, sizeof(buf
));
1071 case PF_OSFP_TCPOPT_WSCALE
:
1072 strlcat(buf
, "W", sizeof(buf
));
1073 if (fp
->fp_flags
& PF_OSFP_WSCALE_DC
)
1074 strlcat(buf
, "*", sizeof(buf
));
1076 if (fp
->fp_flags
& PF_OSFP_WSCALE_MOD
)
1077 strlcat(buf
, "%", sizeof(buf
));
1078 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_wscale
);
1079 strlcat(buf
, tmp
, sizeof(buf
));
1085 strlcat(buf
, ",", sizeof(buf
));
1087 strlcat(buf
, ":", sizeof(buf
));
1089 strlcat(buf
, fp
->fp_os
.fp_class_nm
, sizeof(buf
));
1090 strlcat(buf
, ":", sizeof(buf
));
1091 strlcat(buf
, fp
->fp_os
.fp_version_nm
, sizeof(buf
));
1092 strlcat(buf
, ":", sizeof(buf
));
1093 strlcat(buf
, fp
->fp_os
.fp_subtype_nm
, sizeof(buf
));
1094 strlcat(buf
, ":", sizeof(buf
));
1096 snprintf(tmp
, sizeof(tmp
), "TcpOpts %d 0x%llx", fp
->fp_optcnt
,
1097 (long long int)fp
->fp_tcpopts
);
1098 strlcat(buf
, tmp
, sizeof(buf
));