1 /* Shared library add-on to iptables to add ESP support. */
11 #include <linux/netfilter/xt_esp.h>
13 static void esp_help(void)
16 "esp match options:\n"
17 "[!] --espspi spi[:spi]\n"
18 " match spi (range)\n");
21 static const struct option esp_opts[] = {
22 { "espspi", 1, NULL, '1' },
27 parse_esp_spi(const char *spistr)
29 unsigned long int spi;
32 spi = strtoul(spistr,&ep,0) ;
35 xtables_error(PARAMETER_PROBLEM,
36 "ESP no valid digits in spi `%s'", spistr);
38 if ( spi == ULONG_MAX && errno == ERANGE ) {
39 xtables_error(PARAMETER_PROBLEM,
40 "spi `%s' specified too big: would overflow", spistr);
42 if ( *spistr != '\0' && *ep != '\0' ) {
43 xtables_error(PARAMETER_PROBLEM,
44 "ESP error parsing spi `%s'", spistr);
50 parse_esp_spis(const char *spistring, u_int32_t *spis)
55 buffer = strdup(spistring);
56 if ((cp = strchr(buffer, ':')) == NULL)
57 spis[0] = spis[1] = parse_esp_spi(buffer);
62 spis[0] = buffer[0] ? parse_esp_spi(buffer) : 0;
63 spis[1] = cp[0] ? parse_esp_spi(cp) : 0xFFFFFFFF;
64 if (spis[0] > spis[1])
65 xtables_error(PARAMETER_PROBLEM,
66 "Invalid ESP spi range: %s", spistring);
71 static void esp_init(struct xt_entry_match *m)
73 struct xt_esp *espinfo = (struct xt_esp *)m->data;
75 espinfo->spis[1] = 0xFFFFFFFF;
81 esp_parse(int c, char **argv, int invert, unsigned int *flags,
82 const void *entry, struct xt_entry_match **match)
84 struct xt_esp *espinfo = (struct xt_esp *)(*match)->data;
89 xtables_error(PARAMETER_PROBLEM,
90 "Only one `--espspi' allowed");
91 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
92 parse_esp_spis(optarg, espinfo->spis);
94 espinfo->invflags |= XT_ESP_INV_SPI;
105 print_spis(const char *name, u_int32_t min, u_int32_t max,
108 const char *inv = invert ? "!" : "";
110 if (min != 0 || max != 0xFFFFFFFF || invert) {
112 printf("%s:%s%u ", name, inv, min);
114 printf("%ss:%s%u:%u ", name, inv, min, max);
119 esp_print(const void *ip, const struct xt_entry_match *match, int numeric)
121 const struct xt_esp *esp = (struct xt_esp *)match->data;
124 print_spis("spi", esp->spis[0], esp->spis[1],
125 esp->invflags & XT_ESP_INV_SPI);
126 if (esp->invflags & ~XT_ESP_INV_MASK)
127 printf("Unknown invflags: 0x%X ",
128 esp->invflags & ~XT_ESP_INV_MASK);
131 static void esp_save(const void *ip, const struct xt_entry_match *match)
133 const struct xt_esp *espinfo = (struct xt_esp *)match->data;
135 if (!(espinfo->spis[0] == 0
136 && espinfo->spis[1] == 0xFFFFFFFF)) {
137 printf("%s--espspi ",
138 (espinfo->invflags & XT_ESP_INV_SPI) ? "! " : "");
151 static struct xtables_match esp_match = {
152 .family = NFPROTO_UNSPEC,
154 .version = XTABLES_VERSION,
155 .size = XT_ALIGN(sizeof(struct xt_esp)),
156 .userspacesize = XT_ALIGN(sizeof(struct xt_esp)),
162 .extra_opts = esp_opts,
168 xtables_register_match(&esp_match);