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 .window = unlimited_xfer_window,
107 .alloc_iob = default_xfer_alloc_iob,
108 .deliver_iob = pxe_udp_deliver_iob,
109 .deliver_raw = xfer_deliver_as_iob,
112 /** The PXE UDP connection */
113 static struct pxe_udp_connection pxe_udp = {
116 .dest = &null_xfer.intf,
119 .op = &pxe_udp_xfer_operations,
122 .sin_family = AF_INET,
129 * @v pxenv_udp_open Pointer to a struct s_PXENV_UDP_OPEN
130 * @v s_PXENV_UDP_OPEN::src_ip IP address of this station, or 0.0.0.0
131 * @ret #PXENV_EXIT_SUCCESS Always
132 * @ret s_PXENV_UDP_OPEN::Status PXE status code
133 * @err #PXENV_STATUS_UDP_OPEN UDP connection already open
134 * @err #PXENV_STATUS_OUT_OF_RESOURCES Could not open connection
136 * Prepares the PXE stack for communication using pxenv_udp_write()
137 * and pxenv_udp_read().
139 * The IP address supplied in s_PXENV_UDP_OPEN::src_ip will be
140 * recorded and used as the local station's IP address for all further
141 * communication, including communication by means other than
142 * pxenv_udp_write() and pxenv_udp_read(). (If
143 * s_PXENV_UDP_OPEN::src_ip is 0.0.0.0, the local station's IP address
144 * will remain unchanged.)
146 * You can only have one open UDP connection at a time. This is not a
147 * meaningful restriction, since pxenv_udp_write() and
148 * pxenv_udp_read() allow you to specify arbitrary local and remote
149 * ports and an arbitrary remote address for each packet. According
150 * to the PXE specifiation, you cannot have a UDP connection open at
151 * the same time as a TFTP connection; this restriction does not apply
154 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
155 * value before calling this function in protected mode. You cannot
156 * call this function with a 32-bit stack segment. (See the relevant
157 * @ref pxe_x86_pmode16 "implementation note" for more details.)
159 * @note The PXE specification does not make it clear whether the IP
160 * address supplied in s_PXENV_UDP_OPEN::src_ip should be used only
161 * for this UDP connection, or retained for all future communication.
162 * The latter seems more consistent with typical PXE stack behaviour.
164 * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
168 PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
171 DBG ( "PXENV_UDP_OPEN" );
173 /* Record source IP address */
174 pxe_udp.local.sin_addr.s_addr = pxenv_udp_open->src_ip;
176 /* Open promiscuous UDP connection */
177 xfer_close ( &pxe_udp.xfer, 0 );
178 if ( ( rc = udp_open_promisc ( &pxe_udp.xfer ) ) != 0 ) {
179 pxenv_udp_open->Status = PXENV_STATUS ( rc );
180 return PXENV_EXIT_FAILURE;
183 pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
184 return PXENV_EXIT_SUCCESS;
190 * @v pxenv_udp_close Pointer to a struct s_PXENV_UDP_CLOSE
191 * @ret #PXENV_EXIT_SUCCESS Always
192 * @ret s_PXENV_UDP_CLOSE::Status PXE status code
195 * Closes a UDP connection opened with pxenv_udp_open().
197 * You can only have one open UDP connection at a time. You cannot
198 * have a UDP connection open at the same time as a TFTP connection.
199 * You cannot use pxenv_udp_close() to close a TFTP connection; use
200 * pxenv_tftp_close() instead.
202 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
203 * value before calling this function in protected mode. You cannot
204 * call this function with a 32-bit stack segment. (See the relevant
205 * @ref pxe_x86_pmode16 "implementation note" for more details.)
208 PXENV_EXIT_t pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
209 DBG ( "PXENV_UDP_CLOSE" );
211 /* Close UDP connection */
212 xfer_close ( &pxe_udp.xfer, 0 );
214 pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
215 return PXENV_EXIT_SUCCESS;
221 * @v pxenv_udp_write Pointer to a struct s_PXENV_UDP_WRITE
222 * @v s_PXENV_UDP_WRITE::ip Destination IP address
223 * @v s_PXENV_UDP_WRITE::gw Relay agent IP address, or 0.0.0.0
224 * @v s_PXENV_UDP_WRITE::src_port Source UDP port, or 0
225 * @v s_PXENV_UDP_WRITE::dst_port Destination UDP port
226 * @v s_PXENV_UDP_WRITE::buffer_size Length of the UDP payload
227 * @v s_PXENV_UDP_WRITE::buffer Address of the UDP payload
228 * @ret #PXENV_EXIT_SUCCESS Packet was transmitted successfully
229 * @ret #PXENV_EXIT_FAILURE Packet could not be transmitted
230 * @ret s_PXENV_UDP_WRITE::Status PXE status code
231 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
232 * @err #PXENV_STATUS_UNDI_TRANSMIT_ERROR Could not transmit packet
234 * Transmits a single UDP packet. A valid IP and UDP header will be
235 * prepended to the payload in s_PXENV_UDP_WRITE::buffer; the buffer
236 * should not contain precomputed IP and UDP headers, nor should it
237 * contain space allocated for these headers. The first byte of the
238 * buffer will be transmitted as the first byte following the UDP
241 * If s_PXENV_UDP_WRITE::gw is 0.0.0.0, normal IP routing will take
242 * place. See the relevant @ref pxe_routing "implementation note" for
245 * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
247 * You must have opened a UDP connection with pxenv_udp_open() before
248 * calling pxenv_udp_write().
250 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
251 * value before calling this function in protected mode. You cannot
252 * call this function with a 32-bit stack segment. (See the relevant
253 * @ref pxe_x86_pmode16 "implementation note" for more details.)
255 * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
259 PXENV_EXIT_t pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
260 struct sockaddr_in dest;
261 struct xfer_metadata meta = {
262 .src = ( struct sockaddr * ) &pxe_udp.local,
263 .dest = ( struct sockaddr * ) &dest,
266 struct io_buffer *iobuf;
270 DBG ( "PXENV_UDP_WRITE" );
272 /* Construct destination socket address */
273 memset ( &dest, 0, sizeof ( dest ) );
274 dest.sin_family = AF_INET;
275 dest.sin_addr.s_addr = pxenv_udp_write->ip;
276 dest.sin_port = pxenv_udp_write->dst_port;
278 /* Set local (source) port. PXE spec says source port is 2069
279 * if not specified. Really, this ought to be set at UDP open
280 * time but hey, we didn't design this API.
282 pxe_udp.local.sin_port = pxenv_udp_write->src_port;
283 if ( ! pxe_udp.local.sin_port )
284 pxe_udp.local.sin_port = htons ( 2069 );
286 /* FIXME: we ignore the gateway specified, since we're
287 * confident of being able to do our own routing. We should
288 * probably allow for multiple gateways.
291 /* Allocate and fill data buffer */
292 len = pxenv_udp_write->buffer_size;
293 iobuf = xfer_alloc_iob ( &pxe_udp.xfer, len );
295 pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
296 return PXENV_EXIT_FAILURE;
298 buffer = real_to_user ( pxenv_udp_write->buffer.segment,
299 pxenv_udp_write->buffer.offset );
300 copy_from_user ( iob_put ( iobuf, len ), buffer, 0, len );
302 DBG ( " %04x:%04x+%x %d->%s:%d", pxenv_udp_write->buffer.segment,
303 pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
304 ntohs ( pxenv_udp_write->src_port ),
305 inet_ntoa ( dest.sin_addr ),
306 ntohs ( pxenv_udp_write->dst_port ) );
308 /* Transmit packet */
309 if ( ( rc = xfer_deliver_iob_meta ( &pxe_udp.xfer, iobuf,
311 pxenv_udp_write->Status = PXENV_STATUS ( rc );
312 return PXENV_EXIT_FAILURE;
315 pxenv_udp_write->Status = PXENV_STATUS_SUCCESS;
316 return PXENV_EXIT_SUCCESS;
322 * @v pxenv_udp_read Pointer to a struct s_PXENV_UDP_READ
323 * @v s_PXENV_UDP_READ::dest_ip Destination IP address, or 0.0.0.0
324 * @v s_PXENV_UDP_READ::d_port Destination UDP port, or 0
325 * @v s_PXENV_UDP_READ::buffer_size Size of the UDP payload buffer
326 * @v s_PXENV_UDP_READ::buffer Address of the UDP payload buffer
327 * @ret #PXENV_EXIT_SUCCESS A packet has been received
328 * @ret #PXENV_EXIT_FAILURE No packet has been received
329 * @ret s_PXENV_UDP_READ::Status PXE status code
330 * @ret s_PXENV_UDP_READ::src_ip Source IP address
331 * @ret s_PXENV_UDP_READ::dest_ip Destination IP address
332 * @ret s_PXENV_UDP_READ::s_port Source UDP port
333 * @ret s_PXENV_UDP_READ::d_port Destination UDP port
334 * @ret s_PXENV_UDP_READ::buffer_size Length of UDP payload
335 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
336 * @err #PXENV_STATUS_FAILURE No packet was ready to read
338 * Receive a single UDP packet. This is a non-blocking call; if no
339 * packet is ready to read, the call will return instantly with
340 * s_PXENV_UDP_READ::Status==PXENV_STATUS_FAILURE.
342 * If s_PXENV_UDP_READ::dest_ip is 0.0.0.0, UDP packets addressed to
343 * any IP address will be accepted and may be returned to the caller.
345 * If s_PXENV_UDP_READ::d_port is 0, UDP packets addressed to any UDP
346 * port will be accepted and may be returned to the caller.
348 * You must have opened a UDP connection with pxenv_udp_open() before
349 * calling pxenv_udp_read().
351 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
352 * value before calling this function in protected mode. You cannot
353 * call this function with a 32-bit stack segment. (See the relevant
354 * @ref pxe_x86_pmode16 "implementation note" for more details.)
356 * @note The PXE specification (version 2.1) does not state that we
357 * should fill in s_PXENV_UDP_READ::dest_ip and
358 * s_PXENV_UDP_READ::d_port, but Microsoft Windows' NTLDR program
359 * expects us to do so, and will fail if we don't.
362 PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
363 struct in_addr dest_ip = { .s_addr = pxenv_udp_read->dest_ip };
364 uint16_t d_port = pxenv_udp_read->d_port;
366 DBG ( "PXENV_UDP_READ" );
368 /* Try receiving a packet */
369 pxe_udp.pxenv_udp_read = pxenv_udp_read;
371 if ( pxe_udp.pxenv_udp_read ) {
372 /* No packet received */
373 pxe_udp.pxenv_udp_read = NULL;
377 /* Filter on destination address and/or port */
378 if ( dest_ip.s_addr && ( dest_ip.s_addr != pxenv_udp_read->dest_ip ) )
380 if ( d_port && ( d_port != pxenv_udp_read->d_port ) )
383 DBG ( " %04x:%04x+%x %s:", pxenv_udp_read->buffer.segment,
384 pxenv_udp_read->buffer.offset, pxenv_udp_read->buffer_size,
385 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->src_ip ) ));
386 DBG ( "%d<-%s:%d", ntohs ( pxenv_udp_read->s_port ),
387 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->dest_ip ) ),
388 ntohs ( pxenv_udp_read->d_port ) );
390 pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
391 return PXENV_EXIT_SUCCESS;
394 pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
395 return PXENV_EXIT_FAILURE;