xref: /netbsd/external/bsd/tcpdump/dist/print-dccp.c (revision 193b2145)
1546f56c3Schristos /*
2546f56c3Schristos  * Copyright (C) Arnaldo Carvalho de Melo 2004
3546f56c3Schristos  * Copyright (C) Ian McDonald 2005
4546f56c3Schristos  * Copyright (C) Yoshifumi Nishida 2005
5546f56c3Schristos  *
6546f56c3Schristos  * This software may be distributed either under the terms of the
7546f56c3Schristos  * BSD-style license that accompanies tcpdump or the GNU GPL version 2
8546f56c3Schristos  */
9546f56c3Schristos 
10c8632dc8Schristos #include <sys/cdefs.h>
11546f56c3Schristos #ifndef lint
12*193b2145Schristos __RCSID("$NetBSD: print-dccp.c,v 1.8 2019/10/01 16:06:16 christos Exp $");
13546f56c3Schristos #endif
14546f56c3Schristos 
15c767dfb8Sspz /* \summary: Datagram Congestion Control Protocol (DCCP) printer */
16c767dfb8Sspz 
17546f56c3Schristos #ifdef HAVE_CONFIG_H
18546f56c3Schristos #include "config.h"
19546f56c3Schristos #endif
20546f56c3Schristos 
21c746cb4fSchristos #include <netdissect-stdinc.h>
22546f56c3Schristos 
23546f56c3Schristos #include <stdio.h>
24546f56c3Schristos #include <string.h>
25546f56c3Schristos 
26c746cb4fSchristos #include "netdissect.h"
27546f56c3Schristos #include "addrtoname.h"
28c746cb4fSchristos #include "extract.h"
29546f56c3Schristos #include "ip.h"
30546f56c3Schristos #include "ip6.h"
31546f56c3Schristos #include "ipproto.h"
32546f56c3Schristos 
3366c3c5b8Schristos /* RFC4340: Datagram Congestion Control Protocol (DCCP) */
3466c3c5b8Schristos 
357e4823a9Schristos /**
367e4823a9Schristos  * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
377e4823a9Schristos  * sequence number
387e4823a9Schristos  *
397e4823a9Schristos  * @dccph_sport - Relevant port on the endpoint that sent this packet
407e4823a9Schristos  * @dccph_dport - Relevant port on the other endpoint
417e4823a9Schristos  * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
427e4823a9Schristos  * @dccph_ccval - Used by the HC-Sender CCID
437e4823a9Schristos  * @dccph_cscov - Parts of the packet that are covered by the Checksum field
447e4823a9Schristos  * @dccph_checksum - Internet checksum, depends on dccph_cscov
457e4823a9Schristos  * @dccph_x - 0 = 24 bit sequence number, 1 = 48
467e4823a9Schristos  * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
477e4823a9Schristos  * @dccph_seq - 24-bit sequence number
487e4823a9Schristos  */
497e4823a9Schristos struct dccp_hdr {
507e4823a9Schristos 	uint16_t	dccph_sport,
517e4823a9Schristos 			dccph_dport;
527e4823a9Schristos 	uint8_t		dccph_doff;
537e4823a9Schristos 	uint8_t		dccph_ccval_cscov;
547e4823a9Schristos 	uint16_t	dccph_checksum;
557e4823a9Schristos 	uint8_t		dccph_xtr;
567e4823a9Schristos 	uint8_t		dccph_seq[3];
577e4823a9Schristos } UNALIGNED;
587e4823a9Schristos 
597e4823a9Schristos /**
607e4823a9Schristos  * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
617e4823a9Schristos  * sequence number
627e4823a9Schristos  *
637e4823a9Schristos  * @dccph_sport - Relevant port on the endpoint that sent this packet
647e4823a9Schristos  * @dccph_dport - Relevant port on the other endpoint
657e4823a9Schristos  * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
667e4823a9Schristos  * @dccph_ccval - Used by the HC-Sender CCID
677e4823a9Schristos  * @dccph_cscov - Parts of the packet that are covered by the Checksum field
687e4823a9Schristos  * @dccph_checksum - Internet checksum, depends on dccph_cscov
697e4823a9Schristos  * @dccph_x - 0 = 24 bit sequence number, 1 = 48
707e4823a9Schristos  * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
717e4823a9Schristos  * @dccph_seq - 48-bit sequence number
727e4823a9Schristos  */
737e4823a9Schristos struct dccp_hdr_ext {
747e4823a9Schristos 	uint16_t	dccph_sport,
757e4823a9Schristos 			dccph_dport;
767e4823a9Schristos 	uint8_t		dccph_doff;
777e4823a9Schristos 	uint8_t		dccph_ccval_cscov;
787e4823a9Schristos 	uint16_t	dccph_checksum;
797e4823a9Schristos 	uint8_t		dccph_xtr;
807e4823a9Schristos 	uint8_t		reserved;
817e4823a9Schristos 	uint8_t		dccph_seq[6];
827e4823a9Schristos } UNALIGNED;
837e4823a9Schristos 
847e4823a9Schristos #define DCCPH_CCVAL(dh)	(((dh)->dccph_ccval_cscov >> 4) & 0xF)
857e4823a9Schristos #define DCCPH_CSCOV(dh)	(((dh)->dccph_ccval_cscov) & 0xF)
867e4823a9Schristos 
877e4823a9Schristos #define DCCPH_X(dh)	((dh)->dccph_xtr & 1)
887e4823a9Schristos #define DCCPH_TYPE(dh)	(((dh)->dccph_xtr >> 1) & 0xF)
897e4823a9Schristos 
907e4823a9Schristos /**
917e4823a9Schristos  * struct dccp_hdr_request - Conection initiation request header
927e4823a9Schristos  *
937e4823a9Schristos  * @dccph_req_service - Service to which the client app wants to connect
947e4823a9Schristos  */
957e4823a9Schristos struct dccp_hdr_request {
967e4823a9Schristos 	uint32_t	dccph_req_service;
977e4823a9Schristos } UNALIGNED;
987e4823a9Schristos 
997e4823a9Schristos /**
1007e4823a9Schristos  * struct dccp_hdr_response - Conection initiation response header
1017e4823a9Schristos  *
1027e4823a9Schristos  * @dccph_resp_ack - 48 bit ack number, contains GSR
1037e4823a9Schristos  * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
1047e4823a9Schristos  */
1057e4823a9Schristos struct dccp_hdr_response {
1067e4823a9Schristos 	uint8_t				dccph_resp_ack[8];	/* always 8 bytes */
1077e4823a9Schristos 	uint32_t			dccph_resp_service;
1087e4823a9Schristos } UNALIGNED;
1097e4823a9Schristos 
1107e4823a9Schristos /**
1117e4823a9Schristos  * struct dccp_hdr_reset - Unconditionally shut down a connection
1127e4823a9Schristos  *
1137e4823a9Schristos  * @dccph_resp_ack - 48 bit ack number
1147e4823a9Schristos  * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
1157e4823a9Schristos  */
1167e4823a9Schristos struct dccp_hdr_reset {
1177e4823a9Schristos 	uint8_t				dccph_reset_ack[8];	/* always 8 bytes */
1187e4823a9Schristos 	uint8_t				dccph_reset_code,
1197e4823a9Schristos 					dccph_reset_data[3];
1207e4823a9Schristos } UNALIGNED;
1217e4823a9Schristos 
1227e4823a9Schristos enum dccp_pkt_type {
1237e4823a9Schristos 	DCCP_PKT_REQUEST = 0,
1247e4823a9Schristos 	DCCP_PKT_RESPONSE,
1257e4823a9Schristos 	DCCP_PKT_DATA,
1267e4823a9Schristos 	DCCP_PKT_ACK,
1277e4823a9Schristos 	DCCP_PKT_DATAACK,
1287e4823a9Schristos 	DCCP_PKT_CLOSEREQ,
1297e4823a9Schristos 	DCCP_PKT_CLOSE,
1307e4823a9Schristos 	DCCP_PKT_RESET,
1317e4823a9Schristos 	DCCP_PKT_SYNC,
13266c3c5b8Schristos 	DCCP_PKT_SYNCACK
13366c3c5b8Schristos };
13466c3c5b8Schristos 
13566c3c5b8Schristos static const struct tok dccp_pkt_type_str[] = {
13666c3c5b8Schristos 	{ DCCP_PKT_REQUEST, "DCCP-Request" },
13766c3c5b8Schristos 	{ DCCP_PKT_RESPONSE, "DCCP-Response" },
13866c3c5b8Schristos 	{ DCCP_PKT_DATA, "DCCP-Data" },
13966c3c5b8Schristos 	{ DCCP_PKT_ACK, "DCCP-Ack" },
14066c3c5b8Schristos 	{ DCCP_PKT_DATAACK, "DCCP-DataAck" },
14166c3c5b8Schristos 	{ DCCP_PKT_CLOSEREQ, "DCCP-CloseReq" },
14266c3c5b8Schristos 	{ DCCP_PKT_CLOSE, "DCCP-Close" },
14366c3c5b8Schristos 	{ DCCP_PKT_RESET, "DCCP-Reset" },
14466c3c5b8Schristos 	{ DCCP_PKT_SYNC, "DCCP-Sync" },
14566c3c5b8Schristos 	{ DCCP_PKT_SYNCACK, "DCCP-SyncAck" },
14666c3c5b8Schristos 	{ 0, NULL}
1477e4823a9Schristos };
1487e4823a9Schristos 
1497e4823a9Schristos enum dccp_reset_codes {
1507e4823a9Schristos 	DCCP_RESET_CODE_UNSPECIFIED = 0,
1517e4823a9Schristos 	DCCP_RESET_CODE_CLOSED,
1527e4823a9Schristos 	DCCP_RESET_CODE_ABORTED,
1537e4823a9Schristos 	DCCP_RESET_CODE_NO_CONNECTION,
1547e4823a9Schristos 	DCCP_RESET_CODE_PACKET_ERROR,
1557e4823a9Schristos 	DCCP_RESET_CODE_OPTION_ERROR,
1567e4823a9Schristos 	DCCP_RESET_CODE_MANDATORY_ERROR,
1577e4823a9Schristos 	DCCP_RESET_CODE_CONNECTION_REFUSED,
1587e4823a9Schristos 	DCCP_RESET_CODE_BAD_SERVICE_CODE,
1597e4823a9Schristos 	DCCP_RESET_CODE_TOO_BUSY,
1607e4823a9Schristos 	DCCP_RESET_CODE_BAD_INIT_COOKIE,
1617e4823a9Schristos 	DCCP_RESET_CODE_AGGRESSION_PENALTY,
1627e4823a9Schristos 	__DCCP_RESET_CODE_LAST
1637e4823a9Schristos };
1647e4823a9Schristos 
1657e4823a9Schristos static const char tstr[] = "[|dccp]";
1667e4823a9Schristos 
167546f56c3Schristos static const char *dccp_reset_codes[] = {
168546f56c3Schristos 	"unspecified",
169546f56c3Schristos 	"closed",
170546f56c3Schristos 	"aborted",
171546f56c3Schristos 	"no_connection",
172546f56c3Schristos 	"packet_error",
173546f56c3Schristos 	"option_error",
174546f56c3Schristos 	"mandatory_error",
175546f56c3Schristos 	"connection_refused",
176546f56c3Schristos 	"bad_service_code",
177546f56c3Schristos 	"too_busy",
178546f56c3Schristos 	"bad_init_cookie",
179546f56c3Schristos 	"aggression_penalty",
180546f56c3Schristos };
181546f56c3Schristos 
182546f56c3Schristos static const char *dccp_feature_nums[] = {
183546f56c3Schristos 	"reserved",
184546f56c3Schristos 	"ccid",
185546f56c3Schristos 	"allow_short_seqno",
186546f56c3Schristos 	"sequence_window",
187546f56c3Schristos 	"ecn_incapable",
188546f56c3Schristos 	"ack_ratio",
189546f56c3Schristos 	"send_ack_vector",
190546f56c3Schristos 	"send_ndp_count",
191546f56c3Schristos 	"minimum checksum coverage",
192546f56c3Schristos 	"check data checksum",
193546f56c3Schristos };
194546f56c3Schristos 
dccp_csum_coverage(const struct dccp_hdr * dh,u_int len)1957becbaa8Schristos static inline u_int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
196546f56c3Schristos {
197546f56c3Schristos 	u_int cov;
198546f56c3Schristos 
199546f56c3Schristos 	if (DCCPH_CSCOV(dh) == 0)
200546f56c3Schristos 		return len;
2017e4823a9Schristos 	cov = (dh->dccph_doff + DCCPH_CSCOV(dh) - 1) * sizeof(uint32_t);
202546f56c3Schristos 	return (cov > len)? len : cov;
203546f56c3Schristos }
204546f56c3Schristos 
dccp_cksum(netdissect_options * ndo,const struct ip * ip,const struct dccp_hdr * dh,u_int len)2057e4823a9Schristos static int dccp_cksum(netdissect_options *ndo, const struct ip *ip,
206546f56c3Schristos 	const struct dccp_hdr *dh, u_int len)
207546f56c3Schristos {
208c746cb4fSchristos 	return nextproto4_cksum(ndo, ip, (const uint8_t *)(const void *)dh, len,
2097becbaa8Schristos 				dccp_csum_coverage(dh, len), IPPROTO_DCCP);
210546f56c3Schristos }
211546f56c3Schristos 
dccp6_cksum(netdissect_options * ndo,const struct ip6_hdr * ip6,const struct dccp_hdr * dh,u_int len)212c746cb4fSchristos static int dccp6_cksum(netdissect_options *ndo, const struct ip6_hdr *ip6,
213c746cb4fSchristos 	const struct dccp_hdr *dh, u_int len)
214546f56c3Schristos {
215c746cb4fSchristos 	return nextproto6_cksum(ndo, ip6, (const uint8_t *)(const void *)dh, len,
2167becbaa8Schristos 				dccp_csum_coverage(dh, len), IPPROTO_DCCP);
217546f56c3Schristos }
218546f56c3Schristos 
dccp_reset_code(uint8_t code)2197e4823a9Schristos static const char *dccp_reset_code(uint8_t code)
220546f56c3Schristos {
221546f56c3Schristos 	if (code >= __DCCP_RESET_CODE_LAST)
222546f56c3Schristos 		return "invalid";
223546f56c3Schristos 	return dccp_reset_codes[code];
224546f56c3Schristos }
225546f56c3Schristos 
dccp_seqno(const u_char * bp)2267e4823a9Schristos static uint64_t dccp_seqno(const u_char *bp)
227546f56c3Schristos {
2287e4823a9Schristos 	const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
2297e4823a9Schristos 	uint64_t seqno;
230546f56c3Schristos 
231546f56c3Schristos 	if (DCCPH_X(dh) != 0) {
2327e4823a9Schristos 		const struct dccp_hdr_ext *dhx = (const struct dccp_hdr_ext *)bp;
2337e4823a9Schristos 		seqno = EXTRACT_48BITS(dhx->dccph_seq);
2347e4823a9Schristos 	} else {
2357e4823a9Schristos 		seqno = EXTRACT_24BITS(dh->dccph_seq);
236546f56c3Schristos 	}
237546f56c3Schristos 
238546f56c3Schristos 	return seqno;
239546f56c3Schristos }
240546f56c3Schristos 
dccp_basic_hdr_len(const struct dccp_hdr * dh)241546f56c3Schristos static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh)
242546f56c3Schristos {
2437e4823a9Schristos 	return DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : sizeof(struct dccp_hdr);
244546f56c3Schristos }
245546f56c3Schristos 
dccp_print_ack_no(netdissect_options * ndo,const u_char * bp)2467e4823a9Schristos static void dccp_print_ack_no(netdissect_options *ndo, const u_char *bp)
247546f56c3Schristos {
248546f56c3Schristos 	const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
2497e4823a9Schristos 	const u_char *ackp = bp + dccp_basic_hdr_len(dh);
2507e4823a9Schristos 	uint64_t ackno;
251546f56c3Schristos 
252546f56c3Schristos 	if (DCCPH_X(dh) != 0) {
2537e4823a9Schristos 		ND_TCHECK2(*ackp, 8);
2547e4823a9Schristos 		ackno = EXTRACT_48BITS(ackp + 2);
2557e4823a9Schristos 	} else {
2567e4823a9Schristos 		ND_TCHECK2(*ackp, 4);
2577e4823a9Schristos 		ackno = EXTRACT_24BITS(ackp + 1);
258546f56c3Schristos 	}
259546f56c3Schristos 
2607e4823a9Schristos 	ND_PRINT((ndo, "(ack=%" PRIu64 ") ", ackno));
261546f56c3Schristos trunc:
262546f56c3Schristos 	return;
263546f56c3Schristos }
264546f56c3Schristos 
2657e4823a9Schristos static int dccp_print_option(netdissect_options *, const u_char *, u_int);
266546f56c3Schristos 
267546f56c3Schristos /**
268546f56c3Schristos  * dccp_print - show dccp packet
269546f56c3Schristos  * @bp - beginning of dccp packet
270546f56c3Schristos  * @data2 - beginning of enclosing
271546f56c3Schristos  * @len - lenght of ip packet
272546f56c3Schristos  */
dccp_print(netdissect_options * ndo,const u_char * bp,const u_char * data2,u_int len)2737e4823a9Schristos void dccp_print(netdissect_options *ndo, const u_char *bp, const u_char *data2,
2747e4823a9Schristos 		u_int len)
275546f56c3Schristos {
276546f56c3Schristos 	const struct dccp_hdr *dh;
277546f56c3Schristos 	const struct ip *ip;
278546f56c3Schristos 	const struct ip6_hdr *ip6;
279546f56c3Schristos 	const u_char *cp;
280546f56c3Schristos 	u_short sport, dport;
281546f56c3Schristos 	u_int hlen;
2827e4823a9Schristos 	u_int fixed_hdrlen;
28366c3c5b8Schristos 	uint8_t	dccph_type;
284546f56c3Schristos 
285546f56c3Schristos 	dh = (const struct dccp_hdr *)bp;
286546f56c3Schristos 
287c746cb4fSchristos 	ip = (const struct ip *)data2;
288546f56c3Schristos 	if (IP_V(ip) == 6)
289546f56c3Schristos 		ip6 = (const struct ip6_hdr *)data2;
290546f56c3Schristos 	else
291546f56c3Schristos 		ip6 = NULL;
2927e4823a9Schristos 
2937e4823a9Schristos 	/* make sure we have enough data to look at the X bit */
294546f56c3Schristos 	cp = (const u_char *)(dh + 1);
2957e4823a9Schristos 	if (cp > ndo->ndo_snapend) {
2967e4823a9Schristos 		ND_PRINT((ndo, "[Invalid packet|dccp]"));
2977e4823a9Schristos 		return;
2987e4823a9Schristos 	}
2997e4823a9Schristos 	if (len < sizeof(struct dccp_hdr)) {
3007e4823a9Schristos 		ND_PRINT((ndo, "truncated-dccp - %u bytes missing!",
3017e4823a9Schristos 			  len - (u_int)sizeof(struct dccp_hdr)));
302546f56c3Schristos 		return;
303546f56c3Schristos 	}
304546f56c3Schristos 
3057e4823a9Schristos 	/* get the length of the generic header */
3067e4823a9Schristos 	fixed_hdrlen = dccp_basic_hdr_len(dh);
3077e4823a9Schristos 	if (len < fixed_hdrlen) {
3087e4823a9Schristos 		ND_PRINT((ndo, "truncated-dccp - %u bytes missing!",
3097e4823a9Schristos 			  len - fixed_hdrlen));
310546f56c3Schristos 		return;
311546f56c3Schristos 	}
3127e4823a9Schristos 	ND_TCHECK2(*dh, fixed_hdrlen);
313546f56c3Schristos 
314546f56c3Schristos 	sport = EXTRACT_16BITS(&dh->dccph_sport);
315546f56c3Schristos 	dport = EXTRACT_16BITS(&dh->dccph_dport);
316546f56c3Schristos 	hlen = dh->dccph_doff * 4;
317546f56c3Schristos 
318546f56c3Schristos 	if (ip6) {
3197e4823a9Schristos 		ND_PRINT((ndo, "%s.%d > %s.%d: ",
3207e4823a9Schristos 			  ip6addr_string(ndo, &ip6->ip6_src), sport,
3217e4823a9Schristos 			  ip6addr_string(ndo, &ip6->ip6_dst), dport));
322c746cb4fSchristos 	} else {
3237e4823a9Schristos 		ND_PRINT((ndo, "%s.%d > %s.%d: ",
3247e4823a9Schristos 			  ipaddr_string(ndo, &ip->ip_src), sport,
3257e4823a9Schristos 			  ipaddr_string(ndo, &ip->ip_dst), dport));
326546f56c3Schristos 	}
327546f56c3Schristos 
32866c3c5b8Schristos 	ND_PRINT((ndo, "DCCP"));
32966c3c5b8Schristos 
3307e4823a9Schristos 	if (ndo->ndo_qflag) {
3317e4823a9Schristos 		ND_PRINT((ndo, " %d", len - hlen));
332546f56c3Schristos 		if (hlen > len) {
33366c3c5b8Schristos 			ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]",
3347e4823a9Schristos 				  hlen, len));
335546f56c3Schristos 		}
336546f56c3Schristos 		return;
337546f56c3Schristos 	}
338546f56c3Schristos 
339546f56c3Schristos 	/* other variables in generic header */
3407e4823a9Schristos 	if (ndo->ndo_vflag) {
34166c3c5b8Schristos 		ND_PRINT((ndo, " (CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh)));
342546f56c3Schristos 	}
343546f56c3Schristos 
344546f56c3Schristos 	/* checksum calculation */
3457e4823a9Schristos 	if (ndo->ndo_vflag && ND_TTEST2(bp[0], len)) {
3467e4823a9Schristos 		uint16_t sum = 0, dccp_sum;
347546f56c3Schristos 
348546f56c3Schristos 		dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
3497e4823a9Schristos 		ND_PRINT((ndo, "cksum 0x%04x ", dccp_sum));
350546f56c3Schristos 		if (IP_V(ip) == 4)
3517e4823a9Schristos 			sum = dccp_cksum(ndo, ip, dh, len);
352546f56c3Schristos 		else if (IP_V(ip) == 6)
353c746cb4fSchristos 			sum = dccp6_cksum(ndo, ip6, dh, len);
354546f56c3Schristos 		if (sum != 0)
35566c3c5b8Schristos 			ND_PRINT((ndo, "(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum, sum)));
356546f56c3Schristos 		else
35766c3c5b8Schristos 			ND_PRINT((ndo, "(correct)"));
358546f56c3Schristos 	}
359546f56c3Schristos 
36066c3c5b8Schristos 	if (ndo->ndo_vflag)
36166c3c5b8Schristos 		ND_PRINT((ndo, ")"));
36266c3c5b8Schristos 	ND_PRINT((ndo, " "));
36366c3c5b8Schristos 
36466c3c5b8Schristos 	dccph_type = DCCPH_TYPE(dh);
36566c3c5b8Schristos 	switch (dccph_type) {
366546f56c3Schristos 	case DCCP_PKT_REQUEST: {
367c746cb4fSchristos 		const struct dccp_hdr_request *dhr =
368c746cb4fSchristos 			(const struct dccp_hdr_request *)(bp + fixed_hdrlen);
3697e4823a9Schristos 		fixed_hdrlen += 4;
3707e4823a9Schristos 		if (len < fixed_hdrlen) {
37166c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
37266c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
3737e4823a9Schristos 				  len - fixed_hdrlen));
3747e4823a9Schristos 			return;
3757e4823a9Schristos 		}
3767e4823a9Schristos 		ND_TCHECK(*dhr);
37766c3c5b8Schristos 		ND_PRINT((ndo, "%s (service=%d) ",
37866c3c5b8Schristos 			  tok2str(dccp_pkt_type_str, "", dccph_type),
3797e4823a9Schristos 			  EXTRACT_32BITS(&dhr->dccph_req_service)));
380546f56c3Schristos 		break;
381546f56c3Schristos 	}
382546f56c3Schristos 	case DCCP_PKT_RESPONSE: {
383c746cb4fSchristos 		const struct dccp_hdr_response *dhr =
384c746cb4fSchristos 			(const struct dccp_hdr_response *)(bp + fixed_hdrlen);
3857e4823a9Schristos 		fixed_hdrlen += 12;
3867e4823a9Schristos 		if (len < fixed_hdrlen) {
38766c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
38866c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
3897e4823a9Schristos 				  len - fixed_hdrlen));
3907e4823a9Schristos 			return;
3917e4823a9Schristos 		}
3927e4823a9Schristos 		ND_TCHECK(*dhr);
39366c3c5b8Schristos 		ND_PRINT((ndo, "%s (service=%d) ",
39466c3c5b8Schristos 			  tok2str(dccp_pkt_type_str, "", dccph_type),
3957e4823a9Schristos 			  EXTRACT_32BITS(&dhr->dccph_resp_service)));
396546f56c3Schristos 		break;
397546f56c3Schristos 	}
398546f56c3Schristos 	case DCCP_PKT_DATA:
39966c3c5b8Schristos 		ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
400546f56c3Schristos 		break;
401546f56c3Schristos 	case DCCP_PKT_ACK: {
4027e4823a9Schristos 		fixed_hdrlen += 8;
4037e4823a9Schristos 		if (len < fixed_hdrlen) {
40466c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
40566c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
4067e4823a9Schristos 				  len - fixed_hdrlen));
4077e4823a9Schristos 			return;
4087e4823a9Schristos 		}
40966c3c5b8Schristos 		ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
410546f56c3Schristos 		break;
411546f56c3Schristos 	}
412546f56c3Schristos 	case DCCP_PKT_DATAACK: {
4137e4823a9Schristos 		fixed_hdrlen += 8;
4147e4823a9Schristos 		if (len < fixed_hdrlen) {
41566c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
41666c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
4177e4823a9Schristos 				  len - fixed_hdrlen));
4187e4823a9Schristos 			return;
4197e4823a9Schristos 		}
42066c3c5b8Schristos 		ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
421546f56c3Schristos 		break;
422546f56c3Schristos 	}
423546f56c3Schristos 	case DCCP_PKT_CLOSEREQ:
4247e4823a9Schristos 		fixed_hdrlen += 8;
4257e4823a9Schristos 		if (len < fixed_hdrlen) {
42666c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
42766c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
4287e4823a9Schristos 				  len - fixed_hdrlen));
4297e4823a9Schristos 			return;
4307e4823a9Schristos 		}
43166c3c5b8Schristos 		ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
432546f56c3Schristos 		break;
433546f56c3Schristos 	case DCCP_PKT_CLOSE:
4347e4823a9Schristos 		fixed_hdrlen += 8;
4357e4823a9Schristos 		if (len < fixed_hdrlen) {
43666c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
43766c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
4387e4823a9Schristos 				  len - fixed_hdrlen));
4397e4823a9Schristos 			return;
4407e4823a9Schristos 		}
44166c3c5b8Schristos 		ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
442546f56c3Schristos 		break;
443546f56c3Schristos 	case DCCP_PKT_RESET: {
444c746cb4fSchristos 		const struct dccp_hdr_reset *dhr =
445c746cb4fSchristos 			(const struct dccp_hdr_reset *)(bp + fixed_hdrlen);
4467e4823a9Schristos 		fixed_hdrlen += 12;
4477e4823a9Schristos 		if (len < fixed_hdrlen) {
44866c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
44966c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
4507e4823a9Schristos 				  len - fixed_hdrlen));
4517e4823a9Schristos 			return;
4527e4823a9Schristos 		}
4537e4823a9Schristos 		ND_TCHECK(*dhr);
45466c3c5b8Schristos 		ND_PRINT((ndo, "%s (code=%s) ",
45566c3c5b8Schristos 			  tok2str(dccp_pkt_type_str, "", dccph_type),
4567e4823a9Schristos 			  dccp_reset_code(dhr->dccph_reset_code)));
457546f56c3Schristos 		break;
458546f56c3Schristos 	}
459546f56c3Schristos 	case DCCP_PKT_SYNC:
4607e4823a9Schristos 		fixed_hdrlen += 8;
4617e4823a9Schristos 		if (len < fixed_hdrlen) {
46266c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
46366c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
4647e4823a9Schristos 				  len - fixed_hdrlen));
4657e4823a9Schristos 			return;
4667e4823a9Schristos 		}
46766c3c5b8Schristos 		ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
468546f56c3Schristos 		break;
469546f56c3Schristos 	case DCCP_PKT_SYNCACK:
4707e4823a9Schristos 		fixed_hdrlen += 8;
4717e4823a9Schristos 		if (len < fixed_hdrlen) {
47266c3c5b8Schristos 			ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
47366c3c5b8Schristos 				  tok2str(dccp_pkt_type_str, "", dccph_type),
4747e4823a9Schristos 				  len - fixed_hdrlen));
4757e4823a9Schristos 			return;
4767e4823a9Schristos 		}
47766c3c5b8Schristos 		ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
478546f56c3Schristos 		break;
479546f56c3Schristos 	default:
48066c3c5b8Schristos 		ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "unknown-type-%u", dccph_type)));
481546f56c3Schristos 		break;
482546f56c3Schristos 	}
483546f56c3Schristos 
484546f56c3Schristos 	if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
485546f56c3Schristos 			(DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
4867e4823a9Schristos 		dccp_print_ack_no(ndo, bp);
487546f56c3Schristos 
4887e4823a9Schristos 	if (ndo->ndo_vflag < 2)
489546f56c3Schristos 		return;
490546f56c3Schristos 
4917e4823a9Schristos 	ND_PRINT((ndo, "seq %" PRIu64, dccp_seqno(bp)));
492546f56c3Schristos 
493546f56c3Schristos 	/* process options */
4947e4823a9Schristos 	if (hlen > fixed_hdrlen){
495546f56c3Schristos 		u_int optlen;
4967e4823a9Schristos 		cp = bp + fixed_hdrlen;
4977e4823a9Schristos 		ND_PRINT((ndo, " <"));
498546f56c3Schristos 
4997e4823a9Schristos 		hlen -= fixed_hdrlen;
500546f56c3Schristos 		while(1){
5017e4823a9Schristos 			optlen = dccp_print_option(ndo, cp, hlen);
5027e4823a9Schristos 			if (!optlen)
5037e4823a9Schristos 				break;
5047e4823a9Schristos 			if (hlen <= optlen)
5057e4823a9Schristos 				break;
506546f56c3Schristos 			hlen -= optlen;
507546f56c3Schristos 			cp += optlen;
5087e4823a9Schristos 			ND_PRINT((ndo, ", "));
509546f56c3Schristos 		}
5107e4823a9Schristos 		ND_PRINT((ndo, ">"));
511546f56c3Schristos 	}
512546f56c3Schristos 	return;
513546f56c3Schristos trunc:
5147e4823a9Schristos 	ND_PRINT((ndo, "%s", tstr));
515546f56c3Schristos 	return;
516546f56c3Schristos }
517546f56c3Schristos 
5187e4823a9Schristos static const struct tok dccp_option_values[] = {
5197e4823a9Schristos 	{ 0, "nop" },
5207e4823a9Schristos 	{ 1, "mandatory" },
5217e4823a9Schristos 	{ 2, "slowreceiver" },
5227e4823a9Schristos 	{ 32, "change_l" },
5237e4823a9Schristos 	{ 33, "confirm_l" },
5247e4823a9Schristos 	{ 34, "change_r" },
5257e4823a9Schristos 	{ 35, "confirm_r" },
5267e4823a9Schristos 	{ 36, "initcookie" },
5277e4823a9Schristos 	{ 37, "ndp_count" },
5287e4823a9Schristos 	{ 38, "ack_vector0" },
5297e4823a9Schristos 	{ 39, "ack_vector1" },
5307e4823a9Schristos 	{ 40, "data_dropped" },
5317e4823a9Schristos 	{ 41, "timestamp" },
5327e4823a9Schristos 	{ 42, "timestamp_echo" },
5337e4823a9Schristos 	{ 43, "elapsed_time" },
5347e4823a9Schristos 	{ 44, "data_checksum" },
5357e4823a9Schristos 	{ 0, NULL }
5367e4823a9Schristos };
537546f56c3Schristos 
538*193b2145Schristos static int
dccp_print_option(netdissect_options * ndo,const u_char * option,u_int hlen)539*193b2145Schristos dccp_print_option(netdissect_options *ndo, const u_char *option, u_int hlen)
5407e4823a9Schristos {
5417e4823a9Schristos 	uint8_t optlen, i;
5427e4823a9Schristos 
5437e4823a9Schristos 	ND_TCHECK(*option);
544546f56c3Schristos 
545546f56c3Schristos 	if (*option >= 32) {
5467e4823a9Schristos 		ND_TCHECK(*(option+1));
547546f56c3Schristos 		optlen = *(option +1);
548546f56c3Schristos 		if (optlen < 2) {
5497e4823a9Schristos 			if (*option >= 128)
5507e4823a9Schristos 				ND_PRINT((ndo, "CCID option %u optlen too short", *option));
5517e4823a9Schristos 			else
5527e4823a9Schristos 				ND_PRINT((ndo, "%s optlen too short",
5537e4823a9Schristos 					  tok2str(dccp_option_values, "Option %u", *option)));
5547e4823a9Schristos 			return 0;
555546f56c3Schristos 		}
5567e4823a9Schristos 	} else
5577e4823a9Schristos 		optlen = 1;
558546f56c3Schristos 
5597e4823a9Schristos 	if (hlen < optlen) {
5607e4823a9Schristos 		if (*option >= 128)
5617e4823a9Schristos 			ND_PRINT((ndo, "CCID option %u optlen goes past header length",
5627e4823a9Schristos 				  *option));
5637e4823a9Schristos 		else
5647e4823a9Schristos 			ND_PRINT((ndo, "%s optlen goes past header length",
5657e4823a9Schristos 				  tok2str(dccp_option_values, "Option %u", *option)));
5667e4823a9Schristos 		return 0;
5677e4823a9Schristos 	}
5687e4823a9Schristos 	ND_TCHECK2(*option, optlen);
569546f56c3Schristos 
5707e4823a9Schristos 	if (*option >= 128) {
5717e4823a9Schristos 		ND_PRINT((ndo, "CCID option %d", *option));
5727e4823a9Schristos 		switch (optlen) {
5737e4823a9Schristos 			case 4:
5747e4823a9Schristos 				ND_PRINT((ndo, " %u", EXTRACT_16BITS(option + 2)));
5757e4823a9Schristos 				break;
5767e4823a9Schristos 			case 6:
5777e4823a9Schristos 				ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
5787e4823a9Schristos 				break;
5797e4823a9Schristos 			default:
5807e4823a9Schristos 				break;
5817e4823a9Schristos 		}
5827e4823a9Schristos 	} else {
5837e4823a9Schristos 		ND_PRINT((ndo, "%s", tok2str(dccp_option_values, "Option %u", *option)));
584546f56c3Schristos 		switch (*option) {
585546f56c3Schristos 		case 32:
586546f56c3Schristos 		case 33:
587546f56c3Schristos 		case 34:
588546f56c3Schristos 		case 35:
5897e4823a9Schristos 			if (optlen < 3) {
5907e4823a9Schristos 				ND_PRINT((ndo, " optlen too short"));
5917e4823a9Schristos 				return optlen;
5927e4823a9Schristos 			}
593546f56c3Schristos 			if (*(option + 2) < 10){
5947e4823a9Schristos 				ND_PRINT((ndo, " %s", dccp_feature_nums[*(option + 2)]));
5957e4823a9Schristos 				for (i = 0; i < optlen - 3; i++)
5967e4823a9Schristos 					ND_PRINT((ndo, " %d", *(option + 3 + i)));
597546f56c3Schristos 			}
598546f56c3Schristos 			break;
599546f56c3Schristos 		case 36:
6007e4823a9Schristos 			if (optlen > 2) {
6017e4823a9Schristos 				ND_PRINT((ndo, " 0x"));
6027e4823a9Schristos 				for (i = 0; i < optlen - 2; i++)
6037e4823a9Schristos 					ND_PRINT((ndo, "%02x", *(option + 2 + i)));
6047e4823a9Schristos 			}
605546f56c3Schristos 			break;
606546f56c3Schristos 		case 37:
6077e4823a9Schristos 			for (i = 0; i < optlen - 2; i++)
6087e4823a9Schristos 				ND_PRINT((ndo, " %d", *(option + 2 + i)));
609546f56c3Schristos 			break;
610546f56c3Schristos 		case 38:
6117e4823a9Schristos 			if (optlen > 2) {
6127e4823a9Schristos 				ND_PRINT((ndo, " 0x"));
6137e4823a9Schristos 				for (i = 0; i < optlen - 2; i++)
6147e4823a9Schristos 					ND_PRINT((ndo, "%02x", *(option + 2 + i)));
6157e4823a9Schristos 			}
616546f56c3Schristos 			break;
617546f56c3Schristos 		case 39:
6187e4823a9Schristos 			if (optlen > 2) {
6197e4823a9Schristos 				ND_PRINT((ndo, " 0x"));
6207e4823a9Schristos 				for (i = 0; i < optlen - 2; i++)
6217e4823a9Schristos 					ND_PRINT((ndo, "%02x", *(option + 2 + i)));
6227e4823a9Schristos 			}
623546f56c3Schristos 			break;
624546f56c3Schristos 		case 40:
6257e4823a9Schristos 			if (optlen > 2) {
6267e4823a9Schristos 				ND_PRINT((ndo, " 0x"));
6277e4823a9Schristos 				for (i = 0; i < optlen - 2; i++)
6287e4823a9Schristos 					ND_PRINT((ndo, "%02x", *(option + 2 + i)));
6297e4823a9Schristos 			}
630546f56c3Schristos 			break;
631546f56c3Schristos 		case 41:
632*193b2145Schristos 		/*
633*193b2145Schristos 		 * 13.1.  Timestamp Option
634*193b2145Schristos 		 *
635*193b2145Schristos 		 *  +--------+--------+--------+--------+--------+--------+
636*193b2145Schristos 		 *  |00101001|00000110|          Timestamp Value          |
637*193b2145Schristos 		 *  +--------+--------+--------+--------+--------+--------+
638*193b2145Schristos 		 *   Type=41  Length=6
639*193b2145Schristos 		 */
640*193b2145Schristos 			if (optlen == 6)
6417e4823a9Schristos 				ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
6427e4823a9Schristos 			else
643*193b2145Schristos 				ND_PRINT((ndo, " [optlen != 6]"));
644546f56c3Schristos 			break;
645546f56c3Schristos 		case 42:
646*193b2145Schristos 		/*
647*193b2145Schristos 		 * 13.3.  Timestamp Echo Option
648*193b2145Schristos 		 *
649*193b2145Schristos 		 *  +--------+--------+--------+--------+--------+--------+
650*193b2145Schristos 		 *  |00101010|00000110|           Timestamp Echo          |
651*193b2145Schristos 		 *  +--------+--------+--------+--------+--------+--------+
652*193b2145Schristos 		 *   Type=42    Len=6
653*193b2145Schristos 		 *
654*193b2145Schristos 		 *  +--------+--------+------- ... -------+--------+--------+
655*193b2145Schristos 		 *  |00101010|00001000|  Timestamp Echo   |   Elapsed Time  |
656*193b2145Schristos 		 *  +--------+--------+------- ... -------+--------+--------+
657*193b2145Schristos 		 *   Type=42    Len=8       (4 bytes)
658*193b2145Schristos 		 *
659*193b2145Schristos 		 *  +--------+--------+------- ... -------+------- ... -------+
660*193b2145Schristos 		 *  |00101010|00001010|  Timestamp Echo   |    Elapsed Time   |
661*193b2145Schristos 		 *  +--------+--------+------- ... -------+------- ... -------+
662*193b2145Schristos 		 *   Type=42   Len=10       (4 bytes)           (4 bytes)
663*193b2145Schristos 		 */
664*193b2145Schristos 			switch (optlen) {
665*193b2145Schristos 			case 6:
6667e4823a9Schristos 				ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
667*193b2145Schristos 				break;
668*193b2145Schristos 			case 8:
669*193b2145Schristos 				ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
670*193b2145Schristos 				ND_PRINT((ndo, " (elapsed time %u)", EXTRACT_16BITS(option + 6)));
671*193b2145Schristos 				break;
672*193b2145Schristos 			case 10:
673*193b2145Schristos 				ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
674*193b2145Schristos 				ND_PRINT((ndo, " (elapsed time %u)", EXTRACT_32BITS(option + 6)));
675*193b2145Schristos 				break;
676*193b2145Schristos 			default:
677*193b2145Schristos 				ND_PRINT((ndo, " [optlen != 6 or 8 or 10]"));
678*193b2145Schristos 				break;
679*193b2145Schristos 			}
680546f56c3Schristos 			break;
681546f56c3Schristos 		case 43:
682546f56c3Schristos 			if (optlen == 6)
6837e4823a9Schristos 				ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
6847e4823a9Schristos 			else if (optlen == 4)
6857e4823a9Schristos 				ND_PRINT((ndo, " %u", EXTRACT_16BITS(option + 2)));
686546f56c3Schristos 			else
687*193b2145Schristos 				ND_PRINT((ndo, " [optlen != 4 or 6]"));
688546f56c3Schristos 			break;
689546f56c3Schristos 		case 44:
6907e4823a9Schristos 			if (optlen > 2) {
6917e4823a9Schristos 				ND_PRINT((ndo, " "));
6927e4823a9Schristos 				for (i = 0; i < optlen - 2; i++)
6937e4823a9Schristos 					ND_PRINT((ndo, "%02x", *(option + 2 + i)));
694546f56c3Schristos 			}
695546f56c3Schristos 			break;
696546f56c3Schristos 		}
697546f56c3Schristos 	}
698546f56c3Schristos 
699546f56c3Schristos 	return optlen;
700546f56c3Schristos trunc:
7017e4823a9Schristos 	ND_PRINT((ndo, "%s", tstr));
702546f56c3Schristos 	return 0;
703546f56c3Schristos }
704