Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / extensions / libipt_ah.c
blob443c9f8286b691e8340c4676a72683b8afcfded1
1 /* Shared library add-on to iptables to add AH 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 <iptables.h>
9 #include <linux/netfilter_ipv4/ipt_ah.h>
11 /* Function which prints out usage message. */
12 static void
13 help(void)
15 printf(
16 "AH v%s options:\n"
17 " --ahspi [!] spi[:spi]\n"
18 " match spi (range)\n",
19 IPTABLES_VERSION);
22 static struct option opts[] = {
23 { "ahspi", 1, 0, '1' },
24 {0}
27 static u_int32_t
28 parse_ah_spi(const char *spistr)
30 unsigned long int spi;
31 char* ep;
33 spi = strtoul(spistr,&ep,0) ;
35 if ( spistr == ep ) {
36 exit_error(PARAMETER_PROBLEM,
37 "AH 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 "AH error parsing spi `%s'", spistr);
47 return (u_int32_t) spi;
50 static void
51 parse_ah_spis(const char *spistring, u_int32_t *spis)
53 char *buffer;
54 char *cp;
56 buffer = strdup(spistring);
57 if ((cp = strchr(buffer, ':')) == NULL)
58 spis[0] = spis[1] = parse_ah_spi(buffer);
59 else {
60 *cp = '\0';
61 cp++;
63 spis[0] = buffer[0] ? parse_ah_spi(buffer) : 0;
64 spis[1] = cp[0] ? parse_ah_spi(cp) : 0xFFFFFFFF;
66 free(buffer);
69 /* Initialize the match. */
70 static void
71 init(struct ipt_entry_match *m, unsigned int *nfcache)
73 struct ipt_ah *ahinfo = (struct ipt_ah *)m->data;
75 ahinfo->spis[1] = 0xFFFFFFFF;
78 #define AH_SPI 0x01
80 /* Function which parses command options; returns true if it
81 ate an option */
82 static int
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_ah *ahinfo = (struct ipt_ah *)(*match)->data;
90 switch (c) {
91 case '1':
92 if (*flags & AH_SPI)
93 exit_error(PARAMETER_PROBLEM,
94 "Only one `--ahspi' allowed");
95 check_inverse(optarg, &invert, &optind, 0);
96 parse_ah_spis(argv[optind-1], ahinfo->spis);
97 if (invert)
98 ahinfo->invflags |= IPT_AH_INV_SPI;
99 *flags |= AH_SPI;
100 break;
101 default:
102 return 0;
105 return 1;
108 /* Final check; we don't care. */
109 static void
110 final_check(unsigned int flags)
114 static void
115 print_spis(const char *name, u_int32_t min, u_int32_t max,
116 int invert)
118 const char *inv = invert ? "!" : "";
120 if (min != 0 || max != 0xFFFFFFFF || invert) {
121 printf("%s", name);
122 if (min == max) {
123 printf(":%s", inv);
124 printf("%u", min);
125 } else {
126 printf("s:%s", inv);
127 printf("%u",min);
128 printf(":");
129 printf("%u",max);
131 printf(" ");
135 /* Prints out the union ipt_matchinfo. */
136 static void
137 print(const struct ipt_ip *ip,
138 const struct ipt_entry_match *match, int numeric)
140 const struct ipt_ah *ah = (struct ipt_ah *)match->data;
142 printf("ah ");
143 print_spis("spi", ah->spis[0], ah->spis[1],
144 ah->invflags & IPT_AH_INV_SPI);
145 if (ah->invflags & ~IPT_AH_INV_MASK)
146 printf("Unknown invflags: 0x%X ",
147 ah->invflags & ~IPT_AH_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_ah *ahinfo = (struct ipt_ah *)match->data;
155 if (!(ahinfo->spis[0] == 0
156 && ahinfo->spis[1] == 0xFFFFFFFF)) {
157 printf("--ahspi %s",
158 (ahinfo->invflags & IPT_AH_INV_SPI) ? "! " : "");
159 if (ahinfo->spis[0]
160 != ahinfo->spis[1])
161 printf("%u:%u ",
162 ahinfo->spis[0],
163 ahinfo->spis[1]);
164 else
165 printf("%u ",
166 ahinfo->spis[0]);
171 static struct iptables_match ah = {
172 .next = NULL,
173 .name = "ah",
174 .version = IPTABLES_VERSION,
175 .size = IPT_ALIGN(sizeof(struct ipt_ah)),
176 .userspacesize = IPT_ALIGN(sizeof(struct ipt_ah)),
177 .help = &help,
178 .init = &init,
179 .parse = &parse,
180 .final_check = &final_check,
181 .print = &print,
182 .save = &save,
183 .extra_opts = opts
186 void
187 _init(void)
189 register_match(&ah);