From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: alexander@alemayhu.com
Subject: [PATCH iptables 2/2] libxtables: pass AI_ADDRCONFIG flag to getaddrinfo()
Date: Wed, 8 Mar 2017 14:16:10 +0100 [thread overview]
Message-ID: <1488978970-30802-2-git-send-email-pablo@netfilter.org> (raw)
In-Reply-To: <1488978970-30802-1-git-send-email-pablo@netfilter.org>
According to man getaddrinfo(3):
If hints.ai_flags includes the AI_ADDRCONFIG flag, then IPv4 addresses
are returned in the list pointed to by res only if the local system has
at least one IPv4 address configured, and IPv6 addresses are only
returned if the local system has at least one IPv6 address configured.
The loopback address is not considered for this case as valid as a
configured address.
This patch removes AI_CANONNAME since we don't need the ->ai_canonname
field set in this code.
hints.ai_family has been changed to AF_UNSPEC otherwise the
AI_ADDRCONFIG flag is ignored.
Originally reported as a problem for iptables-translate, but this also
affects iptables and ip6tables.
$ iptables-translate -A INPUT -s localhost -j ACCEPT
gives duplicated rules:
nft add rule ip filter INPUT ip saddr 127.0.0.1 counter accept
nft add rule ip filter INPUT ip saddr 127.0.0.1 counter accept
Reported-by: Alexander Alemayhu <alexander@alemayhu.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
libxtables/xtables.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index d43f97066ea9..aa0b1eb71c0c 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -1367,21 +1367,29 @@ static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
unsigned int i;
memset(&hints, 0, sizeof(hints));
- hints.ai_flags = AI_CANONNAME;
- hints.ai_family = AF_INET;
+ hints.ai_flags = AI_ADDRCONFIG;
+ hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_RAW;
*naddr = 0;
if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
return NULL;
} else {
- for (p = res; p != NULL; p = p->ai_next)
+ for (p = res; p != NULL; p = p->ai_next) {
+ if (p->ai_family != AF_INET)
+ continue;
+
++*naddr;
+ }
addr = xtables_calloc(*naddr, sizeof(struct in_addr));
- for (i = 0, p = res; p != NULL; p = p->ai_next)
+ for (i = 0, p = res; p != NULL; p = p->ai_next) {
+ if (p->ai_family != AF_INET)
+ continue;
+
memcpy(&addr[i++],
&((const struct sockaddr_in *)p->ai_addr)->sin_addr,
sizeof(struct in_addr));
+ }
freeaddrinfo(res);
return addr;
}
@@ -1657,8 +1665,8 @@ host_to_ip6addr(const char *name, unsigned int *naddr)
unsigned int i;
memset(&hints, 0, sizeof(hints));
- hints.ai_flags = AI_CANONNAME;
- hints.ai_family = AF_INET6;
+ hints.ai_flags = AI_ADDRCONFIG;
+ hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_RAW;
*naddr = 0;
@@ -1666,14 +1674,22 @@ host_to_ip6addr(const char *name, unsigned int *naddr)
return NULL;
} else {
/* Find length of address chain */
- for (p = res; p != NULL; p = p->ai_next)
+ for (p = res; p != NULL; p = p->ai_next) {
+ if (p->ai_family != AF_INET6)
+ continue;
+
++*naddr;
+ }
/* Copy each element of the address chain */
addr = xtables_calloc(*naddr, sizeof(struct in6_addr));
- for (i = 0, p = res; p != NULL; p = p->ai_next)
+ for (i = 0, p = res; p != NULL; p = p->ai_next) {
+ if (p->ai_family != AF_INET6)
+ continue;
+
memcpy(&addr[i++],
&((const struct sockaddr_in6 *)p->ai_addr)->sin6_addr,
sizeof(struct in6_addr));
+ }
freeaddrinfo(res);
return addr;
}
--
2.1.4
next prev parent reply other threads:[~2017-03-08 13:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-08 13:16 [PATCH iptables 1/2] iptables-translate: print nft command for each expand rules via dns names Pablo Neira Ayuso
2017-03-08 13:16 ` Pablo Neira Ayuso [this message]
2017-03-08 13:56 ` [PATCH iptables 2/2] libxtables: pass AI_ADDRCONFIG flag to getaddrinfo() Jan Engelhardt
2017-03-08 14:00 ` Pablo Neira Ayuso
2017-03-09 7:23 ` [PATCH iptables 1/2] iptables-translate: print nft command for each expand rules via dns names Alexander Alemayhu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1488978970-30802-2-git-send-email-pablo@netfilter.org \
--to=pablo@netfilter.org \
--cc=alexander@alemayhu.com \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).