/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2010,2011 Free Software Foundation, Inc. * * VAS_EBOOT 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. * * VAS_EBOOT 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 VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include #include #include struct VasEBoot_dhcp_discover_options { VasEBoot_uint8_t magic[4]; struct { VasEBoot_uint8_t code; VasEBoot_uint8_t len; VasEBoot_uint8_t data; } VAS_EBOOT_PACKED message_type; VasEBoot_uint8_t end; } VAS_EBOOT_PACKED; struct VasEBoot_dhcp_request_options { VasEBoot_uint8_t magic[4]; struct { VasEBoot_uint8_t code; VasEBoot_uint8_t len; VasEBoot_uint8_t data; } VAS_EBOOT_PACKED message_type; struct { VasEBoot_uint8_t type; VasEBoot_uint8_t len; VasEBoot_uint32_t data; } VAS_EBOOT_PACKED server_identifier; struct { VasEBoot_uint8_t type; VasEBoot_uint8_t len; VasEBoot_uint32_t data; } VAS_EBOOT_PACKED requested_ip; struct { VasEBoot_uint8_t type; VasEBoot_uint8_t len; VasEBoot_uint8_t data[7]; } VAS_EBOOT_PACKED parameter_request; VasEBoot_uint8_t end; } VAS_EBOOT_PACKED; enum { VAS_EBOOT_DHCP_OPT_OVERLOAD_FILE = 1, VAS_EBOOT_DHCP_OPT_OVERLOAD_SNAME = 2, }; enum { VAS_EBOOT_DHCP_MESSAGE_UNKNOWN, VAS_EBOOT_DHCP_MESSAGE_DISCOVER, VAS_EBOOT_DHCP_MESSAGE_OFFER, VAS_EBOOT_DHCP_MESSAGE_REQUEST, VAS_EBOOT_DHCP_MESSAGE_DECLINE, VAS_EBOOT_DHCP_MESSAGE_ACK, VAS_EBOOT_DHCP_MESSAGE_NAK, VAS_EBOOT_DHCP_MESSAGE_RELEASE, VAS_EBOOT_DHCP_MESSAGE_INFORM, }; #define VAS_EBOOT_BOOTP_MAX_OPTIONS_SIZE 64 /* Max timeout when waiting for BOOTP/DHCP reply */ #define VAS_EBOOT_DHCP_MAX_PACKET_TIMEOUT 32 #define VAS_EBOOT_BOOTP_MAX_OPTIONS_SIZE 64 /* Max timeout when waiting for BOOTP/DHCP reply */ #define VAS_EBOOT_DHCP_MAX_PACKET_TIMEOUT 32 static char hexdigit (VasEBoot_uint8_t val) { if (val < 10) return val + '0'; return val + 'a' - 10; } static const void * find_dhcp_option (const struct VasEBoot_net_bootp_packet *bp, VasEBoot_size_t size, VasEBoot_uint8_t opt_code, VasEBoot_uint8_t *opt_len) { const VasEBoot_uint8_t *ptr; VasEBoot_uint8_t overload = 0; int end = 0; VasEBoot_size_t i; if (opt_len) *opt_len = 0; /* Is the packet big enough to hold at least the magic cookie? */ if (size < sizeof (*bp) + sizeof (VasEBoot_uint32_t)) return NULL; /* * Pointer arithmetic to point behind the common stub packet, where * the options start. */ ptr = (VasEBoot_uint8_t *) (bp + 1); if (ptr[0] != VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_0 || ptr[1] != VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_1 || ptr[2] != VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_2 || ptr[3] != VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_3) return NULL; size -= sizeof (*bp); i = sizeof (VasEBoot_uint32_t); again: while (i < size) { VasEBoot_uint8_t tagtype; VasEBoot_uint8_t taglength; tagtype = ptr[i++]; /* Pad tag. */ if (tagtype == VAS_EBOOT_NET_BOOTP_PAD) continue; /* End tag. */ if (tagtype == VAS_EBOOT_NET_BOOTP_END) { end = 1; break; } if (i >= size) return NULL; taglength = ptr[i++]; if (i + taglength >= size) return NULL; VasEBoot_dprintf("net", "DHCP option %u (0x%02x) found with length %u.\n", tagtype, tagtype, taglength); /* FIXME RFC 3396 options concatentation */ if (tagtype == opt_code) { if (opt_len) *opt_len = taglength; return &ptr[i]; } if (tagtype == VAS_EBOOT_NET_DHCP_OVERLOAD && taglength == 1) overload = ptr[i]; i += taglength; } if (!end) return NULL; /* RFC2131, 4.1, 23ff: * If the options in a DHCP message extend into the 'sname' and 'file' * fields, the 'option overload' option MUST appear in the 'options' * field, with value 1, 2 or 3, as specified in RFC 1533. If the * 'option overload' option is present in the 'options' field, the * options in the 'options' field MUST be terminated by an 'end' option, * and MAY contain one or more 'pad' options to fill the options field. * The options in the 'sname' and 'file' fields (if in use as indicated * by the 'options overload' option) MUST begin with the first octet of * the field, MUST be terminated by an 'end' option, and MUST be * followed by 'pad' options to fill the remainder of the field. Any * individual option in the 'options', 'sname' and 'file' fields MUST be * entirely contained in that field. The options in the 'options' field * MUST be interpreted first, so that any 'option overload' options may * be interpreted. The 'file' field MUST be interpreted next (if the * 'option overload' option indicates that the 'file' field contains * DHCP options), followed by the 'sname' field. * * FIXME: We do not explicitly check for trailing 'pad' options here. */ end = 0; if (overload & VAS_EBOOT_DHCP_OPT_OVERLOAD_FILE) { overload &= ~VAS_EBOOT_DHCP_OPT_OVERLOAD_FILE; ptr = (VasEBoot_uint8_t *) &bp->boot_file[0]; size = sizeof (bp->boot_file); i = 0; goto again; } if (overload & VAS_EBOOT_DHCP_OPT_OVERLOAD_SNAME) { overload &= ~VAS_EBOOT_DHCP_OPT_OVERLOAD_SNAME; ptr = (VasEBoot_uint8_t *) &bp->server_name[0]; size = sizeof (bp->server_name); i = 0; goto again; } return NULL; } #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")]; const VasEBoot_uint8_t *opt; VasEBoot_uint8_t opt_len, overload = 0; const char *boot_file = 0, *server_name = 0; VasEBoot_size_t boot_file_len, server_name_len; addr.type = VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_IPV4; addr.ipv4 = bp->your_ip; addr.option = 0; 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 = VAS_EBOOT_NET_LINK_LEVEL_PROTOCOL_ETHERNET; inter = VasEBoot_net_add_addr (name, card, &addr, &hwaddr, flags); if (!inter) return 0; opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_DHCP_OVERLOAD, &opt_len); if (opt && opt_len == 1) overload = *opt; opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_DHCP_TFTP_SERVER_NAME, &opt_len); if (opt && opt_len) { server_name = (const char *) opt; server_name_len = opt_len; } else if (size > OFFSET_OF (server_name, bp) && !(overload & VAS_EBOOT_DHCP_OPT_OVERLOAD_SNAME) && bp->server_name[0]) { server_name = bp->server_name; server_name_len = sizeof (bp->server_name); } opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_DHCP_BOOTFILE_NAME, &opt_len); if (opt && opt_len) { boot_file = (const char *) opt; boot_file_len = opt_len; } else if (size > OFFSET_OF (boot_file, bp) && !(overload && VAS_EBOOT_DHCP_OPT_OVERLOAD_FILE) && bp->boot_file[0]) { boot_file = bp->boot_file; boot_file_len = 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 (server_name) { VasEBoot_env_set_net_property (name, "dhcp_server_name", server_name, server_name_len); if (is_def && !VasEBoot_net_default_server) { VasEBoot_net_default_server = VasEBoot_strdup (server_name); VasEBoot_print_error (); } if (device && !*device) { *device = VasEBoot_xasprintf ("tftp,%s", server_name); VasEBoot_print_error (); } } if (boot_file) { VasEBoot_env_set_net_property (name, "boot_file", boot_file, boot_file_len); if (path) { *path = VasEBoot_strndup (boot_file, boot_file_len); VasEBoot_print_error (); if (*path) { char *slash; slash = VasEBoot_strrchr (*path, '/'); if (slash) *slash = 0; else **path = 0; } } } opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_NETMASK, &opt_len); if (opt && opt_len == 4) { int i; for (i = 0; i < 32; i++) if (!(opt[i / 8] & (1 << (7 - (i % 8))))) break; mask = i; } VasEBoot_net_add_ipv4_local (inter, mask); /* We do not implement dead gateway detection and the first entry SHOULD be preferred one */ opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_ROUTER, &opt_len); if (opt && opt_len && !(opt_len & 3)) { VasEBoot_net_network_level_netaddress_t target; VasEBoot_net_network_level_address_t gw; char *rname; target.type = VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_IPV4; target.ipv4.base = 0; target.ipv4.masksize = 0; gw.type = VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_IPV4; gw.ipv4 = VasEBoot_get_unaligned32 (opt); rname = VasEBoot_xasprintf ("%s:default", name); if (rname) VasEBoot_net_add_route_gw (rname, target, gw, 0); VasEBoot_free (rname); } opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_DNS, &opt_len); if (opt && opt_len && !(opt_len & 3)) { int i; for (i = 0; i < opt_len / 4; i++) { struct VasEBoot_net_network_level_address s; s.type = VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_IPV4; s.ipv4 = VasEBoot_get_unaligned32 (opt); s.option = DNS_OPTION_PREFER_IPV4; VasEBoot_net_add_dns_server (&s); opt += 4; } } opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_HOSTNAME, &opt_len); if (opt && opt_len) VasEBoot_env_set_net_property (name, "hostname", (const char *) opt, opt_len); opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_DOMAIN, &opt_len); if (opt && opt_len) VasEBoot_env_set_net_property (name, "domain", (const char *) opt, opt_len); opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_ROOT_PATH, &opt_len); if (opt && opt_len) VasEBoot_env_set_net_property (name, "rootpath", (const char *) opt, opt_len); opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_EXTENSIONS_PATH, &opt_len); if (opt && opt_len) VasEBoot_env_set_net_property (name, "extensionspath", (const char *) opt, opt_len); opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_CLIENT_ID, &opt_len); if (opt && opt_len) VasEBoot_env_set_net_property (name, "clientid", (const char *) opt, opt_len); opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_BOOTP_CLIENT_UUID, &opt_len); if (opt && opt_len == 17) { /* The format is 9cfe245e-d0c8-bd45-a79f-54ea5fbd3d97 */ char *val; int i, j = 0; opt += 1; opt_len -= 1; val = VasEBoot_malloc (2 * opt_len + 4 + 1); if (!val) return inter; for (i = 0; i < opt_len; i++) { val[2 * i + j] = hexdigit (opt[i] >> 4); val[2 * i + 1 + j] = hexdigit (opt[i] & 0xf); if ((i == 3) || (i == 5) || (i == 7) || (i == 9)) { j++; val[2 * i + 1+ j] = '-'; } } VasEBoot_env_set_net_property (name, "clientuuid", (char *) val, 2 * opt_len + 4); VasEBoot_free (val); } inter->dhcp_ack = VasEBoot_malloc (size); if (inter->dhcp_ack) { VasEBoot_memcpy (inter->dhcp_ack, bp, size); inter->dhcp_acklen = size; } else VasEBoot_errno = VAS_EBOOT_ERR_NONE; return inter; } static VasEBoot_err_t send_dhcp_packet (struct VasEBoot_net_network_level_interface *iface) { VasEBoot_err_t err; struct VasEBoot_net_bootp_packet *pack; struct VasEBoot_datetime date; VasEBoot_int64_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; static struct VasEBoot_dhcp_discover_options discover_options = { { VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_0, VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_1, VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_2, VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_3, }, { VAS_EBOOT_NET_DHCP_MESSAGE_TYPE, sizeof (discover_options.message_type.data), VAS_EBOOT_DHCP_MESSAGE_DISCOVER, }, VAS_EBOOT_NET_BOOTP_END, }; static struct VasEBoot_dhcp_request_options request_options = { { VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_0, VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_1, VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_2, VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_3, }, { VAS_EBOOT_NET_DHCP_MESSAGE_TYPE, sizeof (request_options.message_type.data), VAS_EBOOT_DHCP_MESSAGE_REQUEST, }, { VAS_EBOOT_NET_DHCP_SERVER_IDENTIFIER, sizeof (request_options.server_identifier.data), 0, }, { VAS_EBOOT_NET_DHCP_REQUESTED_IP_ADDRESS, sizeof (request_options.requested_ip.data), 0, }, { VAS_EBOOT_NET_DHCP_PARAMETER_REQUEST_LIST, sizeof (request_options.parameter_request.data), { VAS_EBOOT_NET_BOOTP_NETMASK, VAS_EBOOT_NET_BOOTP_ROUTER, VAS_EBOOT_NET_BOOTP_DNS, VAS_EBOOT_NET_BOOTP_DOMAIN, VAS_EBOOT_NET_BOOTP_HOSTNAME, VAS_EBOOT_NET_BOOTP_ROOT_PATH, VAS_EBOOT_NET_BOOTP_EXTENSIONS_PATH, }, }, VAS_EBOOT_NET_BOOTP_END, }; COMPILE_TIME_ASSERT (sizeof (discover_options) <= VAS_EBOOT_BOOTP_MAX_OPTIONS_SIZE); COMPILE_TIME_ASSERT (sizeof (request_options) <= VAS_EBOOT_BOOTP_MAX_OPTIONS_SIZE); nb = VasEBoot_netbuff_alloc (sizeof (*pack) + VAS_EBOOT_BOOTP_MAX_OPTIONS_SIZE + 128); if (!nb) return VasEBoot_errno; err = VasEBoot_netbuff_reserve (nb, sizeof (*pack) + VAS_EBOOT_BOOTP_MAX_OPTIONS_SIZE + 128); if (err) goto out; err = VasEBoot_netbuff_push (nb, VAS_EBOOT_BOOTP_MAX_OPTIONS_SIZE); if (err) goto out; VasEBoot_memset (nb->data, 0, VAS_EBOOT_BOOTP_MAX_OPTIONS_SIZE); if (!iface->srv_id) { VasEBoot_memcpy (nb->data, &discover_options, sizeof (discover_options)); } else { struct VasEBoot_dhcp_request_options *ro = (struct VasEBoot_dhcp_request_options *) nb->data; VasEBoot_memcpy (nb->data, &request_options, sizeof (request_options)); /* my_ip and srv_id are stored in network order so do not need conversion. */ VasEBoot_set_unaligned32 (&ro->server_identifier.data, iface->srv_id); VasEBoot_set_unaligned32 (&ro->requested_ip.data, iface->my_ip); } err = VasEBoot_netbuff_push (nb, sizeof (*pack)); if (err) goto out; pack = (void *) nb->data; VasEBoot_memset (pack, 0, sizeof (*pack)); pack->opcode = 1; pack->hw_type = 1; pack->hw_len = 6; err = VasEBoot_get_datetime (&date); if (err || !VasEBoot_datetime2unixtime (&date, &t)) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; t = 0; } pack->seconds = VasEBoot_cpu_to_be16 (t); if (!iface->srv_id) iface->xid = pack->ident = VasEBoot_cpu_to_be32 (t); else pack->ident = iface->xid; VasEBoot_memcpy (&pack->mac_addr, &iface->hwaddress.mac, 6); err = VasEBoot_netbuff_push (nb, sizeof (*udph)); if (err) goto out; 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 = VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_IPV4; target.ipv4 = 0xffffffff; err = VasEBoot_net_link_layer_resolve (iface, &target, &ll_target); if (err) goto out; udph->chksum = VasEBoot_net_ip_transport_checksum (nb, VAS_EBOOT_NET_IP_UDP, &iface->address, &target); err = VasEBoot_net_send_ip_packet (iface, &target, &ll_target, nb, VAS_EBOOT_NET_IP_UDP); out: VasEBoot_netbuff_free (nb); return err; } /* * This is called directly from net/ip.c:handle_dgram(), because those * BOOTP/DHCP packets are a bit special due to their improper * sender/receiver IP fields. */ void VasEBoot_net_process_dhcp (struct VasEBoot_net_buff *nb, struct VasEBoot_net_network_level_interface *iface) { char *name; struct VasEBoot_net_card *card = iface->card; const struct VasEBoot_net_bootp_packet *bp = (const struct VasEBoot_net_bootp_packet *) nb->data; VasEBoot_size_t size = nb->tail - nb->data; const VasEBoot_uint8_t *opt; VasEBoot_uint8_t opt_len, type; VasEBoot_uint32_t srv_id = 0; opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_DHCP_MESSAGE_TYPE, &opt_len); if (opt && opt_len == 1) type = *opt; else type = VAS_EBOOT_DHCP_MESSAGE_UNKNOWN; opt = find_dhcp_option (bp, size, VAS_EBOOT_NET_DHCP_SERVER_IDENTIFIER, &opt_len); if (opt && opt_len == sizeof (srv_id)) srv_id = VasEBoot_get_unaligned32 (opt); /* * If we received BOOTP reply or DHCPACK, proceed with configuration. * Otherwise store offered address and server id for later processing * of DHCPACK. * xid and srv_id are stored in network order so do not need conversion. */ if ((!iface->srv_id && type == VAS_EBOOT_DHCP_MESSAGE_UNKNOWN) || (iface->srv_id && type == VAS_EBOOT_DHCP_MESSAGE_ACK && bp->ident == iface->xid && srv_id == iface->srv_id)) { name = VasEBoot_xasprintf ("%s:dhcp", card->name); if (!name) { VasEBoot_print_error (); return; } VasEBoot_net_configure_by_dhcp_ack (name, card, 0, bp, size, 0, 0, 0); VasEBoot_free (name); if (VasEBoot_errno) VasEBoot_print_error (); else VasEBoot_net_network_level_interface_unregister (iface); } else if (!iface->srv_id && type == VAS_EBOOT_DHCP_MESSAGE_OFFER && srv_id) { iface->srv_id = srv_id; iface->my_ip = bp->your_ip; /* Reset retransmission timer */ iface->dhcp_tmo = iface->dhcp_tmo_left = 1; } else if (iface->srv_id && type == VAS_EBOOT_DHCP_MESSAGE_NAK && bp->ident == iface->xid && srv_id == iface->srv_id) { iface->xid = iface->srv_id = iface->my_ip = 0; /* Reset retransmission timer */ iface->dhcp_tmo = iface->dhcp_tmo_left = 1; } } static VasEBoot_err_t VasEBoot_cmd_dhcpopt (struct VasEBoot_command *cmd __attribute__ ((unused)), int argc, char **args) { struct VasEBoot_net_network_level_interface *inter; unsigned num; const VasEBoot_uint8_t *ptr; VasEBoot_uint8_t taglength; VasEBoot_uint8_t len; if (argc < 4) return VasEBoot_error (VAS_EBOOT_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 (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("unrecognised network interface `%s'"), args[1]); if (!inter->dhcp_ack) return VasEBoot_error (VAS_EBOOT_ERR_IO, N_("no DHCP info found")); ptr = inter->dhcp_ack->vendor; /* This duplicates check in find_dhcp_option to preserve previous error return */ if (inter->dhcp_acklen < OFFSET_OF (vendor, inter->dhcp_ack) + sizeof (VasEBoot_uint32_t) || ptr[0] != VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_0 || ptr[1] != VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_1 || ptr[2] != VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_2 || ptr[3] != VAS_EBOOT_NET_BOOTP_RFC1048_MAGIC_3) return VasEBoot_error (VAS_EBOOT_ERR_IO, N_("no DHCP options found")); num = VasEBoot_strtoul (args[2], 0, 0); if (VasEBoot_errno) return VasEBoot_errno; /* Exclude PAD (0) and END (255) option codes */ if (num == 0 || num > 254) return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("invalid DHCP option code")); ptr = find_dhcp_option (inter->dhcp_ack, inter->dhcp_acklen, num, &taglength); if (!ptr) return VasEBoot_error (VAS_EBOOT_ERR_IO, N_("no DHCP option %u found"), num); if (VasEBoot_strcmp (args[3], "string") == 0) { VasEBoot_err_t err = VAS_EBOOT_ERR_NONE; char *val; if (VasEBoot_add (taglength, 1, &len)) return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("tag length overflow")); val = VasEBoot_malloc (len); 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 VAS_EBOOT_ERR_NONE; } if (VasEBoot_strcmp (args[3], "hex") == 0) { VasEBoot_err_t err = VAS_EBOOT_ERR_NONE; char *val; if (VasEBoot_mul (taglength, 2, &len) || VasEBoot_add (len, 1, &len)) return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("tag length overflow")); val = VasEBoot_malloc (len); 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 (VAS_EBOOT_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; VasEBoot_err_t err; unsigned i; FOR_NET_CARDS (card) { if (argc > 0 && VasEBoot_strcmp (card->name, args[0]) != 0) continue; ncards++; } if (ncards == 0) return VasEBoot_error (VAS_EBOOT_ERR_NET_NO_CARD, N_("no network card found")); ifaces = VasEBoot_calloc (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) { for (i = 0; i < j; i++) VasEBoot_free (ifaces[i].name); VasEBoot_free (ifaces); return VasEBoot_errno; } ifaces[j].address.type = VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV; VasEBoot_memcpy (&ifaces[j].hwaddress, &card->default_address, sizeof (ifaces[j].hwaddress)); ifaces[j].dhcp_tmo = ifaces[j].dhcp_tmo_left = 1; 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; /* * Running DHCP restransmission timer is kept per interface in dhcp_tmo_left. * When it runs off, dhcp_tmo is increased exponentionally and dhcp_tmo_left * initialized to it. Max value is 32 which gives approximately 12s total per * packet timeout assuming 200ms poll tick. Timeout is reset when DHCP OFFER * is received, so total timeout is 25s in the worst case. * * DHCP NAK also resets timer and transaction starts again. * * Total wait time is limited to ~25s to prevent endless loop in case of * permanent NAK */ for (i = 0; i < VAS_EBOOT_DHCP_MAX_PACKET_TIMEOUT * 4; i++) { int need_poll = 0; for (j = 0; j < ncards; j++) { if (!ifaces[j].prev || ifaces[j].dhcp_tmo > VAS_EBOOT_DHCP_MAX_PACKET_TIMEOUT) continue; if (--ifaces[j].dhcp_tmo_left) { need_poll = 1; continue; } ifaces[j].dhcp_tmo *= 2; if (ifaces[j].dhcp_tmo > VAS_EBOOT_DHCP_MAX_PACKET_TIMEOUT) continue; err = send_dhcp_packet (&ifaces[j]); if (err) { VasEBoot_print_error (); /* To ignore it during next poll */ ifaces[j].dhcp_tmo = VAS_EBOOT_DHCP_MAX_PACKET_TIMEOUT + 1; continue; } ifaces[j].dhcp_tmo_left = ifaces[j].dhcp_tmo; need_poll = 1; } if (!need_poll) break; VasEBoot_net_poll_cards (200, 0); } err = VAS_EBOOT_ERR_NONE; for (j = 0; j < ncards; j++) { if (!ifaces[j].prev) { VasEBoot_free (ifaces[j].name); continue; } VasEBoot_error_push (); VasEBoot_net_network_level_interface_unregister (&ifaces[j]); err = VasEBoot_error (VAS_EBOOT_ERR_FILE_NOT_FOUND, N_("couldn't autoconfigure %s"), ifaces[j].card->name); VasEBoot_free (ifaces[j].name); } VasEBoot_free (ifaces); return err; } static VasEBoot_command_t cmd_getdhcp, cmd_bootp, cmd_dhcp; void VasEBoot_bootp_init (void) { cmd_bootp = VasEBoot_register_command ("net_bootp", VasEBoot_cmd_bootp, N_("[CARD]"), N_("perform a bootp autoconfiguration")); cmd_dhcp = VasEBoot_register_command ("net_dhcp", VasEBoot_cmd_bootp, N_("[CARD]"), N_("perform a DHCP 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); VasEBoot_unregister_command (cmd_dhcp); }