xref: /titanic_50/usr/src/stand/lib/inet/udp.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1991-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * udp.c, Code implementing the UDP internet protocol.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <socket_impl.h>
33*7c478bd9Sstevel@tonic-gate #include <socket_inet.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/salib.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
36*7c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
37*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
38*7c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
39*7c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include "udp_inet.h"
42*7c478bd9Sstevel@tonic-gate #include "ipv4.h"
43*7c478bd9Sstevel@tonic-gate #include "ipv4_impl.h"
44*7c478bd9Sstevel@tonic-gate #include "mac.h"
45*7c478bd9Sstevel@tonic-gate #include "mac_impl.h"
46*7c478bd9Sstevel@tonic-gate #include "v4_sum_impl.h"
47*7c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h>
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /* Checksum all architectures */
50*7c478bd9Sstevel@tonic-gate static int udp_cksum_flag = TRUE;	/* checksum on if set */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * Initialize the udp-specific parts of a socket.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate void
udp_socket_init(struct inetboot_socket * isp)56*7c478bd9Sstevel@tonic-gate udp_socket_init(struct inetboot_socket *isp)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	isp->type = INETBOOT_DGRAM;
59*7c478bd9Sstevel@tonic-gate 	isp->proto = IPPROTO_UDP;
60*7c478bd9Sstevel@tonic-gate 	isp->input[TRANSPORT_LVL] = udp_input;
61*7c478bd9Sstevel@tonic-gate 	isp->output[TRANSPORT_LVL] = udp_output;
62*7c478bd9Sstevel@tonic-gate 	isp->close[TRANSPORT_LVL] = NULL;
63*7c478bd9Sstevel@tonic-gate 	isp->headerlen[TRANSPORT_LVL] = udp_header_len;
64*7c478bd9Sstevel@tonic-gate 	isp->ports = udp_ports;
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * Return the size of an UDP header
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
71*7c478bd9Sstevel@tonic-gate int
udp_header_len(struct inetgram * igm)72*7c478bd9Sstevel@tonic-gate udp_header_len(struct inetgram *igm)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	return (sizeof (struct udphdr));
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /*
78*7c478bd9Sstevel@tonic-gate  * Return the requested port number in network order.
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate in_port_t
udp_ports(uint16_t * udphp,enum Ports request)81*7c478bd9Sstevel@tonic-gate udp_ports(uint16_t *udphp, enum Ports request)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	if (request == SOURCE)
84*7c478bd9Sstevel@tonic-gate 		return (((struct udphdr *)udphp)->uh_sport);
85*7c478bd9Sstevel@tonic-gate 	return (((struct udphdr *)udphp)->uh_dport);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate /*
89*7c478bd9Sstevel@tonic-gate  * Process the IPv4 datagram that IPv4 has given us. We:
90*7c478bd9Sstevel@tonic-gate  *
91*7c478bd9Sstevel@tonic-gate  *	1) Checksum the datagram if checksum is turned on.
92*7c478bd9Sstevel@tonic-gate  *	2) Strip the udp header from the inetgram.
93*7c478bd9Sstevel@tonic-gate  *	3) Return the number of TRANSPORT frames for success, -1 if a
94*7c478bd9Sstevel@tonic-gate  *		failure occurred.
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  * Arguments: index: index into the open socket table, ip is the inetgram
97*7c478bd9Sstevel@tonic-gate  * we got from IPv4, which will contain the udp header and all the data.
98*7c478bd9Sstevel@tonic-gate  */
99*7c478bd9Sstevel@tonic-gate int
udp_input(int index)100*7c478bd9Sstevel@tonic-gate udp_input(int index)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	int frames = 0, header_len;
103*7c478bd9Sstevel@tonic-gate 	struct inetgram	*igp, *ugp = NULL;
104*7c478bd9Sstevel@tonic-gate 	struct udphdr	*udphp;
105*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
108*7c478bd9Sstevel@tonic-gate 	printf("udp_input(%d) ###############################\n", index);
109*7c478bd9Sstevel@tonic-gate #endif
110*7c478bd9Sstevel@tonic-gate 	while ((igp = sockets[index].inq) != NULL) {
111*7c478bd9Sstevel@tonic-gate 		if (igp->igm_level != TRANSPORT_LVL) {
112*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
113*7c478bd9Sstevel@tonic-gate 			printf("udp_input(%d): level %d datagram discarded.\n",
114*7c478bd9Sstevel@tonic-gate 			    index, igp->igm_level);
115*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
116*7c478bd9Sstevel@tonic-gate 			del_gram(&sockets[index].inq, igp, TRUE);
117*7c478bd9Sstevel@tonic-gate 			continue;
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 		mp = igp->igm_mp;
120*7c478bd9Sstevel@tonic-gate 		udphp = (struct udphdr *)(mp->b_rptr +
121*7c478bd9Sstevel@tonic-gate 		    IPH_HDR_LENGTH(mp->b_rptr));
122*7c478bd9Sstevel@tonic-gate 		header_len = (sockets[index].headerlen[TRANSPORT_LVL])(NULL);
123*7c478bd9Sstevel@tonic-gate 		mp->b_rptr = ((unsigned char *)udphp) + header_len;
124*7c478bd9Sstevel@tonic-gate 		mp->b_wptr = ((unsigned char *)udphp) + ntohs(udphp->uh_ulen);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 		/* generate checksum */
127*7c478bd9Sstevel@tonic-gate 		if (udp_cksum_flag && udphp->uh_sum != 0) {
128*7c478bd9Sstevel@tonic-gate 			if (udp_chksum(udphp, &igp->igm_saddr.sin_addr,
129*7c478bd9Sstevel@tonic-gate 			    &igp->igm_target, sockets[index].proto) != 0) {
130*7c478bd9Sstevel@tonic-gate 				dprintf("udp_input(%d): bad udp chksum "
131*7c478bd9Sstevel@tonic-gate 				    "from %s.\n", index,
132*7c478bd9Sstevel@tonic-gate 				    inet_ntoa(igp->igm_saddr.sin_addr));
133*7c478bd9Sstevel@tonic-gate 				del_gram(&sockets[index].inq, igp, TRUE);
134*7c478bd9Sstevel@tonic-gate 				continue;
135*7c478bd9Sstevel@tonic-gate 			}
136*7c478bd9Sstevel@tonic-gate 		}
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 		/* validate port number */
139*7c478bd9Sstevel@tonic-gate 		if (sockets[index].bind.sin_port != udphp->uh_dport) {
140*7c478bd9Sstevel@tonic-gate 			dprintf("udp_input(%d): Unexpected port number: "
141*7c478bd9Sstevel@tonic-gate 			    "%d != %d from %s.\n", index,
142*7c478bd9Sstevel@tonic-gate 			    ntohs(udphp->uh_dport), ntohs(
143*7c478bd9Sstevel@tonic-gate 			    sockets[index].bind.sin_port),
144*7c478bd9Sstevel@tonic-gate 			    inet_ntoa(igp->igm_saddr.sin_addr));
145*7c478bd9Sstevel@tonic-gate 			del_gram(&sockets[index].inq, igp, TRUE);
146*7c478bd9Sstevel@tonic-gate 			continue;
147*7c478bd9Sstevel@tonic-gate 		}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		igp->igm_level = APP_LVL;
150*7c478bd9Sstevel@tonic-gate 		del_gram(&sockets[index].inq, igp, FALSE);
151*7c478bd9Sstevel@tonic-gate 		add_grams(&ugp, igp);
152*7c478bd9Sstevel@tonic-gate 		frames++;
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	add_grams(&sockets[index].inq, ugp);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	return (frames);
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate /*
160*7c478bd9Sstevel@tonic-gate  * Create a UDP datagram given the data and sockaddr_in we got from sendto().
161*7c478bd9Sstevel@tonic-gate  * We will calculate the checksum if checksumming is turned on, and fill in
162*7c478bd9Sstevel@tonic-gate  * appropriate length and port fields. We convert the inetgram from a
163*7c478bd9Sstevel@tonic-gate  * block of data into a udp datagram... Returns the number of bytes contained
164*7c478bd9Sstevel@tonic-gate  * in the udp datagram (including header).
165*7c478bd9Sstevel@tonic-gate  *
166*7c478bd9Sstevel@tonic-gate  * Arguments: index: index into the open socket table, ogp is the inetgram
167*7c478bd9Sstevel@tonic-gate  * we got from sendto(), which will contain just the data and sockaddr_in.
168*7c478bd9Sstevel@tonic-gate  */
169*7c478bd9Sstevel@tonic-gate int
udp_output(int index,struct inetgram * ogp)170*7c478bd9Sstevel@tonic-gate udp_output(int index, struct inetgram *ogp)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 	struct udphdr	*udphp;
173*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
176*7c478bd9Sstevel@tonic-gate 	printf("udp_output(%d): 0x%x, %d\n", index, ogp->igm_mp,
177*7c478bd9Sstevel@tonic-gate 	    ogp->igm_mp->b_wptr - ogp->igm_mp->b_rptr);
178*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	mp = ogp->igm_mp;
181*7c478bd9Sstevel@tonic-gate 	mp->b_rptr -= sizeof (struct udphdr);
182*7c478bd9Sstevel@tonic-gate 	udphp = (struct udphdr *)(mp->b_rptr);
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	udphp->uh_dport = ogp->igm_saddr.sin_port;
185*7c478bd9Sstevel@tonic-gate 	if (sockets[index].bound)
186*7c478bd9Sstevel@tonic-gate 		udphp->uh_sport = sockets[index].bind.sin_port;
187*7c478bd9Sstevel@tonic-gate 	else
188*7c478bd9Sstevel@tonic-gate 		udphp->uh_sport = ogp->igm_saddr.sin_port;
189*7c478bd9Sstevel@tonic-gate 	udphp->uh_ulen = htons(mp->b_wptr - mp->b_rptr);
190*7c478bd9Sstevel@tonic-gate 	udphp->uh_sum = 0;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	if (udp_cksum_flag) {
193*7c478bd9Sstevel@tonic-gate 		udphp->uh_sum = udp_chksum(udphp, &sockets[index].bind.sin_addr,
194*7c478bd9Sstevel@tonic-gate 		    &ogp->igm_saddr.sin_addr, sockets[index].proto);
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	ogp->igm_level = NETWORK_LVL;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	return (0);
200*7c478bd9Sstevel@tonic-gate }
201