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