4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * Subject to the following obligations and disclaimer of warranty, use and
8 * redistribution of this software, in source or object code forms, with or
9 * without modifications are expressly permitted by Whistle Communications;
10 * provided, however, that:
11 * 1. Any and all reproductions of the source or object code must include the
12 * copyright notice above and the following disclaimer of warranties; and
13 * 2. No rights are granted, in any manner or form, to use Whistle
14 * Communications, Inc. trademarks, including the mark "WHISTLE
15 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16 * such appears in the above copyright notice or in the software.
18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * Author: Archie Cobbs <archie@whistle.com>
38 * $Whistle: debug.c,v 1.24 1999/01/24 01:15:33 archie Exp $
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
46 #include <sys/ioctl.h>
50 #include <netinet/in.h>
51 #include <net/ethernet.h>
54 #include <netgraph/ng_message.h>
55 #include <netgraph/ng_socket.h>
60 #include <netgraph/ng_UI.h>
61 #include <netgraph/ng_async.h>
62 #include <netgraph/ng_atmllc.h>
63 #include <netgraph/ng_bpf.h>
64 #include <netgraph/ng_bridge.h>
65 #include <netgraph/ng_car.h>
66 #include <netgraph/ng_cisco.h>
67 #include <netgraph/ng_deflate.h>
68 #include <netgraph/ng_device.h>
69 #include <netgraph/ng_echo.h>
70 #include <netgraph/ng_eiface.h>
71 #include <netgraph/ng_etf.h>
72 #include <netgraph/ng_ether.h>
73 #include <netgraph/ng_fec.h>
74 #include <netgraph/ng_ether_echo.h>
75 #include <netgraph/ng_frame_relay.h>
76 #include <netgraph/ng_gif.h>
77 #include <netgraph/ng_gif_demux.h>
78 #include <netgraph/ng_hole.h>
79 #include <netgraph/ng_hub.h>
80 #include <netgraph/ng_iface.h>
81 #include <netgraph/ng_ip_input.h>
82 #include <netgraph/ng_ipfw.h>
83 #include <netgraph/ng_ksocket.h>
84 #include <netgraph/ng_l2tp.h>
85 #include <netgraph/ng_lmi.h>
86 #include <netgraph/ng_mppc.h>
87 #include <netgraph/ng_nat.h>
88 #include <netgraph/netflow/ng_netflow.h>
89 #include <netgraph/ng_one2many.h>
90 #include <netgraph/ng_patch.h>
91 #include <netgraph/ng_pipe.h>
92 #include <netgraph/ng_ppp.h>
93 #include <netgraph/ng_pppoe.h>
94 #include <netgraph/ng_pptpgre.h>
95 #include <netgraph/ng_pred1.h>
96 #include <netgraph/ng_rfc1490.h>
97 #include <netgraph/ng_socket.h>
98 #include <netgraph/ng_source.h>
99 #include <netgraph/ng_split.h>
100 #include <netgraph/ng_sppp.h>
101 #include <netgraph/ng_tag.h>
102 #include <netgraph/ng_tcpmss.h>
103 #include <netgraph/ng_tee.h>
104 #include <netgraph/ng_tty.h>
105 #include <netgraph/ng_vjc.h>
106 #include <netgraph/ng_vlan.h>
108 #include <machine/../isa/df_def.h>
109 #include <machine/../isa/if_wfra.h>
110 #include <machine/../isa/ipac.h>
111 #include <netgraph/ng_df.h>
112 #include <netgraph/ng_ipac.h>
113 #include <netgraph/ng_tn.h>
116 /* Global debug level */
117 int _gNgDebugLevel = 0;
119 /* Debug printing functions */
120 void (*_NgLog) (const char *fmt,...) = warn;
121 void (*_NgLogx) (const char *fmt,...) = warnx;
123 /* Internal functions */
124 static const char *NgCookie(int cookie);
126 /* Known typecookie list */
132 #define COOKIE(c) { NGM_ ## c ## _COOKIE, #c }
134 /* List of known cookies */
135 static const struct ng_cookie cookies[] = {
194 * Set debug level, ie, verbosity, if "level" is non-negative.
195 * Returns old debug level.
198 NgSetDebug(int level)
200 int old = _gNgDebugLevel;
203 _gNgDebugLevel = level;
208 * Set debug logging functions.
211 NgSetErrLog(void (*log) (const char *fmt,...),
212 void (*logx) (const char *fmt,...))
219 * Display a netgraph sockaddr
222 _NgDebugSockaddr(const struct sockaddr_ng *sg)
224 NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
225 sg->sg_family, sg->sg_len, sg->sg_data);
228 #define ARGS_BUFSIZE 2048
229 #define RECURSIVE_DEBUG_ADJUST 4
232 * Display a negraph message
235 _NgDebugMsg(const struct ng_mesg *msg, const char *path)
237 u_char buf[2 * sizeof(struct ng_mesg) + ARGS_BUFSIZE];
238 struct ng_mesg *const req = (struct ng_mesg *)buf;
239 struct ng_mesg *const bin = (struct ng_mesg *)req->data;
240 int arglen, csock = -1;
242 /* Display header stuff */
244 NGLOGX(" vers %d", msg->header.version);
245 NGLOGX(" arglen %u", msg->header.arglen);
246 NGLOGX(" flags %x", msg->header.flags);
247 NGLOGX(" token %u", msg->header.token);
248 NGLOGX(" cookie %s (%u)",
249 NgCookie(msg->header.typecookie), msg->header.typecookie);
251 /* At lower debugging levels, skip ASCII translation */
252 if (_gNgDebugLevel <= 2)
255 /* If path is not absolute, don't bother trying to use relative
256 address on a different socket for the ASCII translation */
257 if (strchr(path, ':') == NULL)
260 /* Get a temporary socket */
261 if (NgMkSockNode(NULL, &csock, NULL) < 0)
264 /* Copy binary message into request message payload */
265 arglen = msg->header.arglen;
266 if (arglen > ARGS_BUFSIZE)
267 arglen = ARGS_BUFSIZE;
268 memcpy(bin, msg, sizeof(*msg) + arglen);
269 bin->header.arglen = arglen;
271 /* Lower debugging to avoid infinite recursion */
272 _gNgDebugLevel -= RECURSIVE_DEBUG_ADJUST;
274 /* Ask the node to translate the binary message to ASCII for us */
275 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
276 NGM_BINARY2ASCII, bin, sizeof(*bin) + bin->header.arglen) < 0) {
277 _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
280 if (NgRecvMsg(csock, req, sizeof(buf), NULL) < 0) {
281 _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
285 /* Restore debugging level */
286 _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
288 /* Display command string and arguments */
289 NGLOGX(" cmd %s (%d)", bin->header.cmdstr, bin->header.cmd);
290 NGLOGX(" args %s", bin->data);
294 /* Just display binary version */
295 NGLOGX(" [error decoding message: %s]", strerror(errno));
297 NGLOGX(" cmd %d", msg->header.cmd);
298 NGLOGX(" args (%d bytes)", msg->header.arglen);
299 _NgDebugBytes((u_char *)msg->data, msg->header.arglen);
307 * Return the name of the node type corresponding to the cookie
314 for (k = 0; cookies[k].cookie != 0; k++) {
315 if (cookies[k].cookie == cookie)
316 return cookies[k].type;
325 _NgDebugBytes(const u_char *ptr, int len)
332 for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {
335 snprintf(buf, sizeof(buf), "%04x: ", count);
336 for (k = 0; k < BYPERLINE; k++, count++)
338 snprintf(buf + strlen(buf),
339 sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
341 snprintf(buf + strlen(buf),
342 sizeof(buf) - strlen(buf), " ");
343 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
347 for (k = 0; k < BYPERLINE; k++, count++)
349 snprintf(buf + strlen(buf),
350 sizeof(buf) - strlen(buf),
351 "%c", isprint(ptr[k]) ? ptr[k] : '.');
353 snprintf(buf + strlen(buf),
354 sizeof(buf) - strlen(buf), " ");