ACPI / IPMI: Fix potential response buffer overflow
[deliverable/linux.git] / drivers / acpi / acpi_ipmi.c
CommitLineData
e92b297c
ZY
1/*
2 * acpi_ipmi.c - ACPI IPMI opregion
3 *
4 * Copyright (C) 2010 Intel Corporation
5 * Copyright (C) 2010 Zhao Yakui <yakui.zhao@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/delay.h>
31#include <linux/proc_fs.h>
32#include <linux/seq_file.h>
33#include <linux/interrupt.h>
34#include <linux/list.h>
35#include <linux/spinlock.h>
36#include <linux/io.h>
37#include <acpi/acpi_bus.h>
38#include <acpi/acpi_drivers.h>
39#include <linux/ipmi.h>
40#include <linux/device.h>
41#include <linux/pnp.h>
06a8566b 42#include <linux/spinlock.h>
e92b297c
ZY
43
44MODULE_AUTHOR("Zhao Yakui");
45MODULE_DESCRIPTION("ACPI IPMI Opregion driver");
46MODULE_LICENSE("GPL");
47
48#define IPMI_FLAGS_HANDLER_INSTALL 0
49
50#define ACPI_IPMI_OK 0
51#define ACPI_IPMI_TIMEOUT 0x10
52#define ACPI_IPMI_UNKNOWN 0x07
53/* the IPMI timeout is 5s */
54#define IPMI_TIMEOUT (5 * HZ)
6b68f03f 55#define ACPI_IPMI_MAX_MSG_LENGTH 64
e92b297c
ZY
56
57struct acpi_ipmi_device {
58 /* the device list attached to driver_data.ipmi_devices */
59 struct list_head head;
60 /* the IPMI request message list */
61 struct list_head tx_msg_list;
06a8566b 62 spinlock_t tx_msg_lock;
e92b297c
ZY
63 acpi_handle handle;
64 struct pnp_dev *pnp_dev;
65 ipmi_user_t user_interface;
66 int ipmi_ifnum; /* IPMI interface number */
67 long curr_msgid;
68 unsigned long flags;
69 struct ipmi_smi_info smi_data;
70};
71
72struct ipmi_driver_data {
73 struct list_head ipmi_devices;
74 struct ipmi_smi_watcher bmc_events;
75 struct ipmi_user_hndl ipmi_hndlrs;
76 struct mutex ipmi_lock;
77};
78
79struct acpi_ipmi_msg {
80 struct list_head head;
81 /*
82 * General speaking the addr type should be SI_ADDR_TYPE. And
83 * the addr channel should be BMC.
84 * In fact it can also be IPMB type. But we will have to
85 * parse it from the Netfn command buffer. It is so complex
86 * that it is skipped.
87 */
88 struct ipmi_addr addr;
89 long tx_msgid;
90 /* it is used to track whether the IPMI message is finished */
91 struct completion tx_complete;
92 struct kernel_ipmi_msg tx_message;
93 int msg_done;
6b68f03f
LZ
94 /* tx/rx data . And copy it from/to ACPI object buffer */
95 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
96 u8 rx_len;
e92b297c
ZY
97 struct acpi_ipmi_device *device;
98};
99
100/* IPMI request/response buffer per ACPI 4.0, sec 5.5.2.4.3.2 */
101struct acpi_ipmi_buffer {
102 u8 status;
103 u8 length;
6b68f03f 104 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
e92b297c
ZY
105};
106
107static void ipmi_register_bmc(int iface, struct device *dev);
108static void ipmi_bmc_gone(int iface);
109static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
110static void acpi_add_ipmi_device(struct acpi_ipmi_device *ipmi_device);
111static void acpi_remove_ipmi_device(struct acpi_ipmi_device *ipmi_device);
112
113static struct ipmi_driver_data driver_data = {
114 .ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
115 .bmc_events = {
116 .owner = THIS_MODULE,
117 .new_smi = ipmi_register_bmc,
118 .smi_gone = ipmi_bmc_gone,
119 },
120 .ipmi_hndlrs = {
121 .ipmi_recv_hndl = ipmi_msg_handler,
122 },
123};
124
125static struct acpi_ipmi_msg *acpi_alloc_ipmi_msg(struct acpi_ipmi_device *ipmi)
126{
127 struct acpi_ipmi_msg *ipmi_msg;
128 struct pnp_dev *pnp_dev = ipmi->pnp_dev;
129
130 ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
131 if (!ipmi_msg) {
132 dev_warn(&pnp_dev->dev, "Can't allocate memory for ipmi_msg\n");
133 return NULL;
134 }
135 init_completion(&ipmi_msg->tx_complete);
136 INIT_LIST_HEAD(&ipmi_msg->head);
137 ipmi_msg->device = ipmi;
138 return ipmi_msg;
139}
140
141#define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
142#define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
6b68f03f 143static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
e92b297c
ZY
144 acpi_physical_address address,
145 acpi_integer *value)
146{
147 struct kernel_ipmi_msg *msg;
148 struct acpi_ipmi_buffer *buffer;
149 struct acpi_ipmi_device *device;
06a8566b 150 unsigned long flags;
e92b297c
ZY
151
152 msg = &tx_msg->tx_message;
153 /*
154 * IPMI network function and command are encoded in the address
155 * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
156 */
157 msg->netfn = IPMI_OP_RGN_NETFN(address);
158 msg->cmd = IPMI_OP_RGN_CMD(address);
6b68f03f 159 msg->data = tx_msg->data;
e92b297c
ZY
160 /*
161 * value is the parameter passed by the IPMI opregion space handler.
162 * It points to the IPMI request message buffer
163 */
164 buffer = (struct acpi_ipmi_buffer *)value;
165 /* copy the tx message data */
6b68f03f
LZ
166 if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
167 dev_WARN_ONCE(&tx_msg->device->pnp_dev->dev, true,
168 "Unexpected request (msg len %d).\n",
169 buffer->length);
170 return -EINVAL;
171 }
e92b297c 172 msg->data_len = buffer->length;
6b68f03f 173 memcpy(tx_msg->data, buffer->data, msg->data_len);
e92b297c
ZY
174 /*
175 * now the default type is SYSTEM_INTERFACE and channel type is BMC.
176 * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
177 * the addr type should be changed to IPMB. Then we will have to parse
178 * the IPMI request message buffer to get the IPMB address.
179 * If so, please fix me.
180 */
181 tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
182 tx_msg->addr.channel = IPMI_BMC_CHANNEL;
183 tx_msg->addr.data[0] = 0;
184
185 /* Get the msgid */
186 device = tx_msg->device;
06a8566b 187 spin_lock_irqsave(&device->tx_msg_lock, flags);
e92b297c
ZY
188 device->curr_msgid++;
189 tx_msg->tx_msgid = device->curr_msgid;
06a8566b 190 spin_unlock_irqrestore(&device->tx_msg_lock, flags);
6b68f03f 191 return 0;
e92b297c
ZY
192}
193
194static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
195 acpi_integer *value, int rem_time)
196{
197 struct acpi_ipmi_buffer *buffer;
198
199 /*
200 * value is also used as output parameter. It represents the response
201 * IPMI message returned by IPMI command.
202 */
203 buffer = (struct acpi_ipmi_buffer *)value;
204 if (!rem_time && !msg->msg_done) {
205 buffer->status = ACPI_IPMI_TIMEOUT;
206 return;
207 }
208 /*
209 * If the flag of msg_done is not set or the recv length is zero, it
210 * means that the IPMI command is not executed correctly.
211 * The status code will be ACPI_IPMI_UNKNOWN.
212 */
213 if (!msg->msg_done || !msg->rx_len) {
214 buffer->status = ACPI_IPMI_UNKNOWN;
215 return;
216 }
217 /*
218 * If the IPMI response message is obtained correctly, the status code
219 * will be ACPI_IPMI_OK
220 */
221 buffer->status = ACPI_IPMI_OK;
222 buffer->length = msg->rx_len;
6b68f03f 223 memcpy(buffer->data, msg->data, msg->rx_len);
e92b297c
ZY
224}
225
226static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
227{
228 struct acpi_ipmi_msg *tx_msg, *temp;
229 int count = HZ / 10;
230 struct pnp_dev *pnp_dev = ipmi->pnp_dev;
231
232 list_for_each_entry_safe(tx_msg, temp, &ipmi->tx_msg_list, head) {
233 /* wake up the sleep thread on the Tx msg */
234 complete(&tx_msg->tx_complete);
235 }
236
237 /* wait for about 100ms to flush the tx message list */
238 while (count--) {
239 if (list_empty(&ipmi->tx_msg_list))
240 break;
241 schedule_timeout(1);
242 }
243 if (!list_empty(&ipmi->tx_msg_list))
244 dev_warn(&pnp_dev->dev, "tx msg list is not NULL\n");
245}
246
247static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
248{
249 struct acpi_ipmi_device *ipmi_device = user_msg_data;
250 int msg_found = 0;
251 struct acpi_ipmi_msg *tx_msg;
252 struct pnp_dev *pnp_dev = ipmi_device->pnp_dev;
06a8566b 253 unsigned long flags;
e92b297c
ZY
254
255 if (msg->user != ipmi_device->user_interface) {
256 dev_warn(&pnp_dev->dev, "Unexpected response is returned. "
257 "returned user %p, expected user %p\n",
258 msg->user, ipmi_device->user_interface);
6b68f03f 259 goto out_msg;
e92b297c 260 }
06a8566b 261 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
e92b297c
ZY
262 list_for_each_entry(tx_msg, &ipmi_device->tx_msg_list, head) {
263 if (msg->msgid == tx_msg->tx_msgid) {
264 msg_found = 1;
265 break;
266 }
267 }
268
06a8566b 269 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
e92b297c
ZY
270 if (!msg_found) {
271 dev_warn(&pnp_dev->dev, "Unexpected response (msg id %ld) is "
272 "returned.\n", msg->msgid);
6b68f03f 273 goto out_msg;
e92b297c
ZY
274 }
275
6b68f03f
LZ
276 /* copy the response data to Rx_data buffer */
277 if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
278 dev_WARN_ONCE(&pnp_dev->dev, true,
279 "Unexpected response (msg len %d).\n",
280 msg->msg.data_len);
281 } else {
e92b297c 282 tx_msg->rx_len = msg->msg.data_len;
6b68f03f 283 memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
e92b297c
ZY
284 tx_msg->msg_done = 1;
285 }
286 complete(&tx_msg->tx_complete);
6b68f03f 287out_msg:
e92b297c
ZY
288 ipmi_free_recv_msg(msg);
289};
290
291static void ipmi_register_bmc(int iface, struct device *dev)
292{
293 struct acpi_ipmi_device *ipmi_device, *temp;
294 struct pnp_dev *pnp_dev;
295 ipmi_user_t user;
296 int err;
297 struct ipmi_smi_info smi_data;
298 acpi_handle handle;
299
300 err = ipmi_get_smi_info(iface, &smi_data);
301
302 if (err)
303 return;
304
305 if (smi_data.addr_src != SI_ACPI) {
306 put_device(smi_data.dev);
307 return;
308 }
309
310 handle = smi_data.addr_info.acpi_info.acpi_handle;
311
312 mutex_lock(&driver_data.ipmi_lock);
313 list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
314 /*
315 * if the corresponding ACPI handle is already added
316 * to the device list, don't add it again.
317 */
318 if (temp->handle == handle)
319 goto out;
320 }
321
322 ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
323
324 if (!ipmi_device)
325 goto out;
326
327 pnp_dev = to_pnp_dev(smi_data.dev);
328 ipmi_device->handle = handle;
329 ipmi_device->pnp_dev = pnp_dev;
330
331 err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
332 ipmi_device, &user);
333 if (err) {
334 dev_warn(&pnp_dev->dev, "Can't create IPMI user interface\n");
335 kfree(ipmi_device);
336 goto out;
337 }
338 acpi_add_ipmi_device(ipmi_device);
339 ipmi_device->user_interface = user;
340 ipmi_device->ipmi_ifnum = iface;
341 mutex_unlock(&driver_data.ipmi_lock);
342 memcpy(&ipmi_device->smi_data, &smi_data, sizeof(struct ipmi_smi_info));
343 return;
344
345out:
346 mutex_unlock(&driver_data.ipmi_lock);
347 put_device(smi_data.dev);
348 return;
349}
350
351static void ipmi_bmc_gone(int iface)
352{
353 struct acpi_ipmi_device *ipmi_device, *temp;
354
355 mutex_lock(&driver_data.ipmi_lock);
356 list_for_each_entry_safe(ipmi_device, temp,
357 &driver_data.ipmi_devices, head) {
358 if (ipmi_device->ipmi_ifnum != iface)
359 continue;
360
361 acpi_remove_ipmi_device(ipmi_device);
362 put_device(ipmi_device->smi_data.dev);
363 kfree(ipmi_device);
364 break;
365 }
366 mutex_unlock(&driver_data.ipmi_lock);
367}
368/* --------------------------------------------------------------------------
369 * Address Space Management
370 * -------------------------------------------------------------------------- */
371/*
372 * This is the IPMI opregion space handler.
373 * @function: indicates the read/write. In fact as the IPMI message is driven
374 * by command, only write is meaningful.
375 * @address: This contains the netfn/command of IPMI request message.
376 * @bits : not used.
377 * @value : it is an in/out parameter. It points to the IPMI message buffer.
378 * Before the IPMI message is sent, it represents the actual request
379 * IPMI message. After the IPMI message is finished, it represents
380 * the response IPMI message returned by IPMI command.
381 * @handler_context: IPMI device context.
382 */
383
384static acpi_status
385acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
386 u32 bits, acpi_integer *value,
387 void *handler_context, void *region_context)
388{
389 struct acpi_ipmi_msg *tx_msg;
390 struct acpi_ipmi_device *ipmi_device = handler_context;
391 int err, rem_time;
392 acpi_status status;
06a8566b 393 unsigned long flags;
e92b297c
ZY
394 /*
395 * IPMI opregion message.
396 * IPMI message is firstly written to the BMC and system software
397 * can get the respsonse. So it is unmeaningful for the read access
398 * of IPMI opregion.
399 */
400 if ((function & ACPI_IO_MASK) == ACPI_READ)
401 return AE_TYPE;
402
403 if (!ipmi_device->user_interface)
404 return AE_NOT_EXIST;
405
406 tx_msg = acpi_alloc_ipmi_msg(ipmi_device);
407 if (!tx_msg)
408 return AE_NO_MEMORY;
409
6b68f03f
LZ
410 if (acpi_format_ipmi_request(tx_msg, address, value) != 0) {
411 status = AE_TYPE;
412 goto out_msg;
413 }
06a8566b 414 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
e92b297c 415 list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
06a8566b 416 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
e92b297c
ZY
417 err = ipmi_request_settime(ipmi_device->user_interface,
418 &tx_msg->addr,
419 tx_msg->tx_msgid,
420 &tx_msg->tx_message,
421 NULL, 0, 0, 0);
422 if (err) {
423 status = AE_ERROR;
6b68f03f 424 goto out_list;
e92b297c
ZY
425 }
426 rem_time = wait_for_completion_timeout(&tx_msg->tx_complete,
427 IPMI_TIMEOUT);
428 acpi_format_ipmi_response(tx_msg, value, rem_time);
429 status = AE_OK;
430
6b68f03f 431out_list:
06a8566b 432 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
e92b297c 433 list_del(&tx_msg->head);
06a8566b 434 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
6b68f03f 435out_msg:
e92b297c
ZY
436 kfree(tx_msg);
437 return status;
438}
439
440static void ipmi_remove_space_handler(struct acpi_ipmi_device *ipmi)
441{
442 if (!test_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags))
443 return;
444
445 acpi_remove_address_space_handler(ipmi->handle,
446 ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler);
447
448 clear_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags);
449}
450
451static int ipmi_install_space_handler(struct acpi_ipmi_device *ipmi)
452{
453 acpi_status status;
454
455 if (test_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags))
456 return 0;
457
458 status = acpi_install_address_space_handler(ipmi->handle,
459 ACPI_ADR_SPACE_IPMI,
460 &acpi_ipmi_space_handler,
461 NULL, ipmi);
462 if (ACPI_FAILURE(status)) {
463 struct pnp_dev *pnp_dev = ipmi->pnp_dev;
464 dev_warn(&pnp_dev->dev, "Can't register IPMI opregion space "
465 "handle\n");
466 return -EINVAL;
467 }
468 set_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags);
469 return 0;
470}
471
472static void acpi_add_ipmi_device(struct acpi_ipmi_device *ipmi_device)
473{
474
475 INIT_LIST_HEAD(&ipmi_device->head);
476
06a8566b 477 spin_lock_init(&ipmi_device->tx_msg_lock);
e92b297c
ZY
478 INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
479 ipmi_install_space_handler(ipmi_device);
480
481 list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
482}
483
484static void acpi_remove_ipmi_device(struct acpi_ipmi_device *ipmi_device)
485{
486 /*
487 * If the IPMI user interface is created, it should be
488 * destroyed.
489 */
490 if (ipmi_device->user_interface) {
491 ipmi_destroy_user(ipmi_device->user_interface);
492 ipmi_device->user_interface = NULL;
493 }
494 /* flush the Tx_msg list */
495 if (!list_empty(&ipmi_device->tx_msg_list))
496 ipmi_flush_tx_msg(ipmi_device);
497
498 list_del(&ipmi_device->head);
499 ipmi_remove_space_handler(ipmi_device);
500}
501
502static int __init acpi_ipmi_init(void)
503{
504 int result = 0;
505
506 if (acpi_disabled)
507 return result;
508
509 mutex_init(&driver_data.ipmi_lock);
510
511 result = ipmi_smi_watcher_register(&driver_data.bmc_events);
512
513 return result;
514}
515
516static void __exit acpi_ipmi_exit(void)
517{
518 struct acpi_ipmi_device *ipmi_device, *temp;
519
520 if (acpi_disabled)
521 return;
522
523 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
524
525 /*
526 * When one smi_watcher is unregistered, it is only deleted
527 * from the smi_watcher list. But the smi_gone callback function
528 * is not called. So explicitly uninstall the ACPI IPMI oregion
529 * handler and free it.
530 */
531 mutex_lock(&driver_data.ipmi_lock);
532 list_for_each_entry_safe(ipmi_device, temp,
533 &driver_data.ipmi_devices, head) {
534 acpi_remove_ipmi_device(ipmi_device);
535 put_device(ipmi_device->smi_data.dev);
536 kfree(ipmi_device);
537 }
538 mutex_unlock(&driver_data.ipmi_lock);
539}
540
541module_init(acpi_ipmi_init);
542module_exit(acpi_ipmi_exit);
This page took 0.342592 seconds and 5 git commands to generate.