summaryrefslogtreecommitdiff
path: root/src/plugins/ipmi_intf.c
blob: 9225a34bd4fcd7944820ff8ddc39e02b09bc609c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
 * Copyright (c) 2003 Sun Microsystems, Inc.  All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistribution of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
 * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL
 * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
 * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */
#define _XOPEN_SOURCE 700
#define _GNU_SOURCE 1
#define _DARWIN_C_SOURCE 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif

#if defined(IPMI_INTF_LAN) || defined (IPMI_INTF_LANPLUS)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <netdb.h>
#endif


#include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi.h>
#include <ipmitool/ipmi_sdr.h>
#include <ipmitool/log.h>

#define IPMI_DEFAULT_PAYLOAD_SIZE   25

#ifdef IPMI_INTF_OPEN
extern struct ipmi_intf ipmi_open_intf;
#endif
#ifdef IPMI_INTF_IMB
extern struct ipmi_intf ipmi_imb_intf;
#endif
#ifdef IPMI_INTF_LIPMI
extern struct ipmi_intf ipmi_lipmi_intf;
#endif
#ifdef IPMI_INTF_BMC
extern struct ipmi_intf ipmi_bmc_intf;
#endif
#ifdef IPMI_INTF_LAN
extern struct ipmi_intf ipmi_lan_intf;
#endif
#ifdef IPMI_INTF_LANPLUS
extern struct ipmi_intf ipmi_lanplus_intf;
#endif
#ifdef IPMI_INTF_FREE
extern struct ipmi_intf ipmi_free_intf;
#endif
#ifdef IPMI_INTF_SERIAL
extern struct ipmi_intf ipmi_serial_term_intf;
extern struct ipmi_intf ipmi_serial_bm_intf;
#endif
#ifdef IPMI_INTF_DUMMY
extern struct ipmi_intf ipmi_dummy_intf;
#endif
#ifdef IPMI_INTF_USB
extern struct ipmi_intf ipmi_usb_intf;
#endif

struct ipmi_intf * ipmi_intf_table[] = {
#ifdef IPMI_INTF_OPEN
	&ipmi_open_intf,
#endif
#ifdef IPMI_INTF_IMB
	&ipmi_imb_intf,
#endif
#ifdef IPMI_INTF_LIPMI
	&ipmi_lipmi_intf,
#endif
#ifdef IPMI_INTF_BMC
	&ipmi_bmc_intf,
#endif
#ifdef IPMI_INTF_LAN
	&ipmi_lan_intf,
#endif
#ifdef IPMI_INTF_LANPLUS
	&ipmi_lanplus_intf,
#endif
#ifdef IPMI_INTF_FREE
	&ipmi_free_intf,
#endif
#ifdef IPMI_INTF_SERIAL
	&ipmi_serial_term_intf,
	&ipmi_serial_bm_intf,
#endif
#ifdef IPMI_INTF_DUMMY
	&ipmi_dummy_intf,
#endif
#ifdef IPMI_INTF_USB
	&ipmi_usb_intf,
#endif
	NULL
};

/* ipmi_intf_print  -  Print list of interfaces
 *
 * no meaningful return code
 */
void ipmi_intf_print(struct ipmi_intf_support * intflist)
{
	struct ipmi_intf ** intf;
	struct ipmi_intf_support * sup;
	int def = 1;
	int found;

	lprintf(LOG_NOTICE, "Interfaces:");

	for (intf = ipmi_intf_table; intf && *intf; intf++) {

		if (intflist != NULL) {
			found = 0;
			for (sup=intflist; sup->name != NULL; sup++) {
				if (strncmp(sup->name, (*intf)->name, strlen(sup->name)) == 0 &&
				    strncmp(sup->name, (*intf)->name, strlen((*intf)->name)) == 0 &&
				    sup->supported == 1)
					found = 1;
			}
			if (found == 0)
				continue;
		}

		lprintf(LOG_NOTICE, "\t%-12s  %s %s",
			(*intf)->name, (*intf)->desc,
			def ? "[default]" : "");
		def = 0;
	}
	lprintf(LOG_NOTICE, "");
}

/* ipmi_intf_load  -  Load an interface from the interface table above
 *                    If no interface name is given return first entry
 *
 * @name:	interface name to try and load
 *
 * returns pointer to inteface structure if found
 * returns NULL on error
 */
struct ipmi_intf * ipmi_intf_load(char * name)
{
	struct ipmi_intf ** intf;
	struct ipmi_intf * i;

	if (name == NULL) {
		i = ipmi_intf_table[0];
		if (i->setup != NULL && (i->setup(i) < 0)) {
			lprintf(LOG_ERR, "Unable to setup "
				"interface %s", name);
			return NULL;
		}
		return i;
	}

	for (intf = ipmi_intf_table;
	     ((intf != NULL) && (*intf != NULL));
	     intf++) {
		i = *intf;
		if (strncmp(name, i->name, strlen(name)) == 0) {
			if (i->setup != NULL && (i->setup(i) < 0)) {
				lprintf(LOG_ERR, "Unable to setup "
					"interface %s", name);
				return NULL;
			}
			return i;
		}
	}

	return NULL;
}

void
ipmi_intf_session_set_hostname(struct ipmi_intf * intf, char * hostname)
{
	if (intf->ssn_params.hostname != NULL) {
		free(intf->ssn_params.hostname);
		intf->ssn_params.hostname = NULL;
	}
	if (hostname == NULL) {
		return;
	}
	intf->ssn_params.hostname = strdup(hostname);
}

void
ipmi_intf_session_set_username(struct ipmi_intf * intf, char * username)
{
	memset(intf->ssn_params.username, 0, 17);

	if (username == NULL)
		return;

	memcpy(intf->ssn_params.username, username, __min(strlen(username), 16));
}

void
ipmi_intf_session_set_password(struct ipmi_intf * intf, char * password)
{
	memset(intf->ssn_params.authcode_set, 0, IPMI_AUTHCODE_BUFFER_SIZE);

	if (password == NULL) {
		intf->ssn_params.password = 0;
		return;
	}

	intf->ssn_params.password = 1;
	memcpy(intf->ssn_params.authcode_set, password,
	       __min(strlen(password), IPMI_AUTHCODE_BUFFER_SIZE));
}

void
ipmi_intf_session_set_privlvl(struct ipmi_intf * intf, uint8_t level)
{
	intf->ssn_params.privlvl = level;
}

void
ipmi_intf_session_set_lookupbit(struct ipmi_intf * intf, uint8_t lookupbit)
{
	intf->ssn_params.lookupbit = lookupbit;
}

void
ipmi_intf_session_set_cipher_suite_id(struct ipmi_intf * intf, uint8_t cipher_suite_id)
{
	intf->ssn_params.cipher_suite_id = cipher_suite_id;
}

void
ipmi_intf_session_set_sol_escape_char(struct ipmi_intf * intf, char sol_escape_char)
{
	intf->ssn_params.sol_escape_char = sol_escape_char;
}

void
ipmi_intf_session_set_kgkey(struct ipmi_intf *intf, const uint8_t *kgkey)
{
	memcpy(intf->ssn_params.kg, kgkey, IPMI_KG_BUFFER_SIZE);
}

void
ipmi_intf_session_set_port(struct ipmi_intf * intf, int port)
{
	intf->ssn_params.port = port;
}

void
ipmi_intf_session_set_authtype(struct ipmi_intf * intf, uint8_t authtype)
{
	/* clear password field if authtype NONE specified */
	if (authtype == IPMI_SESSION_AUTHTYPE_NONE) {
		memset(intf->ssn_params.authcode_set, 0, IPMI_AUTHCODE_BUFFER_SIZE);
		intf->ssn_params.password = 0;
	}

	intf->ssn_params.authtype_set = authtype;
}

void
ipmi_intf_session_set_timeout(struct ipmi_intf * intf, uint32_t timeout)
{
	intf->ssn_params.timeout = timeout;
}

void
ipmi_intf_session_set_retry(struct ipmi_intf * intf, int retry)
{
	intf->ssn_params.retry = retry;
}

void
ipmi_intf_session_cleanup(struct ipmi_intf *intf)
{
	if (intf->session == NULL) {
		return;
	}

	free(intf->session);
	intf->session = NULL;
}

void
ipmi_cleanup(struct ipmi_intf * intf)
{
	ipmi_sdr_list_empty(intf);
	ipmi_intf_session_set_hostname(intf, NULL);
}

#if defined(IPMI_INTF_LAN) || defined (IPMI_INTF_LANPLUS)
int
ipmi_intf_socket_connect(struct ipmi_intf * intf)
{
	struct ipmi_session_params *params;

	struct sockaddr_storage addr;
	struct addrinfo hints;
	struct addrinfo *rp0 = NULL, *rp;
	char service[NI_MAXSERV];

	if (!intf) {
		return -1;
	}

	params = &intf->ssn_params;

	if (params->hostname == NULL || strlen((const char *)params->hostname) == 0) {
		lprintf(LOG_ERR, "No hostname specified!");
		return -1;
	}

	/* open port to BMC */
	memset(&addr, 0, sizeof(addr));

	sprintf(service, "%d", params->port);
	/* Obtain address(es) matching host/port */
	memset(&hints, 0, sizeof(hints));
	hints.ai_family   = intf->ai_family;    /* Allow IPv4 or IPv6 */
	hints.ai_socktype = SOCK_DGRAM;   /* Datagram socket */
	hints.ai_flags    = 0;            /* use AI_NUMERICSERV for no name resolution */
	hints.ai_protocol = IPPROTO_UDP; /*  */

	if (getaddrinfo(params->hostname, service, &hints, &rp0) != 0) {
		lprintf(LOG_ERR, "Address lookup for %s failed",
				params->hostname);
		return -1;
	}

	/* getaddrinfo() returns a list of address structures.
	 * Try each address until we successfully connect(2).
	 * If socket(2) (or connect(2)) fails, we (close the socket
	 * and) try the next address.
	 */

	for (rp = rp0; rp != NULL; rp = rp->ai_next) {
		/* We are only interested in IPv4 and IPv6 */
		if ((rp->ai_family != AF_INET6) && (rp->ai_family != AF_INET)) {
			continue;
		}

		intf->fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
		if (intf->fd == -1) {
			continue;
		}

		if (rp->ai_family == AF_INET) {
			if (connect(intf->fd, rp->ai_addr, rp->ai_addrlen) != -1) {
				hints.ai_family = rp->ai_family;
				break;  /* Success */
			}
		}  else if (rp->ai_family == AF_INET6) {
			struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)rp->ai_addr;
			char hbuf[NI_MAXHOST];
			socklen_t len;

			/* The scope was specified on the command line e.g. with -H FE80::219:99FF:FEA0:BD95%eth0 */
			if (addr6->sin6_scope_id != 0) {
				len = sizeof(struct sockaddr_in6);
				if (getnameinfo((struct sockaddr *)addr6, len, hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0) {
					lprintf(LOG_DEBUG, "Trying address: %s scope=%d",
						hbuf,
						addr6->sin6_scope_id);
				}
				if (connect(intf->fd, rp->ai_addr, rp->ai_addrlen) != -1) {
					hints.ai_family = rp->ai_family;
					break;  /* Success */
				}
			} else {
				/* No scope specified, try to get this from the list of interfaces */
				struct ifaddrs *ifaddrs = NULL;
				struct ifaddrs *ifa = NULL;

				if (getifaddrs(&ifaddrs) < 0) {
					lprintf(LOG_ERR, "Interface address lookup for %s failed",
						params->hostname);
					break;
				}

				for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
					if (ifa->ifa_addr == NULL) {
						continue;
					}

					if (ifa->ifa_addr->sa_family == AF_INET6) {
						struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *)ifa->ifa_addr;

						/* Skip unwanted addresses */
						if (IN6_IS_ADDR_MULTICAST(&tmp6->sin6_addr)) {
							continue;
						}
						if (IN6_IS_ADDR_LOOPBACK(&tmp6->sin6_addr)) {
							continue;
						}
						len = sizeof(struct sockaddr_in6);
						if ( getnameinfo((struct sockaddr *)tmp6, len, hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0) {
							lprintf(LOG_DEBUG, "Testing %s interface address: %s scope=%d",
								ifa->ifa_name != NULL ? ifa->ifa_name : "???",
								hbuf,
								tmp6->sin6_scope_id);
						}

						if (tmp6->sin6_scope_id != 0) {
							addr6->sin6_scope_id = tmp6->sin6_scope_id;
						} else {
							/*
							 * No scope information in interface address information
							 * On some OS'es, getifaddrs() is returning out the 'kernel' representation
							 * of scoped addresses which stores the scope in the 3rd and 4th
							 * byte. See also this page:
							 * http://www.freebsd.org/doc/en/books/developers-handbook/ipv6.html
							 */
							if (IN6_IS_ADDR_LINKLOCAL(&tmp6->sin6_addr)
									&& (tmp6->sin6_addr.s6_addr[1] != 0)) {
								addr6->sin6_scope_id = ntohs(tmp6->sin6_addr.s6_addr[1]);
							}
						}

						/* OK, now try to connect with the scope id from this interface address */
						if (addr6->sin6_scope_id != 0 || !IN6_IS_ADDR_LINKLOCAL(&tmp6->sin6_addr)) {
							if (connect(intf->fd, rp->ai_addr, rp->ai_addrlen) != -1) {
								hints.ai_family = rp->ai_family;
								lprintf(LOG_DEBUG, "Successful connected on %s interface with scope id %d", ifa->ifa_name, tmp6->sin6_scope_id);
								break;  /* Success */
							}
						}
					}
				}
				freeifaddrs(ifaddrs);
			}
		}
		if (hints.ai_family != AF_UNSPEC) {
			break;
		}
		close(intf->fd);
		intf->fd = -1;
	}

	/* No longer needed */
	freeaddrinfo(rp0);

	return ((intf->fd != -1) ? 0 : -1);
}
#endif

uint16_t
ipmi_intf_get_max_request_data_size(struct ipmi_intf * intf)
{
	int16_t size;

	size = intf->max_request_data_size;

	/* check if request size is not specified */
	if (!size) {
		/*
		 * The IPMB standard overall message length for ‘non -bridging’
		 * messages is specified as 32 bytes, maximum, including slave
		 * address. This sets the upper limit for typical IPMI messages.
		 * With the exception of messages used for bridging messages to
		 * other busses or interfaces (e.g. Master Write-Read and Send Message)
		 * IPMI messages should be designed to fit within this 32-byte maximum.
		 * In order to support bridging, the Master Write -Read and Send Message
		 * commands are allowed to exceed the 32-byte maximum transaction on IPMB
		 */

		size = IPMI_DEFAULT_PAYLOAD_SIZE;

		/* check if message is forwarded */
		if (intf->target_addr && intf->target_addr != intf->my_addr) {
			/* add Send Message request size */
			size += 8;
		}
	}

	/* check if message is forwarded */
	if (intf->target_addr && intf->target_addr != intf->my_addr) {
		/* subtract send message request size */
		size -= 8;

		/*
		 * Check that forwarded request size is not greater
		 * than the default payload size.
		 */
		if (size > IPMI_DEFAULT_PAYLOAD_SIZE) {
			size = IPMI_DEFAULT_PAYLOAD_SIZE;
		}

		/* check for double bridging */
		if (intf->transit_addr && intf->transit_addr != intf->target_addr) {
			/* subtract inner send message request size */
			size -= 8;
		}
	}

	/* check for underflow */
	if (size < 0) {
		return 0;
	}

	return size;
}

uint16_t
ipmi_intf_get_max_response_data_size(struct ipmi_intf * intf)
{
	int16_t size;

	size = intf->max_response_data_size;

	/* check if response size is not specified */
	if (!size) {
		/*
		 * The IPMB standard overall message length for ‘non -bridging’
		 * messages is specified as 32 bytes, maximum, including slave
		 * address. This sets the upper limit for typical IPMI messages.
		 * With the exception of messages used for bridging messages to
		 * other busses or interfaces (e.g. Master Write-Read and Send Message)
		 * IPMI messages should be designed to fit within this 32-byte maximum.
		 * In order to support bridging, the Master Write -Read and Send Message
		 * commands are allowed to exceed the 32-byte maximum transaction on IPMB
		 */

		size = IPMI_DEFAULT_PAYLOAD_SIZE; /* response length with subtracted header and checksum byte */

		/* check if message is forwarded */
		if (intf->target_addr && intf->target_addr != intf->my_addr) {
			/* add Send Message header size */
			size += 7;
		}
	}

	/* check if message is forwarded */
	if (intf->target_addr && intf->target_addr != intf->my_addr) {
		/*
		 * Some IPMI controllers like PICMG AMC Carriers embed responses
		 * to the forwarded messages into the Send Message response.
		 * In order to be sure that the response is not truncated,
		 * subtract the internal message header size.
		 */
		size -= 8;

		/*
		 * Check that forwarded response is not greater
		 * than the default payload size.
		 */
		if (size > IPMI_DEFAULT_PAYLOAD_SIZE) {
			size = IPMI_DEFAULT_PAYLOAD_SIZE;
		}

		/* check for double bridging */
		if (intf->transit_addr && intf->transit_addr != intf->target_addr) {
			/* subtract inner send message header size */
			size -= 8;
		}
	}

	/* check for underflow */
	if (size < 0) {
		return 0;
	}

	return size;
}

void
ipmi_intf_set_max_request_data_size(struct ipmi_intf * intf, uint16_t size)
{
	if (size < IPMI_DEFAULT_PAYLOAD_SIZE) {
		lprintf(LOG_ERR, "Request size is too small (%d), leave default size",
				size);
		return;
	}

	if (intf->set_max_request_data_size) {
		intf->set_max_request_data_size(intf, size);
	} else {
		intf->max_request_data_size = size;
	}
}

void
ipmi_intf_set_max_response_data_size(struct ipmi_intf * intf, uint16_t size)
{
	if (size < IPMI_DEFAULT_PAYLOAD_SIZE - 1) {
		lprintf(LOG_ERR, "Response size is too small (%d), leave default size",
				size);
		return;
	}

	if (intf->set_max_response_data_size) {
		intf->set_max_response_data_size(intf, size);
	} else {
		intf->max_response_data_size = size;
	}
}