Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add ESP 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_esp.h> |
| 10 | |
| 11 | /* Function which prints out usage message. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "ESP v%s options:\n" |
| 17 | " --espspi [!] spi[:spi]\n" |
| 18 | " match spi (range)\n", |
| 19 | NETFILTER_VERSION); |
| 20 | } |
| 21 | |
| 22 | static struct option opts[] = { |
| 23 | { "espspi", 1, 0, '1' }, |
| 24 | {0} |
| 25 | }; |
| 26 | |
| 27 | static u_int32_t |
| 28 | parse_esp_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 | "ESP 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 | "ESP error parsing spi `%s'", spistr); |
| 46 | } |
| 47 | return (u_int32_t) spi; |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | parse_esp_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_esp_spi(buffer); |
| 59 | else { |
| 60 | *cp = '\0'; |
| 61 | cp++; |
| 62 | |
| 63 | spis[0] = buffer[0] ? parse_esp_spi(buffer) : 0; |
| 64 | spis[1] = cp[0] ? parse_esp_spi(cp) : 0xFFFFFFFF; |
| 65 | } |
| 66 | free(buffer); |
| 67 | } |
| 68 | |
| 69 | /* Initialize the match. */ |
| 70 | static void |
| 71 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 72 | { |
| 73 | struct ipt_esp *espinfo = (struct ipt_esp *)m->data; |
| 74 | |
| 75 | espinfo->spis[1] = 0xFFFFFFFF; |
| 76 | } |
| 77 | |
| 78 | #define ESP_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, |
| 84 | const struct ipt_entry *entry, |
| 85 | unsigned int *nfcache, |
| 86 | struct ipt_entry_match **match) |
| 87 | { |
| 88 | struct ipt_esp *espinfo = (struct ipt_esp *)(*match)->data; |
| 89 | |
| 90 | switch (c) { |
| 91 | case '1': |
| 92 | if (*flags & ESP_SPI) |
| 93 | exit_error(PARAMETER_PROBLEM, |
| 94 | "Only one `--spi' allowed"); |
| 95 | if (check_inverse(optarg, &invert)) |
| 96 | optind++; |
| 97 | parse_esp_spis(argv[optind-1], espinfo->spis); |
| 98 | if (invert) |
| 99 | espinfo->invflags |= IPT_ESP_INV_SPI; |
| 100 | *flags |= ESP_SPI; |
| 101 | break; |
| 102 | default: |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | /* Final check; we don't care. */ |
| 110 | static void |
| 111 | final_check(unsigned int flags) |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | print_spis(const char *name, u_int32_t min, u_int32_t max, |
| 117 | int invert) |
| 118 | { |
| 119 | const char *inv = invert ? "!" : ""; |
| 120 | |
| 121 | if (min != 0 || max != 0xFFFFFFFF || invert) { |
| 122 | printf("%s", name); |
| 123 | if (min == max) { |
| 124 | printf(":%s", inv); |
| 125 | printf("%u", min); |
| 126 | } else { |
| 127 | printf("s:%s", inv); |
| 128 | printf("%u",min); |
| 129 | printf(":"); |
| 130 | printf("%u",max); |
| 131 | } |
| 132 | printf(" "); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* Prints out the union ipt_matchinfo. */ |
| 137 | static void |
| 138 | print(const struct ipt_ip *ip, |
| 139 | const struct ipt_entry_match *match, int numeric) |
| 140 | { |
| 141 | const struct ipt_esp *esp = (struct ipt_esp *)match->data; |
| 142 | |
| 143 | printf("esp "); |
| 144 | print_spis("spi", esp->spis[0], esp->spis[1], |
| 145 | esp->invflags & IPT_ESP_INV_SPI); |
| 146 | if (esp->invflags & ~IPT_ESP_INV_MASK) |
| 147 | printf("Unknown invflags: 0x%X ", |
| 148 | esp->invflags & ~IPT_ESP_INV_MASK); |
| 149 | } |
| 150 | |
| 151 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
| 152 | static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 153 | { |
| 154 | const struct ipt_esp *espinfo = (struct ipt_esp *)match->data; |
| 155 | |
| 156 | if (espinfo->spis[0] != 0 |
| 157 | && espinfo->spis[1] != 0xFFFFFFFF) { |
| 158 | if (espinfo->invflags & IPT_ESP_INV_SPI) |
| 159 | printf("! "); |
| 160 | if (espinfo->spis[0] |
| 161 | != espinfo->spis[1]) |
| 162 | printf("--spi %u-%u ", |
| 163 | espinfo->spis[0], |
| 164 | espinfo->spis[1]); |
| 165 | else |
| 166 | printf("--spi %u ", |
| 167 | espinfo->spis[0]); |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | |
| 172 | struct iptables_match esp |
| 173 | = { NULL, |
| 174 | "esp", |
| 175 | NETFILTER_VERSION, |
| 176 | IPT_ALIGN(sizeof(struct ipt_esp)), |
| 177 | IPT_ALIGN(sizeof(struct ipt_esp)), |
| 178 | &help, |
| 179 | &init, |
| 180 | &parse, |
| 181 | &final_check, |
| 182 | &print, |
| 183 | &save, |
| 184 | opts |
| 185 | }; |
| 186 | |
| 187 | void |
| 188 | _init(void) |
| 189 | { |
| 190 | register_match(&esp); |
| 191 | } |