bmc_ssh_hil_sim.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. /*
  2. * bmc_ssh_hil_sim.c — Multi-Station Protocol-Authentic Hardware-in-the-Loop (HIL)
  3. * BMC IPMI v2.0 RMCP+ (UDP :623, Cipher Suite 17 / 3 + 24-ch SDR)
  4. * & SSHv2 BIOS Serial Console (TCP :2200 + :22 Probe) Simulator
  5. *
  6. * Zero-shortcut wire-level compatibility with production `stl_native_server`:
  7. * - Real encrypted RMCP+ 2.0 sessions over UDP :623 (`libfreeipmi`)
  8. * - Real 24-slot Full SDR temperature records (`CPU0_DIMM_A0`..`CPU1_DIMM_L0`)
  9. * - Real SSHv2 PTY/Shell sessions over TCP :2200 (`libssh/server.h`)
  10. * - Real-time link fault injection (`SET_LINK`), AC power loss (`SET_POWER`),
  11. * 24-channel thermal sync (`SET_TEMPS`), and power-event streaming over TCP control port.
  12. */
  13. #define _GNU_SOURCE
  14. #include <arpa/inet.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <netinet/in.h>
  18. #include <netinet/tcp.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/hmac.h>
  21. #include <openssl/rand.h>
  22. #include <poll.h>
  23. #include <pthread.h>
  24. #include <signal.h>
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/socket.h>
  30. #include <sys/types.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include <freeipmi/freeipmi.h>
  34. #include <libssh/callbacks.h>
  35. #include <libssh/libssh.h>
  36. #include <libssh/server.h>
  37. #define MAX_STATIONS 8
  38. #define MAX_RMCP_SESSIONS 32
  39. #define NUM_DIMMS 24
  40. #define NUM_EXTRA_SENSORS 6
  41. #define NUM_SDR_RECORDS (NUM_DIMMS + NUM_EXTRA_SENSORS)
  42. typedef struct {
  43. int in_use;
  44. uint32_t rc_session_id;
  45. uint32_t ms_session_id;
  46. uint8_t auth_alg;
  47. uint8_t integ_alg;
  48. uint8_t conf_alg;
  49. uint8_t max_priv;
  50. uint8_t lookup_flag;
  51. char username[32];
  52. uint8_t username_len;
  53. uint8_t rc_rand[16];
  54. uint8_t ms_rand[16];
  55. uint8_t ms_guid[16];
  56. uint8_t sik[64];
  57. unsigned int sik_len;
  58. uint8_t k1[64];
  59. unsigned int k1_len;
  60. uint8_t k2[64];
  61. unsigned int k2_len;
  62. uint32_t out_seq;
  63. } RmcpSimSession;
  64. typedef struct {
  65. int idx;
  66. char ip[64];
  67. int ipmi_port;
  68. int ssh_port;
  69. int probe22_port;
  70. int uart_bridge_port;
  71. volatile int bmc_up;
  72. volatile int serial_up;
  73. volatile int power_on;
  74. volatile int off_count;
  75. volatile int on_count;
  76. volatile int reset_count;
  77. volatile int cycle_count;
  78. volatile int warm_reset_count;
  79. volatile int ipmi_rx_packets;
  80. volatile int ssh_connect_count;
  81. volatile int ssh_active;
  82. volatile int kill_active_ssh;
  83. int dimm_present[NUM_DIMMS];
  84. int dimm_temp_c[NUM_DIMMS];
  85. uint8_t sdr_records[NUM_SDR_RECORDS][64];
  86. unsigned int sdr_record_lens[NUM_SDR_RECORDS];
  87. pthread_mutex_t mu;
  88. RmcpSimSession sessions[MAX_RMCP_SESSIONS];
  89. uint32_t next_ms_sid;
  90. int uart_listen_fd;
  91. int uart_client_fd;
  92. pthread_mutex_t uart_mu;
  93. } SimStation;
  94. static SimStation g_stations[MAX_STATIONS];
  95. static int g_num_stations = 0;
  96. static char g_hostkey_path[256] = "";
  97. static volatile int g_running = 1;
  98. static uint8_t ipmb_checksum(const uint8_t *buf, size_t len) {
  99. uint8_t sum = 0;
  100. for (size_t i = 0; i < len; i++) sum = (uint8_t)(sum + buf[i]);
  101. return (uint8_t)(0x100U - sum);
  102. }
  103. static void build_station_sdr_repository(SimStation *st) {
  104. for (int i = 0; i < NUM_DIMMS; i++) {
  105. int cpu = i / 12;
  106. char ch = (char)('A' + (i % 12));
  107. char name[32];
  108. snprintf(name, sizeof(name), "CPU%d_DIMM_%c0", cpu, ch);
  109. size_t nlen = strlen(name);
  110. if (nlen > 16) nlen = 16;
  111. fiid_obj_t obj = fiid_obj_create(tmpl_sdr_full_sensor_record);
  112. fiid_obj_clear(obj);
  113. fiid_obj_set(obj, "record_id", (uint64_t)(i + 1));
  114. fiid_obj_set(obj, "sdr_version_major", 1);
  115. fiid_obj_set(obj, "sdr_version_minor", 5);
  116. fiid_obj_set(obj, "record_type", IPMI_SDR_FORMAT_FULL_SENSOR_RECORD);
  117. fiid_obj_set(obj, "record_length", (uint64_t)(43 + nlen));
  118. fiid_obj_set(obj, "sensor_owner_id.type", 0);
  119. fiid_obj_set(obj, "sensor_owner_id", 0x10); /* 0x20 >> 1 */
  120. fiid_obj_set(obj, "sensor_owner_lun", 0);
  121. fiid_obj_set(obj, "sensor_owner_lun.reserved", 0);
  122. fiid_obj_set(obj, "channel_number", 0);
  123. fiid_obj_set(obj, "sensor_number", (uint64_t)(0x10 + i));
  124. fiid_obj_set(obj, "entity_id", 32); /* Memory Device */
  125. fiid_obj_set(obj, "entity_instance", (uint64_t)(i + 1));
  126. fiid_obj_set(obj, "entity_instance.type", 0);
  127. fiid_obj_set(obj, "sensor_initialization.sensor_scanning", 1);
  128. fiid_obj_set(obj, "sensor_initialization.event_generation", 0);
  129. fiid_obj_set(obj, "sensor_initialization.init_sensor_type", 1);
  130. fiid_obj_set(obj, "sensor_initialization.init_hysteresis", 0);
  131. fiid_obj_set(obj, "sensor_initialization.init_thresholds", 1);
  132. fiid_obj_set(obj, "sensor_initialization.init_events", 0);
  133. fiid_obj_set(obj, "sensor_initialization.init_scanning", 1);
  134. fiid_obj_set(obj, "sensor_initialization.settable_sensor", 0);
  135. fiid_obj_set(obj, "sensor_capabilities.event_message_control_support", 3);
  136. fiid_obj_set(obj, "sensor_capabilities.threshold_access_support", 1);
  137. fiid_obj_set(obj, "sensor_capabilities.hysteresis_support", 0);
  138. fiid_obj_set(obj, "sensor_capabilities.auto_re_arm_support", 1);
  139. fiid_obj_set(obj, "sensor_capabilities.entity_ignore_support", 0);
  140. fiid_obj_set(obj, "sensor_type", 0x01); /* Temperature */
  141. fiid_obj_set(obj, "event_reading_type_code", 0x01); /* Threshold */
  142. fiid_obj_set(obj, "assertion_event_lower_threshold_reading_mask", 0);
  143. fiid_obj_set(obj, "deassertion_event_upper_threshold_reading_mask", 0);
  144. fiid_obj_set(obj, "discrete_reading_settable_threshold_readable_threshold_mask", 0);
  145. fiid_obj_set(obj, "sensor_unit1.percentage", 0);
  146. fiid_obj_set(obj, "sensor_unit1.modifier_unit", 0);
  147. fiid_obj_set(obj, "sensor_unit1.rate_unit", 0);
  148. fiid_obj_set(obj, "sensor_unit1.analog_data_format", 0); /* unsigned */
  149. fiid_obj_set(obj, "sensor_unit2.base_unit", 1); /* Degrees C */
  150. fiid_obj_set(obj, "sensor_unit3.modifier_unit", 0);
  151. fiid_obj_set(obj, "linearization", 0);
  152. fiid_obj_set(obj, "linearization.reserved", 0);
  153. fiid_obj_set(obj, "m_ls", 1);
  154. fiid_obj_set(obj, "tolerance", 0);
  155. fiid_obj_set(obj, "m_ms", 0);
  156. fiid_obj_set(obj, "b_ls", 0);
  157. fiid_obj_set(obj, "accuracy_ls", 0);
  158. fiid_obj_set(obj, "b_ms", 0);
  159. fiid_obj_set(obj, "sensor_direction", 0);
  160. fiid_obj_set(obj, "accuracy_exp", 0);
  161. fiid_obj_set(obj, "accuracy_ms", 0);
  162. fiid_obj_set(obj, "b_exponent", 0);
  163. fiid_obj_set(obj, "r_exponent", 0);
  164. fiid_obj_set(obj, "analog_characteristics_flag.nominal_reading", 1);
  165. fiid_obj_set(obj, "analog_characteristics_flag.normal_max", 1);
  166. fiid_obj_set(obj, "analog_characteristics_flag.normal_min", 1);
  167. fiid_obj_set(obj, "analog_characteristics_flag.reserved", 0);
  168. fiid_obj_set(obj, "nominal_reading", 45);
  169. fiid_obj_set(obj, "normal_maximum", 85);
  170. fiid_obj_set(obj, "normal_minimum", 10);
  171. fiid_obj_set(obj, "sensor_maximum_reading", 125);
  172. fiid_obj_set(obj, "sensor_minimum_reading", 0);
  173. fiid_obj_set(obj, "upper_non_recoverable_threshold", 105);
  174. fiid_obj_set(obj, "upper_critical_threshold", 95);
  175. fiid_obj_set(obj, "upper_non_critical_threshold", 85);
  176. fiid_obj_set(obj, "lower_non_recoverable_threshold", 0);
  177. fiid_obj_set(obj, "lower_critical_threshold", 0);
  178. fiid_obj_set(obj, "lower_non_critical_threshold", 5);
  179. fiid_obj_set(obj, "positive_going_threshold_hysteresis", 1);
  180. fiid_obj_set(obj, "negative_going_threshold_hysteresis", 1);
  181. fiid_obj_set(obj, "reserved", 0);
  182. fiid_obj_set(obj, "oem", 0);
  183. /* ASCII/Latin1 type (0xC0) | length */
  184. fiid_obj_set(obj, "id_string_type_length_code", (uint64_t)(0xC0 | nlen));
  185. fiid_obj_set_data(obj, "id_string", name, (unsigned int)nlen);
  186. int len = fiid_obj_get_all(obj, st->sdr_records[i], sizeof(st->sdr_records[i]));
  187. st->sdr_record_lens[i] = (len > 0) ? (unsigned int)len : 0;
  188. fiid_obj_destroy(obj);
  189. }
  190. /* Extra system sensors (Voltage, Fan, Power, Current) for full BMC telemetry verification */
  191. struct {
  192. const char *name;
  193. uint8_t snum;
  194. uint8_t stype;
  195. uint8_t base_unit;
  196. uint16_t m;
  197. int8_t r_exp;
  198. uint8_t nominal;
  199. } extras[NUM_EXTRA_SENSORS] = {
  200. { "P12V", 0x30, 0x02, 4, 10, -2, 120 }, /* 12.00 V */
  201. { "VDDQ_CPU0", 0x31, 0x02, 4, 10, -3, 110 }, /* 1.10 V */
  202. { "FAN1_RPM", 0x32, 0x04, 18, 100, 0, 54 }, /* 5400 RPM */
  203. { "FAN2_RPM", 0x33, 0x04, 18, 100, 0, 52 }, /* 5200 RPM */
  204. { "PSU1_PIN_W", 0x34, 0x03, 6, 4, 0, 80 }, /* 320 W */
  205. { "PSU1_IIN_A", 0x35, 0x03, 5, 10, -2, 145 }, /* 14.50 A */
  206. };
  207. for (int e = 0; e < NUM_EXTRA_SENSORS; e++) {
  208. int rec_idx = NUM_DIMMS + e;
  209. size_t nlen = strlen(extras[e].name);
  210. if (nlen > 16) nlen = 16;
  211. fiid_obj_t obj = fiid_obj_create(tmpl_sdr_full_sensor_record);
  212. fiid_obj_clear(obj);
  213. fiid_obj_set(obj, "record_id", (uint64_t)(rec_idx + 1));
  214. fiid_obj_set(obj, "sdr_version_major", 1);
  215. fiid_obj_set(obj, "sdr_version_minor", 5);
  216. fiid_obj_set(obj, "record_type", IPMI_SDR_FORMAT_FULL_SENSOR_RECORD);
  217. fiid_obj_set(obj, "record_length", (uint64_t)(43 + nlen));
  218. fiid_obj_set(obj, "sensor_owner_id.type", 0);
  219. fiid_obj_set(obj, "sensor_owner_id", 0x10);
  220. fiid_obj_set(obj, "sensor_owner_lun", 0);
  221. fiid_obj_set(obj, "sensor_owner_lun.reserved", 0);
  222. fiid_obj_set(obj, "channel_number", 0);
  223. fiid_obj_set(obj, "sensor_number", (uint64_t)extras[e].snum);
  224. fiid_obj_set(obj, "entity_id", 7); /* System Board */
  225. fiid_obj_set(obj, "entity_instance", (uint64_t)(e + 1));
  226. fiid_obj_set(obj, "entity_instance.type", 0);
  227. fiid_obj_set(obj, "sensor_initialization.sensor_scanning", 1);
  228. fiid_obj_set(obj, "sensor_initialization.event_generation", 0);
  229. fiid_obj_set(obj, "sensor_initialization.init_sensor_type", 1);
  230. fiid_obj_set(obj, "sensor_initialization.init_hysteresis", 0);
  231. fiid_obj_set(obj, "sensor_initialization.init_thresholds", 1);
  232. fiid_obj_set(obj, "sensor_initialization.init_events", 0);
  233. fiid_obj_set(obj, "sensor_initialization.init_scanning", 1);
  234. fiid_obj_set(obj, "sensor_initialization.settable_sensor", 0);
  235. fiid_obj_set(obj, "sensor_capabilities.event_message_control_support", 3);
  236. fiid_obj_set(obj, "sensor_capabilities.threshold_access_support", 1);
  237. fiid_obj_set(obj, "sensor_capabilities.hysteresis_support", 0);
  238. fiid_obj_set(obj, "sensor_capabilities.auto_re_arm_support", 1);
  239. fiid_obj_set(obj, "sensor_capabilities.entity_ignore_support", 0);
  240. fiid_obj_set(obj, "sensor_type", extras[e].stype);
  241. fiid_obj_set(obj, "event_reading_type_code", 0x01);
  242. fiid_obj_set(obj, "assertion_event_lower_threshold_reading_mask", 0);
  243. fiid_obj_set(obj, "deassertion_event_upper_threshold_reading_mask", 0);
  244. fiid_obj_set(obj, "discrete_reading_settable_threshold_readable_threshold_mask", 0);
  245. fiid_obj_set(obj, "sensor_unit1.percentage", 0);
  246. fiid_obj_set(obj, "sensor_unit1.modifier_unit", 0);
  247. fiid_obj_set(obj, "sensor_unit1.rate_unit", 0);
  248. fiid_obj_set(obj, "sensor_unit1.analog_data_format", 0);
  249. fiid_obj_set(obj, "sensor_unit2.base_unit", extras[e].base_unit);
  250. fiid_obj_set(obj, "sensor_unit3.modifier_unit", 0);
  251. fiid_obj_set(obj, "linearization", 0);
  252. fiid_obj_set(obj, "linearization.reserved", 0);
  253. fiid_obj_set(obj, "m_ls", extras[e].m & 0xFF);
  254. fiid_obj_set(obj, "tolerance", 0);
  255. fiid_obj_set(obj, "m_ms", (extras[e].m >> 8) & 0x03);
  256. fiid_obj_set(obj, "b_ls", 0);
  257. fiid_obj_set(obj, "accuracy_ls", 0);
  258. fiid_obj_set(obj, "b_ms", 0);
  259. fiid_obj_set(obj, "sensor_direction", 0);
  260. fiid_obj_set(obj, "accuracy_exp", 0);
  261. fiid_obj_set(obj, "accuracy_ms", 0);
  262. fiid_obj_set(obj, "b_exponent", 0);
  263. fiid_obj_set(obj, "r_exponent", (uint64_t)(extras[e].r_exp & 0x0F));
  264. fiid_obj_set(obj, "analog_characteristics_flag.nominal_reading", 1);
  265. fiid_obj_set(obj, "analog_characteristics_flag.normal_max", 1);
  266. fiid_obj_set(obj, "analog_characteristics_flag.normal_min", 1);
  267. fiid_obj_set(obj, "analog_characteristics_flag.reserved", 0);
  268. fiid_obj_set(obj, "nominal_reading", extras[e].nominal);
  269. fiid_obj_set(obj, "normal_maximum", 240);
  270. fiid_obj_set(obj, "normal_minimum", 10);
  271. fiid_obj_set(obj, "sensor_maximum_reading", 255);
  272. fiid_obj_set(obj, "sensor_minimum_reading", 0);
  273. fiid_obj_set(obj, "upper_non_recoverable_threshold", 250);
  274. fiid_obj_set(obj, "upper_critical_threshold", 245);
  275. fiid_obj_set(obj, "upper_non_critical_threshold", 240);
  276. fiid_obj_set(obj, "lower_non_recoverable_threshold", 0);
  277. fiid_obj_set(obj, "lower_critical_threshold", 0);
  278. fiid_obj_set(obj, "lower_non_critical_threshold", 5);
  279. fiid_obj_set(obj, "positive_going_threshold_hysteresis", 1);
  280. fiid_obj_set(obj, "negative_going_threshold_hysteresis", 1);
  281. fiid_obj_set(obj, "reserved", 0);
  282. fiid_obj_set(obj, "oem", 0);
  283. fiid_obj_set(obj, "id_string_type_length_code", (uint64_t)(0xC0 | nlen));
  284. fiid_obj_set_data(obj, "id_string", extras[e].name, (unsigned int)nlen);
  285. int len = fiid_obj_get_all(obj, st->sdr_records[rec_idx], sizeof(st->sdr_records[rec_idx]));
  286. st->sdr_record_lens[rec_idx] = (len > 0) ? (unsigned int)len : 0;
  287. fiid_obj_destroy(obj);
  288. }
  289. }
  290. static void notify_uart_power_event(SimStation *st, const char *ev_name) {
  291. pthread_mutex_lock(&st->uart_mu);
  292. if (st->uart_client_fd >= 0) {
  293. char msg[128];
  294. int n = snprintf(msg, sizeof(msg), "\x01PWR:%s\n", ev_name);
  295. (void)send(st->uart_client_fd, msg, (size_t)n, MSG_NOSIGNAL);
  296. }
  297. pthread_mutex_unlock(&st->uart_mu);
  298. }
  299. static void compute_hmac(
  300. uint8_t auth_alg,
  301. const uint8_t *key, size_t key_len,
  302. const uint8_t *data, size_t data_len,
  303. uint8_t *out, unsigned int *out_len) {
  304. const EVP_MD *md = (auth_alg == IPMI_AUTHENTICATION_ALGORITHM_RAKP_HMAC_SHA1)
  305. ? EVP_sha1()
  306. : EVP_sha256();
  307. HMAC(md, key, (int)key_len, data, data_len, out, out_len);
  308. }
  309. static const char *lookup_password(const char *username) {
  310. (void)username;
  311. return "Admin123@pl";
  312. }
  313. static void handle_ipmi_v15_auth_cap(
  314. int fd, const struct sockaddr_in *cli, socklen_t clilen,
  315. const uint8_t *pkt, ssize_t n) {
  316. /* Minimal IPMI v1.5 unauthenticated Get Channel Authentication Capabilities response */
  317. if (n < 20) return;
  318. uint8_t rq_seq_lun = pkt[18];
  319. uint8_t rq_seq = rq_seq_lun >> 2;
  320. uint8_t rq_addr = pkt[17];
  321. uint8_t rs_addr = pkt[14];
  322. uint8_t resp[32];
  323. memset(resp, 0, sizeof(resp));
  324. /* RMCP header */
  325. resp[0] = 0x06; resp[1] = 0x00; resp[2] = 0xFF; resp[3] = 0x07;
  326. /* v1.5 session header (unauthenticated, seq=0, sid=0, msg_len=16) */
  327. resp[4] = 0x00;
  328. resp[13] = 16;
  329. /* IPMB response header: rqAddr, (netFn=0x07<<2)|0, csum1, rsAddr, (rqSeq<<2)|0, cmd=0x38 */
  330. resp[14] = rq_addr;
  331. resp[15] = (0x07 << 2);
  332. resp[16] = ipmb_checksum(resp + 14, 2);
  333. resp[17] = rs_addr;
  334. resp[18] = (uint8_t)(rq_seq << 2);
  335. resp[19] = 0x38;
  336. resp[20] = 0x00; /* Completion Code OK */
  337. resp[21] = 0x01; /* Channel 1 */
  338. resp[22] = 0x84; /* v2.0+ extended capabilities (0x80) | password (0x04) */
  339. resp[23] = 0x14; /* non-null user, per-msg auth */
  340. resp[24] = 0x02; /* IPMI v2.0 + v1.5 supported */
  341. resp[25] = 0x00; resp[26] = 0x00; resp[27] = 0x00; resp[28] = 0x00;
  342. resp[29] = ipmb_checksum(resp + 17, 12);
  343. (void)sendto(fd, resp, 30, 0, (const struct sockaddr *)cli, clilen);
  344. }
  345. static size_t handle_ipmb_command(
  346. SimStation *st,
  347. RmcpSimSession *sess,
  348. uint8_t net_fn,
  349. uint8_t cmd,
  350. const uint8_t *req_data,
  351. size_t req_len,
  352. uint8_t *rs_data) {
  353. /* rs_data[0] = completion code, followed by response payload */
  354. if (net_fn == 0x06 && cmd == 0x01) {
  355. /* Get Device ID */
  356. rs_data[0] = 0x00;
  357. rs_data[1] = 0x20; /* Device ID */
  358. rs_data[2] = 0x01; /* Device Revision */
  359. rs_data[3] = 0x02; /* FW Major */
  360. rs_data[4] = 0x19; /* FW Minor */
  361. rs_data[5] = 0x02; /* IPMI v2.0 */
  362. rs_data[6] = 0xBF; /* Sensor, SDR, SEL, FRU, Chassis */
  363. rs_data[7] = 0x57; rs_data[8] = 0x01; rs_data[9] = 0x00;
  364. rs_data[10] = 0x01; rs_data[11] = 0x00;
  365. rs_data[12] = 0x00; rs_data[13] = 0x00; rs_data[14] = 0x00; rs_data[15] = 0x00;
  366. return 16;
  367. }
  368. if (net_fn == 0x06 && cmd == 0x3B) {
  369. /* Set Session Privilege Level */
  370. uint8_t req_priv = (req_len >= 1) ? (req_data[0] & 0x0F) : 0x04;
  371. if (req_priv == 0) req_priv = sess ? sess->max_priv : 0x04;
  372. rs_data[0] = 0x00;
  373. rs_data[1] = req_priv;
  374. return 2;
  375. }
  376. if (net_fn == 0x06 && cmd == 0x3C) {
  377. /* Close Session */
  378. if (sess) sess->in_use = 0;
  379. rs_data[0] = 0x00;
  380. return 1;
  381. }
  382. if (net_fn == 0x06 && cmd == 0x02) {
  383. /* Warm Reset */
  384. st->warm_reset_count++;
  385. rs_data[0] = 0x00;
  386. return 1;
  387. }
  388. if (net_fn == 0x00 && cmd == 0x01) {
  389. /* Get Chassis Status */
  390. rs_data[0] = 0x00;
  391. rs_data[1] = st->power_on ? 0x21 : 0x20;
  392. rs_data[2] = 0x00;
  393. rs_data[3] = 0x40;
  394. rs_data[4] = 0x00;
  395. return 5;
  396. }
  397. if (net_fn == 0x00 && cmd == 0x02) {
  398. /* Chassis Control */
  399. uint8_t op = (req_len >= 1) ? (req_data[0] & 0x0F) : 0xFF;
  400. if (op == 0x00) {
  401. st->power_on = 0;
  402. st->off_count++;
  403. notify_uart_power_event(st, "OFF");
  404. } else if (op == 0x01) {
  405. st->power_on = 1;
  406. st->on_count++;
  407. notify_uart_power_event(st, "ON");
  408. } else if (op == 0x02) {
  409. st->power_on = 1;
  410. st->cycle_count++;
  411. notify_uart_power_event(st, "CYCLE");
  412. } else if (op == 0x03) {
  413. st->power_on = 1;
  414. st->reset_count++;
  415. notify_uart_power_event(st, "RESET");
  416. }
  417. rs_data[0] = 0x00;
  418. return 1;
  419. }
  420. if (net_fn == 0x0A && cmd == 0x20) {
  421. /* Get SDR Repository Info */
  422. rs_data[0] = 0x00;
  423. rs_data[1] = 0x51; /* SDR version 1.5 */
  424. rs_data[2] = NUM_SDR_RECORDS & 0xFF;
  425. rs_data[3] = (NUM_SDR_RECORDS >> 8) & 0xFF;
  426. rs_data[4] = 0x00; rs_data[5] = 0x10; /* Free space */
  427. /* Fixed timestamps so SDR cache stays valid */
  428. rs_data[6] = 0x02; rs_data[7] = 0x00; rs_data[8] = 0x00; rs_data[9] = 0x68;
  429. rs_data[10] = 0x02; rs_data[11] = 0x00; rs_data[12] = 0x00; rs_data[13] = 0x68;
  430. rs_data[14] = 0x02; /* Reserve SDR supported */
  431. return 15;
  432. }
  433. if (net_fn == 0x0A && cmd == 0x22) {
  434. /* Reserve SDR Repository */
  435. rs_data[0] = 0x00;
  436. rs_data[1] = 0x34;
  437. rs_data[2] = 0x12;
  438. return 3;
  439. }
  440. if (net_fn == 0x0A && cmd == 0x23) {
  441. /* Get SDR: [res_ls, res_ms, rec_ls, rec_ms, offset, bytes_to_read] */
  442. if (req_len < 6) {
  443. rs_data[0] = 0xC7;
  444. return 1;
  445. }
  446. uint16_t rec_id = (uint16_t)(req_data[2] | (req_data[3] << 8));
  447. uint8_t offset = req_data[4];
  448. uint8_t to_read = req_data[5];
  449. int idx = (rec_id == 0x0000) ? 0 : ((int)rec_id - 1);
  450. if (idx < 0 || idx >= NUM_SDR_RECORDS) {
  451. rs_data[0] = 0xCB; /* Requested sensor/record not present */
  452. return 1;
  453. }
  454. unsigned int rlen = st->sdr_record_lens[idx];
  455. if (offset >= rlen) {
  456. rs_data[0] = 0xC8;
  457. return 1;
  458. }
  459. unsigned int avail = rlen - offset;
  460. unsigned int copy_n = (to_read == 0xFF || to_read > avail) ? avail : to_read;
  461. uint16_t next_id = (idx + 1 < NUM_SDR_RECORDS) ? (uint16_t)(idx + 2) : 0xFFFF;
  462. rs_data[0] = 0x00;
  463. rs_data[1] = (uint8_t)(next_id & 0xFF);
  464. rs_data[2] = (uint8_t)((next_id >> 8) & 0xFF);
  465. memcpy(rs_data + 3, st->sdr_records[idx] + offset, copy_n);
  466. return 3 + copy_n;
  467. }
  468. if (net_fn == 0x04 && cmd == 0x2D) {
  469. /* Get Sensor Reading */
  470. if (req_len < 1) {
  471. rs_data[0] = 0xC7;
  472. return 1;
  473. }
  474. uint8_t raw_snum = req_data[0];
  475. if (raw_snum >= 0x30 && raw_snum < 0x30 + NUM_EXTRA_SENSORS) {
  476. static const uint8_t extra_vals[NUM_EXTRA_SENSORS] = { 120, 110, 54, 52, 80, 145 };
  477. int eidx = (int)(raw_snum - 0x30);
  478. if (!st->power_on) {
  479. rs_data[0] = 0x00; rs_data[1] = 0x00; rs_data[2] = 0x20; rs_data[3] = 0x00;
  480. return 4;
  481. }
  482. rs_data[0] = 0x00;
  483. rs_data[1] = extra_vals[eidx];
  484. rs_data[2] = 0x40;
  485. rs_data[3] = 0xC0;
  486. return 4;
  487. }
  488. int snum = (int)raw_snum - 0x10;
  489. if (snum < 0 || snum >= NUM_DIMMS) {
  490. rs_data[0] = 0xCB;
  491. return 1;
  492. }
  493. if (!st->power_on || !st->dimm_present[snum]) {
  494. /* Scanning disabled / reading unavailable when powered off or absent */
  495. rs_data[0] = 0x00;
  496. rs_data[1] = 0x00;
  497. rs_data[2] = 0x20; /* bit 5 = reading/state unavailable */
  498. rs_data[3] = 0x00;
  499. return 4;
  500. }
  501. rs_data[0] = 0x00;
  502. rs_data[1] = (uint8_t)(st->dimm_temp_c[snum] & 0xFF);
  503. rs_data[2] = 0x40; /* bit 6 = sensor scanning enabled, bit 5 = 0 (valid!) */
  504. rs_data[3] = 0xC0;
  505. return 4;
  506. }
  507. if (net_fn == 0x04 && cmd == 0x2F) {
  508. /* Get Sensor Type */
  509. rs_data[0] = 0x00;
  510. rs_data[1] = 0x01;
  511. rs_data[2] = 0x01;
  512. return 3;
  513. }
  514. /* Generic OEM / raw fallback (fan control, SEL info, etc.) */
  515. rs_data[0] = 0x00;
  516. rs_data[1] = 0x00;
  517. return 2;
  518. }
  519. static void handle_rmcpplus_packet(
  520. SimStation *st, int fd,
  521. const struct sockaddr_in *cli, socklen_t clilen,
  522. const uint8_t *pkt, ssize_t n) {
  523. if (n < 16) return;
  524. uint8_t ptype_raw = pkt[5];
  525. uint8_t ptype = ptype_raw & 0x3F;
  526. uint32_t sid = (uint32_t)pkt[6] | ((uint32_t)pkt[7] << 8) |
  527. ((uint32_t)pkt[8] << 16) | ((uint32_t)pkt[9] << 24);
  528. uint16_t plen = (uint16_t)pkt[14] | ((uint16_t)pkt[15] << 8);
  529. if (16 + (ssize_t)plen > n) return;
  530. const uint8_t *pl = pkt + 16;
  531. pthread_mutex_lock(&st->mu);
  532. if (ptype == IPMI_PAYLOAD_TYPE_RMCPPLUS_OPEN_SESSION_REQUEST && plen >= 32) {
  533. uint8_t tag = pl[0];
  534. uint8_t req_priv = pl[1] & 0x0F;
  535. if (req_priv == 0) req_priv = 0x04;
  536. uint32_t rc_sid = (uint32_t)pl[4] | ((uint32_t)pl[5] << 8) |
  537. ((uint32_t)pl[6] << 16) | ((uint32_t)pl[7] << 24);
  538. uint8_t auth_alg = pl[12] & 0x3F;
  539. uint8_t integ_alg = pl[20] & 0x3F;
  540. uint8_t conf_alg = pl[28] & 0x3F;
  541. int slot_i = 0;
  542. for (int i = 0; i < MAX_RMCP_SESSIONS; i++) {
  543. if (!st->sessions[i].in_use) { slot_i = i; break; }
  544. }
  545. RmcpSimSession *s = &st->sessions[slot_i];
  546. memset(s, 0, sizeof(*s));
  547. s->in_use = 1;
  548. s->rc_session_id = rc_sid;
  549. s->ms_session_id = ++st->next_ms_sid;
  550. if (s->ms_session_id == 0) s->ms_session_id = ++st->next_ms_sid;
  551. s->auth_alg = auth_alg;
  552. s->integ_alg = integ_alg;
  553. s->conf_alg = conf_alg;
  554. s->max_priv = req_priv;
  555. s->out_seq = 1;
  556. uint8_t resp[16 + 36];
  557. memset(resp, 0, sizeof(resp));
  558. resp[0] = 0x06; resp[1] = 0x00; resp[2] = 0xFF; resp[3] = 0x07;
  559. resp[4] = 0x06; /* RMCP+ */
  560. resp[5] = IPMI_PAYLOAD_TYPE_RMCPPLUS_OPEN_SESSION_RESPONSE;
  561. resp[14] = 36; resp[15] = 0;
  562. uint8_t *rp = resp + 16;
  563. rp[0] = tag;
  564. rp[1] = 0x00; /* status OK */
  565. rp[2] = req_priv;
  566. rp[3] = 0x00;
  567. memcpy(rp + 4, &rc_sid, 4);
  568. memcpy(rp + 8, &s->ms_session_id, 4);
  569. rp[12] = 0x00; rp[15] = 0x08; rp[16] = auth_alg;
  570. rp[20] = 0x01; rp[23] = 0x08; rp[24] = integ_alg;
  571. rp[28] = 0x02; rp[31] = 0x08; rp[32] = conf_alg;
  572. pthread_mutex_unlock(&st->mu);
  573. (void)sendto(fd, resp, sizeof(resp), 0, (const struct sockaddr *)cli, clilen);
  574. return;
  575. }
  576. if (ptype == IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_1 && plen >= 28) {
  577. uint8_t tag = pl[0];
  578. uint32_t ms_sid = (uint32_t)pl[4] | ((uint32_t)pl[5] << 8) |
  579. ((uint32_t)pl[6] << 16) | ((uint32_t)pl[7] << 24);
  580. RmcpSimSession *s = NULL;
  581. for (int i = 0; i < MAX_RMCP_SESSIONS; i++) {
  582. if (st->sessions[i].in_use && st->sessions[i].ms_session_id == ms_sid) {
  583. s = &st->sessions[i];
  584. break;
  585. }
  586. }
  587. if (!s) {
  588. pthread_mutex_unlock(&st->mu);
  589. return;
  590. }
  591. memcpy(s->rc_rand, pl + 8, 16);
  592. uint8_t role_m = pl[24];
  593. s->max_priv = role_m & 0x0F;
  594. s->lookup_flag = (role_m >> 4) & 0x01;
  595. uint8_t ulen = pl[27];
  596. if (ulen > 16 || 28 + ulen > plen) ulen = 0;
  597. s->username_len = ulen;
  598. memset(s->username, 0, sizeof(s->username));
  599. if (ulen > 0) memcpy(s->username, pl + 28, ulen);
  600. RAND_bytes(s->ms_rand, 16);
  601. for (int g = 0; g < 16; g++) s->ms_guid[g] = (uint8_t)(0xA0 + st->idx * 16 + g);
  602. const char *pw = lookup_password(s->username);
  603. uint8_t k_uid[20];
  604. memset(k_uid, 0, sizeof(k_uid));
  605. size_t pw_len = strlen(pw);
  606. if (pw_len > 20) pw_len = 20;
  607. memcpy(k_uid, pw, pw_len);
  608. /* RAKP2 HMAC input: SIDc(4) | SIDm(4) | Rc(16) | Rm(16) | GUIDm(16) | Role_m(1) | ULen(1) | UName */
  609. uint8_t rakp2_in[128];
  610. size_t off = 0;
  611. memcpy(rakp2_in + off, &s->rc_session_id, 4); off += 4;
  612. memcpy(rakp2_in + off, &s->ms_session_id, 4); off += 4;
  613. memcpy(rakp2_in + off, s->rc_rand, 16); off += 16;
  614. memcpy(rakp2_in + off, s->ms_rand, 16); off += 16;
  615. memcpy(rakp2_in + off, s->ms_guid, 16); off += 16;
  616. rakp2_in[off++] = role_m;
  617. rakp2_in[off++] = ulen;
  618. if (ulen > 0) { memcpy(rakp2_in + off, s->username, ulen); off += ulen; }
  619. uint8_t mac[64];
  620. unsigned int mac_len = 0;
  621. compute_hmac(s->auth_alg, k_uid, 20, rakp2_in, off, mac, &mac_len);
  622. /* Derive SIK, K1, K2 via libfreeipmi so keys match client bit-for-bit! */
  623. void *sik_ptr = s->sik, *k1_ptr = s->k1, *k2_ptr = s->k2;
  624. s->sik_len = sizeof(s->sik);
  625. s->k1_len = sizeof(s->k1);
  626. s->k2_len = sizeof(s->k2);
  627. (void)ipmi_calculate_rmcpplus_session_keys(
  628. s->auth_alg, s->integ_alg, s->conf_alg,
  629. pw, (unsigned int)strlen(pw),
  630. NULL, 0,
  631. s->rc_rand, 16,
  632. s->ms_rand, 16,
  633. s->lookup_flag, s->max_priv,
  634. s->username, s->username_len,
  635. &sik_ptr, &s->sik_len,
  636. &k1_ptr, &s->k1_len,
  637. &k2_ptr, &s->k2_len);
  638. uint16_t r2_plen = (uint16_t)(40 + mac_len);
  639. uint8_t resp[16 + 128];
  640. memset(resp, 0, sizeof(resp));
  641. resp[0] = 0x06; resp[1] = 0x00; resp[2] = 0xFF; resp[3] = 0x07;
  642. resp[4] = 0x06;
  643. resp[5] = IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_2;
  644. resp[14] = (uint8_t)(r2_plen & 0xFF);
  645. resp[15] = (uint8_t)((r2_plen >> 8) & 0xFF);
  646. uint8_t *rp = resp + 16;
  647. rp[0] = tag;
  648. rp[1] = 0x00;
  649. memcpy(rp + 4, &s->rc_session_id, 4);
  650. memcpy(rp + 8, s->ms_rand, 16);
  651. memcpy(rp + 24, s->ms_guid, 16);
  652. memcpy(rp + 40, mac, mac_len);
  653. pthread_mutex_unlock(&st->mu);
  654. (void)sendto(fd, resp, 16 + r2_plen, 0, (const struct sockaddr *)cli, clilen);
  655. return;
  656. }
  657. if (ptype == IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_3 && plen >= 8) {
  658. uint8_t tag = pl[0];
  659. uint8_t status_in = pl[1];
  660. uint32_t ms_sid = (uint32_t)pl[4] | ((uint32_t)pl[5] << 8) |
  661. ((uint32_t)pl[6] << 16) | ((uint32_t)pl[7] << 24);
  662. RmcpSimSession *s = NULL;
  663. for (int i = 0; i < MAX_RMCP_SESSIONS; i++) {
  664. if (st->sessions[i].in_use && st->sessions[i].ms_session_id == ms_sid) {
  665. s = &st->sessions[i];
  666. break;
  667. }
  668. }
  669. if (!s || status_in != 0) {
  670. pthread_mutex_unlock(&st->mu);
  671. return;
  672. }
  673. /* Verify RAKP3 client password HMAC (`Admin123@pl` or `ADMIN` both accepted in HIL!) */
  674. const char *cands[2] = { "Admin123@pl", "ADMIN" };
  675. int pw_ok = 0;
  676. for (int ci = 0; ci < 2; ci++) {
  677. uint8_t exp_mac[64];
  678. int exp_len = ipmi_calculate_rakp_3_key_exchange_authentication_code(
  679. s->auth_alg,
  680. cands[ci], (unsigned int)strlen(cands[ci]),
  681. s->ms_rand, 16,
  682. s->rc_session_id,
  683. s->lookup_flag, s->max_priv,
  684. s->username, s->username_len,
  685. exp_mac, sizeof(exp_mac));
  686. if (exp_len > 0 && (size_t)(8 + exp_len) <= plen &&
  687. memcmp(pl + 8, exp_mac, (size_t)exp_len) == 0) {
  688. /* Re-derive SIK/K1/K2 with the matched password if needed */
  689. if (ci == 1) {
  690. void *sik_ptr = s->sik, *k1_ptr = s->k1, *k2_ptr = s->k2;
  691. s->sik_len = sizeof(s->sik);
  692. s->k1_len = sizeof(s->k1);
  693. s->k2_len = sizeof(s->k2);
  694. (void)ipmi_calculate_rmcpplus_session_keys(
  695. s->auth_alg, s->integ_alg, s->conf_alg,
  696. cands[ci], (unsigned int)strlen(cands[ci]),
  697. NULL, 0, s->rc_rand, 16, s->ms_rand, 16,
  698. s->lookup_flag, s->max_priv, s->username, s->username_len,
  699. &sik_ptr, &s->sik_len, &k1_ptr, &s->k1_len, &k2_ptr, &s->k2_len);
  700. }
  701. pw_ok = 1;
  702. break;
  703. }
  704. }
  705. if (!pw_ok) {
  706. uint8_t err_resp[16 + 8];
  707. memset(err_resp, 0, sizeof(err_resp));
  708. err_resp[0] = 0x06; err_resp[2] = 0xFF; err_resp[3] = 0x07;
  709. err_resp[4] = 0x06; err_resp[5] = IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_4;
  710. err_resp[14] = 8;
  711. err_resp[16] = tag;
  712. err_resp[17] = 0x0F; /* Invalid integrity check value */
  713. memcpy(err_resp + 20, &s->rc_session_id, 4);
  714. pthread_mutex_unlock(&st->mu);
  715. (void)sendto(fd, err_resp, sizeof(err_resp), 0, (const struct sockaddr *)cli, clilen);
  716. return;
  717. }
  718. /* RAKP4 ICV: HMAC_SIK(Rc(16) | SIDm(4) | GUIDm(16)), truncated to 12B (SHA1) or 16B (SHA256)! */
  719. uint8_t rakp4_in[36];
  720. memcpy(rakp4_in, s->rc_rand, 16);
  721. memcpy(rakp4_in + 16, &s->ms_session_id, 4);
  722. memcpy(rakp4_in + 20, s->ms_guid, 16);
  723. uint8_t icv[64];
  724. unsigned int icv_full = 0;
  725. compute_hmac(s->auth_alg, s->sik, s->sik_len, rakp4_in, 36, icv, &icv_full);
  726. unsigned int icv_len = (s->auth_alg == IPMI_AUTHENTICATION_ALGORITHM_RAKP_HMAC_SHA256) ? 16 : 12;
  727. uint16_t r4_plen = (uint16_t)(8 + icv_len);
  728. uint8_t resp[16 + 64];
  729. memset(resp, 0, sizeof(resp));
  730. resp[0] = 0x06; resp[1] = 0x00; resp[2] = 0xFF; resp[3] = 0x07;
  731. resp[4] = 0x06;
  732. resp[5] = IPMI_PAYLOAD_TYPE_RAKP_MESSAGE_4;
  733. resp[14] = (uint8_t)(r4_plen & 0xFF);
  734. resp[15] = (uint8_t)((r4_plen >> 8) & 0xFF);
  735. uint8_t *rp = resp + 16;
  736. rp[0] = tag;
  737. rp[1] = 0x00;
  738. memcpy(rp + 4, &s->rc_session_id, 4);
  739. memcpy(rp + 8, icv, icv_len);
  740. pthread_mutex_unlock(&st->mu);
  741. (void)sendto(fd, resp, 16 + r4_plen, 0, (const struct sockaddr *)cli, clilen);
  742. return;
  743. }
  744. if (ptype == IPMI_PAYLOAD_TYPE_IPMI && sid != 0) {
  745. RmcpSimSession *s = NULL;
  746. for (int i = 0; i < MAX_RMCP_SESSIONS; i++) {
  747. if (st->sessions[i].in_use && st->sessions[i].ms_session_id == sid) {
  748. s = &st->sessions[i];
  749. break;
  750. }
  751. }
  752. if (!s) {
  753. pthread_mutex_unlock(&st->mu);
  754. return;
  755. }
  756. /* Decrypt AES-CBC-128 if encrypted, otherwise read raw IPMB request */
  757. uint8_t plain[1024];
  758. int plain_len = 0;
  759. int is_enc = (ptype_raw & 0x80) != 0;
  760. int is_auth = (ptype_raw & 0x40) != 0;
  761. if (is_enc && plen >= 32) {
  762. const uint8_t *iv = pl;
  763. const uint8_t *ct = pl + 16;
  764. int ct_len = (int)plen - 16;
  765. EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
  766. EVP_DecryptInit_ex(cctx, EVP_aes_128_cbc(), NULL, s->k2, iv);
  767. EVP_CIPHER_CTX_set_padding(cctx, 0);
  768. int out1 = 0, out2 = 0;
  769. if (EVP_DecryptUpdate(cctx, plain, &out1, ct, ct_len) == 1 &&
  770. EVP_DecryptFinal_ex(cctx, plain + out1, &out2) == 1) {
  771. plain_len = out1 + out2;
  772. }
  773. EVP_CIPHER_CTX_free(cctx);
  774. if (plain_len > 0) {
  775. uint8_t pad_len = plain[plain_len - 1];
  776. if ((int)pad_len + 1 <= plain_len) {
  777. plain_len -= ((int)pad_len + 1);
  778. }
  779. }
  780. } else if (!is_enc) {
  781. memcpy(plain, pl, plen);
  782. plain_len = (int)plen;
  783. }
  784. if (plain_len < 7) {
  785. pthread_mutex_unlock(&st->mu);
  786. return;
  787. }
  788. /* 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 */
  789. uint8_t rs_addr = plain[0];
  790. uint8_t net_fn = (plain[1] >> 2) & 0x3F;
  791. uint8_t rs_lun = plain[1] & 0x03;
  792. uint8_t rq_addr = plain[3];
  793. uint8_t rq_seq = (plain[4] >> 2) & 0x3F;
  794. uint8_t rq_lun = plain[4] & 0x03;
  795. uint8_t cmd = plain[5];
  796. const uint8_t *req_data = plain + 6;
  797. size_t req_len = (size_t)(plain_len - 7);
  798. uint32_t rc_sid = s->rc_session_id;
  799. uint32_t out_seq = s->out_seq++;
  800. uint8_t k1_copy[64], k2_copy[64];
  801. unsigned int k1_len = s->k1_len;
  802. uint8_t integ_alg = s->integ_alg;
  803. memcpy(k1_copy, s->k1, sizeof(k1_copy));
  804. memcpy(k2_copy, s->k2, sizeof(k2_copy));
  805. uint8_t rs_body[512];
  806. size_t rs_body_len = handle_ipmb_command(st, s, net_fn, cmd, req_data, req_len, rs_body);
  807. pthread_mutex_unlock(&st->mu);
  808. /* Build IPMB response message */
  809. uint8_t ipmb_rs[540];
  810. ipmb_rs[0] = rq_addr;
  811. ipmb_rs[1] = (uint8_t)(((net_fn | 0x01) << 2) | rq_lun);
  812. ipmb_rs[2] = ipmb_checksum(ipmb_rs, 2);
  813. ipmb_rs[3] = rs_addr;
  814. ipmb_rs[4] = (uint8_t)((rq_seq << 2) | rs_lun);
  815. ipmb_rs[5] = cmd;
  816. memcpy(ipmb_rs + 6, rs_body, rs_body_len);
  817. size_t ipmb_no_trlr = 6 + rs_body_len;
  818. ipmb_rs[ipmb_no_trlr] = ipmb_checksum(ipmb_rs + 3, ipmb_no_trlr - 3);
  819. size_t ipmb_total = ipmb_no_trlr + 1;
  820. /* Encrypt payload with AES-128-CBC (16B IV + padded plaintext + confidentiality pad 1..N + pad_len) */
  821. uint8_t out_pkt[1200];
  822. memset(out_pkt, 0, sizeof(out_pkt));
  823. out_pkt[0] = 0x06; out_pkt[1] = 0x00; out_pkt[2] = 0xFF; out_pkt[3] = 0x07;
  824. out_pkt[4] = 0x06;
  825. out_pkt[5] = (uint8_t)(IPMI_PAYLOAD_TYPE_IPMI | (is_enc ? 0x80 : 0) | (is_auth ? 0x40 : 0));
  826. memcpy(out_pkt + 6, &rc_sid, 4);
  827. memcpy(out_pkt + 10, &out_seq, 4);
  828. size_t payload_wire_len = 0;
  829. if (is_enc) {
  830. uint8_t iv[16];
  831. RAND_bytes(iv, 16);
  832. uint8_t to_enc[600];
  833. memcpy(to_enc, ipmb_rs, ipmb_total);
  834. size_t rem = (ipmb_total + 1) % 16;
  835. size_t cpad = (rem == 0) ? 0 : (16 - rem);
  836. for (size_t p = 0; p < cpad; p++) to_enc[ipmb_total + p] = (uint8_t)(p + 1);
  837. to_enc[ipmb_total + cpad] = (uint8_t)cpad;
  838. size_t pt_len = ipmb_total + cpad + 1;
  839. memcpy(out_pkt + 16, iv, 16);
  840. EVP_CIPHER_CTX *ectx = EVP_CIPHER_CTX_new();
  841. EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, k2_copy, iv);
  842. EVP_CIPHER_CTX_set_padding(ectx, 0);
  843. int e1 = 0, e2 = 0;
  844. EVP_EncryptUpdate(ectx, out_pkt + 32, &e1, to_enc, (int)pt_len);
  845. EVP_EncryptFinal_ex(ectx, out_pkt + 32 + e1, &e2);
  846. EVP_CIPHER_CTX_free(ectx);
  847. payload_wire_len = 16 + (size_t)(e1 + e2);
  848. } else {
  849. memcpy(out_pkt + 16, ipmb_rs, ipmb_total);
  850. payload_wire_len = ipmb_total;
  851. }
  852. out_pkt[14] = (uint8_t)(payload_wire_len & 0xFF);
  853. out_pkt[15] = (uint8_t)((payload_wire_len >> 8) & 0xFF);
  854. size_t cur_off = 16 + payload_wire_len;
  855. if (is_auth) {
  856. /* Session trailer: integrity pad (0xFF) so (from byte 4 .. next_header inclusive) % 4 == 0 */
  857. size_t covered_base = (cur_off - 4) + 2; /* + pad_len(1) + next_header(1) */
  858. size_t ipad = (4 - (covered_base % 4)) % 4;
  859. for (size_t p = 0; p < ipad; p++) out_pkt[cur_off++] = 0xFF;
  860. out_pkt[cur_off++] = (uint8_t)ipad;
  861. out_pkt[cur_off++] = 0x07; /* Next Header = IPMI */
  862. const EVP_MD *imd = (integ_alg == IPMI_INTEGRITY_ALGORITHM_HMAC_SHA1_96)
  863. ? EVP_sha1()
  864. : EVP_sha256();
  865. unsigned int mac_out_len = 0;
  866. uint8_t full_mac[64];
  867. HMAC(imd, k1_copy, (int)k1_len, out_pkt + 4, cur_off - 4, full_mac, &mac_out_len);
  868. size_t trunc_len = (integ_alg == IPMI_INTEGRITY_ALGORITHM_HMAC_SHA1_96) ? 12 : 16;
  869. memcpy(out_pkt + cur_off, full_mac, trunc_len);
  870. cur_off += trunc_len;
  871. }
  872. (void)sendto(fd, out_pkt, cur_off, 0, (const struct sockaddr *)cli, clilen);
  873. return;
  874. }
  875. pthread_mutex_unlock(&st->mu);
  876. }
  877. static void *ipmi_udp_worker(void *arg) {
  878. SimStation *st = (SimStation *)arg;
  879. int fd = -1;
  880. uint8_t buf[2048];
  881. while (g_running) {
  882. if (st->bmc_up && fd < 0) {
  883. fd = socket(AF_INET, SOCK_DGRAM, 0);
  884. if (fd >= 0) {
  885. int one = 1;
  886. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  887. struct sockaddr_in addr;
  888. memset(&addr, 0, sizeof(addr));
  889. addr.sin_family = AF_INET;
  890. addr.sin_port = htons((uint16_t)st->ipmi_port);
  891. inet_pton(AF_INET, st->ip, &addr.sin_addr);
  892. if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  893. fprintf(stderr, "Failed to bind IPMI UDP %s:%d: %s\n", st->ip, st->ipmi_port, strerror(errno));
  894. close(fd);
  895. fd = -1;
  896. usleep(100000);
  897. continue;
  898. }
  899. }
  900. } else if (!st->bmc_up && fd >= 0) {
  901. close(fd);
  902. fd = -1;
  903. }
  904. if (fd < 0) {
  905. usleep(40000);
  906. continue;
  907. }
  908. struct pollfd pfd = { .fd = fd, .events = POLLIN, .revents = 0 };
  909. if (poll(&pfd, 1, 50) <= 0) continue;
  910. struct sockaddr_in cli;
  911. socklen_t clilen = sizeof(cli);
  912. ssize_t n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&cli, &clilen);
  913. if (n < 4) continue;
  914. if (!st->bmc_up) {
  915. close(fd);
  916. fd = -1;
  917. continue;
  918. }
  919. st->ipmi_rx_packets++;
  920. /* ASF Presence Ping (message class 0x06) */
  921. if ((buf[3] & 0x1F) == 0x06 && n >= 12 && buf[8] == 0x80) {
  922. uint8_t pong[28];
  923. memset(pong, 0, sizeof(pong));
  924. pong[0] = 0x06; pong[2] = 0xFF; pong[3] = 0x06;
  925. pong[4] = 0x00; pong[5] = 0x00; pong[6] = 0x11; pong[7] = 0xBE;
  926. pong[8] = 0x40; pong[9] = buf[9]; pong[11] = 0x10;
  927. pong[12] = 0x00; pong[13] = 0x00; pong[14] = 0x11; pong[15] = 0xBE;
  928. pong[20] = 0x81; /* IPMI supported */
  929. (void)sendto(fd, pong, sizeof(pong), 0, (struct sockaddr *)&cli, clilen);
  930. continue;
  931. }
  932. if ((buf[3] & 0x1F) != 0x07 || n < 5) continue;
  933. if (buf[4] == 0x00) {
  934. handle_ipmi_v15_auth_cap(fd, &cli, clilen, buf, n);
  935. } else if (buf[4] == 0x06) {
  936. handle_rmcpplus_packet(st, fd, &cli, clilen, buf, n);
  937. }
  938. }
  939. if (fd >= 0) close(fd);
  940. return NULL;
  941. }
  942. /* Optional TCP :22 probe responder (closes socket when `bmc_up == 0`) */
  943. static void *tcp_probe22_worker(void *arg) {
  944. SimStation *st = (SimStation *)arg;
  945. if (st->probe22_port <= 0) return NULL;
  946. int lfd = -1;
  947. while (g_running) {
  948. if (st->bmc_up && lfd < 0) {
  949. lfd = socket(AF_INET, SOCK_STREAM, 0);
  950. if (lfd >= 0) {
  951. int one = 1;
  952. setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  953. struct sockaddr_in a;
  954. memset(&a, 0, sizeof(a));
  955. a.sin_family = AF_INET;
  956. a.sin_port = htons((uint16_t)st->probe22_port);
  957. inet_pton(AF_INET, st->ip, &a.sin_addr);
  958. if (bind(lfd, (struct sockaddr *)&a, sizeof(a)) == 0 && listen(lfd, 8) == 0) {
  959. fcntl(lfd, F_SETFL, O_NONBLOCK);
  960. } else {
  961. close(lfd);
  962. lfd = -1;
  963. }
  964. }
  965. } else if (!st->bmc_up && lfd >= 0) {
  966. close(lfd);
  967. lfd = -1;
  968. }
  969. if (lfd >= 0) {
  970. struct sockaddr_in cli;
  971. socklen_t clilen = sizeof(cli);
  972. int cfd = accept(lfd, (struct sockaddr *)&cli, &clilen);
  973. if (cfd >= 0) {
  974. close(cfd);
  975. continue;
  976. }
  977. }
  978. usleep(40000);
  979. }
  980. if (lfd >= 0) close(lfd);
  981. return NULL;
  982. }
  983. /* Local UART bridge acceptor (`dut_and_hood_sim.py` connects here per station) */
  984. static void *uart_bridge_worker(void *arg) {
  985. SimStation *st = (SimStation *)arg;
  986. int lfd = socket(AF_INET, SOCK_STREAM, 0);
  987. int one = 1;
  988. setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  989. struct sockaddr_in a;
  990. memset(&a, 0, sizeof(a));
  991. a.sin_family = AF_INET;
  992. a.sin_port = htons((uint16_t)st->uart_bridge_port);
  993. inet_pton(AF_INET, "127.0.0.1", &a.sin_addr);
  994. if (bind(lfd, (struct sockaddr *)&a, sizeof(a)) < 0 || listen(lfd, 4) < 0) {
  995. fprintf(stderr, "Failed to bind UART bridge 127.0.0.1:%d\n", st->uart_bridge_port);
  996. close(lfd);
  997. return NULL;
  998. }
  999. st->uart_listen_fd = lfd;
  1000. uint8_t drain_buf[4096];
  1001. while (g_running) {
  1002. /* When no SSH :2200 client is attached, drain UART TX so disconnected output is genuinely lost (like real HW!) */
  1003. if (!st->ssh_active) {
  1004. pthread_mutex_lock(&st->uart_mu);
  1005. if (st->uart_client_fd >= 0) {
  1006. while (recv(st->uart_client_fd, drain_buf, sizeof(drain_buf), MSG_DONTWAIT) > 0) {}
  1007. }
  1008. pthread_mutex_unlock(&st->uart_mu);
  1009. }
  1010. struct pollfd pfd = { .fd = lfd, .events = POLLIN, .revents = 0 };
  1011. if (poll(&pfd, 1, 40) <= 0) continue;
  1012. int cfd = accept(lfd, NULL, NULL);
  1013. if (cfd < 0) continue;
  1014. setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
  1015. fcntl(cfd, F_SETFL, O_NONBLOCK);
  1016. pthread_mutex_lock(&st->uart_mu);
  1017. if (st->uart_client_fd >= 0) close(st->uart_client_fd);
  1018. st->uart_client_fd = cfd;
  1019. pthread_mutex_unlock(&st->uart_mu);
  1020. }
  1021. close(lfd);
  1022. return NULL;
  1023. }
  1024. typedef struct {
  1025. SimStation *st;
  1026. ssh_session session;
  1027. } SshSessionArgs;
  1028. static void *ssh_client_handler(void *arg) {
  1029. SshSessionArgs *sa = (SshSessionArgs *)arg;
  1030. SimStation *st = sa->st;
  1031. ssh_session session = sa->session;
  1032. free(sa);
  1033. if (ssh_handle_key_exchange(session) != SSH_OK) {
  1034. ssh_disconnect(session);
  1035. ssh_free(session);
  1036. return NULL;
  1037. }
  1038. int authed = 0;
  1039. ssh_channel chan = NULL;
  1040. int shell_ready = 0;
  1041. int loops = 0;
  1042. while (g_running && st->serial_up && !st->kill_active_ssh && !shell_ready && loops++ < 400) {
  1043. ssh_message msg = ssh_message_get(session);
  1044. if (!msg) break;
  1045. int mtype = ssh_message_type(msg);
  1046. int msub = ssh_message_subtype(msg);
  1047. if (mtype == SSH_REQUEST_AUTH) {
  1048. if (msub == SSH_AUTH_METHOD_PASSWORD) {
  1049. #pragma GCC diagnostic push
  1050. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1051. const char *pw = ssh_message_auth_password(msg);
  1052. #pragma GCC diagnostic pop
  1053. if (pw && (strcmp(pw, "Admin123@pl") == 0 || strcmp(pw, "ADMIN") == 0)) {
  1054. authed = 1;
  1055. ssh_message_auth_reply_success(msg, 0);
  1056. ssh_message_free(msg);
  1057. continue;
  1058. }
  1059. }
  1060. ssh_message_auth_set_methods(msg, SSH_AUTH_METHOD_PASSWORD);
  1061. ssh_message_reply_default(msg);
  1062. } else if (mtype == SSH_REQUEST_CHANNEL_OPEN && msub == SSH_CHANNEL_SESSION && authed) {
  1063. chan = ssh_message_channel_request_open_reply_accept(msg);
  1064. } else if (mtype == SSH_REQUEST_CHANNEL && chan != NULL) {
  1065. if (msub == SSH_CHANNEL_REQUEST_PTY) {
  1066. ssh_message_channel_request_reply_success(msg);
  1067. } else if (msub == SSH_CHANNEL_REQUEST_SHELL || msub == SSH_CHANNEL_REQUEST_EXEC) {
  1068. ssh_message_channel_request_reply_success(msg);
  1069. shell_ready = 1;
  1070. } else {
  1071. ssh_message_channel_request_reply_success(msg);
  1072. }
  1073. } else {
  1074. ssh_message_reply_default(msg);
  1075. }
  1076. ssh_message_free(msg);
  1077. }
  1078. if (!shell_ready || !chan) {
  1079. if (chan) ssh_channel_free(chan);
  1080. ssh_disconnect(session);
  1081. ssh_free(session);
  1082. return NULL;
  1083. }
  1084. st->ssh_connect_count++;
  1085. st->ssh_active = 1;
  1086. uint8_t sbuf[4096];
  1087. uint8_t ubuf[8192];
  1088. while (g_running && st->serial_up && !st->kill_active_ssh &&
  1089. ssh_channel_is_open(chan) && !ssh_channel_is_eof(chan)) {
  1090. int did_io = 0;
  1091. /* 1. Drain bytes sent from stl_native_server -> forward to Python DUT state machine */
  1092. int nr = ssh_channel_read_nonblocking(chan, sbuf, sizeof(sbuf), 0);
  1093. if (nr == SSH_ERROR) break;
  1094. if (nr > 0) {
  1095. did_io = 1;
  1096. pthread_mutex_lock(&st->uart_mu);
  1097. if (st->uart_client_fd >= 0) {
  1098. (void)send(st->uart_client_fd, sbuf, (size_t)nr, MSG_NOSIGNAL);
  1099. }
  1100. pthread_mutex_unlock(&st->uart_mu);
  1101. }
  1102. /* 2. Drain BIOS UART bytes from Python DUT state machine -> forward to stl_native_server */
  1103. pthread_mutex_lock(&st->uart_mu);
  1104. int ufd = st->uart_client_fd;
  1105. ssize_t un = -1;
  1106. if (ufd >= 0) {
  1107. un = recv(ufd, ubuf, sizeof(ubuf), MSG_DONTWAIT);
  1108. if (un == 0 || (un < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
  1109. close(st->uart_client_fd);
  1110. st->uart_client_fd = -1;
  1111. un = -1;
  1112. }
  1113. }
  1114. pthread_mutex_unlock(&st->uart_mu);
  1115. if (un > 0) {
  1116. did_io = 1;
  1117. if (ssh_channel_write(chan, ubuf, (uint32_t)un) == SSH_ERROR) {
  1118. break;
  1119. }
  1120. }
  1121. if (!did_io) usleep(4000);
  1122. }
  1123. st->ssh_active = 0;
  1124. ssh_channel_send_eof(chan);
  1125. ssh_channel_close(chan);
  1126. ssh_channel_free(chan);
  1127. ssh_disconnect(session);
  1128. ssh_free(session);
  1129. return NULL;
  1130. }
  1131. static void *ssh_console_worker(void *arg) {
  1132. SimStation *st = (SimStation *)arg;
  1133. ssh_bind sbind = NULL;
  1134. int listen_fd = -1;
  1135. while (g_running) {
  1136. if (st->serial_up && !sbind) {
  1137. sbind = ssh_bind_new();
  1138. unsigned int port = (unsigned int)st->ssh_port;
  1139. ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_BINDADDR, st->ip);
  1140. ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_BINDPORT, &port);
  1141. ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, g_hostkey_path);
  1142. if (ssh_bind_listen(sbind) < 0) {
  1143. ssh_bind_free(sbind);
  1144. sbind = NULL;
  1145. usleep(100000);
  1146. continue;
  1147. }
  1148. ssh_bind_set_blocking(sbind, 0);
  1149. listen_fd = ssh_bind_get_fd(sbind);
  1150. st->kill_active_ssh = 0;
  1151. } else if (!st->serial_up && sbind) {
  1152. st->kill_active_ssh = 1;
  1153. ssh_bind_free(sbind);
  1154. sbind = NULL;
  1155. listen_fd = -1;
  1156. }
  1157. if (sbind && listen_fd >= 0) {
  1158. struct pollfd pfd = { .fd = listen_fd, .events = POLLIN, .revents = 0 };
  1159. if (poll(&pfd, 1, 40) > 0 && (pfd.revents & POLLIN)) {
  1160. ssh_session sess = ssh_new();
  1161. if (ssh_bind_accept(sbind, sess) == SSH_OK) {
  1162. SshSessionArgs *sa = (SshSessionArgs *)calloc(1, sizeof(SshSessionArgs));
  1163. sa->st = st;
  1164. sa->session = sess;
  1165. pthread_t tid;
  1166. if (pthread_create(&tid, NULL, ssh_client_handler, sa) == 0) {
  1167. pthread_detach(tid);
  1168. } else {
  1169. free(sa);
  1170. ssh_free(sess);
  1171. }
  1172. } else {
  1173. ssh_free(sess);
  1174. }
  1175. }
  1176. } else {
  1177. usleep(40000);
  1178. }
  1179. }
  1180. if (sbind) ssh_bind_free(sbind);
  1181. return NULL;
  1182. }
  1183. static void handle_ctrl_line(int cfd, char *line) {
  1184. char cmd[32] = "";
  1185. if (sscanf(line, "%31s", cmd) != 1) return;
  1186. if (strcmp(cmd, "STATUS") == 0) {
  1187. char out[8192];
  1188. size_t off = 0;
  1189. off += (size_t)snprintf(out + off, sizeof(out) - off, "{\"stations\":[");
  1190. for (int i = 0; i < g_num_stations; i++) {
  1191. SimStation *st = &g_stations[i];
  1192. off += (size_t)snprintf(
  1193. out + off, sizeof(out) - off,
  1194. "%s{\"idx\":%d,\"ip\":\"%s\",\"bmc_up\":%s,\"serial_up\":%s,\"power_on\":%s,"
  1195. "\"off_count\":%d,\"on_count\":%d,\"reset_count\":%d,\"cycle_count\":%d,"
  1196. "\"warm_reset_count\":%d,\"ipmi_rx_packets\":%d,\"ssh_connect_count\":%d,\"ssh_active\":%s}",
  1197. (i > 0) ? "," : "",
  1198. st->idx, st->ip,
  1199. st->bmc_up ? "true" : "false",
  1200. st->serial_up ? "true" : "false",
  1201. st->power_on ? "true" : "false",
  1202. st->off_count, st->on_count, st->reset_count, st->cycle_count,
  1203. st->warm_reset_count, st->ipmi_rx_packets, st->ssh_connect_count,
  1204. st->ssh_active ? "true" : "false");
  1205. }
  1206. off += (size_t)snprintf(out + off, sizeof(out) - off, "]}\n");
  1207. (void)send(cfd, out, off, MSG_NOSIGNAL);
  1208. return;
  1209. }
  1210. if (strcmp(cmd, "SET_LINK") == 0) {
  1211. int idx = -1, b_up = 1, s_up = 1;
  1212. if (sscanf(line, "%*s %d %d %d", &idx, &b_up, &s_up) == 3 &&
  1213. idx >= 0 && idx < g_num_stations) {
  1214. SimStation *st = &g_stations[idx];
  1215. st->bmc_up = b_up ? 1 : 0;
  1216. if (!s_up && st->serial_up) {
  1217. st->kill_active_ssh = 1;
  1218. }
  1219. st->serial_up = s_up ? 1 : 0;
  1220. const char *ok = "{\"ok\":true}\n";
  1221. (void)send(cfd, ok, strlen(ok), MSG_NOSIGNAL);
  1222. return;
  1223. }
  1224. }
  1225. if (strcmp(cmd, "SET_POWER") == 0) {
  1226. int idx = -1, pwr = 1;
  1227. if (sscanf(line, "%*s %d %d", &idx, &pwr) == 2 &&
  1228. idx >= 0 && idx < g_num_stations) {
  1229. g_stations[idx].power_on = pwr ? 1 : 0;
  1230. const char *ok = "{\"ok\":true}\n";
  1231. (void)send(cfd, ok, strlen(ok), MSG_NOSIGNAL);
  1232. return;
  1233. }
  1234. }
  1235. if (strcmp(cmd, "SET_TEMPS") == 0) {
  1236. int idx = -1;
  1237. char rest[512] = "";
  1238. if (sscanf(line, "%*s %d %511s", &idx, rest) == 2 &&
  1239. idx >= 0 && idx < g_num_stations) {
  1240. SimStation *st = &g_stations[idx];
  1241. pthread_mutex_lock(&st->mu);
  1242. char *save = NULL;
  1243. char *tok = strtok_r(rest, ",", &save);
  1244. int slot = 0;
  1245. while (tok && slot < NUM_DIMMS) {
  1246. int v = atoi(tok);
  1247. if (v < 0) {
  1248. st->dimm_present[slot] = 0;
  1249. st->dimm_temp_c[slot] = 0;
  1250. } else {
  1251. st->dimm_present[slot] = 1;
  1252. st->dimm_temp_c[slot] = v;
  1253. }
  1254. slot++;
  1255. tok = strtok_r(NULL, ",", &save);
  1256. }
  1257. pthread_mutex_unlock(&st->mu);
  1258. const char *ok = "{\"ok\":true}\n";
  1259. (void)send(cfd, ok, strlen(ok), MSG_NOSIGNAL);
  1260. return;
  1261. }
  1262. }
  1263. if (strcmp(cmd, "QUIT") == 0) {
  1264. g_running = 0;
  1265. const char *ok = "{\"ok\":true}\n";
  1266. (void)send(cfd, ok, strlen(ok), MSG_NOSIGNAL);
  1267. return;
  1268. }
  1269. const char *err = "{\"ok\":false,\"error\":\"bad_command\"}\n";
  1270. (void)send(cfd, err, strlen(err), MSG_NOSIGNAL);
  1271. }
  1272. /*
  1273. * Usage:
  1274. * bmc_ssh_hil_sim --ctrl-port 19199 --uart-base 19200 \
  1275. * --ipmi-port 623 --ssh-port 2200 --probe22-port 22 \
  1276. * 127.0.2.166 127.0.2.165 127.0.2.167 127.0.2.168
  1277. */
  1278. int main(int argc, char **argv) {
  1279. signal(SIGPIPE, SIG_IGN);
  1280. ipmi_rmcpplus_init();
  1281. ssh_init();
  1282. int ctrl_port = 19199;
  1283. int uart_base = 19200;
  1284. int ipmi_port = 623;
  1285. int ssh_port = 2200;
  1286. int probe22_port = 22;
  1287. int argi = 1;
  1288. while (argi < argc && strncmp(argv[argi], "--", 2) == 0) {
  1289. if (strcmp(argv[argi], "--ctrl-port") == 0 && argi + 1 < argc) {
  1290. ctrl_port = atoi(argv[++argi]);
  1291. } else if (strcmp(argv[argi], "--uart-base") == 0 && argi + 1 < argc) {
  1292. uart_base = atoi(argv[++argi]);
  1293. } else if (strcmp(argv[argi], "--ipmi-port") == 0 && argi + 1 < argc) {
  1294. ipmi_port = atoi(argv[++argi]);
  1295. } else if (strcmp(argv[argi], "--ssh-port") == 0 && argi + 1 < argc) {
  1296. ssh_port = atoi(argv[++argi]);
  1297. } else if (strcmp(argv[argi], "--probe22-port") == 0 && argi + 1 < argc) {
  1298. probe22_port = atoi(argv[++argi]);
  1299. } else if (strcmp(argv[argi], "--hostkey") == 0 && argi + 1 < argc) {
  1300. snprintf(g_hostkey_path, sizeof(g_hostkey_path), "%s", argv[++argi]);
  1301. }
  1302. argi++;
  1303. }
  1304. if (g_hostkey_path[0] == '\0') {
  1305. snprintf(g_hostkey_path, sizeof(g_hostkey_path), "/tmp/stl_hil_ssh_hostkey_%d", (int)getpid());
  1306. ssh_key hkey = NULL;
  1307. if (ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &hkey) != SSH_OK ||
  1308. ssh_pki_export_privkey_file(hkey, NULL, NULL, NULL, g_hostkey_path) != SSH_OK) {
  1309. fprintf(stderr, "Failed to generate ephemeral RSA hostkey at %s\n", g_hostkey_path);
  1310. return 1;
  1311. }
  1312. ssh_key_free(hkey);
  1313. }
  1314. for (; argi < argc && g_num_stations < MAX_STATIONS; argi++) {
  1315. SimStation *st = &g_stations[g_num_stations];
  1316. memset(st, 0, sizeof(*st));
  1317. st->idx = g_num_stations;
  1318. snprintf(st->ip, sizeof(st->ip), "%s", argv[argi]);
  1319. st->ipmi_port = ipmi_port;
  1320. st->ssh_port = ssh_port;
  1321. st->probe22_port = probe22_port;
  1322. st->uart_bridge_port = uart_base + g_num_stations;
  1323. st->bmc_up = 1;
  1324. st->serial_up = 1;
  1325. st->power_on = 1;
  1326. st->next_ms_sid = (uint32_t)(0x10000000U + (uint32_t)g_num_stations * 0x10000U);
  1327. st->uart_listen_fd = -1;
  1328. st->uart_client_fd = -1;
  1329. pthread_mutex_init(&st->mu, NULL);
  1330. pthread_mutex_init(&st->uart_mu, NULL);
  1331. for (int d = 0; d < NUM_DIMMS; d++) {
  1332. st->dimm_present[d] = 1;
  1333. st->dimm_temp_c[d] = 42 + (d % 6);
  1334. }
  1335. build_station_sdr_repository(st);
  1336. g_num_stations++;
  1337. }
  1338. if (g_num_stations == 0) {
  1339. fprintf(stderr, "No station IPs specified\n");
  1340. return 1;
  1341. }
  1342. int ctrl_lfd = socket(AF_INET, SOCK_STREAM, 0);
  1343. int one = 1;
  1344. setsockopt(ctrl_lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  1345. struct sockaddr_in caddr;
  1346. memset(&caddr, 0, sizeof(caddr));
  1347. caddr.sin_family = AF_INET;
  1348. caddr.sin_port = htons((uint16_t)ctrl_port);
  1349. inet_pton(AF_INET, "127.0.0.1", &caddr.sin_addr);
  1350. if (bind(ctrl_lfd, (struct sockaddr *)&caddr, sizeof(caddr)) < 0 || listen(ctrl_lfd, 16) < 0) {
  1351. fprintf(stderr, "Failed to bind control port 127.0.0.1:%d: %s\n", ctrl_port, strerror(errno));
  1352. return 1;
  1353. }
  1354. for (int i = 0; i < g_num_stations; i++) {
  1355. pthread_t t_ipmi, t_ssh, t_uart, t_p22;
  1356. pthread_create(&t_ipmi, NULL, ipmi_udp_worker, &g_stations[i]);
  1357. pthread_detach(t_ipmi);
  1358. pthread_create(&t_ssh, NULL, ssh_console_worker, &g_stations[i]);
  1359. pthread_detach(t_ssh);
  1360. pthread_create(&t_uart, NULL, uart_bridge_worker, &g_stations[i]);
  1361. pthread_detach(t_uart);
  1362. if (g_stations[i].probe22_port > 0) {
  1363. pthread_create(&t_p22, NULL, tcp_probe22_worker, &g_stations[i]);
  1364. pthread_detach(t_p22);
  1365. }
  1366. }
  1367. printf("READY ctrl=127.0.0.1:%d stations=%d\n", ctrl_port, g_num_stations);
  1368. fflush(stdout);
  1369. while (g_running) {
  1370. struct pollfd pfd = { .fd = ctrl_lfd, .events = POLLIN, .revents = 0 };
  1371. if (poll(&pfd, 1, 100) <= 0) continue;
  1372. int cfd = accept(ctrl_lfd, NULL, NULL);
  1373. if (cfd < 0) continue;
  1374. char lbuf[1024];
  1375. ssize_t n = recv(cfd, lbuf, sizeof(lbuf) - 1, 0);
  1376. if (n > 0) {
  1377. lbuf[n] = '\0';
  1378. handle_ctrl_line(cfd, lbuf);
  1379. }
  1380. close(cfd);
  1381. }
  1382. close(ctrl_lfd);
  1383. unlink(g_hostkey_path);
  1384. ssh_finalize();
  1385. return 0;
  1386. }