]>
Commit | Line | Data |
---|---|---|
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 | ||
27 | static 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 | ||
52 | static 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 | ||
70 | static 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 | ||
101 | static 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 |
126 | static 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 |
160 | static void cmd_node_set_key_help(struct cmdl *cmdl) |
161 | { | |
162 | fprintf(stderr, | |
163 | "Usage: %s node set key KEY [algname ALGNAME] [nodeid NODEID]\n\n" | |
164 | "PROPERTIES\n" | |
165 | " KEY - Symmetric KEY & SALT as a normal or hex string\n" | |
166 | " that consists of two parts:\n" | |
167 | " [KEY: 16, 24 or 32 octets][SALT: 4 octets]\n\n" | |
168 | " algname ALGNAME - Default: \"gcm(aes)\"\n\n" | |
169 | " nodeid NODEID - Own or peer node identity to which the key will\n" | |
170 | " be attached. If not present, the key is a cluster\n" | |
171 | " key!\n\n" | |
172 | "EXAMPLES\n" | |
173 | " %s node set key this_is_a_key16_salt algname \"gcm(aes)\" nodeid node1\n" | |
174 | " %s node set key 0x746869735F69735F615F6B657931365F73616C74 nodeid node2\n\n", | |
175 | cmdl->argv[0], cmdl->argv[0], cmdl->argv[0]); | |
176 | } | |
177 | ||
178 | static int cmd_node_set_key(struct nlmsghdr *nlh, const struct cmd *cmd, | |
179 | struct cmdl *cmdl, void *data) | |
180 | { | |
181 | struct { | |
d5391e18 TL |
182 | union { |
183 | struct tipc_aead_key key; | |
184 | char mem[TIPC_AEAD_KEY_SIZE_MAX]; | |
185 | }; | |
24bee3bf TL |
186 | } input = {}; |
187 | struct opt opts[] = { | |
188 | { "algname", OPT_KEYVAL, NULL }, | |
189 | { "nodeid", OPT_KEYVAL, NULL }, | |
190 | { NULL } | |
191 | }; | |
192 | struct nlattr *nest; | |
193 | struct opt *opt_algname, *opt_nodeid; | |
194 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
195 | uint8_t id[TIPC_NODEID_LEN] = {0,}; | |
196 | int keysize; | |
197 | char *str; | |
198 | ||
199 | if (help_flag) { | |
200 | (cmd->help)(cmdl); | |
201 | return -EINVAL; | |
202 | } | |
203 | ||
204 | if (cmdl->optind >= cmdl->argc) { | |
205 | fprintf(stderr, "error, missing key\n"); | |
206 | return -EINVAL; | |
207 | } | |
208 | ||
209 | /* Get user key */ | |
210 | str = shift_cmdl(cmdl); | |
211 | if (str2key(str, &input.key)) { | |
212 | fprintf(stderr, "error, invalid key input\n"); | |
213 | return -EINVAL; | |
214 | } | |
215 | ||
216 | if (parse_opts(opts, cmdl) < 0) | |
217 | return -EINVAL; | |
218 | ||
219 | /* Get algorithm name, default: "gcm(aes)" */ | |
220 | opt_algname = get_opt(opts, "algname"); | |
221 | if (!opt_algname) | |
222 | strcpy(input.key.alg_name, "gcm(aes)"); | |
223 | else | |
224 | strcpy(input.key.alg_name, opt_algname->val); | |
225 | ||
226 | /* Get node identity */ | |
227 | opt_nodeid = get_opt(opts, "nodeid"); | |
228 | if (opt_nodeid && str2nodeid(opt_nodeid->val, id)) { | |
229 | fprintf(stderr, "error, invalid node identity\n"); | |
230 | return -EINVAL; | |
231 | } | |
232 | ||
233 | /* Init & do the command */ | |
234 | nlh = msg_init(buf, TIPC_NL_KEY_SET); | |
235 | if (!nlh) { | |
236 | fprintf(stderr, "error, message initialisation failed\n"); | |
237 | return -1; | |
238 | } | |
239 | nest = mnl_attr_nest_start(nlh, TIPC_NLA_NODE); | |
240 | keysize = tipc_aead_key_size(&input.key); | |
241 | mnl_attr_put(nlh, TIPC_NLA_NODE_KEY, keysize, &input.key); | |
242 | if (opt_nodeid) | |
243 | mnl_attr_put(nlh, TIPC_NLA_NODE_ID, TIPC_NODEID_LEN, id); | |
244 | mnl_attr_nest_end(nlh, nest); | |
245 | return msg_doit(nlh, NULL, NULL); | |
246 | } | |
247 | ||
248 | static int cmd_node_flush_key(struct nlmsghdr *nlh, const struct cmd *cmd, | |
249 | struct cmdl *cmdl, void *data) | |
250 | { | |
251 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
252 | ||
253 | if (help_flag) { | |
254 | (cmd->help)(cmdl); | |
255 | return -EINVAL; | |
256 | } | |
257 | ||
258 | /* Init & do the command */ | |
259 | nlh = msg_init(buf, TIPC_NL_KEY_FLUSH); | |
260 | if (!nlh) { | |
261 | fprintf(stderr, "error, message initialisation failed\n"); | |
262 | return -1; | |
263 | } | |
264 | return msg_doit(nlh, NULL, NULL); | |
265 | } | |
266 | ||
725ebfbf JM |
267 | static int nodeid_get_cb(const struct nlmsghdr *nlh, void *data) |
268 | { | |
725ebfbf JM |
269 | struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
270 | struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {}; | |
271 | char str[33] = {0,}; | |
272 | uint8_t id[16] = {0,}; | |
273 | uint64_t *w0 = (uint64_t *) &id[0]; | |
274 | uint64_t *w1 = (uint64_t *) &id[8]; | |
725ebfbf | 275 | |
8d05f33a | 276 | mnl_attr_parse(nlh, sizeof(struct genlmsghdr), parse_attrs, info); |
725ebfbf JM |
277 | if (!info[TIPC_NLA_NET]) |
278 | return MNL_CB_ERROR; | |
279 | ||
280 | mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs); | |
281 | if (!attrs[TIPC_NLA_NET_ID]) | |
282 | return MNL_CB_ERROR; | |
283 | ||
284 | *w0 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID]); | |
285 | *w1 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]); | |
286 | nodeid2str(id, str); | |
5947046d JM |
287 | printf("Node Identity Hash\n"); |
288 | printf("%-33s", str); | |
725ebfbf JM |
289 | cmd_node_get_addr(NULL, NULL, NULL, NULL); |
290 | return MNL_CB_OK; | |
291 | } | |
292 | ||
293 | static int cmd_node_get_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd, | |
294 | struct cmdl *cmdl, void *data) | |
295 | { | |
296 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
297 | ||
298 | if (help_flag) { | |
299 | (cmd->help)(cmdl); | |
300 | return -EINVAL; | |
301 | } | |
302 | ||
303 | nlh = msg_init(buf, TIPC_NL_NET_GET); | |
304 | if (!nlh) { | |
305 | fprintf(stderr, "error, message initialisation failed\n"); | |
306 | return -1; | |
307 | } | |
308 | ||
309 | return msg_dumpit(nlh, nodeid_get_cb, NULL); | |
310 | } | |
311 | ||
312 | ||
f043759d RA |
313 | static int netid_get_cb(const struct nlmsghdr *nlh, void *data) |
314 | { | |
f043759d RA |
315 | struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
316 | struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {}; | |
317 | ||
8d05f33a | 318 | mnl_attr_parse(nlh, sizeof(struct genlmsghdr), parse_attrs, info); |
f043759d RA |
319 | if (!info[TIPC_NLA_NET]) |
320 | return MNL_CB_ERROR; | |
321 | ||
322 | mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs); | |
323 | if (!attrs[TIPC_NLA_NET_ID]) | |
324 | return MNL_CB_ERROR; | |
325 | ||
326 | printf("%u\n", mnl_attr_get_u32(attrs[TIPC_NLA_NET_ID])); | |
327 | ||
328 | return MNL_CB_OK; | |
329 | } | |
330 | ||
331 | static int cmd_node_get_netid(struct nlmsghdr *nlh, const struct cmd *cmd, | |
332 | struct cmdl *cmdl, void *data) | |
333 | { | |
334 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
335 | ||
336 | if (help_flag) { | |
337 | (cmd->help)(cmdl); | |
338 | return -EINVAL; | |
339 | } | |
340 | ||
341 | if (!(nlh = msg_init(buf, TIPC_NL_NET_GET))) { | |
342 | fprintf(stderr, "error, message initialisation failed\n"); | |
343 | return -1; | |
344 | } | |
345 | ||
346 | return msg_dumpit(nlh, netid_get_cb, NULL); | |
347 | } | |
348 | ||
349 | static int cmd_node_set_netid(struct nlmsghdr *nlh, const struct cmd *cmd, | |
350 | struct cmdl *cmdl, void *data) | |
351 | { | |
352 | int netid; | |
353 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
354 | struct nlattr *nest; | |
355 | ||
356 | if (help_flag) { | |
357 | (cmd->help)(cmdl); | |
358 | return -EINVAL; | |
359 | } | |
360 | ||
361 | if (!(nlh = msg_init(buf, TIPC_NL_NET_SET))) { | |
362 | fprintf(stderr, "error, message initialisation failed\n"); | |
363 | return -1; | |
364 | } | |
365 | ||
366 | if (cmdl->argc != cmdl->optind + 1) { | |
367 | fprintf(stderr, "Usage: %s node set netid NETID\n", | |
368 | cmdl->argv[0]); | |
369 | return -EINVAL; | |
370 | } | |
371 | netid = atoi(shift_cmdl(cmdl)); | |
372 | ||
373 | nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET); | |
374 | mnl_attr_put_u32(nlh, TIPC_NLA_NET_ID, netid); | |
375 | mnl_attr_nest_end(nlh, nest); | |
376 | ||
377 | return msg_doit(nlh, NULL, NULL); | |
378 | } | |
379 | ||
24bee3bf TL |
380 | static void cmd_node_flush_help(struct cmdl *cmdl) |
381 | { | |
382 | fprintf(stderr, | |
383 | "Usage: %s node flush PROPERTY\n\n" | |
384 | "PROPERTIES\n" | |
385 | " key - Flush all symmetric-keys\n", | |
386 | cmdl->argv[0]); | |
387 | } | |
388 | ||
389 | static int cmd_node_flush(struct nlmsghdr *nlh, const struct cmd *cmd, | |
390 | struct cmdl *cmdl, void *data) | |
391 | { | |
392 | const struct cmd cmds[] = { | |
393 | { "key", cmd_node_flush_key, NULL }, | |
394 | { NULL } | |
395 | }; | |
396 | ||
397 | return run_cmd(nlh, cmd, cmds, cmdl, NULL); | |
398 | } | |
399 | ||
f043759d RA |
400 | static void cmd_node_set_help(struct cmdl *cmdl) |
401 | { | |
402 | fprintf(stderr, | |
403 | "Usage: %s node set PROPERTY\n\n" | |
404 | "PROPERTIES\n" | |
725ebfbf | 405 | " identity NODEID - Set node identity\n" |
24bee3bf TL |
406 | " clusterid CLUSTERID - Set local cluster id\n" |
407 | " key PROPERTY - Set symmetric-key\n", | |
f043759d RA |
408 | cmdl->argv[0]); |
409 | } | |
410 | ||
411 | static int cmd_node_set(struct nlmsghdr *nlh, const struct cmd *cmd, | |
412 | struct cmdl *cmdl, void *data) | |
413 | { | |
414 | const struct cmd cmds[] = { | |
725ebfbf JM |
415 | { "address", cmd_node_set_addr, NULL }, |
416 | { "identity", cmd_node_set_nodeid, NULL }, | |
f043759d | 417 | { "netid", cmd_node_set_netid, NULL }, |
725ebfbf | 418 | { "clusterid", cmd_node_set_netid, NULL }, |
24bee3bf | 419 | { "key", cmd_node_set_key, cmd_node_set_key_help }, |
f043759d RA |
420 | { NULL } |
421 | }; | |
422 | ||
423 | return run_cmd(nlh, cmd, cmds, cmdl, NULL); | |
424 | } | |
425 | ||
426 | static void cmd_node_get_help(struct cmdl *cmdl) | |
427 | { | |
428 | fprintf(stderr, | |
429 | "Usage: %s node get PROPERTY\n\n" | |
430 | "PROPERTIES\n" | |
725ebfbf JM |
431 | " identity - Get node identity\n" |
432 | " clusterid - Get local clusterid\n", | |
f043759d RA |
433 | cmdl->argv[0]); |
434 | } | |
435 | ||
436 | static int cmd_node_get(struct nlmsghdr *nlh, const struct cmd *cmd, | |
437 | struct cmdl *cmdl, void *data) | |
438 | { | |
439 | const struct cmd cmds[] = { | |
440 | { "address", cmd_node_get_addr, NULL }, | |
725ebfbf | 441 | { "identity", cmd_node_get_nodeid, NULL }, |
f043759d | 442 | { "netid", cmd_node_get_netid, NULL }, |
725ebfbf | 443 | { "clusterid", cmd_node_get_netid, NULL }, |
f043759d RA |
444 | { NULL } |
445 | }; | |
446 | ||
447 | return run_cmd(nlh, cmd, cmds, cmdl, NULL); | |
448 | } | |
449 | ||
450 | void cmd_node_help(struct cmdl *cmdl) | |
451 | { | |
452 | fprintf(stderr, | |
02573698 | 453 | "Usage: %s node COMMAND [ARGS] ...\n\n" |
f043759d RA |
454 | "COMMANDS\n" |
455 | " list - List remote nodes\n" | |
456 | " get - Get local node parameters\n" | |
24bee3bf TL |
457 | " set - Set local node parameters\n" |
458 | " flush - Flush local node parameters\n", | |
f043759d RA |
459 | cmdl->argv[0]); |
460 | } | |
461 | ||
462 | int cmd_node(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl, | |
463 | void *data) | |
464 | { | |
465 | const struct cmd cmds[] = { | |
466 | { "list", cmd_node_list, NULL }, | |
467 | { "get", cmd_node_get, cmd_node_get_help }, | |
468 | { "set", cmd_node_set, cmd_node_set_help }, | |
24bee3bf | 469 | { "flush", cmd_node_flush, cmd_node_flush_help}, |
f043759d RA |
470 | { NULL } |
471 | }; | |
472 | ||
473 | return run_cmd(nlh, cmd, cmds, cmdl, NULL); | |
474 | } |