1 /* Shared library add-on to iptables to add UDP support. */
8 #include <linux/netfilter_ipv4/ip_tables.h>
10 /* Function which prints out usage message. */
16 " --source-port [!] port[:port]\n"
18 " match source port(s)\n"
19 " --destination-port [!] port[:port]\n"
21 " match destination port(s)\n",
25 static struct option opts[] = {
26 { "source-port", 1, 0, '1' },
27 { "sport", 1, 0, '1' }, /* synonym */
28 { "destination-port", 1, 0, '2' },
29 { "dport", 1, 0, '2' }, /* synonym */
34 service_to_port(const char *name)
36 struct servent *service;
38 if ((service = getservbyname(name, "udp")) != NULL)
39 return ntohs((unsigned short) service->s_port);
45 parse_udp_port(const char *port)
49 if (string_to_number(port, 0, 65535, &portnum) != -1 ||
50 (portnum = service_to_port(port)) != -1)
51 return (u_int16_t)portnum;
53 exit_error(PARAMETER_PROBLEM,
54 "invalid UDP port/service `%s' specified", port);
58 parse_udp_ports(const char *portstring, u_int16_t *ports)
63 buffer = strdup(portstring);
64 if ((cp = strchr(buffer, ':')) == NULL)
65 ports[0] = ports[1] = parse_udp_port(buffer);
70 ports[0] = buffer[0] ? parse_udp_port(buffer) : 0;
71 ports[1] = cp[0] ? parse_udp_port(cp) : 0xFFFF;
73 if (ports[0] > ports[1])
74 exit_error(PARAMETER_PROBLEM,
75 "invalid portrange (min > max)");
80 /* Initialize the match. */
82 init(struct ipt_entry_match *m, unsigned int *nfcache)
84 struct ipt_udp *udpinfo = (struct ipt_udp *)m->data;
86 udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF;
89 #define UDP_SRC_PORTS 0x01
90 #define UDP_DST_PORTS 0x02
92 /* Function which parses command options; returns true if it
95 parse(int c, char **argv, int invert, unsigned int *flags,
96 const struct ipt_entry *entry,
97 unsigned int *nfcache,
98 struct ipt_entry_match **match)
100 struct ipt_udp *udpinfo = (struct ipt_udp *)(*match)->data;
104 if (*flags & UDP_SRC_PORTS)
105 exit_error(PARAMETER_PROBLEM,
106 "Only one `--source-port' allowed");
107 check_inverse(optarg, &invert, &optind, 0);
108 parse_udp_ports(argv[optind-1], udpinfo->spts);
110 udpinfo->invflags |= IPT_UDP_INV_SRCPT;
111 *flags |= UDP_SRC_PORTS;
115 if (*flags & UDP_DST_PORTS)
116 exit_error(PARAMETER_PROBLEM,
117 "Only one `--destination-port' allowed");
118 check_inverse(optarg, &invert, &optind, 0);
119 parse_udp_ports(argv[optind-1], udpinfo->dpts);
121 udpinfo->invflags |= IPT_UDP_INV_DSTPT;
122 *flags |= UDP_DST_PORTS;
132 /* Final check; we don't care. */
134 final_check(unsigned int flags)
139 port_to_service(int port)
141 struct servent *service;
143 if ((service = getservbyport(htons(port), "udp")))
144 return service->s_name;
150 print_port(u_int16_t port, int numeric)
154 if (numeric || (service = port_to_service(port)) == NULL)
157 printf("%s", service);
161 print_ports(const char *name, u_int16_t min, u_int16_t max,
162 int invert, int numeric)
164 const char *inv = invert ? "!" : "";
166 if (min != 0 || max != 0xFFFF || invert) {
170 print_port(min, numeric);
173 print_port(min, numeric);
175 print_port(max, numeric);
181 /* Prints out the union ipt_matchinfo. */
183 print(const struct ipt_ip *ip,
184 const struct ipt_entry_match *match, int numeric)
186 const struct ipt_udp *udp = (struct ipt_udp *)match->data;
189 print_ports("spt", udp->spts[0], udp->spts[1],
190 udp->invflags & IPT_UDP_INV_SRCPT,
192 print_ports("dpt", udp->dpts[0], udp->dpts[1],
193 udp->invflags & IPT_UDP_INV_DSTPT,
195 if (udp->invflags & ~IPT_UDP_INV_MASK)
196 printf("Unknown invflags: 0x%X ",
197 udp->invflags & ~IPT_UDP_INV_MASK);
200 /* Saves the union ipt_matchinfo in parsable form to stdout. */
201 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
203 const struct ipt_udp *udpinfo = (struct ipt_udp *)match->data;
205 if (udpinfo->spts[0] != 0
206 || udpinfo->spts[1] != 0xFFFF) {
207 if (udpinfo->invflags & IPT_UDP_INV_SRCPT)
211 printf("--sport %u:%u ",
215 printf("--sport %u ",
219 if (udpinfo->dpts[0] != 0
220 || udpinfo->dpts[1] != 0xFFFF) {
221 if (udpinfo->invflags & IPT_UDP_INV_DSTPT)
225 printf("--dport %u:%u ",
229 printf("--dport %u ",
235 struct iptables_match udp = {
238 .version = IPTABLES_VERSION,
239 .size = IPT_ALIGN(sizeof(struct ipt_udp)),
240 .userspacesize = IPT_ALIGN(sizeof(struct ipt_udp)),
244 .final_check = &final_check,
253 register_match(&udp);