From patchwork Mon Oct 12 07:43:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Packham X-Patchwork-Id: 528920 X-Patchwork-Delegate: joe.hershberger@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id DC8611402A1 for ; Mon, 12 Oct 2015 18:44:02 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=WIdIUEpy; dkim-atps=neutral Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id AE3A84B7D4; Mon, 12 Oct 2015 09:44:01 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hsq0hFi4Zndn; Mon, 12 Oct 2015 09:44:01 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5A2014B7B6; Mon, 12 Oct 2015 09:44:01 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A8FEC4B7C3 for ; Mon, 12 Oct 2015 09:43:57 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ox4BDzeHNUYI for ; Mon, 12 Oct 2015 09:43:57 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-pa0-f48.google.com (mail-pa0-f48.google.com [209.85.220.48]) by theia.denx.de (Postfix) with ESMTPS id 0A3844B7BD for ; Mon, 12 Oct 2015 09:43:52 +0200 (CEST) Received: by pabrc13 with SMTP id rc13so12266925pab.0 for ; Mon, 12 Oct 2015 00:43:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mXRHH14gNDCj+eS8IRUJaa/eHR2iyVB3YljTTb/DJtc=; b=WIdIUEpyUrZ7OqymUsRB19kMiq5rK8Sx4slRQP+Uz2kEgjHAjlHwCc1Mm89lmeGGx9 wU+F/tssEFvxrpEeQqiGYQon8I34OCHaOely56xCyDLNzCczkA1rg9bqCp1gJ6kXEqTX w3g+hzI5CmQ5w0jUcOSpHXOwJct+DxVV/6pkZ3yhLuHdTWONTCNItkcDYQF08BRlFHFg 3js4mXYZsH4AmvGbvCB7+3a1ZsfslLiREu+r+hTUwcLgjFiTyNExwMzZVTFStxHOPubC v5iTSBLCM19B34d9GcXjMvWR57jRIuRzKL8NC+p3so/4cDT6tpEZ+TC/wqUVZKxiNw9Q actA== X-Received: by 10.68.104.66 with SMTP id gc2mr32481613pbb.21.1444635831467; Mon, 12 Oct 2015 00:43:51 -0700 (PDT) Received: from chrisp-dl.ws.atlnz.lc (2-163-36-202-static.alliedtelesis.co.nz. [202.36.163.2]) by smtp.gmail.com with ESMTPSA id cn4sm16451115pbc.94.2015.10.12.00.43.47 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 12 Oct 2015 00:43:51 -0700 (PDT) From: Chris Packham To: u-boot@lists.denx.de, Joe Hershberger Date: Mon, 12 Oct 2015 20:43:08 +1300 Message-Id: <1444635794-16285-3-git-send-email-judge.packham@gmail.com> X-Mailer: git-send-email 2.5.3 In-Reply-To: <1444635794-16285-1-git-send-email-judge.packham@gmail.com> References: <1444635794-16285-1-git-send-email-judge.packham@gmail.com> Cc: Tom Rini , Angga , hannah@marvell.com, Masahiro Yamada , jp.tosoni@acksys.fr, Chris Packham Subject: [U-Boot] [RFC PATCH 2/8] lib: vsprintf: add IPv6 compressed format %pI6c X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Add support for "human friendly" IPv6 address representations as specified in http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00 This code has been adapted from Linux kernel with minimal modification. Signed-off-by: Chris Packham Acked-by: Joe Hershberger --- include/net6.h | 13 ++++++ lib/vsprintf.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 137 insertions(+), 20 deletions(-) diff --git a/include/net6.h b/include/net6.h index b622951..1b82c25 100644 --- a/include/net6.h +++ b/include/net6.h @@ -45,4 +45,17 @@ struct ip6_hdr { struct in6_addr daddr; }; +/* ::ffff:0:0/96 is reserved for v4 mapped addresses */ +static inline int ipv6_addr_v4mapped(const struct in6_addr *a) +{ + return (a->s6_addr32[0] | a->s6_addr32[1] | + (a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0; +} + +/* Intra-Site Automatic Tunnel Addressing Protocol Address */ +static inline int ipv6_addr_is_isatap(const struct in6_addr *a) +{ + return (a->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE); +} + #endif /* __NET6_H__ */ diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 4c82837..349234a 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -23,6 +23,7 @@ #endif #include +#include #define noinline __attribute__((noinline)) unsigned long simple_strtoul(const char *cp, char **endp, @@ -304,6 +305,7 @@ static noinline char *put_dec(char *buf, uint64_t num) #define LEFT 16 /* left justified */ #define SMALL 32 /* Must be 32 == 0x20 */ #define SPECIAL 64 /* 0x */ +#define COMPRESSED 128 /* use compressed format */ #ifdef CONFIG_SYS_VSNPRINTF /* @@ -469,12 +471,108 @@ static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width, flags & ~SPECIAL); } -static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, - int precision, int flags) +static char *ip4_string(char *p, u8 *addr) +{ + char temp[3]; /* hold each IP quad in reverse order */ + int i, digits; + + for (i = 0; i < 4; i++) { + digits = put_dec_trunc(temp, addr[i]) - temp; + /* reverse the digits in the quad */ + while (digits--) + *p++ = temp[digits]; + if (i != 3) + *p++ = '.'; + } + *p = '\0'; + + return p; +} + +static char *ip6_compressed_string(char *p, u8 *addr) +{ + int i, j, range; + unsigned char zerolength[8]; + int longest = 1; + int colonpos = -1; + u16 word; + u8 hi, lo; + int needcolon = 0; + int useIPv4; + struct in6_addr in6; + + memcpy(&in6, addr, sizeof(struct in6_addr)); + + useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); + + memset(zerolength, 0, sizeof(zerolength)); + + if (useIPv4) + range = 6; + else + range = 8; + + /* find position of longest 0 run */ + for (i = 0; i < range; i++) { + for (j = i; j < range; j++) { + if (in6.s6_addr16[j] != 0) + break; + zerolength[i]++; + } + } + for (i = 0; i < range; i++) { + if (zerolength[i] > longest) { + longest = zerolength[i]; + colonpos = i; + } + } + if (longest == 1) /* don't compress a single 0 */ + colonpos = -1; + + /* emit address */ + for (i = 0; i < range; i++) { + if (i == colonpos) { + if (needcolon || i == 0) + *p++ = ':'; + *p++ = ':'; + needcolon = 0; + i += longest - 1; + continue; + } + if (needcolon) { + *p++ = ':'; + needcolon = 0; + } + /* hex u16 without leading 0s */ + word = ntohs(in6.s6_addr16[i]); + hi = word >> 8; + lo = word & 0xff; + if (hi) { + if (hi > 0x0f) + p = pack_hex_byte(p, hi); + else + *p++ = hex_asc_lo(hi); + p = pack_hex_byte(p, lo); + } else if (lo > 0x0f) { + p = pack_hex_byte(p, lo); + } else { + *p++ = hex_asc_lo(lo); + } + needcolon = 1; + } + + if (useIPv4) { + if (needcolon) + *p++ = ':'; + p = ip4_string(p, &in6.s6_addr[12]); + } + *p = '\0'; + + return p; +} + +static char *ip6_string(char *p, u8 *addr, int flags) { - /* (8 * 4 hex digits), 7 colons and trailing zero */ - char ip6_addr[8 * 5]; - char *p = ip6_addr; int i; for (i = 0; i < 8; i++) { @@ -485,6 +583,19 @@ static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, } *p = '\0'; + return p; +} + +static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, + int precision, int flags) +{ + char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; + + if (flags & COMPRESSED) + ip6_compressed_string(ip6_addr, addr); + else + ip6_string(ip6_addr, addr, flags); + return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL); } @@ -492,21 +603,9 @@ static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, int precision, int flags) { - /* (4 * 3 decimal digits), 3 dots and trailing zero */ - char ip4_addr[4 * 4]; - char temp[3]; /* hold each IP quad in reverse order */ - char *p = ip4_addr; - int i, digits; + char ip4_addr[sizeof("255.255.255.255")]; - for (i = 0; i < 4; i++) { - digits = put_dec_trunc(temp, addr[i]) - temp; - /* reverse the digits in the quad */ - while (digits--) - *p++ = temp[digits]; - if (i != 3) - *p++ = '.'; - } - *p = '\0'; + ip4_string(ip4_addr, addr); return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL); @@ -526,6 +625,8 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, * decimal for v4 and colon separated network-order 16 bit hex for v6) * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is * currently the same + * - 'I6c' for IPv6 addresses printed as specified by + * http://tools.ietf.org/html/rfc5952 * * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 * function pointers are really function descriptors, which contain a @@ -569,9 +670,12 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, flags |= SPECIAL; /* Fallthrough */ case 'I': - if (fmt[1] == '6') + if (fmt[1] == '6') { + if (fmt[2] == 'c') + flags |= COMPRESSED; return ip6_addr_string(buf, end, ptr, field_width, precision, flags); + } if (fmt[1] == '4') return ip4_addr_string(buf, end, ptr, field_width, precision, flags);