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