stacktrace_save() sync

2020-04-15 Thread Visa Hankala
This diff:

* upgrades stacktrace_save() to stacktrace_save_at() if the latter
  is missing on the architecture,
* defines stacktrace_save() as an inline function in 
  to replace MD definitions.

OK?

Index: arch/amd64/amd64/db_trace.c
===
RCS file: src/sys/arch/amd64/amd64/db_trace.c,v
retrieving revision 1.51
diff -u -p -r1.51 db_trace.c
--- arch/amd64/amd64/db_trace.c 29 Mar 2020 15:14:28 -  1.51
+++ arch/amd64/amd64/db_trace.c 15 Apr 2020 15:55:08 -
@@ -288,12 +288,6 @@ stacktrace_save_at(struct stacktrace *st
}
 }
 
-void
-stacktrace_save(struct stacktrace *st)
-{
-   return stacktrace_save_at(st, 0);
-}
-
 vaddr_t
 db_get_pc(struct trapframe *tf)
 {
Index: arch/arm64/arm64/db_trace.c
===
RCS file: src/sys/arch/arm64/arm64/db_trace.c,v
retrieving revision 1.10
diff -u -p -r1.10 db_trace.c
--- arch/arm64/arm64/db_trace.c 29 Mar 2020 15:14:28 -  1.10
+++ arch/arm64/arm64/db_trace.c 15 Apr 2020 15:55:08 -
@@ -150,7 +150,7 @@ db_stack_trace_print(db_expr_t addr, int
 }
 
 void
-stacktrace_save(struct stacktrace *st)
+stacktrace_save_at(struct stacktrace *st, unsigned int skip)
 {
struct callframe *frame, *lastframe, *limit;
struct proc *p = curproc;
@@ -166,7 +166,10 @@ stacktrace_save(struct stacktrace *st)
sizeof(struct trapframe) - 0x10);
 
while (st->st_count < STACKTRACE_MAX) {
-   st->st_pc[st->st_count++] = frame->f_lr;
+   if (skip == 0)
+   st->st_pc[st->st_count++] = frame->f_lr;
+   else
+   skip--;
 
lastframe = frame;
frame = frame->f_frame;
Index: arch/hppa/hppa/db_interface.c
===
RCS file: src/sys/arch/hppa/hppa/db_interface.c,v
retrieving revision 1.47
diff -u -p -r1.47 db_interface.c
--- arch/hppa/hppa/db_interface.c   20 Jan 2020 15:58:23 -  1.47
+++ arch/hppa/hppa/db_interface.c   15 Apr 2020 15:55:09 -
@@ -316,7 +316,7 @@ db_stack_trace_print(db_expr_t addr, int
 }
 
 void
-stacktrace_save(struct stacktrace *st)
+stacktrace_save_at(struct stacktrace *st, unsigned int skip)
 {
register_t *fp, pc, rp;
int i;
@@ -327,7 +327,10 @@ stacktrace_save(struct stacktrace *st)
 
st->st_count = 0;
for (i = 0; i < STACKTRACE_MAX; i++) {
-   st->st_pc[st->st_count++] = rp;
+   if (skip == 0)
+   st->st_pc[st->st_count++] = rp;
+   else
+   skip--;
 
/* next frame */
pc = rp;
Index: arch/i386/i386/db_trace.c
===
RCS file: src/sys/arch/i386/i386/db_trace.c,v
retrieving revision 1.40
diff -u -p -r1.40 db_trace.c
--- arch/i386/i386/db_trace.c   29 Mar 2020 15:14:28 -  1.40
+++ arch/i386/i386/db_trace.c   15 Apr 2020 15:55:09 -
@@ -293,12 +293,6 @@ stacktrace_save_at(struct stacktrace *st
}
 }
 
-void
-stacktrace_save(struct stacktrace *st)
-{
-   return stacktrace_save_at(st, 0);
-}
-
 vaddr_t
 db_get_pc(struct trapframe *tf)
 {
Index: arch/mips64/mips64/trap.c
===
RCS file: src/sys/arch/mips64/mips64/trap.c,v
retrieving revision 1.143
diff -u -p -r1.143 trap.c
--- arch/mips64/mips64/trap.c   20 Jan 2020 15:58:23 -  1.143
+++ arch/mips64/mips64/trap.c   15 Apr 2020 15:55:09 -
@@ -1477,7 +1477,7 @@ end:
 
 #ifdef DDB
 void
-stacktrace_save(struct stacktrace *st)
+stacktrace_save_at(struct stacktrace *st, unsigned int skip)
 {
extern char k_general[];
extern char u_general[];
@@ -1503,8 +1503,12 @@ stacktrace_save(struct stacktrace *st)
if (!VALID_ADDRESS(pc) || !VALID_ADDRESS(sp))
break;
 
-   if (!first)
-   st->st_pc[st->st_count++] = pc;
+   if (!first) {
+   if (skip == 0)
+   st->st_pc[st->st_count++] = pc;
+   else
+   skip--;
+   }
first = 0;
 
/* Determine the start address of the current subroutine. */
Index: arch/powerpc/ddb/db_trace.c
===
RCS file: src/sys/arch/powerpc/ddb/db_trace.c,v
retrieving revision 1.15
diff -u -p -r1.15 db_trace.c
--- arch/powerpc/ddb/db_trace.c 10 Apr 2020 07:23:21 -  1.15
+++ arch/powerpc/ddb/db_trace.c 15 Apr 2020 15:55:09 -
@@ -253,9 +253,3 @@ stacktrace_save_at(struct stacktrace *st
break;
}
 }
-
-void
-stacktrace_save(struct stacktrace *st)
-{
-   return stacktrace_save_at(st, 0);
-}
Index: arch/sparc64/sparc64/db_trace.c

Re: Warning in net/art.c w/ -Wuninitialized

2020-04-15 Thread Martin Pieuchot
On 15/04/20(Wed) 09:26, Martin Pieuchot wrote:
> jca@ is currently building kernels with "-Wno-error=uninitialized" and
> reported a warning in ART in SP builds:
> 
> /usr/src/sys/net/art.c:256:10: warning: variable 'ndsr' is uninitialized when 
> used here [-Wuninitialized]
> dsr = ndsr;
>   ^~~~
> /usr/src/sys/net/art.c:221:2: note: variable 'ndsr' is declared here
> struct srp_ref  dsr, ndsr;
> ^
> warning generated.
> 
> With a single CPU there's no need to save a reference and srp_leave()
> just does nothing.  In other words the warning above is harmless. 
> 
> The diff below prevents the false positive by turning srp_enter() into
> a static inline function, I find that nicer rather than clutter the code
> with #ifdef.

Now without typo, spotted by jca@ thanks!

Index: sys/srp.h
===
RCS file: /cvs/src/sys/sys/srp.h,v
retrieving revision 1.14
diff -u -p -r1.14 srp.h
--- sys/srp.h   31 Mar 2019 14:03:40 -  1.14
+++ sys/srp.h   15 Apr 2020 15:03:00 -
@@ -96,11 +96,17 @@ void*srp_enter(struct srp_ref *, struc
 void   *srp_follow(struct srp_ref *, struct srp *);
 voidsrp_leave(struct srp_ref *);
 #else /* MULTIPROCESSOR */
+
+static inline void *
+srp_enter(struct srp_ref *sr, struct srp *srp)
+{
+   return (srp->ref);
+}
+
 #define srp_swap(_srp, _v) srp_swap_locked((_srp), (_v))
 #define srp_update(_gc, _srp, _v)  srp_update_locked((_gc), (_srp), (_v))
 #define srp_finalize(_v, _wchan)   ((void)0)
-#define srp_enter(_sr, _srp)   ((_srp)->ref)
-#define srp_follow(_sr, _srp)  ((_srp)->ref)
+#define srp_follow(_sr, _srp)  srp_enter(_sr, _srp)
 #define srp_leave(_sr) do { } while (0)
 #endif /* MULTIPROCESSOR */
 



Re: update strstr.c and memmem.c to latest musl

2020-04-15 Thread Todd C . Miller
On Wed, 15 Apr 2020 09:52:31 +0200, Claudio Jeker wrote:

> Our strstr.c is based on musl but since the import a few minor tweaks
> happened and this brings us back in sync. For memmem.c our libc has a very
> simple implementation. This switches the code to the memmem.c from musl
> which is O(n) like strstr.c. For memmem() similar configure checks are
> done as for strstr to verify the runtime so it makes sense to provide a
> good version in libc.

OK millert@

 - todd



update strstr.c and memmem.c to latest musl

2020-04-15 Thread Claudio Jeker
Our strstr.c is based on musl but since the import a few minor tweaks
happened and this brings us back in sync. For memmem.c our libc has a very
simple implementation. This switches the code to the memmem.c from musl
which is O(n) like strstr.c. For memmem() similar configure checks are
done as for strstr to verify the runtime so it makes sense to provide a
good version in libc.

-- 
:wq Claudio

Index: string/memmem.c
===
RCS file: /cvs/src/lib/libc/string/memmem.c,v
retrieving revision 1.4
diff -u -p -r1.4 memmem.c
--- string/memmem.c 31 Aug 2015 02:53:57 -  1.4
+++ string/memmem.c 15 Apr 2020 07:38:09 -
@@ -1,64 +1,184 @@
-/* $OpenBSD: memmem.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */
-/*-
- * Copyright (c) 2005 Pascal Gloor 
+/* $OpenBSD$ */
+
+/*
+ * Copyright (c) 2005-2020 Rich Felker, et al.
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *notice, this list of conditions and the following disclaimer in the
- *documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote
- *products derived from this software without specific prior written
- *permission.
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
  *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
 #include 
+#include 
+
+static char *
+twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
+{
+   uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
+   for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++)
+   if (hw == nw) return (char *)h-2;
+   return hw == nw ? (char *)h-2 : 0;
+}
+
+static char *
+threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
+{
+   uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
+   uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
+   for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
+   if (hw == nw) return (char *)h-3;
+   return hw == nw ? (char *)h-3 : 0;
+}
+
+static char *
+fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
+{
+   uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
+   uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
+   for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
+   if (hw == nw) return (char *)h-4;
+   return hw == nw ? (char *)h-4 : 0;
+}
+
+#define MAX(a,b) ((a)>(b)?(a):(b))
+#define MIN(a,b) ((a)<(b)?(a):(b))
+
+#define BITOP(a,b,op) \
+ ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a
 
 /*
- * Find the first occurrence of the byte string s in byte string l.
+ * Maxime Crochemore and Dominique Perrin, Two-way string-matching,
+ * Journal of the ACM, 38(3):651-675, July 1991.
  */
+static char *
+twoway_memmem(const unsigned char *h, const unsigned char *z,
+const unsigned char *n, 

Warning in net/art.c w/ -Wuninitialized

2020-04-15 Thread Martin Pieuchot
jca@ is currently building kernels with "-Wno-error=uninitialized" and
reported a warning in ART in SP builds:

/usr/src/sys/net/art.c:256:10: warning: variable 'ndsr' is uninitialized when 
used here [-Wuninitialized]
dsr = ndsr;
  ^~~~
/usr/src/sys/net/art.c:221:2: note: variable 'ndsr' is declared here
struct srp_ref  dsr, ndsr;
^
warning generated.

With a single CPU there's no need to save a reference and srp_leave()
just does nothing.  In other words the warning above is harmless. 

The diff below prevents the false positive by turning srp_enter() into
a static inline function, I find that nicer rather than clutter the code
with #ifdef.

ok?

Index: sys/srp.h
===
RCS file: /cvs/src/sys/sys/srp.h,v
retrieving revision 1.14
diff -u -p -r1.14 srp.h
--- sys/srp.h   31 Mar 2019 14:03:40 -  1.14
+++ sys/srp.h   14 Apr 2020 08:51:00 -
@@ -96,11 +96,17 @@ void*srp_enter(struct srp_ref *, struc
 void   *srp_follow(struct srp_ref *, struct srp *);
 voidsrp_leave(struct srp_ref *);
 #else /* MULTIPROCESSOR */
+
+static inline
+srp_enter(struct srp_ref *sr, struct srp *srp)
+{
+   return (srp->ref);
+}
+
 #define srp_swap(_srp, _v) srp_swap_locked((_srp), (_v))
 #define srp_update(_gc, _srp, _v)  srp_update_locked((_gc), (_srp), (_v))
 #define srp_finalize(_v, _wchan)   ((void)0)
-#define srp_enter(_sr, _srp)   ((_srp)->ref)
-#define srp_follow(_sr, _srp)  ((_srp)->ref)
+#define srp_follow(_sr, _srp)  srp_enter(_sr, _srp)
 #define srp_leave(_sr) do { } while (0)
 #endif /* MULTIPROCESSOR */
 



Re: tcpdump: print nhrp packets

2020-04-15 Thread David Gwynne



> On 14 Apr 2020, at 8:15 pm, Remi Locherer  wrote:
> 
> On Tue, Apr 14, 2020 at 01:49:32PM +1000, David Gwynne wrote:
>> 
>> 
>>> On 13 Apr 2020, at 19:03, Remi Locherer  wrote:
>>> 
>>> Hi,
>>> 
>>> I recently looked into NHRP (RFC 2332) and noticed that our tcpdump does
>>> not have a printer for it. So I added support for NHRP to tcpdump.
>>> 
>>> Initially I was surprised: I expected a simpler protocol! But it is from
>>> the 90's with all the protocols from then in mind (frame relay, ATM, ...).
>>> 
>>> I tested with public available pcap files and compared the output with
>>> wirshark.
>>> https://packetlife.net/captures/protocol/nhrp/
>>> https://www.networkingwithfish.com/fun-in-the-lab-sniffer-tracing-a-dmvpn-tunnel-startup/
>>> 
>>> The output looks like this:
>>> 
>>> 08:34:45.647483 172.16.25.2 > 172.16.15.2: gre NHRP: reg request, id 7 [tos 
>>> 0xc0]
>>> 08:34:45.671422 172.16.15.2 > 172.16.25.2: gre NHRP: reg reply, id 7 [tos 
>>> 0xc0]
>>> 
>>> 08:47:16.138679 172.16.15.2 > 172.16.25.2: gre NHRP: res request, id 6 [tos 
>>> 0xc0]
>>> 08:47:16.148863 172.16.25.2 > 172.16.15.2: gre NHRP: res reply, id 6 [tos 
>>> 0xc0]
>>> 
>>> With -v set:
>>> 
>>> 08:34:45.647483 172.16.25.2 > 172.16.15.2: gre [] 2001 NHRP: reg request, 
>>> id 7, hopcnt 255, src nbma 172.16.25.2, 192.168.0.2 -> 192.168.0.1 (code 0, 
>>> pl 255, mtu 1514, htime 7200, pref 0) [tos 0xc0] (ttl 254, id 22, len 116)
>>> 08:34:45.671422 172.16.15.2 > 172.16.25.2: gre [] 2001 NHRP: reg reply, id 
>>> 7, hopcnt 255, src nbma 172.16.25.2, 192.168.0.2 -> 192.168.0.1 (code 0, pl 
>>> 255, mtu 1514, htime 7200, pref 0) [tos 0xc0] (ttl 255, id 7, len 136)
>>> 
>>> 08:47:16.138679 172.16.15.2 > 172.16.25.2: gre [] 2001 NHRP: res request, 
>>> id 6, hopcnt 254, src nbma 172.16.45.2, 192.168.0.4 -> 192.168.0.2 (code 0, 
>>> pl 0, mtu 1514, htime 7200, pref 0) [tos 0xc0] (ttl 254, id 20, len 116)
>>> 08:47:16.148863 172.16.25.2 > 172.16.15.2: gre [] 2001 NHRP: res reply, id 
>>> 6, hopcnt 255, src nbma 172.16.45.2, 192.168.0.4 -> 192.168.0.2 (code 0, pl 
>>> 32, mtu 1514, htime 7199, pref 0, nbma 172.16.25.2, proto 192.168.0.2) [tos 
>>> 0xc0] (ttl 255, id 31, len 144)
>>> 
>>> Extensions are not parsed and printed.
>>> 
>>> It would be nice to get pcaps with expamles that use address or protocol
>>> combinations other than GRE and IPv4.
>>> 
>>> Comments, OKs?
>> 
>> Can you print the addresses when -v is not set too?
>> 
>> Otherwise I'm keen.
>> 
> 
> Like this?

yes. ok by me.

> tcpdump -n:
> 08:47:16.068855 172.16.25.2 > 172.16.15.2: gre NHRP: res request, id 8, src 
> nbma 172.16.25.2, 192.168.0.2 -> 192.168.0.4 (code 0) [tos 0xc0]
> 08:47:16.150679 172.16.15.2 > 172.16.25.2: gre NHRP: res reply, id 8, src 
> nbma 172.16.25.2, 192.168.0.2 -> 192.168.0.4 (code 0, nbma 172.16.45.2, proto 
> 192.168.0.4) [tos 0xc0]
> 
> tcpdump -nv:
> 08:47:16.068855 172.16.25.2 > 172.16.15.2: gre [] 2001 NHRP: res request, id 
> 8, hopcnt 255, src nbma 172.16.25.2, 192.168.0.2 -> 192.168.0.4 (code 0, pl 
> 0, mtu 1514, htime 7200, pref 0) [tos 0xc0] (ttl 255, id 29, len 96)
> 08:47:16.150679 172.16.15.2 > 172.16.25.2: gre [] 2001 NHRP: res reply, id 8, 
> hopcnt 254, src nbma 172.16.25.2, 192.168.0.2 -> 192.168.0.4 (code 0, pl 32, 
> mtu 1514, htime 7199, pref 0, nbma 172.16.45.2, proto 192.168.0.4) [tos 0xc0] 
> (ttl 254, id 21, len 164)
> 
> 
> 
> Index: Makefile
> ===
> RCS file: /cvs/src/usr.sbin/tcpdump/Makefile,v
> retrieving revision 1.64
> diff -u -p -r1.64 Makefile
> --- Makefile  3 Dec 2019 01:43:33 -   1.64
> +++ Makefile  28 Mar 2020 17:07:22 -
> @@ -48,7 +48,7 @@ SRCS=   tcpdump.c addrtoname.c privsep.c p
>   print-bgp.c print-ospf6.c print-ripng.c print-rt6.c print-stp.c \
>   print-etherip.c print-lwres.c print-lldp.c print-cdp.c print-pflog.c \
>   print-pfsync.c pf_print_state.c print-ofp.c ofp_map.c \
> - print-udpencap.c print-carp.c \
> + print-udpencap.c print-carp.c print-nhrp.c \
>   print-802_11.c print-iapp.c print-mpls.c print-slow.c print-usbpcap.c \
>   gmt2local.c savestr.c setsignal.c in_cksum.c
> 
> Index: interface.h
> ===
> RCS file: /cvs/src/usr.sbin/tcpdump/interface.h,v
> retrieving revision 1.83
> diff -u -p -r1.83 interface.h
> --- interface.h   3 Dec 2019 01:43:33 -   1.83
> +++ interface.h   28 Mar 2020 17:07:22 -
> @@ -217,6 +217,7 @@ extern void ppp_ether_if_print(u_char *,
> extern void gre_print(const u_char *, u_int);
> extern void vxlan_print(const u_char *, u_int);
> extern void nsh_print(const u_char *, u_int);
> +extern void nhrp_print(const u_char *, u_int);
> extern void icmp_print(const u_char *, u_int, const u_char *);
> extern void ieee802_11_if_print(u_char *, const struct pcap_pkthdr *,
> const u_char *);
> Index: print-ether.c
> ===
>