1 /* Shared library add-on to ip6tables to add AH support. */
9 #include <linux/netfilter_ipv6/ip6t_ah.h>
11 /* Function which prints out usage message. */
12 static void ah_help(void)
16 " --ahspi [!] spi[:spi] match spi (range)\n"
17 " --ahlen [!] length total length of this header\n"
18 " --ahres check the reserved filed, too\n");
21 static const struct option ah_opts[] = {
22 { .name = "ahspi", .has_arg = 1, .val = '1' },
23 { .name = "ahlen", .has_arg = 1, .val = '2' },
24 { .name = "ahres", .has_arg = 0, .val = '3' },
29 parse_ah_spi(const char *spistr, const char *typestr)
31 unsigned long int spi;
34 spi = strtoul(spistr, &ep, 0);
37 exit_error(PARAMETER_PROBLEM,
38 "AH no valid digits in %s `%s'", typestr, spistr);
40 if ( spi == ULONG_MAX && errno == ERANGE )
41 exit_error(PARAMETER_PROBLEM,
42 "%s `%s' specified too big: would overflow",
45 if ( *spistr != '\0' && *ep != '\0' )
46 exit_error(PARAMETER_PROBLEM,
47 "AH error parsing %s `%s'", typestr, spistr);
49 return (u_int32_t) spi;
53 parse_ah_spis(const char *spistring, u_int32_t *spis)
58 buffer = strdup(spistring);
59 if ((cp = strchr(buffer, ':')) == NULL)
60 spis[0] = spis[1] = parse_ah_spi(buffer, "spi");
65 spis[0] = buffer[0] ? parse_ah_spi(buffer, "spi") : 0;
66 spis[1] = cp[0] ? parse_ah_spi(cp, "spi") : 0xFFFFFFFF;
71 /* Initialize the match. */
72 static void ah_init(struct xt_entry_match *m)
74 struct ip6t_ah *ahinfo = (struct ip6t_ah *)m->data;
76 ahinfo->spis[1] = 0xFFFFFFFF;
81 /* Function which parses command options; returns true if it
83 static int ah_parse(int c, char **argv, int invert, unsigned int *flags,
84 const void *entry, struct xt_entry_match **match)
86 struct ip6t_ah *ahinfo = (struct ip6t_ah *)(*match)->data;
90 if (*flags & IP6T_AH_SPI)
91 exit_error(PARAMETER_PROBLEM,
92 "Only one `--ahspi' allowed");
93 check_inverse(optarg, &invert, &optind, 0);
94 parse_ah_spis(argv[optind-1], ahinfo->spis);
96 ahinfo->invflags |= IP6T_AH_INV_SPI;
97 *flags |= IP6T_AH_SPI;
100 if (*flags & IP6T_AH_LEN)
101 exit_error(PARAMETER_PROBLEM,
102 "Only one `--ahlen' allowed");
103 check_inverse(optarg, &invert, &optind, 0);
104 ahinfo->hdrlen = parse_ah_spi(argv[optind-1], "length");
106 ahinfo->invflags |= IP6T_AH_INV_LEN;
107 *flags |= IP6T_AH_LEN;
110 if (*flags & IP6T_AH_RES)
111 exit_error(PARAMETER_PROBLEM,
112 "Only one `--ahres' allowed");
114 *flags |= IP6T_AH_RES;
124 print_spis(const char *name, u_int32_t min, u_int32_t max,
127 const char *inv = invert ? "!" : "";
129 if (min != 0 || max != 0xFFFFFFFF || invert) {
131 printf("%s:%s%u ", name, inv, min);
133 printf("%ss:%s%u:%u ", name, inv, min, max);
138 print_len(const char *name, u_int32_t len, int invert)
140 const char *inv = invert ? "!" : "";
142 if (len != 0 || invert)
143 printf("%s:%s%u ", name, inv, len);
146 /* Prints out the union ip6t_matchinfo. */
147 static void ah_print(const void *ip, const struct xt_entry_match *match,
150 const struct ip6t_ah *ah = (struct ip6t_ah *)match->data;
153 print_spis("spi", ah->spis[0], ah->spis[1],
154 ah->invflags & IP6T_AH_INV_SPI);
155 print_len("length", ah->hdrlen,
156 ah->invflags & IP6T_AH_INV_LEN);
161 if (ah->invflags & ~IP6T_AH_INV_MASK)
162 printf("Unknown invflags: 0x%X ",
163 ah->invflags & ~IP6T_AH_INV_MASK);
166 /* Saves the union ip6t_matchinfo in parsable form to stdout. */
167 static void ah_save(const void *ip, const struct xt_entry_match *match)
169 const struct ip6t_ah *ahinfo = (struct ip6t_ah *)match->data;
171 if (!(ahinfo->spis[0] == 0
172 && ahinfo->spis[1] == 0xFFFFFFFF)) {
174 (ahinfo->invflags & IP6T_AH_INV_SPI) ? "! " : "");
185 if (ahinfo->hdrlen != 0 || (ahinfo->invflags & IP6T_AH_INV_LEN) ) {
186 printf("--ahlen %s%u ",
187 (ahinfo->invflags & IP6T_AH_INV_LEN) ? "! " : "",
191 if (ahinfo->hdrres != 0 )
195 static struct xtables_match ah_mt6_reg = {
197 .version = XTABLES_VERSION,
199 .size = XT_ALIGN(sizeof(struct ip6t_ah)),
200 .userspacesize = XT_ALIGN(sizeof(struct ip6t_ah)),
206 .extra_opts = ah_opts,
212 xtables_register_match(&ah_mt6_reg);