9 #include <ipxe/iobuf.h>
10 #include <ipxe/xfer.h>
12 #include <ipxe/uaccess.h>
13 #include <ipxe/process.h>
17 * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation; either version 2 of the
22 * License, or any later version.
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 FILE_LICENCE ( GPL2_OR_LATER );
37 /** A PXE UDP connection */
38 struct pxe_udp_connection {
39 /** Data transfer interface to UDP stack */
40 struct interface xfer;
42 struct sockaddr_in local;
43 /** Current PXENV_UDP_READ parameter block */
44 struct s_PXENV_UDP_READ *pxenv_udp_read;
48 * Receive PXE UDP data
50 * @v pxe_udp PXE UDP connection
52 * @v meta Data transfer metadata
53 * @ret rc Return status code
55 * Receives a packet as part of the current pxenv_udp_read()
58 static int pxe_udp_deliver ( struct pxe_udp_connection *pxe_udp,
59 struct io_buffer *iobuf,
60 struct xfer_metadata *meta ) {
61 struct s_PXENV_UDP_READ *pxenv_udp_read = pxe_udp->pxenv_udp_read;
62 struct sockaddr_in *sin_src;
63 struct sockaddr_in *sin_dest;
68 if ( ! pxenv_udp_read ) {
69 DBG ( "PXE discarded UDP packet\n" );
74 /* Copy packet to buffer and record length */
75 buffer = real_to_user ( pxenv_udp_read->buffer.segment,
76 pxenv_udp_read->buffer.offset );
77 len = iob_len ( iobuf );
78 if ( len > pxenv_udp_read->buffer_size )
79 len = pxenv_udp_read->buffer_size;
80 copy_to_user ( buffer, 0, iobuf->data, len );
81 pxenv_udp_read->buffer_size = len;
83 /* Fill in source/dest information */
85 sin_src = ( struct sockaddr_in * ) meta->src;
87 assert ( sin_src->sin_family == AF_INET );
88 pxenv_udp_read->src_ip = sin_src->sin_addr.s_addr;
89 pxenv_udp_read->s_port = sin_src->sin_port;
90 sin_dest = ( struct sockaddr_in * ) meta->dest;
92 assert ( sin_dest->sin_family == AF_INET );
93 pxenv_udp_read->dest_ip = sin_dest->sin_addr.s_addr;
94 pxenv_udp_read->d_port = sin_dest->sin_port;
96 /* Mark as received */
97 pxe_udp->pxenv_udp_read = NULL;
104 /** PXE UDP data transfer interface operations */
105 static struct interface_operation pxe_udp_xfer_operations[] = {
106 INTF_OP ( xfer_deliver, struct pxe_udp_connection *, pxe_udp_deliver ),
109 /** PXE UDP data transfer interface descriptor */
110 static struct interface_descriptor pxe_udp_xfer_desc =
111 INTF_DESC ( struct pxe_udp_connection, xfer, pxe_udp_xfer_operations );
113 /** The PXE UDP connection */
114 static struct pxe_udp_connection pxe_udp = {
115 .xfer = INTF_INIT ( pxe_udp_xfer_desc ),
117 .sin_family = AF_INET,
124 * @v pxenv_udp_open Pointer to a struct s_PXENV_UDP_OPEN
125 * @v s_PXENV_UDP_OPEN::src_ip IP address of this station, or 0.0.0.0
126 * @ret #PXENV_EXIT_SUCCESS Always
127 * @ret s_PXENV_UDP_OPEN::Status PXE status code
128 * @err #PXENV_STATUS_UDP_OPEN UDP connection already open
129 * @err #PXENV_STATUS_OUT_OF_RESOURCES Could not open connection
131 * Prepares the PXE stack for communication using pxenv_udp_write()
132 * and pxenv_udp_read().
134 * The IP address supplied in s_PXENV_UDP_OPEN::src_ip will be
135 * recorded and used as the local station's IP address for all further
136 * communication, including communication by means other than
137 * pxenv_udp_write() and pxenv_udp_read(). (If
138 * s_PXENV_UDP_OPEN::src_ip is 0.0.0.0, the local station's IP address
139 * will remain unchanged.)
141 * You can only have one open UDP connection at a time. This is not a
142 * meaningful restriction, since pxenv_udp_write() and
143 * pxenv_udp_read() allow you to specify arbitrary local and remote
144 * ports and an arbitrary remote address for each packet. According
145 * to the PXE specifiation, you cannot have a UDP connection open at
146 * the same time as a TFTP connection; this restriction does not apply
149 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
150 * value before calling this function in protected mode. You cannot
151 * call this function with a 32-bit stack segment. (See the relevant
152 * @ref pxe_x86_pmode16 "implementation note" for more details.)
154 * @note The PXE specification does not make it clear whether the IP
155 * address supplied in s_PXENV_UDP_OPEN::src_ip should be used only
156 * for this UDP connection, or retained for all future communication.
157 * The latter seems more consistent with typical PXE stack behaviour.
159 * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
163 static PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
166 DBG ( "PXENV_UDP_OPEN" );
168 /* Record source IP address */
169 pxe_udp.local.sin_addr.s_addr = pxenv_udp_open->src_ip;
170 DBG ( " %s\n", inet_ntoa ( pxe_udp.local.sin_addr ) );
172 /* Open promiscuous UDP connection */
173 intf_restart ( &pxe_udp.xfer, 0 );
174 if ( ( rc = udp_open_promisc ( &pxe_udp.xfer ) ) != 0 ) {
175 DBG ( "PXENV_UDP_OPEN could not open promiscuous socket: %s\n",
177 pxenv_udp_open->Status = PXENV_STATUS ( rc );
178 return PXENV_EXIT_FAILURE;
181 pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
182 return PXENV_EXIT_SUCCESS;
188 * @v pxenv_udp_close Pointer to a struct s_PXENV_UDP_CLOSE
189 * @ret #PXENV_EXIT_SUCCESS Always
190 * @ret s_PXENV_UDP_CLOSE::Status PXE status code
193 * Closes a UDP connection opened with pxenv_udp_open().
195 * You can only have one open UDP connection at a time. You cannot
196 * have a UDP connection open at the same time as a TFTP connection.
197 * You cannot use pxenv_udp_close() to close a TFTP connection; use
198 * pxenv_tftp_close() instead.
200 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
201 * value before calling this function in protected mode. You cannot
202 * call this function with a 32-bit stack segment. (See the relevant
203 * @ref pxe_x86_pmode16 "implementation note" for more details.)
207 pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
208 DBG ( "PXENV_UDP_CLOSE\n" );
210 /* Close UDP connection */
211 intf_restart ( &pxe_udp.xfer, 0 );
213 pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
214 return PXENV_EXIT_SUCCESS;
220 * @v pxenv_udp_write Pointer to a struct s_PXENV_UDP_WRITE
221 * @v s_PXENV_UDP_WRITE::ip Destination IP address
222 * @v s_PXENV_UDP_WRITE::gw Relay agent IP address, or 0.0.0.0
223 * @v s_PXENV_UDP_WRITE::src_port Source UDP port, or 0
224 * @v s_PXENV_UDP_WRITE::dst_port Destination UDP port
225 * @v s_PXENV_UDP_WRITE::buffer_size Length of the UDP payload
226 * @v s_PXENV_UDP_WRITE::buffer Address of the UDP payload
227 * @ret #PXENV_EXIT_SUCCESS Packet was transmitted successfully
228 * @ret #PXENV_EXIT_FAILURE Packet could not be transmitted
229 * @ret s_PXENV_UDP_WRITE::Status PXE status code
230 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
231 * @err #PXENV_STATUS_UNDI_TRANSMIT_ERROR Could not transmit packet
233 * Transmits a single UDP packet. A valid IP and UDP header will be
234 * prepended to the payload in s_PXENV_UDP_WRITE::buffer; the buffer
235 * should not contain precomputed IP and UDP headers, nor should it
236 * contain space allocated for these headers. The first byte of the
237 * buffer will be transmitted as the first byte following the UDP
240 * If s_PXENV_UDP_WRITE::gw is 0.0.0.0, normal IP routing will take
241 * place. See the relevant @ref pxe_routing "implementation note" for
244 * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
246 * You must have opened a UDP connection with pxenv_udp_open() before
247 * calling pxenv_udp_write().
249 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
250 * value before calling this function in protected mode. You cannot
251 * call this function with a 32-bit stack segment. (See the relevant
252 * @ref pxe_x86_pmode16 "implementation note" for more details.)
254 * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
259 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,
264 .netdev = pxe_netdev,
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 DBG ( " out of memory\n" );
297 pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
298 return PXENV_EXIT_FAILURE;
300 buffer = real_to_user ( pxenv_udp_write->buffer.segment,
301 pxenv_udp_write->buffer.offset );
302 copy_from_user ( iob_put ( iobuf, len ), buffer, 0, len );
304 DBG ( " %04x:%04x+%x %d->%s:%d\n", pxenv_udp_write->buffer.segment,
305 pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
306 ntohs ( pxenv_udp_write->src_port ),
307 inet_ntoa ( dest.sin_addr ),
308 ntohs ( pxenv_udp_write->dst_port ) );
310 /* Transmit packet */
311 if ( ( rc = xfer_deliver ( &pxe_udp.xfer, iobuf, &meta ) ) != 0 ) {
312 DBG ( "PXENV_UDP_WRITE could not transmit: %s\n",
314 pxenv_udp_write->Status = PXENV_STATUS ( rc );
315 return PXENV_EXIT_FAILURE;
318 pxenv_udp_write->Status = PXENV_STATUS_SUCCESS;
319 return PXENV_EXIT_SUCCESS;
325 * @v pxenv_udp_read Pointer to a struct s_PXENV_UDP_READ
326 * @v s_PXENV_UDP_READ::dest_ip Destination IP address, or 0.0.0.0
327 * @v s_PXENV_UDP_READ::d_port Destination UDP port, or 0
328 * @v s_PXENV_UDP_READ::buffer_size Size of the UDP payload buffer
329 * @v s_PXENV_UDP_READ::buffer Address of the UDP payload buffer
330 * @ret #PXENV_EXIT_SUCCESS A packet has been received
331 * @ret #PXENV_EXIT_FAILURE No packet has been received
332 * @ret s_PXENV_UDP_READ::Status PXE status code
333 * @ret s_PXENV_UDP_READ::src_ip Source IP address
334 * @ret s_PXENV_UDP_READ::dest_ip Destination IP address
335 * @ret s_PXENV_UDP_READ::s_port Source UDP port
336 * @ret s_PXENV_UDP_READ::d_port Destination UDP port
337 * @ret s_PXENV_UDP_READ::buffer_size Length of UDP payload
338 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
339 * @err #PXENV_STATUS_FAILURE No packet was ready to read
341 * Receive a single UDP packet. This is a non-blocking call; if no
342 * packet is ready to read, the call will return instantly with
343 * s_PXENV_UDP_READ::Status==PXENV_STATUS_FAILURE.
345 * If s_PXENV_UDP_READ::dest_ip is 0.0.0.0, UDP packets addressed to
346 * any IP address will be accepted and may be returned to the caller.
348 * If s_PXENV_UDP_READ::d_port is 0, UDP packets addressed to any UDP
349 * port will be accepted and may be returned to the caller.
351 * You must have opened a UDP connection with pxenv_udp_open() before
352 * calling pxenv_udp_read().
354 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
355 * value before calling this function in protected mode. You cannot
356 * call this function with a 32-bit stack segment. (See the relevant
357 * @ref pxe_x86_pmode16 "implementation note" for more details.)
359 * @note The PXE specification (version 2.1) does not state that we
360 * should fill in s_PXENV_UDP_READ::dest_ip and
361 * s_PXENV_UDP_READ::d_port, but Microsoft Windows' NTLDR program
362 * expects us to do so, and will fail if we don't.
365 static PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
366 struct in_addr dest_ip_wanted = { .s_addr = pxenv_udp_read->dest_ip };
367 struct in_addr dest_ip;
368 uint16_t d_port_wanted = pxenv_udp_read->d_port;
371 /* Try receiving a packet */
372 pxe_udp.pxenv_udp_read = pxenv_udp_read;
374 if ( pxe_udp.pxenv_udp_read ) {
375 /* No packet received */
376 DBG2 ( "PXENV_UDP_READ\n" );
377 pxe_udp.pxenv_udp_read = NULL;
380 dest_ip.s_addr = pxenv_udp_read->dest_ip;
381 d_port = pxenv_udp_read->d_port;
382 DBG ( "PXENV_UDP_READ" );
384 /* Filter on destination address and/or port */
385 if ( dest_ip_wanted.s_addr &&
386 ( dest_ip_wanted.s_addr != dest_ip.s_addr ) ) {
387 DBG ( " wrong IP %s", inet_ntoa ( dest_ip ) );
388 DBG ( " (wanted %s)\n", inet_ntoa ( dest_ip_wanted ) );
391 if ( d_port_wanted && ( d_port_wanted != d_port ) ) {
392 DBG ( " wrong port %d", htons ( d_port ) );
393 DBG ( " (wanted %d)\n", htons ( d_port_wanted ) );
397 DBG ( " %04x:%04x+%x %s:", pxenv_udp_read->buffer.segment,
398 pxenv_udp_read->buffer.offset, pxenv_udp_read->buffer_size,
399 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->src_ip ) ));
400 DBG ( "%d<-%s:%d\n", ntohs ( pxenv_udp_read->s_port ),
401 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->dest_ip ) ),
402 ntohs ( pxenv_udp_read->d_port ) );
404 pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
405 return PXENV_EXIT_SUCCESS;
408 pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
409 return PXENV_EXIT_FAILURE;
413 struct pxe_api_call pxe_udp_api[] __pxe_api_call = {
414 PXE_API_CALL ( PXENV_UDP_OPEN, pxenv_udp_open,
415 struct s_PXENV_UDP_OPEN ),
416 PXE_API_CALL ( PXENV_UDP_CLOSE, pxenv_udp_close,
417 struct s_PXENV_UDP_CLOSE ),
418 PXE_API_CALL ( PXENV_UDP_WRITE, pxenv_udp_write,
419 struct s_PXENV_UDP_WRITE ),
420 PXE_API_CALL ( PXENV_UDP_READ, pxenv_udp_read,
421 struct s_PXENV_UDP_READ ),