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