1 /* Shared library add-on to iptables to add UDP support. */
7 #include <netinet/in.h>
9 #include <linux/netfilter/xt_tcpudp.h>
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] = xtables_parse_port(buffer, "udp");
44 ports[0] = buffer[0] ? xtables_parse_port(buffer, "udp") : 0;
45 ports[1] = cp[0] ? xtables_parse_port(cp, "udp") : 0xFFFF;
47 if (ports[0] > ports[1])
48 xtables_error(PARAMETER_PROBLEM,
49 "invalid portrange (min > max)");
54 static void udp_init(struct xt_entry_match *m)
56 struct xt_udp *udpinfo = (struct xt_udp *)m->data;
58 udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF;
61 #define UDP_SRC_PORTS 0x01
62 #define UDP_DST_PORTS 0x02
65 udp_parse(int c, char **argv, int invert, unsigned int *flags,
66 const void *entry, struct xt_entry_match **match)
68 struct xt_udp *udpinfo = (struct xt_udp *)(*match)->data;
72 if (*flags & UDP_SRC_PORTS)
73 xtables_error(PARAMETER_PROBLEM,
74 "Only one `--source-port' allowed");
75 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
76 parse_udp_ports(optarg, udpinfo->spts);
78 udpinfo->invflags |= XT_UDP_INV_SRCPT;
79 *flags |= UDP_SRC_PORTS;
83 if (*flags & UDP_DST_PORTS)
84 xtables_error(PARAMETER_PROBLEM,
85 "Only one `--destination-port' allowed");
86 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
87 parse_udp_ports(optarg, udpinfo->dpts);
89 udpinfo->invflags |= XT_UDP_INV_DSTPT;
90 *flags |= UDP_DST_PORTS;
101 port_to_service(int port)
103 struct servent *service;
105 if ((service = getservbyport(htons(port), "udp")))
106 return service->s_name;
112 print_port(u_int16_t port, int numeric)
116 if (numeric || (service = port_to_service(port)) == NULL)
119 printf("%s", service);
123 print_ports(const char *name, u_int16_t min, u_int16_t max,
124 int invert, int numeric)
126 const char *inv = invert ? "!" : "";
128 if (min != 0 || max != 0xFFFF || invert) {
132 print_port(min, numeric);
135 print_port(min, numeric);
137 print_port(max, numeric);
144 udp_print(const void *ip, const struct xt_entry_match *match, int numeric)
146 const struct xt_udp *udp = (struct xt_udp *)match->data;
149 print_ports("spt", udp->spts[0], udp->spts[1],
150 udp->invflags & XT_UDP_INV_SRCPT,
152 print_ports("dpt", udp->dpts[0], udp->dpts[1],
153 udp->invflags & XT_UDP_INV_DSTPT,
155 if (udp->invflags & ~XT_UDP_INV_MASK)
156 printf("Unknown invflags: 0x%X ",
157 udp->invflags & ~XT_UDP_INV_MASK);
160 static void udp_save(const void *ip, const struct xt_entry_match *match)
162 const struct xt_udp *udpinfo = (struct xt_udp *)match->data;
164 if (udpinfo->spts[0] != 0
165 || udpinfo->spts[1] != 0xFFFF) {
166 if (udpinfo->invflags & XT_UDP_INV_SRCPT)
170 printf("--sport %u:%u ",
174 printf("--sport %u ",
178 if (udpinfo->dpts[0] != 0
179 || udpinfo->dpts[1] != 0xFFFF) {
180 if (udpinfo->invflags & XT_UDP_INV_DSTPT)
184 printf("--dport %u:%u ",
188 printf("--dport %u ",
193 static struct xtables_match udp_match = {
194 .family = NFPROTO_UNSPEC,
196 .version = XTABLES_VERSION,
197 .size = XT_ALIGN(sizeof(struct xt_udp)),
198 .userspacesize = XT_ALIGN(sizeof(struct xt_udp)),
204 .extra_opts = udp_opts,
210 xtables_register_match(&udp_match);