xref: /freebsd/lib/libnetgraph/debug.c (revision e489a907c49f80c9fe0e4738ec08e4f2407da3df)
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>
44f8307e12SArchie Cobbs 
454cf49a43SJulian Elischer #include <stdarg.h>
46f8307e12SArchie Cobbs 
47f8307e12SArchie Cobbs #include <netinet/in.h>
48f8307e12SArchie Cobbs #include <net/ethernet.h>
49f8307e12SArchie Cobbs 
504cf49a43SJulian Elischer #include <netgraph/ng_message.h>
514cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
524cf49a43SJulian Elischer 
534cf49a43SJulian Elischer #include "netgraph.h"
544cf49a43SJulian Elischer #include "internal.h"
554cf49a43SJulian Elischer 
56f8307e12SArchie Cobbs #include <netgraph/ng_UI.h>
574cf49a43SJulian Elischer #include <netgraph/ng_async.h>
58f8307e12SArchie Cobbs #include <netgraph/ng_cisco.h>
59f8307e12SArchie Cobbs #include <netgraph/ng_echo.h>
60f8307e12SArchie Cobbs #include <netgraph/ng_ether.h>
614cf49a43SJulian Elischer #include <netgraph/ng_frame_relay.h>
62f8307e12SArchie Cobbs #include <netgraph/ng_hole.h>
63f8307e12SArchie Cobbs #include <netgraph/ng_iface.h>
64f8307e12SArchie Cobbs #include <netgraph/ng_ksocket.h>
654cf49a43SJulian Elischer #include <netgraph/ng_lmi.h>
66f8307e12SArchie Cobbs #include <netgraph/ng_ppp.h>
67f8307e12SArchie Cobbs #include <netgraph/ng_pppoe.h>
68f8307e12SArchie Cobbs #include <netgraph/ng_rfc1490.h>
69f8307e12SArchie Cobbs #include <netgraph/ng_socket.h>
70f8307e12SArchie Cobbs #include <netgraph/ng_tee.h>
714cf49a43SJulian Elischer #include <netgraph/ng_tty.h>
72f8307e12SArchie Cobbs #include <netgraph/ng_vjc.h>
73f8307e12SArchie Cobbs #ifdef	WHISTLE
74f8307e12SArchie Cobbs #include <machine/../isa/df_def.h>
75f8307e12SArchie Cobbs #include <machine/../isa/if_wfra.h>
76f8307e12SArchie Cobbs #include <machine/../isa/ipac.h>
77f8307e12SArchie Cobbs #include <netgraph/ng_df.h>
78f8307e12SArchie Cobbs #include <netgraph/ng_ipac.h>
79f8307e12SArchie Cobbs #include <netgraph/ng_mppc.h>
80f8307e12SArchie Cobbs #include <netgraph/ng_pptpgre.h>
81f8307e12SArchie Cobbs #include <netgraph/ng_tn.h>
82f8307e12SArchie Cobbs #endif
834cf49a43SJulian Elischer 
844cf49a43SJulian Elischer /* Global debug level */
854cf49a43SJulian Elischer int     _gNgDebugLevel = 0;
864cf49a43SJulian Elischer 
874cf49a43SJulian Elischer /* Debug printing functions */
884cf49a43SJulian Elischer void    (*_NgLog) (const char *fmt,...) = warn;
894cf49a43SJulian Elischer void    (*_NgLogx) (const char *fmt,...) = warnx;
904cf49a43SJulian Elischer 
914cf49a43SJulian Elischer /* Internal functions */
924cf49a43SJulian Elischer static const	char *NgCookie(int cookie);
93f8307e12SArchie Cobbs 
94f8307e12SArchie Cobbs /* Known typecookie list */
95f8307e12SArchie Cobbs struct ng_cookie {
96f8307e12SArchie Cobbs 	int		cookie;
97f8307e12SArchie Cobbs 	const char	*type;
98f8307e12SArchie Cobbs };
99f8307e12SArchie Cobbs 
100f8307e12SArchie Cobbs #define COOKIE(c)	{ NGM_ ## c ## _COOKIE, #c }
101f8307e12SArchie Cobbs 
102f8307e12SArchie Cobbs /* List of known cookies */
103f8307e12SArchie Cobbs static const struct ng_cookie cookies[] = {
104f8307e12SArchie Cobbs 	COOKIE(UI),
105f8307e12SArchie Cobbs 	COOKIE(ASYNC),
106e489a907SArchie Cobbs 	COOKIE(BPF),
107f8307e12SArchie Cobbs 	COOKIE(CISCO),
108f8307e12SArchie Cobbs 	COOKIE(ECHO),
109f8307e12SArchie Cobbs 	COOKIE(ETHER),
110f8307e12SArchie Cobbs 	COOKIE(FRAMERELAY),
111f8307e12SArchie Cobbs 	COOKIE(GENERIC),
112f8307e12SArchie Cobbs 	COOKIE(HOLE),
113f8307e12SArchie Cobbs 	COOKIE(IFACE),
114f8307e12SArchie Cobbs 	COOKIE(KSOCKET),
115f8307e12SArchie Cobbs 	COOKIE(LMI),
116f8307e12SArchie Cobbs 	COOKIE(PPP),
117f8307e12SArchie Cobbs 	COOKIE(PPPOE),
118f8307e12SArchie Cobbs 	COOKIE(RFC1490),
119f8307e12SArchie Cobbs 	COOKIE(SOCKET),
120f8307e12SArchie Cobbs 	COOKIE(TEE),
121f8307e12SArchie Cobbs 	COOKIE(TTY),
122f8307e12SArchie Cobbs 	COOKIE(VJC),
123f8307e12SArchie Cobbs #ifdef WHISTLE
124f8307e12SArchie Cobbs 	COOKIE(DF),
125f8307e12SArchie Cobbs 	COOKIE(IPAC),
126f8307e12SArchie Cobbs 	COOKIE(MPPC),
127f8307e12SArchie Cobbs 	COOKIE(PPTPGRE),
128f8307e12SArchie Cobbs 	COOKIE(TN),
129f8307e12SArchie Cobbs 	COOKIE(WFRA),
130f8307e12SArchie Cobbs #endif
131f8307e12SArchie Cobbs 	{ 0, NULL }
132f8307e12SArchie Cobbs };
1334cf49a43SJulian Elischer 
1344cf49a43SJulian Elischer /*
1354cf49a43SJulian Elischer  * Set debug level, ie, verbosity, if "level" is non-negative.
1364cf49a43SJulian Elischer  * Returns old debug level.
1374cf49a43SJulian Elischer  */
1384cf49a43SJulian Elischer int
1394cf49a43SJulian Elischer NgSetDebug(int level)
1404cf49a43SJulian Elischer {
1414cf49a43SJulian Elischer 	int old = _gNgDebugLevel;
1424cf49a43SJulian Elischer 
1434cf49a43SJulian Elischer 	if (level < 0)
1444cf49a43SJulian Elischer 		level = old;
1454cf49a43SJulian Elischer 	_gNgDebugLevel = level;
1464cf49a43SJulian Elischer 	return (old);
1474cf49a43SJulian Elischer }
1484cf49a43SJulian Elischer 
1494cf49a43SJulian Elischer /*
1504cf49a43SJulian Elischer  * Set debug logging functions.
1514cf49a43SJulian Elischer  */
1524cf49a43SJulian Elischer void
1534cf49a43SJulian Elischer NgSetErrLog(void (*log) (const char *fmt,...),
1544cf49a43SJulian Elischer 		void (*logx) (const char *fmt,...))
1554cf49a43SJulian Elischer {
1564cf49a43SJulian Elischer 	_NgLog = log;
1574cf49a43SJulian Elischer 	_NgLogx = logx;
1584cf49a43SJulian Elischer }
1594cf49a43SJulian Elischer 
1604cf49a43SJulian Elischer /*
1614cf49a43SJulian Elischer  * Display a netgraph sockaddr
1624cf49a43SJulian Elischer  */
1634cf49a43SJulian Elischer void
164f8307e12SArchie Cobbs _NgDebugSockaddr(const struct sockaddr_ng *sg)
1654cf49a43SJulian Elischer {
1664cf49a43SJulian Elischer 	NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
1674cf49a43SJulian Elischer 	       sg->sg_family, sg->sg_len, sg->sg_data);
1684cf49a43SJulian Elischer }
1694cf49a43SJulian Elischer 
170f8307e12SArchie Cobbs #define ARGS_BUFSIZE	1024
171f8307e12SArchie Cobbs 
1724cf49a43SJulian Elischer /*
1734cf49a43SJulian Elischer  * Display a negraph message
1744cf49a43SJulian Elischer  */
1754cf49a43SJulian Elischer void
176f8307e12SArchie Cobbs _NgDebugMsg(const struct ng_mesg *msg, const char *path)
1774cf49a43SJulian Elischer {
178f8307e12SArchie Cobbs 	u_char buf[2 * sizeof(struct ng_mesg) + ARGS_BUFSIZE];
179f8307e12SArchie Cobbs 	struct ng_mesg *const req = (struct ng_mesg *)buf;
180f8307e12SArchie Cobbs 	struct ng_mesg *const bin = (struct ng_mesg *)req->data;
181f8307e12SArchie Cobbs 	int arglen, debugSave, csock = -1;
182f8307e12SArchie Cobbs 
183f8307e12SArchie Cobbs 	/* Lower debugging to avoid infinite recursion */
184f8307e12SArchie Cobbs 	debugSave = _gNgDebugLevel;
185f8307e12SArchie Cobbs 	_gNgDebugLevel -= 4;
186f8307e12SArchie Cobbs 
187f8307e12SArchie Cobbs 	/* Display header stuff */
1884cf49a43SJulian Elischer 	NGLOGX("NG_MESG :");
1894cf49a43SJulian Elischer 	NGLOGX("  vers   %d", msg->header.version);
1904cf49a43SJulian Elischer 	NGLOGX("  arglen %d", msg->header.arglen);
1914cf49a43SJulian Elischer 	NGLOGX("  flags  %ld", msg->header.flags);
1924cf49a43SJulian Elischer 	NGLOGX("  token  %lu", (u_long)msg->header.token);
193f8307e12SArchie Cobbs 	NGLOGX("  cookie %s (%d)",
194f8307e12SArchie Cobbs 	    NgCookie(msg->header.typecookie), msg->header.typecookie);
195f8307e12SArchie Cobbs 
196f8307e12SArchie Cobbs 	/* At lower debugging levels, skip ASCII translation */
197f8307e12SArchie Cobbs 	if (_gNgDebugLevel <= 2)
198f8307e12SArchie Cobbs 		goto fail2;
199f8307e12SArchie Cobbs 
200f8307e12SArchie Cobbs 	/* If path is not absolute, don't bother trying to use relative
201f8307e12SArchie Cobbs 	   address on a different socket for the ASCII translation */
202f8307e12SArchie Cobbs 	if (strchr(path, ':') == NULL)
203f8307e12SArchie Cobbs 		goto fail2;
204f8307e12SArchie Cobbs 
205f8307e12SArchie Cobbs 	/* Get a temporary socket */
206f8307e12SArchie Cobbs 	if (NgMkSockNode(NULL, &csock, NULL) < 0)
207f8307e12SArchie Cobbs 		goto fail;
208f8307e12SArchie Cobbs 
209f8307e12SArchie Cobbs 	/* Copy binary message into request message payload */
210f8307e12SArchie Cobbs 	arglen = msg->header.arglen;
211f8307e12SArchie Cobbs 	if (arglen > ARGS_BUFSIZE)
212f8307e12SArchie Cobbs 		arglen = ARGS_BUFSIZE;
213f8307e12SArchie Cobbs 	memcpy(bin, msg, sizeof(*msg) + arglen);
214f8307e12SArchie Cobbs 	bin->header.arglen = arglen;
215f8307e12SArchie Cobbs 
216f8307e12SArchie Cobbs 	/* Ask the node to translate the binary message to ASCII for us */
217f8307e12SArchie Cobbs 	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
218f8307e12SArchie Cobbs 	    NGM_BINARY2ASCII, bin, sizeof(*bin) + bin->header.arglen) < 0)
219f8307e12SArchie Cobbs 		goto fail;
220f8307e12SArchie Cobbs 	if (NgRecvMsg(csock, req, sizeof(buf), NULL) < 0)
221f8307e12SArchie Cobbs 		goto fail;
222f8307e12SArchie Cobbs 
223f8307e12SArchie Cobbs 	/* Display command string and arguments */
224f8307e12SArchie Cobbs 	NGLOGX("  cmd    %s (%d)", bin->header.cmdstr, bin->header.cmd);
225f8307e12SArchie Cobbs 	NGLOGX("  args   %s", bin->data);
226f8307e12SArchie Cobbs 	goto done;
227f8307e12SArchie Cobbs 
228f8307e12SArchie Cobbs fail:
229f8307e12SArchie Cobbs 	/* Just display binary version */
230f8307e12SArchie Cobbs 	NGLOGX("  [error decoding message: %s]", strerror(errno));
231f8307e12SArchie Cobbs fail2:
232f8307e12SArchie Cobbs 	NGLOGX("  cmd    %d", msg->header.cmd);
233f8307e12SArchie Cobbs 	NGLOGX("  args (%d bytes)", msg->header.arglen);
234f8307e12SArchie Cobbs 	_NgDebugBytes(msg->data, msg->header.arglen);
235f8307e12SArchie Cobbs 
236f8307e12SArchie Cobbs done:
237f8307e12SArchie Cobbs 	if (csock != -1)
238f8307e12SArchie Cobbs 		(void)close(csock);
239f8307e12SArchie Cobbs 	_gNgDebugLevel = debugSave;
2404cf49a43SJulian Elischer }
2414cf49a43SJulian Elischer 
2424cf49a43SJulian Elischer /*
2434cf49a43SJulian Elischer  * Return the name of the node type corresponding to the cookie
2444cf49a43SJulian Elischer  */
2454cf49a43SJulian Elischer static const char *
2464cf49a43SJulian Elischer NgCookie(int cookie)
2474cf49a43SJulian Elischer {
248f8307e12SArchie Cobbs 	int k;
2494cf49a43SJulian Elischer 
250f8307e12SArchie Cobbs 	for (k = 0; cookies[k].cookie != 0; k++) {
251f8307e12SArchie Cobbs 		if (cookies[k].cookie == cookie)
252f8307e12SArchie Cobbs 			return cookies[k].type;
2534cf49a43SJulian Elischer 	}
254f8307e12SArchie Cobbs 	return "??";
2554cf49a43SJulian Elischer }
2564cf49a43SJulian Elischer 
2574cf49a43SJulian Elischer /*
2584cf49a43SJulian Elischer  * Dump bytes in hex
2594cf49a43SJulian Elischer  */
2604cf49a43SJulian Elischer void
2614cf49a43SJulian Elischer _NgDebugBytes(const u_char *ptr, int len)
2624cf49a43SJulian Elischer {
2634cf49a43SJulian Elischer 	char    buf[100];
2644cf49a43SJulian Elischer 	int     k, count;
2654cf49a43SJulian Elischer 
2664cf49a43SJulian Elischer #define BYPERLINE	16
2674cf49a43SJulian Elischer 
2684cf49a43SJulian Elischer 	for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {
2694cf49a43SJulian Elischer 
2704cf49a43SJulian Elischer 		/* Do hex */
2714cf49a43SJulian Elischer 		snprintf(buf, sizeof(buf), "%04x:  ", count);
2724cf49a43SJulian Elischer 		for (k = 0; k < BYPERLINE; k++, count++)
2734cf49a43SJulian Elischer 			if (count < len)
2744cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
2754cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
2764cf49a43SJulian Elischer 			else
2774cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
2784cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "   ");
2794cf49a43SJulian Elischer 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "  ");
2804cf49a43SJulian Elischer 		count -= BYPERLINE;
2814cf49a43SJulian Elischer 
2824cf49a43SJulian Elischer 		/* Do ASCII */
2834cf49a43SJulian Elischer 		for (k = 0; k < BYPERLINE; k++, count++)
2844cf49a43SJulian Elischer 			if (count < len)
2854cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
2864cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf),
2874cf49a43SJulian Elischer 				    "%c", isprint(ptr[k]) ? ptr[k] : '.');
2884cf49a43SJulian Elischer 			else
2894cf49a43SJulian Elischer 				snprintf(buf + strlen(buf),
2904cf49a43SJulian Elischer 				    sizeof(buf) - strlen(buf), "  ");
2914cf49a43SJulian Elischer 		count -= BYPERLINE;
2924cf49a43SJulian Elischer 
2934cf49a43SJulian Elischer 		/* Print it */
2944cf49a43SJulian Elischer 		NGLOGX("%s", buf);
2954cf49a43SJulian Elischer 	}
2964cf49a43SJulian Elischer }
2974cf49a43SJulian Elischer 
298