1 /* Shared library add-on to iptables to add ESP support. */
9 #include <linux/netfilter_ipv4/ipt_esp.h>
11 /* Function which prints out usage message. */
17 " --espspi [!] spi[:spi]\n"
18 " match spi (range)\n",
22 static struct option opts[] = {
23 { "espspi", 1, 0, '1' },
28 parse_esp_spi(const char *spistr)
30 unsigned long int spi;
33 spi = strtoul(spistr,&ep,0) ;
36 exit_error(PARAMETER_PROBLEM,
37 "ESP no valid digits in spi `%s'", spistr);
39 if ( spi == ULONG_MAX && errno == ERANGE ) {
40 exit_error(PARAMETER_PROBLEM,
41 "spi `%s' specified too big: would overflow", spistr);
43 if ( *spistr != '\0' && *ep != '\0' ) {
44 exit_error(PARAMETER_PROBLEM,
45 "ESP error parsing spi `%s'", spistr);
47 return (u_int32_t) spi;
51 parse_esp_spis(const char *spistring, u_int32_t *spis)
56 buffer = strdup(spistring);
57 if ((cp = strchr(buffer, ':')) == NULL)
58 spis[0] = spis[1] = parse_esp_spi(buffer);
63 spis[0] = buffer[0] ? parse_esp_spi(buffer) : 0;
64 spis[1] = cp[0] ? parse_esp_spi(cp) : 0xFFFFFFFF;
69 /* Initialize the match. */
71 init(struct ipt_entry_match *m, unsigned int *nfcache)
73 struct ipt_esp *espinfo = (struct ipt_esp *)m->data;
75 espinfo->spis[1] = 0xFFFFFFFF;
80 /* Function which parses command options; returns true if it
83 parse(int c, char **argv, int invert, unsigned int *flags,
84 const struct ipt_entry *entry,
85 unsigned int *nfcache,
86 struct ipt_entry_match **match)
88 struct ipt_esp *espinfo = (struct ipt_esp *)(*match)->data;
93 exit_error(PARAMETER_PROBLEM,
94 "Only one `--espspi' allowed");
95 check_inverse(optarg, &invert, &optind, 0);
96 parse_esp_spis(argv[optind-1], espinfo->spis);
98 espinfo->invflags |= IPT_ESP_INV_SPI;
108 /* Final check; we don't care. */
110 final_check(unsigned int flags)
115 print_spis(const char *name, u_int32_t min, u_int32_t max,
118 const char *inv = invert ? "!" : "";
120 if (min != 0 || max != 0xFFFFFFFF || invert) {
135 /* Prints out the union ipt_matchinfo. */
137 print(const struct ipt_ip *ip,
138 const struct ipt_entry_match *match, int numeric)
140 const struct ipt_esp *esp = (struct ipt_esp *)match->data;
143 print_spis("spi", esp->spis[0], esp->spis[1],
144 esp->invflags & IPT_ESP_INV_SPI);
145 if (esp->invflags & ~IPT_ESP_INV_MASK)
146 printf("Unknown invflags: 0x%X ",
147 esp->invflags & ~IPT_ESP_INV_MASK);
150 /* Saves the union ipt_matchinfo in parsable form to stdout. */
151 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
153 const struct ipt_esp *espinfo = (struct ipt_esp *)match->data;
155 if (!(espinfo->spis[0] == 0
156 && espinfo->spis[1] == 0xFFFFFFFF)) {
157 printf("--espspi %s",
158 (espinfo->invflags & IPT_ESP_INV_SPI) ? "! " : "");
171 static struct iptables_match esp = {
174 .version = IPTABLES_VERSION,
175 .size = IPT_ALIGN(sizeof(struct ipt_esp)),
176 .userspacesize = IPT_ALIGN(sizeof(struct ipt_esp)),
180 .final_check = &final_check,
189 register_match(&esp);