Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add UDP support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 7 | #include <xtables.h> |
| 8 | #include <linux/netfilter/xt_tcpudp.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 9 | |
| 10 | /* Function which prints out usage message. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 11 | static void udp_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 12 | { |
| 13 | printf( |
| 14 | "UDP v%s options:\n" |
| 15 | " --source-port [!] port[:port]\n" |
| 16 | " --sport ...\n" |
| 17 | " match source port(s)\n" |
| 18 | " --destination-port [!] port[:port]\n" |
| 19 | " --dport ...\n" |
| 20 | " match destination port(s)\n", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 21 | IPTABLES_VERSION); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 24 | static const struct option udp_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 25 | { "source-port", 1, NULL, '1' }, |
| 26 | { "sport", 1, NULL, '1' }, /* synonym */ |
| 27 | { "destination-port", 1, NULL, '2' }, |
| 28 | { "dport", 1, NULL, '2' }, /* synonym */ |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 29 | { .name = NULL } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 32 | static void |
| 33 | parse_udp_ports(const char *portstring, u_int16_t *ports) |
| 34 | { |
| 35 | char *buffer; |
| 36 | char *cp; |
| 37 | |
| 38 | buffer = strdup(portstring); |
| 39 | if ((cp = strchr(buffer, ':')) == NULL) |
Phil Oester | dbac8ad | 2006-07-20 17:01:54 +0000 | [diff] [blame] | 40 | ports[0] = ports[1] = parse_port(buffer, "udp"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 41 | else { |
| 42 | *cp = '\0'; |
| 43 | cp++; |
| 44 | |
Phil Oester | dbac8ad | 2006-07-20 17:01:54 +0000 | [diff] [blame] | 45 | ports[0] = buffer[0] ? parse_port(buffer, "udp") : 0; |
| 46 | ports[1] = cp[0] ? parse_port(cp, "udp") : 0xFFFF; |
Harald Welte | d15fb34 | 2002-07-26 16:27:57 +0000 | [diff] [blame] | 47 | |
| 48 | if (ports[0] > ports[1]) |
| 49 | exit_error(PARAMETER_PROBLEM, |
| 50 | "invalid portrange (min > max)"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 51 | } |
| 52 | free(buffer); |
| 53 | } |
| 54 | |
| 55 | /* Initialize the match. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 56 | static void udp_init(struct xt_entry_match *m) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 57 | { |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 58 | struct xt_udp *udpinfo = (struct xt_udp *)m->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 59 | |
| 60 | udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF; |
| 61 | } |
| 62 | |
| 63 | #define UDP_SRC_PORTS 0x01 |
| 64 | #define UDP_DST_PORTS 0x02 |
| 65 | |
| 66 | /* Function which parses command options; returns true if it |
| 67 | ate an option */ |
| 68 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 69 | udp_parse(int c, char **argv, int invert, unsigned int *flags, |
| 70 | const void *entry, struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 71 | { |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 72 | struct xt_udp *udpinfo = (struct xt_udp *)(*match)->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 73 | |
| 74 | switch (c) { |
| 75 | case '1': |
| 76 | if (*flags & UDP_SRC_PORTS) |
| 77 | exit_error(PARAMETER_PROBLEM, |
| 78 | "Only one `--source-port' allowed"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 79 | check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 80 | parse_udp_ports(argv[optind-1], udpinfo->spts); |
| 81 | if (invert) |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 82 | udpinfo->invflags |= XT_UDP_INV_SRCPT; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 83 | *flags |= UDP_SRC_PORTS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 84 | break; |
| 85 | |
| 86 | case '2': |
| 87 | if (*flags & UDP_DST_PORTS) |
| 88 | exit_error(PARAMETER_PROBLEM, |
| 89 | "Only one `--destination-port' allowed"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 90 | check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 91 | parse_udp_ports(argv[optind-1], udpinfo->dpts); |
| 92 | if (invert) |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 93 | udpinfo->invflags |= XT_UDP_INV_DSTPT; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 94 | *flags |= UDP_DST_PORTS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 95 | break; |
| 96 | |
| 97 | default: |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | return 1; |
| 102 | } |
| 103 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 104 | static char * |
| 105 | port_to_service(int port) |
| 106 | { |
| 107 | struct servent *service; |
| 108 | |
| 109 | if ((service = getservbyport(htons(port), "udp"))) |
| 110 | return service->s_name; |
| 111 | |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | print_port(u_int16_t port, int numeric) |
| 117 | { |
| 118 | char *service; |
| 119 | |
| 120 | if (numeric || (service = port_to_service(port)) == NULL) |
| 121 | printf("%u", port); |
| 122 | else |
| 123 | printf("%s", service); |
| 124 | } |
| 125 | |
| 126 | static void |
| 127 | print_ports(const char *name, u_int16_t min, u_int16_t max, |
| 128 | int invert, int numeric) |
| 129 | { |
| 130 | const char *inv = invert ? "!" : ""; |
| 131 | |
| 132 | if (min != 0 || max != 0xFFFF || invert) { |
| 133 | printf("%s", name); |
| 134 | if (min == max) { |
| 135 | printf(":%s", inv); |
| 136 | print_port(min, numeric); |
| 137 | } else { |
| 138 | printf("s:%s", inv); |
| 139 | print_port(min, numeric); |
| 140 | printf(":"); |
| 141 | print_port(max, numeric); |
| 142 | } |
| 143 | printf(" "); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Prints out the union ipt_matchinfo. */ |
| 148 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 149 | udp_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 150 | { |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 151 | const struct xt_udp *udp = (struct xt_udp *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 152 | |
| 153 | printf("udp "); |
| 154 | print_ports("spt", udp->spts[0], udp->spts[1], |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 155 | udp->invflags & XT_UDP_INV_SRCPT, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 156 | numeric); |
| 157 | print_ports("dpt", udp->dpts[0], udp->dpts[1], |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 158 | udp->invflags & XT_UDP_INV_DSTPT, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 159 | numeric); |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 160 | if (udp->invflags & ~XT_UDP_INV_MASK) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 161 | printf("Unknown invflags: 0x%X ", |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 162 | udp->invflags & ~XT_UDP_INV_MASK); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 166 | static void udp_save(const void *ip, const struct xt_entry_match *match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 167 | { |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 168 | const struct xt_udp *udpinfo = (struct xt_udp *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 169 | |
| 170 | if (udpinfo->spts[0] != 0 |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 171 | || udpinfo->spts[1] != 0xFFFF) { |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 172 | if (udpinfo->invflags & XT_UDP_INV_SRCPT) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 173 | printf("! "); |
| 174 | if (udpinfo->spts[0] |
| 175 | != udpinfo->spts[1]) |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 176 | printf("--sport %u:%u ", |
| 177 | udpinfo->spts[0], |
Marc Boucher | 2382c8c | 2000-04-07 17:32:49 +0000 | [diff] [blame] | 178 | udpinfo->spts[1]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 179 | else |
| 180 | printf("--sport %u ", |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 181 | udpinfo->spts[0]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (udpinfo->dpts[0] != 0 |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 185 | || udpinfo->dpts[1] != 0xFFFF) { |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 186 | if (udpinfo->invflags & XT_UDP_INV_DSTPT) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 187 | printf("! "); |
| 188 | if (udpinfo->dpts[0] |
| 189 | != udpinfo->dpts[1]) |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 190 | printf("--dport %u:%u ", |
| 191 | udpinfo->dpts[0], |
| 192 | udpinfo->dpts[1]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 193 | else |
| 194 | printf("--dport %u ", |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 195 | udpinfo->dpts[0]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 199 | static struct xtables_match udp_match = { |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 200 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 201 | .name = "udp", |
| 202 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 203 | .size = XT_ALIGN(sizeof(struct xt_udp)), |
| 204 | .userspacesize = XT_ALIGN(sizeof(struct xt_udp)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 205 | .help = udp_help, |
| 206 | .init = udp_init, |
| 207 | .parse = udp_parse, |
| 208 | .print = udp_print, |
| 209 | .save = udp_save, |
| 210 | .extra_opts = udp_opts, |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 213 | static struct xtables_match udp_match6 = { |
Yasuyuki KOZAKAI | 17908e4 | 2007-07-24 06:56:21 +0000 | [diff] [blame] | 214 | .family = AF_INET6, |
| 215 | .name = "udp", |
| 216 | .version = IPTABLES_VERSION, |
| 217 | .size = XT_ALIGN(sizeof(struct xt_udp)), |
| 218 | .userspacesize = XT_ALIGN(sizeof(struct xt_udp)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 219 | .help = udp_help, |
| 220 | .init = udp_init, |
| 221 | .parse = udp_parse, |
| 222 | .print = udp_print, |
| 223 | .save = udp_save, |
| 224 | .extra_opts = udp_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | void |
| 228 | _init(void) |
| 229 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 230 | xtables_register_match(&udp_match); |
| 231 | xtables_register_match(&udp_match6); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 232 | } |