Home | History | Annotate | Line # | Download | only in dist
print-udp.c revision 1.10
      1   1.1  christos /*
      2   1.1  christos  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
      3   1.1  christos  *	The Regents of the University of California.  All rights reserved.
      4   1.1  christos  *
      5   1.1  christos  * Redistribution and use in source and binary forms, with or without
      6   1.1  christos  * modification, are permitted provided that: (1) source code distributions
      7   1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
      8   1.1  christos  * distributions including binary code include the above copyright notice and
      9   1.1  christos  * this paragraph in its entirety in the documentation or other materials
     10   1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     11   1.1  christos  * features or use of this software display the following acknowledgement:
     12   1.1  christos  * ``This product includes software developed by the University of California,
     13   1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14   1.1  christos  * the University nor the names of its contributors may be used to endorse
     15   1.1  christos  * or promote products derived from this software without specific prior
     16   1.1  christos  * written permission.
     17   1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18   1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19   1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20   1.1  christos  */
     21   1.1  christos 
     22   1.2  christos #include <sys/cdefs.h>
     23   1.1  christos #ifndef lint
     24  1.10  christos __RCSID("$NetBSD: print-udp.c,v 1.10 2023/08/17 20:19:40 christos Exp $");
     25   1.1  christos #endif
     26   1.1  christos 
     27   1.8       spz /* \summary: UDP printer */
     28   1.8       spz 
     29   1.1  christos #ifdef HAVE_CONFIG_H
     30  1.10  christos #include <config.h>
     31   1.1  christos #endif
     32   1.1  christos 
     33  1.10  christos #include "netdissect-stdinc.h"
     34   1.1  christos 
     35   1.7  christos #include "netdissect.h"
     36   1.1  christos #include "addrtoname.h"
     37   1.1  christos #include "extract.h"
     38   1.1  christos #include "appletalk.h"
     39   1.1  christos 
     40   1.1  christos #include "udp.h"
     41   1.1  christos 
     42   1.1  christos #include "ip.h"
     43   1.1  christos #include "ip6.h"
     44   1.1  christos #include "ipproto.h"
     45   1.1  christos #include "rpc_auth.h"
     46   1.1  christos #include "rpc_msg.h"
     47   1.1  christos 
     48   1.1  christos #include "nfs.h"
     49   1.1  christos 
     50   1.8       spz 
     51   1.1  christos struct rtcphdr {
     52  1.10  christos 	nd_uint16_t rh_flags;	/* T:2 P:1 CNT:5 PT:8 */
     53  1.10  christos 	nd_uint16_t rh_len;	/* length of message (in words) */
     54  1.10  christos 	nd_uint32_t rh_ssrc;	/* synchronization src id */
     55   1.1  christos };
     56   1.1  christos 
     57   1.1  christos typedef struct {
     58  1.10  christos 	nd_uint32_t upper;	/* more significant 32 bits */
     59  1.10  christos 	nd_uint32_t lower;	/* less significant 32 bits */
     60   1.1  christos } ntp64;
     61   1.1  christos 
     62   1.1  christos /*
     63   1.1  christos  * Sender report.
     64   1.1  christos  */
     65   1.1  christos struct rtcp_sr {
     66  1.10  christos 	ntp64       sr_ntp;	/* 64-bit ntp timestamp */
     67  1.10  christos 	nd_uint32_t sr_ts;	/* reference media timestamp */
     68  1.10  christos 	nd_uint32_t sr_np;	/* no. packets sent */
     69  1.10  christos 	nd_uint32_t sr_nb;	/* no. bytes sent */
     70   1.1  christos };
     71   1.1  christos 
     72   1.1  christos /*
     73   1.1  christos  * Receiver report.
     74   1.1  christos  * Time stamps are middle 32-bits of ntp timestamp.
     75   1.1  christos  */
     76   1.1  christos struct rtcp_rr {
     77  1.10  christos 	nd_uint32_t rr_srcid;	/* sender being reported */
     78  1.10  christos 	nd_uint32_t rr_nl;	/* no. packets lost */
     79  1.10  christos 	nd_uint32_t rr_ls;	/* extended last seq number received */
     80  1.10  christos 	nd_uint32_t rr_dv;	/* jitter (delay variance) */
     81  1.10  christos 	nd_uint32_t rr_lsr;	/* orig. ts from last rr from this src  */
     82  1.10  christos 	nd_uint32_t rr_dlsr;	/* time from recpt of last rr to xmit time */
     83   1.1  christos };
     84   1.1  christos 
     85   1.1  christos /*XXX*/
     86   1.1  christos #define RTCP_PT_SR	200
     87   1.1  christos #define RTCP_PT_RR	201
     88   1.1  christos #define RTCP_PT_SDES	202
     89  1.10  christos #define    RTCP_SDES_CNAME	1
     90  1.10  christos #define    RTCP_SDES_NAME	2
     91  1.10  christos #define    RTCP_SDES_EMAIL	3
     92  1.10  christos #define    RTCP_SDES_PHONE	4
     93  1.10  christos #define    RTCP_SDES_LOC	5
     94  1.10  christos #define    RTCP_SDES_TOOL	6
     95  1.10  christos #define    RTCP_SDES_NOTE	7
     96  1.10  christos #define    RTCP_SDES_PRIV	8
     97   1.1  christos #define RTCP_PT_BYE	203
     98   1.1  christos #define RTCP_PT_APP	204
     99   1.1  christos 
    100   1.1  christos static void
    101  1.10  christos vat_print(netdissect_options *ndo, const u_char *hdr, u_int length)
    102   1.1  christos {
    103   1.1  christos 	/* vat/vt audio */
    104   1.8       spz 	u_int ts;
    105   1.8       spz 
    106  1.10  christos 	ndo->ndo_protocol = "vat";
    107  1.10  christos 	if (length < 2) {
    108  1.10  christos 		ND_PRINT("udp/va/vat, length %u < 2", length);
    109  1.10  christos 		return;
    110  1.10  christos 	}
    111  1.10  christos 	ts = GET_BE_U_2(hdr);
    112   1.1  christos 	if ((ts & 0xf060) != 0) {
    113   1.1  christos 		/* probably vt */
    114  1.10  christos 		ND_PRINT("udp/vt %u %u / %u",
    115  1.10  christos 			     length,
    116  1.10  christos 			     ts & 0x3ff, ts >> 10);
    117   1.1  christos 	} else {
    118   1.1  christos 		/* probably vat */
    119   1.8       spz 		uint32_t i0, i1;
    120   1.8       spz 
    121  1.10  christos 		if (length < 8) {
    122  1.10  christos 			ND_PRINT("udp/vat, length %u < 8", length);
    123  1.10  christos 			return;
    124  1.10  christos 		}
    125  1.10  christos 		i0 = GET_BE_U_4(&((const u_int *)hdr)[0]);
    126  1.10  christos 		i1 = GET_BE_U_4(&((const u_int *)hdr)[1]);
    127  1.10  christos 		ND_PRINT("udp/vat %u c%u %u%s",
    128  1.10  christos 			length - 8,
    129   1.1  christos 			i0 & 0xffff,
    130  1.10  christos 			i1, i0 & 0x800000? "*" : "");
    131   1.1  christos 		/* audio format */
    132   1.1  christos 		if (i0 & 0x1f0000)
    133  1.10  christos 			ND_PRINT(" f%u", (i0 >> 16) & 0x1f);
    134   1.1  christos 		if (i0 & 0x3f000000)
    135  1.10  christos 			ND_PRINT(" s%u", (i0 >> 24) & 0x3f);
    136   1.1  christos 	}
    137   1.1  christos }
    138   1.1  christos 
    139   1.1  christos static void
    140  1.10  christos rtp_print(netdissect_options *ndo, const u_char *hdr, u_int len)
    141   1.1  christos {
    142   1.1  christos 	/* rtp v1 or v2 */
    143   1.7  christos 	const u_int *ip = (const u_int *)hdr;
    144   1.8       spz 	u_int hasopt, hasext, contype, hasmarker, dlen;
    145   1.8       spz 	uint32_t i0, i1;
    146   1.1  christos 	const char * ptype;
    147   1.1  christos 
    148  1.10  christos 	ndo->ndo_protocol = "rtp";
    149  1.10  christos 	if (len < 8) {
    150  1.10  christos 		ND_PRINT("udp/rtp, length %u < 8", len);
    151  1.10  christos 		return;
    152  1.10  christos 	}
    153  1.10  christos 	i0 = GET_BE_U_4(&((const u_int *)hdr)[0]);
    154  1.10  christos 	i1 = GET_BE_U_4(&((const u_int *)hdr)[1]);
    155  1.10  christos 	dlen = len - 8;
    156   1.1  christos 	ip += 2;
    157   1.1  christos 	len >>= 2;
    158   1.1  christos 	len -= 2;
    159   1.1  christos 	hasopt = 0;
    160   1.1  christos 	hasext = 0;
    161   1.1  christos 	if ((i0 >> 30) == 1) {
    162   1.8       spz 		/* rtp v1 - draft-ietf-avt-rtp-04 */
    163   1.1  christos 		hasopt = i0 & 0x800000;
    164   1.1  christos 		contype = (i0 >> 16) & 0x3f;
    165   1.1  christos 		hasmarker = i0 & 0x400000;
    166   1.1  christos 		ptype = "rtpv1";
    167   1.1  christos 	} else {
    168   1.8       spz 		/* rtp v2 - RFC 3550 */
    169  1.10  christos 		if (dlen < 4) {
    170  1.10  christos 			ND_PRINT("udp/rtp, length %u < 12", dlen + 8);
    171  1.10  christos 			return;
    172  1.10  christos 		}
    173   1.1  christos 		hasext = i0 & 0x10000000;
    174   1.1  christos 		contype = (i0 >> 16) & 0x7f;
    175   1.1  christos 		hasmarker = i0 & 0x800000;
    176   1.1  christos 		dlen -= 4;
    177   1.1  christos 		ptype = "rtp";
    178   1.1  christos 		ip += 1;
    179   1.1  christos 		len -= 1;
    180   1.1  christos 	}
    181  1.10  christos 	ND_PRINT("udp/%s %u c%u %s%s %u %u",
    182   1.1  christos 		ptype,
    183   1.1  christos 		dlen,
    184   1.1  christos 		contype,
    185   1.1  christos 		(hasopt || hasext)? "+" : "",
    186   1.1  christos 		hasmarker? "*" : "",
    187   1.1  christos 		i0 & 0xffff,
    188  1.10  christos 		i1);
    189   1.5  christos 	if (ndo->ndo_vflag) {
    190  1.10  christos 		ND_PRINT(" %u", GET_BE_U_4(&((const u_int *)hdr)[2]));
    191   1.1  christos 		if (hasopt) {
    192   1.1  christos 			u_int i2, optlen;
    193   1.1  christos 			do {
    194  1.10  christos 				i2 = GET_BE_U_4(ip);
    195   1.1  christos 				optlen = (i2 >> 16) & 0xff;
    196   1.1  christos 				if (optlen == 0 || optlen > len) {
    197  1.10  christos 					ND_PRINT(" !opt");
    198   1.1  christos 					return;
    199   1.1  christos 				}
    200   1.1  christos 				ip += optlen;
    201   1.1  christos 				len -= optlen;
    202   1.1  christos 			} while ((int)i2 >= 0);
    203   1.1  christos 		}
    204   1.1  christos 		if (hasext) {
    205   1.1  christos 			u_int i2, extlen;
    206  1.10  christos 			i2 = GET_BE_U_4(ip);
    207   1.1  christos 			extlen = (i2 & 0xffff) + 1;
    208   1.1  christos 			if (extlen > len) {
    209  1.10  christos 				ND_PRINT(" !ext");
    210   1.1  christos 				return;
    211   1.1  christos 			}
    212   1.1  christos 			ip += extlen;
    213   1.1  christos 		}
    214   1.1  christos 		if (contype == 0x1f) /*XXX H.261 */
    215  1.10  christos 			ND_PRINT(" 0x%04x", GET_BE_U_4(ip) >> 16);
    216   1.1  christos 	}
    217   1.1  christos }
    218   1.1  christos 
    219   1.1  christos static const u_char *
    220   1.5  christos rtcp_print(netdissect_options *ndo, const u_char *hdr, const u_char *ep)
    221   1.1  christos {
    222   1.1  christos 	/* rtp v2 control (rtcp) */
    223   1.7  christos 	const struct rtcp_rr *rr = 0;
    224   1.7  christos 	const struct rtcp_sr *sr;
    225   1.7  christos 	const struct rtcphdr *rh = (const struct rtcphdr *)hdr;
    226   1.1  christos 	u_int len;
    227   1.5  christos 	uint16_t flags;
    228  1.10  christos 	u_int cnt;
    229   1.1  christos 	double ts, dts;
    230  1.10  christos 
    231  1.10  christos 	ndo->ndo_protocol = "rtcp";
    232   1.8       spz 	if ((const u_char *)(rh + 1) > ep)
    233   1.8       spz 		goto trunc;
    234  1.10  christos 	ND_TCHECK_SIZE(rh);
    235  1.10  christos 	len = (GET_BE_U_2(rh->rh_len) + 1) * 4;
    236  1.10  christos 	flags = GET_BE_U_2(rh->rh_flags);
    237   1.1  christos 	cnt = (flags >> 8) & 0x1f;
    238   1.1  christos 	switch (flags & 0xff) {
    239   1.1  christos 	case RTCP_PT_SR:
    240   1.7  christos 		sr = (const struct rtcp_sr *)(rh + 1);
    241  1.10  christos 		ND_PRINT(" sr");
    242   1.1  christos 		if (len != cnt * sizeof(*rr) + sizeof(*sr) + sizeof(*rh))
    243  1.10  christos 			ND_PRINT(" [%u]", len);
    244   1.5  christos 		if (ndo->ndo_vflag)
    245  1.10  christos 			ND_PRINT(" %u", GET_BE_U_4(rh->rh_ssrc));
    246   1.8       spz 		if ((const u_char *)(sr + 1) > ep)
    247   1.8       spz 			goto trunc;
    248  1.10  christos 		ND_TCHECK_SIZE(sr);
    249  1.10  christos 		ts = (double)(GET_BE_U_4(sr->sr_ntp.upper)) +
    250  1.10  christos 		    ((double)(GET_BE_U_4(sr->sr_ntp.lower)) /
    251  1.10  christos 		     FMAXINT);
    252  1.10  christos 		ND_PRINT(" @%.2f %u %up %ub", ts, GET_BE_U_4(sr->sr_ts),
    253  1.10  christos 			  GET_BE_U_4(sr->sr_np), GET_BE_U_4(sr->sr_nb));
    254   1.7  christos 		rr = (const struct rtcp_rr *)(sr + 1);
    255   1.1  christos 		break;
    256   1.1  christos 	case RTCP_PT_RR:
    257  1.10  christos 		ND_PRINT(" rr");
    258   1.1  christos 		if (len != cnt * sizeof(*rr) + sizeof(*rh))
    259  1.10  christos 			ND_PRINT(" [%u]", len);
    260   1.7  christos 		rr = (const struct rtcp_rr *)(rh + 1);
    261   1.5  christos 		if (ndo->ndo_vflag)
    262  1.10  christos 			ND_PRINT(" %u", GET_BE_U_4(rh->rh_ssrc));
    263   1.1  christos 		break;
    264   1.1  christos 	case RTCP_PT_SDES:
    265  1.10  christos 		ND_PRINT(" sdes %u", len);
    266   1.5  christos 		if (ndo->ndo_vflag)
    267  1.10  christos 			ND_PRINT(" %u", GET_BE_U_4(rh->rh_ssrc));
    268   1.1  christos 		cnt = 0;
    269   1.1  christos 		break;
    270   1.1  christos 	case RTCP_PT_BYE:
    271  1.10  christos 		ND_PRINT(" bye %u", len);
    272   1.5  christos 		if (ndo->ndo_vflag)
    273  1.10  christos 			ND_PRINT(" %u", GET_BE_U_4(rh->rh_ssrc));
    274   1.1  christos 		cnt = 0;
    275   1.1  christos 		break;
    276   1.1  christos 	default:
    277  1.10  christos 		ND_PRINT(" type-0x%x %u", flags & 0xff, len);
    278   1.1  christos 		cnt = 0;
    279   1.1  christos 		break;
    280   1.1  christos 	}
    281   1.1  christos 	if (cnt > 1)
    282  1.10  christos 		ND_PRINT(" c%u", cnt);
    283  1.10  christos 	while (cnt != 0) {
    284   1.8       spz 		if ((const u_char *)(rr + 1) > ep)
    285   1.8       spz 			goto trunc;
    286  1.10  christos 		ND_TCHECK_SIZE(rr);
    287   1.5  christos 		if (ndo->ndo_vflag)
    288  1.10  christos 			ND_PRINT(" %u", GET_BE_U_4(rr->rr_srcid));
    289  1.10  christos 		ts = (double)(GET_BE_U_4(rr->rr_lsr)) / 65536.;
    290  1.10  christos 		dts = (double)(GET_BE_U_4(rr->rr_dlsr)) / 65536.;
    291  1.10  christos 		ND_PRINT(" %ul %us %uj @%.2f+%.2f",
    292  1.10  christos 		    GET_BE_U_4(rr->rr_nl) & 0x00ffffff,
    293  1.10  christos 		    GET_BE_U_4(rr->rr_ls),
    294  1.10  christos 		    GET_BE_U_4(rr->rr_dv), ts, dts);
    295  1.10  christos 		cnt--;
    296   1.1  christos 	}
    297   1.1  christos 	return (hdr + len);
    298   1.8       spz 
    299   1.8       spz trunc:
    300  1.10  christos 	nd_print_trunc(ndo);
    301   1.8       spz 	return ep;
    302   1.1  christos }
    303   1.1  christos 
    304  1.10  christos static uint16_t udp_cksum(netdissect_options *ndo, const struct ip *ip,
    305  1.10  christos 		     const struct udphdr *up,
    306  1.10  christos 		     u_int len)
    307   1.1  christos {
    308   1.7  christos 	return nextproto4_cksum(ndo, ip, (const uint8_t *)(const void *)up, len, len,
    309   1.5  christos 				IPPROTO_UDP);
    310   1.1  christos }
    311   1.1  christos 
    312  1.10  christos static uint16_t udp6_cksum(netdissect_options *ndo, const struct ip6_hdr *ip6,
    313   1.7  christos 		      const struct udphdr *up, u_int len)
    314   1.1  christos {
    315   1.7  christos 	return nextproto6_cksum(ndo, ip6, (const uint8_t *)(const void *)up, len, len,
    316   1.7  christos 				IPPROTO_UDP);
    317   1.1  christos }
    318   1.1  christos 
    319   1.1  christos static void
    320   1.5  christos udpipaddr_print(netdissect_options *ndo, const struct ip *ip, int sport, int dport)
    321   1.1  christos {
    322   1.1  christos 	const struct ip6_hdr *ip6;
    323   1.1  christos 
    324   1.1  christos 	if (IP_V(ip) == 6)
    325   1.1  christos 		ip6 = (const struct ip6_hdr *)ip;
    326   1.1  christos 	else
    327   1.1  christos 		ip6 = NULL;
    328   1.1  christos 
    329   1.1  christos 	if (ip6) {
    330  1.10  christos 		if (GET_U_1(ip6->ip6_nxt) == IPPROTO_UDP) {
    331   1.1  christos 			if (sport == -1) {
    332  1.10  christos 				ND_PRINT("%s > %s: ",
    333  1.10  christos 					GET_IP6ADDR_STRING(ip6->ip6_src),
    334  1.10  christos 					GET_IP6ADDR_STRING(ip6->ip6_dst));
    335   1.1  christos 			} else {
    336  1.10  christos 				ND_PRINT("%s.%s > %s.%s: ",
    337  1.10  christos 					GET_IP6ADDR_STRING(ip6->ip6_src),
    338  1.10  christos 					udpport_string(ndo, (uint16_t)sport),
    339  1.10  christos 					GET_IP6ADDR_STRING(ip6->ip6_dst),
    340  1.10  christos 					udpport_string(ndo, (uint16_t)dport));
    341   1.1  christos 			}
    342   1.1  christos 		} else {
    343   1.1  christos 			if (sport != -1) {
    344  1.10  christos 				ND_PRINT("%s > %s: ",
    345  1.10  christos 					udpport_string(ndo, (uint16_t)sport),
    346  1.10  christos 					udpport_string(ndo, (uint16_t)dport));
    347   1.1  christos 			}
    348   1.1  christos 		}
    349   1.7  christos 	} else {
    350  1.10  christos 		if (GET_U_1(ip->ip_p) == IPPROTO_UDP) {
    351   1.1  christos 			if (sport == -1) {
    352  1.10  christos 				ND_PRINT("%s > %s: ",
    353  1.10  christos 					GET_IPADDR_STRING(ip->ip_src),
    354  1.10  christos 					GET_IPADDR_STRING(ip->ip_dst));
    355   1.1  christos 			} else {
    356  1.10  christos 				ND_PRINT("%s.%s > %s.%s: ",
    357  1.10  christos 					GET_IPADDR_STRING(ip->ip_src),
    358  1.10  christos 					udpport_string(ndo, (uint16_t)sport),
    359  1.10  christos 					GET_IPADDR_STRING(ip->ip_dst),
    360  1.10  christos 					udpport_string(ndo, (uint16_t)dport));
    361   1.1  christos 			}
    362   1.1  christos 		} else {
    363   1.1  christos 			if (sport != -1) {
    364  1.10  christos 				ND_PRINT("%s > %s: ",
    365  1.10  christos 					udpport_string(ndo, (uint16_t)sport),
    366  1.10  christos 					udpport_string(ndo, (uint16_t)dport));
    367   1.1  christos 			}
    368   1.1  christos 		}
    369   1.1  christos 	}
    370   1.1  christos }
    371   1.1  christos 
    372   1.9     kamil UNALIGNED_OK
    373   1.1  christos void
    374  1.10  christos udp_print(netdissect_options *ndo, const u_char *bp, u_int length,
    375  1.10  christos 	  const u_char *bp2, int fragmented, u_int ttl_hl)
    376   1.1  christos {
    377  1.10  christos 	const struct udphdr *up;
    378  1.10  christos 	const struct ip *ip;
    379  1.10  christos 	const u_char *cp;
    380  1.10  christos 	const u_char *ep = ndo->ndo_snapend;
    381  1.10  christos 	uint16_t sport, dport;
    382  1.10  christos 	u_int ulen;
    383  1.10  christos 	const struct ip6_hdr *ip6;
    384   1.1  christos 
    385  1.10  christos 	ndo->ndo_protocol = "udp";
    386   1.7  christos 	up = (const struct udphdr *)bp;
    387   1.7  christos 	ip = (const struct ip *)bp2;
    388   1.1  christos 	if (IP_V(ip) == 6)
    389   1.7  christos 		ip6 = (const struct ip6_hdr *)bp2;
    390   1.1  christos 	else
    391   1.1  christos 		ip6 = NULL;
    392  1.10  christos 	if (!ND_TTEST_2(up->uh_dport)) {
    393   1.5  christos 		udpipaddr_print(ndo, ip, -1, -1);
    394   1.8       spz 		goto trunc;
    395   1.1  christos 	}
    396   1.1  christos 
    397  1.10  christos 	sport = GET_BE_U_2(up->uh_sport);
    398  1.10  christos 	dport = GET_BE_U_2(up->uh_dport);
    399   1.1  christos 
    400   1.1  christos 	if (length < sizeof(struct udphdr)) {
    401   1.5  christos 		udpipaddr_print(ndo, ip, sport, dport);
    402  1.10  christos 		ND_PRINT("truncated-udp %u", length);
    403   1.1  christos 		return;
    404   1.1  christos 	}
    405  1.10  christos 	if (!ND_TTEST_2(up->uh_ulen)) {
    406   1.8       spz 		udpipaddr_print(ndo, ip, sport, dport);
    407   1.8       spz 		goto trunc;
    408   1.8       spz 	}
    409  1.10  christos 	ulen = GET_BE_U_2(up->uh_ulen);
    410  1.10  christos 	/*
    411  1.10  christos 	 * IPv6 Jumbo Datagrams; see RFC 2675.
    412  1.10  christos 	 * If the length is zero, and the length provided to us is
    413  1.10  christos 	 * > 65535, use the provided length as the length.
    414  1.10  christos 	 */
    415  1.10  christos 	if (ulen == 0 && length > 65535)
    416  1.10  christos 		ulen = length;
    417   1.6  christos 	if (ulen < sizeof(struct udphdr)) {
    418   1.6  christos 		udpipaddr_print(ndo, ip, sport, dport);
    419  1.10  christos 		ND_PRINT("truncated-udplength %u", ulen);
    420   1.6  christos 		return;
    421   1.6  christos 	}
    422   1.6  christos 	ulen -= sizeof(struct udphdr);
    423   1.1  christos 	length -= sizeof(struct udphdr);
    424   1.6  christos 	if (ulen < length)
    425   1.6  christos 		length = ulen;
    426   1.1  christos 
    427   1.7  christos 	cp = (const u_char *)(up + 1);
    428   1.5  christos 	if (cp > ndo->ndo_snapend) {
    429   1.5  christos 		udpipaddr_print(ndo, ip, sport, dport);
    430   1.8       spz 		goto trunc;
    431   1.1  christos 	}
    432   1.1  christos 
    433   1.5  christos 	if (ndo->ndo_packettype) {
    434  1.10  christos 		const struct sunrpc_msg *rp;
    435   1.1  christos 		enum sunrpc_msg_type direction;
    436   1.1  christos 
    437   1.5  christos 		switch (ndo->ndo_packettype) {
    438   1.1  christos 
    439   1.1  christos 		case PT_VAT:
    440   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    441  1.10  christos 			vat_print(ndo, cp, length);
    442   1.1  christos 			break;
    443   1.1  christos 
    444   1.1  christos 		case PT_WB:
    445   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    446  1.10  christos 			wb_print(ndo, cp, length);
    447   1.1  christos 			break;
    448   1.1  christos 
    449   1.1  christos 		case PT_RPC:
    450  1.10  christos 			rp = (const struct sunrpc_msg *)cp;
    451  1.10  christos 			direction = (enum sunrpc_msg_type) GET_BE_U_4(rp->rm_direction);
    452   1.1  christos 			if (direction == SUNRPC_CALL)
    453  1.10  christos 				sunrpc_print(ndo, (const u_char *)rp, length,
    454   1.7  christos 				    (const u_char *)ip);
    455   1.1  christos 			else
    456   1.7  christos 				nfsreply_print(ndo, (const u_char *)rp, length,
    457   1.7  christos 				    (const u_char *)ip);			/*XXX*/
    458   1.1  christos 			break;
    459   1.1  christos 
    460   1.1  christos 		case PT_RTP:
    461   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    462  1.10  christos 			rtp_print(ndo, cp, length);
    463   1.1  christos 			break;
    464   1.1  christos 
    465   1.1  christos 		case PT_RTCP:
    466   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    467   1.1  christos 			while (cp < ep)
    468   1.5  christos 				cp = rtcp_print(ndo, cp, ep);
    469   1.1  christos 			break;
    470   1.1  christos 
    471   1.1  christos 		case PT_SNMP:
    472   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    473  1.10  christos 			snmp_print(ndo, cp, length);
    474   1.1  christos 			break;
    475   1.1  christos 
    476   1.1  christos 		case PT_CNFP:
    477   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    478   1.6  christos 			cnfp_print(ndo, cp);
    479   1.1  christos 			break;
    480   1.1  christos 
    481   1.1  christos 		case PT_TFTP:
    482   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    483   1.5  christos 			tftp_print(ndo, cp, length);
    484   1.1  christos 			break;
    485   1.1  christos 
    486   1.1  christos 		case PT_AODV:
    487   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    488  1.10  christos 			aodv_print(ndo, cp, length,
    489   1.1  christos 			    ip6 != NULL);
    490   1.1  christos 			break;
    491   1.4  christos 
    492   1.4  christos 		case PT_RADIUS:
    493   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    494   1.5  christos 			radius_print(ndo, cp, length);
    495   1.4  christos 			break;
    496   1.4  christos 
    497   1.4  christos 		case PT_VXLAN:
    498   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    499  1.10  christos 			vxlan_print(ndo, cp, length);
    500   1.4  christos 			break;
    501   1.4  christos 
    502   1.4  christos 		case PT_PGM:
    503   1.4  christos 		case PT_PGM_ZMTP1:
    504   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    505   1.5  christos 			pgm_print(ndo, cp, length, bp2);
    506   1.4  christos 			break;
    507   1.4  christos 		case PT_LMP:
    508   1.5  christos 			udpipaddr_print(ndo, ip, sport, dport);
    509   1.5  christos 			lmp_print(ndo, cp, length);
    510   1.4  christos 			break;
    511  1.10  christos 		case PT_PTP:
    512  1.10  christos 			udpipaddr_print(ndo, ip, sport, dport);
    513  1.10  christos 			ptp_print(ndo, cp, length);
    514  1.10  christos 			break;
    515  1.10  christos 		case PT_SOMEIP:
    516  1.10  christos 			udpipaddr_print(ndo, ip, sport, dport);
    517  1.10  christos 			someip_print(ndo, cp, length);
    518  1.10  christos 			break;
    519  1.10  christos 		case PT_DOMAIN:
    520  1.10  christos 			udpipaddr_print(ndo, ip, sport, dport);
    521  1.10  christos 			/* over_tcp: FALSE, is_mdns: FALSE */
    522  1.10  christos 			domain_print(ndo, cp, length, FALSE, FALSE);
    523  1.10  christos 			break;
    524   1.1  christos 		}
    525   1.1  christos 		return;
    526   1.1  christos 	}
    527   1.1  christos 
    528   1.5  christos 	udpipaddr_print(ndo, ip, sport, dport);
    529   1.5  christos 	if (!ndo->ndo_qflag) {
    530  1.10  christos 		const struct sunrpc_msg *rp;
    531   1.1  christos 		enum sunrpc_msg_type direction;
    532   1.1  christos 
    533  1.10  christos 		rp = (const struct sunrpc_msg *)cp;
    534  1.10  christos 		if (ND_TTEST_4(rp->rm_direction)) {
    535  1.10  christos 			direction = (enum sunrpc_msg_type) GET_BE_U_4(rp->rm_direction);
    536   1.1  christos 			if (dport == NFS_PORT && direction == SUNRPC_CALL) {
    537  1.10  christos 				ND_PRINT("NFS request xid %u ",
    538  1.10  christos 					 GET_BE_U_4(rp->rm_xid));
    539  1.10  christos 				nfsreq_noaddr_print(ndo, (const u_char *)rp, length,
    540   1.7  christos 				    (const u_char *)ip);
    541   1.1  christos 				return;
    542   1.1  christos 			}
    543   1.1  christos 			if (sport == NFS_PORT && direction == SUNRPC_REPLY) {
    544  1.10  christos 				ND_PRINT("NFS reply xid %u ",
    545  1.10  christos 					 GET_BE_U_4(rp->rm_xid));
    546  1.10  christos 				nfsreply_noaddr_print(ndo, (const u_char *)rp, length,
    547   1.7  christos 				    (const u_char *)ip);
    548   1.1  christos 				return;
    549   1.1  christos 			}
    550   1.1  christos #ifdef notdef
    551   1.1  christos 			if (dport == SUNRPC_PORT && direction == SUNRPC_CALL) {
    552  1.10  christos 				sunrpc_print((const u_char *)rp, length, (const u_char *)ip);
    553   1.1  christos 				return;
    554   1.1  christos 			}
    555   1.1  christos #endif
    556   1.1  christos 		}
    557   1.1  christos 	}
    558   1.1  christos 
    559   1.5  christos 	if (ndo->ndo_vflag && !ndo->ndo_Kflag && !fragmented) {
    560  1.10  christos 		/* Check the checksum, if possible. */
    561  1.10  christos 		uint16_t sum, udp_sum;
    562   1.3  christos 
    563   1.3  christos 		/*
    564   1.3  christos 		 * XXX - do this even if vflag == 1?
    565   1.3  christos 		 * TCP does, and we do so for UDP-over-IPv6.
    566   1.3  christos 		 */
    567  1.10  christos 		if (IP_V(ip) == 4 && (ndo->ndo_vflag > 1)) {
    568  1.10  christos 			udp_sum = GET_BE_U_2(up->uh_sum);
    569   1.3  christos 			if (udp_sum == 0) {
    570  1.10  christos 				ND_PRINT("[no cksum] ");
    571  1.10  christos 			} else if (ND_TTEST_LEN(cp, length)) {
    572   1.5  christos 				sum = udp_cksum(ndo, ip, up, length + sizeof(struct udphdr));
    573   1.3  christos 
    574  1.10  christos 				if (sum != 0) {
    575  1.10  christos 					ND_PRINT("[bad udp cksum 0x%04x -> 0x%04x!] ",
    576   1.3  christos 					    udp_sum,
    577  1.10  christos 					    in_cksum_shouldbe(udp_sum, sum));
    578   1.3  christos 				} else
    579  1.10  christos 					ND_PRINT("[udp sum ok] ");
    580   1.3  christos 			}
    581   1.1  christos 		}
    582  1.10  christos 		else if (IP_V(ip) == 6) {
    583   1.3  christos 			/* for IPv6, UDP checksum is mandatory */
    584  1.10  christos 			if (ND_TTEST_LEN(cp, length)) {
    585   1.7  christos 				sum = udp6_cksum(ndo, ip6, up, length + sizeof(struct udphdr));
    586  1.10  christos 				udp_sum = GET_BE_U_2(up->uh_sum);
    587   1.3  christos 
    588  1.10  christos 				if (sum != 0) {
    589  1.10  christos 					ND_PRINT("[bad udp cksum 0x%04x -> 0x%04x!] ",
    590   1.3  christos 					    udp_sum,
    591  1.10  christos 					    in_cksum_shouldbe(udp_sum, sum));
    592   1.3  christos 				} else
    593  1.10  christos 					ND_PRINT("[udp sum ok] ");
    594   1.3  christos 			}
    595   1.1  christos 		}
    596   1.1  christos 	}
    597   1.1  christos 
    598   1.5  christos 	if (!ndo->ndo_qflag) {
    599   1.7  christos 		if (IS_SRC_OR_DST_PORT(NAMESERVER_PORT))
    600  1.10  christos 			/* over_tcp: FALSE, is_mdns: FALSE */
    601  1.10  christos 			domain_print(ndo, cp, length, FALSE, FALSE);
    602   1.7  christos 		else if (IS_SRC_OR_DST_PORT(MULTICASTDNS_PORT))
    603  1.10  christos 			/* over_tcp: FALSE, is_mdns: TRUE */
    604  1.10  christos 			domain_print(ndo, cp, length, FALSE, TRUE);
    605   1.7  christos 		else if (IS_SRC_OR_DST_PORT(TIMED_PORT))
    606  1.10  christos 			timed_print(ndo, (const u_char *)cp);
    607   1.7  christos 		else if (IS_SRC_OR_DST_PORT(TFTP_PORT))
    608  1.10  christos 			tftp_print(ndo, cp, length);
    609   1.7  christos 		else if (IS_SRC_OR_DST_PORT(BOOTPC_PORT) || IS_SRC_OR_DST_PORT(BOOTPS_PORT))
    610  1.10  christos 			bootp_print(ndo, cp, length);
    611   1.7  christos 		else if (IS_SRC_OR_DST_PORT(RIP_PORT))
    612  1.10  christos 			rip_print(ndo, cp, length);
    613   1.7  christos 		else if (IS_SRC_OR_DST_PORT(AODV_PORT))
    614  1.10  christos 			aodv_print(ndo, cp, length,
    615   1.1  christos 			    ip6 != NULL);
    616  1.10  christos 		else if (IS_SRC_OR_DST_PORT(ISAKMP_PORT))
    617  1.10  christos 			 isakmp_print(ndo, cp, length, bp2);
    618  1.10  christos 		else if (IS_SRC_OR_DST_PORT(ISAKMP_PORT_NATT))
    619  1.10  christos 			 isakmp_rfc3948_print(ndo, cp, length, bp2, IP_V(ip), fragmented, ttl_hl);
    620  1.10  christos 		else if (IS_SRC_OR_DST_PORT(ISAKMP_PORT_USER1) || IS_SRC_OR_DST_PORT(ISAKMP_PORT_USER2))
    621  1.10  christos 			isakmp_print(ndo, cp, length, bp2);
    622   1.7  christos 		else if (IS_SRC_OR_DST_PORT(SNMP_PORT) || IS_SRC_OR_DST_PORT(SNMPTRAP_PORT))
    623  1.10  christos 			snmp_print(ndo, cp, length);
    624   1.7  christos 		else if (IS_SRC_OR_DST_PORT(NTP_PORT))
    625  1.10  christos 			ntp_print(ndo, cp, length);
    626   1.7  christos 		else if (IS_SRC_OR_DST_PORT(KERBEROS_PORT) || IS_SRC_OR_DST_PORT(KERBEROS_SEC_PORT))
    627  1.10  christos 			krb_print(ndo, (const u_char *)cp);
    628   1.7  christos 		else if (IS_SRC_OR_DST_PORT(L2TP_PORT))
    629  1.10  christos 			l2tp_print(ndo, cp, length);
    630   1.7  christos #ifdef ENABLE_SMB
    631   1.7  christos 		else if (IS_SRC_OR_DST_PORT(NETBIOS_NS_PORT))
    632  1.10  christos 			nbt_udp137_print(ndo, cp, length);
    633   1.7  christos 		else if (IS_SRC_OR_DST_PORT(NETBIOS_DGRAM_PORT))
    634  1.10  christos 			nbt_udp138_print(ndo, cp, length);
    635   1.1  christos #endif
    636   1.4  christos 		else if (dport == VAT_PORT)
    637  1.10  christos 			vat_print(ndo, cp, length);
    638   1.7  christos 		else if (IS_SRC_OR_DST_PORT(ZEPHYR_SRV_PORT) || IS_SRC_OR_DST_PORT(ZEPHYR_CLT_PORT))
    639  1.10  christos 			zephyr_print(ndo, cp, length);
    640   1.1  christos 		/*
    641   1.1  christos 		 * Since there are 10 possible ports to check, I think
    642   1.1  christos 		 * a <> test would be more efficient
    643   1.1  christos 		 */
    644   1.1  christos 		else if ((sport >= RX_PORT_LOW && sport <= RX_PORT_HIGH) ||
    645   1.1  christos 			 (dport >= RX_PORT_LOW && dport <= RX_PORT_HIGH))
    646  1.10  christos 			rx_print(ndo, cp, length, sport, dport,
    647   1.7  christos 				 (const u_char *) ip);
    648   1.7  christos 		else if (IS_SRC_OR_DST_PORT(RIPNG_PORT))
    649  1.10  christos 			ripng_print(ndo, cp, length);
    650   1.7  christos 		else if (IS_SRC_OR_DST_PORT(DHCP6_SERV_PORT) || IS_SRC_OR_DST_PORT(DHCP6_CLI_PORT))
    651  1.10  christos 			dhcp6_print(ndo, cp, length);
    652   1.7  christos 		else if (IS_SRC_OR_DST_PORT(AHCP_PORT))
    653  1.10  christos 			ahcp_print(ndo, cp, length);
    654   1.7  christos 		else if (IS_SRC_OR_DST_PORT(BABEL_PORT) || IS_SRC_OR_DST_PORT(BABEL_PORT_OLD))
    655  1.10  christos 			babel_print(ndo, cp, length);
    656   1.7  christos 		else if (IS_SRC_OR_DST_PORT(HNCP_PORT))
    657  1.10  christos 			hncp_print(ndo, cp, length);
    658   1.1  christos 		/*
    659   1.1  christos 		 * Kludge in test for whiteboard packets.
    660   1.1  christos 		 */
    661   1.4  christos 		else if (dport == WB_PORT)
    662  1.10  christos 			wb_print(ndo, cp, length);
    663   1.7  christos 		else if (IS_SRC_OR_DST_PORT(CISCO_AUTORP_PORT))
    664  1.10  christos 			cisco_autorp_print(ndo, cp, length);
    665   1.7  christos 		else if (IS_SRC_OR_DST_PORT(RADIUS_PORT) ||
    666   1.7  christos 			 IS_SRC_OR_DST_PORT(RADIUS_NEW_PORT) ||
    667   1.7  christos 			 IS_SRC_OR_DST_PORT(RADIUS_ACCOUNTING_PORT) ||
    668   1.7  christos 			 IS_SRC_OR_DST_PORT(RADIUS_NEW_ACCOUNTING_PORT) ||
    669   1.7  christos 			 IS_SRC_OR_DST_PORT(RADIUS_CISCO_COA_PORT) ||
    670   1.7  christos 			 IS_SRC_OR_DST_PORT(RADIUS_COA_PORT) )
    671  1.10  christos 			radius_print(ndo, cp, length);
    672   1.1  christos 		else if (dport == HSRP_PORT)
    673  1.10  christos 			hsrp_print(ndo, cp, length);
    674   1.7  christos 		else if (IS_SRC_OR_DST_PORT(LWRES_PORT))
    675  1.10  christos 			lwres_print(ndo, cp, length);
    676   1.7  christos 		else if (IS_SRC_OR_DST_PORT(LDP_PORT))
    677  1.10  christos 			ldp_print(ndo, cp, length);
    678   1.7  christos 		else if (IS_SRC_OR_DST_PORT(OLSR_PORT))
    679  1.10  christos 			olsr_print(ndo, cp, length,
    680   1.1  christos 					(IP_V(ip) == 6) ? 1 : 0);
    681   1.7  christos 		else if (IS_SRC_OR_DST_PORT(MPLS_LSP_PING_PORT))
    682  1.10  christos 			lspping_print(ndo, cp, length);
    683  1.10  christos 		else if (sport == BCM_LI_PORT)
    684  1.10  christos 			bcm_li_print(ndo, cp, length);
    685   1.1  christos 		else if (dport == BFD_CONTROL_PORT ||
    686  1.10  christos 			 dport == BFD_MULTIHOP_PORT ||
    687  1.10  christos 			 dport == BFD_LAG_PORT ||
    688   1.1  christos 			 dport == BFD_ECHO_PORT )
    689  1.10  christos 			bfd_print(ndo, cp, length, dport);
    690  1.10  christos 		else if (IS_SRC_OR_DST_PORT(LMP_PORT))
    691  1.10  christos 			lmp_print(ndo, cp, length);
    692   1.7  christos 		else if (IS_SRC_OR_DST_PORT(VQP_PORT))
    693  1.10  christos 			vqp_print(ndo, cp, length);
    694  1.10  christos 		else if (IS_SRC_OR_DST_PORT(SFLOW_PORT))
    695  1.10  christos 			sflow_print(ndo, cp, length);
    696  1.10  christos 		else if (dport == LWAPP_CONTROL_PORT)
    697  1.10  christos 			lwapp_control_print(ndo, cp, length, 1);
    698  1.10  christos 		else if (sport == LWAPP_CONTROL_PORT)
    699  1.10  christos 			lwapp_control_print(ndo, cp, length, 0);
    700  1.10  christos 		else if (IS_SRC_OR_DST_PORT(LWAPP_DATA_PORT))
    701  1.10  christos 			lwapp_data_print(ndo, cp, length);
    702  1.10  christos 		else if (IS_SRC_OR_DST_PORT(SIP_PORT))
    703  1.10  christos 			sip_print(ndo, cp, length);
    704  1.10  christos 		else if (IS_SRC_OR_DST_PORT(SYSLOG_PORT))
    705  1.10  christos 			syslog_print(ndo, cp, length);
    706  1.10  christos 		else if (IS_SRC_OR_DST_PORT(OTV_PORT))
    707  1.10  christos 			otv_print(ndo, cp, length);
    708  1.10  christos 		else if (IS_SRC_OR_DST_PORT(VXLAN_PORT))
    709  1.10  christos 			vxlan_print(ndo, cp, length);
    710  1.10  christos 		else if (dport == GENEVE_PORT)
    711  1.10  christos 			geneve_print(ndo, cp, length);
    712   1.7  christos 		else if (IS_SRC_OR_DST_PORT(LISP_CONTROL_PORT))
    713  1.10  christos 			lisp_print(ndo, cp, length);
    714   1.7  christos 		else if (IS_SRC_OR_DST_PORT(VXLAN_GPE_PORT))
    715  1.10  christos 			vxlan_gpe_print(ndo, cp, length);
    716  1.10  christos 		else if (IS_SRC_OR_DST_PORT(ZEP_PORT))
    717  1.10  christos 			zep_print(ndo, cp, length);
    718  1.10  christos 		else if (IS_SRC_OR_DST_PORT(MPLS_PORT))
    719  1.10  christos 			mpls_print(ndo, cp, length);
    720  1.10  christos 		else if (ND_TTEST_1(((const struct LAP *)cp)->type) &&
    721  1.10  christos 			 GET_U_1(((const struct LAP *)cp)->type) == lapDDP &&
    722  1.10  christos 			 (atalk_port(sport) || atalk_port(dport))) {
    723   1.7  christos 			if (ndo->ndo_vflag)
    724  1.10  christos 				ND_PRINT("kip ");
    725   1.7  christos 			llap_print(ndo, cp, length);
    726  1.10  christos 		} else if (IS_SRC_OR_DST_PORT(PTP_EVENT_PORT) ||
    727  1.10  christos 			IS_SRC_OR_DST_PORT(PTP_GENERAL_PORT)) {
    728  1.10  christos 			ptp_print(ndo, cp, length);
    729  1.10  christos 		} else if (IS_SRC_OR_DST_PORT(SOMEIP_PORT))
    730  1.10  christos 			someip_print(ndo, cp, length);
    731  1.10  christos 		else {
    732  1.10  christos 			if (ulen > length && !fragmented)
    733  1.10  christos 				ND_PRINT("UDP, bad length %u > %u",
    734  1.10  christos 				    ulen, length);
    735   1.6  christos 			else
    736  1.10  christos 				ND_PRINT("UDP, length %u", ulen);
    737   1.6  christos 		}
    738   1.6  christos 	} else {
    739  1.10  christos 		if (ulen > length && !fragmented)
    740  1.10  christos 			ND_PRINT("UDP, bad length %u > %u",
    741  1.10  christos 			    ulen, length);
    742   1.1  christos 		else
    743  1.10  christos 			ND_PRINT("UDP, length %u", ulen);
    744   1.6  christos 	}
    745   1.8       spz 	return;
    746   1.8       spz 
    747   1.8       spz trunc:
    748  1.10  christos 	nd_print_trunc(ndo);
    749   1.1  christos }
    750