1 /* $OpenBSD: print-udp.c,v 1.51 2018/10/22 16:12:45 kn Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 24 #include <sys/time.h> 25 #include <sys/socket.h> 26 27 #include <netinet/in.h> 28 #include <netinet/ip.h> 29 #include <netinet/ip6.h> 30 #include <netinet/ip_var.h> 31 #include <netinet/udp.h> 32 #include <netinet/udp_var.h> 33 34 #include <net80211/ieee80211.h> 35 36 #ifdef NOERROR 37 #undef NOERROR /* Solaris sucks */ 38 #endif 39 #ifdef T_UNSPEC 40 #undef T_UNSPEC /* SINIX does too */ 41 #endif 42 #include <arpa/nameser.h> 43 #ifdef SEGSIZE 44 #undef SEGSIZE 45 #endif 46 #include <arpa/tftp.h> 47 48 #include <rpc/rpc.h> 49 50 #include <stdio.h> 51 #include <string.h> 52 53 #include "interface.h" 54 #include "addrtoname.h" 55 #include "extract.h" 56 #include "appletalk.h" 57 58 #include "nfsv2.h" 59 #include "bootp.h" 60 #include "iapp.h" 61 62 struct rtcphdr { 63 u_short rh_flags; /* T:2 P:1 CNT:5 PT:8 */ 64 u_short rh_len; /* length of message (in words) */ 65 u_int rh_ssrc; /* synchronization src id */ 66 }; 67 68 typedef struct { 69 u_int upper; /* more significant 32 bits */ 70 u_int lower; /* less significant 32 bits */ 71 } ntp64; 72 73 /* 74 * Sender report. 75 */ 76 struct rtcp_sr { 77 ntp64 sr_ntp; /* 64-bit ntp timestamp */ 78 u_int sr_ts; /* reference media timestamp */ 79 u_int sr_np; /* no. packets sent */ 80 u_int sr_nb; /* no. bytes sent */ 81 }; 82 83 /* 84 * Receiver report. 85 * Time stamps are middle 32-bits of ntp timestamp. 86 */ 87 struct rtcp_rr { 88 u_int rr_srcid; /* sender being reported */ 89 u_int rr_nl; /* no. packets lost */ 90 u_int rr_ls; /* extended last seq number received */ 91 u_int rr_dv; /* jitter (delay variance) */ 92 u_int rr_lsr; /* orig. ts from last rr from this src */ 93 u_int rr_dlsr; /* time from recpt of last rr to xmit time */ 94 }; 95 96 /*XXX*/ 97 #define RTCP_PT_SR 200 98 #define RTCP_PT_RR 201 99 #define RTCP_PT_SDES 202 100 #define RTCP_SDES_CNAME 1 101 #define RTCP_SDES_NAME 2 102 #define RTCP_SDES_EMAIL 3 103 #define RTCP_SDES_PHONE 4 104 #define RTCP_SDES_LOC 5 105 #define RTCP_SDES_TOOL 6 106 #define RTCP_SDES_NOTE 7 107 #define RTCP_SDES_PRIV 8 108 #define RTCP_PT_BYE 203 109 #define RTCP_PT_APP 204 110 111 static void 112 vat_print(const void *hdr, u_int len, const struct udphdr *up) 113 { 114 /* vat/vt audio */ 115 u_int ts = *(u_short *)hdr; 116 if ((ts & 0xf060) != 0) { 117 /* probably vt */ 118 (void)printf("udp/vt %u %d / %d", 119 (u_int32_t)(ntohs(up->uh_ulen) - sizeof(*up)), 120 ts & 0x3ff, ts >> 10); 121 } else { 122 /* probably vat */ 123 u_int i0 = ntohl(((u_int *)hdr)[0]); 124 u_int i1 = ntohl(((u_int *)hdr)[1]); 125 printf("udp/vat %u c%d %u%s", 126 (u_int32_t)(ntohs(up->uh_ulen) - sizeof(*up) - 8), 127 i0 & 0xffff, 128 i1, i0 & 0x800000? "*" : ""); 129 /* audio format */ 130 if (i0 & 0x1f0000) 131 printf(" f%d", (i0 >> 16) & 0x1f); 132 if (i0 & 0x3f000000) 133 printf(" s%d", (i0 >> 24) & 0x3f); 134 } 135 } 136 137 static void 138 rtp_print(const void *hdr, u_int len, const struct udphdr *up) 139 { 140 /* rtp v1 or v2 */ 141 u_int *ip = (u_int *)hdr; 142 u_int hasopt, hasext, contype, hasmarker; 143 u_int i0 = ntohl(((u_int *)hdr)[0]); 144 u_int i1 = ntohl(((u_int *)hdr)[1]); 145 u_int dlen = ntohs(up->uh_ulen) - sizeof(*up) - 8; 146 const char * ptype; 147 148 ip += 2; 149 len >>= 2; 150 len -= 2; 151 hasopt = 0; 152 hasext = 0; 153 if ((i0 >> 30) == 1) { 154 /* rtp v1 */ 155 hasopt = i0 & 0x800000; 156 contype = (i0 >> 16) & 0x3f; 157 hasmarker = i0 & 0x400000; 158 ptype = "rtpv1"; 159 } else { 160 /* rtp v2 */ 161 hasext = i0 & 0x10000000; 162 contype = (i0 >> 16) & 0x7f; 163 hasmarker = i0 & 0x800000; 164 dlen -= 4; 165 ptype = "rtp"; 166 ip += 1; 167 len -= 1; 168 } 169 printf(" udp/%s %d c%d %s%s %d %u", 170 ptype, 171 dlen, 172 contype, 173 (hasopt || hasext)? "+" : "", 174 hasmarker? "*" : "", 175 i0 & 0xffff, 176 i1); 177 if (vflag) { 178 printf(" %u", i1); 179 if (hasopt) { 180 u_int i2, optlen; 181 do { 182 i2 = ip[0]; 183 optlen = (i2 >> 16) & 0xff; 184 if (optlen == 0 || optlen > len) { 185 printf(" !opt"); 186 return; 187 } 188 ip += optlen; 189 len -= optlen; 190 } while ((int)i2 >= 0); 191 } 192 if (hasext) { 193 u_int i2, extlen; 194 i2 = ip[0]; 195 extlen = (i2 & 0xffff) + 1; 196 if (extlen > len) { 197 printf(" !ext"); 198 return; 199 } 200 ip += extlen; 201 } 202 if (contype == 0x1f) /*XXX H.261 */ 203 printf(" 0x%04x", ip[0] >> 16); 204 } 205 } 206 207 static const u_char * 208 rtcp_print(const u_char *hdr, const u_char *ep) 209 { 210 /* rtp v2 control (rtcp) */ 211 struct rtcp_rr *rr = NULL; 212 struct rtcp_sr *sr; 213 struct rtcphdr *rh = (struct rtcphdr *)hdr; 214 u_int len; 215 u_short flags; 216 int cnt; 217 double ts, dts; 218 if ((u_char *)(rh + 1) > ep) { 219 printf(" [|rtcp]"); 220 return (ep); 221 } 222 len = (ntohs(rh->rh_len) + 1) * 4; 223 flags = ntohs(rh->rh_flags); 224 cnt = (flags >> 8) & 0x1f; 225 switch (flags & 0xff) { 226 case RTCP_PT_SR: 227 sr = (struct rtcp_sr *)(rh + 1); 228 printf(" sr"); 229 if (len != cnt * sizeof(*rr) + sizeof(*sr) + sizeof(*rh)) 230 printf(" [%d]", len); 231 if (vflag) 232 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc)); 233 if ((u_char *)(sr + 1) > ep) { 234 printf(" [|rtcp]"); 235 return (ep); 236 } 237 ts = (double)((u_int32_t)ntohl(sr->sr_ntp.upper)) + 238 ((double)((u_int32_t)ntohl(sr->sr_ntp.lower)) / 239 4294967296.0); 240 printf(" @%.2f %u %up %ub", ts, (u_int32_t)ntohl(sr->sr_ts), 241 (u_int32_t)ntohl(sr->sr_np), (u_int32_t)ntohl(sr->sr_nb)); 242 rr = (struct rtcp_rr *)(sr + 1); 243 break; 244 case RTCP_PT_RR: 245 printf(" rr"); 246 if (len != cnt * sizeof(*rr) + sizeof(*rh)) 247 printf(" [%d]", len); 248 rr = (struct rtcp_rr *)(rh + 1); 249 if (vflag) 250 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc)); 251 break; 252 case RTCP_PT_SDES: 253 printf(" sdes %d", len); 254 if (vflag) 255 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc)); 256 cnt = 0; 257 break; 258 case RTCP_PT_BYE: 259 printf(" bye %d", len); 260 if (vflag) 261 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc)); 262 cnt = 0; 263 break; 264 default: 265 printf(" type-0x%x %d", flags & 0xff, len); 266 cnt = 0; 267 break; 268 } 269 if (cnt > 1) 270 printf(" c%d", cnt); 271 while (--cnt >= 0) { 272 if ((u_char *)(rr + 1) > ep) { 273 printf(" [|rtcp]"); 274 return (ep); 275 } 276 if (vflag) 277 printf(" %u", (u_int32_t)ntohl(rr->rr_srcid)); 278 ts = (double)((u_int32_t)ntohl(rr->rr_lsr)) / 65536.; 279 dts = (double)((u_int32_t)ntohl(rr->rr_dlsr)) / 65536.; 280 printf(" %ul %us %uj @%.2f+%.2f", 281 (u_int32_t)ntohl(rr->rr_nl) & 0x00ffffff, 282 (u_int32_t)ntohl(rr->rr_ls), 283 (u_int32_t)ntohl(rr->rr_dv), ts, dts); 284 } 285 return (hdr + len); 286 } 287 288 /* XXX probably should use getservbyname() and cache answers */ 289 #define TFTP_PORT 69 /*XXX*/ 290 #define KERBEROS_PORT 88 /*XXX*/ 291 #define SUNRPC_PORT 111 /*XXX*/ 292 #define NTP_PORT 123 /*XXX*/ 293 #define NETBIOS_NS_PORT 137 /*XXX*/ 294 #define NETBIOS_DGRAM_PORT 138 /*XXX*/ 295 #define SNMP_PORT 161 /*XXX*/ 296 #define SNMPTRAP_PORT 162 /*XXX*/ 297 #define ISAKMP_PORT 500 /*XXX*/ 298 #define RIP_PORT 520 /*XXX*/ 299 #define TIMED_PORT 525 /*XXX*/ 300 #define KERBEROS_SEC_PORT 750 /*XXX*/ 301 #define LWRES_PORT 921 302 #define VQP_PORT 1589 303 #define OLD_RADIUS_AUTH_PORT 1645 304 #define OLD_RADIUS_ACCT_PORT 1646 305 #define L2TP_PORT 1701 /*XXX*/ 306 #define RADIUS_AUTH_PORT 1812 307 #define RADIUS_ACCT_PORT 1813 308 #define HSRP_PORT 1985 /*XXX*/ 309 #define GTP_C_PORT 2123 310 #define GTP_U_PORT 2152 311 #define GTP_PRIME_PORT 3386 312 #define UDPENCAP_PORT 4500 /*XXX*/ 313 #define GRE_PORT 4754 314 #define VXLAN_PORT 4789 315 #define MULTICASTDNS_PORT 5353 316 #define MPLS_PORT 6635 317 318 #define RIPNG_PORT 521 /*XXX*/ 319 #define DHCP6_PORT1 546 /*XXX*/ 320 #define DHCP6_PORT2 547 /*XXX*/ 321 322 void 323 udp_print(const u_char *bp, u_int length, const void *iph) 324 { 325 const struct udphdr *up; 326 const u_char *cp; 327 const u_char *ep = bp + length; 328 u_int16_t sport, dport, ulen; 329 const char *ipsrc = NULL, *ipdst = NULL; 330 unsigned int ipv = 0; 331 uint32_t cksum = 0; 332 333 if (ep > snapend) 334 ep = snapend; 335 336 if (iph != NULL) { 337 const struct ip *ip = iph; 338 ipv = ip->ip_v; 339 340 switch (ipv) { 341 case 6: { 342 const struct ip6_hdr *ip6 = iph; 343 344 ipsrc = ip6addr_string(&ip6->ip6_src); 345 ipdst = ip6addr_string(&ip6->ip6_dst); 346 347 cksum = in_cksum_add(&ip6->ip6_src, 348 sizeof(ip6->ip6_src), cksum); 349 cksum = in_cksum_add(&ip6->ip6_dst, 350 sizeof(ip6->ip6_dst), cksum); 351 break; 352 } 353 case 4: 354 ipsrc = ipaddr_string(&ip->ip_src); 355 ipdst = ipaddr_string(&ip->ip_dst); 356 357 cksum = in_cksum_add(&ip->ip_src, 358 sizeof(ip->ip_src), cksum); 359 cksum = in_cksum_add(&ip->ip_dst, 360 sizeof(ip->ip_dst), cksum); 361 break; 362 } 363 } 364 365 up = (const struct udphdr *)bp; 366 cp = (const u_char *)(up + 1); 367 368 /* check if the udp header was captured */ 369 if (cp > snapend) { 370 if (ipv) 371 printf("%s > %s: ", ipsrc, ipdst); 372 373 printf("[|udp]"); 374 return; 375 } 376 377 /* check if the packet payload is long enough */ 378 if (length < sizeof(*up)) { 379 if (ipv) 380 printf("%s > %s: ", ipsrc, ipdst); 381 382 printf("truncated-udp %u", length); 383 return; 384 } 385 386 sport = ntohs(up->uh_sport); 387 dport = ntohs(up->uh_dport); 388 389 if (ipv) { 390 printf("%s.%s > %s.%s", 391 ipsrc, udpport_string(sport), 392 ipdst, udpport_string(dport)); 393 } else { 394 printf("udp %s > %s", 395 udpport_string(sport), 396 udpport_string(dport)); 397 } 398 399 printf(": "); 400 401 cksum += htons(length); 402 403 ulen = ntohs(up->uh_ulen); 404 if (length < ulen) 405 printf(" truncated-udp - %u bytes missing!", ulen - length); 406 407 length -= sizeof(*up); 408 409 if (vflag && ipv && TTEST2(cp[0], length)) { 410 uint16_t sum, usum = up->uh_sum; 411 412 if (usum == 0) { 413 if (ipv == 4) 414 printf("[no udp cksum] "); 415 else 416 printf("[invalid udp cksum 0] "); 417 } else { 418 cksum += htons(IPPROTO_UDP); 419 cksum += up->uh_sport; 420 cksum += up->uh_dport; 421 cksum += up->uh_ulen; 422 423 sum = in_cksum(cp, length, cksum); 424 425 if (sum == usum) 426 printf("[udp sum ok] "); 427 else { 428 printf("[bad udp cksum %04x! -> %04x] ", 429 usum, sum); 430 } 431 } 432 } 433 434 if (packettype) { 435 struct rpc_msg *rp; 436 enum msg_type direction; 437 438 switch (packettype) { 439 case PT_VAT: 440 vat_print(cp, length, up); 441 break; 442 443 case PT_WB: 444 wb_print(cp, length); 445 break; 446 447 case PT_RPC: 448 rp = (struct rpc_msg *)cp; 449 direction = (enum msg_type)ntohl(rp->rm_direction); 450 if (direction == CALL) 451 sunrpcrequest_print(cp, length, iph); 452 else 453 nfsreply_print(cp, length, iph); 454 break; 455 456 case PT_RTP: 457 rtp_print(cp, length, up); 458 break; 459 460 case PT_RTCP: 461 while (cp < ep) 462 cp = rtcp_print(cp, ep); 463 break; 464 case PT_CNFP: 465 cnfp_print(cp, length); 466 break; 467 case PT_GRE: 468 gre_print(cp, length); 469 break; 470 case PT_VXLAN: 471 vxlan_print(cp, length); 472 break; 473 case PT_MPLS: 474 mpls_print(cp, length); 475 break; 476 case PT_TFTP: 477 tftp_print(cp, length); 478 break; 479 } 480 return; 481 } 482 483 if (!qflag) { 484 struct rpc_msg *rp; 485 enum msg_type direction; 486 487 rp = (struct rpc_msg *)cp; 488 if (TTEST(rp->rm_direction)) { 489 direction = (enum msg_type)ntohl(rp->rm_direction); 490 if (dport == NFS_PORT && direction == CALL) { 491 nfsreq_print(cp, length, iph); 492 return; 493 } 494 if (sport == NFS_PORT && direction == REPLY) { 495 nfsreply_print(cp, length, iph); 496 return; 497 } 498 #ifdef notdef 499 if (dport == SUNRPC_PORT && direction == CALL) { 500 sunrpcrequest_print(cp, length, iph); 501 return; 502 } 503 #endif 504 } 505 if (TTEST(((struct LAP *)cp)->type) && 506 ((struct LAP *)cp)->type == lapDDP && 507 (atalk_port(sport) || atalk_port(dport))) { 508 if (vflag) 509 fputs("kip ", stdout); 510 atalk_print_llap(cp, length); 511 return; 512 } 513 } 514 515 if (!qflag) { 516 #define ISPORT(p) (dport == (p) || sport == (p)) 517 if (ISPORT(NAMESERVER_PORT)) 518 ns_print(cp, length, 0); 519 else if (ISPORT(MULTICASTDNS_PORT)) 520 ns_print(cp, length, 1); 521 else if (ISPORT(LWRES_PORT)) 522 lwres_print(cp, length); 523 else if (ISPORT(TIMED_PORT)) 524 timed_print(cp, length); 525 else if (ISPORT(TFTP_PORT)) 526 tftp_print(cp, length); 527 else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS)) 528 bootp_print(cp, length, sport, dport); 529 else if (ISPORT(RIP_PORT)) 530 rip_print(cp, length); 531 else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT)) 532 snmp_print(cp, length); 533 else if (ISPORT(NTP_PORT)) 534 ntp_print(cp, length); 535 else if (ISPORT(KERBEROS_PORT) || ISPORT(KERBEROS_SEC_PORT)) 536 krb_print(cp, length); 537 else if (ISPORT(L2TP_PORT)) 538 l2tp_print(cp, length); 539 else if (ISPORT(UDPENCAP_PORT)) 540 udpencap_print(cp, length, iph); 541 else if (ISPORT(ISAKMP_PORT)) 542 ike_print(cp, length); 543 #if 0 544 else if (ISPORT(NETBIOS_NS_PORT)) 545 nbt_udp137_print(cp, length); 546 else if (ISPORT(NETBIOS_DGRAM_PORT)) 547 nbt_udp138_print(cp, length); 548 #endif 549 else if (ISPORT(OLD_RADIUS_AUTH_PORT) || 550 ISPORT(OLD_RADIUS_ACCT_PORT) || 551 ISPORT(RADIUS_AUTH_PORT) || 552 ISPORT(RADIUS_ACCT_PORT)) 553 radius_print(cp, length); 554 else if (dport == 3456) 555 vat_print(cp, length, up); 556 else if (ISPORT(IAPP_PORT) || ISPORT(IAPP_OLD_PORT)) 557 iapp_print(cp, length); 558 else if (ISPORT(VQP_PORT)) 559 vqp_print(cp, length); 560 else if (ISPORT(GRE_PORT)) 561 gre_print(cp, length); 562 else if (ISPORT(VXLAN_PORT)) 563 vxlan_print(cp, length); 564 else if (ISPORT(MPLS_PORT)) 565 mpls_print(cp, length); 566 else if (ISPORT(RIPNG_PORT)) 567 ripng_print(cp, length); 568 else if (ISPORT(DHCP6_PORT1) || ISPORT(DHCP6_PORT2)) { 569 dhcp6_print(cp, length, sport, dport); 570 } 571 else if (ISPORT(GTP_C_PORT) || ISPORT(GTP_U_PORT) || 572 ISPORT(GTP_PRIME_PORT)) 573 gtp_print(cp, length, sport, dport); 574 /* 575 * Kludge in test for whiteboard packets. 576 */ 577 else if (dport == 4567) 578 wb_print(cp, length); 579 else if (dport == HSRP_PORT) 580 hsrp_print(cp, length); 581 else 582 printf("udp %u", length); 583 #undef ISPORT 584 } else 585 printf("udp %u", length); 586 } 587