[PATCH 1/2] rtnl_addr: decode ifaddrmsg netlink attributes

Dmitry V. Levin ldv at altlinux.org
Mon Aug 21 15:51:51 UTC 2017


On Sun, Aug 20, 2017 at 08:17:18PM +0800, JingPiao Chen wrote:
> * rtnl_addr.c: Include <arpa/inet.h> and <linux/netdevice.h>.
> (decode_ifa_address, decode_ifa_cacheinfo,
> decode_ifa_flags): New functions.
> (ifaddrmsg_nla_decoders): New array.
> (decode_ifaddrmsg): Use it.
> ---
>  rtnl_addr.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 93 insertions(+), 1 deletion(-)
> 
> diff --git a/rtnl_addr.c b/rtnl_addr.c
> index c7e37ea..c4bde06 100644
> --- a/rtnl_addr.c
> +++ b/rtnl_addr.c
> @@ -32,16 +32,106 @@
>  #include "nlattr.h"
>  #include "print_fields.h"
>  
> +#include <arpa/inet.h>
> +
>  #include "netlink.h"
>  #include <linux/rtnetlink.h>
>  #ifdef HAVE_LINUX_IF_ADDR_H
>  # include <linux/if_addr.h>
>  #endif
> +#include <linux/netdevice.h>
>  
>  #include "xlat/ifaddrflags.h"
>  #include "xlat/routing_scopes.h"
>  #include "xlat/rtnl_addr_attrs.h"
>  
> +static bool
> +decode_ifa_address(struct tcb *const tcp,
> +		   const kernel_ulong_t addr,
> +		   const unsigned int len,
> +		   const void *const opaque_data)
> +{
> +	const struct ifaddrmsg *const ifaddr = opaque_data;
> +	const unsigned int addr_len =
> +		len < MAX_ADDR_LEN ? len : MAX_ADDR_LEN;
> +	char buf[MAX_ADDR_LEN];
> +
> +	if (!umoven_or_printaddr(tcp, addr, addr_len, buf)) {
> +		switch (ifaddr->ifa_family) {
> +		case AF_INET: {
> +			char str[INET_ADDRSTRLEN];
> +
> +			if (addr_len < sizeof(struct in_addr)
> +			    || !inet_ntop(AF_INET, buf, str, sizeof(str)))
> +				return false;

Could we check whether len >= sizeof(struct in_addr)
before umoven_or_printaddr?  It's a good idea to avoid umoven if it's
known in advance that it's result will be ignored.

> +			tprints(str);
> +			break;
> +		}
> +		case AF_INET6: {
> +			char str[INET6_ADDRSTRLEN];
> +
> +			if (addr_len < sizeof(struct in6_addr)
> +			    || !inet_ntop(AF_INET6, buf, str, sizeof(str)))
> +				break;

Likewise, with len >= sizeof(struct in6_addr).

> +			tprints(str);
> +			break;
> +		}
> +		default:
> +			return false;
> +		}
> +	}
> +
> +	return true;
> +}

BTW, decode_ifa_address looks very similar to decode_ifla_address,
and both of them share ideas with print_inet_addr.  Could we reuse
the code somehow?  Maybe create a separate function, say decode_inet_addr,
and use it both in decode_ifla_address and decode_ifa_address?


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20170821/10787bc3/attachment.bin>


More information about the Strace-devel mailing list