11 #include <gpxe/uaccess.h>
12 #include <gpxe/process.h>
16 * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of the
21 * License, or any later version.
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 /** A PXE UDP connection */
34 struct pxe_udp_connection {
35 /** Data transfer interface to UDP stack */
36 struct xfer_interface xfer;
38 struct sockaddr_in local;
39 /** Current PXENV_UDP_READ parameter block */
40 struct s_PXENV_UDP_READ *pxenv_udp_read;
44 * Receive PXE UDP data
46 * @v xfer Data transfer interface
48 * @v meta Data transfer metadata
49 * @ret rc Return status code
51 * Receives a packet as part of the current pxenv_udp_read()
54 static int pxe_udp_deliver_iob ( struct xfer_interface *xfer,
55 struct io_buffer *iobuf,
56 struct xfer_metadata *meta ) {
57 struct pxe_udp_connection *pxe_udp =
58 container_of ( xfer, struct pxe_udp_connection, xfer );
59 struct s_PXENV_UDP_READ *pxenv_udp_read = pxe_udp->pxenv_udp_read;
60 struct sockaddr_in *sin_src;
61 struct sockaddr_in *sin_dest;
66 if ( ! pxenv_udp_read ) {
67 DBG ( "PXE discarded UDP packet\n" );
72 /* Copy packet to buffer and record length */
73 buffer = real_to_user ( pxenv_udp_read->buffer.segment,
74 pxenv_udp_read->buffer.offset );
75 len = iob_len ( iobuf );
76 if ( len > pxenv_udp_read->buffer_size )
77 len = pxenv_udp_read->buffer_size;
78 copy_to_user ( buffer, 0, iobuf->data, len );
79 pxenv_udp_read->buffer_size = len;
81 /* Fill in source/dest information */
83 sin_src = ( struct sockaddr_in * ) meta->src;
85 assert ( sin_src->sin_family == AF_INET );
86 pxenv_udp_read->src_ip = sin_src->sin_addr.s_addr;
87 pxenv_udp_read->s_port = sin_src->sin_port;
88 sin_dest = ( struct sockaddr_in * ) meta->dest;
90 assert ( sin_dest->sin_family == AF_INET );
91 pxenv_udp_read->dest_ip = sin_dest->sin_addr.s_addr;
92 pxenv_udp_read->d_port = sin_dest->sin_port;
94 /* Mark as received */
95 pxe_udp->pxenv_udp_read = NULL;
102 /** PXE UDP data transfer interface operations */
103 static struct xfer_interface_operations pxe_udp_xfer_operations = {
104 .close = ignore_xfer_close,
105 .vredirect = ignore_xfer_vredirect,
106 .request = ignore_xfer_request,
107 .seek = ignore_xfer_seek,
108 .alloc_iob = default_xfer_alloc_iob,
109 .deliver_iob = pxe_udp_deliver_iob,
110 .deliver_raw = xfer_deliver_as_iob,
113 /** The PXE UDP connection */
114 static struct pxe_udp_connection pxe_udp = {
117 .dest = &null_xfer.intf,
120 .op = &pxe_udp_xfer_operations,
123 .sin_family = AF_INET,
130 * @v pxenv_udp_open Pointer to a struct s_PXENV_UDP_OPEN
131 * @v s_PXENV_UDP_OPEN::src_ip IP address of this station, or 0.0.0.0
132 * @ret #PXENV_EXIT_SUCCESS Always
133 * @ret s_PXENV_UDP_OPEN::Status PXE status code
134 * @err #PXENV_STATUS_UDP_OPEN UDP connection already open
135 * @err #PXENV_STATUS_OUT_OF_RESOURCES Could not open connection
137 * Prepares the PXE stack for communication using pxenv_udp_write()
138 * and pxenv_udp_read().
140 * The IP address supplied in s_PXENV_UDP_OPEN::src_ip will be
141 * recorded and used as the local station's IP address for all further
142 * communication, including communication by means other than
143 * pxenv_udp_write() and pxenv_udp_read(). (If
144 * s_PXENV_UDP_OPEN::src_ip is 0.0.0.0, the local station's IP address
145 * will remain unchanged.)
147 * You can only have one open UDP connection at a time. This is not a
148 * meaningful restriction, since pxenv_udp_write() and
149 * pxenv_udp_read() allow you to specify arbitrary local and remote
150 * ports and an arbitrary remote address for each packet. According
151 * to the PXE specifiation, you cannot have a UDP connection open at
152 * the same time as a TFTP connection; this restriction does not apply
155 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
156 * value before calling this function in protected mode. You cannot
157 * call this function with a 32-bit stack segment. (See the relevant
158 * @ref pxe_x86_pmode16 "implementation note" for more details.)
160 * @note The PXE specification does not make it clear whether the IP
161 * address supplied in s_PXENV_UDP_OPEN::src_ip should be used only
162 * for this UDP connection, or retained for all future communication.
163 * The latter seems more consistent with typical PXE stack behaviour.
165 * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
169 PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
172 DBG ( "PXENV_UDP_OPEN" );
174 /* Record source IP address */
175 pxe_udp.local.sin_addr.s_addr = pxenv_udp_open->src_ip;
177 /* Open promiscuous UDP connection */
178 xfer_close ( &pxe_udp.xfer, 0 );
179 if ( ( rc = udp_open_promisc ( &pxe_udp.xfer ) ) != 0 ) {
180 pxenv_udp_open->Status = PXENV_STATUS ( rc );
181 return PXENV_EXIT_FAILURE;
184 pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
185 return PXENV_EXIT_SUCCESS;
191 * @v pxenv_udp_close Pointer to a struct s_PXENV_UDP_CLOSE
192 * @ret #PXENV_EXIT_SUCCESS Always
193 * @ret s_PXENV_UDP_CLOSE::Status PXE status code
196 * Closes a UDP connection opened with pxenv_udp_open().
198 * You can only have one open UDP connection at a time. You cannot
199 * have a UDP connection open at the same time as a TFTP connection.
200 * You cannot use pxenv_udp_close() to close a TFTP connection; use
201 * pxenv_tftp_close() instead.
203 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
204 * value before calling this function in protected mode. You cannot
205 * call this function with a 32-bit stack segment. (See the relevant
206 * @ref pxe_x86_pmode16 "implementation note" for more details.)
209 PXENV_EXIT_t pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
210 DBG ( "PXENV_UDP_CLOSE" );
212 /* Close UDP connection */
213 xfer_close ( &pxe_udp.xfer, 0 );
215 pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
216 return PXENV_EXIT_SUCCESS;
222 * @v pxenv_udp_write Pointer to a struct s_PXENV_UDP_WRITE
223 * @v s_PXENV_UDP_WRITE::ip Destination IP address
224 * @v s_PXENV_UDP_WRITE::gw Relay agent IP address, or 0.0.0.0
225 * @v s_PXENV_UDP_WRITE::src_port Source UDP port, or 0
226 * @v s_PXENV_UDP_WRITE::dst_port Destination UDP port
227 * @v s_PXENV_UDP_WRITE::buffer_size Length of the UDP payload
228 * @v s_PXENV_UDP_WRITE::buffer Address of the UDP payload
229 * @ret #PXENV_EXIT_SUCCESS Packet was transmitted successfully
230 * @ret #PXENV_EXIT_FAILURE Packet could not be transmitted
231 * @ret s_PXENV_UDP_WRITE::Status PXE status code
232 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
233 * @err #PXENV_STATUS_UNDI_TRANSMIT_ERROR Could not transmit packet
235 * Transmits a single UDP packet. A valid IP and UDP header will be
236 * prepended to the payload in s_PXENV_UDP_WRITE::buffer; the buffer
237 * should not contain precomputed IP and UDP headers, nor should it
238 * contain space allocated for these headers. The first byte of the
239 * buffer will be transmitted as the first byte following the UDP
242 * If s_PXENV_UDP_WRITE::gw is 0.0.0.0, normal IP routing will take
243 * place. See the relevant @ref pxe_routing "implementation note" for
246 * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
248 * You must have opened a UDP connection with pxenv_udp_open() before
249 * calling pxenv_udp_write().
251 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
252 * value before calling this function in protected mode. You cannot
253 * call this function with a 32-bit stack segment. (See the relevant
254 * @ref pxe_x86_pmode16 "implementation note" for more details.)
256 * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
260 PXENV_EXIT_t pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
261 struct sockaddr_in dest;
262 struct xfer_metadata meta = {
263 .src = ( struct sockaddr * ) &pxe_udp.local,
264 .dest = ( struct sockaddr * ) &dest,
267 struct io_buffer *iobuf;
271 DBG ( "PXENV_UDP_WRITE" );
273 /* Construct destination socket address */
274 memset ( &dest, 0, sizeof ( dest ) );
275 dest.sin_family = AF_INET;
276 dest.sin_addr.s_addr = pxenv_udp_write->ip;
277 dest.sin_port = pxenv_udp_write->dst_port;
279 /* Set local (source) port. PXE spec says source port is 2069
280 * if not specified. Really, this ought to be set at UDP open
281 * time but hey, we didn't design this API.
283 pxe_udp.local.sin_port = pxenv_udp_write->src_port;
284 if ( ! pxe_udp.local.sin_port )
285 pxe_udp.local.sin_port = htons ( 2069 );
287 /* FIXME: we ignore the gateway specified, since we're
288 * confident of being able to do our own routing. We should
289 * probably allow for multiple gateways.
292 /* Allocate and fill data buffer */
293 len = pxenv_udp_write->buffer_size;
294 iobuf = xfer_alloc_iob ( &pxe_udp.xfer, len );
296 pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
297 return PXENV_EXIT_FAILURE;
299 buffer = real_to_user ( pxenv_udp_write->buffer.segment,
300 pxenv_udp_write->buffer.offset );
301 copy_from_user ( iob_put ( iobuf, len ), buffer, 0, len );
303 DBG ( " %04x:%04x+%x %d->%s:%d", pxenv_udp_write->buffer.segment,
304 pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
305 ntohs ( pxenv_udp_write->src_port ),
306 inet_ntoa ( dest.sin_addr ),
307 ntohs ( pxenv_udp_write->dst_port ) );
309 /* Transmit packet */
310 if ( ( rc = xfer_deliver_iob_meta ( &pxe_udp.xfer, iobuf,
312 pxenv_udp_write->Status = PXENV_STATUS ( rc );
313 return PXENV_EXIT_FAILURE;
316 pxenv_udp_write->Status = PXENV_STATUS_SUCCESS;
317 return PXENV_EXIT_SUCCESS;
323 * @v pxenv_udp_read Pointer to a struct s_PXENV_UDP_READ
324 * @v s_PXENV_UDP_READ::dest_ip Destination IP address, or 0.0.0.0
325 * @v s_PXENV_UDP_READ::d_port Destination UDP port, or 0
326 * @v s_PXENV_UDP_READ::buffer_size Size of the UDP payload buffer
327 * @v s_PXENV_UDP_READ::buffer Address of the UDP payload buffer
328 * @ret #PXENV_EXIT_SUCCESS A packet has been received
329 * @ret #PXENV_EXIT_FAILURE No packet has been received
330 * @ret s_PXENV_UDP_READ::Status PXE status code
331 * @ret s_PXENV_UDP_READ::src_ip Source IP address
332 * @ret s_PXENV_UDP_READ::dest_ip Destination IP address
333 * @ret s_PXENV_UDP_READ::s_port Source UDP port
334 * @ret s_PXENV_UDP_READ::d_port Destination UDP port
335 * @ret s_PXENV_UDP_READ::buffer_size Length of UDP payload
336 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
337 * @err #PXENV_STATUS_FAILURE No packet was ready to read
339 * Receive a single UDP packet. This is a non-blocking call; if no
340 * packet is ready to read, the call will return instantly with
341 * s_PXENV_UDP_READ::Status==PXENV_STATUS_FAILURE.
343 * If s_PXENV_UDP_READ::dest_ip is 0.0.0.0, UDP packets addressed to
344 * any IP address will be accepted and may be returned to the caller.
346 * If s_PXENV_UDP_READ::d_port is 0, UDP packets addressed to any UDP
347 * port will be accepted and may be returned to the caller.
349 * You must have opened a UDP connection with pxenv_udp_open() before
350 * calling pxenv_udp_read().
352 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
353 * value before calling this function in protected mode. You cannot
354 * call this function with a 32-bit stack segment. (See the relevant
355 * @ref pxe_x86_pmode16 "implementation note" for more details.)
357 * @note The PXE specification (version 2.1) does not state that we
358 * should fill in s_PXENV_UDP_READ::dest_ip and
359 * s_PXENV_UDP_READ::d_port, but Microsoft Windows' NTLDR program
360 * expects us to do so, and will fail if we don't.
363 PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
364 struct in_addr dest_ip = { .s_addr = pxenv_udp_read->dest_ip };
365 uint16_t d_port = pxenv_udp_read->d_port;
367 DBG ( "PXENV_UDP_READ" );
369 /* Try receiving a packet */
370 pxe_udp.pxenv_udp_read = pxenv_udp_read;
372 if ( pxe_udp.pxenv_udp_read ) {
373 /* No packet received */
374 pxe_udp.pxenv_udp_read = NULL;
378 /* Filter on destination address and/or port */
379 if ( dest_ip.s_addr && ( dest_ip.s_addr != pxenv_udp_read->dest_ip ) )
381 if ( d_port && ( d_port != pxenv_udp_read->d_port ) )
384 DBG ( " %04x:%04x+%x %s:", pxenv_udp_read->buffer.segment,
385 pxenv_udp_read->buffer.offset, pxenv_udp_read->buffer_size,
386 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->src_ip ) ));
387 DBG ( "%d<-%s:%d", ntohs ( pxenv_udp_read->s_port ),
388 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->dest_ip ) ),
389 ntohs ( pxenv_udp_read->d_port ) );
391 pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
392 return PXENV_EXIT_SUCCESS;
395 pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
396 return PXENV_EXIT_FAILURE;