These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / pxe / pxe_udp.c
1 /** @file
2  *
3  * PXE UDP API
4  *
5  */
6
7 #include <string.h>
8 #include <byteswap.h>
9 #include <ipxe/iobuf.h>
10 #include <ipxe/xfer.h>
11 #include <ipxe/udp.h>
12 #include <ipxe/uaccess.h>
13 #include <ipxe/process.h>
14 #include <realmode.h>
15 #include <pxe.h>
16
17 /*
18  * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
19  *
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.
24  *
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.
29  *
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
33  * 02110-1301, USA.
34  *
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.
38  */
39
40 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
41
42 /** A PXE UDP pseudo-header */
43 struct pxe_udp_pseudo_header {
44         /** Source IP address */
45         IP4_t src_ip;
46         /** Source port */
47         UDP_PORT_t s_port;
48         /** Destination IP address */
49         IP4_t dest_ip;
50         /** Destination port */
51         UDP_PORT_t d_port;
52 } __attribute__ (( packed ));
53
54 /** A PXE UDP connection */
55 struct pxe_udp_connection {
56         /** Data transfer interface to UDP stack */
57         struct interface xfer;
58         /** Local address */
59         struct sockaddr_in local;
60         /** List of received packets */
61         struct list_head list;
62 };
63
64 /**
65  * Receive PXE UDP data
66  *
67  * @v pxe_udp                   PXE UDP connection
68  * @v iobuf                     I/O buffer
69  * @v meta                      Data transfer metadata
70  * @ret rc                      Return status code
71  *
72  * Receives a packet as part of the current pxenv_udp_read()
73  * operation.
74  */
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;
81         int rc;
82
83         /* Extract metadata */
84         assert ( meta );
85         sin_src = ( struct sockaddr_in * ) meta->src;
86         assert ( sin_src );
87         assert ( sin_src->sin_family == AF_INET );
88         sin_dest = ( struct sockaddr_in * ) meta->dest;
89         assert ( sin_dest );
90         assert ( sin_dest->sin_family == AF_INET );
91
92         /* Construct pseudo-header */
93         if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *pshdr ) ) ) != 0 ) {
94                 DBG ( "PXE could not prepend pseudo-header\n" );
95                 rc = -ENOMEM;
96                 goto drop;
97         }
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;
103
104         /* Add to queue */
105         list_add_tail ( &iobuf->list, &pxe_udp->list );
106
107         return 0;
108
109  drop:
110         free_iob ( iobuf );
111         return rc;
112 }
113
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 ),
117 };
118
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 );
122
123 /** The PXE UDP connection */
124 static struct pxe_udp_connection pxe_udp = {
125         .xfer = INTF_INIT ( pxe_udp_xfer_desc ),
126         .local = {
127                 .sin_family = AF_INET,
128         },
129         .list = LIST_HEAD_INIT ( pxe_udp.list ),
130 };
131
132 /**
133  * UDP OPEN
134  *
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
141  *
142  * Prepares the PXE stack for communication using pxenv_udp_write()
143  * and pxenv_udp_read().
144  *
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.)
151  *
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
158  * to Etherboot.
159  *
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.)
164  *
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.
169  *
170  * @note Etherboot currently ignores the s_PXENV_UDP_OPEN::src_ip
171  * parameter.
172  *
173  */
174 static PXENV_EXIT_t pxenv_udp_open ( struct s_PXENV_UDP_OPEN *pxenv_udp_open ) {
175         int rc;
176
177         DBG ( "PXENV_UDP_OPEN" );
178
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 ) );
182
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",
187                       strerror ( rc ) );
188                 pxenv_udp_open->Status = PXENV_STATUS ( rc );
189                 return PXENV_EXIT_FAILURE;
190         }
191
192         pxenv_udp_open->Status = PXENV_STATUS_SUCCESS;
193         return PXENV_EXIT_SUCCESS;
194 }
195
196 /**
197  * UDP CLOSE
198  *
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
202  * @err None                            -
203  *
204  * Closes a UDP connection opened with pxenv_udp_open().
205  *
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.
210  *
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.)
215  *
216  */
217 static PXENV_EXIT_t
218 pxenv_udp_close ( struct s_PXENV_UDP_CLOSE *pxenv_udp_close ) {
219         struct io_buffer *iobuf;
220         struct io_buffer *tmp;
221
222         DBG ( "PXENV_UDP_CLOSE\n" );
223
224         /* Close UDP connection */
225         intf_restart ( &pxe_udp.xfer, 0 );
226
227         /* Discard any received packets */
228         list_for_each_entry_safe ( iobuf, tmp, &pxe_udp.list, list ) {
229                 list_del ( &iobuf->list );
230                 free_iob ( iobuf );
231         }
232
233         pxenv_udp_close->Status = PXENV_STATUS_SUCCESS;
234         return PXENV_EXIT_SUCCESS;
235 }
236
237 /**
238  * UDP WRITE
239  *
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
252  *
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
258  * header.
259  *
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
262  * more details.
263  *
264  * If s_PXENV_UDP_WRITE::src_port is 0, port 2069 will be used.
265  *
266  * You must have opened a UDP connection with pxenv_udp_open() before
267  * calling pxenv_udp_write().
268  *
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.)
273  *
274  * @note Etherboot currently ignores the s_PXENV_UDP_WRITE::gw
275  * parameter.
276  *
277  */
278 static PXENV_EXIT_t
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,
285         };
286         size_t len;
287         struct io_buffer *iobuf;
288         userptr_t buffer;
289         int rc;
290
291         DBG ( "PXENV_UDP_WRITE" );
292
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;
298
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.
302          */
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 );
306
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.
310          */
311
312         /* Allocate and fill data buffer */
313         len = pxenv_udp_write->buffer_size;
314         iobuf = xfer_alloc_iob ( &pxe_udp.xfer, len );
315         if ( ! iobuf ) {
316                 DBG ( " out of memory\n" );
317                 pxenv_udp_write->Status = PXENV_STATUS_OUT_OF_RESOURCES;
318                 return PXENV_EXIT_FAILURE;
319         }
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 );
323
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 ) );
329         
330         /* Transmit packet */
331         if ( ( rc = xfer_deliver ( &pxe_udp.xfer, iobuf, &meta ) ) != 0 ) {
332                 DBG ( "PXENV_UDP_WRITE could not transmit: %s\n",
333                       strerror ( rc ) );
334                 pxenv_udp_write->Status = PXENV_STATUS ( rc );
335                 return PXENV_EXIT_FAILURE;
336         }
337
338         pxenv_udp_write->Status = PXENV_STATUS_SUCCESS;
339         return PXENV_EXIT_SUCCESS;
340 }
341
342 /**
343  * UDP READ
344  *
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
360  *
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.
364  *
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.
367  *
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.
370  *
371  * You must have opened a UDP connection with pxenv_udp_open() before
372  * calling pxenv_udp_read().
373  *
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.)
378  *
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.
383  *
384  */
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;
391         uint16_t d_port;
392         userptr_t buffer;
393         size_t len;
394
395         /* Try receiving a packet, if the queue is empty */
396         if ( list_empty ( &pxe_udp.list ) )
397                 step();
398
399         /* Remove first packet from the queue */
400         iobuf = list_first_entry ( &pxe_udp.list, struct io_buffer, list );
401         if ( ! iobuf ) {
402                 /* No packet received */
403                 DBG2 ( "PXENV_UDP_READ\n" );
404                 goto no_packet;
405         }
406         list_del ( &iobuf->list );
407
408         /* Strip pseudo-header */
409         assert ( iob_len ( iobuf ) >= sizeof ( *pshdr ) );
410         pshdr = iobuf->data;
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" );
415
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 ) );
421                 goto drop;
422         }
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 ) );
426                 goto drop;
427         }
428
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;
437
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;
443
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 ) );
450
451         /* Free I/O buffer */
452         free_iob ( iobuf );
453
454         pxenv_udp_read->Status = PXENV_STATUS_SUCCESS;
455         return PXENV_EXIT_SUCCESS;
456
457  drop:
458         free_iob ( iobuf );
459  no_packet:
460         pxenv_udp_read->Status = PXENV_STATUS_FAILURE;
461         return PXENV_EXIT_FAILURE;
462 }
463
464 /** PXE UDP API */
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 ),
474 };