print-dccp.c revision 236192
1156952Sume/* 2156952Sume * Copyright (C) Arnaldo Carvalho de Melo 2004 3156952Sume * Copyright (C) Ian McDonald 2005 4156952Sume * Copyright (C) Yoshifumi Nishida 2005 5156952Sume * 6156952Sume * This software may be distributed either under the terms of the 7156952Sume * BSD-style license that accompanies tcpdump or the GNU GPL version 2 8156952Sume */ 9156952Sume 10156952Sume#ifndef lint 11156952Sumestatic const char rcsid[] _U_ = 12156952Sume "@(#) $Header: /tcpdump/master/tcpdump/print-dccp.c,v 1.8 2007-11-09 00:44:09 guy Exp $ (LBL)"; 13156952Sume#endif 14156952Sume 15156952Sume#ifdef HAVE_CONFIG_H 16156952Sume#include "config.h" 17156952Sume#endif 18170244Sume 19170244Sume#include <tcpdump-stdinc.h> 20170244Sume 21156952Sume#include "dccp.h" 22170244Sume 23156956Sume#include <stdio.h> 24156952Sume#include <string.h> 25156952Sume 26156952Sume#include "interface.h" 27156952Sume#include "addrtoname.h" 28156952Sume#include "extract.h" /* must come after interface.h */ 29156952Sume#include "ip.h" 30156952Sume#ifdef INET6 31156952Sume#include "ip6.h" 32156952Sume#endif 33156952Sume#include "ipproto.h" 34156952Sume 35156952Sumestatic const char *dccp_reset_codes[] = { 36156952Sume "unspecified", 37156952Sume "closed", 38156952Sume "aborted", 39156952Sume "no_connection", 40156952Sume "packet_error", 41156952Sume "option_error", 42156952Sume "mandatory_error", 43156956Sume "connection_refused", 44156956Sume "bad_service_code", 45156952Sume "too_busy", 46156952Sume "bad_init_cookie", 47156956Sume "aggression_penalty", 48156952Sume}; 49156952Sume 50156952Sumestatic const char *dccp_feature_nums[] = { 51156952Sume "reserved", 52165258Sume "ccid", 53165258Sume "allow_short_seqno", 54156952Sume "sequence_window", 55156952Sume "ecn_incapable", 56156952Sume "ack_ratio", 57156952Sume "send_ack_vector", 58156952Sume "send_ndp_count", 59156952Sume "minimum checksum coverage", 60156952Sume "check data checksum", 61156952Sume}; 62156952Sume 63156952Sumestatic inline u_int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len) 64156952Sume{ 65156952Sume u_int cov; 66156952Sume 67156952Sume if (DCCPH_CSCOV(dh) == 0) 68156952Sume return len; 69156952Sume cov = (dh->dccph_doff + DCCPH_CSCOV(dh) - 1) * sizeof(u_int32_t); 70156952Sume return (cov > len)? len : cov; 71156952Sume} 72156952Sume 73156952Sumestatic int dccp_cksum(const struct ip *ip, 74156952Sume const struct dccp_hdr *dh, u_int len) 75156952Sume{ 76156952Sume return nextproto4_cksum(ip, (const u_int8_t *)(void *)dh, 77156952Sume dccp_csum_coverage(dh, len), IPPROTO_DCCP); 78156952Sume} 79156952Sume 80156952Sume#ifdef INET6 81156952Sumestatic int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len) 82156952Sume{ 83156952Sume return nextproto6_cksum(ip6, (const u_int8_t *)(void *)dh, 84170244Sume dccp_csum_coverage(dh, len), IPPROTO_DCCP); 85170244Sume} 86170244Sume#endif 87156952Sume 88156952Sumestatic const char *dccp_reset_code(u_int8_t code) 89156952Sume{ 90156952Sume if (code >= __DCCP_RESET_CODE_LAST) 91156952Sume return "invalid"; 92156956Sume return dccp_reset_codes[code]; 93156952Sume} 94156952Sume 95156952Sumestatic u_int64_t dccp_seqno(const struct dccp_hdr *dh) 96156952Sume{ 97156952Sume u_int32_t seq_high = DCCPH_SEQ(dh); 98156952Sume u_int64_t seqno = EXTRACT_24BITS(&seq_high) & 0xFFFFFF; 99156952Sume 100156952Sume if (DCCPH_X(dh) != 0) { 101156952Sume const struct dccp_hdr_ext *dhx = (void *)(dh + 1); 102156952Sume u_int32_t seq_low = dhx->dccph_seq_low; 103156952Sume seqno &= 0x00FFFF; /* clear reserved field */ 104156952Sume seqno = (seqno << 32) + EXTRACT_32BITS(&seq_low); 105156952Sume } 106156952Sume 107156952Sume return seqno; 108156952Sume} 109156952Sume 110156952Sumestatic inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh) 111156952Sume{ 112156952Sume return sizeof(*dh) + (DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : 0); 113156952Sume} 114156952Sume 115156952Sumestatic void dccp_print_ack_no(const u_char *bp) 116156952Sume{ 117156952Sume const struct dccp_hdr *dh = (const struct dccp_hdr *)bp; 118156952Sume const struct dccp_hdr_ack_bits *dh_ack = 119156952Sume (struct dccp_hdr_ack_bits *)(bp + dccp_basic_hdr_len(dh)); 120156952Sume u_int32_t ack_high; 121156952Sume u_int64_t ackno; 122156952Sume 123156952Sume TCHECK2(*dh_ack,4); 124156952Sume ack_high = DCCPH_ACK(dh_ack); 125156952Sume ackno = EXTRACT_24BITS(&ack_high) & 0xFFFFFF; 126156952Sume 127156952Sume if (DCCPH_X(dh) != 0) { 128156952Sume u_int32_t ack_low; 129156952Sume 130156952Sume TCHECK2(*dh_ack,8); 131156952Sume ack_low = dh_ack->dccph_ack_nr_low; 132156952Sume 133156952Sume ackno &= 0x00FFFF; /* clear reserved field */ 134170244Sume ackno = (ackno << 32) + EXTRACT_32BITS(&ack_low); 135156952Sume } 136156952Sume 137156952Sume (void)printf("(ack=%" PRIu64 ") ", ackno); 138156952Sumetrunc: 139156952Sume return; 140156952Sume} 141156952Sume 142156952Sumestatic inline unsigned int dccp_packet_hdr_len(const u_int8_t type) 143156952Sume{ 144156952Sume if (type == DCCP_PKT_DATA) 145156952Sume return 0; 146156952Sume if (type == DCCP_PKT_DATAACK || 147156952Sume type == DCCP_PKT_ACK || 148156952Sume type == DCCP_PKT_SYNC || 149156952Sume type == DCCP_PKT_SYNCACK || 150156952Sume type == DCCP_PKT_CLOSE || 151156952Sume type == DCCP_PKT_CLOSEREQ) 152156952Sume return sizeof(struct dccp_hdr_ack_bits); 153156952Sume if (type == DCCP_PKT_REQUEST) 154156952Sume return sizeof(struct dccp_hdr_request); 155156952Sume if (type == DCCP_PKT_RESPONSE) 156156952Sume return sizeof(struct dccp_hdr_response); 157156952Sume return sizeof(struct dccp_hdr_reset); 158156952Sume} 159156952Sume 160156952Sumestatic int dccp_print_option(const u_char *option); 161156952Sume 162156952Sume/** 163156952Sume * dccp_print - show dccp packet 164156952Sume * @bp - beginning of dccp packet 165156952Sume * @data2 - beginning of enclosing 166156952Sume * @len - lenght of ip packet 167156952Sume */ 168156952Sumevoid dccp_print(const u_char *bp, const u_char *data2, u_int len) 169156952Sume{ 170156952Sume const struct dccp_hdr *dh; 171156952Sume const struct ip *ip; 172156952Sume#ifdef INET6 173156952Sume const struct ip6_hdr *ip6; 174156952Sume#endif 175156952Sume const u_char *cp; 176156952Sume u_short sport, dport; 177156952Sume u_int hlen; 178156952Sume u_int extlen = 0; 179156952Sume 180156952Sume dh = (const struct dccp_hdr *)bp; 181156952Sume 182156956Sume ip = (struct ip *)data2; 183156952Sume#ifdef INET6 184156952Sume if (IP_V(ip) == 6) 185156952Sume ip6 = (const struct ip6_hdr *)data2; 186156952Sume else 187156952Sume ip6 = NULL; 188156952Sume#endif /*INET6*/ 189156952Sume cp = (const u_char *)(dh + 1); 190156952Sume if (cp > snapend) { 191156952Sume printf("[Invalid packet|dccp]"); 192156952Sume return; 193156952Sume } 194156952Sume 195156952Sume if (len < sizeof(struct dccp_hdr)) { 196156952Sume printf("truncated-dccp - %ld bytes missing!", 197156952Sume (long)len - sizeof(struct dccp_hdr)); 198156952Sume return; 199156952Sume } 200156952Sume 201156952Sume sport = EXTRACT_16BITS(&dh->dccph_sport); 202156952Sume dport = EXTRACT_16BITS(&dh->dccph_dport); 203156952Sume hlen = dh->dccph_doff * 4; 204156952Sume 205156952Sume#ifdef INET6 206156952Sume if (ip6) { 207156952Sume (void)printf("%s.%d > %s.%d: ", 208156952Sume ip6addr_string(&ip6->ip6_src), sport, 209156952Sume ip6addr_string(&ip6->ip6_dst), dport); 210156952Sume } else 211156952Sume#endif /*INET6*/ 212156952Sume { 213156952Sume (void)printf("%s.%d > %s.%d: ", 214156952Sume ipaddr_string(&ip->ip_src), sport, 215156952Sume ipaddr_string(&ip->ip_dst), dport); 216156952Sume } 217156952Sume fflush(stdout); 218156956Sume 219156952Sume if (qflag) { 220156952Sume (void)printf(" %d", len - hlen); 221156952Sume if (hlen > len) { 222156952Sume (void)printf("dccp [bad hdr length %u - too long, > %u]", 223156952Sume hlen, len); 224156952Sume } 225156952Sume return; 226156952Sume } 227156952Sume 228156952Sume /* other variables in generic header */ 229156952Sume if (vflag) { 230156952Sume (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh)); 231156952Sume } 232156952Sume 233156952Sume /* checksum calculation */ 234156952Sume if (vflag && TTEST2(bp[0], len)) { 235156952Sume u_int16_t sum = 0, dccp_sum; 236156952Sume 237156952Sume dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum); 238156952Sume (void)printf("cksum 0x%04x ", dccp_sum); 239156952Sume if (IP_V(ip) == 4) 240156952Sume sum = dccp_cksum(ip, dh, len); 241156952Sume#ifdef INET6 242156952Sume else if (IP_V(ip) == 6) 243156952Sume sum = dccp6_cksum(ip6, dh, len); 244156952Sume#endif 245156952Sume if (sum != 0) 246156952Sume (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum)); 247156952Sume else 248156952Sume (void)printf("(correct), "); 249156952Sume } 250156952Sume 251156952Sume switch (DCCPH_TYPE(dh)) { 252156952Sume case DCCP_PKT_REQUEST: { 253156952Sume struct dccp_hdr_request *dhr = 254156952Sume (struct dccp_hdr_request *)(bp + dccp_basic_hdr_len(dh)); 255156952Sume TCHECK(*dhr); 256156952Sume (void)printf("request (service=%d) ", 257156952Sume EXTRACT_32BITS(&dhr->dccph_req_service)); 258156952Sume extlen += 4; 259156952Sume break; 260156952Sume } 261156952Sume case DCCP_PKT_RESPONSE: { 262156952Sume struct dccp_hdr_response *dhr = 263156952Sume (struct dccp_hdr_response *)(bp + dccp_basic_hdr_len(dh)); 264156952Sume TCHECK(*dhr); 265156952Sume (void)printf("response (service=%d) ", 266156952Sume EXTRACT_32BITS(&dhr->dccph_resp_service)); 267156952Sume extlen += 12; 268156952Sume break; 269156952Sume } 270156952Sume case DCCP_PKT_DATA: 271156952Sume (void)printf("data "); 272156952Sume break; 273156952Sume case DCCP_PKT_ACK: { 274156952Sume (void)printf("ack "); 275156952Sume extlen += 8; 276156952Sume break; 277156952Sume } 278156952Sume case DCCP_PKT_DATAACK: { 279156952Sume (void)printf("dataack "); 280156952Sume extlen += 8; 281156952Sume break; 282156952Sume } 283156956Sume case DCCP_PKT_CLOSEREQ: 284156952Sume (void)printf("closereq "); 285156952Sume extlen += 8; 286162541Skan break; 287156952Sume case DCCP_PKT_CLOSE: 288162541Skan (void)printf("close "); 289156952Sume extlen += 8; 290156952Sume break; 291 case DCCP_PKT_RESET: { 292 struct dccp_hdr_reset *dhr = 293 (struct dccp_hdr_reset *)(bp + dccp_basic_hdr_len(dh)); 294 TCHECK(*dhr); 295 (void)printf("reset (code=%s) ", 296 dccp_reset_code(dhr->dccph_reset_code)); 297 extlen += 12; 298 break; 299 } 300 case DCCP_PKT_SYNC: 301 (void)printf("sync "); 302 extlen += 8; 303 break; 304 case DCCP_PKT_SYNCACK: 305 (void)printf("syncack "); 306 extlen += 8; 307 break; 308 default: 309 (void)printf("invalid "); 310 break; 311 } 312 313 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) && 314 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST)) 315 dccp_print_ack_no(bp); 316 317 if (vflag < 2) 318 return; 319 320 (void)printf("seq %" PRIu64, dccp_seqno(dh)); 321 322 /* process options */ 323 if (hlen > dccp_basic_hdr_len(dh) + extlen){ 324 const u_char *cp; 325 u_int optlen; 326 cp = bp + dccp_basic_hdr_len(dh) + extlen; 327 printf(" <"); 328 329 hlen -= dccp_basic_hdr_len(dh) + extlen; 330 while(1){ 331 TCHECK(*cp); 332 optlen = dccp_print_option(cp); 333 if (!optlen) goto trunc2; 334 if (hlen <= optlen) break; 335 hlen -= optlen; 336 cp += optlen; 337 printf(", "); 338 } 339 printf(">"); 340 } 341 return; 342trunc: 343 printf("[|dccp]"); 344trunc2: 345 return; 346} 347 348static int dccp_print_option(const u_char *option) 349{ 350 u_int8_t optlen, i; 351 352 TCHECK(*option); 353 354 if (*option >= 32) { 355 TCHECK(*(option+1)); 356 optlen = *(option +1); 357 if (optlen < 2) { 358 printf("Option %d optlen too short",*option); 359 return 1; 360 } 361 } else optlen = 1; 362 363 TCHECK2(*option,optlen); 364 365 switch (*option){ 366 case 0: 367 printf("nop"); 368 break; 369 case 1: 370 printf("mandatory"); 371 break; 372 case 2: 373 printf("slowreceiver"); 374 break; 375 case 32: 376 printf("change_l"); 377 if (*(option +2) < 10){ 378 printf(" %s", dccp_feature_nums[*(option +2)]); 379 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i)); 380 } 381 break; 382 case 33: 383 printf("confirm_l"); 384 if (*(option +2) < 10){ 385 printf(" %s", dccp_feature_nums[*(option +2)]); 386 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i)); 387 } 388 break; 389 case 34: 390 printf("change_r"); 391 if (*(option +2) < 10){ 392 printf(" %s", dccp_feature_nums[*(option +2)]); 393 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i)); 394 } 395 break; 396 case 35: 397 printf("confirm_r"); 398 if (*(option +2) < 10){ 399 printf(" %s", dccp_feature_nums[*(option +2)]); 400 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i)); 401 } 402 break; 403 case 36: 404 printf("initcookie 0x"); 405 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i)); 406 break; 407 case 37: 408 printf("ndp_count"); 409 for (i = 0; i < optlen -2; i ++) printf(" %d", *(option +2 + i)); 410 break; 411 case 38: 412 printf("ack_vector0 0x"); 413 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i)); 414 break; 415 case 39: 416 printf("ack_vector1 0x"); 417 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i)); 418 break; 419 case 40: 420 printf("data_dropped 0x"); 421 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i)); 422 break; 423 case 41: 424 printf("timestamp %u", EXTRACT_32BITS(option + 2)); 425 break; 426 case 42: 427 printf("timestamp_echo %u", EXTRACT_32BITS(option + 2)); 428 break; 429 case 43: 430 printf("elapsed_time "); 431 if (optlen == 6) 432 printf("%u", EXTRACT_32BITS(option + 2)); 433 else 434 printf("%u", EXTRACT_16BITS(option + 2)); 435 break; 436 case 44: 437 printf("data_checksum "); 438 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i)); 439 break; 440 default : 441 if (*option >= 128) { 442 printf("CCID option %d",*option); 443 switch (optlen) { 444 case 4: 445 printf(" %u", EXTRACT_16BITS(option + 2)); 446 break; 447 case 6: 448 printf(" %u", EXTRACT_32BITS(option + 2)); 449 break; 450 default: 451 break; 452 } 453 break; 454 } 455 456 printf("unknown_opt %d", *option); 457 break; 458 } 459 460 return optlen; 461trunc: 462 printf("[|dccp]"); 463 return 0; 464} 465