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
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.
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]
23 * Copyright 1991-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * udp.c, Code implementing the UDP internet protocol.
29 #pragma ident "%Z%%M% %I% %E% SMI"
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>
43 #include "ipv4_impl.h"
46 #include "v4_sum_impl.h"
47 #include <sys/bootdebug.h>
49 /* Checksum all architectures */
50 static int udp_cksum_flag
= TRUE
; /* checksum on if set */
53 * Initialize the udp-specific parts of a socket.
56 udp_socket_init(struct inetboot_socket
*isp
)
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
;
68 * Return the size of an UDP header
72 udp_header_len(struct inetgram
*igm
)
74 return (sizeof (struct udphdr
));
78 * Return the requested port number in network order.
81 udp_ports(uint16_t *udphp
, enum Ports request
)
83 if (request
== SOURCE
)
84 return (((struct udphdr
*)udphp
)->uh_sport
);
85 return (((struct udphdr
*)udphp
)->uh_dport
);
89 * Process the IPv4 datagram that IPv4 has given us. We:
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
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.
102 int frames
= 0, header_len
;
103 struct inetgram
*igp
, *ugp
= NULL
;
104 struct udphdr
*udphp
;
108 printf("udp_input(%d) ###############################\n", index
);
110 while ((igp
= sockets
[index
].inq
) != NULL
) {
111 if (igp
->igm_level
!= TRANSPORT_LVL
) {
113 printf("udp_input(%d): level %d datagram discarded.\n",
114 index
, igp
->igm_level
);
116 del_gram(&sockets
[index
].inq
, igp
, TRUE
);
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
);
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 "
132 inet_ntoa(igp
->igm_saddr
.sin_addr
));
133 del_gram(&sockets
[index
].inq
, igp
, TRUE
);
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
);
149 igp
->igm_level
= APP_LVL
;
150 del_gram(&sockets
[index
].inq
, igp
, FALSE
);
151 add_grams(&ugp
, igp
);
154 add_grams(&sockets
[index
].inq
, ugp
);
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).
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.
170 udp_output(int index
, struct inetgram
*ogp
)
172 struct udphdr
*udphp
;
176 printf("udp_output(%d): 0x%x, %d\n", index
, ogp
->igm_mp
,
177 ogp
->igm_mp
->b_wptr
- ogp
->igm_mp
->b_rptr
);
181 mp
->b_rptr
-= sizeof (struct udphdr
);
182 udphp
= (struct udphdr
*)(mp
->b_rptr
);
184 udphp
->uh_dport
= ogp
->igm_saddr
.sin_port
;
185 if (sockets
[index
].bound
)
186 udphp
->uh_sport
= sockets
[index
].bind
.sin_port
;
188 udphp
->uh_sport
= ogp
->igm_saddr
.sin_port
;
189 udphp
->uh_ulen
= htons(mp
->b_wptr
- mp
->b_rptr
);
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
);
197 ogp
->igm_level
= NETWORK_LVL
;