1 /* 2 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * Redistribution of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * Redistribution in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * Neither the name of Sun Microsystems, Inc. or the names of 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * This software is provided "AS IS," without a warranty of any kind. 20 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 21 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 22 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. 23 * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE 24 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 25 * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL 26 * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, 27 * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR 28 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF 29 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 30 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 31 */ 32 33 #include <stdio.h> 34 #include <fcntl.h> 35 #include <unistd.h> 36 #include <sys/ioctl.h> 37 #include <errno.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 43 #include <ipmitool/ipmi.h> 44 #include <ipmitool/ipmi_intf.h> 45 #include <ipmitool/helper.h> 46 #include <ipmitool/log.h> 47 48 #if defined(HAVE_CONFIG_H) 49 # include <config.h> 50 #endif 51 52 #if defined(HAVE_SYS_IOCCOM_H) 53 # include <sys/ioccom.h> 54 #endif 55 56 #if defined(HAVE_OPENIPMI_H) 57 # if defined(HAVE_LINUX_COMPILER_H) 58 # include <linux/compiler.h> 59 # endif 60 # include <linux/ipmi.h> 61 #elif defined(HAVE_FREEBSD_IPMI_H) 62 /* FreeBSD OpenIPMI-compatible header */ 63 # include <sys/ipmi.h> 64 #else 65 # include "open.h" 66 #endif 67 68 /** 69 * Maximum input message size for KCS/SMIC is 40 with 2 utility bytes and 70 * 38 bytes of data. 71 * Maximum input message size for BT is 42 with 4 utility bytes and 72 * 38 bytes of data. 73 */ 74 #define IPMI_OPENIPMI_MAX_RQ_DATA_SIZE 38 75 76 /** 77 * Maximum output message size for KCS/SMIC is 38 with 2 utility bytes, a byte 78 * for completion code and 35 bytes of data. 79 * Maximum output message size for BT is 40 with 4 utility bytes, a byte 80 * for completion code and 35 bytes of data. 81 */ 82 #define IPMI_OPENIPMI_MAX_RS_DATA_SIZE 35 83 84 extern int verbose; 85 86 static int 87 ipmi_openipmi_open(struct ipmi_intf * intf) 88 { 89 int i = 0; 90 91 char ipmi_dev[16]; 92 char ipmi_devfs[16]; 93 char ipmi_devfs2[16]; 94 int devnum = 0; 95 96 devnum = intf->devnum; 97 98 sprintf(ipmi_dev, "/dev/ipmi%d", devnum); 99 sprintf(ipmi_devfs, "/dev/ipmi/%d", devnum); 100 sprintf(ipmi_devfs2, "/dev/ipmidev/%d", devnum); 101 lprintf(LOG_DEBUG, "Using ipmi device %d", devnum); 102 103 intf->fd = open(ipmi_dev, O_RDWR); 104 105 if (intf->fd < 0) { 106 intf->fd = open(ipmi_devfs, O_RDWR); 107 if (intf->fd < 0) { 108 intf->fd = open(ipmi_devfs2, O_RDWR); 109 } 110 if (intf->fd < 0) { 111 lperror(LOG_ERR, "Could not open device at %s or %s or %s", 112 ipmi_dev, ipmi_devfs , ipmi_devfs2); 113 return -1; 114 } 115 } 116 117 if (ioctl(intf->fd, IPMICTL_SET_GETS_EVENTS_CMD, &i) < 0) { 118 lperror(LOG_ERR, "Could not enable event receiver"); 119 return -1; 120 } 121 122 intf->opened = 1; 123 124 /* This is never set to 0, the default is IPMI_BMC_SLAVE_ADDR */ 125 if (intf->my_addr != 0) { 126 if (intf->set_my_addr(intf, intf->my_addr) < 0) { 127 lperror(LOG_ERR, "Could not set IPMB address"); 128 return -1; 129 } 130 lprintf(LOG_DEBUG, "Set IPMB address to 0x%x", 131 intf->my_addr ); 132 } 133 134 intf->manufacturer_id = ipmi_get_oem(intf); 135 return intf->fd; 136 } 137 static int 138 ipmi_openipmi_set_my_addr(struct ipmi_intf *intf, uint8_t addr) 139 { 140 unsigned int a = addr; 141 if (ioctl(intf->fd, IPMICTL_SET_MY_ADDRESS_CMD, &a) < 0) { 142 lperror(LOG_ERR, "Could not set IPMB address"); 143 return -1; 144 } 145 intf->my_addr = addr; 146 return 0; 147 } 148 149 static void 150 ipmi_openipmi_close(struct ipmi_intf * intf) 151 { 152 if (intf->fd >= 0) { 153 close(intf->fd); 154 intf->fd = -1; 155 } 156 157 intf->opened = 0; 158 intf->manufacturer_id = IPMI_OEM_UNKNOWN; 159 } 160 161 static struct ipmi_rs * 162 ipmi_openipmi_send_cmd(struct ipmi_intf * intf, struct ipmi_rq * req) 163 { 164 struct ipmi_recv recv; 165 struct ipmi_addr addr; 166 struct ipmi_system_interface_addr bmc_addr = { 167 addr_type: IPMI_SYSTEM_INTERFACE_ADDR_TYPE, 168 channel: IPMI_BMC_CHANNEL, 169 }; 170 struct ipmi_ipmb_addr ipmb_addr = { 171 addr_type: IPMI_IPMB_ADDR_TYPE, 172 }; 173 struct ipmi_req _req; 174 static struct ipmi_rs rsp; 175 static int curr_seq = 0; 176 fd_set rset; 177 178 uint8_t * data = NULL; 179 int data_len = 0; 180 181 182 if (intf == NULL || req == NULL) 183 return NULL; 184 185 ipmb_addr.channel = intf->target_channel & 0x0f; 186 187 if (intf->opened == 0 && intf->open != NULL) 188 if (intf->open(intf) < 0) 189 return NULL; 190 191 if (verbose > 2) 192 printbuf(req->msg.data, req->msg.data_len, 193 "OpenIPMI Request Message"); 194 195 /* 196 * setup and send message 197 */ 198 199 memset(&_req, 0, sizeof(struct ipmi_req)); 200 201 if (intf->target_addr != 0 && 202 intf->target_addr != intf->my_addr) { 203 /* use IPMB address if needed */ 204 ipmb_addr.slave_addr = intf->target_addr; 205 ipmb_addr.lun = req->msg.lun; 206 lprintf(LOG_DEBUG, "Sending request 0x%x to " 207 "IPMB target @ 0x%x:0x%x (from 0x%x)", 208 req->msg.cmd, 209 intf->target_addr,intf->target_channel, intf->my_addr); 210 211 if(intf->transit_addr != 0 && intf->transit_addr != intf->my_addr) { 212 uint8_t index = 0; 213 214 lprintf(LOG_DEBUG, "Encapsulating data sent to " 215 "end target [0x%02x,0x%02x] using transit [0x%02x,0x%02x] from 0x%x ", 216 (0x40 | intf->target_channel), 217 intf->target_addr, 218 intf->transit_channel, 219 intf->transit_addr, 220 intf->my_addr 221 ); 222 223 /* Convert Message to 'Send Message' */ 224 /* Supplied req : req , internal req : _req */ 225 226 if (verbose > 4) { 227 fprintf(stderr, "Converting message:\n"); 228 fprintf(stderr, " netfn = 0x%x\n", req->msg.netfn ); 229 fprintf(stderr, " cmd = 0x%x\n", req->msg.cmd); 230 if (recv.msg.data && recv.msg.data_len) { 231 fprintf(stderr, " data_len = %d\n", req->msg.data_len); 232 fprintf(stderr, " data = %s\n", 233 buf2str(req->msg.data,req->msg.data_len)); 234 } 235 } 236 237 /* Modify target address to use 'transit' instead */ 238 ipmb_addr.slave_addr = intf->transit_addr; 239 ipmb_addr.channel = intf->transit_channel; 240 241 /* FIXME backup "My address" */ 242 data_len = req->msg.data_len + 8; 243 data = malloc(data_len); 244 if (data == NULL) { 245 lprintf(LOG_ERR, "ipmitool: malloc failure"); 246 return NULL; 247 } 248 249 memset(data, 0, data_len); 250 251 data[index++] = (0x40|intf->target_channel); 252 data[index++] = intf->target_addr; 253 data[index++] = ( req->msg.netfn << 2 ) | req->msg.lun ; 254 data[index++] = ipmi_csum(data+1, 2); 255 data[index++] = 0xFF; /* normally 0x20 , overwritten by IPMC */ 256 data[index++] = ( (0) << 2) | 0 ; /* FIXME */ 257 data[index++] = req->msg.cmd; 258 memcpy( (data+index) , req->msg.data, req->msg.data_len); 259 index += req->msg.data_len; 260 data[index++] = ipmi_csum( (data+4),(req->msg.data_len + 3) ); 261 262 if (verbose > 4) { 263 fprintf(stderr, "Encapsulated message:\n"); 264 fprintf(stderr, " netfn = 0x%x\n", IPMI_NETFN_APP ); 265 fprintf(stderr, " cmd = 0x%x\n", 0x34 ); 266 if (data && data_len) { 267 fprintf(stderr, " data_len = %d\n", data_len); 268 fprintf(stderr, " data = %s\n", 269 buf2str(data,data_len)); 270 } 271 } 272 } 273 _req.addr = (unsigned char *) &ipmb_addr; 274 _req.addr_len = sizeof(ipmb_addr); 275 } else { 276 /* otherwise use system interface */ 277 lprintf(LOG_DEBUG+2, "Sending request 0x%x to " 278 "System Interface", req->msg.cmd); 279 bmc_addr.lun = req->msg.lun; 280 _req.addr = (unsigned char *) &bmc_addr; 281 _req.addr_len = sizeof(bmc_addr); 282 } 283 284 _req.msgid = curr_seq++; 285 286 /* In case of a bridge request */ 287 if( data != NULL && data_len != 0 ) { 288 _req.msg.data = data; 289 _req.msg.data_len = data_len; 290 _req.msg.netfn = IPMI_NETFN_APP; 291 _req.msg.cmd = 0x34; 292 293 } else { 294 _req.msg.data = req->msg.data; 295 _req.msg.data_len = req->msg.data_len; 296 _req.msg.netfn = req->msg.netfn; 297 _req.msg.cmd = req->msg.cmd; 298 } 299 300 if (ioctl(intf->fd, IPMICTL_SEND_COMMAND, &_req) < 0) { 301 lperror(LOG_ERR, "Unable to send command"); 302 if (data != NULL) { 303 free(data); 304 data = NULL; 305 } 306 return NULL; 307 } 308 309 /* 310 * wait for and retrieve response 311 */ 312 313 if (intf->noanswer) { 314 if (data != NULL) { 315 free(data); 316 data = NULL; 317 } 318 return NULL; 319 } 320 321 FD_ZERO(&rset); 322 FD_SET(intf->fd, &rset); 323 324 if (select(intf->fd+1, &rset, NULL, NULL, NULL) < 0) { 325 lperror(LOG_ERR, "I/O Error"); 326 if (data != NULL) { 327 free(data); 328 data = NULL; 329 } 330 return NULL; 331 } 332 if (FD_ISSET(intf->fd, &rset) == 0) { 333 lprintf(LOG_ERR, "No data available"); 334 if (data != NULL) { 335 free(data); 336 data = NULL; 337 } 338 return NULL; 339 } 340 341 recv.addr = (unsigned char *) &addr; 342 recv.addr_len = sizeof(addr); 343 recv.msg.data = rsp.data; 344 recv.msg.data_len = sizeof(rsp.data); 345 346 /* get data */ 347 if (ioctl(intf->fd, IPMICTL_RECEIVE_MSG_TRUNC, &recv) < 0) { 348 lperror(LOG_ERR, "Error receiving message"); 349 if (errno != EMSGSIZE) { 350 if (data != NULL) { 351 free(data); 352 data = NULL; 353 } 354 return NULL; 355 } 356 } 357 358 if (verbose > 4) { 359 fprintf(stderr, "Got message:"); 360 fprintf(stderr, " type = %d\n", recv.recv_type); 361 fprintf(stderr, " channel = 0x%x\n", addr.channel); 362 fprintf(stderr, " msgid = %ld\n", recv.msgid); 363 fprintf(stderr, " netfn = 0x%x\n", recv.msg.netfn); 364 fprintf(stderr, " cmd = 0x%x\n", recv.msg.cmd); 365 if (recv.msg.data && recv.msg.data_len) { 366 fprintf(stderr, " data_len = %d\n", recv.msg.data_len); 367 fprintf(stderr, " data = %s\n", 368 buf2str(recv.msg.data, recv.msg.data_len)); 369 } 370 } 371 372 if(intf->transit_addr != 0 && intf->transit_addr != intf->my_addr) { 373 uint8_t index = 0; 374 375 /* ipmb_addr.transit_slave_addr = intf->transit_addr; */ 376 lprintf(LOG_DEBUG, "Decapsulating data received from transit " 377 "IPMB target @ 0x%x", intf->transit_addr); 378 379 /* comp code */ 380 /* Check data */ 381 382 if( recv.msg.data[0] == 0 ) { 383 recv.msg.netfn = recv.msg.data[2] >> 2; 384 recv.msg.cmd = recv.msg.data[6]; 385 386 recv.msg.data = memmove(recv.msg.data ,recv.msg.data+7 , recv.msg.data_len - 7); 387 recv.msg.data_len -=8; 388 389 if (verbose > 4) { 390 fprintf(stderr, "Decapsulated message:\n"); 391 fprintf(stderr, " netfn = 0x%x\n", recv.msg.netfn ); 392 fprintf(stderr, " cmd = 0x%x\n", recv.msg.cmd); 393 if (recv.msg.data && recv.msg.data_len) { 394 fprintf(stderr, " data_len = %d\n", recv.msg.data_len); 395 fprintf(stderr, " data = %s\n", 396 buf2str(recv.msg.data,recv.msg.data_len)); 397 } 398 } 399 } 400 } 401 402 /* save completion code */ 403 rsp.ccode = recv.msg.data[0]; 404 rsp.data_len = recv.msg.data_len - 1; 405 406 /* save response data for caller */ 407 if (rsp.ccode == 0 && rsp.data_len > 0) { 408 memmove(rsp.data, rsp.data + 1, rsp.data_len); 409 rsp.data[rsp.data_len] = 0; 410 } 411 412 if (data != NULL) { 413 free(data); 414 data = NULL; 415 } 416 417 return &rsp; 418 } 419 420 int ipmi_openipmi_setup(struct ipmi_intf * intf) 421 { 422 /* set default payload size */ 423 intf->max_request_data_size = IPMI_OPENIPMI_MAX_RQ_DATA_SIZE; 424 intf->max_response_data_size = IPMI_OPENIPMI_MAX_RS_DATA_SIZE; 425 426 return 0; 427 } 428 429 struct ipmi_intf ipmi_open_intf = { 430 name: "open", 431 desc: "Linux OpenIPMI Interface", 432 setup: ipmi_openipmi_setup, 433 open: ipmi_openipmi_open, 434 close: ipmi_openipmi_close, 435 sendrecv: ipmi_openipmi_send_cmd, 436 set_my_addr: ipmi_openipmi_set_my_addr, 437 my_addr: IPMI_BMC_SLAVE_ADDR, 438 target_addr: 0, /* init so -m local_addr does not cause bridging */ 439 }; 440