File: iptc.c

package info (click to toggle)
linux-igd 1.0%2Bcvs20070630-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 452 kB
  • ctags: 163
  • sloc: ansic: 1,951; xml: 593; sh: 135; makefile: 38
file content (459 lines) | stat: -rw-r--r-- 12,358 bytes parent folder | download | duplicates (5)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#if HAVE_LIBIPTC
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <iptables.h>
#include <libiptc/libiptc.h>
#include <linux/netfilter_ipv4/ip_nat.h>
#include <arpa/inet.h> /* inet_addr */
#include "globals.h"
#include "util.h"
#include "iptc.h"

struct ipt_natinfo
{
	struct ipt_entry_target t;
	struct ip_nat_multi_range mr;
};

struct ipt_entry_match *get_tcp_match(const char *sports, const char *dports, unsigned int *nfcache);
struct ipt_entry_match *get_udp_match(const char *sports, const char *dports, unsigned int *nfcache);
struct ipt_entry_target *get_dnat_target(const char *input, unsigned int *nfcache);

static u_int16_t parse_port(const char *port);
static void parse_ports(const char *portstring, u_int16_t *ports);
static int service_to_port(const char *name);

static void parse_range(const char *input, struct ip_nat_range *range);
static struct ipt_natinfo *append_range(struct ipt_natinfo *info, const struct ip_nat_range *range);

static int matchcmp(const struct ipt_entry_match *match, const char *srcports, const char *destports);

void iptc_add_rule(const char *table,
                   const char *chain,
                   const char *protocol,
                   const char *iniface,
                   const char *outiface,
                   const char *src,
                   const char *dest,
                   const char *srcports,
                   const char *destports,
                   const char *target,
                   const char *dnat_to,
                   const int append)
{
	iptc_handle_t handle;
	struct ipt_entry *chain_entry;
	struct ipt_entry_match *entry_match = NULL;
	struct ipt_entry_target *entry_target = NULL;
	ipt_chainlabel labelit;
	long match_size;
	int result = 0;

	chain_entry = calloc(1, sizeof(*chain_entry));

	if (src) {
		chain_entry->ip.src.s_addr = inet_addr(src);
		chain_entry->ip.smsk.s_addr = inet_addr("255.255.255.255");
	}
	if (dest) {
		chain_entry->ip.dst.s_addr = inet_addr(dest);
		chain_entry->ip.dmsk.s_addr = inet_addr("255.255.255.255");
	}

	if (iniface) strncpy(chain_entry->ip.iniface, iniface, IFNAMSIZ);
	if (outiface) strncpy(chain_entry->ip.outiface, outiface, IFNAMSIZ);

	if (strcmp(protocol, "TCP") == 0) {
		chain_entry->ip.proto = IPPROTO_TCP;
		entry_match = get_tcp_match(srcports, destports, &chain_entry->nfcache);
	}
	else if (strcmp(protocol, "UDP") == 0) {
		chain_entry->ip.proto = IPPROTO_UDP;
		entry_match = get_udp_match(srcports, destports, &chain_entry->nfcache);
	}
	else {
		trace(1, "Unsupported protocol: %s", protocol);
		return;
	}

	if (strcmp(target, "") == 0
	    || strcmp(target, IPTC_LABEL_ACCEPT) == 0
	    || strcmp(target, IPTC_LABEL_DROP) == 0
	    || strcmp(target, IPTC_LABEL_QUEUE) == 0
	    || strcmp(target, IPTC_LABEL_RETURN) == 0) {
		size_t size;

		size = IPT_ALIGN(sizeof(struct ipt_entry_target)) + IPT_ALIGN(sizeof(int));
		entry_target = calloc(1, size);
		entry_target->u.user.target_size = size;
		strncpy(entry_target->u.user.name, target, IPT_FUNCTION_MAXNAMELEN);
	}
	else if (strcmp(target, "DNAT") == 0) {
		entry_target = get_dnat_target(dnat_to, &chain_entry->nfcache);
	}

	if (entry_match)
		match_size = entry_match->u.match_size;
	else
		match_size = 0;

	chain_entry = realloc(chain_entry, sizeof(*chain_entry) + match_size + entry_target->u.target_size);
	memcpy(chain_entry->elems + match_size, entry_target, entry_target->u.target_size);
	chain_entry->target_offset = sizeof(*chain_entry) + match_size;
	chain_entry->next_offset = sizeof(*chain_entry) + match_size + entry_target->u.target_size;

	if (entry_match)
		memcpy(chain_entry->elems, entry_match, match_size);

	handle = iptc_init(table);
	if (!handle) {
		trace(1, "libiptc error: Can't initialize table %s, %s", table, iptc_strerror(errno));
		return;
	}

	strncpy(labelit, chain, sizeof(ipt_chainlabel));
	result = iptc_is_chain(chain, handle);
	if (!result) {
		trace(1, "libiptc error: Chain %s does not exist!", chain);
		return;
	}
	if (append)
		result = iptc_append_entry(labelit, chain_entry, &handle);
	else
		result = iptc_insert_entry(labelit, chain_entry, 0, &handle);

	if (!result) {
		trace(1, "libiptc error: Can't add, %s", iptc_strerror(errno));
		return;
	}
	result = iptc_commit(&handle);
	if (!result) {
	  trace(1, "libiptc error: Commit error, %s", iptc_strerror(errno));
		return;
	}
	else 
	  trace(3, "added new rule to block successfully");

	if (entry_match) free(entry_match);
	free(entry_target);
	free(chain_entry);
}

void iptc_delete_rule(const char *table,
                      const char *chain,
                      const char *protocol,
                      const char *iniface,
                      const char *outiface,
                      const char *src,
                      const char *dest,
                      const char *srcports,
                      const char *destports,
                      const char *target,
                      const char *dnat_to)
{
	iptc_handle_t handle;
	const struct ipt_entry *e;
	ipt_chainlabel labelit;
	int i, result;
	unsigned long int s_src = INADDR_NONE, s_dest = INADDR_NONE;

	if (src) s_src = inet_addr(src);
	if (dest) s_dest = inet_addr(dest);

	handle = iptc_init(table);
	if (!handle) {
	  trace(1, "libiptc error: Can't initialize table %s, %s", table, iptc_strerror(errno));
		return;
	}

	strncpy(labelit, chain, sizeof(ipt_chainlabel));
	result = iptc_is_chain(chain, handle);
	if (!result) {
	  trace(1, "libiptc error: Chain %s does not exist!", chain);
		return;
	}
	
	/* check through rules to find match */
	for (e = iptc_first_rule(chain, &handle), i=0; e; e = iptc_next_rule(e, &handle), i++)  {
		if (s_src != INADDR_NONE && e->ip.src.s_addr != s_src) continue;
		if (s_dest != INADDR_NONE && e->ip.dst.s_addr != s_dest) continue;
		if (iniface && strcmp(e->ip.iniface, iniface) != 0) continue;
		if (outiface && strcmp(e->ip.outiface, outiface) != 0) continue;
		if (protocol && strcmp(protocol, "TCP") == 0 && e->ip.proto != IPPROTO_TCP) continue;
		if (protocol && strcmp(protocol, "UDP") == 0 && e->ip.proto != IPPROTO_UDP) continue;
		if ((srcports || destports) && IPT_MATCH_ITERATE(e, matchcmp, srcports, destports) == 0) continue;
		if (target && strcmp(target, iptc_get_target(e, &handle)) != 0) continue;
		if (dnat_to && strcmp(target, "DNAT") == 0) {
			struct ipt_entry_target *t;
			struct ip_nat_multi_range *mr;
			struct ip_nat_range *r, range;

			t = (void *) e+e->target_offset;
			mr = (void *) &t->data;

			if (mr->rangesize != 1) continue; /* we have only single dnat_to target now */
			r = mr->range;
			parse_range(dnat_to, &range);
			if (r->flags == range.flags
			    && r->min_ip == range.min_ip
			    && r->max_ip == range.max_ip
			    && r->min.all == range.min.all
			    && r->max.all == range.max.all) {
				break;
			}
		}
		
		break;
	}
	if (!e) return;
	result = iptc_delete_num_entry(chain, i, &handle);
	if (!result) {
	  trace(1, "libiptc error: Delete error, %s", iptc_strerror(errno));
		return;
	}
	result = iptc_commit(&handle);
	if (!result) {
	  trace(1, "libiptc error: Commit error, %s", iptc_strerror(errno));
		return;
	}
	else 
	  trace(3, "deleted rule from block successfully");
}

static int matchcmp(const struct ipt_entry_match *match, const char *srcports, const char *destports) {
	u_int16_t temp[2];

	if (strcmp(match->u.user.name, "tcp") == 0) {
		struct ipt_tcp *tcpinfo = (struct ipt_tcp *)match->data;

		if (srcports) {
			parse_ports(srcports, temp);
			if (temp[0] != tcpinfo->spts[0] || temp[1] != tcpinfo->spts[1]) return 0;
		}
		if (destports) {
			parse_ports(destports, temp);
			if (temp[0] != tcpinfo->dpts[0] || temp[1] != tcpinfo->dpts[1]) return 0;
		}
		return 1;
	}
	else if (strcmp(match->u.user.name, "udp") == 0) {
		struct ipt_udp *udpinfo = (struct ipt_udp *)match->data;

		if (srcports) {
			parse_ports(srcports, temp);
			if (temp[0] != udpinfo->spts[0] || temp[1] != udpinfo->spts[1]) return 0;
		}
		if (destports) {
			parse_ports(destports, temp);
			if (temp[0] != udpinfo->dpts[0] || temp[1] != udpinfo->dpts[1]) return 0;
		}
		return 1;
	}
	else return 0;
}

/* These functions are used to create structs */

struct ipt_entry_match *
get_tcp_match(const char *sports, const char *dports, unsigned int *nfcache) {
	struct ipt_entry_match *match;
	struct ipt_tcp *tcpinfo;
	size_t size;

	size = IPT_ALIGN(sizeof(*match)) + IPT_ALIGN(sizeof(*tcpinfo));
	match = calloc(1, size);
	match->u.match_size = size;
	strncpy(match->u.user.name, "tcp", IPT_FUNCTION_MAXNAMELEN);

	tcpinfo = (struct ipt_tcp *)match->data;
	tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;

	if (sports) {
		*nfcache |= NFC_IP_SRC_PT;
		parse_ports(sports, tcpinfo->spts);
	}
	if (dports) {
		*nfcache |= NFC_IP_DST_PT;
		parse_ports(dports, tcpinfo->dpts);
	}

	return match;
}

struct ipt_entry_match *
get_udp_match(const char *sports, const char *dports, unsigned int *nfcache) {
	struct ipt_entry_match *match;
	struct ipt_udp *udpinfo;
	size_t size;

	size = IPT_ALIGN(sizeof(*match)) + IPT_ALIGN(sizeof(*udpinfo));
	match = calloc(1, size);
	match->u.match_size = size;
	strncpy(match->u.user.name, "udp", IPT_FUNCTION_MAXNAMELEN);

	udpinfo = (struct ipt_udp *)match->data;
	udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF;

	if (sports) {
		*nfcache |= NFC_IP_SRC_PT;
		parse_ports(sports, udpinfo->spts);
	}
	if (dports) {
		*nfcache |= NFC_IP_DST_PT;
		parse_ports(dports, udpinfo->dpts);
	}

	return match;
}

struct ipt_entry_target *
get_dnat_target(const char *input, unsigned int *nfcache) {
	struct ipt_entry_target *target;
	struct ipt_natinfo *info;
	struct ip_nat_range range;

	char *buffer;
	size_t size;

	/* Can't cache this */
	*nfcache |= NFC_UNKNOWN;

	buffer = strdup(input);
	size = IPT_ALIGN(sizeof(*target)) + IPT_ALIGN(sizeof(struct ip_nat_multi_range));
	target = calloc(1, size);
	target->u.target_size = size;
	strncpy(target->u.user.name, "DNAT", IPT_FUNCTION_MAXNAMELEN);

	info = (void *)target;
	parse_range(buffer, &range);
	target = &(append_range(info, &range)->t);
	free(buffer);

	return target;
}

/* Copied and modified from libipt_tcp.c and libipt_udp.c */

static u_int16_t
parse_port(const char *port)
{
	unsigned int portnum;

	if ((portnum = service_to_port(port)) != -1) {
		return (u_int16_t)portnum;
	}
	else {
		return atoi(port);
	}
}

static void
parse_ports(const char *portstring, u_int16_t *ports)
{
	char *buffer;
	char *cp;

	buffer = strdup(portstring);
	if ((cp = strchr(buffer, ':')) == NULL)
		ports[0] = ports[1] = parse_port(buffer);
	else {
		*cp = '\0';
		cp++;

		ports[0] = buffer[0] ? parse_port(buffer) : 0;
		ports[1] = cp[0] ? parse_port(cp) : 0xFFFF;
	}
	free(buffer);
}

static int
service_to_port(const char *name)
{
	struct servent *service;

	if ((service = getservbyname(name, "tcp")) != NULL)
	return ntohs((unsigned short) service->s_port);

	return -1;
}




/* Copied and modified from libipt_DNAT.c */

static void
parse_range(const char *input, struct ip_nat_range *range) {
	char *colon, *dash, *buffer;
	in_addr_t ip;

	buffer = strdup(input);
	memset(range, 0, sizeof(*range));
	colon = strchr(buffer, ':');

	if (colon) {
		int port;

		range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;

		port = atoi(colon+1);
		dash = strchr(colon, '-');
		if (!dash) {
			range->min.all
				= range->max.all
				= htons(port);
		} else {
			int maxport;

			maxport = atoi(dash + 1);
			range->min.all = htons(port);
			range->max.all = htons(maxport);
		}
		/* Starts with a colon? No IP info...*/
		if (colon == buffer) {
			free(buffer);
			return;
		}
		*colon = '\0';
	}

	range->flags |= IP_NAT_RANGE_MAP_IPS;
	dash = strchr(buffer, '-');
	if (colon && dash && dash > colon)
		dash = NULL;

	if (dash)
		*dash = '\0';

	ip = inet_addr(buffer);
	range->min_ip = ip;
	if (dash) {
		ip = inet_addr(dash+1);
		range->max_ip = ip;
	} else
		range->max_ip = range->min_ip;

	free(buffer);
	return;
}


static struct ipt_natinfo *
append_range(struct ipt_natinfo *info, const struct ip_nat_range *range) {
	unsigned int size;

	/* One ip_nat_range already included in ip_nat_multi_range */
	size = IPT_ALIGN(sizeof(*info) + info->mr.rangesize * sizeof(*range));

	info = realloc(info, size);

	info->t.u.target_size = size;
	info->mr.range[info->mr.rangesize] = *range;
	info->mr.rangesize++;

	return info;
}
#endif