blob: 34df876d6c14c7cf4c1a81fa6362a45dbfefa662 [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>
Phil Oester9a90f902008-09-01 15:07:26 +02008#include <limits.h>
9
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000010#include <xtables.h>
11#include <linux/netfilter/xt_esp.h>
Rusty Russell52451822000-08-27 07:47:46 +000012
Jan Engelhardt181dead2007-10-04 16:27:07 +000013static void esp_help(void)
Rusty Russell52451822000-08-27 07:47:46 +000014{
15 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020016"esp match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020017"[!] --espspi spi[:spi]\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020018" match spi (range)\n");
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 }
Jan Engelhardt213e1852009-01-27 17:24:34 +010046 return spi;
Rusty Russell52451822000-08-27 07:47:46 +000047}
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
Jan Engelhardt181dead2007-10-04 16:27:07 +000071static void esp_init(struct xt_entry_match *m)
Rusty Russell52451822000-08-27 07:47:46 +000072{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000073 struct xt_esp *espinfo = (struct xt_esp *)m->data;
Rusty Russell52451822000-08-27 07:47:46 +000074
75 espinfo->spis[1] = 0xFFFFFFFF;
76}
77
78#define ESP_SPI 0x01
79
Rusty Russell52451822000-08-27 07:47:46 +000080static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000081esp_parse(int c, char **argv, int invert, unsigned int *flags,
82 const void *entry, struct xt_entry_match **match)
Rusty Russell52451822000-08-27 07:47:46 +000083{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000084 struct xt_esp *espinfo = (struct xt_esp *)(*match)->data;
Rusty Russell52451822000-08-27 07:47:46 +000085
86 switch (c) {
87 case '1':
88 if (*flags & ESP_SPI)
89 exit_error(PARAMETER_PROBLEM,
Harald Weltef0ac8142002-03-26 12:50:28 +000090 "Only one `--espspi' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +000091 check_inverse(optarg, &invert, &optind, 0);
Rusty Russell52451822000-08-27 07:47:46 +000092 parse_esp_spis(argv[optind-1], espinfo->spis);
93 if (invert)
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +000094 espinfo->invflags |= XT_ESP_INV_SPI;
Rusty Russell52451822000-08-27 07:47:46 +000095 *flags |= ESP_SPI;
96 break;
97 default:
98 return 0;
99 }
100
101 return 1;
102}
103
Rusty Russell52451822000-08-27 07:47:46 +0000104static void
105print_spis(const char *name, u_int32_t min, u_int32_t max,
106 int invert)
107{
108 const char *inv = invert ? "!" : "";
109
110 if (min != 0 || max != 0xFFFFFFFF || invert) {
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000111 if (min == max)
112 printf("%s:%s%u ", name, inv, min);
113 else
114 printf("%ss:%s%u:%u ", name, inv, min, max);
Rusty Russell52451822000-08-27 07:47:46 +0000115 }
116}
117
Rusty Russell52451822000-08-27 07:47:46 +0000118static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000119esp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Rusty Russell52451822000-08-27 07:47:46 +0000120{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000121 const struct xt_esp *esp = (struct xt_esp *)match->data;
Rusty Russell52451822000-08-27 07:47:46 +0000122
123 printf("esp ");
124 print_spis("spi", esp->spis[0], esp->spis[1],
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000125 esp->invflags & XT_ESP_INV_SPI);
126 if (esp->invflags & ~XT_ESP_INV_MASK)
Rusty Russell52451822000-08-27 07:47:46 +0000127 printf("Unknown invflags: 0x%X ",
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000128 esp->invflags & ~XT_ESP_INV_MASK);
Rusty Russell52451822000-08-27 07:47:46 +0000129}
130
Jan Engelhardt181dead2007-10-04 16:27:07 +0000131static void esp_save(const void *ip, const struct xt_entry_match *match)
Rusty Russell52451822000-08-27 07:47:46 +0000132{
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000133 const struct xt_esp *espinfo = (struct xt_esp *)match->data;
Rusty Russell52451822000-08-27 07:47:46 +0000134
Harald Weltef0ac8142002-03-26 12:50:28 +0000135 if (!(espinfo->spis[0] == 0
136 && espinfo->spis[1] == 0xFFFFFFFF)) {
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100137 printf("%s--espspi ",
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000138 (espinfo->invflags & XT_ESP_INV_SPI) ? "! " : "");
Rusty Russell52451822000-08-27 07:47:46 +0000139 if (espinfo->spis[0]
140 != espinfo->spis[1])
Harald Weltef0ac8142002-03-26 12:50:28 +0000141 printf("%u:%u ",
Rusty Russell52451822000-08-27 07:47:46 +0000142 espinfo->spis[0],
143 espinfo->spis[1]);
144 else
Harald Weltef0ac8142002-03-26 12:50:28 +0000145 printf("%u ",
Rusty Russell52451822000-08-27 07:47:46 +0000146 espinfo->spis[0]);
147 }
148
149}
150
Jan Engelhardt181dead2007-10-04 16:27:07 +0000151static struct xtables_match esp_match = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100152 .family = NFPROTO_IPV4,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000153 .name = "esp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200154 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000155 .size = XT_ALIGN(sizeof(struct xt_esp)),
156 .userspacesize = XT_ALIGN(sizeof(struct xt_esp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000157 .help = esp_help,
158 .init = esp_init,
159 .parse = esp_parse,
160 .print = esp_print,
161 .save = esp_save,
162 .extra_opts = esp_opts,
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000163};
164
Jan Engelhardt181dead2007-10-04 16:27:07 +0000165static struct xtables_match esp_match6 = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100166 .family = NFPROTO_IPV6,
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000167 .name = "esp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200168 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI0a04e8d2007-07-24 07:16:20 +0000169 .size = XT_ALIGN(sizeof(struct xt_esp)),
170 .userspacesize = XT_ALIGN(sizeof(struct xt_esp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000171 .help = esp_help,
172 .init = esp_init,
173 .parse = esp_parse,
174 .print = esp_print,
175 .save = esp_save,
176 .extra_opts = esp_opts,
Rusty Russell52451822000-08-27 07:47:46 +0000177};
178
179void
180_init(void)
181{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000182 xtables_register_match(&esp_match);
183 xtables_register_match(&esp_match6);
Rusty Russell52451822000-08-27 07:47:46 +0000184}