]> The Tcpdump Group git mirrors - tcpdump/blob - print-dccp.c
Propagate from main branch:
[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.1.2.4 2005-12-05 21:46:37 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 int dccp_cksum(const struct ip *ip,
64 const struct dccp_hdr *dh, u_int len)
65 {
66 union phu {
67 struct phdr {
68 u_int32_t src;
69 u_int32_t dst;
70 u_char mbz;
71 u_char proto;
72 u_int16_t len;
73 } ph;
74 u_int16_t pa[6];
75 } phu;
76 const u_int16_t *sp;
77
78 /* pseudo-header.. */
79 phu.ph.mbz = 0;
80 phu.ph.len = htons(len);
81 phu.ph.proto = IPPROTO_DCCP;
82 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
83 if (IP_HL(ip) == 5)
84 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
85 else
86 phu.ph.dst = ip_finddst(ip);
87
88 sp = &phu.pa[0];
89 return in_cksum((u_short *)dh, len, sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
90 }
91
92 #ifdef INET6
93 static int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len)
94 {
95 size_t i;
96 const u_int16_t *sp;
97 u_int32_t sum;
98 union {
99 struct {
100 struct in6_addr ph_src;
101 struct in6_addr ph_dst;
102 u_int32_t ph_len;
103 u_int8_t ph_zero[3];
104 u_int8_t ph_nxt;
105 } ph;
106 u_int16_t pa[20];
107 } phu;
108
109 /* pseudo-header */
110 memset(&phu, 0, sizeof(phu));
111 phu.ph.ph_src = ip6->ip6_src;
112 phu.ph.ph_dst = ip6->ip6_dst;
113 phu.ph.ph_len = htonl(len);
114 phu.ph.ph_nxt = IPPROTO_DCCP;
115
116 sum = 0;
117 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
118 sum += phu.pa[i];
119
120 sp = (const u_int16_t *)dh;
121
122 for (i = 0; i < (len & ~1); i += 2)
123 sum += *sp++;
124
125 if (len & 1)
126 sum += htons((*(const u_int8_t *)sp) << 8);
127
128 while (sum > 0xffff)
129 sum = (sum & 0xffff) + (sum >> 16);
130 sum = ~sum & 0xffff;
131
132 return (sum);
133 }
134 #endif
135
136 static const char *dccp_reset_code(u_int8_t code)
137 {
138 if (code >= __DCCP_RESET_CODE_LAST)
139 return "invalid";
140 return dccp_reset_codes[code];
141 }
142
143 static u_int64_t dccp_seqno(const struct dccp_hdr *dh)
144 {
145 u_int32_t seq_high = DCCPH_SEQ(dh);
146 u_int64_t seqno = EXTRACT_24BITS(&seq_high) & 0xFFFFFF;
147
148 if (DCCPH_X(dh) != 0) {
149 const struct dccp_hdr_ext *dhx = (void *)(dh + 1);
150 u_int32_t seq_low = dhx->dccph_seq_low;
151 seqno &= 0x00FFFF; /* clear reserved field */
152 seqno = (seqno << 32) + EXTRACT_32BITS(&seq_low);
153 }
154
155 return seqno;
156 }
157
158 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh)
159 {
160 return sizeof(*dh) + (DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : 0);
161 }
162
163 static void dccp_print_ack_no(const u_char *bp)
164 {
165 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
166 const struct dccp_hdr_ack_bits *dh_ack =
167 (struct dccp_hdr_ack_bits *)(bp + dccp_basic_hdr_len(dh));
168
169 TCHECK2(*dh_ack,4);
170 u_int32_t ack_high = DCCPH_ACK(dh_ack);
171 u_int64_t ackno = EXTRACT_24BITS(&ack_high) & 0xFFFFFF;
172
173 if (DCCPH_X(dh) != 0) {
174 TCHECK2(*dh_ack,8);
175 u_int32_t ack_low = dh_ack->dccph_ack_nr_low;
176
177 ackno &= 0x00FFFF; /* clear reserved field */
178 ackno = (ackno << 32) + EXTRACT_32BITS(&ack_low);
179 }
180
181 (void)printf("(ack=%" PRIu64 ") ", ackno);
182 trunc:
183 return;
184 }
185
186 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type)
187 {
188 if (type == DCCP_PKT_DATA)
189 return 0;
190 if (type == DCCP_PKT_DATAACK ||
191 type == DCCP_PKT_ACK ||
192 type == DCCP_PKT_SYNC ||
193 type == DCCP_PKT_SYNCACK ||
194 type == DCCP_PKT_CLOSE ||
195 type == DCCP_PKT_CLOSEREQ)
196 return sizeof(struct dccp_hdr_ack_bits);
197 if (type == DCCP_PKT_REQUEST)
198 return sizeof(struct dccp_hdr_request);
199 if (type == DCCP_PKT_RESPONSE)
200 return sizeof(struct dccp_hdr_response);
201 return sizeof(struct dccp_hdr_reset);
202 }
203
204 static int dccp_print_option(const u_char *option);
205
206 /**
207 * dccp_print - show dccp packet
208 * @bp - beginning of dccp packet
209 * @data2 - beginning of enclosing
210 * @len - lenght of ip packet
211 */
212 void dccp_print(const u_char *bp, const u_char *data2, u_int len)
213 {
214 const struct dccp_hdr *dh;
215 const struct ip *ip;
216 #ifdef INET6
217 const struct ip6_hdr *ip6;
218 #endif
219 const u_char *cp;
220 u_short sport, dport;
221 u_int hlen;
222 u_int extlen = 0;
223
224 dh = (const struct dccp_hdr *)bp;
225
226 ip = (struct ip *)data2;
227 #ifdef INET6
228 if (IP_V(ip) == 6)
229 ip6 = (const struct ip6_hdr *)data2;
230 else
231 ip6 = NULL;
232 #endif /*INET6*/
233 cp = (const u_char *)(dh + 1);
234 if (cp > snapend) {
235 printf("[Invalid packet|dccp]");
236 return;
237 }
238
239 if (len < sizeof(struct dccp_hdr)) {
240 printf("truncated-dccp - %ld bytes missing!",
241 (long)len - sizeof(struct dccp_hdr));
242 return;
243 }
244
245 sport = EXTRACT_16BITS(&dh->dccph_sport);
246 dport = EXTRACT_16BITS(&dh->dccph_dport);
247 hlen = dh->dccph_doff * 4;
248
249 #ifdef INET6
250 if (ip6) {
251 (void)printf("%s.%d > %s.%d: ",
252 ip6addr_string(&ip6->ip6_src), sport,
253 ip6addr_string(&ip6->ip6_dst), dport);
254 } else
255 #endif /*INET6*/
256 {
257 (void)printf("%s.%d > %s.%d: ",
258 ipaddr_string(&ip->ip_src), sport,
259 ipaddr_string(&ip->ip_dst), dport);
260 }
261 fflush(stdout);
262
263 if (qflag) {
264 (void)printf(" %d", len - hlen);
265 if (hlen > len) {
266 (void)printf("dccp [bad hdr length %u - too long, > %u]",
267 hlen, len);
268 }
269 return;
270 }
271
272 /* other variables in generic header */
273 if (vflag) {
274 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh));
275 }
276
277 /* checksum calculation */
278 #ifdef INET6
279 if (ip6) {
280 if (ip6->ip6_plen && vflag) {
281 u_int16_t sum, dccp_sum;
282
283 sum = dccp6_cksum(ip6, dh, len);
284 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
285 printf("cksum 0x%04x", dccp_sum);
286 if (sum != 0) {
287 (void)printf(" (incorrect (-> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
288 } else
289 (void)printf(" (correct), ");
290 }
291 } else
292 #endif /* INET6 */
293 if (vflag)
294 {
295 u_int16_t sum, dccp_sum;
296
297 sum = dccp_cksum(ip, dh, len);
298 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
299 printf("cksum 0x%04x", dccp_sum);
300 if (sum != 0) {
301 (void)printf(" (incorrect (-> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
302 } else
303 (void)printf(" (correct), ");
304 }
305
306 switch (DCCPH_TYPE(dh)) {
307 case DCCP_PKT_REQUEST: {
308 struct dccp_hdr_request *dhr =
309 (struct dccp_hdr_request *)(bp + dccp_basic_hdr_len(dh));
310 TCHECK(*dhr);
311 (void)printf("request (service=%d) ", dhr->dccph_req_service);
312 extlen += 4;
313 break;
314 }
315 case DCCP_PKT_RESPONSE: {
316 struct dccp_hdr_response *dhr =
317 (struct dccp_hdr_response *)(bp + dccp_basic_hdr_len(dh));
318 TCHECK(*dhr);
319 (void)printf("response (service=%d) ", dhr->dccph_resp_service);
320 extlen += 12;
321 break;
322 }
323 case DCCP_PKT_DATA:
324 (void)printf("data ");
325 break;
326 case DCCP_PKT_ACK: {
327 (void)printf("ack ");
328 extlen += 8;
329 break;
330 }
331 case DCCP_PKT_DATAACK: {
332 (void)printf("dataack ");
333 extlen += 8;
334 break;
335 }
336 case DCCP_PKT_CLOSEREQ:
337 (void)printf("closereq ");
338 extlen += 8;
339 break;
340 case DCCP_PKT_CLOSE:
341 (void)printf("close ");
342 extlen += 8;
343 break;
344 case DCCP_PKT_RESET: {
345 struct dccp_hdr_reset *dhr =
346 (struct dccp_hdr_reset *)(bp + dccp_basic_hdr_len(dh));
347 TCHECK(*dhr);
348 (void)printf("reset (code=%s) ",
349 dccp_reset_code(dhr->dccph_reset_code));
350 extlen += 12;
351 break;
352 }
353 case DCCP_PKT_SYNC:
354 (void)printf("sync ");
355 extlen += 8;
356 break;
357 case DCCP_PKT_SYNCACK:
358 (void)printf("syncack ");
359 extlen += 8;
360 break;
361 default:
362 (void)printf("invalid ");
363 break;
364 }
365
366 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
367 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
368 dccp_print_ack_no(bp);
369
370 if (vflag < 2)
371 return;
372
373 (void)printf("seq %" PRIu64, dccp_seqno(dh));
374
375 /* process options */
376 if (hlen > dccp_basic_hdr_len(dh) + extlen){
377 const u_char *cp;
378 u_int optlen;
379 cp = bp + dccp_basic_hdr_len(dh) + extlen;
380 printf(" <");
381
382 hlen -= dccp_basic_hdr_len(dh) + extlen;
383 while(1){
384 TCHECK(*cp);
385 optlen = dccp_print_option(cp);
386 if (!optlen) goto trunc2;
387 if (hlen <= optlen) break;
388 hlen -= optlen;
389 cp += optlen;
390 printf(", ");
391 }
392 printf(">");
393 }
394 return;
395 trunc:
396 printf("[|dccp]");
397 trunc2:
398 return;
399 }
400
401 static int dccp_print_option(const u_char *option)
402 {
403 u_int8_t optlen, i;
404 u_int32_t *ts;
405 u_int16_t *var16;
406 u_int32_t *var32;
407
408 TCHECK(*option);
409
410 if (*option >= 32) {
411 TCHECK(*(option+1));
412 optlen = *(option +1);
413 if (optlen < 2) {
414 printf("Option %d optlen too short",*option);
415 return 1;
416 }
417 } else optlen = 1;
418
419 TCHECK2(*option,optlen);
420
421 switch (*option){
422 case 0:
423 printf("nop");
424 break;
425 case 1:
426 printf("mandatory");
427 break;
428 case 2:
429 printf("slowreceiver");
430 break;
431 case 32:
432 printf("change_l");
433 if (*(option +2) < 10){
434 printf(" %s", dccp_feature_nums[*(option +2)]);
435 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
436 }
437 break;
438 case 33:
439 printf("confirm_l");
440 if (*(option +2) < 10){
441 printf(" %s", dccp_feature_nums[*(option +2)]);
442 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
443 }
444 break;
445 case 34:
446 printf("change_r");
447 if (*(option +2) < 10){
448 printf(" %s", dccp_feature_nums[*(option +2)]);
449 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
450 }
451 break;
452 case 35:
453 printf("confirm_r");
454 if (*(option +2) < 10){
455 printf(" %s", dccp_feature_nums[*(option +2)]);
456 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
457 }
458 break;
459 case 36:
460 printf("initcookie 0x");
461 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
462 break;
463 case 37:
464 printf("ndp_count");
465 for (i = 0; i < optlen -2; i ++) printf(" %d", *(option +2 + i));
466 break;
467 case 38:
468 printf("ack_vector0 0x");
469 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
470 break;
471 case 39:
472 printf("ack_vector1 0x");
473 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
474 break;
475 case 40:
476 printf("data_dropped 0x");
477 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
478 break;
479 case 41:
480 ts = (u_int32_t *)(option + 2);
481 printf("timestamp %u", (u_int32_t)ntohl(*ts));
482 break;
483 case 42:
484 ts = (u_int32_t *)(option + 2);
485 printf("timestamp_echo %u", (u_int32_t)ntohl(*ts));
486 break;
487 case 43:
488 printf("elapsed_time ");
489 if (optlen == 6){
490 ts = (u_int32_t *)(option + 2);
491 printf("%u", (u_int32_t)ntohl(*ts));
492 } else {
493 var16 = (u_int16_t *)(option + 2);
494 printf("%u", ntohs(*var16));
495 }
496 break;
497 case 44:
498 printf("data_checksum ");
499 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
500 break;
501 default :
502 if (*option >= 128) {
503 printf("CCID option %d",*option);
504 switch (optlen) {
505 case 4:
506 var16 = (u_int16_t *)(option + 2);
507 printf(" %u",ntohs(*var16));
508 break;
509 case 6:
510 var32 = (u_int32_t *)(option + 2);
511 printf(" %u",(u_int32_t)ntohl(*var32));
512 break;
513 default:
514 break;
515 }
516 break;
517 }
518
519 printf("unknown_opt %d", *option);
520 break;
521 }
522
523 return optlen;
524 trunc:
525 printf("[|dccp]");
526 return 0;
527 }