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