From patchwork Thu Aug 13 16:27:35 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Haley X-Patchwork-Id: 31340 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@bilbo.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ozlabs.org (ozlabs.org [203.10.76.45]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "mx.ozlabs.org", Issuer "CA Cert Signing Authority" (verified OK)) by bilbo.ozlabs.org (Postfix) with ESMTPS id A761BB6F1F for ; Fri, 14 Aug 2009 02:28:08 +1000 (EST) Received: by ozlabs.org (Postfix) id 9B2F7DDD0B; Fri, 14 Aug 2009 02:28:08 +1000 (EST) Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by ozlabs.org (Postfix) with ESMTP id 20C67DDD01 for ; Fri, 14 Aug 2009 02:28:08 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753514AbZHMQ1s (ORCPT ); Thu, 13 Aug 2009 12:27:48 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752124AbZHMQ1r (ORCPT ); Thu, 13 Aug 2009 12:27:47 -0400 Received: from g1t0028.austin.hp.com ([15.216.28.35]:22873 "EHLO g1t0028.austin.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751569AbZHMQ1r (ORCPT ); Thu, 13 Aug 2009 12:27:47 -0400 Received: from g5t0030.atlanta.hp.com (g5t0030.atlanta.hp.com [16.228.8.142]) by g1t0028.austin.hp.com (Postfix) with ESMTP id C094B1C4B8; Thu, 13 Aug 2009 16:27:48 +0000 (UTC) Received: from [16.1.1.100] (squirrel.fc.hp.com [15.11.146.57]) by g5t0030.atlanta.hp.com (Postfix) with ESMTP id CA61924271; Thu, 13 Aug 2009 16:27:37 +0000 (UTC) Message-ID: <4A843EF7.4010700@hp.com> Date: Thu, 13 Aug 2009 12:27:35 -0400 From: Brian Haley Organization: Open Source and Linux Organization User-Agent: Thunderbird 2.0.0.22 (X11/20090608) MIME-Version: 1.0 To: Jens Rosenboom CC: Linux Network Developers Subject: Re: [RFC] ipv6: Change %pI6 format to output compacted addresses? References: <1250091560.6641.48.camel@fnki-nb00130> <4A836D6D.1040400@hp.com> <1250174390.6641.89.camel@fnki-nb00130> In-Reply-To: <1250174390.6641.89.camel@fnki-nb00130> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Jens Rosenboom wrote: >> Anyways, can you try this patch, it's less than 40 new lines :) >> It might be good enough, but could probably use some help. > > For a start, it didn't even compile. ;-) It did on net-next-2.6 last night, weird. > Here is a new version that also > fixes > > - Leave %pi6 alone > - Don't compress a single :0: > - Do output 0 > > The results and also the remaining issues can be seen with the attached > test program, that also exposes a bug in glibc for v4-mapped addresses > from 0/16. > > To fully conform to the cited draft, we would still have to implement > v4-mapped and also check whether a second run of zeros would be longer > than the first one, although the draft also suggests that operators > should avoid using this kind of addresses, so maybe this second issue > can be neglected. Yes, the "compress the most zeros" would be harder, and require two passes over the address. I had to cut corners somewhere :) And regarding v4-mapped, the easy fix to that is just detect it and call the IPv4 routine. The attached patch does that, but without the ::ffff: > diff --git a/lib/vsprintf.c b/lib/vsprintf.c > index 756ccaf..5710c65 100644 > --- a/lib/vsprintf.c > +++ b/lib/vsprintf.c > @@ -652,13 +652,53 @@ static char *ip6_addr_string(char *buf, char *end, > u8 *addr, > { > char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing > zero */ > char *p = ip6_addr; > - int i; > + int i, needcolon = 0, printhi; > + u16 *addr16 = (u16 *)addr; > + enum { DC_START, DC_MIDDLE, DC_DONE } colon = DC_START; > + > + /* omit leading zeros and shorten using "::" */ > > - for (i = 0; i < 8; i++) { > - p = pack_hex_byte(p, addr[2 * i]); > - p = pack_hex_byte(p, addr[2 * i + 1]); > - if (!(spec.flags & SPECIAL) && i != 7) > + if (!(spec.flags & SPECIAL)) { > + for (i = 0; i < 8; i++) { > + if (addr16[i] == 0 && addr16[i+1] == 0 && colon == DC_START) { This will access the array out-of-bounds when i=7. Another hack below. -Brian --- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 756ccaf..ba70f2a 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -647,25 +647,6 @@ static char *mac_address_string(char *buf, char *end, u8 *addr, return string(buf, end, mac_addr, spec); } -static char *ip6_addr_string(char *buf, char *end, u8 *addr, - struct printf_spec spec) -{ - char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */ - char *p = ip6_addr; - int i; - - for (i = 0; i < 8; i++) { - p = pack_hex_byte(p, addr[2 * i]); - p = pack_hex_byte(p, addr[2 * i + 1]); - if (!(spec.flags & SPECIAL) && i != 7) - *p++ = ':'; - } - *p = '\0'; - spec.flags &= ~SPECIAL; - - return string(buf, end, ip6_addr, spec); -} - static char *ip4_addr_string(char *buf, char *end, u8 *addr, struct printf_spec spec) { @@ -688,6 +669,73 @@ static char *ip4_addr_string(char *buf, char *end, u8 *addr, return string(buf, end, ip4_addr, spec); } +static char *ip6_addr_string(char *buf, char *end, u8 *addr, + struct printf_spec spec) +{ + char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */ + char *p = ip6_addr; + int i, needcolon, printhi; + u16 *addr16 = (u16 *)addr; + u32 *addr32 = (u32 *)addr; + enum { DC_START, DC_MIDDLE, DC_DONE } colon = DC_START; + + if (!(spec.flags & SPECIAL)) { + /* omit leading zeros and shorten using "::" */ + + /* v4mapped */ + if ((addr32[0] | addr32[1] | + (addr32[2] ^ htonl(0x0000ffff))) == 0) + return ip4_addr_string(buf, end, &addr[12], spec); + + needcolon = 0; + for (i = 0; i < 8; i++) { + if (addr16[i] == 0 && i < 7 && addr16[i+1] == 0 && + colon == DC_START) { + colon = DC_MIDDLE; + continue; + } + if (colon == DC_MIDDLE) { + if (addr16[i] == 0) + continue; + colon = DC_DONE; + *p++ = ':'; + *p++ = ':'; + } else if (needcolon) + *p++ = ':'; + printhi = 0; + if (addr[2 * i]) { + if (addr[2 * i] > 0x0f) + p = pack_hex_byte(p, addr[2 * i]); + else + *p++ = hex_asc_lo(addr[2 * i]); + printhi++; + } + /* + * If we printed the high-order bits we must print the + * low-order ones, even if they're all zeros. + */ + if (printhi || addr[2 * i + 1] > 0x0f) + p = pack_hex_byte(p, addr[2 * i + 1]); + else + *p++ = hex_asc_lo(addr[2 * i + 1]); + needcolon++; + } + if (colon == DC_MIDDLE) { + *p++ = ':'; + *p++ = ':'; + } + } else { + for (i = 0; i < 8; i++) { + p = pack_hex_byte(p, addr[2 * i]); + p = pack_hex_byte(p, addr[2 * i + 1]); + } + } + *p = '\0'; + spec.flags &= ~SPECIAL; + + return string(buf, end, ip6_addr, spec); +} + /* * Show a '%p' thing. A kernel extension is that the '%p' is followed * by an extra set of alphanumeric characters that are extended format