File: ncap_icmp.c

package info (click to toggle)
ncap 1.9.2-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,704 kB
  • sloc: sh: 10,135; ansic: 5,829; perl: 68; makefile: 42; python: 33
file content (339 lines) | stat: -rw-r--r-- 7,680 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* ncap_icmp.c - ICMP filters for NCAP
 */

#ifndef lint
static const char rcsid[] = "$Id$";
static const char copyright[] =
	"Copyright (c) 2008 by Internet Systems Consortium, Inc. (\"ISC\")";
#endif

/*
 * Copyright (c) 2008 by Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* Import. */

#include "ncap_pvt.h"
#include "ncap_port.h"
#include "ncap_port_net.h"

#include <sys/uio.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef DMALLOC
# include <dmalloc.h>
#endif

#include "ncap_list.h"

/* Data structures. */

struct icmp_param {
	const char *	text;
	const char *	bpf;
	unsigned	value;
};

/* Forward. */

static int	match_icmp(ncap_rule_ptr, ncap_msg_ct); 
static int	match_icmptype(ncap_rule_ptr, ncap_msg_ct msg);
static int	match_icmptype_num(ncap_rule_ptr, ncap_msg_ct msg);
static int	match_icmpcode(ncap_rule_ptr, ncap_msg_ct msg);
static char *	bpf_match_param(unsigned, const struct icmp_param *);
static char *	bpf_match_icmptype(unsigned, int);
static char *	bpf_match_icmpcode(unsigned, int);

/* Constants. */

#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif

/* Macros. */

#define	NCAP_ICMP_NEG	0x80000000

/* Private data. */

static struct icmp_param param_types[] = {
	{
		.text  = "echoreply",
		.bpf   = "(icmp[icmptype] = icmp-echoreply)",
		.value = 0
	},
	{
		.text  = "unreach",
		.bpf   = "(icmp[icmptype] = icmp-unreach)",
		.value = 3
	},
	{
		.text  = "sourcequench",
		.bpf   = "(icmp[icmptype] = icmp-sourcequench)",
		.value = 4
	},
	{
		.text  = "redirect",
		.bpf   = "(icmp[icmptype] = icmp-redirect)",
		.value = 5
	},
	{
		.text  = "echorequest",
		.bpf   = "(icmp[icmptype] = icmp-echo)",
		.value = 8
	},
	{
		.text  = "routeradvert",
		.bpf   = "(icmp[icmptype] = icmp-routeradvert)",
		.value = 9
	},
	{
		.text  = "routersolicit",
		.bpf   = "(icmp[icmptype] = icmp-routersolicit)",
		.value = 10
	},
	{
		.text  = "timxceed",
		.bpf   = "(icmp[icmptype] = icmp-timxceed)",
		.value = 11
	},
	{
		.text  = "paramproblem",
		.bpf   = "(icmp[icmptype] = icmp-paramprob)",
		.value = 12
	},
	{
		.text  = "tstamprequest",
		.bpf   = "(icmp[icmptype] = icmp-tstamp)",
		.value = 13
	},
	{
		.text  = "tstampreply",
		.bpf   = "(icmp[icmptype] = icmp-tstampreply)",
		.value = 14
	},
	{
		.text  = "inforeq",
		.bpf   = "(icmp[icmptype] = icmp-ireq)",
		.value = 15
	},
	{
		.text  = "inforeply",
		.bpf   = "(icmp[icmptype] = icmp-ireqreply)",
		.value = 16
	},
	{
		.text  = "maskreq",
		.bpf   = "(icmp[icmptype] = icmp-maskreq)",
		.value = 17
	},
	{
		.text  = "maskreply",
		.bpf   = "(icmp[icmptype] = icmp-maskreply)",
		.value = 18
	},
	{
		.text  = "traceroute",
		.bpf   = "(icmp[icmptype] = 30)",
		.value = 30
	},
	/* XXX more types require a wider flags field */
	{ NULL, NULL, 0 }
};

/* Export. */

/* Process an ICMP filter term.
 *
 * Returns the BPF used to preselect such packets, "" if none, NULL if error.
 */
char *
ncap_filter_icmp(ncap_t ncap, const char *term, char sep, const char *arg) {
	struct icmp_param *ip;
	char *saveptr = NULL;
	char *word;
	char *tmp;
	size_t n;

	if (term == NULL || sep == '\0' || arg == NULL) {
		ncap_addrule(ncap, match_icmp, NULL, 0U);
		return (strdup("icmp"));
	}

	n = strlen(term);

	if (strncmp(term, "type", n) == 0) {
		unsigned type;
		unsigned *flags = calloc(1, sizeof *flags);
		assert(flags != NULL);

		type = strtoul(arg, &tmp, 0);
		if (*tmp != '\0') {
			/* symbolic icmp type */
			tmp = strdup(arg);
			if (sep == '#')
				*flags |= NCAP_ICMP_NEG;
			for (word = strtok_r(tmp, ",", &saveptr);
			     word != NULL;
			     word = strtok_r(NULL, ",", &saveptr))
			{
				int recog = false;
				n = strlen(word);
				for (ip = param_types; ip->text != NULL; ip++) {
					if (strncmp(word, ip->text, n) == 0) {
						*flags |= 1 << ip->value;
						recog = true;
					}
				}
				if (!recog) {
					NCAP_SETERR("unrecognized icmp type");
					free(tmp);
					return (NULL);
				}
			}
			free(tmp);
			ncap_addrule(ncap, match_icmptype, flags, 0U);
			return (bpf_match_param(*flags, param_types));
		} else {
			/* numeric icmp type */
			unsigned *ptype = malloc(sizeof *ptype);
			assert(ptype != NULL);
			*ptype = type;
			ncap_addrule(ncap, match_icmptype_num, ptype, sep == '#');
			return (bpf_match_icmptype(type, sep == '#'));

		}
	} else if (strncmp(term, "code", n) == 0) {
		unsigned *code = calloc(1, sizeof *code);
		assert(code != NULL);
		*code = strtoul(arg, &tmp, 0);
		if (*tmp != '\0') {
			NCAP_SETERR("invalid icmp code");
			free(code);
			return (NULL);
		}
		ncap_addrule(ncap, match_icmpcode, code, sep == '#');
		return (bpf_match_icmpcode(*code, sep == '#'));
	}

	return (strdup(""));
}

/* Private. */

static int
match_icmp(ncap_rule_ptr rule __attribute__((unused)), ncap_msg_ct msg) {
	return ((msg->np == ncap_ip4 || msg->np == ncap_ip6) &&
		(msg->tp == ncap_icmp));
}

static int
match_icmptype(ncap_rule_ptr rule, ncap_msg_ct msg) {
	struct icmp_param *ip;
	int res = false;
	unsigned flags = *((int *) rule->payload);
	
	for (ip = param_types; ip->text != NULL; ip++) {
		if (flags & (1 << ip->value) &&
		    msg->tpu.icmp.type == ip->value)
		{
			res = true;
			break;
		}
	}
	return (((flags & NCAP_ICMP_NEG) != 0) ^ res);
}

static int
match_icmptype_num(ncap_rule_ptr rule, ncap_msg_ct msg) {
	unsigned type = *((unsigned *) rule->payload);
	if (rule->argument == true) /* invert */
		return (type != msg->tpu.icmp.type);
	else
		return (type == msg->tpu.icmp.type);
}

static int
match_icmpcode(ncap_rule_ptr rule, ncap_msg_ct msg) {
	unsigned code = *((unsigned *) rule->payload);
	if (rule->argument == true) /* invert */
		return (code != msg->tpu.icmp.code);
	else
		return (code == msg->tpu.icmp.code);
}

static char *
bpf_match_icmptype(unsigned type, int invert) {
	char *bpf;
	ncap_asprintf(&bpf, "(icmp[icmptype] %s= %u)", invert ? "!" : "", type);
	assert(bpf != NULL);
	return (bpf);
}

static char *
bpf_match_icmpcode(unsigned code, int invert) {
	char *bpf;
	ncap_asprintf(&bpf, "(icmp[icmpcode] %s= %u)", invert ? "!" : "", code);
	assert(bpf != NULL);
	return (bpf);
}

/* XXX code mostly duplicated from ncap_dns */
static char *
bpf_match_param(unsigned flags, const struct icmp_param *params) {
	const struct icmp_param *ip;
	char *bpf = strdup("");
	int n, len;

	assert(bpf != NULL);
	len = 1 + strlen(bpf);
	n = 0;

	if (flags & NCAP_ICMP_NEG) {
		len += strlen("not (");
		bpf = realloc(bpf, len);
		strcat(bpf, "not (");
	} else {
		len += strlen("(");
		bpf = realloc(bpf, len);
		strcat(bpf, "(");
	}

	for (ip = params; ip->bpf != NULL; ip++) {
		if ((flags & (1 << ip->value)) != 0) {
			len += strlen(ip->bpf);
			len += n > 0 ? strlen(" or ") : 0;
			bpf = realloc(bpf, len);
			if (n > 0)
				strcat(bpf, " or ");
			strcat(bpf, ip->bpf);
			n++;
		}
	}

	len += strlen(")");
	bpf = realloc(bpf, len);
	strcat(bpf, ")");

	return (bpf);
}