blob: 29e865d40b3cd5d20dac76e0fa68df189f3e137c [file] [log] [blame]
Harald Welted32980d2002-03-25 08:38:26 +00001/* Shared library add-on to ip6tables 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>
8#include <ip6tables.h>
9#include <linux/netfilter_ipv6/ip6t_esp.h>
10
11/* Function which prints out usage message. */
12static void
13help(void)
14{
15 printf(
16"ESP v%s options:\n"
17" --espspi [!] spi[:spi] match spi (range)\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000018IPTABLES_VERSION);
Harald Welted32980d2002-03-25 08:38:26 +000019}
20
21static struct option opts[] = {
Stephane Ouellette703575d2003-08-23 18:41:47 +000022 { .name = "espspi", .has_arg = 1, .flag = 0, .val = '1' },
23 { .name = 0 }
Harald Welted32980d2002-03-25 08:38:26 +000024};
25
26static u_int32_t
27parse_esp_spi(const char *spistr)
28{
29 unsigned long int spi;
30 char* ep;
31
Stephane Ouellette703575d2003-08-23 18:41:47 +000032 spi = strtoul(spistr, &ep, 0);
Harald Welted32980d2002-03-25 08:38:26 +000033
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;
64 }
65 free(buffer);
66}
67
68/* Initialize the match. */
69static void
70init(struct ip6t_entry_match *m, unsigned int *nfcache)
71{
72 struct ip6t_esp *espinfo = (struct ip6t_esp *)m->data;
73
74 espinfo->spis[1] = 0xFFFFFFFF;
75}
76
77#define ESP_SPI 0x01
78
79/* Function which parses command options; returns true if it
80 ate an option */
81static int
82parse(int c, char **argv, int invert, unsigned int *flags,
83 const struct ip6t_entry *entry,
84 unsigned int *nfcache,
85 struct ip6t_entry_match **match)
86{
87 struct ip6t_esp *espinfo = (struct ip6t_esp *)(*match)->data;
88
89 switch (c) {
90 case '1':
91 if (*flags & ESP_SPI)
92 exit_error(PARAMETER_PROBLEM,
93 "Only one `--espspi' allowed");
94 check_inverse(optarg, &invert, &optind, 0);
95 parse_esp_spis(argv[optind-1], espinfo->spis);
96 if (invert)
97 espinfo->invflags |= IP6T_ESP_INV_SPI;
98 *flags |= ESP_SPI;
99 break;
100 default:
101 return 0;
102 }
103
104 return 1;
105}
106
107/* Final check; we don't care. */
108static void
109final_check(unsigned int flags)
110{
111}
112
113static void
114print_spis(const char *name, u_int32_t min, u_int32_t max,
115 int invert)
116{
117 const char *inv = invert ? "!" : "";
118
119 if (min != 0 || max != 0xFFFFFFFF || invert) {
Stephane Ouellette703575d2003-08-23 18:41:47 +0000120 if (min == max)
121 printf("%s:%s%u ", name, inv, min);
122 else
123 printf("%ss:%s%u:%u ", name, inv, min, max);
Harald Welted32980d2002-03-25 08:38:26 +0000124 }
125}
126
127/* Prints out the union ip6t_matchinfo. */
128static void
129print(const struct ip6t_ip6 *ip,
130 const struct ip6t_entry_match *match, int numeric)
131{
132 const struct ip6t_esp *esp = (struct ip6t_esp *)match->data;
133
134 printf("esp ");
135 print_spis("spi", esp->spis[0], esp->spis[1],
136 esp->invflags & IP6T_ESP_INV_SPI);
137 if (esp->invflags & ~IP6T_ESP_INV_MASK)
138 printf("Unknown invflags: 0x%X ",
139 esp->invflags & ~IP6T_ESP_INV_MASK);
140}
141
142/* Saves the union ip6t_matchinfo in parsable form to stdout. */
143static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
144{
145 const struct ip6t_esp *espinfo = (struct ip6t_esp *)match->data;
146
147 if (!(espinfo->spis[0] == 0
148 && espinfo->spis[1] == 0xFFFFFFFF)) {
149 printf("--espspi %s",
150 (espinfo->invflags & IP6T_ESP_INV_SPI) ? "! " : "");
151 if (espinfo->spis[0]
152 != espinfo->spis[1])
153 printf("%u:%u ",
154 espinfo->spis[0],
155 espinfo->spis[1]);
156 else
157 printf("%u ",
158 espinfo->spis[0]);
159 }
160
161}
162
163static
Stephane Ouellette703575d2003-08-23 18:41:47 +0000164struct ip6tables_match esp = {
165 .name = "esp",
166 .version = IPTABLES_VERSION,
167 .size = IP6T_ALIGN(sizeof(struct ip6t_esp)),
168 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_esp)),
169 .help = &help,
170 .init = &init,
171 .parse = &parse,
172 .final_check = &final_check,
173 .print = &print,
174 .save = &save,
175 .extra_opts = opts
Harald Welted32980d2002-03-25 08:38:26 +0000176};
177
178void
179_init(void)
180{
181 register_match6(&esp);
182}