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