xref: /netbsd-src/external/bsd/tcpdump/dist/print-l2tp.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*
2  * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * L2TP support contributed by Motonori Shindo (mshindo@mshindo.net)
22  */
23 
24 #include <sys/cdefs.h>
25 #ifndef lint
26 __RCSID("$NetBSD: print-l2tp.c,v 1.7 2017/02/05 04:05:05 spz Exp $");
27 #endif
28 
29 /* \summary: Layer Two Tunneling Protocol (L2TP) printer */
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <netdissect-stdinc.h>
36 
37 #include "netdissect.h"
38 #include "extract.h"
39 
40 #define L2TP_FLAG_TYPE		0x8000	/* Type (0=Data, 1=Control) */
41 #define L2TP_FLAG_LENGTH	0x4000	/* Length */
42 #define L2TP_FLAG_SEQUENCE	0x0800	/* Sequence */
43 #define L2TP_FLAG_OFFSET	0x0200	/* Offset */
44 #define L2TP_FLAG_PRIORITY	0x0100	/* Priority */
45 
46 #define L2TP_VERSION_MASK	0x000f	/* Version Mask */
47 #define L2TP_VERSION_L2F	0x0001	/* L2F */
48 #define L2TP_VERSION_L2TP	0x0002	/* L2TP */
49 
50 #define L2TP_AVP_HDR_FLAG_MANDATORY	0x8000	/* Mandatory Flag */
51 #define L2TP_AVP_HDR_FLAG_HIDDEN	0x4000	/* Hidden Flag */
52 #define L2TP_AVP_HDR_LEN_MASK		0x03ff	/* Length Mask */
53 
54 #define L2TP_FRAMING_CAP_SYNC_MASK	0x00000001	/* Synchronous */
55 #define L2TP_FRAMING_CAP_ASYNC_MASK	0x00000002	/* Asynchronous */
56 
57 #define L2TP_FRAMING_TYPE_SYNC_MASK	0x00000001	/* Synchronous */
58 #define L2TP_FRAMING_TYPE_ASYNC_MASK	0x00000002	/* Asynchronous */
59 
60 #define L2TP_BEARER_CAP_DIGITAL_MASK	0x00000001	/* Digital */
61 #define L2TP_BEARER_CAP_ANALOG_MASK	0x00000002	/* Analog */
62 
63 #define L2TP_BEARER_TYPE_DIGITAL_MASK	0x00000001	/* Digital */
64 #define L2TP_BEARER_TYPE_ANALOG_MASK	0x00000002	/* Analog */
65 
66 /* Authen Type */
67 #define L2TP_AUTHEN_TYPE_RESERVED	0x0000	/* Reserved */
68 #define L2TP_AUTHEN_TYPE_TEXTUAL	0x0001	/* Textual username/password exchange */
69 #define L2TP_AUTHEN_TYPE_CHAP		0x0002	/* PPP CHAP */
70 #define L2TP_AUTHEN_TYPE_PAP		0x0003	/* PPP PAP */
71 #define L2TP_AUTHEN_TYPE_NO_AUTH	0x0004	/* No Authentication */
72 #define L2TP_AUTHEN_TYPE_MSCHAPv1	0x0005	/* MSCHAPv1 */
73 
74 #define L2TP_PROXY_AUTH_ID_MASK		0x00ff
75 
76 static const char tstr[] = " [|l2tp]";
77 
78 #define	L2TP_MSGTYPE_SCCRQ	1  /* Start-Control-Connection-Request */
79 #define	L2TP_MSGTYPE_SCCRP	2  /* Start-Control-Connection-Reply */
80 #define	L2TP_MSGTYPE_SCCCN	3  /* Start-Control-Connection-Connected */
81 #define	L2TP_MSGTYPE_STOPCCN	4  /* Stop-Control-Connection-Notification */
82 #define	L2TP_MSGTYPE_HELLO	6  /* Hello */
83 #define	L2TP_MSGTYPE_OCRQ	7  /* Outgoing-Call-Request */
84 #define	L2TP_MSGTYPE_OCRP	8  /* Outgoing-Call-Reply */
85 #define	L2TP_MSGTYPE_OCCN	9  /* Outgoing-Call-Connected */
86 #define	L2TP_MSGTYPE_ICRQ	10 /* Incoming-Call-Request */
87 #define	L2TP_MSGTYPE_ICRP	11 /* Incoming-Call-Reply */
88 #define	L2TP_MSGTYPE_ICCN	12 /* Incoming-Call-Connected */
89 #define	L2TP_MSGTYPE_CDN	14 /* Call-Disconnect-Notify */
90 #define	L2TP_MSGTYPE_WEN	15 /* WAN-Error-Notify */
91 #define	L2TP_MSGTYPE_SLI	16 /* Set-Link-Info */
92 
93 static const struct tok l2tp_msgtype2str[] = {
94 	{ L2TP_MSGTYPE_SCCRQ, 	"SCCRQ" },
95 	{ L2TP_MSGTYPE_SCCRP,	"SCCRP" },
96 	{ L2TP_MSGTYPE_SCCCN,	"SCCCN" },
97 	{ L2TP_MSGTYPE_STOPCCN,	"StopCCN" },
98 	{ L2TP_MSGTYPE_HELLO,	"HELLO" },
99 	{ L2TP_MSGTYPE_OCRQ,	"OCRQ" },
100 	{ L2TP_MSGTYPE_OCRP,	"OCRP" },
101 	{ L2TP_MSGTYPE_OCCN,	"OCCN" },
102 	{ L2TP_MSGTYPE_ICRQ,	"ICRQ" },
103 	{ L2TP_MSGTYPE_ICRP,	"ICRP" },
104 	{ L2TP_MSGTYPE_ICCN,	"ICCN" },
105 	{ L2TP_MSGTYPE_CDN,	"CDN" },
106 	{ L2TP_MSGTYPE_WEN,	"WEN" },
107 	{ L2TP_MSGTYPE_SLI,	"SLI" },
108 	{ 0,			NULL }
109 };
110 
111 #define L2TP_AVP_MSGTYPE		0  /* Message Type */
112 #define L2TP_AVP_RESULT_CODE		1  /* Result Code */
113 #define L2TP_AVP_PROTO_VER		2  /* Protocol Version */
114 #define L2TP_AVP_FRAMING_CAP		3  /* Framing Capabilities */
115 #define L2TP_AVP_BEARER_CAP		4  /* Bearer Capabilities */
116 #define L2TP_AVP_TIE_BREAKER		5  /* Tie Breaker */
117 #define L2TP_AVP_FIRM_VER		6  /* Firmware Revision */
118 #define L2TP_AVP_HOST_NAME		7  /* Host Name */
119 #define L2TP_AVP_VENDOR_NAME		8  /* Vendor Name */
120 #define L2TP_AVP_ASSND_TUN_ID 		9  /* Assigned Tunnel ID */
121 #define L2TP_AVP_RECV_WIN_SIZE		10 /* Receive Window Size */
122 #define L2TP_AVP_CHALLENGE		11 /* Challenge */
123 #define L2TP_AVP_Q931_CC		12 /* Q.931 Cause Code */
124 #define L2TP_AVP_CHALLENGE_RESP		13 /* Challenge Response */
125 #define L2TP_AVP_ASSND_SESS_ID  	14 /* Assigned Session ID */
126 #define L2TP_AVP_CALL_SER_NUM 		15 /* Call Serial Number */
127 #define L2TP_AVP_MINIMUM_BPS		16 /* Minimum BPS */
128 #define L2TP_AVP_MAXIMUM_BPS		17 /* Maximum BPS */
129 #define L2TP_AVP_BEARER_TYPE		18 /* Bearer Type */
130 #define L2TP_AVP_FRAMING_TYPE 		19 /* Framing Type */
131 #define L2TP_AVP_PACKET_PROC_DELAY	20 /* Packet Processing Delay (OBSOLETE) */
132 #define L2TP_AVP_CALLED_NUMBER		21 /* Called Number */
133 #define L2TP_AVP_CALLING_NUMBER		22 /* Calling Number */
134 #define L2TP_AVP_SUB_ADDRESS		23 /* Sub-Address */
135 #define L2TP_AVP_TX_CONN_SPEED		24 /* (Tx) Connect Speed */
136 #define L2TP_AVP_PHY_CHANNEL_ID		25 /* Physical Channel ID */
137 #define L2TP_AVP_INI_RECV_LCP		26 /* Initial Received LCP CONFREQ */
138 #define L2TP_AVP_LAST_SENT_LCP		27 /* Last Sent LCP CONFREQ */
139 #define L2TP_AVP_LAST_RECV_LCP		28 /* Last Received LCP CONFREQ */
140 #define L2TP_AVP_PROXY_AUTH_TYPE	29 /* Proxy Authen Type */
141 #define L2TP_AVP_PROXY_AUTH_NAME	30 /* Proxy Authen Name */
142 #define L2TP_AVP_PROXY_AUTH_CHAL	31 /* Proxy Authen Challenge */
143 #define L2TP_AVP_PROXY_AUTH_ID		32 /* Proxy Authen ID */
144 #define L2TP_AVP_PROXY_AUTH_RESP	33 /* Proxy Authen Response */
145 #define L2TP_AVP_CALL_ERRORS		34 /* Call Errors */
146 #define L2TP_AVP_ACCM			35 /* ACCM */
147 #define L2TP_AVP_RANDOM_VECTOR		36 /* Random Vector */
148 #define L2TP_AVP_PRIVATE_GRP_ID		37 /* Private Group ID */
149 #define L2TP_AVP_RX_CONN_SPEED		38 /* (Rx) Connect Speed */
150 #define L2TP_AVP_SEQ_REQUIRED 		39 /* Sequencing Required */
151 #define L2TP_AVP_PPP_DISCON_CC		46 /* PPP Disconnect Cause Code */
152 
153 static const struct tok l2tp_avp2str[] = {
154 	{ L2TP_AVP_MSGTYPE,		"MSGTYPE" },
155 	{ L2TP_AVP_RESULT_CODE,		"RESULT_CODE" },
156 	{ L2TP_AVP_PROTO_VER,		"PROTO_VER" },
157 	{ L2TP_AVP_FRAMING_CAP,		"FRAMING_CAP" },
158 	{ L2TP_AVP_BEARER_CAP,		"BEARER_CAP" },
159 	{ L2TP_AVP_TIE_BREAKER,		"TIE_BREAKER" },
160 	{ L2TP_AVP_FIRM_VER,		"FIRM_VER" },
161 	{ L2TP_AVP_HOST_NAME,		"HOST_NAME" },
162 	{ L2TP_AVP_VENDOR_NAME,		"VENDOR_NAME" },
163 	{ L2TP_AVP_ASSND_TUN_ID,	"ASSND_TUN_ID" },
164 	{ L2TP_AVP_RECV_WIN_SIZE,	"RECV_WIN_SIZE" },
165 	{ L2TP_AVP_CHALLENGE,		"CHALLENGE" },
166 	{ L2TP_AVP_Q931_CC,		"Q931_CC", },
167 	{ L2TP_AVP_CHALLENGE_RESP,	"CHALLENGE_RESP" },
168 	{ L2TP_AVP_ASSND_SESS_ID,	"ASSND_SESS_ID" },
169 	{ L2TP_AVP_CALL_SER_NUM,	"CALL_SER_NUM" },
170 	{ L2TP_AVP_MINIMUM_BPS,		"MINIMUM_BPS" },
171 	{ L2TP_AVP_MAXIMUM_BPS,		"MAXIMUM_BPS" },
172 	{ L2TP_AVP_BEARER_TYPE,		"BEARER_TYPE" },
173 	{ L2TP_AVP_FRAMING_TYPE,	"FRAMING_TYPE" },
174 	{ L2TP_AVP_PACKET_PROC_DELAY,	"PACKET_PROC_DELAY" },
175 	{ L2TP_AVP_CALLED_NUMBER,	"CALLED_NUMBER" },
176 	{ L2TP_AVP_CALLING_NUMBER,	"CALLING_NUMBER" },
177 	{ L2TP_AVP_SUB_ADDRESS,		"SUB_ADDRESS" },
178 	{ L2TP_AVP_TX_CONN_SPEED,	"TX_CONN_SPEED" },
179 	{ L2TP_AVP_PHY_CHANNEL_ID,	"PHY_CHANNEL_ID" },
180 	{ L2TP_AVP_INI_RECV_LCP,	"INI_RECV_LCP" },
181 	{ L2TP_AVP_LAST_SENT_LCP,	"LAST_SENT_LCP" },
182 	{ L2TP_AVP_LAST_RECV_LCP,	"LAST_RECV_LCP" },
183 	{ L2TP_AVP_PROXY_AUTH_TYPE,	"PROXY_AUTH_TYPE" },
184 	{ L2TP_AVP_PROXY_AUTH_NAME,	"PROXY_AUTH_NAME" },
185 	{ L2TP_AVP_PROXY_AUTH_CHAL,	"PROXY_AUTH_CHAL" },
186 	{ L2TP_AVP_PROXY_AUTH_ID,	"PROXY_AUTH_ID" },
187 	{ L2TP_AVP_PROXY_AUTH_RESP,	"PROXY_AUTH_RESP" },
188 	{ L2TP_AVP_CALL_ERRORS,		"CALL_ERRORS" },
189 	{ L2TP_AVP_ACCM,		"ACCM" },
190 	{ L2TP_AVP_RANDOM_VECTOR,	"RANDOM_VECTOR" },
191 	{ L2TP_AVP_PRIVATE_GRP_ID,	"PRIVATE_GRP_ID" },
192 	{ L2TP_AVP_RX_CONN_SPEED,	"RX_CONN_SPEED" },
193 	{ L2TP_AVP_SEQ_REQUIRED,	"SEQ_REQUIRED" },
194 	{ L2TP_AVP_PPP_DISCON_CC,	"PPP_DISCON_CC" },
195 	{ 0,				NULL }
196 };
197 
198 static const struct tok l2tp_authentype2str[] = {
199 	{ L2TP_AUTHEN_TYPE_RESERVED,	"Reserved" },
200 	{ L2TP_AUTHEN_TYPE_TEXTUAL,	"Textual" },
201 	{ L2TP_AUTHEN_TYPE_CHAP,	"CHAP" },
202 	{ L2TP_AUTHEN_TYPE_PAP,		"PAP" },
203 	{ L2TP_AUTHEN_TYPE_NO_AUTH,	"No Auth" },
204 	{ L2TP_AUTHEN_TYPE_MSCHAPv1,	"MS-CHAPv1" },
205 	{ 0,				NULL }
206 };
207 
208 #define L2TP_PPP_DISCON_CC_DIRECTION_GLOBAL	0
209 #define L2TP_PPP_DISCON_CC_DIRECTION_AT_PEER	1
210 #define L2TP_PPP_DISCON_CC_DIRECTION_AT_LOCAL	2
211 
212 static const struct tok l2tp_cc_direction2str[] = {
213 	{ L2TP_PPP_DISCON_CC_DIRECTION_GLOBAL,	"global error" },
214 	{ L2TP_PPP_DISCON_CC_DIRECTION_AT_PEER,	"at peer" },
215 	{ L2TP_PPP_DISCON_CC_DIRECTION_AT_LOCAL,"at local" },
216 	{ 0,					NULL }
217 };
218 
219 #if 0
220 static char *l2tp_result_code_StopCCN[] = {
221          "Reserved",
222          "General request to clear control connection",
223          "General error--Error Code indicates the problem",
224          "Control channel already exists",
225          "Requester is not authorized to establish a control channel",
226          "The protocol version of the requester is not supported",
227          "Requester is being shut down",
228          "Finite State Machine error"
229 #define L2TP_MAX_RESULT_CODE_STOPCC_INDEX	8
230 };
231 #endif
232 
233 #if 0
234 static char *l2tp_result_code_CDN[] = {
235 	"Reserved",
236 	"Call disconnected due to loss of carrier",
237 	"Call disconnected for the reason indicated in error code",
238 	"Call disconnected for administrative reasons",
239 	"Call failed due to lack of appropriate facilities being " \
240 	"available (temporary condition)",
241 	"Call failed due to lack of appropriate facilities being " \
242 	"available (permanent condition)",
243 	"Invalid destination",
244 	"Call failed due to no carrier detected",
245 	"Call failed due to detection of a busy signal",
246 	"Call failed due to lack of a dial tone",
247 	"Call was not established within time allotted by LAC",
248 	"Call was connected but no appropriate framing was detected"
249 #define L2TP_MAX_RESULT_CODE_CDN_INDEX	12
250 };
251 #endif
252 
253 #if 0
254 static char *l2tp_error_code_general[] = {
255 	"No general error",
256 	"No control connection exists yet for this LAC-LNS pair",
257 	"Length is wrong",
258 	"One of the field values was out of range or " \
259 	"reserved field was non-zero"
260 	"Insufficient resources to handle this operation now",
261 	"The Session ID is invalid in this context",
262 	"A generic vendor-specific error occurred in the LAC",
263 	"Try another"
264 #define L2TP_MAX_ERROR_CODE_GENERAL_INDEX	8
265 };
266 #endif
267 
268 /******************************/
269 /* generic print out routines */
270 /******************************/
271 static void
272 print_string(netdissect_options *ndo, const u_char *dat, u_int length)
273 {
274 	u_int i;
275 	for (i=0; i<length; i++) {
276 		ND_PRINT((ndo, "%c", *dat++));
277 	}
278 }
279 
280 static void
281 print_octets(netdissect_options *ndo, const u_char *dat, u_int length)
282 {
283 	u_int i;
284 	for (i=0; i<length; i++) {
285 		ND_PRINT((ndo, "%02x", *dat++));
286 	}
287 }
288 
289 static void
290 print_16bits_val(netdissect_options *ndo, const uint16_t *dat)
291 {
292 	ND_PRINT((ndo, "%u", EXTRACT_16BITS(dat)));
293 }
294 
295 static void
296 print_32bits_val(netdissect_options *ndo, const uint32_t *dat)
297 {
298 	ND_PRINT((ndo, "%lu", (u_long)EXTRACT_32BITS(dat)));
299 }
300 
301 /***********************************/
302 /* AVP-specific print out routines */
303 /***********************************/
304 static void
305 l2tp_msgtype_print(netdissect_options *ndo, const u_char *dat)
306 {
307 	const uint16_t *ptr = (const uint16_t *)dat;
308 
309 	ND_PRINT((ndo, "%s", tok2str(l2tp_msgtype2str, "MSGTYPE-#%u",
310 	    EXTRACT_16BITS(ptr))));
311 }
312 
313 static void
314 l2tp_result_code_print(netdissect_options *ndo, const u_char *dat, u_int length)
315 {
316 	const uint16_t *ptr = (const uint16_t *)dat;
317 
318 	ND_PRINT((ndo, "%u", EXTRACT_16BITS(ptr))); ptr++;	/* Result Code */
319 	if (length > 2) {				/* Error Code (opt) */
320 	        ND_PRINT((ndo, "/%u", EXTRACT_16BITS(ptr))); ptr++;
321 	}
322 	if (length > 4) {				/* Error Message (opt) */
323 		ND_PRINT((ndo, " "));
324 		print_string(ndo, (const u_char *)ptr, length - 4);
325 	}
326 }
327 
328 static void
329 l2tp_proto_ver_print(netdissect_options *ndo, const uint16_t *dat)
330 {
331 	ND_PRINT((ndo, "%u.%u", (EXTRACT_16BITS(dat) >> 8),
332 	    (EXTRACT_16BITS(dat) & 0xff)));
333 }
334 
335 static void
336 l2tp_framing_cap_print(netdissect_options *ndo, const u_char *dat)
337 {
338 	const uint32_t *ptr = (const uint32_t *)dat;
339 
340 	if (EXTRACT_32BITS(ptr) &  L2TP_FRAMING_CAP_ASYNC_MASK) {
341 		ND_PRINT((ndo, "A"));
342 	}
343 	if (EXTRACT_32BITS(ptr) &  L2TP_FRAMING_CAP_SYNC_MASK) {
344 		ND_PRINT((ndo, "S"));
345 	}
346 }
347 
348 static void
349 l2tp_bearer_cap_print(netdissect_options *ndo, const u_char *dat)
350 {
351 	const uint32_t *ptr = (const uint32_t *)dat;
352 
353 	if (EXTRACT_32BITS(ptr) &  L2TP_BEARER_CAP_ANALOG_MASK) {
354 		ND_PRINT((ndo, "A"));
355 	}
356 	if (EXTRACT_32BITS(ptr) &  L2TP_BEARER_CAP_DIGITAL_MASK) {
357 		ND_PRINT((ndo, "D"));
358 	}
359 }
360 
361 static void
362 l2tp_q931_cc_print(netdissect_options *ndo, const u_char *dat, u_int length)
363 {
364 	print_16bits_val(ndo, (const uint16_t *)dat);
365 	ND_PRINT((ndo, ", %02x", dat[2]));
366 	if (length > 3) {
367 		ND_PRINT((ndo, " "));
368 		print_string(ndo, dat+3, length-3);
369 	}
370 }
371 
372 static void
373 l2tp_bearer_type_print(netdissect_options *ndo, const u_char *dat)
374 {
375 	const uint32_t *ptr = (const uint32_t *)dat;
376 
377 	if (EXTRACT_32BITS(ptr) &  L2TP_BEARER_TYPE_ANALOG_MASK) {
378 		ND_PRINT((ndo, "A"));
379 	}
380 	if (EXTRACT_32BITS(ptr) &  L2TP_BEARER_TYPE_DIGITAL_MASK) {
381 		ND_PRINT((ndo, "D"));
382 	}
383 }
384 
385 static void
386 l2tp_framing_type_print(netdissect_options *ndo, const u_char *dat)
387 {
388 	const uint32_t *ptr = (const uint32_t *)dat;
389 
390 	if (EXTRACT_32BITS(ptr) &  L2TP_FRAMING_TYPE_ASYNC_MASK) {
391 		ND_PRINT((ndo, "A"));
392 	}
393 	if (EXTRACT_32BITS(ptr) &  L2TP_FRAMING_TYPE_SYNC_MASK) {
394 		ND_PRINT((ndo, "S"));
395 	}
396 }
397 
398 static void
399 l2tp_packet_proc_delay_print(netdissect_options *ndo)
400 {
401 	ND_PRINT((ndo, "obsolete"));
402 }
403 
404 static void
405 l2tp_proxy_auth_type_print(netdissect_options *ndo, const u_char *dat)
406 {
407 	const uint16_t *ptr = (const uint16_t *)dat;
408 
409 	ND_PRINT((ndo, "%s", tok2str(l2tp_authentype2str,
410 			     "AuthType-#%u", EXTRACT_16BITS(ptr))));
411 }
412 
413 static void
414 l2tp_proxy_auth_id_print(netdissect_options *ndo, const u_char *dat)
415 {
416 	const uint16_t *ptr = (const uint16_t *)dat;
417 
418 	ND_PRINT((ndo, "%u", EXTRACT_16BITS(ptr) & L2TP_PROXY_AUTH_ID_MASK));
419 }
420 
421 static void
422 l2tp_call_errors_print(netdissect_options *ndo, const u_char *dat)
423 {
424 	const uint16_t *ptr = (const uint16_t *)dat;
425 	uint16_t val_h, val_l;
426 
427 	ptr++;		/* skip "Reserved" */
428 
429 	val_h = EXTRACT_16BITS(ptr); ptr++;
430 	val_l = EXTRACT_16BITS(ptr); ptr++;
431 	ND_PRINT((ndo, "CRCErr=%u ", (val_h<<16) + val_l));
432 
433 	val_h = EXTRACT_16BITS(ptr); ptr++;
434 	val_l = EXTRACT_16BITS(ptr); ptr++;
435 	ND_PRINT((ndo, "FrameErr=%u ", (val_h<<16) + val_l));
436 
437 	val_h = EXTRACT_16BITS(ptr); ptr++;
438 	val_l = EXTRACT_16BITS(ptr); ptr++;
439 	ND_PRINT((ndo, "HardOver=%u ", (val_h<<16) + val_l));
440 
441 	val_h = EXTRACT_16BITS(ptr); ptr++;
442 	val_l = EXTRACT_16BITS(ptr); ptr++;
443 	ND_PRINT((ndo, "BufOver=%u ", (val_h<<16) + val_l));
444 
445 	val_h = EXTRACT_16BITS(ptr); ptr++;
446 	val_l = EXTRACT_16BITS(ptr); ptr++;
447 	ND_PRINT((ndo, "Timeout=%u ", (val_h<<16) + val_l));
448 
449 	val_h = EXTRACT_16BITS(ptr); ptr++;
450 	val_l = EXTRACT_16BITS(ptr); ptr++;
451 	ND_PRINT((ndo, "AlignErr=%u ", (val_h<<16) + val_l));
452 }
453 
454 static void
455 l2tp_accm_print(netdissect_options *ndo, const u_char *dat)
456 {
457 	const uint16_t *ptr = (const uint16_t *)dat;
458 	uint16_t val_h, val_l;
459 
460 	ptr++;		/* skip "Reserved" */
461 
462 	val_h = EXTRACT_16BITS(ptr); ptr++;
463 	val_l = EXTRACT_16BITS(ptr); ptr++;
464 	ND_PRINT((ndo, "send=%08x ", (val_h<<16) + val_l));
465 
466 	val_h = EXTRACT_16BITS(ptr); ptr++;
467 	val_l = EXTRACT_16BITS(ptr); ptr++;
468 	ND_PRINT((ndo, "recv=%08x ", (val_h<<16) + val_l));
469 }
470 
471 static void
472 l2tp_ppp_discon_cc_print(netdissect_options *ndo, const u_char *dat, u_int length)
473 {
474 	const uint16_t *ptr = (const uint16_t *)dat;
475 
476 	ND_PRINT((ndo, "%04x, ", EXTRACT_16BITS(ptr))); ptr++;	/* Disconnect Code */
477 	ND_PRINT((ndo, "%04x ",  EXTRACT_16BITS(ptr))); ptr++;	/* Control Protocol Number */
478 	ND_PRINT((ndo, "%s", tok2str(l2tp_cc_direction2str,
479 			     "Direction-#%u", *((const u_char *)ptr++))));
480 
481 	if (length > 5) {
482 		ND_PRINT((ndo, " "));
483 		print_string(ndo, (const u_char *)ptr, length-5);
484 	}
485 }
486 
487 static void
488 l2tp_avp_print(netdissect_options *ndo, const u_char *dat, int length)
489 {
490 	u_int len;
491 	const uint16_t *ptr = (const uint16_t *)dat;
492 	uint16_t attr_type;
493 	int hidden = FALSE;
494 
495 	if (length <= 0) {
496 		return;
497 	}
498 
499 	ND_PRINT((ndo, " "));
500 
501 	ND_TCHECK(*ptr);	/* Flags & Length */
502 	len = EXTRACT_16BITS(ptr) & L2TP_AVP_HDR_LEN_MASK;
503 
504 	/* If it is not long enough to contain the header, we'll give up. */
505 	if (len < 6)
506 		goto trunc;
507 
508 	/* If it goes past the end of the remaining length of the packet,
509 	   we'll give up. */
510 	if (len > (u_int)length)
511 		goto trunc;
512 
513 	/* If it goes past the end of the remaining length of the captured
514 	   data, we'll give up. */
515 	ND_TCHECK2(*ptr, len);
516 	/* After this point, no need to worry about truncation */
517 
518 	if (EXTRACT_16BITS(ptr) & L2TP_AVP_HDR_FLAG_MANDATORY) {
519 		ND_PRINT((ndo, "*"));
520 	}
521 	if (EXTRACT_16BITS(ptr) & L2TP_AVP_HDR_FLAG_HIDDEN) {
522 		hidden = TRUE;
523 		ND_PRINT((ndo, "?"));
524 	}
525 	ptr++;
526 
527 	if (EXTRACT_16BITS(ptr)) {
528 		/* Vendor Specific Attribute */
529 	        ND_PRINT((ndo, "VENDOR%04x:", EXTRACT_16BITS(ptr))); ptr++;
530 		ND_PRINT((ndo, "ATTR%04x", EXTRACT_16BITS(ptr))); ptr++;
531 		ND_PRINT((ndo, "("));
532 		print_octets(ndo, (const u_char *)ptr, len-6);
533 		ND_PRINT((ndo, ")"));
534 	} else {
535 		/* IETF-defined Attributes */
536 		ptr++;
537 		attr_type = EXTRACT_16BITS(ptr); ptr++;
538 		ND_PRINT((ndo, "%s", tok2str(l2tp_avp2str, "AVP-#%u", attr_type)));
539 		ND_PRINT((ndo, "("));
540 		if (hidden) {
541 			ND_PRINT((ndo, "???"));
542 		} else {
543 			switch (attr_type) {
544 			case L2TP_AVP_MSGTYPE:
545 				l2tp_msgtype_print(ndo, (const u_char *)ptr);
546 				break;
547 			case L2TP_AVP_RESULT_CODE:
548 				l2tp_result_code_print(ndo, (const u_char *)ptr, len-6);
549 				break;
550 			case L2TP_AVP_PROTO_VER:
551 				l2tp_proto_ver_print(ndo, ptr);
552 				break;
553 			case L2TP_AVP_FRAMING_CAP:
554 				l2tp_framing_cap_print(ndo, (const u_char *)ptr);
555 				break;
556 			case L2TP_AVP_BEARER_CAP:
557 				l2tp_bearer_cap_print(ndo, (const u_char *)ptr);
558 				break;
559 			case L2TP_AVP_TIE_BREAKER:
560 				print_octets(ndo, (const u_char *)ptr, 8);
561 				break;
562 			case L2TP_AVP_FIRM_VER:
563 			case L2TP_AVP_ASSND_TUN_ID:
564 			case L2TP_AVP_RECV_WIN_SIZE:
565 			case L2TP_AVP_ASSND_SESS_ID:
566 				print_16bits_val(ndo, ptr);
567 				break;
568 			case L2TP_AVP_HOST_NAME:
569 			case L2TP_AVP_VENDOR_NAME:
570 			case L2TP_AVP_CALLING_NUMBER:
571 			case L2TP_AVP_CALLED_NUMBER:
572 			case L2TP_AVP_SUB_ADDRESS:
573 			case L2TP_AVP_PROXY_AUTH_NAME:
574 			case L2TP_AVP_PRIVATE_GRP_ID:
575 				print_string(ndo, (const u_char *)ptr, len-6);
576 				break;
577 			case L2TP_AVP_CHALLENGE:
578 			case L2TP_AVP_INI_RECV_LCP:
579 			case L2TP_AVP_LAST_SENT_LCP:
580 			case L2TP_AVP_LAST_RECV_LCP:
581 			case L2TP_AVP_PROXY_AUTH_CHAL:
582 			case L2TP_AVP_PROXY_AUTH_RESP:
583 			case L2TP_AVP_RANDOM_VECTOR:
584 				print_octets(ndo, (const u_char *)ptr, len-6);
585 				break;
586 			case L2TP_AVP_Q931_CC:
587 				l2tp_q931_cc_print(ndo, (const u_char *)ptr, len-6);
588 				break;
589 			case L2TP_AVP_CHALLENGE_RESP:
590 				print_octets(ndo, (const u_char *)ptr, 16);
591 				break;
592 			case L2TP_AVP_CALL_SER_NUM:
593 			case L2TP_AVP_MINIMUM_BPS:
594 			case L2TP_AVP_MAXIMUM_BPS:
595 			case L2TP_AVP_TX_CONN_SPEED:
596 			case L2TP_AVP_PHY_CHANNEL_ID:
597 			case L2TP_AVP_RX_CONN_SPEED:
598 				print_32bits_val(ndo, (const uint32_t *)ptr);
599 				break;
600 			case L2TP_AVP_BEARER_TYPE:
601 				l2tp_bearer_type_print(ndo, (const u_char *)ptr);
602 				break;
603 			case L2TP_AVP_FRAMING_TYPE:
604 				l2tp_framing_type_print(ndo, (const u_char *)ptr);
605 				break;
606 			case L2TP_AVP_PACKET_PROC_DELAY:
607 				l2tp_packet_proc_delay_print(ndo);
608 				break;
609 			case L2TP_AVP_PROXY_AUTH_TYPE:
610 				l2tp_proxy_auth_type_print(ndo, (const u_char *)ptr);
611 				break;
612 			case L2TP_AVP_PROXY_AUTH_ID:
613 				l2tp_proxy_auth_id_print(ndo, (const u_char *)ptr);
614 				break;
615 			case L2TP_AVP_CALL_ERRORS:
616 				l2tp_call_errors_print(ndo, (const u_char *)ptr);
617 				break;
618 			case L2TP_AVP_ACCM:
619 				l2tp_accm_print(ndo, (const u_char *)ptr);
620 				break;
621 			case L2TP_AVP_SEQ_REQUIRED:
622 				break;	/* No Attribute Value */
623 			case L2TP_AVP_PPP_DISCON_CC:
624 				l2tp_ppp_discon_cc_print(ndo, (const u_char *)ptr, len-6);
625 				break;
626 			default:
627 				break;
628 			}
629 		}
630 		ND_PRINT((ndo, ")"));
631 	}
632 
633 	l2tp_avp_print(ndo, dat+len, length-len);
634 	return;
635 
636  trunc:
637 	ND_PRINT((ndo, "|..."));
638 }
639 
640 
641 void
642 l2tp_print(netdissect_options *ndo, const u_char *dat, u_int length)
643 {
644 	const u_char *ptr = dat;
645 	u_int cnt = 0;			/* total octets consumed */
646 	uint16_t pad;
647 	int flag_t, flag_l, flag_s, flag_o;
648 	uint16_t l2tp_len;
649 
650 	flag_t = flag_l = flag_s = flag_o = FALSE;
651 
652 	ND_TCHECK2(*ptr, 2);	/* Flags & Version */
653 	if ((EXTRACT_16BITS(ptr) & L2TP_VERSION_MASK) == L2TP_VERSION_L2TP) {
654 		ND_PRINT((ndo, " l2tp:"));
655 	} else if ((EXTRACT_16BITS(ptr) & L2TP_VERSION_MASK) == L2TP_VERSION_L2F) {
656 		ND_PRINT((ndo, " l2f:"));
657 		return;		/* nothing to do */
658 	} else {
659 		ND_PRINT((ndo, " Unknown Version, neither L2F(1) nor L2TP(2)"));
660 		return;		/* nothing we can do */
661 	}
662 
663 	ND_PRINT((ndo, "["));
664 	if (EXTRACT_16BITS(ptr) & L2TP_FLAG_TYPE) {
665 		flag_t = TRUE;
666 		ND_PRINT((ndo, "T"));
667 	}
668 	if (EXTRACT_16BITS(ptr) & L2TP_FLAG_LENGTH) {
669 		flag_l = TRUE;
670 		ND_PRINT((ndo, "L"));
671 	}
672 	if (EXTRACT_16BITS(ptr) & L2TP_FLAG_SEQUENCE) {
673 		flag_s = TRUE;
674 		ND_PRINT((ndo, "S"));
675 	}
676 	if (EXTRACT_16BITS(ptr) & L2TP_FLAG_OFFSET) {
677 		flag_o = TRUE;
678 		ND_PRINT((ndo, "O"));
679 	}
680 	if (EXTRACT_16BITS(ptr) & L2TP_FLAG_PRIORITY)
681 		ND_PRINT((ndo, "P"));
682 	ND_PRINT((ndo, "]"));
683 
684 	ptr += 2;
685 	cnt += 2;
686 
687 	if (flag_l) {
688 		ND_TCHECK2(*ptr, 2);	/* Length */
689 		l2tp_len = EXTRACT_16BITS(ptr);
690 		ptr += 2;
691 		cnt += 2;
692 	} else {
693 		l2tp_len = 0;
694 	}
695 
696 	ND_TCHECK2(*ptr, 2);		/* Tunnel ID */
697 	ND_PRINT((ndo, "(%u/", EXTRACT_16BITS(ptr)));
698 	ptr += 2;
699 	cnt += 2;
700 	ND_TCHECK2(*ptr, 2);		/* Session ID */
701 	ND_PRINT((ndo, "%u)",  EXTRACT_16BITS(ptr)));
702 	ptr += 2;
703 	cnt += 2;
704 
705 	if (flag_s) {
706 		ND_TCHECK2(*ptr, 2);	/* Ns */
707 		ND_PRINT((ndo, "Ns=%u,", EXTRACT_16BITS(ptr)));
708 		ptr += 2;
709 		cnt += 2;
710 		ND_TCHECK2(*ptr, 2);	/* Nr */
711 		ND_PRINT((ndo, "Nr=%u",  EXTRACT_16BITS(ptr)));
712 		ptr += 2;
713 		cnt += 2;
714 	}
715 
716 	if (flag_o) {
717 		ND_TCHECK2(*ptr, 2);	/* Offset Size */
718 		pad =  EXTRACT_16BITS(ptr);
719 		ptr += (2 + pad);
720 		cnt += (2 + pad);
721 	}
722 
723 	if (flag_l) {
724 		if (length < l2tp_len) {
725 			ND_PRINT((ndo, " Length %u larger than packet", l2tp_len));
726 			return;
727 		}
728 		length = l2tp_len;
729 	}
730 	if (length < cnt) {
731 		ND_PRINT((ndo, " Length %u smaller than header length", length));
732 		return;
733 	}
734 	if (flag_t) {
735 		if (!flag_l) {
736 			ND_PRINT((ndo, " No length"));
737 			return;
738 		}
739 		if (length - cnt == 0) {
740 			ND_PRINT((ndo, " ZLB"));
741 		} else {
742 			l2tp_avp_print(ndo, ptr, length - cnt);
743 		}
744 	} else {
745 		ND_PRINT((ndo, " {"));
746 		ppp_print(ndo, ptr, length - cnt);
747 		ND_PRINT((ndo, "}"));
748 	}
749 
750 	return;
751 
752  trunc:
753 	ND_PRINT((ndo, "%s", tstr));
754 }
755