| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add AH support. */ |
| Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 2 | #include <stdbool.h> |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <netdb.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <getopt.h> |
| 8 | #include <errno.h> |
| Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 9 | #include <xtables.h> |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 10 | #include <linux/netfilter_ipv4/ipt_ah.h> |
| Jan Engelhardt | ddac6c5 | 2008-09-01 14:22:19 +0200 | [diff] [blame] | 11 | |
| Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 12 | static void ah_help(void) |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 13 | { |
| 14 | printf( |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 15 | "ah match options:\n" |
| Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 16 | "[!] --ahspi spi[:spi]\n" |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 17 | " match spi (range)\n"); |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 20 | static const struct option ah_opts[] = { |
| Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 21 | {.name = "ahspi", .has_arg = true, .val = '1'}, |
| 22 | XT_GETOPT_TABLEEND, |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | static u_int32_t |
| 26 | parse_ah_spi(const char *spistr) |
| 27 | { |
| 28 | unsigned long int spi; |
| 29 | char* ep; |
| 30 | |
| 31 | spi = strtoul(spistr,&ep,0) ; |
| 32 | |
| 33 | if ( spistr == ep ) { |
| Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 34 | xtables_error(PARAMETER_PROBLEM, |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 35 | "AH no valid digits in spi `%s'", spistr); |
| 36 | } |
| 37 | if ( spi == ULONG_MAX && errno == ERANGE ) { |
| Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 38 | xtables_error(PARAMETER_PROBLEM, |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 39 | "spi `%s' specified too big: would overflow", spistr); |
| 40 | } |
| 41 | if ( *spistr != '\0' && *ep != '\0' ) { |
| Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 42 | xtables_error(PARAMETER_PROBLEM, |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 43 | "AH error parsing spi `%s'", spistr); |
| 44 | } |
| Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 45 | return spi; |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void |
| 49 | parse_ah_spis(const char *spistring, u_int32_t *spis) |
| 50 | { |
| 51 | char *buffer; |
| 52 | char *cp; |
| 53 | |
| 54 | buffer = strdup(spistring); |
| 55 | if ((cp = strchr(buffer, ':')) == NULL) |
| 56 | spis[0] = spis[1] = parse_ah_spi(buffer); |
| 57 | else { |
| 58 | *cp = '\0'; |
| 59 | cp++; |
| 60 | |
| 61 | spis[0] = buffer[0] ? parse_ah_spi(buffer) : 0; |
| 62 | spis[1] = cp[0] ? parse_ah_spi(cp) : 0xFFFFFFFF; |
| 63 | } |
| 64 | free(buffer); |
| 65 | } |
| 66 | |
| Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 67 | static void ah_init(struct xt_entry_match *m) |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 68 | { |
| 69 | struct ipt_ah *ahinfo = (struct ipt_ah *)m->data; |
| 70 | |
| 71 | ahinfo->spis[1] = 0xFFFFFFFF; |
| 72 | } |
| 73 | |
| 74 | #define AH_SPI 0x01 |
| 75 | |
| Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 76 | static int ah_parse(int c, char **argv, int invert, unsigned int *flags, |
| 77 | const void *entry, struct xt_entry_match **match) |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 78 | { |
| 79 | struct ipt_ah *ahinfo = (struct ipt_ah *)(*match)->data; |
| 80 | |
| 81 | switch (c) { |
| 82 | case '1': |
| 83 | if (*flags & AH_SPI) |
| Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 84 | xtables_error(PARAMETER_PROBLEM, |
| Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 85 | "Only one `--ahspi' allowed"); |
| Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 86 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
| Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 87 | parse_ah_spis(optarg, ahinfo->spis); |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 88 | if (invert) |
| 89 | ahinfo->invflags |= IPT_AH_INV_SPI; |
| 90 | *flags |= AH_SPI; |
| 91 | break; |
| 92 | default: |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | return 1; |
| 97 | } |
| 98 | |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 99 | static void |
| 100 | print_spis(const char *name, u_int32_t min, u_int32_t max, |
| 101 | int invert) |
| 102 | { |
| 103 | const char *inv = invert ? "!" : ""; |
| 104 | |
| 105 | if (min != 0 || max != 0xFFFFFFFF || invert) { |
| 106 | printf("%s", name); |
| 107 | if (min == max) { |
| 108 | printf(":%s", inv); |
| 109 | printf("%u", min); |
| 110 | } else { |
| 111 | printf("s:%s", inv); |
| 112 | printf("%u",min); |
| 113 | printf(":"); |
| 114 | printf("%u",max); |
| 115 | } |
| 116 | printf(" "); |
| 117 | } |
| 118 | } |
| 119 | |
| Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 120 | static void ah_print(const void *ip, const struct xt_entry_match *match, |
| 121 | int numeric) |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 122 | { |
| 123 | const struct ipt_ah *ah = (struct ipt_ah *)match->data; |
| 124 | |
| 125 | printf("ah "); |
| 126 | print_spis("spi", ah->spis[0], ah->spis[1], |
| 127 | ah->invflags & IPT_AH_INV_SPI); |
| 128 | if (ah->invflags & ~IPT_AH_INV_MASK) |
| 129 | printf("Unknown invflags: 0x%X ", |
| 130 | ah->invflags & ~IPT_AH_INV_MASK); |
| 131 | } |
| 132 | |
| Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 133 | static void ah_save(const void *ip, const struct xt_entry_match *match) |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 134 | { |
| 135 | const struct ipt_ah *ahinfo = (struct ipt_ah *)match->data; |
| 136 | |
| Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 137 | if (!(ahinfo->spis[0] == 0 |
| 138 | && ahinfo->spis[1] == 0xFFFFFFFF)) { |
| Jan Engelhardt | cea9f71 | 2008-12-09 15:06:20 +0100 | [diff] [blame] | 139 | printf("%s--ahspi ", |
| Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 140 | (ahinfo->invflags & IPT_AH_INV_SPI) ? "! " : ""); |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 141 | if (ahinfo->spis[0] |
| 142 | != ahinfo->spis[1]) |
| Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 143 | printf("%u:%u ", |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 144 | ahinfo->spis[0], |
| 145 | ahinfo->spis[1]); |
| 146 | else |
| Harald Welte | f0ac814 | 2002-03-26 12:50:28 +0000 | [diff] [blame] | 147 | printf("%u ", |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 148 | ahinfo->spis[0]); |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 153 | static struct xtables_match ah_mt_reg = { |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 154 | .name = "ah", |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 155 | .version = XTABLES_VERSION, |
| Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 156 | .family = NFPROTO_IPV4, |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 157 | .size = XT_ALIGN(sizeof(struct ipt_ah)), |
| 158 | .userspacesize = XT_ALIGN(sizeof(struct ipt_ah)), |
| Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 159 | .help = ah_help, |
| 160 | .init = ah_init, |
| 161 | .parse = ah_parse, |
| 162 | .print = ah_print, |
| 163 | .save = ah_save, |
| 164 | .extra_opts = ah_opts, |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | void |
| 168 | _init(void) |
| 169 | { |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 170 | xtables_register_match(&ah_mt_reg); |
| Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 171 | } |