Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / pf / sbin / pfctl / pfctl_osfp.c
bloba597b45e0e17ef65a8011f761d8e58e966db3ea4
1 /* $NetBSD$ */
2 /* $OpenBSD: pfctl_osfp.c,v 1.15 2006/12/13 05:10:15 itojun Exp $ */
4 /*
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>
24 #include <net/if.h>
25 #include <net/pfvar.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <netinet/ip6.h>
31 #include <ctype.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "pfctl_parser.h"
39 #include "pfctl.h"
41 #ifndef MIN
42 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
43 #endif /* MIN */
44 #ifndef MAX
45 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
46 #endif /* MAX */
49 #if 0
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);
53 #else
54 # define DEBUG(fp, str, v...) ((void)0)
55 #endif
58 struct name_entry;
59 LIST_HEAD(name_list, name_entry);
60 struct name_entry {
61 LIST_ENTRY(name_entry) nm_entry;
62 int nm_num;
63 char nm_name[PF_OSFP_LEN];
65 struct name_list nm_sublist;
66 int nm_sublist_num;
68 struct name_list classes = LIST_HEAD_INITIALIZER(&classes);
69 int class_count;
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,
79 const char *, int);
80 int get_tcpopts(const char *, int, const char *,
81 pf_tcpopts_t *, int *, int *, int *, int *, int *,
82 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 */
90 int
91 pfctl_file_fingerprints(int dev, int opts, const char *fp_filename)
93 FILE *in;
94 char *line;
95 size_t len;
96 int i, lineno = 0;
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);
107 return (1);
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) {
115 lineno++;
116 if (class)
117 free(class);
118 if (version)
119 free(version);
120 if (subtype)
121 free(subtype);
122 if (desc)
123 free(desc);
124 if (tcpopts)
125 free(tcpopts);
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] == '#') {
132 len = i;
133 break;
135 /* Chop off whitespace */
136 while (len > 0 && isspace((unsigned char)line[len - 1]))
137 len--;
138 while (len > 0 && isspace((unsigned char)line[0])) {
139 len--;
140 line++;
142 if (len == 0)
143 continue;
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|
156 T_MOD, 0xffff) ||
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,
160 8192) ||
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))
166 continue;
167 if (get_tcpopts(fp_filename, lineno, tcpopts, &packed_tcpopts,
168 &optcnt, &mss, &mss_mod, &wscale, &wscale_mod, &ts0))
169 continue;
170 if (len != 0) {
171 fprintf(stderr, "%s:%d excess field\n", fp_filename,
172 lineno);
173 continue;
176 fp.fp_ttl = ttl;
177 if (df)
178 fp.fp_flags |= PF_OSFP_DF;
179 switch (w_mod) {
180 case 0:
181 break;
182 case T_DC:
183 fp.fp_flags |= PF_OSFP_WSIZE_DC;
184 break;
185 case T_MSS:
186 fp.fp_flags |= PF_OSFP_WSIZE_MSS;
187 break;
188 case T_MTU:
189 fp.fp_flags |= PF_OSFP_WSIZE_MTU;
190 break;
191 case T_MOD:
192 fp.fp_flags |= PF_OSFP_WSIZE_MOD;
193 break;
195 fp.fp_wsize = window;
197 switch (p_mod) {
198 case T_DC:
199 fp.fp_flags |= PF_OSFP_PSIZE_DC;
200 break;
201 case T_MOD:
202 fp.fp_flags |= PF_OSFP_PSIZE_MOD;
204 fp.fp_psize = psize;
207 switch (wscale_mod) {
208 case T_DC:
209 fp.fp_flags |= PF_OSFP_WSCALE_DC;
210 break;
211 case T_MOD:
212 fp.fp_flags |= PF_OSFP_WSCALE_MOD;
214 fp.fp_wscale = wscale;
216 switch (mss_mod) {
217 case T_DC:
218 fp.fp_flags |= PF_OSFP_MSS_DC;
219 break;
220 case T_MOD:
221 fp.fp_flags |= PF_OSFP_MSS_MOD;
222 break;
224 fp.fp_mss = mss;
226 fp.fp_tcpopts = packed_tcpopts;
227 fp.fp_optcnt = optcnt;
228 if (ts0)
229 fp.fp_flags |= PF_OSFP_TS0;
231 if (class[0] == '@')
232 fp.fp_os.fp_enflags |= PF_OSFP_GENERIC;
233 if (class[0] == '*')
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));
239 else
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);
254 if (class)
255 free(class);
256 if (version)
257 free(version);
258 if (subtype)
259 free(subtype);
260 if (desc)
261 free(desc);
262 if (tcpopts)
263 free(tcpopts);
265 fclose(in);
267 if (opts & PF_OPT_VERBOSE2)
268 printf("Loaded %d passive OS fingerprints\n",
269 fingerprint_count);
270 return (0);
273 /* flush the kernel's fingerprints */
274 void
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 */
282 void
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);
290 free(nm);
292 fingerprint_count = 0;
293 class_count = 0;
296 /* Fetch the active fingerprints from the kernel */
298 pfctl_load_fingerprints(int dev, int opts)
300 struct pf_osfp_ioctl io;
301 int i;
303 pfctl_flush_my_fingerprints(&classes);
305 for (i = 0; i >= 0; i++) {
306 memset(&io, 0, sizeof(io));
307 io.fp_getnum = i;
308 if (ioctl(dev, DIOCOSFPGET, &io)) {
309 if (errno == EBUSY)
310 break;
311 warn("DIOCOSFPGET");
312 return (1);
314 import_fingerprint(&io);
316 return (0);
319 /* List the fingerprints */
320 void
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);
327 } else {
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 */
337 pf_osfp_t
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;
345 char *ptr, *wr_name;
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))) {
352 class = nm->nm_num;
353 version = PF_OSFP_ANY;
354 subtype = PF_OSFP_ANY;
355 goto found;
356 } else {
358 /* Chop it up into class/version/subtype */
360 if ((wr_name = strdup(name)) == NULL)
361 err(1, "malloc");
362 if ((ptr = strchr(wr_name, ' ')) == NULL) {
363 free(wr_name);
364 return (PF_OSFP_NOMATCH);
366 *ptr++ = '\0';
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) {
370 free(wr_name);
371 return (PF_OSFP_NOMATCH);
373 class = class_nm->nm_num;
375 /* Try no subtype */
376 if ((version_nm = lookup_name_list(&class_nm->nm_sublist, ptr)))
378 version = version_nm->nm_num;
379 subtype = PF_OSFP_ANY;
380 free(wr_name);
381 goto found;
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]))
398 continue;
399 /* first part of the string must be version */
400 if (strncasecmp(ptr, version_nm->nm_name,
401 version_len))
402 continue;
404 LIST_FOREACH(subtype_nm, &version_nm->nm_sublist,
405 nm_entry) {
406 subtype_len = strlen(subtype_nm->nm_name);
407 if (wr_len != version_len + subtype_len + 1)
408 continue;
410 /* last part of the string must be subtype */
411 if (strcasecmp(&ptr[version_len+1],
412 subtype_nm->nm_name) != 0)
413 continue;
415 /* Found it!! */
416 version = version_nm->nm_num;
417 subtype = subtype_nm->nm_num;
418 free(wr_name);
419 goto found;
423 free(wr_name);
424 return (PF_OSFP_NOMATCH);
427 found:
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 "
433 "classes\n");
434 return (PF_OSFP_NOMATCH);
436 if (version != unp_version) {
437 fprintf(stderr, "warning: fingerprint table overflowed "
438 "versions\n");
439 return (PF_OSFP_NOMATCH);
441 if (subtype != unp_subtype) {
442 fprintf(stderr, "warning: fingerprint table overflowed "
443 "subtypes\n");
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);
453 return (ret);
456 /* Lookup a fingerprint name by ID */
457 char *
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);
469 return (buf);
471 if (fp == PF_OSFP_ANY) {
472 strlcpy(buf, "any", len);
473 return (buf);
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);
482 return (buf);
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)
489 goto found;
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)
495 goto found;
496 list = &nm->nm_sublist;
497 LIST_FOREACH(nm, list, nm_entry) {
498 if (nm->nm_num == subtype) {
499 subtype_name =
500 nm->nm_name;
501 goto found;
503 } /* foreach subtype */
504 strlcpy(buf, "nomatch", len);
505 return (buf);
507 } /* foreach version */
508 strlcpy(buf, "nomatch", len);
509 return (buf);
511 } /* foreach class */
513 strlcpy(buf, "nomatch", len);
514 return (buf);
516 found:
517 snprintf(buf, len, "%s", class_name);
518 if (version_name) {
519 strlcat(buf, " ", len);
520 strlcat(buf, version_name, len);
521 if (subtype_name) {
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);
527 else
528 strlcat(buf, " ", len);
529 strlcat(buf, subtype_name, len);
532 return (buf);
535 /* lookup a name in a list */
536 struct name_entry *
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)
542 return (nm);
544 return (NULL);
548 void
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'; \
561 _i += 2; \
563 if (isdigit((unsigned char)fp->field[_i])) \
564 _start = fp->field[_i++] - '0'; \
565 else \
566 break; \
567 if (isdigit((unsigned char)fp->field[_i])) \
568 _start = (_start * 10) + fp->field[_i++] - '0'; \
569 if (fp->field[_i++] != '-') \
570 break; \
571 if (isdigit((unsigned char)fp->field[_i]) && fp->field[_i+1] == '.' && \
572 fp->field[_i] - '0' == _dot) \
573 _i += 2; \
574 else if (_dot != -1) \
575 break; \
576 if (isdigit((unsigned char)fp->field[_i])) \
577 _end = fp->field[_i++] - '0'; \
578 else \
579 break; \
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') \
585 break; \
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; \
590 if (_dot == -1) \
591 snprintf(fptmp.field, sizeof(fptmp.field), \
592 "%d", _start); \
593 else \
594 snprintf(fptmp.field, sizeof(fptmp.field), \
595 "%d.%d", _dot, _start); \
596 add_fingerprint(dev, opts, &fptmp); \
598 } while(0)
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);
617 if (nm_version) {
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);
623 if (nm_subtype) {
624 if (nm_subtype->nm_num == 0)
625 nm_subtype->nm_num =
626 ++nm_version->nm_sublist_num;
627 subtype = nm_subtype->nm_num;
632 DEBUG(fp, "\tsignature %d:%d:%d %s", class, version, subtype,
633 print_ioctl(fp));
635 PF_OSFP_PACK(fp->fp_os.fp_os, class, version, subtype);
636 fingerprint_count++;
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)))
641 #else
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);
651 } else {
652 err(1, "DIOCOSFPADD");
657 /* import a fingerprint from the kernel */
658 void
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);
674 if (nm_version) {
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,
678 version);
680 nm_subtype = fingerprint_name_entry(&nm_version->nm_sublist,
681 fp->fp_os.fp_subtype_nm);
682 if (nm_subtype) {
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);
692 fingerprint_count++;
693 DEBUG(fp, "import signature %d:%d:%d", class, version, subtype);
696 /* Find an entry for a fingerprints class/version/subtype */
697 struct name_entry *
698 fingerprint_name_entry(struct name_list *list, char *name)
700 struct name_entry *nm_entry;
702 if (name == NULL || strlen(name) == 0)
703 return (NULL);
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);
709 break;
712 if (nm_entry == NULL) {
713 nm_entry = calloc(1, sizeof(*nm_entry));
714 if (nm_entry == NULL)
715 err(1, "calloc");
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);
720 return (nm_entry);
724 void
725 print_name_list(int opts, struct name_list *nml, const char *prefix)
727 char newprefix[32];
728 struct name_entry *nm;
730 LIST_FOREACH(nm, nml, nm_entry) {
731 snprintf(newprefix, sizeof(newprefix), "%s%s\t", prefix,
732 nm->nm_name);
733 printf("%s\n", newprefix);
734 print_name_list(opts, &nm->nm_sublist, newprefix);
738 void
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 */
746 LIST_INIT(&new);
748 while ((nm = LIST_FIRST(nml)) != NULL) {
749 LIST_REMOVE(nm, nm_entry);
750 nmlast = NULL;
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);
754 break;
756 nmlast = nmsearch;
758 if (nmsearch == NULL) {
759 if (nmlast)
760 LIST_INSERT_AFTER(nmlast, nm, nm_entry);
761 else
762 LIST_INSERT_HEAD(&new, nm, nm_entry);
765 sort_name_list(opts, &nm->nm_sublist);
767 nmlast = NULL;
768 while ((nm = LIST_FIRST(&new)) != NULL) {
769 LIST_REMOVE(nm, nm_entry);
770 if (nmlast == NULL)
771 LIST_INSERT_HEAD(nml, nm, nm_entry);
772 else
773 LIST_INSERT_AFTER(nmlast, nm, nm_entry);
774 nmlast = nm;
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)
783 int fieldlen, i;
784 char *field;
785 long val = 0;
787 if (mod)
788 *mod = 0;
789 *var = 0;
791 field = get_field(line, len, &fieldlen);
792 if (field == NULL)
793 return (1);
794 if (fieldlen == 0) {
795 fprintf(stderr, "%s:%d empty %s\n", filename, lineno, name);
796 return (1);
799 i = 0;
800 if ((*field == '%' || *field == 'S' || *field == 'T' || *field == '*')
801 && fieldlen >= 1) {
802 switch (*field) {
803 case 'S':
804 if (mod && (flags & T_MSS))
805 *mod = T_MSS;
806 if (fieldlen == 1)
807 return (0);
808 break;
809 case 'T':
810 if (mod && (flags & T_MTU))
811 *mod = T_MTU;
812 if (fieldlen == 1)
813 return (0);
814 break;
815 case '*':
816 if (fieldlen != 1) {
817 fprintf(stderr, "%s:%d long '%c' %s\n",
818 filename, lineno, *field, name);
819 return (1);
821 if (mod && (flags & T_DC)) {
822 *mod = T_DC;
823 return (0);
825 case '%':
826 if (mod && (flags & T_MOD))
827 *mod = T_MOD;
828 if (fieldlen == 1) {
829 fprintf(stderr, "%s:%d modulus %s must have a "
830 "value\n", filename, lineno, name);
831 return (1);
833 break;
835 if (mod == NULL || *mod == 0) {
836 fprintf(stderr, "%s:%d does not allow %c' %s\n",
837 filename, lineno, *field, name);
838 return (1);
840 i++;
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);
847 return (1);
849 val = val * 10 + field[i] - '0';
850 if (val < 0) {
851 fprintf(stderr, "%s:%d %s overflowed\n", filename,
852 lineno, name);
853 return (1);
857 if (val > max) {
858 fprintf(stderr, "%s:%d %s value %ld > %d\n", filename, lineno,
859 name, val, max);
860 return (1);
862 *var = (int)val;
864 return (0);
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)
872 int fieldlen;
873 char *ptr;
875 ptr = get_field(line, len, &fieldlen);
876 if (ptr == NULL)
877 return (1);
878 if (fieldlen < minlen) {
879 fprintf(stderr, "%s:%d too short %s\n", filename, lineno, name);
880 return (1);
882 if ((*v = malloc(fieldlen + 1)) == NULL) {
883 perror("malloc()");
884 return (1);
886 memcpy(*v, ptr, fieldlen);
887 (*v)[fieldlen] = '\0';
889 return (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)
898 int i, opt;
900 *packed = 0;
901 *optcnt = 0;
902 *wscale = 0;
903 *wscale_mod = T_DC;
904 *mss = 0;
905 *mss_mod = T_DC;
906 *ts0 = 0;
907 if (strcmp(tcpopts, ".") == 0)
908 return (0);
910 for (i = 0; tcpopts[i] && *optcnt < PF_OSFP_MAX_OPTS;) {
911 switch ((opt = toupper((unsigned char)tcpopts[i++]))) {
912 case 'N': /* FALLTHROUGH */
913 case 'S':
914 *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
915 (opt == 'N' ? PF_OSFP_TCPOPT_NOP :
916 PF_OSFP_TCPOPT_SACK);
917 break;
918 case 'W': /* FALLTHROUGH */
919 case 'M': {
920 int *this_mod, *this;
922 if (opt == 'W') {
923 this = wscale;
924 this_mod = wscale_mod;
925 } else {
926 this = mss;
927 this_mod = mss_mod;
929 *this = 0;
930 *this_mod = 0;
932 *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
933 (opt == 'W' ? PF_OSFP_TCPOPT_WSCALE :
934 PF_OSFP_TCPOPT_MSS);
935 if (tcpopts[i] == '*' && (tcpopts[i + 1] == '\0' ||
936 tcpopts[i + 1] == ',')) {
937 *this_mod = T_DC;
938 i++;
939 break;
942 if (tcpopts[i] == '%') {
943 *this_mod = T_MOD;
944 i++;
946 do {
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);
951 return (1);
953 *this = (*this * 10) + tcpopts[i++] - '0';
954 } while(tcpopts[i] != ',' && tcpopts[i] != '\0');
955 break;
957 case 'T':
958 if (tcpopts[i] == '0') {
959 *ts0 = 1;
960 i++;
962 *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
963 PF_OSFP_TCPOPT_TS;
964 break;
966 (*optcnt) ++;
967 if (tcpopts[i] == '\0')
968 break;
969 if (tcpopts[i] != ',') {
970 fprintf(stderr, "%s:%d unknown option to %c TCP opt\n",
971 filename, lineno, opt);
972 return (1);
974 i++;
977 return (0);
980 /* rip the next field ouf of a formatted config file line */
981 char *
982 get_field(char **line, size_t *len, int *fieldlen)
984 char *ret, *ptr = *line;
985 size_t plen = *len;
988 while (plen && isspace((unsigned char)*ptr)) {
989 plen--;
990 ptr++;
992 ret = ptr;
993 *fieldlen = 0;
995 for (; plen > 0 && *ptr != ':'; plen--, ptr++)
996 (*fieldlen)++;
997 if (plen) {
998 *line = ptr + 1;
999 *len = plen - 1;
1000 } else {
1001 *len = 0;
1003 while (*fieldlen && isspace((unsigned char)ret[*fieldlen - 1]))
1004 (*fieldlen)--;
1005 return (ret);
1009 const char *
1010 print_ioctl(struct pf_osfp_ioctl *fp)
1012 static char buf[1024];
1013 char tmp[32];
1014 int i, opt;
1016 *buf = '\0';
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));
1023 else {
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));
1037 else
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));
1043 else {
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;
1056 switch (opt) {
1057 case PF_OSFP_TCPOPT_NOP:
1058 strlcat(buf, "N", sizeof(buf));
1059 break;
1060 case PF_OSFP_TCPOPT_SACK:
1061 strlcat(buf, "S", sizeof(buf));
1062 break;
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));
1067 break;
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));
1072 else {
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));
1078 break;
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));
1083 else {
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));
1089 break;
1092 if (i != 0)
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));
1108 return (buf);