aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLutz Donnerhacke <donner@FreeBSD.org>2021-05-07 18:59:34 +0000
committerLutz Donnerhacke <donner@FreeBSD.org>2021-05-18 06:10:00 +0000
commit7200fdb9da3ac04ef8e577c947969a0ba8d69128 (patch)
treeafaf4d9f501a356edd9c188397fd9acb3175d88d
parent692c271beb8a14a5a142faf4a1d3f8832347b041 (diff)
downloadsrc-7200fdb9da3ac04ef8e577c947969a0ba8d69128.tar.gz
src-7200fdb9da3ac04ef8e577c947969a0ba8d69128.zip
sbin/ipfw: Fix parsing error in table based forward
The argument parser does not recognise the optional port for an "tablearg" argument. Fix simplifies the code by make the internal representation expicit for the parser. Includes the fix from D30208. PR: 252744 Reported by: <bugs.freebsd.org@mx.zzux.com> Approved by: nc Tested by: <bugs.freebsd.org@mx.zzux.com> Differential Revision: https://reviews.freebsd.org/D30164 (cherry picked from commit 6cb13813caa09305046e0cecad8bba3ae2287b0d) (cherry picked from commit f6f297871d469daf808f78faead8f950a2c81e36)
-rw-r--r--sbin/ipfw/ipfw2.c88
1 files changed, 43 insertions, 45 deletions
diff --git a/sbin/ipfw/ipfw2.c b/sbin/ipfw/ipfw2.c
index c17fbbca7dfa..fb1d9a4a180b 100644
--- a/sbin/ipfw/ipfw2.c
+++ b/sbin/ipfw/ipfw2.c
@@ -4021,57 +4021,55 @@ chkarg:
NEED1("missing forward address[:port]");
- if (_substrcmp(*av, "tablearg") == 0) {
- family = PF_INET;
- ((struct sockaddr_in*)&result)->sin_addr.s_addr =
- INADDR_ANY;
- } else {
- /*
- * Are we an bracket-enclosed IPv6 address?
- */
- if (strchr(*av, '['))
- (*av)++;
+ if (strncmp(*av, "tablearg", 8) == 0 &&
+ ((*av)[8] == '\0' || (*av)[8] == ',' || (*av)[8] == ':'))
+ memcpy(++(*av), "0.0.0.0", 7);
- /*
- * locate the address-port separator (':' or ',')
- */
- s = strchr(*av, ',');
- if (s == NULL) {
- s = strchr(*av, ']');
- /* Prevent erroneous parsing on brackets. */
- if (s != NULL)
- *(s++) = '\0';
- else
- s = *av;
-
- /* Distinguish between IPv4:port and IPv6 cases. */
- s = strchr(s, ':');
- if (s && strchr(s+1, ':'))
- s = NULL; /* no port */
- }
+ /*
+ * Are we an bracket-enclosed IPv6 address?
+ */
+ if (strchr(*av, '['))
+ (*av)++;
- if (s != NULL) {
- /* Terminate host portion and set s to start of port. */
+ /*
+ * locate the address-port separator (':' or ',')
+ */
+ s = strchr(*av, ',');
+ if (s == NULL) {
+ s = strchr(*av, ']');
+ /* Prevent erroneous parsing on brackets. */
+ if (s != NULL)
*(s++) = '\0';
- i = strtoport(s, &end, 0 /* base */, 0 /* proto */);
- if (s == end)
- errx(EX_DATAERR,
- "illegal forwarding port ``%s''", s);
- port_number = (u_short)i;
- }
+ else
+ s = *av;
- /*
- * Resolve the host name or address to a family and a
- * network representation of the address.
- */
- if (getaddrinfo(*av, NULL, NULL, &res))
- errx(EX_DATAERR, NULL);
- /* Just use the first host in the answer. */
- family = res->ai_family;
- memcpy(&result, res->ai_addr, res->ai_addrlen);
- freeaddrinfo(res);
+ /* Distinguish between IPv4:port and IPv6 cases. */
+ s = strchr(s, ':');
+ if (s && strchr(s+1, ':'))
+ s = NULL; /* no port */
}
+ if (s != NULL) {
+ /* Terminate host portion and set s to start of port. */
+ *(s++) = '\0';
+ i = strtoport(s, &end, 0 /* base */, 0 /* proto */);
+ if (s == end)
+ errx(EX_DATAERR,
+ "illegal forwarding port ``%s''", s);
+ port_number = (u_short)i;
+ }
+
+ /*
+ * Resolve the host name or address to a family and a
+ * network representation of the address.
+ */
+ if (getaddrinfo(*av, NULL, NULL, &res))
+ errx(EX_DATAERR, NULL);
+ /* Just use the first host in the answer. */
+ family = res->ai_family;
+ memcpy(&result, res->ai_addr, res->ai_addrlen);
+ freeaddrinfo(res);
+
if (family == PF_INET) {
ipfw_insn_sa *p = (ipfw_insn_sa *)action;