2 /* $OpenBSD: pfctl_osfp.c,v 1.15 2006/12/13 05:10:15 itojun 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/pfvar.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <netinet/ip6.h>
38 #include "pfctl_parser.h"
42 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
45 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
50 # define DEBUG(fp, str, v...) \
51 fprintf(stderr, "%s:%s:%s " str "\n", (fp)->fp_os.fp_class_nm, \
52 (fp)->fp_os.fp_version_nm, (fp)->fp_os.fp_subtype_nm , ## v);
54 # define DEBUG(fp, str, v...) ((void)0)
59 LIST_HEAD(name_list
, name_entry
);
61 LIST_ENTRY(name_entry
) nm_entry
;
63 char nm_name
[PF_OSFP_LEN
];
65 struct name_list nm_sublist
;
68 struct name_list classes
= LIST_HEAD_INITIALIZER(&classes
);
70 int fingerprint_count
;
72 void add_fingerprint(int, int, struct pf_osfp_ioctl
*);
73 struct name_entry
*fingerprint_name_entry(struct name_list
*, char *);
74 void pfctl_flush_my_fingerprints(struct name_list
*);
75 char *get_field(char **, size_t *, int *);
76 int get_int(char **, size_t *, int *, int *, const char *,
77 int, int, const char *, int);
78 int get_str(char **, size_t *, char **, const char *, int,
80 int get_tcpopts(const char *, int, const char *,
81 pf_tcpopts_t
*, int *, int *, int *, int *, int *,
83 void import_fingerprint(struct pf_osfp_ioctl
*);
84 const char *print_ioctl(struct pf_osfp_ioctl
*);
85 void print_name_list(int, struct name_list
*, const char *);
86 void sort_name_list(int, struct name_list
*);
87 struct name_entry
*lookup_name_list(struct name_list
*, const char *);
89 /* Load fingerprints from a file */
91 pfctl_file_fingerprints(int dev
, int opts
, const char *fp_filename
)
97 int window
, w_mod
, ttl
, df
, psize
, p_mod
, mss
, mss_mod
, wscale
,
98 wscale_mod
, optcnt
, ts0
;
99 pf_tcpopts_t packed_tcpopts
;
100 char *class, *version
, *subtype
, *desc
, *tcpopts
;
101 struct pf_osfp_ioctl fp
;
103 pfctl_flush_my_fingerprints(&classes
);
105 if ((in
= pfctl_fopen(fp_filename
, "r")) == NULL
) {
106 warn("%s", fp_filename
);
109 class = version
= subtype
= desc
= tcpopts
= NULL
;
111 if ((opts
& PF_OPT_NOACTION
) == 0)
112 pfctl_clear_fingerprints(dev
, opts
);
114 while ((line
= fgetln(in
, &len
)) != NULL
) {
126 class = version
= subtype
= desc
= tcpopts
= NULL
;
127 memset(&fp
, 0, sizeof(fp
));
129 /* Chop off comment */
130 for (i
= 0; i
< len
; i
++)
131 if (line
[i
] == '#') {
135 /* Chop off whitespace */
136 while (len
> 0 && isspace((unsigned char)line
[len
- 1]))
138 while (len
> 0 && isspace((unsigned char)line
[0])) {
145 #define T_DC 0x01 /* Allow don't care */
146 #define T_MSS 0x02 /* Allow MSS multiple */
147 #define T_MTU 0x04 /* Allow MTU multiple */
148 #define T_MOD 0x08 /* Allow modulus */
150 #define GET_INT(v, mod, n, ty, mx) \
151 get_int(&line, &len, &v, mod, n, ty, mx, fp_filename, lineno)
152 #define GET_STR(v, n, mn) \
153 get_str(&line, &len, &v, n, mn, fp_filename, lineno)
155 if (GET_INT(window
, &w_mod
, "window size", T_DC
|T_MSS
|T_MTU
|
157 GET_INT(ttl
, NULL
, "ttl", 0, 0xff) ||
158 GET_INT(df
, NULL
, "don't fragment frag", 0, 1) ||
159 GET_INT(psize
, &p_mod
, "overall packet size", T_MOD
|T_DC
,
161 GET_STR(tcpopts
, "TCP Options", 1) ||
162 GET_STR(class, "OS class", 1) ||
163 GET_STR(version
, "OS version", 0) ||
164 GET_STR(subtype
, "OS subtype", 0) ||
165 GET_STR(desc
, "OS description", 2))
167 if (get_tcpopts(fp_filename
, lineno
, tcpopts
, &packed_tcpopts
,
168 &optcnt
, &mss
, &mss_mod
, &wscale
, &wscale_mod
, &ts0
))
171 fprintf(stderr
, "%s:%d excess field\n", fp_filename
,
178 fp
.fp_flags
|= PF_OSFP_DF
;
183 fp
.fp_flags
|= PF_OSFP_WSIZE_DC
;
186 fp
.fp_flags
|= PF_OSFP_WSIZE_MSS
;
189 fp
.fp_flags
|= PF_OSFP_WSIZE_MTU
;
192 fp
.fp_flags
|= PF_OSFP_WSIZE_MOD
;
195 fp
.fp_wsize
= window
;
199 fp
.fp_flags
|= PF_OSFP_PSIZE_DC
;
202 fp
.fp_flags
|= PF_OSFP_PSIZE_MOD
;
207 switch (wscale_mod
) {
209 fp
.fp_flags
|= PF_OSFP_WSCALE_DC
;
212 fp
.fp_flags
|= PF_OSFP_WSCALE_MOD
;
214 fp
.fp_wscale
= wscale
;
218 fp
.fp_flags
|= PF_OSFP_MSS_DC
;
221 fp
.fp_flags
|= PF_OSFP_MSS_MOD
;
226 fp
.fp_tcpopts
= packed_tcpopts
;
227 fp
.fp_optcnt
= optcnt
;
229 fp
.fp_flags
|= PF_OSFP_TS0
;
232 fp
.fp_os
.fp_enflags
|= PF_OSFP_GENERIC
;
234 fp
.fp_os
.fp_enflags
|= PF_OSFP_NODETAIL
;
236 if (class[0] == '@' || class[0] == '*')
237 strlcpy(fp
.fp_os
.fp_class_nm
, class + 1,
238 sizeof(fp
.fp_os
.fp_class_nm
));
240 strlcpy(fp
.fp_os
.fp_class_nm
, class,
241 sizeof(fp
.fp_os
.fp_class_nm
));
242 strlcpy(fp
.fp_os
.fp_version_nm
, version
,
243 sizeof(fp
.fp_os
.fp_version_nm
));
244 strlcpy(fp
.fp_os
.fp_subtype_nm
, subtype
,
245 sizeof(fp
.fp_os
.fp_subtype_nm
));
247 add_fingerprint(dev
, opts
, &fp
);
249 fp
.fp_flags
|= (PF_OSFP_DF
| PF_OSFP_INET6
);
250 fp
.fp_psize
+= sizeof(struct ip6_hdr
) - sizeof(struct ip
);
251 add_fingerprint(dev
, opts
, &fp
);
267 if (opts
& PF_OPT_VERBOSE2
)
268 printf("Loaded %d passive OS fingerprints\n",
273 /* flush the kernel's fingerprints */
275 pfctl_clear_fingerprints(int dev
, int opts
)
277 if (ioctl(dev
, DIOCOSFPFLUSH
))
278 err(1, "DIOCOSFPFLUSH");
281 /* flush pfctl's view of the fingerprints */
283 pfctl_flush_my_fingerprints(struct name_list
*list
)
285 struct name_entry
*nm
;
287 while ((nm
= LIST_FIRST(list
)) != NULL
) {
288 LIST_REMOVE(nm
, nm_entry
);
289 pfctl_flush_my_fingerprints(&nm
->nm_sublist
);
292 fingerprint_count
= 0;
296 /* Fetch the active fingerprints from the kernel */
298 pfctl_load_fingerprints(int dev
, int opts
)
300 struct pf_osfp_ioctl io
;
303 pfctl_flush_my_fingerprints(&classes
);
305 for (i
= 0; i
>= 0; i
++) {
306 memset(&io
, 0, sizeof(io
));
308 if (ioctl(dev
, DIOCOSFPGET
, &io
)) {
314 import_fingerprint(&io
);
319 /* List the fingerprints */
321 pfctl_show_fingerprints(int opts
)
323 if (LIST_FIRST(&classes
) != NULL
) {
324 if (opts
& PF_OPT_SHOWALL
) {
325 pfctl_print_title("OS FINGERPRINTS:");
326 printf("%u fingerprints loaded\n", fingerprint_count
);
328 printf("Class\tVersion\tSubtype(subversion)\n");
329 printf("-----\t-------\t-------------------\n");
330 sort_name_list(opts
, &classes
);
331 print_name_list(opts
, &classes
, "");
336 /* Lookup a fingerprint */
338 pfctl_get_fingerprint(const char *name
)
340 struct name_entry
*nm
, *class_nm
, *version_nm
, *subtype_nm
;
341 pf_osfp_t ret
= PF_OSFP_NOMATCH
;
342 int class, version
, subtype
;
343 int unp_class
, unp_version
, unp_subtype
;
344 int wr_len
, version_len
, subtype_len
;
347 if (strcasecmp(name
, "unknown") == 0)
348 return (PF_OSFP_UNKNOWN
);
350 /* Try most likely no version and no subtype */
351 if ((nm
= lookup_name_list(&classes
, name
))) {
353 version
= PF_OSFP_ANY
;
354 subtype
= PF_OSFP_ANY
;
358 /* Chop it up into class/version/subtype */
360 if ((wr_name
= strdup(name
)) == NULL
)
362 if ((ptr
= strchr(wr_name
, ' ')) == NULL
) {
364 return (PF_OSFP_NOMATCH
);
368 /* The class is easy to find since it is delimited by a space */
369 if ((class_nm
= lookup_name_list(&classes
, wr_name
)) == NULL
) {
371 return (PF_OSFP_NOMATCH
);
373 class = class_nm
->nm_num
;
376 if ((version_nm
= lookup_name_list(&class_nm
->nm_sublist
, ptr
)))
378 version
= version_nm
->nm_num
;
379 subtype
= PF_OSFP_ANY
;
386 * There must be a version and a subtype.
387 * We'll do some fuzzy matching to pick up things like:
388 * Linux 2.2.14 (version=2.2 subtype=14)
389 * FreeBSD 4.0-STABLE (version=4.0 subtype=STABLE)
390 * Windows 2000 SP2 (version=2000 subtype=SP2)
392 #define CONNECTOR(x) ((x) == '.' || (x) == ' ' || (x) == '\t' || (x) == '-')
393 wr_len
= strlen(ptr
);
394 LIST_FOREACH(version_nm
, &class_nm
->nm_sublist
, nm_entry
) {
395 version_len
= strlen(version_nm
->nm_name
);
396 if (wr_len
< version_len
+ 2 ||
397 !CONNECTOR(ptr
[version_len
]))
399 /* first part of the string must be version */
400 if (strncasecmp(ptr
, version_nm
->nm_name
,
404 LIST_FOREACH(subtype_nm
, &version_nm
->nm_sublist
,
406 subtype_len
= strlen(subtype_nm
->nm_name
);
407 if (wr_len
!= version_len
+ subtype_len
+ 1)
410 /* last part of the string must be subtype */
411 if (strcasecmp(&ptr
[version_len
+1],
412 subtype_nm
->nm_name
) != 0)
416 version
= version_nm
->nm_num
;
417 subtype
= subtype_nm
->nm_num
;
424 return (PF_OSFP_NOMATCH
);
428 PF_OSFP_PACK(ret
, class, version
, subtype
);
429 if (ret
!= PF_OSFP_NOMATCH
) {
430 PF_OSFP_UNPACK(ret
, unp_class
, unp_version
, unp_subtype
);
431 if (class != unp_class
) {
432 fprintf(stderr
, "warning: fingerprint table overflowed "
434 return (PF_OSFP_NOMATCH
);
436 if (version
!= unp_version
) {
437 fprintf(stderr
, "warning: fingerprint table overflowed "
439 return (PF_OSFP_NOMATCH
);
441 if (subtype
!= unp_subtype
) {
442 fprintf(stderr
, "warning: fingerprint table overflowed "
444 return (PF_OSFP_NOMATCH
);
447 if (ret
== PF_OSFP_ANY
) {
448 /* should never happen */
449 fprintf(stderr
, "warning: fingerprint packed to 'any'\n");
450 return (PF_OSFP_NOMATCH
);
456 /* Lookup a fingerprint name by ID */
458 pfctl_lookup_fingerprint(pf_osfp_t fp
, char *buf
, size_t len
)
460 int class, version
, subtype
;
461 struct name_list
*list
;
462 struct name_entry
*nm
;
464 char *class_name
, *version_name
, *subtype_name
;
465 class_name
= version_name
= subtype_name
= NULL
;
467 if (fp
== PF_OSFP_UNKNOWN
) {
468 strlcpy(buf
, "unknown", len
);
471 if (fp
== PF_OSFP_ANY
) {
472 strlcpy(buf
, "any", len
);
476 PF_OSFP_UNPACK(fp
, class, version
, subtype
);
477 if (class >= (1 << _FP_CLASS_BITS
) ||
478 version
>= (1 << _FP_VERSION_BITS
) ||
479 subtype
>= (1 << _FP_SUBTYPE_BITS
)) {
480 warnx("PF_OSFP_UNPACK(0x%x) failed!!", fp
);
481 strlcpy(buf
, "nomatch", len
);
485 LIST_FOREACH(nm
, &classes
, nm_entry
) {
486 if (nm
->nm_num
== class) {
487 class_name
= nm
->nm_name
;
488 if (version
== PF_OSFP_ANY
)
490 list
= &nm
->nm_sublist
;
491 LIST_FOREACH(nm
, list
, nm_entry
) {
492 if (nm
->nm_num
== version
) {
493 version_name
= nm
->nm_name
;
494 if (subtype
== PF_OSFP_ANY
)
496 list
= &nm
->nm_sublist
;
497 LIST_FOREACH(nm
, list
, nm_entry
) {
498 if (nm
->nm_num
== subtype
) {
503 } /* foreach subtype */
504 strlcpy(buf
, "nomatch", len
);
507 } /* foreach version */
508 strlcpy(buf
, "nomatch", len
);
511 } /* foreach class */
513 strlcpy(buf
, "nomatch", len
);
517 snprintf(buf
, len
, "%s", class_name
);
519 strlcat(buf
, " ", len
);
520 strlcat(buf
, version_name
, len
);
522 if (strchr(version_name
, ' '))
523 strlcat(buf
, " ", len
);
524 else if (strchr(version_name
, '.') &&
525 isdigit((unsigned char)*subtype_name
))
526 strlcat(buf
, ".", len
);
528 strlcat(buf
, " ", len
);
529 strlcat(buf
, subtype_name
, len
);
535 /* lookup a name in a list */
537 lookup_name_list(struct name_list
*list
, const char *name
)
539 struct name_entry
*nm
;
540 LIST_FOREACH(nm
, list
, nm_entry
)
541 if (strcasecmp(name
, nm
->nm_name
) == 0)
549 add_fingerprint(int dev
, int opts
, struct pf_osfp_ioctl
*fp
)
551 struct pf_osfp_ioctl fptmp
;
552 struct name_entry
*nm_class
, *nm_version
, *nm_subtype
;
553 int class, version
, subtype
;
555 /* We expand #-# or #.#-#.# version/subtypes into multiple fingerprints */
556 #define EXPAND(field) do { \
557 int _dot = -1, _start = -1, _end = -1, _i = 0; \
558 /* pick major version out of #.# */ \
559 if (isdigit((unsigned char)fp->field[_i]) && fp->field[_i+1] == '.') { \
560 _dot = fp->field[_i] - '0'; \
563 if (isdigit((unsigned char)fp->field[_i])) \
564 _start = fp->field[_i++] - '0'; \
567 if (isdigit((unsigned char)fp->field[_i])) \
568 _start = (_start * 10) + fp->field[_i++] - '0'; \
569 if (fp->field[_i++] != '-') \
571 if (isdigit((unsigned char)fp->field[_i]) && fp->field[_i+1] == '.' && \
572 fp->field[_i] - '0' == _dot) \
574 else if (_dot != -1) \
576 if (isdigit((unsigned char)fp->field[_i])) \
577 _end = fp->field[_i++] - '0'; \
580 if (isdigit((unsigned char)fp->field[_i])) \
581 _end = (_end * 10) + fp->field[_i++] - '0'; \
582 if (isdigit((unsigned char)fp->field[_i])) \
583 _end = (_end * 10) + fp->field[_i++] - '0'; \
584 if (fp->field[_i] != '\0') \
586 memcpy(&fptmp, fp, sizeof(fptmp)); \
587 for (;_start <= _end; _start++) { \
588 memset(fptmp.field, 0, sizeof(fptmp.field)); \
589 fptmp.fp_os.fp_enflags |= PF_OSFP_EXPANDED; \
591 snprintf(fptmp.field, sizeof(fptmp.field), \
594 snprintf(fptmp.field, sizeof(fptmp.field), \
595 "%d.%d", _dot, _start); \
596 add_fingerprint(dev, opts, &fptmp); \
600 /* We allow "#-#" as a version or subtype and we'll expand it */
601 EXPAND(fp_os
.fp_version_nm
);
602 EXPAND(fp_os
.fp_subtype_nm
);
604 if (strcasecmp(fp
->fp_os
.fp_class_nm
, "nomatch") == 0)
605 errx(1, "fingerprint class \"nomatch\" is reserved");
607 version
= PF_OSFP_ANY
;
608 subtype
= PF_OSFP_ANY
;
610 nm_class
= fingerprint_name_entry(&classes
, fp
->fp_os
.fp_class_nm
);
611 if (nm_class
->nm_num
== 0)
612 nm_class
->nm_num
= ++class_count
;
613 class = nm_class
->nm_num
;
615 nm_version
= fingerprint_name_entry(&nm_class
->nm_sublist
,
616 fp
->fp_os
.fp_version_nm
);
618 if (nm_version
->nm_num
== 0)
619 nm_version
->nm_num
= ++nm_class
->nm_sublist_num
;
620 version
= nm_version
->nm_num
;
621 nm_subtype
= fingerprint_name_entry(&nm_version
->nm_sublist
,
622 fp
->fp_os
.fp_subtype_nm
);
624 if (nm_subtype
->nm_num
== 0)
626 ++nm_version
->nm_sublist_num
;
627 subtype
= nm_subtype
->nm_num
;
632 DEBUG(fp
, "\tsignature %d:%d:%d %s", class, version
, subtype
,
635 PF_OSFP_PACK(fp
->fp_os
.fp_os
, class, version
, subtype
);
638 #ifdef FAKE_PF_KERNEL
639 /* Linked to the sys/net/pf_osfp.c. Call pf_osfp_add() */
640 if ((errno
= pf_osfp_add(fp
)))
642 if ((opts
& PF_OPT_NOACTION
) == 0 && ioctl(dev
, DIOCOSFPADD
, fp
))
643 #endif /* FAKE_PF_KERNEL */
645 if (errno
== EEXIST
) {
646 warn("Duplicate signature for %s %s %s",
647 fp
->fp_os
.fp_class_nm
,
648 fp
->fp_os
.fp_version_nm
,
649 fp
->fp_os
.fp_subtype_nm
);
652 err(1, "DIOCOSFPADD");
657 /* import a fingerprint from the kernel */
659 import_fingerprint(struct pf_osfp_ioctl
*fp
)
661 struct name_entry
*nm_class
, *nm_version
, *nm_subtype
;
662 int class, version
, subtype
;
664 PF_OSFP_UNPACK(fp
->fp_os
.fp_os
, class, version
, subtype
);
666 nm_class
= fingerprint_name_entry(&classes
, fp
->fp_os
.fp_class_nm
);
667 if (nm_class
->nm_num
== 0) {
668 nm_class
->nm_num
= class;
669 class_count
= MAX(class_count
, class);
672 nm_version
= fingerprint_name_entry(&nm_class
->nm_sublist
,
673 fp
->fp_os
.fp_version_nm
);
675 if (nm_version
->nm_num
== 0) {
676 nm_version
->nm_num
= version
;
677 nm_class
->nm_sublist_num
= MAX(nm_class
->nm_sublist_num
,
680 nm_subtype
= fingerprint_name_entry(&nm_version
->nm_sublist
,
681 fp
->fp_os
.fp_subtype_nm
);
683 if (nm_subtype
->nm_num
== 0) {
684 nm_subtype
->nm_num
= subtype
;
685 nm_version
->nm_sublist_num
=
686 MAX(nm_version
->nm_sublist_num
, subtype
);
693 DEBUG(fp
, "import signature %d:%d:%d", class, version
, subtype
);
696 /* Find an entry for a fingerprints class/version/subtype */
698 fingerprint_name_entry(struct name_list
*list
, char *name
)
700 struct name_entry
*nm_entry
;
702 if (name
== NULL
|| strlen(name
) == 0)
705 LIST_FOREACH(nm_entry
, list
, nm_entry
) {
706 if (strcasecmp(nm_entry
->nm_name
, name
) == 0) {
707 /* We'll move this to the front of the list later */
708 LIST_REMOVE(nm_entry
, nm_entry
);
712 if (nm_entry
== NULL
) {
713 nm_entry
= calloc(1, sizeof(*nm_entry
));
714 if (nm_entry
== NULL
)
716 LIST_INIT(&nm_entry
->nm_sublist
);
717 strlcpy(nm_entry
->nm_name
, name
, sizeof(nm_entry
->nm_name
));
719 LIST_INSERT_HEAD(list
, nm_entry
, nm_entry
);
725 print_name_list(int opts
, struct name_list
*nml
, const char *prefix
)
728 struct name_entry
*nm
;
730 LIST_FOREACH(nm
, nml
, nm_entry
) {
731 snprintf(newprefix
, sizeof(newprefix
), "%s%s\t", prefix
,
733 printf("%s\n", newprefix
);
734 print_name_list(opts
, &nm
->nm_sublist
, newprefix
);
739 sort_name_list(int opts
, struct name_list
*nml
)
741 struct name_list
new;
742 struct name_entry
*nm
, *nmsearch
, *nmlast
;
744 /* yes yes, it's a very slow sort. so sue me */
748 while ((nm
= LIST_FIRST(nml
)) != NULL
) {
749 LIST_REMOVE(nm
, nm_entry
);
751 LIST_FOREACH(nmsearch
, &new, nm_entry
) {
752 if (strcasecmp(nmsearch
->nm_name
, nm
->nm_name
) > 0) {
753 LIST_INSERT_BEFORE(nmsearch
, nm
, nm_entry
);
758 if (nmsearch
== NULL
) {
760 LIST_INSERT_AFTER(nmlast
, nm
, nm_entry
);
762 LIST_INSERT_HEAD(&new, nm
, nm_entry
);
765 sort_name_list(opts
, &nm
->nm_sublist
);
768 while ((nm
= LIST_FIRST(&new)) != NULL
) {
769 LIST_REMOVE(nm
, nm_entry
);
771 LIST_INSERT_HEAD(nml
, nm
, nm_entry
);
773 LIST_INSERT_AFTER(nmlast
, nm
, nm_entry
);
778 /* parse the next integer in a formatted config file line */
780 get_int(char **line
, size_t *len
, int *var
, int *mod
,
781 const char *name
, int flags
, int max
, const char *filename
, int lineno
)
791 field
= get_field(line
, len
, &fieldlen
);
795 fprintf(stderr
, "%s:%d empty %s\n", filename
, lineno
, name
);
800 if ((*field
== '%' || *field
== 'S' || *field
== 'T' || *field
== '*')
804 if (mod
&& (flags
& T_MSS
))
810 if (mod
&& (flags
& T_MTU
))
817 fprintf(stderr
, "%s:%d long '%c' %s\n",
818 filename
, lineno
, *field
, name
);
821 if (mod
&& (flags
& T_DC
)) {
826 if (mod
&& (flags
& T_MOD
))
829 fprintf(stderr
, "%s:%d modulus %s must have a "
830 "value\n", filename
, lineno
, name
);
835 if (mod
== NULL
|| *mod
== 0) {
836 fprintf(stderr
, "%s:%d does not allow %c' %s\n",
837 filename
, lineno
, *field
, name
);
843 for (; i
< fieldlen
; i
++) {
844 if (field
[i
] < '0' || field
[i
] > '9') {
845 fprintf(stderr
, "%s:%d non-digit character in %s\n",
846 filename
, lineno
, name
);
849 val
= val
* 10 + field
[i
] - '0';
851 fprintf(stderr
, "%s:%d %s overflowed\n", filename
,
858 fprintf(stderr
, "%s:%d %s value %ld > %d\n", filename
, lineno
,
867 /* parse the next string in a formatted config file line */
869 get_str(char **line
, size_t *len
, char **v
, const char *name
, int minlen
,
870 const char *filename
, int lineno
)
875 ptr
= get_field(line
, len
, &fieldlen
);
878 if (fieldlen
< minlen
) {
879 fprintf(stderr
, "%s:%d too short %s\n", filename
, lineno
, name
);
882 if ((*v
= malloc(fieldlen
+ 1)) == NULL
) {
886 memcpy(*v
, ptr
, fieldlen
);
887 (*v
)[fieldlen
] = '\0';
892 /* Parse out the TCP opts */
894 get_tcpopts(const char *filename
, int lineno
, const char *tcpopts
,
895 pf_tcpopts_t
*packed
, int *optcnt
, int *mss
, int *mss_mod
, int *wscale
,
896 int *wscale_mod
, int *ts0
)
907 if (strcmp(tcpopts
, ".") == 0)
910 for (i
= 0; tcpopts
[i
] && *optcnt
< PF_OSFP_MAX_OPTS
;) {
911 switch ((opt
= toupper((unsigned char)tcpopts
[i
++]))) {
912 case 'N': /* FALLTHROUGH */
914 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
915 (opt
== 'N' ? PF_OSFP_TCPOPT_NOP
:
916 PF_OSFP_TCPOPT_SACK
);
918 case 'W': /* FALLTHROUGH */
920 int *this_mod
, *this;
924 this_mod
= wscale_mod
;
932 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
933 (opt
== 'W' ? PF_OSFP_TCPOPT_WSCALE
:
935 if (tcpopts
[i
] == '*' && (tcpopts
[i
+ 1] == '\0' ||
936 tcpopts
[i
+ 1] == ',')) {
942 if (tcpopts
[i
] == '%') {
947 if (!isdigit((unsigned char)tcpopts
[i
])) {
948 fprintf(stderr
, "%s:%d unknown "
949 "character '%c' in %c TCP opt\n",
950 filename
, lineno
, tcpopts
[i
], opt
);
953 *this = (*this * 10) + tcpopts
[i
++] - '0';
954 } while(tcpopts
[i
] != ',' && tcpopts
[i
] != '\0');
958 if (tcpopts
[i
] == '0') {
962 *packed
= (*packed
<< PF_OSFP_TCPOPT_BITS
) |
967 if (tcpopts
[i
] == '\0')
969 if (tcpopts
[i
] != ',') {
970 fprintf(stderr
, "%s:%d unknown option to %c TCP opt\n",
971 filename
, lineno
, opt
);
980 /* rip the next field ouf of a formatted config file line */
982 get_field(char **line
, size_t *len
, int *fieldlen
)
984 char *ret
, *ptr
= *line
;
988 while (plen
&& isspace((unsigned char)*ptr
)) {
995 for (; plen
> 0 && *ptr
!= ':'; plen
--, ptr
++)
1003 while (*fieldlen
&& isspace((unsigned char)ret
[*fieldlen
- 1]))
1010 print_ioctl(struct pf_osfp_ioctl
*fp
)
1012 static char buf
[1024];
1017 if (fp
->fp_flags
& PF_OSFP_WSIZE_DC
)
1018 strlcat(buf
, "*", sizeof(buf
));
1019 else if (fp
->fp_flags
& PF_OSFP_WSIZE_MSS
)
1020 strlcat(buf
, "S", sizeof(buf
));
1021 else if (fp
->fp_flags
& PF_OSFP_WSIZE_MTU
)
1022 strlcat(buf
, "T", sizeof(buf
));
1024 if (fp
->fp_flags
& PF_OSFP_WSIZE_MOD
)
1025 strlcat(buf
, "%", sizeof(buf
));
1026 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_wsize
);
1027 strlcat(buf
, tmp
, sizeof(buf
));
1029 strlcat(buf
, ":", sizeof(buf
));
1031 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_ttl
);
1032 strlcat(buf
, tmp
, sizeof(buf
));
1033 strlcat(buf
, ":", sizeof(buf
));
1035 if (fp
->fp_flags
& PF_OSFP_DF
)
1036 strlcat(buf
, "1", sizeof(buf
));
1038 strlcat(buf
, "0", sizeof(buf
));
1039 strlcat(buf
, ":", sizeof(buf
));
1041 if (fp
->fp_flags
& PF_OSFP_PSIZE_DC
)
1042 strlcat(buf
, "*", sizeof(buf
));
1044 if (fp
->fp_flags
& PF_OSFP_PSIZE_MOD
)
1045 strlcat(buf
, "%", sizeof(buf
));
1046 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_psize
);
1047 strlcat(buf
, tmp
, sizeof(buf
));
1049 strlcat(buf
, ":", sizeof(buf
));
1051 if (fp
->fp_optcnt
== 0)
1052 strlcat(buf
, ".", sizeof(buf
));
1053 for (i
= fp
->fp_optcnt
- 1; i
>= 0; i
--) {
1054 opt
= fp
->fp_tcpopts
>> (i
* PF_OSFP_TCPOPT_BITS
);
1055 opt
&= (1 << PF_OSFP_TCPOPT_BITS
) - 1;
1057 case PF_OSFP_TCPOPT_NOP
:
1058 strlcat(buf
, "N", sizeof(buf
));
1060 case PF_OSFP_TCPOPT_SACK
:
1061 strlcat(buf
, "S", sizeof(buf
));
1063 case PF_OSFP_TCPOPT_TS
:
1064 strlcat(buf
, "T", sizeof(buf
));
1065 if (fp
->fp_flags
& PF_OSFP_TS0
)
1066 strlcat(buf
, "0", sizeof(buf
));
1068 case PF_OSFP_TCPOPT_MSS
:
1069 strlcat(buf
, "M", sizeof(buf
));
1070 if (fp
->fp_flags
& PF_OSFP_MSS_DC
)
1071 strlcat(buf
, "*", sizeof(buf
));
1073 if (fp
->fp_flags
& PF_OSFP_MSS_MOD
)
1074 strlcat(buf
, "%", sizeof(buf
));
1075 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_mss
);
1076 strlcat(buf
, tmp
, sizeof(buf
));
1079 case PF_OSFP_TCPOPT_WSCALE
:
1080 strlcat(buf
, "W", sizeof(buf
));
1081 if (fp
->fp_flags
& PF_OSFP_WSCALE_DC
)
1082 strlcat(buf
, "*", sizeof(buf
));
1084 if (fp
->fp_flags
& PF_OSFP_WSCALE_MOD
)
1085 strlcat(buf
, "%", sizeof(buf
));
1086 snprintf(tmp
, sizeof(tmp
), "%d", fp
->fp_wscale
);
1087 strlcat(buf
, tmp
, sizeof(buf
));
1093 strlcat(buf
, ",", sizeof(buf
));
1095 strlcat(buf
, ":", sizeof(buf
));
1097 strlcat(buf
, fp
->fp_os
.fp_class_nm
, sizeof(buf
));
1098 strlcat(buf
, ":", sizeof(buf
));
1099 strlcat(buf
, fp
->fp_os
.fp_version_nm
, sizeof(buf
));
1100 strlcat(buf
, ":", sizeof(buf
));
1101 strlcat(buf
, fp
->fp_os
.fp_subtype_nm
, sizeof(buf
));
1102 strlcat(buf
, ":", sizeof(buf
));
1104 snprintf(tmp
, sizeof(tmp
), "TcpOpts %d 0x%llx", fp
->fp_optcnt
,
1105 (long long int)fp
->fp_tcpopts
);
1106 strlcat(buf
, tmp
, sizeof(buf
));