pfctl_osfp.c revision 1.3 1 1.3 dsl /* $NetBSD: pfctl_osfp.c,v 1.3 2004/10/29 19:46:27 dsl Exp $ */
2 1.1 itojun /* $OpenBSD: pfctl_osfp.c,v 1.8 2004/02/27 10:42:00 henning 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.1 itojun if ((in = fopen(fp_filename, "r")) == NULL) {
106 1.1 itojun warn("fopen(%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.1 itojun
259 1.1 itojun fclose(in);
260 1.1 itojun
261 1.1 itojun if (opts & PF_OPT_VERBOSE2)
262 1.1 itojun printf("Loaded %d passive OS fingerprints\n",
263 1.1 itojun fingerprint_count);
264 1.1 itojun return (0);
265 1.1 itojun }
266 1.1 itojun
267 1.1 itojun /* flush the kernel's fingerprints */
268 1.1 itojun void
269 1.1 itojun pfctl_clear_fingerprints(int dev, int opts)
270 1.1 itojun {
271 1.1 itojun if (ioctl(dev, DIOCOSFPFLUSH))
272 1.1 itojun err(1, "DIOCOSFPFLUSH");
273 1.1 itojun }
274 1.1 itojun
275 1.1 itojun /* flush pfctl's view of the fingerprints */
276 1.1 itojun void
277 1.1 itojun pfctl_flush_my_fingerprints(struct name_list *list)
278 1.1 itojun {
279 1.1 itojun struct name_entry *nm;
280 1.1 itojun
281 1.1 itojun while ((nm = LIST_FIRST(list)) != NULL) {
282 1.1 itojun LIST_REMOVE(nm, nm_entry);
283 1.1 itojun pfctl_flush_my_fingerprints(&nm->nm_sublist);
284 1.1 itojun fingerprint_count--;
285 1.1 itojun free(nm);
286 1.1 itojun }
287 1.1 itojun class_count = 0;
288 1.1 itojun }
289 1.1 itojun
290 1.1 itojun /* Fetch the active fingerprints from the kernel */
291 1.1 itojun int
292 1.1 itojun pfctl_load_fingerprints(int dev, int opts)
293 1.1 itojun {
294 1.1 itojun struct pf_osfp_ioctl io;
295 1.1 itojun int i;
296 1.1 itojun
297 1.1 itojun pfctl_flush_my_fingerprints(&classes);
298 1.1 itojun
299 1.1 itojun for (i = 0; i >= 0; i++) {
300 1.1 itojun memset(&io, 0, sizeof(io));
301 1.1 itojun io.fp_getnum = i;
302 1.1 itojun if (ioctl(dev, DIOCOSFPGET, &io)) {
303 1.1 itojun if (errno == EBUSY)
304 1.1 itojun break;
305 1.1 itojun warn("DIOCOSFPGET");
306 1.1 itojun return (1);
307 1.1 itojun }
308 1.1 itojun import_fingerprint(&io);
309 1.1 itojun }
310 1.1 itojun return (0);
311 1.1 itojun }
312 1.1 itojun
313 1.1 itojun /* List the fingerprints */
314 1.1 itojun void
315 1.1 itojun pfctl_show_fingerprints(int opts)
316 1.1 itojun {
317 1.1 itojun if (LIST_FIRST(&classes) != NULL) {
318 1.1 itojun if (opts & PF_OPT_SHOWALL) {
319 1.1 itojun pfctl_print_title("OS FINGERPRINTS:");
320 1.1 itojun printf("%u fingerprints loaded\n", fingerprint_count);
321 1.1 itojun } else {
322 1.1 itojun printf("Class\tVersion\tSubtype(subversion)\n");
323 1.1 itojun printf("-----\t-------\t-------------------\n");
324 1.1 itojun sort_name_list(opts, &classes);
325 1.1 itojun print_name_list(opts, &classes, "");
326 1.1 itojun }
327 1.1 itojun }
328 1.1 itojun }
329 1.1 itojun
330 1.1 itojun /* Lookup a fingerprint */
331 1.1 itojun pf_osfp_t
332 1.1 itojun pfctl_get_fingerprint(const char *name)
333 1.1 itojun {
334 1.1 itojun struct name_entry *nm, *class_nm, *version_nm, *subtype_nm;
335 1.1 itojun pf_osfp_t ret = PF_OSFP_NOMATCH;
336 1.1 itojun int class, version, subtype;
337 1.1 itojun int unp_class, unp_version, unp_subtype;
338 1.1 itojun int wr_len, version_len, subtype_len;
339 1.1 itojun char *ptr, *wr_name;
340 1.1 itojun
341 1.1 itojun if (strcasecmp(name, "unknown") == 0)
342 1.1 itojun return (PF_OSFP_UNKNOWN);
343 1.1 itojun
344 1.1 itojun /* Try most likely no version and no subtype */
345 1.1 itojun if ((nm = lookup_name_list(&classes, name))) {
346 1.1 itojun class = nm->nm_num;
347 1.1 itojun version = PF_OSFP_ANY;
348 1.1 itojun subtype = PF_OSFP_ANY;
349 1.1 itojun goto found;
350 1.1 itojun } else {
351 1.1 itojun
352 1.1 itojun /* Chop it up into class/version/subtype */
353 1.1 itojun
354 1.1 itojun if ((wr_name = strdup(name)) == NULL)
355 1.1 itojun err(1, "malloc");
356 1.1 itojun if ((ptr = index(wr_name, ' ')) == NULL) {
357 1.1 itojun free(wr_name);
358 1.1 itojun return (PF_OSFP_NOMATCH);
359 1.1 itojun }
360 1.1 itojun *ptr++ = '\0';
361 1.1 itojun
362 1.1 itojun /* The class is easy to find since it is delimited by a space */
363 1.1 itojun if ((class_nm = lookup_name_list(&classes, wr_name)) == NULL) {
364 1.1 itojun free(wr_name);
365 1.1 itojun return (PF_OSFP_NOMATCH);
366 1.1 itojun }
367 1.1 itojun class = class_nm->nm_num;
368 1.1 itojun
369 1.1 itojun /* Try no subtype */
370 1.1 itojun if ((version_nm = lookup_name_list(&class_nm->nm_sublist, ptr)))
371 1.1 itojun {
372 1.1 itojun version = version_nm->nm_num;
373 1.1 itojun subtype = PF_OSFP_ANY;
374 1.1 itojun free(wr_name);
375 1.1 itojun goto found;
376 1.1 itojun }
377 1.1 itojun
378 1.1 itojun
379 1.1 itojun /*
380 1.1 itojun * There must be a version and a subtype.
381 1.1 itojun * We'll do some fuzzy matching to pick up things like:
382 1.1 itojun * Linux 2.2.14 (version=2.2 subtype=14)
383 1.1 itojun * FreeBSD 4.0-STABLE (version=4.0 subtype=STABLE)
384 1.1 itojun * Windows 2000 SP2 (version=2000 subtype=SP2)
385 1.1 itojun */
386 1.1 itojun #define CONNECTOR(x) ((x) == '.' || (x) == ' ' || (x) == '\t' || (x) == '-')
387 1.1 itojun wr_len = strlen(ptr);
388 1.1 itojun LIST_FOREACH(version_nm, &class_nm->nm_sublist, nm_entry) {
389 1.1 itojun version_len = strlen(version_nm->nm_name);
390 1.1 itojun if (wr_len < version_len + 2 ||
391 1.1 itojun !CONNECTOR(ptr[version_len]))
392 1.1 itojun continue;
393 1.1 itojun /* first part of the string must be version */
394 1.1 itojun if (strncasecmp(ptr, version_nm->nm_name,
395 1.1 itojun version_len))
396 1.1 itojun continue;
397 1.1 itojun
398 1.1 itojun LIST_FOREACH(subtype_nm, &version_nm->nm_sublist,
399 1.1 itojun nm_entry) {
400 1.1 itojun subtype_len = strlen(subtype_nm->nm_name);
401 1.1 itojun if (wr_len != version_len + subtype_len + 1)
402 1.1 itojun continue;
403 1.1 itojun
404 1.1 itojun /* last part of the string must be subtype */
405 1.1 itojun if (strcasecmp(&ptr[version_len+1],
406 1.1 itojun subtype_nm->nm_name) != 0)
407 1.1 itojun continue;
408 1.1 itojun
409 1.1 itojun /* Found it!! */
410 1.1 itojun version = version_nm->nm_num;
411 1.1 itojun subtype = subtype_nm->nm_num;
412 1.1 itojun free(wr_name);
413 1.1 itojun goto found;
414 1.1 itojun }
415 1.1 itojun }
416 1.1 itojun
417 1.1 itojun free(wr_name);
418 1.1 itojun return (PF_OSFP_NOMATCH);
419 1.1 itojun }
420 1.1 itojun
421 1.1 itojun found:
422 1.1 itojun PF_OSFP_PACK(ret, class, version, subtype);
423 1.1 itojun if (ret != PF_OSFP_NOMATCH) {
424 1.1 itojun PF_OSFP_UNPACK(ret, unp_class, unp_version, unp_subtype);
425 1.1 itojun if (class != unp_class) {
426 1.1 itojun fprintf(stderr, "warning: fingerprint table overflowed "
427 1.1 itojun "classes\n");
428 1.1 itojun return (PF_OSFP_NOMATCH);
429 1.1 itojun }
430 1.1 itojun if (version != unp_version) {
431 1.1 itojun fprintf(stderr, "warning: fingerprint table overflowed "
432 1.1 itojun "versions\n");
433 1.1 itojun return (PF_OSFP_NOMATCH);
434 1.1 itojun }
435 1.1 itojun if (subtype != unp_subtype) {
436 1.1 itojun fprintf(stderr, "warning: fingerprint table overflowed "
437 1.1 itojun "subtypes\n");
438 1.1 itojun return (PF_OSFP_NOMATCH);
439 1.1 itojun }
440 1.1 itojun }
441 1.1 itojun if (ret == PF_OSFP_ANY) {
442 1.1 itojun /* should never happen */
443 1.1 itojun fprintf(stderr, "warning: fingerprint packed to 'any'\n");
444 1.1 itojun return (PF_OSFP_NOMATCH);
445 1.1 itojun }
446 1.1 itojun
447 1.1 itojun return (ret);
448 1.1 itojun }
449 1.1 itojun
450 1.1 itojun /* Lookup a fingerprint name by ID */
451 1.1 itojun char *
452 1.1 itojun pfctl_lookup_fingerprint(pf_osfp_t fp, char *buf, size_t len)
453 1.1 itojun {
454 1.1 itojun int class, version, subtype;
455 1.1 itojun struct name_list *list;
456 1.1 itojun struct name_entry *nm;
457 1.1 itojun
458 1.1 itojun char *class_name, *version_name, *subtype_name;
459 1.1 itojun class_name = version_name = subtype_name = NULL;
460 1.1 itojun
461 1.1 itojun if (fp == PF_OSFP_UNKNOWN) {
462 1.1 itojun strlcpy(buf, "unknown", len);
463 1.1 itojun return (buf);
464 1.1 itojun }
465 1.1 itojun if (fp == PF_OSFP_ANY) {
466 1.1 itojun strlcpy(buf, "any", len);
467 1.1 itojun return (buf);
468 1.1 itojun }
469 1.1 itojun
470 1.1 itojun PF_OSFP_UNPACK(fp, class, version, subtype);
471 1.1 itojun if (class >= (1 << _FP_CLASS_BITS) ||
472 1.1 itojun version >= (1 << _FP_VERSION_BITS) ||
473 1.1 itojun subtype >= (1 << _FP_SUBTYPE_BITS)) {
474 1.1 itojun warnx("PF_OSFP_UNPACK(0x%x) failed!!", fp);
475 1.1 itojun strlcpy(buf, "nomatch", len);
476 1.1 itojun return (buf);
477 1.1 itojun }
478 1.1 itojun
479 1.1 itojun LIST_FOREACH(nm, &classes, nm_entry) {
480 1.1 itojun if (nm->nm_num == class) {
481 1.1 itojun class_name = nm->nm_name;
482 1.1 itojun if (version == PF_OSFP_ANY)
483 1.1 itojun goto found;
484 1.1 itojun list = &nm->nm_sublist;
485 1.1 itojun LIST_FOREACH(nm, list, nm_entry) {
486 1.1 itojun if (nm->nm_num == version) {
487 1.1 itojun version_name = nm->nm_name;
488 1.1 itojun if (subtype == PF_OSFP_ANY)
489 1.1 itojun goto found;
490 1.1 itojun list = &nm->nm_sublist;
491 1.1 itojun LIST_FOREACH(nm, list, nm_entry) {
492 1.1 itojun if (nm->nm_num == subtype) {
493 1.1 itojun subtype_name =
494 1.1 itojun nm->nm_name;
495 1.1 itojun goto found;
496 1.1 itojun }
497 1.1 itojun } /* foreach subtype */
498 1.1 itojun strlcpy(buf, "nomatch", len);
499 1.1 itojun return (buf);
500 1.1 itojun }
501 1.1 itojun } /* foreach version */
502 1.1 itojun strlcpy(buf, "nomatch", len);
503 1.1 itojun return (buf);
504 1.1 itojun }
505 1.1 itojun } /* foreach class */
506 1.1 itojun
507 1.1 itojun strlcpy(buf, "nomatch", len);
508 1.1 itojun return (buf);
509 1.1 itojun
510 1.1 itojun found:
511 1.1 itojun snprintf(buf, len, "%s", class_name);
512 1.1 itojun if (version_name) {
513 1.1 itojun strlcat(buf, " ", len);
514 1.1 itojun strlcat(buf, version_name, len);
515 1.1 itojun if (subtype_name) {
516 1.1 itojun if (index(version_name, ' '))
517 1.1 itojun strlcat(buf, " ", len);
518 1.1 itojun else if (index(version_name, '.') &&
519 1.3 dsl isdigit((unsigned char)*subtype_name))
520 1.1 itojun strlcat(buf, ".", len);
521 1.1 itojun else
522 1.1 itojun strlcat(buf, " ", len);
523 1.1 itojun strlcat(buf, subtype_name, len);
524 1.1 itojun }
525 1.1 itojun }
526 1.1 itojun return (buf);
527 1.1 itojun }
528 1.1 itojun
529 1.1 itojun /* lookup a name in a list */
530 1.1 itojun struct name_entry *
531 1.1 itojun lookup_name_list(struct name_list *list, const char *name)
532 1.1 itojun {
533 1.1 itojun struct name_entry *nm;
534 1.1 itojun LIST_FOREACH(nm, list, nm_entry)
535 1.1 itojun if (strcasecmp(name, nm->nm_name) == 0)
536 1.1 itojun return (nm);
537 1.1 itojun
538 1.1 itojun return (NULL);
539 1.1 itojun }
540 1.1 itojun
541 1.1 itojun
542 1.1 itojun void
543 1.1 itojun add_fingerprint(int dev, int opts, struct pf_osfp_ioctl *fp)
544 1.1 itojun {
545 1.1 itojun struct pf_osfp_ioctl fptmp;
546 1.1 itojun struct name_entry *nm_class, *nm_version, *nm_subtype;
547 1.1 itojun int class, version, subtype;
548 1.1 itojun
549 1.1 itojun /* We expand #-# or #.#-#.# version/subtypes into multiple fingerprints */
550 1.1 itojun #define EXPAND(field) do { \
551 1.1 itojun int _dot = -1, _start = -1, _end = -1, _i = 0; \
552 1.1 itojun /* pick major version out of #.# */ \
553 1.3 dsl if (isdigit((unsigned char)fp->field[_i]) && fp->field[_i+1] == '.') { \
554 1.1 itojun _dot = fp->field[_i] - '0'; \
555 1.1 itojun _i += 2; \
556 1.1 itojun } \
557 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
558 1.1 itojun _start = fp->field[_i++] - '0'; \
559 1.1 itojun else \
560 1.1 itojun break; \
561 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
562 1.1 itojun _start = (_start * 10) + fp->field[_i++] - '0'; \
563 1.1 itojun if (fp->field[_i++] != '-') \
564 1.1 itojun break; \
565 1.3 dsl if (isdigit((unsigned char)fp->field[_i]) && fp->field[_i+1] == '.' && \
566 1.1 itojun fp->field[_i] - '0' == _dot) \
567 1.1 itojun _i += 2; \
568 1.1 itojun else if (_dot != -1) \
569 1.1 itojun break; \
570 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
571 1.1 itojun _end = fp->field[_i++] - '0'; \
572 1.1 itojun else \
573 1.1 itojun break; \
574 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
575 1.1 itojun _end = (_end * 10) + fp->field[_i++] - '0'; \
576 1.3 dsl if (isdigit((unsigned char)fp->field[_i])) \
577 1.1 itojun _end = (_end * 10) + fp->field[_i++] - '0'; \
578 1.1 itojun if (fp->field[_i] != '\0') \
579 1.1 itojun break; \
580 1.1 itojun memcpy(&fptmp, fp, sizeof(fptmp)); \
581 1.1 itojun for (;_start <= _end; _start++) { \
582 1.1 itojun memset(fptmp.field, 0, sizeof(fptmp.field)); \
583 1.1 itojun fptmp.fp_os.fp_enflags |= PF_OSFP_EXPANDED; \
584 1.1 itojun if (_dot == -1) \
585 1.1 itojun snprintf(fptmp.field, sizeof(fptmp.field), \
586 1.1 itojun "%d", _start); \
587 1.1 itojun else \
588 1.1 itojun snprintf(fptmp.field, sizeof(fptmp.field), \
589 1.1 itojun "%d.%d", _dot, _start); \
590 1.1 itojun add_fingerprint(dev, opts, &fptmp); \
591 1.1 itojun } \
592 1.1 itojun } while(0)
593 1.1 itojun
594 1.1 itojun /* We allow "#-#" as a version or subtype and we'll expand it */
595 1.1 itojun EXPAND(fp_os.fp_version_nm);
596 1.1 itojun EXPAND(fp_os.fp_subtype_nm);
597 1.1 itojun
598 1.1 itojun if (strcasecmp(fp->fp_os.fp_class_nm, "nomatch") == 0)
599 1.1 itojun errx(1, "fingerprint class \"nomatch\" is reserved");
600 1.1 itojun
601 1.1 itojun version = PF_OSFP_ANY;
602 1.1 itojun subtype = PF_OSFP_ANY;
603 1.1 itojun
604 1.1 itojun nm_class = fingerprint_name_entry(&classes, fp->fp_os.fp_class_nm);
605 1.1 itojun if (nm_class->nm_num == 0)
606 1.1 itojun nm_class->nm_num = ++class_count;
607 1.1 itojun class = nm_class->nm_num;
608 1.1 itojun
609 1.1 itojun nm_version = fingerprint_name_entry(&nm_class->nm_sublist,
610 1.1 itojun fp->fp_os.fp_version_nm);
611 1.1 itojun if (nm_version) {
612 1.1 itojun if (nm_version->nm_num == 0)
613 1.1 itojun nm_version->nm_num = ++nm_class->nm_sublist_num;
614 1.1 itojun version = nm_version->nm_num;
615 1.1 itojun nm_subtype = fingerprint_name_entry(&nm_version->nm_sublist,
616 1.1 itojun fp->fp_os.fp_subtype_nm);
617 1.1 itojun if (nm_subtype) {
618 1.1 itojun if (nm_subtype->nm_num == 0)
619 1.1 itojun nm_subtype->nm_num =
620 1.1 itojun ++nm_version->nm_sublist_num;
621 1.1 itojun subtype = nm_subtype->nm_num;
622 1.1 itojun }
623 1.1 itojun }
624 1.1 itojun
625 1.1 itojun
626 1.1 itojun DEBUG(fp, "\tsignature %d:%d:%d %s", class, version, subtype,
627 1.1 itojun print_ioctl(fp));
628 1.1 itojun
629 1.1 itojun PF_OSFP_PACK(fp->fp_os.fp_os, class, version, subtype);
630 1.1 itojun fingerprint_count++;
631 1.1 itojun
632 1.1 itojun #ifdef FAKE_PF_KERNEL
633 1.1 itojun /* Linked to the sys/net/pf_osfp.c. Call pf_osfp_add() */
634 1.1 itojun if ((errno = pf_osfp_add(fp)))
635 1.1 itojun #else
636 1.1 itojun if ((opts & PF_OPT_NOACTION) == 0 && ioctl(dev, DIOCOSFPADD, fp))
637 1.1 itojun #endif /* FAKE_PF_KERNEL */
638 1.1 itojun {
639 1.1 itojun if (errno == EEXIST) {
640 1.1 itojun warn("Duplicate signature for %s %s %s",
641 1.1 itojun fp->fp_os.fp_class_nm,
642 1.1 itojun fp->fp_os.fp_version_nm,
643 1.1 itojun fp->fp_os.fp_subtype_nm);
644 1.1 itojun
645 1.1 itojun } else {
646 1.1 itojun err(1, "DIOCOSFPADD");
647 1.1 itojun }
648 1.1 itojun }
649 1.1 itojun }
650 1.1 itojun
651 1.1 itojun /* import a fingerprint from the kernel */
652 1.1 itojun void
653 1.1 itojun import_fingerprint(struct pf_osfp_ioctl *fp)
654 1.1 itojun {
655 1.1 itojun struct name_entry *nm_class, *nm_version, *nm_subtype;
656 1.1 itojun int class, version, subtype;
657 1.1 itojun
658 1.1 itojun PF_OSFP_UNPACK(fp->fp_os.fp_os, class, version, subtype);
659 1.1 itojun
660 1.1 itojun nm_class = fingerprint_name_entry(&classes, fp->fp_os.fp_class_nm);
661 1.1 itojun if (nm_class->nm_num == 0) {
662 1.1 itojun nm_class->nm_num = class;
663 1.1 itojun class_count = MAX(class_count, class);
664 1.1 itojun }
665 1.1 itojun
666 1.1 itojun nm_version = fingerprint_name_entry(&nm_class->nm_sublist,
667 1.1 itojun fp->fp_os.fp_version_nm);
668 1.1 itojun if (nm_version) {
669 1.1 itojun if (nm_version->nm_num == 0) {
670 1.1 itojun nm_version->nm_num = version;
671 1.1 itojun nm_class->nm_sublist_num = MAX(nm_class->nm_sublist_num,
672 1.1 itojun version);
673 1.1 itojun }
674 1.1 itojun nm_subtype = fingerprint_name_entry(&nm_version->nm_sublist,
675 1.1 itojun fp->fp_os.fp_subtype_nm);
676 1.1 itojun if (nm_subtype) {
677 1.1 itojun if (nm_subtype->nm_num == 0) {
678 1.1 itojun nm_subtype->nm_num = subtype;
679 1.1 itojun nm_version->nm_sublist_num =
680 1.1 itojun MAX(nm_version->nm_sublist_num, subtype);
681 1.1 itojun }
682 1.1 itojun }
683 1.1 itojun }
684 1.1 itojun
685 1.1 itojun
686 1.1 itojun fingerprint_count++;
687 1.1 itojun DEBUG(fp, "import signature %d:%d:%d", class, version, subtype);
688 1.1 itojun }
689 1.1 itojun
690 1.1 itojun /* Find an entry for a fingerprints class/version/subtype */
691 1.1 itojun struct name_entry *
692 1.1 itojun fingerprint_name_entry(struct name_list *list, char *name)
693 1.1 itojun {
694 1.1 itojun struct name_entry *nm_entry;
695 1.1 itojun
696 1.1 itojun if (name == NULL || strlen(name) == 0)
697 1.1 itojun return (NULL);
698 1.1 itojun
699 1.1 itojun LIST_FOREACH(nm_entry, list, nm_entry) {
700 1.1 itojun if (strcasecmp(nm_entry->nm_name, name) == 0) {
701 1.1 itojun /* We'll move this to the front of the list later */
702 1.1 itojun LIST_REMOVE(nm_entry, nm_entry);
703 1.1 itojun break;
704 1.1 itojun }
705 1.1 itojun }
706 1.1 itojun if (nm_entry == NULL) {
707 1.1 itojun nm_entry = calloc(1, sizeof(*nm_entry));
708 1.1 itojun if (nm_entry == NULL)
709 1.1 itojun err(1, "calloc");
710 1.1 itojun LIST_INIT(&nm_entry->nm_sublist);
711 1.1 itojun strlcpy(nm_entry->nm_name, name,
712 1.1 itojun sizeof(nm_entry->nm_name));
713 1.1 itojun }
714 1.1 itojun LIST_INSERT_HEAD(list, nm_entry, nm_entry);
715 1.1 itojun return (nm_entry);
716 1.1 itojun }
717 1.1 itojun
718 1.1 itojun
719 1.1 itojun void
720 1.1 itojun print_name_list(int opts, struct name_list *nml, const char *prefix)
721 1.1 itojun {
722 1.1 itojun char newprefix[32];
723 1.1 itojun struct name_entry *nm;
724 1.1 itojun
725 1.1 itojun LIST_FOREACH(nm, nml, nm_entry) {
726 1.1 itojun snprintf(newprefix, sizeof(newprefix), "%s%s\t", prefix,
727 1.1 itojun nm->nm_name);
728 1.1 itojun printf("%s\n", newprefix);
729 1.1 itojun print_name_list(opts, &nm->nm_sublist, newprefix);
730 1.1 itojun }
731 1.1 itojun }
732 1.1 itojun
733 1.1 itojun void
734 1.1 itojun sort_name_list(int opts, struct name_list *nml)
735 1.1 itojun {
736 1.1 itojun struct name_list new;
737 1.1 itojun struct name_entry *nm, *nmsearch, *nmlast;
738 1.1 itojun
739 1.1 itojun /* yes yes, it's a very slow sort. so sue me */
740 1.1 itojun
741 1.1 itojun LIST_INIT(&new);
742 1.1 itojun
743 1.1 itojun while ((nm = LIST_FIRST(nml)) != NULL) {
744 1.1 itojun LIST_REMOVE(nm, nm_entry);
745 1.1 itojun nmlast = NULL;
746 1.1 itojun LIST_FOREACH(nmsearch, &new, nm_entry) {
747 1.1 itojun if (strcasecmp(nmsearch->nm_name, nm->nm_name) > 0) {
748 1.1 itojun LIST_INSERT_BEFORE(nmsearch, nm, nm_entry);
749 1.1 itojun break;
750 1.1 itojun }
751 1.1 itojun nmlast = nmsearch;
752 1.1 itojun }
753 1.1 itojun if (nmsearch == NULL) {
754 1.1 itojun if (nmlast)
755 1.1 itojun LIST_INSERT_AFTER(nmlast, nm, nm_entry);
756 1.1 itojun else
757 1.1 itojun LIST_INSERT_HEAD(&new, nm, nm_entry);
758 1.1 itojun }
759 1.1 itojun
760 1.1 itojun sort_name_list(opts, &nm->nm_sublist);
761 1.1 itojun }
762 1.1 itojun nmlast = NULL;
763 1.1 itojun while ((nm = LIST_FIRST(&new)) != NULL) {
764 1.1 itojun LIST_REMOVE(nm, nm_entry);
765 1.1 itojun if (nmlast == NULL)
766 1.1 itojun LIST_INSERT_HEAD(nml, nm, nm_entry);
767 1.1 itojun else
768 1.1 itojun LIST_INSERT_AFTER(nmlast, nm, nm_entry);
769 1.1 itojun nmlast = nm;
770 1.1 itojun }
771 1.1 itojun return;
772 1.1 itojun }
773 1.1 itojun
774 1.1 itojun /* parse the next integer in a formatted config file line */
775 1.1 itojun int
776 1.1 itojun get_int(char **line, size_t *len, int *var, int *mod,
777 1.1 itojun const char *name, int flags, int max, const char *filename, int lineno)
778 1.1 itojun {
779 1.1 itojun int fieldlen, i;
780 1.1 itojun char *field;
781 1.1 itojun long val = 0;
782 1.1 itojun
783 1.1 itojun if (mod)
784 1.1 itojun *mod = 0;
785 1.1 itojun *var = 0;
786 1.1 itojun
787 1.1 itojun field = get_field(line, len, &fieldlen);
788 1.1 itojun if (field == NULL)
789 1.1 itojun return (1);
790 1.1 itojun if (fieldlen == 0) {
791 1.1 itojun fprintf(stderr, "%s:%d empty %s\n", filename, lineno, name);
792 1.1 itojun return (1);
793 1.1 itojun }
794 1.1 itojun
795 1.1 itojun i = 0;
796 1.1 itojun if ((*field == '%' || *field == 'S' || *field == 'T' || *field == '*')
797 1.1 itojun && fieldlen >= 1) {
798 1.1 itojun switch (*field) {
799 1.1 itojun case 'S':
800 1.1 itojun if (mod && (flags & T_MSS))
801 1.1 itojun *mod = T_MSS;
802 1.1 itojun if (fieldlen == 1)
803 1.1 itojun return (0);
804 1.1 itojun break;
805 1.1 itojun case 'T':
806 1.1 itojun if (mod && (flags & T_MTU))
807 1.1 itojun *mod = T_MTU;
808 1.1 itojun if (fieldlen == 1)
809 1.1 itojun return (0);
810 1.1 itojun break;
811 1.1 itojun case '*':
812 1.1 itojun if (fieldlen != 1) {
813 1.1 itojun fprintf(stderr, "%s:%d long '%c' %s\n",
814 1.1 itojun filename, lineno, *field, name);
815 1.1 itojun return (1);
816 1.1 itojun }
817 1.1 itojun if (mod && (flags & T_DC)) {
818 1.1 itojun *mod = T_DC;
819 1.1 itojun return (0);
820 1.1 itojun }
821 1.1 itojun case '%':
822 1.1 itojun if (mod && (flags & T_MOD))
823 1.1 itojun *mod = T_MOD;
824 1.1 itojun if (fieldlen == 1) {
825 1.1 itojun fprintf(stderr, "%s:%d modulus %s must have a "
826 1.1 itojun "value\n", filename, lineno, name);
827 1.1 itojun return (1);
828 1.1 itojun }
829 1.1 itojun break;
830 1.1 itojun }
831 1.1 itojun if (mod == NULL || *mod == 0) {
832 1.1 itojun fprintf(stderr, "%s:%d does not allow %c' %s\n",
833 1.1 itojun filename, lineno, *field, name);
834 1.1 itojun return (1);
835 1.1 itojun }
836 1.1 itojun i++;
837 1.1 itojun }
838 1.1 itojun
839 1.1 itojun for (; i < fieldlen; i++) {
840 1.1 itojun if (field[i] < '0' || field[i] > '9') {
841 1.1 itojun fprintf(stderr, "%s:%d non-digit character in %s\n",
842 1.1 itojun filename, lineno, name);
843 1.1 itojun return (1);
844 1.1 itojun }
845 1.1 itojun val = val * 10 + field[i] - '0';
846 1.1 itojun if (val < 0) {
847 1.1 itojun fprintf(stderr, "%s:%d %s overflowed\n", filename,
848 1.1 itojun lineno, name);
849 1.1 itojun return (1);
850 1.1 itojun }
851 1.1 itojun }
852 1.1 itojun
853 1.1 itojun if (val > max) {
854 1.1 itojun fprintf(stderr, "%s:%d %s value %ld > %d\n", filename, lineno,
855 1.1 itojun name, val, max);
856 1.1 itojun return (1);
857 1.1 itojun }
858 1.1 itojun *var = (int)val;
859 1.1 itojun
860 1.1 itojun return (0);
861 1.1 itojun }
862 1.1 itojun
863 1.1 itojun /* parse the next string in a formatted config file line */
864 1.1 itojun int
865 1.1 itojun get_str(char **line, size_t *len, char **v, const char *name, int minlen,
866 1.1 itojun const char *filename, int lineno)
867 1.1 itojun {
868 1.1 itojun int fieldlen;
869 1.1 itojun char *ptr;
870 1.1 itojun
871 1.1 itojun ptr = get_field(line, len, &fieldlen);
872 1.1 itojun if (ptr == NULL)
873 1.1 itojun return (1);
874 1.1 itojun if (fieldlen < minlen) {
875 1.1 itojun fprintf(stderr, "%s:%d too short %s\n", filename, lineno, name);
876 1.1 itojun return (1);
877 1.1 itojun }
878 1.1 itojun if ((*v = malloc(fieldlen + 1)) == NULL) {
879 1.1 itojun perror("malloc()");
880 1.1 itojun return (1);
881 1.1 itojun }
882 1.1 itojun memcpy(*v, ptr, fieldlen);
883 1.1 itojun (*v)[fieldlen] = '\0';
884 1.1 itojun
885 1.1 itojun return (0);
886 1.1 itojun }
887 1.1 itojun
888 1.1 itojun /* Parse out the TCP opts */
889 1.1 itojun int
890 1.1 itojun get_tcpopts(const char *filename, int lineno, const char *tcpopts,
891 1.1 itojun pf_tcpopts_t *packed, int *optcnt, int *mss, int *mss_mod, int *wscale,
892 1.1 itojun int *wscale_mod, int *ts0)
893 1.1 itojun {
894 1.1 itojun int i, opt;
895 1.1 itojun
896 1.1 itojun *packed = 0;
897 1.1 itojun *optcnt = 0;
898 1.1 itojun *wscale = 0;
899 1.1 itojun *wscale_mod = T_DC;
900 1.1 itojun *mss = 0;
901 1.1 itojun *mss_mod = T_DC;
902 1.1 itojun *ts0 = 0;
903 1.1 itojun if (strcmp(tcpopts, ".") == 0)
904 1.1 itojun return (0);
905 1.1 itojun
906 1.1 itojun for (i = 0; tcpopts[i] && *optcnt < PF_OSFP_MAX_OPTS;) {
907 1.3 dsl switch ((opt = toupper((unsigned char)tcpopts[i++]))) {
908 1.1 itojun case 'N': /* FALLTHROUGH */
909 1.1 itojun case 'S':
910 1.1 itojun *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
911 1.1 itojun (opt == 'N' ? PF_OSFP_TCPOPT_NOP :
912 1.1 itojun PF_OSFP_TCPOPT_SACK);
913 1.1 itojun break;
914 1.1 itojun case 'W': /* FALLTHROUGH */
915 1.1 itojun case 'M': {
916 1.1 itojun int *this_mod, *this;
917 1.1 itojun
918 1.1 itojun if (opt == 'W') {
919 1.1 itojun this = wscale;
920 1.1 itojun this_mod = wscale_mod;
921 1.1 itojun } else {
922 1.1 itojun this = mss;
923 1.1 itojun this_mod = mss_mod;
924 1.1 itojun }
925 1.1 itojun *this = 0;
926 1.1 itojun *this_mod = 0;
927 1.1 itojun
928 1.1 itojun *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
929 1.1 itojun (opt == 'W' ? PF_OSFP_TCPOPT_WSCALE :
930 1.1 itojun PF_OSFP_TCPOPT_MSS);
931 1.1 itojun if (tcpopts[i] == '*' && (tcpopts[i + 1] == '\0' ||
932 1.1 itojun tcpopts[i + 1] == ',')) {
933 1.1 itojun *this_mod = T_DC;
934 1.1 itojun i++;
935 1.1 itojun break;
936 1.1 itojun }
937 1.1 itojun
938 1.1 itojun if (tcpopts[i] == '%') {
939 1.1 itojun *this_mod = T_MOD;
940 1.1 itojun i++;
941 1.1 itojun }
942 1.1 itojun do {
943 1.3 dsl if (!isdigit((unsigned char)tcpopts[i])) {
944 1.1 itojun fprintf(stderr, "%s:%d unknown "
945 1.1 itojun "character '%c' in %c TCP opt\n",
946 1.1 itojun filename, lineno, tcpopts[i], opt);
947 1.1 itojun return (1);
948 1.1 itojun }
949 1.1 itojun *this = (*this * 10) + tcpopts[i++] - '0';
950 1.1 itojun } while(tcpopts[i] != ',' && tcpopts[i] != '\0');
951 1.1 itojun break;
952 1.1 itojun }
953 1.1 itojun case 'T':
954 1.1 itojun if (tcpopts[i] == '0') {
955 1.1 itojun *ts0 = 1;
956 1.1 itojun i++;
957 1.1 itojun }
958 1.1 itojun *packed = (*packed << PF_OSFP_TCPOPT_BITS) |
959 1.1 itojun PF_OSFP_TCPOPT_TS;
960 1.1 itojun break;
961 1.1 itojun }
962 1.1 itojun (*optcnt) ++;
963 1.1 itojun if (tcpopts[i] == '\0')
964 1.1 itojun break;
965 1.1 itojun if (tcpopts[i] != ',') {
966 1.1 itojun fprintf(stderr, "%s:%d unknown option to %c TCP opt\n",
967 1.1 itojun filename, lineno, opt);
968 1.1 itojun return (1);
969 1.1 itojun }
970 1.1 itojun i++;
971 1.1 itojun }
972 1.1 itojun
973 1.1 itojun return (0);
974 1.1 itojun }
975 1.1 itojun
976 1.1 itojun /* rip the next field ouf of a formatted config file line */
977 1.1 itojun char *
978 1.1 itojun get_field(char **line, size_t *len, int *fieldlen)
979 1.1 itojun {
980 1.1 itojun char *ret, *ptr = *line;
981 1.1 itojun size_t plen = *len;
982 1.1 itojun
983 1.1 itojun
984 1.3 dsl while (plen && isspace((unsigned char)*ptr)) {
985 1.1 itojun plen--;
986 1.1 itojun ptr++;
987 1.1 itojun }
988 1.1 itojun ret = ptr;
989 1.1 itojun *fieldlen = 0;
990 1.1 itojun
991 1.1 itojun for (; plen > 0 && *ptr != ':'; plen--, ptr++)
992 1.1 itojun (*fieldlen)++;
993 1.1 itojun if (plen) {
994 1.1 itojun *line = ptr + 1;
995 1.1 itojun *len = plen - 1;
996 1.1 itojun } else {
997 1.1 itojun *len = 0;
998 1.1 itojun }
999 1.3 dsl while (*fieldlen && isspace((unsigned char)ret[*fieldlen - 1]))
1000 1.1 itojun (*fieldlen)--;
1001 1.1 itojun return (ret);
1002 1.1 itojun }
1003 1.1 itojun
1004 1.1 itojun
1005 1.1 itojun const char *
1006 1.1 itojun print_ioctl(struct pf_osfp_ioctl *fp)
1007 1.1 itojun {
1008 1.1 itojun static char buf[1024];
1009 1.1 itojun char tmp[32];
1010 1.1 itojun int i, opt;
1011 1.1 itojun
1012 1.1 itojun *buf = '\0';
1013 1.1 itojun if (fp->fp_flags & PF_OSFP_WSIZE_DC)
1014 1.1 itojun strlcat(buf, "*", sizeof(buf));
1015 1.1 itojun else if (fp->fp_flags & PF_OSFP_WSIZE_MSS)
1016 1.1 itojun strlcat(buf, "S", sizeof(buf));
1017 1.1 itojun else if (fp->fp_flags & PF_OSFP_WSIZE_MTU)
1018 1.1 itojun strlcat(buf, "T", sizeof(buf));
1019 1.1 itojun else {
1020 1.1 itojun if (fp->fp_flags & PF_OSFP_WSIZE_MOD)
1021 1.1 itojun strlcat(buf, "%", sizeof(buf));
1022 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_wsize);
1023 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1024 1.1 itojun }
1025 1.1 itojun strlcat(buf, ":", sizeof(buf));
1026 1.1 itojun
1027 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_ttl);
1028 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1029 1.1 itojun strlcat(buf, ":", sizeof(buf));
1030 1.1 itojun
1031 1.1 itojun if (fp->fp_flags & PF_OSFP_DF)
1032 1.1 itojun strlcat(buf, "1", sizeof(buf));
1033 1.1 itojun else
1034 1.1 itojun strlcat(buf, "0", sizeof(buf));
1035 1.1 itojun strlcat(buf, ":", sizeof(buf));
1036 1.1 itojun
1037 1.1 itojun if (fp->fp_flags & PF_OSFP_PSIZE_DC)
1038 1.1 itojun strlcat(buf, "*", sizeof(buf));
1039 1.1 itojun else {
1040 1.1 itojun if (fp->fp_flags & PF_OSFP_PSIZE_MOD)
1041 1.1 itojun strlcat(buf, "%", sizeof(buf));
1042 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_psize);
1043 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1044 1.1 itojun }
1045 1.1 itojun strlcat(buf, ":", sizeof(buf));
1046 1.1 itojun
1047 1.1 itojun if (fp->fp_optcnt == 0)
1048 1.1 itojun strlcat(buf, ".", sizeof(buf));
1049 1.1 itojun for (i = fp->fp_optcnt - 1; i >= 0; i--) {
1050 1.1 itojun opt = fp->fp_tcpopts >> (i * PF_OSFP_TCPOPT_BITS);
1051 1.1 itojun opt &= (1 << PF_OSFP_TCPOPT_BITS) - 1;
1052 1.1 itojun switch (opt) {
1053 1.1 itojun case PF_OSFP_TCPOPT_NOP:
1054 1.1 itojun strlcat(buf, "N", sizeof(buf));
1055 1.1 itojun break;
1056 1.1 itojun case PF_OSFP_TCPOPT_SACK:
1057 1.1 itojun strlcat(buf, "S", sizeof(buf));
1058 1.1 itojun break;
1059 1.1 itojun case PF_OSFP_TCPOPT_TS:
1060 1.1 itojun strlcat(buf, "T", sizeof(buf));
1061 1.1 itojun if (fp->fp_flags & PF_OSFP_TS0)
1062 1.1 itojun strlcat(buf, "0", sizeof(buf));
1063 1.1 itojun break;
1064 1.1 itojun case PF_OSFP_TCPOPT_MSS:
1065 1.1 itojun strlcat(buf, "M", sizeof(buf));
1066 1.1 itojun if (fp->fp_flags & PF_OSFP_MSS_DC)
1067 1.1 itojun strlcat(buf, "*", sizeof(buf));
1068 1.1 itojun else {
1069 1.1 itojun if (fp->fp_flags & PF_OSFP_MSS_MOD)
1070 1.1 itojun strlcat(buf, "%", sizeof(buf));
1071 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_mss);
1072 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1073 1.1 itojun }
1074 1.1 itojun break;
1075 1.1 itojun case PF_OSFP_TCPOPT_WSCALE:
1076 1.1 itojun strlcat(buf, "W", sizeof(buf));
1077 1.1 itojun if (fp->fp_flags & PF_OSFP_WSCALE_DC)
1078 1.1 itojun strlcat(buf, "*", sizeof(buf));
1079 1.1 itojun else {
1080 1.1 itojun if (fp->fp_flags & PF_OSFP_WSCALE_MOD)
1081 1.1 itojun strlcat(buf, "%", sizeof(buf));
1082 1.1 itojun snprintf(tmp, sizeof(tmp), "%d", fp->fp_wscale);
1083 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1084 1.1 itojun }
1085 1.1 itojun break;
1086 1.1 itojun }
1087 1.1 itojun
1088 1.1 itojun if (i != 0)
1089 1.1 itojun strlcat(buf, ",", sizeof(buf));
1090 1.1 itojun }
1091 1.1 itojun strlcat(buf, ":", sizeof(buf));
1092 1.1 itojun
1093 1.1 itojun strlcat(buf, fp->fp_os.fp_class_nm, sizeof(buf));
1094 1.1 itojun strlcat(buf, ":", sizeof(buf));
1095 1.1 itojun strlcat(buf, fp->fp_os.fp_version_nm, sizeof(buf));
1096 1.1 itojun strlcat(buf, ":", sizeof(buf));
1097 1.1 itojun strlcat(buf, fp->fp_os.fp_subtype_nm, sizeof(buf));
1098 1.1 itojun strlcat(buf, ":", sizeof(buf));
1099 1.1 itojun
1100 1.1 itojun snprintf(tmp, sizeof(tmp), "TcpOpts %d 0x%llx", fp->fp_optcnt,
1101 1.1 itojun (long long int)fp->fp_tcpopts);
1102 1.1 itojun strlcat(buf, tmp, sizeof(buf));
1103 1.1 itojun
1104 1.1 itojun return (buf);
1105 1.1 itojun }
1106