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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
/*
* node.c TIPC node functionality.
*
* 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.
*
* Authors: Richard Alpe <richard.alpe@ericsson.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <linux/tipc_netlink.h>
#include <linux/tipc.h>
#include <linux/genetlink.h>
#include "cmdl.h"
#include "msg.h"
#include "misc.h"
#include "node.h"
static int node_list_cb(const struct nlmsghdr *nlh, void *data)
{
struct nlattr *info[TIPC_NLA_MAX + 1] = {};
struct nlattr *attrs[TIPC_NLA_NODE_MAX + 1] = {};
char str[33] = {};
uint32_t addr;
mnl_attr_parse(nlh, sizeof(struct genlmsghdr), parse_attrs, info);
if (!info[TIPC_NLA_NODE])
return MNL_CB_ERROR;
mnl_attr_parse_nested(info[TIPC_NLA_NODE], parse_attrs, attrs);
if (!attrs[TIPC_NLA_NODE_ADDR])
return MNL_CB_ERROR;
addr = mnl_attr_get_u32(attrs[TIPC_NLA_NODE_ADDR]);
hash2nodestr(addr, str);
printf("%-32s %08x ", str, addr);
if (attrs[TIPC_NLA_NODE_UP])
printf("up\n");
else
printf("down\n");
return MNL_CB_OK;
}
static int cmd_node_list(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
if (help_flag) {
fprintf(stderr, "Usage: %s node list\n", cmdl->argv[0]);
return -EINVAL;
}
nlh = msg_init(TIPC_NL_NODE_GET);
if (!nlh) {
fprintf(stderr, "error, message initialisation failed\n");
return -1;
}
printf("Node Identity Hash State\n");
return msg_dumpit(nlh, node_list_cb, NULL);
}
static int cmd_node_set_addr(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
char *str;
uint32_t addr;
struct nlattr *nest;
if (cmdl->argc != cmdl->optind + 1) {
fprintf(stderr, "Usage: %s node set address ADDRESS\n",
cmdl->argv[0]);
return -EINVAL;
}
str = shift_cmdl(cmdl);
addr = str2addr(str);
if (!addr)
return -1;
nlh = msg_init(TIPC_NL_NET_SET);
if (!nlh) {
fprintf(stderr, "error, message initialisation failed\n");
return -1;
}
nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
mnl_attr_put_u32(nlh, TIPC_NLA_NET_ADDR, addr);
mnl_attr_nest_end(nlh, nest);
return msg_doit(nlh, NULL, NULL);
}
static int cmd_node_get_addr(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
int sk;
socklen_t sz = sizeof(struct sockaddr_tipc);
struct sockaddr_tipc addr;
sk = socket(AF_TIPC, SOCK_RDM, 0);
if (sk < 0) {
fprintf(stderr, "opening TIPC socket: %s\n", strerror(errno));
return -1;
}
if (getsockname(sk, (struct sockaddr *)&addr, &sz) < 0) {
fprintf(stderr, "getting TIPC socket address: %s\n",
strerror(errno));
close(sk);
return -1;
}
close(sk);
printf("%08x\n", addr.addr.id.node);
return 0;
}
static int cmd_node_set_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
uint8_t id[16] = {0,};
uint64_t *w0 = (uint64_t *) &id[0];
uint64_t *w1 = (uint64_t *) &id[8];
struct nlattr *nest;
char *str;
if (cmdl->argc != cmdl->optind + 1) {
fprintf(stderr, "Usage: %s node set nodeid NODE_ID\n",
cmdl->argv[0]);
return -EINVAL;
}
str = shift_cmdl(cmdl);
if (str2nodeid(str, id)) {
fprintf(stderr, "Invalid node identity\n");
return -EINVAL;
}
nlh = msg_init(TIPC_NL_NET_SET);
if (!nlh) {
fprintf(stderr, "error, message initialisation failed\n");
return -1;
}
nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID, *w0);
mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID_W1, *w1);
mnl_attr_nest_end(nlh, nest);
return msg_doit(nlh, NULL, NULL);
}
static void cmd_node_set_key_help(struct cmdl *cmdl)
{
fprintf(stderr,
"Usage: %s node set key KEY [algname ALGNAME] [PROPERTIES]\n"
" %s node set key rekeying REKEYING\n\n"
"KEY\n"
" Symmetric KEY & SALT as a composite ASCII or hex string (0x...) in form:\n"
" [KEY: 16, 24 or 32 octets][SALT: 4 octets]\n\n"
"ALGNAME\n"
" Cipher algorithm [default: \"gcm(aes)\"]\n\n"
"PROPERTIES\n"
" master - Set KEY as a cluster master key\n"
" <empty> - Set KEY as a cluster key\n"
" nodeid NODEID - Set KEY as a per-node key for own or peer\n\n"
"REKEYING\n"
" INTERVAL - Set rekeying interval (in minutes) [0: disable]\n"
" now - Trigger one (first) rekeying immediately\n\n"
"EXAMPLES\n"
" %s node set key this_is_a_master_key master\n"
" %s node set key 0x746869735F69735F615F6B657931365F73616C74\n"
" %s node set key this_is_a_key16_salt algname \"gcm(aes)\" nodeid 1001002\n"
" %s node set key rekeying 600\n\n",
cmdl->argv[0], cmdl->argv[0], cmdl->argv[0], cmdl->argv[0],
cmdl->argv[0], cmdl->argv[0]);
}
static int cmd_node_set_key(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
struct {
union {
struct tipc_aead_key key;
char mem[TIPC_AEAD_KEY_SIZE_MAX];
};
} input = {};
struct opt opts[] = {
{ "algname", OPT_KEYVAL, NULL },
{ "nodeid", OPT_KEYVAL, NULL },
{ "master", OPT_KEY, NULL },
{ "rekeying", OPT_KEYVAL, NULL },
{ NULL }
};
struct nlattr *nest;
struct opt *opt_algname, *opt_nodeid, *opt_master, *opt_rekeying;
uint8_t id[TIPC_NODEID_LEN] = {0,};
uint32_t rekeying = 0;
bool has_key = false;
int keysize;
char *str;
if (help_flag || cmdl->optind >= cmdl->argc) {
(cmd->help)(cmdl);
return -EINVAL;
}
/* Check if command starts with opts i.e. "rekeying" opt without key */
if (find_opt(opts, cmdl->argv[cmdl->optind]))
goto get_ops;
/* Get user key */
has_key = true;
str = shift_cmdl(cmdl);
if (str2key(str, &input.key)) {
fprintf(stderr, "error, invalid key input\n");
return -EINVAL;
}
get_ops:
if (parse_opts(opts, cmdl) < 0)
return -EINVAL;
/* Get rekeying time */
opt_rekeying = get_opt(opts, "rekeying");
if (opt_rekeying) {
if (!strcmp(opt_rekeying->val, "now"))
rekeying = TIPC_REKEYING_NOW;
else
rekeying = atoi(opt_rekeying->val);
}
/* Get algorithm name, default: "gcm(aes)" */
opt_algname = get_opt(opts, "algname");
if (!opt_algname) {
strcpy(input.key.alg_name, "gcm(aes)");
} else {
if (strlen(opt_algname->val) > TIPC_AEAD_ALG_NAME) {
fprintf(stderr, "error, invalid algname\n");
return -EINVAL;
}
strcpy(input.key.alg_name, opt_algname->val);
}
/* Get node identity */
opt_nodeid = get_opt(opts, "nodeid");
if (opt_nodeid && str2nodeid(opt_nodeid->val, id)) {
fprintf(stderr, "error, invalid node identity\n");
return -EINVAL;
}
/* Get master key indication */
opt_master = get_opt(opts, "master");
/* Sanity check if wrong option */
if (opt_nodeid && opt_master) {
fprintf(stderr, "error, per-node key cannot be master\n");
return -EINVAL;
}
/* Init & do the command */
nlh = msg_init(TIPC_NL_KEY_SET);
if (!nlh) {
fprintf(stderr, "error, message initialisation failed\n");
return -1;
}
nest = mnl_attr_nest_start(nlh, TIPC_NLA_NODE);
if (has_key) {
keysize = tipc_aead_key_size(&input.key);
mnl_attr_put(nlh, TIPC_NLA_NODE_KEY, keysize, &input.key);
if (opt_nodeid)
mnl_attr_put(nlh, TIPC_NLA_NODE_ID, TIPC_NODEID_LEN, id);
if (opt_master)
mnl_attr_put(nlh, TIPC_NLA_NODE_KEY_MASTER, 0, NULL);
}
if (opt_rekeying)
mnl_attr_put_u32(nlh, TIPC_NLA_NODE_REKEYING, rekeying);
mnl_attr_nest_end(nlh, nest);
return msg_doit(nlh, NULL, NULL);
}
static int cmd_node_flush_key(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
if (help_flag) {
(cmd->help)(cmdl);
return -EINVAL;
}
/* Init & do the command */
nlh = msg_init(TIPC_NL_KEY_FLUSH);
if (!nlh) {
fprintf(stderr, "error, message initialisation failed\n");
return -1;
}
return msg_doit(nlh, NULL, NULL);
}
static int nodeid_get_cb(const struct nlmsghdr *nlh, void *data)
{
struct nlattr *info[TIPC_NLA_MAX + 1] = {};
struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {};
char str[33] = {0,};
uint8_t id[16] = {0,};
uint64_t *w0 = (uint64_t *) &id[0];
uint64_t *w1 = (uint64_t *) &id[8];
mnl_attr_parse(nlh, sizeof(struct genlmsghdr), parse_attrs, info);
if (!info[TIPC_NLA_NET])
return MNL_CB_ERROR;
mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs);
if (!attrs[TIPC_NLA_NET_ID])
return MNL_CB_ERROR;
*w0 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID]);
*w1 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
nodeid2str(id, str);
printf("Node Identity Hash\n");
printf("%-33s", str);
cmd_node_get_addr(NULL, NULL, NULL, NULL);
return MNL_CB_OK;
}
static int cmd_node_get_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
if (help_flag) {
(cmd->help)(cmdl);
return -EINVAL;
}
nlh = msg_init(TIPC_NL_NET_GET);
if (!nlh) {
fprintf(stderr, "error, message initialisation failed\n");
return -1;
}
return msg_dumpit(nlh, nodeid_get_cb, NULL);
}
static int netid_get_cb(const struct nlmsghdr *nlh, void *data)
{
struct nlattr *info[TIPC_NLA_MAX + 1] = {};
struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {};
mnl_attr_parse(nlh, sizeof(struct genlmsghdr), parse_attrs, info);
if (!info[TIPC_NLA_NET])
return MNL_CB_ERROR;
mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs);
if (!attrs[TIPC_NLA_NET_ID])
return MNL_CB_ERROR;
printf("%u\n", mnl_attr_get_u32(attrs[TIPC_NLA_NET_ID]));
return MNL_CB_OK;
}
static int cmd_node_get_netid(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
if (help_flag) {
(cmd->help)(cmdl);
return -EINVAL;
}
nlh = msg_init(TIPC_NL_NET_GET);
if (!nlh) {
fprintf(stderr, "error, message initialisation failed\n");
return -1;
}
return msg_dumpit(nlh, netid_get_cb, NULL);
}
static int cmd_node_set_netid(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
int netid;
struct nlattr *nest;
if (help_flag) {
(cmd->help)(cmdl);
return -EINVAL;
}
nlh = msg_init(TIPC_NL_NET_SET);
if (!nlh) {
fprintf(stderr, "error, message initialisation failed\n");
return -1;
}
if (cmdl->argc != cmdl->optind + 1) {
fprintf(stderr, "Usage: %s node set netid NETID\n",
cmdl->argv[0]);
return -EINVAL;
}
netid = atoi(shift_cmdl(cmdl));
nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
mnl_attr_put_u32(nlh, TIPC_NLA_NET_ID, netid);
mnl_attr_nest_end(nlh, nest);
return msg_doit(nlh, NULL, NULL);
}
static void cmd_node_flush_help(struct cmdl *cmdl)
{
fprintf(stderr,
"Usage: %s node flush PROPERTY\n\n"
"PROPERTIES\n"
" key - Flush all symmetric-keys\n",
cmdl->argv[0]);
}
static int cmd_node_flush(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
const struct cmd cmds[] = {
{ "key", cmd_node_flush_key, NULL },
{ NULL }
};
return run_cmd(nlh, cmd, cmds, cmdl, NULL);
}
static void cmd_node_set_help(struct cmdl *cmdl)
{
fprintf(stderr,
"Usage: %s node set PROPERTY\n\n"
"PROPERTIES\n"
" identity NODEID - Set node identity\n"
" clusterid CLUSTERID - Set local cluster id\n"
" key PROPERTY - Set symmetric-key\n",
cmdl->argv[0]);
}
static int cmd_node_set(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
const struct cmd cmds[] = {
{ "address", cmd_node_set_addr, NULL },
{ "identity", cmd_node_set_nodeid, NULL },
{ "netid", cmd_node_set_netid, NULL },
{ "clusterid", cmd_node_set_netid, NULL },
{ "key", cmd_node_set_key, cmd_node_set_key_help },
{ NULL }
};
return run_cmd(nlh, cmd, cmds, cmdl, NULL);
}
static void cmd_node_get_help(struct cmdl *cmdl)
{
fprintf(stderr,
"Usage: %s node get PROPERTY\n\n"
"PROPERTIES\n"
" identity - Get node identity\n"
" clusterid - Get local clusterid\n",
cmdl->argv[0]);
}
static int cmd_node_get(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
const struct cmd cmds[] = {
{ "address", cmd_node_get_addr, NULL },
{ "identity", cmd_node_get_nodeid, NULL },
{ "netid", cmd_node_get_netid, NULL },
{ "clusterid", cmd_node_get_netid, NULL },
{ NULL }
};
return run_cmd(nlh, cmd, cmds, cmdl, NULL);
}
void cmd_node_help(struct cmdl *cmdl)
{
fprintf(stderr,
"Usage: %s node COMMAND [ARGS] ...\n\n"
"COMMANDS\n"
" list - List remote nodes\n"
" get - Get local node parameters\n"
" set - Set local node parameters\n"
" flush - Flush local node parameters\n",
cmdl->argv[0]);
}
int cmd_node(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
void *data)
{
const struct cmd cmds[] = {
{ "list", cmd_node_list, NULL },
{ "get", cmd_node_get, cmd_node_get_help },
{ "set", cmd_node_set, cmd_node_set_help },
{ "flush", cmd_node_flush, cmd_node_flush_help},
{ NULL }
};
return run_cmd(nlh, cmd, cmds, cmdl, NULL);
}
|