xref: /dragonfly/lib/libnetgraph7/debug.c (revision cfd1aba3)
1 /*
2  * debug.c
3  *
4  * Copyright (c) 1996-1999 Whistle Communications, Inc.
5  * All rights reserved.
6  *
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.
17  *
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
34  * OF SUCH DAMAGE.
35  *
36  * Author: Archie Cobbs <archie@whistle.com>
37  *
38  * $FreeBSD: src/lib/libnetgraph/debug.c,v 1.9 2005/10/25 20:58:30 ru Exp $
39  * $DragonFly: src/lib/libnetgraph/debug.c,v 1.4 2007/06/17 20:33:14 swildner Exp $
40  * $Whistle: debug.c,v 1.24 1999/01/24 01:15:33 archie Exp $
41  */
42 
43 #include <sys/types.h>
44 #include <sys/time.h>
45 #include <sys/ioctl.h>
46 
47 #include <stdarg.h>
48 
49 #include <netinet/in.h>
50 #include <net/ethernet.h>
51 #include <net/bpf.h>
52 
53 #include <netgraph7/ng_message.h>
54 #include <netgraph7/socket/ng_socket.h>
55 
56 #include "netgraph.h"
57 #include "internal.h"
58 
59 #include <netgraph7/UI/ng_UI.h>
60 #include <netgraph7/async/ng_async.h>
61 #include <netgraph7/atmllc/ng_atmllc.h>
62 #include <netgraph7/bpf/ng_bpf.h>
63 #include <netgraph/bridge/ng_bridge.h>
64 #include <netgraph7/cisco/ng_cisco.h>
65 #include <netgraph7/deflate/ng_deflate.h>
66 #include <netgraph7/echo/ng_echo.h>
67 /*
68 #include <netgraph7/eiface/ng_eiface.h>
69 */
70 #include <netgraph7/etf/ng_etf.h>
71 #include <netgraph7/ether/ng_ether.h>
72 /*
73 #include <netgraph7/fec/ng_fec.h>
74 */
75 #include <netgraph7/frame_relay/ng_frame_relay.h>
76 #include <netgraph7/hole/ng_hole.h>
77 #include <netgraph7/hub/ng_hub.h>
78 #include <netgraph7/iface/ng_iface.h>
79 /*
80 #include <netgraph7/ip_input/ng_ip_input.h>
81 */
82 #include <netgraph7/ksocket/ng_ksocket.h>
83 #include <netgraph7/l2tp/ng_l2tp.h>
84 #include <netgraph7/lmi/ng_lmi.h>
85 #include <netgraph7/mppc/ng_mppc.h>
86 #include <netgraph7/one2many/ng_one2many.h>
87 #include <netgraph7/ppp/ng_ppp.h>
88 #include <netgraph7/pppoe/ng_pppoe.h>
89 #include <netgraph7/pptpgre/ng_pptpgre.h>
90 #include <netgraph7/rfc1490/ng_rfc1490.h>
91 /*
92 #include <netgraph7/socket/ng_socket.h>
93 #include <netgraph7/source/ng_source.h>
94 #include <netgraph7/split/ng_split.h>
95 */
96 #include <netgraph7/tcpmss/ng_tcpmss.h>
97 #include <netgraph7/tee/ng_tee.h>
98 #include <netgraph7/tty/ng_tty.h>
99 #include <netgraph7/vjc/ng_vjc.h>
100 #ifdef	WHISTLE
101 #include <machine/../isa/df_def.h>
102 #include <machine/../isa/if_wfra.h>
103 #include <machine/../isa/ipac.h>
104 #include <netgraph/ng_df.h>
105 #include <netgraph/ng_ipac.h>
106 #include <netgraph/ng_tn.h>
107 #endif
108 
109 /* Global debug level */
110 int     _gNgDebugLevel = 0;
111 
112 /* Debug printing functions */
113 void    (*_NgLog) (const char *fmt,...) = warn;
114 void    (*_NgLogx) (const char *fmt,...) = warnx;
115 
116 /* Internal functions */
117 static const	char *NgCookie(int cookie);
118 
119 /* Known typecookie list */
120 struct ng_cookie {
121 	int		cookie;
122 	const char	*type;
123 };
124 
125 #define COOKIE(c)	{ NGM_ ## c ## _COOKIE, #c }
126 
127 /* List of known cookies */
128 static const struct ng_cookie cookies[] = {
129 	COOKIE(UI),
130 	COOKIE(ASYNC),
131 	COOKIE(ATMLLC),
132 	COOKIE(BPF),
133 	COOKIE(BRIDGE),
134 	COOKIE(CISCO),
135 	COOKIE(DEFLATE),
136 	COOKIE(ECHO),
137 /*
138 	COOKIE(EIFACE),
139 */
140 	COOKIE(ETF),
141 	COOKIE(ETHER),
142 /*
143 	COOKIE(FEC),
144 */
145 	COOKIE(FRAMERELAY),
146 	COOKIE(GENERIC),
147 	COOKIE(HOLE),
148 	COOKIE(HUB),
149 	COOKIE(IFACE),
150 /*
151 	COOKIE(IP_INPUT),
152 */
153 	COOKIE(KSOCKET),
154 	COOKIE(L2TP),
155 	COOKIE(LMI),
156 	COOKIE(MPPC),
157 	COOKIE(ONE2MANY),
158 	COOKIE(PPP),
159 	COOKIE(PPPOE),
160 	COOKIE(PPTPGRE),
161 	COOKIE(RFC1490),
162 	COOKIE(SOCKET),
163 /*
164 	COOKIE(SOURCE),
165 	COOKIE(SPLIT),
166 */
167 	COOKIE(TCPMSS),
168 	COOKIE(TEE),
169 	COOKIE(TTY),
170 	COOKIE(VJC),
171 #ifdef WHISTLE
172 	COOKIE(DF),
173 	COOKIE(IPAC),
174 	COOKIE(TN),
175 	COOKIE(WFRA),
176 #endif
177 	{ 0, NULL }
178 };
179 
180 /*
181  * Set debug level, ie, verbosity, if "level" is non-negative.
182  * Returns old debug level.
183  */
184 int
185 NgSetDebug(int level)
186 {
187 	int old = _gNgDebugLevel;
188 
189 	if (level < 0)
190 		level = old;
191 	_gNgDebugLevel = level;
192 	return (old);
193 }
194 
195 /*
196  * Set debug logging functions.
197  */
198 void
199 NgSetErrLog(void (*log) (const char *fmt,...),
200 		void (*logx) (const char *fmt,...))
201 {
202 	_NgLog = log;
203 	_NgLogx = logx;
204 }
205 
206 /*
207  * Display a netgraph sockaddr
208  */
209 void
210 _NgDebugSockaddr(const struct sockaddr_ng *sg)
211 {
212 	NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
213 	       sg->sg_family, sg->sg_len, sg->sg_data);
214 }
215 
216 #define ARGS_BUFSIZE		2048
217 #define RECURSIVE_DEBUG_ADJUST	4
218 
219 /*
220  * Display a negraph message
221  */
222 void
223 _NgDebugMsg(const struct ng_mesg *msg, const char *path)
224 {
225 	u_char buf[2 * sizeof(struct ng_mesg) + ARGS_BUFSIZE];
226 	struct ng_mesg *const req = (struct ng_mesg *)buf;
227 	struct ng_mesg *const bin = (struct ng_mesg *)req->data;
228 	int arglen, csock = -1;
229 
230 	/* Display header stuff */
231 	NGLOGX("NG_MESG :");
232 	NGLOGX("  vers   %d", msg->header.version);
233 	NGLOGX("  arglen %d", msg->header.arglen);
234 	NGLOGX("  flags  %ld", msg->header.flags);
235 	NGLOGX("  token  %lu", (u_long)msg->header.token);
236 	NGLOGX("  cookie %s (%d)",
237 	    NgCookie(msg->header.typecookie), msg->header.typecookie);
238 
239 	/* At lower debugging levels, skip ASCII translation */
240 	if (_gNgDebugLevel <= 2)
241 		goto fail2;
242 
243 	/* If path is not absolute, don't bother trying to use relative
244 	   address on a different socket for the ASCII translation */
245 	if (strchr(path, ':') == NULL)
246 		goto fail2;
247 
248 	/* Get a temporary socket */
249 	if (NgMkSockNode(NULL, &csock, NULL) < 0)
250 		goto fail;
251 
252 	/* Copy binary message into request message payload */
253 	arglen = msg->header.arglen;
254 	if (arglen > ARGS_BUFSIZE)
255 		arglen = ARGS_BUFSIZE;
256 	memcpy(bin, msg, sizeof(*msg) + arglen);
257 	bin->header.arglen = arglen;
258 
259 	/* Lower debugging to avoid infinite recursion */
260 	_gNgDebugLevel -= RECURSIVE_DEBUG_ADJUST;
261 
262 	/* Ask the node to translate the binary message to ASCII for us */
263 	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
264 	    NGM_BINARY2ASCII, bin, sizeof(*bin) + bin->header.arglen) < 0) {
265 		_gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
266 		goto fail;
267 	}
268 	if (NgRecvMsg(csock, req, sizeof(buf), NULL) < 0) {
269 		_gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
270 		goto fail;
271 	}
272 
273 	/* Restore debugging level */
274 	_gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
275 
276 	/* Display command string and arguments */
277 	NGLOGX("  cmd    %s (%d)", bin->header.cmdstr, bin->header.cmd);
278 	NGLOGX("  args   %s", bin->data);
279 	goto done;
280 
281 fail:
282 	/* Just display binary version */
283 	NGLOGX("  [error decoding message: %s]", strerror(errno));
284 fail2:
285 	NGLOGX("  cmd    %d", msg->header.cmd);
286 	NGLOGX("  args (%d bytes)", msg->header.arglen);
287 	_NgDebugBytes(msg->data, msg->header.arglen);
288 
289 done:
290 	if (csock != -1)
291 		(void)close(csock);
292 }
293 
294 /*
295  * Return the name of the node type corresponding to the cookie
296  */
297 static const char *
298 NgCookie(int cookie)
299 {
300 	int k;
301 
302 	for (k = 0; cookies[k].cookie != 0; k++) {
303 		if (cookies[k].cookie == cookie)
304 			return cookies[k].type;
305 	}
306 	return "??";
307 }
308 
309 /*
310  * Dump bytes in hex
311  */
312 void
313 _NgDebugBytes(const u_char *ptr, int len)
314 {
315 	char    buf[100];
316 	int     k, count;
317 
318 #define BYPERLINE	16
319 
320 	for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {
321 
322 		/* Do hex */
323 		snprintf(buf, sizeof(buf), "%04x:  ", count);
324 		for (k = 0; k < BYPERLINE; k++, count++)
325 			if (count < len)
326 				snprintf(buf + strlen(buf),
327 				    sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
328 			else
329 				snprintf(buf + strlen(buf),
330 				    sizeof(buf) - strlen(buf), "   ");
331 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "  ");
332 		count -= BYPERLINE;
333 
334 		/* Do ASCII */
335 		for (k = 0; k < BYPERLINE; k++, count++)
336 			if (count < len)
337 				snprintf(buf + strlen(buf),
338 				    sizeof(buf) - strlen(buf),
339 				    "%c", isprint(ptr[k]) ? ptr[k] : '.');
340 			else
341 				snprintf(buf + strlen(buf),
342 				    sizeof(buf) - strlen(buf), "  ");
343 		count -= BYPERLINE;
344 
345 		/* Print it */
346 		NGLOGX("%s", buf);
347 	}
348 }
349 
350