1*86d7f5d3SJohn Marino
2*86d7f5d3SJohn Marino /*
3*86d7f5d3SJohn Marino * debug.c
4*86d7f5d3SJohn Marino *
5*86d7f5d3SJohn Marino * Copyright (c) 1996-1999 Whistle Communications, Inc.
6*86d7f5d3SJohn Marino * All rights reserved.
7*86d7f5d3SJohn Marino *
8*86d7f5d3SJohn Marino * Subject to the following obligations and disclaimer of warranty, use and
9*86d7f5d3SJohn Marino * redistribution of this software, in source or object code forms, with or
10*86d7f5d3SJohn Marino * without modifications are expressly permitted by Whistle Communications;
11*86d7f5d3SJohn Marino * provided, however, that:
12*86d7f5d3SJohn Marino * 1. Any and all reproductions of the source or object code must include the
13*86d7f5d3SJohn Marino * copyright notice above and the following disclaimer of warranties; and
14*86d7f5d3SJohn Marino * 2. No rights are granted, in any manner or form, to use Whistle
15*86d7f5d3SJohn Marino * Communications, Inc. trademarks, including the mark "WHISTLE
16*86d7f5d3SJohn Marino * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17*86d7f5d3SJohn Marino * such appears in the above copyright notice or in the software.
18*86d7f5d3SJohn Marino *
19*86d7f5d3SJohn Marino * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20*86d7f5d3SJohn Marino * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21*86d7f5d3SJohn Marino * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22*86d7f5d3SJohn Marino * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23*86d7f5d3SJohn Marino * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24*86d7f5d3SJohn Marino * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25*86d7f5d3SJohn Marino * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26*86d7f5d3SJohn Marino * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27*86d7f5d3SJohn Marino * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28*86d7f5d3SJohn Marino * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29*86d7f5d3SJohn Marino * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30*86d7f5d3SJohn Marino * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31*86d7f5d3SJohn Marino * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32*86d7f5d3SJohn Marino * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33*86d7f5d3SJohn Marino * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34*86d7f5d3SJohn Marino * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35*86d7f5d3SJohn Marino * OF SUCH DAMAGE.
36*86d7f5d3SJohn Marino *
37*86d7f5d3SJohn Marino * Author: Archie Cobbs <archie@whistle.com>
38*86d7f5d3SJohn Marino *
39*86d7f5d3SJohn Marino * $FreeBSD: src/lib/libnetgraph/debug.c,v 1.5.2.1 2000/05/01 18:09:54 archie Exp $
40*86d7f5d3SJohn Marino * $Whistle: debug.c,v 1.24 1999/01/24 01:15:33 archie Exp $
41*86d7f5d3SJohn Marino */
42*86d7f5d3SJohn Marino
43*86d7f5d3SJohn Marino #include <sys/types.h>
44*86d7f5d3SJohn Marino #include <sys/time.h>
45*86d7f5d3SJohn Marino #include <sys/ioctl.h>
46*86d7f5d3SJohn Marino
47*86d7f5d3SJohn Marino #include <stdarg.h>
48*86d7f5d3SJohn Marino
49*86d7f5d3SJohn Marino #include <netinet/in.h>
50*86d7f5d3SJohn Marino #include <net/ethernet.h>
51*86d7f5d3SJohn Marino #include <net/bpf.h>
52*86d7f5d3SJohn Marino
53*86d7f5d3SJohn Marino #include <netgraph/ng_message.h>
54*86d7f5d3SJohn Marino #include <netgraph/socket/ng_socket.h>
55*86d7f5d3SJohn Marino
56*86d7f5d3SJohn Marino #include "netgraph.h"
57*86d7f5d3SJohn Marino #include "internal.h"
58*86d7f5d3SJohn Marino
59*86d7f5d3SJohn Marino #include <netgraph/UI/ng_UI.h>
60*86d7f5d3SJohn Marino #include <netgraph/async/ng_async.h>
61*86d7f5d3SJohn Marino #include <netgraph/bpf/ng_bpf.h>
62*86d7f5d3SJohn Marino #include <netgraph/bridge/ng_bridge.h>
63*86d7f5d3SJohn Marino #include <netgraph/cisco/ng_cisco.h>
64*86d7f5d3SJohn Marino #include <netgraph/echo/ng_echo.h>
65*86d7f5d3SJohn Marino #include <netgraph/eiface/ng_eiface.h>
66*86d7f5d3SJohn Marino #include <netgraph/etf/ng_etf.h>
67*86d7f5d3SJohn Marino #include <netgraph/ether/ng_ether.h>
68*86d7f5d3SJohn Marino #include <netgraph/frame_relay/ng_frame_relay.h>
69*86d7f5d3SJohn Marino #include <netgraph/hole/ng_hole.h>
70*86d7f5d3SJohn Marino #include <netgraph/iface/ng_iface.h>
71*86d7f5d3SJohn Marino #include <netgraph/ksocket/ng_ksocket.h>
72*86d7f5d3SJohn Marino #include <netgraph/l2tp/ng_l2tp.h>
73*86d7f5d3SJohn Marino #include <netgraph/lmi/ng_lmi.h>
74*86d7f5d3SJohn Marino #include <netgraph/mppc/ng_mppc.h>
75*86d7f5d3SJohn Marino #include <netgraph/one2many/ng_one2many.h>
76*86d7f5d3SJohn Marino #include <netgraph/ppp/ng_ppp.h>
77*86d7f5d3SJohn Marino #include <netgraph/pppoe/ng_pppoe.h>
78*86d7f5d3SJohn Marino #include <netgraph/pptpgre/ng_pptpgre.h>
79*86d7f5d3SJohn Marino #include <netgraph/rfc1490/ng_rfc1490.h>
80*86d7f5d3SJohn Marino #include <netgraph/tee/ng_tee.h>
81*86d7f5d3SJohn Marino #include <netgraph/tty/ng_tty.h>
82*86d7f5d3SJohn Marino #include <netgraph/vjc/ng_vjc.h>
83*86d7f5d3SJohn Marino #ifdef WHISTLE
84*86d7f5d3SJohn Marino #include <machine/../isa/df_def.h>
85*86d7f5d3SJohn Marino #include <machine/../isa/if_wfra.h>
86*86d7f5d3SJohn Marino #include <machine/../isa/ipac.h>
87*86d7f5d3SJohn Marino #include <netgraph/ng_df.h>
88*86d7f5d3SJohn Marino #include <netgraph/ng_ipac.h>
89*86d7f5d3SJohn Marino #include <netgraph/ng_tn.h>
90*86d7f5d3SJohn Marino #endif
91*86d7f5d3SJohn Marino
92*86d7f5d3SJohn Marino /* Global debug level */
93*86d7f5d3SJohn Marino int _gNgDebugLevel = 0;
94*86d7f5d3SJohn Marino
95*86d7f5d3SJohn Marino /* Debug printing functions */
96*86d7f5d3SJohn Marino void (*_NgLog) (const char *fmt,...) __printflike(1, 2) = warn;
97*86d7f5d3SJohn Marino void (*_NgLogx) (const char *fmt,...) __printflike(1, 2) = warnx;
98*86d7f5d3SJohn Marino
99*86d7f5d3SJohn Marino /* Internal functions */
100*86d7f5d3SJohn Marino static const char *NgCookie(int cookie);
101*86d7f5d3SJohn Marino
102*86d7f5d3SJohn Marino /* Known typecookie list */
103*86d7f5d3SJohn Marino struct ng_cookie {
104*86d7f5d3SJohn Marino int cookie;
105*86d7f5d3SJohn Marino const char *type;
106*86d7f5d3SJohn Marino };
107*86d7f5d3SJohn Marino
108*86d7f5d3SJohn Marino #define COOKIE(c) { NGM_ ## c ## _COOKIE, #c }
109*86d7f5d3SJohn Marino
110*86d7f5d3SJohn Marino /* List of known cookies */
111*86d7f5d3SJohn Marino static const struct ng_cookie cookies[] = {
112*86d7f5d3SJohn Marino COOKIE(UI),
113*86d7f5d3SJohn Marino COOKIE(ASYNC),
114*86d7f5d3SJohn Marino COOKIE(BPF),
115*86d7f5d3SJohn Marino COOKIE(BRIDGE),
116*86d7f5d3SJohn Marino COOKIE(CISCO),
117*86d7f5d3SJohn Marino COOKIE(ECHO),
118*86d7f5d3SJohn Marino COOKIE(EIFACE),
119*86d7f5d3SJohn Marino COOKIE(ETF),
120*86d7f5d3SJohn Marino COOKIE(ETHER),
121*86d7f5d3SJohn Marino COOKIE(FRAMERELAY),
122*86d7f5d3SJohn Marino COOKIE(GENERIC),
123*86d7f5d3SJohn Marino COOKIE(HOLE),
124*86d7f5d3SJohn Marino COOKIE(IFACE),
125*86d7f5d3SJohn Marino COOKIE(KSOCKET),
126*86d7f5d3SJohn Marino COOKIE(L2TP),
127*86d7f5d3SJohn Marino COOKIE(LMI),
128*86d7f5d3SJohn Marino COOKIE(MPPC),
129*86d7f5d3SJohn Marino COOKIE(ONE2MANY),
130*86d7f5d3SJohn Marino COOKIE(PPP),
131*86d7f5d3SJohn Marino COOKIE(PPPOE),
132*86d7f5d3SJohn Marino COOKIE(PPTPGRE),
133*86d7f5d3SJohn Marino COOKIE(RFC1490),
134*86d7f5d3SJohn Marino COOKIE(SOCKET),
135*86d7f5d3SJohn Marino COOKIE(TEE),
136*86d7f5d3SJohn Marino COOKIE(TTY),
137*86d7f5d3SJohn Marino COOKIE(VJC),
138*86d7f5d3SJohn Marino #ifdef WHISTLE
139*86d7f5d3SJohn Marino COOKIE(DF),
140*86d7f5d3SJohn Marino COOKIE(IPAC),
141*86d7f5d3SJohn Marino COOKIE(TN),
142*86d7f5d3SJohn Marino COOKIE(WFRA),
143*86d7f5d3SJohn Marino #endif
144*86d7f5d3SJohn Marino { 0, NULL }
145*86d7f5d3SJohn Marino };
146*86d7f5d3SJohn Marino
147*86d7f5d3SJohn Marino /*
148*86d7f5d3SJohn Marino * Set debug level, ie, verbosity, if "level" is non-negative.
149*86d7f5d3SJohn Marino * Returns old debug level.
150*86d7f5d3SJohn Marino */
151*86d7f5d3SJohn Marino int
NgSetDebug(int level)152*86d7f5d3SJohn Marino NgSetDebug(int level)
153*86d7f5d3SJohn Marino {
154*86d7f5d3SJohn Marino int old = _gNgDebugLevel;
155*86d7f5d3SJohn Marino
156*86d7f5d3SJohn Marino if (level < 0)
157*86d7f5d3SJohn Marino level = old;
158*86d7f5d3SJohn Marino _gNgDebugLevel = level;
159*86d7f5d3SJohn Marino return (old);
160*86d7f5d3SJohn Marino }
161*86d7f5d3SJohn Marino
162*86d7f5d3SJohn Marino /*
163*86d7f5d3SJohn Marino * Set debug logging functions.
164*86d7f5d3SJohn Marino */
165*86d7f5d3SJohn Marino void
NgSetErrLog(void (* log)(const char * fmt,...),void (* logx)(const char * fmt,...))166*86d7f5d3SJohn Marino NgSetErrLog(void (*log) (const char *fmt,...),
167*86d7f5d3SJohn Marino void (*logx) (const char *fmt,...))
168*86d7f5d3SJohn Marino {
169*86d7f5d3SJohn Marino _NgLog = log;
170*86d7f5d3SJohn Marino _NgLogx = logx;
171*86d7f5d3SJohn Marino }
172*86d7f5d3SJohn Marino
173*86d7f5d3SJohn Marino /*
174*86d7f5d3SJohn Marino * Display a netgraph sockaddr
175*86d7f5d3SJohn Marino */
176*86d7f5d3SJohn Marino void
_NgDebugSockaddr(const struct sockaddr_ng * sg)177*86d7f5d3SJohn Marino _NgDebugSockaddr(const struct sockaddr_ng *sg)
178*86d7f5d3SJohn Marino {
179*86d7f5d3SJohn Marino NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
180*86d7f5d3SJohn Marino sg->sg_family, sg->sg_len, sg->sg_data);
181*86d7f5d3SJohn Marino }
182*86d7f5d3SJohn Marino
183*86d7f5d3SJohn Marino #define ARGS_BUFSIZE 2048
184*86d7f5d3SJohn Marino #define RECURSIVE_DEBUG_ADJUST 4
185*86d7f5d3SJohn Marino
186*86d7f5d3SJohn Marino /*
187*86d7f5d3SJohn Marino * Display a negraph message
188*86d7f5d3SJohn Marino */
189*86d7f5d3SJohn Marino void
_NgDebugMsg(const struct ng_mesg * msg,const char * path)190*86d7f5d3SJohn Marino _NgDebugMsg(const struct ng_mesg *msg, const char *path)
191*86d7f5d3SJohn Marino {
192*86d7f5d3SJohn Marino u_char buf[2 * sizeof(struct ng_mesg) + ARGS_BUFSIZE];
193*86d7f5d3SJohn Marino struct ng_mesg *const req = (struct ng_mesg *)buf;
194*86d7f5d3SJohn Marino struct ng_mesg *const bin = (struct ng_mesg *)req->data;
195*86d7f5d3SJohn Marino int arglen, csock = -1;
196*86d7f5d3SJohn Marino
197*86d7f5d3SJohn Marino /* Display header stuff */
198*86d7f5d3SJohn Marino NGLOGX("NG_MESG :");
199*86d7f5d3SJohn Marino NGLOGX(" vers %d", msg->header.version);
200*86d7f5d3SJohn Marino NGLOGX(" arglen %d", msg->header.arglen);
201*86d7f5d3SJohn Marino NGLOGX(" flags %u", msg->header.flags);
202*86d7f5d3SJohn Marino NGLOGX(" token %u", msg->header.token);
203*86d7f5d3SJohn Marino NGLOGX(" cookie %s (%d)",
204*86d7f5d3SJohn Marino NgCookie(msg->header.typecookie), msg->header.typecookie);
205*86d7f5d3SJohn Marino
206*86d7f5d3SJohn Marino /* At lower debugging levels, skip ASCII translation */
207*86d7f5d3SJohn Marino if (_gNgDebugLevel <= 2)
208*86d7f5d3SJohn Marino goto fail2;
209*86d7f5d3SJohn Marino
210*86d7f5d3SJohn Marino /* If path is not absolute, don't bother trying to use relative
211*86d7f5d3SJohn Marino address on a different socket for the ASCII translation */
212*86d7f5d3SJohn Marino if (strchr(path, ':') == NULL)
213*86d7f5d3SJohn Marino goto fail2;
214*86d7f5d3SJohn Marino
215*86d7f5d3SJohn Marino /* Get a temporary socket */
216*86d7f5d3SJohn Marino if (NgMkSockNode(NULL, &csock, NULL) < 0)
217*86d7f5d3SJohn Marino goto fail;
218*86d7f5d3SJohn Marino
219*86d7f5d3SJohn Marino /* Copy binary message into request message payload */
220*86d7f5d3SJohn Marino arglen = msg->header.arglen;
221*86d7f5d3SJohn Marino if (arglen > ARGS_BUFSIZE)
222*86d7f5d3SJohn Marino arglen = ARGS_BUFSIZE;
223*86d7f5d3SJohn Marino memcpy(bin, msg, sizeof(*msg) + arglen);
224*86d7f5d3SJohn Marino bin->header.arglen = arglen;
225*86d7f5d3SJohn Marino
226*86d7f5d3SJohn Marino /* Lower debugging to avoid infinite recursion */
227*86d7f5d3SJohn Marino _gNgDebugLevel -= RECURSIVE_DEBUG_ADJUST;
228*86d7f5d3SJohn Marino
229*86d7f5d3SJohn Marino /* Ask the node to translate the binary message to ASCII for us */
230*86d7f5d3SJohn Marino if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
231*86d7f5d3SJohn Marino NGM_BINARY2ASCII, bin, sizeof(*bin) + bin->header.arglen) < 0) {
232*86d7f5d3SJohn Marino _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
233*86d7f5d3SJohn Marino goto fail;
234*86d7f5d3SJohn Marino }
235*86d7f5d3SJohn Marino if (NgRecvMsg(csock, req, sizeof(buf), NULL) < 0) {
236*86d7f5d3SJohn Marino _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
237*86d7f5d3SJohn Marino goto fail;
238*86d7f5d3SJohn Marino }
239*86d7f5d3SJohn Marino
240*86d7f5d3SJohn Marino /* Restore debugging level */
241*86d7f5d3SJohn Marino _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
242*86d7f5d3SJohn Marino
243*86d7f5d3SJohn Marino /* Display command string and arguments */
244*86d7f5d3SJohn Marino NGLOGX(" cmd %s (%d)", bin->header.cmdstr, bin->header.cmd);
245*86d7f5d3SJohn Marino NGLOGX(" args %s", bin->data);
246*86d7f5d3SJohn Marino goto done;
247*86d7f5d3SJohn Marino
248*86d7f5d3SJohn Marino fail:
249*86d7f5d3SJohn Marino /* Just display binary version */
250*86d7f5d3SJohn Marino NGLOGX(" [error decoding message: %s]", strerror(errno));
251*86d7f5d3SJohn Marino fail2:
252*86d7f5d3SJohn Marino NGLOGX(" cmd %d", msg->header.cmd);
253*86d7f5d3SJohn Marino NGLOGX(" args (%d bytes)", msg->header.arglen);
254*86d7f5d3SJohn Marino _NgDebugBytes(msg->data, msg->header.arglen);
255*86d7f5d3SJohn Marino
256*86d7f5d3SJohn Marino done:
257*86d7f5d3SJohn Marino if (csock != -1)
258*86d7f5d3SJohn Marino (void)close(csock);
259*86d7f5d3SJohn Marino }
260*86d7f5d3SJohn Marino
261*86d7f5d3SJohn Marino /*
262*86d7f5d3SJohn Marino * Return the name of the node type corresponding to the cookie
263*86d7f5d3SJohn Marino */
264*86d7f5d3SJohn Marino static const char *
NgCookie(int cookie)265*86d7f5d3SJohn Marino NgCookie(int cookie)
266*86d7f5d3SJohn Marino {
267*86d7f5d3SJohn Marino int k;
268*86d7f5d3SJohn Marino
269*86d7f5d3SJohn Marino for (k = 0; cookies[k].cookie != 0; k++) {
270*86d7f5d3SJohn Marino if (cookies[k].cookie == cookie)
271*86d7f5d3SJohn Marino return cookies[k].type;
272*86d7f5d3SJohn Marino }
273*86d7f5d3SJohn Marino return "??";
274*86d7f5d3SJohn Marino }
275*86d7f5d3SJohn Marino
276*86d7f5d3SJohn Marino /*
277*86d7f5d3SJohn Marino * Dump bytes in hex
278*86d7f5d3SJohn Marino */
279*86d7f5d3SJohn Marino void
_NgDebugBytes(const u_char * ptr,int len)280*86d7f5d3SJohn Marino _NgDebugBytes(const u_char *ptr, int len)
281*86d7f5d3SJohn Marino {
282*86d7f5d3SJohn Marino char buf[100];
283*86d7f5d3SJohn Marino int k, count;
284*86d7f5d3SJohn Marino
285*86d7f5d3SJohn Marino #define BYPERLINE 16
286*86d7f5d3SJohn Marino
287*86d7f5d3SJohn Marino for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {
288*86d7f5d3SJohn Marino
289*86d7f5d3SJohn Marino /* Do hex */
290*86d7f5d3SJohn Marino snprintf(buf, sizeof(buf), "%04x: ", count);
291*86d7f5d3SJohn Marino for (k = 0; k < BYPERLINE; k++, count++)
292*86d7f5d3SJohn Marino if (count < len)
293*86d7f5d3SJohn Marino snprintf(buf + strlen(buf),
294*86d7f5d3SJohn Marino sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
295*86d7f5d3SJohn Marino else
296*86d7f5d3SJohn Marino snprintf(buf + strlen(buf),
297*86d7f5d3SJohn Marino sizeof(buf) - strlen(buf), " ");
298*86d7f5d3SJohn Marino snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
299*86d7f5d3SJohn Marino count -= BYPERLINE;
300*86d7f5d3SJohn Marino
301*86d7f5d3SJohn Marino /* Do ASCII */
302*86d7f5d3SJohn Marino for (k = 0; k < BYPERLINE; k++, count++)
303*86d7f5d3SJohn Marino if (count < len)
304*86d7f5d3SJohn Marino snprintf(buf + strlen(buf),
305*86d7f5d3SJohn Marino sizeof(buf) - strlen(buf),
306*86d7f5d3SJohn Marino "%c", isprint(ptr[k]) ? ptr[k] : '.');
307*86d7f5d3SJohn Marino else
308*86d7f5d3SJohn Marino snprintf(buf + strlen(buf),
309*86d7f5d3SJohn Marino sizeof(buf) - strlen(buf), " ");
310*86d7f5d3SJohn Marino count -= BYPERLINE;
311*86d7f5d3SJohn Marino
312*86d7f5d3SJohn Marino /* Print it */
313*86d7f5d3SJohn Marino NGLOGX("%s", buf);
314*86d7f5d3SJohn Marino }
315*86d7f5d3SJohn Marino }
316*86d7f5d3SJohn Marino
317