summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/lib/parseipfexpr.c
blob: 9a2a2071e9dbc8c9269624f3e1365ce767e89dfe (plain)
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
#include "ipf.h"
#include <ctype.h>


typedef struct ipfopentry {
	int	ipoe_cmd;
	int	ipoe_nbasearg;
	int	ipoe_maxarg;
	int	ipoe_argsize;
	char	*ipoe_word;
} ipfopentry_t;

static ipfopentry_t opwords[17] = {
	{ IPF_EXP_IP_ADDR, 2, 0, 1, "ip.addr" },
	{ IPF_EXP_IP6_ADDR, 2, 0, 4, "ip6.addr" },
	{ IPF_EXP_IP_PR, 1, 0, 1, "ip.p" },
	{ IPF_EXP_IP_SRCADDR, 2, 0, 1, "ip.src" },
	{ IPF_EXP_IP_DSTADDR, 2, 0, 1, "ip.dst" },
	{ IPF_EXP_IP6_SRCADDR, 2, 0, 4, "ip6.src" },
	{ IPF_EXP_IP6_DSTADDR, 2, 0, 4, "ip6.dst" },
	{ IPF_EXP_TCP_PORT, 1, 0, 1, "tcp.port" },
	{ IPF_EXP_TCP_DPORT, 1, 0, 1, "tcp.dport" },
	{ IPF_EXP_TCP_SPORT, 1, 0, 1, "tcp.sport" },
	{ IPF_EXP_TCP_FLAGS, 2, 0, 1, "tcp.flags" },
	{ IPF_EXP_UDP_PORT, 1, 0, 1, "udp.port" },
	{ IPF_EXP_UDP_DPORT, 1, 0, 1, "udp.dport" },
	{ IPF_EXP_UDP_SPORT, 1, 0, 1, "udp.sport" },
	{ IPF_EXP_TCP_STATE, 1, 0, 1, "tcp.state" },
	{ IPF_EXP_IDLE_GT, 1, 1, 1, "idle-gt" },
	{ -1, 0, 0, 0, NULL  }
};


int *
parseipfexpr(line, errorptr)
	char *line;
	char **errorptr;
{
	int not, items, asize, *oplist, osize, i;
	char *temp, *arg, *s, *t, *ops, *error;
	ipfopentry_t *e;
	ipfexp_t *ipfe;

	asize = 0;
	error = NULL;
	oplist = NULL;

	temp = strdup(line);
	if (temp == NULL) {
		error = "strdup failed";
		goto parseerror;
	}

	/*
	 * Eliminate any white spaces to make parsing easier.
	 */
	for (s = temp; *s != '\0'; ) {
		if (ISSPACE(*s))
			strcpy(s, s + 1);
		else
			s++;
	}

	/*
	 * Parse the string.
	 * It should be sets of "ip.dst=1.2.3.4/32;" things.
	 * There must be a "=" or "!=" and it must end in ";".
	 */
	if (temp[strlen(temp) - 1] != ';') {
		error = "last character not ';'";
		goto parseerror;
	}

	/*
	 * Work through the list of complete operands present.
	 */
	for (ops = strtok(temp, ";"); ops != NULL; ops = strtok(NULL, ";")) {
		arg = strchr(ops, '=');
		if ((arg < ops + 2) || (arg == NULL)) {
			error = "bad 'arg' vlaue";
			goto parseerror;
		}

		if (*(arg - 1) == '!') {
			*(arg - 1) = '\0';
			not = 1;
		} else {
			not = 0;
		}
		*arg++ = '\0';


		for (e = opwords; e->ipoe_word; e++) {
			if (strcmp(ops, e->ipoe_word) == 0)
				break;
		}
		if (e->ipoe_word == NULL) {
			error = malloc(32);
			if (error != NULL) {
				sprintf(error, "keyword (%.10s) not found",
					ops);
			}
			goto parseerror;
		}

		/*
		 * Count the number of commas so we know how big to
		 * build the array
		 */
		for (s = arg, items = 1; *s != '\0'; s++)
			if (*s == ',')
				items++;

		if ((e->ipoe_maxarg != 0) && (items > e->ipoe_maxarg)) {
			error = "too many items";
			goto parseerror;
		}

		/*
		 * osize will mark the end of where we have filled up to
		 * and is thus where we start putting new data.
		 */
		osize = asize;
		asize += 4 + (items * e->ipoe_nbasearg * e->ipoe_argsize);
		if (oplist == NULL)
			oplist = calloc(1, sizeof(int) * (asize + 2));
		else
			oplist = realloc(oplist, sizeof(int) * (asize + 2));
		if (oplist == NULL) {
			error = "oplist alloc failed";
			goto parseerror;
		}
		ipfe = (ipfexp_t *)(oplist + osize);
		osize += 4;
		ipfe->ipfe_cmd = e->ipoe_cmd;
		ipfe->ipfe_not = not;
		ipfe->ipfe_narg = items * e->ipoe_nbasearg;
		ipfe->ipfe_size = items * e->ipoe_nbasearg * e->ipoe_argsize;
		ipfe->ipfe_size += 4;

		for (s = arg; (*s != '\0') && (osize < asize); s = t) {
			/*
			 * Look for the end of this arg or the ',' to say
			 * there is another following.
			 */
			for (t = s; (*t != '\0') && (*t != ','); t++)
				;
			if (*t == ',')
				*t++ = '\0';

			if (!strcasecmp(ops, "ip.addr") ||
			    !strcasecmp(ops, "ip.src") ||
			    !strcasecmp(ops, "ip.dst")) {
				i6addr_t mask, addr;
				char *delim;

				delim = strchr(s, '/');
				if (delim != NULL) {
					*delim++ = '\0';
					if (genmask(AF_INET, delim,
						    &mask) == -1) {
						error = "genmask failed";
						goto parseerror;
					}
				} else {
					mask.in4.s_addr = 0xffffffff;
				}
				if (gethost(AF_INET, s, &addr) == -1) {
					error = "gethost failed";
					goto parseerror;
				}

				oplist[osize++] = addr.in4.s_addr;
				oplist[osize++] = mask.in4.s_addr;

#ifdef USE_INET6
			} else if (!strcasecmp(ops, "ip6.addr") ||
			    !strcasecmp(ops, "ip6.src") ||
			    !strcasecmp(ops, "ip6.dst")) {
				i6addr_t mask, addr;
				char *delim;

				delim = strchr(s, '/');
				if (delim != NULL) {
					*delim++ = '\0';
					if (genmask(AF_INET6, delim,
						    &mask) == -1) {
						error = "genmask failed";
						goto parseerror;
					}
				} else {
					mask.i6[0] = 0xffffffff;
					mask.i6[1] = 0xffffffff;
					mask.i6[2] = 0xffffffff;
					mask.i6[3] = 0xffffffff;
				}
				if (gethost(AF_INET6, s, &addr) == -1) {
					error = "gethost failed";
					goto parseerror;
				}

				oplist[osize++] = addr.i6[0];
				oplist[osize++] = addr.i6[1];
				oplist[osize++] = addr.i6[2];
				oplist[osize++] = addr.i6[3];
				oplist[osize++] = mask.i6[0];
				oplist[osize++] = mask.i6[1];
				oplist[osize++] = mask.i6[2];
				oplist[osize++] = mask.i6[3];
#endif

			} else if (!strcasecmp(ops, "ip.p")) {
				int p;

				p = getproto(s);
				if (p == -1)
					goto parseerror;
				oplist[osize++] = p;

			} else if (!strcasecmp(ops, "tcp.flags")) {
				u_32_t mask, flags;
				char *delim;

				delim = strchr(s, '/');
				if (delim != NULL) {
					*delim++ = '\0';
					mask = tcpflags(delim);
				} else {
					mask = 0xff;
				}
				flags = tcpflags(s);

				oplist[osize++] = flags;
				oplist[osize++] = mask;


			} else if (!strcasecmp(ops, "tcp.port") ||
			    !strcasecmp(ops, "tcp.sport") ||
			    !strcasecmp(ops, "tcp.dport") ||
			    !strcasecmp(ops, "udp.port") ||
			    !strcasecmp(ops, "udp.sport") ||
			    !strcasecmp(ops, "udp.dport")) {
				char proto[4];
				u_short port;

				strncpy(proto, ops, 3);
				proto[3] = '\0';
				if (getport(NULL, s, &port, proto) == -1)
					goto parseerror;
				oplist[osize++] = port;

			} else if (!strcasecmp(ops, "tcp.state")) {
				oplist[osize++] = atoi(s);

			} else {
				error = "unknown word";
				goto parseerror;
			}
		}
	}

	free(temp);

	if (errorptr != NULL)
		*errorptr = NULL;

	for (i = asize; i > 0; i--)
		oplist[i] = oplist[i - 1];

	oplist[0] = asize + 2;
	oplist[asize + 1] = IPF_EXP_END;

	return oplist;

parseerror:
	if (errorptr != NULL)
		*errorptr = error;
	if (oplist != NULL)
		free(oplist);
	if (temp != NULL)
		free(temp);
	return NULL;
}
OpenPOWER on IntegriCloud