1 /** @file
2 *
3 * PXE UDP API
4 *
5 */
6
7 #include <string.h>
8 #include <byteswap.h>
9 #include <gpxe/xfer.h>
10 #include <gpxe/udp.h>
11 #include <gpxe/uaccess.h>
12 #include <gpxe/process.h>
13 #include <pxe.h>
14
15 /*
16 * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
17 *
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.
22 *
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.
27 *
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.
31 */
32
33 FILE_LICENCE ( GPL2_OR_LATER );
34
35 /** A PXE UDP connection */
36 struct pxe_udp_connection {
37 /** Data transfer interface to UDP stack */
38 struct xfer_interface xfer;
39 /** Local address */
40 struct sockaddr_in local;
41 /** Current PXENV_UDP_READ parameter block */
42 struct s_PXENV_UDP_READ *pxenv_udp_read;
43 };
44
45 /**
46 * Receive PXE UDP data
47 *
48 * @v xfer Data transfer interface
49 * @v iobuf I/O buffer
50 * @v meta Data transfer metadata
51 * @ret rc Return status code
52 *
53 * Receives a packet as part of the current pxenv_udp_read()
54 * operation.
55 */
pxe_udp_deliver_iob(struct xfer_interface * xfer,struct io_buffer * iobuf,struct xfer_metadata * meta)56 static int pxe_udp_deliver_iob ( struct xfer_interface *xfer,
57 struct io_buffer *iobuf,
58 struct xfer_metadata *meta ) {
59 struct pxe_udp_connection *pxe_udp =
60 container_of ( xfer, struct pxe_udp_connection, xfer );
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;
64 userptr_t buffer;
65 size_t len;
66 int rc = 0;
67
68 if ( ! pxenv_udp_read ) {
69 DBG ( "PXE discarded UDP packet\n" );
70 rc = -ENOBUFS;
71 goto done;
72 }
73
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;
82
83 /* Fill in source/dest information */
84 assert ( meta );
85 sin_src = ( struct sockaddr_in * ) meta->src;
86 assert ( sin_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;
91 assert ( sin_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;
95
96 /* Mark as received */
97 pxe_udp->pxenv_udp_read = NULL;
98
99 done:
100 free_iob ( iobuf );
101 return rc;
102 }
103
104 /** PXE UDP data transfer interface operations */
105 static struct xfer_interface_operations pxe_udp_xfer_operations = {
106 .close = ignore_xfer_close,
107 .vredirect = ignore_xfer_vredirect,
108 .window = unlimited_xfer_window,
109 .alloc_iob = default_xfer_alloc_iob,
110 .deliver_iob = pxe_udp_deliver_iob,
111 .deliver_raw = xfer_deliver_as_iob,
112 };
113
114 /** The PXE UDP connection */
115 static struct pxe_udp_connection pxe_udp = {
116 .xfer = XFER_INIT ( &pxe_udp_xfer_operations ),
117 .local = {
118 .sin_family = AF_INET,
119 },
120 };
121
122 /**
123 * UDP OPEN
124 *
125 * @v pxenv_udp_open Pointer to a struct s_PXENV_UDP_OPEN
126 * @v s_PXENV_UDP_OPEN::src_ip IP address of this station, or 0.0.0.0
127 * @ret #PXENV_EXIT_SUCCESS Always
128 * @ret s_PXENV_UDP_OPEN::Status PXE status code
129 * @err #PXENV_STATUS_UDP_OPEN UDP connection already open
130 * @err #PXENV_STATUS_OUT_OF_RESOURCES Could not open connection
131 *
132 * Prepares the PXE stack for communication using pxenv_udp_write()
133 * and pxenv_udp_read().
134 *
135 * The IP address supplied in s_PXENV_UDP_OPEN::src_ip will be
136 * recorded and used as the local station's IP address for all further
137 * communication, including communication by means other than
138 * pxenv_udp_write() and pxenv_udp_read(). (If
139 * s_PXENV_UDP_OPEN::src_ip is 0.0.0.0, the local station's IP address
140 * will remain unchanged.)
141 *
142 * You can only have one open UDP connection at a time. This is not a
143 * meaningful restriction, since pxenv_udp_write() and
144 * pxenv_udp_read() allow you to specify arbitrary local and remote
145 * ports and an arbitrary remote address for each packet. According
146 * to the PXE specifiation, you cannot have a UDP connection open at
147 * the same time as a TFTP connection; this restriction does not apply
148 * to Etherboot.
149 *
150 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
151 * value before calling this function in protected mode. You cannot
152 * call this function with a 32-bit stack segment. (See the relevant
153 * @ref pxe_x86_pmode16 "implementation note" for more details.)
154 *
155 * @note The PXE specification does not make it clear whether the IP
156 * address supplied in s_PXENV_UDP_OPEN::src_ip should be used only
157 * for this UDP connection, or retained for all future communication.
158 * The latter seems more consistent with typical PXE stack behaviour.
159 *
160 * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
161 * parameter.
162 *
163 */
pxenv_udp_open(struct s_PXENV_UDP_OPEN * pxenv_udp_open)164 PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
165 int rc;
166
167 DBG ( "PXENV_UDP_OPEN" );
168
169 /* Record source IP address */
170 pxe_udp.local.sin_addr.s_addr = pxenv_udp_open->src_ip;
171 DBG ( " %s", inet_ntoa ( pxe_udp.local.sin_addr ) );
172
173 /* Open promiscuous UDP connection */
174 xfer_close ( &pxe_udp.xfer, 0 );
175 if ( ( rc = udp_open_promisc ( &pxe_udp.xfer ) ) != 0 ) {
176 pxenv_udp_open->Status = PXENV_STATUS ( rc );
177 return PXENV_EXIT_FAILURE;
178 }
179
180 pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
181 return PXENV_EXIT_SUCCESS;
182 }
183
184 /**
185 * UDP CLOSE
186 *
187 * @v pxenv_udp_close Pointer to a struct s_PXENV_UDP_CLOSE
188 * @ret #PXENV_EXIT_SUCCESS Always
189 * @ret s_PXENV_UDP_CLOSE::Status PXE status code
190 * @err None -
191 *
192 * Closes a UDP connection opened with pxenv_udp_open().
193 *
194 * You can only have one open UDP connection at a time. You cannot
195 * have a UDP connection open at the same time as a TFTP connection.
196 * You cannot use pxenv_udp_close() to close a TFTP connection; use
197 * pxenv_tftp_close() instead.
198 *
199 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
200 * value before calling this function in protected mode. You cannot
201 * call this function with a 32-bit stack segment. (See the relevant
202 * @ref pxe_x86_pmode16 "implementation note" for more details.)
203 *
204 */
pxenv_udp_close(struct s_PXENV_UDP_CLOSE * pxenv_udp_close)205 PXENV_EXIT_t pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
206 DBG ( "PXENV_UDP_CLOSE" );
207
208 /* Close UDP connection */
209 xfer_close ( &pxe_udp.xfer, 0 );
210
211 pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
212 return PXENV_EXIT_SUCCESS;
213 }
214
215 /**
216 * UDP WRITE
217 *
218 * @v pxenv_udp_write Pointer to a struct s_PXENV_UDP_WRITE
219 * @v s_PXENV_UDP_WRITE::ip Destination IP address
220 * @v s_PXENV_UDP_WRITE::gw Relay agent IP address, or 0.0.0.0
221 * @v s_PXENV_UDP_WRITE::src_port Source UDP port, or 0
222 * @v s_PXENV_UDP_WRITE::dst_port Destination UDP port
223 * @v s_PXENV_UDP_WRITE::buffer_size Length of the UDP payload
224 * @v s_PXENV_UDP_WRITE::buffer Address of the UDP payload
225 * @ret #PXENV_EXIT_SUCCESS Packet was transmitted successfully
226 * @ret #PXENV_EXIT_FAILURE Packet could not be transmitted
227 * @ret s_PXENV_UDP_WRITE::Status PXE status code
228 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
229 * @err #PXENV_STATUS_UNDI_TRANSMIT_ERROR Could not transmit packet
230 *
231 * Transmits a single UDP packet. A valid IP and UDP header will be
232 * prepended to the payload in s_PXENV_UDP_WRITE::buffer; the buffer
233 * should not contain precomputed IP and UDP headers, nor should it
234 * contain space allocated for these headers. The first byte of the
235 * buffer will be transmitted as the first byte following the UDP
236 * header.
237 *
238 * If s_PXENV_UDP_WRITE::gw is 0.0.0.0, normal IP routing will take
239 * place. See the relevant @ref pxe_routing "implementation note" for
240 * more details.
241 *
242 * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
243 *
244 * You must have opened a UDP connection with pxenv_udp_open() before
245 * calling pxenv_udp_write().
246 *
247 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
248 * value before calling this function in protected mode. You cannot
249 * call this function with a 32-bit stack segment. (See the relevant
250 * @ref pxe_x86_pmode16 "implementation note" for more details.)
251 *
252 * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
253 * parameter.
254 *
255 */
pxenv_udp_write(struct s_PXENV_UDP_WRITE * pxenv_udp_write)256 PXENV_EXIT_t pxenv_udp_write ( struct s_PXENV_UDP_WRITE *pxenv_udp_write ) {
257 struct sockaddr_in dest;
258 struct xfer_metadata meta = {
259 .src = ( struct sockaddr * ) &pxe_udp.local,
260 .dest = ( struct sockaddr * ) &dest,
261 .netdev = pxe_netdev,
262 };
263 size_t len;
264 struct io_buffer *iobuf;
265 userptr_t buffer;
266 int rc;
267
268 DBG ( "PXENV_UDP_WRITE" );
269
270 /* Construct destination socket address */
271 memset ( &dest, 0, sizeof ( dest ) );
272 dest.sin_family = AF_INET;
273 dest.sin_addr.s_addr = pxenv_udp_write->ip;
274 dest.sin_port = pxenv_udp_write->dst_port;
275
276 /* Set local (source) port. PXE spec says source port is 2069
277 * if not specified. Really, this ought to be set at UDP open
278 * time but hey, we didn't design this API.
279 */
280 pxe_udp.local.sin_port = pxenv_udp_write->src_port;
281 if ( ! pxe_udp.local.sin_port )
282 pxe_udp.local.sin_port = htons ( 2069 );
283
284 /* FIXME: we ignore the gateway specified, since we're
285 * confident of being able to do our own routing. We should
286 * probably allow for multiple gateways.
287 */
288
289 /* Allocate and fill data buffer */
290 len = pxenv_udp_write->buffer_size;
291 iobuf = xfer_alloc_iob ( &pxe_udp.xfer, len );
292 if ( ! iobuf ) {
293 pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
294 return PXENV_EXIT_FAILURE;
295 }
296 buffer = real_to_user ( pxenv_udp_write->buffer.segment,
297 pxenv_udp_write->buffer.offset );
298 copy_from_user ( iob_put ( iobuf, len ), buffer, 0, len );
299
300 DBG ( " %04x:%04x+%x %d->%s:%d", pxenv_udp_write->buffer.segment,
301 pxenv_udp_write->buffer.offset, pxenv_udp_write->buffer_size,
302 ntohs ( pxenv_udp_write->src_port ),
303 inet_ntoa ( dest.sin_addr ),
304 ntohs ( pxenv_udp_write->dst_port ) );
305
306 /* Transmit packet */
307 if ( ( rc = xfer_deliver_iob_meta ( &pxe_udp.xfer, iobuf,
308 &meta ) ) != 0 ) {
309 pxenv_udp_write->Status = PXENV_STATUS ( rc );
310 return PXENV_EXIT_FAILURE;
311 }
312
313 pxenv_udp_write->Status = PXENV_STATUS_SUCCESS;
314 return PXENV_EXIT_SUCCESS;
315 }
316
317 /**
318 * UDP READ
319 *
320 * @v pxenv_udp_read Pointer to a struct s_PXENV_UDP_READ
321 * @v s_PXENV_UDP_READ::dest_ip Destination IP address, or 0.0.0.0
322 * @v s_PXENV_UDP_READ::d_port Destination UDP port, or 0
323 * @v s_PXENV_UDP_READ::buffer_size Size of the UDP payload buffer
324 * @v s_PXENV_UDP_READ::buffer Address of the UDP payload buffer
325 * @ret #PXENV_EXIT_SUCCESS A packet has been received
326 * @ret #PXENV_EXIT_FAILURE No packet has been received
327 * @ret s_PXENV_UDP_READ::Status PXE status code
328 * @ret s_PXENV_UDP_READ::src_ip Source IP address
329 * @ret s_PXENV_UDP_READ::dest_ip Destination IP address
330 * @ret s_PXENV_UDP_READ::s_port Source UDP port
331 * @ret s_PXENV_UDP_READ::d_port Destination UDP port
332 * @ret s_PXENV_UDP_READ::buffer_size Length of UDP payload
333 * @err #PXENV_STATUS_UDP_CLOSED UDP connection is not open
334 * @err #PXENV_STATUS_FAILURE No packet was ready to read
335 *
336 * Receive a single UDP packet. This is a non-blocking call; if no
337 * packet is ready to read, the call will return instantly with
338 * s_PXENV_UDP_READ::Status==PXENV_STATUS_FAILURE.
339 *
340 * If s_PXENV_UDP_READ::dest_ip is 0.0.0.0, UDP packets addressed to
341 * any IP address will be accepted and may be returned to the caller.
342 *
343 * If s_PXENV_UDP_READ::d_port is 0, UDP packets addressed to any UDP
344 * port will be accepted and may be returned to the caller.
345 *
346 * You must have opened a UDP connection with pxenv_udp_open() before
347 * calling pxenv_udp_read().
348 *
349 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
350 * value before calling this function in protected mode. You cannot
351 * call this function with a 32-bit stack segment. (See the relevant
352 * @ref pxe_x86_pmode16 "implementation note" for more details.)
353 *
354 * @note The PXE specification (version 2.1) does not state that we
355 * should fill in s_PXENV_UDP_READ::dest_ip and
356 * s_PXENV_UDP_READ::d_port, but Microsoft Windows' NTLDR program
357 * expects us to do so, and will fail if we don't.
358 *
359 */
pxenv_udp_read(struct s_PXENV_UDP_READ * pxenv_udp_read)360 PXENV_EXIT_t pxenv_udp_read ( struct s_PXENV_UDP_READ *pxenv_udp_read ) {
361 struct in_addr dest_ip_wanted = { .s_addr = pxenv_udp_read->dest_ip };
362 struct in_addr dest_ip;
363 uint16_t d_port_wanted = pxenv_udp_read->d_port;
364 uint16_t d_port;
365
366 DBG ( "PXENV_UDP_READ" );
367
368 /* Try receiving a packet */
369 pxe_udp.pxenv_udp_read = pxenv_udp_read;
370 step();
371 if ( pxe_udp.pxenv_udp_read ) {
372 /* No packet received */
373 pxe_udp.pxenv_udp_read = NULL;
374 goto no_packet;
375 }
376 dest_ip.s_addr = pxenv_udp_read->dest_ip;
377 d_port = pxenv_udp_read->d_port;
378
379 /* Filter on destination address and/or port */
380 if ( dest_ip_wanted.s_addr &&
381 ( dest_ip_wanted.s_addr != dest_ip.s_addr ) ) {
382 DBG ( " wrong IP %s", inet_ntoa ( dest_ip ) );
383 DBG ( " (wanted %s)", inet_ntoa ( dest_ip_wanted ) );
384 goto no_packet;
385 }
386 if ( d_port_wanted && ( d_port_wanted != d_port ) ) {
387 DBG ( " wrong port %d ", htons ( d_port ) );
388 DBG ( " (wanted %d)", htons ( d_port_wanted ) );
389 goto no_packet;
390 }
391
392 DBG ( " %04x:%04x+%x %s:", pxenv_udp_read->buffer.segment,
393 pxenv_udp_read->buffer.offset, pxenv_udp_read->buffer_size,
394 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->src_ip ) ));
395 DBG ( "%d<-%s:%d", ntohs ( pxenv_udp_read->s_port ),
396 inet_ntoa ( *( ( struct in_addr * ) &pxenv_udp_read->dest_ip ) ),
397 ntohs ( pxenv_udp_read->d_port ) );
398
399 pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
400 return PXENV_EXIT_SUCCESS;
401
402 no_packet:
403 pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
404 return PXENV_EXIT_FAILURE;
405 }
406