libip6t_frag.c revision 6944f2c8190f1c4319aeac748470c71b0ba45025
10de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#include <stdio.h>
20de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#include <xtables.h>
30de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#include <linux/netfilter_ipv6/ip6t_frag.h>
40de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)
50de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)enum {
60de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	O_FRAGID = 0,
70de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	O_FRAGLEN,
80de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	O_FRAGRES,
90de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	O_FRAGFIRST,
100de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	O_FRAGMORE,
110de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	O_FRAGLAST,
120de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	F_FRAGMORE = 1 << O_FRAGMORE,
130de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	F_FRAGLAST = 1 << O_FRAGLAST,
140de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)};
150de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)static void frag_help(void)
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles){
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)	printf(
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)"frag match options:\n"
200de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)"[!] --fragid id[:id]           match the id (range)\n"
210de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)"[!] --fraglen length           total length of this header\n"
220de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)" --fragres                     check the reserved field too\n"
230de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)" --fragfirst                   matches on the first fragment\n"
240de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)" [--fragmore|--fraglast]       there are more fragments or this\n"
250de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)"                               is the last one\n");
260de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)}
270de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)
280de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#define s struct ip6t_frag
290de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)static const struct xt_option_entry frag_opts[] = {
300de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	{.name = "fragid", .id = O_FRAGID, .type = XTTYPE_UINT32RC,
310de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, ids)},
320de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	{.name = "fraglen", .id = O_FRAGLEN, .type = XTTYPE_UINT32,
330de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, hdrlen)},
340de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	{.name = "fragres", .id = O_FRAGRES, .type = XTTYPE_NONE},
350de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	{.name = "fragfirst", .id = O_FRAGFIRST, .type = XTTYPE_NONE},
360de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	{.name = "fragmore", .id = O_FRAGMORE, .type = XTTYPE_NONE,
370de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	 .excl = F_FRAGLAST},
380de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	{.name = "fraglast", .id = O_FRAGLAST, .type = XTTYPE_NONE,
390de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	 .excl = F_FRAGMORE},
400de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	XTOPT_TABLEEND,
410de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)};
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#undef s
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)static void frag_parse(struct xt_option_call *cb)
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles){
460de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	struct ip6t_frag *fraginfo = cb->data;
470de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)
480de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	xtables_option_parse(cb);
490de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	switch (cb->entry->id) {
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)	case O_FRAGID:
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)		if (cb->nvals == 1)
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)			fraginfo->ids[1] = fraginfo->ids[0];
530de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)		break;
540de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	case O_FRAGRES:
550de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)		fraginfo->flags |= IP6T_FRAG_RES;
560de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)		break;
570de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	case O_FRAGFIRST:
580de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)		fraginfo->flags |= IP6T_FRAG_FST;
590de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)		break;
600de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)	case O_FRAGMORE:
610de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)		fraginfo->flags |= IP6T_FRAG_MF;
620de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)		break;
63	case O_FRAGLAST:
64		fraginfo->flags |= IP6T_FRAG_NMF;
65		break;
66	}
67}
68
69static void
70print_ids(const char *name, uint32_t min, uint32_t max,
71	    int invert)
72{
73	const char *inv = invert ? "!" : "";
74
75	if (min != 0 || max != 0xFFFFFFFF || invert) {
76		printf("%s", name);
77		if (min == max)
78			printf(":%s%u", inv, min);
79		else
80			printf("s:%s%u:%u", inv, min, max);
81	}
82}
83
84static void frag_print(const void *ip, const struct xt_entry_match *match,
85                       int numeric)
86{
87	const struct ip6t_frag *frag = (struct ip6t_frag *)match->data;
88
89	printf(" frag ");
90	print_ids("id", frag->ids[0], frag->ids[1],
91		    frag->invflags & IP6T_FRAG_INV_IDS);
92
93	if (frag->flags & IP6T_FRAG_LEN) {
94		printf(" length:%s%u",
95			frag->invflags & IP6T_FRAG_INV_LEN ? "!" : "",
96			frag->hdrlen);
97	}
98
99	if (frag->flags & IP6T_FRAG_RES)
100		printf(" reserved");
101
102	if (frag->flags & IP6T_FRAG_FST)
103		printf(" first");
104
105	if (frag->flags & IP6T_FRAG_MF)
106		printf(" more");
107
108	if (frag->flags & IP6T_FRAG_NMF)
109		printf(" last");
110
111	if (frag->invflags & ~IP6T_FRAG_INV_MASK)
112		printf(" Unknown invflags: 0x%X",
113		       frag->invflags & ~IP6T_FRAG_INV_MASK);
114}
115
116static void frag_save(const void *ip, const struct xt_entry_match *match)
117{
118	const struct ip6t_frag *fraginfo = (struct ip6t_frag *)match->data;
119
120	if (!(fraginfo->ids[0] == 0
121	    && fraginfo->ids[1] == 0xFFFFFFFF)) {
122		printf("%s --fragid ",
123			(fraginfo->invflags & IP6T_FRAG_INV_IDS) ? " !" : "");
124		if (fraginfo->ids[0]
125		    != fraginfo->ids[1])
126			printf("%u:%u",
127			       fraginfo->ids[0],
128			       fraginfo->ids[1]);
129		else
130			printf("%u",
131			       fraginfo->ids[0]);
132	}
133
134	if (fraginfo->flags & IP6T_FRAG_LEN) {
135		printf("%s --fraglen %u",
136			(fraginfo->invflags & IP6T_FRAG_INV_LEN) ? " !" : "",
137			fraginfo->hdrlen);
138	}
139
140	if (fraginfo->flags & IP6T_FRAG_RES)
141		printf(" --fragres");
142
143	if (fraginfo->flags & IP6T_FRAG_FST)
144		printf(" --fragfirst");
145
146	if (fraginfo->flags & IP6T_FRAG_MF)
147		printf(" --fragmore");
148
149	if (fraginfo->flags & IP6T_FRAG_NMF)
150		printf(" --fraglast");
151}
152
153static struct xtables_match frag_mt6_reg = {
154	.name          = "frag",
155	.version       = XTABLES_VERSION,
156	.family        = NFPROTO_IPV6,
157	.size          = XT_ALIGN(sizeof(struct ip6t_frag)),
158	.userspacesize = XT_ALIGN(sizeof(struct ip6t_frag)),
159	.help          = frag_help,
160	.print         = frag_print,
161	.save          = frag_save,
162	.x6_parse      = frag_parse,
163	.x6_options    = frag_opts,
164};
165
166void
167_init(void)
168{
169	xtables_register_match(&frag_mt6_reg);
170}
171