blob: 9711f764097932e30244db5a7c6dd329d2dad436 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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
22struct mh_name {
23 const char *name;
24 u_int8_t type;
25};
26
27static 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
46static void print_types_all(void)
47{
48 unsigned int i;
49 printf("Valid MH types:");
50
51 for (i = 0; i < sizeof(mh_names)/sizeof(struct mh_name); 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
60static 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
68static 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
75static unsigned int name_to_type(const char *name)
76{
77 int namelen = strlen(name);
78 unsigned int limit = sizeof(mh_names)/sizeof(struct mh_name);
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
102static 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
126static 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);
137 parse_mh_types(argv[optind-1], 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
150static const char *type_to_name(u_int8_t type)
151{
152 unsigned int i;
153
154 for (i = 0; i < sizeof(mh_names)/sizeof(struct mh_name); i++) {
155 if (mh_names[i].type == type)
156 return mh_names[i].name;
157 }
158
159 return NULL;
160}
161
162static void print_type(u_int8_t type, int numeric)
163{
164 const char *name;
165 if (numeric || !(name = type_to_name(type)))
166 printf("%u", type);
167 else
168 printf("%s", name);
169}
170
171static void print_types(u_int8_t min, u_int8_t max, int invert, int numeric)
172{
173 const char *inv = invert ? "!" : "";
174
175 if (min != 0 || max != 0xFF || invert) {
176 if (min == max) {
177 printf("%s", inv);
178 print_type(min, numeric);
179 } else {
180 printf("%s", inv);
181 print_type(min, numeric);
182 printf(":");
183 print_type(max, numeric);
184 }
185 printf(" ");
186 }
187}
188
189static void mh_print(const void *ip, const struct xt_entry_match *match,
190 int numeric)
191{
192 const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
193
194 printf("mh ");
195 print_types(mhinfo->types[0], mhinfo->types[1],
196 mhinfo->invflags & IP6T_MH_INV_TYPE,
197 numeric);
198 if (mhinfo->invflags & ~IP6T_MH_INV_MASK)
199 printf("Unknown invflags: 0x%X ",
200 mhinfo->invflags & ~IP6T_MH_INV_MASK);
201}
202
203static void mh_save(const void *ip, const struct xt_entry_match *match)
204{
205 const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
206
207 if (mhinfo->types[0] == 0 && mhinfo->types[1] == 0xFF)
208 return;
209
210 if (mhinfo->invflags & IP6T_MH_INV_TYPE)
211 printf("! ");
212
213 if (mhinfo->types[0] != mhinfo->types[1])
214 printf("--mh-type %u:%u ", mhinfo->types[0], mhinfo->types[1]);
215 else
216 printf("--mh-type %u ", mhinfo->types[0]);
217}
218
219static const struct option mh_opts[] = {
220 { "mh-type", 1, NULL, '1' },
221 { .name = NULL }
222};
223
224static struct xtables_match mh_mt6_reg = {
225 .name = "mh",
226 .version = XTABLES_VERSION,
227 .family = NFPROTO_IPV6,
228 .size = XT_ALIGN(sizeof(struct ip6t_mh)),
229 .userspacesize = XT_ALIGN(sizeof(struct ip6t_mh)),
230 .help = mh_help,
231 .init = mh_init,
232 .parse = mh_parse,
233 .print = mh_print,
234 .save = mh_save,
235 .extra_opts = mh_opts,
236};
237
238void _init(void)
239{
240 xtables_register_match(&mh_mt6_reg);
241}