GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/pfctl/pfctl_osfp.c Lines: 0 491 0.0 %
Date: 2016-12-06 Branches: 0 437 0.0 %

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