-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsr_node.c
389 lines (321 loc) · 9.11 KB
/
hsr_node.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
/* (C) 2020 by Taehee Yoo <[email protected]>
*
* Author: Taehee Yoo <[email protected]>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Based on libgtpnl by Pablo Neira Ayuso <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <inttypes.h>
#include <libmnl/libmnl.h>
#include <linux/genetlink.h>
#include <linux/if_link.h>
#include <linux/if_ether.h>
#include <linux/hsr_netlink.h>
struct nlmsghdr *
genl_nlmsg_build_hdr(char *buf, uint16_t type, uint16_t flags, uint32_t seq,
uint8_t cmd)
{
struct nlmsghdr *nlh;
struct genlmsghdr *genl;
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = type;
nlh->nlmsg_flags = NLM_F_REQUEST | flags;
nlh->nlmsg_seq = seq;
genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
genl->cmd = cmd;
genl->version = 0;
return nlh;
}
static int genl_ctrl_validate_cb(const struct nlattr *attr, void *data)
{
const struct nlattr **tb = data;
int type = mnl_attr_get_type(attr);
if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
return MNL_CB_OK;
switch(type) {
case CTRL_ATTR_FAMILY_ID:
if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
break;
}
tb[type] = attr;
return MNL_CB_OK;
}
static int genl_ctrl_cb(const struct nlmsghdr *nlh, void *data)
{
struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
int32_t *genl_id = data;
mnl_attr_parse(nlh, sizeof(*genl), genl_ctrl_validate_cb, tb);
if (tb[CTRL_ATTR_FAMILY_ID])
*genl_id = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
else
*genl_id = -1;
return MNL_CB_OK;
}
struct mnl_socket *genl_socket_open(void)
{
struct mnl_socket *nl;
nl = mnl_socket_open(NETLINK_GENERIC);
if (nl == NULL) {
perror("mnl_socket_open");
return NULL;
}
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
perror("mnl_socket_bind");
return NULL;
}
return nl;
}
void genl_socket_close(struct mnl_socket *nl)
{
mnl_socket_close(nl);
}
int genl_socket_talk(struct mnl_socket *nl, struct nlmsghdr *nlh, uint32_t seq,
int (*cb)(const struct nlmsghdr *nlh, void *data),
void *data)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
int ret;
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
perror("mnl_socket_send");
return -1;
}
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
while (ret > 0) {
ret = mnl_cb_run(buf, ret, seq, mnl_socket_get_portid(nl),
cb, data);
if (ret <= 0)
break;
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
}
return ret;
}
static struct nlmsghdr *
genl_nlmsg_build_lookup(char *buf, const char *subsys_name)
{
struct nlmsghdr *nlh;
struct genlmsghdr *genl;
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = GENL_ID_CTRL;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
nlh->nlmsg_seq = time(NULL);
genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
genl->cmd = CTRL_CMD_GETFAMILY;
genl->version = 1;
mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL);
mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, subsys_name);
return nlh;
}
int genl_lookup_family(struct mnl_socket *nl, const char *family)
{
int32_t genl_id;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh = genl_nlmsg_build_lookup(buf, family);
int err;
err = genl_socket_talk(nl, nlh, nlh->nlmsg_seq, genl_ctrl_cb, &genl_id);
if (err < 0)
return -1;
return genl_id;
}
static int genl_hsr_validate_cb(const struct nlattr *attr, void *data)
{
int type = mnl_attr_get_type(attr);
unsigned char addr[ETH_ALEN];
char buf[IFNAMSIZ];
int ifindex;
if (mnl_attr_type_valid(attr, HSR_C_MAX) < 0)
return MNL_CB_OK;
switch(type) {
case HSR_A_IFINDEX:
ifindex = mnl_attr_get_u32(attr);
if (if_indextoname(ifindex, buf) == NULL)
snprintf(buf, IFNAMSIZ, "if%u", ifindex);
printf("Interface: %s\n", buf);
break;
case HSR_A_NODE_ADDR:
memcpy(addr, mnl_attr_get_payload(attr), ETH_ALEN);
printf("MAC address A: %02x:%02x:%02x:%02x:%02x:%02x\n",
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
break;
case HSR_A_IF1_AGE:
printf("Interface1 age: %ums\n", mnl_attr_get_u32(attr));
break;
case HSR_A_IF2_AGE:
printf("Interface2 age: %ums\n", mnl_attr_get_u32(attr));
break;
case HSR_A_NODE_ADDR_B:
memcpy(addr, mnl_attr_get_payload(attr), ETH_ALEN);
printf("MAC address B: %02x:%02x:%02x:%02x:%02x:%02x\n",
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
break;
case HSR_A_IF1_SEQ:
printf("Interface1 sequence: %u\n", mnl_attr_get_u16(attr));
break;
case HSR_A_IF2_SEQ:
printf("Interface2 sequence: %u\n", mnl_attr_get_u16(attr));
break;
case HSR_A_IF1_IFINDEX:
ifindex = mnl_attr_get_u32(attr);
if (if_indextoname(ifindex, buf) == NULL)
snprintf(buf, IFNAMSIZ, "if%u", ifindex);
printf("Slave interface1: %s\n", buf);
break;
case HSR_A_IF2_IFINDEX:
ifindex = mnl_attr_get_u32(attr);
if (if_indextoname(ifindex, buf) == NULL)
snprintf(buf, IFNAMSIZ, "if%u", ifindex);
printf("Slave interface2: %s\n", buf);
break;
case HSR_A_ADDR_B_IFINDEX:
ifindex = mnl_attr_get_u32(attr);
printf("Address B index: %u\n", ifindex);
break;
default:
printf("[TEST]%s %u type = %d \n", __func__, __LINE__, type);
break;
}
return MNL_CB_OK;
}
static int genl_hsr_attr_cb(const struct nlmsghdr *nlh, void *data)
{
struct genlmsghdr *genl;
mnl_attr_parse(nlh, sizeof(*genl), genl_hsr_validate_cb, NULL);
return MNL_CB_OK;
}
int hsr_list_nodes(int genl_id, int ifindex, struct mnl_socket *nl)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
uint32_t seq = time(NULL);
nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
HSR_C_GET_NODE_LIST);
mnl_attr_put_u32(nlh, HSR_A_IFINDEX, ifindex);
if (genl_socket_talk(nl, nlh, seq, genl_hsr_attr_cb, NULL) < 0) {
perror("genl_socket_talk");
return 0;
}
return 0;
}
int hsr_status_node(int genl_id, int ifindex, struct mnl_socket *nl, char *lladdr)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
uint32_t seq = time(NULL);
nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
HSR_C_GET_NODE_STATUS);
mnl_attr_put_u32(nlh, HSR_A_IFINDEX, ifindex);
mnl_attr_put_check(nlh, MNL_SOCKET_BUFFER_SIZE, HSR_A_NODE_ADDR, ETH_ALEN, lladdr);
if (genl_socket_talk(nl, nlh, seq, genl_hsr_attr_cb, NULL) < 0) {
perror("genl_socket_talk");
return 0;
}
return 0;
}
int hsr_dump_nodes(int genl_id, struct mnl_socket *nl)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
uint32_t seq = time(NULL);
nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
HSR_C_GET_NODE_LIST);
if (genl_socket_talk(nl, nlh, seq, genl_hsr_attr_cb, NULL) < 0) {
perror("genl_socket_talk");
return 0;
}
return 0;
}
static int
list_nodes(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
{
int ifindex;
ifindex = if_nametoindex(argv[2]);
if (ifindex == 0) {
perror("if_nametoindex");
return -1;
}
return hsr_list_nodes(genl_id, ifindex , nl);
}
static int
status_node(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
{
int ifindex;
char lladdr[ETH_ALEN];
ifindex = if_nametoindex(argv[2]);
if (ifindex == 0) {
perror("if_nametoindex");
return -1;
}
if (sscanf(argv[3], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
lladdr, lladdr+1, lladdr+2, lladdr+3, lladdr+4, lladdr+5) != 6) {
perror("Invalid mac address\n");
return 0;
}
return hsr_status_node(genl_id, ifindex , nl, lladdr);
}
static int
dump_nodes(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
{
return hsr_dump_nodes(genl_id, nl);
}
int main(int argc, char *argv[])
{
struct mnl_socket *nl;
int32_t genl_id;
int ret;
if (argc < 2) {
printf("%s <list|dump> [<options,...>]\n", argv[0]);
exit(EXIT_FAILURE);
}
nl = genl_socket_open();
if (nl == NULL) {
perror("mnl_socket_open");
exit(EXIT_FAILURE);
}
genl_id = genl_lookup_family(nl, "HSR");
if (genl_id < 0) {
printf("not found hsr genl family\n");
exit(EXIT_FAILURE);
}
if (strncmp(argv[1], "list", strlen(argv[1])) == 0) {
if (argc < 3)
exit(EXIT_FAILURE);
ret = list_nodes(argc, argv, genl_id, nl);
} else if (strncmp(argv[1], "status", strlen(argv[1])) == 0) {
if (argc < 4)
exit(EXIT_FAILURE);
ret = status_node(argc, argv, genl_id, nl);
} else if (strncmp(argv[1], "dump", strlen(argv[1])) == 0) {
ret = dump_nodes(argc, argv, genl_id, nl);
} else {
printf("Unknown command `%s'\n", argv[1]);
exit(EXIT_FAILURE);
}
mnl_socket_close(nl);
return ret;
}