libip6t_rt.c 7.97 KB
Newer Older
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <xtables.h>
#include <linux/netfilter_ipv6/ip6t_rt.h>
#include <arpa/inet.h>

enum {
	O_RT_TYPE = 0,
	O_RT_SEGSLEFT,
	O_RT_LEN,
	O_RT0RES,
	O_RT0ADDRS,
	O_RT0NSTRICT,
	F_RT_TYPE  = 1 << O_RT_TYPE,
	F_RT0ADDRS = 1 << O_RT0ADDRS,
};

static void rt_help(void)
{
	printf(
"rt match options:\n"
"[!] --rt-type type             match the type\n"
"[!] --rt-segsleft num[:num]    match the Segments Left field (range)\n"
"[!] --rt-len length            total length of this header\n"
" --rt-0-res                    check the reserved field too (type 0)\n"
" --rt-0-addrs ADDR[,ADDR...]   Type=0 addresses (list, max: %d)\n"
" --rt-0-not-strict             List of Type=0 addresses not a strict list\n",
IP6T_RT_HOPS);
}

#define s struct ip6t_rt
static const struct xt_option_entry rt_opts[] = {
	{.name = "rt-type", .id = O_RT_TYPE, .type = XTTYPE_UINT32,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, rt_type)},
	{.name = "rt-segsleft", .id = O_RT_SEGSLEFT, .type = XTTYPE_UINT32RC,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, segsleft)},
	{.name = "rt-len", .id = O_RT_LEN, .type = XTTYPE_UINT32,
	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, hdrlen)},
	{.name = "rt-0-res", .id = O_RT0RES, .type = XTTYPE_NONE},
	{.name = "rt-0-addrs", .id = O_RT0ADDRS, .type = XTTYPE_STRING},
	{.name = "rt-0-not-strict", .id = O_RT0NSTRICT, .type = XTTYPE_NONE},
	XTOPT_TABLEEND,
};
#undef s

static const char *
addr_to_numeric(const struct in6_addr *addrp)
{
	static char buf[50+1];
	return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
}

static struct in6_addr *
numeric_to_addr(const char *num)
{
	static struct in6_addr ap;
	int err;

	if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
		return &ap;
#ifdef DEBUG
	fprintf(stderr, "\nnumeric2addr: %d\n", err);
#endif
	xtables_error(PARAMETER_PROBLEM, "bad address: %s", num);

	return (struct in6_addr *)NULL;
}


static int
parse_addresses(const char *addrstr, struct in6_addr *addrp)
{
        char *buffer, *cp, *next;
        unsigned int i;
	
	buffer = strdup(addrstr);
	if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
			
        for (cp=buffer, i=0; cp && i<IP6T_RT_HOPS; cp=next,i++)
        {
                next=strchr(cp, ',');
                if (next) *next++='\0';
		memcpy(&(addrp[i]), numeric_to_addr(cp), sizeof(struct in6_addr));
#if DEBUG
		printf("addr str: %s\n", cp);
		printf("addr ip6: %s\n", addr_to_numeric((numeric_to_addr(cp))));
		printf("addr [%d]: %s\n", i, addr_to_numeric(&(addrp[i])));
#endif
	}
	if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified");

	free(buffer);

#if DEBUG
	printf("addr nr: %d\n", i);
#endif

	return i;
}

102
103
104
105
106
107
108
static void rt_init(struct xt_entry_match *m)
{
	struct ip6t_rt *rtinfo = (void *)m->data;

	rtinfo->segsleft[1] = ~0U;
}

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
static void rt_parse(struct xt_option_call *cb)
{
	struct ip6t_rt *rtinfo = cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_RT_TYPE:
		if (cb->invert)
			rtinfo->invflags |= IP6T_RT_INV_TYP;
		rtinfo->flags |= IP6T_RT_TYP;
		break;
	case O_RT_SEGSLEFT:
		if (cb->nvals == 1)
			rtinfo->segsleft[1] = rtinfo->segsleft[0];
		if (cb->invert)
			rtinfo->invflags |= IP6T_RT_INV_SGS;
		rtinfo->flags |= IP6T_RT_SGS;
		break;
	case O_RT_LEN:
		if (cb->invert)
			rtinfo->invflags |= IP6T_RT_INV_LEN;
		rtinfo->flags |= IP6T_RT_LEN;
		break;
	case O_RT0RES:
		if (!(cb->xflags & F_RT_TYPE) || rtinfo->rt_type != 0 ||
		    rtinfo->invflags & IP6T_RT_INV_TYP)
			xtables_error(PARAMETER_PROBLEM,
				   "`--rt-type 0' required before `--rt-0-res'");
		rtinfo->flags |= IP6T_RT_RES;
		break;
	case O_RT0ADDRS:
		if (!(cb->xflags & F_RT_TYPE) || rtinfo->rt_type != 0 ||
		    rtinfo->invflags & IP6T_RT_INV_TYP)
			xtables_error(PARAMETER_PROBLEM,
				   "`--rt-type 0' required before `--rt-0-addrs'");
		rtinfo->addrnr = parse_addresses(cb->arg, rtinfo->addrs);
		rtinfo->flags |= IP6T_RT_FST;
		break;
	case O_RT0NSTRICT:
		if (!(cb->xflags & F_RT0ADDRS))
			xtables_error(PARAMETER_PROBLEM,
				   "`--rt-0-addr ...' required before `--rt-0-not-strict'");
		rtinfo->flags |= IP6T_RT_FST_NSTRICT;
		break;
	}
}

static void
print_nums(const char *name, uint32_t min, uint32_t max,
	    int invert)
{
	const char *inv = invert ? "!" : "";

	if (min != 0 || max != 0xFFFFFFFF || invert) {
		printf(" %s", name);
		if (min == max) {
			printf(":%s", inv);
			printf("%u", min);
		} else {
			printf("s:%s", inv);
			printf("%u",min);
			printf(":");
			printf("%u",max);
		}
	}
}

static void
print_addresses(unsigned int addrnr, struct in6_addr *addrp)
{
	unsigned int i;

	for(i=0; i<addrnr; i++){
		printf("%c%s", (i==0)?' ':',', addr_to_numeric(&(addrp[i])));
	}
}

static void rt_print(const void *ip, const struct xt_entry_match *match,
                     int numeric)
{
	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;

	printf(" rt");
	if (rtinfo->flags & IP6T_RT_TYP)
	    printf(" type:%s%d", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "",
		    rtinfo->rt_type);
	print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1],
		    rtinfo->invflags & IP6T_RT_INV_SGS);
	if (rtinfo->flags & IP6T_RT_LEN) {
		printf(" length");
		printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : "");
		printf("%u", rtinfo->hdrlen);
	}
	if (rtinfo->flags & IP6T_RT_RES) printf(" reserved");
	if (rtinfo->flags & IP6T_RT_FST) printf(" 0-addrs");
	print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
	if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" 0-not-strict");
	if (rtinfo->invflags & ~IP6T_RT_INV_MASK)
		printf(" Unknown invflags: 0x%X",
		       rtinfo->invflags & ~IP6T_RT_INV_MASK);
}

static void rt_save(const void *ip, const struct xt_entry_match *match)
{
	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data;

	if (rtinfo->flags & IP6T_RT_TYP) {
		printf("%s --rt-type %u",
			(rtinfo->invflags & IP6T_RT_INV_TYP) ? " !" : "",
			rtinfo->rt_type);
	}

	if (!(rtinfo->segsleft[0] == 0
	    && rtinfo->segsleft[1] == 0xFFFFFFFF)) {
		printf("%s --rt-segsleft ",
			(rtinfo->invflags & IP6T_RT_INV_SGS) ? " !" : "");
		if (rtinfo->segsleft[0]
		    != rtinfo->segsleft[1])
			printf("%u:%u",
			       rtinfo->segsleft[0],
			       rtinfo->segsleft[1]);
		else
			printf("%u",
			       rtinfo->segsleft[0]);
	}

	if (rtinfo->flags & IP6T_RT_LEN) {
		printf("%s --rt-len %u",
			(rtinfo->invflags & IP6T_RT_INV_LEN) ? " !" : "", 
			rtinfo->hdrlen);
	}

	if (rtinfo->flags & IP6T_RT_RES) printf(" --rt-0-res");
	if (rtinfo->flags & IP6T_RT_FST) printf(" --rt-0-addrs");
	print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs);
	if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" --rt-0-not-strict");

}

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
static int rt_xlate(struct xt_xlate *xl,
		    const struct xt_xlate_mt_params *params)
{
	const struct ip6t_rt *rtinfo = (struct ip6t_rt *)params->match->data;
	char *space = "";

	if (rtinfo->flags & IP6T_RT_TYP) {
		xt_xlate_add(xl, "rt type%s %u",
			     (rtinfo->invflags & IP6T_RT_INV_TYP) ? " !=" : "",
			      rtinfo->rt_type);
		space = " ";
	}

	if (!(rtinfo->segsleft[0] == 0 && rtinfo->segsleft[1] == 0xFFFFFFFF)) {
		xt_xlate_add(xl, "%srt seg-left%s ", space,
			     (rtinfo->invflags & IP6T_RT_INV_SGS) ? " !=" : "");

		if (rtinfo->segsleft[0] != rtinfo->segsleft[1])
			xt_xlate_add(xl, "%u-%u", rtinfo->segsleft[0],
					rtinfo->segsleft[1]);
		else
			xt_xlate_add(xl, "%u", rtinfo->segsleft[0]);
		space = " ";
	}

	if (rtinfo->flags & IP6T_RT_LEN) {
		xt_xlate_add(xl, "%srt hdrlength%s %u", space,
			     (rtinfo->invflags & IP6T_RT_INV_LEN) ? " !=" : "",
			      rtinfo->hdrlen);
	}

	if (rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST | IP6T_RT_FST_NSTRICT))
		return 0;

	return 1;
}

285
286
287
288
289
290
291
static struct xtables_match rt_mt6_reg = {
	.name		= "rt",
	.version	= XTABLES_VERSION,
	.family		= NFPROTO_IPV6,
	.size		= XT_ALIGN(sizeof(struct ip6t_rt)),
	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_rt)),
	.help		= rt_help,
292
	.init		= rt_init,
293
294
295
296
	.x6_parse	= rt_parse,
	.print		= rt_print,
	.save		= rt_save,
	.x6_options	= rt_opts,
297
	.xlate		= rt_xlate,
298
299
300
301
302
303
304
};

void
_init(void)
{
	xtables_register_match(&rt_mt6_reg);
}