summaryrefslogtreecommitdiffstats
path: root/transporthandler.C
blob: ca8522dd34d4f5dddaa096717edbdee84f8d673e (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
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "ipmid-api.h"
#include "ipmid.H"
#include "transporthandler.h"

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;

    int i = 0;
    char syscmd[128];

    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 == 3) // IP
    {
        sprintf(syscmd, "ifconfig eth0 %d.%d.%d.%d", reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
        system(syscmd);
    }
    else if (reqptr->parameter == 6) // Subnet
    {
        sprintf(syscmd, "ifconfig eth0 netmask %d.%d.%d.%d", reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
        system(syscmd);
    }
    else if (reqptr->parameter == 12) // Gateway
    {
        sprintf(syscmd, "route add default gw %d.%d.%d.%d", reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
        system(syscmd);
    }
    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;

    const uint8_t current_revision = 0x11; // Current rev per IPMI Spec 2.0
    char syscmd[128];
    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, &current_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 == 0) // In progress
    {
        uint8_t buf[] = {current_revision,0};
        *data_len = sizeof(buf);
        memcpy(response, &buf, *data_len);
        return IPMI_CC_OK;
    }
    else if (reqptr->parameter == 1) // Authentication support
    {
        uint8_t buf[] = {current_revision,0x04};
        *data_len = sizeof(buf);
        memcpy(response, &buf, *data_len);
        return IPMI_CC_OK;
    }
    else if (reqptr->parameter == 2) // Authentication enables
    {
        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 == 3) // IP
    {
        //string to parse: inet xx.xx.xxx.xxx/xx

        uint8_t buf[5];
        memcpy((void*)&buf[0], &current_revision, 1);

        for (i=0; i<4; i++)
        {
            char ip[5];

            sprintf(syscmd, "ip address show dev eth0|grep inet|cut -d'/' -f1|cut -d' ' -f 6|cut -d'.' -f%d|head -n1", i+1);
            FILE *fp = popen(syscmd, "r");

            memset(ip,0,sizeof(ip));
            while (fgets(ip, sizeof(ip), fp) != 0)
            {
                int tmpip = strtoul(ip, NULL, 10);
                memcpy((void*)&buf[i+1], &tmpip, 1);
            }
            pclose(fp);
        }

        *data_len = sizeof(buf);
        memcpy(response, &buf, *data_len);
        return IPMI_CC_OK;
    }
    else if (reqptr->parameter == 5) // MAC
    {
        //string to parse: link/ether xx:xx:xx:xx:xx:xx

        uint8_t buf[7];
        memcpy((void*)&buf[0], &current_revision, 1);

        for (i=0; i<6; i++)
        {
            char mac[4];

            sprintf(syscmd, "ip address show dev eth0|grep link|cut -d' ' -f 6|cut -d':' -f%d", i+1);
            FILE *fp = popen(syscmd, "r");

            memset(mac,0,sizeof(mac));
            while (fgets(mac, sizeof(mac), fp) != 0)
            {
                int tmpmac = strtoul(mac, NULL, 16);
                memcpy((void*)&buf[i+1], &tmpmac, 1);
            }
            pclose(fp);
        }

        *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;
}

OpenPOWER on IntegriCloud