xref: /freebsd/lib/libnetgraph/debug.c (revision baa60a3b175739c68b9eaf6797ed415ef38ad2c3)
14cf49a43SJulian Elischer 
24cf49a43SJulian Elischer /*
34cf49a43SJulian Elischer  * debug.c
44cf49a43SJulian Elischer  *
54cf49a43SJulian Elischer  * Copyright (c) 1996-1999 Whistle Communications, Inc.
64cf49a43SJulian Elischer  * All rights reserved.
74cf49a43SJulian Elischer  *
84cf49a43SJulian Elischer  * Subject to the following obligations and disclaimer of warranty, use and
94cf49a43SJulian Elischer  * redistribution of this software, in source or object code forms, with or
104cf49a43SJulian Elischer  * without modifications are expressly permitted by Whistle Communications;
114cf49a43SJulian Elischer  * provided, however, that:
124cf49a43SJulian Elischer  * 1. Any and all reproductions of the source or object code must include the
134cf49a43SJulian Elischer  *    copyright notice above and the following disclaimer of warranties; and
144cf49a43SJulian Elischer  * 2. No rights are granted, in any manner or form, to use Whistle
154cf49a43SJulian Elischer  *    Communications, Inc. trademarks, including the mark "WHISTLE
164cf49a43SJulian Elischer  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
174cf49a43SJulian Elischer  *    such appears in the above copyright notice or in the software.
184cf49a43SJulian Elischer  *
194cf49a43SJulian Elischer  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
204cf49a43SJulian Elischer  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
214cf49a43SJulian Elischer  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
224cf49a43SJulian Elischer  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
234cf49a43SJulian Elischer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
244cf49a43SJulian Elischer  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
254cf49a43SJulian Elischer  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
264cf49a43SJulian Elischer  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
274cf49a43SJulian Elischer  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
284cf49a43SJulian Elischer  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
294cf49a43SJulian Elischer  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
304cf49a43SJulian Elischer  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
314cf49a43SJulian Elischer  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
324cf49a43SJulian Elischer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
334cf49a43SJulian Elischer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
344cf49a43SJulian Elischer  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
354cf49a43SJulian Elischer  * OF SUCH DAMAGE.
364cf49a43SJulian Elischer  *
374cf49a43SJulian Elischer  * Author: Archie Cobbs <archie@whistle.com>
384cf49a43SJulian Elischer  *
394cf49a43SJulian Elischer  * $FreeBSD$
404cf49a43SJulian Elischer  * $Whistle: debug.c,v 1.24 1999/01/24 01:15:33 archie Exp $
414cf49a43SJulian Elischer  */
424cf49a43SJulian Elischer 
434cf49a43SJulian Elischer #include <sys/types.h>
44baa60a3bSArchie Cobbs #include <sys/time.h>
45baa60a3bSArchie Cobbs #include <sys/ioctl.h>
46f8307e12SArchie Cobbs 
474cf49a43SJulian Elischer #include <stdarg.h>
48f8307e12SArchie Cobbs 
49f8307e12SArchie Cobbs #include <netinet/in.h>
50f8307e12SArchie Cobbs #include <net/ethernet.h>
51baa60a3bSArchie Cobbs #include <net/bpf.h>
52f8307e12SArchie Cobbs 
534cf49a43SJulian Elischer #include <netgraph/ng_message.h>
544cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
554cf49a43SJulian Elischer 
564cf49a43SJulian Elischer #include "netgraph.h"
574cf49a43SJulian Elischer #include "internal.h"
584cf49a43SJulian Elischer 
59f8307e12SArchie Cobbs #include <netgraph/ng_UI.h>
604cf49a43SJulian Elischer #include <netgraph/ng_async.h>
61baa60a3bSArchie Cobbs #include <netgraph/ng_bpf.h>
62f8307e12SArchie Cobbs #include <netgraph/ng_cisco.h>
63f8307e12SArchie Cobbs #include <netgraph/ng_echo.h>
64f8307e12SArchie Cobbs #include <netgraph/ng_ether.h>
654cf49a43SJulian Elischer #include <netgraph/ng_frame_relay.h>
66f8307e12SArchie Cobbs #include <netgraph/ng_hole.h>
67f8307e12SArchie Cobbs #include <netgraph/ng_iface.h>
68f8307e12SArchie Cobbs #include <netgraph/ng_ksocket.h>
694cf49a43SJulian Elischer #include <netgraph/ng_lmi.h>
70f8307e12SArchie Cobbs #include <netgraph/ng_ppp.h>
71f8307e12SArchie Cobbs #include <netgraph/ng_pppoe.h>
72f8307e12SArchie Cobbs #include <netgraph/ng_rfc1490.h>
73f8307e12SArchie Cobbs #include <netgraph/ng_socket.h>
74f8307e12SArchie Cobbs #include <netgraph/ng_tee.h>
754cf49a43SJulian Elischer #include <netgraph/ng_tty.h>
76f8307e12SArchie Cobbs #include <netgraph/ng_vjc.h>
77f8307e12SArchie Cobbs #ifdef	WHISTLE
78f8307e12SArchie Cobbs #include <machine/../isa/df_def.h>
79f8307e12SArchie Cobbs #include <machine/../isa/if_wfra.h>
80f8307e12SArchie Cobbs #include <machine/../isa/ipac.h>
81f8307e12SArchie Cobbs #include <netgraph/ng_df.h>
82f8307e12SArchie Cobbs #include <netgraph/ng_ipac.h>
83f8307e12SArchie Cobbs #include <netgraph/ng_mppc.h>
84f8307e12SArchie Cobbs #include <netgraph/ng_pptpgre.h>
85f8307e12SArchie Cobbs #include <netgraph/ng_tn.h>
86f8307e12SArchie Cobbs #endif
874cf49a43SJulian Elischer 
884cf49a43SJulian Elischer /* Global debug level */
894cf49a43SJulian Elischer int     _gNgDebugLevel = 0;
904cf49a43SJulian Elischer 
914cf49a43SJulian Elischer /* Debug printing functions */
924cf49a43SJulian Elischer void    (*_NgLog) (const char *fmt,...) = warn;
934cf49a43SJulian Elischer void    (*_NgLogx) (const char *fmt,...) = warnx;
944cf49a43SJulian Elischer 
954cf49a43SJulian Elischer /* Internal functions */
964cf49a43SJulian Elischer static const	char *NgCookie(int cookie);
97f8307e12SArchie Cobbs 
98f8307e12SArchie Cobbs /* Known typecookie list */
99f8307e12SArchie Cobbs struct ng_cookie {
100f8307e12SArchie Cobbs 	int		cookie;
101f8307e12SArchie Cobbs 	const char	*type;
102f8307e12SArchie Cobbs };
103f8307e12SArchie Cobbs 
104f8307e12SArchie Cobbs #define COOKIE(c)	{ NGM_ ## c ## _COOKIE, #c }
105f8307e12SArchie Cobbs 
106f8307e12SArchie Cobbs /* List of known cookies */
107f8307e12SArchie Cobbs static const struct ng_cookie cookies[] = {
108f8307e12SArchie Cobbs 	COOKIE(UI),
109f8307e12SArchie Cobbs 	COOKIE(ASYNC),
110e489a907SArchie Cobbs 	COOKIE(BPF),
111f8307e12SArchie Cobbs 	COOKIE(CISCO),
112f8307e12SArchie Cobbs 	COOKIE(ECHO),
113f8307e12SArchie Cobbs 	COOKIE(ETHER),
114f8307e12SArchie Cobbs 	COOKIE(FRAMERELAY),
115f8307e12SArchie Cobbs 	COOKIE(GENERIC),
116f8307e12SArchie Cobbs 	COOKIE(HOLE),
117f8307e12SArchie Cobbs 	COOKIE(IFACE),
118f8307e12SArchie Cobbs 	COOKIE(KSOCKET),
119f8307e12SArchie Cobbs 	COOKIE(LMI),
120f8307e12SArchie Cobbs 	COOKIE(PPP),
121f8307e12SArchie Cobbs 	COOKIE(PPPOE),
122f8307e12SArchie Cobbs 	COOKIE(RFC1490),
123f8307e12SArchie Cobbs 	COOKIE(SOCKET),
124f8307e12SArchie Cobbs 	COOKIE(TEE),
125f8307e12SArchie Cobbs 	COOKIE(TTY),
126f8307e12SArchie Cobbs 	COOKIE(VJC),
127f8307e12SArchie Cobbs #ifdef WHISTLE
128f8307e12SArchie Cobbs 	COOKIE(DF),
129f8307e12SArchie Cobbs 	COOKIE(IPAC),
130f8307e12SArchie Cobbs 	COOKIE(MPPC),
131f8307e12SArchie Cobbs 	COOKIE(PPTPGRE),
132f8307e12SArchie Cobbs 	COOKIE(TN),
133f8307e12SArchie Cobbs 	COOKIE(WFRA),
134f8307e12SArchie Cobbs #endif
135f8307e12SArchie Cobbs 	{ 0, NULL }
136f8307e12SArchie Cobbs };
1374cf49a43SJulian Elischer 
1384cf49a43SJulian Elischer /*
1394cf49a43SJulian Elischer  * Set debug level, ie, verbosity, if "level" is non-negative.
1404cf49a43SJulian Elischer  * Returns old debug level.
1414cf49a43SJulian Elischer  */
1424cf49a43SJulian Elischer int
1434cf49a43SJulian Elischer NgSetDebug(int level)
1444cf49a43SJulian Elischer {
1454cf49a43SJulian Elischer 	int old = _gNgDebugLevel;
1464cf49a43SJulian Elischer 
1474cf49a43SJulian Elischer 	if (level < 0)
1484cf49a43SJulian Elischer 		level = old;
1494cf49a43SJulian Elischer 	_gNgDebugLevel = level;
1504cf49a43SJulian Elischer 	return (old);
1514cf49a43SJulian Elischer }
1524cf49a43SJulian Elischer 
1534cf49a43SJulian Elischer /*
1544cf49a43SJulian Elischer  * Set debug logging functions.
1554cf49a43SJulian Elischer  */
1564cf49a43SJulian Elischer void
1574cf49a43SJulian Elischer NgSetErrLog(void (*log) (const char *fmt,...),
1584cf49a43SJulian Elischer 		void (*logx) (const char *fmt,...))
1594cf49a43SJulian Elischer {
1604cf49a43SJulian Elischer 	_NgLog = log;
1614cf49a43SJulian Elischer 	_NgLogx = logx;
1624cf49a43SJulian Elischer }
1634cf49a43SJulian Elischer 
1644cf49a43SJulian Elischer /*
1654cf49a43SJulian Elischer  * Display a netgraph sockaddr
1664cf49a43SJulian Elischer  */
1674cf49a43SJulian Elischer void
168f8307e12SArchie Cobbs _NgDebugSockaddr(const struct sockaddr_ng *sg)
1694cf49a43SJulian Elischer {
1704cf49a43SJulian Elischer 	NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
1714cf49a43SJulian Elischer 	       sg->sg_family, sg->sg_len, sg->sg_data);
1724cf49a43SJulian Elischer }
1734cf49a43SJulian Elischer 
174f8307e12SArchie Cobbs #define ARGS_BUFSIZE	1024
175f8307e12SArchie Cobbs 
1764cf49a43SJulian Elischer /*
1774cf49a43SJulian Elischer  * Display a negraph message
1784cf49a43SJulian Elischer  */
1794cf49a43SJulian Elischer void
180f8307e12SArchie Cobbs _NgDebugMsg(const struct ng_mesg *msg, const char *path)
1814cf49a43SJulian Elischer {
182f8307e12SArchie Cobbs 	u_char buf[2 * sizeof(struct ng_mesg) + ARGS_BUFSIZE];
183f8307e12SArchie Cobbs 	struct ng_mesg *const req = (struct ng_mesg *)buf;
184f8307e12SArchie Cobbs 	struct ng_mesg *const bin = (struct ng_mesg *)req->data;
185f8307e12SArchie Cobbs 	int arglen, debugSave, csock = -1;
186f8307e12SArchie Cobbs 
187f8307e12SArchie Cobbs 	/* Lower debugging to avoid infinite recursion */
188f8307e12SArchie Cobbs 	debugSave = _gNgDebugLevel;
189f8307e12SArchie Cobbs 	_gNgDebugLevel -= 4;
190f8307e12SArchie Cobbs 
191f8307e12SArchie Cobbs 	/* Display header stuff */
1924cf49a43SJulian Elischer 	NGLOGX("NG_MESG :");
1934cf49a43SJulian Elischer 	NGLOGX("  vers   %d", msg->header.version);
1944cf49a43SJulian Elischer 	NGLOGX("  arglen %d", msg->header.arglen);
1954cf49a43SJulian Elischer 	NGLOGX("  flags  %ld", msg->header.flags);
1964cf49a43SJulian Elischer 	NGLOGX("  token  %lu", (u_long)msg->header.token);
197f8307e12SArchie Cobbs 	NGLOGX("  cookie %s (%d)",
198f8307e12SArchie Cobbs 	    NgCookie(msg->header.typecookie), msg->header.typecookie);
199f8307e12SArchie Cobbs 
200f8307e12SArchie Cobbs 	/* At lower debugging levels, skip ASCII translation */
201f8307e12SArchie Cobbs 	if (_gNgDebugLevel <= 2)
202f8307e12SArchie Cobbs 		goto fail2;
203f8307e12SArchie Cobbs 
204f8307e12SArchie Cobbs 	/* If path is not absolute, don't bother trying to use relative
205f8307e12SArchie Cobbs 	   address on a different socket for the ASCII translation */
206f8307e12SArchie Cobbs 	if (strchr(path, ':') == NULL)
207f8307e12SArchie Cobbs 		goto fail2;
208f8307e12SArchie Cobbs 
209f8307e12SArchie Cobbs 	/* Get a temporary socket */
210f8307e12SArchie Cobbs 	if (NgMkSockNode(NULL, &csock, NULL) < 0)
211f8307e12SArchie Cobbs 		goto fail;
212f8307e12SArchie Cobbs 
213f8307e12SArchie Cobbs 	/* Copy binary message into request message payload */
214f8307e12SArchie Cobbs 	arglen = msg->header.arglen;
215f8307e12SArchie Cobbs 	if (arglen > ARGS_BUFSIZE)
216f8307e12SArchie Cobbs 		arglen = ARGS_BUFSIZE;
217f8307e12SArchie Cobbs 	memcpy(bin, msg, sizeof(*msg) + arglen);
218f8307e12SArchie Cobbs 	bin->header.arglen = arglen;
219f8307e12SArchie Cobbs 
220f8307e12SArchie Cobbs 	/* Ask the node to translate the binary message to ASCII for us */
221f8307e12SArchie Cobbs 	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
222f8307e12SArchie Cobbs 	    NGM_BINARY2ASCII, bin, sizeof(*bin) + bin->header.arglen) < 0)
223f8307e12SArchie Cobbs 		goto fail;
224f8307e12SArchie Cobbs 	if (NgRecvMsg(csock, req, sizeof(buf), NULL) < 0)
225f8307e12SArchie Cobbs 		goto fail;
226f8307e12SArchie Cobbs 
227f8307e12SArchie Cobbs 	/* Display command string and arguments */
228f8307e12SArchie Cobbs 	NGLOGX("  cmd    %s (%d)", bin->header.cmdstr, bin->header.cmd);
229f8307e12SArchie Cobbs 	NGLOGX("  args   %s", bin->data);
230f8307e12SArchie Cobbs 	goto done;
231f8307e12SArchie Cobbs 
232f8307e12SArchie Cobbs fail:
233f8307e12SArchie Cobbs 	/* Just display binary version */
234f8307e12SArchie Cobbs 	NGLOGX("  [error decoding message: %s]", strerror(errno));
235f8307e12SArchie Cobbs fail2:
236f8307e12SArchie Cobbs 	NGLOGX("  cmd    %d", msg->header.cmd);
237f8307e12SArchie Cobbs 	NGLOGX("  args (%d bytes)", msg->header.arglen);
238f8307e12SArchie Cobbs 	_NgDebugBytes(msg->data, msg->header.arglen);
239f8307e12SArchie Cobbs 
240f8307e12SArchie Cobbs done:
241f8307e12SArchie Cobbs 	if (csock != -1)
242f8307e12SArchie Cobbs 		(void)close(csock);
243f8307e12SArchie Cobbs 	_gNgDebugLevel = debugSave;
2444cf49a43SJulian Elischer }
2454cf49a43SJulian Elischer 
2464cf49a43SJulian Elischer /*
2474cf49a43SJulian Elischer  * Return the name of the node type corresponding to the cookie
2484cf49a43SJulian Elischer  */
2494cf49a43SJulian Elischer static const char *
2504cf49a43SJulian Elischer NgCookie(int cookie)
2514cf49a43SJulian Elischer {
252f8307e12SArchie Cobbs 	int k;
2534cf49a43SJulian Elischer 
254f8307e12SArchie Cobbs 	for (k = 0; cookies[k].cookie != 0; k++) {
255f8307e12SArchie Cobbs 		if (cookies[k].cookie == cookie)
256f8307e12SArchie Cobbs 			return cookies[k].type;
2574cf49a43SJulian Elischer 	}
258f8307e12SArchie Cobbs 	return "??";
2594cf49a43SJulian Elischer }
2604cf49a43SJulian Elischer 
2614cf49a43SJulian Elischer /*
2624cf49a43SJulian Elischer  * Dump bytes in hex
2634cf49a43SJulian Elischer  */
2644cf49a43SJulian Elischer void
2654cf49a43SJulian Elischer _NgDebugBytes(const u_char *ptr, int len)
2664cf49a43SJulian Elischer {
2674cf49a43SJulian Elischer 	char    buf[100];
2684cf49a43SJulian Elischer 	int     k, count;
2694cf49a43SJulian Elischer 
2704cf49a43SJulian Elischer #define BYPERLINE	16
2714cf49a43SJulian Elischer 
2724cf49a43SJulian Elischer 	for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {
2734cf49a43SJulian Elischer 
2744cf49a43SJulian Elischer 		/* Do hex */
2754cf49a43SJulian Elischer 		snprintf(buf, sizeof(buf), "%04x:  ", count);
2764cf49a43SJulian Elischer 		for (k = 0; k < BYPERLINE; k++, count++)
2774cf49a43SJulian Elischer 			if (count < len)
2784cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
2794cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
2804cf49a43SJulian Elischer 			else
2814cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
2824cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "   ");
2834cf49a43SJulian Elischer 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "  ");
2844cf49a43SJulian Elischer 		count -= BYPERLINE;
2854cf49a43SJulian Elischer 
2864cf49a43SJulian Elischer 		/* Do ASCII */
2874cf49a43SJulian Elischer 		for (k = 0; k < BYPERLINE; k++, count++)
2884cf49a43SJulian Elischer 			if (count < len)
2894cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
2904cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf),
2914cf49a43SJulian Elischer 				    "%c", isprint(ptr[k]) ? ptr[k] : '.');
2924cf49a43SJulian Elischer 			else
2934cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
2944cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "  ");
2954cf49a43SJulian Elischer 		count -= BYPERLINE;
2964cf49a43SJulian Elischer 
2974cf49a43SJulian Elischer 		/* Print it */
2984cf49a43SJulian Elischer 		NGLOGX("%s", buf);
2994cf49a43SJulian Elischer 	}
3004cf49a43SJulian Elischer }
3014cf49a43SJulian Elischer 
302