blob: c38adea5e05700cc8b00bdd3fe0d3995622985d2 [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
Adriana Kobylakd54a9dd2016-02-03 17:25:54 -060017extern sd_bus *bus;
Hariharasubramanian R83951912016-01-20 07:06:36 -060018const char *app = "org.openbmc.NetworkManager";
19const char *obj = "/org/openbmc/NetworkManager/Interface";
20const char *ifc = "org.openbmc.NetworkManager";
21
22char cur_ipaddr [16] = "";
23char cur_netmask [16] = "";
24char cur_gateway [16] = "";
25
26char new_ipaddr [16] = "";
27char new_netmask [16] = "";
28char new_gateway [16] = "";
29
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050030void register_netfn_transport_functions() __attribute__((constructor));
31
32ipmi_ret_t ipmi_transport_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
33 ipmi_request_t request, ipmi_response_t response,
34 ipmi_data_len_t data_len, ipmi_context_t context)
35{
36 printf("Handling TRANSPORT WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
37 // Status code.
38 ipmi_ret_t rc = IPMI_CC_OK;
39 *data_len = 0;
40 return rc;
41}
42
43struct set_lan_t {
44 uint8_t channel;
45 uint8_t parameter;
46 uint8_t data[8]; // Per IPMI spec, not expecting more than this size
47} __attribute__ ((packed));
48
49ipmi_ret_t ipmi_transport_set_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
50 ipmi_request_t request, ipmi_response_t response,
51 ipmi_data_len_t data_len, ipmi_context_t context)
52{
53 ipmi_ret_t rc = IPMI_CC_OK;
54 *data_len = 0;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050055
56 printf("IPMI SET_LAN\n");
57
58 set_lan_t *reqptr = (set_lan_t*) request;
59
60 // TODO Use dbus interface once available. For now use cmd line.
61 // TODO Add the rest of the parameters like setting auth type
62 // TODO Add error handling
63
64 if (reqptr->parameter == 3) // IP
65 {
Hariharasubramanian R83951912016-01-20 07:06:36 -060066 sprintf(new_ipaddr, "%d.%d.%d.%d", reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050067 }
Adriana Kobylakd54a9dd2016-02-03 17:25:54 -060068 else if (reqptr->parameter == 5) // MAC
69 {
70 char mac[18];
71 sd_bus_message *reply = NULL, *m = NULL;
72 sd_bus_error error = SD_BUS_ERROR_NULL;
73 int r = 0;
74
75 sprintf(mac, "%x:%x:%x:%x:%x:%x",
76 reqptr->data[0],
77 reqptr->data[1],
78 reqptr->data[2],
79 reqptr->data[3],
80 reqptr->data[4],
81 reqptr->data[5]);
82
83 r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"SetHwAddress");
84 if (r < 0) {
85 fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
86 return -1;
87 }
88 r = sd_bus_message_append(m, "s", mac);
89 if (r < 0) {
90 fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
91 return -1;
92 }
93 r = sd_bus_call(bus, m, 0, &error, &reply);
94 if (r < 0) {
95 fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
96 return -1;
97 }
98 }
Adriana Kobylak5d6481f2015-10-29 21:44:55 -050099 else if (reqptr->parameter == 6) // Subnet
100 {
Hariharasubramanian R83951912016-01-20 07:06:36 -0600101 sprintf(new_netmask, "%d.%d.%d.%d", reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500102 }
103 else if (reqptr->parameter == 12) // Gateway
104 {
Hariharasubramanian R83951912016-01-20 07:06:36 -0600105 sprintf(new_gateway, "%d.%d.%d.%d", reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
106 }
107 else if (reqptr->parameter == 0) // Apply config
108 {
109 int rc = 0;
110 sd_bus_message *req = NULL;
111 sd_bus_message *res = NULL;
112 sd_bus *bus = NULL;
113 sd_bus_error err = SD_BUS_ERROR_NULL;
114
115 if (!strcmp(new_ipaddr, "") || !strcmp (new_netmask, "") || !strcmp (new_gateway, ""))
116 {
117 fprintf(stderr,"ERROR: Incomplete LAN Parameters\n");
118 return -1;
119 }
120
121 rc = sd_bus_open_system(&bus);
122 if(rc < 0)
123 {
124 fprintf(stderr,"ERROR: Getting a SYSTEM bus hook\n");
125 return -1;
126 }
127
128 if (strcmp(cur_ipaddr, ""))
129 {
130 sd_bus_error_free(&err);
131 sd_bus_message_unref(req);
132 sd_bus_message_unref(res);
133
134 rc = sd_bus_call_method(bus, // On the System Bus
135 app, // Service to contact
136 obj, // Object path
137 ifc, // Interface name
138 "DelAddress4", // Method to be called
139 &err, // object to return error
140 &res, // Response message on success
141 "ssss", // input message (dev,ip,nm,gw)
142 "eth0",
143 cur_ipaddr,
144 cur_netmask,
145 cur_gateway);
146 }
147
148 if(rc < 0)
149 {
150 fprintf(stderr, "Failed to remove existing IP %s: %s\n", cur_ipaddr, err.message);
151 return -1;
152 }
153
154 sd_bus_error_free(&err);
155 sd_bus_message_unref(req);
156 sd_bus_message_unref(res);
157
158 rc = sd_bus_call_method(bus, // On the System Bus
159 app, // Service to contact
160 obj, // Object path
161 ifc, // Interface name
162 "AddAddress4", // Method to be called
163 &err, // object to return error
164 &res, // Response message on success
165 "ssss", // input message (dev,ip,nm,gw)
166 "eth0",
167 new_ipaddr,
168 new_netmask,
169 new_gateway);
170 if(rc < 0)
171 {
172 fprintf(stderr, "Failed to set IP %s: %s\n", new_ipaddr, err.message);
173 return -1;
174 }
175
176 strcpy (cur_ipaddr, new_ipaddr);
177 strcpy (cur_netmask, new_netmask);
178 strcpy (cur_gateway, new_gateway);
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500179 }
180 else
181 {
182 fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
183 return IPMI_CC_PARM_NOT_SUPPORTED;
184 }
185
186 return rc;
187}
188
189struct get_lan_t {
190 uint8_t rev_channel;
191 uint8_t parameter;
192 uint8_t parameter_set;
193 uint8_t parameter_block;
194} __attribute__ ((packed));
195
196ipmi_ret_t ipmi_transport_get_lan(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
197 ipmi_request_t request, ipmi_response_t response,
198 ipmi_data_len_t data_len, ipmi_context_t context)
199{
200 ipmi_ret_t rc = IPMI_CC_OK;
201 *data_len = 0;
Hariharasubramanian R83951912016-01-20 07:06:36 -0600202 sd_bus_error err = SD_BUS_ERROR_NULL; /* fixme */
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500203 const uint8_t current_revision = 0x11; // Current rev per IPMI Spec 2.0
Hariharasubramanian R83951912016-01-20 07:06:36 -0600204
205 int family;
206 unsigned char prefixlen;
207 unsigned char scope;
208 unsigned int flags;
209 char saddr [128];
210 char gateway [128];
211 uint8_t buf[11];
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500212
213 printf("IPMI GET_LAN\n");
214
215 get_lan_t *reqptr = (get_lan_t*) request;
216
217 if (reqptr->rev_channel & 0x80) // Revision is bit 7
218 {
219 // Only current revision was requested
220 *data_len = sizeof(current_revision);
221 memcpy(response, &current_revision, *data_len);
222 return IPMI_CC_OK;
223 }
224
225 // TODO Use dbus interface once available. For now use ip cmd.
226 // TODO Add the rest of the parameters, like gateway
227
228 if (reqptr->parameter == 0) // In progress
229 {
230 uint8_t buf[] = {current_revision,0};
231 *data_len = sizeof(buf);
232 memcpy(response, &buf, *data_len);
233 return IPMI_CC_OK;
234 }
235 else if (reqptr->parameter == 1) // Authentication support
236 {
237 uint8_t buf[] = {current_revision,0x04};
238 *data_len = sizeof(buf);
239 memcpy(response, &buf, *data_len);
240 return IPMI_CC_OK;
241 }
242 else if (reqptr->parameter == 2) // Authentication enables
243 {
244 uint8_t buf[] = {current_revision,0x04,0x04,0x04,0x04,0x04};
245 *data_len = sizeof(buf);
246 memcpy(response, &buf, *data_len);
247 return IPMI_CC_OK;
248 }
249 else if (reqptr->parameter == 3) // IP
250 {
Hariharasubramanian R83951912016-01-20 07:06:36 -0600251 const char* device = "eth0";
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500252
Hariharasubramanian R83951912016-01-20 07:06:36 -0600253 sd_bus_message *res = NULL;
254 sd_bus *bus = NULL;
255 sd_bus_error err = SD_BUS_ERROR_NULL;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500256
Hariharasubramanian R83951912016-01-20 07:06:36 -0600257 rc = sd_bus_open_system(&bus);
258 if(rc < 0)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500259 {
Hariharasubramanian R83951912016-01-20 07:06:36 -0600260 fprintf(stderr,"ERROR: Getting a SYSTEM bus hook\n");
261 return -1;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500262 }
263
Hariharasubramanian R83951912016-01-20 07:06:36 -0600264 rc = sd_bus_call_method(bus, // On the System Bus
265 app, // Service to contact
266 obj, // Object path
267 ifc, // Interface name
268 "GetAddress4", // Method to be called
269 &err, // object to return error
270 &res, // Response message on success
271 "s", // input message (dev,ip,nm,gw)
272 "eth0");
273 if(rc < 0)
274 {
275 fprintf(stderr, "Failed to Get IP on interface : %s\n", device);
276 return -1;
277 }
278
279 /* rc = sd_bus_message_read(res, "a(iyyus)s", ...); */
280 rc = sd_bus_message_enter_container (res, 'a', "(iyyus)");
281 if(rc < 0)
282 {
283 fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
284 return -1;
285 }
286
287 while ((rc = sd_bus_message_read(res, "(iyyus)", &family, &prefixlen, &scope, &flags, &saddr)) > 0) {
288 printf("%s:%d:%d:%d:%s\n", family==AF_INET?"IPv4":"IPv6", prefixlen, scope, flags, saddr);
289 }
290
291 rc = sd_bus_message_read (res, "s", &gateway);
292 if(rc < 0)
293 {
294 fprintf(stderr, "Failed to parse gateway from response message:[%s]\n", strerror(-rc));
295 return -1;
296 }
297
298 memcpy((void*)&buf[0], &current_revision, 1);
299 sscanf (saddr, "%c.%c.%c.%c", &buf[1], &buf[2], &buf[3], &buf[4]);
300 buf[5] = family;
301 buf[6] = prefixlen;
302 sscanf (gateway, "%c.%c.%c.%c", &buf[7], &buf[8], &buf[9], &buf[10]);
303
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500304 *data_len = sizeof(buf);
305 memcpy(response, &buf, *data_len);
Hariharasubramanian R83951912016-01-20 07:06:36 -0600306
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500307 return IPMI_CC_OK;
308 }
309 else if (reqptr->parameter == 5) // MAC
310 {
311 //string to parse: link/ether xx:xx:xx:xx:xx:xx
312
Hariharasubramanian R83951912016-01-20 07:06:36 -0600313 const char* device = "eth0";
314 char eaddr [12];
315 uint8_t buf[7];
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500316
Hariharasubramanian R83951912016-01-20 07:06:36 -0600317 sd_bus_message *res = NULL;
318 sd_bus *bus = NULL;
319 sd_bus_error err = SD_BUS_ERROR_NULL;
320
321 rc = sd_bus_open_system(&bus);
322 if(rc < 0)
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500323 {
Hariharasubramanian R83951912016-01-20 07:06:36 -0600324 fprintf(stderr,"ERROR: Getting a SYSTEM bus hook\n");
325 return -1;
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500326 }
327
Hariharasubramanian R83951912016-01-20 07:06:36 -0600328 rc = sd_bus_call_method(bus, // On the System Bus
329 app, // Service to contact
330 obj, // Object path
331 ifc, // Interface name
332 "GetHwAddress", // Method to be called
333 &err, // object to return error
334 &res, // Response message on success
335 "s", // input message (dev,ip,nm,gw)
336 device);
337 if(rc < 0)
338 {
339 fprintf(stderr, "Failed to Get HW address of device : %s\n", device);
340 return -1;
341 }
342
343 rc = sd_bus_message_read (res, "s", &eaddr);
344 if(rc < 0)
345 {
346 fprintf(stderr, "Failed to parse gateway from response message:[%s]\n", strerror(-rc));
347 return -1;
348 }
349
350 memcpy((void*)&buf[0], &current_revision, 1);
351 sscanf (eaddr, "%x:%x:%x:%x:%x:%x", &buf[1], &buf[2], &buf[3], &buf[4], &buf[5], &buf[6]);
352
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500353 *data_len = sizeof(buf);
354 memcpy(response, &buf, *data_len);
Hariharasubramanian R83951912016-01-20 07:06:36 -0600355
Adriana Kobylak5d6481f2015-10-29 21:44:55 -0500356 return IPMI_CC_OK;
357 }
358 else
359 {
360 fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
361 return IPMI_CC_PARM_NOT_SUPPORTED;
362 }
363
364 return rc;
365}
366
367void register_netfn_transport_functions()
368{
369 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_WILDCARD);
370 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_WILDCARD, NULL, ipmi_transport_wildcard);
371
372 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_SET_LAN);
373 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_SET_LAN, NULL, ipmi_transport_set_lan);
374
375 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_TRANSPORT, IPMI_CMD_GET_LAN);
376 ipmi_register_callback(NETFUN_TRANSPORT, IPMI_CMD_GET_LAN, NULL, ipmi_transport_get_lan);
377
378 return;
379}