blob: adeeec7994a62cadfba09d5ee9970edf72ec6613 [file] [log] [blame]
Rusty Russell52451822000-08-27 07:47:46 +00001/* Shared library add-on to iptables to add ESP 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>
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +00008#include <xtables.h>
9#include <linux/netfilter/xt_esp.h>
Rusty Russell52451822000-08-27 07:47:46 +000010
11/* Function which prints out usage message. */
12static void
13help(void)
14{
15 printf(
16"ESP v%s options:\n"
17" --espspi [!] spi[:spi]\n"
18" match spi (range)\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000019IPTABLES_VERSION);
Rusty Russell52451822000-08-27 07:47:46 +000020}
21
Jan Engelhardt661f1122007-07-30 14:46:51 +000022static const struct option opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000023 { "espspi", 1, NULL, '1' },
24 { }
Rusty Russell52451822000-08-27 07:47:46 +000025};
26
27static u_int32_t
28parse_esp_spi(const char *spistr)
29{
30 unsigned long int spi;
31 char* ep;
32
33 spi = strtoul(spistr,&ep,0) ;
34
35 if ( spistr == ep ) {
36 exit_error(PARAMETER_PROBLEM,
37 "ESP no valid digits in spi `%s'", spistr);
38 }
39 if ( spi == ULONG_MAX && errno == ERANGE ) {
40 exit_error(PARAMETER_PROBLEM,
41 "spi `%s' specified too big: would overflow", spistr);
42 }
43 if ( *spistr != '\0' && *ep != '\0' ) {
44 exit_error(PARAMETER_PROBLEM,
45 "ESP error parsing spi `%s'", spistr);
46 }
47 return (u_int32_t) spi;
48}
49
50static void
51parse_esp_spis(const char *spistring, u_int32_t *spis)
52{
53 char *buffer;
54 char *cp;
55
56 buffer = strdup(spistring);
57 if ((cp = strchr(buffer, ':')) == NULL)
58 spis[0] = spis[1] = parse_esp_spi(buffer);
59 else {
60 *cp = '\0';
61 cp++;
62
63 spis[0] = buffer[0] ? parse_esp_spi(buffer) : 0;
64 spis[1] = cp[0] ? parse_esp_spi(cp) : 0xFFFFFFFF;
Yasuyuki KOZAKAI2c627cf2006-04-15 03:11:15 +000065 if (spis[0] > spis[1])
66 exit_error(PARAMETER_PROBLEM,
67 "Invalid ESP spi range: %s", spistring);
Rusty Russell52451822000-08-27 07:47:46 +000068 }
69 free(buffer);
70}
71
72/* Initialize the match. */
73static void
Peter Rileyea146a92007-09-02 13:09:07 +000074init(struct xt_entry_match *m)
Rusty Russell52451822000-08-27 07:47:46 +000075{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000076 struct xt_esp *espinfo = (struct xt_esp *)m->data;
Rusty Russell52451822000-08-27 07:47:46 +000077
78 espinfo->spis[1] = 0xFFFFFFFF;
79}
80
81#define ESP_SPI 0x01
82
83/* Function which parses command options; returns true if it
84 ate an option */
85static int
86parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000087 const void *entry,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000088 struct xt_entry_match **match)
Rusty Russell52451822000-08-27 07:47:46 +000089{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000090 struct xt_esp *espinfo = (struct xt_esp *)(*match)->data;
Rusty Russell52451822000-08-27 07:47:46 +000091
92 switch (c) {
93 case '1':
94 if (*flags & ESP_SPI)
95 exit_error(PARAMETER_PROBLEM,
Harald Weltef0ac8142002-03-26 12:50:28 +000096 "Only one `--espspi' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +000097 check_inverse(optarg, &invert, &optind, 0);
Rusty Russell52451822000-08-27 07:47:46 +000098 parse_esp_spis(argv[optind-1], espinfo->spis);
99 if (invert)
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000100 espinfo->invflags |= XT_ESP_INV_SPI;
Rusty Russell52451822000-08-27 07:47:46 +0000101 *flags |= ESP_SPI;
102 break;
103 default:
104 return 0;
105 }
106
107 return 1;
108}
109
110/* Final check; we don't care. */
111static void
112final_check(unsigned int flags)
113{
114}
115
116static void
117print_spis(const char *name, u_int32_t min, u_int32_t max,
118 int invert)
119{
120 const char *inv = invert ? "!" : "";
121
122 if (min != 0 || max != 0xFFFFFFFF || invert) {
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000123 if (min == max)
124 printf("%s:%s%u ", name, inv, min);
125 else
126 printf("%ss:%s%u:%u ", name, inv, min, max);
Rusty Russell52451822000-08-27 07:47:46 +0000127 }
128}
129
130/* Prints out the union ipt_matchinfo. */
131static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000132print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +0000133 const struct xt_entry_match *match, int numeric)
Rusty Russell52451822000-08-27 07:47:46 +0000134{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000135 const struct xt_esp *esp = (struct xt_esp *)match->data;
Rusty Russell52451822000-08-27 07:47:46 +0000136
137 printf("esp ");
138 print_spis("spi", esp->spis[0], esp->spis[1],
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000139 esp->invflags & XT_ESP_INV_SPI);
140 if (esp->invflags & ~XT_ESP_INV_MASK)
Rusty Russell52451822000-08-27 07:47:46 +0000141 printf("Unknown invflags: 0x%X ",
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000142 esp->invflags & ~XT_ESP_INV_MASK);
Rusty Russell52451822000-08-27 07:47:46 +0000143}
144
145/* Saves the union ipt_matchinfo in parsable form to stdout. */
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000146static void save(const void *ip, const struct xt_entry_match *match)
Rusty Russell52451822000-08-27 07:47:46 +0000147{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000148 const struct xt_esp *espinfo = (struct xt_esp *)match->data;
Rusty Russell52451822000-08-27 07:47:46 +0000149
Harald Weltef0ac8142002-03-26 12:50:28 +0000150 if (!(espinfo->spis[0] == 0
151 && espinfo->spis[1] == 0xFFFFFFFF)) {
152 printf("--espspi %s",
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000153 (espinfo->invflags & XT_ESP_INV_SPI) ? "! " : "");
Rusty Russell52451822000-08-27 07:47:46 +0000154 if (espinfo->spis[0]
155 != espinfo->spis[1])
Harald Weltef0ac8142002-03-26 12:50:28 +0000156 printf("%u:%u ",
Rusty Russell52451822000-08-27 07:47:46 +0000157 espinfo->spis[0],
158 espinfo->spis[1]);
159 else
Harald Weltef0ac8142002-03-26 12:50:28 +0000160 printf("%u ",
Rusty Russell52451822000-08-27 07:47:46 +0000161 espinfo->spis[0]);
162 }
163
164}
165
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000166static struct xtables_match esp = {
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000167 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000168 .name = "esp",
169 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000170 .size = XT_ALIGN(sizeof(struct xt_esp)),
171 .userspacesize = XT_ALIGN(sizeof(struct xt_esp)),
172 .help = &help,
173 .init = &init,
174 .parse = &parse,
175 .final_check = &final_check,
176 .print = &print,
177 .save = &save,
178 .extra_opts = opts
179};
180
181static struct xtables_match esp6 = {
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000182 .family = AF_INET6,
183 .name = "esp",
184 .version = IPTABLES_VERSION,
185 .size = XT_ALIGN(sizeof(struct xt_esp)),
186 .userspacesize = XT_ALIGN(sizeof(struct xt_esp)),
Pablo Neira8caee8b2004-12-28 13:11:59 +0000187 .help = &help,
188 .init = &init,
189 .parse = &parse,
190 .final_check = &final_check,
191 .print = &print,
192 .save = &save,
193 .extra_opts = opts
Rusty Russell52451822000-08-27 07:47:46 +0000194};
195
196void
197_init(void)
198{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000199 xtables_register_match(&esp);
200 xtables_register_match(&esp6);
Rusty Russell52451822000-08-27 07:47:46 +0000201}