]> The Tcpdump Group git mirrors - tcpdump/blob - print-dccp.c
Use nextproto6_cksum() for XXX-over-IPv6 checksums.
[tcpdump] / print-dccp.c
1 /*
2 * Copyright (C) Arnaldo Carvalho de Melo 2004
3 * Copyright (C) Ian McDonald 2005
4 * Copyright (C) Yoshifumi Nishida 2005
5 *
6 * This software may be distributed either under the terms of the
7 * BSD-style license that accompanies tcpdump or the GNU GPL version 2
8 */
9
10 #ifndef lint
11 static const char rcsid[] _U_ =
12 "@(#) $Header: /tcpdump/master/tcpdump/print-dccp.c,v 1.8 2007-11-09 00:44:09 guy Exp $ (LBL)";
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include <tcpdump-stdinc.h>
20
21 #include "dccp.h"
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "interface.h"
27 #include "addrtoname.h"
28 #include "extract.h" /* must come after interface.h */
29 #include "ip.h"
30 #ifdef INET6
31 #include "ip6.h"
32 #endif
33 #include "ipproto.h"
34
35 static const char *dccp_reset_codes[] = {
36 "unspecified",
37 "closed",
38 "aborted",
39 "no_connection",
40 "packet_error",
41 "option_error",
42 "mandatory_error",
43 "connection_refused",
44 "bad_service_code",
45 "too_busy",
46 "bad_init_cookie",
47 "aggression_penalty",
48 };
49
50 static const char *dccp_feature_nums[] = {
51 "reserved",
52 "ccid",
53 "allow_short_seqno",
54 "sequence_window",
55 "ecn_incapable",
56 "ack_ratio",
57 "send_ack_vector",
58 "send_ndp_count",
59 "minimum checksum coverage",
60 "check data checksum",
61 };
62
63 static inline int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
64 {
65 u_int cov;
66
67 if (DCCPH_CSCOV(dh) == 0)
68 return len;
69 cov = (dh->dccph_doff + DCCPH_CSCOV(dh) - 1) * sizeof(u_int32_t);
70 return (cov > len)? len : cov;
71 }
72
73 static int dccp_cksum(const struct ip *ip,
74 const struct dccp_hdr *dh, u_int len)
75 {
76 int cov = dccp_csum_coverage(dh, len);
77 struct phdr {
78 u_int32_t src;
79 u_int32_t dst;
80 u_char mbz;
81 u_char proto;
82 u_int16_t len;
83 } ph;
84 struct cksum_vec vec[2];
85
86 /* pseudo-header.. */
87 ph.mbz = 0;
88 ph.len = htons(len);
89 ph.proto = IPPROTO_DCCP;
90 memcpy(&ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
91 if (IP_HL(ip) == 5)
92 memcpy(&ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
93 else
94 ph.dst = ip_finddst(ip);
95
96 vec[0].ptr = (const u_int8_t *)(void *)&ph;
97 vec[0].len = sizeof(ph);
98 vec[1].ptr = (const u_int8_t *)(void *)dh;
99 vec[1].len = cov;
100 return in_cksum(vec, 2);
101 }
102
103 #ifdef INET6
104 static int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len)
105 {
106 return nextproto6_cksum(ip6, (const u_int8_t *)(void *)dh,
107 dccp_csum_coverage(dh, len), IPPROTO_DCCP);
108 }
109 #endif
110
111 static const char *dccp_reset_code(u_int8_t code)
112 {
113 if (code >= __DCCP_RESET_CODE_LAST)
114 return "invalid";
115 return dccp_reset_codes[code];
116 }
117
118 static u_int64_t dccp_seqno(const struct dccp_hdr *dh)
119 {
120 u_int32_t seq_high = DCCPH_SEQ(dh);
121 u_int64_t seqno = EXTRACT_24BITS(&seq_high) & 0xFFFFFF;
122
123 if (DCCPH_X(dh) != 0) {
124 const struct dccp_hdr_ext *dhx = (void *)(dh + 1);
125 u_int32_t seq_low = dhx->dccph_seq_low;
126 seqno &= 0x00FFFF; /* clear reserved field */
127 seqno = (seqno << 32) + EXTRACT_32BITS(&seq_low);
128 }
129
130 return seqno;
131 }
132
133 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh)
134 {
135 return sizeof(*dh) + (DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : 0);
136 }
137
138 static void dccp_print_ack_no(const u_char *bp)
139 {
140 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
141 const struct dccp_hdr_ack_bits *dh_ack =
142 (struct dccp_hdr_ack_bits *)(bp + dccp_basic_hdr_len(dh));
143 u_int32_t ack_high;
144 u_int64_t ackno;
145
146 TCHECK2(*dh_ack,4);
147 ack_high = DCCPH_ACK(dh_ack);
148 ackno = EXTRACT_24BITS(&ack_high) & 0xFFFFFF;
149
150 if (DCCPH_X(dh) != 0) {
151 u_int32_t ack_low;
152
153 TCHECK2(*dh_ack,8);
154 ack_low = dh_ack->dccph_ack_nr_low;
155
156 ackno &= 0x00FFFF; /* clear reserved field */
157 ackno = (ackno << 32) + EXTRACT_32BITS(&ack_low);
158 }
159
160 (void)printf("(ack=%" PRIu64 ") ", ackno);
161 trunc:
162 return;
163 }
164
165 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type)
166 {
167 if (type == DCCP_PKT_DATA)
168 return 0;
169 if (type == DCCP_PKT_DATAACK ||
170 type == DCCP_PKT_ACK ||
171 type == DCCP_PKT_SYNC ||
172 type == DCCP_PKT_SYNCACK ||
173 type == DCCP_PKT_CLOSE ||
174 type == DCCP_PKT_CLOSEREQ)
175 return sizeof(struct dccp_hdr_ack_bits);
176 if (type == DCCP_PKT_REQUEST)
177 return sizeof(struct dccp_hdr_request);
178 if (type == DCCP_PKT_RESPONSE)
179 return sizeof(struct dccp_hdr_response);
180 return sizeof(struct dccp_hdr_reset);
181 }
182
183 static int dccp_print_option(const u_char *option);
184
185 /**
186 * dccp_print - show dccp packet
187 * @bp - beginning of dccp packet
188 * @data2 - beginning of enclosing
189 * @len - lenght of ip packet
190 */
191 void dccp_print(const u_char *bp, const u_char *data2, u_int len)
192 {
193 const struct dccp_hdr *dh;
194 const struct ip *ip;
195 #ifdef INET6
196 const struct ip6_hdr *ip6;
197 #endif
198 const u_char *cp;
199 u_short sport, dport;
200 u_int hlen;
201 u_int extlen = 0;
202
203 dh = (const struct dccp_hdr *)bp;
204
205 ip = (struct ip *)data2;
206 #ifdef INET6
207 if (IP_V(ip) == 6)
208 ip6 = (const struct ip6_hdr *)data2;
209 else
210 ip6 = NULL;
211 #endif /*INET6*/
212 cp = (const u_char *)(dh + 1);
213 if (cp > snapend) {
214 printf("[Invalid packet|dccp]");
215 return;
216 }
217
218 if (len < sizeof(struct dccp_hdr)) {
219 printf("truncated-dccp - %ld bytes missing!",
220 (long)len - sizeof(struct dccp_hdr));
221 return;
222 }
223
224 sport = EXTRACT_16BITS(&dh->dccph_sport);
225 dport = EXTRACT_16BITS(&dh->dccph_dport);
226 hlen = dh->dccph_doff * 4;
227
228 #ifdef INET6
229 if (ip6) {
230 (void)printf("%s.%d > %s.%d: ",
231 ip6addr_string(&ip6->ip6_src), sport,
232 ip6addr_string(&ip6->ip6_dst), dport);
233 } else
234 #endif /*INET6*/
235 {
236 (void)printf("%s.%d > %s.%d: ",
237 ipaddr_string(&ip->ip_src), sport,
238 ipaddr_string(&ip->ip_dst), dport);
239 }
240 fflush(stdout);
241
242 if (qflag) {
243 (void)printf(" %d", len - hlen);
244 if (hlen > len) {
245 (void)printf("dccp [bad hdr length %u - too long, > %u]",
246 hlen, len);
247 }
248 return;
249 }
250
251 /* other variables in generic header */
252 if (vflag) {
253 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh));
254 }
255
256 /* checksum calculation */
257 if (vflag && TTEST2(bp[0], len)) {
258 u_int16_t sum = 0, dccp_sum;
259
260 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
261 (void)printf("cksum 0x%04x ", dccp_sum);
262 if (IP_V(ip) == 4)
263 sum = dccp_cksum(ip, dh, len);
264 #ifdef INET6
265 else if (IP_V(ip) == 6)
266 sum = dccp6_cksum(ip6, dh, len);
267 #endif
268 if (sum != 0)
269 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
270 else
271 (void)printf("(correct), ");
272 }
273
274 switch (DCCPH_TYPE(dh)) {
275 case DCCP_PKT_REQUEST: {
276 struct dccp_hdr_request *dhr =
277 (struct dccp_hdr_request *)(bp + dccp_basic_hdr_len(dh));
278 TCHECK(*dhr);
279 (void)printf("request (service=%d) ",
280 EXTRACT_32BITS(&dhr->dccph_req_service));
281 extlen += 4;
282 break;
283 }
284 case DCCP_PKT_RESPONSE: {
285 struct dccp_hdr_response *dhr =
286 (struct dccp_hdr_response *)(bp + dccp_basic_hdr_len(dh));
287 TCHECK(*dhr);
288 (void)printf("response (service=%d) ",
289 EXTRACT_32BITS(&dhr->dccph_resp_service));
290 extlen += 12;
291 break;
292 }
293 case DCCP_PKT_DATA:
294 (void)printf("data ");
295 break;
296 case DCCP_PKT_ACK: {
297 (void)printf("ack ");
298 extlen += 8;
299 break;
300 }
301 case DCCP_PKT_DATAACK: {
302 (void)printf("dataack ");
303 extlen += 8;
304 break;
305 }
306 case DCCP_PKT_CLOSEREQ:
307 (void)printf("closereq ");
308 extlen += 8;
309 break;
310 case DCCP_PKT_CLOSE:
311 (void)printf("close ");
312 extlen += 8;
313 break;
314 case DCCP_PKT_RESET: {
315 struct dccp_hdr_reset *dhr =
316 (struct dccp_hdr_reset *)(bp + dccp_basic_hdr_len(dh));
317 TCHECK(*dhr);
318 (void)printf("reset (code=%s) ",
319 dccp_reset_code(dhr->dccph_reset_code));
320 extlen += 12;
321 break;
322 }
323 case DCCP_PKT_SYNC:
324 (void)printf("sync ");
325 extlen += 8;
326 break;
327 case DCCP_PKT_SYNCACK:
328 (void)printf("syncack ");
329 extlen += 8;
330 break;
331 default:
332 (void)printf("invalid ");
333 break;
334 }
335
336 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
337 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
338 dccp_print_ack_no(bp);
339
340 if (vflag < 2)
341 return;
342
343 (void)printf("seq %" PRIu64, dccp_seqno(dh));
344
345 /* process options */
346 if (hlen > dccp_basic_hdr_len(dh) + extlen){
347 const u_char *cp;
348 u_int optlen;
349 cp = bp + dccp_basic_hdr_len(dh) + extlen;
350 printf(" <");
351
352 hlen -= dccp_basic_hdr_len(dh) + extlen;
353 while(1){
354 TCHECK(*cp);
355 optlen = dccp_print_option(cp);
356 if (!optlen) goto trunc2;
357 if (hlen <= optlen) break;
358 hlen -= optlen;
359 cp += optlen;
360 printf(", ");
361 }
362 printf(">");
363 }
364 return;
365 trunc:
366 printf("[|dccp]");
367 trunc2:
368 return;
369 }
370
371 static int dccp_print_option(const u_char *option)
372 {
373 u_int8_t optlen, i;
374
375 TCHECK(*option);
376
377 if (*option >= 32) {
378 TCHECK(*(option+1));
379 optlen = *(option +1);
380 if (optlen < 2) {
381 printf("Option %d optlen too short",*option);
382 return 1;
383 }
384 } else optlen = 1;
385
386 TCHECK2(*option,optlen);
387
388 switch (*option){
389 case 0:
390 printf("nop");
391 break;
392 case 1:
393 printf("mandatory");
394 break;
395 case 2:
396 printf("slowreceiver");
397 break;
398 case 32:
399 printf("change_l");
400 if (*(option +2) < 10){
401 printf(" %s", dccp_feature_nums[*(option +2)]);
402 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
403 }
404 break;
405 case 33:
406 printf("confirm_l");
407 if (*(option +2) < 10){
408 printf(" %s", dccp_feature_nums[*(option +2)]);
409 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
410 }
411 break;
412 case 34:
413 printf("change_r");
414 if (*(option +2) < 10){
415 printf(" %s", dccp_feature_nums[*(option +2)]);
416 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
417 }
418 break;
419 case 35:
420 printf("confirm_r");
421 if (*(option +2) < 10){
422 printf(" %s", dccp_feature_nums[*(option +2)]);
423 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
424 }
425 break;
426 case 36:
427 printf("initcookie 0x");
428 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
429 break;
430 case 37:
431 printf("ndp_count");
432 for (i = 0; i < optlen -2; i ++) printf(" %d", *(option +2 + i));
433 break;
434 case 38:
435 printf("ack_vector0 0x");
436 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
437 break;
438 case 39:
439 printf("ack_vector1 0x");
440 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
441 break;
442 case 40:
443 printf("data_dropped 0x");
444 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
445 break;
446 case 41:
447 printf("timestamp %u", EXTRACT_32BITS(option + 2));
448 break;
449 case 42:
450 printf("timestamp_echo %u", EXTRACT_32BITS(option + 2));
451 break;
452 case 43:
453 printf("elapsed_time ");
454 if (optlen == 6)
455 printf("%u", EXTRACT_32BITS(option + 2));
456 else
457 printf("%u", EXTRACT_16BITS(option + 2));
458 break;
459 case 44:
460 printf("data_checksum ");
461 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
462 break;
463 default :
464 if (*option >= 128) {
465 printf("CCID option %d",*option);
466 switch (optlen) {
467 case 4:
468 printf(" %u", EXTRACT_16BITS(option + 2));
469 break;
470 case 6:
471 printf(" %u", EXTRACT_32BITS(option + 2));
472 break;
473 default:
474 break;
475 }
476 break;
477 }
478
479 printf("unknown_opt %d", *option);
480 break;
481 }
482
483 return optlen;
484 trunc:
485 printf("[|dccp]");
486 return 0;
487 }