1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
|
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2010,2011 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/net/netbuff.h>
#include <grub/dl.h>
#include <grub/net.h>
#include <grub/time.h>
#include <grub/efi/api.h>
#include <grub/efi/efi.h>
#include <grub/i18n.h>
#include <grub/net/netbuff.h>
GRUB_MOD_LICENSE ("GPLv3+");
/* GUID. */
static grub_efi_guid_t net_io_guid = GRUB_EFI_SIMPLE_NETWORK_GUID;
static grub_efi_guid_t pxe_io_guid = GRUB_EFI_PXE_GUID;
static grub_efi_guid_t ip4_config_guid = GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID;
static grub_efi_guid_t ip6_config_guid = GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID;
static grub_err_t
send_card_buffer (struct grub_net_card *dev,
struct grub_net_buff *pack)
{
grub_efi_status_t st;
grub_efi_simple_network_t *net = dev->efi_net;
grub_uint64_t limit_time = grub_get_time_ms () + 4000;
void *txbuf;
if (dev->txbusy)
while (1)
{
txbuf = NULL;
st = efi_call_3 (net->get_status, net, 0, &txbuf);
if (st != GRUB_EFI_SUCCESS)
return grub_error (GRUB_ERR_IO,
N_("couldn't send network packet"));
/*
Some buggy firmware could return an arbitrary address instead of the
txbuf address we trasmitted, so just check that txbuf is non NULL
for success. This is ok because we open the SNP protocol in
exclusive mode so we know we're the only ones transmitting on this
box and since we only transmit one packet at a time we know our
transmit was successfull.
*/
if (txbuf)
{
dev->txbusy = 0;
break;
}
if (limit_time < grub_get_time_ms ())
return grub_error (GRUB_ERR_TIMEOUT,
N_("couldn't send network packet"));
}
dev->last_pkt_size = (pack->tail - pack->data);
if (dev->last_pkt_size > dev->mtu)
dev->last_pkt_size = dev->mtu;
grub_memcpy (dev->txbuf, pack->data, dev->last_pkt_size);
st = efi_call_7 (net->transmit, net, 0, dev->last_pkt_size,
dev->txbuf, NULL, NULL, NULL);
if (st != GRUB_EFI_SUCCESS)
return grub_error (GRUB_ERR_IO, N_("couldn't send network packet"));
/*
The card may have sent out the packet immediately - set txbusy
to 0 in this case.
Cases were observed where checking txbuf at the next call
of send_card_buffer() is too late: 0 is returned in txbuf and
we run in the GRUB_ERR_TIMEOUT case above.
Perhaps a timeout in the FW has discarded the recycle buffer.
*/
txbuf = NULL;
st = efi_call_3 (net->get_status, net, 0, &txbuf);
dev->txbusy = !(st == GRUB_EFI_SUCCESS && txbuf);
return GRUB_ERR_NONE;
}
static struct grub_net_buff *
get_card_packet (struct grub_net_card *dev)
{
grub_efi_simple_network_t *net = dev->efi_net;
grub_err_t err;
grub_efi_status_t st;
grub_efi_uintn_t bufsize = dev->rcvbufsize;
struct grub_net_buff *nb;
int i;
for (i = 0; i < 2; i++)
{
if (!dev->rcvbuf)
dev->rcvbuf = grub_malloc (dev->rcvbufsize);
if (!dev->rcvbuf)
return NULL;
st = efi_call_7 (net->receive, net, NULL, &bufsize,
dev->rcvbuf, NULL, NULL, NULL);
if (st != GRUB_EFI_BUFFER_TOO_SMALL)
break;
dev->rcvbufsize = 2 * ALIGN_UP (dev->rcvbufsize > bufsize
? dev->rcvbufsize : bufsize, 64);
grub_free (dev->rcvbuf);
dev->rcvbuf = 0;
}
if (st != GRUB_EFI_SUCCESS)
return NULL;
nb = grub_netbuff_alloc (bufsize + 2);
if (!nb)
return NULL;
/* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
by 4. So that IP header is aligned on 4 bytes. */
if (grub_netbuff_reserve (nb, 2))
{
grub_netbuff_free (nb);
return NULL;
}
grub_memcpy (nb->data, dev->rcvbuf, bufsize);
err = grub_netbuff_put (nb, bufsize);
if (err)
{
grub_netbuff_free (nb);
return NULL;
}
return nb;
}
static grub_err_t
open_card (struct grub_net_card *dev)
{
grub_efi_simple_network_t *net;
/* Try to reopen SNP exlusively to close any active MNP protocol instance
that may compete for packet polling
*/
net = grub_efi_open_protocol (dev->efi_handle, &net_io_guid,
GRUB_EFI_OPEN_PROTOCOL_BY_EXCLUSIVE);
if (net)
{
if (net->mode->state == GRUB_EFI_NETWORK_STOPPED
&& efi_call_1 (net->start, net) != GRUB_EFI_SUCCESS)
return grub_error (GRUB_ERR_NET_NO_CARD, "%s: net start failed",
dev->name);
if (net->mode->state == GRUB_EFI_NETWORK_STOPPED)
return grub_error (GRUB_ERR_NET_NO_CARD, "%s: card stopped",
dev->name);
if (net->mode->state == GRUB_EFI_NETWORK_STARTED
&& efi_call_3 (net->initialize, net, 0, 0) != GRUB_EFI_SUCCESS)
return grub_error (GRUB_ERR_NET_NO_CARD, "%s: net initialize failed",
dev->name);
/* Enable hardware receive filters if driver declares support for it.
We need unicast and broadcast and additionaly all nodes and
solicited multicast for IPv6. Solicited multicast is per-IPv6
address and we currently do not have API to do it so simply
try to enable receive of all multicast packets or evertyhing in
the worst case (i386 PXE driver always enables promiscuous too).
This does trust firmware to do what it claims to do.
*/
if (net->mode->receive_filter_mask)
{
grub_uint32_t filters = GRUB_EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
GRUB_EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST |
GRUB_EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
filters &= net->mode->receive_filter_mask;
if (!(filters & GRUB_EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST))
filters |= (net->mode->receive_filter_mask &
GRUB_EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS);
efi_call_6 (net->receive_filters, net, filters, 0, 0, 0, NULL);
}
efi_call_4 (grub_efi_system_table->boot_services->close_protocol,
dev->efi_net, &net_io_guid,
grub_efi_image_handle, dev->efi_handle);
dev->efi_net = net;
}
/* If it failed we just try to run as best as we can */
return GRUB_ERR_NONE;
}
static void
close_card (struct grub_net_card *dev)
{
efi_call_1 (dev->efi_net->shutdown, dev->efi_net);
efi_call_1 (dev->efi_net->stop, dev->efi_net);
efi_call_4 (grub_efi_system_table->boot_services->close_protocol,
dev->efi_net, &net_io_guid,
grub_efi_image_handle, dev->efi_handle);
}
static struct grub_net_card_driver efidriver =
{
.name = "efinet",
.open = open_card,
.close = close_card,
.send = send_card_buffer,
.recv = get_card_packet
};
grub_efi_handle_t
grub_efinet_get_device_handle (struct grub_net_card *card)
{
if (!card || card->driver != &efidriver)
return 0;
return card->efi_handle;
}
static void
grub_efinet_findcards (void)
{
grub_efi_uintn_t num_handles;
grub_efi_handle_t *handles;
grub_efi_handle_t *handle;
int i = 0;
/* Find handles which support the disk io interface. */
handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &net_io_guid,
0, &num_handles);
if (! handles)
return;
for (handle = handles; num_handles--; handle++)
{
grub_efi_simple_network_t *net;
struct grub_net_card *card;
grub_efi_device_path_t *dp, *parent = NULL, *child = NULL;
/* EDK2 UEFI PXE driver creates IPv4 and IPv6 messaging devices as
children of main MAC messaging device. We only need one device with
bound SNP per physical card, otherwise they compete with each other
when polling for incoming packets.
*/
dp = grub_efi_get_device_path (*handle);
if (!dp)
continue;
for (; ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp); dp = GRUB_EFI_NEXT_DEVICE_PATH (dp))
{
parent = child;
child = dp;
}
if (child
&& GRUB_EFI_DEVICE_PATH_TYPE (child) == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
&& (GRUB_EFI_DEVICE_PATH_SUBTYPE (child) == GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
|| GRUB_EFI_DEVICE_PATH_SUBTYPE (child) == GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE)
&& parent
&& GRUB_EFI_DEVICE_PATH_TYPE (parent) == GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (parent) == GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE)
continue;
net = grub_efi_open_protocol (*handle, &net_io_guid,
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (! net)
/* This should not happen... Why? */
continue;
if (net->mode->state == GRUB_EFI_NETWORK_STOPPED
&& efi_call_1 (net->start, net) != GRUB_EFI_SUCCESS)
continue;
if (net->mode->state == GRUB_EFI_NETWORK_STOPPED)
continue;
if (net->mode->state == GRUB_EFI_NETWORK_STARTED
&& efi_call_3 (net->initialize, net, 0, 0) != GRUB_EFI_SUCCESS)
continue;
card = grub_zalloc (sizeof (struct grub_net_card));
if (!card)
{
grub_print_error ();
grub_free (handles);
return;
}
card->mtu = net->mode->max_packet_size;
card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
card->txbuf = grub_zalloc (card->txbufsize);
if (!card->txbuf)
{
grub_print_error ();
grub_free (handles);
grub_free (card);
return;
}
card->txbusy = 0;
card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
card->name = grub_xasprintf ("efinet%d", i++);
card->driver = &efidriver;
card->flags = 0;
card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
grub_memcpy (card->default_address.mac,
net->mode->current_address,
sizeof (card->default_address.mac));
card->efi_net = net;
card->efi_handle = *handle;
grub_net_card_register (card);
}
grub_free (handles);
}
static grub_efi_handle_t
grub_efi_locate_device_path (grub_efi_guid_t *protocol, grub_efi_device_path_t *device_path,
grub_efi_device_path_t **r_device_path)
{
grub_efi_handle_t handle;
grub_efi_status_t status;
status = efi_call_3 (grub_efi_system_table->boot_services->locate_device_path,
protocol, &device_path, &handle);
if (status != GRUB_EFI_SUCCESS)
return 0;
if (r_device_path)
*r_device_path = device_path;
return handle;
}
static grub_efi_ipv4_address_t *
grub_dns_server_ip4_address (grub_efi_device_path_t *dp, grub_efi_uintn_t *num_dns)
{
grub_efi_handle_t hnd;
grub_efi_status_t status;
grub_efi_ip4_config2_protocol_t *conf;
grub_efi_ipv4_address_t *addrs;
grub_efi_uintn_t data_size = 1 * sizeof (grub_efi_ipv4_address_t);
hnd = grub_efi_locate_device_path (&ip4_config_guid, dp, NULL);
if (!hnd)
return 0;
conf = grub_efi_open_protocol (hnd, &ip4_config_guid,
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (!conf)
return 0;
addrs = grub_malloc (data_size);
if (!addrs)
return 0;
status = efi_call_4 (conf->get_data, conf,
GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
&data_size, addrs);
if (status == GRUB_EFI_BUFFER_TOO_SMALL)
{
grub_free (addrs);
addrs = grub_malloc (data_size);
if (!addrs)
return 0;
status = efi_call_4 (conf->get_data, conf,
GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
&data_size, addrs);
}
if (status != GRUB_EFI_SUCCESS)
{
grub_free (addrs);
return 0;
}
*num_dns = data_size / sizeof (grub_efi_ipv4_address_t);
return addrs;
}
static grub_efi_ipv6_address_t *
grub_dns_server_ip6_address (grub_efi_device_path_t *dp, grub_efi_uintn_t *num_dns)
{
grub_efi_handle_t hnd;
grub_efi_status_t status;
grub_efi_ip6_config_protocol_t *conf;
grub_efi_ipv6_address_t *addrs;
grub_efi_uintn_t data_size = 1 * sizeof (grub_efi_ipv6_address_t);
hnd = grub_efi_locate_device_path (&ip6_config_guid, dp, NULL);
if (!hnd)
return 0;
conf = grub_efi_open_protocol (hnd, &ip6_config_guid,
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (!conf)
return 0;
addrs = grub_malloc (data_size);
if (!addrs)
return 0;
status = efi_call_4 (conf->get_data, conf,
GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
&data_size, addrs);
if (status == GRUB_EFI_BUFFER_TOO_SMALL)
{
grub_free (addrs);
addrs = grub_malloc (data_size);
if (!addrs)
return 0;
status = efi_call_4 (conf->get_data, conf,
GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
&data_size, addrs);
}
if (status != GRUB_EFI_SUCCESS)
{
grub_free (addrs);
return 0;
}
*num_dns = data_size / sizeof (grub_efi_ipv6_address_t);
return addrs;
}
static struct grub_net_buff *
grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *use_ipv6)
{
grub_efi_uint16_t uri_len;
grub_efi_device_path_t *ldp, *ddp;
grub_efi_uri_device_path_t *uri_dp;
struct grub_net_buff *nb;
grub_err_t err;
ddp = grub_efi_duplicate_device_path (dp);
ldp = grub_efi_find_last_device_path (ddp);
if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
|| GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE)
{
grub_free (ddp);
return NULL;
}
uri_len = GRUB_EFI_DEVICE_PATH_LENGTH (ldp) > 4 ? GRUB_EFI_DEVICE_PATH_LENGTH (ldp) - 4 : 0;
if (!uri_len)
{
grub_free (ddp);
return NULL;
}
uri_dp = (grub_efi_uri_device_path_t *) ldp;
ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
ldp->length = sizeof (*ldp);
ldp = grub_efi_find_last_device_path (ddp);
if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
|| (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE))
{
grub_free (ddp);
return NULL;
}
nb = grub_netbuff_alloc (512);
if (!nb)
return NULL;
if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE)
{
grub_efi_ipv4_device_path_t *ipv4 = (grub_efi_ipv4_device_path_t *) ldp;
struct grub_net_bootp_packet *bp;
grub_uint8_t *ptr;
grub_efi_ipv4_address_t *dns;
grub_efi_uintn_t num_dns;
bp = (struct grub_net_bootp_packet *) nb->tail;
err = grub_netbuff_put (nb, sizeof (*bp) + 4);
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
if (sizeof(bp->boot_file) < uri_len)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
grub_memcpy (bp->boot_file, uri_dp->uri, uri_len);
grub_memcpy (&bp->your_ip, ipv4->local_ip_address, sizeof (bp->your_ip));
grub_memcpy (&bp->server_ip, ipv4->remote_ip_address, sizeof (bp->server_ip));
bp->vendor[0] = GRUB_NET_BOOTP_RFC1048_MAGIC_0;
bp->vendor[1] = GRUB_NET_BOOTP_RFC1048_MAGIC_1;
bp->vendor[2] = GRUB_NET_BOOTP_RFC1048_MAGIC_2;
bp->vendor[3] = GRUB_NET_BOOTP_RFC1048_MAGIC_3;
ptr = nb->tail;
err = grub_netbuff_put (nb, sizeof (ipv4->subnet_mask) + 2);
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
*ptr++ = GRUB_NET_BOOTP_NETMASK;
*ptr++ = sizeof (ipv4->subnet_mask);
grub_memcpy (ptr, ipv4->subnet_mask, sizeof (ipv4->subnet_mask));
ptr = nb->tail;
err = grub_netbuff_put (nb, sizeof (ipv4->gateway_ip_address) + 2);
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
*ptr++ = GRUB_NET_BOOTP_ROUTER;
*ptr++ = sizeof (ipv4->gateway_ip_address);
grub_memcpy (ptr, ipv4->gateway_ip_address, sizeof (ipv4->gateway_ip_address));
ptr = nb->tail;
err = grub_netbuff_put (nb, sizeof ("HTTPClient") + 1);
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
*ptr++ = GRUB_NET_BOOTP_VENDOR_CLASS_IDENTIFIER;
*ptr++ = sizeof ("HTTPClient") - 1;
grub_memcpy (ptr, "HTTPClient", sizeof ("HTTPClient") - 1);
dns = grub_dns_server_ip4_address (dp, &num_dns);
if (dns)
{
grub_efi_uintn_t size_dns = sizeof (*dns) * num_dns;
ptr = nb->tail;
err = grub_netbuff_put (nb, size_dns + 2);
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
*ptr++ = GRUB_NET_BOOTP_DNS;
*ptr++ = size_dns;
grub_memcpy (ptr, dns, size_dns);
grub_free (dns);
}
ptr = nb->tail;
err = grub_netbuff_put (nb, 1);
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
*ptr = GRUB_NET_BOOTP_END;
*use_ipv6 = 0;
ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
ldp->length = sizeof (*ldp);
ldp = grub_efi_find_last_device_path (ddp);
if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE)
{
grub_efi_mac_address_device_path_t *mac = (grub_efi_mac_address_device_path_t *) ldp;
bp->hw_type = mac->if_type;
bp->hw_len = sizeof (bp->mac_addr);
grub_memcpy (bp->mac_addr, mac->mac_address, bp->hw_len);
}
}
else
{
grub_efi_ipv6_device_path_t *ipv6 = (grub_efi_ipv6_device_path_t *) ldp;
struct grub_net_dhcp6_packet *d6p;
struct grub_net_dhcp6_option *opt;
struct grub_net_dhcp6_option_iana *iana;
struct grub_net_dhcp6_option_iaaddr *iaaddr;
grub_efi_ipv6_address_t *dns;
grub_efi_uintn_t num_dns;
d6p = (struct grub_net_dhcp6_packet *)nb->tail;
err = grub_netbuff_put (nb, sizeof(*d6p));
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
d6p->message_type = GRUB_NET_DHCP6_REPLY;
opt = (struct grub_net_dhcp6_option *)nb->tail;
err = grub_netbuff_put (nb, sizeof(*opt));
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
opt->len = grub_cpu_to_be16_compile_time (sizeof(*iana) + sizeof(*opt) + sizeof(*iaaddr));
err = grub_netbuff_put (nb, sizeof(*iana));
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
opt = (struct grub_net_dhcp6_option *)nb->tail;
err = grub_netbuff_put (nb, sizeof(*opt));
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR);
opt->len = grub_cpu_to_be16_compile_time (sizeof (*iaaddr));
iaaddr = (struct grub_net_dhcp6_option_iaaddr *)nb->tail;
err = grub_netbuff_put (nb, sizeof(*iaaddr));
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
grub_memcpy (iaaddr->addr, ipv6->local_ip_address, sizeof(ipv6->local_ip_address));
opt = (struct grub_net_dhcp6_option *)nb->tail;
err = grub_netbuff_put (nb, sizeof(*opt) + uri_len);
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL);
opt->len = grub_cpu_to_be16 (uri_len);
grub_memcpy (opt->data, uri_dp->uri, uri_len);
dns = grub_dns_server_ip6_address (dp, &num_dns);
if (dns)
{
grub_efi_uintn_t size_dns = sizeof (*dns) * num_dns;
opt = (struct grub_net_dhcp6_option *)nb->tail;
err = grub_netbuff_put (nb, sizeof(*opt) + size_dns);
if (err)
{
grub_free (ddp);
grub_netbuff_free (nb);
return NULL;
}
opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS);
opt->len = grub_cpu_to_be16 (size_dns);
grub_memcpy (opt->data, dns, size_dns);
grub_free (dns);
}
*use_ipv6 = 1;
}
grub_free (ddp);
return nb;
}
static void
grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
char **path)
{
struct grub_net_card *card;
grub_efi_device_path_t *dp;
dp = grub_efi_get_device_path (hnd);
if (! dp)
return;
FOR_NET_CARDS (card)
{
grub_efi_device_path_t *cdp;
struct grub_efi_pxe *pxe;
struct grub_efi_pxe_mode *pxe_mode;
grub_uint8_t *packet_buf;
grub_size_t packet_bufsz ;
int ipv6;
struct grub_net_buff *nb = NULL;
if (card->driver != &efidriver)
continue;
cdp = grub_efi_get_device_path (card->efi_handle);
if (! cdp)
continue;
if (grub_efi_compare_device_paths (dp, cdp) != 0)
{
grub_efi_device_path_t *ldp, *dup_dp, *dup_ldp;
int match;
/* EDK2 UEFI PXE driver creates pseudo devices with type IPv4/IPv6
as children of Ethernet card and binds PXE and Load File protocols
to it. Loaded Image Device Path protocol will point to these pseudo
devices. We skip them when enumerating cards, so here we need to
find matching MAC device.
*/
ldp = grub_efi_find_last_device_path (dp);
if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
|| (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE
&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE))
continue;
dup_dp = grub_efi_duplicate_device_path (dp);
if (!dup_dp)
continue;
if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_URI_DEVICE_PATH_SUBTYPE)
{
dup_ldp = grub_efi_find_last_device_path (dup_dp);
dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
dup_ldp->length = sizeof (*dup_ldp);
}
dup_ldp = grub_efi_find_last_device_path (dup_dp);
dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
dup_ldp->length = sizeof (*dup_ldp);
match = grub_efi_compare_device_paths (dup_dp, cdp) == 0;
grub_free (dup_dp);
if (!match)
continue;
}
pxe = grub_efi_open_protocol (hnd, &pxe_io_guid,
GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (!pxe)
{
nb = grub_efinet_create_dhcp_ack_from_device_path (dp, &ipv6);
if (!nb)
{
grub_print_error ();
continue;
}
packet_buf = nb->head;
packet_bufsz = nb->tail - nb->head;
}
else
{
pxe_mode = pxe->mode;
packet_buf = (grub_uint8_t *) &pxe_mode->dhcp_ack;
packet_bufsz = sizeof (pxe_mode->dhcp_ack);
ipv6 = pxe_mode->using_ipv6;
}
if (ipv6)
{
grub_net_configure_by_dhcpv6_reply (card->name, card, 0,
(struct grub_net_dhcp6_packet *)
packet_buf,
packet_bufsz,
1, device, path);
if (grub_errno)
grub_print_error ();
}
else
{
grub_net_configure_by_dhcp_ack (card->name, card, 0,
(struct grub_net_bootp_packet *)
packet_buf,
packet_bufsz,
1, device, path);
}
if (nb)
grub_netbuff_free (nb);
return;
}
}
GRUB_MOD_INIT(efinet)
{
grub_efinet_findcards ();
grub_efi_net_config = grub_efi_net_config_real;
}
GRUB_MOD_FINI(efinet)
{
struct grub_net_card *card, *next;
FOR_NET_CARDS_SAFE (card, next)
if (card->driver == &efidriver)
grub_net_card_unregister (card);
}
|