2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-udp.c,v 1.142 2007-08-08 17:20:58 hannes Exp $ (LBL)";
33 #include <tcpdump-stdinc.h>
42 #include "interface.h"
43 #include "addrtoname.h"
45 #include "appletalk.h"
62 u_int16_t rh_flags; /* T:2 P:1 CNT:5 PT:8 */
63 u_int16_t rh_len; /* length of message (in words) */
64 u_int32_t rh_ssrc; /* synchronization src id */
68 u_int32_t upper; /* more significant 32 bits */
69 u_int32_t lower; /* less significant 32 bits */
76 ntp64 sr_ntp; /* 64-bit ntp timestamp */
77 u_int32_t sr_ts; /* reference media timestamp */
78 u_int32_t sr_np; /* no. packets sent */
79 u_int32_t sr_nb; /* no. bytes sent */
84 * Time stamps are middle 32-bits of ntp timestamp.
87 u_int32_t rr_srcid; /* sender being reported */
88 u_int32_t rr_nl; /* no. packets lost */
89 u_int32_t rr_ls; /* extended last seq number received */
90 u_int32_t rr_dv; /* jitter (delay variance) */
91 u_int32_t rr_lsr; /* orig. ts from last rr from this src */
92 u_int32_t rr_dlsr; /* time from recpt of last rr to xmit time */
96 #define RTCP_PT_SR 200
97 #define RTCP_PT_RR 201
98 #define RTCP_PT_SDES 202
99 #define RTCP_SDES_CNAME 1
100 #define RTCP_SDES_NAME 2
101 #define RTCP_SDES_EMAIL 3
102 #define RTCP_SDES_PHONE 4
103 #define RTCP_SDES_LOC 5
104 #define RTCP_SDES_TOOL 6
105 #define RTCP_SDES_NOTE 7
106 #define RTCP_SDES_PRIV 8
107 #define RTCP_PT_BYE 203
108 #define RTCP_PT_APP 204
111 vat_print(const void *hdr, register const struct udphdr *up)
114 u_int ts = *(u_int16_t *)hdr;
115 if ((ts & 0xf060) != 0) {
117 (void)printf("udp/vt %u %d / %d",
118 (u_int32_t)(EXTRACT_16BITS(&up->uh_ulen) - sizeof(*up)),
119 ts & 0x3ff, ts >> 10);
122 u_int32_t i0 = EXTRACT_32BITS(&((u_int *)hdr)[0]);
123 u_int32_t i1 = EXTRACT_32BITS(&((u_int *)hdr)[1]);
124 printf("udp/vat %u c%d %u%s",
125 (u_int32_t)(EXTRACT_16BITS(&up->uh_ulen) - sizeof(*up) - 8),
127 i1, i0 & 0x800000? "*" : "");
130 printf(" f%d", (i0 >> 16) & 0x1f);
132 printf(" s%d", (i0 >> 24) & 0x3f);
137 rtp_print(const void *hdr, u_int len, register const struct udphdr *up)
140 u_int *ip = (u_int *)hdr;
141 u_int hasopt, hasext, contype, hasmarker;
142 u_int32_t i0 = EXTRACT_32BITS(&((u_int *)hdr)[0]);
143 u_int32_t i1 = EXTRACT_32BITS(&((u_int *)hdr)[1]);
144 u_int dlen = EXTRACT_16BITS(&up->uh_ulen) - sizeof(*up) - 8;
152 if ((i0 >> 30) == 1) {
154 hasopt = i0 & 0x800000;
155 contype = (i0 >> 16) & 0x3f;
156 hasmarker = i0 & 0x400000;
160 hasext = i0 & 0x10000000;
161 contype = (i0 >> 16) & 0x7f;
162 hasmarker = i0 & 0x800000;
168 printf("udp/%s %d c%d %s%s %d %u",
172 (hasopt || hasext)? "+" : "",
177 printf(" %u", EXTRACT_32BITS(&((u_int *)hdr)[2]));
182 optlen = (i2 >> 16) & 0xff;
183 if (optlen == 0 || optlen > len) {
189 } while ((int)i2 >= 0);
194 extlen = (i2 & 0xffff) + 1;
201 if (contype == 0x1f) /*XXX H.261 */
202 printf(" 0x%04x", ip[0] >> 16);
206 static const u_char *
207 rtcp_print(const u_char *hdr, const u_char *ep)
209 /* rtp v2 control (rtcp) */
210 struct rtcp_rr *rr = 0;
212 struct rtcphdr *rh = (struct rtcphdr *)hdr;
217 if ((u_char *)(rh + 1) > ep) {
221 len = (EXTRACT_16BITS(&rh->rh_len) + 1) * 4;
222 flags = EXTRACT_16BITS(&rh->rh_flags);
223 cnt = (flags >> 8) & 0x1f;
224 switch (flags & 0xff) {
226 sr = (struct rtcp_sr *)(rh + 1);
228 if (len != cnt * sizeof(*rr) + sizeof(*sr) + sizeof(*rh))
229 printf(" [%d]", len);
231 printf(" %u", EXTRACT_32BITS(&rh->rh_ssrc));
232 if ((u_char *)(sr + 1) > ep) {
236 ts = (double)(EXTRACT_32BITS(&sr->sr_ntp.upper)) +
237 ((double)(EXTRACT_32BITS(&sr->sr_ntp.lower)) /
239 printf(" @%.2f %u %up %ub", ts, EXTRACT_32BITS(&sr->sr_ts),
240 EXTRACT_32BITS(&sr->sr_np), EXTRACT_32BITS(&sr->sr_nb));
241 rr = (struct rtcp_rr *)(sr + 1);
245 if (len != cnt * sizeof(*rr) + sizeof(*rh))
246 printf(" [%d]", len);
247 rr = (struct rtcp_rr *)(rh + 1);
249 printf(" %u", EXTRACT_32BITS(&rh->rh_ssrc));
252 printf(" sdes %d", len);
254 printf(" %u", EXTRACT_32BITS(&rh->rh_ssrc));
258 printf(" bye %d", len);
260 printf(" %u", EXTRACT_32BITS(&rh->rh_ssrc));
264 printf(" type-0x%x %d", flags & 0xff, len);
271 if ((u_char *)(rr + 1) > ep) {
276 printf(" %u", EXTRACT_32BITS(&rr->rr_srcid));
277 ts = (double)(EXTRACT_32BITS(&rr->rr_lsr)) / 65536.;
278 dts = (double)(EXTRACT_32BITS(&rr->rr_dlsr)) / 65536.;
279 printf(" %ul %us %uj @%.2f+%.2f",
280 EXTRACT_32BITS(&rr->rr_nl) & 0x00ffffff,
281 EXTRACT_32BITS(&rr->rr_ls),
282 EXTRACT_32BITS(&rr->rr_dv), ts, dts);
287 static int udp_cksum(register const struct ip *ip,
288 register const struct udphdr *up,
291 return (nextproto4_cksum(ip, (const u_int8_t *)(void *)up, len,
296 static int udp6_cksum(const struct ip6_hdr *ip6, const struct udphdr *up,
299 return (nextproto6_cksum(ip6, (const u_int8_t *)(void *)up, len,
305 udpipaddr_print(const struct ip *ip, int sport, int dport)
308 const struct ip6_hdr *ip6;
311 ip6 = (const struct ip6_hdr *)ip;
316 if (ip6->ip6_nxt == IPPROTO_UDP) {
318 (void)printf("%s > %s: ",
319 ip6addr_string(&ip6->ip6_src),
320 ip6addr_string(&ip6->ip6_dst));
322 (void)printf("%s.%s > %s.%s: ",
323 ip6addr_string(&ip6->ip6_src),
324 udpport_string(sport),
325 ip6addr_string(&ip6->ip6_dst),
326 udpport_string(dport));
330 (void)printf("%s > %s: ",
331 udpport_string(sport),
332 udpport_string(dport));
338 if (ip->ip_p == IPPROTO_UDP) {
340 (void)printf("%s > %s: ",
341 ipaddr_string(&ip->ip_src),
342 ipaddr_string(&ip->ip_dst));
344 (void)printf("%s.%s > %s.%s: ",
345 ipaddr_string(&ip->ip_src),
346 udpport_string(sport),
347 ipaddr_string(&ip->ip_dst),
348 udpport_string(dport));
352 (void)printf("%s > %s: ",
353 udpport_string(sport),
354 udpport_string(dport));
361 udp_print(register const u_char *bp, u_int length,
362 register const u_char *bp2, int fragmented)
364 register const struct udphdr *up;
365 register const struct ip *ip;
366 register const u_char *cp;
367 register const u_char *ep = bp + length;
368 u_int16_t sport, dport, ulen;
370 register const struct ip6_hdr *ip6;
375 up = (struct udphdr *)bp;
376 ip = (struct ip *)bp2;
379 ip6 = (struct ip6_hdr *)bp2;
383 cp = (u_char *)(up + 1);
384 if (!TTEST(up->uh_dport)) {
385 udpipaddr_print(ip, -1, -1);
386 (void)printf("[|udp]");
390 sport = EXTRACT_16BITS(&up->uh_sport);
391 dport = EXTRACT_16BITS(&up->uh_dport);
393 if (length < sizeof(struct udphdr)) {
394 udpipaddr_print(ip, sport, dport);
395 (void)printf("truncated-udp %d", length);
398 length -= sizeof(struct udphdr);
401 udpipaddr_print(ip, sport, dport);
402 (void)printf("[|udp]");
406 ulen = EXTRACT_16BITS(&up->uh_ulen);
408 udpipaddr_print(ip, sport, dport);
409 (void)printf("truncated-udplength %d", ulen);
413 register struct sunrpc_msg *rp;
414 enum sunrpc_msg_type direction;
416 switch (packettype) {
419 udpipaddr_print(ip, sport, dport);
420 vat_print((void *)(up + 1), up);
424 udpipaddr_print(ip, sport, dport);
425 wb_print((void *)(up + 1), length);
429 rp = (struct sunrpc_msg *)(up + 1);
430 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
431 if (direction == SUNRPC_CALL)
432 sunrpcrequest_print((u_char *)rp, length,
435 nfsreply_print((u_char *)rp, length,
436 (u_char *)ip); /*XXX*/
440 udpipaddr_print(ip, sport, dport);
441 rtp_print((void *)(up + 1), length, up);
445 udpipaddr_print(ip, sport, dport);
447 cp = rtcp_print(cp, ep);
451 udpipaddr_print(ip, sport, dport);
452 snmp_print((const u_char *)(up + 1), length);
456 udpipaddr_print(ip, sport, dport);
457 cnfp_print(cp, (const u_char *)ip);
461 udpipaddr_print(ip, sport, dport);
462 tftp_print(cp, length);
466 udpipaddr_print(ip, sport, dport);
467 aodv_print((const u_char *)(up + 1), length,
476 udpipaddr_print(ip, sport, dport);
477 radius_print(cp, length);
481 udpipaddr_print(ip, sport, dport);
482 vxlan_print((const u_char *)(up + 1), length);
489 register struct sunrpc_msg *rp;
490 enum sunrpc_msg_type direction;
492 rp = (struct sunrpc_msg *)(up + 1);
493 if (TTEST(rp->rm_direction)) {
494 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
495 if (dport == NFS_PORT && direction == SUNRPC_CALL) {
496 nfsreq_print((u_char *)rp, length,
500 if (sport == NFS_PORT && direction == SUNRPC_REPLY) {
501 nfsreply_print((u_char *)rp, length,
506 if (dport == SUNRPC_PORT && direction == SUNRPC_CALL) {
507 sunrpcrequest_print((u_char *)rp, length, (u_char *)ip);
512 if (TTEST(((struct LAP *)cp)->type) &&
513 ((struct LAP *)cp)->type == lapDDP &&
514 (atalk_port(sport) || atalk_port(dport))) {
516 fputs("kip ", stdout);
517 llap_print(cp, length);
521 udpipaddr_print(ip, sport, dport);
523 if (vflag && !Kflag && !fragmented) {
524 /* Check the checksum, if possible. */
525 u_int16_t sum, udp_sum;
528 * XXX - do this even if vflag == 1?
529 * TCP does, and we do so for UDP-over-IPv6.
531 if (IP_V(ip) == 4 && (vflag > 1)) {
532 udp_sum = EXTRACT_16BITS(&up->uh_sum);
534 (void)printf("[no cksum] ");
535 } else if (TTEST2(cp[0], length)) {
536 sum = udp_cksum(ip, up, length + sizeof(struct udphdr));
539 (void)printf("[bad udp cksum 0x%04x -> 0x%04x!] ",
541 in_cksum_shouldbe(udp_sum, sum));
543 (void)printf("[udp sum ok] ");
547 else if (IP_V(ip) == 6 && ip6->ip6_plen) {
548 /* for IPv6, UDP checksum is mandatory */
549 if (TTEST2(cp[0], length)) {
550 sum = udp6_cksum(ip6, up, length + sizeof(struct udphdr));
551 udp_sum = EXTRACT_16BITS(&up->uh_sum);
554 (void)printf("[bad udp cksum 0x%04x -> 0x%04x!] ",
556 in_cksum_shouldbe(udp_sum, sum));
558 (void)printf("[udp sum ok] ");
565 #define ISPORT(p) (dport == (p) || sport == (p))
566 if (ISPORT(NAMESERVER_PORT))
567 ns_print((const u_char *)(up + 1), length, 0);
568 else if (ISPORT(MULTICASTDNS_PORT))
569 ns_print((const u_char *)(up + 1), length, 1);
570 else if (ISPORT(TIMED_PORT))
571 timed_print((const u_char *)(up + 1));
572 else if (ISPORT(TFTP_PORT))
573 tftp_print((const u_char *)(up + 1), length);
574 else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
575 bootp_print((const u_char *)(up + 1), length);
576 else if (ISPORT(RIP_PORT))
577 rip_print((const u_char *)(up + 1), length);
578 else if (ISPORT(AODV_PORT))
579 aodv_print((const u_char *)(up + 1), length,
585 else if (ISPORT(ISAKMP_PORT))
586 isakmp_print(gndo, (const u_char *)(up + 1), length, bp2);
587 else if (ISPORT(ISAKMP_PORT_NATT))
588 isakmp_rfc3948_print(gndo, (const u_char *)(up + 1), length, bp2);
590 else if (ISPORT(ISAKMP_PORT_USER1) || ISPORT(ISAKMP_PORT_USER2))
591 isakmp_print(gndo, (const u_char *)(up + 1), length, bp2);
593 else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
594 snmp_print((const u_char *)(up + 1), length);
595 else if (ISPORT(NTP_PORT))
596 ntp_print((const u_char *)(up + 1), length);
597 else if (ISPORT(KERBEROS_PORT) || ISPORT(KERBEROS_SEC_PORT))
598 krb_print((const void *)(up + 1));
599 else if (ISPORT(L2TP_PORT))
600 l2tp_print((const u_char *)(up + 1), length);
601 #ifdef TCPDUMP_DO_SMB
602 else if (ISPORT(NETBIOS_NS_PORT))
603 nbt_udp137_print((const u_char *)(up + 1), length);
604 else if (ISPORT(NETBIOS_DGRAM_PORT))
605 nbt_udp138_print((const u_char *)(up + 1), length);
607 else if (dport == 3456)
608 vat_print((const void *)(up + 1), up);
609 else if (ISPORT(ZEPHYR_SRV_PORT) || ISPORT(ZEPHYR_CLT_PORT))
610 zephyr_print((const void *)(up + 1), length);
612 * Since there are 10 possible ports to check, I think
613 * a <> test would be more efficient
615 else if ((sport >= RX_PORT_LOW && sport <= RX_PORT_HIGH) ||
616 (dport >= RX_PORT_LOW && dport <= RX_PORT_HIGH))
617 rx_print((const void *)(up + 1), length, sport, dport,
620 else if (ISPORT(RIPNG_PORT))
621 ripng_print((const u_char *)(up + 1), length);
622 else if (ISPORT(DHCP6_SERV_PORT) || ISPORT(DHCP6_CLI_PORT))
623 dhcp6_print((const u_char *)(up + 1), length);
624 else if (ISPORT(BABEL_PORT) || ISPORT(BABEL_PORT_OLD))
625 babel_print((const u_char *)(up + 1), length);
628 * Kludge in test for whiteboard packets.
630 else if (dport == 4567)
631 wb_print((const void *)(up + 1), length);
632 else if (ISPORT(CISCO_AUTORP_PORT))
633 cisco_autorp_print((const void *)(up + 1), length);
634 else if (ISPORT(RADIUS_PORT) ||
635 ISPORT(RADIUS_NEW_PORT) ||
636 ISPORT(RADIUS_ACCOUNTING_PORT) ||
637 ISPORT(RADIUS_NEW_ACCOUNTING_PORT) )
638 radius_print((const u_char *)(up+1), length);
639 else if (dport == HSRP_PORT)
640 hsrp_print((const u_char *)(up + 1), length);
641 else if (ISPORT(LWRES_PORT))
642 lwres_print((const u_char *)(up + 1), length);
643 else if (ISPORT(LDP_PORT))
644 ldp_print((const u_char *)(up + 1), length);
645 else if (ISPORT(OLSR_PORT))
646 olsr_print((const u_char *)(up + 1), length,
648 (IP_V(ip) == 6) ? 1 : 0);
652 else if (ISPORT(MPLS_LSP_PING_PORT))
653 lspping_print((const u_char *)(up + 1), length);
654 else if (dport == BFD_CONTROL_PORT ||
655 dport == BFD_ECHO_PORT )
656 bfd_print((const u_char *)(up+1), length, dport);
657 else if (ISPORT(LMP_PORT))
658 lmp_print((const u_char *)(up + 1), length);
659 else if (ISPORT(VQP_PORT))
660 vqp_print((const u_char *)(up + 1), length);
661 else if (ISPORT(SFLOW_PORT))
662 sflow_print((const u_char *)(up + 1), length);
663 else if (dport == LWAPP_CONTROL_PORT)
664 lwapp_control_print((const u_char *)(up + 1), length, 1);
665 else if (sport == LWAPP_CONTROL_PORT)
666 lwapp_control_print((const u_char *)(up + 1), length, 0);
667 else if (ISPORT(LWAPP_DATA_PORT))
668 lwapp_data_print((const u_char *)(up + 1), length);
669 else if (ISPORT(SIP_PORT))
670 sip_print((const u_char *)(up + 1), length);
671 else if (ISPORT(SYSLOG_PORT))
672 syslog_print((const u_char *)(up + 1), length);
673 else if (ISPORT(OTV_PORT))
674 otv_print((const u_char *)(up + 1), length);
676 (void)printf("UDP, length %u",
677 (u_int32_t)(ulen - sizeof(*up)));
680 (void)printf("UDP, length %u", (u_int32_t)(ulen - sizeof(*up)));
686 * c-style: whitesmith