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