/* * bmc_ssh_hil_sim.c — Multi-Station Protocol-Authentic Hardware-in-the-Loop (HIL) * BMC IPMI v2.0 RMCP+ (UDP :623, Cipher Suite 17 / 3 + 24-ch SDR) * & SSHv2 BIOS Serial Console (TCP :2200 + :22 Probe) Simulator * * Zero-shortcut wire-level compatibility with production `stl_native_server`: * - Real encrypted RMCP+ 2.0 sessions over UDP :623 (`libfreeipmi`) * - Real 24-slot Full SDR temperature records (`CPU0_DIMM_A0`..`CPU1_DIMM_L0`) * - Real SSHv2 PTY/Shell sessions over TCP :2200 (`libssh/server.h`) * - Real-time link fault injection (`SET_LINK`), AC power loss (`SET_POWER`), * 24-channel thermal sync (`SET_TEMPS`), and power-event streaming over TCP control port. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MAX_STATIONS 8 #define MAX_RMCP_SESSIONS 32 #define NUM_DIMMS 24 #define NUM_EXTRA_SENSORS 6 #define NUM_SDR_RECORDS (NUM_DIMMS + NUM_EXTRA_SENSORS) typedef struct { int in_use; uint32_t rc_session_id; uint32_t ms_session_id; uint8_t auth_alg; uint8_t integ_alg; uint8_t conf_alg; uint8_t max_priv; uint8_t lookup_flag; char username[32]; uint8_t username_len; uint8_t rc_rand[16]; uint8_t ms_rand[16]; uint8_t ms_guid[16]; uint8_t sik[64]; unsigned int sik_len; uint8_t k1[64]; unsigned int k1_len; uint8_t k2[64]; unsigned int k2_len; uint32_t out_seq; } RmcpSimSession; typedef struct { int idx; char ip[64]; int ipmi_port; int ssh_port; int probe22_port; int uart_bridge_port; volatile int bmc_up; volatile int serial_up; volatile int power_on; volatile int off_count; volatile int on_count; volatile int reset_count; volatile int cycle_count; volatile int warm_reset_count; volatile int ipmi_rx_packets; volatile int ssh_connect_count; volatile int ssh_active; volatile int kill_active_ssh; int dimm_present[NUM_DIMMS]; int dimm_temp_c[NUM_DIMMS]; uint8_t sdr_records[NUM_SDR_RECORDS][64]; unsigned int sdr_record_lens[NUM_SDR_RECORDS]; pthread_mutex_t mu; RmcpSimSession sessions[MAX_RMCP_SESSIONS]; uint32_t next_ms_sid; int uart_listen_fd; int uart_client_fd; pthread_mutex_t uart_mu; } SimStation; static SimStation g_stations[MAX_STATIONS]; static int g_num_stations = 0; static char g_hostkey_path[256] = ""; static volatile int g_running = 1; static uint8_t ipmb_checksum(const uint8_t *buf, size_t len) { uint8_t sum = 0; for (size_t i = 0; i < len; i++) sum = (uint8_t)(sum + buf[i]); return (uint8_t)(0x100U - sum); } static void build_station_sdr_repository(SimStation *st) { for (int i = 0; i < NUM_DIMMS; i++) { int cpu = i / 12; char ch = (char)('A' + (i % 12)); char name[32]; snprintf(name, sizeof(name), "CPU%d_DIMM_%c0", cpu, ch); size_t nlen = strlen(name); if (nlen > 16) nlen = 16; fiid_obj_t obj = fiid_obj_create(tmpl_sdr_full_sensor_record); fiid_obj_clear(obj); fiid_obj_set(obj, "record_id", (uint64_t)(i + 1)); fiid_obj_set(obj, "sdr_version_major", 1); fiid_obj_set(obj, "sdr_version_minor", 5); fiid_obj_set(obj, "record_type", IPMI_SDR_FORMAT_FULL_SENSOR_RECORD); fiid_obj_set(obj, "record_length", (uint64_t)(43 + nlen)); fiid_obj_set(obj, "sensor_owner_id.type", 0); fiid_obj_set(obj, "sensor_owner_id", 0x10); /* 0x20 >> 1 */ fiid_obj_set(obj, "sensor_owner_lun", 0); fiid_obj_set(obj, "sensor_owner_lun.reserved", 0); fiid_obj_set(obj, "channel_number", 0); fiid_obj_set(obj, "sensor_number", (uint64_t)(0x10 + i)); fiid_obj_set(obj, "entity_id", 32); /* Memory Device */ fiid_obj_set(obj, "entity_instance", (uint64_t)(i + 1)); fiid_obj_set(obj, "entity_instance.type", 0); fiid_obj_set(obj, "sensor_initialization.sensor_scanning", 1); fiid_obj_set(obj, "sensor_initialization.event_generation", 0); fiid_obj_set(obj, "sensor_initialization.init_sensor_type", 1); fiid_obj_set(obj, "sensor_initialization.init_hysteresis", 0); fiid_obj_set(obj, "sensor_initialization.init_thresholds", 1); fiid_obj_set(obj, "sensor_initialization.init_events", 0); fiid_obj_set(obj, "sensor_initialization.init_scanning", 1); fiid_obj_set(obj, "sensor_initialization.settable_sensor", 0); fiid_obj_set(obj, "sensor_capabilities.event_message_control_support", 3); fiid_obj_set(obj, "sensor_capabilities.threshold_access_support", 1); fiid_obj_set(obj, "sensor_capabilities.hysteresis_support", 0); fiid_obj_set(obj, "sensor_capabilities.auto_re_arm_support", 1); fiid_obj_set(obj, "sensor_capabilities.entity_ignore_support", 0); fiid_obj_set(obj, "sensor_type", 0x01); /* Temperature */ fiid_obj_set(obj, "event_reading_type_code", 0x01); /* Threshold */ fiid_obj_set(obj, "assertion_event_lower_threshold_reading_mask", 0); fiid_obj_set(obj, "deassertion_event_upper_threshold_reading_mask", 0); fiid_obj_set(obj, "discrete_reading_settable_threshold_readable_threshold_mask", 0); fiid_obj_set(obj, "sensor_unit1.percentage", 0); fiid_obj_set(obj, "sensor_unit1.modifier_unit", 0); fiid_obj_set(obj, "sensor_unit1.rate_unit", 0); fiid_obj_set(obj, "sensor_unit1.analog_data_format", 0); /* unsigned */ fiid_obj_set(obj, "sensor_unit2.base_unit", 1); /* Degrees C */ fiid_obj_set(obj, "sensor_unit3.modifier_unit", 0); fiid_obj_set(obj, "linearization", 0); fiid_obj_set(obj, "linearization.reserved", 0); fiid_obj_set(obj, "m_ls", 1); fiid_obj_set(obj, "tolerance", 0); fiid_obj_set(obj, "m_ms", 0); fiid_obj_set(obj, "b_ls", 0); fiid_obj_set(obj, "accuracy_ls", 0); fiid_obj_set(obj, "b_ms", 0); fiid_obj_set(obj, "sensor_direction", 0); fiid_obj_set(obj, "accuracy_exp", 0); fiid_obj_set(obj, "accuracy_ms", 0); fiid_obj_set(obj, "b_exponent", 0); fiid_obj_set(obj, "r_exponent", 0); fiid_obj_set(obj, "analog_characteristics_flag.nominal_reading", 1); fiid_obj_set(obj, "analog_characteristics_flag.normal_max", 1); fiid_obj_set(obj, "analog_characteristics_flag.normal_min", 1); fiid_obj_set(obj, "analog_characteristics_flag.reserved", 0); fiid_obj_set(obj, "nominal_reading", 45); fiid_obj_set(obj, "normal_maximum", 85); fiid_obj_set(obj, "normal_minimum", 10); fiid_obj_set(obj, "sensor_maximum_reading", 125); fiid_obj_set(obj, "sensor_minimum_reading", 0); fiid_obj_set(obj, "upper_non_recoverable_threshold", 105); fiid_obj_set(obj, "upper_critical_threshold", 95); fiid_obj_set(obj, "upper_non_critical_threshold", 85); fiid_obj_set(obj, "lower_non_recoverable_threshold", 0); fiid_obj_set(obj, "lower_critical_threshold", 0); fiid_obj_set(obj, "lower_non_critical_threshold", 5); fiid_obj_set(obj, "positive_going_threshold_hysteresis", 1); fiid_obj_set(obj, "negative_going_threshold_hysteresis", 1); fiid_obj_set(obj, "reserved", 0); fiid_obj_set(obj, "oem", 0); /* ASCII/Latin1 type (0xC0) | length */ fiid_obj_set(obj, "id_string_type_length_code", (uint64_t)(0xC0 | nlen)); fiid_obj_set_data(obj, "id_string", name, (unsigned int)nlen); int len = fiid_obj_get_all(obj, st->sdr_records[i], sizeof(st->sdr_records[i])); st->sdr_record_lens[i] = (len > 0) ? (unsigned int)len : 0; fiid_obj_destroy(obj); } /* Extra system sensors (Voltage, Fan, Power, Current) for full BMC telemetry verification */ struct { const char *name; uint8_t snum; uint8_t stype; uint8_t base_unit; uint16_t m; int8_t r_exp; uint8_t nominal; } extras[NUM_EXTRA_SENSORS] = { { "P12V", 0x30, 0x02, 4, 10, -2, 120 }, /* 12.00 V */ { "VDDQ_CPU0", 0x31, 0x02, 4, 10, -3, 110 }, /* 1.10 V */ { "FAN1_RPM", 0x32, 0x04, 18, 100, 0, 54 }, /* 5400 RPM */ { "FAN2_RPM", 0x33, 0x04, 18, 100, 0, 52 }, /* 5200 RPM */ { "PSU1_PIN_W", 0x34, 0x03, 6, 4, 0, 80 }, /* 320 W */ { "PSU1_IIN_A", 0x35, 0x03, 5, 10, -2, 145 }, /* 14.50 A */ }; for (int e = 0; e < NUM_EXTRA_SENSORS; e++) { int rec_idx = NUM_DIMMS + e; size_t nlen = strlen(extras[e].name); if (nlen > 16) nlen = 16; fiid_obj_t obj = fiid_obj_create(tmpl_sdr_full_sensor_record); fiid_obj_clear(obj); fiid_obj_set(obj, "record_id", (uint64_t)(rec_idx + 1)); fiid_obj_set(obj, "sdr_version_major", 1); fiid_obj_set(obj, "sdr_version_minor", 5); fiid_obj_set(obj, "record_type", IPMI_SDR_FORMAT_FULL_SENSOR_RECORD); fiid_obj_set(obj, "record_length", (uint64_t)(43 + nlen)); fiid_obj_set(obj, "sensor_owner_id.type", 0); fiid_obj_set(obj, "sensor_owner_id", 0x10); fiid_obj_set(obj, "sensor_owner_lun", 0); fiid_obj_set(obj, "sensor_owner_lun.reserved", 0); fiid_obj_set(obj, "channel_number", 0); fiid_obj_set(obj, "sensor_number", (uint64_t)extras[e].snum); fiid_obj_set(obj, "entity_id", 7); /* System Board */ fiid_obj_set(obj, "entity_instance", (uint64_t)(e + 1)); fiid_obj_set(obj, "entity_instance.type", 0); fiid_obj_set(obj, "sensor_initialization.sensor_scanning", 1); fiid_obj_set(obj, "sensor_initialization.event_generation", 0); fiid_obj_set(obj, "sensor_initialization.init_sensor_type", 1); fiid_obj_set(obj, "sensor_initialization.init_hysteresis", 0); fiid_obj_set(obj, "sensor_initialization.init_thresholds", 1); fiid_obj_set(obj, "sensor_initialization.init_events", 0); fiid_obj_set(obj, "sensor_initialization.init_scanning", 1); fiid_obj_set(obj, "sensor_initialization.settable_sensor", 0); fiid_obj_set(obj, "sensor_capabilities.event_message_control_support", 3); fiid_obj_set(obj, "sensor_capabilities.threshold_access_support", 1); fiid_obj_set(obj, "sensor_capabilities.hysteresis_support", 0); fiid_obj_set(obj, "sensor_capabilities.auto_re_arm_support", 1); fiid_obj_set(obj, "sensor_capabilities.entity_ignore_support", 0); fiid_obj_set(obj, "sensor_type", extras[e].stype); fiid_obj_set(obj, "event_reading_type_code", 0x01); fiid_obj_set(obj, "assertion_event_lower_threshold_reading_mask", 0); fiid_obj_set(obj, "deassertion_event_upper_threshold_reading_mask", 0); fiid_obj_set(obj, "discrete_reading_settable_threshold_readable_threshold_mask", 0); fiid_obj_set(obj, "sensor_unit1.percentage", 0); fiid_obj_set(obj, "sensor_unit1.modifier_unit", 0); fiid_obj_set(obj, "sensor_unit1.rate_unit", 0); fiid_obj_set(obj, "sensor_unit1.analog_data_format", 0); fiid_obj_set(obj, "sensor_unit2.base_unit", extras[e].base_unit); fiid_obj_set(obj, "sensor_unit3.modifier_unit", 0); fiid_obj_set(obj, "linearization", 0); fiid_obj_set(obj, "linearization.reserved", 0); fiid_obj_set(obj, "m_ls", extras[e].m & 0xFF); fiid_obj_set(obj, "tolerance", 0); fiid_obj_set(obj, "m_ms", (extras[e].m >> 8) & 0x03); fiid_obj_set(obj, "b_ls", 0); fiid_obj_set(obj, "accuracy_ls", 0); fiid_obj_set(obj, "b_ms", 0); fiid_obj_set(obj, "sensor_direction", 0); fiid_obj_set(obj, "accuracy_exp", 0); fiid_obj_set(obj, "accuracy_ms", 0); fiid_obj_set(obj, "b_exponent", 0); fiid_obj_set(obj, "r_exponent", (uint64_t)(extras[e].r_exp & 0x0F)); fiid_obj_set(obj, "analog_characteristics_flag.nominal_reading", 1); fiid_obj_set(obj, "analog_characteristics_flag.normal_max", 1); fiid_obj_set(obj, "analog_characteristics_flag.normal_min", 1); fiid_obj_set(obj, "analog_characteristics_flag.reserved", 0); fiid_obj_set(obj, "nominal_reading", extras[e].nominal); fiid_obj_set(obj, "normal_maximum", 240); fiid_obj_set(obj, "normal_minimum", 10); fiid_obj_set(obj, "sensor_maximum_reading", 255); fiid_obj_set(obj, "sensor_minimum_reading", 0); fiid_obj_set(obj, "upper_non_recoverable_threshold", 250); fiid_obj_set(obj, "upper_critical_threshold", 245); fiid_obj_set(obj, "upper_non_critical_threshold", 240); fiid_obj_set(obj, "lower_non_recoverable_threshold", 0); fiid_obj_set(obj, "lower_critical_threshold", 0); fiid_obj_set(obj, "lower_non_critical_threshold", 5); fiid_obj_set(obj, "positive_going_threshold_hysteresis", 1); fiid_obj_set(obj, "negative_going_threshold_hysteresis", 1); fiid_obj_set(obj, "reserved", 0); fiid_obj_set(obj, "oem", 0); fiid_obj_set(obj, "id_string_type_length_code", (uint64_t)(0xC0 | nlen)); fiid_obj_set_data(obj, "id_string", extras[e].name, (unsigned int)nlen); int len = fiid_obj_get_all(obj, st->sdr_records[rec_idx], sizeof(st->sdr_records[rec_idx])); st->sdr_record_lens[rec_idx] = (len > 0) ? (unsigned int)len : 0; fiid_obj_destroy(obj); } } static void notify_uart_power_event(SimStation *st, const char *ev_name) { pthread_mutex_lock(&st->uart_mu); if (st->uart_client_fd >= 0) { char msg[128]; int n = snprintf(msg, sizeof(msg), "\x01PWR:%s\n", ev_name); (void)send(st->uart_client_fd, msg, (size_t)n, MSG_NOSIGNAL); } pthread_mutex_unlock(&st->uart_mu); } static void compute_hmac( uint8_t auth_alg, const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *out, unsigned int *out_len) { const EVP_MD *md = (auth_alg == IPMI_AUTHENTICATION_ALGORITHM_RAKP_HMAC_SHA1) ? EVP_sha1() : EVP_sha256(); HMAC(md, key, (int)key_len, data, data_len, out, out_len); } static const char *lookup_password(const char *username) { (void)username; return "Admin123@pl"; } static void handle_ipmi_v15_auth_cap( int fd, const struct sockaddr_in *cli, socklen_t clilen, const uint8_t *pkt, ssize_t n) { /* Minimal IPMI v1.5 unauthenticated Get Channel Authentication Capabilities response */ if (n < 20) return; uint8_t rq_seq_lun = pkt[18]; uint8_t rq_seq = rq_seq_lun >> 2; uint8_t rq_addr = pkt[17]; uint8_t rs_addr = pkt[14]; uint8_t resp[32]; memset(resp, 0, sizeof(resp)); /* RMCP header */ resp[0] = 0x06; resp[1] = 0x00; resp[2] = 0xFF; resp[3] = 0x07; /* v1.5 session header (unauthenticated, seq=0, sid=0, msg_len=16) */ resp[4] = 0x00; resp[13] = 16; /* IPMB response header: rqAddr, (netFn=0x07<<2)|0, csum1, rsAddr, (rqSeq<<2)|0, cmd=0x38 */ resp[14] = rq_addr; resp[15] = (0x07 << 2); resp[16] = ipmb_checksum(resp + 14, 2); resp[17] = rs_addr; resp[18] = (uint8_t)(rq_seq << 2); resp[19] = 0x38; resp[20] = 0x00; /* Completion Code OK */ resp[21] = 0x01; /* Channel 1 */ resp[22] = 0x84; /* v2.0+ extended capabilities (0x80) | password (0x04) */ resp[23] = 0x14; /* non-null user, per-msg auth */ resp[24] = 0x02; /* IPMI v2.0 + v1.5 supported */ resp[25] = 0x00; resp[26] = 0x00; resp[27] = 0x00; resp[28] = 0x00; resp[29] = ipmb_checksum(resp + 17, 12); (void)sendto(fd, resp, 30, 0, (const struct sockaddr *)cli, clilen); } static size_t handle_ipmb_command( SimStation *st, RmcpSimSession *sess, uint8_t net_fn, uint8_t cmd, const uint8_t *req_data, size_t req_len, uint8_t *rs_data) { /* rs_data[0] = completion code, followed by response payload */ if (net_fn == 0x06 && cmd == 0x01) { /* Get Device ID */ rs_data[0] = 0x00; rs_data[1] = 0x20; /* Device ID */ rs_data[2] = 0x01; /* Device Revision */ rs_data[3] = 0x02; /* FW Major */ rs_data[4] = 0x19; /* FW Minor */ rs_data[5] = 0x02; /* IPMI v2.0 */ rs_data[6] = 0xBF; /* Sensor, SDR, SEL, FRU, Chassis */ rs_data[7] = 0x57; rs_data[8] = 0x01; rs_data[9] = 0x00; rs_data[10] = 0x01; rs_data[11] = 0x00; rs_data[12] = 0x00; rs_data[13] = 0x00; rs_data[14] = 0x00; rs_data[15] = 0x00; return 16; } if (net_fn == 0x06 && cmd == 0x3B) { /* Set Session Privilege Level */ uint8_t req_priv = (req_len >= 1) ? (req_data[0] & 0x0F) : 0x04; if (req_priv == 0) req_priv = sess ? sess->max_priv : 0x04; rs_data[0] = 0x00; rs_data[1] = req_priv; return 2; } if (net_fn == 0x06 && cmd == 0x3C) { /* Close Session */ if (sess) sess->in_use = 0; rs_data[0] = 0x00; return 1; } if (net_fn == 0x06 && cmd == 0x02) { /* Warm Reset */ st->warm_reset_count++; rs_data[0] = 0x00; return 1; } if (net_fn == 0x00 && cmd == 0x01) { /* Get Chassis Status */ rs_data[0] = 0x00; rs_data[1] = st->power_on ? 0x21 : 0x20; rs_data[2] = 0x00; rs_data[3] = 0x40; rs_data[4] = 0x00; return 5; } if (net_fn == 0x00 && cmd == 0x02) { /* Chassis Control */ uint8_t op = (req_len >= 1) ? (req_data[0] & 0x0F) : 0xFF; if (op == 0x00) { st->power_on = 0; st->off_count++; notify_uart_power_event(st, "OFF"); } else if (op == 0x01) { st->power_on = 1; st->on_count++; notify_uart_power_event(st, "ON"); } else if (op == 0x02) { st->power_on = 1; st->cycle_count++; notify_uart_power_event(st, "CYCLE"); } else if (op == 0x03) { st->power_on = 1; st->reset_count++; notify_uart_power_event(st, "RESET"); } rs_data[0] = 0x00; return 1; } if (net_fn == 0x0A && cmd == 0x20) { /* Get SDR Repository Info */ rs_data[0] = 0x00; rs_data[1] = 0x51; /* SDR version 1.5 */ rs_data[2] = NUM_SDR_RECORDS & 0xFF; rs_data[3] = (NUM_SDR_RECORDS >> 8) & 0xFF; rs_data[4] = 0x00; rs_data[5] = 0x10; /* Free space */ /* Fixed timestamps so SDR cache stays valid */ rs_data[6] = 0x02; rs_data[7] = 0x00; rs_data[8] = 0x00; rs_data[9] = 0x68; rs_data[10] = 0x02; rs_data[11] = 0x00; rs_data[12] = 0x00; rs_data[13] = 0x68; rs_data[14] = 0x02; /* Reserve SDR supported */ return 15; } if (net_fn == 0x0A && cmd == 0x22) { /* Reserve SDR Repository */ rs_data[0] = 0x00; rs_data[1] = 0x34; rs_data[2] = 0x12; return 3; } if (net_fn == 0x0A && cmd == 0x23) { /* Get SDR: [res_ls, res_ms, rec_ls, rec_ms, offset, bytes_to_read] */ if (req_len < 6) { rs_data[0] = 0xC7; return 1; } uint16_t rec_id = (uint16_t)(req_data[2] | (req_data[3] << 8)); uint8_t offset = req_data[4]; uint8_t to_read = req_data[5]; int idx = (rec_id == 0x0000) ? 0 : ((int)rec_id - 1); if (idx < 0 || idx >= NUM_SDR_RECORDS) { rs_data[0] = 0xCB; /* Requested sensor/record not present */ return 1; } unsigned int rlen = st->sdr_record_lens[idx]; if (offset >= rlen) { rs_data[0] = 0xC8; return 1; } unsigned int avail = rlen - offset; unsigned int copy_n = (to_read == 0xFF || to_read > avail) ? avail : to_read; uint16_t next_id = (idx + 1 < NUM_SDR_RECORDS) ? (uint16_t)(idx + 2) : 0xFFFF; rs_data[0] = 0x00; rs_data[1] = (uint8_t)(next_id & 0xFF); rs_data[2] = (uint8_t)((next_id >> 8) & 0xFF); memcpy(rs_data + 3, st->sdr_records[idx] + offset, copy_n); return 3 + copy_n; } if (net_fn == 0x04 && cmd == 0x2D) { /* Get Sensor Reading */ if (req_len < 1) { rs_data[0] = 0xC7; return 1; } uint8_t raw_snum = req_data[0]; if (raw_snum >= 0x30 && raw_snum < 0x30 + NUM_EXTRA_SENSORS) { static const uint8_t extra_vals[NUM_EXTRA_SENSORS] = { 120, 110, 54, 52, 80, 145 }; int eidx = (int)(raw_snum - 0x30); if (!st->power_on) { rs_data[0] = 0x00; rs_data[1] = 0x00; rs_data[2] = 0x20; rs_data[3] = 0x00; return 4; } rs_data[0] = 0x00; rs_data[1] = extra_vals[eidx]; rs_data[2] = 0x40; rs_data[3] = 0xC0; return 4; } int snum = (int)raw_snum - 0x10; if (snum < 0 || snum >= NUM_DIMMS) { rs_data[0] = 0xCB; return 1; } if (!st->power_on || !st->dimm_present[snum]) { /* Scanning disabled / reading unavailable when powered off or absent */ rs_data[0] = 0x00; rs_data[1] = 0x00; rs_data[2] = 0x20; /* bit 5 = reading/state unavailable */ rs_data[3] = 0x00; return 4; } rs_data[0] = 0x00; rs_data[1] = (uint8_t)(st->dimm_temp_c[snum] & 0xFF); rs_data[2] = 0x40; /* bit 6 = sensor scanning enabled, bit 5 = 0 (valid!) */ rs_data[3] = 0xC0; return 4; } if (net_fn == 0x04 && cmd == 0x2F) { /* Get Sensor Type */ rs_data[0] = 0x00; rs_data[1] = 0x01; rs_data[2] = 0x01; return 3; } /* Generic OEM / raw fallback (fan control, SEL info, etc.) */ rs_data[0] = 0x00; rs_data[1] = 0x00; return 2; } static void handle_rmcpplus_packet( SimStation *st, int fd, const struct sockaddr_in *cli, socklen_t clilen, const uint8_t *pkt, ssize_t n) { if (n < 16) return; uint8_t ptype_raw = pkt[5]; uint8_t ptype = ptype_raw & 0x3F; uint32_t sid = (uint32_t)pkt[6] | ((uint32_t)pkt[7] << 8) | ((uint32_t)pkt[8] << 16) | ((uint32_t)pkt[9] << 24); uint16_t plen = (uint16_t)pkt[14] | ((uint16_t)pkt[15] << 8); if (16 + (ssize_t)plen > n) return; const uint8_t *pl = pkt + 16; pthread_mutex_lock(&st->mu); if (ptype == IPMI_PAYLOAD_TYPE_RMCPPLUS_OPEN_SESSION_REQUEST && plen >= 32) { uint8_t tag = pl[0]; uint8_t req_priv = pl[1] & 0x0F; if (req_priv == 0) req_priv = 0x04; uint32_t rc_sid = (uint32_t)pl[4] | ((uint32_t)pl[5] << 8) | ((uint32_t)pl[6] << 16) | ((uint32_t)pl[7] << 24); uint8_t auth_alg = pl[12] & 0x3F; uint8_t integ_alg = pl[20] & 0x3F; uint8_t conf_alg = pl[28] & 0x3F; int slot_i = 0; for (int i = 0; i < MAX_RMCP_SESSIONS; i++) { if (!st->sessions[i].in_use) { slot_i = i; break; } } RmcpSimSession *s = &st->sessions[slot_i]; memset(s, 0, sizeof(*s)); s->in_use = 1; s->rc_session_id = rc_sid; s->ms_session_id = ++st->next_ms_sid; if (s->ms_session_id == 0) s->ms_session_id = ++st->next_ms_sid; s->auth_alg = auth_alg; s->integ_alg = integ_alg; s->conf_alg = conf_alg; s->max_priv = req_priv; s->out_seq = 1; uint8_t resp[16 + 36]; memset(resp, 0, sizeof(resp)); resp[0] = 0x06; resp[1] = 0x00; resp[2] = 0xFF; resp[3] = 0x07; resp[4] = 0x06; /* RMCP+ */ resp[5] = IPMI_PAYLOAD_TYPE_RMCPPLUS_OPEN_SESSION_RESPONSE; resp[14] = 36; resp[15] = 0; uint8_t *rp = resp + 16; rp[0] = tag; rp[1] = 0x00; /* status OK */ rp[2] = req_priv; rp[3] = 0x00; memcpy(rp + 4, &rc_sid, 4); memcpy(rp + 8, &s->ms_session_id, 4); rp[12] = 0x00; rp[15] = 0x08; rp[16] = auth_alg; rp[20] = 0x01; rp[23] = 0x08; rp[24] = integ_alg; rp[28] = 0x02; rp[31] = 0x08; rp[32] = conf_alg; pthread_mutex_unlock(&st->mu); (void)sendto(fd, resp, sizeof(resp), 0, (const struct sockaddr *)cli, clilen); return; } if (ptype == IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_1 && plen >= 28) { uint8_t tag = pl[0]; uint32_t ms_sid = (uint32_t)pl[4] | ((uint32_t)pl[5] << 8) | ((uint32_t)pl[6] << 16) | ((uint32_t)pl[7] << 24); RmcpSimSession *s = NULL; for (int i = 0; i < MAX_RMCP_SESSIONS; i++) { if (st->sessions[i].in_use && st->sessions[i].ms_session_id == ms_sid) { s = &st->sessions[i]; break; } } if (!s) { pthread_mutex_unlock(&st->mu); return; } memcpy(s->rc_rand, pl + 8, 16); uint8_t role_m = pl[24]; s->max_priv = role_m & 0x0F; s->lookup_flag = (role_m >> 4) & 0x01; uint8_t ulen = pl[27]; if (ulen > 16 || 28 + ulen > plen) ulen = 0; s->username_len = ulen; memset(s->username, 0, sizeof(s->username)); if (ulen > 0) memcpy(s->username, pl + 28, ulen); RAND_bytes(s->ms_rand, 16); for (int g = 0; g < 16; g++) s->ms_guid[g] = (uint8_t)(0xA0 + st->idx * 16 + g); const char *pw = lookup_password(s->username); uint8_t k_uid[20]; memset(k_uid, 0, sizeof(k_uid)); size_t pw_len = strlen(pw); if (pw_len > 20) pw_len = 20; memcpy(k_uid, pw, pw_len); /* RAKP2 HMAC input: SIDc(4) | SIDm(4) | Rc(16) | Rm(16) | GUIDm(16) | Role_m(1) | ULen(1) | UName */ uint8_t rakp2_in[128]; size_t off = 0; memcpy(rakp2_in + off, &s->rc_session_id, 4); off += 4; memcpy(rakp2_in + off, &s->ms_session_id, 4); off += 4; memcpy(rakp2_in + off, s->rc_rand, 16); off += 16; memcpy(rakp2_in + off, s->ms_rand, 16); off += 16; memcpy(rakp2_in + off, s->ms_guid, 16); off += 16; rakp2_in[off++] = role_m; rakp2_in[off++] = ulen; if (ulen > 0) { memcpy(rakp2_in + off, s->username, ulen); off += ulen; } uint8_t mac[64]; unsigned int mac_len = 0; compute_hmac(s->auth_alg, k_uid, 20, rakp2_in, off, mac, &mac_len); /* Derive SIK, K1, K2 via libfreeipmi so keys match client bit-for-bit! */ void *sik_ptr = s->sik, *k1_ptr = s->k1, *k2_ptr = s->k2; s->sik_len = sizeof(s->sik); s->k1_len = sizeof(s->k1); s->k2_len = sizeof(s->k2); (void)ipmi_calculate_rmcpplus_session_keys( s->auth_alg, s->integ_alg, s->conf_alg, pw, (unsigned int)strlen(pw), NULL, 0, s->rc_rand, 16, s->ms_rand, 16, s->lookup_flag, s->max_priv, s->username, s->username_len, &sik_ptr, &s->sik_len, &k1_ptr, &s->k1_len, &k2_ptr, &s->k2_len); uint16_t r2_plen = (uint16_t)(40 + mac_len); uint8_t resp[16 + 128]; memset(resp, 0, sizeof(resp)); resp[0] = 0x06; resp[1] = 0x00; resp[2] = 0xFF; resp[3] = 0x07; resp[4] = 0x06; resp[5] = IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_2; resp[14] = (uint8_t)(r2_plen & 0xFF); resp[15] = (uint8_t)((r2_plen >> 8) & 0xFF); uint8_t *rp = resp + 16; rp[0] = tag; rp[1] = 0x00; memcpy(rp + 4, &s->rc_session_id, 4); memcpy(rp + 8, s->ms_rand, 16); memcpy(rp + 24, s->ms_guid, 16); memcpy(rp + 40, mac, mac_len); pthread_mutex_unlock(&st->mu); (void)sendto(fd, resp, 16 + r2_plen, 0, (const struct sockaddr *)cli, clilen); return; } if (ptype == IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_3 && plen >= 8) { uint8_t tag = pl[0]; uint8_t status_in = pl[1]; uint32_t ms_sid = (uint32_t)pl[4] | ((uint32_t)pl[5] << 8) | ((uint32_t)pl[6] << 16) | ((uint32_t)pl[7] << 24); RmcpSimSession *s = NULL; for (int i = 0; i < MAX_RMCP_SESSIONS; i++) { if (st->sessions[i].in_use && st->sessions[i].ms_session_id == ms_sid) { s = &st->sessions[i]; break; } } if (!s || status_in != 0) { pthread_mutex_unlock(&st->mu); return; } /* Verify RAKP3 client password HMAC (`Admin123@pl` or `ADMIN` both accepted in HIL!) */ const char *cands[2] = { "Admin123@pl", "ADMIN" }; int pw_ok = 0; for (int ci = 0; ci < 2; ci++) { uint8_t exp_mac[64]; int exp_len = ipmi_calculate_rakp_3_key_exchange_authentication_code( s->auth_alg, cands[ci], (unsigned int)strlen(cands[ci]), s->ms_rand, 16, s->rc_session_id, s->lookup_flag, s->max_priv, s->username, s->username_len, exp_mac, sizeof(exp_mac)); if (exp_len > 0 && (size_t)(8 + exp_len) <= plen && memcmp(pl + 8, exp_mac, (size_t)exp_len) == 0) { /* Re-derive SIK/K1/K2 with the matched password if needed */ if (ci == 1) { void *sik_ptr = s->sik, *k1_ptr = s->k1, *k2_ptr = s->k2; s->sik_len = sizeof(s->sik); s->k1_len = sizeof(s->k1); s->k2_len = sizeof(s->k2); (void)ipmi_calculate_rmcpplus_session_keys( s->auth_alg, s->integ_alg, s->conf_alg, cands[ci], (unsigned int)strlen(cands[ci]), NULL, 0, s->rc_rand, 16, s->ms_rand, 16, s->lookup_flag, s->max_priv, s->username, s->username_len, &sik_ptr, &s->sik_len, &k1_ptr, &s->k1_len, &k2_ptr, &s->k2_len); } pw_ok = 1; break; } } if (!pw_ok) { uint8_t err_resp[16 + 8]; memset(err_resp, 0, sizeof(err_resp)); err_resp[0] = 0x06; err_resp[2] = 0xFF; err_resp[3] = 0x07; err_resp[4] = 0x06; err_resp[5] = IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_4; err_resp[14] = 8; err_resp[16] = tag; err_resp[17] = 0x0F; /* Invalid integrity check value */ memcpy(err_resp + 20, &s->rc_session_id, 4); pthread_mutex_unlock(&st->mu); (void)sendto(fd, err_resp, sizeof(err_resp), 0, (const struct sockaddr *)cli, clilen); return; } /* RAKP4 ICV: HMAC_SIK(Rc(16) | SIDm(4) | GUIDm(16)), truncated to 12B (SHA1) or 16B (SHA256)! */ uint8_t rakp4_in[36]; memcpy(rakp4_in, s->rc_rand, 16); memcpy(rakp4_in + 16, &s->ms_session_id, 4); memcpy(rakp4_in + 20, s->ms_guid, 16); uint8_t icv[64]; unsigned int icv_full = 0; compute_hmac(s->auth_alg, s->sik, s->sik_len, rakp4_in, 36, icv, &icv_full); unsigned int icv_len = (s->auth_alg == IPMI_AUTHENTICATION_ALGORITHM_RAKP_HMAC_SHA256) ? 16 : 12; uint16_t r4_plen = (uint16_t)(8 + icv_len); uint8_t resp[16 + 64]; memset(resp, 0, sizeof(resp)); resp[0] = 0x06; resp[1] = 0x00; resp[2] = 0xFF; resp[3] = 0x07; resp[4] = 0x06; resp[5] = IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_4; resp[14] = (uint8_t)(r4_plen & 0xFF); resp[15] = (uint8_t)((r4_plen >> 8) & 0xFF); uint8_t *rp = resp + 16; rp[0] = tag; rp[1] = 0x00; memcpy(rp + 4, &s->rc_session_id, 4); memcpy(rp + 8, icv, icv_len); pthread_mutex_unlock(&st->mu); (void)sendto(fd, resp, 16 + r4_plen, 0, (const struct sockaddr *)cli, clilen); return; } if (ptype == IPMI_PAYLOAD_TYPE_IPMI && sid != 0) { RmcpSimSession *s = NULL; for (int i = 0; i < MAX_RMCP_SESSIONS; i++) { if (st->sessions[i].in_use && st->sessions[i].ms_session_id == sid) { s = &st->sessions[i]; break; } } if (!s) { pthread_mutex_unlock(&st->mu); return; } /* Decrypt AES-CBC-128 if encrypted, otherwise read raw IPMB request */ uint8_t plain[1024]; int plain_len = 0; int is_enc = (ptype_raw & 0x80) != 0; int is_auth = (ptype_raw & 0x40) != 0; if (is_enc && plen >= 32) { const uint8_t *iv = pl; const uint8_t *ct = pl + 16; int ct_len = (int)plen - 16; EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(cctx, EVP_aes_128_cbc(), NULL, s->k2, iv); EVP_CIPHER_CTX_set_padding(cctx, 0); int out1 = 0, out2 = 0; if (EVP_DecryptUpdate(cctx, plain, &out1, ct, ct_len) == 1 && EVP_DecryptFinal_ex(cctx, plain + out1, &out2) == 1) { plain_len = out1 + out2; } EVP_CIPHER_CTX_free(cctx); if (plain_len > 0) { uint8_t pad_len = plain[plain_len - 1]; if ((int)pad_len + 1 <= plain_len) { plain_len -= ((int)pad_len + 1); } } } else if (!is_enc) { memcpy(plain, pl, plen); plain_len = (int)plen; } if (plain_len < 7) { pthread_mutex_unlock(&st->mu); return; } /* IPMB request: [0]=rsAddr, [1]=(netFn<<2)|rsLUN, [2]=csum1, [3]=rqAddr, [4]=(rqSeq<<2)|rqLUN, [5]=cmd, [6..plain_len-2]=data, [plain_len-1]=csum2 */ uint8_t rs_addr = plain[0]; uint8_t net_fn = (plain[1] >> 2) & 0x3F; uint8_t rs_lun = plain[1] & 0x03; uint8_t rq_addr = plain[3]; uint8_t rq_seq = (plain[4] >> 2) & 0x3F; uint8_t rq_lun = plain[4] & 0x03; uint8_t cmd = plain[5]; const uint8_t *req_data = plain + 6; size_t req_len = (size_t)(plain_len - 7); uint32_t rc_sid = s->rc_session_id; uint32_t out_seq = s->out_seq++; uint8_t k1_copy[64], k2_copy[64]; unsigned int k1_len = s->k1_len; uint8_t integ_alg = s->integ_alg; memcpy(k1_copy, s->k1, sizeof(k1_copy)); memcpy(k2_copy, s->k2, sizeof(k2_copy)); uint8_t rs_body[512]; size_t rs_body_len = handle_ipmb_command(st, s, net_fn, cmd, req_data, req_len, rs_body); pthread_mutex_unlock(&st->mu); /* Build IPMB response message */ uint8_t ipmb_rs[540]; ipmb_rs[0] = rq_addr; ipmb_rs[1] = (uint8_t)(((net_fn | 0x01) << 2) | rq_lun); ipmb_rs[2] = ipmb_checksum(ipmb_rs, 2); ipmb_rs[3] = rs_addr; ipmb_rs[4] = (uint8_t)((rq_seq << 2) | rs_lun); ipmb_rs[5] = cmd; memcpy(ipmb_rs + 6, rs_body, rs_body_len); size_t ipmb_no_trlr = 6 + rs_body_len; ipmb_rs[ipmb_no_trlr] = ipmb_checksum(ipmb_rs + 3, ipmb_no_trlr - 3); size_t ipmb_total = ipmb_no_trlr + 1; /* Encrypt payload with AES-128-CBC (16B IV + padded plaintext + confidentiality pad 1..N + pad_len) */ uint8_t out_pkt[1200]; memset(out_pkt, 0, sizeof(out_pkt)); out_pkt[0] = 0x06; out_pkt[1] = 0x00; out_pkt[2] = 0xFF; out_pkt[3] = 0x07; out_pkt[4] = 0x06; out_pkt[5] = (uint8_t)(IPMI_PAYLOAD_TYPE_IPMI | (is_enc ? 0x80 : 0) | (is_auth ? 0x40 : 0)); memcpy(out_pkt + 6, &rc_sid, 4); memcpy(out_pkt + 10, &out_seq, 4); size_t payload_wire_len = 0; if (is_enc) { uint8_t iv[16]; RAND_bytes(iv, 16); uint8_t to_enc[600]; memcpy(to_enc, ipmb_rs, ipmb_total); size_t rem = (ipmb_total + 1) % 16; size_t cpad = (rem == 0) ? 0 : (16 - rem); for (size_t p = 0; p < cpad; p++) to_enc[ipmb_total + p] = (uint8_t)(p + 1); to_enc[ipmb_total + cpad] = (uint8_t)cpad; size_t pt_len = ipmb_total + cpad + 1; memcpy(out_pkt + 16, iv, 16); EVP_CIPHER_CTX *ectx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, k2_copy, iv); EVP_CIPHER_CTX_set_padding(ectx, 0); int e1 = 0, e2 = 0; EVP_EncryptUpdate(ectx, out_pkt + 32, &e1, to_enc, (int)pt_len); EVP_EncryptFinal_ex(ectx, out_pkt + 32 + e1, &e2); EVP_CIPHER_CTX_free(ectx); payload_wire_len = 16 + (size_t)(e1 + e2); } else { memcpy(out_pkt + 16, ipmb_rs, ipmb_total); payload_wire_len = ipmb_total; } out_pkt[14] = (uint8_t)(payload_wire_len & 0xFF); out_pkt[15] = (uint8_t)((payload_wire_len >> 8) & 0xFF); size_t cur_off = 16 + payload_wire_len; if (is_auth) { /* Session trailer: integrity pad (0xFF) so (from byte 4 .. next_header inclusive) % 4 == 0 */ size_t covered_base = (cur_off - 4) + 2; /* + pad_len(1) + next_header(1) */ size_t ipad = (4 - (covered_base % 4)) % 4; for (size_t p = 0; p < ipad; p++) out_pkt[cur_off++] = 0xFF; out_pkt[cur_off++] = (uint8_t)ipad; out_pkt[cur_off++] = 0x07; /* Next Header = IPMI */ const EVP_MD *imd = (integ_alg == IPMI_INTEGRITY_ALGORITHM_HMAC_SHA1_96) ? EVP_sha1() : EVP_sha256(); unsigned int mac_out_len = 0; uint8_t full_mac[64]; HMAC(imd, k1_copy, (int)k1_len, out_pkt + 4, cur_off - 4, full_mac, &mac_out_len); size_t trunc_len = (integ_alg == IPMI_INTEGRITY_ALGORITHM_HMAC_SHA1_96) ? 12 : 16; memcpy(out_pkt + cur_off, full_mac, trunc_len); cur_off += trunc_len; } (void)sendto(fd, out_pkt, cur_off, 0, (const struct sockaddr *)cli, clilen); return; } pthread_mutex_unlock(&st->mu); } static void *ipmi_udp_worker(void *arg) { SimStation *st = (SimStation *)arg; int fd = -1; uint8_t buf[2048]; while (g_running) { if (st->bmc_up && fd < 0) { fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd >= 0) { int one = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons((uint16_t)st->ipmi_port); inet_pton(AF_INET, st->ip, &addr.sin_addr); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { fprintf(stderr, "Failed to bind IPMI UDP %s:%d: %s\n", st->ip, st->ipmi_port, strerror(errno)); close(fd); fd = -1; usleep(100000); continue; } } } else if (!st->bmc_up && fd >= 0) { close(fd); fd = -1; } if (fd < 0) { usleep(40000); continue; } struct pollfd pfd = { .fd = fd, .events = POLLIN, .revents = 0 }; if (poll(&pfd, 1, 50) <= 0) continue; struct sockaddr_in cli; socklen_t clilen = sizeof(cli); ssize_t n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&cli, &clilen); if (n < 4) continue; if (!st->bmc_up) { close(fd); fd = -1; continue; } st->ipmi_rx_packets++; /* ASF Presence Ping (message class 0x06) */ if ((buf[3] & 0x1F) == 0x06 && n >= 12 && buf[8] == 0x80) { uint8_t pong[28]; memset(pong, 0, sizeof(pong)); pong[0] = 0x06; pong[2] = 0xFF; pong[3] = 0x06; pong[4] = 0x00; pong[5] = 0x00; pong[6] = 0x11; pong[7] = 0xBE; pong[8] = 0x40; pong[9] = buf[9]; pong[11] = 0x10; pong[12] = 0x00; pong[13] = 0x00; pong[14] = 0x11; pong[15] = 0xBE; pong[20] = 0x81; /* IPMI supported */ (void)sendto(fd, pong, sizeof(pong), 0, (struct sockaddr *)&cli, clilen); continue; } if ((buf[3] & 0x1F) != 0x07 || n < 5) continue; if (buf[4] == 0x00) { handle_ipmi_v15_auth_cap(fd, &cli, clilen, buf, n); } else if (buf[4] == 0x06) { handle_rmcpplus_packet(st, fd, &cli, clilen, buf, n); } } if (fd >= 0) close(fd); return NULL; } /* Optional TCP :22 probe responder (closes socket when `bmc_up == 0`) */ static void *tcp_probe22_worker(void *arg) { SimStation *st = (SimStation *)arg; if (st->probe22_port <= 0) return NULL; int lfd = -1; while (g_running) { if (st->bmc_up && lfd < 0) { lfd = socket(AF_INET, SOCK_STREAM, 0); if (lfd >= 0) { int one = 1; setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); struct sockaddr_in a; memset(&a, 0, sizeof(a)); a.sin_family = AF_INET; a.sin_port = htons((uint16_t)st->probe22_port); inet_pton(AF_INET, st->ip, &a.sin_addr); if (bind(lfd, (struct sockaddr *)&a, sizeof(a)) == 0 && listen(lfd, 8) == 0) { fcntl(lfd, F_SETFL, O_NONBLOCK); } else { close(lfd); lfd = -1; } } } else if (!st->bmc_up && lfd >= 0) { close(lfd); lfd = -1; } if (lfd >= 0) { struct sockaddr_in cli; socklen_t clilen = sizeof(cli); int cfd = accept(lfd, (struct sockaddr *)&cli, &clilen); if (cfd >= 0) { close(cfd); continue; } } usleep(40000); } if (lfd >= 0) close(lfd); return NULL; } /* Local UART bridge acceptor (`dut_and_hood_sim.py` connects here per station) */ static void *uart_bridge_worker(void *arg) { SimStation *st = (SimStation *)arg; int lfd = socket(AF_INET, SOCK_STREAM, 0); int one = 1; setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); struct sockaddr_in a; memset(&a, 0, sizeof(a)); a.sin_family = AF_INET; a.sin_port = htons((uint16_t)st->uart_bridge_port); inet_pton(AF_INET, "127.0.0.1", &a.sin_addr); if (bind(lfd, (struct sockaddr *)&a, sizeof(a)) < 0 || listen(lfd, 4) < 0) { fprintf(stderr, "Failed to bind UART bridge 127.0.0.1:%d\n", st->uart_bridge_port); close(lfd); return NULL; } st->uart_listen_fd = lfd; uint8_t drain_buf[4096]; while (g_running) { /* When no SSH :2200 client is attached, drain UART TX so disconnected output is genuinely lost (like real HW!) */ if (!st->ssh_active) { pthread_mutex_lock(&st->uart_mu); if (st->uart_client_fd >= 0) { while (recv(st->uart_client_fd, drain_buf, sizeof(drain_buf), MSG_DONTWAIT) > 0) {} } pthread_mutex_unlock(&st->uart_mu); } struct pollfd pfd = { .fd = lfd, .events = POLLIN, .revents = 0 }; if (poll(&pfd, 1, 40) <= 0) continue; int cfd = accept(lfd, NULL, NULL); if (cfd < 0) continue; setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); fcntl(cfd, F_SETFL, O_NONBLOCK); pthread_mutex_lock(&st->uart_mu); if (st->uart_client_fd >= 0) close(st->uart_client_fd); st->uart_client_fd = cfd; pthread_mutex_unlock(&st->uart_mu); } close(lfd); return NULL; } typedef struct { SimStation *st; ssh_session session; } SshSessionArgs; static void *ssh_client_handler(void *arg) { SshSessionArgs *sa = (SshSessionArgs *)arg; SimStation *st = sa->st; ssh_session session = sa->session; free(sa); if (ssh_handle_key_exchange(session) != SSH_OK) { ssh_disconnect(session); ssh_free(session); return NULL; } int authed = 0; ssh_channel chan = NULL; int shell_ready = 0; int loops = 0; while (g_running && st->serial_up && !st->kill_active_ssh && !shell_ready && loops++ < 400) { ssh_message msg = ssh_message_get(session); if (!msg) break; int mtype = ssh_message_type(msg); int msub = ssh_message_subtype(msg); if (mtype == SSH_REQUEST_AUTH) { if (msub == SSH_AUTH_METHOD_PASSWORD) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" const char *pw = ssh_message_auth_password(msg); #pragma GCC diagnostic pop if (pw && (strcmp(pw, "Admin123@pl") == 0 || strcmp(pw, "ADMIN") == 0)) { authed = 1; ssh_message_auth_reply_success(msg, 0); ssh_message_free(msg); continue; } } ssh_message_auth_set_methods(msg, SSH_AUTH_METHOD_PASSWORD); ssh_message_reply_default(msg); } else if (mtype == SSH_REQUEST_CHANNEL_OPEN && msub == SSH_CHANNEL_SESSION && authed) { chan = ssh_message_channel_request_open_reply_accept(msg); } else if (mtype == SSH_REQUEST_CHANNEL && chan != NULL) { if (msub == SSH_CHANNEL_REQUEST_PTY) { ssh_message_channel_request_reply_success(msg); } else if (msub == SSH_CHANNEL_REQUEST_SHELL || msub == SSH_CHANNEL_REQUEST_EXEC) { ssh_message_channel_request_reply_success(msg); shell_ready = 1; } else { ssh_message_channel_request_reply_success(msg); } } else { ssh_message_reply_default(msg); } ssh_message_free(msg); } if (!shell_ready || !chan) { if (chan) ssh_channel_free(chan); ssh_disconnect(session); ssh_free(session); return NULL; } st->ssh_connect_count++; st->ssh_active = 1; uint8_t sbuf[4096]; uint8_t ubuf[8192]; while (g_running && st->serial_up && !st->kill_active_ssh && ssh_channel_is_open(chan) && !ssh_channel_is_eof(chan)) { int did_io = 0; /* 1. Drain bytes sent from stl_native_server -> forward to Python DUT state machine */ int nr = ssh_channel_read_nonblocking(chan, sbuf, sizeof(sbuf), 0); if (nr == SSH_ERROR) break; if (nr > 0) { did_io = 1; pthread_mutex_lock(&st->uart_mu); if (st->uart_client_fd >= 0) { (void)send(st->uart_client_fd, sbuf, (size_t)nr, MSG_NOSIGNAL); } pthread_mutex_unlock(&st->uart_mu); } /* 2. Drain BIOS UART bytes from Python DUT state machine -> forward to stl_native_server */ pthread_mutex_lock(&st->uart_mu); int ufd = st->uart_client_fd; ssize_t un = -1; if (ufd >= 0) { un = recv(ufd, ubuf, sizeof(ubuf), MSG_DONTWAIT); if (un == 0 || (un < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) { close(st->uart_client_fd); st->uart_client_fd = -1; un = -1; } } pthread_mutex_unlock(&st->uart_mu); if (un > 0) { did_io = 1; if (ssh_channel_write(chan, ubuf, (uint32_t)un) == SSH_ERROR) { break; } } if (!did_io) usleep(4000); } st->ssh_active = 0; ssh_channel_send_eof(chan); ssh_channel_close(chan); ssh_channel_free(chan); ssh_disconnect(session); ssh_free(session); return NULL; } static void *ssh_console_worker(void *arg) { SimStation *st = (SimStation *)arg; ssh_bind sbind = NULL; int listen_fd = -1; while (g_running) { if (st->serial_up && !sbind) { sbind = ssh_bind_new(); unsigned int port = (unsigned int)st->ssh_port; ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_BINDADDR, st->ip); ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_BINDPORT, &port); ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, g_hostkey_path); if (ssh_bind_listen(sbind) < 0) { ssh_bind_free(sbind); sbind = NULL; usleep(100000); continue; } ssh_bind_set_blocking(sbind, 0); listen_fd = ssh_bind_get_fd(sbind); st->kill_active_ssh = 0; } else if (!st->serial_up && sbind) { st->kill_active_ssh = 1; ssh_bind_free(sbind); sbind = NULL; listen_fd = -1; } if (sbind && listen_fd >= 0) { struct pollfd pfd = { .fd = listen_fd, .events = POLLIN, .revents = 0 }; if (poll(&pfd, 1, 40) > 0 && (pfd.revents & POLLIN)) { ssh_session sess = ssh_new(); if (ssh_bind_accept(sbind, sess) == SSH_OK) { SshSessionArgs *sa = (SshSessionArgs *)calloc(1, sizeof(SshSessionArgs)); sa->st = st; sa->session = sess; pthread_t tid; if (pthread_create(&tid, NULL, ssh_client_handler, sa) == 0) { pthread_detach(tid); } else { free(sa); ssh_free(sess); } } else { ssh_free(sess); } } } else { usleep(40000); } } if (sbind) ssh_bind_free(sbind); return NULL; } static void handle_ctrl_line(int cfd, char *line) { char cmd[32] = ""; if (sscanf(line, "%31s", cmd) != 1) return; if (strcmp(cmd, "STATUS") == 0) { char out[8192]; size_t off = 0; off += (size_t)snprintf(out + off, sizeof(out) - off, "{\"stations\":["); for (int i = 0; i < g_num_stations; i++) { SimStation *st = &g_stations[i]; off += (size_t)snprintf( out + off, sizeof(out) - off, "%s{\"idx\":%d,\"ip\":\"%s\",\"bmc_up\":%s,\"serial_up\":%s,\"power_on\":%s," "\"off_count\":%d,\"on_count\":%d,\"reset_count\":%d,\"cycle_count\":%d," "\"warm_reset_count\":%d,\"ipmi_rx_packets\":%d,\"ssh_connect_count\":%d,\"ssh_active\":%s}", (i > 0) ? "," : "", st->idx, st->ip, st->bmc_up ? "true" : "false", st->serial_up ? "true" : "false", st->power_on ? "true" : "false", st->off_count, st->on_count, st->reset_count, st->cycle_count, st->warm_reset_count, st->ipmi_rx_packets, st->ssh_connect_count, st->ssh_active ? "true" : "false"); } off += (size_t)snprintf(out + off, sizeof(out) - off, "]}\n"); (void)send(cfd, out, off, MSG_NOSIGNAL); return; } if (strcmp(cmd, "SET_LINK") == 0) { int idx = -1, b_up = 1, s_up = 1; if (sscanf(line, "%*s %d %d %d", &idx, &b_up, &s_up) == 3 && idx >= 0 && idx < g_num_stations) { SimStation *st = &g_stations[idx]; st->bmc_up = b_up ? 1 : 0; if (!s_up && st->serial_up) { st->kill_active_ssh = 1; } st->serial_up = s_up ? 1 : 0; const char *ok = "{\"ok\":true}\n"; (void)send(cfd, ok, strlen(ok), MSG_NOSIGNAL); return; } } if (strcmp(cmd, "SET_POWER") == 0) { int idx = -1, pwr = 1; if (sscanf(line, "%*s %d %d", &idx, &pwr) == 2 && idx >= 0 && idx < g_num_stations) { g_stations[idx].power_on = pwr ? 1 : 0; const char *ok = "{\"ok\":true}\n"; (void)send(cfd, ok, strlen(ok), MSG_NOSIGNAL); return; } } if (strcmp(cmd, "SET_TEMPS") == 0) { int idx = -1; char rest[512] = ""; if (sscanf(line, "%*s %d %511s", &idx, rest) == 2 && idx >= 0 && idx < g_num_stations) { SimStation *st = &g_stations[idx]; pthread_mutex_lock(&st->mu); char *save = NULL; char *tok = strtok_r(rest, ",", &save); int slot = 0; while (tok && slot < NUM_DIMMS) { int v = atoi(tok); if (v < 0) { st->dimm_present[slot] = 0; st->dimm_temp_c[slot] = 0; } else { st->dimm_present[slot] = 1; st->dimm_temp_c[slot] = v; } slot++; tok = strtok_r(NULL, ",", &save); } pthread_mutex_unlock(&st->mu); const char *ok = "{\"ok\":true}\n"; (void)send(cfd, ok, strlen(ok), MSG_NOSIGNAL); return; } } if (strcmp(cmd, "QUIT") == 0) { g_running = 0; const char *ok = "{\"ok\":true}\n"; (void)send(cfd, ok, strlen(ok), MSG_NOSIGNAL); return; } const char *err = "{\"ok\":false,\"error\":\"bad_command\"}\n"; (void)send(cfd, err, strlen(err), MSG_NOSIGNAL); } /* * Usage: * bmc_ssh_hil_sim --ctrl-port 19199 --uart-base 19200 \ * --ipmi-port 623 --ssh-port 2200 --probe22-port 22 \ * 127.0.2.166 127.0.2.165 127.0.2.167 127.0.2.168 */ int main(int argc, char **argv) { signal(SIGPIPE, SIG_IGN); ipmi_rmcpplus_init(); ssh_init(); int ctrl_port = 19199; int uart_base = 19200; int ipmi_port = 623; int ssh_port = 2200; int probe22_port = 22; int argi = 1; while (argi < argc && strncmp(argv[argi], "--", 2) == 0) { if (strcmp(argv[argi], "--ctrl-port") == 0 && argi + 1 < argc) { ctrl_port = atoi(argv[++argi]); } else if (strcmp(argv[argi], "--uart-base") == 0 && argi + 1 < argc) { uart_base = atoi(argv[++argi]); } else if (strcmp(argv[argi], "--ipmi-port") == 0 && argi + 1 < argc) { ipmi_port = atoi(argv[++argi]); } else if (strcmp(argv[argi], "--ssh-port") == 0 && argi + 1 < argc) { ssh_port = atoi(argv[++argi]); } else if (strcmp(argv[argi], "--probe22-port") == 0 && argi + 1 < argc) { probe22_port = atoi(argv[++argi]); } else if (strcmp(argv[argi], "--hostkey") == 0 && argi + 1 < argc) { snprintf(g_hostkey_path, sizeof(g_hostkey_path), "%s", argv[++argi]); } argi++; } if (g_hostkey_path[0] == '\0') { snprintf(g_hostkey_path, sizeof(g_hostkey_path), "/tmp/stl_hil_ssh_hostkey_%d", (int)getpid()); ssh_key hkey = NULL; if (ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &hkey) != SSH_OK || ssh_pki_export_privkey_file(hkey, NULL, NULL, NULL, g_hostkey_path) != SSH_OK) { fprintf(stderr, "Failed to generate ephemeral RSA hostkey at %s\n", g_hostkey_path); return 1; } ssh_key_free(hkey); } for (; argi < argc && g_num_stations < MAX_STATIONS; argi++) { SimStation *st = &g_stations[g_num_stations]; memset(st, 0, sizeof(*st)); st->idx = g_num_stations; snprintf(st->ip, sizeof(st->ip), "%s", argv[argi]); st->ipmi_port = ipmi_port; st->ssh_port = ssh_port; st->probe22_port = probe22_port; st->uart_bridge_port = uart_base + g_num_stations; st->bmc_up = 1; st->serial_up = 1; st->power_on = 1; st->next_ms_sid = (uint32_t)(0x10000000U + (uint32_t)g_num_stations * 0x10000U); st->uart_listen_fd = -1; st->uart_client_fd = -1; pthread_mutex_init(&st->mu, NULL); pthread_mutex_init(&st->uart_mu, NULL); for (int d = 0; d < NUM_DIMMS; d++) { st->dimm_present[d] = 1; st->dimm_temp_c[d] = 42 + (d % 6); } build_station_sdr_repository(st); g_num_stations++; } if (g_num_stations == 0) { fprintf(stderr, "No station IPs specified\n"); return 1; } int ctrl_lfd = socket(AF_INET, SOCK_STREAM, 0); int one = 1; setsockopt(ctrl_lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); struct sockaddr_in caddr; memset(&caddr, 0, sizeof(caddr)); caddr.sin_family = AF_INET; caddr.sin_port = htons((uint16_t)ctrl_port); inet_pton(AF_INET, "127.0.0.1", &caddr.sin_addr); if (bind(ctrl_lfd, (struct sockaddr *)&caddr, sizeof(caddr)) < 0 || listen(ctrl_lfd, 16) < 0) { fprintf(stderr, "Failed to bind control port 127.0.0.1:%d: %s\n", ctrl_port, strerror(errno)); return 1; } for (int i = 0; i < g_num_stations; i++) { pthread_t t_ipmi, t_ssh, t_uart, t_p22; pthread_create(&t_ipmi, NULL, ipmi_udp_worker, &g_stations[i]); pthread_detach(t_ipmi); pthread_create(&t_ssh, NULL, ssh_console_worker, &g_stations[i]); pthread_detach(t_ssh); pthread_create(&t_uart, NULL, uart_bridge_worker, &g_stations[i]); pthread_detach(t_uart); if (g_stations[i].probe22_port > 0) { pthread_create(&t_p22, NULL, tcp_probe22_worker, &g_stations[i]); pthread_detach(t_p22); } } printf("READY ctrl=127.0.0.1:%d stations=%d\n", ctrl_port, g_num_stations); fflush(stdout); while (g_running) { struct pollfd pfd = { .fd = ctrl_lfd, .events = POLLIN, .revents = 0 }; if (poll(&pfd, 1, 100) <= 0) continue; int cfd = accept(ctrl_lfd, NULL, NULL); if (cfd < 0) continue; char lbuf[1024]; ssize_t n = recv(cfd, lbuf, sizeof(lbuf) - 1, 0); if (n > 0) { lbuf[n] = '\0'; handle_ctrl_line(cfd, lbuf); } close(cfd); } close(ctrl_lfd); unlink(g_hostkey_path); ssh_finalize(); return 0; }