iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libip6t_esp.c
1 /* 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. */
12 static void
13 help(void)
14 {
15         printf(
16 "ESP v%s options:\n"
17 " --espspi [!] spi[:spi]        match spi (range)\n",
18 IPTABLES_VERSION);
19 }
20
21 static struct option opts[] = {
22         { .name = "espspi", .has_arg = 1, .flag = 0, .val = '1' },
23         { .name = 0 }
24 };
25
26 static u_int32_t
27 parse_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
49 static void
50 parse_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. */
69 static void
70 init(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 */
81 static int
82 parse(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. */
108 static void
109 final_check(unsigned int flags)
110 {
111 }
112
113 static void
114 print_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                 if (min == max)
121                         printf("%s:%s%u ", name, inv, min);
122                 else
123                         printf("%ss:%s%u:%u ", name, inv, min, max);
124         }
125 }
126
127 /* Prints out the union ip6t_matchinfo. */
128 static void
129 print(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. */
143 static 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
163 static
164 struct 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
176 };
177
178 void
179 _init(void)
180 {
181         register_match6(&esp);
182 }