blob: f37c088a2d407beced223a2fed298dac5511dd2c [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. */
Jan Engelhardt181dead2007-10-04 16:27:07 +000012static void esp_help(void)
Rusty Russell52451822000-08-27 07:47:46 +000013{
14 printf(
15"ESP v%s options:\n"
16" --espspi [!] spi[:spi]\n"
17" match spi (range)\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000018IPTABLES_VERSION);
Rusty Russell52451822000-08-27 07:47:46 +000019}
20
Jan Engelhardt181dead2007-10-04 16:27:07 +000021static const struct option esp_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000022 { "espspi", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000023 { .name = NULL }
Rusty Russell52451822000-08-27 07:47:46 +000024};
25
26static u_int32_t
27parse_esp_spi(const char *spistr)
28{
29 unsigned long int spi;
30 char* ep;
31
32 spi = strtoul(spistr,&ep,0) ;
33
34 if ( spistr == ep ) {
35 exit_error(PARAMETER_PROBLEM,
36 "ESP no valid digits in spi `%s'", spistr);
37 }
38 if ( spi == ULONG_MAX && errno == ERANGE ) {
39 exit_error(PARAMETER_PROBLEM,
40 "spi `%s' specified too big: would overflow", spistr);
41 }
42 if ( *spistr != '\0' && *ep != '\0' ) {
43 exit_error(PARAMETER_PROBLEM,
44 "ESP error parsing spi `%s'", spistr);
45 }
46 return (u_int32_t) spi;
47}
48
49static void
50parse_esp_spis(const char *spistring, u_int32_t *spis)
51{
52 char *buffer;
53 char *cp;
54
55 buffer = strdup(spistring);
56 if ((cp = strchr(buffer, ':')) == NULL)
57 spis[0] = spis[1] = parse_esp_spi(buffer);
58 else {
59 *cp = '\0';
60 cp++;
61
62 spis[0] = buffer[0] ? parse_esp_spi(buffer) : 0;
63 spis[1] = cp[0] ? parse_esp_spi(cp) : 0xFFFFFFFF;
Yasuyuki KOZAKAI2c627cf2006-04-15 03:11:15 +000064 if (spis[0] > spis[1])
65 exit_error(PARAMETER_PROBLEM,
66 "Invalid ESP spi range: %s", spistring);
Rusty Russell52451822000-08-27 07:47:46 +000067 }
68 free(buffer);
69}
70
71/* Initialize the match. */
Jan Engelhardt181dead2007-10-04 16:27:07 +000072static void esp_init(struct xt_entry_match *m)
Rusty Russell52451822000-08-27 07:47:46 +000073{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000074 struct xt_esp *espinfo = (struct xt_esp *)m->data;
Rusty Russell52451822000-08-27 07:47:46 +000075
76 espinfo->spis[1] = 0xFFFFFFFF;
77}
78
79#define ESP_SPI 0x01
80
81/* Function which parses command options; returns true if it
82 ate an option */
83static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000084esp_parse(int c, char **argv, int invert, unsigned int *flags,
85 const void *entry, struct xt_entry_match **match)
Rusty Russell52451822000-08-27 07:47:46 +000086{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000087 struct xt_esp *espinfo = (struct xt_esp *)(*match)->data;
Rusty Russell52451822000-08-27 07:47:46 +000088
89 switch (c) {
90 case '1':
91 if (*flags & ESP_SPI)
92 exit_error(PARAMETER_PROBLEM,
Harald Weltef0ac8142002-03-26 12:50:28 +000093 "Only one `--espspi' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +000094 check_inverse(optarg, &invert, &optind, 0);
Rusty Russell52451822000-08-27 07:47:46 +000095 parse_esp_spis(argv[optind-1], espinfo->spis);
96 if (invert)
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000097 espinfo->invflags |= XT_ESP_INV_SPI;
Rusty Russell52451822000-08-27 07:47:46 +000098 *flags |= ESP_SPI;
99 break;
100 default:
101 return 0;
102 }
103
104 return 1;
105}
106
Rusty Russell52451822000-08-27 07:47:46 +0000107static void
108print_spis(const char *name, u_int32_t min, u_int32_t max,
109 int invert)
110{
111 const char *inv = invert ? "!" : "";
112
113 if (min != 0 || max != 0xFFFFFFFF || invert) {
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000114 if (min == max)
115 printf("%s:%s%u ", name, inv, min);
116 else
117 printf("%ss:%s%u:%u ", name, inv, min, max);
Rusty Russell52451822000-08-27 07:47:46 +0000118 }
119}
120
121/* Prints out the union ipt_matchinfo. */
122static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000123esp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Rusty Russell52451822000-08-27 07:47:46 +0000124{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000125 const struct xt_esp *esp = (struct xt_esp *)match->data;
Rusty Russell52451822000-08-27 07:47:46 +0000126
127 printf("esp ");
128 print_spis("spi", esp->spis[0], esp->spis[1],
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000129 esp->invflags & XT_ESP_INV_SPI);
130 if (esp->invflags & ~XT_ESP_INV_MASK)
Rusty Russell52451822000-08-27 07:47:46 +0000131 printf("Unknown invflags: 0x%X ",
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000132 esp->invflags & ~XT_ESP_INV_MASK);
Rusty Russell52451822000-08-27 07:47:46 +0000133}
134
135/* Saves the union ipt_matchinfo in parsable form to stdout. */
Jan Engelhardt181dead2007-10-04 16:27:07 +0000136static void esp_save(const void *ip, const struct xt_entry_match *match)
Rusty Russell52451822000-08-27 07:47:46 +0000137{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000138 const struct xt_esp *espinfo = (struct xt_esp *)match->data;
Rusty Russell52451822000-08-27 07:47:46 +0000139
Harald Weltef0ac8142002-03-26 12:50:28 +0000140 if (!(espinfo->spis[0] == 0
141 && espinfo->spis[1] == 0xFFFFFFFF)) {
142 printf("--espspi %s",
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000143 (espinfo->invflags & XT_ESP_INV_SPI) ? "! " : "");
Rusty Russell52451822000-08-27 07:47:46 +0000144 if (espinfo->spis[0]
145 != espinfo->spis[1])
Harald Weltef0ac8142002-03-26 12:50:28 +0000146 printf("%u:%u ",
Rusty Russell52451822000-08-27 07:47:46 +0000147 espinfo->spis[0],
148 espinfo->spis[1]);
149 else
Harald Weltef0ac8142002-03-26 12:50:28 +0000150 printf("%u ",
Rusty Russell52451822000-08-27 07:47:46 +0000151 espinfo->spis[0]);
152 }
153
154}
155
Jan Engelhardt181dead2007-10-04 16:27:07 +0000156static struct xtables_match esp_match = {
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000157 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000158 .name = "esp",
159 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000160 .size = XT_ALIGN(sizeof(struct xt_esp)),
161 .userspacesize = XT_ALIGN(sizeof(struct xt_esp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000162 .help = esp_help,
163 .init = esp_init,
164 .parse = esp_parse,
165 .print = esp_print,
166 .save = esp_save,
167 .extra_opts = esp_opts,
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000168};
169
Jan Engelhardt181dead2007-10-04 16:27:07 +0000170static struct xtables_match esp_match6 = {
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000171 .family = AF_INET6,
172 .name = "esp",
173 .version = IPTABLES_VERSION,
174 .size = XT_ALIGN(sizeof(struct xt_esp)),
175 .userspacesize = XT_ALIGN(sizeof(struct xt_esp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000176 .help = esp_help,
177 .init = esp_init,
178 .parse = esp_parse,
179 .print = esp_print,
180 .save = esp_save,
181 .extra_opts = esp_opts,
Rusty Russell52451822000-08-27 07:47:46 +0000182};
183
184void
185_init(void)
186{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000187 xtables_register_match(&esp_match);
188 xtables_register_match(&esp_match6);
Rusty Russell52451822000-08-27 07:47:46 +0000189}