9 #include <ipxe/iobuf.h>
10 #include <ipxe/xfer.h>
12 #include <ipxe/uaccess.h>
13 #include <ipxe/process.h>
18 * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation; either version 2 of the
23 * License, or any later version.
25 * This program is distributed in the hope that it will be useful, but
26 * WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 * You can also choose to distribute this program under the terms of
36 * the Unmodified Binary Distribution Licence (as given in the file
37 * COPYING.UBDL), provided that you have satisfied its requirements.
40 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
42 /** A PXE UDP pseudo-header */
43 struct pxe_udp_pseudo_header {
44 /** Source IP address */
48 /** Destination IP address */
50 /** Destination port */
52 } __attribute__ (( packed ));
54 /** A PXE UDP connection */
55 struct pxe_udp_connection {
56 /** Data transfer interface to UDP stack */
57 struct interface xfer;
59 struct sockaddr_in local;
60 /** List of received packets */
61 struct list_head list;
65 * Receive PXE UDP data
67 * @v pxe_udp PXE UDP connection
69 * @v meta Data transfer metadata
70 * @ret rc Return status code
72 * Receives a packet as part of the current pxenv_udp_read()
75 static int pxe_udp_deliver ( struct pxe_udp_connection *pxe_udp,
76 struct io_buffer *iobuf,
77 struct xfer_metadata *meta ) {
78 struct pxe_udp_pseudo_header *pshdr;
79 struct sockaddr_in *sin_src;
80 struct sockaddr_in *sin_dest;
83 /* Extract metadata */
85 sin_src = ( struct sockaddr_in * ) meta->src;
87 assert ( sin_src->sin_family == AF_INET );
88 sin_dest = ( struct sockaddr_in * ) meta->dest;
90 assert ( sin_dest->sin_family == AF_INET );
92 /* Construct pseudo-header */
93 if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *pshdr ) ) ) != 0 ) {
94 DBG ( "PXE could not prepend pseudo-header\n" );
98 pshdr = iob_push ( iobuf, sizeof ( *pshdr ) );
99 pshdr->src_ip = sin_src->sin_addr.s_addr;
100 pshdr->s_port = sin_src->sin_port;
101 pshdr->dest_ip = sin_dest->sin_addr.s_addr;
102 pshdr->d_port = sin_dest->sin_port;
105 list_add_tail ( &iobuf->list, &pxe_udp->list );
114 /** PXE UDP data transfer interface operations */
115 static struct interface_operation pxe_udp_xfer_operations[] = {
116 INTF_OP ( xfer_deliver, struct pxe_udp_connection *, pxe_udp_deliver ),
119 /** PXE UDP data transfer interface descriptor */
120 static struct interface_descriptor pxe_udp_xfer_desc =
121 INTF_DESC ( struct pxe_udp_connection, xfer, pxe_udp_xfer_operations );
123 /** The PXE UDP connection */
124 static struct pxe_udp_connection pxe_udp = {
125 .xfer = INTF_INIT ( pxe_udp_xfer_desc ),
127 .sin_family = AF_INET,
129 .list = LIST_HEAD_INIT ( pxe_udp.list ),
135 * @v pxenv_udp_open Pointer to a struct s_PXENV_UDP_OPEN
136 * @v s_PXENV_UDP_OPEN::src_ip IP address of this station, or 0.0.0.0
137 * @ret #PXENV_EXIT_SUCCESS Always
138 * @ret s_PXENV_UDP_OPEN::Status PXE status code
139 * @err #PXENV_STATUS_UDP_OPEN UDP connection already open
140 * @err #PXENV_STATUS_OUT_OF_RESOURCES Could not open connection
142 * Prepares the PXE stack for communication using pxenv_udp_write()
143 * and pxenv_udp_read().
145 * The IP address supplied in s_PXENV_UDP_OPEN::src_ip will be
146 * recorded and used as the local station's IP address for all further
147 * communication, including communication by means other than
148 * pxenv_udp_write() and pxenv_udp_read(). (If
149 * s_PXENV_UDP_OPEN::src_ip is 0.0.0.0, the local station's IP address
150 * will remain unchanged.)
152 * You can only have one open UDP connection at a time. This is not a
153 * meaningful restriction, since pxenv_udp_write() and
154 * pxenv_udp_read() allow you to specify arbitrary local and remote
155 * ports and an arbitrary remote address for each packet. According
156 * to the PXE specifiation, you cannot have a UDP connection open at
157 * the same time as a TFTP connection; this restriction does not apply
160 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
161 * value before calling this function in protected mode. You cannot
162 * call this function with a 32-bit stack segment. (See the relevant
163 * @ref pxe_x86_pmode16 "implementation note" for more details.)
165 * @note The PXE specification does not make it clear whether the IP
166 * address supplied in s_PXENV_UDP_OPEN::src_ip should be used only
167 * for this UDP connection, or retained for all future communication.
168 * The latter seems more consistent with typical PXE stack behaviour.
170 * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
174 static PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
177 DBG ( "PXENV_UDP_OPEN" );
179 /* Record source IP address */
180 pxe_udp.local.sin_addr.s_addr = pxenv_udp_open->src_ip;
181 DBG ( " %s\n", inet_ntoa ( pxe_udp.local.sin_addr ) );
183 /* Open promiscuous UDP connection */
184 intf_restart ( &pxe_udp.xfer, 0 );
185 if ( ( rc = udp_open_promisc ( &pxe_udp.xfer ) ) != 0 ) {
186 DBG ( "PXENV_UDP_OPEN could not open promiscuous socket: %s\n",
188 pxenv_udp_open->Status = PXENV_STATUS ( rc );
189 return PXENV_EXIT_FAILURE;
192 pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
193 return PXENV_EXIT_SUCCESS;
199 * @v pxenv_udp_close Pointer to a struct s_PXENV_UDP_CLOSE
200 * @ret #PXENV_EXIT_SUCCESS Always
201 * @ret s_PXENV_UDP_CLOSE::Status PXE status code
204 * Closes a UDP connection opened with pxenv_udp_open().
206 * You can only have one open UDP connection at a time. You cannot
207 * have a UDP connection open at the same time as a TFTP connection.
208 * You cannot use pxenv_udp_close() to close a TFTP connection; use
209 * pxenv_tftp_close() instead.
211 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
212 * value before calling this function in protected mode. You cannot
213 * call this function with a 32-bit stack segment. (See the relevant
214 * @ref pxe_x86_pmode16 "implementation note" for more details.)
218 pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
219 struct io_buffer *iobuf;
220 struct io_buffer *tmp;
222 DBG ( "PXENV_UDP_CLOSE\n" );
224 /* Close UDP connection */
225 intf_restart ( &pxe_udp.xfer, 0 );
227 /* Discard any received packets */
228 list_for_each_entry_safe ( iobuf, tmp, &pxe_udp.list, list ) {
229 list_del ( &iobuf->list );
233 pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
234 return PXENV_EXIT_SUCCESS;
240 * @v pxenv_udp_write Pointer to a struct s_PXENV_UDP_WRITE
241 * @v s_PXENV_UDP_WRITE::ip Destination IP address
242 * @v s_PXENV_UDP_WRITE::gw Relay agent IP address, or 0.0.0.0
243 * @v s_PXENV_UDP_WRITE::src_port Source UDP port, or 0
244 * @v s_PXENV_UDP_WRITE::dst_port Destination UDP port
245 * @v s_PXENV_UDP_WRITE::buffer_size Length of the UDP payload
246 * @v s_PXENV_UDP_WRITE::buffer Address of the UDP payload
247 * @ret #PXENV_EXIT_SUCCESS Packet was transmitted successfully
248 * @ret #PXENV_EXIT_FAILURE Packet could not be transmitted
249 * @ret s_PXENV_UDP_WRITE::Status PXE status code
250 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
251 * @err #PXENV_STATUS_UNDI_TRANSMIT_ERROR Could not transmit packet
253 * Transmits a single UDP packet. A valid IP and UDP header will be
254 * prepended to the payload in s_PXENV_UDP_WRITE::buffer; the buffer
255 * should not contain precomputed IP and UDP headers, nor should it
256 * contain space allocated for these headers. The first byte of the
257 * buffer will be transmitted as the first byte following the UDP
260 * If s_PXENV_UDP_WRITE::gw is 0.0.0.0, normal IP routing will take
261 * place. See the relevant @ref pxe_routing "implementation note" for
264 * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
266 * You must have opened a UDP connection with pxenv_udp_open() before
267 * calling pxenv_udp_write().
269 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
270 * value before calling this function in protected mode. You cannot
271 * call this function with a 32-bit stack segment. (See the relevant
272 * @ref pxe_x86_pmode16 "implementation note" for more details.)
274 * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
279 pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
280 struct sockaddr_in dest;
281 struct xfer_metadata meta = {
282 .src = ( struct sockaddr * ) &pxe_udp.local,
283 .dest = ( struct sockaddr * ) &dest,
284 .netdev = pxe_netdev,
287 struct io_buffer *iobuf;
291 DBG ( "PXENV_UDP_WRITE" );
293 /* Construct destination socket address */
294 memset ( &dest, 0, sizeof ( dest ) );
295 dest.sin_family = AF_INET;
296 dest.sin_addr.s_addr = pxenv_udp_write->ip;
297 dest.sin_port = pxenv_udp_write->dst_port;
299 /* Set local (source) port. PXE spec says source port is 2069
300 * if not specified. Really, this ought to be set at UDP open
301 * time but hey, we didn't design this API.
303 pxe_udp.local.sin_port = pxenv_udp_write->src_port;
304 if ( ! pxe_udp.local.sin_port )
305 pxe_udp.local.sin_port = htons ( 2069 );
307 /* FIXME: we ignore the gateway specified, since we're
308 * confident of being able to do our own routing. We should
309 * probably allow for multiple gateways.
312 /* Allocate and fill data buffer */
313 len = pxenv_udp_write->buffer_size;
314 iobuf = xfer_alloc_iob ( &pxe_udp.xfer, len );
316 DBG ( " out of memory\n" );
317 pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
318 return PXENV_EXIT_FAILURE;
320 buffer = real_to_user ( pxenv_udp_write->buffer.segment,
321 pxenv_udp_write->buffer.offset );
322 copy_from_user ( iob_put ( iobuf, len ), buffer, 0, len );
324 DBG ( " %04x:%04x+%x %d->%s:%d\n", pxenv_udp_write->buffer.segment,
325 pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
326 ntohs ( pxenv_udp_write->src_port ),
327 inet_ntoa ( dest.sin_addr ),
328 ntohs ( pxenv_udp_write->dst_port ) );
330 /* Transmit packet */
331 if ( ( rc = xfer_deliver ( &pxe_udp.xfer, iobuf, &meta ) ) != 0 ) {
332 DBG ( "PXENV_UDP_WRITE could not transmit: %s\n",
334 pxenv_udp_write->Status = PXENV_STATUS ( rc );
335 return PXENV_EXIT_FAILURE;
338 pxenv_udp_write->Status = PXENV_STATUS_SUCCESS;
339 return PXENV_EXIT_SUCCESS;
345 * @v pxenv_udp_read Pointer to a struct s_PXENV_UDP_READ
346 * @v s_PXENV_UDP_READ::dest_ip Destination IP address, or 0.0.0.0
347 * @v s_PXENV_UDP_READ::d_port Destination UDP port, or 0
348 * @v s_PXENV_UDP_READ::buffer_size Size of the UDP payload buffer
349 * @v s_PXENV_UDP_READ::buffer Address of the UDP payload buffer
350 * @ret #PXENV_EXIT_SUCCESS A packet has been received
351 * @ret #PXENV_EXIT_FAILURE No packet has been received
352 * @ret s_PXENV_UDP_READ::Status PXE status code
353 * @ret s_PXENV_UDP_READ::src_ip Source IP address
354 * @ret s_PXENV_UDP_READ::dest_ip Destination IP address
355 * @ret s_PXENV_UDP_READ::s_port Source UDP port
356 * @ret s_PXENV_UDP_READ::d_port Destination UDP port
357 * @ret s_PXENV_UDP_READ::buffer_size Length of UDP payload
358 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
359 * @err #PXENV_STATUS_FAILURE No packet was ready to read
361 * Receive a single UDP packet. This is a non-blocking call; if no
362 * packet is ready to read, the call will return instantly with
363 * s_PXENV_UDP_READ::Status==PXENV_STATUS_FAILURE.
365 * If s_PXENV_UDP_READ::dest_ip is 0.0.0.0, UDP packets addressed to
366 * any IP address will be accepted and may be returned to the caller.
368 * If s_PXENV_UDP_READ::d_port is 0, UDP packets addressed to any UDP
369 * port will be accepted and may be returned to the caller.
371 * You must have opened a UDP connection with pxenv_udp_open() before
372 * calling pxenv_udp_read().
374 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
375 * value before calling this function in protected mode. You cannot
376 * call this function with a 32-bit stack segment. (See the relevant
377 * @ref pxe_x86_pmode16 "implementation note" for more details.)
379 * @note The PXE specification (version 2.1) does not state that we
380 * should fill in s_PXENV_UDP_READ::dest_ip and
381 * s_PXENV_UDP_READ::d_port, but Microsoft Windows' NTLDR program
382 * expects us to do so, and will fail if we don't.
385 static PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
386 struct in_addr dest_ip_wanted = { .s_addr = pxenv_udp_read->dest_ip };
387 struct in_addr dest_ip;
388 struct io_buffer *iobuf;
389 struct pxe_udp_pseudo_header *pshdr;
390 uint16_t d_port_wanted = pxenv_udp_read->d_port;
395 /* Try receiving a packet, if the queue is empty */
396 if ( list_empty ( &pxe_udp.list ) )
399 /* Remove first packet from the queue */
400 iobuf = list_first_entry ( &pxe_udp.list, struct io_buffer, list );
402 /* No packet received */
403 DBG2 ( "PXENV_UDP_READ\n" );
406 list_del ( &iobuf->list );
408 /* Strip pseudo-header */
409 assert ( iob_len ( iobuf ) >= sizeof ( *pshdr ) );
411 iob_pull ( iobuf, sizeof ( *pshdr ) );
412 dest_ip.s_addr = pshdr->dest_ip;
413 d_port = pshdr->d_port;
414 DBG ( "PXENV_UDP_READ" );
416 /* Filter on destination address and/or port */
417 if ( dest_ip_wanted.s_addr &&
418 ( dest_ip_wanted.s_addr != dest_ip.s_addr ) ) {
419 DBG ( " wrong IP %s", inet_ntoa ( dest_ip ) );
420 DBG ( " (wanted %s)\n", inet_ntoa ( dest_ip_wanted ) );
423 if ( d_port_wanted && ( d_port_wanted != d_port ) ) {
424 DBG ( " wrong port %d", htons ( d_port ) );
425 DBG ( " (wanted %d)\n", htons ( d_port_wanted ) );
429 /* Copy packet to buffer and record length */
430 buffer = real_to_user ( pxenv_udp_read->buffer.segment,
431 pxenv_udp_read->buffer.offset );
432 len = iob_len ( iobuf );
433 if ( len > pxenv_udp_read->buffer_size )
434 len = pxenv_udp_read->buffer_size;
435 copy_to_user ( buffer, 0, iobuf->data, len );
436 pxenv_udp_read->buffer_size = len;
438 /* Fill in source/dest information */
439 pxenv_udp_read->src_ip = pshdr->src_ip;
440 pxenv_udp_read->s_port = pshdr->s_port;
441 pxenv_udp_read->dest_ip = pshdr->dest_ip;
442 pxenv_udp_read->d_port = pshdr->d_port;
444 DBG ( " %04x:%04x+%x %s:", pxenv_udp_read->buffer.segment,
445 pxenv_udp_read->buffer.offset, pxenv_udp_read->buffer_size,
446 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->src_ip ) ));
447 DBG ( "%d<-%s:%d\n", ntohs ( pxenv_udp_read->s_port ),
448 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->dest_ip ) ),
449 ntohs ( pxenv_udp_read->d_port ) );
451 /* Free I/O buffer */
454 pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
455 return PXENV_EXIT_SUCCESS;
460 pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
461 return PXENV_EXIT_FAILURE;
465 struct pxe_api_call pxe_udp_api[] __pxe_api_call = {
466 PXE_API_CALL ( PXENV_UDP_OPEN, pxenv_udp_open,
467 struct s_PXENV_UDP_OPEN ),
468 PXE_API_CALL ( PXENV_UDP_CLOSE, pxenv_udp_close,
469 struct s_PXENV_UDP_CLOSE ),
470 PXE_API_CALL ( PXENV_UDP_WRITE, pxenv_udp_write,
471 struct s_PXENV_UDP_WRITE ),
472 PXE_API_CALL ( PXENV_UDP_READ, pxenv_udp_read,
473 struct s_PXENV_UDP_READ ),