]> git.proxmox.com Git - mirror_iproute2.git/blame - tipc/node.c
tipc: add option to set master key for encryption
[mirror_iproute2.git] / tipc / node.c
CommitLineData
f043759d
RA
1/*
2 * node.c TIPC node functionality.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Richard Alpe <richard.alpe@ericsson.com>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <errno.h>
16
17#include <linux/tipc_netlink.h>
18#include <linux/tipc.h>
19#include <linux/genetlink.h>
20#include <libmnl/libmnl.h>
21
22#include "cmdl.h"
23#include "msg.h"
24#include "misc.h"
25#include "node.h"
26
27static int node_list_cb(const struct nlmsghdr *nlh, void *data)
28{
f043759d
RA
29 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
30 struct nlattr *attrs[TIPC_NLA_NODE_MAX + 1] = {};
5947046d
JM
31 char str[33] = {};
32 uint32_t addr;
f043759d 33
8d05f33a 34 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), parse_attrs, info);
f043759d
RA
35 if (!info[TIPC_NLA_NODE])
36 return MNL_CB_ERROR;
37
38 mnl_attr_parse_nested(info[TIPC_NLA_NODE], parse_attrs, attrs);
39 if (!attrs[TIPC_NLA_NODE_ADDR])
40 return MNL_CB_ERROR;
41
42 addr = mnl_attr_get_u32(attrs[TIPC_NLA_NODE_ADDR]);
5947046d
JM
43 hash2nodestr(addr, str);
44 printf("%-32s %08x ", str, addr);
f043759d
RA
45 if (attrs[TIPC_NLA_NODE_UP])
46 printf("up\n");
47 else
48 printf("down\n");
f043759d
RA
49 return MNL_CB_OK;
50}
51
52static int cmd_node_list(struct nlmsghdr *nlh, const struct cmd *cmd,
53 struct cmdl *cmdl, void *data)
54{
55 char buf[MNL_SOCKET_BUFFER_SIZE];
56
57 if (help_flag) {
58 fprintf(stderr, "Usage: %s node list\n", cmdl->argv[0]);
59 return -EINVAL;
60 }
61
62 if (!(nlh = msg_init(buf, TIPC_NL_NODE_GET))) {
63 fprintf(stderr, "error, message initialisation failed\n");
64 return -1;
65 }
5947046d 66 printf("Node Identity Hash State\n");
f043759d
RA
67 return msg_dumpit(nlh, node_list_cb, NULL);
68}
69
70static int cmd_node_set_addr(struct nlmsghdr *nlh, const struct cmd *cmd,
71 struct cmdl *cmdl, void *data)
72{
73 char *str;
74 uint32_t addr;
75 struct nlattr *nest;
76 char buf[MNL_SOCKET_BUFFER_SIZE];
77
78 if (cmdl->argc != cmdl->optind + 1) {
79 fprintf(stderr, "Usage: %s node set address ADDRESS\n",
80 cmdl->argv[0]);
81 return -EINVAL;
82 }
83
84 str = shift_cmdl(cmdl);
85 addr = str2addr(str);
86 if (!addr)
87 return -1;
88
89 if (!(nlh = msg_init(buf, TIPC_NL_NET_SET))) {
90 fprintf(stderr, "error, message initialisation failed\n");
91 return -1;
92 }
93
94 nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
95 mnl_attr_put_u32(nlh, TIPC_NLA_NET_ADDR, addr);
96 mnl_attr_nest_end(nlh, nest);
97
98 return msg_doit(nlh, NULL, NULL);
99}
100
101static int cmd_node_get_addr(struct nlmsghdr *nlh, const struct cmd *cmd,
102 struct cmdl *cmdl, void *data)
103{
104 int sk;
105 socklen_t sz = sizeof(struct sockaddr_tipc);
106 struct sockaddr_tipc addr;
107
436270a4
PS
108 sk = socket(AF_TIPC, SOCK_RDM, 0);
109 if (sk < 0) {
f043759d
RA
110 fprintf(stderr, "opening TIPC socket: %s\n", strerror(errno));
111 return -1;
112 }
113
114 if (getsockname(sk, (struct sockaddr *)&addr, &sz) < 0) {
115 fprintf(stderr, "getting TIPC socket address: %s\n",
116 strerror(errno));
117 close(sk);
118 return -1;
119 }
120 close(sk);
121
5947046d 122 printf("%08x\n", addr.addr.id.node);
f043759d
RA
123 return 0;
124}
125
725ebfbf
JM
126static int cmd_node_set_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
127 struct cmdl *cmdl, void *data)
128{
129 char buf[MNL_SOCKET_BUFFER_SIZE];
130 uint8_t id[16] = {0,};
131 uint64_t *w0 = (uint64_t *) &id[0];
132 uint64_t *w1 = (uint64_t *) &id[8];
133 struct nlattr *nest;
134 char *str;
135
136 if (cmdl->argc != cmdl->optind + 1) {
137 fprintf(stderr, "Usage: %s node set nodeid NODE_ID\n",
138 cmdl->argv[0]);
139 return -EINVAL;
140 }
141
142 str = shift_cmdl(cmdl);
143 if (str2nodeid(str, id)) {
144 fprintf(stderr, "Invalid node identity\n");
145 return -EINVAL;
146 }
147
148 nlh = msg_init(buf, TIPC_NL_NET_SET);
149 if (!nlh) {
150 fprintf(stderr, "error, message initialisation failed\n");
151 return -1;
152 }
153 nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
154 mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID, *w0);
155 mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID_W1, *w1);
156 mnl_attr_nest_end(nlh, nest);
157 return msg_doit(nlh, NULL, NULL);
158}
159
24bee3bf
TL
160static void cmd_node_set_key_help(struct cmdl *cmdl)
161{
162 fprintf(stderr,
5fb36818
TL
163 "Usage: %s node set key KEY [algname ALGNAME] [PROPERTIES]\n\n"
164 "KEY\n"
165 " Symmetric KEY & SALT as a composite ASCII or hex string (0x...) in form:\n"
166 " [KEY: 16, 24 or 32 octets][SALT: 4 octets]\n\n"
167 "ALGNAME\n"
168 " Cipher algorithm [default: \"gcm(aes)\"]\n\n"
24bee3bf 169 "PROPERTIES\n"
5fb36818
TL
170 " master - Set KEY as a cluster master key\n"
171 " <empty> - Set KEY as a cluster key\n"
172 " nodeid NODEID - Set KEY as a per-node key for own or peer\n\n"
24bee3bf 173 "EXAMPLES\n"
5fb36818
TL
174 " %s node set key this_is_a_master_key master\n"
175 " %s node set key 0x746869735F69735F615F6B657931365F73616C74\n"
176 " %s node set key this_is_a_key16_salt algname \"gcm(aes)\" nodeid 1001002\n\n",
177 cmdl->argv[0], cmdl->argv[0], cmdl->argv[0], cmdl->argv[0]);
24bee3bf
TL
178}
179
180static int cmd_node_set_key(struct nlmsghdr *nlh, const struct cmd *cmd,
181 struct cmdl *cmdl, void *data)
182{
183 struct {
d5391e18
TL
184 union {
185 struct tipc_aead_key key;
186 char mem[TIPC_AEAD_KEY_SIZE_MAX];
187 };
24bee3bf
TL
188 } input = {};
189 struct opt opts[] = {
190 { "algname", OPT_KEYVAL, NULL },
191 { "nodeid", OPT_KEYVAL, NULL },
5fb36818 192 { "master", OPT_KEY, NULL },
24bee3bf
TL
193 { NULL }
194 };
195 struct nlattr *nest;
5fb36818 196 struct opt *opt_algname, *opt_nodeid, *opt_master;
24bee3bf
TL
197 char buf[MNL_SOCKET_BUFFER_SIZE];
198 uint8_t id[TIPC_NODEID_LEN] = {0,};
199 int keysize;
200 char *str;
201
5fb36818 202 if (help_flag || cmdl->optind >= cmdl->argc) {
24bee3bf
TL
203 (cmd->help)(cmdl);
204 return -EINVAL;
205 }
206
24bee3bf
TL
207
208 /* Get user key */
209 str = shift_cmdl(cmdl);
210 if (str2key(str, &input.key)) {
211 fprintf(stderr, "error, invalid key input\n");
212 return -EINVAL;
213 }
214
215 if (parse_opts(opts, cmdl) < 0)
216 return -EINVAL;
217
218 /* Get algorithm name, default: "gcm(aes)" */
219 opt_algname = get_opt(opts, "algname");
220 if (!opt_algname)
221 strcpy(input.key.alg_name, "gcm(aes)");
222 else
223 strcpy(input.key.alg_name, opt_algname->val);
224
225 /* Get node identity */
226 opt_nodeid = get_opt(opts, "nodeid");
227 if (opt_nodeid && str2nodeid(opt_nodeid->val, id)) {
228 fprintf(stderr, "error, invalid node identity\n");
229 return -EINVAL;
230 }
231
5fb36818
TL
232 /* Get master key indication */
233 opt_master = get_opt(opts, "master");
234
235 /* Sanity check if wrong option */
236 if (opt_nodeid && opt_master) {
237 fprintf(stderr, "error, per-node key cannot be master\n");
238 return -EINVAL;
239 }
240
24bee3bf
TL
241 /* Init & do the command */
242 nlh = msg_init(buf, TIPC_NL_KEY_SET);
243 if (!nlh) {
244 fprintf(stderr, "error, message initialisation failed\n");
245 return -1;
246 }
5fb36818 247
24bee3bf
TL
248 nest = mnl_attr_nest_start(nlh, TIPC_NLA_NODE);
249 keysize = tipc_aead_key_size(&input.key);
250 mnl_attr_put(nlh, TIPC_NLA_NODE_KEY, keysize, &input.key);
251 if (opt_nodeid)
252 mnl_attr_put(nlh, TIPC_NLA_NODE_ID, TIPC_NODEID_LEN, id);
5fb36818
TL
253 if (opt_master)
254 mnl_attr_put(nlh, TIPC_NLA_NODE_KEY_MASTER, 0, NULL);
255
24bee3bf
TL
256 mnl_attr_nest_end(nlh, nest);
257 return msg_doit(nlh, NULL, NULL);
258}
259
260static int cmd_node_flush_key(struct nlmsghdr *nlh, const struct cmd *cmd,
261 struct cmdl *cmdl, void *data)
262{
263 char buf[MNL_SOCKET_BUFFER_SIZE];
264
265 if (help_flag) {
266 (cmd->help)(cmdl);
267 return -EINVAL;
268 }
269
270 /* Init & do the command */
271 nlh = msg_init(buf, TIPC_NL_KEY_FLUSH);
272 if (!nlh) {
273 fprintf(stderr, "error, message initialisation failed\n");
274 return -1;
275 }
276 return msg_doit(nlh, NULL, NULL);
277}
278
725ebfbf
JM
279static int nodeid_get_cb(const struct nlmsghdr *nlh, void *data)
280{
725ebfbf
JM
281 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
282 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {};
283 char str[33] = {0,};
284 uint8_t id[16] = {0,};
285 uint64_t *w0 = (uint64_t *) &id[0];
286 uint64_t *w1 = (uint64_t *) &id[8];
725ebfbf 287
8d05f33a 288 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), parse_attrs, info);
725ebfbf
JM
289 if (!info[TIPC_NLA_NET])
290 return MNL_CB_ERROR;
291
292 mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs);
293 if (!attrs[TIPC_NLA_NET_ID])
294 return MNL_CB_ERROR;
295
296 *w0 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID]);
297 *w1 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
298 nodeid2str(id, str);
5947046d
JM
299 printf("Node Identity Hash\n");
300 printf("%-33s", str);
725ebfbf
JM
301 cmd_node_get_addr(NULL, NULL, NULL, NULL);
302 return MNL_CB_OK;
303}
304
305static int cmd_node_get_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
306 struct cmdl *cmdl, void *data)
307{
308 char buf[MNL_SOCKET_BUFFER_SIZE];
309
310 if (help_flag) {
311 (cmd->help)(cmdl);
312 return -EINVAL;
313 }
314
315 nlh = msg_init(buf, TIPC_NL_NET_GET);
316 if (!nlh) {
317 fprintf(stderr, "error, message initialisation failed\n");
318 return -1;
319 }
320
321 return msg_dumpit(nlh, nodeid_get_cb, NULL);
322}
323
324
f043759d
RA
325static int netid_get_cb(const struct nlmsghdr *nlh, void *data)
326{
f043759d
RA
327 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
328 struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {};
329
8d05f33a 330 mnl_attr_parse(nlh, sizeof(struct genlmsghdr), parse_attrs, info);
f043759d
RA
331 if (!info[TIPC_NLA_NET])
332 return MNL_CB_ERROR;
333
334 mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs);
335 if (!attrs[TIPC_NLA_NET_ID])
336 return MNL_CB_ERROR;
337
338 printf("%u\n", mnl_attr_get_u32(attrs[TIPC_NLA_NET_ID]));
339
340 return MNL_CB_OK;
341}
342
343static int cmd_node_get_netid(struct nlmsghdr *nlh, const struct cmd *cmd,
344 struct cmdl *cmdl, void *data)
345{
346 char buf[MNL_SOCKET_BUFFER_SIZE];
347
348 if (help_flag) {
349 (cmd->help)(cmdl);
350 return -EINVAL;
351 }
352
353 if (!(nlh = msg_init(buf, TIPC_NL_NET_GET))) {
354 fprintf(stderr, "error, message initialisation failed\n");
355 return -1;
356 }
357
358 return msg_dumpit(nlh, netid_get_cb, NULL);
359}
360
361static int cmd_node_set_netid(struct nlmsghdr *nlh, const struct cmd *cmd,
362 struct cmdl *cmdl, void *data)
363{
364 int netid;
365 char buf[MNL_SOCKET_BUFFER_SIZE];
366 struct nlattr *nest;
367
368 if (help_flag) {
369 (cmd->help)(cmdl);
370 return -EINVAL;
371 }
372
373 if (!(nlh = msg_init(buf, TIPC_NL_NET_SET))) {
374 fprintf(stderr, "error, message initialisation failed\n");
375 return -1;
376 }
377
378 if (cmdl->argc != cmdl->optind + 1) {
379 fprintf(stderr, "Usage: %s node set netid NETID\n",
380 cmdl->argv[0]);
381 return -EINVAL;
382 }
383 netid = atoi(shift_cmdl(cmdl));
384
385 nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
386 mnl_attr_put_u32(nlh, TIPC_NLA_NET_ID, netid);
387 mnl_attr_nest_end(nlh, nest);
388
389 return msg_doit(nlh, NULL, NULL);
390}
391
24bee3bf
TL
392static void cmd_node_flush_help(struct cmdl *cmdl)
393{
394 fprintf(stderr,
395 "Usage: %s node flush PROPERTY\n\n"
396 "PROPERTIES\n"
397 " key - Flush all symmetric-keys\n",
398 cmdl->argv[0]);
399}
400
401static int cmd_node_flush(struct nlmsghdr *nlh, const struct cmd *cmd,
402 struct cmdl *cmdl, void *data)
403{
404 const struct cmd cmds[] = {
405 { "key", cmd_node_flush_key, NULL },
406 { NULL }
407 };
408
409 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
410}
411
f043759d
RA
412static void cmd_node_set_help(struct cmdl *cmdl)
413{
414 fprintf(stderr,
415 "Usage: %s node set PROPERTY\n\n"
416 "PROPERTIES\n"
725ebfbf 417 " identity NODEID - Set node identity\n"
24bee3bf
TL
418 " clusterid CLUSTERID - Set local cluster id\n"
419 " key PROPERTY - Set symmetric-key\n",
f043759d
RA
420 cmdl->argv[0]);
421}
422
423static int cmd_node_set(struct nlmsghdr *nlh, const struct cmd *cmd,
424 struct cmdl *cmdl, void *data)
425{
426 const struct cmd cmds[] = {
725ebfbf
JM
427 { "address", cmd_node_set_addr, NULL },
428 { "identity", cmd_node_set_nodeid, NULL },
f043759d 429 { "netid", cmd_node_set_netid, NULL },
725ebfbf 430 { "clusterid", cmd_node_set_netid, NULL },
24bee3bf 431 { "key", cmd_node_set_key, cmd_node_set_key_help },
f043759d
RA
432 { NULL }
433 };
434
435 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
436}
437
438static void cmd_node_get_help(struct cmdl *cmdl)
439{
440 fprintf(stderr,
441 "Usage: %s node get PROPERTY\n\n"
442 "PROPERTIES\n"
725ebfbf
JM
443 " identity - Get node identity\n"
444 " clusterid - Get local clusterid\n",
f043759d
RA
445 cmdl->argv[0]);
446}
447
448static int cmd_node_get(struct nlmsghdr *nlh, const struct cmd *cmd,
449 struct cmdl *cmdl, void *data)
450{
451 const struct cmd cmds[] = {
452 { "address", cmd_node_get_addr, NULL },
725ebfbf 453 { "identity", cmd_node_get_nodeid, NULL },
f043759d 454 { "netid", cmd_node_get_netid, NULL },
725ebfbf 455 { "clusterid", cmd_node_get_netid, NULL },
f043759d
RA
456 { NULL }
457 };
458
459 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
460}
461
462void cmd_node_help(struct cmdl *cmdl)
463{
464 fprintf(stderr,
02573698 465 "Usage: %s node COMMAND [ARGS] ...\n\n"
f043759d
RA
466 "COMMANDS\n"
467 " list - List remote nodes\n"
468 " get - Get local node parameters\n"
24bee3bf
TL
469 " set - Set local node parameters\n"
470 " flush - Flush local node parameters\n",
f043759d
RA
471 cmdl->argv[0]);
472}
473
474int cmd_node(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
475 void *data)
476{
477 const struct cmd cmds[] = {
478 { "list", cmd_node_list, NULL },
479 { "get", cmd_node_get, cmd_node_get_help },
480 { "set", cmd_node_set, cmd_node_set_help },
24bee3bf 481 { "flush", cmd_node_flush, cmd_node_flush_help},
f043759d
RA
482 { NULL }
483 };
484
485 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
486}