PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/OpenIPMI-2.0.18/lib/ipmi_lan.c

#
C | 7041 lines | 5799 code | 902 blank | 340 comment | 1110 complexity | fde67a6e79a3f878053cd89526877d82 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * ipmi_lan.c
  3. *
  4. * MontaVista IPMI code for handling IPMI LAN connections
  5. *
  6. * Author: MontaVista Software, Inc.
  7. * Corey Minyard <minyard@mvista.com>
  8. * source@mvista.com
  9. *
  10. * Copyright 2002,2003,2004 MontaVista Software Inc.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public License
  14. * as published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  26. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  27. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this program; if not, write to the Free
  31. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <config.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <sys/stat.h>
  38. #include <sys/poll.h>
  39. #include <sys/time.h>
  40. #include <fcntl.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include <netdb.h>
  46. #include <arpa/inet.h>
  47. #include <OpenIPMI/ipmi_conn.h>
  48. #include <OpenIPMI/ipmi_msgbits.h>
  49. #include <OpenIPMI/ipmi_auth.h>
  50. #include <OpenIPMI/ipmi_err.h>
  51. #include <OpenIPMI/ipmi_lan.h>
  52. #include <OpenIPMI/internal/ipmi_event.h>
  53. #include <OpenIPMI/internal/ipmi_int.h>
  54. #include <OpenIPMI/internal/locked_list.h>
  55. #if defined(DEBUG_MSG) || defined(DEBUG_RAWMSG)
  56. static void
  57. dump_hex(void *vdata, int len)
  58. {
  59. unsigned char *data = vdata;
  60. int i;
  61. for (i=0; i<len; i++) {
  62. if ((i != 0) && ((i % 16) == 0)) {
  63. ipmi_log(IPMI_LOG_DEBUG_CONT, "\n ");
  64. }
  65. ipmi_log(IPMI_LOG_DEBUG_CONT, " %2.2x", data[i]);
  66. }
  67. }
  68. #endif
  69. #define LAN_AUDIT_TIMEOUT 10000000
  70. /* Timeout to wait for IPMI responses, in microseconds. For commands
  71. with side effects, we wait 5 seconds, not one. */
  72. #define LAN_RSP_TIMEOUT 1000000
  73. #define LAN_RSP_TIMEOUT_SIDEEFF 5000000
  74. /* # of times to try a message before we fail it. */
  75. #define LAN_RSP_RETRIES 6
  76. /* Number of microseconds of consecutive failures allowed on an IP
  77. before it is considered failed. */
  78. #define IP_FAIL_TIME 7000000
  79. /* Number of consecutive failures that must occur before an IP is
  80. considered failed. */
  81. #define IP_FAIL_COUNT 4
  82. /* The default for the maximum number of messages that are allowed to be
  83. outstanding. This is a pretty conservative number. */
  84. #define DEFAULT_MAX_OUTSTANDING_MSG_COUNT 2
  85. #define MAX_POSSIBLE_OUTSTANDING_MSG_COUNT 63
  86. typedef struct lan_data_s lan_data_t;
  87. typedef struct audit_timer_info_s
  88. {
  89. int cancelled;
  90. ipmi_con_t *ipmi;
  91. } audit_timer_info_t;
  92. typedef struct lan_timer_info_s
  93. {
  94. int cancelled;
  95. ipmi_con_t *ipmi;
  96. os_hnd_timer_id_t *timer;
  97. unsigned int seq;
  98. } lan_timer_info_t;
  99. typedef struct lan_wait_queue_s
  100. {
  101. lan_timer_info_t *info;
  102. ipmi_addr_t addr;
  103. unsigned int addr_len;
  104. ipmi_msg_t msg;
  105. unsigned char data[IPMI_MAX_MSG_LENGTH];
  106. ipmi_ll_rsp_handler_t rsp_handler;
  107. ipmi_msgi_t *rsp_item;
  108. int side_effects;
  109. struct lan_wait_queue_s *next;
  110. } lan_wait_queue_t;
  111. #define MAX_IP_ADDR 2
  112. /* We must keep this number small, if it's too big and a failure
  113. occurs, we will be outside the sequence number before we switch. */
  114. #define SENDS_BETWEEN_IP_SWITCHES 3
  115. /* Because sizeof(sockaddr_in6) > sizeof(sockaddr_in), this structure
  116. * is used as a replacement of struct sockaddr. */
  117. typedef struct sockaddr_ip_s {
  118. union
  119. {
  120. struct sockaddr s_addr;
  121. struct sockaddr_in s_addr4;
  122. #ifdef PF_INET6
  123. struct sockaddr_in6 s_addr6;
  124. #endif
  125. } s_ipsock;
  126. socklen_t ip_addr_len;
  127. } sockaddr_ip_t;
  128. struct ipmi_rmcpp_auth_s
  129. {
  130. lan_data_t *lan;
  131. int addr_num;
  132. uint8_t role;
  133. /* Filled in by the auth algorithm. */
  134. unsigned char my_rand[16];
  135. unsigned int my_rand_len;
  136. unsigned char mgsys_rand[16];
  137. unsigned int mgsys_rand_len;
  138. unsigned char mgsys_guid[16];
  139. unsigned int mgsys_guid_len;
  140. unsigned char sik[20];
  141. unsigned int sik_len;
  142. unsigned char k1[20];
  143. unsigned int k1_len;
  144. unsigned char k2[20];
  145. unsigned int k2_len;
  146. };
  147. typedef struct lan_conn_parms_s
  148. {
  149. unsigned int num_ip_addr;
  150. char *ip_addr_str[MAX_IP_ADDR];
  151. char *ip_port_str[MAX_IP_ADDR];
  152. sockaddr_ip_t ip_addr[MAX_IP_ADDR];
  153. unsigned int authtype;
  154. unsigned int privilege;
  155. unsigned char username[IPMI_USERNAME_MAX];
  156. unsigned int username_len;
  157. unsigned char password[IPMI_PASSWORD_MAX];
  158. unsigned int password_len;
  159. unsigned int conf;
  160. unsigned int integ;
  161. unsigned int auth;
  162. unsigned int name_lookup_only;
  163. unsigned char bmc_key[IPMI_PASSWORD_MAX];
  164. unsigned int bmc_key_len;
  165. } lan_conn_parms_t;
  166. typedef struct lan_link_s lan_link_t;
  167. struct lan_link_s
  168. {
  169. lan_link_t *next, *prev;
  170. lan_data_t *lan;
  171. };
  172. typedef struct lan_fd_s lan_fd_t;
  173. typedef struct lan_stat_info_s
  174. {
  175. #define STAT_RECV_PACKETS 0
  176. #define STAT_XMIT_PACKETS 1
  177. #define STAT_REXMITS 2
  178. #define STAT_TIMED_OUT 3
  179. #define STAT_INVALID_RMCP 4
  180. #define STAT_TOO_SHORT 5
  181. #define STAT_INVALID_AUTH 6
  182. #define STAT_BAD_SESSION_ID 7
  183. #define STAT_AUTH_FAIL 8
  184. #define STAT_DUPLICATES 9
  185. #define STAT_SEQ_OUT_OF_RANGE 10
  186. #define STAT_ASYNC_EVENTS 11
  187. #define STAT_CONN_DOWN 12
  188. #define STAT_CONN_UP 13
  189. #define STAT_BAD_SIZE 14
  190. #define STAT_DECRYPT_FAIL 15
  191. #define STAT_INVALID_PAYLOAD 16
  192. #define STAT_SEQ_ERR 17
  193. #define STAT_RSP_NO_CMD 18
  194. #define NUM_STATS 19
  195. /* Statistics */
  196. void *stats[NUM_STATS];
  197. } lan_stat_info_t;
  198. static const char *lan_stat_names[NUM_STATS] =
  199. {
  200. "lan_recv_packets",
  201. "lan_xmit_packets",
  202. "lan_rexmits",
  203. "lan_timed_out",
  204. "lan_invalid_rmcp",
  205. "lan_too_short",
  206. "lan_invalid_auth",
  207. "lan_bad_session_id",
  208. "lan_auth_fail",
  209. "lan_duplicates",
  210. "lan_seq_out_of_range",
  211. "lan_async_events",
  212. "lan_conn_down",
  213. "lan_conn_up",
  214. "lan_bad_size",
  215. "lan_decrypt_fail",
  216. "lan_invalid_payload",
  217. "lan_seq_err",
  218. "lan_rsp_no_cmd"
  219. };
  220. /* Per-IP specific information. */
  221. typedef struct lan_ip_data_s
  222. {
  223. int working;
  224. unsigned int consecutive_failures;
  225. struct timeval failure_time;
  226. /* For both RMCP and RMCP+. For RMCP+, the session id is the one
  227. I receive and the sequence numbers are the authenticated
  228. ones. */
  229. unsigned char working_authtype;
  230. uint32_t session_id;
  231. uint32_t outbound_seq_num;
  232. uint32_t inbound_seq_num;
  233. uint16_t recv_msg_map;
  234. /* RMCP+ specific info */
  235. uint32_t unauth_out_seq_num;
  236. uint32_t unauth_in_seq_num;
  237. uint16_t unauth_recv_msg_map;
  238. unsigned char working_integ;
  239. unsigned char working_conf;
  240. uint32_t mgsys_session_id;
  241. ipmi_rmcpp_auth_t ainfo;
  242. /* Used to hold the session id before the connection is up. */
  243. uint32_t precon_session_id;
  244. uint32_t precon_mgsys_session_id;
  245. ipmi_rmcpp_confidentiality_t *conf_info;
  246. void *conf_data;
  247. ipmi_rmcpp_integrity_t *integ_info;
  248. void *integ_data;
  249. /* Use for linked-lists of IP addresses. */
  250. lan_link_t ip_link;
  251. } lan_ip_data_t;
  252. #if IPMI_MAX_MSG_LENGTH > 80
  253. # define LAN_MAX_RAW_MSG IPMI_MAX_MSG_LENGTH
  254. #else
  255. # define LAN_MAX_RAW_MSG 80 /* Enough to hold the rmcp+ session messages */
  256. #endif
  257. struct lan_data_s
  258. {
  259. unsigned int refcount;
  260. unsigned int users;
  261. ipmi_con_t *ipmi;
  262. lan_fd_t *fd;
  263. int fd_slot;
  264. unsigned char slave_addr[MAX_IPMI_USED_CHANNELS];
  265. int is_active;
  266. /* Have we already been started? */
  267. int started;
  268. /* Are we currently in cleanup? Don't allow any outgoing messages. */
  269. int in_cleanup;
  270. /* Protects modifiecations to working, curr_ip_addr, RMCP
  271. sequence numbers, the con_change_handler, and other
  272. connection-related data. Note that if the seq_num_lock must
  273. also be held, it must be locked before this lock. */
  274. ipmi_lock_t *ip_lock;
  275. /* If 0, we don't have a connection to the BMC right now. */
  276. int connected;
  277. /* If 0, we have not yet initialized */
  278. int initialized;
  279. /* If 0, the OEM handlers have not been called. */
  280. int oem_conn_handlers_called;
  281. /* Number of packets sent on the connection. Used to track when
  282. to switch between IP addresses. */
  283. unsigned int num_sends;
  284. /* The IP address we are currently using. */
  285. unsigned int curr_ip_addr;
  286. /* Data about each IP address */
  287. lan_ip_data_t ip[MAX_IP_ADDR];
  288. /* We keep a session on each LAN connection. I don't think all
  289. systems require that, but it's safer. */
  290. /* From the get channel auth */
  291. unsigned char oem_iana[3];
  292. unsigned char oem_aux;
  293. /* Parms we were configured with. */
  294. lan_conn_parms_t cparm;
  295. /* IPMI LAN 1.5 specific info. */
  296. unsigned char chosen_authtype;
  297. unsigned char challenge_string[16];
  298. ipmi_authdata_t authdata;
  299. /* RMCP+ specific info */
  300. unsigned int use_two_keys : 1;
  301. struct {
  302. unsigned int inuse : 1;
  303. ipmi_addr_t addr;
  304. unsigned int addr_len;
  305. ipmi_msg_t msg;
  306. unsigned char data[LAN_MAX_RAW_MSG];
  307. ipmi_ll_rsp_handler_t rsp_handler;
  308. ipmi_msgi_t *rsp_item;
  309. int use_orig_addr;
  310. ipmi_addr_t orig_addr;
  311. unsigned int orig_addr_len;
  312. os_hnd_timer_id_t *timer;
  313. lan_timer_info_t *timer_info;
  314. int retries_left;
  315. int side_effects;
  316. /* If -1, just use the normal algorithm. If not -1, force to
  317. this address. */
  318. int addr_num;
  319. /* The number of the last IP address sent on. */
  320. int last_ip_num;
  321. } seq_table[64];
  322. ipmi_lock_t *seq_num_lock;
  323. /* The current sequence number. Note that we reserve sequence
  324. number 0 for our own neferous purposes. */
  325. unsigned int last_seq;
  326. /* The number of messages that are outstanding with the remote
  327. MC. */
  328. unsigned int outstanding_msg_count;
  329. /* The maximum number of outstanding messages. This must NEVER be
  330. larger than 63 (64 sequence numbers minus 1 for our reserved
  331. sequence zero. */
  332. unsigned int max_outstanding_msg_count;
  333. /* List of messages waiting to be sent. */
  334. lan_wait_queue_t *wait_q, *wait_q_tail;
  335. locked_list_t *event_handlers;
  336. os_hnd_timer_id_t *audit_timer;
  337. audit_timer_info_t *audit_info;
  338. /* Handles connection shutdown reporting. */
  339. ipmi_ll_con_closed_cb close_done;
  340. void *close_cb_data;
  341. /* This lock is used to assure that the conn changes occur in
  342. proper order. The user code is called with this lock held, but
  343. it should be harmless to the user as this is the only use for
  344. it. But the user cannot do a wait on I/O in the handler. */
  345. ipmi_lock_t *con_change_lock;
  346. locked_list_t *con_change_handlers;
  347. locked_list_t *ipmb_change_handlers;
  348. lan_link_t link;
  349. locked_list_t *lan_stat_list;
  350. };
  351. /************************************************************************
  352. *
  353. * Authentication and encryption information and functions.
  354. *
  355. ***********************************************************************/
  356. extern ipmi_payload_t _ipmi_payload;
  357. static int
  358. open_format_msg(ipmi_con_t *ipmi,
  359. const ipmi_addr_t *addr,
  360. unsigned int addr_len,
  361. const ipmi_msg_t *msg,
  362. unsigned char *out_data,
  363. unsigned int *out_data_len,
  364. int *out_of_session,
  365. unsigned char seq)
  366. {
  367. unsigned char *tmsg = out_data;
  368. if (msg->data_len > *out_data_len)
  369. return E2BIG;
  370. memcpy(tmsg, msg->data, msg->data_len);
  371. tmsg[0] = seq; /* We use the message tag for the sequence # */
  372. *out_of_session = 1;
  373. *out_data_len = msg->data_len;
  374. return 0;
  375. }
  376. static int
  377. open_get_recv_seq(ipmi_con_t *ipmi,
  378. unsigned char *data,
  379. unsigned int data_len,
  380. unsigned char *seq)
  381. {
  382. if (data_len < 1) { /* Minimum size of an IPMI msg. */
  383. if (DEBUG_RAWMSG || DEBUG_MSG_ERR)
  384. ipmi_log(IPMI_LOG_DEBUG,
  385. "%sDropped message because too small(7)",
  386. IPMI_CONN_NAME(ipmi));
  387. return EINVAL;
  388. }
  389. *seq = data[0];
  390. return 0;
  391. }
  392. static int
  393. open_handle_recv(ipmi_con_t *ipmi,
  394. ipmi_msgi_t *rspi,
  395. ipmi_addr_t *orig_addr,
  396. unsigned int orig_addr_len,
  397. ipmi_msg_t *orig_msg,
  398. unsigned char *data,
  399. unsigned int data_len)
  400. {
  401. ipmi_msg_t *msg = &(rspi->msg);
  402. if (data_len > sizeof(rspi->data))
  403. return E2BIG;
  404. memcpy(rspi->data, data, data_len);
  405. msg->data = rspi->data;
  406. msg->data_len = data_len;
  407. return 0;
  408. }
  409. static void
  410. open_handle_recv_async(ipmi_con_t *ipmi,
  411. unsigned char *tmsg,
  412. unsigned int data_len)
  413. {
  414. }
  415. static int
  416. open_get_msg_tag(unsigned char *tmsg,
  417. unsigned int data_len,
  418. unsigned char *tag)
  419. {
  420. if (data_len < 8)
  421. return EINVAL;
  422. *tag = ipmi_get_uint32(tmsg+4) - 1; /* session id */
  423. return 0;
  424. }
  425. static ipmi_payload_t open_payload =
  426. { open_format_msg, open_get_recv_seq, open_handle_recv,
  427. open_handle_recv_async, open_get_msg_tag };
  428. static ipmi_payload_t *payloads[64] =
  429. {
  430. &_ipmi_payload,
  431. [IPMI_RMCPP_PAYLOAD_TYPE_OPEN_SESSION_REQUEST] = &open_payload,
  432. [IPMI_RMCPP_PAYLOAD_TYPE_OPEN_SESSION_RESPONSE] = &open_payload
  433. };
  434. typedef struct payload_entry_s payload_entry_t;
  435. struct payload_entry_s
  436. {
  437. unsigned int payload_type;
  438. unsigned char iana[3];
  439. unsigned int payload_id;
  440. ipmi_payload_t *payload;
  441. payload_entry_t *next;
  442. };
  443. /* Note that we only add payloads to the head, so no lock is required
  444. except for addition. */
  445. static ipmi_lock_t *lan_payload_lock = NULL;
  446. payload_entry_t *oem_payload_list = NULL;
  447. int
  448. ipmi_rmcpp_register_payload(unsigned int payload_type,
  449. ipmi_payload_t *payload)
  450. {
  451. if ((payload_type == IPMI_RMCPP_PAYLOAD_TYPE_IPMI)
  452. || (payload_type == IPMI_RMCPP_PAYLOAD_TYPE_OEM_EXPLICIT)
  453. || (payload_type == IPMI_RMCPP_PAYLOAD_TYPE_OPEN_SESSION_REQUEST)
  454. || (payload_type == IPMI_RMCPP_PAYLOAD_TYPE_OPEN_SESSION_RESPONSE)
  455. || (payload_type >= 64)
  456. || ((payload_type >= 0x20) && (payload_type <= 0x27))) /* No OEM here*/
  457. {
  458. return EINVAL;
  459. }
  460. ipmi_lock(lan_payload_lock);
  461. if (payloads[payload_type] && payload) {
  462. ipmi_unlock(lan_payload_lock);
  463. return EAGAIN;
  464. }
  465. payloads[payload_type] = payload;
  466. ipmi_unlock(lan_payload_lock);
  467. return 0;
  468. }
  469. int
  470. ipmi_rmcpp_register_oem_payload(unsigned int payload_type,
  471. unsigned char iana[3],
  472. unsigned int payload_id,
  473. ipmi_payload_t *payload)
  474. {
  475. payload_entry_t *e;
  476. payload_entry_t *c;
  477. e = ipmi_mem_alloc(sizeof(*e));
  478. if (!e)
  479. return ENOMEM;
  480. e->payload_type = payload_type;
  481. memcpy(e->iana, iana, 3);
  482. if (payload_type == IPMI_RMCPP_PAYLOAD_TYPE_OEM_EXPLICIT)
  483. e->payload_id = payload_id;
  484. else
  485. e->payload_id = 0;
  486. e->payload = payload;
  487. ipmi_lock(lan_payload_lock);
  488. c = oem_payload_list;
  489. while (c) {
  490. if ((c->payload_type == payload_type)
  491. && (memcmp(c->iana, iana, 3) == 0)
  492. && (c->payload_id == payload_id))
  493. {
  494. ipmi_unlock(lan_payload_lock);
  495. ipmi_mem_free(e);
  496. return EAGAIN;
  497. }
  498. c = c->next;
  499. }
  500. e->next = oem_payload_list;
  501. oem_payload_list = e;
  502. ipmi_unlock(lan_payload_lock);
  503. return 0;
  504. }
  505. static ipmi_lock_t *lan_auth_lock = NULL;
  506. typedef struct auth_entry_s auth_entry_t;
  507. struct auth_entry_s
  508. {
  509. unsigned int auth_num;
  510. unsigned char iana[3];
  511. ipmi_rmcpp_authentication_t *auth;
  512. auth_entry_t *next;
  513. };
  514. static auth_entry_t *oem_auth_list = NULL;
  515. static ipmi_rmcpp_authentication_t *auths[64];
  516. int
  517. ipmi_rmcpp_register_authentication(unsigned int auth_num,
  518. ipmi_rmcpp_authentication_t *auth)
  519. {
  520. if (auth_num >= 64)
  521. return EINVAL;
  522. if (auths[auth_num] && auth)
  523. return EAGAIN;
  524. auths[auth_num] = auth;
  525. return 0;
  526. }
  527. int
  528. ipmi_rmcpp_register_oem_authentication(unsigned int auth_num,
  529. unsigned char iana[3],
  530. ipmi_rmcpp_authentication_t *auth)
  531. {
  532. auth_entry_t *e;
  533. auth_entry_t *c;
  534. e = ipmi_mem_alloc(sizeof(*e));
  535. if (!e)
  536. return ENOMEM;
  537. e->auth_num = auth_num;
  538. memcpy(e->iana, iana, 3);
  539. e->auth = auth;
  540. ipmi_lock(lan_auth_lock);
  541. c = oem_auth_list;
  542. while (c) {
  543. if ((c->auth_num == auth_num)
  544. && (memcmp(c->iana, iana, 3) == 0))
  545. {
  546. ipmi_unlock(lan_auth_lock);
  547. ipmi_mem_free(e);
  548. return EAGAIN;
  549. }
  550. }
  551. e->next = oem_auth_list;
  552. oem_auth_list = e;
  553. ipmi_unlock(lan_auth_lock);
  554. return 0;
  555. }
  556. typedef struct conf_entry_s conf_entry_t;
  557. struct conf_entry_s
  558. {
  559. unsigned int conf_num;
  560. unsigned char iana[3];
  561. ipmi_rmcpp_confidentiality_t *conf;
  562. conf_entry_t *next;
  563. };
  564. static conf_entry_t *oem_conf_list = NULL;
  565. static int
  566. conf_none_init(ipmi_con_t *ipmi, ipmi_rmcpp_auth_t *ainfo, void **conf_data)
  567. {
  568. *conf_data = NULL;
  569. return 0;
  570. }
  571. static void
  572. conf_none_free(ipmi_con_t *ipmi, void *conf_data)
  573. {
  574. }
  575. static int
  576. conf_none_encrypt(ipmi_con_t *ipmi,
  577. void *conf_data,
  578. unsigned char **payload,
  579. unsigned int *header_len,
  580. unsigned int *payload_len,
  581. unsigned int *max_payload_len)
  582. {
  583. return 0;
  584. }
  585. static int
  586. conf_none_decrypt(ipmi_con_t *ipmi,
  587. void *conf_data,
  588. unsigned char **payload,
  589. unsigned int *payload_len)
  590. {
  591. return 0;
  592. }
  593. static ipmi_rmcpp_confidentiality_t conf_none =
  594. { conf_none_init, conf_none_free, conf_none_encrypt, conf_none_decrypt};
  595. static ipmi_rmcpp_confidentiality_t *confs[64] =
  596. {
  597. &conf_none
  598. };
  599. int ipmi_rmcpp_register_confidentiality(unsigned int conf_num,
  600. ipmi_rmcpp_confidentiality_t *conf)
  601. {
  602. if ((conf_num == 0) || (conf_num >= 64))
  603. return EINVAL;
  604. if (confs[conf_num] && conf)
  605. return EAGAIN;
  606. confs[conf_num] = conf;
  607. return 0;
  608. }
  609. int
  610. ipmi_rmcpp_register_oem_confidentiality(unsigned int conf_num,
  611. unsigned char iana[3],
  612. ipmi_rmcpp_confidentiality_t *conf)
  613. {
  614. conf_entry_t *e;
  615. conf_entry_t *c;
  616. e = ipmi_mem_alloc(sizeof(*e));
  617. if (!e)
  618. return ENOMEM;
  619. e->conf_num = conf_num;
  620. memcpy(e->iana, iana, 3);
  621. e->conf = conf;
  622. ipmi_lock(lan_auth_lock);
  623. c = oem_conf_list;
  624. while (c) {
  625. if ((c->conf_num == conf_num)
  626. && (memcmp(c->iana, iana, 3) == 0))
  627. {
  628. ipmi_unlock(lan_auth_lock);
  629. ipmi_mem_free(e);
  630. return EAGAIN;
  631. }
  632. }
  633. e->next = oem_conf_list;
  634. oem_conf_list = e;
  635. ipmi_unlock(lan_auth_lock);
  636. return 0;
  637. }
  638. typedef struct integ_entry_s integ_entry_t;
  639. struct integ_entry_s
  640. {
  641. unsigned int integ_num;
  642. unsigned char iana[3];
  643. ipmi_rmcpp_integrity_t *integ;
  644. integ_entry_t *next;
  645. };
  646. static integ_entry_t *oem_integ_list = NULL;
  647. static int
  648. integ_none_init(ipmi_con_t *ipmi,
  649. ipmi_rmcpp_auth_t *ainfo,
  650. void **integ_data)
  651. {
  652. *integ_data = NULL;
  653. return 0;
  654. }
  655. static void
  656. integ_none_free(ipmi_con_t *ipmi,
  657. void *integ_data)
  658. {
  659. }
  660. static int
  661. integ_none_pad(ipmi_con_t *ipmi,
  662. void *integ_data,
  663. unsigned char *payload,
  664. unsigned int *payload_len,
  665. unsigned int max_payload_len)
  666. {
  667. return 0;
  668. }
  669. static int
  670. integ_none_add(ipmi_con_t *ipmi,
  671. void *integ_data,
  672. unsigned char *payload,
  673. unsigned int *payload_len,
  674. unsigned int max_payload_len)
  675. {
  676. return 0;
  677. }
  678. static int
  679. integ_none_check(ipmi_con_t *ipmi,
  680. void *integ_data,
  681. unsigned char *payload,
  682. unsigned int payload_len,
  683. unsigned int total_len)
  684. {
  685. return 0;
  686. }
  687. static ipmi_rmcpp_integrity_t integ_none =
  688. { integ_none_init, integ_none_free, integ_none_pad, integ_none_add,
  689. integ_none_check };
  690. static ipmi_rmcpp_integrity_t *integs[64] =
  691. {
  692. &integ_none
  693. };
  694. int ipmi_rmcpp_register_integrity(unsigned int integ_num,
  695. ipmi_rmcpp_integrity_t *integ)
  696. {
  697. if ((integ_num == 0) || (integ_num >= 64))
  698. return EINVAL;
  699. if (integs[integ_num] && integ)
  700. return EAGAIN;
  701. integs[integ_num] = integ;
  702. return 0;
  703. }
  704. int
  705. ipmi_rmcpp_register_oem_integrity(unsigned int integ_num,
  706. unsigned char iana[3],
  707. ipmi_rmcpp_integrity_t *integ)
  708. {
  709. integ_entry_t *e;
  710. integ_entry_t *c;
  711. e = ipmi_mem_alloc(sizeof(*e));
  712. if (!e)
  713. return ENOMEM;
  714. e->integ_num = integ_num;
  715. memcpy(e->iana, iana, 3);
  716. e->integ = integ;
  717. ipmi_lock(lan_auth_lock);
  718. c = oem_integ_list;
  719. while (c) {
  720. if ((c->integ_num == integ_num)
  721. && (memcmp(c->iana, iana, 3) == 0))
  722. {
  723. ipmi_unlock(lan_auth_lock);
  724. ipmi_mem_free(e);
  725. return EAGAIN;
  726. }
  727. }
  728. e->next = oem_integ_list;
  729. oem_integ_list = e;
  730. ipmi_unlock(lan_auth_lock);
  731. return 0;
  732. }
  733. uint32_t
  734. ipmi_rmcpp_auth_get_my_session_id(ipmi_rmcpp_auth_t *ainfo)
  735. {
  736. return ainfo->lan->ip[ainfo->addr_num].precon_session_id;
  737. }
  738. uint32_t
  739. ipmi_rmcpp_auth_get_mgsys_session_id(ipmi_rmcpp_auth_t *ainfo)
  740. {
  741. return ainfo->lan->ip[ainfo->addr_num].precon_mgsys_session_id;
  742. }
  743. uint8_t
  744. ipmi_rmcpp_auth_get_role(ipmi_rmcpp_auth_t *ainfo)
  745. {
  746. return ainfo->role;
  747. }
  748. const unsigned char *
  749. ipmi_rmcpp_auth_get_username(ipmi_rmcpp_auth_t *ainfo,
  750. unsigned int *max_len)
  751. {
  752. *max_len = 16;
  753. return ainfo->lan->cparm.username;
  754. }
  755. unsigned int
  756. ipmi_rmcpp_auth_get_username_len(ipmi_rmcpp_auth_t *ainfo)
  757. {
  758. return ainfo->lan->cparm.username_len;
  759. }
  760. const unsigned char *
  761. ipmi_rmcpp_auth_get_password(ipmi_rmcpp_auth_t *ainfo,
  762. unsigned int *max_len)
  763. {
  764. *max_len = 20;
  765. return ainfo->lan->cparm.password;
  766. }
  767. unsigned int
  768. ipmi_rmcpp_auth_get_password_len(ipmi_rmcpp_auth_t *ainfo)
  769. {
  770. return ainfo->lan->cparm.password_len;
  771. }
  772. int
  773. ipmi_rmcpp_auth_get_use_two_keys(ipmi_rmcpp_auth_t *ainfo)
  774. {
  775. return ainfo->lan->use_two_keys;
  776. }
  777. const unsigned char *
  778. ipmi_rmcpp_auth_get_bmc_key(ipmi_rmcpp_auth_t *ainfo,
  779. unsigned int *max_len)
  780. {
  781. *max_len = 20;
  782. if (ainfo->lan->use_two_keys)
  783. return ainfo->lan->cparm.bmc_key;
  784. else
  785. return ainfo->lan->cparm.password;
  786. }
  787. unsigned int
  788. ipmi_rmcpp_auth_get_bmc_key_len(ipmi_rmcpp_auth_t *ainfo)
  789. {
  790. if (ainfo->lan->use_two_keys)
  791. return ainfo->lan->cparm.bmc_key_len;
  792. else
  793. return ainfo->lan->cparm.password_len;
  794. }
  795. /* From the get channel auth. */
  796. const unsigned char *
  797. ipmi_rmcpp_auth_get_oem_iana(ipmi_rmcpp_auth_t *ainfo,
  798. unsigned int *len)
  799. {
  800. *len = 3;
  801. return ainfo->lan->oem_iana;
  802. }
  803. unsigned char
  804. ipmi_rmcpp_auth_get_oem_aux(ipmi_rmcpp_auth_t *ainfo)
  805. {
  806. return ainfo->lan->oem_aux;
  807. }
  808. /* Should be filled in by the auth algorithm. */
  809. unsigned char *
  810. ipmi_rmcpp_auth_get_my_rand(ipmi_rmcpp_auth_t *ainfo,
  811. unsigned int *max_len)
  812. {
  813. *max_len = 16;
  814. return ainfo->my_rand;
  815. }
  816. unsigned int
  817. ipmi_rmcpp_auth_get_my_rand_len(ipmi_rmcpp_auth_t *ainfo)
  818. {
  819. return ainfo->my_rand_len;
  820. }
  821. void
  822. ipmi_rmcpp_auth_set_my_rand_len(ipmi_rmcpp_auth_t *ainfo,
  823. unsigned int length)
  824. {
  825. ainfo->my_rand_len = length;
  826. }
  827. unsigned char *
  828. ipmi_rmcpp_auth_get_mgsys_rand(ipmi_rmcpp_auth_t *ainfo,
  829. unsigned int *max_len)
  830. {
  831. *max_len = 16;
  832. return ainfo->mgsys_rand;
  833. }
  834. unsigned int
  835. ipmi_rmcpp_auth_get_mgsys_rand_len(ipmi_rmcpp_auth_t *ainfo)
  836. {
  837. return ainfo->mgsys_rand_len;
  838. }
  839. void
  840. ipmi_rmcpp_auth_set_mgsys_rand_len(ipmi_rmcpp_auth_t *ainfo,
  841. unsigned int length)
  842. {
  843. ainfo->mgsys_rand_len = length;
  844. }
  845. unsigned char *
  846. ipmi_rmcpp_auth_get_mgsys_guid(ipmi_rmcpp_auth_t *ainfo,
  847. unsigned int *max_len)
  848. {
  849. *max_len = 16;
  850. return ainfo->mgsys_guid;
  851. }
  852. unsigned int
  853. ipmi_rmcpp_auth_get_mgsys_guid_len(ipmi_rmcpp_auth_t *ainfo)
  854. {
  855. return ainfo->mgsys_guid_len;
  856. }
  857. void
  858. ipmi_rmcpp_auth_set_mgsys_guid_len(ipmi_rmcpp_auth_t *ainfo,
  859. unsigned int length)
  860. {
  861. ainfo->mgsys_guid_len = length;
  862. }
  863. unsigned char *
  864. ipmi_rmcpp_auth_get_sik(ipmi_rmcpp_auth_t *ainfo,
  865. unsigned int *max_len)
  866. {
  867. *max_len = 20;
  868. return ainfo->sik;
  869. }
  870. unsigned int
  871. ipmi_rmcpp_auth_get_sik_len(ipmi_rmcpp_auth_t *ainfo)
  872. {
  873. return ainfo->sik_len;
  874. }
  875. void
  876. ipmi_rmcpp_auth_set_sik_len(ipmi_rmcpp_auth_t *ainfo,
  877. unsigned int length)
  878. {
  879. ainfo->sik_len = length;
  880. }
  881. unsigned char *
  882. ipmi_rmcpp_auth_get_k1(ipmi_rmcpp_auth_t *ainfo,
  883. unsigned int *max_len)
  884. {
  885. *max_len = 20;
  886. return ainfo->k1;
  887. }
  888. unsigned int
  889. ipmi_rmcpp_auth_get_k1_len(ipmi_rmcpp_auth_t *ainfo)
  890. {
  891. return ainfo->k1_len;
  892. }
  893. void
  894. ipmi_rmcpp_auth_set_k1_len(ipmi_rmcpp_auth_t *ainfo,
  895. unsigned int length)
  896. {
  897. ainfo->k1_len = length;
  898. }
  899. unsigned char *
  900. ipmi_rmcpp_auth_get_k2(ipmi_rmcpp_auth_t *ainfo,
  901. unsigned int *max_len)
  902. {
  903. *max_len = 20;
  904. return ainfo->k2;
  905. }
  906. unsigned int
  907. ipmi_rmcpp_auth_get_k2_len(ipmi_rmcpp_auth_t *ainfo)
  908. {
  909. return ainfo->k2_len;
  910. }
  911. void
  912. ipmi_rmcpp_auth_set_k2_len(ipmi_rmcpp_auth_t *ainfo,
  913. unsigned int length)
  914. {
  915. ainfo->k2_len = length;
  916. }
  917. static void check_command_queue(ipmi_con_t *ipmi, lan_data_t *lan);
  918. static int send_auth_cap(ipmi_con_t *ipmi, lan_data_t *lan, int addr_num,
  919. int force_ipmiv15);
  920. static os_handler_t *lan_os_hnd;
  921. #define MAX_CONS_PER_FD 32
  922. struct lan_fd_s
  923. {
  924. int fd;
  925. os_hnd_fd_id_t *fd_wait_id;
  926. unsigned int cons_in_use;
  927. lan_data_t *lan[MAX_CONS_PER_FD];
  928. lan_fd_t *next, *prev;
  929. ipmi_lock_t *con_lock;
  930. /* Main list info. */
  931. ipmi_lock_t *lock;
  932. lan_fd_t **free_list;
  933. lan_fd_t *list;
  934. };
  935. /* This is a list, but the only searching is to find an fd with a free
  936. slot (when creating a new lan). This is O(1) because the first
  937. entry is guaranteed to have a free slot if any have free slots.
  938. Note that once one of these is created, it is never destroyed
  939. (destruction is very difficult because of the race conditions). */
  940. static ipmi_lock_t *fd_list_lock = NULL;
  941. static lan_fd_t fd_list;
  942. static lan_fd_t *fd_free_list;
  943. #ifdef PF_INET6
  944. static ipmi_lock_t *fd6_list_lock = NULL;
  945. static lan_fd_t fd6_list;
  946. static lan_fd_t *fd6_free_list;
  947. #endif
  948. static void data_handler(int fd,
  949. void *cb_data,
  950. os_hnd_fd_id_t *id);
  951. static int
  952. lan_addr_same(sockaddr_ip_t *a1, sockaddr_ip_t *a2)
  953. {
  954. if (a1->ip_addr_len != a2->ip_addr_len)
  955. return 0;
  956. if (a1->s_ipsock.s_addr.sa_family != a2->s_ipsock.s_addr.sa_family) {
  957. if (DEBUG_RAWMSG || DEBUG_MSG_ERR)
  958. ipmi_log(IPMI_LOG_DEBUG, "Address family mismatch: %d %d",
  959. a1->s_ipsock.s_addr.sa_family,
  960. a2->s_ipsock.s_addr.sa_family);
  961. return 0;
  962. }
  963. switch (a1->s_ipsock.s_addr.sa_family) {
  964. case PF_INET:
  965. {
  966. struct sockaddr_in *ip1 = &a1->s_ipsock.s_addr4;
  967. struct sockaddr_in *ip2 = &a2->s_ipsock.s_addr4;
  968. if ((ip1->sin_port == ip2->sin_port)
  969. && (ip1->sin_addr.s_addr == ip2->sin_addr.s_addr))
  970. return 1;
  971. }
  972. break;
  973. #ifdef PF_INET6
  974. case PF_INET6:
  975. {
  976. struct sockaddr_in6 *ip1 = &a1->s_ipsock.s_addr6;
  977. struct sockaddr_in6 *ip2 = &a2->s_ipsock.s_addr6;
  978. if ((ip1->sin6_port == ip2->sin6_port)
  979. && (bcmp(ip1->sin6_addr.s6_addr, ip2->sin6_addr.s6_addr,
  980. sizeof(struct in6_addr)) == 0))
  981. return 1;
  982. }
  983. break;
  984. #endif
  985. default:
  986. ipmi_log(IPMI_LOG_ERR_INFO,
  987. "ipmi_lan: Unknown protocol family: 0x%x",
  988. a1->s_ipsock.s_addr.sa_family);
  989. break;
  990. }
  991. return 0;
  992. }
  993. static void
  994. move_to_lan_list_end(lan_fd_t *item)
  995. {
  996. lan_fd_t *list = item->list;
  997. item->next->prev = item->prev;
  998. item->prev->next = item->next;
  999. item->next = list;
  1000. item->prev = list->prev;
  1001. list->prev->next = item;
  1002. list->prev = item;
  1003. }
  1004. static void
  1005. move_to_lan_list_head(lan_fd_t *item)
  1006. {
  1007. lan_fd_t *list = item->list;
  1008. item->next->prev = item->prev;
  1009. item->prev->next = item->next;
  1010. item->next = list->next;
  1011. item->prev = list;
  1012. list->next->prev = item;
  1013. list->next = item;
  1014. }
  1015. static lan_fd_t *
  1016. find_free_lan_fd(int family, lan_data_t *lan, int *slot)
  1017. {
  1018. ipmi_lock_t *lock;
  1019. lan_fd_t *list, *item;
  1020. lan_fd_t **free_list;
  1021. int rv;
  1022. int i;
  1023. if (family == PF_INET) {
  1024. lock = fd_list_lock;
  1025. list = &fd_list;
  1026. free_list = &fd_free_list;
  1027. }
  1028. #ifdef PF_INET6
  1029. else if (family == PF_INET6) {
  1030. lock = fd6_list_lock;
  1031. list = &fd6_list;
  1032. free_list = &fd6_free_list;
  1033. }
  1034. #endif
  1035. else {
  1036. return NULL;
  1037. }
  1038. ipmi_lock(lock);
  1039. item = list->next;
  1040. retry:
  1041. if (item->cons_in_use < MAX_CONS_PER_FD) {
  1042. int tslot = -1;
  1043. /* Got an entry with a slot, just reuse it. */
  1044. for (i=0; i<MAX_CONS_PER_FD; i++) {
  1045. if (item->lan[i]) {
  1046. /* Check for a matching IP address. Can't have two
  1047. systems with the same address in the same fd entry. */
  1048. unsigned int j, k;
  1049. lan_data_t *l = item->lan[i];
  1050. for (j=0; j<l->cparm.num_ip_addr; j++) {
  1051. for (k=0; k<lan->cparm.num_ip_addr; k++) {
  1052. if (lan_addr_same(&l->cparm.ip_addr[j],
  1053. &lan->cparm.ip_addr[k]))
  1054. {
  1055. /* Found the same address in the same
  1056. lan_data file. Try another one. */
  1057. item = item->next;
  1058. goto retry;
  1059. }
  1060. }
  1061. }
  1062. } else if (tslot < 0)
  1063. tslot = i;
  1064. }
  1065. if (tslot < 0) {
  1066. lan_fd_t *next = item->next;
  1067. /* Can't happen, but log and fix it up. */
  1068. ipmi_log(IPMI_LOG_SEVERE, "ipmi_lan.c: Internal error, count"
  1069. " in lan fd list item incorrect, but we can recover.");
  1070. item->cons_in_use = MAX_CONS_PER_FD;
  1071. move_to_lan_list_end(item);
  1072. item = next;
  1073. goto retry;
  1074. }
  1075. item->cons_in_use++;
  1076. item->lan[tslot] = lan;
  1077. *slot = tslot;
  1078. if (item->cons_in_use == MAX_CONS_PER_FD)
  1079. /* Out of connections in this item, move it to the end of
  1080. the list. */
  1081. move_to_lan_list_end(item);
  1082. } else {
  1083. /* No free entries, create one */
  1084. if (*free_list) {
  1085. /* Pull them off the free list first. */
  1086. item = *free_list;
  1087. *free_list = item->next;
  1088. } else {
  1089. item = ipmi_mem_alloc(sizeof(*item));
  1090. if (item) {
  1091. memset(item, 0, sizeof(*item));
  1092. rv = ipmi_create_global_lock(&item->con_lock);
  1093. if (rv) {
  1094. ipmi_mem_free(item);
  1095. goto out_unlock;
  1096. }
  1097. item->lock = lock;
  1098. item->free_list = free_list;
  1099. item->list = list;
  1100. }
  1101. }
  1102. if (!item)
  1103. goto out_unlock;
  1104. item->next = item;
  1105. item->prev = item;
  1106. item->fd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
  1107. if (item->fd == -1) {
  1108. item->next = *free_list;
  1109. *free_list = item;
  1110. item = NULL;
  1111. goto out_unlock;
  1112. }
  1113. /* Bind is not necessary, we don't care what port we are. */
  1114. /* We want it to be non-blocking. */
  1115. rv = fcntl(item->fd, F_SETFL, O_NONBLOCK);
  1116. if (rv) {
  1117. close(item->fd);
  1118. item->next = *free_list;
  1119. *free_list = item;
  1120. item = NULL;
  1121. goto out_unlock;
  1122. }
  1123. rv = lan_os_hnd->add_fd_to_wait_for(lan_os_hnd,
  1124. item->fd,
  1125. data_handler,
  1126. item,
  1127. NULL,
  1128. &(item->fd_wait_id));
  1129. if (rv) {
  1130. close(item->fd);
  1131. item->next = *free_list;
  1132. *free_list = item;
  1133. item = NULL;
  1134. goto out_unlock;
  1135. }
  1136. item->cons_in_use++;
  1137. item->lan[0] = lan;
  1138. *slot = 0;
  1139. /* This will have free items, put it at the head of the list. */
  1140. move_to_lan_list_head(item);
  1141. }
  1142. out_unlock:
  1143. ipmi_unlock(lock);
  1144. return item;
  1145. }
  1146. static void
  1147. release_lan_fd(lan_fd_t *item, int slot)
  1148. {
  1149. ipmi_lock(item->lock);
  1150. item->lan[slot] = NULL;
  1151. item->cons_in_use--;
  1152. if (item->cons_in_use == 0) {
  1153. lan_os_hnd->remove_fd_to_wait_for(lan_os_hnd, item->fd_wait_id);
  1154. close(item->fd);
  1155. item->next->prev = item->prev;
  1156. item->prev->next = item->next;
  1157. item->next = *(item->free_list);
  1158. *(item->free_list) = item;
  1159. } else {
  1160. /* This has free connections, move it to the head of the
  1161. list. */
  1162. move_to_lan_list_head(item);
  1163. }
  1164. ipmi_unlock(item->lock);
  1165. }
  1166. /*
  1167. * We keep two hash tables, one by IP address and one by connection
  1168. * address.
  1169. */
  1170. #define LAN_HASH_SIZE 256
  1171. #define LAN_HASH_SHIFT 6
  1172. static ipmi_lock_t *lan_list_lock = NULL;
  1173. static lan_link_t lan_list[LAN_HASH_SIZE];
  1174. static lan_link_t lan_ip_list[LAN_HASH_SIZE];
  1175. static unsigned int
  1176. hash_lan(const ipmi_con_t *ipmi)
  1177. {
  1178. unsigned int idx;
  1179. idx = (((unsigned long) ipmi)
  1180. >> (sizeof(unsigned long) + LAN_HASH_SHIFT));
  1181. idx %= LAN_HASH_SIZE;
  1182. return idx;
  1183. }
  1184. static int
  1185. hash_lan_addr(const struct sockaddr *addr)
  1186. {
  1187. int idx;
  1188. switch (addr->sa_family)
  1189. {
  1190. case PF_INET:
  1191. {
  1192. struct sockaddr_in *iaddr = (struct sockaddr_in *) addr;
  1193. idx = ntohl(iaddr->sin_addr.s_addr) % LAN_HASH_SIZE;
  1194. break;
  1195. }
  1196. #ifdef PF_INET6
  1197. case PF_INET6:
  1198. {
  1199. /* Use the lower 4 bytes of the IPV6 address. */
  1200. struct sockaddr_in6 *iaddr = (struct sockaddr_in6 *) addr;
  1201. idx = htonl(*((uint32_t *) &iaddr->sin6_addr.s6_addr[12]));
  1202. idx %= LAN_HASH_SIZE;
  1203. break;
  1204. }
  1205. #endif
  1206. default:
  1207. idx = 0;
  1208. }
  1209. idx %= LAN_HASH_SIZE;
  1210. return idx;
  1211. }
  1212. static void
  1213. lan_add_con(lan_data_t *lan)
  1214. {
  1215. unsigned int idx;
  1216. lan_link_t *head;
  1217. unsigned int i;
  1218. ipmi_lock(lan_list_lock);
  1219. idx = hash_lan(lan->ipmi);
  1220. head = &lan_list[idx];
  1221. lan->link.lan = lan;
  1222. lan->link.next = head;
  1223. lan->link.prev = head->prev;
  1224. head->prev->next = &lan->link;
  1225. head->prev = &lan->link;
  1226. for (i=0; i<lan->cparm.num_ip_addr; i++) {
  1227. struct sockaddr *addr = &lan->cparm.ip_addr[i].s_ipsock.s_addr;
  1228. idx = hash_lan_addr(addr);
  1229. head = &lan_ip_list[idx];
  1230. lan->ip[i].ip_link.lan = lan;
  1231. lan->ip[i].ip_link.next = head;
  1232. lan->ip[i].ip_link.prev = head->prev;
  1233. head->prev->next = &lan->ip[i].ip_link;
  1234. head->prev = &lan->ip[i].ip_link;
  1235. }
  1236. ipmi_unlock(lan_list_lock);
  1237. }
  1238. /* Must be called with the lan list lock held. */
  1239. static void
  1240. lan_remove_con_nolock(lan_data_t *lan)
  1241. {
  1242. unsigned int i;
  1243. if (!lan->link.lan)
  1244. /* Hasn't been initialized. */
  1245. return;
  1246. lan->link.prev->next = lan->link.next;
  1247. lan->link.next->prev = lan->link.prev;
  1248. lan->link.lan = NULL;
  1249. for (i=0; i<lan->cparm.num_ip_addr; i++) {
  1250. lan->ip[i].ip_link.prev->next = lan->ip[i].ip_link.next;
  1251. lan->ip[i].ip_link.next->prev = lan->ip[i].ip_link.prev;
  1252. lan->ip[i].ip_link.lan = NULL;
  1253. }
  1254. }
  1255. static lan_data_t *
  1256. lan_find_con(ipmi_con_t *ipmi)
  1257. {
  1258. unsigned int idx;
  1259. lan_link_t *l;
  1260. ipmi_lock(lan_list_lock);
  1261. idx = hash_lan(ipmi);
  1262. l = lan_list[idx].next;
  1263. while (l->lan) {
  1264. if (l->lan->ipmi == ipmi)
  1265. break;
  1266. l = l->next;
  1267. }
  1268. if (l->lan)
  1269. l->lan->refcount++;
  1270. ipmi_unlock(lan_list_lock);
  1271. return l->lan;
  1272. }
  1273. static inline int
  1274. cmp_timeval(struct timeval *tv1, struct timeval *tv2)
  1275. {
  1276. if (tv1->tv_sec < tv2->tv_sec)
  1277. return -1;
  1278. if (tv1->tv_sec > tv2->tv_sec)
  1279. return 1;
  1280. if (tv1->tv_usec < tv2->tv_usec)
  1281. return -1;
  1282. if (tv1->tv_usec > tv2->tv_usec)
  1283. return 1;
  1284. return 0;
  1285. }
  1286. typedef struct lan_add_stat_info_s
  1287. {
  1288. int statnum;
  1289. int count;
  1290. } lan_add_stat_info_t;
  1291. int
  1292. add_stat_cb(void *cb_data, void *item1, void *item2)
  1293. {
  1294. ipmi_ll_stat_info_t *info = item2;
  1295. lan_stat_info_t *stat = item1;
  1296. lan_add_stat_info_t *sinfo = cb_data;
  1297. if (stat->stats[sinfo->statnum])
  1298. ipmi_ll_con_stat_call_adder(info, stat->stats[sinfo->statnum],
  1299. sinfo->count);
  1300. return LOCKED_LIST_ITER_CONTINUE;
  1301. }
  1302. static inline void
  1303. add_stat(ipmi_con_t *ipmi, int stat, int count)
  1304. {
  1305. lan_data_t *lan = ipmi->con_data;
  1306. lan_add_stat_info_t sinfo;
  1307. sinfo.statnum = stat;
  1308. sinfo.count = count;
  1309. locked_list_iterate(lan->lan_stat_list, add_stat_cb, &sinfo);
  1310. }
  1311. static inline void
  1312. diff_timeval(struct timeval *dest,
  1313. struct timeval *left,
  1314. struct timeval *right)
  1315. {
  1316. if ( (left->tv_sec < right->tv_sec)
  1317. || ( (left->tv_sec == right->tv_sec)
  1318. && (left->tv_usec < right->tv_usec)))
  1319. {
  1320. /* If left < right, just force to zero, don't allow negative
  1321. numbers. */
  1322. dest->tv_sec = 0;
  1323. dest->tv_usec = 0;
  1324. return;
  1325. }
  1326. dest->tv_sec = left->tv_sec - right->tv_sec;
  1327. dest->tv_usec = left->tv_usec - right->tv_usec;
  1328. while (dest->tv_usec < 0) {
  1329. dest->tv_usec += 1000000;
  1330. dest->tv_sec--;
  1331. }
  1332. }
  1333. /* Must be called with the ipmi read or write lock. */
  1334. static int lan_valid_ipmi(ipmi_con_t *ipmi)
  1335. {
  1336. return (lan_find_con(ipmi) != NULL);
  1337. }
  1338. static void lan_cleanup(ipmi_con_t *ipmi);
  1339. static void
  1340. lan_put(ipmi_con_t *ipmi)
  1341. {
  1342. lan_data_t *lan = ipmi->con_data;
  1343. int done;
  1344. ipmi_lock(lan_list_lock);
  1345. lan->refcount--;
  1346. done = lan->refcount == 0;
  1347. /* If done, remove it before we release the lock. */
  1348. if (done)
  1349. lan_remove_con_nolock(lan);
  1350. ipmi_unlock(lan_list_lock);
  1351. if (done)
  1352. lan_cleanup(ipmi);
  1353. }
  1354. static int
  1355. auth_gen(lan_data_t *lan,
  1356. unsigned char *out,
  1357. uint8_t *ses_id,
  1358. uint8_t *seq,
  1359. unsigned char *data,
  1360. unsigned int data_len,
  1361. int addr_num)
  1362. {
  1363. int rv;
  1364. ipmi_auth_sg_t l[] =
  1365. { { ses_id, 4 },
  1366. { data, data_len },
  1367. { seq, 4 },
  1368. { NULL, 0 }};
  1369. rv = ipmi_auths[lan->ip[addr_num].working_authtype]
  1370. .authcode_gen(lan->authdata, l, out);
  1371. return rv;
  1372. }
  1373. static int
  1374. auth_check(lan_data_t *lan,
  1375. uint8_t *ses_id,
  1376. uint8_t *seq,
  1377. unsigned char *data,
  1378. unsigned int data_len,
  1379. unsigned char *code,
  1380. int addr_num)
  1381. {
  1382. int rv;
  1383. ipmi_auth_sg_t l[] =
  1384. { { ses_id, 4 },
  1385. { data, data_len },
  1386. { seq, 4 },
  1387. { NULL, 0 }};
  1388. rv = ipmi_auths[lan->ip[addr_num].working_authtype]
  1389. .authcode_check(lan->authdata, l, code);
  1390. return rv;
  1391. }
  1392. #define IPMI_MAX_LAN_LEN (IPMI_MAX_MSG_LENGTH + 128)
  1393. #define IPMI_LAN_MAX_HEADER 128
  1394. static int
  1395. rmcpp_format_msg(lan_data_t *lan, int addr_num,
  1396. unsigned int payload_type, int in_session,
  1397. unsigned char **msgdata, unsigned int *data_len,
  1398. unsigned int max_data_len, unsigned int header_len,
  1399. unsigned char *oem_iana, unsigned int oem_payload_id,
  1400. const ipmi_con_option_t *options)
  1401. {
  1402. unsigned char *tmsg;
  1403. int rv;
  1404. unsigned int header_used;
  1405. unsigned char *data;
  1406. unsigned int payload_len;
  1407. uint32_t *seqp;
  1408. int do_auth = 1;
  1409. int do_conf = 1;
  1410. if (options) {
  1411. while (options->option != IPMI_CON_OPTION_LIST_END) {
  1412. switch (options->option) {
  1413. case IPMI_CON_MSG_OPTION_AUTH:
  1414. do_auth = options->ival;
  1415. break;
  1416. case IPMI_CON_MSG_OPTION_CONF:
  1417. do_conf = options->ival;
  1418. break;
  1419. default:
  1420. /* Ignore unknown options. */
  1421. break;
  1422. }
  1423. options++;
  1424. }
  1425. }
  1426. do_conf = (do_conf && in_session
  1427. && (lan->ip[addr_num].working_conf
  1428. != IPMI_LANP_CONFIDENTIALITY_ALGORITHM_NONE));
  1429. do_auth = (do_auth && in_session
  1430. && (lan->ip[addr_num].working_integ
  1431. != IPMI_LANP_INTEGRITY_ALGORITHM_NONE));
  1432. if (do_conf) {
  1433. #if 0
  1434. if (! lan->ip[addr_num].working)
  1435. return EAGAIN;
  1436. #endif
  1437. /* Note: This may encrypt the data, the old data will be lost. */
  1438. rv = lan->ip[addr_num].conf_info->conf_encrypt
  1439. (lan->ipmi,
  1440. lan->ip[addr_num].conf_data,
  1441. msgdata,
  1442. &header_len, data_len,
  1443. &max_data_len);
  1444. if (rv)
  1445. return rv;
  1446. }
  1447. payload_len = *data_len;
  1448. if (payload_type == IPMI_RMCPP_PAYLOAD_TYPE_OEM_EXPLICIT)
  1449. header_used = 22;
  1450. else
  1451. header_used = 16;
  1452. if (header_used > header_len)
  1453. return E2BIG;
  1454. data = *msgdata - header_used;
  1455. *data_len += header_used;
  1456. max_data_len += header_used;
  1457. data[0] = 6; /* RMCP version 1.0. */
  1458. data[1] = 0;
  1459. data[2] = 0xff;
  1460. data[3] = 0x07;
  1461. data[4] = lan->ip[addr_num].working_authtype;
  1462. data[5] = payload_type;
  1463. tmsg = data+6;
  1464. if (payload_type == IPMI_RMCPP_PAYLOAD_TYPE_OEM_EXPLICIT) {
  1465. memcpy(tmsg, oem_iana, 3);
  1466. tmsg += 3;
  1467. *tmsg = 0;
  1468. tmsg++;
  1469. ipmi_set_uint16(tmsg, oem_payload_id);
  1470. tmsg += 2;
  1471. }
  1472. if (in_session) {
  1473. if (do_conf)
  1474. data[5] |= 0x80;
  1475. if (do_auth) {
  1476. seqp = &(lan->ip[addr_num].outbound_seq_num);
  1477. data[5] |= 0x40;
  1478. } else {
  1479. seqp = &(lan->ip[addr_num].unauth_out_seq_num);
  1480. }
  1481. ipmi_set_uint32(tmsg, lan->ip[addr_num].mgsys_session_id);
  1482. tmsg += 4;
  1483. ipmi_set_uint32(tmsg, *seqp);
  1484. tmsg += 4;
  1485. } else {
  1486. ipmi_set_uint32(tmsg, 0); /* session id */
  1487. tmsg += 4;
  1488. ipmi_set_uint32(tmsg, 0); /* session sequence number */
  1489. tmsg += 4;
  1490. seqp = NULL;
  1491. }
  1492. /* Payload length doesn't include the padding. */
  1493. ipmi_set_uint16(tmsg, payload_len);
  1494. if (do_auth) {
  1495. rv = lan->ip[addr_num].integ_info->integ_pad
  1496. (lan->ipmi,
  1497. lan->ip[addr_num].integ_data,
  1498. data, data_len,
  1499. max_data_len);
  1500. if (rv)
  1501. return rv;
  1502. rv = lan->ip[addr_num].integ_info->integ_add
  1503. (lan->ipmi,
  1504. lan->ip[addr_num].integ_data,
  1505. data, data_len,
  1506. max_data_len);
  1507. if (rv)
  1508. return rv;
  1509. }
  1510. if (seqp) {
  1511. (*seqp)++;
  1512. if (*seqp == 0)
  1513. *seqp = 1;
  1514. }
  1515. *msgdata = data;
  1516. return 0;
  1517. }
  1518. static int
  1519. lan15_format_msg(lan_data_t *lan, int addr_num,
  1520. unsigned char **msgdata, unsigned int *data_len)
  1521. {
  1522. unsigned char *data;
  1523. int rv;
  1524. if (lan->ip[addr_num].working_authtype == IPMI_AUTHTYPE_NONE)
  1525. data = *msgdata - 14;
  1526. else
  1527. data = *msgdata - 30;
  1528. data[0] = 6; /* RMCP version 1.0. */
  1529. data[1] = 0;
  1530. data[2] = 0xff;
  1531. data[3] = 0x07;
  1532. data[4] = lan->ip[addr_num].working_authtype;
  1533. ipmi_set_uint32(data+5, lan->ip[addr_num].outbound_seq_num);
  1534. ipmi_set_uint32(data+9, lan->ip[addr_num].session_id);
  1535. /* FIXME - need locks for the sequence numbers. */
  1536. /* Increment the outbound number, but make sure it's not zero. If
  1537. it's already zero, ignore it, we are in pre-setup. */
  1538. if (lan->ip[addr_num].outbound_seq_num != 0) {
  1539. (lan->ip[addr_num].outbound_seq_num)++;
  1540. if (lan->ip[addr_num].outbound_seq_num == 0)
  1541. (lan->ip[addr_num].outbound_seq_num)++;
  1542. }
  1543. if (lan->ip[addr_num].working_authtype == IPMI_AUTHTYPE_NONE) {
  1544. /* No authentication, so no authcode. */
  1545. data[13] = *data_len;
  1546. *data_len += 14;
  1547. } else {
  1548. data[29] = *data_len;
  1549. rv = auth_gen(lan, data+13, data+9, data+5, *msgdata, *data_len,
  1550. addr_num);
  1551. if (rv)
  1552. return rv;
  1553. *data_len += 30;
  1554. }
  1555. *msgdata = data;
  1556. return 0;
  1557. }
  1558. static int
  1559. lan_send_addr(lan_data_t *lan,
  1560. const ipmi_addr_t *addr,
  1561. int addr_len,
  1562. const ipmi_msg_t *msg,
  1563. uint8_t seq,
  1564. int addr_num,
  1565. const ipmi_con_option_t *options)
  1566. {
  1567. unsigned char data[IPMI_MAX_LAN_LEN+IPMI_LAN_MAX_HEADER];
  1568. unsigned char *tmsg;
  1569. unsigned int pos;
  1570. int rv;
  1571. unsigned int payload_type;
  1572. int out_of_session = 0;
  1573. ipmi_payload_t *payload = NULL;
  1574. unsigned char oem_iana[3] = {0, 0, 0};
  1575. unsigned int oem_payload_id = 0;
  1576. if ((addr->addr_type >= IPMI_RMCPP_ADDR_START)
  1577. && (addr->addr_type <= IPMI_RMCPP_ADDR_END))
  1578. {
  1579. /*
  1580. * Let through the dodgy IPMI 1.5 Serial-over-LAN packets, but block
  1581. * anything else that tries to send an RMCP+ packet to a non-RMCP+
  1582. * host.
  1583. */
  1584. if ((addr->addr_type != IPMI_RMCPP_ADDR_SOL)
  1585. && (lan->ip[addr_num].working_authtype != IPMI_AUTHTYPE_RMCP_PLUS))
  1586. return EINVAL;
  1587. payload_type = addr->addr_type - IPMI_RMCPP_ADDR_START;
  1588. } else {
  1589. switch (addr->addr_type) {
  1590. case IPMI_SYSTEM_INTERFACE_ADDR_TYPE:
  1591. case IPMI_IPMB_ADDR_TYPE:
  1592. case IPMI_IPMB_BROADCAST_ADDR_TYPE:
  1593. payload_type = IPMI_RMCPP_PAYLOAD_TYPE_IPMI;
  1594. break;
  1595. default:
  1596. return EINVAL;
  1597. }
  1598. }
  1599. if ((payload_type == IPMI_RMCPP_PAYLOAD_TYPE_OEM_EXPLICIT)
  1600. || ((payload_type >= 0x20) && (payload_type <= 0x27)))
  1601. {
  1602. ipmi_rmcpp_addr_t *addr = (ipmi_rmcpp_addr_t *) addr;
  1603. payload_entry_t *e;
  1604. if (payload_type == IPMI_RMCPP_PAYLOAD_TYPE_OEM_EXPLICIT) {
  1605. memcpy(oem_iana, addr->oem_iana, 3);
  1606. oem_payload_id = addr->oem_payload_id;
  1607. } else {
  1608. memcpy(oem_iana, lan->oem_iana, 3);
  1609. oem_payload_id = 0;
  1610. }
  1611. /* No lock required, only payload additions are allowed. */
  1612. e = oem_payload_list;
  1613. while (e) {
  1614. if ((e->payload_type == payload_type)
  1615. && (memcmp(e->iana, oem_iana, 3) == 0)
  1616. && (e->payload_id == oem_payload_id))
  1617. {
  1618. payload = e->payload;
  1619. break;
  1620. }
  1621. e = e->next;
  1622. }
  1623. } else {
  1624. payload = payloads[payload_type];
  1625. }
  1626. tmsg = data + IPMI_LAN_MAX_HEADER;
  1627. if (!payload) {
  1628. return ENOSYS;
  1629. } else {
  1630. pos = IPMI_MAX_LAN_LEN;
  1631. rv = payload->format_for_xmit(lan->ipmi, addr, addr_len,
  1632. msg, tmsg, &pos,
  1633. &out_of_session, seq);
  1634. if (rv)
  1635. return rv;
  1636. }
  1637. if (lan->ip[addr_num].working_authtype == IPMI_AUTHTYPE_RMCP_PLUS) {
  1638. rv = rmcpp_format_msg(lan, addr_num,
  1639. payload_type, !out_of_session,
  1640. &tmsg, &pos,
  1641. IPMI_MAX_LAN_LEN, IPMI_LAN_MAX_HEADER,
  1642. oem_iana, oem_payload_id, options);
  1643. } else {
  1644. rv = lan15_format_msg(lan, addr_num, &tmsg, &pos);
  1645. if (addr->addr_type == IPMI_RMCPP_ADDR_SOL)
  1646. /*
  1647. * We're sending SoL over IPMI 1.5, which requires that we set
  1648. * a "reserved" bit. This is dodgy.
  1649. */
  1650. tmsg[4] |= 0x80;
  1651. }
  1652. if (rv)
  1653. return rv;
  1654. if (DEBUG_RAWMSG) {
  1655. char buf1[32], buf2[32];
  1656. ipmi_log(IPMI_LOG_DEBUG_START, "%soutgoing seq %d\n addr =",
  1657. IPMI_CONN_NAME(lan->ipmi), seq);
  1658. dump_hex((unsigned char *) &(lan->cparm.ip_addr[addr_num]),
  1659. sizeof(sockaddr_ip_t));
  1660. ipmi_log(IPMI_LOG_DEBUG_CONT,
  1661. "\n msg = netfn=%s cmd=%s data_len=%d.",
  1662. ipmi_get_netfn_string(msg->netfn, buf1, 32),
  1663. ipmi_get_command_string(msg->netfn, msg->cmd, buf2, 32),
  1664. msg->data_len);
  1665. if (pos) {
  1666. ipmi_log(IPMI_LOG_DEBUG_CONT, "\n data =\n ");
  1667. dump_hex(tmsg, pos);
  1668. }
  1669. ipmi_log(IPMI_LOG_DEBUG_END, " ");
  1670. }
  1671. add_stat(lan->ipmi, STAT_XMIT_PACKETS, 1);
  1672. rv = sendto(lan->fd->fd, tmsg, pos, 0,
  1673. (struct sockaddr *) &(lan->cparm.ip_addr[addr_num].s_ipsock),
  1674. lan->cparm.ip_addr[addr_num].ip_addr_len);
  1675. if (rv == -1)
  1676. rv = errno;
  1677. else
  1678. rv = 0;
  1679. return rv;
  1680. }
  1681. static int
  1682. lan_send(lan_data_t *lan,
  1683. const ipmi_addr_t *addr,
  1684. int addr_len,
  1685. const ipmi_msg_t *msg,
  1686. uint8_t seq,
  1687. int *send_ip_num,
  1688. const ipmi_con_option_t *options)
  1689. {
  1690. int curr_ip_addr;
  1691. ipmi_lock(lan->ip_lock);
  1692. if (msg->netfn & 1) {
  1693. /* For unacknowledged packets, don't switch addresses. They
  1694. don't contribute to detecting that the link is down. */
  1695. curr_ip_addr = lan->curr_ip_addr;
  1696. } else if (lan->connected) {
  1697. lan->num_sends++;
  1698. /* We periodically switch between IP addresses, just to make sure
  1699. they are all operational. */
  1700. if ((lan->num_sends % SENDS_BETWEEN_IP_SWITCHES) == 0) {
  1701. unsigned int addr_num = lan->curr_ip_addr + 1;
  1702. if (addr_num >= lan->cparm.num_ip_addr)
  1703. addr_num = 0;
  1704. while (addr_num != lan->curr_ip_addr) {
  1705. if (lan->ip[addr_num].working)
  1706. break;
  1707. addr_num++;
  1708. if (addr_num >= lan->cparm.num_ip_addr)
  1709. addr_num = 0;
  1710. }
  1711. lan->curr_ip_addr = addr_num;
  1712. }
  1713. } else {
  1714. /* Just rotate between IP addresses if we are not yet connected */
  1715. unsigned int addr_num = lan->curr_ip_addr + 1;
  1716. if (addr_num >= lan->cparm.num_ip_addr)
  1717. addr_num = 0;
  1718. lan->curr_ip_addr = addr_num;
  1719. }
  1720. curr_ip_addr = lan->curr_ip_addr;
  1721. ipmi_unlock(lan->ip_lock);
  1722. *send_ip_num = curr_ip_addr;
  1723. return lan_send_addr(lan, addr, addr_len, msg, seq, curr_ip_addr, options);
  1724. }
  1725. typedef struct call_ipmb_change_handler_s
  1726. {
  1727. lan_data_t *lan;
  1728. int err;
  1729. const unsigned char *ipmb_addr;
  1730. unsigned int num_ipmb_addr;
  1731. int active;
  1732. unsigned int hacks;
  1733. } call_ipmb_change_handler_t;
  1734. static int
  1735. call_ipmb_change_handler(void *cb_data, void *item1, void *item2)
  1736. {
  1737. call_ipmb_change_handler_t *info = cb_data;
  1738. ipmi_ll_ipmb_addr_cb handler = item1;
  1739. handler(info->lan->ipmi, info->err, info->ipmb_addr, info->num_ipmb_addr,
  1740. info->active, info->hacks, item2);
  1741. return LOCKED_LIST_ITER_CONTINUE;
  1742. }
  1743. static void
  1744. call_ipmb_change_handlers(lan_data_t *lan, int err,
  1745. const unsigned char ipmb_addr[],
  1746. unsigned int num_ipmb_addr,
  1747. int active, unsigned int hacks)
  1748. {
  1749. call_ipmb_change_handler_t info;
  1750. info.lan = lan;
  1751. info.err = err;
  1752. info.ipmb_addr = ipmb_addr;
  1753. info.num_ipmb_addr = num_ipmb_addr;
  1754. info.active = active;
  1755. info.hacks = hacks;
  1756. locked_list_iterate(lan->ipmb_change_handlers, call_ipmb_change_handler,
  1757. &info);
  1758. }
  1759. static int
  1760. lan_add_ipmb_addr_handler(ipmi_con_t *ipmi,
  1761. ipmi_ll_ipmb_addr_cb handler,
  1762. void *cb_data)
  1763. {
  1764. lan_data_t *lan = (lan_data_t *) ipmi->con_data;

Large files files are truncated, but you can click here to view the full file