blob: df2986a54ff1985c95761d185522b6cb2a45c5bf [file] [log] [blame]
Adriana Kobylak5d6481f2015-10-29 21:44:55 -05001#include <stdio.h>
2#include <string.h>
3#include <stdint.h>
Hariharasubramanian R83951912016-01-20 07:06:36 -06004#include <arpa/inet.h>
Adriana Kobylak5d6481f2015-10-29 21:44:55 -05005
6#include "ipmid-api.h"
7#include "ipmid.H"
8#include "transporthandler.h"
9
Hariharasubramanian R83951912016-01-20 07:06:36 -060010#define SYSTEMD_NETWORKD_DBUS 1
11
12#ifdef SYSTEMD_NETWORKD_DBUS
13#include <systemd/sd-bus.h>
14#endif
15
16// OpenBMC System Manager dbus framework
17const char *app = "org.openbmc.NetworkManager";
18const char *obj = "/org/openbmc/NetworkManager/Interface";
19const char *ifc = "org.openbmc.NetworkManager";
20
Adriana Kobylake08fbc62016-02-09 16:17:23 -060021const int SIZE_MAC = 18; //xx:xx:xx:xx:xx:xx
22const int SIZE_LAN_PARM = 16; //xxx.xxx.xxx.xxx
23
Hariharasubramanian R83951912016-01-20 07:06:36 -060024char cur_ipaddr [16] = "";
25char cur_netmask [16] = "";
26char cur_gateway [16] = "";
27
28char new_ipaddr [16] = "";
29char new_netmask [16] = "";
30char new_gateway [16] = "";
31
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050032void register_netfn_transport_functions() __attribute__((constructor));
33
34ipmi_ret_t ipmi_transport_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
35 ipmi_request_t request, ipmi_response_t response,
36 ipmi_data_len_t data_len, ipmi_context_t context)
37{
38 printf("Handling TRANSPORT WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
39 // Status code.
40 ipmi_ret_t rc = IPMI_CC_OK;
41 *data_len = 0;
42 return rc;
43}
44
45struct set_lan_t {
46 uint8_t channel;
47 uint8_t parameter;
48 uint8_t data[8]; // Per IPMI spec, not expecting more than this size
49} __attribute__ ((packed));
50
51ipmi_ret_t ipmi_transport_set_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
52 ipmi_request_t request, ipmi_response_t response,
53 ipmi_data_len_t data_len, ipmi_context_t context)
54{
55 ipmi_ret_t rc = IPMI_CC_OK;
56 *data_len = 0;
Adriana Kobylake08fbc62016-02-09 16:17:23 -060057 sd_bus *bus = ipmid_get_sd_bus_connection();
58 sd_bus_message *reply = NULL, *m = NULL;
59 sd_bus_error error = SD_BUS_ERROR_NULL;
60 int r = 0;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050061
62 printf("IPMI SET_LAN\n");
63
64 set_lan_t *reqptr = (set_lan_t*) request;
65
66 // TODO Use dbus interface once available. For now use cmd line.
67 // TODO Add the rest of the parameters like setting auth type
68 // TODO Add error handling
69
Adriana Kobylake08fbc62016-02-09 16:17:23 -060070 if (reqptr->parameter == LAN_PARM_IP)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050071 {
Adriana Kobylake08fbc62016-02-09 16:17:23 -060072 snprintf(new_ipaddr, SIZE_LAN_PARM, "%d.%d.%d.%d",
73 reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050074 }
Adriana Kobylake08fbc62016-02-09 16:17:23 -060075 else if (reqptr->parameter == LAN_PARM_MAC)
Adriana Kobylakd54a9dd2016-02-03 17:25:54 -060076 {
Adriana Kobylake08fbc62016-02-09 16:17:23 -060077 char mac[SIZE_MAC];
Adriana Kobylakd54a9dd2016-02-03 17:25:54 -060078
Adriana Kobylake08fbc62016-02-09 16:17:23 -060079 snprintf(mac, SIZE_MAC, "%02x:%02x:%02x:%02x:%02x:%02x",
Adriana Kobylakd54a9dd2016-02-03 17:25:54 -060080 reqptr->data[0],
81 reqptr->data[1],
82 reqptr->data[2],
83 reqptr->data[3],
84 reqptr->data[4],
85 reqptr->data[5]);
86
87 r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"SetHwAddress");
88 if (r < 0) {
89 fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
90 return -1;
91 }
92 r = sd_bus_message_append(m, "s", mac);
93 if (r < 0) {
94 fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
95 return -1;
96 }
97 r = sd_bus_call(bus, m, 0, &error, &reply);
98 if (r < 0) {
99 fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
100 return -1;
101 }
102 }
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600103 else if (reqptr->parameter == LAN_PARM_SUBNET)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500104 {
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600105 snprintf(new_netmask, SIZE_LAN_PARM, "%d.%d.%d.%d",
106 reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500107 }
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600108 else if (reqptr->parameter == LAN_PARM_GATEWAY)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500109 {
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600110 snprintf(new_gateway, SIZE_LAN_PARM, "%d.%d.%d.%d",
111 reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Hariharasubramanian R83951912016-01-20 07:06:36 -0600112 }
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600113 else if (reqptr->parameter == LAN_PARM_INPROGRESS) // Apply config
Hariharasubramanian R83951912016-01-20 07:06:36 -0600114 {
115 int rc = 0;
116 sd_bus_message *req = NULL;
117 sd_bus_message *res = NULL;
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600118 sd_bus *bus1 = NULL;
Hariharasubramanian R83951912016-01-20 07:06:36 -0600119 sd_bus_error err = SD_BUS_ERROR_NULL;
120
121 if (!strcmp(new_ipaddr, "") || !strcmp (new_netmask, "") || !strcmp (new_gateway, ""))
122 {
123 fprintf(stderr,"ERROR: Incomplete LAN Parameters\n");
124 return -1;
125 }
126
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600127 rc = sd_bus_open_system(&bus1);
Hariharasubramanian R83951912016-01-20 07:06:36 -0600128 if(rc < 0)
129 {
130 fprintf(stderr,"ERROR: Getting a SYSTEM bus hook\n");
131 return -1;
132 }
133
134 if (strcmp(cur_ipaddr, ""))
135 {
136 sd_bus_error_free(&err);
137 sd_bus_message_unref(req);
138 sd_bus_message_unref(res);
139
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600140 rc = sd_bus_call_method(bus1, // On the System Bus
Hariharasubramanian R83951912016-01-20 07:06:36 -0600141 app, // Service to contact
142 obj, // Object path
143 ifc, // Interface name
144 "DelAddress4", // Method to be called
145 &err, // object to return error
146 &res, // Response message on success
147 "ssss", // input message (dev,ip,nm,gw)
148 "eth0",
149 cur_ipaddr,
150 cur_netmask,
151 cur_gateway);
152 }
153
154 if(rc < 0)
155 {
156 fprintf(stderr, "Failed to remove existing IP %s: %s\n", cur_ipaddr, err.message);
157 return -1;
158 }
159
160 sd_bus_error_free(&err);
161 sd_bus_message_unref(req);
162 sd_bus_message_unref(res);
163
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600164 rc = sd_bus_call_method(bus1, // On the System Bus
Hariharasubramanian R83951912016-01-20 07:06:36 -0600165 app, // Service to contact
166 obj, // Object path
167 ifc, // Interface name
168 "AddAddress4", // Method to be called
169 &err, // object to return error
170 &res, // Response message on success
171 "ssss", // input message (dev,ip,nm,gw)
172 "eth0",
173 new_ipaddr,
174 new_netmask,
175 new_gateway);
176 if(rc < 0)
177 {
178 fprintf(stderr, "Failed to set IP %s: %s\n", new_ipaddr, err.message);
179 return -1;
180 }
181
182 strcpy (cur_ipaddr, new_ipaddr);
183 strcpy (cur_netmask, new_netmask);
184 strcpy (cur_gateway, new_gateway);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500185 }
186 else
187 {
188 fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
189 return IPMI_CC_PARM_NOT_SUPPORTED;
190 }
191
192 return rc;
193}
194
195struct get_lan_t {
196 uint8_t rev_channel;
197 uint8_t parameter;
198 uint8_t parameter_set;
199 uint8_t parameter_block;
200} __attribute__ ((packed));
201
202ipmi_ret_t ipmi_transport_get_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
203 ipmi_request_t request, ipmi_response_t response,
204 ipmi_data_len_t data_len, ipmi_context_t context)
205{
206 ipmi_ret_t rc = IPMI_CC_OK;
207 *data_len = 0;
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600208 sd_bus *bus = ipmid_get_sd_bus_connection();
Adriana Kobylak9355f612016-02-08 17:30:37 -0600209 sd_bus_message *reply = NULL, *m = NULL;
210 sd_bus_error error = SD_BUS_ERROR_NULL;
211 int r = 0;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500212 const uint8_t current_revision = 0x11; // Current rev per IPMI Spec 2.0
Hariharasubramanian R83951912016-01-20 07:06:36 -0600213
214 int family;
215 unsigned char prefixlen;
216 unsigned char scope;
217 unsigned int flags;
218 char saddr [128];
219 char gateway [128];
220 uint8_t buf[11];
Adriana Kobylak9355f612016-02-08 17:30:37 -0600221 int i = 0;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500222
223 printf("IPMI GET_LAN\n");
224
225 get_lan_t *reqptr = (get_lan_t*) request;
226
227 if (reqptr->rev_channel & 0x80) // Revision is bit 7
228 {
229 // Only current revision was requested
230 *data_len = sizeof(current_revision);
231 memcpy(response, &current_revision, *data_len);
232 return IPMI_CC_OK;
233 }
234
235 // TODO Use dbus interface once available. For now use ip cmd.
236 // TODO Add the rest of the parameters, like gateway
237
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600238 if (reqptr->parameter == LAN_PARM_INPROGRESS)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500239 {
240 uint8_t buf[] = {current_revision,0};
241 *data_len = sizeof(buf);
242 memcpy(response, &buf, *data_len);
243 return IPMI_CC_OK;
244 }
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600245 else if (reqptr->parameter == LAN_PARM_AUTHSUPPORT)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500246 {
247 uint8_t buf[] = {current_revision,0x04};
248 *data_len = sizeof(buf);
249 memcpy(response, &buf, *data_len);
250 return IPMI_CC_OK;
251 }
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600252 else if (reqptr->parameter == LAN_PARM_AUTHENABLES)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500253 {
254 uint8_t buf[] = {current_revision,0x04,0x04,0x04,0x04,0x04};
255 *data_len = sizeof(buf);
256 memcpy(response, &buf, *data_len);
257 return IPMI_CC_OK;
258 }
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600259 else if (reqptr->parameter == LAN_PARM_IP)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500260 {
Hariharasubramanian R83951912016-01-20 07:06:36 -0600261 const char* device = "eth0";
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500262
Hariharasubramanian R83951912016-01-20 07:06:36 -0600263 sd_bus_message *res = NULL;
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600264 sd_bus *bus1 = NULL;
Hariharasubramanian R83951912016-01-20 07:06:36 -0600265 sd_bus_error err = SD_BUS_ERROR_NULL;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500266
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600267 rc = sd_bus_open_system(&bus1);
Hariharasubramanian R83951912016-01-20 07:06:36 -0600268 if(rc < 0)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500269 {
Hariharasubramanian R83951912016-01-20 07:06:36 -0600270 fprintf(stderr,"ERROR: Getting a SYSTEM bus hook\n");
271 return -1;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500272 }
273
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600274 rc = sd_bus_call_method(bus1, // On the System Bus
Hariharasubramanian R83951912016-01-20 07:06:36 -0600275 app, // Service to contact
276 obj, // Object path
277 ifc, // Interface name
278 "GetAddress4", // Method to be called
279 &err, // object to return error
280 &res, // Response message on success
281 "s", // input message (dev,ip,nm,gw)
282 "eth0");
283 if(rc < 0)
284 {
285 fprintf(stderr, "Failed to Get IP on interface : %s\n", device);
286 return -1;
287 }
288
289 /* rc = sd_bus_message_read(res, "a(iyyus)s", ...); */
290 rc = sd_bus_message_enter_container (res, 'a', "(iyyus)");
291 if(rc < 0)
292 {
293 fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
294 return -1;
295 }
296
297 while ((rc = sd_bus_message_read(res, "(iyyus)", &family, &prefixlen, &scope, &flags, &saddr)) > 0) {
298 printf("%s:%d:%d:%d:%s\n", family==AF_INET?"IPv4":"IPv6", prefixlen, scope, flags, saddr);
299 }
300
301 rc = sd_bus_message_read (res, "s", &gateway);
302 if(rc < 0)
303 {
304 fprintf(stderr, "Failed to parse gateway from response message:[%s]\n", strerror(-rc));
305 return -1;
306 }
307
308 memcpy((void*)&buf[0], &current_revision, 1);
309 sscanf (saddr, "%c.%c.%c.%c", &buf[1], &buf[2], &buf[3], &buf[4]);
310 buf[5] = family;
311 buf[6] = prefixlen;
312 sscanf (gateway, "%c.%c.%c.%c", &buf[7], &buf[8], &buf[9], &buf[10]);
313
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500314 *data_len = sizeof(buf);
315 memcpy(response, &buf, *data_len);
Hariharasubramanian R83951912016-01-20 07:06:36 -0600316
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500317 return IPMI_CC_OK;
318 }
Adriana Kobylake08fbc62016-02-09 16:17:23 -0600319 else if (reqptr->parameter == LAN_PARM_MAC)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500320 {
321 //string to parse: link/ether xx:xx:xx:xx:xx:xx
322
Hariharasubramanian R83951912016-01-20 07:06:36 -0600323 const char* device = "eth0";
Hariharasubramanian R83951912016-01-20 07:06:36 -0600324 uint8_t buf[7];
Adriana Kobylak9355f612016-02-08 17:30:37 -0600325 char *eaddr1 = NULL;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500326
Adriana Kobylak9355f612016-02-08 17:30:37 -0600327 r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"GetHwAddress");
328 if (r < 0) {
329 fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
Hariharasubramanian R83951912016-01-20 07:06:36 -0600330 return -1;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500331 }
Adriana Kobylak9355f612016-02-08 17:30:37 -0600332 r = sd_bus_message_append(m, "s", device);
333 if (r < 0) {
334 fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
Hariharasubramanian R83951912016-01-20 07:06:36 -0600335 return -1;
336 }
Adriana Kobylak9355f612016-02-08 17:30:37 -0600337 r = sd_bus_call(bus, m, 0, &error, &reply);
338 if (r < 0) {
339 fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
Hariharasubramanian R83951912016-01-20 07:06:36 -0600340 return -1;
341 }
Adriana Kobylak9355f612016-02-08 17:30:37 -0600342 r = sd_bus_message_read(reply, "s", &eaddr1);
343 if (r < 0) {
344 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
345 return IPMI_CC_RESPONSE_ERROR;
346 }
347 if (eaddr1 == NULL)
348 {
349 fprintf(stderr, "Failed to get a valid response: %s", strerror(-r));
350 return IPMI_CC_RESPONSE_ERROR;
351 }
Hariharasubramanian R83951912016-01-20 07:06:36 -0600352
353 memcpy((void*)&buf[0], &current_revision, 1);
Adriana Kobylak9355f612016-02-08 17:30:37 -0600354
355 char *tokptr = NULL;
356 char* digit = strtok_r(eaddr1, ":", &tokptr);
357 if (digit == NULL)
358 {
359 fprintf(stderr, "Unexpected MAC format: %s", eaddr1);
360 return IPMI_CC_RESPONSE_ERROR;
361 }
362
363 i=0;
364 while (digit != NULL)
365 {
366 int resp_byte = strtoul(digit, NULL, 16);
367 memcpy((void*)&buf[i+1], &resp_byte, 1);
368 i++;
369 digit = strtok_r(NULL, ":", &tokptr);
370 }
Hariharasubramanian R83951912016-01-20 07:06:36 -0600371
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500372 *data_len = sizeof(buf);
373 memcpy(response, &buf, *data_len);
Hariharasubramanian R83951912016-01-20 07:06:36 -0600374
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500375 return IPMI_CC_OK;
376 }
377 else
378 {
379 fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
380 return IPMI_CC_PARM_NOT_SUPPORTED;
381 }
382
383 return rc;
384}
385
386void register_netfn_transport_functions()
387{
388 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_WILDCARD);
389 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_WILDCARD, NULL, ipmi_transport_wildcard);
390
391 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_SET_LAN);
392 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_SET_LAN, NULL, ipmi_transport_set_lan);
393
394 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_GET_LAN);
395 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_GET_LAN, NULL, ipmi_transport_get_lan);
396
397 return;
398}