]>
git.proxmox.com Git - mirror_iproute2.git/blob - tipc/node.c
2 * node.c TIPC node functionality.
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.
9 * Authors: Richard Alpe <richard.alpe@ericsson.com>
17 #include <linux/tipc_netlink.h>
18 #include <linux/tipc.h>
19 #include <linux/genetlink.h>
20 #include <libmnl/libmnl.h>
27 static int node_list_cb(const struct nlmsghdr
*nlh
, void *data
)
29 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
30 struct nlattr
*attrs
[TIPC_NLA_NODE_MAX
+ 1] = {};
34 mnl_attr_parse(nlh
, sizeof(struct genlmsghdr
), parse_attrs
, info
);
35 if (!info
[TIPC_NLA_NODE
])
38 mnl_attr_parse_nested(info
[TIPC_NLA_NODE
], parse_attrs
, attrs
);
39 if (!attrs
[TIPC_NLA_NODE_ADDR
])
42 addr
= mnl_attr_get_u32(attrs
[TIPC_NLA_NODE_ADDR
]);
43 hash2nodestr(addr
, str
);
44 printf("%-32s %08x ", str
, addr
);
45 if (attrs
[TIPC_NLA_NODE_UP
])
52 static int cmd_node_list(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
53 struct cmdl
*cmdl
, void *data
)
55 char buf
[MNL_SOCKET_BUFFER_SIZE
];
58 fprintf(stderr
, "Usage: %s node list\n", cmdl
->argv
[0]);
62 if (!(nlh
= msg_init(buf
, TIPC_NL_NODE_GET
))) {
63 fprintf(stderr
, "error, message initialisation failed\n");
66 printf("Node Identity Hash State\n");
67 return msg_dumpit(nlh
, node_list_cb
, NULL
);
70 static int cmd_node_set_addr(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
71 struct cmdl
*cmdl
, void *data
)
76 char buf
[MNL_SOCKET_BUFFER_SIZE
];
78 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
79 fprintf(stderr
, "Usage: %s node set address ADDRESS\n",
84 str
= shift_cmdl(cmdl
);
89 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_SET
))) {
90 fprintf(stderr
, "error, message initialisation failed\n");
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
);
98 return msg_doit(nlh
, NULL
, NULL
);
101 static int cmd_node_get_addr(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
102 struct cmdl
*cmdl
, void *data
)
105 socklen_t sz
= sizeof(struct sockaddr_tipc
);
106 struct sockaddr_tipc addr
;
108 sk
= socket(AF_TIPC
, SOCK_RDM
, 0);
110 fprintf(stderr
, "opening TIPC socket: %s\n", strerror(errno
));
114 if (getsockname(sk
, (struct sockaddr
*)&addr
, &sz
) < 0) {
115 fprintf(stderr
, "getting TIPC socket address: %s\n",
122 printf("%08x\n", addr
.addr
.id
.node
);
126 static int cmd_node_set_nodeid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
127 struct cmdl
*cmdl
, void *data
)
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];
136 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
137 fprintf(stderr
, "Usage: %s node set nodeid NODE_ID\n",
142 str
= shift_cmdl(cmdl
);
143 if (str2nodeid(str
, id
)) {
144 fprintf(stderr
, "Invalid node identity\n");
148 nlh
= msg_init(buf
, TIPC_NL_NET_SET
);
150 fprintf(stderr
, "error, message initialisation failed\n");
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
);
160 static void cmd_node_set_key_help(struct cmdl
*cmdl
)
163 "Usage: %s node set key KEY [algname ALGNAME] [nodeid NODEID]\n\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"
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]);
178 static int cmd_node_set_key(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
179 struct cmdl
*cmdl
, void *data
)
183 struct tipc_aead_key key
;
184 char mem
[TIPC_AEAD_KEY_SIZE_MAX
];
187 struct opt opts
[] = {
188 { "algname", OPT_KEYVAL
, NULL
},
189 { "nodeid", OPT_KEYVAL
, NULL
},
193 struct opt
*opt_algname
, *opt_nodeid
;
194 char buf
[MNL_SOCKET_BUFFER_SIZE
];
195 uint8_t id
[TIPC_NODEID_LEN
] = {0,};
204 if (cmdl
->optind
>= cmdl
->argc
) {
205 fprintf(stderr
, "error, missing key\n");
210 str
= shift_cmdl(cmdl
);
211 if (str2key(str
, &input
.key
)) {
212 fprintf(stderr
, "error, invalid key input\n");
216 if (parse_opts(opts
, cmdl
) < 0)
219 /* Get algorithm name, default: "gcm(aes)" */
220 opt_algname
= get_opt(opts
, "algname");
222 strcpy(input
.key
.alg_name
, "gcm(aes)");
224 strcpy(input
.key
.alg_name
, opt_algname
->val
);
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");
233 /* Init & do the command */
234 nlh
= msg_init(buf
, TIPC_NL_KEY_SET
);
236 fprintf(stderr
, "error, message initialisation failed\n");
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
);
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
);
248 static int cmd_node_flush_key(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
249 struct cmdl
*cmdl
, void *data
)
251 char buf
[MNL_SOCKET_BUFFER_SIZE
];
258 /* Init & do the command */
259 nlh
= msg_init(buf
, TIPC_NL_KEY_FLUSH
);
261 fprintf(stderr
, "error, message initialisation failed\n");
264 return msg_doit(nlh
, NULL
, NULL
);
267 static int nodeid_get_cb(const struct nlmsghdr
*nlh
, void *data
)
269 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
270 struct nlattr
*attrs
[TIPC_NLA_NET_MAX
+ 1] = {};
272 uint8_t id
[16] = {0,};
273 uint64_t *w0
= (uint64_t *) &id
[0];
274 uint64_t *w1
= (uint64_t *) &id
[8];
276 mnl_attr_parse(nlh
, sizeof(struct genlmsghdr
), parse_attrs
, info
);
277 if (!info
[TIPC_NLA_NET
])
280 mnl_attr_parse_nested(info
[TIPC_NLA_NET
], parse_attrs
, attrs
);
281 if (!attrs
[TIPC_NLA_NET_ID
])
284 *w0
= mnl_attr_get_u64(attrs
[TIPC_NLA_NET_NODEID
]);
285 *w1
= mnl_attr_get_u64(attrs
[TIPC_NLA_NET_NODEID_W1
]);
287 printf("Node Identity Hash\n");
288 printf("%-33s", str
);
289 cmd_node_get_addr(NULL
, NULL
, NULL
, NULL
);
293 static int cmd_node_get_nodeid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
294 struct cmdl
*cmdl
, void *data
)
296 char buf
[MNL_SOCKET_BUFFER_SIZE
];
303 nlh
= msg_init(buf
, TIPC_NL_NET_GET
);
305 fprintf(stderr
, "error, message initialisation failed\n");
309 return msg_dumpit(nlh
, nodeid_get_cb
, NULL
);
313 static int netid_get_cb(const struct nlmsghdr
*nlh
, void *data
)
315 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
316 struct nlattr
*attrs
[TIPC_NLA_NET_MAX
+ 1] = {};
318 mnl_attr_parse(nlh
, sizeof(struct genlmsghdr
), parse_attrs
, info
);
319 if (!info
[TIPC_NLA_NET
])
322 mnl_attr_parse_nested(info
[TIPC_NLA_NET
], parse_attrs
, attrs
);
323 if (!attrs
[TIPC_NLA_NET_ID
])
326 printf("%u\n", mnl_attr_get_u32(attrs
[TIPC_NLA_NET_ID
]));
331 static int cmd_node_get_netid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
332 struct cmdl
*cmdl
, void *data
)
334 char buf
[MNL_SOCKET_BUFFER_SIZE
];
341 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_GET
))) {
342 fprintf(stderr
, "error, message initialisation failed\n");
346 return msg_dumpit(nlh
, netid_get_cb
, NULL
);
349 static int cmd_node_set_netid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
350 struct cmdl
*cmdl
, void *data
)
353 char buf
[MNL_SOCKET_BUFFER_SIZE
];
361 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_SET
))) {
362 fprintf(stderr
, "error, message initialisation failed\n");
366 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
367 fprintf(stderr
, "Usage: %s node set netid NETID\n",
371 netid
= atoi(shift_cmdl(cmdl
));
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
);
377 return msg_doit(nlh
, NULL
, NULL
);
380 static void cmd_node_flush_help(struct cmdl
*cmdl
)
383 "Usage: %s node flush PROPERTY\n\n"
385 " key - Flush all symmetric-keys\n",
389 static int cmd_node_flush(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
390 struct cmdl
*cmdl
, void *data
)
392 const struct cmd cmds
[] = {
393 { "key", cmd_node_flush_key
, NULL
},
397 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
400 static void cmd_node_set_help(struct cmdl
*cmdl
)
403 "Usage: %s node set PROPERTY\n\n"
405 " identity NODEID - Set node identity\n"
406 " clusterid CLUSTERID - Set local cluster id\n"
407 " key PROPERTY - Set symmetric-key\n",
411 static int cmd_node_set(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
412 struct cmdl
*cmdl
, void *data
)
414 const struct cmd cmds
[] = {
415 { "address", cmd_node_set_addr
, NULL
},
416 { "identity", cmd_node_set_nodeid
, NULL
},
417 { "netid", cmd_node_set_netid
, NULL
},
418 { "clusterid", cmd_node_set_netid
, NULL
},
419 { "key", cmd_node_set_key
, cmd_node_set_key_help
},
423 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
426 static void cmd_node_get_help(struct cmdl
*cmdl
)
429 "Usage: %s node get PROPERTY\n\n"
431 " identity - Get node identity\n"
432 " clusterid - Get local clusterid\n",
436 static int cmd_node_get(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
437 struct cmdl
*cmdl
, void *data
)
439 const struct cmd cmds
[] = {
440 { "address", cmd_node_get_addr
, NULL
},
441 { "identity", cmd_node_get_nodeid
, NULL
},
442 { "netid", cmd_node_get_netid
, NULL
},
443 { "clusterid", cmd_node_get_netid
, NULL
},
447 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
450 void cmd_node_help(struct cmdl
*cmdl
)
453 "Usage: %s node COMMAND [ARGS] ...\n\n"
455 " list - List remote nodes\n"
456 " get - Get local node parameters\n"
457 " set - Set local node parameters\n"
458 " flush - Flush local node parameters\n",
462 int cmd_node(struct nlmsghdr
*nlh
, const struct cmd
*cmd
, struct cmdl
*cmdl
,
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
},
469 { "flush", cmd_node_flush
, cmd_node_flush_help
},
473 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);