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