blob: 460f9e4741143dc094a3787181f1442ccf4b9696 [file] [log] [blame]
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +00001/* 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 */
Jan Engelhardt32b8e612010-07-23 21:16:14 +020014#include <stdbool.h>
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000015#include <stdio.h>
16#include <netdb.h>
17#include <string.h>
18#include <stdlib.h>
19#include <getopt.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010020#include <xtables.h>
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000021#include <linux/netfilter_ipv6/ip6t_mh.h>
22
23struct mh_name {
24 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +010025 uint8_t type;
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000026};
27
28static const struct mh_name mh_names[] = {
29 { "binding-refresh-request", 0, },
30 /* Alias */ { "brr", 0, },
31 { "home-test-init", 1, },
32 /* Alias */ { "hoti", 1, },
33 { "careof-test-init", 2, },
34 /* Alias */ { "coti", 2, },
35 { "home-test", 3, },
36 /* Alias */ { "hot", 3, },
37 { "careof-test", 4, },
38 /* Alias */ { "cot", 4, },
39 { "binding-update", 5, },
40 /* Alias */ { "bu", 5, },
41 { "binding-acknowledgement", 6, },
42 /* Alias */ { "ba", 6, },
43 { "binding-error", 7, },
44 /* Alias */ { "be", 7, },
45};
46
47static void print_types_all(void)
48{
49 unsigned int i;
50 printf("Valid MH types:");
51
Jan Engelhardt2c69b552009-04-30 19:32:02 +020052 for (i = 0; i < ARRAY_SIZE(mh_names); ++i) {
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000053 if (i && mh_names[i].type == mh_names[i-1].type)
54 printf(" (%s)", mh_names[i].name);
55 else
56 printf("\n%s", mh_names[i].name);
57 }
58 printf("\n");
59}
60
Jan Engelhardt997045f2007-10-04 16:29:21 +000061static void mh_help(void)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000062{
63 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020064"mh match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020065"[!] --mh-type type[:type] match mh type\n");
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000066 print_types_all();
67}
68
Jan Engelhardt997045f2007-10-04 16:29:21 +000069static void mh_init(struct xt_entry_match *m)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000070{
71 struct ip6t_mh *mhinfo = (struct ip6t_mh *)m->data;
72
73 mhinfo->types[1] = 0xFF;
74}
75
76static unsigned int name_to_type(const char *name)
77{
78 int namelen = strlen(name);
Jan Engelhardt2c69b552009-04-30 19:32:02 +020079 static const unsigned int limit = ARRAY_SIZE(mh_names);
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000080 unsigned int match = limit;
81 unsigned int i;
82
83 for (i = 0; i < limit; i++) {
84 if (strncasecmp(mh_names[i].name, name, namelen) == 0) {
85 int len = strlen(mh_names[i].name);
86 if (match == limit || len == namelen)
87 match = i;
88 }
89 }
90
91 if (match != limit) {
92 return mh_names[match].type;
93 } else {
94 unsigned int number;
95
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010096 if (!xtables_strtoui(name, NULL, &number, 0, UINT8_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010097 xtables_error(PARAMETER_PROBLEM,
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000098 "Invalid MH type `%s'\n", name);
99 return number;
100 }
101}
102
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100103static void parse_mh_types(const char *mhtype, uint8_t *types)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000104{
105 char *buffer;
106 char *cp;
107
108 buffer = strdup(mhtype);
109 if ((cp = strchr(buffer, ':')) == NULL)
110 types[0] = types[1] = name_to_type(buffer);
111 else {
112 *cp = '\0';
113 cp++;
114
115 types[0] = buffer[0] ? name_to_type(buffer) : 0;
116 types[1] = cp[0] ? name_to_type(cp) : 0xFF;
117
118 if (types[0] > types[1])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100119 xtables_error(PARAMETER_PROBLEM,
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000120 "Invalid MH type range (min > max)");
121 }
122 free(buffer);
123}
124
125#define MH_TYPES 0x01
126
Jan Engelhardt997045f2007-10-04 16:29:21 +0000127static int mh_parse(int c, char **argv, int invert, unsigned int *flags,
128 const void *entry, struct xt_entry_match **match)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000129{
130 struct ip6t_mh *mhinfo = (struct ip6t_mh *)(*match)->data;
131
132 switch (c) {
133 case '1':
134 if (*flags & MH_TYPES)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100135 xtables_error(PARAMETER_PROBLEM,
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000136 "Only one `--mh-type' allowed");
Jan Engelhardtbf971282009-11-03 19:55:11 +0100137 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
Jan Engelhardtbbe83862009-10-24 00:45:33 +0200138 parse_mh_types(optarg, mhinfo->types);
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000139 if (invert)
140 mhinfo->invflags |= IP6T_MH_INV_TYPE;
141 *flags |= MH_TYPES;
142 break;
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000143 }
144
145 return 1;
146}
147
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100148static const char *type_to_name(uint8_t type)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000149{
150 unsigned int i;
151
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200152 for (i = 0; i < ARRAY_SIZE(mh_names); ++i)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000153 if (mh_names[i].type == type)
154 return mh_names[i].name;
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000155
156 return NULL;
157}
158
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100159static void print_type(uint8_t type, int numeric)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000160{
161 const char *name;
162 if (numeric || !(name = type_to_name(type)))
163 printf("%u", type);
164 else
165 printf("%s", name);
166}
167
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100168static void print_types(uint8_t min, uint8_t max, int invert, int numeric)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000169{
170 const char *inv = invert ? "!" : "";
171
172 if (min != 0 || max != 0xFF || invert) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100173 printf(" ");
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000174 if (min == max) {
175 printf("%s", inv);
176 print_type(min, numeric);
177 } else {
178 printf("%s", inv);
179 print_type(min, numeric);
180 printf(":");
181 print_type(max, numeric);
182 }
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000183 }
184}
185
Jan Engelhardt997045f2007-10-04 16:29:21 +0000186static void mh_print(const void *ip, const struct xt_entry_match *match,
187 int numeric)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000188{
189 const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
190
Jan Engelhardt73866352010-12-18 02:04:59 +0100191 printf(" mh");
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000192 print_types(mhinfo->types[0], mhinfo->types[1],
193 mhinfo->invflags & IP6T_MH_INV_TYPE,
194 numeric);
195 if (mhinfo->invflags & ~IP6T_MH_INV_MASK)
Jan Engelhardt73866352010-12-18 02:04:59 +0100196 printf(" Unknown invflags: 0x%X",
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000197 mhinfo->invflags & ~IP6T_MH_INV_MASK);
198}
199
Jan Engelhardt997045f2007-10-04 16:29:21 +0000200static void mh_save(const void *ip, const struct xt_entry_match *match)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000201{
202 const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
203
204 if (mhinfo->types[0] == 0 && mhinfo->types[1] == 0xFF)
205 return;
206
207 if (mhinfo->invflags & IP6T_MH_INV_TYPE)
Jan Engelhardt73866352010-12-18 02:04:59 +0100208 printf(" !");
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000209
210 if (mhinfo->types[0] != mhinfo->types[1])
Jan Engelhardt73866352010-12-18 02:04:59 +0100211 printf(" --mh-type %u:%u", mhinfo->types[0], mhinfo->types[1]);
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000212 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100213 printf(" --mh-type %u", mhinfo->types[0]);
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000214}
215
Jan Engelhardt997045f2007-10-04 16:29:21 +0000216static const struct option mh_opts[] = {
Jan Engelhardt32b8e612010-07-23 21:16:14 +0200217 {.name = "mh-type", .has_arg = true, .val = '1'},
218 XT_GETOPT_TABLEEND,
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000219};
220
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200221static struct xtables_match mh_mt6_reg = {
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000222 .name = "mh",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200223 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100224 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200225 .size = XT_ALIGN(sizeof(struct ip6t_mh)),
226 .userspacesize = XT_ALIGN(sizeof(struct ip6t_mh)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000227 .help = mh_help,
228 .init = mh_init,
229 .parse = mh_parse,
230 .print = mh_print,
231 .save = mh_save,
232 .extra_opts = mh_opts,
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000233};
234
235void _init(void)
236{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200237 xtables_register_match(&mh_mt6_reg);
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000238}