17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*2e3b6467Skcpoon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*2e3b6467Skcpoon  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/time.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
377c478bd9Sstevel@tonic-gate #include <net/if.h>
387c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
397c478bd9Sstevel@tonic-gate #include <netinet/in.h>
407c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
417c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
427c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
437c478bd9Sstevel@tonic-gate #include "snoop.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate extern char *dlc_header;
467c478bd9Sstevel@tonic-gate 
47*2e3b6467Skcpoon int
interpret_udp(int flags,struct udphdr * udp,int iplen,int fraglen)48*2e3b6467Skcpoon interpret_udp(int flags, struct udphdr *udp, int iplen, int fraglen)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	char *data;
517c478bd9Sstevel@tonic-gate 	int udplen;
527c478bd9Sstevel@tonic-gate 	int sunrpc;
537c478bd9Sstevel@tonic-gate 	char *pname;
547c478bd9Sstevel@tonic-gate 	char buff [32];
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (fraglen < sizeof (struct udphdr))
577c478bd9Sstevel@tonic-gate 		return (fraglen);	/* incomplete header */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	data = (char *)udp + sizeof (struct udphdr);
60*2e3b6467Skcpoon 	udplen = ntohs((ushort_t)udp->uh_ulen) - sizeof (struct udphdr);
617c478bd9Sstevel@tonic-gate 	fraglen -= sizeof (struct udphdr);
627c478bd9Sstevel@tonic-gate 	if (fraglen > udplen)
637c478bd9Sstevel@tonic-gate 		fraglen = udplen;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
667c478bd9Sstevel@tonic-gate 		(void) sprintf(get_sum_line(),
677c478bd9Sstevel@tonic-gate 			"UDP D=%d S=%d LEN=%d",
687c478bd9Sstevel@tonic-gate 			ntohs(udp->uh_dport),
697c478bd9Sstevel@tonic-gate 			ntohs(udp->uh_sport),
70*2e3b6467Skcpoon 			ntohs((ushort_t)udp->uh_ulen));
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	sunrpc = !reservedport(IPPROTO_UDP, ntohs(udp->uh_dport)) &&
747c478bd9Sstevel@tonic-gate 		!reservedport(IPPROTO_UDP, ntohs(udp->uh_sport)) &&
757c478bd9Sstevel@tonic-gate 		valid_rpc(data, udplen);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
787c478bd9Sstevel@tonic-gate 		show_header("UDP:  ", "UDP Header", udplen);
797c478bd9Sstevel@tonic-gate 		show_space();
80*2e3b6467Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)udp->uh_sport -
81*2e3b6467Skcpoon 		    dlc_header, 1), "Source port = %d", ntohs(udp->uh_sport));
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		if (sunrpc) {
847c478bd9Sstevel@tonic-gate 			pname = "(Sun RPC)";
857c478bd9Sstevel@tonic-gate 		} else {
867c478bd9Sstevel@tonic-gate 			pname = getportname(IPPROTO_UDP, ntohs(udp->uh_dport));
877c478bd9Sstevel@tonic-gate 			if (pname == NULL) {
887c478bd9Sstevel@tonic-gate 				pname = "";
897c478bd9Sstevel@tonic-gate 			} else {
907c478bd9Sstevel@tonic-gate 				(void) sprintf(buff, "(%s)", pname);
917c478bd9Sstevel@tonic-gate 				pname = buff;
927c478bd9Sstevel@tonic-gate 			}
937c478bd9Sstevel@tonic-gate 		}
94*2e3b6467Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)udp->uh_dport -
95*2e3b6467Skcpoon 		    dlc_header, 1), "Destination port = %d %s",
96*2e3b6467Skcpoon 		    ntohs(udp->uh_dport), pname);
97*2e3b6467Skcpoon 		(void) sprintf(get_line((char *)(uintptr_t)udp->uh_ulen -
98*2e3b6467Skcpoon 		    dlc_header, 1), "Length = %d %s",
99*2e3b6467Skcpoon 		    ntohs((ushort_t)udp->uh_ulen),
100*2e3b6467Skcpoon 		    udplen > fraglen ?
101*2e3b6467Skcpoon 			"(Not all data contained in this fragment)"
102*2e3b6467Skcpoon 			: "");
103*2e3b6467Skcpoon 		    (void) sprintf(get_line((char *)(uintptr_t)udp->uh_sum -
104*2e3b6467Skcpoon 			dlc_header, 1),	"Checksum = %04X %s",
105*2e3b6467Skcpoon 			ntohs(udp->uh_sum),
106*2e3b6467Skcpoon 			udp->uh_sum == 0 ? "(no checksum)" : "");
1077c478bd9Sstevel@tonic-gate 		show_space();
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/* go to the next protocol layer */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (!interpret_reserved(flags, IPPROTO_UDP,
1147c478bd9Sstevel@tonic-gate 		ntohs(udp->uh_sport),
1157c478bd9Sstevel@tonic-gate 		ntohs(udp->uh_dport),
1167c478bd9Sstevel@tonic-gate 		data, fraglen)) {
1177c478bd9Sstevel@tonic-gate 		if (fraglen > 0 && sunrpc)
1187c478bd9Sstevel@tonic-gate 			interpret_rpc(flags, data, fraglen, IPPROTO_UDP);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	return (fraglen);
1227c478bd9Sstevel@tonic-gate }
123