summaryrefslogtreecommitdiff
path: root/ipsets.c
blob: e7cde16e930a438c8850a79b51b0e0cac65171bf (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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
 * firewall3 - 3rd OpenWrt UCI firewall implementation
 *
 *   Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
 *
 * 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 THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
 */

#include <ctype.h>

#include "ipsets.h"


const struct fw3_option fw3_ipset_opts[] = {
	FW3_OPT("enabled",       bool,           ipset,     enabled),
	FW3_OPT("reload_set",	 bool,           ipset,     reload_set),
	FW3_OPT("counters",	 bool,           ipset,     counters),
	FW3_OPT("comment",	 bool,           ipset,     comment),

	FW3_OPT("name",          string,         ipset,     name),
	FW3_OPT("family",        family,         ipset,     family),

	FW3_OPT("storage",       ipset_method,   ipset,     method),
	FW3_LIST("match",        ipset_datatype, ipset,     datatypes),

	FW3_OPT("iprange",       address,        ipset,     iprange),
	FW3_OPT("portrange",     port,           ipset,     portrange),

	FW3_OPT("netmask",       int,            ipset,     netmask),
	FW3_OPT("maxelem",       int,            ipset,     maxelem),
	FW3_OPT("hashsize",      int,            ipset,     hashsize),
	FW3_OPT("timeout",       int,            ipset,     timeout),

	FW3_OPT("external",      string,         ipset,     external),

	FW3_LIST("entry",        setentry,       ipset,     entries),
	FW3_OPT("loadfile",      string,         ipset,     loadfile),

	{ }
};

#define T(m, t1, t2, t3, r, o) \
	{ FW3_IPSET_METHOD_##m, \
	  FW3_IPSET_TYPE_##t1 | (FW3_IPSET_TYPE_##t2 << 8) | (FW3_IPSET_TYPE_##t3 << 16), \
	  r, o }

enum ipset_optflag {
	OPT_IPRANGE   = (1 << 0),
	OPT_PORTRANGE = (1 << 1),
	OPT_NETMASK   = (1 << 2),
	OPT_HASHSIZE  = (1 << 3),
	OPT_MAXELEM   = (1 << 4),
	OPT_FAMILY    = (1 << 5),
};

struct ipset_type {
	enum fw3_ipset_method method;
	uint32_t types;
	uint8_t required;
	uint8_t optional;
};

static struct ipset_type ipset_types[] = {
	T(BITMAP, IP,   UNSPEC, UNSPEC, OPT_IPRANGE, OPT_NETMASK),
	T(BITMAP, IP,   MAC,    UNSPEC, OPT_IPRANGE, 0),
	T(BITMAP, PORT, UNSPEC, UNSPEC, OPT_PORTRANGE, 0),

	T(HASH,   IP,   UNSPEC, UNSPEC, 0,
	  OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM | OPT_NETMASK),
	T(HASH,   NET,  UNSPEC, UNSPEC, 0,
	  OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
	T(HASH,   IP,   PORT,   UNSPEC, 0,
	  OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
	T(HASH,   NET,  PORT,   UNSPEC, 0,
	  OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
	T(HASH,   IP,   PORT,   IP,     0,
	  OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),
	T(HASH,   IP,   PORT,   NET,    0,
	  OPT_FAMILY | OPT_HASHSIZE | OPT_MAXELEM),

	T(LIST,   SET,  UNSPEC, UNSPEC, 0, OPT_MAXELEM),
};


static bool
check_types(struct uci_element *e, struct fw3_ipset *ipset)
{
	int i = 0;
	uint32_t typelist = 0;
	struct fw3_ipset_datatype *type;

	list_for_each_entry(type, &ipset->datatypes, list)
	{
		if (i >= 3)
		{
			warn_section("ipset", ipset, e, "must not have more than 3 datatypes assigned");
			return false;
		}

		typelist |= (type->type << (i++ * 8));
	}

	/* find a suitable storage method if none specified */
	if (ipset->method == FW3_IPSET_METHOD_UNSPEC)
	{
		for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
		{
			/* skip type for v6 if it does not support family */
			if (ipset->family != FW3_FAMILY_V4 &&
			    !(ipset_types[i].optional & OPT_FAMILY))
				continue;

			if (ipset_types[i].types == typelist)
			{
				ipset->method = ipset_types[i].method;

				warn_section("ipset", ipset, e, "defines no storage method, assuming '%s'",
					fw3_ipset_method_names[ipset->method]);

				break;
			}
		}
	}

	//typelist |= ipset->method;

	for (i = 0; i < ARRAY_SIZE(ipset_types); i++)
	{
		if (ipset_types[i].method == ipset->method &&
		    ipset_types[i].types == typelist)
		{
			if (!ipset->external)
			{
				if ((ipset_types[i].required & OPT_IPRANGE) &&
					!ipset->iprange.set)
				{
					warn_section("ipset", ipset, e, "requires an ip range");
					return false;
				}

				if ((ipset_types[i].required & OPT_PORTRANGE) &&
				    !ipset->portrange.set)
				{
					warn_section("ipset", ipset, e, "requires a port range");
					return false;
				}

				if (!(ipset_types[i].required & OPT_IPRANGE) &&
				    ipset->iprange.set)
				{
					warn_section("ipset", ipset, e, "iprange ignored");
					ipset->iprange.set = false;
				}

				if (!(ipset_types[i].required & OPT_PORTRANGE) &&
				    ipset->portrange.set)
				{
					warn_section("ipset", ipset, e, "portrange ignored");
					ipset->portrange.set = false;
				}

				if (!(ipset_types[i].optional & OPT_NETMASK) &&
				    ipset->netmask > 0)
				{
					warn_section("ipset", ipset, e, "netmask ignored");
					ipset->netmask = 0;
				}

				if (!(ipset_types[i].optional & OPT_HASHSIZE) &&
				    ipset->hashsize > 0)
				{
					warn_section("ipset", ipset, e, "hashsize ignored");
					ipset->hashsize = 0;
				}

				if (!(ipset_types[i].optional & OPT_MAXELEM) &&
				    ipset->maxelem > 0)
				{
					warn_section("ipset", ipset, e, "maxelem ignored");
					ipset->maxelem = 0;
				}

				if (!(ipset_types[i].optional & OPT_FAMILY) &&
				    ipset->family != FW3_FAMILY_V4)
				{
					warn_section("ipset", ipset, e, "family ignored");
					ipset->family = FW3_FAMILY_V4;
				}
			}

			return true;
		}
	}

	warn_section("ipset", ipset, e, "has an invalid combination of storage method and matches");
	return false;
}

static bool
check_ipset(struct fw3_state *state, struct fw3_ipset *ipset, struct uci_element *e)
{
	if (!ipset->enabled) {
		return false;
	}

	if (ipset->external)
	{
		if (!*ipset->external)
			ipset->external = NULL;
		else if (!ipset->name)
			ipset->name = ipset->external;
	}

	if (!ipset->name || !*ipset->name)
	{
		warn_section("ipset", ipset, e, "ipset must have a name assigned");
	}
	//else if (fw3_lookup_ipset(state, ipset->name) != NULL)
	//{
	//	warn_section("ipset", ipset, e, "has duplicated set name", ipset->name);
	//}
	else if (ipset->family == FW3_FAMILY_ANY)
	{
		warn_section("ipset", ipset, e, "must not have family 'any'");
	}
	else if (ipset->iprange.set && ipset->family != ipset->iprange.family)
	{
		warn_section("ipset", ipset, e, "has iprange of wrong address family");
	}
	else if (list_empty(&ipset->datatypes))
	{
		warn_section("ipset", ipset, e, "has no datatypes assigned");
	}
	else if (check_types(e, ipset))
	{
		return true;
	}

	return false;
}

static struct fw3_ipset *
fw3_alloc_ipset(struct fw3_state *state)
{
	struct fw3_ipset *ipset;

	ipset = calloc(1, sizeof(*ipset));
	if (!ipset)
		return NULL;

	INIT_LIST_HEAD(&ipset->datatypes);
	INIT_LIST_HEAD(&ipset->entries);

	ipset->comment    = false;
	ipset->counters   = false;
	ipset->enabled    = true;
	ipset->family     = FW3_FAMILY_V4;
	ipset->reload_set = false;
	ipset->timeout    = -1; /* no timeout by default */

	list_add_tail(&ipset->list, &state->ipsets);

	return ipset;
}

void
fw3_load_ipsets(struct fw3_state *state, struct uci_package *p,
		struct blob_attr *a)
{
	struct uci_section *s;
	struct uci_element *e;
	struct fw3_ipset *ipset;
	struct blob_attr *entry;
	unsigned rem;

	INIT_LIST_HEAD(&state->ipsets);

	if (state->disable_ipsets)
		return;

	blob_for_each_attr(entry, a, rem)
	{
		const char *type;
		const char *name = "ubus ipset";

		if (!fw3_attr_parse_name_type(entry, &name, &type))
			continue;

		if (strcmp(type, "ipset"))
			continue;

		ipset = fw3_alloc_ipset(state);
		if (!ipset)
			continue;

		if (!fw3_parse_blob_options(ipset, fw3_ipset_opts, entry, name))
		{
			warn_section("ipset", ipset, NULL, "skipped due to invalid options");
			fw3_free_ipset(ipset);
			continue;
		}

		if (!check_ipset(state, ipset, NULL))
			fw3_free_ipset(ipset);
	}

	uci_foreach_element(&p->sections, e)
	{
		s = uci_to_section(e);

		if (strcmp(s->type, "ipset"))
			continue;

		ipset = fw3_alloc_ipset(state);

		if (!ipset)
			continue;

		if (!fw3_parse_options(ipset, fw3_ipset_opts, s))
			warn_elem(e, "has invalid options");

		if (!check_ipset(state, ipset, e))
			fw3_free_ipset(ipset);
	}
}


static void
load_file(struct fw3_ipset *ipset)
{
	FILE *f;
	char line[128];
	char *p;

	if (!ipset->loadfile)
		return;

	info("   * Loading file %s", ipset->loadfile);

	f = fopen(ipset->loadfile, "r");

	if (!f) {
		info("     ! Skipping due to open error: %s", strerror(errno));
		return;
	}

	while (fgets(line, sizeof(line), f)) {
		p = line;
		while (isspace(*p))
			p++;
		if (*p && *p != '#')
			fw3_pr("add %s %s", ipset->name, line);
	}

	fclose(f);
}

static void
create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
{
	bool first = true;
	struct fw3_setentry *entry;
	struct fw3_ipset_datatype *type;

	info(" * Creating ipset %s", ipset->name);

	first = true;
	fw3_pr("create %s %s", ipset->name, fw3_ipset_method_names[ipset->method]);

	list_for_each_entry(type, &ipset->datatypes, list)
	{
		fw3_pr("%c%s", first ? ':' : ',', fw3_ipset_type_names[type->type]);
		first = false;
	}

	if (ipset->method == FW3_IPSET_METHOD_HASH)
		fw3_pr(" family inet%s", (ipset->family == FW3_FAMILY_V4) ? "" : "6");

	if (ipset->iprange.set)
	{
		fw3_pr(" range %s", fw3_address_to_string(&ipset->iprange, false, true));
	}
	else if (ipset->portrange.set)
	{
		fw3_pr(" range %u-%u",
		       ipset->portrange.port_min, ipset->portrange.port_max);
	}

	if (ipset->timeout >= 0)
		fw3_pr(" timeout %u", ipset->timeout);

	if (ipset->maxelem > 0)
		fw3_pr(" maxelem %u", ipset->maxelem);

	if (ipset->netmask > 0)
		fw3_pr(" netmask %u", ipset->netmask);

	if (ipset->hashsize > 0)
		fw3_pr(" hashsize %u", ipset->hashsize);

	if (ipset->counters)
		fw3_pr(" counters");

	if (ipset->comment)
		fw3_pr(" comment");

	fw3_pr("\n");

	list_for_each_entry(entry, &ipset->entries, list)
		fw3_pr("add %s %s\n", ipset->name, entry->value);

	load_file(ipset);
}

void
fw3_create_ipsets(struct fw3_state *state, enum fw3_family family,
		  bool reload_set)
{
	unsigned int delay, tries;
	bool exec = false;
	struct fw3_ipset *ipset;

	if (state->disable_ipsets)
		return;

	/* spawn ipsets */
	list_for_each_entry(ipset, &state->ipsets, list)
	{
		if (ipset->family != family)
			continue;

		if (ipset->external)
			continue;

		if (fw3_check_ipset(ipset) &&
		    (reload_set && !ipset->reload_set))
			continue;

		if (!exec)
		{
			exec = fw3_command_pipe(false, "ipset", "-exist", "-");

			if (!exec)
				return;
		}

		create_ipset(ipset, state);
	}

	if (exec)
	{
		fw3_pr("quit\n");
		fw3_command_close();
	}

	/* wait a little expontially for ipsets to appear */
	list_for_each_entry(ipset, &state->ipsets, list)
	{
		if (ipset->external)
			continue;

		delay = 5;
		for (tries = 0; !fw3_check_ipset(ipset) && tries < 10; tries++)
			usleep(delay<<1);
	}
}

void
fw3_destroy_ipsets(struct fw3_state *state, enum fw3_family family,
		   bool reload_set)
{
	unsigned int delay, tries;
	bool exec = false;
	struct fw3_ipset *ipset;

	if (state->disable_ipsets)
		return;

	/* destroy ipsets */
	list_for_each_entry(ipset, &state->ipsets, list)
	{
		if (ipset->family != family ||
		    (reload_set && !ipset->reload_set))
			continue;

		if (!exec)
		{
			exec = fw3_command_pipe(false, "ipset", "-exist", "-");

			if (!exec)
				return;
		}

		info(" * Deleting ipset %s", ipset->name);

		fw3_pr("flush %s\n", ipset->name);
		fw3_pr("destroy %s\n", ipset->name);
	}

	if (exec)
	{
		fw3_pr("quit\n");
		fw3_command_close();
	}

	/* wait for ipsets to disappear */
	list_for_each_entry(ipset, &state->ipsets, list)
	{
		if (ipset->external)
			continue;

		delay = 5;
		for (tries = 0; fw3_check_ipset(ipset) && tries < 10; tries++)
			usleep(delay<<1);
	}
}

struct fw3_ipset *
fw3_lookup_ipset(struct fw3_state *state, const char *name)
{
	struct fw3_ipset *s;

	if (list_empty(&state->ipsets))
		return NULL;

	list_for_each_entry(s, &state->ipsets, list)
	{
		if (strcmp(s->name, name))
			continue;

		return s;
	}

	return NULL;
}

bool
fw3_check_ipset(struct fw3_ipset *set)
{
	bool rv = false;

	socklen_t sz;
	int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
	struct ip_set_req_version req_ver;
	struct ip_set_req_get_set req_name;

	if (s < 0 || fcntl(s, F_SETFD, FD_CLOEXEC))
		goto out;

	sz = sizeof(req_ver);
	req_ver.op = IP_SET_OP_VERSION;

	if (getsockopt(s, SOL_IP, SO_IP_SET, &req_ver, &sz))
		goto out;

	sz = sizeof(req_name);
	req_name.op = IP_SET_OP_GET_BYNAME;
	req_name.version = req_ver.version;
	snprintf(req_name.set.name, IPSET_MAXNAMELEN - 1, "%s",
	         set->external ? set->external : set->name);

	if (getsockopt(s, SOL_IP, SO_IP_SET, &req_name, &sz))
		goto out;

	rv = ((sz == sizeof(req_name)) && (req_name.set.index != IPSET_INVALID_ID));

out:
	if (s >= 0)
		close(s);

	return rv;
}

void
fw3_ipsets_update_run_state(enum fw3_family family, struct fw3_state *run_state,
			    struct fw3_state *cfg_state)
{
	struct fw3_ipset *ipset_run, *ipset_cfg;
	bool in_cfg;

	list_for_each_entry(ipset_run, &run_state->ipsets, list) {
		if (ipset_run->family != family)
			continue;

		in_cfg = false;

		list_for_each_entry(ipset_cfg, &cfg_state->ipsets, list) {
			if (ipset_cfg->family != family)
				continue;

			if (strlen(ipset_run->name) ==
			    strlen(ipset_cfg->name) &&
			    !strcmp(ipset_run->name, ipset_cfg->name)) {
				in_cfg = true;
				break;
			}
		}

		/* If a set is found in run_state, but not in cfg_state then the
		 * set has been deleted/renamed. Set reload_set to true to force
		 * the old set to be destroyed in the "stop" fase of the reload.
		 * If the set is found, then copy the reload_set value from the
		 * configuration state. This ensures that the elements are
		 * always updated according to the configuration, and not the
		 * runtime state (which the user might have forgotten).
		 */
		if (!in_cfg)
			ipset_run->reload_set = true;
		else
			ipset_run->reload_set = ipset_cfg->reload_set;
	}
}