blob: 973338f8b7df7efea0e4f394fddc324c91113f59 [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",
18NETFILTER_VERSION);
19}
20
21static struct option opts[] = {
22 { "espspi", 1, 0, '1' },
23 {0}
24};
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;
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) {
120 printf("%s", name);
121 if (min == max) {
122 printf(":%s", inv);
123 printf("%u", min);
124 } else {
125 printf("s:%s", inv);
126 printf("%u",min);
127 printf(":");
128 printf("%u",max);
129 }
130 printf(" ");
131 }
132}
133
134/* Prints out the union ip6t_matchinfo. */
135static void
136print(const struct ip6t_ip6 *ip,
137 const struct ip6t_entry_match *match, int numeric)
138{
139 const struct ip6t_esp *esp = (struct ip6t_esp *)match->data;
140
141 printf("esp ");
142 print_spis("spi", esp->spis[0], esp->spis[1],
143 esp->invflags & IP6T_ESP_INV_SPI);
144 if (esp->invflags & ~IP6T_ESP_INV_MASK)
145 printf("Unknown invflags: 0x%X ",
146 esp->invflags & ~IP6T_ESP_INV_MASK);
147}
148
149/* Saves the union ip6t_matchinfo in parsable form to stdout. */
150static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
151{
152 const struct ip6t_esp *espinfo = (struct ip6t_esp *)match->data;
153
154 if (!(espinfo->spis[0] == 0
155 && espinfo->spis[1] == 0xFFFFFFFF)) {
156 printf("--espspi %s",
157 (espinfo->invflags & IP6T_ESP_INV_SPI) ? "! " : "");
158 if (espinfo->spis[0]
159 != espinfo->spis[1])
160 printf("%u:%u ",
161 espinfo->spis[0],
162 espinfo->spis[1]);
163 else
164 printf("%u ",
165 espinfo->spis[0]);
166 }
167
168}
169
170static
171struct ip6tables_match esp
172= { NULL,
173 "esp",
174 NETFILTER_VERSION,
175 IP6T_ALIGN(sizeof(struct ip6t_esp)),
176 IP6T_ALIGN(sizeof(struct ip6t_esp)),
177 &help,
178 &init,
179 &parse,
180 &final_check,
181 &print,
182 &save,
183 opts
184};
185
186void
187_init(void)
188{
189 register_match6(&esp);
190}