From 1e1e3417262e44531d2a5c6dd3152d5deb92fb90 Mon Sep 17 00:00:00 2001 From: Sam Mendoza-Jonas Date: Mon, 15 Feb 2016 16:47:01 +1100 Subject: [PATCH] discover: Add support for IPMI network override On BMC platforms the 'Get System Boot Options' command can also be used to check for a temporary network interface config override. This is implemented via the optional 'OEM Parameters' field defined in the IPMI v2 spec. We define the actual format of the field as: - 4 byte cookie value - 2 byte version value - 1 byte hardware address size - 1 byte IP address size - Hardware address - 1 byte flags for 'ignore' and 'method' And for static configs: - IP Address - 1 byte subnet value - Gateway address If set the config override replaces any other interface config, forcing the use of the specified configuration. Signed-off-by: Sam Mendoza-Jonas --- discover/ipmi.c | 97 +++++++++++++++++++++++++++++++++++++ discover/ipmi.h | 4 ++ discover/platform-powerpc.c | 95 ++++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+) diff --git a/discover/ipmi.c b/discover/ipmi.c index f59974a..e52070f 100644 --- a/discover/ipmi.c +++ b/discover/ipmi.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -220,3 +221,99 @@ bool ipmi_present(void) return !access(ipmi_devnode, R_OK | W_OK); } +/* Reads and applies an IPMI interface config override, which closely follows + * the format of an interface config struct as described in lib/types */ +void parse_ipmi_interface_override(struct config *config, uint8_t *buf, + uint16_t len) +{ + struct interface_config *ifconf; + char *ipstr, *gatewaystr; + uint8_t hwsize, ipsize; + int addr_type, i = 0; + socklen_t addr_len; + + /* Get 1-byte hardware address size and ip address size */ + memcpy(&hwsize, &buf[i], sizeof(hwsize)); + i += sizeof(hwsize); + memcpy(&ipsize, &buf[i], sizeof(ipsize)); + i += sizeof(ipsize); + + if (!hwsize || !ipsize) { + pb_log("%s: Empty response\n", __func__); + return; + } + + /* At the moment only support 6-byte MAC addresses */ + if (hwsize != sizeof(ifconf->hwaddr)) { + pb_log("Unsupported HW address size in network override: %u\n", + hwsize); + return; + } + + /* Sanity check the IP address size */ + if (ipsize == 4) { + addr_type = AF_INET; + addr_len = INET_ADDRSTRLEN; + } else if (ipsize == 16) { + addr_type = AF_INET6; + addr_len = INET6_ADDRSTRLEN; + } else { + pb_log("Unsupported IP address size: %u\n", ipsize); + return; + } + + /* Everything past here is the interface config */ + ifconf = talloc_zero(config, struct interface_config); + if (!ifconf) { + pb_log("Failed to allocate network override\n"); + return; + } + + /* Hardware Address */ + memcpy(ifconf->hwaddr, &buf[i], hwsize); + i += hwsize; + + /* Check 1-byte ignore and method flags */ + ifconf->ignore = !!buf[i++]; + ifconf->method = !!buf[i++]; + + if (ifconf->method == CONFIG_METHOD_STATIC) { + if (ipsize + ipsize + 1 > len - i) { + pb_log("Expected data greater than buffer size\n"); + talloc_free(ifconf); + return; + } + + /* IP address */ + ipstr = talloc_array(ifconf, char, addr_len); + if (!inet_ntop(addr_type, &buf[i], ipstr, addr_len)) { + pb_log("Failed to convert ipaddr: %m\n"); + talloc_free(ifconf); + return; + } + i += ipsize; + + /* IP address subnet */ + ifconf->static_config.address = talloc_asprintf(ifconf, + "%s/%u", ipstr, buf[i]); + i++; + + /* Gateway address */ + gatewaystr = talloc_array(ifconf, char, addr_len); + if (!inet_ntop(addr_type, &buf[i], gatewaystr, addr_len)) { + pb_log("Failed to convert gateway: %m\n"); + talloc_free(ifconf); + return; + } + ifconf->static_config.gateway = gatewaystr; + i += ipsize; + } + + pb_log("Applying IPMI network config\n"); + + /* Replace any existing interface config */ + talloc_free(config->network.interfaces); + config->network.n_interfaces = 1; + config->network.interfaces = talloc(config, struct interface_config *); + config->network.interfaces[0] = ifconf; +} diff --git a/discover/ipmi.h b/discover/ipmi.h index 611cd49..85c91f4 100644 --- a/discover/ipmi.h +++ b/discover/ipmi.h @@ -38,4 +38,8 @@ int ipmi_transaction(struct ipmi *ipmi, uint8_t netfn, uint8_t cmd, uint8_t *resp_buf, uint16_t *resp_len, int timeout_ms); +void parse_ipmi_interface_override(struct config *config, uint8_t *buf, + uint16_t len); + + #endif /* _IPMI_H */ diff --git a/discover/platform-powerpc.c b/discover/platform-powerpc.c index 78b76d3..98deea5 100644 --- a/discover/platform-powerpc.c +++ b/discover/platform-powerpc.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -1113,6 +1114,97 @@ static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info) pb_log("Failed to retrieve Golden Device ID from IPMI\n"); } +static void get_ipmi_network_override(struct platform_powerpc *platform, + struct config *config) +{ + uint16_t min_len = 12, resp_len = 53, version; + const uint32_t magic_value = 0x21706221; + uint8_t resp[resp_len]; + uint32_t cookie; + int i, rc; + uint8_t req[] = { + 0x61, /* parameter selector: OEM section (network) */ + 0x00, /* no set selector */ + 0x00, /* no block selector */ + }; + + rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS, + IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS, + req, sizeof(req), + resp, &resp_len, + ipmi_timeout); + + pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len); + if (resp_len > 0) { + for (i = 0; i < resp_len; i++) { + pb_debug(" %02x", resp[i]); + if (i && (i + 1) % 16 == 0 && i != resp_len - 1) + pb_debug("\n"); + else if (i && (i + 1) % 8 == 0) + pb_debug(" "); + } + pb_debug("\n"); + } + + if (rc) { + pb_debug("IPMI network config option unavailable\n"); + return; + } + + if (resp_len < min_len) { + pb_debug("IPMI net response too small\n"); + return; + } + + if (resp[0] != 0) { + pb_log("platform: non-zero completion code %d from IPMI network req\n", + resp[0]); + return; + } + + /* Check for correct parameter version */ + if ((resp[1] & 0xf) != 0x1) { + pb_log("platform: unexpected version (0x%x) in network override response\n", + resp[0]); + return; + } + + /* Check for valid parameters. For now ignore the persistent flag */ + if (resp[2] & 0x80) { + pb_debug("platform: network override is invalid/locked\n"); + return; + } + + /* Check for valid parameters in the boot flags section, ignoring the + * persistent bit */ + if (!(resp[3] & 0x80)) { + pb_debug("platform: network override valid flag not set\n"); + return; + } + + /* Check 4-byte cookie value */ + i = 4; + memcpy(&cookie, &resp[i], sizeof(cookie)); + cookie = __be32_to_cpu(cookie); + if (cookie != magic_value) { + pb_log("%s: Incorrect cookie %x\n", __func__, cookie); + return; + } + i += sizeof(cookie); + + /* Check 2-byte version number */ + memcpy(&version, &resp[i], sizeof(version)); + version = __be16_to_cpu(version); + i += sizeof(version); + if (version != 1) { + pb_debug("Unexpected version: %u\n", version); + return; + } + + /* Interpret the rest of the interface config */ + parse_ipmi_interface_override(config, &resp[i], resp_len - i); +} + static int load_config(struct platform *p, struct config *config) { struct platform_powerpc *platform = to_platform_powerpc(p); @@ -1134,6 +1226,9 @@ static int load_config(struct platform *p, struct config *config) } } + if (platform->ipmi) + get_ipmi_network_override(platform, config); + return 0; } -- 2.39.5