From: Serhey Popovych <serhe.popovych@gmail.com>
To: netdev@vger.kernel.org
Subject: [PATCH iproute2-next v2 1/8] utils: Introduce get_addr_rta() and inet_addr_match_rta()
Date: Tue, 23 Jan 2018 21:19:23 +0200 [thread overview]
Message-ID: <1516735170-20921-2-git-send-email-serhe.popovych@gmail.com> (raw)
In-Reply-To: <1516735170-20921-1-git-send-email-serhe.popovych@gmail.com>
First is used to get address from netlink attribute to
inet_prefix data structure. Use memcpy() with constant
value to let complier optimize by replacing a call by
inlining load/store instructions.
Second is used to match address in given netlink attribute
with one given as reference. It matches successfully if
no attribute is given (@rta is NULL), reference address
family is AF_UNSPEC or it's length isn't given; fails if
get_attr_rta() can't get attribute or it's family does
not match reference; calls inet_addr_match() to get final
verdict.
Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
---
include/utils.h | 2 ++
lib/utils.c | 95 ++++++++++++++++++++++++++++++++++++++++++++-----------
2 files changed, 78 insertions(+), 19 deletions(-)
diff --git a/include/utils.h b/include/utils.h
index f562547..0394268 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -120,6 +120,7 @@ int get_prefix_1(inet_prefix *dst, char *arg, int family);
int get_addr(inet_prefix *dst, const char *arg, int family);
int get_prefix(inet_prefix *dst, char *arg, int family);
int mask2bits(__u32 netmask);
+int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family);
int get_addr_ila(__u64 *val, const char *arg);
int read_prop(const char *dev, char *prop, long *value);
@@ -174,6 +175,7 @@ int check_ifname(const char *);
int get_ifname(char *, const char *);
int matches(const char *arg, const char *pattern);
int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
+int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta);
const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
int dnet_pton(int af, const char *src, void *addr);
diff --git a/lib/utils.c b/lib/utils.c
index e20b60e..8e15625 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -534,6 +534,28 @@ int get_addr64(__u64 *ap, const char *cp)
return 1;
}
+static void set_address_type(inet_prefix *addr)
+{
+ switch (addr->family) {
+ case AF_INET:
+ if (!addr->data[0])
+ addr->flags |= ADDRTYPE_INET_UNSPEC;
+ else if (IN_MULTICAST(ntohl(addr->data[0])))
+ addr->flags |= ADDRTYPE_INET_MULTI;
+ else
+ addr->flags |= ADDRTYPE_INET;
+ break;
+ case AF_INET6:
+ if (IN6_IS_ADDR_UNSPECIFIED(addr->data))
+ addr->flags |= ADDRTYPE_INET_UNSPEC;
+ else if (IN6_IS_ADDR_MULTICAST(addr->data))
+ addr->flags |= ADDRTYPE_INET_MULTI;
+ else
+ addr->flags |= ADDRTYPE_INET;
+ break;
+ }
+}
+
static int __get_addr_1(inet_prefix *addr, const char *name, int family)
{
memset(addr, 0, sizeof(*addr));
@@ -627,25 +649,7 @@ int get_addr_1(inet_prefix *addr, const char *name, int family)
if (ret)
return ret;
- switch (addr->family) {
- case AF_INET:
- if (!addr->data[0])
- addr->flags |= ADDRTYPE_INET_UNSPEC;
- else if (IN_MULTICAST(ntohl(addr->data[0])))
- addr->flags |= ADDRTYPE_INET_MULTI;
- else
- addr->flags |= ADDRTYPE_INET;
- break;
- case AF_INET6:
- if (IN6_IS_ADDR_UNSPECIFIED(addr->data))
- addr->flags |= ADDRTYPE_INET_UNSPEC;
- else if (IN6_IS_ADDR_MULTICAST(addr->data))
- addr->flags |= ADDRTYPE_INET_MULTI;
- else
- addr->flags |= ADDRTYPE_INET;
- break;
- }
-
+ set_address_type(addr);
return 0;
}
@@ -734,6 +738,46 @@ int get_addr(inet_prefix *dst, const char *arg, int family)
return 0;
}
+int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family)
+{
+ const int len = RTA_PAYLOAD(rta);
+ const void *data = RTA_DATA(rta);
+
+ switch (len) {
+ case 4:
+ dst->family = AF_INET;
+ dst->bytelen = 4;
+ memcpy(dst->data, data, 4);
+ break;
+ case 16:
+ dst->family = AF_INET6;
+ dst->bytelen = 16;
+ memcpy(dst->data, data, 16);
+ break;
+ case 2:
+ dst->family = AF_DECnet;
+ dst->bytelen = 2;
+ memcpy(dst->data, data, 2);
+ break;
+ case 10:
+ dst->family = AF_IPX;
+ dst->bytelen = 10;
+ memcpy(dst->data, data, 10);
+ break;
+ default:
+ return -1;
+ }
+
+ if (family != AF_UNSPEC && family != dst->family)
+ return -2;
+
+ dst->bitlen = -1;
+ dst->flags = 0;
+
+ set_address_type(dst);
+ return 0;
+}
+
int get_prefix(inet_prefix *dst, char *arg, int family)
{
if (family == AF_PACKET) {
@@ -864,6 +908,19 @@ int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
return 0;
}
+int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta)
+{
+ inet_prefix dst;
+
+ if (!rta || m->family == AF_UNSPEC || m->bitlen <= 0)
+ return 0;
+
+ if (get_addr_rta(&dst, rta, m->family))
+ return -1;
+
+ return inet_addr_match(&dst, m, m->bitlen);
+}
+
int __iproute2_hz_internal;
int __get_hz(void)
--
1.7.10.4
next prev parent reply other threads:[~2018-01-23 19:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-23 19:19 [PATCH iproute2-next v2 0/8] ip: Introduce and use get_addr_rta()/inet_addr_match_rta() Serhey Popovych
2018-01-23 19:19 ` Serhey Popovych [this message]
2018-01-23 19:19 ` [PATCH iproute2-next v2 2/8] ipaddress: Use inet_addr_match_rta() Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 3/8] iprule: " Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 4/8] ipmroute: " Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 5/8] ipneigh: " Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 6/8] ipl2tp: Use get_addr_rta() Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 7/8] tcp_metric: " Serhey Popovych
2018-01-23 19:19 ` [PATCH iproute2-next v2 8/8] ip/tunnel: Unify local/remote endpoint address printing Serhey Popovych
2018-01-24 18:40 ` David Ahern
2018-01-24 3:45 ` [PATCH iproute2-next v2 0/8] ip: Introduce and use get_addr_rta()/inet_addr_match_rta() David Ahern
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=1516735170-20921-2-git-send-email-serhe.popovych@gmail.com \
--to=serhe.popovych@gmail.com \
--cc=netdev@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).