1 /* Shared library add-on to iptables to add multiple TCP port support. */
8 /* To ensure that iptables compiles with an old kernel */
9 #include "../include/linux/netfilter_ipv4/ipt_multiport.h"
11 /* Function which prints out usage message. */
16 "multiport v%s options:\n"
17 " --source-ports port[,port,port...]\n"
19 " match source port(s)\n"
20 " --destination-ports port[,port,port...]\n"
22 " match destination port(s)\n"
23 " --ports port[,port,port]\n"
24 " match both source and destination port(s)\n"
25 " NOTE: this kernel does not support port ranges in multiport.\n",
33 "multiport v%s options:\n"
34 " --source-ports [!] port[,port:port,port...]\n"
36 " match source port(s)\n"
37 " --destination-ports [!] port[,port:port,port...]\n"
39 " match destination port(s)\n"
40 " --ports [!] port[,port:port,port]\n"
41 " match both source and destination port(s)\n",
45 static struct option opts[] = {
46 { "source-ports", 1, 0, '1' },
47 { "sports", 1, 0, '1' }, /* synonym */
48 { "destination-ports", 1, 0, '2' },
49 { "dports", 1, 0, '2' }, /* synonym */
50 { "ports", 1, 0, '3' },
55 proto_to_name(u_int8_t proto)
74 parse_multi_ports(const char *portstring, u_int16_t *ports, const char *proto)
76 char *buffer, *cp, *next;
79 buffer = strdup(portstring);
80 if (!buffer) exit_error(OTHER_PROBLEM, "strdup failed");
82 for (cp=buffer, i=0; cp && i<IPT_MULTI_PORTS; cp=next,i++)
85 if (next) *next++='\0';
86 ports[i] = parse_port(cp, proto);
88 if (cp) exit_error(PARAMETER_PROBLEM, "too many ports specified");
94 parse_multi_ports_v1(const char *portstring,
95 struct ipt_multiport_v1 *multiinfo,
98 char *buffer, *cp, *next, *range;
102 buffer = strdup(portstring);
103 if (!buffer) exit_error(OTHER_PROBLEM, "strdup failed");
105 for (i=0; i<IPT_MULTI_PORTS; i++)
106 multiinfo->pflags[i] = 0;
108 for (cp=buffer, i=0; cp && i<IPT_MULTI_PORTS; cp=next, i++) {
109 next=strchr(cp, ',');
110 if (next) *next++='\0';
111 range = strchr(cp, ':');
113 if (i == IPT_MULTI_PORTS-1)
114 exit_error(PARAMETER_PROBLEM,
115 "too many ports specified");
118 multiinfo->ports[i] = parse_port(cp, proto);
120 multiinfo->pflags[i] = 1;
121 multiinfo->ports[++i] = parse_port(range, proto);
122 if (multiinfo->ports[i-1] >= multiinfo->ports[i])
123 exit_error(PARAMETER_PROBLEM,
124 "invalid portrange specified");
128 multiinfo->count = i;
129 if (cp) exit_error(PARAMETER_PROBLEM, "too many ports specified");
133 /* Initialize the match. */
135 init(struct ipt_entry_match *m, unsigned int *nfcache)
140 check_proto(const struct ipt_entry *entry)
144 if (entry->ip.invflags & IPT_INV_PROTO)
145 exit_error(PARAMETER_PROBLEM,
146 "multiport only works with TCP, UDP, UDPLITE, SCTP and DCCP");
148 if ((proto = proto_to_name(entry->ip.proto)) != NULL)
150 else if (!entry->ip.proto)
151 exit_error(PARAMETER_PROBLEM,
152 "multiport needs `-p tcp', `-p udp', `-p udplite', "
153 "`-p sctp' or `-p dccp'");
155 exit_error(PARAMETER_PROBLEM,
156 "multiport only works with TCP, UDP, UDPLITE, SCTP and DCCP");
159 /* Function which parses command options; returns true if it
162 parse(int c, char **argv, int invert, unsigned int *flags,
163 const struct ipt_entry *entry,
164 unsigned int *nfcache,
165 struct ipt_entry_match **match)
168 struct ipt_multiport *multiinfo
169 = (struct ipt_multiport *)(*match)->data;
173 check_inverse(argv[optind-1], &invert, &optind, 0);
174 proto = check_proto(entry);
175 multiinfo->count = parse_multi_ports(argv[optind-1],
176 multiinfo->ports, proto);
177 multiinfo->flags = IPT_MULTIPORT_SOURCE;
181 check_inverse(argv[optind-1], &invert, &optind, 0);
182 proto = check_proto(entry);
183 multiinfo->count = parse_multi_ports(argv[optind-1],
184 multiinfo->ports, proto);
185 multiinfo->flags = IPT_MULTIPORT_DESTINATION;
189 check_inverse(argv[optind-1], &invert, &optind, 0);
190 proto = check_proto(entry);
191 multiinfo->count = parse_multi_ports(argv[optind-1],
192 multiinfo->ports, proto);
193 multiinfo->flags = IPT_MULTIPORT_EITHER;
201 exit_error(PARAMETER_PROBLEM,
202 "multiport does not support invert");
205 exit_error(PARAMETER_PROBLEM,
206 "multiport can only have one option");
212 parse_v1(int c, char **argv, int invert, unsigned int *flags,
213 const struct ipt_entry *entry,
214 unsigned int *nfcache,
215 struct ipt_entry_match **match)
218 struct ipt_multiport_v1 *multiinfo
219 = (struct ipt_multiport_v1 *)(*match)->data;
223 check_inverse(argv[optind-1], &invert, &optind, 0);
224 proto = check_proto(entry);
225 parse_multi_ports_v1(argv[optind-1], multiinfo, proto);
226 multiinfo->flags = IPT_MULTIPORT_SOURCE;
230 check_inverse(argv[optind-1], &invert, &optind, 0);
231 proto = check_proto(entry);
232 parse_multi_ports_v1(argv[optind-1], multiinfo, proto);
233 multiinfo->flags = IPT_MULTIPORT_DESTINATION;
237 check_inverse(argv[optind-1], &invert, &optind, 0);
238 proto = check_proto(entry);
239 parse_multi_ports_v1(argv[optind-1], multiinfo, proto);
240 multiinfo->flags = IPT_MULTIPORT_EITHER;
248 multiinfo->invert = 1;
251 exit_error(PARAMETER_PROBLEM,
252 "multiport can only have one option");
257 /* Final check; must specify something. */
259 final_check(unsigned int flags)
262 exit_error(PARAMETER_PROBLEM, "multiport expection an option");
266 port_to_service(int port, u_int8_t proto)
268 struct servent *service;
270 if ((service = getservbyport(htons(port), proto_to_name(proto))))
271 return service->s_name;
277 print_port(u_int16_t port, u_int8_t protocol, int numeric)
281 if (numeric || (service = port_to_service(port, protocol)) == NULL)
284 printf("%s", service);
287 /* Prints out the matchinfo. */
289 print(const struct ipt_ip *ip,
290 const struct ipt_entry_match *match,
293 const struct ipt_multiport *multiinfo
294 = (const struct ipt_multiport *)match->data;
297 printf("multiport ");
299 switch (multiinfo->flags) {
300 case IPT_MULTIPORT_SOURCE:
304 case IPT_MULTIPORT_DESTINATION:
308 case IPT_MULTIPORT_EITHER:
317 for (i=0; i < multiinfo->count; i++) {
318 printf("%s", i ? "," : "");
319 print_port(multiinfo->ports[i], ip->proto, numeric);
325 print_v1(const struct ipt_ip *ip,
326 const struct ipt_entry_match *match,
329 const struct ipt_multiport_v1 *multiinfo
330 = (const struct ipt_multiport_v1 *)match->data;
333 printf("multiport ");
335 switch (multiinfo->flags) {
336 case IPT_MULTIPORT_SOURCE:
340 case IPT_MULTIPORT_DESTINATION:
344 case IPT_MULTIPORT_EITHER:
353 if (multiinfo->invert)
356 for (i=0; i < multiinfo->count; i++) {
357 printf("%s", i ? "," : "");
358 print_port(multiinfo->ports[i], ip->proto, numeric);
359 if (multiinfo->pflags[i]) {
361 print_port(multiinfo->ports[++i], ip->proto, numeric);
367 /* Saves the union ipt_matchinfo in parsable form to stdout. */
368 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
370 const struct ipt_multiport *multiinfo
371 = (const struct ipt_multiport *)match->data;
374 switch (multiinfo->flags) {
375 case IPT_MULTIPORT_SOURCE:
379 case IPT_MULTIPORT_DESTINATION:
383 case IPT_MULTIPORT_EITHER:
388 for (i=0; i < multiinfo->count; i++) {
389 printf("%s", i ? "," : "");
390 print_port(multiinfo->ports[i], ip->proto, 1);
395 static void save_v1(const struct ipt_ip *ip,
396 const struct ipt_entry_match *match)
398 const struct ipt_multiport_v1 *multiinfo
399 = (const struct ipt_multiport_v1 *)match->data;
402 switch (multiinfo->flags) {
403 case IPT_MULTIPORT_SOURCE:
407 case IPT_MULTIPORT_DESTINATION:
411 case IPT_MULTIPORT_EITHER:
416 if (multiinfo->invert)
419 for (i=0; i < multiinfo->count; i++) {
420 printf("%s", i ? "," : "");
421 print_port(multiinfo->ports[i], ip->proto, 1);
422 if (multiinfo->pflags[i]) {
424 print_port(multiinfo->ports[++i], ip->proto, 1);
430 static struct iptables_match multiport = {
434 .version = IPTABLES_VERSION,
435 .size = IPT_ALIGN(sizeof(struct ipt_multiport)),
436 .userspacesize = IPT_ALIGN(sizeof(struct ipt_multiport)),
440 .final_check = &final_check,
446 static struct iptables_match multiport_v1 = {
449 .version = IPTABLES_VERSION,
451 .size = IPT_ALIGN(sizeof(struct ipt_multiport_v1)),
452 .userspacesize = IPT_ALIGN(sizeof(struct ipt_multiport_v1)),
456 .final_check = &final_check,
465 register_match(&multiport);
466 register_match(&multiport_v1);