libxt_udp.c revision 500f483fff529dcd88ec96b9d5054be6cd6363a0
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
107/* Final check; we don't care. */
108static void
109final_check(unsigned int flags)
110{
111}
112
113static char *
114port_to_service(int port)
115{
116	struct servent *service;
117
118	if ((service = getservbyport(htons(port), "udp")))
119		return service->s_name;
120
121	return NULL;
122}
123
124static void
125print_port(u_int16_t port, int numeric)
126{
127	char *service;
128
129	if (numeric || (service = port_to_service(port)) == NULL)
130		printf("%u", port);
131	else
132		printf("%s", service);
133}
134
135static void
136print_ports(const char *name, u_int16_t min, u_int16_t max,
137	    int invert, int numeric)
138{
139	const char *inv = invert ? "!" : "";
140
141	if (min != 0 || max != 0xFFFF || invert) {
142		printf("%s", name);
143		if (min == max) {
144			printf(":%s", inv);
145			print_port(min, numeric);
146		} else {
147			printf("s:%s", inv);
148			print_port(min, numeric);
149			printf(":");
150			print_port(max, numeric);
151		}
152		printf(" ");
153	}
154}
155
156/* Prints out the union ipt_matchinfo. */
157static void
158print(const void *ip,
159      const struct xt_entry_match *match, int numeric)
160{
161	const struct xt_udp *udp = (struct xt_udp *)match->data;
162
163	printf("udp ");
164	print_ports("spt", udp->spts[0], udp->spts[1],
165		    udp->invflags & XT_UDP_INV_SRCPT,
166		    numeric);
167	print_ports("dpt", udp->dpts[0], udp->dpts[1],
168		    udp->invflags & XT_UDP_INV_DSTPT,
169		    numeric);
170	if (udp->invflags & ~XT_UDP_INV_MASK)
171		printf("Unknown invflags: 0x%X ",
172		       udp->invflags & ~XT_UDP_INV_MASK);
173}
174
175/* Saves the union ipt_matchinfo in parsable form to stdout. */
176static void save(const void *ip, const struct xt_entry_match *match)
177{
178	const struct xt_udp *udpinfo = (struct xt_udp *)match->data;
179
180	if (udpinfo->spts[0] != 0
181	    || udpinfo->spts[1] != 0xFFFF) {
182		if (udpinfo->invflags & XT_UDP_INV_SRCPT)
183			printf("! ");
184		if (udpinfo->spts[0]
185		    != udpinfo->spts[1])
186			printf("--sport %u:%u ",
187			       udpinfo->spts[0],
188			       udpinfo->spts[1]);
189		else
190			printf("--sport %u ",
191			       udpinfo->spts[0]);
192	}
193
194	if (udpinfo->dpts[0] != 0
195	    || udpinfo->dpts[1] != 0xFFFF) {
196		if (udpinfo->invflags & XT_UDP_INV_DSTPT)
197			printf("! ");
198		if (udpinfo->dpts[0]
199		    != udpinfo->dpts[1])
200			printf("--dport %u:%u ",
201			       udpinfo->dpts[0],
202			       udpinfo->dpts[1]);
203		else
204			printf("--dport %u ",
205			       udpinfo->dpts[0]);
206	}
207}
208
209static
210struct xtables_match udp = {
211	.family		= AF_INET,
212	.name		= "udp",
213	.version	= IPTABLES_VERSION,
214	.size		= XT_ALIGN(sizeof(struct xt_udp)),
215	.userspacesize	= XT_ALIGN(sizeof(struct xt_udp)),
216	.help		= &help,
217	.init		= &init,
218	.parse		= &parse,
219	.final_check	= &final_check,
220	.print		= &print,
221	.save		= &save,
222	.extra_opts	= opts
223};
224
225static
226struct xtables_match udp6 = {
227	.family		= AF_INET6,
228	.name		= "udp",
229	.version	= IPTABLES_VERSION,
230	.size		= XT_ALIGN(sizeof(struct xt_udp)),
231	.userspacesize	= XT_ALIGN(sizeof(struct xt_udp)),
232	.help		= &help,
233	.init		= &init,
234	.parse		= &parse,
235	.final_check	= &final_check,
236	.print		= &print,
237	.save		= &save,
238	.extra_opts	= opts
239};
240
241void
242_init(void)
243{
244	xtables_register_match(&udp);
245	xtables_register_match(&udp6);
246}
247