vsprintf: do not append unset Scope ID to IPv6
Subsystems (3)
PATCH 1/1 vsprintf: do not append unset Scope ID to IPv6
The sockaddr_in6 has the sin6_scope_id parameter. This contains the netdev index of the output device. When set to 0, sin6_scope_id is considered to be "unset" -- it has no Scope ID (see RFC4007). When it is set to >0, it has a Scope ID.
Other parts of the kernel respect htis. For example, code like this is sometimes used to populate a sockaddr_in6 from a skb:
sin6->sin6_scope_id = ipv6_iface_scope_id(ipv6_hdr(skb)->saddr, skb->skb_iif);
Such a code example makes a call to the ipv6_iface_scope_id function which is defined as follows (in ipv6.h):
static inline __u32 ipv6_iface_scope_id(const struct in6_addr *addr, int iface)
{
return __ipv6_addr_needs_scope_id(__ipv6_addr_type(addr)) ? iface : 0;
}So, here, either it has Scope ID, in which case it's returned, or it doesn't need a Scope ID, in which case 0 - "no scope id" - is returned.
Therefore, vsprintf should treat the 0 Scope ID the same way the rest of the v6 stack and the RFC treats it: 0 Scope ID means no Scope ID. And if there is no Scope ID, there is nothing to be printed.
So, this patch only prints the Scope ID when one exists, and does not print a Scope ID when one does not exist.
It turns out, too, that this is what developers want. The most common purpose of %pISpfsc is to show "what is in that sockaddr so that I can have some idea of how to connect to it?"
--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +1 -1
@@ -1194,7 +1194,7 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, p =number (p, pend,ntohl (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ), spec); } -if (have_s) { +if (have_s && sa->sin6_scope_id) { *p++ ='%' ; p =number (p, pend, sa->sin6_scope_id, spec); }
Signature · 1 line
2.7.0
PATCH 1/1 vsprintf: flowinfo in IPv6 is optional too
--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/vsprintf.c +1 -1
@@ -1189,7 +1189,7 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, *p++ =':' ; p =number (p, pend,ntohs (sa->sin6_port), spec); } -if (have_f) { +if (have_f && (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK )) { *p++ ='/' ; p =number (p, pend,ntohl (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ), spec);
Signature · 1 line
2.7.0
IRe: [PATCH] vsprintf: flowinfo in IPv6 is optional too
Quoted 16 lines · Jason A. Donenfeld
Signed-off-by Jason A. Donenfeld <[email protected]>---
lib/vsprintf.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +1
-1@@ -1189,7 +1189,7 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, *p++ = ':'; p = number(p, pend, ntohs(sa->sin6_port), spec); } -if (have_f) { +if (have_f && (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK )) { *p++ = '/';
Quoted 2 lines
p =
number(p, pend, ntohl(sa->sin6_flowinfo & IPV6_FLOWINFO_MASK), spec);
Why does this matter at all?
The format string "%pIS[...]f" is not used currently in the kernel.
If one were to call out this 'f' qualifier to %pIS, wouldn't it be better to show /0 than elide the / output completely?
Re: IRe: [PATCH] vsprintf: flowinfo in IPv6 is optional too
Quoted 1 line · Joe Perches
Why does this matter at all?
Because I want to printk a sockaddr_in6, and have the most complete yet valid and standard representation I can. Displaying a /0 when there is no flow is confusing, ugly, and unhelpful, not to mention it doesn't accurately reflect what's happening. A flowlabel of zero means no flowlabel.
Andy Shevchenko
reviewerRe: [PATCH] vsprintf: flowinfo in IPv6 is optional too
Quoted 9 lines · Jason A. Donenfeld
Quoted 1 line · Joe Perches
Why does this matter at all?
Because I want to printk a
sockaddr_in6, and have the most complete yet valid and standard representation I can. Displaying a /0 when there is no flow is confusing, ugly, and unhelpful, not to mention it doesn't accurately reflect what's happening. A flowlabel of zero means
no flowlabel.
So, where do you want to put your result? dmesg? User-space application via sysfs?
Seems like here is discussion debug vs. release version of printed data in the kernel.
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Re: IRe: [PATCH] vsprintf: flowinfo in IPv6 is optional too
In reply to Joe PerchesQuoted 22 lines · Joe Perches
Quoted 10 lines · Jason A. Donenfeld
Signed-off-by Jason A. Donenfeld <[email protected]>--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +0
-0lib/vsprintf.c char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, · line 1195
Quoted 8 lines
lib/vsprintf.c +1
-1@@ -1189,7 +1189,7 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, *p++ =':' ; p =number (p, pend,ntohs (sa->sin6_port), spec); } -if (have_f) { +if (have_f && (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK )) { *p++ ='/' ; p =number (p, pend,ntohl (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ), spec);Why does this matter at all?
Quoted 4 lines
The format string "%pIS[...]f" is not used currently in the kernel.
If one were to call out this 'f' qualifier to %pIS, wouldn't it be better to show /0 than elide the / output completely?
+1
Another possibility also in regards to your other patch would be to have a different format string char option instead of 'f'/'s' that would then allow a developer for having both options to choose from. Dunno if it's really worth it, but if you have a use case that definitely needs it, then it'd be probably better. Less surprises during debugging, at least.
Re: IRe: [PATCH] vsprintf: flowinfo in IPv6 is optional too
Quoted 5 lines · Daniel Borkmann
Another possibility also in regards to your other patch would be to have a different format string char option instead of 'f'/'s' that would then allow a developer for having both options to choose from. Dunno if it's really worth it, but if you have a use case that definitely needs it, then it'd be probably better. Less surprises during debugging, at least.
Just came to this too over in the other patch thread. I'll cook this up now.
Quoted 4 lines · Jason A. Donenfeld
The
sockaddr_in6has thesin6_scope_idparameter. This contains the netdev index of the output device. When set to 0,sin6_scope_idis considered to be "unset" -- it has no Scope ID (see RFC4007). When it is set to >0, it has a Scope ID.
[...]
Quoted 6 lines
So, here, either it has Scope ID, in which case it's returned, or it doesn't need a Scope ID, in which case 0 - "no scope id" - is returned.
Therefore, vsprintf should treat the 0 Scope ID the same way the rest of the v6 stack and the RFC treats it: 0 Scope ID means no Scope ID. And if there is no Scope ID, there is nothing to be printed.
I don't see an "issue" here. I.e. if the developer requested to append 's' to the specifier, then it's expected to dump what is in the scope id, even if that is 0 meaning "unset" scope.
It would be principle of least surprise -- I could imagine if not dumped, then a developer would check the source code to find out whether he did a mistake or it's expected behavior, just to make sure. That's what I would do probably.
Is this causing a _real_ issue somewhere (I couldn't parse one out of your commit message other than 'it would be nice to have')?
Quoted 26 lines
So, this patch only prints the Scope ID when one exists, and does not print a Scope ID when one does not exist.
It turns out, too, that this is what developers want. The most common purpose of %pISpfsc is to show "what is in that sockaddr so that I can have some idea of how to connect to it?"
Signed-off-by Jason A. Donenfeld <[email protected]>--- lib/vsprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/vsprintf.c +1
-1@@ -1194,7 +1194,7 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, p =number (p, pend,ntohl (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ), spec); } -if (have_s) { +if (have_s && sa->sin6_scope_id) { *p++ ='%' ; p =number (p, pend, sa->sin6_scope_id, spec); }
Quoted 8 lines · Daniel Borkmann
I don't see an "issue" here. I.e. if the developer requested to append 's' to the specifier, then it's expected to dump what is in the scope id, even if that is 0 meaning "unset" scope.
It would be principle of least surprise -- I could imagine if not dumped, then a developer would check the source code to find out whether he did a mistake or it's expected behavior, just to make sure. That's what I would do probably.
Showing something that's "unset" seems counter-intuitive.
The idea here is to be able to printk a sockaddr_in6, and have it show something that looks like what the user would naturally pass to getaddrinfo(3), which is entirely complete.
However, I could be convinced that this kind of behavior belongs in it's own flag. Maybe I'll cook up a flag for that instead.
Quoted 3 lines
Is this causing a _real_ issue somewhere (I couldn't parse one out of your commit message other than 'it would be nice to have')?
Nice to have.
Quoted 6 lines · Jason A. Donenfeld
The idea here is to be able to printk a
sockaddr_in6, and have it show something that looks like what the user would naturally pass to getaddrinfo(3), which is entirely complete.However, I could be convinced that this kind of behavior belongs in it's own flag. Maybe I'll cook up a flag for that instead.
I think that'd be best.
Maybe using something like %pISG for this that
would optionally show these flow and scope values
only when non-zero.
Something like:
---
lib/vsprintf.c | 30 +++++++++++++++++++-----------
1 file changed, 19 insertions(+), 11 deletions(-)
lib/vsprintf.c +0 -0
@@ -1147,7 +1147,8 @@ static noinline_for_stack
char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
struct printf_spec spec, const char *fmt)
{
- bool have_p = false, have_s = false, have_f = false, have_c = false;
+ u8 show_p = 0, show_s = 0, show_f = 0;
+ bool use_c = false;
char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + sizeof(":12345") + sizeof("/123456789") + sizeof("%1234567890")];
@@ -1160,43 +1161,50 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, while (isalpha(*++fmt)) {
switch (*fmt) {
case 'p':
- have_p = true;
+ show_p = 1;
break;
case 'f':
- have_f = true;
+ show_f = 1;
break;
case 's':
- have_s = true;
+ show_s = 1;
+ break;
+ case 'G':
+ show_p = 2;
+ show_f = 2;
+ show_s = 2;
break;
case 'c':
- have_c = true;
+ use_c = true;
break;
}
}
- if (have_p || have_s || have_f) {
+ if (show_p || show_s || show_f) { *p = '[';
off = 1;
}
- if (fmt6[0] == 'I' && have_c)
+ if (fmt6[0] == 'I' && use_c)
p = ip6_compressed_string(ip6_addr + off, addr); else
p = ip6_string(ip6_addr + off, addr, fmt6);
- if (have_p || have_s || have_f) + if (show_p || show_s || show_f)
*p++ = ']';
- if (have_p) {
+ if (show_p) {
*p++ = ':';
p = number(p, pend, ntohs(sa->sin6_port), spec); }
- if (have_f) {
+ if (show_f == 1 ||
+ (show_f == 2 && (sa->sin6_flowinfo & IPV6_FLOWINFO_MASK))) {
*p++ = '/';
p = number(p, pend, ntohl(sa->sin6_flowinfo & IPV6_FLOWINFO_MASK), spec); }
- if (have_s) {
+ if (show_s == 1 ||
+ (show_s == 2 && sa->sin6_scope_id)) {
*p++ = '%';
p = number(p, pend, sa->sin6_scope_id, spec); }
Quoted 9 lines · Joe Perches
Quoted 6 lines · Jason A. Donenfeld
The idea here is to be able to printk a
sockaddr_in6, and have it show something that looks like what the user would naturally pass to getaddrinfo(3), which is entirely complete.However, I could be convinced that this kind of behavior belongs in it's own flag. Maybe I'll cook up a flag for that instead.
I think that'd be best.
Agreed.
Quoted 5 lines
Maybe using something like %pISG for this that
would optionally show these flow and scope values
only when non-zero.Something like:
Looks good to me, having this as a single generic option seems even cleaner.
Thanks!
I've got something a bit cleaner that more accurately reflects this use case. Five minutes and I'll send over the patch.
PATCH 1/1 vsprintf: automatic parameters for %pIS via 'a'
In reply to Joe PerchesThis patch adds a variable 'a' which indicates that the 'p', 'f', and 's' options should be toggled on or off depending on whether or not those parameters are actually valid inside the passed sockaddr. This is something that probably most users of the %pIS family of functions will prefer to use.
--- Documentation/printk-formats.txt | 6 ++++-- lib/vsprintf.c | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-)
Documentation/printk-formats.txt +4 -2
@@ -193,7 +193,7 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): %piS 001.002.003.004 or 00010002000300040005000600070008 %pISc 1.2.3.4 or 1:2:3:4:5:6:7:8 %pISpc 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345 - %p[Ii]S[pfschnbl] + %p[Ii]S[pfsachnbl] For printing an IP address without the need to distinguish whether it's of type AF_INET or AF_INET6, a pointer to a valid 'struct sockaddr',
@@ -201,7 +201,9 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): The additional 'p', 'f', and 's' specifiers are used to specify port (IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ':' prefix, - flowinfo a '/' and scope a '%', each followed by the actual value. + flowinfo a '/' and scope a '%', each followed by the actual value. If 'a' + is given, 'p', 'f', and 's' are activated or deactivated depending on + whether or not the sockaddr has a non-zero port, flowinfo, and scope. In case of an IPv6 address the compressed IPv6 address as described by http://tools.ietf.org/html/rfc5952 is being used if the additional
lib/vsprintf.c +17 -2
@@ -1145,7 +1145,7 @@ static noinline_for_stackchar *ip6_addr_string_sa (char *buf,char *end,const struct sockaddr_in6 *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false, have_s = false, have_f = false, have_c = false; +bool have_p = false, have_s = false, have_f = false, have_c = false, have_a = false;char ip6_addr[sizeof ("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]" ) +sizeof (":12345" ) +sizeof ("/123456789" ) +sizeof ("%1234567890" )];
@@ -1169,9 +1169,18 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,case 'c' : have_c = true;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) { + have_p = sa->sin6_port !=0 ; + have_s = sa->sin6_scope_id !=0 ; + have_f = sa->sin6_flowinfo !=0 ; + } +if (have_p || have_s || have_f) { *p ='[' ; off =1 ;
@@ -1207,7 +1216,7 @@ static noinline_for_stackchar *ip4_addr_string_sa (char *buf,char *end,const struct sockaddr_in *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false; +bool have_p = false, have_a = false;char *p, ip4_addr[sizeof ("255.255.255.255" ) +sizeof (":12345" )];char *pend = ip4_addr +sizeof (ip4_addr);const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
@@ -1225,9 +1234,15 @@ char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,case 'b' : fmt4[2 ] = *fmt;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) + have_p = sa->sin_port !=0 ; + p =ip4_string (ip4_addr, addr, fmt4);if (have_p) { *p++ =':' ;
Signature · 1 line
2.7.0
Re: [PATCH] vsprintf: automatic parameters for %pIS via 'a'
Quoted 4 lines · Jason A. Donenfeld
This patch adds a variable 'a' which indicates that the 'p', 'f', and 's' options should be toggled on or off depending on whether or not those parameters are actually valid inside the passed sockaddr.
OK
Quoted 2 lines
This is something that probably most users of
the %pIS family of functions will prefer to use.
More doubtful.
There isn't a single user of flowinfo or scope today.
These are the current uses of %pIS
8 %pIS
11 %pISc
5 %pIScp
2 %pISp
23 %pISpc
lib/vsprintf.c
Quoted 1 line
lib/vsprintf.c +0
-0
[]
Quoted 10 lines
lib/vsprintf.c +0
-0@@ -1169,9 +1169,18 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,case 'c':
have_c= true;
break;lib/vsprintf.c +3
-0+case 'a' : + have_a = true; +break ;}
}
lib/vsprintf.c char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, · line 1181
Quoted 4 lines
lib/vsprintf.c +4
-0+if (have_a) { + have_p = sa->sin6_port !=0 ; + have_s = sa->sin6_scope_id !=0 ; + have_f = sa->sin6_flowinfo !=0 ;
Doesn't this needs the mask tested?
sa->sin6_flowinfo & IPV6_FLOWINFO_MASK
given the use of bool, the != 0 are unnecessary.
Other than that, looks good to me.
Re: [PATCH] vsprintf: automatic parameters for %pIS via 'a'
Quoted 4 lines · Joe Perches
Doesn't this needs the mask tested?
sa->sin6_flowinfo & IPV6_FLOWINFO_MASK
Good catch. Fixing and will submit another.
Quoted 2 lines
given the use of bool, the != 0 are unnecessary.
True.
Quoted 1 line
Other than that, looks good to me.
Great!
PATCH v2 1/1 vsprintf: automatic parameters for %pIS via 'a'
In reply to Joe PerchesThis patch adds a variable 'a' which indicates that the 'p', 'f', and 's' options should be toggled on or off depending on whether or not those parameters are actually valid inside the passed sockaddr.
--- Documentation/printk-formats.txt | 6 ++++-- lib/vsprintf.c | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-)
Documentation/printk-formats.txt +4 -2
@@ -193,7 +193,7 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): %piS 001.002.003.004 or 00010002000300040005000600070008 %pISc 1.2.3.4 or 1:2:3:4:5:6:7:8 %pISpc 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345 - %p[Ii]S[pfschnbl] + %p[Ii]S[pfsachnbl] For printing an IP address without the need to distinguish whether it's of type AF_INET or AF_INET6, a pointer to a valid 'struct sockaddr',
@@ -201,7 +201,9 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): The additional 'p', 'f', and 's' specifiers are used to specify port (IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ':' prefix, - flowinfo a '/' and scope a '%', each followed by the actual value. + flowinfo a '/' and scope a '%', each followed by the actual value. If 'a' + is given, 'p', 'f', and 's' are activated or deactivated depending on + whether or not the sockaddr has a non-zero port, flowinfo, and scope. In case of an IPv6 address the compressed IPv6 address as described by http://tools.ietf.org/html/rfc5952 is being used if the additional
lib/vsprintf.c +17 -2
@@ -1145,7 +1145,7 @@ static noinline_for_stackchar *ip6_addr_string_sa (char *buf,char *end,const struct sockaddr_in6 *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false, have_s = false, have_f = false, have_c = false; +bool have_p = false, have_s = false, have_f = false, have_c = false, have_a = false;char ip6_addr[sizeof ("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]" ) +sizeof (":12345" ) +sizeof ("/123456789" ) +sizeof ("%1234567890" )];
@@ -1169,9 +1169,18 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,case 'c' : have_c = true;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) { + have_p = sa->sin6_port; + have_s = sa->sin6_scope_id; + have_f = sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ; + } +if (have_p || have_s || have_f) { *p ='[' ; off =1 ;
@@ -1207,7 +1216,7 @@ static noinline_for_stackchar *ip4_addr_string_sa (char *buf,char *end,const struct sockaddr_in *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false; +bool have_p = false, have_a = false;char *p, ip4_addr[sizeof ("255.255.255.255" ) +sizeof (":12345" )];char *pend = ip4_addr +sizeof (ip4_addr);const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
@@ -1225,9 +1234,15 @@ char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,case 'b' : fmt4[2 ] = *fmt;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) + have_p = sa->sin_port !=0 ; + p =ip4_string (ip4_addr, addr, fmt4);if (have_p) { *p++ =':' ;
Signature · 1 line
2.7.0
Re: [PATCH v2] vsprintf: automatic parameters for %pIS via 'a'
Quoted 4 lines · Jason A. Donenfeld
This patch adds a variable 'a' which indicates that the 'p', 'f', and 's' options should be toggled on or off depending on whether or not those parameters are actually valid inside the passed sockaddr.
trivia:
lib/vsprintf.c
Quoted 1 line
lib/vsprintf.c +0
-0
[]
lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
Quoted 1 line
lib/vsprintf.c +0
-0@@ -1225,9 +1234,15 @@ char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
[]
lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, · line 1245
Quoted 3 lines
lib/vsprintf.c +3
-0+if (have_a) + have_p = sa->sin_port !=0 ; +
different style than the v6 tests
PATCH v3 1/1 vsprintf: automatic parameters for %pIS via 'a'
This patch adds a variable 'a' which indicates that the 'p', 'f', and 's' options should be toggled on or off depending on whether or not those parameters are actually valid inside the passed sockaddr.
--- Documentation/printk-formats.txt | 6 ++++-- lib/vsprintf.c | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-)
Documentation/printk-formats.txt +4 -2
@@ -193,7 +193,7 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): %piS 001.002.003.004 or 00010002000300040005000600070008 %pISc 1.2.3.4 or 1:2:3:4:5:6:7:8 %pISpc 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345 - %p[Ii]S[pfschnbl] + %p[Ii]S[pfsachnbl] For printing an IP address without the need to distinguish whether it's of type AF_INET or AF_INET6, a pointer to a valid 'struct sockaddr',
@@ -201,7 +201,9 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): The additional 'p', 'f', and 's' specifiers are used to specify port (IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ':' prefix, - flowinfo a '/' and scope a '%', each followed by the actual value. + flowinfo a '/' and scope a '%', each followed by the actual value. If 'a' + is given, 'p', 'f', and 's' are activated or deactivated depending on + whether or not the sockaddr has a non-zero port, flowinfo, and scope. In case of an IPv6 address the compressed IPv6 address as described by http://tools.ietf.org/html/rfc5952 is being used if the additional
lib/vsprintf.c +17 -2
@@ -1145,7 +1145,7 @@ static noinline_for_stackchar *ip6_addr_string_sa (char *buf,char *end,const struct sockaddr_in6 *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false, have_s = false, have_f = false, have_c = false; +bool have_p = false, have_s = false, have_f = false, have_c = false, have_a = false;char ip6_addr[sizeof ("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]" ) +sizeof (":12345" ) +sizeof ("/123456789" ) +sizeof ("%1234567890" )];
@@ -1169,9 +1169,18 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,case 'c' : have_c = true;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) { + have_p = sa->sin6_port; + have_s = sa->sin6_scope_id; + have_f = sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ; + } +if (have_p || have_s || have_f) { *p ='[' ; off =1 ;
@@ -1207,7 +1216,7 @@ static noinline_for_stackchar *ip4_addr_string_sa (char *buf,char *end,const struct sockaddr_in *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false; +bool have_p = false, have_a = false;char *p, ip4_addr[sizeof ("255.255.255.255" ) +sizeof (":12345" )];char *pend = ip4_addr +sizeof (ip4_addr);const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
@@ -1225,9 +1234,15 @@ char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,case 'b' : fmt4[2 ] = *fmt;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) + have_p = sa->sin_port; + p =ip4_string (ip4_addr, addr, fmt4);if (have_p) { *p++ =':' ;
Signature · 1 line
2.7.0
Rasmus Villemoes
reviewerRe: [PATCH v2] vsprintf: automatic parameters for %pIS via 'a'
In reply to Jason A. DonenfeldQuoted 54 lines · Jason A. Donenfeld
This patch adds a variable 'a' which indicates that the 'p', 'f', and 's' options should be toggled on or off depending on whether or not those parameters are actually valid inside the passed sockaddr.
Signed-off-by Jason A. Donenfeld <[email protected]>--- Documentation/printk-formats.txt | 6 ++++-- lib/vsprintf.c | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-)Documentation/printk-formats.txt +4
-2@@ -193,7 +193,7 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): %piS 001.002.003.004 or 00010002000300040005000600070008 %pISc 1.2.3.4 or 1:2:3:4:5:6:7:8 %pISpc 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345 - %p[Ii]S[pfschnbl] + %p[Ii]S[pfsachnbl] For printing an IP address without the need to distinguish whether it's of type AF_INET or AF_INET6, a pointer to a valid 'struct sockaddr',@@ -201,7 +201,9 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): The additional 'p', 'f', and 's' specifiers are used to specify port (IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ':' prefix, - flowinfo a '/' and scope a '%', each followed by the actual value. + flowinfo a '/' and scope a '%', each followed by the actual value. If 'a' + is given, 'p', 'f', and 's' are activated or deactivated depending on + whether or not the sockaddr has a non-zero port, flowinfo, and scope. In case of an IPv6 address the compressed IPv6 address as described by http://tools.ietf.org/html/rfc5952 is being used if the additionallib/vsprintf.c +3
-1@@ -1145,7 +1145,7 @@ static noinline_for_stackchar *ip6_addr_string_sa (char *buf,char *end,const struct sockaddr_in6 *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false, have_s = false, have_f = false, have_c = false; +bool have_p = false, have_s = false, have_f = false, have_c = false, have_a = false;char ip6_addr[sizeof ("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]" ) +sizeof (":12345" ) +sizeof ("/123456789" ) +sizeof ("%1234567890" )];@@ -1169,9 +1169,18 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,case 'c' : have_c = true;break ; +case 'a' : + have_a = true;
lib/vsprintf.c char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, · line 1183
Quoted 10 lines
lib/vsprintf.c +7
-0+break ; } } +if (have_a) { + have_p = sa->sin6_port; + have_s = sa->sin6_scope_id; + have_f = sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ; + } +
First of all I don't think vsprintf.c should be extended with yet another piece of unused code, so I hope there's an actual user coming.
Second, please add tests to lib/test_printf.c.
Third, wouldn't it be more convenient/flexible if a meant "print those that are present _and_ requested"; that is, the above would be
lib/vsprintf.c char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, · line 1186
Quoted 3 lines
lib/vsprintf.c +3
-0+ have_p = have_p && sa->sin6_port; + have_s = have_s && sa->sin6_scope_id; + have_f = have_f && sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ;
that way one could say %pISaf and have the flowinfo if and only if it's present, without also unintentionally turning on port or scope_id.
Rasmus
Re: [PATCH v2] vsprintf: automatic parameters for %pIS via 'a'
Quoted 2 lines · Rasmus Villemoes
First of all I don't think
vsprintf.cshould be extended with yet another piece of unused code, so I hope there's an actual user coming.
There is. Don't worry.
Quoted 2 lines
Second, please add tests to
lib/test_printf.c.
Sure, no problem. I'll add this in the next version.
Quoted 10 lines
Third, wouldn't it be more convenient/flexible if a meant "print those that are present _and_ requested"; that is, the above would be
Review
Quoted 3 lines
(quoted diff) +3
-0+ have_p = have_p && sa->sin6_port; + have_s = have_s && sa->sin6_scope_id; + have_f = have_f && sa->sin6_flowinfo & IPV6_FLOWINFO_MASK;that way one could say %pISaf and have the flowinfo if and only if it's present, without also unintentionally turning on port or
scope_id.
That's a great idea. I'll make this change too for the next version.
PATCH 1/1 vsprintf: automatic parameters for %pIS via 'a'
In reply to Rasmus VillemoesThis patch adds a variable 'a' which indicates that the 'p', 'f', and 's' options should active depending on whether or not those parameters are actually valid inside the passed sockaddr.
--- Documentation/printk-formats.txt | 7 +++++-- lib/test_printf.c | 23 +++++++++++++++++++++++ lib/vsprintf.c | 19 +++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-)
Documentation/printk-formats.txt +5 -2
@@ -193,7 +193,7 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): %piS 001.002.003.004 or 00010002000300040005000600070008 %pISc 1.2.3.4 or 1:2:3:4:5:6:7:8 %pISpc 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345 - %p[Ii]S[pfschnbl] + %p[Ii]S[pfsachnbl] For printing an IP address without the need to distinguish whether it's of type AF_INET or AF_INET6, a pointer to a valid 'struct sockaddr',
@@ -201,7 +201,10 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): The additional 'p', 'f', and 's' specifiers are used to specify port (IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ':' prefix, - flowinfo a '/' and scope a '%', each followed by the actual value. + flowinfo a '/' and scope a '%', each followed by the actual value. If 'a' + is given, 'p', 'f', and 's', when also specified, are only printed + depending on whether or not the socketaddr has a non-zero port, + flowinfo, and scope. In case of an IPv6 address the compressed IPv6 address as described by http://tools.ietf.org/html/rfc5952 is being used if the additional
lib/test_printf.c +23 -0
@@ -16,6 +16,7 @@#include <linux/dcache.h> #include <linux/socket.h> #include <linux/in.h> +#include <linux/in6.h> #define BUF_SIZE 256#define PAD_SIZE 16
@@ -301,11 +302,33 @@ ip4(void)test ("127.000.000.001|127.0.0.1" ,"%piS|%pIS" , &sa, &sa); sa.sin_addr.s_addr =cpu_to_be32 (0x01020304 );test ("001.002.003.004:12345|1.2.3.4:12345" ,"%piSp|%pISp" , &sa, &sa); +test ("1.2.3.4:12345" ,"%pISpa" , &sa); +test ("1.2.3.4" ,"%pISa" , &sa); + sa.sin_port =0 ; +test ("1.2.3.4" ,"%pISpa" , &sa); }static void __init ip6 (void ) { +struct sockaddr_in6 sa = {0 }; + + sa.sin6_family =AF_INET6 ; + sa.sin6_port =cpu_to_be16 (12345 ); + sa.sin6_addr.in6_u.u6_addr16[0 ] =cpu_to_be16 (0xabcd ); + sa.sin6_addr.in6_u.u6_addr16[7 ] =cpu_to_be16 (0x1234 ); + sa.sin6_flowinfo =cpu_to_be32 (20 ); + sa.sin6_scope_id =98 ; + +test ("[abcd::1234]:12345" ,"%pISpca" , &sa); +test ("[abcd::1234]:12345/20" ,"%pISpfca" , &sa); +test ("[abcd::1234]:12345/20%98" ,"%pISpfsca" , &sa); + sa.sin6_flowinfo =0 ; +test ("[abcd::1234]:12345%98" ,"%pISpsfca" , &sa); + sa.sin6_port =0 ; +test ("[abcd::1234]%98" ,"%pISpfsca" , &sa); + sa.sin6_scope_id =0 ; +test ("abcd::1234" ,"%pISpfsca" , &sa); } static void __init
lib/vsprintf.c +17 -2
@@ -1145,7 +1145,7 @@ static noinline_for_stackchar *ip6_addr_string_sa (char *buf,char *end,const struct sockaddr_in6 *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false, have_s = false, have_f = false, have_c = false; +bool have_p = false, have_s = false, have_f = false, have_c = false, have_a = false;char ip6_addr[sizeof ("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]" ) +sizeof (":12345" ) +sizeof ("/123456789" ) +sizeof ("%1234567890" )];
@@ -1169,9 +1169,18 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,case 'c' : have_c = true;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) { + have_p = have_p && sa->sin6_port; + have_s = have_s && sa->sin6_scope_id; + have_f = have_f && (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ); + } +if (have_p || have_s || have_f) { *p ='[' ; off =1 ;
@@ -1207,7 +1216,7 @@ static noinline_for_stackchar *ip4_addr_string_sa (char *buf,char *end,const struct sockaddr_in *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false; +bool have_p = false, have_a = false;char *p, ip4_addr[sizeof ("255.255.255.255" ) +sizeof (":12345" )];char *pend = ip4_addr +sizeof (ip4_addr);const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
@@ -1225,9 +1234,15 @@ char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,case 'b' : fmt4[2 ] = *fmt;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) + have_p = have_p && sa->sin_port; + p =ip4_string (ip4_addr, addr, fmt4);if (have_p) { *p++ =':' ;
Signature · 1 line
2.7.0
PATCH v4 1/1 vsprintf: automatic parameters for %pIS via 'a'
In reply to Rasmus VillemoesThis patch adds a variable 'a' which indicates that the 'p', 'f', and 's' options should active depending on whether or not those parameters are actually valid inside the passed sockaddr.
--- Documentation/printk-formats.txt | 7 +++++-- lib/test_printf.c | 23 +++++++++++++++++++++++ lib/vsprintf.c | 19 +++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-)
Documentation/printk-formats.txt +5 -2
@@ -193,7 +193,7 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): %piS 001.002.003.004 or 00010002000300040005000600070008 %pISc 1.2.3.4 or 1:2:3:4:5:6:7:8 %pISpc 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345 - %p[Ii]S[pfschnbl] + %p[Ii]S[pfsachnbl] For printing an IP address without the need to distinguish whether it's of type AF_INET or AF_INET6, a pointer to a valid 'struct sockaddr',
@@ -201,7 +201,10 @@ IPv4/IPv6 addresses (generic, with port, flowinfo, scope): The additional 'p', 'f', and 's' specifiers are used to specify port (IPv4, IPv6), flowinfo (IPv6) and scope (IPv6). Ports have a ':' prefix, - flowinfo a '/' and scope a '%', each followed by the actual value. + flowinfo a '/' and scope a '%', each followed by the actual value. If 'a' + is given, 'p', 'f', and 's', when also specified, are only printed + depending on whether or not the socketaddr has a non-zero port, + flowinfo, and scope. In case of an IPv6 address the compressed IPv6 address as described by http://tools.ietf.org/html/rfc5952 is being used if the additional
lib/test_printf.c +23 -0
@@ -16,6 +16,7 @@#include <linux/dcache.h> #include <linux/socket.h> #include <linux/in.h> +#include <linux/in6.h> #define BUF_SIZE 256#define PAD_SIZE 16
@@ -301,11 +302,33 @@ ip4(void)test ("127.000.000.001|127.0.0.1" ,"%piS|%pIS" , &sa, &sa); sa.sin_addr.s_addr =cpu_to_be32 (0x01020304 );test ("001.002.003.004:12345|1.2.3.4:12345" ,"%piSp|%pISp" , &sa, &sa); +test ("1.2.3.4:12345" ,"%pISpa" , &sa); +test ("1.2.3.4" ,"%pISa" , &sa); + sa.sin_port =0 ; +test ("1.2.3.4" ,"%pISpa" , &sa); }static void __init ip6 (void ) { +struct sockaddr_in6 sa = {0 }; + + sa.sin6_family =AF_INET6 ; + sa.sin6_port =cpu_to_be16 (12345 ); + sa.sin6_addr.in6_u.u6_addr16[0 ] =cpu_to_be16 (0xabcd ); + sa.sin6_addr.in6_u.u6_addr16[7 ] =cpu_to_be16 (0x1234 ); + sa.sin6_flowinfo =cpu_to_be32 (20 ); + sa.sin6_scope_id =98 ; + +test ("[abcd::1234]:12345" ,"%pISpca" , &sa); +test ("[abcd::1234]:12345/20" ,"%pISpfca" , &sa); +test ("[abcd::1234]:12345/20%98" ,"%pISpfsca" , &sa); + sa.sin6_flowinfo =0 ; +test ("[abcd::1234]:12345%98" ,"%pISpsfca" , &sa); + sa.sin6_port =0 ; +test ("[abcd::1234]%98" ,"%pISpfsca" , &sa); + sa.sin6_scope_id =0 ; +test ("abcd::1234" ,"%pISpfsca" , &sa); } static void __init
lib/vsprintf.c +17 -2
@@ -1145,7 +1145,7 @@ static noinline_for_stackchar *ip6_addr_string_sa (char *buf,char *end,const struct sockaddr_in6 *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false, have_s = false, have_f = false, have_c = false; +bool have_p = false, have_s = false, have_f = false, have_c = false, have_a = false;char ip6_addr[sizeof ("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]" ) +sizeof (":12345" ) +sizeof ("/123456789" ) +sizeof ("%1234567890" )];
@@ -1169,9 +1169,18 @@ char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,case 'c' : have_c = true;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) { + have_p = have_p && sa->sin6_port; + have_s = have_s && sa->sin6_scope_id; + have_f = have_f && (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK ); + } +if (have_p || have_s || have_f) { *p ='[' ; off =1 ;
@@ -1207,7 +1216,7 @@ static noinline_for_stackchar *ip4_addr_string_sa (char *buf,char *end,const struct sockaddr_in *sa,struct printf_spec spec,const char *fmt) { -bool have_p = false; +bool have_p = false, have_a = false;char *p, ip4_addr[sizeof ("255.255.255.255" ) +sizeof (":12345" )];char *pend = ip4_addr +sizeof (ip4_addr);const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
@@ -1225,9 +1234,15 @@ char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,case 'b' : fmt4[2 ] = *fmt;break ; +case 'a' : + have_a = true; +break ; } } +if (have_a) + have_p = have_p && sa->sin_port; + p =ip4_string (ip4_addr, addr, fmt4);if (have_p) { *p++ =':' ;
Signature · 1 line
2.7.0