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