1 /* Shared library add-on to iptables to add UDP support. */
8 #include <linux/netfilter/xt_tcpudp.h>
10 /* Function which prints out usage message. */
11 static void udp_help(void)
14 "udp match options:\n"
15 " --source-port [!] port[:port]\n"
17 " match source port(s)\n"
18 " --destination-port [!] port[:port]\n"
20 " match destination port(s)\n");
23 static const struct option udp_opts[] = {
24 { "source-port", 1, NULL, '1' },
25 { "sport", 1, NULL, '1' }, /* synonym */
26 { "destination-port", 1, NULL, '2' },
27 { "dport", 1, NULL, '2' }, /* synonym */
32 parse_udp_ports(const char *portstring, u_int16_t *ports)
37 buffer = strdup(portstring);
38 if ((cp = strchr(buffer, ':')) == NULL)
39 ports[0] = ports[1] = parse_port(buffer, "udp");
44 ports[0] = buffer[0] ? parse_port(buffer, "udp") : 0;
45 ports[1] = cp[0] ? parse_port(cp, "udp") : 0xFFFF;
47 if (ports[0] > ports[1])
48 exit_error(PARAMETER_PROBLEM,
49 "invalid portrange (min > max)");
54 /* Initialize the match. */
55 static void udp_init(struct xt_entry_match *m)
57 struct xt_udp *udpinfo = (struct xt_udp *)m->data;
59 udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF;
62 #define UDP_SRC_PORTS 0x01
63 #define UDP_DST_PORTS 0x02
65 /* Function which parses command options; returns true if it
68 udp_parse(int c, char **argv, int invert, unsigned int *flags,
69 const void *entry, struct xt_entry_match **match)
71 struct xt_udp *udpinfo = (struct xt_udp *)(*match)->data;
75 if (*flags & UDP_SRC_PORTS)
76 exit_error(PARAMETER_PROBLEM,
77 "Only one `--source-port' allowed");
78 check_inverse(optarg, &invert, &optind, 0);
79 parse_udp_ports(argv[optind-1], udpinfo->spts);
81 udpinfo->invflags |= XT_UDP_INV_SRCPT;
82 *flags |= UDP_SRC_PORTS;
86 if (*flags & UDP_DST_PORTS)
87 exit_error(PARAMETER_PROBLEM,
88 "Only one `--destination-port' allowed");
89 check_inverse(optarg, &invert, &optind, 0);
90 parse_udp_ports(argv[optind-1], udpinfo->dpts);
92 udpinfo->invflags |= XT_UDP_INV_DSTPT;
93 *flags |= UDP_DST_PORTS;
104 port_to_service(int port)
106 struct servent *service;
108 if ((service = getservbyport(htons(port), "udp")))
109 return service->s_name;
115 print_port(u_int16_t port, int numeric)
119 if (numeric || (service = port_to_service(port)) == NULL)
122 printf("%s", service);
126 print_ports(const char *name, u_int16_t min, u_int16_t max,
127 int invert, int numeric)
129 const char *inv = invert ? "!" : "";
131 if (min != 0 || max != 0xFFFF || invert) {
135 print_port(min, numeric);
138 print_port(min, numeric);
140 print_port(max, numeric);
146 /* Prints out the union ipt_matchinfo. */
148 udp_print(const void *ip, const struct xt_entry_match *match, int numeric)
150 const struct xt_udp *udp = (struct xt_udp *)match->data;
153 print_ports("spt", udp->spts[0], udp->spts[1],
154 udp->invflags & XT_UDP_INV_SRCPT,
156 print_ports("dpt", udp->dpts[0], udp->dpts[1],
157 udp->invflags & XT_UDP_INV_DSTPT,
159 if (udp->invflags & ~XT_UDP_INV_MASK)
160 printf("Unknown invflags: 0x%X ",
161 udp->invflags & ~XT_UDP_INV_MASK);
164 /* Saves the union ipt_matchinfo in parsable form to stdout. */
165 static void udp_save(const void *ip, const struct xt_entry_match *match)
167 const struct xt_udp *udpinfo = (struct xt_udp *)match->data;
169 if (udpinfo->spts[0] != 0
170 || udpinfo->spts[1] != 0xFFFF) {
171 if (udpinfo->invflags & XT_UDP_INV_SRCPT)
175 printf("--sport %u:%u ",
179 printf("--sport %u ",
183 if (udpinfo->dpts[0] != 0
184 || udpinfo->dpts[1] != 0xFFFF) {
185 if (udpinfo->invflags & XT_UDP_INV_DSTPT)
189 printf("--dport %u:%u ",
193 printf("--dport %u ",
198 static struct xtables_match udp_match = {
201 .version = XTABLES_VERSION,
202 .size = XT_ALIGN(sizeof(struct xt_udp)),
203 .userspacesize = XT_ALIGN(sizeof(struct xt_udp)),
209 .extra_opts = udp_opts,
212 static struct xtables_match udp_match6 = {
215 .version = XTABLES_VERSION,
216 .size = XT_ALIGN(sizeof(struct xt_udp)),
217 .userspacesize = XT_ALIGN(sizeof(struct xt_udp)),
223 .extra_opts = udp_opts,
229 xtables_register_match(&udp_match);
230 xtables_register_match(&udp_match6);