pfctl_osfp.c revision 1.6 1 1.6 christos /* $NetBSD: pfctl_osfp.c,v 1.6 2006/03/21 20:31:56 christos Exp $ */
2 1.5 peter /* $OpenBSD: pfctl_osfp.c,v 1.12 2005/02/17 13:18:00 aaron Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (c) 2003 Mike Frantzen <frantzen (at) openbsd.org>
6 1.1 itojun *
7 1.1 itojun * Permission to use, copy, modify, and distribute this software for any
8 1.1 itojun * purpose with or without fee is hereby granted, provided that the above
9 1.1 itojun * copyright notice and this permission notice appear in all copies.
10 1.1 itojun *
11 1.1 itojun * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 itojun * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 itojun * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 itojun * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 itojun * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 itojun * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 itojun * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 itojun */
19 1.1 itojun
20 1.1 itojun #include <sys/types.h>
21 1.1 itojun #include <sys/ioctl.h>
22 1.1 itojun #include <sys/socket.h>
23 1.1 itojun
24 1.2 itojun #ifdef __NetBSD__
25 1.2 itojun #include <netinet/in.h>
26 1.2 itojun #endif
27 1.2 itojun
28 1.1 itojun #include <net/if.h>
29 1.1 itojun #include <net/pfvar.h>
30 1.1 itojun
31 1.1 itojun #include <ctype.h>
32 1.1 itojun #include <err.h>
33 1.1 itojun #include <errno.h>
34 1.1 itojun #include <stdio.h>
35 1.1 itojun #include <stdlib.h>
36 1.1 itojun #include <string.h>
37 1.1 itojun
38 1.1 itojun #include "pfctl_parser.h"
39 1.1 itojun #include "pfctl.h"
40 1.1 itojun
41 1.1 itojun #ifndef MIN
42 1.1 itojun # define MIN(a,b) (((a) < (b)) ? (a) : (b))
43 1.1 itojun #endif /* MIN */
44 1.1 itojun #ifndef MAX
45 1.1 itojun # define MAX(a,b) (((a) > (b)) ? (a) : (b))
46 1.1 itojun #endif /* MAX */
47 1.1 itojun
48 1.1 itojun
49 1.1 itojun #if 0
50 1.1 itojun # define DEBUG(fp, str, v...) \
51 1.1 itojun fprintf(stderr, "%s:%s:%s " str "\n", (fp)->fp_os.fp_class_nm, \
52 1.1 itojun (fp)->fp_os.fp_version_nm, (fp)->fp_os.fp_subtype_nm , ## v);
53 1.1 itojun #else
54 1.1 itojun # define DEBUG(fp, str, v...) ((void)0)
55 1.1 itojun #endif
56 1.1 itojun
57 1.1 itojun
58 1.1 itojun struct name_entry;
59 1.1 itojun LIST_HEAD(name_list, name_entry);
60 1.1 itojun struct name_entry {
61 1.1 itojun LIST_ENTRY(name_entry) nm_entry;
62 1.1 itojun int nm_num;
63 1.1 itojun char nm_name[PF_OSFP_LEN];
64 1.1 itojun
65 1.1 itojun struct name_list nm_sublist;
66 1.1 itojun int nm_sublist_num;
67 1.1 itojun };
68 1.1 itojun struct name_list classes = LIST_HEAD_INITIALIZER(&classes);
69 1.1 itojun int class_count;
70 1.1 itojun int fingerprint_count;
71 1.1 itojun
72 1.1 itojun void add_fingerprint(int, int, struct pf_osfp_ioctl *);
73 1.1 itojun struct name_entry *fingerprint_name_entry(struct name_list *, char *);
74 1.1 itojun void pfctl_flush_my_fingerprints(struct name_list *);
75 1.1 itojun char *get_field(char **, size_t *, int *);
76 1.1 itojun int get_int(char **, size_t *, int *, int *, const char *,
77 1.1 itojun int, int, const char *, int);
78 1.1 itojun int get_str(char **, size_t *, char **, const char *, int,
79 1.1 itojun const char *, int);
80 1.1 itojun int get_tcpopts(const char *, int, const char *,
81 1.1 itojun pf_tcpopts_t *, int *, int *, int *, int *, int *,
82 1.1 itojun int *);
83 1.1 itojun void import_fingerprint(struct pf_osfp_ioctl *);
84 1.1 itojun const char *print_ioctl(struct pf_osfp_ioctl *);
85 1.1 itojun void print_name_list(int, struct name_list *, const char *);
86 1.1 itojun void sort_name_list(int, struct name_list *);
87 1.1 itojun struct name_entry *lookup_name_list(struct name_list *, const char *);
88 1.1 itojun
89 1.1 itojun /* Load fingerprints from a file */
90 1.1 itojun int
91 1.1 itojun pfctl_file_fingerprints(int dev, int opts, const char *fp_filename)
92 1.1 itojun {
93 1.1 itojun FILE *in;
94 1.1 itojun char *line;
95 1.1 itojun size_t len;
96 1.1 itojun int i, lineno = 0;
97 1.1 itojun int window, w_mod, ttl, df, psize, p_mod, mss, mss_mod, wscale,
98 1.1 itojun wscale_mod, optcnt, ts0;
99 1.1 itojun pf_tcpopts_t packed_tcpopts;
100 1.1 itojun char *class, *version, *subtype, *desc, *tcpopts;
101 1.1 itojun struct pf_osfp_ioctl fp;
102 1.1 itojun
103 1.1 itojun pfctl_flush_my_fingerprints(&classes);
104 1.1 itojun
105 1.4 yamt if ((in = pfctl_fopen(fp_filename, "r")) == NULL) {
106 1.4 yamt warn("%s", fp_filename);
107 1.1 itojun return (1);
108 1.1 itojun }
109 1.1 itojun class = version = subtype = desc = tcpopts = NULL;
110 1.1 itojun
111 1.1 itojun if ((opts & PF_OPT_NOACTION) == 0)
112 1.1 itojun pfctl_clear_fingerprints(dev, opts);
113 1.1 itojun
114 1.1 itojun while ((line = fgetln(in, &len)) != NULL) {
115 1.1 itojun lineno++;
116 1.1 itojun if (class)
117 1.1 itojun free(class);
118 1.1 itojun if (version)
119 1.1 itojun free(version);
120 1.1 itojun if (subtype)
121 1.1 itojun free(subtype);
122 1.1 itojun if (desc)
123 1.1 itojun free(desc);
124 1.1 itojun if (tcpopts)
125 1.1 itojun free(tcpopts);
126 1.1 itojun class = version = subtype = desc = tcpopts = NULL;
127 1.1 itojun memset(&fp, 0, sizeof(fp));
128 1.1 itojun
129 1.1 itojun /* Chop off comment */
130 1.1 itojun for (i = 0; i < len; i++)
131 1.1 itojun if (line[i] == '#') {
132 1.1 itojun len = i;
133 1.1 itojun break;
134 1.1 itojun }
135 1.1 itojun /* Chop off whitespace */
136 1.3 dsl while (len > 0 && isspace((unsigned char)line[len - 1]))
137 1.1 itojun len--;
138 1.3 dsl while (len > 0 && isspace((unsigned char)line[0])) {
139 1.1 itojun len--;
140 1.1 itojun line++;
141 1.1 itojun }
142 1.1 itojun if (len == 0)
143 1.1 itojun continue;
144 1.1 itojun
145 1.1 itojun #define T_DC 0x01 /* Allow don't care */
146 1.1 itojun #define T_MSS 0x02 /* Allow MSS multiple */
147 1.1 itojun #define T_MTU 0x04 /* Allow MTU multiple */
148 1.1 itojun #define T_MOD 0x08 /* Allow modulus */
149 1.1 itojun
150 1.1 itojun #define GET_INT(v, mod, n, ty, mx) \
151 1.1 itojun get_int(&line, &len, &v, mod, n, ty, mx, fp_filename, lineno)
152 1.1 itojun #define GET_STR(v, n, mn) \
153 1.1 itojun get_str(&line, &len, &v, n, mn, fp_filename, lineno)
154 1.1 itojun
155 1.1 itojun if (GET_INT(window, &w_mod, "window size", T_DC|T_MSS|T_MTU|
156 1.1 itojun T_MOD, 0xffff) ||
157 1.1 itojun GET_INT(ttl, NULL, "ttl", 0, 0xff) ||
158 1.1 itojun GET_INT(df, NULL, "don't fragment frag", 0, 1) ||
159 1.1 itojun GET_INT(psize, &p_mod, "overall packet size", T_MOD|T_DC,
160 1.1 itojun 8192) ||
161 1.1 itojun GET_STR(tcpopts, "TCP Options", 1) ||
162 1.1 itojun GET_STR(class, "OS class", 1) ||
163 1.1 itojun GET_STR(version, "OS version", 0) ||
164 1.1 itojun GET_STR(subtype, "OS subtype", 0) ||
165 1.1 itojun GET_STR(desc, "OS description", 2))
166 1.1 itojun continue;
167 1.1 itojun if (get_tcpopts(fp_filename, lineno, tcpopts, &packed_tcpopts,
168 1.1 itojun &optcnt, &mss, &mss_mod, &wscale, &wscale_mod, &ts0))
169 1.1 itojun continue;
170 1.1 itojun if (len != 0) {
171 1.1 itojun fprintf(stderr, "%s:%d excess field\n", fp_filename,
172 1.1 itojun lineno);
173 1.1 itojun continue;
174 1.1 itojun }
175 1.1 itojun
176 1.1 itojun fp.fp_ttl = ttl;
177 1.1 itojun if (df)
178 1.1 itojun fp.fp_flags |= PF_OSFP_DF;
179 1.1 itojun switch (w_mod) {
180 1.1 itojun case 0:
181 1.1 itojun break;
182 1.1 itojun case T_DC:
183 1.1 itojun fp.fp_flags |= PF_OSFP_WSIZE_DC;
184 1.1 itojun break;
185 1.1 itojun case T_MSS:
186 1.1 itojun fp.fp_flags |= PF_OSFP_WSIZE_MSS;
187 1.1 itojun break;
188 1.1 itojun case T_MTU:
189 1.1 itojun fp.fp_flags |= PF_OSFP_WSIZE_MTU;
190 1.1 itojun break;
191 1.1 itojun case T_MOD:
192 1.1 itojun fp.fp_flags |= PF_OSFP_WSIZE_MOD;
193 1.1 itojun break;
194 1.1 itojun }
195 1.1 itojun fp.fp_wsize = window;
196 1.1 itojun
197 1.1 itojun switch (p_mod) {
198 1.1 itojun case T_DC:
199 1.1 itojun fp.fp_flags |= PF_OSFP_PSIZE_DC;
200 1.1 itojun break;
201 1.1 itojun case T_MOD:
202 1.1 itojun fp.fp_flags |= PF_OSFP_PSIZE_MOD;
203 1.1 itojun }
204 1.1 itojun fp.fp_psize = psize;
205 1.1 itojun
206 1.1 itojun
207 1.1 itojun switch (wscale_mod) {
208 1.1 itojun case T_DC:
209 1.1 itojun fp.fp_flags |= PF_OSFP_WSCALE_DC;
210 1.1 itojun break;
211 1.1 itojun case T_MOD:
212 1.1 itojun fp.fp_flags |= PF_OSFP_WSCALE_MOD;
213 1.1 itojun }
214 1.1 itojun fp.fp_wscale = wscale;
215 1.1 itojun
216 1.1 itojun switch (mss_mod) {
217 1.1 itojun case T_DC:
218 1.1 itojun fp.fp_flags |= PF_OSFP_MSS_DC;
219 1.1 itojun break;
220 1.1 itojun case T_MOD:
221 1.1 itojun fp.fp_flags |= PF_OSFP_MSS_MOD;
222 1.1 itojun break;
223 1.1 itojun }
224 1.1 itojun fp.fp_mss = mss;
225 1.1 itojun
226 1.1 itojun fp.fp_tcpopts = packed_tcpopts;
227 1.1 itojun fp.fp_optcnt = optcnt;
228 1.1 itojun if (ts0)
229 1.1 itojun fp.fp_flags |= PF_OSFP_TS0;
230 1.1 itojun
231 1.1 itojun if (class[0] == '@')
232 1.1 itojun fp.fp_os.fp_enflags |= PF_OSFP_GENERIC;
233 1.1 itojun if (class[0] == '*')
234 1.1 itojun fp.fp_os.fp_enflags |= PF_OSFP_NODETAIL;
235 1.1 itojun
236 1.1 itojun if (class[0] == '@' || class[0] == '*')
237 1.1 itojun strlcpy(fp.fp_os.fp_class_nm, class + 1,
238 1.1 itojun sizeof(fp.fp_os.fp_class_nm));
239 1.1 itojun else
240 1.1 itojun strlcpy(fp.fp_os.fp_class_nm, class,
241 1.1 itojun sizeof(fp.fp_os.fp_class_nm));
242 1.1 itojun strlcpy(fp.fp_os.fp_version_nm, version,
243 1.1 itojun sizeof(fp.fp_os.fp_version_nm));
244 1.1 itojun strlcpy(fp.fp_os.fp_subtype_nm, subtype,
245 1.1 itojun sizeof(fp.fp_os.fp_subtype_nm));
246 1.1 itojun
247 1.1 itojun add_fingerprint(dev, opts, &fp);
248 1.1 itojun }
249 1.1 itojun
250 1.1 itojun if (class)
251 1.1 itojun free(class);
252 1.1 itojun if (version)
253 1.1 itojun free(version);
254 1.1 itojun if (subtype)
255 1.1 itojun free(subtype);
256 1.1 itojun if (desc)
257 1.1 itojun free(desc);
258 1.6 christos if (tcpopts)
259 1.6 christos free(tcpopts);
260 1.1 itojun
261 1.1 itojun fclose(in);
262 1.1 itojun
263 1.1 itojun if (opts & PF_OPT_VERBOSE2)
264 1.1 itojun printf("Loaded %d passive OS fingerprints\n",
265 1.1 itojun fingerprint_count);
266 1.1 itojun return (0);
267 1.1 itojun }
268 1.1 itojun
269 1.1 itojun /* flush the kernel's fingerprints */
270 1.1 itojun void
271 1.1 itojun pfctl_clear_fingerprints(int dev, int opts)
272 1.1 itojun {
273 1.1 itojun if (ioctl(dev, DIOCOSFPFLUSH))
274 1.1 itojun err(1, "DIOCOSFPFLUSH");
275 1.1 itojun }
276 1.1 itojun
277 1.1 itojun /* flush pfctl's view of the fingerprints */
278 1.1 itojun void
279 1.1 itojun pfctl_flush_my_fingerprints(struct name_list *list)
280 1.1 itojun {
281 1.1 itojun struct name_entry *nm;
282 1.1 itojun
283 1.1 itojun while ((nm = LIST_FIRST(list)) != NULL) {
284 1.1 itojun LIST_REMOVE(nm, nm_entry);
285 1.1 itojun pfctl_flush_my_fingerprints(&nm->nm_sublist);
286 1.1 itojun free(nm);
287 1.1 itojun }
288 1.5 peter fingerprint_count = 0;
289 1.1 itojun class_count = 0;
290 1.1 itojun }
291 1.1 itojun
292 1.1 itojun /* Fetch the active fingerprints from the kernel */
293 1.1 itojun int
294 1.1 itojun pfctl_load_fingerprints(int dev, int opts)
295 1.1 itojun {
296 1.1 itojun struct pf_osfp_ioctl io;
297 1.1 itojun int i;
298 1.1 itojun
299 1.1 itojun pfctl_flush_my_fingerprints(&classes);
300 1.1 itojun
301 1.1 itojun for (i = 0; i >= 0; i++) {
302 1.1 itojun memset(&io, 0, sizeof(io));
303 1.1 itojun io.fp_getnum = i;
304 1.1 itojun if (ioctl(dev, DIOCOSFPGET, &io)) {
305 1.1 itojun if (errno == EBUSY)
306 1.1 itojun break;
307 1.1 itojun warn("DIOCOSFPGET");
308 1.1 itojun return (1);
309 1.1 itojun }
310 1.1 itojun import_fingerprint(&io);
311 1.1 itojun }
312 1.1 itojun return (0);
313 1.1 itojun }
314 1.1 itojun
315 1.1 itojun /* List the fingerprints */
316 1.1 itojun void
317 1.1 itojun pfctl_show_fingerprints(int opts)
318 1.1 itojun {
319 1.1 itojun if (LIST_FIRST(&classes) != NULL) {
320 1.1 itojun if (opts & PF_OPT_SHOWALL) {
321 1.1 itojun pfctl_print_title("OS FINGERPRINTS:");
322 1.1 itojun printf("%u fingerprints loaded\n", fingerprint_count);
323 1.1 itojun } else {
324 1.1 itojun printf("Class\tVersion\tSubtype(subversion)\n");
325 1.1 itojun printf("-----\t-------\t-------------------\n");
326 1.1 itojun sort_name_list(opts, &classes);
327 1.1 itojun print_name_list(opts, &classes, "");
328 1.1 itojun }
329 1.1 itojun }
330 1.1 itojun }
331 1.1 itojun
332 1.1 itojun /* Lookup a fingerprint */
333 1.1 itojun pf_osfp_t
334 1.1 itojun pfctl_get_fingerprint(const char *name)
335 1.1 itojun {
336 1.1 itojun struct name_entry *nm, *class_nm, *version_nm, *subtype_nm;
337 1.1 itojun pf_osfp_t ret = PF_OSFP_NOMATCH;
338 1.1 itojun int class, version, subtype;
339 1.1 itojun int unp_class, unp_version, unp_subtype;
340 1.1 itojun int wr_len, version_len, subtype_len;
341 1.1 itojun char *ptr, *wr_name;
342 1.1 itojun
343 1.1 itojun if (strcasecmp(name, "unknown") == 0)
344 1.1 itojun return (PF_OSFP_UNKNOWN);
345 1.1 itojun
346 1.1 itojun /* Try most likely no version and no subtype */
347 1.1 itojun if ((nm = lookup_name_list(&classes, name))) {
348 1.1 itojun class = nm->nm_num;
349 1.1 itojun version = PF_OSFP_ANY;
350 1.1 itojun subtype = PF_OSFP_ANY;
351 1.1 itojun goto found;
352 1.1 itojun } else {
353 1.1 itojun
354 1.1 itojun /* Chop it up into class/version/subtype */
355 1.1 itojun
356 1.1 itojun if ((wr_name = strdup(name)) == NULL)
357 1.1 itojun err(1, "malloc");
358 1.5 peter if ((ptr = strchr(wr_name, ' ')) == NULL) {
359 1.1 itojun free(wr_name);
360 1.1 itojun return (PF_OSFP_NOMATCH);
361 1.1 itojun }
362 1.1 itojun *ptr++ = '\0';
363 1.1 itojun
364 1.1 itojun /* The class is easy to find since it is delimited by a space */
365 1.1 itojun if ((class_nm = lookup_name_list(&classes, wr_name)) == NULL) {
366 1.1 itojun free(wr_name);
367 1.1 itojun return (PF_OSFP_NOMATCH);
368 1.1 itojun }
369 1.1 itojun class = class_nm->nm_num;
370 1.1 itojun
371 1.1 itojun /* Try no subtype */
372 1.1 itojun if ((version_nm = lookup_name_list(&class_nm->nm_sublist, ptr)))
373 1.1 itojun {
374 1.1 itojun version = version_nm->nm_num;
375 1.1 itojun subtype = PF_OSFP_ANY;
376 1.1 itojun free(wr_name);
377 1.1 itojun goto found;
378 1.1 itojun }
379 1.1 itojun
380 1.1 itojun
381 1.1 itojun /*
382 1.1 itojun * There must be a version and a subtype.
383 1.1 itojun * We'll do some fuzzy matching to pick up things like:
384 1.1 itojun * Linux 2.2.14 (version=2.2 subtype=14)
385 1.1 itojun * FreeBSD 4.0-STABLE (version=4.0 subtype=STABLE)
386 1.1 itojun * Windows 2000 SP2 (version=2000 subtype=SP2)
387 1.1 itojun */
388 1.1 itojun #define CONNECTOR(x) ((x) == '.' || (x) == ' ' || (x) == '\t' || (x) == '-')
389 1.1 itojun wr_len = strlen(ptr);
390 1.1 itojun LIST_FOREACH(version_nm, &class_nm->nm_sublist, nm_entry) {
391 1.1 itojun version_len = strlen(version_nm->nm_name);
392 1.1 itojun if (wr_len < version_len + 2 ||
393 1.1 itojun !CONNECTOR(ptr[version_len]))
394 1.1 itojun continue;
395 1.1 itojun /* first part of the string must be version */
396 1.1 itojun if (strncasecmp(ptr, version_nm->nm_name,
397 1.1 itojun version_len))
398 1.1 itojun continue;
399 1.1 itojun
400 1.1 itojun LIST_FOREACH(subtype_nm, &version_nm->nm_sublist,
401 1.1 itojun nm_entry) {
402 1.1 itojun subtype_len = strlen(subtype_nm->nm_name);
403 1.1 itojun if (wr_len != version_len + subtype_len + 1)
404 1.1 itojun continue;
405 1.1 itojun
406 1.1 itojun /* last part of the string must be subtype */
407 1.1 itojun if (strcasecmp(&ptr[version_len+1],
408 1.1 itojun subtype_nm->nm_name) != 0)
409 1.1 itojun continue;
410 1.1 itojun
411 1.1 itojun /* Found it!! */
412 1.1 itojun version = version_nm->nm_num;
413 1.1 itojun subtype = subtype_nm->nm_num;
414 1.1 itojun free(wr_name);
415 1.1 itojun goto found;
416 1.1 itojun }
417 1.1 itojun }
418 1.1 itojun
419 1.1 itojun free(wr_name);
420 1.1 itojun return (PF_OSFP_NOMATCH);
421 1.1 itojun }
422 1.1 itojun
423 1.1 itojun found:
424 1.1 itojun PF_OSFP_PACK(ret, class, version, subtype);
425 1.1 itojun if (ret != PF_OSFP_NOMATCH) {
426 1.1 itojun PF_OSFP_UNPACK(ret, unp_class, unp_version, unp_subtype);
427 1.1 itojun if (class != unp_class) {
428 1.1 itojun fprintf(stderr, "warning: fingerprint table overflowed "
429 1.1 itojun "classes\n");
430 1.1 itojun return (PF_OSFP_NOMATCH);
431 1.1 itojun }
432 1.1 itojun if (version != unp_version) {
433 1.1 itojun fprintf(stderr, "warning: fingerprint table overflowed "
434 1.1 itojun "versions\n");
435 1.1 itojun return (PF_OSFP_NOMATCH);
436 1.1 itojun }
437 1.1 itojun if (subtype != unp_subtype) {
438 1.1 itojun fprintf(stderr, "warning: fingerprint table overflowed "
439 1.1 itojun "subtypes\n");
440 1.1 itojun return (PF_OSFP_NOMATCH);
441 1.1 itojun }
442 1.1 itojun }
443 1.1 itojun if (ret == PF_OSFP_ANY) {
444 1.1 itojun /* should never happen */
445 1.1 itojun fprintf(stderr, "warning: fingerprint packed to 'any'\n");
446 1.1 itojun return (PF_OSFP_NOMATCH);
447 1.1 itojun }
448 1.1 itojun
449 1.1 itojun return (ret);
450 1.1 itojun }
451 1.1 itojun
452 1.1 itojun /* Lookup a fingerprint name by ID */
453 1.1 itojun char *
454 1.1 itojun pfctl_lookup_fingerprint(pf_osfp_t fp, char *buf, size_t len)
455 1.1 itojun {
456 1.1 itojun int class, version, subtype;
457 1.1 itojun struct name_list *list;
458 1.1 itojun struct name_entry *nm;
459 1.1 itojun
460 1.1 itojun char *class_name, *version_name, *subtype_name;
461 1.1 itojun class_name = version_name = subtype_name = NULL;
462 1.1 itojun
463 1.1 itojun if (fp == PF_OSFP_UNKNOWN) {
464 1.1 itojun strlcpy(buf, "unknown", len);
465 1.1 itojun return (buf);
466 1.1 itojun }
467 1.1 itojun if (fp == PF_OSFP_ANY) {
468 1.1 itojun strlcpy(buf, "any", len);
469 1.1 itojun return (buf);
470 1.1 itojun }
471 1.1 itojun
472 1.1 itojun PF_OSFP_UNPACK(fp, class, version, subtype);
473 1.1 itojun if (class >= (1 << _FP_CLASS_BITS) ||
474 1.1 itojun version >= (1 << _FP_VERSION_BITS) ||
475 1.1 itojun subtype >= (1 << _FP_SUBTYPE_BITS)) {
476 1.1 itojun warnx("PF_OSFP_UNPACK(0x%x) failed!!", fp);
477 1.1 itojun strlcpy(buf, "nomatch", len);
478 1.1 itojun return (buf);
479 1.1 itojun }
480 1.1 itojun
481 1.1 itojun LIST_FOREACH(nm, &classes, nm_entry) {
482 1.1 itojun if (nm->nm_num == class) {
483 1.1 itojun class_name = nm->nm_name;
484 1.1 itojun if (version == PF_OSFP_ANY)
485 1.1 itojun goto found;
486 1.1 itojun list = &nm->nm_sublist;
487 1.1 itojun LIST_FOREACH(nm, list, nm_entry) {
488 1.1 itojun if (nm->nm_num == version) {
489 1.1 itojun version_name = nm->nm_name;
490 1.1 itojun if (subtype == PF_OSFP_ANY)
491 1.1 itojun goto found;
492 1.1 itojun list = &nm->nm_sublist;
493 1.1 itojun LIST_FOREACH(nm, list, nm_entry) {
494 1.1 itojun if (nm->nm_num == subtype) {
495 1.1 itojun subtype_name =
496 1.1 itojun nm->nm_name;
497 1.1 itojun goto found;
498 1.1 itojun }
499 1.1 itojun } /* foreach subtype */
500 1.1 itojun strlcpy(buf, "nomatch", len);
501 1.1 itojun return (buf);
502 1.1 itojun }
503 1.1 itojun } /* foreach version */
504 1.1 itojun strlcpy(buf, "nomatch", len);
505 1.1 itojun return (buf);
506 1.1 itojun }
507 1.1 itojun } /* foreach class */
508 1.1 itojun
509 1.1 itojun strlcpy(buf, "nomatch", len);
510 1.1 itojun return (buf);
511 1.1 itojun
512 1.1 itojun found:
513 1.1 itojun snprintf(buf, len, "%s", class_name);
514 1.1 itojun if (version_name) {
515 1.1 itojun strlcat(buf, " ", len);
516 1.1 itojun strlcat(buf, version_name, len);
517 1.1 itojun if (subtype_name) {
518 1.5 peter if (strchr(version_name, ' '))
519 1.1 itojun strlcat(buf, " ", len);
520 1.5 peter else if (strchr(version_name, '.') &&
521 1.3 dsl isdigit((unsigned char)*subtype_name))
522 1.1 itojun strlcat(buf, ".", len);
523 1.1 itojun else
524 1.1 itojun strlcat(buf, " ", len);
525 1.1 itojun strlcat(buf, subtype_name, len);
526 1.1 itojun }
527 1.1 itojun }
528 1.1 itojun return (buf);
529 1.1 itojun }
530 1.1 itojun
531 1.1 itojun /* lookup a name in a list */
532 1.1 itojun struct name_entry *
533 1.1 itojun lookup_name_list(struct name_list *list, const char *name)
534 1.1 itojun {
535 1.1 itojun struct name_entry *nm;
536 1.1 itojun LIST_FOREACH(nm, list, nm_entry)
537 1.1 itojun if (strcasecmp(name, nm->nm_name) == 0)
538 1.1 itojun return (nm);
539 1.1 itojun
540 1.1 itojun return (NULL);
541 1.1 itojun }
542 1.1 itojun
543 1.1 itojun
544 1.1 itojun void
545 1.1 itojun add_fingerprint(int dev, int opts, struct pf_osfp_ioctl *fp)
546 1.1 itojun {
547 1.1 itojun struct pf_osfp_ioctl fptmp;
548 1.1 itojun struct name_entry *nm_class, *nm_version, *nm_subtype;
549 1.1 itojun int class, version, subtype;
550 1.1 itojun
551 1.1 itojun /* We expand #-# or #.#-#.# version/subtypes into multiple fingerprints */
552 1.1 itojun #define EXPAND(field) do { \
553 1.1 itojun int _dot = -1, _start = -1, _end = -1, _i = 0; \
554 1.1 itojun /* pick major version out of #.# */ \
555 1.3 dsl if (isdigit((unsigned char)fp->field[_i]) && fp->field[_i+1] == '.') { \
556 1.1 itojun _dot = fp->field[_i] - '0'; \
557 1.1 itojun _i += 2; \
558 1.1 itojun } \
559 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
560 1.1 itojun _start = fp->field[_i++] - '0'; \
561 1.1 itojun else \
562 1.1 itojun break; \
563 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
564 1.1 itojun _start = (_start * 10) + fp->field[_i++] - '0'; \
565 1.1 itojun if (fp->field[_i++] != '-') \
566 1.1 itojun break; \
567 1.3 dsl if (isdigit((unsigned char)fp->field[_i]) && fp->field[_i+1] == '.' && \
568 1.1 itojun fp->field[_i] - '0' == _dot) \
569 1.1 itojun _i += 2; \
570 1.1 itojun else if (_dot != -1) \
571 1.1 itojun break; \
572 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
573 1.1 itojun _end = fp->field[_i++] - '0'; \
574 1.1 itojun else \
575 1.1 itojun break; \
576 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
577 1.1 itojun _end = (_end * 10) + fp->field[_i++] - '0'; \
578 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
579 1.1 itojun _end = (_end * 10) + fp->field[_i++] - '0'; \
580 1.1 itojun if (fp->field[_i] != '\0') \
581 1.1 itojun break; \
582 1.1 itojun memcpy(&fptmp, fp, sizeof(fptmp)); \
583 1.1 itojun for (;_start <= _end; _start++) { \
584 1.1 itojun memset(fptmp.field, 0, sizeof(fptmp.field)); \
585 1.1 itojun fptmp.fp_os.fp_enflags |= PF_OSFP_EXPANDED; \
586 1.1 itojun if (_dot == -1) \
587 1.1 itojun snprintf(fptmp.field, sizeof(fptmp.field), \
588 1.1 itojun "%d", _start); \
589 1.1 itojun else \
590 1.1 itojun snprintf(fptmp.field, sizeof(fptmp.field), \
591 1.1 itojun "%d.%d", _dot, _start); \
592 1.1 itojun add_fingerprint(dev, opts, &fptmp); \
593 1.1 itojun } \
594 1.1 itojun } while(0)
595 1.1 itojun
596 1.1 itojun /* We allow "#-#" as a version or subtype and we'll expand it */
597 1.1 itojun EXPAND(fp_os.fp_version_nm);
598 1.1 itojun EXPAND(fp_os.fp_subtype_nm);
599 1.1 itojun
600 1.1 itojun if (strcasecmp(fp->fp_os.fp_class_nm, "nomatch") == 0)
601 1.1 itojun errx(1, "fingerprint class \"nomatch\" is reserved");
602 1.1 itojun
603 1.1 itojun version = PF_OSFP_ANY;
604 1.1 itojun subtype = PF_OSFP_ANY;
605 1.1 itojun
606 1.1 itojun nm_class = fingerprint_name_entry(&classes, fp->fp_os.fp_class_nm);
607 1.1 itojun if (nm_class->nm_num == 0)
608 1.1 itojun nm_class->nm_num = ++class_count;
609 1.1 itojun class = nm_class->nm_num;
610 1.1 itojun
611 1.1 itojun nm_version = fingerprint_name_entry(&nm_class->nm_sublist,
612 1.1 itojun fp->fp_os.fp_version_nm);
613 1.1 itojun if (nm_version) {
614 1.1 itojun if (nm_version->nm_num == 0)
615 1.1 itojun nm_version->nm_num = ++nm_class->nm_sublist_num;
616 1.1 itojun version = nm_version->nm_num;
617 1.1 itojun nm_subtype = fingerprint_name_entry(&nm_version->nm_sublist,
618 1.1 itojun fp->fp_os.fp_subtype_nm);
619 1.1 itojun if (nm_subtype) {
620 1.1 itojun if (nm_subtype->nm_num == 0)
621 1.1 itojun nm_subtype->nm_num =
622 1.1 itojun ++nm_version->nm_sublist_num;
623 1.1 itojun subtype = nm_subtype->nm_num;
624 1.1 itojun }
625 1.1 itojun }
626 1.1 itojun
627 1.1 itojun
628 1.1 itojun DEBUG(fp, "\tsignature %d:%d:%d %s", class, version, subtype,
629 1.1 itojun print_ioctl(fp));
630 1.1 itojun
631 1.1 itojun PF_OSFP_PACK(fp->fp_os.fp_os, class, version, subtype);
632 1.1 itojun fingerprint_count++;
633 1.1 itojun
634 1.1 itojun #ifdef FAKE_PF_KERNEL
635 1.1 itojun /* Linked to the sys/net/pf_osfp.c. Call pf_osfp_add() */
636 1.1 itojun if ((errno = pf_osfp_add(fp)))
637 1.1 itojun #else
638 1.1 itojun if ((opts & PF_OPT_NOACTION) == 0 && ioctl(dev, DIOCOSFPADD, fp))
639 1.1 itojun #endif /* FAKE_PF_KERNEL */
640 1.1 itojun {
641 1.1 itojun if (errno == EEXIST) {
642 1.1 itojun warn("Duplicate signature for %s %s %s",
643 1.1 itojun fp->fp_os.fp_class_nm,
644 1.1 itojun fp->fp_os.fp_version_nm,
645 1.1 itojun fp->fp_os.fp_subtype_nm);
646 1.1 itojun
647 1.1 itojun } else {
648 1.1 itojun err(1, "DIOCOSFPADD");
649 1.1 itojun }
650 1.1 itojun }
651 1.1 itojun }
652 1.1 itojun
653 1.1 itojun /* import a fingerprint from the kernel */
654 1.1 itojun void
655 1.1 itojun import_fingerprint(struct pf_osfp_ioctl *fp)
656 1.1 itojun {
657 1.1 itojun struct name_entry *nm_class, *nm_version, *nm_subtype;
658 1.1 itojun int class, version, subtype;
659 1.1 itojun
660 1.1 itojun PF_OSFP_UNPACK(fp->fp_os.fp_os, class, version, subtype);
661 1.1 itojun
662 1.1 itojun nm_class = fingerprint_name_entry(&classes, fp->fp_os.fp_class_nm);
663 1.1 itojun if (nm_class->nm_num == 0) {
664 1.1 itojun nm_class->nm_num = class;
665 1.1 itojun class_count = MAX(class_count, class);
666 1.1 itojun }
667 1.1 itojun
668 1.1 itojun nm_version = fingerprint_name_entry(&nm_class->nm_sublist,
669 1.1 itojun fp->fp_os.fp_version_nm);
670 1.1 itojun if (nm_version) {
671 1.1 itojun if (nm_version->nm_num == 0) {
672 1.1 itojun nm_version->nm_num = version;
673 1.1 itojun nm_class->nm_sublist_num = MAX(nm_class->nm_sublist_num,
674 1.1 itojun version);
675 1.1 itojun }
676 1.1 itojun nm_subtype = fingerprint_name_entry(&nm_version->nm_sublist,
677 1.1 itojun fp->fp_os.fp_subtype_nm);
678 1.1 itojun if (nm_subtype) {
679 1.1 itojun if (nm_subtype->nm_num == 0) {
680 1.1 itojun nm_subtype->nm_num = subtype;
681 1.1 itojun nm_version->nm_sublist_num =
682 1.1 itojun MAX(nm_version->nm_sublist_num, subtype);
683 1.1 itojun }
684 1.1 itojun }
685 1.1 itojun }
686 1.1 itojun
687 1.1 itojun
688 1.1 itojun fingerprint_count++;
689 1.1 itojun DEBUG(fp, "import signature %d:%d:%d", class, version, subtype);
690 1.1 itojun }
691 1.1 itojun
692 1.1 itojun /* Find an entry for a fingerprints class/version/subtype */
693 1.1 itojun struct name_entry *
694 1.1 itojun fingerprint_name_entry(struct name_list *list, char *name)
695 1.1 itojun {
696 1.1 itojun struct name_entry *nm_entry;
697 1.1 itojun
698 1.1 itojun if (name == NULL || strlen(name) == 0)
699 1.1 itojun return (NULL);
700 1.1 itojun
701 1.1 itojun LIST_FOREACH(nm_entry, list, nm_entry) {
702 1.1 itojun if (strcasecmp(nm_entry->nm_name, name) == 0) {
703 1.1 itojun /* We'll move this to the front of the list later */
704 1.1 itojun LIST_REMOVE(nm_entry, nm_entry);
705 1.1 itojun break;
706 1.1 itojun }
707 1.1 itojun }
708 1.1 itojun if (nm_entry == NULL) {
709 1.1 itojun nm_entry = calloc(1, sizeof(*nm_entry));
710 1.1 itojun if (nm_entry == NULL)
711 1.1 itojun err(1, "calloc");
712 1.5 peter LIST_INIT(&nm_entry->nm_sublist);
713 1.5 peter strlcpy(nm_entry->nm_name, name, sizeof(nm_entry->nm_name));
714 1.1 itojun }
715 1.1 itojun LIST_INSERT_HEAD(list, nm_entry, nm_entry);
716 1.1 itojun return (nm_entry);
717 1.1 itojun }
718 1.1 itojun
719 1.1 itojun
720 1.1 itojun void
721 1.1 itojun print_name_list(int opts, struct name_list *nml, const char *prefix)
722 1.1 itojun {
723 1.1 itojun char newprefix[32];
724 1.1 itojun struct name_entry *nm;
725 1.1 itojun
726 1.1 itojun LIST_FOREACH(nm, nml, nm_entry) {
727 1.1 itojun snprintf(newprefix, sizeof(newprefix), "%s%s\t", prefix,
728 1.1 itojun nm->nm_name);
729 1.1 itojun printf("%s\n", newprefix);
730 1.1 itojun print_name_list(opts, &nm->nm_sublist, newprefix);
731 1.1 itojun }
732 1.1 itojun }
733 1.1 itojun
734 1.1 itojun void
735 1.1 itojun sort_name_list(int opts, struct name_list *nml)
736 1.1 itojun {
737 1.1 itojun struct name_list new;
738 1.1 itojun struct name_entry *nm, *nmsearch, *nmlast;
739 1.1 itojun
740 1.1 itojun /* yes yes, it's a very slow sort. so sue me */
741 1.1 itojun
742 1.1 itojun LIST_INIT(&new);
743 1.1 itojun
744 1.1 itojun while ((nm = LIST_FIRST(nml)) != NULL) {
745 1.1 itojun LIST_REMOVE(nm, nm_entry);
746 1.1 itojun nmlast = NULL;
747 1.1 itojun LIST_FOREACH(nmsearch, &new, nm_entry) {
748 1.1 itojun if (strcasecmp(nmsearch->nm_name, nm->nm_name) > 0) {
749 1.1 itojun LIST_INSERT_BEFORE(nmsearch, nm, nm_entry);
750 1.1 itojun break;
751 1.1 itojun }
752 1.1 itojun nmlast = nmsearch;
753 1.1 itojun }
754 1.1 itojun if (nmsearch == NULL) {
755 1.1 itojun if (nmlast)
756 1.1 itojun LIST_INSERT_AFTER(nmlast, nm, nm_entry);
757 1.1 itojun else
758 1.1 itojun LIST_INSERT_HEAD(&new, nm, nm_entry);
759 1.1 itojun }
760 1.1 itojun
761 1.1 itojun sort_name_list(opts, &nm->nm_sublist);
762 1.1 itojun }
763 1.1 itojun nmlast = NULL;
764 1.1 itojun while ((nm = LIST_FIRST(&new)) != NULL) {
765 1.1 itojun LIST_REMOVE(nm, nm_entry);
766 1.1 itojun if (nmlast == NULL)
767 1.1 itojun LIST_INSERT_HEAD(nml, nm, nm_entry);
768 1.1 itojun else
769 1.1 itojun LIST_INSERT_AFTER(nmlast, nm, nm_entry);
770 1.1 itojun nmlast = nm;
771 1.1 itojun }
772 1.1 itojun return;
773 1.1 itojun }
774 1.1 itojun
775 1.1 itojun /* parse the next integer in a formatted config file line */
776 1.1 itojun int
777 1.1 itojun get_int(char **line, size_t *len, int *var, int *mod,
778 1.1 itojun const char *name, int flags, int max, const char *filename, int lineno)
779 1.1 itojun {
780 1.1 itojun int fieldlen, i;
781 1.1 itojun char *field;
782 1.1 itojun long val = 0;
783 1.1 itojun
784 1.1 itojun if (mod)
785 1.1 itojun *mod = 0;
786 1.1 itojun *var = 0;
787 1.1 itojun
788 1.1 itojun field = get_field(line, len, &fieldlen);
789 1.1 itojun if (field == NULL)
790 1.1 itojun return (1);
791 1.1 itojun if (fieldlen == 0) {
792 1.1 itojun fprintf(stderr, "%s:%d empty %s\n", filename, lineno, name);
793 1.1 itojun return (1);
794 1.1 itojun }
795 1.1 itojun
796 1.1 itojun i = 0;
797 1.1 itojun if ((*field == '%' || *field == 'S' || *field == 'T' || *field == '*')
798 1.1 itojun && fieldlen >= 1) {
799 1.1 itojun switch (*field) {
800 1.1 itojun case 'S':
801 1.1 itojun if (mod && (flags & T_MSS))
802 1.1 itojun *mod = T_MSS;
803 1.1 itojun if (fieldlen == 1)
804 1.1 itojun return (0);
805 1.1 itojun break;
806 1.1 itojun case 'T':
807 1.1 itojun if (mod && (flags & T_MTU))
808 1.1 itojun *mod = T_MTU;
809 1.1 itojun if (fieldlen == 1)
810 1.1 itojun return (0);
811 1.1 itojun break;
812 1.1 itojun case '*':
813 1.1 itojun if (fieldlen != 1) {
814 1.1 itojun fprintf(stderr, "%s:%d long '%c' %s\n",
815 1.1 itojun filename, lineno, *field, name);
816 1.1 itojun return (1);
817 1.1 itojun }
818 1.1 itojun if (mod && (flags & T_DC)) {
819 1.1 itojun *mod = T_DC;
820 1.1 itojun return (0);
821 1.1 itojun }
822 1.1 itojun case '%':
823 1.1 itojun if (mod && (flags & T_MOD))
824 1.1 itojun *mod = T_MOD;
825 1.1 itojun if (fieldlen == 1) {
826 1.1 itojun fprintf(stderr, "%s:%d modulus %s must have a "
827 1.1 itojun "value\n", filename, lineno, name);
828 1.1 itojun return (1);
829 1.1 itojun }
830 1.1 itojun break;
831 1.1 itojun }
832 1.1 itojun if (mod == NULL || *mod == 0) {
833 1.1 itojun fprintf(stderr, "%s:%d does not allow %c' %s\n",
834 1.1 itojun filename, lineno, *field, name);
835 1.1 itojun return (1);
836 1.1 itojun }
837 1.1 itojun i++;
838 1.1 itojun }
839 1.1 itojun
840 1.1 itojun for (; i < fieldlen; i++) {
841 1.1 itojun if (field[i] < '0' || field[i] > '9') {
842 1.1 itojun fprintf(stderr, "%s:%d non-digit character in %s\n",
843 1.1 itojun filename, lineno, name);
844 1.1 itojun return (1);
845 1.1 itojun }
846 1.1 itojun val = val * 10 + field[i] - '0';
847 1.1 itojun if (val < 0) {
848 1.1 itojun fprintf(stderr, "%s:%d %s overflowed\n", filename,
849 1.1 itojun lineno, name);
850 1.1 itojun return (1);
851 1.1 itojun }
852 1.1 itojun }
853 1.1 itojun
854 1.1 itojun if (val > max) {
855 1.1 itojun fprintf(stderr, "%s:%d %s value %ld > %d\n", filename, lineno,
856 1.1 itojun name, val, max);
857 1.1 itojun return (1);
858 1.1 itojun }
859 1.1 itojun *var = (int)val;
860 1.1 itojun
861 1.1 itojun return (0);
862 1.1 itojun }
863 1.1 itojun
864 1.1 itojun /* parse the next string in a formatted config file line */
865 1.1 itojun int
866 1.1 itojun get_str(char **line, size_t *len, char **v, const char *name, int minlen,
867 1.1 itojun const char *filename, int lineno)
868 1.1 itojun {
869 1.1 itojun int fieldlen;
870 1.1 itojun char *ptr;
871 1.1 itojun
872 1.1 itojun ptr = get_field(line, len, &fieldlen);
873 1.1 itojun if (ptr == NULL)
874 1.1 itojun return (1);
875 1.1 itojun if (fieldlen < minlen) {
876 1.1 itojun fprintf(stderr, "%s:%d too short %s\n", filename, lineno, name);
877 1.1 itojun return (1);
878 1.1 itojun }
879 1.1 itojun if ((*v = malloc(fieldlen + 1)) == NULL) {
880 1.1 itojun perror("malloc()");
881 1.1 itojun return (1);
882 1.1 itojun }
883 1.1 itojun memcpy(*v, ptr, fieldlen);
884 1.1 itojun (*v)[fieldlen] = '\0';
885 1.1 itojun
886 1.1 itojun return (0);
887 1.1 itojun }
888 1.1 itojun
889 1.1 itojun /* Parse out the TCP opts */
890 1.1 itojun int
891 1.1 itojun get_tcpopts(const char *filename, int lineno, const char *tcpopts,
892 1.1 itojun pf_tcpopts_t *packed, int *optcnt, int *mss, int *mss_mod, int *wscale,
893 1.1 itojun int *wscale_mod, int *ts0)
894 1.1 itojun {
895 1.1 itojun int i, opt;
896 1.1 itojun
897 1.1 itojun *packed = 0;
898 1.1 itojun *optcnt = 0;
899 1.1 itojun *wscale = 0;
900 1.1 itojun *wscale_mod = T_DC;
901 1.1 itojun *mss = 0;
902 1.1 itojun *mss_mod = T_DC;
903 1.1 itojun *ts0 = 0;
904 1.1 itojun if (strcmp(tcpopts, ".") == 0)
905 1.1 itojun return (0);
906 1.1 itojun
907 1.1 itojun for (i = 0; tcpopts[i] && *optcnt < PF_OSFP_MAX_OPTS;) {
908 1.3 dsl switch ((opt = toupper((unsigned char)tcpopts[i++]))) {
909 1.1 itojun case 'N': /* FALLTHROUGH */
910 1.1 itojun case 'S':
911 1.1 itojun *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
912 1.1 itojun (opt == 'N' ? PF_OSFP_TCPOPT_NOP :
913 1.1 itojun PF_OSFP_TCPOPT_SACK);
914 1.1 itojun break;
915 1.1 itojun case 'W': /* FALLTHROUGH */
916 1.1 itojun case 'M': {
917 1.1 itojun int *this_mod, *this;
918 1.1 itojun
919 1.1 itojun if (opt == 'W') {
920 1.1 itojun this = wscale;
921 1.1 itojun this_mod = wscale_mod;
922 1.1 itojun } else {
923 1.1 itojun this = mss;
924 1.1 itojun this_mod = mss_mod;
925 1.1 itojun }
926 1.1 itojun *this = 0;
927 1.1 itojun *this_mod = 0;
928 1.1 itojun
929 1.1 itojun *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
930 1.1 itojun (opt == 'W' ? PF_OSFP_TCPOPT_WSCALE :
931 1.1 itojun PF_OSFP_TCPOPT_MSS);
932 1.1 itojun if (tcpopts[i] == '*' && (tcpopts[i + 1] == '\0' ||
933 1.1 itojun tcpopts[i + 1] == ',')) {
934 1.1 itojun *this_mod = T_DC;
935 1.1 itojun i++;
936 1.1 itojun break;
937 1.1 itojun }
938 1.1 itojun
939 1.1 itojun if (tcpopts[i] == '%') {
940 1.1 itojun *this_mod = T_MOD;
941 1.1 itojun i++;
942 1.1 itojun }
943 1.1 itojun do {
944 1.3 dsl if (!isdigit((unsigned char)tcpopts[i])) {
945 1.1 itojun fprintf(stderr, "%s:%d unknown "
946 1.1 itojun "character '%c' in %c TCP opt\n",
947 1.1 itojun filename, lineno, tcpopts[i], opt);
948 1.1 itojun return (1);
949 1.1 itojun }
950 1.1 itojun *this = (*this * 10) + tcpopts[i++] - '0';
951 1.1 itojun } while(tcpopts[i] != ',' && tcpopts[i] != '\0');
952 1.1 itojun break;
953 1.1 itojun }
954 1.1 itojun case 'T':
955 1.1 itojun if (tcpopts[i] == '0') {
956 1.1 itojun *ts0 = 1;
957 1.1 itojun i++;
958 1.1 itojun }
959 1.1 itojun *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
960 1.1 itojun PF_OSFP_TCPOPT_TS;
961 1.1 itojun break;
962 1.1 itojun }
963 1.1 itojun (*optcnt) ++;
964 1.1 itojun if (tcpopts[i] == '\0')
965 1.1 itojun break;
966 1.1 itojun if (tcpopts[i] != ',') {
967 1.1 itojun fprintf(stderr, "%s:%d unknown option to %c TCP opt\n",
968 1.1 itojun filename, lineno, opt);
969 1.1 itojun return (1);
970 1.1 itojun }
971 1.1 itojun i++;
972 1.1 itojun }
973 1.1 itojun
974 1.1 itojun return (0);
975 1.1 itojun }
976 1.1 itojun
977 1.1 itojun /* rip the next field ouf of a formatted config file line */
978 1.1 itojun char *
979 1.1 itojun get_field(char **line, size_t *len, int *fieldlen)
980 1.1 itojun {
981 1.1 itojun char *ret, *ptr = *line;
982 1.1 itojun size_t plen = *len;
983 1.1 itojun
984 1.1 itojun
985 1.3 dsl while (plen && isspace((unsigned char)*ptr)) {
986 1.1 itojun plen--;
987 1.1 itojun ptr++;
988 1.1 itojun }
989 1.1 itojun ret = ptr;
990 1.1 itojun *fieldlen = 0;
991 1.1 itojun
992 1.1 itojun for (; plen > 0 && *ptr != ':'; plen--, ptr++)
993 1.1 itojun (*fieldlen)++;
994 1.1 itojun if (plen) {
995 1.1 itojun *line = ptr + 1;
996 1.1 itojun *len = plen - 1;
997 1.1 itojun } else {
998 1.1 itojun *len = 0;
999 1.1 itojun }
1000 1.3 dsl while (*fieldlen && isspace((unsigned char)ret[*fieldlen - 1]))
1001 1.1 itojun (*fieldlen)--;
1002 1.1 itojun return (ret);
1003 1.1 itojun }
1004 1.1 itojun
1005 1.1 itojun
1006 1.1 itojun const char *
1007 1.1 itojun print_ioctl(struct pf_osfp_ioctl *fp)
1008 1.1 itojun {
1009 1.1 itojun static char buf[1024];
1010 1.1 itojun char tmp[32];
1011 1.1 itojun int i, opt;
1012 1.1 itojun
1013 1.1 itojun *buf = '\0';
1014 1.1 itojun if (fp->fp_flags & PF_OSFP_WSIZE_DC)
1015 1.1 itojun strlcat(buf, "*", sizeof(buf));
1016 1.1 itojun else if (fp->fp_flags & PF_OSFP_WSIZE_MSS)
1017 1.1 itojun strlcat(buf, "S", sizeof(buf));
1018 1.1 itojun else if (fp->fp_flags & PF_OSFP_WSIZE_MTU)
1019 1.1 itojun strlcat(buf, "T", sizeof(buf));
1020 1.1 itojun else {
1021 1.1 itojun if (fp->fp_flags & PF_OSFP_WSIZE_MOD)
1022 1.1 itojun strlcat(buf, "%", sizeof(buf));
1023 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_wsize);
1024 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1025 1.1 itojun }
1026 1.1 itojun strlcat(buf, ":", sizeof(buf));
1027 1.1 itojun
1028 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_ttl);
1029 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1030 1.1 itojun strlcat(buf, ":", sizeof(buf));
1031 1.1 itojun
1032 1.1 itojun if (fp->fp_flags & PF_OSFP_DF)
1033 1.1 itojun strlcat(buf, "1", sizeof(buf));
1034 1.1 itojun else
1035 1.1 itojun strlcat(buf, "0", sizeof(buf));
1036 1.1 itojun strlcat(buf, ":", sizeof(buf));
1037 1.1 itojun
1038 1.1 itojun if (fp->fp_flags & PF_OSFP_PSIZE_DC)
1039 1.1 itojun strlcat(buf, "*", sizeof(buf));
1040 1.1 itojun else {
1041 1.1 itojun if (fp->fp_flags & PF_OSFP_PSIZE_MOD)
1042 1.1 itojun strlcat(buf, "%", sizeof(buf));
1043 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_psize);
1044 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1045 1.1 itojun }
1046 1.1 itojun strlcat(buf, ":", sizeof(buf));
1047 1.1 itojun
1048 1.1 itojun if (fp->fp_optcnt == 0)
1049 1.1 itojun strlcat(buf, ".", sizeof(buf));
1050 1.1 itojun for (i = fp->fp_optcnt - 1; i >= 0; i--) {
1051 1.1 itojun opt = fp->fp_tcpopts >> (i * PF_OSFP_TCPOPT_BITS);
1052 1.1 itojun opt &= (1 << PF_OSFP_TCPOPT_BITS) - 1;
1053 1.1 itojun switch (opt) {
1054 1.1 itojun case PF_OSFP_TCPOPT_NOP:
1055 1.1 itojun strlcat(buf, "N", sizeof(buf));
1056 1.1 itojun break;
1057 1.1 itojun case PF_OSFP_TCPOPT_SACK:
1058 1.1 itojun strlcat(buf, "S", sizeof(buf));
1059 1.1 itojun break;
1060 1.1 itojun case PF_OSFP_TCPOPT_TS:
1061 1.1 itojun strlcat(buf, "T", sizeof(buf));
1062 1.1 itojun if (fp->fp_flags & PF_OSFP_TS0)
1063 1.1 itojun strlcat(buf, "0", sizeof(buf));
1064 1.1 itojun break;
1065 1.1 itojun case PF_OSFP_TCPOPT_MSS:
1066 1.1 itojun strlcat(buf, "M", sizeof(buf));
1067 1.1 itojun if (fp->fp_flags & PF_OSFP_MSS_DC)
1068 1.1 itojun strlcat(buf, "*", sizeof(buf));
1069 1.1 itojun else {
1070 1.1 itojun if (fp->fp_flags & PF_OSFP_MSS_MOD)
1071 1.1 itojun strlcat(buf, "%", sizeof(buf));
1072 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_mss);
1073 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1074 1.1 itojun }
1075 1.1 itojun break;
1076 1.1 itojun case PF_OSFP_TCPOPT_WSCALE:
1077 1.1 itojun strlcat(buf, "W", sizeof(buf));
1078 1.1 itojun if (fp->fp_flags & PF_OSFP_WSCALE_DC)
1079 1.1 itojun strlcat(buf, "*", sizeof(buf));
1080 1.1 itojun else {
1081 1.1 itojun if (fp->fp_flags & PF_OSFP_WSCALE_MOD)
1082 1.1 itojun strlcat(buf, "%", sizeof(buf));
1083 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_wscale);
1084 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1085 1.1 itojun }
1086 1.1 itojun break;
1087 1.1 itojun }
1088 1.1 itojun
1089 1.1 itojun if (i != 0)
1090 1.1 itojun strlcat(buf, ",", sizeof(buf));
1091 1.1 itojun }
1092 1.1 itojun strlcat(buf, ":", sizeof(buf));
1093 1.1 itojun
1094 1.1 itojun strlcat(buf, fp->fp_os.fp_class_nm, sizeof(buf));
1095 1.1 itojun strlcat(buf, ":", sizeof(buf));
1096 1.1 itojun strlcat(buf, fp->fp_os.fp_version_nm, sizeof(buf));
1097 1.1 itojun strlcat(buf, ":", sizeof(buf));
1098 1.1 itojun strlcat(buf, fp->fp_os.fp_subtype_nm, sizeof(buf));
1099 1.1 itojun strlcat(buf, ":", sizeof(buf));
1100 1.1 itojun
1101 1.1 itojun snprintf(tmp, sizeof(tmp), "TcpOpts %d 0x%llx", fp->fp_optcnt,
1102 1.1 itojun (long long int)fp->fp_tcpopts);
1103 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1104 1.1 itojun
1105 1.1 itojun return (buf);
1106 1.1 itojun }
1107