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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include "libipmi.h"
#include "ipmi_lan.h"
#include "ipmi_impl.h"
#define DEF_IPMI_LAN_TIMEOUT 3 /* seconds */
#define DEF_IPMI_LAN_NUM_RETRIES 5
#define IPMI_LAN_CHANNEL_E 0x0e
typedef struct ipmi_rs {
uint8_t ir_data[IPMI_BUF_SIZE];
int ir_dlen;
ipmi_msg_hdr_t ir_ihdr;
uint8_t ir_ccode;
} ipmi_rs_t;
static ipmi_rs_t *ipmi_lan_poll_recv(ipmi_handle_t *);
typedef struct ipmi_rq_entry {
ipmi_list_t ire_list;
ipmi_cmd_t ire_req;
uint8_t ire_target_cmd;
uint8_t ire_rq_seq;
uint8_t *ire_msg_data;
int ire_msg_len;
} ipmi_rq_entry_t;
ipmi_rq_entry_t *ipmi_req_entries = NULL;
/*
* LAN transport-specific data
*/
typedef struct ipmi_lan {
ipmi_handle_t *il_ihp;
char il_host[MAXHOSTNAMELEN + 1];
uint16_t il_port;
char il_user[17];
char il_authcode[IPMI_AUTHCODE_BUF_SIZE + 1];
uint8_t il_challenge[16];
uint32_t il_session_id;
int il_sd;
boolean_t il_send_authcode;
boolean_t il_session_active;
uint8_t il_authtype;
uint8_t il_privlvl;
uint8_t il_num_retries;
uint32_t il_in_seq;
uint32_t il_timeout;
struct sockaddr_in il_addr;
socklen_t il_addrlen;
} ipmi_lan_t;
/*
* Calculate and returns IPMI checksum
*
* Checksum algorithm is described in Section 13.8
*
* d: buffer to check
* s: position in buffer to start checksum from
*/
static uint8_t
ipmi_csum(uint8_t *d, int s)
{
uint8_t c = 0;
for (; s > 0; s--, d++)
c += *d;
return (-c);
}
static ipmi_rq_entry_t *
ipmi_req_add_entry(ipmi_handle_t *ihp, ipmi_cmd_t *req)
{
ipmi_rq_entry_t *e;
if ((e = ipmi_zalloc(ihp, sizeof (ipmi_rq_entry_t))) == NULL)
return (NULL);
(void) memcpy(&e->ire_req, req, sizeof (ipmi_cmd_t));
ipmi_list_append(&ipmi_req_entries->ire_list, e);
return (e);
}
/*ARGSUSED*/
static ipmi_rq_entry_t *
ipmi_req_lookup_entry(ipmi_handle_t *ihp, uint8_t seq, uint8_t cmd)
{
ipmi_rq_entry_t *e;
for (e = ipmi_list_next(&ipmi_req_entries->ire_list); e != NULL;
e = ipmi_list_next(e))
if (e->ire_rq_seq == seq && e->ire_req.ic_cmd == cmd)
return (e);
return (NULL);
}
static void
ipmi_req_remove_entry(ipmi_handle_t *ihp, uint8_t seq, uint8_t cmd)
{
ipmi_rq_entry_t *e;
e = ipmi_req_lookup_entry(ihp, seq, cmd);
if (e) {
ipmi_list_delete(&ipmi_req_entries->ire_list, e);
ipmi_free(ihp, e->ire_msg_data);
ipmi_free(ihp, e);
}
}
static void
ipmi_req_clear_entries(ipmi_handle_t *ihp)
{
ipmi_rq_entry_t *e;
while ((e = ipmi_list_next(&ipmi_req_entries->ire_list)) != NULL) {
ipmi_list_delete(&ipmi_req_entries->ire_list, e);
ipmi_free(ihp, e);
}
}
static int
get_random(void *buf, uint_t len)
{
int fd;
assert(buf != NULL && len > 0);
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
return (-1);
if (read(fd, buf, len) < 0) {
(void) close(fd);
return (-1);
}
(void) close(fd);
return (0);
}
static int
ipmi_lan_send_packet(ipmi_handle_t *ihp, uint8_t *data, int dlen)
{
ipmi_lan_t *ilp = (ipmi_lan_t *)ihp->ih_tdata;
return (send(ilp->il_sd, data, dlen, 0));
}
static ipmi_rs_t *
ipmi_lan_recv_packet(ipmi_handle_t *ihp)
{
static ipmi_rs_t rsp;
fd_set read_set, err_set;
struct timeval tmout;
ipmi_lan_t *ilp = (ipmi_lan_t *)ihp->ih_tdata;
int ret;
FD_ZERO(&read_set);
FD_SET(ilp->il_sd, &read_set);
FD_ZERO(&err_set);
FD_SET(ilp->il_sd, &err_set);
tmout.tv_sec = ilp->il_timeout;
tmout.tv_usec = 0;
ret = select(ilp->il_sd + 1, &read_set, NULL, &err_set, &tmout);
if (ret < 0 || FD_ISSET(ilp->il_sd, &err_set) ||
!FD_ISSET(ilp->il_sd, &read_set))
return (NULL);
/*
* The first read may return ECONNREFUSED because the rmcp ping
* packet--sent to UDP port 623--will be processed by both the
* BMC and the OS.
*
* The problem with this is that the ECONNREFUSED takes
* priority over any other received datagram; that means that
* the Connection Refused shows up _before_ the response packet,
* regardless of the order they were sent out. (unless the
* response is read before the connection refused is returned)
*/
ret = recv(ilp->il_sd, &rsp.ir_data, IPMI_BUF_SIZE, 0);
if (ret < 0) {
FD_ZERO(&read_set);
FD_SET(ilp->il_sd, &read_set);
FD_ZERO(&err_set);
FD_SET(ilp->il_sd, &err_set);
tmout.tv_sec = ilp->il_timeout;
tmout.tv_usec = 0;
ret = select(ilp->il_sd + 1, &read_set, NULL, &err_set, &tmout);
if (ret < 0) {
if (FD_ISSET(ilp->il_sd, &err_set) ||
!FD_ISSET(ilp->il_sd, &read_set))
return (NULL);
ret = recv(ilp->il_sd, &rsp.ir_data, IPMI_BUF_SIZE, 0);
if (ret < 0)
return (NULL);
}
}
if (ret == 0)
return (NULL);
rsp.ir_data[ret] = '\0';
rsp.ir_dlen = ret;
return (&rsp);
}
/*
* ASF/RMCP Pong Message
*
* See section 13.2.4
*/
struct rmcp_pong {
rmcp_hdr_t rp_rmcp;
asf_hdr_t rp_asf;
uint32_t rp_iana;
uint32_t rp_oem;
uint8_t rp_sup_entities;
uint8_t rp_sup_interact;
uint8_t rp_reserved[6];
};
/*
* parse response RMCP "pong" packet
*
* return -1 if ping response not received
* returns 0 if IPMI is NOT supported
* returns 1 if IPMI is supported
*/
/*ARGSUSED*/
static int
ipmi_handle_pong(ipmi_handle_t *ihp, ipmi_rs_t *rsp)
{
struct rmcp_pong *pong;
if (rsp == NULL)
return (-1);
/*LINTED: E_BAD_PTR_CAST_ALIGN*/
pong = (struct rmcp_pong *)rsp->ir_data;
return ((pong->rp_sup_entities & 0x80) ? 1 : 0);
}
/*
* Build and send RMCP presence ping message
*/
static int
ipmi_lan_ping(ipmi_handle_t *ihp)
{
rmcp_hdr_t rmcp_ping;
asf_hdr_t asf_ping;
uint8_t *data;
int rv, dlen = sizeof (rmcp_ping) + sizeof (asf_ping);
(void) memset(&rmcp_ping, 0, sizeof (rmcp_ping));
rmcp_ping.rh_version = RMCP_VERSION_1;
rmcp_ping.rh_msg_class = RMCP_CLASS_ASF;
rmcp_ping.rh_seq = 0xff;
(void) memset(&asf_ping, 0, sizeof (asf_ping));
asf_ping.ah_iana = htonl(ASF_RMCP_IANA);
asf_ping.ah_msg_type = ASF_TYPE_PING;
if ((data = ipmi_zalloc(ihp, dlen)) == NULL)
return (-1);
(void) memcpy(data, &rmcp_ping, sizeof (rmcp_ping));
(void) memcpy(data + sizeof (rmcp_ping), &asf_ping, sizeof (asf_ping));
rv = ipmi_lan_send_packet(ihp, data, dlen);
ipmi_free(ihp, data);
if (rv < 0)
return (ipmi_set_error(ihp, EIPMI_LAN_PING_FAILED, NULL));
if (ipmi_lan_poll_recv(ihp) == NULL)
return (ipmi_set_error(ihp, EIPMI_LAN_PING_FAILED, NULL));
return (0);
}
static ipmi_rs_t *
ipmi_lan_poll_recv(ipmi_handle_t *ihp)
{
rmcp_hdr_t rmcp_rsp;
ipmi_rs_t *rsp;
ipmi_rq_entry_t *entry;
int off = 0, rv;
ipmi_lan_t *ilp = (ipmi_lan_t *)ihp->ih_tdata;
uint8_t rsp_authtype;
rsp = ipmi_lan_recv_packet(ihp);
while (rsp != NULL) {
/* parse response headers */
(void) memcpy(&rmcp_rsp, rsp->ir_data, 4);
switch (rmcp_rsp.rh_msg_class) {
case RMCP_CLASS_ASF:
/* ping response packet */
rv = ipmi_handle_pong(ihp, rsp);
return ((rv <= 0) ? NULL : rsp);
case RMCP_CLASS_IPMI:
/* handled by rest of function */
break;
default:
/* Invalid RMCP class */
rsp = ipmi_lan_recv_packet(ihp);
continue;
}
off = sizeof (rmcp_hdr_t);
rsp_authtype = rsp->ir_data[off];
if (ilp->il_send_authcode && (rsp_authtype || ilp->il_authtype))
off += 26;
else
off += 10;
(void) memcpy(&rsp->ir_ihdr, (void *)(rsp->ir_data + off),
sizeof (rsp->ir_ihdr));
rsp->ir_ihdr.imh_seq = rsp->ir_ihdr.imh_seq >> 2;
off += sizeof (rsp->ir_ihdr);
rsp->ir_ccode = rsp->ir_data[off++];
entry = ipmi_req_lookup_entry(ihp, rsp->ir_ihdr.imh_seq,
rsp->ir_ihdr.imh_cmd);
if (entry) {
ipmi_req_remove_entry(ihp, rsp->ir_ihdr.imh_seq,
rsp->ir_ihdr.imh_cmd);
} else {
rsp = ipmi_lan_recv_packet(ihp);
continue;
}
break;
}
/* shift response data to start of array */
if (rsp && rsp->ir_dlen > off) {
rsp->ir_dlen -= off + 1;
(void) memmove(rsp->ir_data, rsp->ir_data + off, rsp->ir_dlen);
(void) memset(rsp->ir_data + rsp->ir_dlen, 0,
IPMI_BUF_SIZE - rsp->ir_dlen);
}
return (rsp);
}
/*
* IPMI LAN Request Message Format
*
* See section 13.8
*
* +---------------------+
* | rmcp_hdr_t | 4 bytes
* +---------------------+
* | v15_session_hdr_t | 9 bytes
* +---------------------+
* | [authcode] | 16 bytes (if AUTHTYPE != none)
* +---------------------+
* | msg length | 1 byte
* +---------------------+
* | ipmi_msg_hdr_t | 6 bytes
* +---------------------+
* | [msg data] | variable
* +---------------------+
* | msg data checksum | 1 byte
* +---------------------+
*/
static ipmi_rq_entry_t *
ipmi_lan_build_cmd(ipmi_handle_t *ihp, ipmi_cmd_t *req)
{
ipmi_lan_t *ilp = (ipmi_lan_t *)ihp->ih_tdata;
rmcp_hdr_t rmcp_hdr;
v15_session_hdr_t session_hdr;
ipmi_msg_hdr_t msg_hdr;
uint8_t *msg;
int cs, tmp, off = 0, len;
ipmi_rq_entry_t *entry;
static int curr_seq = 0;
if (curr_seq >= 64)
curr_seq = 0;
if ((entry = ipmi_req_add_entry(ihp, req)) == NULL)
return (NULL);
len = req->ic_dlen + 29;
if (ilp->il_send_authcode && ilp->il_authtype)
len += 16;
if ((msg = ipmi_zalloc(ihp, len)) == NULL)
/* ipmi_errno set */
return (NULL);
/* RMCP header */
(void) memset(&rmcp_hdr, 0, sizeof (rmcp_hdr));
rmcp_hdr.rh_version = RMCP_VERSION_1;
rmcp_hdr.rh_msg_class = RMCP_CLASS_IPMI;
rmcp_hdr.rh_seq = 0xff;
(void) memcpy(msg, &rmcp_hdr, sizeof (rmcp_hdr));
off = sizeof (rmcp_hdr);
/* IPMI session header */
(void) memset(&session_hdr, 0, sizeof (session_hdr));
if (! ilp->il_send_authcode)
session_hdr.sh_authtype = 0x00;
else
/* hardcode passwd authentication */
session_hdr.sh_authtype = 0x04;
(void) memcpy(&session_hdr.sh_seq, &ilp->il_in_seq, sizeof (uint32_t));
(void) memcpy(&session_hdr.sh_id, &ilp->il_session_id,
sizeof (uint32_t));
(void) memcpy(msg + off, &session_hdr, sizeof (session_hdr));
off += sizeof (session_hdr);
/* IPMI session authcode */
if (ilp->il_send_authcode && ilp->il_authtype) {
(void) memcpy(msg + off, ilp->il_authcode, 16);
off += 16;
}
/* message length */
msg[off++] = req->ic_dlen + 7;
cs = off;
/* IPMI message header */
(void) memset(&msg_hdr, 0, sizeof (msg_hdr));
msg_hdr.imh_addr1 = IPMI_BMC_SLAVE_ADDR;
msg_hdr.imh_lun = req->ic_lun;
msg_hdr.imh_netfn = req->ic_netfn;
tmp = off - cs;
msg_hdr.imh_csum = ipmi_csum(msg + cs, tmp);
cs = off;
msg_hdr.imh_addr2 = IPMI_BMC_SLAVE_ADDR;
entry->ire_rq_seq = curr_seq++;
msg_hdr.imh_seq = entry->ire_rq_seq << 2;
msg_hdr.imh_cmd = req->ic_cmd;
(void) memcpy(msg + off, &msg_hdr, sizeof (msg_hdr));
off += sizeof (msg_hdr);
/* message data */
if (req->ic_dlen != 0) {
(void) memcpy(msg + off, req->ic_data, req->ic_dlen);
off += req->ic_dlen;
}
/* message data checksum */
tmp = off - cs;
msg[off++] = ipmi_csum(msg + cs, tmp);
if (ilp->il_in_seq) {
ilp->il_in_seq++;
if (ilp->il_in_seq == 0)
ilp->il_in_seq++;
}
entry->ire_msg_len = off;
entry->ire_msg_data = msg;
return (entry);
}
static int
ipmi_lan_send(void *data, ipmi_cmd_t *cmd, ipmi_cmd_t *response,
int *completion)
{
ipmi_lan_t *ilp = (ipmi_lan_t *)data;
ipmi_rq_entry_t *entry = NULL;
ipmi_rs_t *rsp = NULL;
uint_t try = 0;
for (;;) {
if ((entry = ipmi_lan_build_cmd(ilp->il_ihp, cmd)) == NULL)
return (-1);
if (ipmi_lan_send_packet(ilp->il_ihp, entry->ire_msg_data,
entry->ire_msg_len) < 0) {
if (++try >= ilp->il_num_retries)
return (-1);
(void) usleep(5000);
continue;
}
(void) usleep(100);
if ((rsp = ipmi_lan_poll_recv(ilp->il_ihp)) != NULL)
break;
(void) usleep(5000);
ipmi_req_remove_entry(ilp->il_ihp, entry->ire_rq_seq,
entry->ire_req.ic_cmd);
if (++try >= ilp->il_num_retries)
return (-1);
}
response->ic_netfn = rsp->ir_ihdr.imh_netfn;
response->ic_lun = rsp->ir_ihdr.imh_lun;
response->ic_cmd = rsp->ir_ihdr.imh_cmd;
if (rsp->ir_ccode != 0) {
*completion = rsp->ir_ccode;
response->ic_dlen = 0;
response->ic_data = NULL;
} else {
*completion = 0;
response->ic_dlen = rsp->ir_dlen;
response->ic_data = rsp->ir_data;
}
return (0);
}
/*
* IPMI Get Session Challenge Command
*
* Copies the returned session ID and 16-byte challenge string to the supplied
* buffers
*
* See section 22.16
*/
static int
ipmi_get_session_challenge_cmd(ipmi_handle_t *ihp, uint32_t *session_id,
uint8_t *challenge)
{
ipmi_cmd_t cmd, resp;
ipmi_lan_t *ilp = (ipmi_lan_t *)ihp->ih_tdata;
char msg_data[17];
int ccode;
(void) memset(msg_data, 0, 17);
switch (ilp->il_authtype) {
case IPMI_SESSION_AUTHTYPE_NONE:
msg_data[0] = 0x00;
break;
case IPMI_SESSION_AUTHTYPE_MD2:
msg_data[0] = 0x01;
break;
case IPMI_SESSION_AUTHTYPE_MD5:
msg_data[0] = 0x02;
break;
case IPMI_SESSION_AUTHTYPE_PASSWORD:
msg_data[0] = 0x04;
break;
case IPMI_SESSION_AUTHTYPE_OEM:
msg_data[0] = 0x05;
break;
}
(void) memcpy(msg_data + 1, ilp->il_user, 16);
cmd.ic_netfn = IPMI_NETFN_APP;
cmd.ic_lun = 0;
cmd.ic_cmd = IPMI_CMD_GET_SESSION_CHALLENGE;
cmd.ic_data = msg_data;
cmd.ic_dlen = 17;
if (ipmi_lan_send(ilp, &cmd, &resp, &ccode) != 0 || ccode)
return (ipmi_set_error(ihp, EIPMI_LAN_CHALLENGE, NULL));
(void) memcpy(session_id, resp.ic_data, 4);
(void) memcpy(challenge, (uint8_t *)resp.ic_data + 4, 16);
return (0);
}
/*
* IPMI Activate Session Command
*
* See section 22.17
*/
static int
ipmi_activate_session_cmd(ipmi_handle_t *ihp)
{
ipmi_cmd_t cmd, resp;
ipmi_lan_t *ilp = (ipmi_lan_t *)ihp->ih_tdata;
uint8_t msg_data[22], *resp_data;
int ccode;
cmd.ic_netfn = IPMI_NETFN_APP;
cmd.ic_lun = 0;
cmd.ic_cmd = IPMI_CMD_ACTIVATE_SESSION;
switch (ilp->il_authtype) {
case IPMI_SESSION_AUTHTYPE_NONE:
msg_data[0] = 0x00;
break;
case IPMI_SESSION_AUTHTYPE_MD2:
msg_data[0] = 0x01;
break;
case IPMI_SESSION_AUTHTYPE_MD5:
msg_data[0] = 0x02;
break;
case IPMI_SESSION_AUTHTYPE_PASSWORD:
msg_data[0] = 0x04;
break;
case IPMI_SESSION_AUTHTYPE_OEM:
msg_data[0] = 0x05;
break;
}
msg_data[1] = ilp->il_privlvl;
(void) memcpy(msg_data + 2, ilp->il_challenge, 16);
/* setup initial outbound sequence number */
(void) get_random(msg_data + 18, 4);
cmd.ic_data = msg_data;
cmd.ic_dlen = 22;
ilp->il_send_authcode = B_TRUE;
if (ipmi_lan_send(ilp, &cmd, &resp, &ccode) != 0 || ccode) {
ilp->il_send_authcode = B_FALSE;
return (ipmi_set_error(ihp, EIPMI_LAN_SESSION, NULL));
}
resp_data = (uint8_t *)resp.ic_data;
(void) memcpy(&ilp->il_session_id, resp_data + 1, 4);
ilp->il_in_seq = resp_data[8] << 24 | resp_data[7] << 16 |
resp_data[6] << 8 | resp_data[5];
if (ilp->il_in_seq == 0)
++ilp->il_in_seq;
return (0);
}
/*
* See section 22.18
*
* returns privilege level or -1 on error
*/
static int
ipmi_set_session_privlvl_cmd(ipmi_handle_t *ihp, uint8_t privlvl)
{
ipmi_cmd_t cmd, resp;
int ret = 0, ccode;
if (privlvl > IPMI_SESSION_PRIV_OEM)
return (ipmi_set_error(ihp, EIPMI_BADPARAM, NULL));
cmd.ic_netfn = IPMI_NETFN_APP;
cmd.ic_lun = 0;
cmd.ic_cmd = IPMI_CMD_SET_SESSION_PRIVLVL;
cmd.ic_data = &privlvl;
cmd.ic_dlen = 1;
if (ipmi_lan_send(ihp->ih_tdata, &cmd, &resp, &ccode) != 0)
ret = ipmi_set_error(ihp, EIPMI_LAN_SETPRIV, NULL);
return (ret);
}
/*
* See section 22.19
*/
static int
ipmi_close_session_cmd(ipmi_handle_t *ihp)
{
ipmi_lan_t *ilp = (ipmi_lan_t *)ihp->ih_tdata;
ipmi_cmd_t cmd, resp;
uint8_t msg_data[4];
int ret = 0, ccode;
if (! ilp->il_session_active)
return (-1);
(void) memcpy(&msg_data, &ilp->il_session_id, 4);
cmd.ic_netfn = IPMI_NETFN_APP;
cmd.ic_lun = 0;
cmd.ic_cmd = IPMI_CMD_CLOSE_SESSION;
cmd.ic_data = msg_data;
cmd.ic_dlen = 4;
if (ipmi_lan_send(ilp, &cmd, &resp, &ccode) != 0)
ret = -1;
return (ret);
}
/*
* IPMI LAN Session Activation
*
* See section 13.14
*
* 1. send "RMCP Presence Ping" message, response message will
* indicate whether the platform supports IPMI
* 2. send "Get Channel Authentication Capabilities" command
* with AUTHTYPE = none, response packet will contain information
* about supported challenge/response authentication types
* 3. send "Get Session Challenge" command with AUTHTYPE = none
* and indicate the authentication type in the message, response
* packet will contain challenge string and temporary session ID.
* 4. send "Activate Session" command, authenticated with AUTHTYPE
* sent in previous message. Also sends the initial value for
* the outbound sequence number for BMC.
* 5. BMC returns response confirming session activation and
* session ID for this session and initial inbound sequence.
*/
static int
ipmi_lan_activate_session(ipmi_handle_t *ihp)
{
ipmi_lan_t *ilp = (ipmi_lan_t *)ihp->ih_tdata;
ipmi_channel_auth_caps_t *ac;
if (ipmi_lan_ping(ihp) != 0)
return (-1);
if ((ac = ipmi_get_channel_auth_caps(ihp, IPMI_LAN_CHANNEL_E,
ilp->il_privlvl)) == NULL)
return (-1);
/*
* For the sake of simplicity, we're just supporting basic password
* authentication. If this authentication type is not supported then
* we'll bail here.
*/
if (!(ac->cap_authtype & IPMI_SESSION_AUTHTYPE_PASSWORD)) {
free(ac);
return (ipmi_set_error(ihp, EIPMI_LAN_PASSWD_NOTSUP, NULL));
}
free(ac);
if (ipmi_get_session_challenge_cmd(ihp, &ilp->il_session_id,
ilp->il_challenge) != 0)
return (-1);
if (ipmi_activate_session_cmd(ihp) != 0)
return (-1);
ilp->il_session_active = B_TRUE;
if (ipmi_set_session_privlvl_cmd(ihp, ilp->il_privlvl) != 0)
return (-1);
return (0);
}
static void
ipmi_lan_close(void *data)
{
ipmi_lan_t *ilp = (ipmi_lan_t *)data;
if (ilp->il_session_active)
(void) ipmi_close_session_cmd(ilp->il_ihp);
if (ilp->il_sd >= 0)
(void) close(ilp->il_sd);
ipmi_req_clear_entries(ilp->il_ihp);
ipmi_free(ilp->il_ihp, ipmi_req_entries);
ipmi_free(ilp->il_ihp, ilp);
}
static void *
ipmi_lan_open(ipmi_handle_t *ihp, nvlist_t *params)
{
int rc;
struct hostent *host;
ipmi_lan_t *ilp;
char *hostname, *user, *authcode;
if ((ilp = ipmi_zalloc(ihp, sizeof (ipmi_lan_t))) == NULL) {
/* ipmi errno set */
return (NULL);
}
ilp->il_ihp = ihp;
ihp->ih_tdata = ilp;
/*
* Parse the parameters passed in the params nvlist. The following
* parameters are required
* IPMI_LAN_HOST, IPMI_LAN_USER and IPMI_LAN_PASSWD
*
* If any of these were not specified then we abort
*/
if (nvlist_lookup_string(params, IPMI_LAN_HOST, &hostname) ||
nvlist_lookup_string(params, IPMI_LAN_USER, &user) ||
nvlist_lookup_string(params, IPMI_LAN_PASSWD, &authcode)) {
ipmi_free(ihp, ilp);
(void) ipmi_set_error(ihp, EIPMI_BADPARAM, NULL);
return (NULL);
}
(void) strncpy(ilp->il_host, hostname, MAXHOSTNAMELEN);
(void) strncpy(ilp->il_user, user, 16);
(void) strncpy(ilp->il_authcode, authcode, 16);
/*
* IPMI_LAN_PORT is an optional parameter and defaults to port 623
* IPMI_LAN_PRIVLVL is also optional and defaults to admin
* IPMI_LAN_TIMEOUT is optional and will default to 3 seconds
* IPMI_LAN_NUM_RETIES is optional and will default to 5
*/
if (nvlist_lookup_uint16(params, IPMI_LAN_PORT, &ilp->il_port))
ilp->il_port = RMCP_UDP_PORT;
if (nvlist_lookup_uint8(params, IPMI_LAN_PRIVLVL, &ilp->il_privlvl))
ilp->il_privlvl = IPMI_SESSION_PRIV_ADMIN;
if (nvlist_lookup_uint32(params, IPMI_LAN_TIMEOUT, &ilp->il_timeout))
ilp->il_timeout = DEF_IPMI_LAN_TIMEOUT;
if (nvlist_lookup_uint8(params, IPMI_LAN_NUM_RETRIES,
&ilp->il_num_retries))
ilp->il_num_retries = DEF_IPMI_LAN_NUM_RETRIES;
ilp->il_authtype = IPMI_SESSION_AUTHTYPE_PASSWORD;
/*
* Open up and connect a UDP socket between us and the service
* processor
*/
ilp->il_addr.sin_family = AF_INET;
ilp->il_addr.sin_port = htons(ilp->il_port);
rc = inet_pton(AF_INET, (const char *)ilp->il_host,
&ilp->il_addr.sin_addr);
if (rc <= 0) {
if ((host = gethostbyname((const char *)ilp->il_host))
== NULL) {
ipmi_free(ihp, ilp);
(void) ipmi_set_error(ihp, EIPMI_LAN_OPEN_FAILED, NULL);
return (NULL);
}
ilp->il_addr.sin_family = host->h_addrtype;
(void) memcpy(&ilp->il_addr.sin_addr, host->h_addr,
host->h_length);
}
if ((ilp->il_sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
ipmi_free(ihp, ilp);
(void) ipmi_set_error(ihp, EIPMI_LAN_OPEN_FAILED, NULL);
return (NULL);
}
if (connect(ilp->il_sd, (struct sockaddr *)&ilp->il_addr,
sizeof (struct sockaddr_in)) < 0) {
ipmi_lan_close(ilp);
(void) ipmi_set_error(ihp, EIPMI_LAN_OPEN_FAILED, NULL);
return (NULL);
}
if ((ipmi_req_entries = ipmi_zalloc(ihp, sizeof (ipmi_rq_entry_t)))
== NULL)
return (NULL);
/*
* Finally we start up the IPMI LAN session
*/
if ((rc = ipmi_lan_activate_session(ihp)) < 0) {
ipmi_lan_close(ilp);
return (NULL);
}
return (ilp);
}
ipmi_transport_t ipmi_transport_lan = {
ipmi_lan_open,
ipmi_lan_close,
ipmi_lan_send
};
|