#include <poll.h>
#include <string.h>
#include <unistd.h>
+#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/time.h>
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;
+}
#include <sys/wait.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
+#include <asm/byteorder.h>
#include <file/file.h>
#include <talloc/talloc.h>
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);
}
}
+ if (platform->ipmi)
+ get_ipmi_network_override(platform, config);
+
return 0;
}