/* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2010,2011 Free Software Foundation, Inc. * * VasEBoot 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. * * VasEBoot 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 VasEBoot. If not, see . */ #include #include #include #include #include #include #include #include #if !defined(VasEBoot_MACHINE_EFI) && (defined(__i386__) || defined(__x86_64__)) #define VasEBoot_NET_BOOTP_ARCH 0x0000 #elif defined(VasEBoot_MACHINE_EFI) && defined(__x86_64__) #define VasEBoot_NET_BOOTP_ARCH 0x0007 #elif defined(VasEBoot_MACHINE_EFI) && defined(__aarch64__) #define VasEBoot_NET_BOOTP_ARCH 0x000B #else #error "unknown bootp architecture" #endif static VasEBoot_uint8_t dhcp_option_header[] = {VasEBoot_NET_BOOTP_RFC1048_MAGIC_0, VasEBoot_NET_BOOTP_RFC1048_MAGIC_1, VasEBoot_NET_BOOTP_RFC1048_MAGIC_2, VasEBoot_NET_BOOTP_RFC1048_MAGIC_3}; static VasEBoot_uint8_t VasEBoot_userclass[] = {0x4D, 0x06, 0x05, 'G', 'R', 'U', 'B', '2'}; static VasEBoot_uint8_t VasEBoot_dhcpdiscover[] = {0x35, 0x01, 0x01}; static VasEBoot_uint8_t VasEBoot_dhcptime[] = {0x33, 0x04, 0x00, 0x00, 0x0e, 0x10}; static void parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask) { const VasEBoot_uint8_t *ptr, *ptr0; ptr = ptr0 = vend; if (ptr[0] != VasEBoot_NET_BOOTP_RFC1048_MAGIC_0 || ptr[1] != VasEBoot_NET_BOOTP_RFC1048_MAGIC_1 || ptr[2] != VasEBoot_NET_BOOTP_RFC1048_MAGIC_2 || ptr[3] != VasEBoot_NET_BOOTP_RFC1048_MAGIC_3) return; ptr = ptr + sizeof (VasEBoot_uint32_t); while (ptr - ptr0 < limit) { VasEBoot_uint8_t tagtype; VasEBoot_uint8_t taglength; tagtype = *ptr++; /* Pad tag. */ if (tagtype == VasEBoot_NET_BOOTP_PAD) continue; /* End tag. */ if (tagtype == VasEBoot_NET_BOOTP_END) return; taglength = *ptr++; switch (tagtype) { case VasEBoot_NET_BOOTP_NETMASK: if (taglength == 4) { int i; for (i = 0; i < 32; i++) if (!(ptr[i / 8] & (1 << (7 - (i % 8))))) break; *mask = i; } break; case VasEBoot_NET_BOOTP_ROUTER: if (taglength == 4) { VasEBoot_net_network_level_netaddress_t target; VasEBoot_net_network_level_address_t gw; char *rname; target.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4; target.ipv4.base = 0; target.ipv4.masksize = 0; gw.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4; VasEBoot_memcpy (&gw.ipv4, ptr, sizeof (gw.ipv4)); rname = VasEBoot_xasprintf ("%s:default", name); if (rname) VasEBoot_net_add_route_gw (rname, target, gw, NULL); VasEBoot_free (rname); } break; case VasEBoot_NET_BOOTP_DNS: { int i; for (i = 0; i < taglength / 4; i++) { struct VasEBoot_net_network_level_address s; s.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4; s.ipv4 = VasEBoot_get_unaligned32 (ptr); s.option = DNS_OPTION_PREFER_IPV4; VasEBoot_net_add_dns_server (&s); ptr += 4; } } continue; case VasEBoot_NET_BOOTP_HOSTNAME: VasEBoot_env_set_net_property (name, "hostname", (const char *) ptr, taglength); break; case VasEBoot_NET_BOOTP_DOMAIN: VasEBoot_env_set_net_property (name, "domain", (const char *) ptr, taglength); break; case VasEBoot_NET_BOOTP_ROOT_PATH: VasEBoot_env_set_net_property (name, "rootpath", (const char *) ptr, taglength); break; case VasEBoot_NET_BOOTP_EXTENSIONS_PATH: VasEBoot_env_set_net_property (name, "extensionspath", (const char *) ptr, taglength); break; /* If you need any other options please contact VasEBoot development team. */ } ptr += taglength; } } #define OFFSET_OF(x, y) ((VasEBoot_size_t)((VasEBoot_uint8_t *)((y)->x) - (VasEBoot_uint8_t *)(y))) struct VasEBoot_net_network_level_interface * VasEBoot_net_configure_by_dhcp_ack (const char *name, struct VasEBoot_net_card *card, VasEBoot_net_interface_flags_t flags, const struct VasEBoot_net_bootp_packet *bp, VasEBoot_size_t size, int is_def, char **device, char **path) { VasEBoot_net_network_level_address_t addr; VasEBoot_net_link_level_address_t hwaddr; struct VasEBoot_net_network_level_interface *inter; int mask = -1; char server_ip[sizeof ("xxx.xxx.xxx.xxx")]; addr.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4; addr.ipv4 = bp->your_ip; if (device) *device = 0; if (path) *path = 0; VasEBoot_memcpy (hwaddr.mac, bp->mac_addr, bp->hw_len < sizeof (hwaddr.mac) ? bp->hw_len : sizeof (hwaddr.mac)); hwaddr.type = VasEBoot_NET_LINK_LEVEL_PROTOCOL_ETHERNET; inter = VasEBoot_net_add_addr (name, card, &addr, &hwaddr, flags); if (!inter) return 0; #if 0 /* This is likely based on misunderstanding. gateway_ip refers to address of BOOTP relay and should not be used after BOOTP transaction is complete. See RFC1542, 3.4 Interpretation of the 'giaddr' field */ if (bp->gateway_ip) { VasEBoot_net_network_level_netaddress_t target; VasEBoot_net_network_level_address_t gw; char *rname; target.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4; target.ipv4.base = bp->server_ip; target.ipv4.masksize = 32; gw.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4; gw.ipv4 = bp->gateway_ip; rname = VasEBoot_xasprintf ("%s:gw", name); if (rname) VasEBoot_net_add_route_gw (rname, target, gw); VasEBoot_free (rname); target.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4; target.ipv4.base = bp->gateway_ip; target.ipv4.masksize = 32; VasEBoot_net_add_route (name, target, inter); } #endif if (size > OFFSET_OF (boot_file, bp)) VasEBoot_env_set_net_property (name, "boot_file", bp->boot_file, sizeof (bp->boot_file)); if (bp->server_ip) { VasEBoot_snprintf (server_ip, sizeof (server_ip), "%d.%d.%d.%d", ((VasEBoot_uint8_t *) &bp->server_ip)[0], ((VasEBoot_uint8_t *) &bp->server_ip)[1], ((VasEBoot_uint8_t *) &bp->server_ip)[2], ((VasEBoot_uint8_t *) &bp->server_ip)[3]); VasEBoot_env_set_net_property (name, "next_server", server_ip, sizeof (server_ip)); VasEBoot_print_error (); } if (is_def) VasEBoot_net_default_server = 0; if (is_def && !VasEBoot_net_default_server && bp->server_ip) { VasEBoot_net_default_server = VasEBoot_strdup (server_ip); VasEBoot_print_error (); } if (is_def) { VasEBoot_env_set ("net_default_interface", name); VasEBoot_env_export ("net_default_interface"); } if (device && !*device && bp->server_ip) { *device = VasEBoot_xasprintf ("tftp,%s", server_ip); VasEBoot_print_error (); } if (size > OFFSET_OF (server_name, bp) && bp->server_name[0]) { VasEBoot_env_set_net_property (name, "dhcp_server_name", bp->server_name, sizeof (bp->server_name)); if (is_def && !VasEBoot_net_default_server) { VasEBoot_net_default_server = VasEBoot_strdup (bp->server_name); VasEBoot_print_error (); } if (device && !*device) { *device = VasEBoot_xasprintf ("tftp,%s", bp->server_name); VasEBoot_print_error (); } } if (size > OFFSET_OF (boot_file, bp) && path) { *path = VasEBoot_strndup (bp->boot_file, sizeof (bp->boot_file)); VasEBoot_print_error (); if (*path) { char *slash; slash = VasEBoot_strrchr (*path, '/'); if (slash) *slash = 0; else **path = 0; } } if (size > OFFSET_OF (vendor, bp)) parse_dhcp_vendor (name, &bp->vendor, size - OFFSET_OF (vendor, bp), &mask); VasEBoot_net_add_ipv4_local (inter, mask); inter->dhcp_ack = VasEBoot_malloc (size); if (inter->dhcp_ack) { VasEBoot_memcpy (inter->dhcp_ack, bp, size); inter->dhcp_acklen = size; } else VasEBoot_errno = VasEBoot_ERR_NONE; return inter; } void VasEBoot_net_process_dhcp (struct VasEBoot_net_buff *nb, struct VasEBoot_net_card *card) { char *name; struct VasEBoot_net_network_level_interface *inf; name = VasEBoot_xasprintf ("%s:dhcp", card->name); if (!name) { VasEBoot_print_error (); return; } VasEBoot_net_configure_by_dhcp_ack (name, card, 0, (const struct VasEBoot_net_bootp_packet *) nb->data, (nb->tail - nb->data), 0, 0, 0); VasEBoot_free (name); if (VasEBoot_errno) VasEBoot_print_error (); else { FOR_NET_NETWORK_LEVEL_INTERFACES(inf) if (VasEBoot_memcmp (inf->name, card->name, VasEBoot_strlen (card->name)) == 0 && VasEBoot_memcmp (inf->name + VasEBoot_strlen (card->name), ":dhcp_tmp", sizeof (":dhcp_tmp") - 1) == 0) { VasEBoot_net_network_level_interface_unregister (inf); break; } } } static char hexdigit (VasEBoot_uint8_t val) { if (val < 10) return val + '0'; return val + 'a' - 10; } static VasEBoot_err_t VasEBoot_cmd_dhcpopt (struct VasEBoot_command *cmd __attribute__ ((unused)), int argc, char **args) { struct VasEBoot_net_network_level_interface *inter; int num; VasEBoot_uint8_t *ptr; VasEBoot_uint8_t taglength; if (argc < 4) return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("four arguments expected")); FOR_NET_NETWORK_LEVEL_INTERFACES (inter) if (VasEBoot_strcmp (inter->name, args[1]) == 0) break; if (!inter) return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("unrecognised network interface `%s'"), args[1]); if (!inter->dhcp_ack) return VasEBoot_error (VasEBoot_ERR_IO, N_("no DHCP info found")); if (inter->dhcp_acklen <= OFFSET_OF (vendor, inter->dhcp_ack)) return VasEBoot_error (VasEBoot_ERR_IO, N_("no DHCP options found")); num = VasEBoot_strtoul (args[2], 0, 0); if (VasEBoot_errno) return VasEBoot_errno; ptr = inter->dhcp_ack->vendor; if (ptr[0] != VasEBoot_NET_BOOTP_RFC1048_MAGIC_0 || ptr[1] != VasEBoot_NET_BOOTP_RFC1048_MAGIC_1 || ptr[2] != VasEBoot_NET_BOOTP_RFC1048_MAGIC_2 || ptr[3] != VasEBoot_NET_BOOTP_RFC1048_MAGIC_3) return VasEBoot_error (VasEBoot_ERR_IO, N_("no DHCP options found")); ptr = ptr + sizeof (VasEBoot_uint32_t); while (1) { VasEBoot_uint8_t tagtype; if (ptr >= ((VasEBoot_uint8_t *) inter->dhcp_ack) + inter->dhcp_acklen) return VasEBoot_error (VasEBoot_ERR_IO, N_("no DHCP option %d found"), num); tagtype = *ptr++; /* Pad tag. */ if (tagtype == 0) continue; /* End tag. */ if (tagtype == 0xff) return VasEBoot_error (VasEBoot_ERR_IO, N_("no DHCP option %d found"), num); taglength = *ptr++; if (tagtype == num) break; ptr += taglength; } if (VasEBoot_strcmp (args[3], "string") == 0) { VasEBoot_err_t err = VasEBoot_ERR_NONE; char *val = VasEBoot_malloc (taglength + 1); if (!val) return VasEBoot_errno; VasEBoot_memcpy (val, ptr, taglength); val[taglength] = 0; if (args[0][0] == '-' && args[0][1] == 0) VasEBoot_printf ("%s\n", val); else err = VasEBoot_env_set (args[0], val); VasEBoot_free (val); return err; } if (VasEBoot_strcmp (args[3], "number") == 0) { VasEBoot_uint64_t val = 0; int i; for (i = 0; i < taglength; i++) val = (val << 8) | ptr[i]; if (args[0][0] == '-' && args[0][1] == 0) VasEBoot_printf ("%llu\n", (unsigned long long) val); else { char valn[64]; VasEBoot_snprintf (valn, sizeof (valn), "%lld\n", (unsigned long long) val); return VasEBoot_env_set (args[0], valn); } return VasEBoot_ERR_NONE; } if (VasEBoot_strcmp (args[3], "hex") == 0) { VasEBoot_err_t err = VasEBoot_ERR_NONE; char *val = VasEBoot_malloc (2 * taglength + 1); int i; if (!val) return VasEBoot_errno; for (i = 0; i < taglength; i++) { val[2 * i] = hexdigit (ptr[i] >> 4); val[2 * i + 1] = hexdigit (ptr[i] & 0xf); } val[2 * taglength] = 0; if (args[0][0] == '-' && args[0][1] == 0) VasEBoot_printf ("%s\n", val); else err = VasEBoot_env_set (args[0], val); VasEBoot_free (val); return err; } return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("unrecognised DHCP option format specification `%s'"), args[3]); } /* FIXME: allow to specify mac address. */ static VasEBoot_err_t VasEBoot_cmd_bootp (struct VasEBoot_command *cmd __attribute__ ((unused)), int argc, char **args) { struct VasEBoot_net_card *card; struct VasEBoot_net_network_level_interface *ifaces; VasEBoot_size_t ncards = 0; unsigned j = 0; int interval; VasEBoot_err_t err; FOR_NET_CARDS (card) { if (argc > 0 && VasEBoot_strcmp (card->name, args[0]) != 0) continue; ncards++; } if (ncards == 0) return VasEBoot_error (VasEBoot_ERR_NET_NO_CARD, N_("no network card found")); ifaces = VasEBoot_zalloc (ncards * sizeof (ifaces[0])); if (!ifaces) return VasEBoot_errno; j = 0; FOR_NET_CARDS (card) { if (argc > 0 && VasEBoot_strcmp (card->name, args[0]) != 0) continue; ifaces[j].card = card; ifaces[j].next = &ifaces[j+1]; if (j) ifaces[j].prev = &ifaces[j-1].next; ifaces[j].name = VasEBoot_xasprintf ("%s:dhcp_tmp", card->name); card->num_ifaces++; if (!ifaces[j].name) { unsigned i; for (i = 0; i < j; i++) VasEBoot_free (ifaces[i].name); VasEBoot_free (ifaces); return VasEBoot_errno; } ifaces[j].address.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV; VasEBoot_memcpy (&ifaces[j].hwaddress, &card->default_address, sizeof (ifaces[j].hwaddress)); j++; } ifaces[ncards - 1].next = VasEBoot_net_network_level_interfaces; if (VasEBoot_net_network_level_interfaces) VasEBoot_net_network_level_interfaces->prev = & ifaces[ncards - 1].next; VasEBoot_net_network_level_interfaces = &ifaces[0]; ifaces[0].prev = &VasEBoot_net_network_level_interfaces; for (interval = 200; interval < 10000; interval *= 2) { int done = 0; for (j = 0; j < ncards; j++) { struct VasEBoot_net_bootp_packet *pack; struct VasEBoot_datetime date; VasEBoot_int32_t t = 0; struct VasEBoot_net_buff *nb; struct udphdr *udph; VasEBoot_net_network_level_address_t target; VasEBoot_net_link_level_address_t ll_target; VasEBoot_uint8_t *offset; if (!ifaces[j].prev) continue; nb = VasEBoot_netbuff_alloc (sizeof (*pack) + sizeof(dhcp_option_header) + sizeof(VasEBoot_userclass) + sizeof(VasEBoot_dhcpdiscover) + sizeof(VasEBoot_dhcptime) + 64 + 128); if (!nb) { VasEBoot_netbuff_free (nb); return VasEBoot_errno; } err = VasEBoot_netbuff_reserve (nb, sizeof (*pack) + 64 + 128); if (err) { VasEBoot_netbuff_free (nb); return err; } err = VasEBoot_netbuff_push (nb, sizeof (*pack) + 64); if (err) { VasEBoot_netbuff_free (nb); return err; } pack = (void *) nb->data; done = 1; VasEBoot_memset (pack, 0, sizeof (*pack) + 64); pack->opcode = 1; pack->hw_type = 1; pack->hw_len = 6; err = VasEBoot_get_datetime (&date); if (err || !VasEBoot_datetime2unixtime (&date, &t)) { VasEBoot_errno = VasEBoot_ERR_NONE; t = 0; } pack->ident = VasEBoot_cpu_to_be32 (t); pack->seconds = VasEBoot_cpu_to_be16 (t); VasEBoot_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, 6); offset = (VasEBoot_uint8_t *)&pack->vendor; VasEBoot_memcpy (offset, dhcp_option_header, sizeof(dhcp_option_header)); offset += sizeof(dhcp_option_header); VasEBoot_memcpy (offset, VasEBoot_dhcpdiscover, sizeof(VasEBoot_dhcpdiscover)); offset += sizeof(VasEBoot_dhcpdiscover); VasEBoot_memcpy (offset, VasEBoot_userclass, sizeof(VasEBoot_userclass)); offset += sizeof(VasEBoot_userclass); VasEBoot_memcpy (offset, VasEBoot_dhcptime, sizeof(VasEBoot_dhcptime)); /* insert Client System Architecture (option 93) */ offset += sizeof(VasEBoot_dhcptime); offset[0] = 93; offset[1] = 2; offset[2] = (VasEBoot_NET_BOOTP_ARCH >> 8); offset[3] = (VasEBoot_NET_BOOTP_ARCH & 0xFF); /* option terminator */ offset[4] = 255; VasEBoot_netbuff_push (nb, sizeof (*udph)); udph = (struct udphdr *) nb->data; udph->src = VasEBoot_cpu_to_be16_compile_time (68); udph->dst = VasEBoot_cpu_to_be16_compile_time (67); udph->chksum = 0; udph->len = VasEBoot_cpu_to_be16 (nb->tail - nb->data); target.type = VasEBoot_NET_NETWORK_LEVEL_PROTOCOL_IPV4; target.ipv4 = 0xffffffff; err = VasEBoot_net_link_layer_resolve (&ifaces[j], &target, &ll_target); if (err) return err; udph->chksum = VasEBoot_net_ip_transport_checksum (nb, VasEBoot_NET_IP_UDP, &ifaces[j].address, &target); err = VasEBoot_net_send_ip_packet (&ifaces[j], &target, &ll_target, nb, VasEBoot_NET_IP_UDP); VasEBoot_netbuff_free (nb); if (err) return err; } if (!done) break; VasEBoot_net_poll_cards (interval, 0); } err = VasEBoot_ERR_NONE; for (j = 0; j < ncards; j++) { VasEBoot_free (ifaces[j].name); if (!ifaces[j].prev) continue; VasEBoot_error_push (); VasEBoot_net_network_level_interface_unregister (&ifaces[j]); err = VasEBoot_error (VasEBoot_ERR_FILE_NOT_FOUND, N_("couldn't autoconfigure %s"), ifaces[j].card->name); } VasEBoot_free (ifaces); return err; } static VasEBoot_command_t cmd_getdhcp, cmd_bootp; void VasEBoot_bootp_init (void) { cmd_bootp = VasEBoot_register_command ("net_bootp", VasEBoot_cmd_bootp, N_("[CARD]"), N_("perform a bootp autoconfiguration")); cmd_getdhcp = VasEBoot_register_command ("net_get_dhcp_option", VasEBoot_cmd_dhcpopt, N_("VAR INTERFACE NUMBER DESCRIPTION"), N_("retrieve DHCP option and save it into VAR. If VAR is - then print the value.")); } void VasEBoot_bootp_fini (void) { VasEBoot_unregister_command (cmd_getdhcp); VasEBoot_unregister_command (cmd_bootp); }