blob: 10998d8bcd586d36a025472b46a5b7980fbbb094 [file] [log] [blame]
Rusty Russell52451822000-08-27 07:47:46 +00001/* 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>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01008#include <xtables.h>
Rusty Russell52451822000-08-27 07:47:46 +00009#include <linux/netfilter_ipv4/ipt_ah.h>
Jan Engelhardtddac6c52008-09-01 14:22:19 +020010
Jan Engelhardt59d16402007-10-04 16:28:39 +000011static void ah_help(void)
Rusty Russell52451822000-08-27 07:47:46 +000012{
13 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020014"ah match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020015"[!] --ahspi spi[:spi]\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020016" match spi (range)\n");
Rusty Russell52451822000-08-27 07:47:46 +000017}
18
Jan Engelhardt59d16402007-10-04 16:28:39 +000019static const struct option ah_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000020 { "ahspi", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000021 { .name = NULL }
Rusty Russell52451822000-08-27 07:47:46 +000022};
23
24static u_int32_t
25parse_ah_spi(const char *spistr)
26{
27 unsigned long int spi;
28 char* ep;
29
30 spi = strtoul(spistr,&ep,0) ;
31
32 if ( spistr == ep ) {
33 exit_error(PARAMETER_PROBLEM,
34 "AH no valid digits in spi `%s'", spistr);
35 }
36 if ( spi == ULONG_MAX && errno == ERANGE ) {
37 exit_error(PARAMETER_PROBLEM,
38 "spi `%s' specified too big: would overflow", spistr);
39 }
40 if ( *spistr != '\0' && *ep != '\0' ) {
41 exit_error(PARAMETER_PROBLEM,
42 "AH error parsing spi `%s'", spistr);
43 }
Jan Engelhardt213e1852009-01-27 17:24:34 +010044 return spi;
Rusty Russell52451822000-08-27 07:47:46 +000045}
46
47static void
48parse_ah_spis(const char *spistring, u_int32_t *spis)
49{
50 char *buffer;
51 char *cp;
52
53 buffer = strdup(spistring);
54 if ((cp = strchr(buffer, ':')) == NULL)
55 spis[0] = spis[1] = parse_ah_spi(buffer);
56 else {
57 *cp = '\0';
58 cp++;
59
60 spis[0] = buffer[0] ? parse_ah_spi(buffer) : 0;
61 spis[1] = cp[0] ? parse_ah_spi(cp) : 0xFFFFFFFF;
62 }
63 free(buffer);
64}
65
Jan Engelhardt59d16402007-10-04 16:28:39 +000066static void ah_init(struct xt_entry_match *m)
Rusty Russell52451822000-08-27 07:47:46 +000067{
68 struct ipt_ah *ahinfo = (struct ipt_ah *)m->data;
69
70 ahinfo->spis[1] = 0xFFFFFFFF;
71}
72
73#define AH_SPI 0x01
74
Jan Engelhardt59d16402007-10-04 16:28:39 +000075static int ah_parse(int c, char **argv, int invert, unsigned int *flags,
76 const void *entry, struct xt_entry_match **match)
Rusty Russell52451822000-08-27 07:47:46 +000077{
78 struct ipt_ah *ahinfo = (struct ipt_ah *)(*match)->data;
79
80 switch (c) {
81 case '1':
82 if (*flags & AH_SPI)
83 exit_error(PARAMETER_PROBLEM,
Harald Weltef0ac8142002-03-26 12:50:28 +000084 "Only one `--ahspi' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +000085 check_inverse(optarg, &invert, &optind, 0);
Rusty Russell52451822000-08-27 07:47:46 +000086 parse_ah_spis(argv[optind-1], ahinfo->spis);
87 if (invert)
88 ahinfo->invflags |= IPT_AH_INV_SPI;
89 *flags |= AH_SPI;
90 break;
91 default:
92 return 0;
93 }
94
95 return 1;
96}
97
Rusty Russell52451822000-08-27 07:47:46 +000098static void
99print_spis(const char *name, u_int32_t min, u_int32_t max,
100 int invert)
101{
102 const char *inv = invert ? "!" : "";
103
104 if (min != 0 || max != 0xFFFFFFFF || invert) {
105 printf("%s", name);
106 if (min == max) {
107 printf(":%s", inv);
108 printf("%u", min);
109 } else {
110 printf("s:%s", inv);
111 printf("%u",min);
112 printf(":");
113 printf("%u",max);
114 }
115 printf(" ");
116 }
117}
118
Jan Engelhardt59d16402007-10-04 16:28:39 +0000119static void ah_print(const void *ip, const struct xt_entry_match *match,
120 int numeric)
Rusty Russell52451822000-08-27 07:47:46 +0000121{
122 const struct ipt_ah *ah = (struct ipt_ah *)match->data;
123
124 printf("ah ");
125 print_spis("spi", ah->spis[0], ah->spis[1],
126 ah->invflags & IPT_AH_INV_SPI);
127 if (ah->invflags & ~IPT_AH_INV_MASK)
128 printf("Unknown invflags: 0x%X ",
129 ah->invflags & ~IPT_AH_INV_MASK);
130}
131
Jan Engelhardt59d16402007-10-04 16:28:39 +0000132static void ah_save(const void *ip, const struct xt_entry_match *match)
Rusty Russell52451822000-08-27 07:47:46 +0000133{
134 const struct ipt_ah *ahinfo = (struct ipt_ah *)match->data;
135
Harald Weltef0ac8142002-03-26 12:50:28 +0000136 if (!(ahinfo->spis[0] == 0
137 && ahinfo->spis[1] == 0xFFFFFFFF)) {
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100138 printf("%s--ahspi ",
Harald Weltef0ac8142002-03-26 12:50:28 +0000139 (ahinfo->invflags & IPT_AH_INV_SPI) ? "! " : "");
Rusty Russell52451822000-08-27 07:47:46 +0000140 if (ahinfo->spis[0]
141 != ahinfo->spis[1])
Harald Weltef0ac8142002-03-26 12:50:28 +0000142 printf("%u:%u ",
Rusty Russell52451822000-08-27 07:47:46 +0000143 ahinfo->spis[0],
144 ahinfo->spis[1]);
145 else
Harald Weltef0ac8142002-03-26 12:50:28 +0000146 printf("%u ",
Rusty Russell52451822000-08-27 07:47:46 +0000147 ahinfo->spis[0]);
148 }
149
150}
151
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200152static struct xtables_match ah_mt_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000153 .name = "ah",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200154 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100155 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200156 .size = XT_ALIGN(sizeof(struct ipt_ah)),
157 .userspacesize = XT_ALIGN(sizeof(struct ipt_ah)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000158 .help = ah_help,
159 .init = ah_init,
160 .parse = ah_parse,
161 .print = ah_print,
162 .save = ah_save,
163 .extra_opts = ah_opts,
Rusty Russell52451822000-08-27 07:47:46 +0000164};
165
166void
167_init(void)
168{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200169 xtables_register_match(&ah_mt_reg);
Rusty Russell52451822000-08-27 07:47:46 +0000170}