Debian Codegraph @

tipc/node.c

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