Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 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> |
| 10 | |
| 11 | /* Function which prints out usage message. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "AH v%s options:\n" |
| 17 | " --ahspi [!] spi[:spi]\n" |
| 18 | " match spi (range)\n", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 19 | IPTABLES_VERSION); |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Jan Engelhardt | 661f112 | 2007-07-30 14:46:51 +0000 | [diff] [blame] | 22 | static const struct option opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame^] | 23 | { "ahspi", 1, NULL, '1' }, |
| 24 | { } |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | static u_int32_t |
| 28 | parse_ah_spi(const char *spistr) |
| 29 | { |
| 30 | unsigned long int spi; |
| 31 | char* ep; |
| 32 | |
| 33 | spi = strtoul(spistr,&ep,0) ; |
| 34 | |
| 35 | if ( spistr == ep ) { |
| 36 | exit_error(PARAMETER_PROBLEM, |
| 37 | "AH no valid digits in spi `%s'", spistr); |
| 38 | } |
| 39 | if ( spi == ULONG_MAX && errno == ERANGE ) { |
| 40 | exit_error(PARAMETER_PROBLEM, |
| 41 | "spi `%s' specified too big: would overflow", spistr); |
| 42 | } |
| 43 | if ( *spistr != '\0' && *ep != '\0' ) { |
| 44 | exit_error(PARAMETER_PROBLEM, |
| 45 | "AH error parsing spi `%s'", spistr); |
| 46 | } |
| 47 | return (u_int32_t) spi; |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | parse_ah_spis(const char *spistring, u_int32_t *spis) |
| 52 | { |
| 53 | char *buffer; |
| 54 | char *cp; |
| 55 | |
| 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++; |
| 62 | |
| 63 | spis[0] = buffer[0] ? parse_ah_spi(buffer) : 0; |
| 64 | spis[1] = cp[0] ? parse_ah_spi(cp) : 0xFFFFFFFF; |
| 65 | } |
| 66 | free(buffer); |
| 67 | } |
| 68 | |
| 69 | /* Initialize the match. */ |
| 70 | static void |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 71 | init(struct xt_entry_match *m) |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 72 | { |
| 73 | struct ipt_ah *ahinfo = (struct ipt_ah *)m->data; |
| 74 | |
| 75 | ahinfo->spis[1] = 0xFFFFFFFF; |
| 76 | } |
| 77 | |
| 78 | #define AH_SPI 0x01 |
| 79 | |
| 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, |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 84 | const void *entry, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 85 | struct xt_entry_match **match) |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 86 | { |
| 87 | struct ipt_ah *ahinfo = (struct ipt_ah *)(*match)->data; |
| 88 | |
| 89 | switch (c) { |
| 90 | case '1': |
| 91 | if (*flags & AH_SPI) |
| 92 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 93 | "Only one `--ahspi' allowed"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 94 | check_inverse(optarg, &invert, &optind, 0); |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 95 | parse_ah_spis(argv[optind-1], ahinfo->spis); |
| 96 | if (invert) |
| 97 | ahinfo->invflags |= IPT_AH_INV_SPI; |
| 98 | *flags |= AH_SPI; |
| 99 | break; |
| 100 | default: |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | /* Final check; we don't care. */ |
| 108 | static void |
| 109 | final_check(unsigned int flags) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | print_spis(const char *name, u_int32_t min, u_int32_t max, |
| 115 | int invert) |
| 116 | { |
| 117 | const char *inv = invert ? "!" : ""; |
| 118 | |
| 119 | if (min != 0 || max != 0xFFFFFFFF || invert) { |
| 120 | printf("%s", name); |
| 121 | if (min == max) { |
| 122 | printf(":%s", inv); |
| 123 | printf("%u", min); |
| 124 | } else { |
| 125 | printf("s:%s", inv); |
| 126 | printf("%u",min); |
| 127 | printf(":"); |
| 128 | printf("%u",max); |
| 129 | } |
| 130 | printf(" "); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Prints out the union ipt_matchinfo. */ |
| 135 | static void |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 136 | print(const void *ip, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 137 | const struct xt_entry_match *match, int numeric) |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 138 | { |
| 139 | const struct ipt_ah *ah = (struct ipt_ah *)match->data; |
| 140 | |
| 141 | printf("ah "); |
| 142 | print_spis("spi", ah->spis[0], ah->spis[1], |
| 143 | ah->invflags & IPT_AH_INV_SPI); |
| 144 | if (ah->invflags & ~IPT_AH_INV_MASK) |
| 145 | printf("Unknown invflags: 0x%X ", |
| 146 | ah->invflags & ~IPT_AH_INV_MASK); |
| 147 | } |
| 148 | |
| 149 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 150 | static void save(const void *ip, const struct xt_entry_match *match) |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 151 | { |
| 152 | const struct ipt_ah *ahinfo = (struct ipt_ah *)match->data; |
| 153 | |
Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 154 | if (!(ahinfo->spis[0] == 0 |
| 155 | && ahinfo->spis[1] == 0xFFFFFFFF)) { |
| 156 | printf("--ahspi %s", |
| 157 | (ahinfo->invflags & IPT_AH_INV_SPI) ? "! " : ""); |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 158 | if (ahinfo->spis[0] |
| 159 | != ahinfo->spis[1]) |
Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 160 | printf("%u:%u ", |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 161 | ahinfo->spis[0], |
| 162 | ahinfo->spis[1]); |
| 163 | else |
Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 164 | printf("%u ", |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 165 | ahinfo->spis[0]); |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 170 | static struct iptables_match ah = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 171 | .name = "ah", |
| 172 | .version = IPTABLES_VERSION, |
| 173 | .size = IPT_ALIGN(sizeof(struct ipt_ah)), |
| 174 | .userspacesize = IPT_ALIGN(sizeof(struct ipt_ah)), |
| 175 | .help = &help, |
| 176 | .init = &init, |
| 177 | .parse = &parse, |
| 178 | .final_check = &final_check, |
| 179 | .print = &print, |
| 180 | .save = &save, |
| 181 | .extra_opts = opts |
Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | void |
| 185 | _init(void) |
| 186 | { |
| 187 | register_match(&ah); |
| 188 | } |