[infiniband] Do not use GRH for local paths
[ipxe.git] / src / interface / efi / efi_pxe.c
1 /*
2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <errno.h>
28 #include <ipxe/refcnt.h>
29 #include <ipxe/list.h>
30 #include <ipxe/netdevice.h>
31 #include <ipxe/fakedhcp.h>
32 #include <ipxe/process.h>
33 #include <ipxe/uri.h>
34 #include <ipxe/in.h>
35 #include <ipxe/socket.h>
36 #include <ipxe/tcpip.h>
37 #include <ipxe/xferbuf.h>
38 #include <ipxe/open.h>
39 #include <ipxe/dhcppkt.h>
40 #include <ipxe/udp.h>
41 #include <ipxe/efi/efi.h>
42 #include <ipxe/efi/efi_snp.h>
43 #include <ipxe/efi/efi_pxe.h>
44 #include <ipxe/efi/Protocol/PxeBaseCode.h>
45 #include <usr/ifmgmt.h>
46 #include <config/general.h>
47
48 /** @file
49 *
50 * EFI PXE base code protocol
51 *
52 */
53
54 /* Downgrade user experience if configured to do so
55 *
56 * See comments in efi_snp.c
57 */
58 #ifdef EFI_DOWNGRADE_UX
59 static EFI_GUID dummy_pxe_base_code_protocol_guid = {
60 0x70647523, 0x2320, 0x7477,
61 { 0x66, 0x20, 0x23, 0x6d, 0x6f, 0x72, 0x6f, 0x6e }
62 };
63 #define efi_pxe_base_code_protocol_guid dummy_pxe_base_code_protocol_guid
64 #endif
65
66 /** A PXE base code */
67 struct efi_pxe {
68 /** Reference count */
69 struct refcnt refcnt;
70 /** Underlying network device */
71 struct net_device *netdev;
72 /** Name */
73 const char *name;
74 /** List of PXE base codes */
75 struct list_head list;
76
77 /** Installed handle */
78 EFI_HANDLE handle;
79 /** PXE base code protocol */
80 EFI_PXE_BASE_CODE_PROTOCOL base;
81 /** PXE base code mode */
82 EFI_PXE_BASE_CODE_MODE mode;
83
84 /** TCP/IP network-layer protocol */
85 struct tcpip_net_protocol *tcpip;
86 /** Network-layer protocol */
87 struct net_protocol *net;
88
89 /** Data transfer buffer */
90 struct xfer_buffer buf;
91
92 /** (M)TFTP download interface */
93 struct interface tftp;
94 /** Block size (for TFTP) */
95 size_t blksize;
96 /** Overall return status */
97 int rc;
98
99 /** UDP interface */
100 struct interface udp;
101 /** List of received UDP packets */
102 struct list_head queue;
103 /** UDP interface closer process */
104 struct process process;
105 };
106
107 /**
108 * Free PXE base code
109 *
110 * @v refcnt Reference count
111 */
112 static void efi_pxe_free ( struct refcnt *refcnt ) {
113 struct efi_pxe *pxe = container_of ( refcnt, struct efi_pxe, refcnt );
114
115 netdev_put ( pxe->netdev );
116 free ( pxe );
117 }
118
119 /** List of PXE base codes */
120 static LIST_HEAD ( efi_pxes );
121
122 /**
123 * Locate PXE base code
124 *
125 * @v handle EFI handle
126 * @ret pxe PXE base code, or NULL
127 */
128 static struct efi_pxe * efi_pxe_find ( EFI_HANDLE handle ) {
129 struct efi_pxe *pxe;
130
131 /* Locate base code */
132 list_for_each_entry ( pxe, &efi_pxes, list ) {
133 if ( pxe->handle == handle )
134 return pxe;
135 }
136
137 return NULL;
138 }
139
140 /******************************************************************************
141 *
142 * IP addresses
143 *
144 ******************************************************************************
145 */
146
147 /**
148 * An EFI socket address
149 *
150 */
151 struct sockaddr_efi {
152 /** Socket address family (part of struct @c sockaddr) */
153 sa_family_t se_family;
154 /** Flags (part of struct @c sockaddr_tcpip) */
155 uint16_t se_flags;
156 /** TCP/IP port (part of struct @c sockaddr_tcpip) */
157 uint16_t se_port;
158 /** Scope ID (part of struct @c sockaddr_tcpip)
159 *
160 * For link-local or multicast addresses, this is the network
161 * device index.
162 */
163 uint16_t se_scope_id;
164 /** IP address */
165 EFI_IP_ADDRESS se_addr;
166 /** Padding
167 *
168 * This ensures that a struct @c sockaddr_tcpip is large
169 * enough to hold a socket address for any TCP/IP address
170 * family.
171 */
172 char pad[ sizeof ( struct sockaddr ) -
173 ( sizeof ( sa_family_t ) /* se_family */ +
174 sizeof ( uint16_t ) /* se_flags */ +
175 sizeof ( uint16_t ) /* se_port */ +
176 sizeof ( uint16_t ) /* se_scope_id */ +
177 sizeof ( EFI_IP_ADDRESS ) /* se_addr */ ) ];
178 } __attribute__ (( packed, may_alias ));
179
180 /**
181 * Populate socket address from EFI IP address
182 *
183 * @v pxe PXE base code
184 * @v ip EFI IP address
185 * @v sa Socket address to fill in
186 */
187 static void efi_pxe_ip_sockaddr ( struct efi_pxe *pxe, EFI_IP_ADDRESS *ip,
188 struct sockaddr *sa ) {
189 union {
190 struct sockaddr sa;
191 struct sockaddr_efi se;
192 } *sockaddr = container_of ( sa, typeof ( *sockaddr ), sa );
193
194 /* Initialise socket address */
195 memset ( sockaddr, 0, sizeof ( *sockaddr ) );
196 sockaddr->sa.sa_family = pxe->tcpip->sa_family;
197 memcpy ( &sockaddr->se.se_addr, ip, pxe->net->net_addr_len );
198 sockaddr->se.se_scope_id = pxe->netdev->index;
199 }
200
201 /**
202 * Transcribe EFI IP address (for debugging)
203 *
204 * @v pxe PXE base code
205 * @v ip EFI IP address
206 * @ret text Transcribed IP address
207 */
208 static const char * efi_pxe_ip_ntoa ( struct efi_pxe *pxe,
209 EFI_IP_ADDRESS *ip ) {
210
211 return pxe->net->ntoa ( ip );
212 }
213
214 /**
215 * Populate local IP address
216 *
217 * @v pxe PXE base code
218 * @ret rc Return status code
219 */
220 static int efi_pxe_ip ( struct efi_pxe *pxe ) {
221 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
222 struct in_addr address;
223 struct in_addr netmask;
224
225 /* It's unclear which of the potentially many IPv6 addresses
226 * is supposed to be used.
227 */
228 if ( mode->UsingIpv6 )
229 return -ENOTSUP;
230
231 /* Fetch IP address and subnet mask */
232 fetch_ipv4_setting ( netdev_settings ( pxe->netdev ), &ip_setting,
233 &address );
234 fetch_ipv4_setting ( netdev_settings ( pxe->netdev ), &netmask_setting,
235 &netmask );
236
237 /* Populate IP address and subnet mask */
238 memset ( &mode->StationIp, 0, sizeof ( mode->StationIp ) );
239 memcpy ( &mode->StationIp, &address, sizeof ( address ) );
240 memset ( &mode->SubnetMask, 0, sizeof ( mode->SubnetMask ) );
241 memcpy ( &mode->SubnetMask, &netmask, sizeof ( netmask ) );
242
243 return 0;
244 }
245
246 /**
247 * Check if IP address matches filter
248 *
249 * @v pxe PXE base code
250 * @v ip EFI IP address
251 * @ret is_match IP address matches filter
252 */
253 static int efi_pxe_ip_filter ( struct efi_pxe *pxe, EFI_IP_ADDRESS *ip ) {
254 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
255 EFI_PXE_BASE_CODE_IP_FILTER *filter = &mode->IpFilter;
256 uint8_t filters = filter->Filters;
257 union {
258 EFI_IP_ADDRESS ip;
259 struct in_addr in;
260 struct in6_addr in6;
261 } *u = container_of ( ip, typeof ( *u ), ip );
262 size_t addr_len = pxe->net->net_addr_len;
263 unsigned int i;
264
265 /* Match everything, if applicable */
266 if ( filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS )
267 return 1;
268
269 /* Match all multicasts, if applicable */
270 if ( filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST ) {
271 if ( mode->UsingIpv6 ) {
272 if ( IN6_IS_ADDR_MULTICAST ( &u->in6 ) )
273 return 1;
274 } else {
275 if ( IN_IS_MULTICAST ( u->in.s_addr ) )
276 return 1;
277 }
278 }
279
280 /* Match IPv4 broadcasts, if applicable */
281 if ( filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST ) {
282 if ( ( ! mode->UsingIpv6 ) &&
283 ( u->in.s_addr == INADDR_BROADCAST ) )
284 return 1;
285 }
286
287 /* Match station address, if applicable */
288 if ( filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP ) {
289 if ( memcmp ( ip, &mode->StationIp, addr_len ) == 0 )
290 return 1;
291 }
292
293 /* Match explicit addresses, if applicable */
294 for ( i = 0 ; i < filter->IpCnt ; i++ ) {
295 if ( memcmp ( ip, &filter->IpList[i], addr_len ) == 0 )
296 return 1;
297 }
298
299 return 0;
300 }
301
302 /******************************************************************************
303 *
304 * Data transfer buffer
305 *
306 ******************************************************************************
307 */
308
309 /**
310 * Reallocate PXE data transfer buffer
311 *
312 * @v xferbuf Data transfer buffer
313 * @v len New length (or zero to free buffer)
314 * @ret rc Return status code
315 */
316 static int efi_pxe_buf_realloc ( struct xfer_buffer *xferbuf __unused,
317 size_t len __unused ) {
318
319 /* Can never reallocate: return EFI_BUFFER_TOO_SMALL */
320 return -ERANGE;
321 }
322
323 /**
324 * Write data to PXE data transfer buffer
325 *
326 * @v xferbuf Data transfer buffer
327 * @v offset Starting offset
328 * @v data Data to copy
329 * @v len Length of data
330 */
331 static void efi_pxe_buf_write ( struct xfer_buffer *xferbuf, size_t offset,
332 const void *data, size_t len ) {
333
334 /* Copy data to buffer */
335 memcpy ( ( xferbuf->data + offset ), data, len );
336 }
337
338 /** PXE data transfer buffer operations */
339 static struct xfer_buffer_operations efi_pxe_buf_operations = {
340 .realloc = efi_pxe_buf_realloc,
341 .write = efi_pxe_buf_write,
342 };
343
344 /******************************************************************************
345 *
346 * (M)TFTP download interface
347 *
348 ******************************************************************************
349 */
350
351 /**
352 * Close PXE (M)TFTP download interface
353 *
354 * @v pxe PXE base code
355 * @v rc Reason for close
356 */
357 static void efi_pxe_tftp_close ( struct efi_pxe *pxe, int rc ) {
358
359 /* Restart interface */
360 intf_restart ( &pxe->tftp, rc );
361
362 /* Record overall status */
363 pxe->rc = rc;
364 }
365
366 /**
367 * Check PXE (M)TFTP download flow control window
368 *
369 * @v pxe PXE base code
370 * @ret len Length of window
371 */
372 static size_t efi_pxe_tftp_window ( struct efi_pxe *pxe ) {
373
374 /* Return requested blocksize */
375 return pxe->blksize;
376 }
377
378 /**
379 * Receive new PXE (M)TFTP download data
380 *
381 * @v pxe PXE base code
382 * @v iobuf I/O buffer
383 * @v meta Transfer metadata
384 * @ret rc Return status code
385 */
386 static int efi_pxe_tftp_deliver ( struct efi_pxe *pxe,
387 struct io_buffer *iobuf,
388 struct xfer_metadata *meta ) {
389 int rc;
390
391 /* Deliver to data transfer buffer */
392 if ( ( rc = xferbuf_deliver ( &pxe->buf, iob_disown ( iobuf ),
393 meta ) ) != 0 )
394 goto err_deliver;
395
396 return 0;
397
398 err_deliver:
399 efi_pxe_tftp_close ( pxe, rc );
400 return rc;
401 }
402
403 /** PXE file data transfer interface operations */
404 static struct interface_operation efi_pxe_tftp_operations[] = {
405 INTF_OP ( xfer_deliver, struct efi_pxe *, efi_pxe_tftp_deliver ),
406 INTF_OP ( xfer_window, struct efi_pxe *, efi_pxe_tftp_window ),
407 INTF_OP ( intf_close, struct efi_pxe *, efi_pxe_tftp_close ),
408 };
409
410 /** PXE file data transfer interface descriptor */
411 static struct interface_descriptor efi_pxe_tftp_desc =
412 INTF_DESC ( struct efi_pxe, tftp, efi_pxe_tftp_operations );
413
414 /**
415 * Open (M)TFTP download interface
416 *
417 * @v pxe PXE base code
418 * @v ip EFI IP address
419 * @v filename Filename
420 * @ret rc Return status code
421 */
422 static int efi_pxe_tftp_open ( struct efi_pxe *pxe, EFI_IP_ADDRESS *ip,
423 const char *filename ) {
424 struct sockaddr server;
425 struct uri *uri;
426 int rc;
427
428 /* Parse server address and filename */
429 efi_pxe_ip_sockaddr ( pxe, ip, &server );
430 uri = pxe_uri ( &server, filename );
431 if ( ! uri ) {
432 DBGC ( pxe, "PXE %s could not parse %s:%s\n", pxe->name,
433 efi_pxe_ip_ntoa ( pxe, ip ), filename );
434 rc = -ENOTSUP;
435 goto err_parse;
436 }
437
438 /* Open URI */
439 if ( ( rc = xfer_open_uri ( &pxe->tftp, uri ) ) != 0 ) {
440 DBGC ( pxe, "PXE %s could not open: %s\n",
441 pxe->name, strerror ( rc ) );
442 goto err_open;
443 }
444
445 err_open:
446 uri_put ( uri );
447 err_parse:
448 return rc;
449 }
450
451 /******************************************************************************
452 *
453 * UDP interface
454 *
455 ******************************************************************************
456 */
457
458 /** EFI UDP pseudo-header */
459 struct efi_pxe_udp_pseudo_header {
460 /** Network-layer protocol */
461 struct net_protocol *net;
462 /** Destination port */
463 uint16_t dest_port;
464 /** Source port */
465 uint16_t src_port;
466 } __attribute__ (( packed ));
467
468 /**
469 * Close UDP interface
470 *
471 * @v pxe PXE base code
472 * @v rc Reason for close
473 */
474 static void efi_pxe_udp_close ( struct efi_pxe *pxe, int rc ) {
475 struct io_buffer *iobuf;
476 struct io_buffer *tmp;
477
478 /* Release our claim on SNP devices, if applicable */
479 if ( process_running ( &pxe->process ) )
480 efi_snp_release();
481
482 /* Stop process */
483 process_del ( &pxe->process );
484
485 /* Restart UDP interface */
486 intf_restart ( &pxe->udp, rc );
487
488 /* Flush any received UDP packets */
489 list_for_each_entry_safe ( iobuf, tmp, &pxe->queue, list ) {
490 list_del ( &iobuf->list );
491 free_iob ( iobuf );
492 }
493 }
494
495 /**
496 * Receive UDP packet
497 *
498 * @v pxe PXE base code
499 * @v iobuf I/O buffer
500 * @v meta Data transfer metadata
501 * @ret rc Return status code
502 */
503 static int efi_pxe_udp_deliver ( struct efi_pxe *pxe, struct io_buffer *iobuf,
504 struct xfer_metadata *meta ) {
505 struct sockaddr_efi *se_src;
506 struct sockaddr_efi *se_dest;
507 struct tcpip_net_protocol *tcpip;
508 struct net_protocol *net;
509 struct efi_pxe_udp_pseudo_header *pshdr;
510 size_t addr_len;
511 size_t pshdr_len;
512 int rc;
513
514 /* Sanity checks */
515 assert ( meta != NULL );
516 se_src = ( ( struct sockaddr_efi * ) meta->src );
517 assert ( se_src != NULL );
518 se_dest = ( ( struct sockaddr_efi * ) meta->dest );
519 assert ( se_dest != NULL );
520 assert ( se_src->se_family == se_dest->se_family );
521
522 /* Determine protocol */
523 tcpip = tcpip_net_protocol ( se_src->se_family );
524 if ( ! tcpip ) {
525 rc = -ENOTSUP;
526 goto err_unsupported;
527 }
528 net = tcpip->net_protocol;
529 addr_len = net->net_addr_len;
530
531 /* Construct pseudo-header */
532 pshdr_len = ( sizeof ( *pshdr ) + ( 2 * addr_len ) );
533 if ( ( rc = iob_ensure_headroom ( iobuf, pshdr_len ) ) != 0 )
534 goto err_headroom;
535 memcpy ( iob_push ( iobuf, addr_len ), &se_src->se_addr, addr_len );
536 memcpy ( iob_push ( iobuf, addr_len ), &se_dest->se_addr, addr_len );
537 pshdr = iob_push ( iobuf, sizeof ( *pshdr ) );
538 pshdr->net = net;
539 pshdr->dest_port = ntohs ( se_dest->se_port );
540 pshdr->src_port = ntohs ( se_src->se_port );
541
542 /* Add to queue */
543 list_add_tail ( &iobuf->list, &pxe->queue );
544
545 return 0;
546
547 err_unsupported:
548 err_headroom:
549 free_iob ( iobuf );
550 return rc;
551 }
552
553 /** PXE UDP interface operations */
554 static struct interface_operation efi_pxe_udp_operations[] = {
555 INTF_OP ( xfer_deliver, struct efi_pxe *, efi_pxe_udp_deliver ),
556 INTF_OP ( intf_close, struct efi_pxe *, efi_pxe_udp_close ),
557 };
558
559 /** PXE UDP interface descriptor */
560 static struct interface_descriptor efi_pxe_udp_desc =
561 INTF_DESC ( struct efi_pxe, udp, efi_pxe_udp_operations );
562
563 /**
564 * Open UDP interface
565 *
566 * @v pxe PXE base code
567 * @ret rc Return status code
568 */
569 static int efi_pxe_udp_open ( struct efi_pxe *pxe ) {
570 int rc;
571
572 /* If interface is already open, then cancel the scheduled close */
573 if ( process_running ( &pxe->process ) ) {
574 process_del ( &pxe->process );
575 return 0;
576 }
577
578 /* Open promiscuous UDP interface */
579 if ( ( rc = udp_open_promisc ( &pxe->udp ) ) != 0 ) {
580 DBGC ( pxe, "PXE %s could not open UDP connection: %s\n",
581 pxe->name, strerror ( rc ) );
582 return rc;
583 }
584
585 /* Claim network devices */
586 efi_snp_claim();
587
588 return 0;
589 }
590
591 /**
592 * Schedule close of UDP interface
593 *
594 * @v pxe PXE base code
595 */
596 static void efi_pxe_udp_schedule_close ( struct efi_pxe *pxe ) {
597
598 /* The EFI PXE base code protocol does not provide any
599 * explicit UDP open/close methods. To avoid the overhead of
600 * reopening a socket for each read/write operation, we start
601 * a process which will close the socket immediately if the
602 * next call into iPXE is anything other than a UDP
603 * read/write.
604 */
605 process_add ( &pxe->process );
606 }
607
608 /**
609 * Scheduled close of UDP interface
610 *
611 * @v pxe PXE base code
612 */
613 static void efi_pxe_udp_scheduled_close ( struct efi_pxe *pxe ) {
614
615 /* Close UDP interface */
616 efi_pxe_udp_close ( pxe, 0 );
617 }
618
619 /** UDP close process descriptor */
620 static struct process_descriptor efi_pxe_process_desc =
621 PROC_DESC_ONCE ( struct efi_pxe, process, efi_pxe_udp_scheduled_close );
622
623 /******************************************************************************
624 *
625 * Fake DHCP packets
626 *
627 ******************************************************************************
628 */
629
630 /**
631 * Name fake DHCP packet
632 *
633 * @v pxe PXE base code
634 * @v packet Packet
635 * @ret name Name of packet
636 */
637 static const char * efi_pxe_fake_name ( struct efi_pxe *pxe,
638 EFI_PXE_BASE_CODE_PACKET *packet ) {
639 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
640
641 if ( packet == &mode->DhcpDiscover ) {
642 return "DhcpDiscover";
643 } else if ( packet == &mode->DhcpAck ) {
644 return "DhcpAck";
645 } else if ( packet == &mode->ProxyOffer ) {
646 return "ProxyOffer";
647 } else if ( packet == &mode->PxeDiscover ) {
648 return "PxeDiscover";
649 } else if ( packet == &mode->PxeReply ) {
650 return "PxeReply";
651 } else if ( packet == &mode->PxeBisReply ) {
652 return "PxeBisReply";
653 } else {
654 return "<UNKNOWN>";
655 }
656 }
657
658 /**
659 * Construct fake DHCP packet and flag
660 *
661 * @v pxe PXE base code
662 * @v fake Fake packet constructor
663 * @v packet Packet to fill in
664 * @ret exists Packet existence flag
665 */
666 static BOOLEAN efi_pxe_fake ( struct efi_pxe *pxe,
667 int ( * fake ) ( struct net_device *netdev,
668 void *data, size_t len ),
669 EFI_PXE_BASE_CODE_PACKET *packet ) {
670 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
671 struct dhcp_packet dhcppkt;
672 struct dhcphdr *dhcphdr;
673 unsigned int len;
674 int rc;
675
676 /* The fake packet constructors do not support IPv6 */
677 if ( mode->UsingIpv6 )
678 return FALSE;
679
680 /* Attempt to construct packet */
681 if ( ( rc = fake ( pxe->netdev, packet, sizeof ( *packet ) ) != 0 ) ) {
682 DBGC ( pxe, "PXE %s could not fake %s: %s\n", pxe->name,
683 efi_pxe_fake_name ( pxe, packet ), strerror ( rc ) );
684 return FALSE;
685 }
686
687 /* The WDS bootstrap wdsmgfw.efi has a buggy DHCPv4 packet
688 * parser which does not correctly handle DHCP padding bytes.
689 * Specifically, if a padding byte (i.e. a zero) is
690 * encountered, the parse will first increment the pointer by
691 * one to skip over the padding byte but will then drop into
692 * the code path for handling normal options, which increments
693 * the pointer by two to skip over the (already-skipped) type
694 * field and the (non-existent) length field.
695 *
696 * The upshot of this bug in WDS is that the parser will fail
697 * with an error 0xc0000023 if the number of spare bytes after
698 * the end of the options is not an exact multiple of three.
699 *
700 * Work around this buggy parser by adding an explicit
701 * DHCP_END tag.
702 */
703 dhcphdr = container_of ( &packet->Dhcpv4.BootpOpcode,
704 struct dhcphdr, op );
705 dhcppkt_init ( &dhcppkt, dhcphdr, sizeof ( *packet ) );
706 len = dhcppkt_len ( &dhcppkt );
707 if ( len < sizeof ( *packet ) )
708 packet->Raw[len] = DHCP_END;
709
710 return TRUE;
711 }
712
713 /**
714 * Construct fake DHCP packets
715 *
716 * @v pxe PXE base code
717 */
718 static void efi_pxe_fake_all ( struct efi_pxe *pxe ) {
719 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
720
721 /* Construct fake packets */
722 mode->DhcpDiscoverValid =
723 efi_pxe_fake ( pxe, create_fakedhcpdiscover,
724 &mode->DhcpDiscover );
725 mode->DhcpAckReceived =
726 efi_pxe_fake ( pxe, create_fakedhcpack,
727 &mode->DhcpAck );
728 mode->PxeReplyReceived =
729 efi_pxe_fake ( pxe, create_fakepxebsack,
730 &mode->PxeReply );
731 }
732
733 /******************************************************************************
734 *
735 * Base code protocol
736 *
737 ******************************************************************************
738 */
739
740 /**
741 * Start PXE base code
742 *
743 * @v base PXE base code protocol
744 * @v use_ipv6 Use IPv6
745 * @ret efirc EFI status code
746 */
747 static EFI_STATUS EFIAPI efi_pxe_start ( EFI_PXE_BASE_CODE_PROTOCOL *base,
748 BOOLEAN use_ipv6 ) {
749 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
750 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
751 struct tcpip_net_protocol *ipv6 = tcpip_net_protocol ( AF_INET6 );
752 sa_family_t family = ( use_ipv6 ? AF_INET6 : AF_INET );
753 int rc;
754
755 DBGC ( pxe, "PXE %s START %s\n", pxe->name, ( ipv6 ? "IPv6" : "IPv4" ));
756
757 /* Initialise mode structure */
758 memset ( mode, 0, sizeof ( *mode ) );
759 mode->AutoArp = TRUE;
760 mode->TTL = DEFAULT_TTL;
761 mode->ToS = DEFAULT_ToS;
762 mode->IpFilter.Filters =
763 ( EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP |
764 EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST |
765 EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS |
766 EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST );
767
768 /* Check for IPv4/IPv6 support */
769 mode->Ipv6Supported = ( ipv6 != NULL );
770 mode->Ipv6Available = ( ipv6 != NULL );
771 pxe->tcpip = tcpip_net_protocol ( family );
772 if ( ! pxe->tcpip ) {
773 DBGC ( pxe, "PXE %s has no support for %s\n",
774 pxe->name, socket_family_name ( family ) );
775 return EFI_UNSUPPORTED;
776 }
777 pxe->net = pxe->tcpip->net_protocol;
778 mode->UsingIpv6 = use_ipv6;
779
780 /* Populate station IP address */
781 if ( ( rc = efi_pxe_ip ( pxe ) ) != 0 )
782 return rc;
783
784 /* Construct fake DHCP packets */
785 efi_pxe_fake_all ( pxe );
786
787 /* Record that base code is started */
788 mode->Started = TRUE;
789 DBGC ( pxe, "PXE %s using %s\n",
790 pxe->name, pxe->net->ntoa ( &mode->StationIp ) );
791
792 return 0;
793 }
794
795 /**
796 * Stop PXE base code
797 *
798 * @v base PXE base code protocol
799 * @ret efirc EFI status code
800 */
801 static EFI_STATUS EFIAPI efi_pxe_stop ( EFI_PXE_BASE_CODE_PROTOCOL *base ) {
802 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
803 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
804
805 DBGC ( pxe, "PXE %s STOP\n", pxe->name );
806
807 /* Record that base code is stopped */
808 mode->Started = FALSE;
809
810 /* Close TFTP */
811 efi_pxe_tftp_close ( pxe, 0 );
812
813 /* Close UDP */
814 efi_pxe_udp_close ( pxe, 0 );
815
816 return 0;
817 }
818
819 /**
820 * Perform DHCP
821 *
822 * @v base PXE base code protocol
823 * @v sort Offers should be sorted
824 * @ret efirc EFI status code
825 */
826 static EFI_STATUS EFIAPI efi_pxe_dhcp ( EFI_PXE_BASE_CODE_PROTOCOL *base,
827 BOOLEAN sort ) {
828 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
829 struct net_device *netdev = pxe->netdev;
830 int rc;
831
832 DBGC ( pxe, "PXE %s DHCP %s\n",
833 pxe->name, ( sort ? "sorted" : "unsorted" ) );
834
835 /* Claim network devices */
836 efi_snp_claim();
837
838 /* Initiate configuration */
839 if ( ( rc = netdev_configure_all ( netdev ) ) != 0 ) {
840 DBGC ( pxe, "PXE %s could not initiate configuration: %s\n",
841 pxe->name, strerror ( rc ) );
842 goto err_configure;
843 }
844
845 /* Wait for configuration to complete (or time out) */
846 while ( netdev_configuration_in_progress ( netdev ) )
847 step();
848
849 /* Report timeout if configuration failed */
850 if ( ! netdev_configuration_ok ( netdev ) ) {
851 rc = -ETIMEDOUT;
852 goto err_timeout;
853 }
854
855 /* Update station IP address */
856 if ( ( rc = efi_pxe_ip ( pxe ) ) != 0 )
857 goto err_ip;
858
859 /* Update faked DHCP packets */
860 efi_pxe_fake_all ( pxe );
861
862 err_ip:
863 err_timeout:
864 err_configure:
865 efi_snp_release();
866 return EFIRC ( rc );
867 }
868
869 /**
870 * Perform boot server discovery
871 *
872 * @v base PXE base code protocol
873 * @v type Boot server type
874 * @v layer Boot server layer
875 * @v bis Use boot integrity services
876 * @v info Additional information
877 * @ret efirc EFI status code
878 */
879 static EFI_STATUS EFIAPI
880 efi_pxe_discover ( EFI_PXE_BASE_CODE_PROTOCOL *base, UINT16 type, UINT16 *layer,
881 BOOLEAN bis, EFI_PXE_BASE_CODE_DISCOVER_INFO *info ) {
882 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
883 EFI_IP_ADDRESS *ip;
884 unsigned int i;
885
886 DBGC ( pxe, "PXE %s DISCOVER type %d layer %d%s\n",
887 pxe->name, type, *layer, ( bis ? " bis" : "" ) );
888 if ( info ) {
889 DBGC ( pxe, "%s%s%s%s %s",
890 ( info->UseMCast ? " mcast" : "" ),
891 ( info->UseBCast ? " bcast" : "" ),
892 ( info->UseUCast ? " ucast" : "" ),
893 ( info->MustUseList ? " list" : "" ),
894 efi_pxe_ip_ntoa ( pxe, &info->ServerMCastIp ) );
895 for ( i = 0 ; i < info->IpCnt ; i++ ) {
896 ip = &info->SrvList[i].IpAddr;
897 DBGC ( pxe, " %d%s:%s", info->SrvList[i].Type,
898 ( info->SrvList[i].AcceptAnyResponse ?
899 ":any" : "" ), efi_pxe_ip_ntoa ( pxe, ip ) );
900 }
901 }
902 DBGC ( pxe, "\n" );
903
904 /* Not used by any bootstrap I can find to test with */
905 return EFI_UNSUPPORTED;
906 }
907
908 /**
909 * Perform (M)TFTP
910 *
911 * @v base PXE base code protocol
912 * @v opcode TFTP opcode
913 * @v data Data buffer
914 * @v overwrite Overwrite file
915 * @v len Length of data buffer
916 * @v blksize Block size
917 * @v ip Server address
918 * @v filename Filename
919 * @v info Additional information
920 * @v callback Pass packets to callback instead of data buffer
921 * @ret efirc EFI status code
922 */
923 static EFI_STATUS EFIAPI
924 efi_pxe_mtftp ( EFI_PXE_BASE_CODE_PROTOCOL *base,
925 EFI_PXE_BASE_CODE_TFTP_OPCODE opcode, VOID *data,
926 BOOLEAN overwrite, UINT64 *len, UINTN *blksize,
927 EFI_IP_ADDRESS *ip, UINT8 *filename,
928 EFI_PXE_BASE_CODE_MTFTP_INFO *info, BOOLEAN callback ) {
929 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
930 int rc;
931
932 DBGC ( pxe, "PXE %s MTFTP %d%s %p+%llx", pxe->name, opcode,
933 ( overwrite ? " overwrite" : "" ), data, *len );
934 if ( blksize )
935 DBGC ( pxe, " blksize %zd", ( ( size_t ) *blksize ) );
936 DBGC ( pxe, " %s:%s", efi_pxe_ip_ntoa ( pxe, ip ), filename );
937 if ( info ) {
938 DBGC ( pxe, " %s:%d:%d:%d:%d",
939 efi_pxe_ip_ntoa ( pxe, &info->MCastIp ),
940 info->CPort, info->SPort, info->ListenTimeout,
941 info->TransmitTimeout );
942 }
943 DBGC ( pxe, "%s\n", ( callback ? " callback" : "" ) );
944
945 /* Fail unless operation is supported */
946 if ( ! ( ( opcode == EFI_PXE_BASE_CODE_TFTP_READ_FILE ) ||
947 ( opcode == EFI_PXE_BASE_CODE_MTFTP_READ_FILE ) ) ) {
948 DBGC ( pxe, "PXE %s unsupported MTFTP opcode %d\n",
949 pxe->name, opcode );
950 rc = -ENOTSUP;
951 goto err_opcode;
952 }
953
954 /* Claim network devices */
955 efi_snp_claim();
956
957 /* Determine block size. Ignore the requested block size
958 * unless we are using callbacks, since limiting HTTP to a
959 * 512-byte TCP window is not sensible.
960 */
961 pxe->blksize = ( ( callback && blksize ) ? *blksize : -1UL );
962
963 /* Initialise data transfer buffer */
964 pxe->buf.data = data;
965 pxe->buf.len = *len;
966
967 /* Open download */
968 if ( ( rc = efi_pxe_tftp_open ( pxe, ip,
969 ( ( const char * ) filename ) ) ) != 0 )
970 goto err_open;
971
972 /* Wait for download to complete */
973 pxe->rc = -EINPROGRESS;
974 while ( pxe->rc == -EINPROGRESS )
975 step();
976 if ( ( rc = pxe->rc ) != 0 ) {
977 DBGC ( pxe, "PXE %s download failed: %s\n",
978 pxe->name, strerror ( rc ) );
979 goto err_download;
980 }
981
982 err_download:
983 efi_pxe_tftp_close ( pxe, rc );
984 err_open:
985 efi_snp_release();
986 err_opcode:
987 return EFIRC ( rc );
988 }
989
990 /**
991 * Transmit UDP packet
992 *
993 * @v base PXE base code protocol
994 * @v flags Operation flags
995 * @v dest_ip Destination address
996 * @v dest_port Destination port
997 * @v gateway Gateway address
998 * @v src_ip Source address
999 * @v src_port Source port
1000 * @v hdr_len Header length
1001 * @v hdr Header data
1002 * @v len Length
1003 * @v data Data
1004 * @ret efirc EFI status code
1005 */
1006 static EFI_STATUS EFIAPI
1007 efi_pxe_udp_write ( EFI_PXE_BASE_CODE_PROTOCOL *base, UINT16 flags,
1008 EFI_IP_ADDRESS *dest_ip,
1009 EFI_PXE_BASE_CODE_UDP_PORT *dest_port,
1010 EFI_IP_ADDRESS *gateway, EFI_IP_ADDRESS *src_ip,
1011 EFI_PXE_BASE_CODE_UDP_PORT *src_port,
1012 UINTN *hdr_len, VOID *hdr, UINTN *len, VOID *data ) {
1013 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
1014 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
1015 struct io_buffer *iobuf;
1016 struct xfer_metadata meta;
1017 union {
1018 struct sockaddr_tcpip st;
1019 struct sockaddr sa;
1020 } dest;
1021 union {
1022 struct sockaddr_tcpip st;
1023 struct sockaddr sa;
1024 } src;
1025 int rc;
1026
1027 DBGC2 ( pxe, "PXE %s UDP WRITE ", pxe->name );
1028 if ( src_ip )
1029 DBGC2 ( pxe, "%s", efi_pxe_ip_ntoa ( pxe, src_ip ) );
1030 DBGC2 ( pxe, ":" );
1031 if ( src_port &&
1032 ( ! ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT ) ) ) {
1033 DBGC2 ( pxe, "%d", *src_port );
1034 } else {
1035 DBGC2 ( pxe, "*" );
1036 }
1037 DBGC2 ( pxe, "->%s:%d", efi_pxe_ip_ntoa ( pxe, dest_ip ), *dest_port );
1038 if ( gateway )
1039 DBGC2 ( pxe, " via %s", efi_pxe_ip_ntoa ( pxe, gateway ) );
1040 if ( hdr_len )
1041 DBGC2 ( pxe, " %p+%zx", hdr, ( ( size_t ) *hdr_len ) );
1042 DBGC2 ( pxe, " %p+%zx", data, ( ( size_t ) *len ) );
1043 if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT )
1044 DBGC2 ( pxe, " frag" );
1045 DBGC2 ( pxe, "\n" );
1046
1047 /* Open UDP connection (if applicable) */
1048 if ( ( rc = efi_pxe_udp_open ( pxe ) ) != 0 )
1049 goto err_open;
1050
1051 /* Construct destination address */
1052 efi_pxe_ip_sockaddr ( pxe, dest_ip, &dest.sa );
1053 dest.st.st_port = htons ( *dest_port );
1054
1055 /* Construct source address */
1056 efi_pxe_ip_sockaddr ( pxe, ( src_ip ? src_ip : &mode->StationIp ),
1057 &src.sa );
1058 if ( src_port &&
1059 ( ! ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT ) ) ) {
1060 src.st.st_port = htons ( *src_port );
1061 } else {
1062 /* The API does not allow for a sensible concept of
1063 * binding to a local port, so just use a random value.
1064 */
1065 src.st.st_port = ( random() | htons ( 1024 ) );
1066 if ( src_port )
1067 *src_port = ntohs ( src.st.st_port );
1068 }
1069
1070 /* Allocate I/O buffer */
1071 iobuf = xfer_alloc_iob ( &pxe->udp,
1072 ( *len + ( hdr_len ? *hdr_len : 0 ) ) );
1073 if ( ! iobuf ) {
1074 rc = -ENOMEM;
1075 goto err_alloc;
1076 }
1077
1078 /* Populate I/O buffer */
1079 if ( hdr_len )
1080 memcpy ( iob_put ( iobuf, *hdr_len ), hdr, *hdr_len );
1081 memcpy ( iob_put ( iobuf, *len ), data, *len );
1082
1083 /* Construct metadata */
1084 memset ( &meta, 0, sizeof ( meta ) );
1085 meta.src = &src.sa;
1086 meta.dest = &dest.sa;
1087 meta.netdev = pxe->netdev;
1088
1089 /* Deliver I/O buffer */
1090 if ( ( rc = xfer_deliver ( &pxe->udp, iob_disown ( iobuf ),
1091 &meta ) ) != 0 ) {
1092 DBGC ( pxe, "PXE %s could not transmit: %s\n",
1093 pxe->name, strerror ( rc ) );
1094 goto err_deliver;
1095 }
1096
1097 err_deliver:
1098 free_iob ( iobuf );
1099 err_alloc:
1100 efi_pxe_udp_schedule_close ( pxe );
1101 err_open:
1102 return EFIRC ( rc );
1103 }
1104
1105 /**
1106 * Receive UDP packet
1107 *
1108 * @v base PXE base code protocol
1109 * @v flags Operation flags
1110 * @v dest_ip Destination address
1111 * @v dest_port Destination port
1112 * @v src_ip Source address
1113 * @v src_port Source port
1114 * @v hdr_len Header length
1115 * @v hdr Header data
1116 * @v len Length
1117 * @v data Data
1118 * @ret efirc EFI status code
1119 */
1120 static EFI_STATUS EFIAPI
1121 efi_pxe_udp_read ( EFI_PXE_BASE_CODE_PROTOCOL *base, UINT16 flags,
1122 EFI_IP_ADDRESS *dest_ip,
1123 EFI_PXE_BASE_CODE_UDP_PORT *dest_port,
1124 EFI_IP_ADDRESS *src_ip,
1125 EFI_PXE_BASE_CODE_UDP_PORT *src_port,
1126 UINTN *hdr_len, VOID *hdr, UINTN *len, VOID *data ) {
1127 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
1128 struct io_buffer *iobuf;
1129 struct efi_pxe_udp_pseudo_header *pshdr;
1130 EFI_IP_ADDRESS *actual_dest_ip;
1131 EFI_IP_ADDRESS *actual_src_ip;
1132 size_t addr_len;
1133 size_t frag_len;
1134 int rc;
1135
1136 DBGC2 ( pxe, "PXE %s UDP READ ", pxe->name );
1137 if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER ) {
1138 DBGC2 ( pxe, "(filter)" );
1139 } else if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP ) {
1140 DBGC2 ( pxe, "*" );
1141 } else if ( dest_ip ) {
1142 DBGC2 ( pxe, "%s", efi_pxe_ip_ntoa ( pxe, dest_ip ) );
1143 }
1144 DBGC2 ( pxe, ":" );
1145 if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT ) {
1146 DBGC2 ( pxe, "*" );
1147 } else if ( dest_port ) {
1148 DBGC2 ( pxe, "%d", *dest_port );
1149 } else {
1150 DBGC2 ( pxe, "<NULL>" );
1151 }
1152 DBGC2 ( pxe, "<-" );
1153 if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP ) {
1154 DBGC2 ( pxe, "*" );
1155 } else if ( src_ip ) {
1156 DBGC2 ( pxe, "%s", efi_pxe_ip_ntoa ( pxe, src_ip ) );
1157 } else {
1158 DBGC2 ( pxe, "<NULL>" );
1159 }
1160 DBGC2 ( pxe, ":" );
1161 if ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT ) {
1162 DBGC2 ( pxe, "*" );
1163 } else if ( src_port ) {
1164 DBGC2 ( pxe, "%d", *src_port );
1165 } else {
1166 DBGC2 ( pxe, "<NULL>" );
1167 }
1168 if ( hdr_len )
1169 DBGC2 ( pxe, " %p+%zx", hdr, ( ( size_t ) *hdr_len ) );
1170 DBGC2 ( pxe, " %p+%zx\n", data, ( ( size_t ) *len ) );
1171
1172 /* Open UDP connection (if applicable) */
1173 if ( ( rc = efi_pxe_udp_open ( pxe ) ) != 0 )
1174 goto err_open;
1175
1176 /* Try receiving a packet, if the queue is empty */
1177 if ( list_empty ( &pxe->queue ) )
1178 step();
1179
1180 /* Remove first packet from the queue */
1181 iobuf = list_first_entry ( &pxe->queue, struct io_buffer, list );
1182 if ( ! iobuf ) {
1183 rc = -ETIMEDOUT; /* "no packet" */
1184 goto err_empty;
1185 }
1186 list_del ( &iobuf->list );
1187
1188 /* Strip pseudo-header */
1189 pshdr = iobuf->data;
1190 addr_len = ( pshdr->net->net_addr_len );
1191 iob_pull ( iobuf, sizeof ( *pshdr ) );
1192 actual_dest_ip = iobuf->data;
1193 iob_pull ( iobuf, addr_len );
1194 actual_src_ip = iobuf->data;
1195 iob_pull ( iobuf, addr_len );
1196 DBGC2 ( pxe, "PXE %s UDP RX %s:%d", pxe->name,
1197 pshdr->net->ntoa ( actual_dest_ip ), pshdr->dest_port );
1198 DBGC2 ( pxe, "<-%s:%d len %#zx\n", pshdr->net->ntoa ( actual_src_ip ),
1199 pshdr->src_port, iob_len ( iobuf ) );
1200
1201 /* Filter based on network-layer protocol */
1202 if ( pshdr->net != pxe->net ) {
1203 DBGC2 ( pxe, "PXE %s filtered out %s packet\n",
1204 pxe->name, pshdr->net->name );
1205 rc = -ETIMEDOUT; /* "no packet" */
1206 goto err_filter;
1207 }
1208
1209 /* Filter based on port numbers */
1210 if ( ! ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT ) ||
1211 ( dest_port && ( *dest_port == pshdr->dest_port ) ) ) ) {
1212 DBGC2 ( pxe, "PXE %s filtered out destination port %d\n",
1213 pxe->name, pshdr->dest_port );
1214 rc = -ETIMEDOUT; /* "no packet" */
1215 goto err_filter;
1216 }
1217 if ( ! ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT ) ||
1218 ( src_port && ( *src_port == pshdr->src_port ) ) ) ) {
1219 DBGC2 ( pxe, "PXE %s filtered out source port %d\n",
1220 pxe->name, pshdr->src_port );
1221 rc = -ETIMEDOUT; /* "no packet" */
1222 goto err_filter;
1223 }
1224
1225 /* Filter based on source IP address */
1226 if ( ! ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP ) ||
1227 ( src_ip &&
1228 ( memcmp ( src_ip, actual_src_ip, addr_len ) == 0 ) ) ) ) {
1229 DBGC2 ( pxe, "PXE %s filtered out source IP %s\n",
1230 pxe->name, pshdr->net->ntoa ( actual_src_ip ) );
1231 rc = -ETIMEDOUT; /* "no packet" */
1232 goto err_filter;
1233 }
1234
1235 /* Filter based on destination IP address */
1236 if ( ! ( ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER ) &&
1237 efi_pxe_ip_filter ( pxe, actual_dest_ip ) ) ||
1238 ( ( ! ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER ) ) &&
1239 ( ( flags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP ) ||
1240 ( dest_ip && ( memcmp ( dest_ip, actual_dest_ip,
1241 addr_len ) == 0 ) ) ) ) ) ) {
1242 DBGC2 ( pxe, "PXE %s filtered out destination IP %s\n",
1243 pxe->name, pshdr->net->ntoa ( actual_dest_ip ) );
1244 rc = -ETIMEDOUT; /* "no packet" */
1245 goto err_filter;
1246 }
1247
1248 /* Fill in addresses and port numbers */
1249 if ( dest_ip )
1250 memcpy ( dest_ip, actual_dest_ip, addr_len );
1251 if ( dest_port )
1252 *dest_port = pshdr->dest_port;
1253 if ( src_ip )
1254 memcpy ( src_ip, actual_src_ip, addr_len );
1255 if ( src_port )
1256 *src_port = pshdr->src_port;
1257
1258 /* Fill in header, if applicable */
1259 if ( hdr_len ) {
1260 frag_len = iob_len ( iobuf );
1261 if ( frag_len > *hdr_len )
1262 frag_len = *hdr_len;
1263 memcpy ( hdr, iobuf->data, frag_len );
1264 iob_pull ( iobuf, frag_len );
1265 *hdr_len = frag_len;
1266 }
1267
1268 /* Fill in data buffer */
1269 frag_len = iob_len ( iobuf );
1270 if ( frag_len > *len )
1271 frag_len = *len;
1272 memcpy ( data, iobuf->data, frag_len );
1273 iob_pull ( iobuf, frag_len );
1274 *len = frag_len;
1275
1276 /* Check for overflow */
1277 if ( iob_len ( iobuf ) ) {
1278 rc = -ERANGE;
1279 goto err_too_short;
1280 }
1281
1282 /* Success */
1283 rc = 0;
1284
1285 err_too_short:
1286 err_filter:
1287 free_iob ( iobuf );
1288 err_empty:
1289 efi_pxe_udp_schedule_close ( pxe );
1290 err_open:
1291 return EFIRC ( rc );
1292 }
1293
1294 /**
1295 * Set receive filter
1296 *
1297 * @v base PXE base code protocol
1298 * @v filter Receive filter
1299 * @ret efirc EFI status code
1300 */
1301 static EFI_STATUS EFIAPI
1302 efi_pxe_set_ip_filter ( EFI_PXE_BASE_CODE_PROTOCOL *base,
1303 EFI_PXE_BASE_CODE_IP_FILTER *filter ) {
1304 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
1305 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
1306 unsigned int i;
1307
1308 DBGC ( pxe, "PXE %s SET IP FILTER %02x",
1309 pxe->name, filter->Filters );
1310 for ( i = 0 ; i < filter->IpCnt ; i++ ) {
1311 DBGC ( pxe, " %s",
1312 efi_pxe_ip_ntoa ( pxe, &filter->IpList[i] ) );
1313 }
1314 DBGC ( pxe, "\n" );
1315
1316 /* Update filter */
1317 memcpy ( &mode->IpFilter, filter, sizeof ( mode->IpFilter ) );
1318
1319 return 0;
1320 }
1321
1322 /**
1323 * Resolve MAC address
1324 *
1325 * @v base PXE base code protocol
1326 * @v ip IP address
1327 * @v mac MAC address to fill in
1328 * @ret efirc EFI status code
1329 */
1330 static EFI_STATUS EFIAPI efi_pxe_arp ( EFI_PXE_BASE_CODE_PROTOCOL *base,
1331 EFI_IP_ADDRESS *ip,
1332 EFI_MAC_ADDRESS *mac ) {
1333 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
1334
1335 DBGC ( pxe, "PXE %s ARP %s %p\n",
1336 pxe->name, efi_pxe_ip_ntoa ( pxe, ip ), mac );
1337
1338 /* Not used by any bootstrap I can find to test with */
1339 return EFI_UNSUPPORTED;
1340 }
1341
1342 /**
1343 * Set parameters
1344 *
1345 * @v base PXE base code protocol
1346 * @v autoarp Automatic ARP packet generation
1347 * @v sendguid Send GUID as client hardware address
1348 * @v ttl IP time to live
1349 * @v tos IP type of service
1350 * @v callback Make callbacks
1351 * @ret efirc EFI status code
1352 */
1353 static EFI_STATUS EFIAPI
1354 efi_pxe_set_parameters ( EFI_PXE_BASE_CODE_PROTOCOL *base,
1355 BOOLEAN *autoarp, BOOLEAN *sendguid, UINT8 *ttl,
1356 UINT8 *tos, BOOLEAN *callback ) {
1357 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
1358 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
1359
1360 DBGC ( pxe, "PXE %s SET PARAMETERS", pxe->name );
1361 if ( autoarp )
1362 DBGC ( pxe, " %s", ( *autoarp ? "autoarp" : "noautoarp" ) );
1363 if ( sendguid )
1364 DBGC ( pxe, " %s", ( *sendguid ? "sendguid" : "sendmac" ) );
1365 if ( ttl )
1366 DBGC ( pxe, " ttl %d", *ttl );
1367 if ( tos )
1368 DBGC ( pxe, " tos %d", *tos );
1369 if ( callback ) {
1370 DBGC ( pxe, " %s",
1371 ( *callback ? "callback" : "nocallback" ) );
1372 }
1373 DBGC ( pxe, "\n" );
1374
1375 /* Update parameters */
1376 if ( autoarp )
1377 mode->AutoArp = *autoarp;
1378 if ( sendguid )
1379 mode->SendGUID = *sendguid;
1380 if ( ttl )
1381 mode->TTL = *ttl;
1382 if ( tos )
1383 mode->ToS = *tos;
1384 if ( callback )
1385 mode->MakeCallbacks = *callback;
1386
1387 return 0;
1388 }
1389
1390 /**
1391 * Set IP address
1392 *
1393 * @v base PXE base code protocol
1394 * @v ip IP address
1395 * @v netmask Subnet mask
1396 * @ret efirc EFI status code
1397 */
1398 static EFI_STATUS EFIAPI
1399 efi_pxe_set_station_ip ( EFI_PXE_BASE_CODE_PROTOCOL *base,
1400 EFI_IP_ADDRESS *ip, EFI_IP_ADDRESS *netmask ) {
1401 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
1402 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
1403
1404 DBGC ( pxe, "PXE %s SET STATION IP ", pxe->name );
1405 if ( ip )
1406 DBGC ( pxe, "%s", efi_pxe_ip_ntoa ( pxe, ip ) );
1407 if ( netmask )
1408 DBGC ( pxe, "/%s", efi_pxe_ip_ntoa ( pxe, netmask ) );
1409 DBGC ( pxe, "\n" );
1410
1411 /* Update IP address and netmask */
1412 if ( ip )
1413 memcpy ( &mode->StationIp, ip, sizeof ( mode->StationIp ) );
1414 if ( netmask )
1415 memcpy ( &mode->SubnetMask, netmask, sizeof (mode->SubnetMask));
1416
1417 return 0;
1418 }
1419
1420 /**
1421 * Update cached DHCP packets
1422 *
1423 * @v base PXE base code protocol
1424 * @v dhcpdisc_ok DHCPDISCOVER is valid
1425 * @v dhcpack_ok DHCPACK received
1426 * @v proxyoffer_ok ProxyDHCPOFFER received
1427 * @v pxebsdisc_ok PxeBsDISCOVER valid
1428 * @v pxebsack_ok PxeBsACK received
1429 * @v pxebsbis_ok PxeBsBIS received
1430 * @v dhcpdisc DHCPDISCOVER packet
1431 * @v dhcpack DHCPACK packet
1432 * @v proxyoffer ProxyDHCPOFFER packet
1433 * @v pxebsdisc PxeBsDISCOVER packet
1434 * @v pxebsack PxeBsACK packet
1435 * @v pxebsbis PxeBsBIS packet
1436 * @ret efirc EFI status code
1437 */
1438 static EFI_STATUS EFIAPI
1439 efi_pxe_set_packets ( EFI_PXE_BASE_CODE_PROTOCOL *base, BOOLEAN *dhcpdisc_ok,
1440 BOOLEAN *dhcpack_ok, BOOLEAN *proxyoffer_ok,
1441 BOOLEAN *pxebsdisc_ok, BOOLEAN *pxebsack_ok,
1442 BOOLEAN *pxebsbis_ok, EFI_PXE_BASE_CODE_PACKET *dhcpdisc,
1443 EFI_PXE_BASE_CODE_PACKET *dhcpack,
1444 EFI_PXE_BASE_CODE_PACKET *proxyoffer,
1445 EFI_PXE_BASE_CODE_PACKET *pxebsdisc,
1446 EFI_PXE_BASE_CODE_PACKET *pxebsack,
1447 EFI_PXE_BASE_CODE_PACKET *pxebsbis ) {
1448 struct efi_pxe *pxe = container_of ( base, struct efi_pxe, base );
1449 EFI_PXE_BASE_CODE_MODE *mode = &pxe->mode;
1450
1451 DBGC ( pxe, "PXE %s SET PACKETS\n", pxe->name );
1452
1453 /* Update fake packet flags */
1454 if ( dhcpdisc_ok )
1455 mode->DhcpDiscoverValid = *dhcpdisc_ok;
1456 if ( dhcpack_ok )
1457 mode->DhcpAckReceived = *dhcpack_ok;
1458 if ( proxyoffer_ok )
1459 mode->ProxyOfferReceived = *proxyoffer_ok;
1460 if ( pxebsdisc_ok )
1461 mode->PxeDiscoverValid = *pxebsdisc_ok;
1462 if ( pxebsack_ok )
1463 mode->PxeReplyReceived = *pxebsack_ok;
1464 if ( pxebsbis_ok )
1465 mode->PxeBisReplyReceived = *pxebsbis_ok;
1466
1467 /* Update fake packet contents */
1468 if ( dhcpdisc )
1469 memcpy ( &mode->DhcpDiscover, dhcpdisc, sizeof ( *dhcpdisc ) );
1470 if ( dhcpack )
1471 memcpy ( &mode->DhcpAck, dhcpack, sizeof ( *dhcpack ) );
1472 if ( proxyoffer )
1473 memcpy ( &mode->ProxyOffer, proxyoffer, sizeof ( *proxyoffer ));
1474 if ( pxebsdisc )
1475 memcpy ( &mode->PxeDiscover, pxebsdisc, sizeof ( *pxebsdisc ) );
1476 if ( pxebsack )
1477 memcpy ( &mode->PxeReply, pxebsack, sizeof ( *pxebsack ) );
1478 if ( pxebsbis )
1479 memcpy ( &mode->PxeBisReply, pxebsbis, sizeof ( *pxebsbis ) );
1480
1481 return 0;
1482 }
1483
1484 /** PXE base code protocol */
1485 static EFI_PXE_BASE_CODE_PROTOCOL efi_pxe_base_code_protocol = {
1486 .Revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION,
1487 .Start = efi_pxe_start,
1488 .Stop = efi_pxe_stop,
1489 .Dhcp = efi_pxe_dhcp,
1490 .Discover = efi_pxe_discover,
1491 .Mtftp = efi_pxe_mtftp,
1492 .UdpWrite = efi_pxe_udp_write,
1493 .UdpRead = efi_pxe_udp_read,
1494 .SetIpFilter = efi_pxe_set_ip_filter,
1495 .Arp = efi_pxe_arp,
1496 .SetParameters = efi_pxe_set_parameters,
1497 .SetStationIp = efi_pxe_set_station_ip,
1498 .SetPackets = efi_pxe_set_packets,
1499 };
1500
1501 /**
1502 * Install PXE base code protocol
1503 *
1504 * @v handle EFI handle
1505 * @v netdev Underlying network device
1506 * @ret rc Return status code
1507 */
1508 int efi_pxe_install ( EFI_HANDLE handle, struct net_device *netdev ) {
1509 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1510 struct tcpip_net_protocol *ipv6 = tcpip_net_protocol ( AF_INET6 );
1511 struct efi_pxe *pxe;
1512 struct in_addr ip;
1513 BOOLEAN use_ipv6;
1514 EFI_STATUS efirc;
1515 int rc;
1516
1517 /* Allocate and initialise structure */
1518 pxe = zalloc ( sizeof ( *pxe ) );
1519 if ( ! pxe ) {
1520 rc = -ENOMEM;
1521 goto err_alloc;
1522 }
1523 ref_init ( &pxe->refcnt, efi_pxe_free );
1524 pxe->netdev = netdev_get ( netdev );
1525 pxe->name = netdev->name;
1526 pxe->handle = handle;
1527 memcpy ( &pxe->base, &efi_pxe_base_code_protocol, sizeof ( pxe->base ));
1528 pxe->base.Mode = &pxe->mode;
1529 pxe->buf.op = &efi_pxe_buf_operations;
1530 intf_init ( &pxe->tftp, &efi_pxe_tftp_desc, &pxe->refcnt );
1531 intf_init ( &pxe->udp, &efi_pxe_udp_desc, &pxe->refcnt );
1532 INIT_LIST_HEAD ( &pxe->queue );
1533 process_init_stopped ( &pxe->process, &efi_pxe_process_desc,
1534 &pxe->refcnt );
1535
1536 /* Crude heuristic: assume that we prefer to use IPv4 if we
1537 * have an IPv4 address for the network device, otherwise
1538 * prefer IPv6 (if available).
1539 */
1540 fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting, &ip );
1541 use_ipv6 = ( ip.s_addr ? FALSE : ( ipv6 != NULL ) );
1542
1543 /* Start base code */
1544 efi_pxe_start ( &pxe->base, use_ipv6 );
1545
1546 /* Install PXE base code protocol */
1547 if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1548 &handle, &efi_pxe_base_code_protocol_guid, &pxe->base,
1549 NULL ) ) != 0 ) {
1550 rc = -EEFI ( efirc );
1551 DBGC ( pxe, "PXE %s could not install base code protocol: %s\n",
1552 pxe->name, strerror ( rc ) );
1553 goto err_install_protocol;
1554 }
1555
1556 /* Transfer reference to list and return */
1557 list_add_tail ( &pxe->list, &efi_pxes );
1558 DBGC ( pxe, "PXE %s installed for %s\n",
1559 pxe->name, efi_handle_name ( handle ) );
1560 return 0;
1561
1562 bs->UninstallMultipleProtocolInterfaces (
1563 handle, &efi_pxe_base_code_protocol_guid, &pxe->base,
1564 NULL );
1565 err_install_protocol:
1566 ref_put ( &pxe->refcnt );
1567 err_alloc:
1568 return rc;
1569 }
1570
1571 /**
1572 * Uninstall PXE base code protocol
1573 *
1574 * @v handle EFI handle
1575 */
1576 void efi_pxe_uninstall ( EFI_HANDLE handle ) {
1577 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1578 struct efi_pxe *pxe;
1579
1580 /* Locate PXE base code */
1581 pxe = efi_pxe_find ( handle );
1582 if ( ! handle ) {
1583 DBG ( "PXE could not find base code for %s\n",
1584 efi_handle_name ( handle ) );
1585 return;
1586 }
1587
1588 /* Stop base code */
1589 efi_pxe_stop ( &pxe->base );
1590
1591 /* Uninstall PXE base code protocol */
1592 bs->UninstallMultipleProtocolInterfaces (
1593 handle, &efi_pxe_base_code_protocol_guid, &pxe->base,
1594 NULL );
1595
1596 /* Remove from list and drop list's reference */
1597 list_del ( &pxe->list );
1598 ref_put ( &pxe->refcnt );
1599 }