xref: /openbsd-src/usr.sbin/tcpdump/print-udp.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: print-udp.c,v 1.54 2020/01/24 22:46:37 procter 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 		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, dlen, contype, (hasopt || hasext)? "+" : "",
171 	    hasmarker? "*" : "", i0 & 0xffff, i1);
172 	if (vflag) {
173 		printf(" %u", i1);
174 		if (hasopt) {
175 			u_int i2, optlen;
176 			do {
177 				i2 = ip[0];
178 				optlen = (i2 >> 16) & 0xff;
179 				if (optlen == 0 || optlen > len) {
180 					printf(" !opt");
181 					return;
182 				}
183 				ip += optlen;
184 				len -= optlen;
185 			} while ((int)i2 >= 0);
186 		}
187 		if (hasext) {
188 			u_int i2, extlen;
189 			i2 = ip[0];
190 			extlen = (i2 & 0xffff) + 1;
191 			if (extlen > len) {
192 				printf(" !ext");
193 				return;
194 			}
195 			ip += extlen;
196 		}
197 		if (contype == 0x1f) /*XXX H.261 */
198 			printf(" 0x%04x", ip[0] >> 16);
199 	}
200 }
201 
202 static const u_char *
203 rtcp_print(const u_char *hdr, const u_char *ep)
204 {
205 	/* rtp v2 control (rtcp) */
206 	struct rtcp_rr *rr = NULL;
207 	struct rtcp_sr *sr;
208 	struct rtcphdr *rh = (struct rtcphdr *)hdr;
209 	u_int len;
210 	u_short flags;
211 	int cnt;
212 	double ts, dts;
213 	if ((u_char *)(rh + 1) > ep) {
214 		printf(" [|rtcp]");
215 		return (ep);
216 	}
217 	len = (ntohs(rh->rh_len) + 1) * 4;
218 	flags = ntohs(rh->rh_flags);
219 	cnt = (flags >> 8) & 0x1f;
220 	switch (flags & 0xff) {
221 	case RTCP_PT_SR:
222 		sr = (struct rtcp_sr *)(rh + 1);
223 		printf(" sr");
224 		if (len != cnt * sizeof(*rr) + sizeof(*sr) + sizeof(*rh))
225 			printf(" [%d]", len);
226 		if (vflag)
227 			printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
228 		if ((u_char *)(sr + 1) > ep) {
229 			printf(" [|rtcp]");
230 			return (ep);
231 		}
232 		ts = (double)((u_int32_t)ntohl(sr->sr_ntp.upper)) +
233 		    ((double)((u_int32_t)ntohl(sr->sr_ntp.lower)) /
234 		    4294967296.0);
235 		printf(" @%.2f %u %up %ub", ts, (u_int32_t)ntohl(sr->sr_ts),
236 		    (u_int32_t)ntohl(sr->sr_np), (u_int32_t)ntohl(sr->sr_nb));
237 		rr = (struct rtcp_rr *)(sr + 1);
238 		break;
239 	case RTCP_PT_RR:
240 		printf(" rr");
241 		if (len != cnt * sizeof(*rr) + sizeof(*rh))
242 			printf(" [%d]", len);
243 		rr = (struct rtcp_rr *)(rh + 1);
244 		if (vflag)
245 			printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
246 		break;
247 	case RTCP_PT_SDES:
248 		printf(" sdes %d", len);
249 		if (vflag)
250 			printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
251 		cnt = 0;
252 		break;
253 	case RTCP_PT_BYE:
254 		printf(" bye %d", len);
255 		if (vflag)
256 			printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
257 		cnt = 0;
258 		break;
259 	default:
260 		printf(" type-0x%x %d", flags & 0xff, len);
261 		cnt = 0;
262 		break;
263 	}
264 	if (cnt > 1)
265 		printf(" c%d", cnt);
266 	while (--cnt >= 0) {
267 		if ((u_char *)(rr + 1) > ep) {
268 			printf(" [|rtcp]");
269 			return (ep);
270 		}
271 		if (vflag)
272 			printf(" %u", (u_int32_t)ntohl(rr->rr_srcid));
273 		ts = (double)((u_int32_t)ntohl(rr->rr_lsr)) / 65536.;
274 		dts = (double)((u_int32_t)ntohl(rr->rr_dlsr)) / 65536.;
275 		printf(" %ul %us %uj @%.2f+%.2f",
276 		    (u_int32_t)ntohl(rr->rr_nl) & 0x00ffffff,
277 		    (u_int32_t)ntohl(rr->rr_ls),
278 		    (u_int32_t)ntohl(rr->rr_dv), ts, dts);
279 	}
280 	return (hdr + len);
281 }
282 
283 /* XXX probably should use getservbyname() and cache answers */
284 #define TFTP_PORT		69		/*XXX*/
285 #define KERBEROS_PORT		88		/*XXX*/
286 #define SUNRPC_PORT		111		/*XXX*/
287 #define NTP_PORT		123		/*XXX*/
288 #define NETBIOS_NS_PORT		137		/*XXX*/
289 #define NETBIOS_DGRAM_PORT	138		/*XXX*/
290 #define SNMP_PORT		161		/*XXX*/
291 #define SNMPTRAP_PORT		162		/*XXX*/
292 #define ISAKMP_PORT		500		/*XXX*/
293 #define RIP_PORT		520		/*XXX*/
294 #define TIMED_PORT		525		/*XXX*/
295 #define KERBEROS_SEC_PORT	750		/*XXX*/
296 #define LWRES_PORT		921
297 #define VQP_PORT		1589
298 #define OLD_RADIUS_AUTH_PORT	1645
299 #define OLD_RADIUS_ACCT_PORT	1646
300 #define L2TP_PORT		1701		/*XXX*/
301 #define RADIUS_AUTH_PORT	1812
302 #define RADIUS_ACCT_PORT	1813
303 #define HSRP_PORT		1985		/*XXX*/
304 #define GTP_C_PORT		2123
305 #define GTP_U_PORT		2152
306 #define GTP_PRIME_PORT		3386
307 #define UDPENCAP_PORT		4500		/*XXX*/
308 #define GRE_PORT		4754
309 #define VXLAN_PORT		4789
310 #define VXLAN_GPE_PORT		4790
311 #define MULTICASTDNS_PORT	5353
312 #define MPLS_PORT		6635
313 
314 #define RIPNG_PORT		521		/*XXX*/
315 #define DHCP6_PORT1		546		/*XXX*/
316 #define DHCP6_PORT2		547		/*XXX*/
317 
318 void
319 udp_print(const u_char *bp, u_int length, const void *iph)
320 {
321 	const struct udphdr *up;
322 	const u_char *cp;
323 	const u_char *ep = bp + length;
324 	u_int16_t sport, dport, ulen;
325 	const char *ipsrc = NULL, *ipdst = NULL;
326 	unsigned int ipv = 0;
327 	uint32_t cksum = 0;
328 
329 	if (ep > snapend)
330 		ep = snapend;
331 
332 	if (iph != NULL) {
333 		const struct ip *ip = iph;
334 		ipv = ip->ip_v;
335 
336 		switch (ipv) {
337 		case 6: {
338 			const struct ip6_hdr *ip6 = iph;
339 
340 			ipsrc = ip6addr_string(&ip6->ip6_src);
341 			ipdst = ip6addr_string(&ip6->ip6_dst);
342 
343 			cksum = in_cksum_add(&ip6->ip6_src,
344 			    sizeof(ip6->ip6_src), cksum);
345 			cksum = in_cksum_add(&ip6->ip6_dst,
346 			    sizeof(ip6->ip6_dst), cksum);
347 			break;
348 		}
349 		case 4:
350 			ipsrc = ipaddr_string(&ip->ip_src);
351 			ipdst = ipaddr_string(&ip->ip_dst);
352 
353 			cksum = in_cksum_add(&ip->ip_src,
354 			    sizeof(ip->ip_src), cksum);
355 			cksum = in_cksum_add(&ip->ip_dst,
356 			    sizeof(ip->ip_dst), cksum);
357 			break;
358 		}
359 	}
360 
361 	up = (const struct udphdr *)bp;
362 	cp = (const u_char *)(up + 1);
363 
364 	/* check if the udp header was captured */
365 	if (cp > snapend) {
366 		if (ipv)
367 			printf("%s > %s: ", ipsrc, ipdst);
368 
369 		printf("[|udp]");
370 		return;
371 	}
372 
373 	/* check if the packet payload is long enough */
374 	if (length < sizeof(*up)) {
375 		if (ipv)
376 			printf("%s > %s: ", ipsrc, ipdst);
377 
378 		printf("truncated-udp %u", length);
379 		return;
380 	}
381 
382 	sport = ntohs(up->uh_sport);
383 	dport = ntohs(up->uh_dport);
384 
385 	if (ipv) {
386 		printf("%s.%s > %s.%s",
387 		    ipsrc, udpport_string(sport),
388 		    ipdst, udpport_string(dport));
389 	} else {
390 		printf("udp %s > %s",
391 		    udpport_string(sport),
392 		    udpport_string(dport));
393 	}
394 
395 	printf(": ");
396 
397 	cksum += htons(length);
398 
399 	ulen = ntohs(up->uh_ulen);
400 	if (length < ulen)
401 		printf(" truncated-udp - %u bytes missing!", ulen - length);
402 
403 	length -= sizeof(*up);
404 
405 	if (vflag && ipv && TTEST2(cp[0], length)) {
406 		uint16_t sum, usum = up->uh_sum;
407 
408 		if (usum == 0) {
409 			if (ipv == 4)
410 				printf("[no udp cksum] ");
411 			else
412 				printf("[invalid udp cksum 0] ");
413 		} else {
414 			cksum += htons(IPPROTO_UDP);
415 			cksum += up->uh_sport;
416 			cksum += up->uh_dport;
417 			cksum += up->uh_ulen;
418 
419 			sum = in_cksum(cp, length, cksum);
420 
421 			if (sum == usum)
422 				printf("[udp sum ok] ");
423 			else {
424 				printf("[bad udp cksum %04x! -> %04x] ",
425 				    usum, sum);
426 			}
427 		}
428 	}
429 
430 	if (packettype) {
431 		struct rpc_msg *rp;
432 		enum msg_type direction;
433 
434 		switch (packettype) {
435 		case PT_VAT:
436 			vat_print(cp, length, up);
437 			break;
438 
439 		case PT_WB:
440 			wb_print(cp, length);
441 			break;
442 
443 		case PT_RPC:
444 			rp = (struct rpc_msg *)cp;
445 			direction = (enum msg_type)ntohl(rp->rm_direction);
446 			if (direction == CALL)
447 				sunrpcrequest_print(cp, length, iph);
448 			else
449 				nfsreply_print(cp, length, iph);
450 			break;
451 
452 		case PT_RTP:
453 			rtp_print(cp, length, up);
454 			break;
455 
456 		case PT_RTCP:
457 			while (cp < ep)
458 				cp = rtcp_print(cp, ep);
459 			break;
460 		case PT_CNFP:
461 			cnfp_print(cp, length);
462 			break;
463 		case PT_GRE:
464 			gre_print(cp, length);
465 			break;
466 		case PT_VXLAN:
467 			vxlan_print(cp, length);
468 			break;
469 		case PT_MPLS:
470 			mpls_print(cp, length);
471 			break;
472 		case PT_TFTP:
473 			tftp_print(cp, length);
474 			break;
475 		}
476 		return;
477 	}
478 
479 	if (!qflag) {
480 		struct rpc_msg *rp;
481 		enum msg_type direction;
482 
483 		rp = (struct rpc_msg *)cp;
484 		if (TTEST(rp->rm_direction)) {
485 			direction = (enum msg_type)ntohl(rp->rm_direction);
486 			if (dport == NFS_PORT && direction == CALL) {
487 				nfsreq_print(cp, length, iph);
488 				return;
489 			}
490 			if (sport == NFS_PORT && direction == REPLY) {
491 				nfsreply_print(cp, length, iph);
492 				return;
493 			}
494 #ifdef notdef
495 			if (dport == SUNRPC_PORT && direction == CALL) {
496 				sunrpcrequest_print(cp, length, iph);
497 				return;
498 			}
499 #endif
500 		}
501 		if (TTEST(((struct LAP *)cp)->type) &&
502 		    ((struct LAP *)cp)->type == lapDDP &&
503 		    (atalk_port(sport) || atalk_port(dport))) {
504 			if (vflag)
505 				printf("kip ");
506 			atalk_print_llap(cp, length);
507 			return;
508 		}
509 	}
510 
511 	if (!qflag) {
512 #define ISPORT(p) (dport == (p) || sport == (p))
513 		if (ISPORT(NAMESERVER_PORT))
514 			ns_print(cp, length, 0);
515 		else if (ISPORT(MULTICASTDNS_PORT))
516 			ns_print(cp, length, 1);
517 		else if (ISPORT(LWRES_PORT))
518 			lwres_print(cp, length);
519 		else if (ISPORT(TIMED_PORT))
520 			timed_print(cp, length);
521 		else if (ISPORT(TFTP_PORT))
522 			tftp_print(cp, length);
523 		else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
524 			bootp_print(cp, length, sport, dport);
525 		else if (ISPORT(RIP_PORT))
526 			rip_print(cp, length);
527 		else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
528 			snmp_print(cp, length);
529 		else if (ISPORT(NTP_PORT))
530 			ntp_print(cp, length);
531 		else if (ISPORT(KERBEROS_PORT) || ISPORT(KERBEROS_SEC_PORT))
532 			krb_print(cp, length);
533 		else if (ISPORT(L2TP_PORT))
534 			l2tp_print(cp, length);
535 		else if (ISPORT(UDPENCAP_PORT))
536 			udpencap_print(cp, length, iph);
537 		else if (ISPORT(ISAKMP_PORT))
538 			ike_print(cp, length);
539 #if 0
540 		else if (ISPORT(NETBIOS_NS_PORT))
541 			nbt_udp137_print(cp, length);
542 		else if (ISPORT(NETBIOS_DGRAM_PORT))
543 			nbt_udp138_print(cp, length);
544 #endif
545                 else if (ISPORT(OLD_RADIUS_AUTH_PORT) ||
546                          ISPORT(OLD_RADIUS_ACCT_PORT) ||
547                          ISPORT(RADIUS_AUTH_PORT)     ||
548                          ISPORT(RADIUS_ACCT_PORT))
549                         radius_print(cp, length);
550 		else if (dport == 3456)
551 			vat_print(cp, length, up);
552 		else if (ISPORT(IAPP_PORT) || ISPORT(IAPP_OLD_PORT))
553 			iapp_print(cp, length);
554 		else if (ISPORT(VQP_PORT))
555 			vqp_print(cp, length);
556 		else if (ISPORT(GRE_PORT))
557 			gre_print(cp, length);
558 		else if (ISPORT(VXLAN_PORT) || ISPORT(VXLAN_GPE_PORT))
559 			vxlan_print(cp, length);
560 		else if (ISPORT(MPLS_PORT))
561 			mpls_print(cp, length);
562 		else if (ISPORT(RIPNG_PORT))
563 			ripng_print(cp, length);
564 		else if (ISPORT(DHCP6_PORT1) || ISPORT(DHCP6_PORT2))
565 			dhcp6_print(cp, length);
566 		else if (ISPORT(GTP_C_PORT) || ISPORT(GTP_U_PORT) ||
567 		    ISPORT(GTP_PRIME_PORT))
568 			gtp_print(cp, length, sport, dport);
569 		/*
570 		 * Kludge in test for whiteboard packets.
571 		 */
572 		else if (dport == 4567)
573 			wb_print(cp, length);
574 		else if (dport == HSRP_PORT)
575 			hsrp_print(cp, length);
576 		else
577 			printf("udp %u", length);
578 #undef ISPORT
579 	} else
580 		printf("udp %u", length);
581 }
582