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