Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add IP range matching support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | |
Thomas Jarosch | 240eee6 | 2008-10-23 15:40:52 +0200 | [diff] [blame] | 8 | #include <netinet/in.h> |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 9 | #include <xtables.h> |
| 10 | #include <linux/netfilter.h> |
| 11 | #include <linux/netfilter/xt_iprange.h> |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 12 | #include <linux/netfilter_ipv4/ipt_iprange.h> |
| 13 | |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 14 | enum { |
| 15 | F_SRCIP = 1 << 0, |
| 16 | F_DSTIP = 1 << 1, |
| 17 | }; |
| 18 | |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 19 | static void iprange_mt_help(void) |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 20 | { |
| 21 | printf( |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 22 | "iprange match options:\n" |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 23 | "[!] --src-range ip-ip Match source IP in the specified range\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 24 | "[!] --dst-range ip-ip Match destination IP in the specified range\n"); |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 27 | static const struct option iprange_mt_opts[] = { |
| 28 | {.name = "src-range", .has_arg = true, .val = '1'}, |
| 29 | {.name = "dst-range", .has_arg = true, .val = '2'}, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 30 | { .name = NULL } |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 33 | static void |
| 34 | parse_iprange(char *arg, struct ipt_iprange *range) |
| 35 | { |
| 36 | char *dash; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 37 | const struct in_addr *ip; |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 38 | |
| 39 | dash = strchr(arg, '-'); |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 40 | if (dash != NULL) |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 41 | *dash = '\0'; |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 42 | |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 43 | ip = xtables_numeric_to_ipaddr(arg); |
James King | dbe6c3b | 2008-04-01 21:17:36 +0200 | [diff] [blame] | 44 | if (!ip) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 45 | xtables_error(PARAMETER_PROBLEM, "iprange match: Bad IP address \"%s\"\n", |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 46 | arg); |
| 47 | range->min_ip = ip->s_addr; |
| 48 | |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 49 | if (dash != NULL) { |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 50 | ip = xtables_numeric_to_ipaddr(dash+1); |
James King | dbe6c3b | 2008-04-01 21:17:36 +0200 | [diff] [blame] | 51 | if (!ip) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 52 | xtables_error(PARAMETER_PROBLEM, "iprange match: Bad IP address \"%s\"\n", |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 53 | dash+1); |
| 54 | range->max_ip = ip->s_addr; |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 55 | } else { |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 56 | range->max_ip = range->min_ip; |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 57 | } |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 60 | static int iprange_parse(int c, char **argv, int invert, unsigned int *flags, |
| 61 | const void *entry, struct xt_entry_match **match) |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 62 | { |
| 63 | struct ipt_iprange_info *info = (struct ipt_iprange_info *)(*match)->data; |
| 64 | |
| 65 | switch (c) { |
| 66 | case '1': |
| 67 | if (*flags & IPRANGE_SRC) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 68 | xtables_error(PARAMETER_PROBLEM, |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 69 | "iprange match: Only use --src-range ONCE!"); |
| 70 | *flags |= IPRANGE_SRC; |
| 71 | |
| 72 | info->flags |= IPRANGE_SRC; |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 73 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 74 | if (invert) |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 75 | info->flags |= IPRANGE_SRC_INV; |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 76 | parse_iprange(optarg, &info->src); |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 77 | |
| 78 | break; |
| 79 | |
| 80 | case '2': |
| 81 | if (*flags & IPRANGE_DST) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 82 | xtables_error(PARAMETER_PROBLEM, |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 83 | "iprange match: Only use --dst-range ONCE!"); |
| 84 | *flags |= IPRANGE_DST; |
| 85 | |
| 86 | info->flags |= IPRANGE_DST; |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 87 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 88 | if (invert) |
| 89 | info->flags |= IPRANGE_DST_INV; |
| 90 | |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 91 | parse_iprange(optarg, &info->dst); |
Nicolas Bouliane | b9c6ec1 | 2004-07-12 07:16:54 +0000 | [diff] [blame] | 92 | |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 93 | break; |
| 94 | |
| 95 | default: |
| 96 | return 0; |
| 97 | } |
| 98 | return 1; |
| 99 | } |
| 100 | |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 101 | static int |
| 102 | iprange_mt4_parse(int c, char **argv, int invert, unsigned int *flags, |
| 103 | const void *entry, struct xt_entry_match **match) |
| 104 | { |
| 105 | struct xt_iprange_mtinfo *info = (void *)(*match)->data; |
| 106 | const struct in_addr *ia; |
| 107 | char *end; |
| 108 | |
| 109 | switch (c) { |
Jan Engelhardt | bfb7e0b | 2008-09-01 14:19:03 +0200 | [diff] [blame] | 110 | case '1': /* --src-range */ |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 111 | end = strchr(optarg, '-'); |
| 112 | if (end == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 113 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--src-range", optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 114 | *end = '\0'; |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 115 | ia = xtables_numeric_to_ipaddr(optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 116 | if (ia == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 117 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--src-range", optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 118 | memcpy(&info->src_min.in, ia, sizeof(*ia)); |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 119 | ia = xtables_numeric_to_ipaddr(end+1); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 120 | if (ia == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 121 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--src-range", end + 1); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 122 | memcpy(&info->src_max.in, ia, sizeof(*ia)); |
Jan Engelhardt | 6a0cd58 | 2008-06-13 17:59:29 +0200 | [diff] [blame] | 123 | info->flags |= IPRANGE_SRC; |
| 124 | if (invert) |
| 125 | info->flags |= IPRANGE_SRC_INV; |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 126 | *flags |= F_SRCIP; |
| 127 | return true; |
| 128 | |
Jan Engelhardt | bfb7e0b | 2008-09-01 14:19:03 +0200 | [diff] [blame] | 129 | case '2': /* --dst-range */ |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 130 | end = strchr(optarg, '-'); |
| 131 | if (end == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 132 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--dst-range", optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 133 | *end = '\0'; |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 134 | ia = xtables_numeric_to_ipaddr(optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 135 | if (ia == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 136 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--dst-range", optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 137 | memcpy(&info->dst_min.in, ia, sizeof(*ia)); |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 138 | ia = xtables_numeric_to_ipaddr(end + 1); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 139 | if (ia == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 140 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--dst-range", end + 1); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 141 | memcpy(&info->dst_max.in, ia, sizeof(*ia)); |
Jan Engelhardt | 6a0cd58 | 2008-06-13 17:59:29 +0200 | [diff] [blame] | 142 | info->flags |= IPRANGE_DST; |
| 143 | if (invert) |
| 144 | info->flags |= IPRANGE_DST_INV; |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 145 | *flags |= F_DSTIP; |
| 146 | return true; |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | static int |
| 152 | iprange_mt6_parse(int c, char **argv, int invert, unsigned int *flags, |
| 153 | const void *entry, struct xt_entry_match **match) |
| 154 | { |
| 155 | struct xt_iprange_mtinfo *info = (void *)(*match)->data; |
| 156 | const struct in6_addr *ia; |
| 157 | char *end; |
| 158 | |
| 159 | switch (c) { |
Jan Engelhardt | bfb7e0b | 2008-09-01 14:19:03 +0200 | [diff] [blame] | 160 | case '1': /* --src-range */ |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 161 | end = strchr(optarg, '-'); |
| 162 | if (end == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 163 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--src-range", optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 164 | *end = '\0'; |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 165 | ia = xtables_numeric_to_ip6addr(optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 166 | if (ia == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 167 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--src-range", optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 168 | memcpy(&info->src_min.in, ia, sizeof(*ia)); |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 169 | ia = xtables_numeric_to_ip6addr(end+1); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 170 | if (ia == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 171 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--src-range", end + 1); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 172 | memcpy(&info->src_max.in, ia, sizeof(*ia)); |
| 173 | info->flags |= IPRANGE_SRC; |
| 174 | if (invert) |
| 175 | info->flags |= IPRANGE_SRC_INV; |
| 176 | *flags |= F_SRCIP; |
| 177 | return true; |
| 178 | |
Jan Engelhardt | bfb7e0b | 2008-09-01 14:19:03 +0200 | [diff] [blame] | 179 | case '2': /* --dst-range */ |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 180 | end = strchr(optarg, '-'); |
| 181 | if (end == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 182 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--dst-range", optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 183 | *end = '\0'; |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 184 | ia = xtables_numeric_to_ip6addr(optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 185 | if (ia == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 186 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--dst-range", optarg); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 187 | memcpy(&info->dst_min.in, ia, sizeof(*ia)); |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 188 | ia = xtables_numeric_to_ip6addr(end + 1); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 189 | if (ia == NULL) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 190 | xtables_param_act(XTF_BAD_VALUE, "iprange", "--dst-range", end + 1); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 191 | memcpy(&info->dst_max.in, ia, sizeof(*ia)); |
| 192 | info->flags |= IPRANGE_DST; |
| 193 | if (invert) |
| 194 | info->flags |= IPRANGE_DST_INV; |
| 195 | *flags |= F_DSTIP; |
| 196 | return true; |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 201 | static void iprange_mt_check(unsigned int flags) |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 202 | { |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 203 | if (flags == 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 204 | xtables_error(PARAMETER_PROBLEM, |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 205 | "iprange match: You must specify `--src-range' or `--dst-range'"); |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | print_iprange(const struct ipt_iprange *range) |
| 210 | { |
| 211 | const unsigned char *byte_min, *byte_max; |
| 212 | |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 213 | byte_min = (const unsigned char *)&range->min_ip; |
| 214 | byte_max = (const unsigned char *)&range->max_ip; |
| 215 | printf("%u.%u.%u.%u-%u.%u.%u.%u ", |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 216 | byte_min[0], byte_min[1], byte_min[2], byte_min[3], |
| 217 | byte_max[0], byte_max[1], byte_max[2], byte_max[3]); |
| 218 | } |
| 219 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 220 | static void iprange_print(const void *ip, const struct xt_entry_match *match, |
| 221 | int numeric) |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 222 | { |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 223 | const struct ipt_iprange_info *info = (const void *)match->data; |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 224 | |
| 225 | if (info->flags & IPRANGE_SRC) { |
| 226 | printf("source IP range "); |
| 227 | if (info->flags & IPRANGE_SRC_INV) |
| 228 | printf("! "); |
| 229 | print_iprange(&info->src); |
| 230 | } |
| 231 | if (info->flags & IPRANGE_DST) { |
| 232 | printf("destination IP range "); |
| 233 | if (info->flags & IPRANGE_DST_INV) |
| 234 | printf("! "); |
| 235 | print_iprange(&info->dst); |
| 236 | } |
| 237 | } |
| 238 | |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 239 | static void |
| 240 | iprange_mt4_print(const void *ip, const struct xt_entry_match *match, |
| 241 | int numeric) |
| 242 | { |
| 243 | const struct xt_iprange_mtinfo *info = (const void *)match->data; |
| 244 | |
| 245 | if (info->flags & IPRANGE_SRC) { |
| 246 | printf("source IP range "); |
| 247 | if (info->flags & IPRANGE_SRC_INV) |
| 248 | printf("! "); |
| 249 | /* |
| 250 | * ipaddr_to_numeric() uses a static buffer, so cannot |
| 251 | * combine the printf() calls. |
| 252 | */ |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 253 | printf("%s", xtables_ipaddr_to_numeric(&info->src_min.in)); |
| 254 | printf("-%s ", xtables_ipaddr_to_numeric(&info->src_max.in)); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 255 | } |
| 256 | if (info->flags & IPRANGE_DST) { |
| 257 | printf("destination IP range "); |
| 258 | if (info->flags & IPRANGE_DST_INV) |
| 259 | printf("! "); |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 260 | printf("%s", xtables_ipaddr_to_numeric(&info->dst_min.in)); |
| 261 | printf("-%s ", xtables_ipaddr_to_numeric(&info->dst_max.in)); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | static void |
| 266 | iprange_mt6_print(const void *ip, const struct xt_entry_match *match, |
| 267 | int numeric) |
| 268 | { |
| 269 | const struct xt_iprange_mtinfo *info = (const void *)match->data; |
| 270 | |
| 271 | if (info->flags & IPRANGE_SRC) { |
| 272 | printf("source IP range "); |
| 273 | if (info->flags & IPRANGE_SRC_INV) |
| 274 | printf("! "); |
| 275 | /* |
| 276 | * ipaddr_to_numeric() uses a static buffer, so cannot |
| 277 | * combine the printf() calls. |
| 278 | */ |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 279 | printf("%s", xtables_ip6addr_to_numeric(&info->src_min.in6)); |
| 280 | printf("-%s ", xtables_ip6addr_to_numeric(&info->src_max.in6)); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 281 | } |
| 282 | if (info->flags & IPRANGE_DST) { |
| 283 | printf("destination IP range "); |
| 284 | if (info->flags & IPRANGE_DST_INV) |
| 285 | printf("! "); |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 286 | printf("%s", xtables_ip6addr_to_numeric(&info->dst_min.in6)); |
| 287 | printf("-%s ", xtables_ip6addr_to_numeric(&info->dst_max.in6)); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 291 | static void iprange_save(const void *ip, const struct xt_entry_match *match) |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 292 | { |
Jan Engelhardt | 41daaa0 | 2008-01-20 13:42:43 +0000 | [diff] [blame] | 293 | const struct ipt_iprange_info *info = (const void *)match->data; |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 294 | |
| 295 | if (info->flags & IPRANGE_SRC) { |
| 296 | if (info->flags & IPRANGE_SRC_INV) |
| 297 | printf("! "); |
| 298 | printf("--src-range "); |
| 299 | print_iprange(&info->src); |
| 300 | if (info->flags & IPRANGE_DST) |
| 301 | fputc(' ', stdout); |
| 302 | } |
| 303 | if (info->flags & IPRANGE_DST) { |
| 304 | if (info->flags & IPRANGE_DST_INV) |
| 305 | printf("! "); |
| 306 | printf("--dst-range "); |
| 307 | print_iprange(&info->dst); |
| 308 | } |
| 309 | } |
| 310 | |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 311 | static void iprange_mt4_save(const void *ip, const struct xt_entry_match *match) |
| 312 | { |
| 313 | const struct xt_iprange_mtinfo *info = (const void *)match->data; |
| 314 | |
| 315 | if (info->flags & IPRANGE_SRC) { |
| 316 | if (info->flags & IPRANGE_SRC_INV) |
| 317 | printf("! "); |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 318 | printf("--src-range %s", xtables_ipaddr_to_numeric(&info->src_min.in)); |
| 319 | printf("-%s ", xtables_ipaddr_to_numeric(&info->src_max.in)); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 320 | } |
| 321 | if (info->flags & IPRANGE_DST) { |
| 322 | if (info->flags & IPRANGE_DST_INV) |
| 323 | printf("! "); |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 324 | printf("--dst-range %s", xtables_ipaddr_to_numeric(&info->dst_min.in)); |
| 325 | printf("-%s ", xtables_ipaddr_to_numeric(&info->dst_max.in)); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | static void iprange_mt6_save(const void *ip, const struct xt_entry_match *match) |
| 330 | { |
| 331 | const struct xt_iprange_mtinfo *info = (const void *)match->data; |
| 332 | |
| 333 | if (info->flags & IPRANGE_SRC) { |
| 334 | if (info->flags & IPRANGE_SRC_INV) |
| 335 | printf("! "); |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 336 | printf("--src-range %s", xtables_ip6addr_to_numeric(&info->src_min.in6)); |
| 337 | printf("-%s ", xtables_ip6addr_to_numeric(&info->src_max.in6)); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 338 | } |
| 339 | if (info->flags & IPRANGE_DST) { |
| 340 | if (info->flags & IPRANGE_DST_INV) |
| 341 | printf("! "); |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 342 | printf("--dst-range %s", xtables_ip6addr_to_numeric(&info->dst_min.in6)); |
| 343 | printf("-%s ", xtables_ip6addr_to_numeric(&info->dst_max.in6)); |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame^] | 347 | static struct xtables_match iprange_mt_reg[] = { |
| 348 | { |
| 349 | .version = XTABLES_VERSION, |
| 350 | .name = "iprange", |
| 351 | .revision = 0, |
| 352 | .family = NFPROTO_IPV4, |
| 353 | .size = XT_ALIGN(sizeof(struct ipt_iprange_info)), |
| 354 | .userspacesize = XT_ALIGN(sizeof(struct ipt_iprange_info)), |
| 355 | .help = iprange_mt_help, |
| 356 | .parse = iprange_parse, |
| 357 | .final_check = iprange_mt_check, |
| 358 | .print = iprange_print, |
| 359 | .save = iprange_save, |
| 360 | .extra_opts = iprange_mt_opts, |
| 361 | }, |
| 362 | { |
| 363 | .version = XTABLES_VERSION, |
| 364 | .name = "iprange", |
| 365 | .revision = 1, |
| 366 | .family = NFPROTO_IPV4, |
| 367 | .size = XT_ALIGN(sizeof(struct xt_iprange_mtinfo)), |
| 368 | .userspacesize = XT_ALIGN(sizeof(struct xt_iprange_mtinfo)), |
| 369 | .help = iprange_mt_help, |
| 370 | .parse = iprange_mt4_parse, |
| 371 | .final_check = iprange_mt_check, |
| 372 | .print = iprange_mt4_print, |
| 373 | .save = iprange_mt4_save, |
| 374 | .extra_opts = iprange_mt_opts, |
| 375 | }, |
| 376 | { |
| 377 | .version = XTABLES_VERSION, |
| 378 | .name = "iprange", |
| 379 | .revision = 1, |
| 380 | .family = NFPROTO_IPV6, |
| 381 | .size = XT_ALIGN(sizeof(struct xt_iprange_mtinfo)), |
| 382 | .userspacesize = XT_ALIGN(sizeof(struct xt_iprange_mtinfo)), |
| 383 | .help = iprange_mt_help, |
| 384 | .parse = iprange_mt6_parse, |
| 385 | .final_check = iprange_mt_check, |
| 386 | .print = iprange_mt6_print, |
| 387 | .save = iprange_mt6_save, |
| 388 | .extra_opts = iprange_mt_opts, |
| 389 | }, |
Jan Engelhardt | fc11b0b | 2008-01-20 13:43:49 +0000 | [diff] [blame] | 390 | }; |
| 391 | |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 392 | void _init(void) |
| 393 | { |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame^] | 394 | xtables_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg)); |
Joszef Kadlecsik | 9cb6615 | 2003-04-23 13:27:09 +0000 | [diff] [blame] | 395 | } |