forked from openbmc/phosphor-host-ipmid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransporthandler.C
395 lines (343 loc) · 12.9 KB
/
transporthandler.C
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
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h>
#include "ipmid-api.h"
#include "ipmid.H"
#include "transporthandler.h"
#define SYSTEMD_NETWORKD_DBUS 1
#ifdef SYSTEMD_NETWORKD_DBUS
#include <systemd/sd-bus.h>
#endif
// OpenBMC System Manager dbus framework
const char *app = "org.openbmc.NetworkManager";
const char *obj = "/org/openbmc/NetworkManager/Interface";
const char *ifc = "org.openbmc.NetworkManager";
const int SIZE_MAC = 18; //xx:xx:xx:xx:xx:xx
const int SIZE_LAN_PARM = 16; //xxx.xxx.xxx.xxx
char cur_ipaddr [16] = "";
char cur_netmask [16] = "";
char cur_gateway [16] = "";
char new_ipaddr [16] = "";
char new_netmask [16] = "";
char new_gateway [16] = "";
void register_netfn_transport_functions() __attribute__((constructor));
ipmi_ret_t ipmi_transport_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
ipmi_request_t request, ipmi_response_t response,
ipmi_data_len_t data_len, ipmi_context_t context)
{
printf("Handling TRANSPORT WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
// Status code.
ipmi_ret_t rc = IPMI_CC_OK;
*data_len = 0;
return rc;
}
struct set_lan_t {
uint8_t channel;
uint8_t parameter;
uint8_t data[8]; // Per IPMI spec, not expecting more than this size
} __attribute__ ((packed));
ipmi_ret_t ipmi_transport_set_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
ipmi_request_t request, ipmi_response_t response,
ipmi_data_len_t data_len, ipmi_context_t context)
{
ipmi_ret_t rc = IPMI_CC_OK;
*data_len = 0;
sd_bus *bus = ipmid_get_sd_bus_connection();
sd_bus_message *reply = NULL, *m = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
int r = 0;
printf("IPMI SET_LAN\n");
set_lan_t *reqptr = (set_lan_t*) request;
// TODO Use dbus interface once available. For now use cmd line.
// TODO Add the rest of the parameters like setting auth type
// TODO Add error handling
if (reqptr->parameter == LAN_PARM_IP)
{
snprintf(new_ipaddr, SIZE_LAN_PARM, "%d.%d.%d.%d",
reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
}
else if (reqptr->parameter == LAN_PARM_MAC)
{
char mac[SIZE_MAC];
snprintf(mac, SIZE_MAC, "%02x:%02x:%02x:%02x:%02x:%02x",
reqptr->data[0],
reqptr->data[1],
reqptr->data[2],
reqptr->data[3],
reqptr->data[4],
reqptr->data[5]);
r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"SetHwAddress");
if (r < 0) {
fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
return -1;
}
r = sd_bus_message_append(m, "s", mac);
if (r < 0) {
fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
return -1;
}
r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0) {
fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
return -1;
}
}
else if (reqptr->parameter == LAN_PARM_SUBNET)
{
snprintf(new_netmask, SIZE_LAN_PARM, "%d.%d.%d.%d",
reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
}
else if (reqptr->parameter == LAN_PARM_GATEWAY)
{
snprintf(new_gateway, SIZE_LAN_PARM, "%d.%d.%d.%d",
reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
}
else if (reqptr->parameter == LAN_PARM_INPROGRESS) // Apply config
{
int rc = 0;
sd_bus_message *req = NULL;
sd_bus_message *res = NULL;
sd_bus *bus1 = NULL;
sd_bus_error err = SD_BUS_ERROR_NULL;
if (!strcmp(new_ipaddr, "") || !strcmp (new_netmask, "") || !strcmp (new_gateway, ""))
{
fprintf(stderr,"ERROR: Incomplete LAN Parameters\n");
return -1;
}
rc = sd_bus_open_system(&bus1);
if(rc < 0)
{
fprintf(stderr,"ERROR: Getting a SYSTEM bus hook\n");
return -1;
}
if (strcmp(cur_ipaddr, ""))
{
sd_bus_error_free(&err);
sd_bus_message_unref(req);
sd_bus_message_unref(res);
rc = sd_bus_call_method(bus1, // On the System Bus
app, // Service to contact
obj, // Object path
ifc, // Interface name
"DelAddress4", // Method to be called
&err, // object to return error
&res, // Response message on success
"ssss", // input message (dev,ip,nm,gw)
"eth0",
cur_ipaddr,
cur_netmask,
cur_gateway);
}
if(rc < 0)
{
fprintf(stderr, "Failed to remove existing IP %s: %s\n", cur_ipaddr, err.message);
return -1;
}
sd_bus_error_free(&err);
sd_bus_message_unref(req);
sd_bus_message_unref(res);
rc = sd_bus_call_method(bus1, // On the System Bus
app, // Service to contact
obj, // Object path
ifc, // Interface name
"AddAddress4", // Method to be called
&err, // object to return error
&res, // Response message on success
"ssss", // input message (dev,ip,nm,gw)
"eth0",
new_ipaddr,
new_netmask,
new_gateway);
if(rc < 0)
{
fprintf(stderr, "Failed to set IP %s: %s\n", new_ipaddr, err.message);
return -1;
}
strcpy (cur_ipaddr, new_ipaddr);
strcpy (cur_netmask, new_netmask);
strcpy (cur_gateway, new_gateway);
}
else
{
fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
return IPMI_CC_PARM_NOT_SUPPORTED;
}
return rc;
}
struct get_lan_t {
uint8_t rev_channel;
uint8_t parameter;
uint8_t parameter_set;
uint8_t parameter_block;
} __attribute__ ((packed));
ipmi_ret_t ipmi_transport_get_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
ipmi_request_t request, ipmi_response_t response,
ipmi_data_len_t data_len, ipmi_context_t context)
{
ipmi_ret_t rc = IPMI_CC_OK;
*data_len = 0;
sd_bus *bus = ipmid_get_sd_bus_connection();
sd_bus_message *reply = NULL, *m = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
int r = 0;
const uint8_t current_revision = 0x11; // Current rev per IPMI Spec 2.0
int family;
unsigned char prefixlen;
unsigned char scope;
unsigned int flags;
char *saddr = NULL;
int i = 0;
printf("IPMI GET_LAN\n");
get_lan_t *reqptr = (get_lan_t*) request;
if (reqptr->rev_channel & 0x80) // Revision is bit 7
{
// Only current revision was requested
*data_len = sizeof(current_revision);
memcpy(response, ¤t_revision, *data_len);
return IPMI_CC_OK;
}
// TODO Use dbus interface once available. For now use ip cmd.
// TODO Add the rest of the parameters, like gateway
if (reqptr->parameter == LAN_PARM_INPROGRESS)
{
uint8_t buf[] = {current_revision,0};
*data_len = sizeof(buf);
memcpy(response, &buf, *data_len);
return IPMI_CC_OK;
}
else if (reqptr->parameter == LAN_PARM_AUTHSUPPORT)
{
uint8_t buf[] = {current_revision,0x04};
*data_len = sizeof(buf);
memcpy(response, &buf, *data_len);
return IPMI_CC_OK;
}
else if (reqptr->parameter == LAN_PARM_AUTHENABLES)
{
uint8_t buf[] = {current_revision,0x04,0x04,0x04,0x04,0x04};
*data_len = sizeof(buf);
memcpy(response, &buf, *data_len);
return IPMI_CC_OK;
}
else if (reqptr->parameter == LAN_PARM_IP)
{
const char* device = "eth0";
uint8_t buf[5]; // Size of expected IPMI response msg
r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"GetAddress4");
if (r < 0) {
fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
return -1;
}
r = sd_bus_message_append(m, "s", device);
if (r < 0) {
fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
return -1;
}
r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0) {
fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
return -1;
}
rc = sd_bus_message_enter_container (reply, 'a', "(iyyus)");
if(rc < 0)
{
fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
return -1;
}
rc = sd_bus_message_read(reply, "(iyyus)", &family, &prefixlen, &scope, &flags, &saddr);
if (rc < 0)
{
fprintf(stderr, "Failed to receive response: %s\n", strerror(-r));
return -1;
}
printf("%s:%d:%d:%d:%s\n", family==AF_INET?"IPv4":"IPv6", prefixlen, scope, flags, saddr);
memcpy((void*)&buf[0], ¤t_revision, 1);
// Parse IP address
char *tokptr = NULL;
char* digit = strtok_r(saddr, ".", &tokptr);
if (digit == NULL)
{
fprintf(stderr, "Unexpected IP format: %s", saddr);
return IPMI_CC_RESPONSE_ERROR;
}
i = 0;
while (digit != NULL)
{
int resp_byte = strtoul(digit, NULL, 10);
memcpy((void*)&buf[i+1], &resp_byte, 1);
i++;
digit = strtok_r(NULL, ".", &tokptr);
}
*data_len = sizeof(buf);
memcpy(response, &buf, *data_len);
return IPMI_CC_OK;
}
else if (reqptr->parameter == LAN_PARM_MAC)
{
//string to parse: link/ether xx:xx:xx:xx:xx:xx
const char* device = "eth0";
uint8_t buf[7];
char *eaddr1 = NULL;
r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"GetHwAddress");
if (r < 0) {
fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
return -1;
}
r = sd_bus_message_append(m, "s", device);
if (r < 0) {
fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
return -1;
}
r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0) {
fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
return -1;
}
r = sd_bus_message_read(reply, "s", &eaddr1);
if (r < 0) {
fprintf(stderr, "Failed to get a response: %s", strerror(-r));
return IPMI_CC_RESPONSE_ERROR;
}
if (eaddr1 == NULL)
{
fprintf(stderr, "Failed to get a valid response: %s", strerror(-r));
return IPMI_CC_RESPONSE_ERROR;
}
memcpy((void*)&buf[0], ¤t_revision, 1);
char *tokptr = NULL;
char* digit = strtok_r(eaddr1, ":", &tokptr);
if (digit == NULL)
{
fprintf(stderr, "Unexpected MAC format: %s", eaddr1);
return IPMI_CC_RESPONSE_ERROR;
}
i=0;
while (digit != NULL)
{
int resp_byte = strtoul(digit, NULL, 16);
memcpy((void*)&buf[i+1], &resp_byte, 1);
i++;
digit = strtok_r(NULL, ":", &tokptr);
}
*data_len = sizeof(buf);
memcpy(response, &buf, *data_len);
return IPMI_CC_OK;
}
else
{
fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
return IPMI_CC_PARM_NOT_SUPPORTED;
}
return rc;
}
void register_netfn_transport_functions()
{
printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_WILDCARD);
ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_WILDCARD, NULL, ipmi_transport_wildcard);
printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_SET_LAN);
ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_SET_LAN, NULL, ipmi_transport_set_lan);
printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_GET_LAN);
ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_GET_LAN, NULL, ipmi_transport_get_lan);
return;
}