13b3a8eb9SGleb Smirnoff /* $OpenBSD: pfctl_osfp.c,v 1.14 2006/04/08 02:13:14 ray Exp $ */
23b3a8eb9SGleb Smirnoff
33b3a8eb9SGleb Smirnoff /*
43b3a8eb9SGleb Smirnoff * Copyright (c) 2003 Mike Frantzen <frantzen@openbsd.org>
53b3a8eb9SGleb Smirnoff *
63b3a8eb9SGleb Smirnoff * Permission to use, copy, modify, and distribute this software for any
73b3a8eb9SGleb Smirnoff * purpose with or without fee is hereby granted, provided that the above
83b3a8eb9SGleb Smirnoff * copyright notice and this permission notice appear in all copies.
93b3a8eb9SGleb Smirnoff *
103b3a8eb9SGleb Smirnoff * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
113b3a8eb9SGleb Smirnoff * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
123b3a8eb9SGleb Smirnoff * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
133b3a8eb9SGleb Smirnoff * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
143b3a8eb9SGleb Smirnoff * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
153b3a8eb9SGleb Smirnoff * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
163b3a8eb9SGleb Smirnoff * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
173b3a8eb9SGleb Smirnoff */
183b3a8eb9SGleb Smirnoff
193b3a8eb9SGleb Smirnoff #include <sys/types.h>
203b3a8eb9SGleb Smirnoff #include <sys/ioctl.h>
213b3a8eb9SGleb Smirnoff #include <sys/socket.h>
223b3a8eb9SGleb Smirnoff
233b3a8eb9SGleb Smirnoff #include <net/if.h>
243b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
253b3a8eb9SGleb Smirnoff
263b3a8eb9SGleb Smirnoff #include <netinet/in_systm.h>
273b3a8eb9SGleb Smirnoff #include <netinet/ip.h>
283b3a8eb9SGleb Smirnoff #include <netinet/ip6.h>
293b3a8eb9SGleb Smirnoff
303b3a8eb9SGleb Smirnoff #include <ctype.h>
313b3a8eb9SGleb Smirnoff #include <err.h>
323b3a8eb9SGleb Smirnoff #include <errno.h>
333b3a8eb9SGleb Smirnoff #include <stdio.h>
343b3a8eb9SGleb Smirnoff #include <stdlib.h>
353b3a8eb9SGleb Smirnoff #include <string.h>
363b3a8eb9SGleb Smirnoff
373b3a8eb9SGleb Smirnoff #include "pfctl_parser.h"
383b3a8eb9SGleb Smirnoff #include "pfctl.h"
393b3a8eb9SGleb Smirnoff
403b3a8eb9SGleb Smirnoff #ifndef MIN
413b3a8eb9SGleb Smirnoff # define MIN(a,b) (((a) < (b)) ? (a) : (b))
423b3a8eb9SGleb Smirnoff #endif /* MIN */
433b3a8eb9SGleb Smirnoff #ifndef MAX
443b3a8eb9SGleb Smirnoff # define MAX(a,b) (((a) > (b)) ? (a) : (b))
453b3a8eb9SGleb Smirnoff #endif /* MAX */
463b3a8eb9SGleb Smirnoff
473b3a8eb9SGleb Smirnoff
483b3a8eb9SGleb Smirnoff #if 0
493b3a8eb9SGleb Smirnoff # define DEBUG(fp, str, v...) \
503b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%s:%s " str "\n", (fp)->fp_os.fp_class_nm, \
513b3a8eb9SGleb Smirnoff (fp)->fp_os.fp_version_nm, (fp)->fp_os.fp_subtype_nm , ## v);
523b3a8eb9SGleb Smirnoff #else
533b3a8eb9SGleb Smirnoff # define DEBUG(fp, str, v...) ((void)0)
543b3a8eb9SGleb Smirnoff #endif
553b3a8eb9SGleb Smirnoff
563b3a8eb9SGleb Smirnoff
573b3a8eb9SGleb Smirnoff struct name_entry;
583b3a8eb9SGleb Smirnoff LIST_HEAD(name_list, name_entry);
593b3a8eb9SGleb Smirnoff struct name_entry {
603b3a8eb9SGleb Smirnoff LIST_ENTRY(name_entry) nm_entry;
613b3a8eb9SGleb Smirnoff int nm_num;
623b3a8eb9SGleb Smirnoff char nm_name[PF_OSFP_LEN];
633b3a8eb9SGleb Smirnoff
643b3a8eb9SGleb Smirnoff struct name_list nm_sublist;
653b3a8eb9SGleb Smirnoff int nm_sublist_num;
663b3a8eb9SGleb Smirnoff };
67*13cfafabSKristof Provost static struct name_list classes = LIST_HEAD_INITIALIZER(&classes);
68*13cfafabSKristof Provost static int class_count;
69*13cfafabSKristof Provost static int fingerprint_count;
703b3a8eb9SGleb Smirnoff
713b3a8eb9SGleb Smirnoff void add_fingerprint(int, int, struct pf_osfp_ioctl *);
723b3a8eb9SGleb Smirnoff struct name_entry *fingerprint_name_entry(struct name_list *, char *);
733b3a8eb9SGleb Smirnoff void pfctl_flush_my_fingerprints(struct name_list *);
743b3a8eb9SGleb Smirnoff char *get_field(char **, size_t *, int *);
753b3a8eb9SGleb Smirnoff int get_int(char **, size_t *, int *, int *, const char *,
763b3a8eb9SGleb Smirnoff int, int, const char *, int);
773b3a8eb9SGleb Smirnoff int get_str(char **, size_t *, char **, const char *, int,
783b3a8eb9SGleb Smirnoff const char *, int);
793b3a8eb9SGleb Smirnoff int get_tcpopts(const char *, int, const char *,
803b3a8eb9SGleb Smirnoff pf_tcpopts_t *, int *, int *, int *, int *, int *,
813b3a8eb9SGleb Smirnoff int *);
823b3a8eb9SGleb Smirnoff void import_fingerprint(struct pf_osfp_ioctl *);
833b3a8eb9SGleb Smirnoff const char *print_ioctl(struct pf_osfp_ioctl *);
843b3a8eb9SGleb Smirnoff void print_name_list(int, struct name_list *, const char *);
853b3a8eb9SGleb Smirnoff void sort_name_list(int, struct name_list *);
863b3a8eb9SGleb Smirnoff struct name_entry *lookup_name_list(struct name_list *, const char *);
873b3a8eb9SGleb Smirnoff
883b3a8eb9SGleb Smirnoff /* Load fingerprints from a file */
893b3a8eb9SGleb Smirnoff int
pfctl_file_fingerprints(int dev,int opts,const char * fp_filename)903b3a8eb9SGleb Smirnoff pfctl_file_fingerprints(int dev, int opts, const char *fp_filename)
913b3a8eb9SGleb Smirnoff {
923b3a8eb9SGleb Smirnoff FILE *in;
933b3a8eb9SGleb Smirnoff char *line;
943b3a8eb9SGleb Smirnoff size_t len;
953b3a8eb9SGleb Smirnoff int i, lineno = 0;
963b3a8eb9SGleb Smirnoff int window, w_mod, ttl, df, psize, p_mod, mss, mss_mod, wscale,
973b3a8eb9SGleb Smirnoff wscale_mod, optcnt, ts0;
983b3a8eb9SGleb Smirnoff pf_tcpopts_t packed_tcpopts;
993b3a8eb9SGleb Smirnoff char *class, *version, *subtype, *desc, *tcpopts;
1003b3a8eb9SGleb Smirnoff struct pf_osfp_ioctl fp;
1013b3a8eb9SGleb Smirnoff
1023b3a8eb9SGleb Smirnoff pfctl_flush_my_fingerprints(&classes);
1033b3a8eb9SGleb Smirnoff
1043b3a8eb9SGleb Smirnoff if ((in = pfctl_fopen(fp_filename, "r")) == NULL) {
1053b3a8eb9SGleb Smirnoff warn("%s", fp_filename);
1063b3a8eb9SGleb Smirnoff return (1);
1073b3a8eb9SGleb Smirnoff }
1083b3a8eb9SGleb Smirnoff class = version = subtype = desc = tcpopts = NULL;
1093b3a8eb9SGleb Smirnoff
1103b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_NOACTION) == 0)
1113b3a8eb9SGleb Smirnoff pfctl_clear_fingerprints(dev, opts);
1123b3a8eb9SGleb Smirnoff
1133b3a8eb9SGleb Smirnoff while ((line = fgetln(in, &len)) != NULL) {
1143b3a8eb9SGleb Smirnoff lineno++;
1153b3a8eb9SGleb Smirnoff if (class)
1163b3a8eb9SGleb Smirnoff free(class);
1173b3a8eb9SGleb Smirnoff if (version)
1183b3a8eb9SGleb Smirnoff free(version);
1193b3a8eb9SGleb Smirnoff if (subtype)
1203b3a8eb9SGleb Smirnoff free(subtype);
1213b3a8eb9SGleb Smirnoff if (desc)
1223b3a8eb9SGleb Smirnoff free(desc);
1233b3a8eb9SGleb Smirnoff if (tcpopts)
1243b3a8eb9SGleb Smirnoff free(tcpopts);
1253b3a8eb9SGleb Smirnoff class = version = subtype = desc = tcpopts = NULL;
1263b3a8eb9SGleb Smirnoff memset(&fp, 0, sizeof(fp));
1273b3a8eb9SGleb Smirnoff
1283b3a8eb9SGleb Smirnoff /* Chop off comment */
1293b3a8eb9SGleb Smirnoff for (i = 0; i < len; i++)
1303b3a8eb9SGleb Smirnoff if (line[i] == '#') {
1313b3a8eb9SGleb Smirnoff len = i;
1323b3a8eb9SGleb Smirnoff break;
1333b3a8eb9SGleb Smirnoff }
1343b3a8eb9SGleb Smirnoff /* Chop off whitespace */
1353b3a8eb9SGleb Smirnoff while (len > 0 && isspace(line[len - 1]))
1363b3a8eb9SGleb Smirnoff len--;
1373b3a8eb9SGleb Smirnoff while (len > 0 && isspace(line[0])) {
1383b3a8eb9SGleb Smirnoff len--;
1393b3a8eb9SGleb Smirnoff line++;
1403b3a8eb9SGleb Smirnoff }
1413b3a8eb9SGleb Smirnoff if (len == 0)
1423b3a8eb9SGleb Smirnoff continue;
1433b3a8eb9SGleb Smirnoff
1443b3a8eb9SGleb Smirnoff #define T_DC 0x01 /* Allow don't care */
1453b3a8eb9SGleb Smirnoff #define T_MSS 0x02 /* Allow MSS multiple */
1463b3a8eb9SGleb Smirnoff #define T_MTU 0x04 /* Allow MTU multiple */
1473b3a8eb9SGleb Smirnoff #define T_MOD 0x08 /* Allow modulus */
1483b3a8eb9SGleb Smirnoff
1493b3a8eb9SGleb Smirnoff #define GET_INT(v, mod, n, ty, mx) \
1503b3a8eb9SGleb Smirnoff get_int(&line, &len, &v, mod, n, ty, mx, fp_filename, lineno)
1513b3a8eb9SGleb Smirnoff #define GET_STR(v, n, mn) \
1523b3a8eb9SGleb Smirnoff get_str(&line, &len, &v, n, mn, fp_filename, lineno)
1533b3a8eb9SGleb Smirnoff
1543b3a8eb9SGleb Smirnoff if (GET_INT(window, &w_mod, "window size", T_DC|T_MSS|T_MTU|
1553b3a8eb9SGleb Smirnoff T_MOD, 0xffff) ||
1563b3a8eb9SGleb Smirnoff GET_INT(ttl, NULL, "ttl", 0, 0xff) ||
1573b3a8eb9SGleb Smirnoff GET_INT(df, NULL, "don't fragment frag", 0, 1) ||
1583b3a8eb9SGleb Smirnoff GET_INT(psize, &p_mod, "overall packet size", T_MOD|T_DC,
1593b3a8eb9SGleb Smirnoff 8192) ||
1603b3a8eb9SGleb Smirnoff GET_STR(tcpopts, "TCP Options", 1) ||
1613b3a8eb9SGleb Smirnoff GET_STR(class, "OS class", 1) ||
1623b3a8eb9SGleb Smirnoff GET_STR(version, "OS version", 0) ||
1633b3a8eb9SGleb Smirnoff GET_STR(subtype, "OS subtype", 0) ||
1643b3a8eb9SGleb Smirnoff GET_STR(desc, "OS description", 2))
1653b3a8eb9SGleb Smirnoff continue;
1663b3a8eb9SGleb Smirnoff if (get_tcpopts(fp_filename, lineno, tcpopts, &packed_tcpopts,
1673b3a8eb9SGleb Smirnoff &optcnt, &mss, &mss_mod, &wscale, &wscale_mod, &ts0))
1683b3a8eb9SGleb Smirnoff continue;
1693b3a8eb9SGleb Smirnoff if (len != 0) {
1703b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d excess field\n", fp_filename,
1713b3a8eb9SGleb Smirnoff lineno);
1723b3a8eb9SGleb Smirnoff continue;
1733b3a8eb9SGleb Smirnoff }
1743b3a8eb9SGleb Smirnoff
1753b3a8eb9SGleb Smirnoff fp.fp_ttl = ttl;
1763b3a8eb9SGleb Smirnoff if (df)
1773b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_DF;
1783b3a8eb9SGleb Smirnoff switch (w_mod) {
1793b3a8eb9SGleb Smirnoff case 0:
1803b3a8eb9SGleb Smirnoff break;
1813b3a8eb9SGleb Smirnoff case T_DC:
1823b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_WSIZE_DC;
1833b3a8eb9SGleb Smirnoff break;
1843b3a8eb9SGleb Smirnoff case T_MSS:
1853b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_WSIZE_MSS;
1863b3a8eb9SGleb Smirnoff break;
1873b3a8eb9SGleb Smirnoff case T_MTU:
1883b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_WSIZE_MTU;
1893b3a8eb9SGleb Smirnoff break;
1903b3a8eb9SGleb Smirnoff case T_MOD:
1913b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_WSIZE_MOD;
1923b3a8eb9SGleb Smirnoff break;
1933b3a8eb9SGleb Smirnoff }
1943b3a8eb9SGleb Smirnoff fp.fp_wsize = window;
1953b3a8eb9SGleb Smirnoff
1963b3a8eb9SGleb Smirnoff switch (p_mod) {
1973b3a8eb9SGleb Smirnoff case T_DC:
1983b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_PSIZE_DC;
1993b3a8eb9SGleb Smirnoff break;
2003b3a8eb9SGleb Smirnoff case T_MOD:
2013b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_PSIZE_MOD;
2023b3a8eb9SGleb Smirnoff }
2033b3a8eb9SGleb Smirnoff fp.fp_psize = psize;
2043b3a8eb9SGleb Smirnoff
2053b3a8eb9SGleb Smirnoff
2063b3a8eb9SGleb Smirnoff switch (wscale_mod) {
2073b3a8eb9SGleb Smirnoff case T_DC:
2083b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_WSCALE_DC;
2093b3a8eb9SGleb Smirnoff break;
2103b3a8eb9SGleb Smirnoff case T_MOD:
2113b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_WSCALE_MOD;
2123b3a8eb9SGleb Smirnoff }
2133b3a8eb9SGleb Smirnoff fp.fp_wscale = wscale;
2143b3a8eb9SGleb Smirnoff
2153b3a8eb9SGleb Smirnoff switch (mss_mod) {
2163b3a8eb9SGleb Smirnoff case T_DC:
2173b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_MSS_DC;
2183b3a8eb9SGleb Smirnoff break;
2193b3a8eb9SGleb Smirnoff case T_MOD:
2203b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_MSS_MOD;
2213b3a8eb9SGleb Smirnoff break;
2223b3a8eb9SGleb Smirnoff }
2233b3a8eb9SGleb Smirnoff fp.fp_mss = mss;
2243b3a8eb9SGleb Smirnoff
2253b3a8eb9SGleb Smirnoff fp.fp_tcpopts = packed_tcpopts;
2263b3a8eb9SGleb Smirnoff fp.fp_optcnt = optcnt;
2273b3a8eb9SGleb Smirnoff if (ts0)
2283b3a8eb9SGleb Smirnoff fp.fp_flags |= PF_OSFP_TS0;
2293b3a8eb9SGleb Smirnoff
2303b3a8eb9SGleb Smirnoff if (class[0] == '@')
2313b3a8eb9SGleb Smirnoff fp.fp_os.fp_enflags |= PF_OSFP_GENERIC;
2323b3a8eb9SGleb Smirnoff if (class[0] == '*')
2333b3a8eb9SGleb Smirnoff fp.fp_os.fp_enflags |= PF_OSFP_NODETAIL;
2343b3a8eb9SGleb Smirnoff
2353b3a8eb9SGleb Smirnoff if (class[0] == '@' || class[0] == '*')
2363b3a8eb9SGleb Smirnoff strlcpy(fp.fp_os.fp_class_nm, class + 1,
2373b3a8eb9SGleb Smirnoff sizeof(fp.fp_os.fp_class_nm));
2383b3a8eb9SGleb Smirnoff else
2393b3a8eb9SGleb Smirnoff strlcpy(fp.fp_os.fp_class_nm, class,
2403b3a8eb9SGleb Smirnoff sizeof(fp.fp_os.fp_class_nm));
2413b3a8eb9SGleb Smirnoff strlcpy(fp.fp_os.fp_version_nm, version,
2423b3a8eb9SGleb Smirnoff sizeof(fp.fp_os.fp_version_nm));
2433b3a8eb9SGleb Smirnoff strlcpy(fp.fp_os.fp_subtype_nm, subtype,
2443b3a8eb9SGleb Smirnoff sizeof(fp.fp_os.fp_subtype_nm));
2453b3a8eb9SGleb Smirnoff
2463b3a8eb9SGleb Smirnoff add_fingerprint(dev, opts, &fp);
2473b3a8eb9SGleb Smirnoff
2483b3a8eb9SGleb Smirnoff fp.fp_flags |= (PF_OSFP_DF | PF_OSFP_INET6);
2493b3a8eb9SGleb Smirnoff fp.fp_psize += sizeof(struct ip6_hdr) - sizeof(struct ip);
2503b3a8eb9SGleb Smirnoff add_fingerprint(dev, opts, &fp);
2513b3a8eb9SGleb Smirnoff }
2523b3a8eb9SGleb Smirnoff
2533b3a8eb9SGleb Smirnoff if (class)
2543b3a8eb9SGleb Smirnoff free(class);
2553b3a8eb9SGleb Smirnoff if (version)
2563b3a8eb9SGleb Smirnoff free(version);
2573b3a8eb9SGleb Smirnoff if (subtype)
2583b3a8eb9SGleb Smirnoff free(subtype);
2593b3a8eb9SGleb Smirnoff if (desc)
2603b3a8eb9SGleb Smirnoff free(desc);
2613b3a8eb9SGleb Smirnoff if (tcpopts)
2623b3a8eb9SGleb Smirnoff free(tcpopts);
2633b3a8eb9SGleb Smirnoff
2643b3a8eb9SGleb Smirnoff fclose(in);
2653b3a8eb9SGleb Smirnoff
2663b3a8eb9SGleb Smirnoff if (opts & PF_OPT_VERBOSE2)
2673b3a8eb9SGleb Smirnoff printf("Loaded %d passive OS fingerprints\n",
2683b3a8eb9SGleb Smirnoff fingerprint_count);
2693b3a8eb9SGleb Smirnoff return (0);
2703b3a8eb9SGleb Smirnoff }
2713b3a8eb9SGleb Smirnoff
2723b3a8eb9SGleb Smirnoff /* flush the kernel's fingerprints */
2733b3a8eb9SGleb Smirnoff void
pfctl_clear_fingerprints(int dev,int opts)2743b3a8eb9SGleb Smirnoff pfctl_clear_fingerprints(int dev, int opts)
2753b3a8eb9SGleb Smirnoff {
2763b3a8eb9SGleb Smirnoff if (ioctl(dev, DIOCOSFPFLUSH))
2773b3a8eb9SGleb Smirnoff err(1, "DIOCOSFPFLUSH");
2783b3a8eb9SGleb Smirnoff }
2793b3a8eb9SGleb Smirnoff
2803b3a8eb9SGleb Smirnoff /* flush pfctl's view of the fingerprints */
2813b3a8eb9SGleb Smirnoff void
pfctl_flush_my_fingerprints(struct name_list * list)2823b3a8eb9SGleb Smirnoff pfctl_flush_my_fingerprints(struct name_list *list)
2833b3a8eb9SGleb Smirnoff {
2843b3a8eb9SGleb Smirnoff struct name_entry *nm;
2853b3a8eb9SGleb Smirnoff
2863b3a8eb9SGleb Smirnoff while ((nm = LIST_FIRST(list)) != NULL) {
2873b3a8eb9SGleb Smirnoff LIST_REMOVE(nm, nm_entry);
2883b3a8eb9SGleb Smirnoff pfctl_flush_my_fingerprints(&nm->nm_sublist);
2893b3a8eb9SGleb Smirnoff free(nm);
2903b3a8eb9SGleb Smirnoff }
2913b3a8eb9SGleb Smirnoff fingerprint_count = 0;
2923b3a8eb9SGleb Smirnoff class_count = 0;
2933b3a8eb9SGleb Smirnoff }
2943b3a8eb9SGleb Smirnoff
2953b3a8eb9SGleb Smirnoff /* Fetch the active fingerprints from the kernel */
2963b3a8eb9SGleb Smirnoff int
pfctl_load_fingerprints(int dev,int opts)2973b3a8eb9SGleb Smirnoff pfctl_load_fingerprints(int dev, int opts)
2983b3a8eb9SGleb Smirnoff {
2993b3a8eb9SGleb Smirnoff struct pf_osfp_ioctl io;
3003b3a8eb9SGleb Smirnoff int i;
3013b3a8eb9SGleb Smirnoff
3023b3a8eb9SGleb Smirnoff pfctl_flush_my_fingerprints(&classes);
3033b3a8eb9SGleb Smirnoff
3043b3a8eb9SGleb Smirnoff for (i = 0; i >= 0; i++) {
3053b3a8eb9SGleb Smirnoff memset(&io, 0, sizeof(io));
3063b3a8eb9SGleb Smirnoff io.fp_getnum = i;
3073b3a8eb9SGleb Smirnoff if (ioctl(dev, DIOCOSFPGET, &io)) {
3083b3a8eb9SGleb Smirnoff if (errno == EBUSY)
3093b3a8eb9SGleb Smirnoff break;
3103b3a8eb9SGleb Smirnoff warn("DIOCOSFPGET");
3113b3a8eb9SGleb Smirnoff return (1);
3123b3a8eb9SGleb Smirnoff }
3133b3a8eb9SGleb Smirnoff import_fingerprint(&io);
3143b3a8eb9SGleb Smirnoff }
3153b3a8eb9SGleb Smirnoff return (0);
3163b3a8eb9SGleb Smirnoff }
3173b3a8eb9SGleb Smirnoff
3183b3a8eb9SGleb Smirnoff /* List the fingerprints */
3193b3a8eb9SGleb Smirnoff void
pfctl_show_fingerprints(int opts)3203b3a8eb9SGleb Smirnoff pfctl_show_fingerprints(int opts)
3213b3a8eb9SGleb Smirnoff {
3223b3a8eb9SGleb Smirnoff if (LIST_FIRST(&classes) != NULL) {
3233b3a8eb9SGleb Smirnoff if (opts & PF_OPT_SHOWALL) {
3243b3a8eb9SGleb Smirnoff pfctl_print_title("OS FINGERPRINTS:");
3253b3a8eb9SGleb Smirnoff printf("%u fingerprints loaded\n", fingerprint_count);
3263b3a8eb9SGleb Smirnoff } else {
3273b3a8eb9SGleb Smirnoff printf("Class\tVersion\tSubtype(subversion)\n");
3283b3a8eb9SGleb Smirnoff printf("-----\t-------\t-------------------\n");
3293b3a8eb9SGleb Smirnoff sort_name_list(opts, &classes);
3303b3a8eb9SGleb Smirnoff print_name_list(opts, &classes, "");
3313b3a8eb9SGleb Smirnoff }
3323b3a8eb9SGleb Smirnoff }
3333b3a8eb9SGleb Smirnoff }
3343b3a8eb9SGleb Smirnoff
3353b3a8eb9SGleb Smirnoff /* Lookup a fingerprint */
3363b3a8eb9SGleb Smirnoff pf_osfp_t
pfctl_get_fingerprint(const char * name)3373b3a8eb9SGleb Smirnoff pfctl_get_fingerprint(const char *name)
3383b3a8eb9SGleb Smirnoff {
3393b3a8eb9SGleb Smirnoff struct name_entry *nm, *class_nm, *version_nm, *subtype_nm;
3403b3a8eb9SGleb Smirnoff pf_osfp_t ret = PF_OSFP_NOMATCH;
3413b3a8eb9SGleb Smirnoff int class, version, subtype;
3423b3a8eb9SGleb Smirnoff int unp_class, unp_version, unp_subtype;
3433b3a8eb9SGleb Smirnoff int wr_len, version_len, subtype_len;
3443b3a8eb9SGleb Smirnoff char *ptr, *wr_name;
3453b3a8eb9SGleb Smirnoff
3463b3a8eb9SGleb Smirnoff if (strcasecmp(name, "unknown") == 0)
3473b3a8eb9SGleb Smirnoff return (PF_OSFP_UNKNOWN);
3483b3a8eb9SGleb Smirnoff
3493b3a8eb9SGleb Smirnoff /* Try most likely no version and no subtype */
3503b3a8eb9SGleb Smirnoff if ((nm = lookup_name_list(&classes, name))) {
3513b3a8eb9SGleb Smirnoff class = nm->nm_num;
3523b3a8eb9SGleb Smirnoff version = PF_OSFP_ANY;
3533b3a8eb9SGleb Smirnoff subtype = PF_OSFP_ANY;
3543b3a8eb9SGleb Smirnoff goto found;
3553b3a8eb9SGleb Smirnoff } else {
3563b3a8eb9SGleb Smirnoff
3573b3a8eb9SGleb Smirnoff /* Chop it up into class/version/subtype */
3583b3a8eb9SGleb Smirnoff
3593b3a8eb9SGleb Smirnoff if ((wr_name = strdup(name)) == NULL)
3603b3a8eb9SGleb Smirnoff err(1, "malloc");
3613b3a8eb9SGleb Smirnoff if ((ptr = strchr(wr_name, ' ')) == NULL) {
3623b3a8eb9SGleb Smirnoff free(wr_name);
3633b3a8eb9SGleb Smirnoff return (PF_OSFP_NOMATCH);
3643b3a8eb9SGleb Smirnoff }
3653b3a8eb9SGleb Smirnoff *ptr++ = '\0';
3663b3a8eb9SGleb Smirnoff
3673b3a8eb9SGleb Smirnoff /* The class is easy to find since it is delimited by a space */
3683b3a8eb9SGleb Smirnoff if ((class_nm = lookup_name_list(&classes, wr_name)) == NULL) {
3693b3a8eb9SGleb Smirnoff free(wr_name);
3703b3a8eb9SGleb Smirnoff return (PF_OSFP_NOMATCH);
3713b3a8eb9SGleb Smirnoff }
3723b3a8eb9SGleb Smirnoff class = class_nm->nm_num;
3733b3a8eb9SGleb Smirnoff
3743b3a8eb9SGleb Smirnoff /* Try no subtype */
3753b3a8eb9SGleb Smirnoff if ((version_nm = lookup_name_list(&class_nm->nm_sublist, ptr)))
3763b3a8eb9SGleb Smirnoff {
3773b3a8eb9SGleb Smirnoff version = version_nm->nm_num;
3783b3a8eb9SGleb Smirnoff subtype = PF_OSFP_ANY;
3793b3a8eb9SGleb Smirnoff free(wr_name);
3803b3a8eb9SGleb Smirnoff goto found;
3813b3a8eb9SGleb Smirnoff }
3823b3a8eb9SGleb Smirnoff
3833b3a8eb9SGleb Smirnoff
3843b3a8eb9SGleb Smirnoff /*
3853b3a8eb9SGleb Smirnoff * There must be a version and a subtype.
3863b3a8eb9SGleb Smirnoff * We'll do some fuzzy matching to pick up things like:
3873b3a8eb9SGleb Smirnoff * Linux 2.2.14 (version=2.2 subtype=14)
3883b3a8eb9SGleb Smirnoff * FreeBSD 4.0-STABLE (version=4.0 subtype=STABLE)
3893b3a8eb9SGleb Smirnoff * Windows 2000 SP2 (version=2000 subtype=SP2)
3903b3a8eb9SGleb Smirnoff */
3913b3a8eb9SGleb Smirnoff #define CONNECTOR(x) ((x) == '.' || (x) == ' ' || (x) == '\t' || (x) == '-')
3923b3a8eb9SGleb Smirnoff wr_len = strlen(ptr);
3933b3a8eb9SGleb Smirnoff LIST_FOREACH(version_nm, &class_nm->nm_sublist, nm_entry) {
3943b3a8eb9SGleb Smirnoff version_len = strlen(version_nm->nm_name);
3953b3a8eb9SGleb Smirnoff if (wr_len < version_len + 2 ||
3963b3a8eb9SGleb Smirnoff !CONNECTOR(ptr[version_len]))
3973b3a8eb9SGleb Smirnoff continue;
3983b3a8eb9SGleb Smirnoff /* first part of the string must be version */
3993b3a8eb9SGleb Smirnoff if (strncasecmp(ptr, version_nm->nm_name,
4003b3a8eb9SGleb Smirnoff version_len))
4013b3a8eb9SGleb Smirnoff continue;
4023b3a8eb9SGleb Smirnoff
4033b3a8eb9SGleb Smirnoff LIST_FOREACH(subtype_nm, &version_nm->nm_sublist,
4043b3a8eb9SGleb Smirnoff nm_entry) {
4053b3a8eb9SGleb Smirnoff subtype_len = strlen(subtype_nm->nm_name);
4063b3a8eb9SGleb Smirnoff if (wr_len != version_len + subtype_len + 1)
4073b3a8eb9SGleb Smirnoff continue;
4083b3a8eb9SGleb Smirnoff
4093b3a8eb9SGleb Smirnoff /* last part of the string must be subtype */
4103b3a8eb9SGleb Smirnoff if (strcasecmp(&ptr[version_len+1],
4113b3a8eb9SGleb Smirnoff subtype_nm->nm_name) != 0)
4123b3a8eb9SGleb Smirnoff continue;
4133b3a8eb9SGleb Smirnoff
4143b3a8eb9SGleb Smirnoff /* Found it!! */
4153b3a8eb9SGleb Smirnoff version = version_nm->nm_num;
4163b3a8eb9SGleb Smirnoff subtype = subtype_nm->nm_num;
4173b3a8eb9SGleb Smirnoff free(wr_name);
4183b3a8eb9SGleb Smirnoff goto found;
4193b3a8eb9SGleb Smirnoff }
4203b3a8eb9SGleb Smirnoff }
4213b3a8eb9SGleb Smirnoff
4223b3a8eb9SGleb Smirnoff free(wr_name);
4233b3a8eb9SGleb Smirnoff return (PF_OSFP_NOMATCH);
4243b3a8eb9SGleb Smirnoff }
4253b3a8eb9SGleb Smirnoff
4263b3a8eb9SGleb Smirnoff found:
4273b3a8eb9SGleb Smirnoff PF_OSFP_PACK(ret, class, version, subtype);
4283b3a8eb9SGleb Smirnoff if (ret != PF_OSFP_NOMATCH) {
4293b3a8eb9SGleb Smirnoff PF_OSFP_UNPACK(ret, unp_class, unp_version, unp_subtype);
4303b3a8eb9SGleb Smirnoff if (class != unp_class) {
4313b3a8eb9SGleb Smirnoff fprintf(stderr, "warning: fingerprint table overflowed "
4323b3a8eb9SGleb Smirnoff "classes\n");
4333b3a8eb9SGleb Smirnoff return (PF_OSFP_NOMATCH);
4343b3a8eb9SGleb Smirnoff }
4353b3a8eb9SGleb Smirnoff if (version != unp_version) {
4363b3a8eb9SGleb Smirnoff fprintf(stderr, "warning: fingerprint table overflowed "
4373b3a8eb9SGleb Smirnoff "versions\n");
4383b3a8eb9SGleb Smirnoff return (PF_OSFP_NOMATCH);
4393b3a8eb9SGleb Smirnoff }
4403b3a8eb9SGleb Smirnoff if (subtype != unp_subtype) {
4413b3a8eb9SGleb Smirnoff fprintf(stderr, "warning: fingerprint table overflowed "
4423b3a8eb9SGleb Smirnoff "subtypes\n");
4433b3a8eb9SGleb Smirnoff return (PF_OSFP_NOMATCH);
4443b3a8eb9SGleb Smirnoff }
4453b3a8eb9SGleb Smirnoff }
4463b3a8eb9SGleb Smirnoff if (ret == PF_OSFP_ANY) {
4473b3a8eb9SGleb Smirnoff /* should never happen */
4483b3a8eb9SGleb Smirnoff fprintf(stderr, "warning: fingerprint packed to 'any'\n");
4493b3a8eb9SGleb Smirnoff return (PF_OSFP_NOMATCH);
4503b3a8eb9SGleb Smirnoff }
4513b3a8eb9SGleb Smirnoff
4523b3a8eb9SGleb Smirnoff return (ret);
4533b3a8eb9SGleb Smirnoff }
4543b3a8eb9SGleb Smirnoff
4553b3a8eb9SGleb Smirnoff /* Lookup a fingerprint name by ID */
4563b3a8eb9SGleb Smirnoff char *
pfctl_lookup_fingerprint(pf_osfp_t fp,char * buf,size_t len)4573b3a8eb9SGleb Smirnoff pfctl_lookup_fingerprint(pf_osfp_t fp, char *buf, size_t len)
4583b3a8eb9SGleb Smirnoff {
4593b3a8eb9SGleb Smirnoff int class, version, subtype;
4603b3a8eb9SGleb Smirnoff struct name_list *list;
4613b3a8eb9SGleb Smirnoff struct name_entry *nm;
4623b3a8eb9SGleb Smirnoff
4633b3a8eb9SGleb Smirnoff char *class_name, *version_name, *subtype_name;
4643b3a8eb9SGleb Smirnoff class_name = version_name = subtype_name = NULL;
4653b3a8eb9SGleb Smirnoff
4663b3a8eb9SGleb Smirnoff if (fp == PF_OSFP_UNKNOWN) {
4673b3a8eb9SGleb Smirnoff strlcpy(buf, "unknown", len);
4683b3a8eb9SGleb Smirnoff return (buf);
4693b3a8eb9SGleb Smirnoff }
4703b3a8eb9SGleb Smirnoff if (fp == PF_OSFP_ANY) {
4713b3a8eb9SGleb Smirnoff strlcpy(buf, "any", len);
4723b3a8eb9SGleb Smirnoff return (buf);
4733b3a8eb9SGleb Smirnoff }
4743b3a8eb9SGleb Smirnoff
4753b3a8eb9SGleb Smirnoff PF_OSFP_UNPACK(fp, class, version, subtype);
4763b3a8eb9SGleb Smirnoff if (class >= (1 << _FP_CLASS_BITS) ||
4773b3a8eb9SGleb Smirnoff version >= (1 << _FP_VERSION_BITS) ||
4783b3a8eb9SGleb Smirnoff subtype >= (1 << _FP_SUBTYPE_BITS)) {
4793b3a8eb9SGleb Smirnoff warnx("PF_OSFP_UNPACK(0x%x) failed!!", fp);
4803b3a8eb9SGleb Smirnoff strlcpy(buf, "nomatch", len);
4813b3a8eb9SGleb Smirnoff return (buf);
4823b3a8eb9SGleb Smirnoff }
4833b3a8eb9SGleb Smirnoff
4843b3a8eb9SGleb Smirnoff LIST_FOREACH(nm, &classes, nm_entry) {
4853b3a8eb9SGleb Smirnoff if (nm->nm_num == class) {
4863b3a8eb9SGleb Smirnoff class_name = nm->nm_name;
4873b3a8eb9SGleb Smirnoff if (version == PF_OSFP_ANY)
4883b3a8eb9SGleb Smirnoff goto found;
4893b3a8eb9SGleb Smirnoff list = &nm->nm_sublist;
4903b3a8eb9SGleb Smirnoff LIST_FOREACH(nm, list, nm_entry) {
4913b3a8eb9SGleb Smirnoff if (nm->nm_num == version) {
4923b3a8eb9SGleb Smirnoff version_name = nm->nm_name;
4933b3a8eb9SGleb Smirnoff if (subtype == PF_OSFP_ANY)
4943b3a8eb9SGleb Smirnoff goto found;
4953b3a8eb9SGleb Smirnoff list = &nm->nm_sublist;
4963b3a8eb9SGleb Smirnoff LIST_FOREACH(nm, list, nm_entry) {
4973b3a8eb9SGleb Smirnoff if (nm->nm_num == subtype) {
4983b3a8eb9SGleb Smirnoff subtype_name =
4993b3a8eb9SGleb Smirnoff nm->nm_name;
5003b3a8eb9SGleb Smirnoff goto found;
5013b3a8eb9SGleb Smirnoff }
5023b3a8eb9SGleb Smirnoff } /* foreach subtype */
5033b3a8eb9SGleb Smirnoff strlcpy(buf, "nomatch", len);
5043b3a8eb9SGleb Smirnoff return (buf);
5053b3a8eb9SGleb Smirnoff }
5063b3a8eb9SGleb Smirnoff } /* foreach version */
5073b3a8eb9SGleb Smirnoff strlcpy(buf, "nomatch", len);
5083b3a8eb9SGleb Smirnoff return (buf);
5093b3a8eb9SGleb Smirnoff }
5103b3a8eb9SGleb Smirnoff } /* foreach class */
5113b3a8eb9SGleb Smirnoff
5123b3a8eb9SGleb Smirnoff strlcpy(buf, "nomatch", len);
5133b3a8eb9SGleb Smirnoff return (buf);
5143b3a8eb9SGleb Smirnoff
5153b3a8eb9SGleb Smirnoff found:
5163b3a8eb9SGleb Smirnoff snprintf(buf, len, "%s", class_name);
5173b3a8eb9SGleb Smirnoff if (version_name) {
5183b3a8eb9SGleb Smirnoff strlcat(buf, " ", len);
5193b3a8eb9SGleb Smirnoff strlcat(buf, version_name, len);
5203b3a8eb9SGleb Smirnoff if (subtype_name) {
5213b3a8eb9SGleb Smirnoff if (strchr(version_name, ' '))
5223b3a8eb9SGleb Smirnoff strlcat(buf, " ", len);
5233b3a8eb9SGleb Smirnoff else if (strchr(version_name, '.') &&
5243b3a8eb9SGleb Smirnoff isdigit(*subtype_name))
5253b3a8eb9SGleb Smirnoff strlcat(buf, ".", len);
5263b3a8eb9SGleb Smirnoff else
5273b3a8eb9SGleb Smirnoff strlcat(buf, " ", len);
5283b3a8eb9SGleb Smirnoff strlcat(buf, subtype_name, len);
5293b3a8eb9SGleb Smirnoff }
5303b3a8eb9SGleb Smirnoff }
5313b3a8eb9SGleb Smirnoff return (buf);
5323b3a8eb9SGleb Smirnoff }
5333b3a8eb9SGleb Smirnoff
5343b3a8eb9SGleb Smirnoff /* lookup a name in a list */
5353b3a8eb9SGleb Smirnoff struct name_entry *
lookup_name_list(struct name_list * list,const char * name)5363b3a8eb9SGleb Smirnoff lookup_name_list(struct name_list *list, const char *name)
5373b3a8eb9SGleb Smirnoff {
5383b3a8eb9SGleb Smirnoff struct name_entry *nm;
5393b3a8eb9SGleb Smirnoff LIST_FOREACH(nm, list, nm_entry)
5403b3a8eb9SGleb Smirnoff if (strcasecmp(name, nm->nm_name) == 0)
5413b3a8eb9SGleb Smirnoff return (nm);
5423b3a8eb9SGleb Smirnoff
5433b3a8eb9SGleb Smirnoff return (NULL);
5443b3a8eb9SGleb Smirnoff }
5453b3a8eb9SGleb Smirnoff
5463b3a8eb9SGleb Smirnoff
5473b3a8eb9SGleb Smirnoff void
add_fingerprint(int dev,int opts,struct pf_osfp_ioctl * fp)5483b3a8eb9SGleb Smirnoff add_fingerprint(int dev, int opts, struct pf_osfp_ioctl *fp)
5493b3a8eb9SGleb Smirnoff {
5503b3a8eb9SGleb Smirnoff struct pf_osfp_ioctl fptmp;
5513b3a8eb9SGleb Smirnoff struct name_entry *nm_class, *nm_version, *nm_subtype;
5523b3a8eb9SGleb Smirnoff int class, version, subtype;
5533b3a8eb9SGleb Smirnoff
5543b3a8eb9SGleb Smirnoff /* We expand #-# or #.#-#.# version/subtypes into multiple fingerprints */
5553b3a8eb9SGleb Smirnoff #define EXPAND(field) do { \
5563b3a8eb9SGleb Smirnoff int _dot = -1, _start = -1, _end = -1, _i = 0; \
5573b3a8eb9SGleb Smirnoff /* pick major version out of #.# */ \
5583b3a8eb9SGleb Smirnoff if (isdigit(fp->field[_i]) && fp->field[_i+1] == '.') { \
5593b3a8eb9SGleb Smirnoff _dot = fp->field[_i] - '0'; \
5603b3a8eb9SGleb Smirnoff _i += 2; \
5613b3a8eb9SGleb Smirnoff } \
5623b3a8eb9SGleb Smirnoff if (isdigit(fp->field[_i])) \
5633b3a8eb9SGleb Smirnoff _start = fp->field[_i++] - '0'; \
5643b3a8eb9SGleb Smirnoff else \
5653b3a8eb9SGleb Smirnoff break; \
5663b3a8eb9SGleb Smirnoff if (isdigit(fp->field[_i])) \
5673b3a8eb9SGleb Smirnoff _start = (_start * 10) + fp->field[_i++] - '0'; \
5683b3a8eb9SGleb Smirnoff if (fp->field[_i++] != '-') \
5693b3a8eb9SGleb Smirnoff break; \
5703b3a8eb9SGleb Smirnoff if (isdigit(fp->field[_i]) && fp->field[_i+1] == '.' && \
5713b3a8eb9SGleb Smirnoff fp->field[_i] - '0' == _dot) \
5723b3a8eb9SGleb Smirnoff _i += 2; \
5733b3a8eb9SGleb Smirnoff else if (_dot != -1) \
5743b3a8eb9SGleb Smirnoff break; \
5753b3a8eb9SGleb Smirnoff if (isdigit(fp->field[_i])) \
5763b3a8eb9SGleb Smirnoff _end = fp->field[_i++] - '0'; \
5773b3a8eb9SGleb Smirnoff else \
5783b3a8eb9SGleb Smirnoff break; \
5793b3a8eb9SGleb Smirnoff if (isdigit(fp->field[_i])) \
5803b3a8eb9SGleb Smirnoff _end = (_end * 10) + fp->field[_i++] - '0'; \
5813b3a8eb9SGleb Smirnoff if (isdigit(fp->field[_i])) \
5823b3a8eb9SGleb Smirnoff _end = (_end * 10) + fp->field[_i++] - '0'; \
5833b3a8eb9SGleb Smirnoff if (fp->field[_i] != '\0') \
5843b3a8eb9SGleb Smirnoff break; \
5853b3a8eb9SGleb Smirnoff memcpy(&fptmp, fp, sizeof(fptmp)); \
5863b3a8eb9SGleb Smirnoff for (;_start <= _end; _start++) { \
5873b3a8eb9SGleb Smirnoff memset(fptmp.field, 0, sizeof(fptmp.field)); \
5883b3a8eb9SGleb Smirnoff fptmp.fp_os.fp_enflags |= PF_OSFP_EXPANDED; \
5893b3a8eb9SGleb Smirnoff if (_dot == -1) \
5903b3a8eb9SGleb Smirnoff snprintf(fptmp.field, sizeof(fptmp.field), \
5913b3a8eb9SGleb Smirnoff "%d", _start); \
5923b3a8eb9SGleb Smirnoff else \
5933b3a8eb9SGleb Smirnoff snprintf(fptmp.field, sizeof(fptmp.field), \
5943b3a8eb9SGleb Smirnoff "%d.%d", _dot, _start); \
5953b3a8eb9SGleb Smirnoff add_fingerprint(dev, opts, &fptmp); \
5963b3a8eb9SGleb Smirnoff } \
5973b3a8eb9SGleb Smirnoff } while(0)
5983b3a8eb9SGleb Smirnoff
5993b3a8eb9SGleb Smirnoff /* We allow "#-#" as a version or subtype and we'll expand it */
6003b3a8eb9SGleb Smirnoff EXPAND(fp_os.fp_version_nm);
6013b3a8eb9SGleb Smirnoff EXPAND(fp_os.fp_subtype_nm);
6023b3a8eb9SGleb Smirnoff
6033b3a8eb9SGleb Smirnoff if (strcasecmp(fp->fp_os.fp_class_nm, "nomatch") == 0)
6043b3a8eb9SGleb Smirnoff errx(1, "fingerprint class \"nomatch\" is reserved");
6053b3a8eb9SGleb Smirnoff
6063b3a8eb9SGleb Smirnoff version = PF_OSFP_ANY;
6073b3a8eb9SGleb Smirnoff subtype = PF_OSFP_ANY;
6083b3a8eb9SGleb Smirnoff
6093b3a8eb9SGleb Smirnoff nm_class = fingerprint_name_entry(&classes, fp->fp_os.fp_class_nm);
6103b3a8eb9SGleb Smirnoff if (nm_class->nm_num == 0)
6113b3a8eb9SGleb Smirnoff nm_class->nm_num = ++class_count;
6123b3a8eb9SGleb Smirnoff class = nm_class->nm_num;
6133b3a8eb9SGleb Smirnoff
6143b3a8eb9SGleb Smirnoff nm_version = fingerprint_name_entry(&nm_class->nm_sublist,
6153b3a8eb9SGleb Smirnoff fp->fp_os.fp_version_nm);
6163b3a8eb9SGleb Smirnoff if (nm_version) {
6173b3a8eb9SGleb Smirnoff if (nm_version->nm_num == 0)
6183b3a8eb9SGleb Smirnoff nm_version->nm_num = ++nm_class->nm_sublist_num;
6193b3a8eb9SGleb Smirnoff version = nm_version->nm_num;
6203b3a8eb9SGleb Smirnoff nm_subtype = fingerprint_name_entry(&nm_version->nm_sublist,
6213b3a8eb9SGleb Smirnoff fp->fp_os.fp_subtype_nm);
6223b3a8eb9SGleb Smirnoff if (nm_subtype) {
6233b3a8eb9SGleb Smirnoff if (nm_subtype->nm_num == 0)
6243b3a8eb9SGleb Smirnoff nm_subtype->nm_num =
6253b3a8eb9SGleb Smirnoff ++nm_version->nm_sublist_num;
6263b3a8eb9SGleb Smirnoff subtype = nm_subtype->nm_num;
6273b3a8eb9SGleb Smirnoff }
6283b3a8eb9SGleb Smirnoff }
6293b3a8eb9SGleb Smirnoff
6303b3a8eb9SGleb Smirnoff
6313b3a8eb9SGleb Smirnoff DEBUG(fp, "\tsignature %d:%d:%d %s", class, version, subtype,
6323b3a8eb9SGleb Smirnoff print_ioctl(fp));
6333b3a8eb9SGleb Smirnoff
6343b3a8eb9SGleb Smirnoff PF_OSFP_PACK(fp->fp_os.fp_os, class, version, subtype);
6353b3a8eb9SGleb Smirnoff fingerprint_count++;
6363b3a8eb9SGleb Smirnoff
6373b3a8eb9SGleb Smirnoff #ifdef FAKE_PF_KERNEL
6383b3a8eb9SGleb Smirnoff /* Linked to the sys/net/pf_osfp.c. Call pf_osfp_add() */
6393b3a8eb9SGleb Smirnoff if ((errno = pf_osfp_add(fp)))
6403b3a8eb9SGleb Smirnoff #else
6413b3a8eb9SGleb Smirnoff if ((opts & PF_OPT_NOACTION) == 0 && ioctl(dev, DIOCOSFPADD, fp))
6423b3a8eb9SGleb Smirnoff #endif /* FAKE_PF_KERNEL */
6433b3a8eb9SGleb Smirnoff {
6443b3a8eb9SGleb Smirnoff if (errno == EEXIST) {
6453b3a8eb9SGleb Smirnoff warn("Duplicate signature for %s %s %s",
6463b3a8eb9SGleb Smirnoff fp->fp_os.fp_class_nm,
6473b3a8eb9SGleb Smirnoff fp->fp_os.fp_version_nm,
6483b3a8eb9SGleb Smirnoff fp->fp_os.fp_subtype_nm);
6493b3a8eb9SGleb Smirnoff
6503b3a8eb9SGleb Smirnoff } else {
6513b3a8eb9SGleb Smirnoff err(1, "DIOCOSFPADD");
6523b3a8eb9SGleb Smirnoff }
6533b3a8eb9SGleb Smirnoff }
6543b3a8eb9SGleb Smirnoff }
6553b3a8eb9SGleb Smirnoff
6563b3a8eb9SGleb Smirnoff /* import a fingerprint from the kernel */
6573b3a8eb9SGleb Smirnoff void
import_fingerprint(struct pf_osfp_ioctl * fp)6583b3a8eb9SGleb Smirnoff import_fingerprint(struct pf_osfp_ioctl *fp)
6593b3a8eb9SGleb Smirnoff {
6603b3a8eb9SGleb Smirnoff struct name_entry *nm_class, *nm_version, *nm_subtype;
6613b3a8eb9SGleb Smirnoff int class, version, subtype;
6623b3a8eb9SGleb Smirnoff
6633b3a8eb9SGleb Smirnoff PF_OSFP_UNPACK(fp->fp_os.fp_os, class, version, subtype);
6643b3a8eb9SGleb Smirnoff
6653b3a8eb9SGleb Smirnoff nm_class = fingerprint_name_entry(&classes, fp->fp_os.fp_class_nm);
6663b3a8eb9SGleb Smirnoff if (nm_class->nm_num == 0) {
6673b3a8eb9SGleb Smirnoff nm_class->nm_num = class;
6683b3a8eb9SGleb Smirnoff class_count = MAX(class_count, class);
6693b3a8eb9SGleb Smirnoff }
6703b3a8eb9SGleb Smirnoff
6713b3a8eb9SGleb Smirnoff nm_version = fingerprint_name_entry(&nm_class->nm_sublist,
6723b3a8eb9SGleb Smirnoff fp->fp_os.fp_version_nm);
6733b3a8eb9SGleb Smirnoff if (nm_version) {
6743b3a8eb9SGleb Smirnoff if (nm_version->nm_num == 0) {
6753b3a8eb9SGleb Smirnoff nm_version->nm_num = version;
6763b3a8eb9SGleb Smirnoff nm_class->nm_sublist_num = MAX(nm_class->nm_sublist_num,
6773b3a8eb9SGleb Smirnoff version);
6783b3a8eb9SGleb Smirnoff }
6793b3a8eb9SGleb Smirnoff nm_subtype = fingerprint_name_entry(&nm_version->nm_sublist,
6803b3a8eb9SGleb Smirnoff fp->fp_os.fp_subtype_nm);
6813b3a8eb9SGleb Smirnoff if (nm_subtype) {
6823b3a8eb9SGleb Smirnoff if (nm_subtype->nm_num == 0) {
6833b3a8eb9SGleb Smirnoff nm_subtype->nm_num = subtype;
6843b3a8eb9SGleb Smirnoff nm_version->nm_sublist_num =
6853b3a8eb9SGleb Smirnoff MAX(nm_version->nm_sublist_num, subtype);
6863b3a8eb9SGleb Smirnoff }
6873b3a8eb9SGleb Smirnoff }
6883b3a8eb9SGleb Smirnoff }
6893b3a8eb9SGleb Smirnoff
6903b3a8eb9SGleb Smirnoff
6913b3a8eb9SGleb Smirnoff fingerprint_count++;
6923b3a8eb9SGleb Smirnoff DEBUG(fp, "import signature %d:%d:%d", class, version, subtype);
6933b3a8eb9SGleb Smirnoff }
6943b3a8eb9SGleb Smirnoff
6953b3a8eb9SGleb Smirnoff /* Find an entry for a fingerprints class/version/subtype */
6963b3a8eb9SGleb Smirnoff struct name_entry *
fingerprint_name_entry(struct name_list * list,char * name)6973b3a8eb9SGleb Smirnoff fingerprint_name_entry(struct name_list *list, char *name)
6983b3a8eb9SGleb Smirnoff {
6993b3a8eb9SGleb Smirnoff struct name_entry *nm_entry;
7003b3a8eb9SGleb Smirnoff
7013b3a8eb9SGleb Smirnoff if (name == NULL || strlen(name) == 0)
7023b3a8eb9SGleb Smirnoff return (NULL);
7033b3a8eb9SGleb Smirnoff
7043b3a8eb9SGleb Smirnoff LIST_FOREACH(nm_entry, list, nm_entry) {
7053b3a8eb9SGleb Smirnoff if (strcasecmp(nm_entry->nm_name, name) == 0) {
7063b3a8eb9SGleb Smirnoff /* We'll move this to the front of the list later */
7073b3a8eb9SGleb Smirnoff LIST_REMOVE(nm_entry, nm_entry);
7083b3a8eb9SGleb Smirnoff break;
7093b3a8eb9SGleb Smirnoff }
7103b3a8eb9SGleb Smirnoff }
7113b3a8eb9SGleb Smirnoff if (nm_entry == NULL) {
7123b3a8eb9SGleb Smirnoff nm_entry = calloc(1, sizeof(*nm_entry));
7133b3a8eb9SGleb Smirnoff if (nm_entry == NULL)
7143b3a8eb9SGleb Smirnoff err(1, "calloc");
7153b3a8eb9SGleb Smirnoff LIST_INIT(&nm_entry->nm_sublist);
7163b3a8eb9SGleb Smirnoff strlcpy(nm_entry->nm_name, name, sizeof(nm_entry->nm_name));
7173b3a8eb9SGleb Smirnoff }
7183b3a8eb9SGleb Smirnoff LIST_INSERT_HEAD(list, nm_entry, nm_entry);
7193b3a8eb9SGleb Smirnoff return (nm_entry);
7203b3a8eb9SGleb Smirnoff }
7213b3a8eb9SGleb Smirnoff
7223b3a8eb9SGleb Smirnoff
7233b3a8eb9SGleb Smirnoff void
print_name_list(int opts,struct name_list * nml,const char * prefix)7243b3a8eb9SGleb Smirnoff print_name_list(int opts, struct name_list *nml, const char *prefix)
7253b3a8eb9SGleb Smirnoff {
7263b3a8eb9SGleb Smirnoff char newprefix[32];
7273b3a8eb9SGleb Smirnoff struct name_entry *nm;
7283b3a8eb9SGleb Smirnoff
7293b3a8eb9SGleb Smirnoff LIST_FOREACH(nm, nml, nm_entry) {
7303b3a8eb9SGleb Smirnoff snprintf(newprefix, sizeof(newprefix), "%s%s\t", prefix,
7313b3a8eb9SGleb Smirnoff nm->nm_name);
7323b3a8eb9SGleb Smirnoff printf("%s\n", newprefix);
7333b3a8eb9SGleb Smirnoff print_name_list(opts, &nm->nm_sublist, newprefix);
7343b3a8eb9SGleb Smirnoff }
7353b3a8eb9SGleb Smirnoff }
7363b3a8eb9SGleb Smirnoff
7373b3a8eb9SGleb Smirnoff void
sort_name_list(int opts,struct name_list * nml)7383b3a8eb9SGleb Smirnoff sort_name_list(int opts, struct name_list *nml)
7393b3a8eb9SGleb Smirnoff {
7403b3a8eb9SGleb Smirnoff struct name_list new;
7413b3a8eb9SGleb Smirnoff struct name_entry *nm, *nmsearch, *nmlast;
7423b3a8eb9SGleb Smirnoff
7433b3a8eb9SGleb Smirnoff /* yes yes, it's a very slow sort. so sue me */
7443b3a8eb9SGleb Smirnoff
7453b3a8eb9SGleb Smirnoff LIST_INIT(&new);
7463b3a8eb9SGleb Smirnoff
7473b3a8eb9SGleb Smirnoff while ((nm = LIST_FIRST(nml)) != NULL) {
7483b3a8eb9SGleb Smirnoff LIST_REMOVE(nm, nm_entry);
7493b3a8eb9SGleb Smirnoff nmlast = NULL;
7503b3a8eb9SGleb Smirnoff LIST_FOREACH(nmsearch, &new, nm_entry) {
7513b3a8eb9SGleb Smirnoff if (strcasecmp(nmsearch->nm_name, nm->nm_name) > 0) {
7523b3a8eb9SGleb Smirnoff LIST_INSERT_BEFORE(nmsearch, nm, nm_entry);
7533b3a8eb9SGleb Smirnoff break;
7543b3a8eb9SGleb Smirnoff }
7553b3a8eb9SGleb Smirnoff nmlast = nmsearch;
7563b3a8eb9SGleb Smirnoff }
7573b3a8eb9SGleb Smirnoff if (nmsearch == NULL) {
7583b3a8eb9SGleb Smirnoff if (nmlast)
7593b3a8eb9SGleb Smirnoff LIST_INSERT_AFTER(nmlast, nm, nm_entry);
7603b3a8eb9SGleb Smirnoff else
7613b3a8eb9SGleb Smirnoff LIST_INSERT_HEAD(&new, nm, nm_entry);
7623b3a8eb9SGleb Smirnoff }
7633b3a8eb9SGleb Smirnoff
7643b3a8eb9SGleb Smirnoff sort_name_list(opts, &nm->nm_sublist);
7653b3a8eb9SGleb Smirnoff }
7663b3a8eb9SGleb Smirnoff nmlast = NULL;
7673b3a8eb9SGleb Smirnoff while ((nm = LIST_FIRST(&new)) != NULL) {
7683b3a8eb9SGleb Smirnoff LIST_REMOVE(nm, nm_entry);
7693b3a8eb9SGleb Smirnoff if (nmlast == NULL)
7703b3a8eb9SGleb Smirnoff LIST_INSERT_HEAD(nml, nm, nm_entry);
7713b3a8eb9SGleb Smirnoff else
7723b3a8eb9SGleb Smirnoff LIST_INSERT_AFTER(nmlast, nm, nm_entry);
7733b3a8eb9SGleb Smirnoff nmlast = nm;
7743b3a8eb9SGleb Smirnoff }
7753b3a8eb9SGleb Smirnoff }
7763b3a8eb9SGleb Smirnoff
7773b3a8eb9SGleb Smirnoff /* parse the next integer in a formatted config file line */
7783b3a8eb9SGleb Smirnoff int
get_int(char ** line,size_t * len,int * var,int * mod,const char * name,int flags,int max,const char * filename,int lineno)7793b3a8eb9SGleb Smirnoff get_int(char **line, size_t *len, int *var, int *mod,
7803b3a8eb9SGleb Smirnoff const char *name, int flags, int max, const char *filename, int lineno)
7813b3a8eb9SGleb Smirnoff {
7823b3a8eb9SGleb Smirnoff int fieldlen, i;
7833b3a8eb9SGleb Smirnoff char *field;
7843b3a8eb9SGleb Smirnoff long val = 0;
7853b3a8eb9SGleb Smirnoff
7863b3a8eb9SGleb Smirnoff if (mod)
7873b3a8eb9SGleb Smirnoff *mod = 0;
7883b3a8eb9SGleb Smirnoff *var = 0;
7893b3a8eb9SGleb Smirnoff
7903b3a8eb9SGleb Smirnoff field = get_field(line, len, &fieldlen);
7913b3a8eb9SGleb Smirnoff if (field == NULL)
7923b3a8eb9SGleb Smirnoff return (1);
7933b3a8eb9SGleb Smirnoff if (fieldlen == 0) {
7943b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d empty %s\n", filename, lineno, name);
7953b3a8eb9SGleb Smirnoff return (1);
7963b3a8eb9SGleb Smirnoff }
7973b3a8eb9SGleb Smirnoff
7983b3a8eb9SGleb Smirnoff i = 0;
7993b3a8eb9SGleb Smirnoff if ((*field == '%' || *field == 'S' || *field == 'T' || *field == '*')
8003b3a8eb9SGleb Smirnoff && fieldlen >= 1) {
8013b3a8eb9SGleb Smirnoff switch (*field) {
8023b3a8eb9SGleb Smirnoff case 'S':
8033b3a8eb9SGleb Smirnoff if (mod && (flags & T_MSS))
8043b3a8eb9SGleb Smirnoff *mod = T_MSS;
8053b3a8eb9SGleb Smirnoff if (fieldlen == 1)
8063b3a8eb9SGleb Smirnoff return (0);
8073b3a8eb9SGleb Smirnoff break;
8083b3a8eb9SGleb Smirnoff case 'T':
8093b3a8eb9SGleb Smirnoff if (mod && (flags & T_MTU))
8103b3a8eb9SGleb Smirnoff *mod = T_MTU;
8113b3a8eb9SGleb Smirnoff if (fieldlen == 1)
8123b3a8eb9SGleb Smirnoff return (0);
8133b3a8eb9SGleb Smirnoff break;
8143b3a8eb9SGleb Smirnoff case '*':
8153b3a8eb9SGleb Smirnoff if (fieldlen != 1) {
8163b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d long '%c' %s\n",
8173b3a8eb9SGleb Smirnoff filename, lineno, *field, name);
8183b3a8eb9SGleb Smirnoff return (1);
8193b3a8eb9SGleb Smirnoff }
8203b3a8eb9SGleb Smirnoff if (mod && (flags & T_DC)) {
8213b3a8eb9SGleb Smirnoff *mod = T_DC;
8223b3a8eb9SGleb Smirnoff return (0);
8233b3a8eb9SGleb Smirnoff }
8243b3a8eb9SGleb Smirnoff case '%':
8253b3a8eb9SGleb Smirnoff if (mod && (flags & T_MOD))
8263b3a8eb9SGleb Smirnoff *mod = T_MOD;
8273b3a8eb9SGleb Smirnoff if (fieldlen == 1) {
8283b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d modulus %s must have a "
8293b3a8eb9SGleb Smirnoff "value\n", filename, lineno, name);
8303b3a8eb9SGleb Smirnoff return (1);
8313b3a8eb9SGleb Smirnoff }
8323b3a8eb9SGleb Smirnoff break;
8333b3a8eb9SGleb Smirnoff }
8343b3a8eb9SGleb Smirnoff if (mod == NULL || *mod == 0) {
8353b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d does not allow %c' %s\n",
8363b3a8eb9SGleb Smirnoff filename, lineno, *field, name);
8373b3a8eb9SGleb Smirnoff return (1);
8383b3a8eb9SGleb Smirnoff }
8393b3a8eb9SGleb Smirnoff i++;
8403b3a8eb9SGleb Smirnoff }
8413b3a8eb9SGleb Smirnoff
8423b3a8eb9SGleb Smirnoff for (; i < fieldlen; i++) {
8433b3a8eb9SGleb Smirnoff if (field[i] < '0' || field[i] > '9') {
8443b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d non-digit character in %s\n",
8453b3a8eb9SGleb Smirnoff filename, lineno, name);
8463b3a8eb9SGleb Smirnoff return (1);
8473b3a8eb9SGleb Smirnoff }
8483b3a8eb9SGleb Smirnoff val = val * 10 + field[i] - '0';
8493b3a8eb9SGleb Smirnoff if (val < 0) {
8503b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d %s overflowed\n", filename,
8513b3a8eb9SGleb Smirnoff lineno, name);
8523b3a8eb9SGleb Smirnoff return (1);
8533b3a8eb9SGleb Smirnoff }
8543b3a8eb9SGleb Smirnoff }
8553b3a8eb9SGleb Smirnoff
8563b3a8eb9SGleb Smirnoff if (val > max) {
8573b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d %s value %ld > %d\n", filename, lineno,
8583b3a8eb9SGleb Smirnoff name, val, max);
8593b3a8eb9SGleb Smirnoff return (1);
8603b3a8eb9SGleb Smirnoff }
8613b3a8eb9SGleb Smirnoff *var = (int)val;
8623b3a8eb9SGleb Smirnoff
8633b3a8eb9SGleb Smirnoff return (0);
8643b3a8eb9SGleb Smirnoff }
8653b3a8eb9SGleb Smirnoff
8663b3a8eb9SGleb Smirnoff /* parse the next string in a formatted config file line */
8673b3a8eb9SGleb Smirnoff int
get_str(char ** line,size_t * len,char ** v,const char * name,int minlen,const char * filename,int lineno)8683b3a8eb9SGleb Smirnoff get_str(char **line, size_t *len, char **v, const char *name, int minlen,
8693b3a8eb9SGleb Smirnoff const char *filename, int lineno)
8703b3a8eb9SGleb Smirnoff {
8713b3a8eb9SGleb Smirnoff int fieldlen;
8723b3a8eb9SGleb Smirnoff char *ptr;
8733b3a8eb9SGleb Smirnoff
8743b3a8eb9SGleb Smirnoff ptr = get_field(line, len, &fieldlen);
8753b3a8eb9SGleb Smirnoff if (ptr == NULL)
8763b3a8eb9SGleb Smirnoff return (1);
8773b3a8eb9SGleb Smirnoff if (fieldlen < minlen) {
8783b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d too short %s\n", filename, lineno, name);
8793b3a8eb9SGleb Smirnoff return (1);
8803b3a8eb9SGleb Smirnoff }
8813b3a8eb9SGleb Smirnoff if ((*v = malloc(fieldlen + 1)) == NULL) {
8823b3a8eb9SGleb Smirnoff perror("malloc()");
8833b3a8eb9SGleb Smirnoff return (1);
8843b3a8eb9SGleb Smirnoff }
8853b3a8eb9SGleb Smirnoff memcpy(*v, ptr, fieldlen);
8863b3a8eb9SGleb Smirnoff (*v)[fieldlen] = '\0';
8873b3a8eb9SGleb Smirnoff
8883b3a8eb9SGleb Smirnoff return (0);
8893b3a8eb9SGleb Smirnoff }
8903b3a8eb9SGleb Smirnoff
8913b3a8eb9SGleb Smirnoff /* Parse out the TCP opts */
8923b3a8eb9SGleb Smirnoff int
get_tcpopts(const char * filename,int lineno,const char * tcpopts,pf_tcpopts_t * packed,int * optcnt,int * mss,int * mss_mod,int * wscale,int * wscale_mod,int * ts0)8933b3a8eb9SGleb Smirnoff get_tcpopts(const char *filename, int lineno, const char *tcpopts,
8943b3a8eb9SGleb Smirnoff pf_tcpopts_t *packed, int *optcnt, int *mss, int *mss_mod, int *wscale,
8953b3a8eb9SGleb Smirnoff int *wscale_mod, int *ts0)
8963b3a8eb9SGleb Smirnoff {
8973b3a8eb9SGleb Smirnoff int i, opt;
8983b3a8eb9SGleb Smirnoff
8993b3a8eb9SGleb Smirnoff *packed = 0;
9003b3a8eb9SGleb Smirnoff *optcnt = 0;
9013b3a8eb9SGleb Smirnoff *wscale = 0;
9023b3a8eb9SGleb Smirnoff *wscale_mod = T_DC;
9033b3a8eb9SGleb Smirnoff *mss = 0;
9043b3a8eb9SGleb Smirnoff *mss_mod = T_DC;
9053b3a8eb9SGleb Smirnoff *ts0 = 0;
9063b3a8eb9SGleb Smirnoff if (strcmp(tcpopts, ".") == 0)
9073b3a8eb9SGleb Smirnoff return (0);
9083b3a8eb9SGleb Smirnoff
9093b3a8eb9SGleb Smirnoff for (i = 0; tcpopts[i] && *optcnt < PF_OSFP_MAX_OPTS;) {
9103b3a8eb9SGleb Smirnoff switch ((opt = toupper(tcpopts[i++]))) {
9113b3a8eb9SGleb Smirnoff case 'N': /* FALLTHROUGH */
9123b3a8eb9SGleb Smirnoff case 'S':
9133b3a8eb9SGleb Smirnoff *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
9143b3a8eb9SGleb Smirnoff (opt == 'N' ? PF_OSFP_TCPOPT_NOP :
9153b3a8eb9SGleb Smirnoff PF_OSFP_TCPOPT_SACK);
9163b3a8eb9SGleb Smirnoff break;
9173b3a8eb9SGleb Smirnoff case 'W': /* FALLTHROUGH */
9183b3a8eb9SGleb Smirnoff case 'M': {
9193b3a8eb9SGleb Smirnoff int *this_mod, *this;
9203b3a8eb9SGleb Smirnoff
9213b3a8eb9SGleb Smirnoff if (opt == 'W') {
9223b3a8eb9SGleb Smirnoff this = wscale;
9233b3a8eb9SGleb Smirnoff this_mod = wscale_mod;
9243b3a8eb9SGleb Smirnoff } else {
9253b3a8eb9SGleb Smirnoff this = mss;
9263b3a8eb9SGleb Smirnoff this_mod = mss_mod;
9273b3a8eb9SGleb Smirnoff }
9283b3a8eb9SGleb Smirnoff *this = 0;
9293b3a8eb9SGleb Smirnoff *this_mod = 0;
9303b3a8eb9SGleb Smirnoff
9313b3a8eb9SGleb Smirnoff *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
9323b3a8eb9SGleb Smirnoff (opt == 'W' ? PF_OSFP_TCPOPT_WSCALE :
9333b3a8eb9SGleb Smirnoff PF_OSFP_TCPOPT_MSS);
9343b3a8eb9SGleb Smirnoff if (tcpopts[i] == '*' && (tcpopts[i + 1] == '\0' ||
9353b3a8eb9SGleb Smirnoff tcpopts[i + 1] == ',')) {
9363b3a8eb9SGleb Smirnoff *this_mod = T_DC;
9373b3a8eb9SGleb Smirnoff i++;
9383b3a8eb9SGleb Smirnoff break;
9393b3a8eb9SGleb Smirnoff }
9403b3a8eb9SGleb Smirnoff
9413b3a8eb9SGleb Smirnoff if (tcpopts[i] == '%') {
9423b3a8eb9SGleb Smirnoff *this_mod = T_MOD;
9433b3a8eb9SGleb Smirnoff i++;
9443b3a8eb9SGleb Smirnoff }
9453b3a8eb9SGleb Smirnoff do {
9463b3a8eb9SGleb Smirnoff if (!isdigit(tcpopts[i])) {
9473b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d unknown "
9483b3a8eb9SGleb Smirnoff "character '%c' in %c TCP opt\n",
9493b3a8eb9SGleb Smirnoff filename, lineno, tcpopts[i], opt);
9503b3a8eb9SGleb Smirnoff return (1);
9513b3a8eb9SGleb Smirnoff }
9523b3a8eb9SGleb Smirnoff *this = (*this * 10) + tcpopts[i++] - '0';
9533b3a8eb9SGleb Smirnoff } while(tcpopts[i] != ',' && tcpopts[i] != '\0');
9543b3a8eb9SGleb Smirnoff break;
9553b3a8eb9SGleb Smirnoff }
9563b3a8eb9SGleb Smirnoff case 'T':
9573b3a8eb9SGleb Smirnoff if (tcpopts[i] == '0') {
9583b3a8eb9SGleb Smirnoff *ts0 = 1;
9593b3a8eb9SGleb Smirnoff i++;
9603b3a8eb9SGleb Smirnoff }
9613b3a8eb9SGleb Smirnoff *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
9623b3a8eb9SGleb Smirnoff PF_OSFP_TCPOPT_TS;
9633b3a8eb9SGleb Smirnoff break;
9643b3a8eb9SGleb Smirnoff }
9653b3a8eb9SGleb Smirnoff (*optcnt) ++;
9663b3a8eb9SGleb Smirnoff if (tcpopts[i] == '\0')
9673b3a8eb9SGleb Smirnoff break;
9683b3a8eb9SGleb Smirnoff if (tcpopts[i] != ',') {
9693b3a8eb9SGleb Smirnoff fprintf(stderr, "%s:%d unknown option to %c TCP opt\n",
9703b3a8eb9SGleb Smirnoff filename, lineno, opt);
9713b3a8eb9SGleb Smirnoff return (1);
9723b3a8eb9SGleb Smirnoff }
9733b3a8eb9SGleb Smirnoff i++;
9743b3a8eb9SGleb Smirnoff }
9753b3a8eb9SGleb Smirnoff
9763b3a8eb9SGleb Smirnoff return (0);
9773b3a8eb9SGleb Smirnoff }
9783b3a8eb9SGleb Smirnoff
9793b3a8eb9SGleb Smirnoff /* rip the next field ouf of a formatted config file line */
9803b3a8eb9SGleb Smirnoff char *
get_field(char ** line,size_t * len,int * fieldlen)9813b3a8eb9SGleb Smirnoff get_field(char **line, size_t *len, int *fieldlen)
9823b3a8eb9SGleb Smirnoff {
9833b3a8eb9SGleb Smirnoff char *ret, *ptr = *line;
9843b3a8eb9SGleb Smirnoff size_t plen = *len;
9853b3a8eb9SGleb Smirnoff
9863b3a8eb9SGleb Smirnoff
9873b3a8eb9SGleb Smirnoff while (plen && isspace(*ptr)) {
9883b3a8eb9SGleb Smirnoff plen--;
9893b3a8eb9SGleb Smirnoff ptr++;
9903b3a8eb9SGleb Smirnoff }
9913b3a8eb9SGleb Smirnoff ret = ptr;
9923b3a8eb9SGleb Smirnoff *fieldlen = 0;
9933b3a8eb9SGleb Smirnoff
9943b3a8eb9SGleb Smirnoff for (; plen > 0 && *ptr != ':'; plen--, ptr++)
9953b3a8eb9SGleb Smirnoff (*fieldlen)++;
9963b3a8eb9SGleb Smirnoff if (plen) {
9973b3a8eb9SGleb Smirnoff *line = ptr + 1;
9983b3a8eb9SGleb Smirnoff *len = plen - 1;
9993b3a8eb9SGleb Smirnoff } else {
10003b3a8eb9SGleb Smirnoff *len = 0;
10013b3a8eb9SGleb Smirnoff }
10023b3a8eb9SGleb Smirnoff while (*fieldlen && isspace(ret[*fieldlen - 1]))
10033b3a8eb9SGleb Smirnoff (*fieldlen)--;
10043b3a8eb9SGleb Smirnoff return (ret);
10053b3a8eb9SGleb Smirnoff }
10063b3a8eb9SGleb Smirnoff
10073b3a8eb9SGleb Smirnoff
10083b3a8eb9SGleb Smirnoff const char *
print_ioctl(struct pf_osfp_ioctl * fp)10093b3a8eb9SGleb Smirnoff print_ioctl(struct pf_osfp_ioctl *fp)
10103b3a8eb9SGleb Smirnoff {
10113b3a8eb9SGleb Smirnoff static char buf[1024];
10123b3a8eb9SGleb Smirnoff char tmp[32];
10133b3a8eb9SGleb Smirnoff int i, opt;
10143b3a8eb9SGleb Smirnoff
10153b3a8eb9SGleb Smirnoff *buf = '\0';
10163b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_WSIZE_DC)
10173b3a8eb9SGleb Smirnoff strlcat(buf, "*", sizeof(buf));
10183b3a8eb9SGleb Smirnoff else if (fp->fp_flags & PF_OSFP_WSIZE_MSS)
10193b3a8eb9SGleb Smirnoff strlcat(buf, "S", sizeof(buf));
10203b3a8eb9SGleb Smirnoff else if (fp->fp_flags & PF_OSFP_WSIZE_MTU)
10213b3a8eb9SGleb Smirnoff strlcat(buf, "T", sizeof(buf));
10223b3a8eb9SGleb Smirnoff else {
10233b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_WSIZE_MOD)
10243b3a8eb9SGleb Smirnoff strlcat(buf, "%", sizeof(buf));
10253b3a8eb9SGleb Smirnoff snprintf(tmp, sizeof(tmp), "%d", fp->fp_wsize);
10263b3a8eb9SGleb Smirnoff strlcat(buf, tmp, sizeof(buf));
10273b3a8eb9SGleb Smirnoff }
10283b3a8eb9SGleb Smirnoff strlcat(buf, ":", sizeof(buf));
10293b3a8eb9SGleb Smirnoff
10303b3a8eb9SGleb Smirnoff snprintf(tmp, sizeof(tmp), "%d", fp->fp_ttl);
10313b3a8eb9SGleb Smirnoff strlcat(buf, tmp, sizeof(buf));
10323b3a8eb9SGleb Smirnoff strlcat(buf, ":", sizeof(buf));
10333b3a8eb9SGleb Smirnoff
10343b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_DF)
10353b3a8eb9SGleb Smirnoff strlcat(buf, "1", sizeof(buf));
10363b3a8eb9SGleb Smirnoff else
10373b3a8eb9SGleb Smirnoff strlcat(buf, "0", sizeof(buf));
10383b3a8eb9SGleb Smirnoff strlcat(buf, ":", sizeof(buf));
10393b3a8eb9SGleb Smirnoff
10403b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_PSIZE_DC)
10413b3a8eb9SGleb Smirnoff strlcat(buf, "*", sizeof(buf));
10423b3a8eb9SGleb Smirnoff else {
10433b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_PSIZE_MOD)
10443b3a8eb9SGleb Smirnoff strlcat(buf, "%", sizeof(buf));
10453b3a8eb9SGleb Smirnoff snprintf(tmp, sizeof(tmp), "%d", fp->fp_psize);
10463b3a8eb9SGleb Smirnoff strlcat(buf, tmp, sizeof(buf));
10473b3a8eb9SGleb Smirnoff }
10483b3a8eb9SGleb Smirnoff strlcat(buf, ":", sizeof(buf));
10493b3a8eb9SGleb Smirnoff
10503b3a8eb9SGleb Smirnoff if (fp->fp_optcnt == 0)
10513b3a8eb9SGleb Smirnoff strlcat(buf, ".", sizeof(buf));
10523b3a8eb9SGleb Smirnoff for (i = fp->fp_optcnt - 1; i >= 0; i--) {
10533b3a8eb9SGleb Smirnoff opt = fp->fp_tcpopts >> (i * PF_OSFP_TCPOPT_BITS);
10543b3a8eb9SGleb Smirnoff opt &= (1 << PF_OSFP_TCPOPT_BITS) - 1;
10553b3a8eb9SGleb Smirnoff switch (opt) {
10563b3a8eb9SGleb Smirnoff case PF_OSFP_TCPOPT_NOP:
10573b3a8eb9SGleb Smirnoff strlcat(buf, "N", sizeof(buf));
10583b3a8eb9SGleb Smirnoff break;
10593b3a8eb9SGleb Smirnoff case PF_OSFP_TCPOPT_SACK:
10603b3a8eb9SGleb Smirnoff strlcat(buf, "S", sizeof(buf));
10613b3a8eb9SGleb Smirnoff break;
10623b3a8eb9SGleb Smirnoff case PF_OSFP_TCPOPT_TS:
10633b3a8eb9SGleb Smirnoff strlcat(buf, "T", sizeof(buf));
10643b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_TS0)
10653b3a8eb9SGleb Smirnoff strlcat(buf, "0", sizeof(buf));
10663b3a8eb9SGleb Smirnoff break;
10673b3a8eb9SGleb Smirnoff case PF_OSFP_TCPOPT_MSS:
10683b3a8eb9SGleb Smirnoff strlcat(buf, "M", sizeof(buf));
10693b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_MSS_DC)
10703b3a8eb9SGleb Smirnoff strlcat(buf, "*", sizeof(buf));
10713b3a8eb9SGleb Smirnoff else {
10723b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_MSS_MOD)
10733b3a8eb9SGleb Smirnoff strlcat(buf, "%", sizeof(buf));
10743b3a8eb9SGleb Smirnoff snprintf(tmp, sizeof(tmp), "%d", fp->fp_mss);
10753b3a8eb9SGleb Smirnoff strlcat(buf, tmp, sizeof(buf));
10763b3a8eb9SGleb Smirnoff }
10773b3a8eb9SGleb Smirnoff break;
10783b3a8eb9SGleb Smirnoff case PF_OSFP_TCPOPT_WSCALE:
10793b3a8eb9SGleb Smirnoff strlcat(buf, "W", sizeof(buf));
10803b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_WSCALE_DC)
10813b3a8eb9SGleb Smirnoff strlcat(buf, "*", sizeof(buf));
10823b3a8eb9SGleb Smirnoff else {
10833b3a8eb9SGleb Smirnoff if (fp->fp_flags & PF_OSFP_WSCALE_MOD)
10843b3a8eb9SGleb Smirnoff strlcat(buf, "%", sizeof(buf));
10853b3a8eb9SGleb Smirnoff snprintf(tmp, sizeof(tmp), "%d", fp->fp_wscale);
10863b3a8eb9SGleb Smirnoff strlcat(buf, tmp, sizeof(buf));
10873b3a8eb9SGleb Smirnoff }
10883b3a8eb9SGleb Smirnoff break;
10893b3a8eb9SGleb Smirnoff }
10903b3a8eb9SGleb Smirnoff
10913b3a8eb9SGleb Smirnoff if (i != 0)
10923b3a8eb9SGleb Smirnoff strlcat(buf, ",", sizeof(buf));
10933b3a8eb9SGleb Smirnoff }
10943b3a8eb9SGleb Smirnoff strlcat(buf, ":", sizeof(buf));
10953b3a8eb9SGleb Smirnoff
10963b3a8eb9SGleb Smirnoff strlcat(buf, fp->fp_os.fp_class_nm, sizeof(buf));
10973b3a8eb9SGleb Smirnoff strlcat(buf, ":", sizeof(buf));
10983b3a8eb9SGleb Smirnoff strlcat(buf, fp->fp_os.fp_version_nm, sizeof(buf));
10993b3a8eb9SGleb Smirnoff strlcat(buf, ":", sizeof(buf));
11003b3a8eb9SGleb Smirnoff strlcat(buf, fp->fp_os.fp_subtype_nm, sizeof(buf));
11013b3a8eb9SGleb Smirnoff strlcat(buf, ":", sizeof(buf));
11023b3a8eb9SGleb Smirnoff
11033b3a8eb9SGleb Smirnoff snprintf(tmp, sizeof(tmp), "TcpOpts %d 0x%llx", fp->fp_optcnt,
11043b3a8eb9SGleb Smirnoff (long long int)fp->fp_tcpopts);
11053b3a8eb9SGleb Smirnoff strlcat(buf, tmp, sizeof(buf));
11063b3a8eb9SGleb Smirnoff
11073b3a8eb9SGleb Smirnoff return (buf);
11083b3a8eb9SGleb Smirnoff }
1109