]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-udp.c
Allow tcpdump to work with earlier libpcaps.
[tcpdump] / print-udp.c
index e44cabff7a07fd1cf19a2e7703d65494841c8780..a95683ec45a48341d5567886b670850151ecc745 100644 (file)
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-udp.c,v 1.73 2000-04-27 11:09:08 itojun Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-udp.c,v 1.89 2000-11-17 19:08:16 itojun Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -30,22 +30,9 @@ static const char rcsid[] =
 
 #include <sys/param.h>
 #include <sys/time.h>
-#include <sys/socket.h>
 
 #include <netinet/in.h>
-#include <netinet/in_systm.h>
-#include <netinet/ip.h>
-#include <netinet/ip_var.h>
-#include <netinet/udp.h>
-#include <netinet/udp_var.h>
 
-#ifdef NOERROR
-#undef NOERROR                                 /* Solaris sucks */
-#endif
-#ifdef T_UNSPEC
-#undef T_UNSPEC                                        /* SINIX does too */
-#endif
-#include <arpa/nameser.h>
 #ifdef SEGSIZE
 #undef SEGSIZE
 #endif
@@ -54,15 +41,26 @@ static const char rcsid[] =
 #include <rpc/rpc.h>
 
 #include <stdio.h>
-
-#ifdef INET6
-#include <netinet/ip6.h>
-#endif
+#include <string.h>
 
 #include "interface.h"
 #include "addrtoname.h"
 #include "appletalk.h"
 
+#include "udp.h"
+
+#include "ip.h"
+#ifdef INET6
+#include "ip6.h"
+#endif
+
+#ifdef NOERROR
+#undef NOERROR                                 /* Solaris sucks */
+#endif
+#ifdef T_UNSPEC
+#undef T_UNSPEC                                        /* SINIX does too */
+#endif
+#include "nameser.h"
 #include "nfs.h"
 #include "bootp.h"
 
@@ -182,7 +180,7 @@ rtp_print(const void *hdr, u_int len, register const struct udphdr *up)
                i0 & 0xffff,
                i1);
        if (vflag) {
-               printf(" %u", i1);
+               printf(" %u", (u_int32_t)ntohl(((u_int *)hdr)[2]));
                if (hasopt) {
                        u_int i2, optlen;
                        do {
@@ -292,6 +290,100 @@ rtcp_print(const u_char *hdr, const u_char *ep)
        return (hdr + len);
 }
 
+static int udp_cksum(register const struct ip *ip,
+                    register const struct udphdr *up,
+                    register int len)
+{
+       int i, tlen;
+       union phu {
+               struct phdr {
+                       u_int32_t src;
+                       u_int32_t dst;
+                       u_char mbz;
+                       u_char proto;
+                       u_int16_t len;
+               } ph;
+               u_int16_t pa[6];
+       } phu;
+       register const u_int16_t *sp;
+       u_int32_t sum;
+       tlen = ntohs(ip->ip_len) - ((const char *)up-(const char*)ip);
+
+       /* pseudo-header.. */
+       phu.ph.len = htons(tlen);
+       phu.ph.mbz = 0;
+       phu.ph.proto = IPPROTO_UDP;
+       memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
+       memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
+
+       sp = &phu.pa[0];
+       sum = sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5];
+
+       sp = (const u_int16_t *)up;
+
+       for (i=0; i<(tlen&~1); i+= 2)
+               sum += *sp++;
+
+       if (tlen & 1) {
+               sum += htons( (*(const char *)sp) << 8);
+       }
+
+       while (sum > 0xffff)
+               sum = (sum & 0xffff) + (sum >> 16);
+       sum = ~sum & 0xffff;
+
+       return (sum);
+}
+
+#ifdef INET6
+static int udp6_cksum(const struct ip6_hdr *ip6, const struct udphdr *up,
+       int len)
+{
+       int i, tlen;
+       register const u_int16_t *sp;
+       u_int32_t sum;
+       union {
+               struct {
+                       struct in6_addr ph_src;
+                       struct in6_addr ph_dst;
+                       u_int32_t       ph_len;
+                       u_int8_t        ph_zero[3];
+                       u_int8_t        ph_nxt;
+               } ph;
+               u_int16_t pa[20];
+       } phu;
+
+       tlen = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr) -
+           ((const char *)up - (const char*)ip6);
+
+       /* pseudo-header */
+       memset(&phu, 0, sizeof(phu));
+       phu.ph.ph_src = ip6->ip6_src;
+       phu.ph.ph_dst = ip6->ip6_dst;
+       phu.ph.ph_len = htonl(tlen);
+       phu.ph.ph_nxt = IPPROTO_UDP;
+
+       sum = 0;
+       for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
+               sum += phu.pa[i];
+
+       sp = (const u_int16_t *)up;
+
+       for (i = 0; i < (tlen & ~1); i += 2)
+               sum += *sp++;
+
+       if (tlen & 1)
+               sum += htons((*(const char *)sp) << 8);
+
+       while (sum > 0xffff)
+               sum = (sum & 0xffff) + (sum >> 16);
+       sum = ~sum & 0xffff;
+
+       return (sum);
+}
+#endif
+
+
 /* XXX probably should use getservbyname() and cache answers */
 #define TFTP_PORT 69           /*XXX*/
 #define KERBEROS_PORT 88       /*XXX*/
@@ -300,6 +392,7 @@ rtcp_print(const u_char *hdr, const u_char *ep)
 #define NTP_PORT 123           /*XXX*/
 #define SNMPTRAP_PORT 162      /*XXX*/
 #define ISAKMP_PORT 500                /*XXX*/
+#define TIMED_PORT 525         /*XXX*/
 #define RIP_PORT 520           /*XXX*/
 #define KERBEROS_SEC_PORT 750  /*XXX*/
 #define L2TP_PORT 1701         /*XXX*/
@@ -310,6 +403,10 @@ rtcp_print(const u_char *hdr, const u_char *ep)
 #define NETBIOS_NS_PORT   137
 #define NETBIOS_DGRAM_PORT   138
 #define CISCO_AUTORP_PORT 496  /*XXX*/
+#define RADIUS_PORT 1645
+#define RADIUS_NEW_PORT 1812
+#define RADIUS_ACCOUNTING_PORT 1646
+#define RADIUS_NEW_ACCOUNTING_PORT 1813
 
 #ifdef INET6
 #define RIPNG_PORT 521         /*XXX*/
@@ -318,7 +415,8 @@ rtcp_print(const u_char *hdr, const u_char *ep)
 #endif
 
 void
-udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
+udp_print(register const u_char *bp, u_int length,
+         register const u_char *bp2, int fragmented)
 {
        register const struct udphdr *up;
        register const struct ip *ip;
@@ -334,7 +432,7 @@ udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
        up = (struct udphdr *)bp;
        ip = (struct ip *)bp2;
 #ifdef INET6
-       if (ip->ip_v == 6)
+       if (IP_V(ip) == 6)
                ip6 = (struct ip6_hdr *)bp2;
        else
                ip6 = NULL;
@@ -356,6 +454,13 @@ udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
        sport = ntohs(up->uh_sport);
        dport = ntohs(up->uh_dport);
        ulen = ntohs(up->uh_ulen);
+       if (ulen < 8) {
+               (void)printf("%s > %s: truncated-udplength %d",
+                            ipaddr_string(&ip->ip_src),
+                            ipaddr_string(&ip->ip_dst),
+                            ulen);
+               return;
+       }
        if (packettype) {
                register struct rpc_msg *rp;
                enum msg_type direction;
@@ -460,7 +565,7 @@ udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
                    (atalk_port(sport) || atalk_port(dport))) {
                        if (vflag)
                                fputs("kip ", stdout);
-                       atalk_print(cp, length);
+                       llap_print(cp, length);
                        return;
                }
        }
@@ -497,10 +602,38 @@ udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
        }
 #endif
 
+       if (IP_V(ip) == 4 && vflag && !fragmented) {
+               int sum = up->uh_sum;
+               if (sum == 0) {
+                       (void)printf(" [no cksum]");
+               } else if (TTEST2(cp[0], length)) {
+                       sum = udp_cksum(ip, up, length);
+                       if (sum != 0)
+                               (void)printf(" [bad udp cksum %x!]", sum);
+                       else
+                               (void)printf(" [udp sum ok]");
+               }
+       }
+#ifdef INET6
+       if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !fragmented) {
+               int sum = up->uh_sum;
+               /* for IPv6, UDP checksum is mandatory */
+               if (TTEST2(cp[0], length)) {
+                       sum = udp6_cksum(ip6, up, length);
+                       if (sum != 0)
+                               (void)printf(" [bad udp cksum %x!]", sum);
+                       else
+                               (void)printf(" [udp sum ok]");
+               }
+       }
+#endif
+
        if (!qflag) {
 #define ISPORT(p) (dport == (p) || sport == (p))
                if (ISPORT(NAMESERVER_PORT))
                        ns_print((const u_char *)(up + 1), length);
+               else if (ISPORT(TIMED_PORT))
+                       timed_print((const u_char *)(up + 1), length);
                else if (ISPORT(TFTP_PORT))
                        tftp_print((const u_char *)(up + 1), length);
                else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
@@ -553,6 +686,11 @@ udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
                        wb_print((const void *)(up + 1), length);
                else if (ISPORT(CISCO_AUTORP_PORT))
                        cisco_autorp_print((const void *)(up + 1), length);
+               else if (ISPORT(RADIUS_PORT) ||
+                        ISPORT(RADIUS_NEW_PORT) ||
+                        ISPORT(RADIUS_ACCOUNTING_PORT) || 
+                        ISPORT(RADIUS_NEW_ACCOUNTING_PORT) )
+                        radius_print((const u_char *)(up+1), length);
                else
                        (void)printf(" udp %u",
                            (u_int32_t)(ulen - sizeof(*up)));