Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / extensions / libip6t_udp.c
blobcd3c3d4568d01670cf9ca6a75ba89310eff99c13
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 <ip6tables.h>
8 #include <linux/netfilter_ipv6/ip6_tables.h>
10 /* Function which prints out usage message. */
11 static void
12 help(void)
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",
22 IPTABLES_VERSION);
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 */
30 {0}
33 static void
34 parse_udp_ports(const char *portstring, u_int16_t *ports)
36 char *buffer;
37 char *cp;
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++;
46 ports[0] = buffer[0] ? parse_port(buffer, "udp") : 0;
47 ports[1] = cp[0] ? parse_port(cp, "udp") : 0xFFFF;
49 if (ports[0] > ports[1])
50 exit_error(PARAMETER_PROBLEM,
51 "invalid portrange (min > max)");
53 free(buffer);
56 /* Initialize the match. */
57 static void
58 init(struct ip6t_entry_match *m, unsigned int *nfcache)
60 struct ip6t_udp *udpinfo = (struct ip6t_udp *)m->data;
62 udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF;
65 #define UDP_SRC_PORTS 0x01
66 #define UDP_DST_PORTS 0x02
68 /* Function which parses command options; returns true if it
69 ate an option */
70 static int
71 parse(int c, char **argv, int invert, unsigned int *flags,
72 const struct ip6t_entry *entry,
73 unsigned int *nfcache,
74 struct ip6t_entry_match **match)
76 struct ip6t_udp *udpinfo = (struct ip6t_udp *)(*match)->data;
78 switch (c) {
79 case '1':
80 if (*flags & UDP_SRC_PORTS)
81 exit_error(PARAMETER_PROBLEM,
82 "Only one `--source-port' allowed");
83 check_inverse(optarg, &invert, &optind, 0);
84 parse_udp_ports(argv[optind-1], udpinfo->spts);
85 if (invert)
86 udpinfo->invflags |= IP6T_UDP_INV_SRCPT;
87 *flags |= UDP_SRC_PORTS;
88 break;
90 case '2':
91 if (*flags & UDP_DST_PORTS)
92 exit_error(PARAMETER_PROBLEM,
93 "Only one `--destination-port' allowed");
94 check_inverse(optarg, &invert, &optind, 0);
95 parse_udp_ports(argv[optind-1], udpinfo->dpts);
96 if (invert)
97 udpinfo->invflags |= IP6T_UDP_INV_DSTPT;
98 *flags |= UDP_DST_PORTS;
99 break;
101 default:
102 return 0;
105 return 1;
108 /* Final check; we don't care. */
109 static void
110 final_check(unsigned int flags)
114 static char *
115 port_to_service(int port)
117 struct servent *service;
119 if ((service = getservbyport(htons(port), "udp")))
120 return service->s_name;
122 return NULL;
125 static void
126 print_port(u_int16_t port, int numeric)
128 char *service;
130 if (numeric || (service = port_to_service(port)) == NULL)
131 printf("%u", port);
132 else
133 printf("%s", service);
136 static void
137 print_ports(const char *name, u_int16_t min, u_int16_t max,
138 int invert, int numeric)
140 const char *inv = invert ? "!" : "";
142 if (min != 0 || max != 0xFFFF || invert) {
143 printf("%s", name);
144 if (min == max) {
145 printf(":%s", inv);
146 print_port(min, numeric);
147 } else {
148 printf("s:%s", inv);
149 print_port(min, numeric);
150 printf(":");
151 print_port(max, numeric);
153 printf(" ");
157 /* Prints out the union ipt_matchinfo. */
158 static void
159 print(const struct ip6t_ip6 *ip,
160 const struct ip6t_entry_match *match, int numeric)
162 const struct ip6t_udp *udp = (struct ip6t_udp *)match->data;
164 printf("udp ");
165 print_ports("spt", udp->spts[0], udp->spts[1],
166 udp->invflags & IP6T_UDP_INV_SRCPT,
167 numeric);
168 print_ports("dpt", udp->dpts[0], udp->dpts[1],
169 udp->invflags & IP6T_UDP_INV_DSTPT,
170 numeric);
171 if (udp->invflags & ~IP6T_UDP_INV_MASK)
172 printf("Unknown invflags: 0x%X ",
173 udp->invflags & ~IP6T_UDP_INV_MASK);
176 /* Saves the union ipt_matchinfo in parsable form to stdout. */
177 static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
179 const struct ip6t_udp *udpinfo = (struct ip6t_udp *)match->data;
181 if (udpinfo->spts[0] != 0
182 || udpinfo->spts[1] != 0xFFFF) {
183 if (udpinfo->invflags & IP6T_UDP_INV_SRCPT)
184 printf("! ");
185 if (udpinfo->spts[0]
186 != udpinfo->spts[1])
187 printf("--sport %u:%u ",
188 udpinfo->spts[0],
189 udpinfo->spts[1]);
190 else
191 printf("--sport %u ",
192 udpinfo->spts[0]);
195 if (udpinfo->dpts[0] != 0
196 || udpinfo->dpts[1] != 0xFFFF) {
197 if (udpinfo->invflags & IP6T_UDP_INV_DSTPT)
198 printf("! ");
199 if (udpinfo->dpts[0]
200 != udpinfo->dpts[1])
201 printf("--dport %u:%u ",
202 udpinfo->dpts[0],
203 udpinfo->dpts[1]);
204 else
205 printf("--dport %u ",
206 udpinfo->dpts[0]);
210 static struct ip6tables_match udp = {
211 .name = "udp",
212 .version = IPTABLES_VERSION,
213 .size = IP6T_ALIGN(sizeof(struct ip6t_udp)),
214 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_udp)),
215 .help = &help,
216 .init = &init,
217 .parse = &parse,
218 .final_check = &final_check,
219 .print = &print,
220 .save = &save,
221 .extra_opts = opts,
224 void
225 _init(void)
227 register_match6(&udp);