changing trunk/trunk to trunk
[iptables.git] / extensions / libip6t_frag.c
1 /* Shared library add-on to ip6tables to add Fragmentation header support. */
2 #include <stdio.h>
3 #include <netdb.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <getopt.h>
7 #include <errno.h>
8 #include <ip6tables.h>
9 #include <linux/netfilter_ipv6/ip6t_frag.h>
10                                         
11 /* Function which prints out usage message. */
12 static void frag_help(void)
13 {
14         printf(
15 "frag match options:\n"
16 " --fragid [!] id[:id]          match the id (range)\n"
17 " --fraglen [!] length          total length of this header\n"
18 " --fragres                     check the reserved filed, too\n"
19 " --fragfirst                   matches on the first fragment\n"
20 " [--fragmore|--fraglast]       there are more fragments or this\n"
21 "                               is the last one\n");
22 }
23
24 static const struct option frag_opts[] = {
25         { .name = "fragid",    .has_arg = 1, .val = '1' },
26         { .name = "fraglen",   .has_arg = 1, .val = '2' },
27         { .name = "fragres",   .has_arg = 0, .val = '3' },
28         { .name = "fragfirst", .has_arg = 0, .val = '4' },
29         { .name = "fragmore",  .has_arg = 0, .val = '5' },
30         { .name = "fraglast",  .has_arg = 0, .val = '6' },
31         { .name = NULL }
32 };
33
34 static u_int32_t
35 parse_frag_id(const char *idstr, const char *typestr)
36 {
37         unsigned long int id;
38         char* ep;
39
40         id = strtoul(idstr, &ep, 0);
41
42         if ( idstr == ep ) {
43                 exit_error(PARAMETER_PROBLEM,
44                            "FRAG no valid digits in %s `%s'", typestr, idstr);
45         }
46         if ( id == ULONG_MAX  && errno == ERANGE ) {
47                 exit_error(PARAMETER_PROBLEM,
48                            "%s `%s' specified too big: would overflow",
49                            typestr, idstr);
50         }       
51         if ( *idstr != '\0'  && *ep != '\0' ) {
52                 exit_error(PARAMETER_PROBLEM,
53                            "FRAG error parsing %s `%s'", typestr, idstr);
54         }
55         return (u_int32_t) id;
56 }
57
58 static void
59 parse_frag_ids(const char *idstring, u_int32_t *ids)
60 {
61         char *buffer;
62         char *cp;
63
64         buffer = strdup(idstring);
65         if ((cp = strchr(buffer, ':')) == NULL)
66                 ids[0] = ids[1] = parse_frag_id(buffer,"id");
67         else {
68                 *cp = '\0';
69                 cp++;
70
71                 ids[0] = buffer[0] ? parse_frag_id(buffer,"id") : 0;
72                 ids[1] = cp[0] ? parse_frag_id(cp,"id") : 0xFFFFFFFF;
73         }
74         free(buffer);
75 }
76
77 /* Initialize the match. */
78 static void frag_init(struct xt_entry_match *m)
79 {
80         struct ip6t_frag *fraginfo = (struct ip6t_frag *)m->data;
81
82         fraginfo->ids[0] = 0x0L;
83         fraginfo->ids[1] = 0xFFFFFFFF;
84         fraginfo->hdrlen = 0;
85         fraginfo->flags = 0;
86         fraginfo->invflags = 0;
87 }
88
89 /* Function which parses command options; returns true if it
90    ate an option */
91 static int frag_parse(int c, char **argv, int invert, unsigned int *flags,
92                       const void *entry, struct xt_entry_match **match)
93 {
94         struct ip6t_frag *fraginfo = (struct ip6t_frag *)(*match)->data;
95
96         switch (c) {
97         case '1':
98                 if (*flags & IP6T_FRAG_IDS)
99                         exit_error(PARAMETER_PROBLEM,
100                                    "Only one `--fragid' allowed");
101                 check_inverse(optarg, &invert, &optind, 0);
102                 parse_frag_ids(argv[optind-1], fraginfo->ids);
103                 if (invert)
104                         fraginfo->invflags |= IP6T_FRAG_INV_IDS;
105                 fraginfo->flags |= IP6T_FRAG_IDS;
106                 *flags |= IP6T_FRAG_IDS;
107                 break;
108         case '2':
109                 if (*flags & IP6T_FRAG_LEN)
110                         exit_error(PARAMETER_PROBLEM,
111                                    "Only one `--fraglen' allowed");
112                 check_inverse(optarg, &invert, &optind, 0);
113                 fraginfo->hdrlen = parse_frag_id(argv[optind-1], "length");
114                 if (invert)
115                         fraginfo->invflags |= IP6T_FRAG_INV_LEN;
116                 fraginfo->flags |= IP6T_FRAG_LEN;
117                 *flags |= IP6T_FRAG_LEN;
118                 break;
119         case '3':
120                 if (*flags & IP6T_FRAG_RES)
121                         exit_error(PARAMETER_PROBLEM,
122                                    "Only one `--fragres' allowed");
123                 fraginfo->flags |= IP6T_FRAG_RES;
124                 *flags |= IP6T_FRAG_RES;
125                 break;
126         case '4':
127                 if (*flags & IP6T_FRAG_FST)
128                         exit_error(PARAMETER_PROBLEM,
129                                    "Only one `--fragfirst' allowed");
130                 fraginfo->flags |= IP6T_FRAG_FST;
131                 *flags |= IP6T_FRAG_FST;
132                 break;
133         case '5':
134                 if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF)) 
135                         exit_error(PARAMETER_PROBLEM,
136                            "Only one `--fragmore' or `--fraglast' allowed");
137                 fraginfo->flags |= IP6T_FRAG_MF;
138                 *flags |= IP6T_FRAG_MF;
139                 break;
140         case '6':
141                 if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF)) 
142                         exit_error(PARAMETER_PROBLEM,
143                            "Only one `--fragmore' or `--fraglast' allowed");
144                 fraginfo->flags |= IP6T_FRAG_NMF;
145                 *flags |= IP6T_FRAG_NMF;
146                 break;
147         default:
148                 return 0;
149         }
150
151         return 1;
152 }
153
154 static void
155 print_ids(const char *name, u_int32_t min, u_int32_t max,
156             int invert)
157 {
158         const char *inv = invert ? "!" : "";
159
160         if (min != 0 || max != 0xFFFFFFFF || invert) {
161                 printf("%s", name);
162                 if (min == max)
163                         printf(":%s%u ", inv, min);
164                 else
165                         printf("s:%s%u:%u ", inv, min, max);
166         }
167 }
168
169 /* Prints out the union ip6t_matchinfo. */
170 static void frag_print(const void *ip, const struct xt_entry_match *match,
171                        int numeric)
172 {
173         const struct ip6t_frag *frag = (struct ip6t_frag *)match->data;
174
175         printf("frag ");
176         print_ids("id", frag->ids[0], frag->ids[1],
177                     frag->invflags & IP6T_FRAG_INV_IDS);
178
179         if (frag->flags & IP6T_FRAG_LEN) {
180                 printf("length:%s%u ",
181                         frag->invflags & IP6T_FRAG_INV_LEN ? "!" : "",
182                         frag->hdrlen);
183         }
184
185         if (frag->flags & IP6T_FRAG_RES)
186                 printf("reserved ");
187
188         if (frag->flags & IP6T_FRAG_FST)
189                 printf("first ");
190
191         if (frag->flags & IP6T_FRAG_MF)
192                 printf("more ");
193
194         if (frag->flags & IP6T_FRAG_NMF)
195                 printf("last ");
196
197         if (frag->invflags & ~IP6T_FRAG_INV_MASK)
198                 printf("Unknown invflags: 0x%X ",
199                        frag->invflags & ~IP6T_FRAG_INV_MASK);
200 }
201
202 /* Saves the union ip6t_matchinfo in parsable form to stdout. */
203 static void frag_save(const void *ip, const struct xt_entry_match *match)
204 {
205         const struct ip6t_frag *fraginfo = (struct ip6t_frag *)match->data;
206
207         if (!(fraginfo->ids[0] == 0
208             && fraginfo->ids[1] == 0xFFFFFFFF)) {
209                 printf("--fragid %s", 
210                         (fraginfo->invflags & IP6T_FRAG_INV_IDS) ? "! " : "");
211                 if (fraginfo->ids[0]
212                     != fraginfo->ids[1])
213                         printf("%u:%u ",
214                                fraginfo->ids[0],
215                                fraginfo->ids[1]);
216                 else
217                         printf("%u ",
218                                fraginfo->ids[0]);
219         }
220
221         if (fraginfo->flags & IP6T_FRAG_LEN) {
222                 printf("--fraglen %s%u ", 
223                         (fraginfo->invflags & IP6T_FRAG_INV_LEN) ? "! " : "", 
224                         fraginfo->hdrlen);
225         }
226
227         if (fraginfo->flags & IP6T_FRAG_RES)
228                 printf("--fragres ");
229
230         if (fraginfo->flags & IP6T_FRAG_FST)
231                 printf("--fragfirst ");
232
233         if (fraginfo->flags & IP6T_FRAG_MF)
234                 printf("--fragmore ");
235
236         if (fraginfo->flags & IP6T_FRAG_NMF)
237                 printf("--fraglast ");
238 }
239
240 static struct xtables_match frag_mt6_reg = {
241         .name          = "frag",
242         .version       = XTABLES_VERSION,
243         .family        = PF_INET6,
244         .size          = XT_ALIGN(sizeof(struct ip6t_frag)),
245         .userspacesize = XT_ALIGN(sizeof(struct ip6t_frag)),
246         .help          = frag_help,
247         .init          = frag_init,
248         .parse         = frag_parse,
249         .print         = frag_print,
250         .save          = frag_save,
251         .extra_opts    = frag_opts,
252 };
253
254 void
255 _init(void)
256 {
257         xtables_register_match(&frag_mt6_reg);
258 }