4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
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
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.
24 #include <sys/cdefs.h>
27 static const char rcsid
[] _U_
=
28 "@(#) Header: /tcpdump/master/tcpdump/print-udp.c,v 1.138.2.1 2007/03/28 07:45:46 hannes Exp (LBL)";
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
38 #include <tcpdump-stdinc.h>
43 #include <arpa/tftp.h>
48 #include "interface.h"
49 #include "addrtoname.h"
51 #include "appletalk.h"
68 u_int16_t rh_flags
; /* T:2 P:1 CNT:5 PT:8 */
69 u_int16_t rh_len
; /* length of message (in words) */
70 u_int32_t rh_ssrc
; /* synchronization src id */
74 u_int32_t upper
; /* more significant 32 bits */
75 u_int32_t lower
; /* less significant 32 bits */
82 ntp64 sr_ntp
; /* 64-bit ntp timestamp */
83 u_int32_t sr_ts
; /* reference media timestamp */
84 u_int32_t sr_np
; /* no. packets sent */
85 u_int32_t sr_nb
; /* no. bytes sent */
90 * Time stamps are middle 32-bits of ntp timestamp.
93 u_int32_t rr_srcid
; /* sender being reported */
94 u_int32_t rr_nl
; /* no. packets lost */
95 u_int32_t rr_ls
; /* extended last seq number received */
96 u_int32_t rr_dv
; /* jitter (delay variance) */
97 u_int32_t rr_lsr
; /* orig. ts from last rr from this src */
98 u_int32_t rr_dlsr
; /* time from recpt of last rr to xmit time */
102 #define RTCP_PT_SR 200
103 #define RTCP_PT_RR 201
104 #define RTCP_PT_SDES 202
105 #define RTCP_SDES_CNAME 1
106 #define RTCP_SDES_NAME 2
107 #define RTCP_SDES_EMAIL 3
108 #define RTCP_SDES_PHONE 4
109 #define RTCP_SDES_LOC 5
110 #define RTCP_SDES_TOOL 6
111 #define RTCP_SDES_NOTE 7
112 #define RTCP_SDES_PRIV 8
113 #define RTCP_PT_BYE 203
114 #define RTCP_PT_APP 204
117 vat_print(const void *hdr
, register const struct udphdr
*up
)
120 u_int ts
= *(u_int16_t
*)hdr
;
121 if ((ts
& 0xf060) != 0) {
123 (void)printf("udp/vt %u %d / %d",
124 (u_int32_t
)(EXTRACT_16BITS(&up
->uh_ulen
) - sizeof(*up
)),
125 ts
& 0x3ff, ts
>> 10);
128 u_int32_t i0
= EXTRACT_32BITS(&((u_int
*)hdr
)[0]);
129 u_int32_t i1
= EXTRACT_32BITS(&((u_int
*)hdr
)[1]);
130 printf("udp/vat %u c%d %u%s",
131 (u_int32_t
)(EXTRACT_16BITS(&up
->uh_ulen
) - sizeof(*up
) - 8),
133 i1
, i0
& 0x800000? "*" : "");
136 printf(" f%d", (i0
>> 16) & 0x1f);
138 printf(" s%d", (i0
>> 24) & 0x3f);
143 rtp_print(const void *hdr
, u_int len
, register const struct udphdr
*up
)
146 u_int
*ip
= (u_int
*)hdr
;
147 u_int hasopt
, hasext
, contype
, hasmarker
;
148 u_int32_t i0
= EXTRACT_32BITS(&((u_int
*)hdr
)[0]);
149 u_int32_t i1
= EXTRACT_32BITS(&((u_int
*)hdr
)[1]);
150 u_int dlen
= EXTRACT_16BITS(&up
->uh_ulen
) - sizeof(*up
) - 8;
158 if ((i0
>> 30) == 1) {
160 hasopt
= i0
& 0x800000;
161 contype
= (i0
>> 16) & 0x3f;
162 hasmarker
= i0
& 0x400000;
166 hasext
= i0
& 0x10000000;
167 contype
= (i0
>> 16) & 0x7f;
168 hasmarker
= i0
& 0x800000;
174 printf("udp/%s %d c%d %s%s %d %u",
178 (hasopt
|| hasext
)? "+" : "",
183 printf(" %u", EXTRACT_32BITS(&((u_int
*)hdr
)[2]));
188 optlen
= (i2
>> 16) & 0xff;
189 if (optlen
== 0 || optlen
> len
) {
195 } while ((int)i2
>= 0);
200 extlen
= (i2
& 0xffff) + 1;
207 if (contype
== 0x1f) /*XXX H.261 */
208 printf(" 0x%04x", ip
[0] >> 16);
212 static const u_char
*
213 rtcp_print(const u_char
*hdr
, const u_char
*ep
)
215 /* rtp v2 control (rtcp) */
216 struct rtcp_rr
*rr
= 0;
218 struct rtcphdr
*rh
= (struct rtcphdr
*)hdr
;
223 if ((u_char
*)(rh
+ 1) > ep
) {
227 len
= (EXTRACT_16BITS(&rh
->rh_len
) + 1) * 4;
228 flags
= EXTRACT_16BITS(&rh
->rh_flags
);
229 cnt
= (flags
>> 8) & 0x1f;
230 switch (flags
& 0xff) {
232 sr
= (struct rtcp_sr
*)(rh
+ 1);
234 if (len
!= cnt
* sizeof(*rr
) + sizeof(*sr
) + sizeof(*rh
))
235 printf(" [%d]", len
);
237 printf(" %u", EXTRACT_32BITS(&rh
->rh_ssrc
));
238 if ((u_char
*)(sr
+ 1) > ep
) {
242 ts
= (double)(EXTRACT_32BITS(&sr
->sr_ntp
.upper
)) +
243 ((double)(EXTRACT_32BITS(&sr
->sr_ntp
.lower
)) /
245 printf(" @%.2f %u %up %ub", ts
, EXTRACT_32BITS(&sr
->sr_ts
),
246 EXTRACT_32BITS(&sr
->sr_np
), EXTRACT_32BITS(&sr
->sr_nb
));
247 rr
= (struct rtcp_rr
*)(sr
+ 1);
251 if (len
!= cnt
* sizeof(*rr
) + sizeof(*rh
))
252 printf(" [%d]", len
);
253 rr
= (struct rtcp_rr
*)(rh
+ 1);
255 printf(" %u", EXTRACT_32BITS(&rh
->rh_ssrc
));
258 printf(" sdes %d", len
);
260 printf(" %u", EXTRACT_32BITS(&rh
->rh_ssrc
));
264 printf(" bye %d", len
);
266 printf(" %u", EXTRACT_32BITS(&rh
->rh_ssrc
));
270 printf(" type-0x%x %d", flags
& 0xff, len
);
277 if ((u_char
*)(rr
+ 1) > ep
) {
282 printf(" %u", EXTRACT_32BITS(&rr
->rr_srcid
));
283 ts
= (double)(EXTRACT_32BITS(&rr
->rr_lsr
)) / 65536.;
284 dts
= (double)(EXTRACT_32BITS(&rr
->rr_dlsr
)) / 65536.;
285 printf(" %ul %us %uj @%.2f+%.2f",
286 EXTRACT_32BITS(&rr
->rr_nl
) & 0x00ffffff,
287 EXTRACT_32BITS(&rr
->rr_ls
),
288 EXTRACT_32BITS(&rr
->rr_dv
), ts
, dts
);
293 static int udp_cksum(register const struct ip
*ip
,
294 register const struct udphdr
*up
,
307 register const u_int16_t
*sp
;
309 /* pseudo-header.. */
310 phu
.ph
.len
= htons((u_int16_t
)len
);
312 phu
.ph
.proto
= IPPROTO_UDP
;
313 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
315 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
317 phu
.ph
.dst
= ip_finddst(ip
);
320 return in_cksum((u_short
*)up
, len
,
321 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
325 static int udp6_cksum(const struct ip6_hdr
*ip6
, const struct udphdr
*up
,
329 register const u_int16_t
*sp
;
333 struct in6_addr ph_src
;
334 struct in6_addr ph_dst
;
343 memset(&phu
, 0, sizeof(phu
));
344 phu
.ph
.ph_src
= ip6
->ip6_src
;
345 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
346 phu
.ph
.ph_len
= htonl(len
);
347 phu
.ph
.ph_nxt
= IPPROTO_UDP
;
350 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
353 sp
= (const u_int16_t
*)up
;
355 for (i
= 0; i
< (len
& ~1); i
+= 2)
359 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
362 sum
= (sum
& 0xffff) + (sum
>> 16);
370 udpipaddr_print(const struct ip
*ip
, int sport
, int dport
)
373 const struct ip6_hdr
*ip6
;
376 ip6
= (const struct ip6_hdr
*)ip
;
381 if (ip6
->ip6_nxt
== IPPROTO_UDP
) {
383 (void)printf("%s > %s: ",
384 ip6addr_string(&ip6
->ip6_src
),
385 ip6addr_string(&ip6
->ip6_dst
));
387 (void)printf("%s.%s > %s.%s: ",
388 ip6addr_string(&ip6
->ip6_src
),
389 udpport_string(sport
),
390 ip6addr_string(&ip6
->ip6_dst
),
391 udpport_string(dport
));
395 (void)printf("%s > %s: ",
396 udpport_string(sport
),
397 udpport_string(dport
));
403 if (ip
->ip_p
== IPPROTO_UDP
) {
405 (void)printf("%s > %s: ",
406 ipaddr_string(&ip
->ip_src
),
407 ipaddr_string(&ip
->ip_dst
));
409 (void)printf("%s.%s > %s.%s: ",
410 ipaddr_string(&ip
->ip_src
),
411 udpport_string(sport
),
412 ipaddr_string(&ip
->ip_dst
),
413 udpport_string(dport
));
417 (void)printf("%s > %s: ",
418 udpport_string(sport
),
419 udpport_string(dport
));
426 udp_print(register const u_char
*bp
, u_int length
,
427 register const u_char
*bp2
, int fragmented
)
429 register const struct udphdr
*up
;
430 register const struct ip
*ip
;
431 register const u_char
*cp
;
432 register const u_char
*ep
= bp
+ length
;
433 u_int16_t sport
, dport
, ulen
;
435 register const struct ip6_hdr
*ip6
;
440 up
= (struct udphdr
*)bp
;
441 ip
= (struct ip
*)bp2
;
444 ip6
= (struct ip6_hdr
*)bp2
;
448 cp
= (u_char
*)(up
+ 1);
449 if (!TTEST(up
->uh_dport
)) {
450 udpipaddr_print(ip
, -1, -1);
451 (void)printf("[|udp]");
455 sport
= EXTRACT_16BITS(&up
->uh_sport
);
456 dport
= EXTRACT_16BITS(&up
->uh_dport
);
458 if (length
< sizeof(struct udphdr
)) {
459 udpipaddr_print(ip
, sport
, dport
);
460 (void)printf("truncated-udp %d", length
);
463 length
-= sizeof(struct udphdr
);
466 udpipaddr_print(ip
, sport
, dport
);
467 (void)printf("[|udp]");
471 ulen
= EXTRACT_16BITS(&up
->uh_ulen
);
473 udpipaddr_print(ip
, sport
, dport
);
474 (void)printf("truncated-udplength %d", ulen
);
478 register struct sunrpc_msg
*rp
;
479 enum sunrpc_msg_type direction
;
481 switch (packettype
) {
484 udpipaddr_print(ip
, sport
, dport
);
485 vat_print((void *)(up
+ 1), up
);
489 udpipaddr_print(ip
, sport
, dport
);
490 wb_print((void *)(up
+ 1), length
);
494 rp
= (struct sunrpc_msg
*)(up
+ 1);
495 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
496 if (direction
== SUNRPC_CALL
)
497 sunrpcrequest_print((u_char
*)rp
, length
,
500 nfsreply_print((u_char
*)rp
, length
,
501 (u_char
*)ip
); /*XXX*/
505 udpipaddr_print(ip
, sport
, dport
);
506 rtp_print((void *)(up
+ 1), length
, up
);
510 udpipaddr_print(ip
, sport
, dport
);
512 cp
= rtcp_print(cp
, ep
);
516 udpipaddr_print(ip
, sport
, dport
);
517 snmp_print((const u_char
*)(up
+ 1), length
);
521 udpipaddr_print(ip
, sport
, dport
);
522 cnfp_print(cp
, (const u_char
*)ip
);
526 udpipaddr_print(ip
, sport
, dport
);
527 tftp_print(cp
, length
);
531 udpipaddr_print(ip
, sport
, dport
);
532 aodv_print((const u_char
*)(up
+ 1), length
,
544 register struct sunrpc_msg
*rp
;
545 enum sunrpc_msg_type direction
;
547 rp
= (struct sunrpc_msg
*)(up
+ 1);
548 if (TTEST(rp
->rm_direction
)) {
549 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
550 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
551 nfsreq_print((u_char
*)rp
, length
,
555 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
556 nfsreply_print((u_char
*)rp
, length
,
561 if (dport
== SUNRPC_PORT
&& direction
== SUNRPC_CALL
) {
562 sunrpcrequest_print((u_char
*)rp
, length
, (u_char
*)ip
);
567 if (TTEST(((struct LAP
*)cp
)->type
) &&
568 ((struct LAP
*)cp
)->type
== lapDDP
&&
569 (atalk_port(sport
) || atalk_port(dport
))) {
571 fputs("kip ", stdout
);
572 llap_print(cp
, length
);
576 udpipaddr_print(ip
, sport
, dport
);
578 if (IP_V(ip
) == 4 && (vflag
> 1) && !fragmented
) {
579 int sum
= up
->uh_sum
;
581 (void)printf("[no cksum] ");
582 } else if (TTEST2(cp
[0], length
)) {
583 sum
= udp_cksum(ip
, up
, length
+ sizeof(struct udphdr
));
585 (void)printf("[bad udp cksum %x!] ", sum
);
587 (void)printf("[udp sum ok] ");
591 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
592 int sum
= up
->uh_sum
;
593 /* for IPv6, UDP checksum is mandatory */
594 if (TTEST2(cp
[0], length
)) {
595 sum
= udp6_cksum(ip6
, up
, length
+ sizeof(struct udphdr
));
597 (void)printf("[bad udp cksum %x!] ", sum
);
599 (void)printf("[udp sum ok] ");
605 #define ISPORT(p) (dport == (p) || sport == (p))
606 if (ISPORT(NAMESERVER_PORT
))
607 ns_print((const u_char
*)(up
+ 1), length
, 0);
608 else if (ISPORT(MULTICASTDNS_PORT
))
609 ns_print((const u_char
*)(up
+ 1), length
, 1);
610 else if (ISPORT(TIMED_PORT
))
611 timed_print((const u_char
*)(up
+ 1));
612 else if (ISPORT(TFTP_PORT
))
613 tftp_print((const u_char
*)(up
+ 1), length
);
614 else if (ISPORT(IPPORT_BOOTPC
) || ISPORT(IPPORT_BOOTPS
))
615 bootp_print((const u_char
*)(up
+ 1), length
);
616 else if (ISPORT(RIP_PORT
))
617 rip_print((const u_char
*)(up
+ 1), length
);
618 else if (ISPORT(AODV_PORT
))
619 aodv_print((const u_char
*)(up
+ 1), length
,
625 else if (ISPORT(ISAKMP_PORT
))
626 isakmp_print(gndo
, (const u_char
*)(up
+ 1), length
, bp2
);
627 else if (ISPORT(ISAKMP_PORT_NATT
))
628 isakmp_rfc3948_print(gndo
, (const u_char
*)(up
+ 1), length
, bp2
);
630 else if (ISPORT(ISAKMP_PORT_USER1
) || ISPORT(ISAKMP_PORT_USER2
))
631 isakmp_print(gndo
, (const u_char
*)(up
+ 1), length
, bp2
);
633 else if (ISPORT(SNMP_PORT
) || ISPORT(SNMPTRAP_PORT
))
634 snmp_print((const u_char
*)(up
+ 1), length
);
635 else if (ISPORT(NTP_PORT
))
636 ntp_print((const u_char
*)(up
+ 1), length
);
637 else if (ISPORT(KERBEROS_PORT
) || ISPORT(KERBEROS_SEC_PORT
))
638 krb_print((const void *)(up
+ 1));
639 else if (ISPORT(L2TP_PORT
))
640 l2tp_print((const u_char
*)(up
+ 1), length
);
641 #ifdef TCPDUMP_DO_SMB
642 else if (ISPORT(NETBIOS_NS_PORT
))
643 nbt_udp137_print((const u_char
*)(up
+ 1), length
);
644 else if (ISPORT(NETBIOS_DGRAM_PORT
))
645 nbt_udp138_print((const u_char
*)(up
+ 1), length
);
647 else if (dport
== 3456)
648 vat_print((const void *)(up
+ 1), up
);
649 else if (ISPORT(ZEPHYR_SRV_PORT
) || ISPORT(ZEPHYR_CLT_PORT
))
650 zephyr_print((const void *)(up
+ 1), length
);
652 * Since there are 10 possible ports to check, I think
653 * a <> test would be more efficient
655 else if ((sport
>= RX_PORT_LOW
&& sport
<= RX_PORT_HIGH
) ||
656 (dport
>= RX_PORT_LOW
&& dport
<= RX_PORT_HIGH
))
657 rx_print((const void *)(up
+ 1), length
, sport
, dport
,
660 else if (ISPORT(RIPNG_PORT
))
661 ripng_print((const u_char
*)(up
+ 1), length
);
662 else if (ISPORT(DHCP6_SERV_PORT
) || ISPORT(DHCP6_CLI_PORT
)) {
663 dhcp6_print((const u_char
*)(up
+ 1), length
);
667 * Kludge in test for whiteboard packets.
669 else if (dport
== 4567)
670 wb_print((const void *)(up
+ 1), length
);
671 else if (ISPORT(CISCO_AUTORP_PORT
))
672 cisco_autorp_print((const void *)(up
+ 1), length
);
673 else if (ISPORT(RADIUS_PORT
) ||
674 ISPORT(RADIUS_NEW_PORT
) ||
675 ISPORT(RADIUS_ACCOUNTING_PORT
) ||
676 ISPORT(RADIUS_NEW_ACCOUNTING_PORT
) )
677 radius_print((const u_char
*)(up
+1), length
);
678 else if (dport
== HSRP_PORT
)
679 hsrp_print((const u_char
*)(up
+ 1), length
);
680 else if (ISPORT(LWRES_PORT
))
681 lwres_print((const u_char
*)(up
+ 1), length
);
682 else if (ISPORT(LDP_PORT
))
683 ldp_print((const u_char
*)(up
+ 1), length
);
684 else if (ISPORT(OLSR_PORT
))
685 olsr_print((const u_char
*)(up
+ 1), length
);
686 else if (ISPORT(MPLS_LSP_PING_PORT
))
687 lspping_print((const u_char
*)(up
+ 1), length
);
688 else if (dport
== BFD_CONTROL_PORT
||
689 dport
== BFD_ECHO_PORT
)
690 bfd_print((const u_char
*)(up
+1), length
, dport
);
691 else if (ISPORT(LMP_PORT
))
692 lmp_print((const u_char
*)(up
+ 1), length
);
693 else if (ISPORT(SIP_PORT
))
694 sip_print((const u_char
*)(up
+ 1), length
);
695 else if (ISPORT(SYSLOG_PORT
))
696 syslog_print((const u_char
*)(up
+ 1), length
);
698 (void)printf("UDP, length %u",
699 (u_int32_t
)(ulen
- sizeof(*up
)));
702 (void)printf("UDP, length %u", (u_int32_t
)(ulen
- sizeof(*up
)));
708 * c-style: whitesmith