Fork for IVI and add .changes file
[profile/ivi/iptables.git] / extensions / libip6t_mh.c
1 /* Shared library add-on to ip6tables to add mobility header support. */
2 /*
3  * Copyright (C)2006 USAGI/WIDE Project
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Author:
10  *      Masahide NAKAMURA @USAGI <masahide.nakamura.cz@hitachi.com>
11  *
12  * Based on libip6t_{icmpv6,udp}.c
13  */
14 #include <stdio.h>
15 #include <netdb.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <getopt.h>
19 #include <xtables.h>
20 #include <linux/netfilter_ipv6/ip6t_mh.h>
21
22 struct mh_name {
23         const char *name;
24         u_int8_t type;
25 };
26
27 static const struct mh_name mh_names[] = {
28         { "binding-refresh-request", 0, },
29         /* Alias */ { "brr", 0, },
30         { "home-test-init", 1, },
31         /* Alias */ { "hoti", 1, },
32         { "careof-test-init", 2, },
33         /* Alias */ { "coti", 2, },
34         { "home-test", 3, },
35         /* Alias */ { "hot", 3, },
36         { "careof-test", 4, },
37         /* Alias */ { "cot", 4, },
38         { "binding-update", 5, },
39         /* Alias */ { "bu", 5, },
40         { "binding-acknowledgement", 6, },
41         /* Alias */ { "ba", 6, },
42         { "binding-error", 7, },
43         /* Alias */ { "be", 7, },
44 };
45
46 static void print_types_all(void)
47 {
48         unsigned int i;
49         printf("Valid MH types:");
50
51         for (i = 0; i < ARRAY_SIZE(mh_names); ++i) {
52                 if (i && mh_names[i].type == mh_names[i-1].type)
53                         printf(" (%s)", mh_names[i].name);
54                 else
55                         printf("\n%s", mh_names[i].name);
56         }
57         printf("\n");
58 }
59
60 static void mh_help(void)
61 {
62         printf(
63 "mh match options:\n"
64 "[!] --mh-type type[:type]      match mh type\n");
65         print_types_all();
66 }
67
68 static void mh_init(struct xt_entry_match *m)
69 {
70         struct ip6t_mh *mhinfo = (struct ip6t_mh *)m->data;
71
72         mhinfo->types[1] = 0xFF;
73 }
74
75 static unsigned int name_to_type(const char *name)
76 {
77         int namelen = strlen(name);
78         static const unsigned int limit = ARRAY_SIZE(mh_names);
79         unsigned int match = limit;
80         unsigned int i;
81
82         for (i = 0; i < limit; i++) {
83                 if (strncasecmp(mh_names[i].name, name, namelen) == 0) {
84                         int len = strlen(mh_names[i].name);
85                         if (match == limit || len == namelen)
86                                 match = i;
87                 }
88         }
89
90         if (match != limit) {
91                 return mh_names[match].type;
92         } else {
93                 unsigned int number;
94
95                 if (!xtables_strtoui(name, NULL, &number, 0, UINT8_MAX))
96                         xtables_error(PARAMETER_PROBLEM,
97                                    "Invalid MH type `%s'\n", name);
98                 return number;
99         }
100 }
101
102 static void parse_mh_types(const char *mhtype, u_int8_t *types)
103 {
104         char *buffer;
105         char *cp;
106
107         buffer = strdup(mhtype);
108         if ((cp = strchr(buffer, ':')) == NULL)
109                 types[0] = types[1] = name_to_type(buffer);
110         else {
111                 *cp = '\0';
112                 cp++;
113
114                 types[0] = buffer[0] ? name_to_type(buffer) : 0;
115                 types[1] = cp[0] ? name_to_type(cp) : 0xFF;
116
117                 if (types[0] > types[1])
118                         xtables_error(PARAMETER_PROBLEM,
119                                    "Invalid MH type range (min > max)");
120         }
121         free(buffer);
122 }
123
124 #define MH_TYPES 0x01
125
126 static int mh_parse(int c, char **argv, int invert, unsigned int *flags,
127                     const void *entry, struct xt_entry_match **match)
128 {
129         struct ip6t_mh *mhinfo = (struct ip6t_mh *)(*match)->data;
130
131         switch (c) {
132         case '1':
133                 if (*flags & MH_TYPES)
134                         xtables_error(PARAMETER_PROBLEM,
135                                    "Only one `--mh-type' allowed");
136                 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
137                 parse_mh_types(optarg, mhinfo->types);
138                 if (invert)
139                         mhinfo->invflags |= IP6T_MH_INV_TYPE;
140                 *flags |= MH_TYPES;
141                 break;
142
143         default:
144                 return 0;
145         }
146
147         return 1;
148 }
149
150 static const char *type_to_name(u_int8_t type)
151 {
152         unsigned int i;
153
154         for (i = 0; i < ARRAY_SIZE(mh_names); ++i)
155                 if (mh_names[i].type == type)
156                         return mh_names[i].name;
157
158         return NULL;
159 }
160
161 static void print_type(u_int8_t type, int numeric)
162 {
163         const char *name;
164         if (numeric || !(name = type_to_name(type)))
165                 printf("%u", type);
166         else
167                 printf("%s", name);
168 }
169
170 static void print_types(u_int8_t min, u_int8_t max, int invert, int numeric)
171 {
172         const char *inv = invert ? "!" : "";
173
174         if (min != 0 || max != 0xFF || invert) {
175                 if (min == max) {
176                         printf("%s", inv);
177                         print_type(min, numeric);
178                 } else {
179                         printf("%s", inv);
180                         print_type(min, numeric);
181                         printf(":");
182                         print_type(max, numeric);
183                 }
184                 printf(" ");
185         }
186 }
187
188 static void mh_print(const void *ip, const struct xt_entry_match *match,
189                      int numeric)
190 {
191         const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
192
193         printf("mh ");
194         print_types(mhinfo->types[0], mhinfo->types[1],
195                     mhinfo->invflags & IP6T_MH_INV_TYPE,
196                     numeric);
197         if (mhinfo->invflags & ~IP6T_MH_INV_MASK)
198                 printf("Unknown invflags: 0x%X ",
199                        mhinfo->invflags & ~IP6T_MH_INV_MASK);
200 }
201
202 static void mh_save(const void *ip, const struct xt_entry_match *match)
203 {
204         const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
205
206         if (mhinfo->types[0] == 0 && mhinfo->types[1] == 0xFF)
207                 return;
208
209         if (mhinfo->invflags & IP6T_MH_INV_TYPE)
210                 printf("! ");
211
212         if (mhinfo->types[0] != mhinfo->types[1])
213                 printf("--mh-type %u:%u ", mhinfo->types[0], mhinfo->types[1]);
214         else
215                 printf("--mh-type %u ", mhinfo->types[0]);
216 }
217
218 static const struct option mh_opts[] = {
219         { "mh-type", 1, NULL, '1' },
220         { .name = NULL }
221 };
222
223 static struct xtables_match mh_mt6_reg = {
224         .name           = "mh",
225         .version        = XTABLES_VERSION,
226         .family         = NFPROTO_IPV6,
227         .size           = XT_ALIGN(sizeof(struct ip6t_mh)),
228         .userspacesize  = XT_ALIGN(sizeof(struct ip6t_mh)),
229         .help           = mh_help,
230         .init           = mh_init,
231         .parse          = mh_parse,
232         .print          = mh_print,
233         .save           = mh_save,
234         .extra_opts     = mh_opts,
235 };
236
237 void _init(void)
238 {
239         xtables_register_match(&mh_mt6_reg);
240 }