| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* bearer.c TIPC bearer functionality. |
| 4 |
* |
| 5 |
* Authors: Richard Alpe <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <netdb.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <arpa/inet.h> |
| 14 |
|
| 15 |
#include <linux/tipc_netlink.h> |
| 16 |
#include <linux/tipc.h> |
| 17 |
#include <linux/genetlink.h> |
| 18 |
#include <linux/if.h> |
| 19 |
#include <libmnl/libmnl.h> |
| 20 |
|
| 21 |
#include "mnl_utils.h" |
| 22 |
#include "utils.h" |
| 23 |
#include "cmdl.h" |
| 24 |
#include "msg.h" |
| 25 |
#include "bearer.h" |
| 26 |
|
| 27 |
#define UDP_PROP_IP 1 |
| 28 |
#define UDP_PROP_PORT 2 |
| 29 |
|
| 30 |
struct cb_data { |
| 31 |
int attr; |
| 32 |
int prop; |
| 33 |
struct nlmsghdr *nlh; |
| 34 |
}; |
| 35 |
|
| 36 |
static void _print_bearer_opts(void) |
| 37 |
{ |
| 38 |
fprintf(stderr, |
| 39 |
"OPTIONS\n" |
| 40 |
" priority - Bearer link priority\n" |
| 41 |
" tolerance - Bearer link tolerance\n" |
| 42 |
" window - Bearer link window\n" |
| 43 |
" mtu - Bearer link mtu\n"); |
| 44 |
} |
| 45 |
|
| 46 |
void print_bearer_media(void) |
| 47 |
{ |
| 48 |
fprintf(stderr, |
| 49 |
"\nMEDIA\n" |
| 50 |
" udp - User Datagram Protocol\n" |
| 51 |
" ib - Infiniband\n" |
| 52 |
" eth - Ethernet\n"); |
| 53 |
} |
| 54 |
|
| 55 |
static void cmd_bearer_enable_l2_help(struct cmdl *cmdl, char *media) |
| 56 |
{ |
| 57 |
fprintf(stderr, |
| 58 |
"Usage: %s bearer enable media %s device DEVICE [OPTIONS]\n" |
| 59 |
"\nOPTIONS\n" |
| 60 |
" domain DOMAIN - Discovery domain\n" |
| 61 |
" priority PRIORITY - Bearer priority\n", |
| 62 |
cmdl->argv[0], media); |
| 63 |
} |
| 64 |
|
| 65 |
static void cmd_bearer_enable_udp_help(struct cmdl *cmdl, char *media) |
| 66 |
{ |
| 67 |
fprintf(stderr, |
| 68 |
"Usage: %s bearer enable [OPTIONS] media %s name NAME [localip IP|device DEVICE] [UDP OPTIONS]\n\n" |
| 69 |
"OPTIONS\n" |
| 70 |
" domain DOMAIN - Discovery domain\n" |
| 71 |
" priority PRIORITY - Bearer priority\n\n" |
| 72 |
"UDP OPTIONS\n" |
| 73 |
" localport PORT - Local UDP port (default 6118)\n" |
| 74 |
" remoteip IP - Remote IP address\n" |
| 75 |
" remoteport PORT - Remote UDP port (default 6118)\n", |
| 76 |
cmdl->argv[0], media); |
| 77 |
} |
| 78 |
|
| 79 |
static int get_netid_cb(const struct nlmsghdr *nlh, void *data) |
| 80 |
{ |
| 81 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 82 |
struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
| 83 |
struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {}; |
| 84 |
int *netid = (int*)data; |
| 85 |
|
| 86 |
mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); |
| 87 |
if (!info[TIPC_NLA_NET]) |
| 88 |
return MNL_CB_ERROR; |
| 89 |
mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs); |
| 90 |
if (!attrs[TIPC_NLA_NET_ID]) |
| 91 |
return MNL_CB_ERROR; |
| 92 |
*netid = mnl_attr_get_u32(attrs[TIPC_NLA_NET_ID]); |
| 93 |
|
| 94 |
return MNL_CB_OK; |
| 95 |
} |
| 96 |
|
| 97 |
static int generate_multicast(short af, char *buf, int bufsize) |
| 98 |
{ |
| 99 |
struct mnlu_gen_socket bearer_nlg; |
| 100 |
struct nlmsghdr *nlh; |
| 101 |
int netid; |
| 102 |
int err = 0; |
| 103 |
|
| 104 |
err = mnlu_gen_socket_open(&bearer_nlg, TIPC_GENL_V2_NAME, |
| 105 |
TIPC_GENL_V2_VERSION); |
| 106 |
if (err) |
| 107 |
return -1; |
| 108 |
|
| 109 |
nlh = mnlu_gen_socket_cmd_prepare(&bearer_nlg, TIPC_NL_NET_GET, |
| 110 |
NLM_F_REQUEST | NLM_F_DUMP); |
| 111 |
if (!nlh) { |
| 112 |
fprintf(stderr, "error, message initialization failed\n"); |
| 113 |
mnlu_gen_socket_close(&bearer_nlg); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
err = mnlu_gen_socket_sndrcv(&bearer_nlg, nlh, get_netid_cb, &netid); |
| 118 |
if (err) { |
| 119 |
fprintf(stderr, "error, failed to fetch TIPC network id from kernel\n"); |
| 120 |
mnlu_gen_socket_close(&bearer_nlg); |
| 121 |
return -EINVAL; |
| 122 |
} |
| 123 |
if (af == AF_INET) |
| 124 |
snprintf(buf, bufsize, "228.0.%u.%u", (netid>>8) & 0xFF, netid & 0xFF); |
| 125 |
else |
| 126 |
snprintf(buf, bufsize, "ff02::%u", netid); |
| 127 |
|
| 128 |
mnlu_gen_socket_close(&bearer_nlg); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static struct ifreq ifr; |
| 133 |
static int nl_dump_req_filter(struct nlmsghdr *nlh, int reqlen) |
| 134 |
{ |
| 135 |
struct ifaddrmsg *ifa = NLMSG_DATA(nlh); |
| 136 |
|
| 137 |
ifa->ifa_index = ifr.ifr_ifindex; |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
static int nl_dump_addr_filter(struct nlmsghdr *nlh, void *arg) |
| 143 |
{ |
| 144 |
struct ifaddrmsg *ifa = NLMSG_DATA(nlh); |
| 145 |
char *r_addr = (char *)arg; |
| 146 |
int len = nlh->nlmsg_len; |
| 147 |
struct rtattr *addr_attr; |
| 148 |
|
| 149 |
if (ifr.ifr_ifindex != ifa->ifa_index) |
| 150 |
return 0; |
| 151 |
|
| 152 |
if (strlen(r_addr) > 0) |
| 153 |
return 0; |
| 154 |
|
| 155 |
addr_attr = parse_rtattr_one(IFA_ADDRESS, IFA_RTA(ifa), |
| 156 |
len - NLMSG_LENGTH(sizeof(*ifa))); |
| 157 |
if (!addr_attr) |
| 158 |
return 0; |
| 159 |
|
| 160 |
if (ifa->ifa_family == AF_INET) { |
| 161 |
struct sockaddr_in ip4addr; |
| 162 |
memcpy(&ip4addr.sin_addr, RTA_DATA(addr_attr), |
| 163 |
sizeof(struct in_addr)); |
| 164 |
inet_ntop(AF_INET, &ip4addr.sin_addr, r_addr, |
| 165 |
INET_ADDRSTRLEN); |
| 166 |
} else if (ifa->ifa_family == AF_INET6) { |
| 167 |
struct sockaddr_in6 ip6addr; |
| 168 |
memcpy(&ip6addr.sin6_addr, RTA_DATA(addr_attr), |
| 169 |
sizeof(struct in6_addr)); |
| 170 |
inet_ntop(AF_INET6, &ip6addr.sin6_addr, r_addr, |
| 171 |
INET6_ADDRSTRLEN); |
| 172 |
} |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
static int cmd_bearer_validate_and_get_addr(const char *name, char *r_addr) |
| 177 |
{ |
| 178 |
struct rtnl_handle rth = { .fd = -1 }; |
| 179 |
int err = -1; |
| 180 |
|
| 181 |
memset(&ifr, 0, sizeof(ifr)); |
| 182 |
if (!name || !r_addr || get_ifname(ifr.ifr_name, name)) |
| 183 |
return err; |
| 184 |
|
| 185 |
ifr.ifr_ifindex = ll_name_to_index(ifr.ifr_name); |
| 186 |
if (!ifr.ifr_ifindex) |
| 187 |
return err; |
| 188 |
|
| 189 |
/* remove from cache */ |
| 190 |
ll_drop_by_index(ifr.ifr_ifindex); |
| 191 |
|
| 192 |
if ((err = rtnl_open(&rth, 0)) < 0) |
| 193 |
return err; |
| 194 |
|
| 195 |
if ((err = rtnl_addrdump_req(&rth, AF_UNSPEC, nl_dump_req_filter)) > 0) |
| 196 |
err = rtnl_dump_filter(&rth, nl_dump_addr_filter, r_addr); |
| 197 |
|
| 198 |
rtnl_close(&rth); |
| 199 |
return err; |
| 200 |
} |
| 201 |
|
| 202 |
static int nl_add_udp_enable_opts(struct nlmsghdr *nlh, struct opt *opts, |
| 203 |
struct cmdl *cmdl) |
| 204 |
{ |
| 205 |
int err; |
| 206 |
struct opt *opt; |
| 207 |
struct nlattr *nest; |
| 208 |
char buf[INET6_ADDRSTRLEN]; |
| 209 |
char *locport = "6118"; |
| 210 |
char *remport = "6118"; |
| 211 |
char *locip = NULL; |
| 212 |
char *remip = NULL; |
| 213 |
struct addrinfo *loc = NULL; |
| 214 |
struct addrinfo *rem = NULL; |
| 215 |
struct addrinfo hints = { |
| 216 |
.ai_family = AF_UNSPEC, |
| 217 |
.ai_socktype = SOCK_DGRAM |
| 218 |
}; |
| 219 |
char addr[INET6_ADDRSTRLEN] = {0}; |
| 220 |
|
| 221 |
opt = get_opt(opts, "device"); |
| 222 |
if (opt && cmd_bearer_validate_and_get_addr(opt->val, addr) < 0) { |
| 223 |
fprintf(stderr, "error, no device name available\n"); |
| 224 |
return -EINVAL; |
| 225 |
} |
| 226 |
|
| 227 |
if (strlen(addr) > 0) { |
| 228 |
locip = addr; |
| 229 |
} else { |
| 230 |
opt = get_opt(opts, "localip"); |
| 231 |
if (!opt) { |
| 232 |
fprintf(stderr, "error, udp bearer localip/device missing\n"); |
| 233 |
cmd_bearer_enable_udp_help(cmdl, "udp"); |
| 234 |
return -EINVAL; |
| 235 |
} |
| 236 |
locip = opt->val; |
| 237 |
} |
| 238 |
|
| 239 |
if ((opt = get_opt(opts, "remoteip"))) |
| 240 |
remip = opt->val; |
| 241 |
|
| 242 |
if ((opt = get_opt(opts, "localport"))) |
| 243 |
locport = opt->val; |
| 244 |
|
| 245 |
if ((opt = get_opt(opts, "remoteport"))) |
| 246 |
remport = opt->val; |
| 247 |
|
| 248 |
if ((err = getaddrinfo(locip, locport, &hints, &loc))) { |
| 249 |
fprintf(stderr, "UDP local address error: %s\n", |
| 250 |
gai_strerror(err)); |
| 251 |
return err; |
| 252 |
} |
| 253 |
|
| 254 |
if (!remip) { |
| 255 |
if (generate_multicast(loc->ai_family, buf, sizeof(buf))) { |
| 256 |
fprintf(stderr, "Failed to generate multicast address\n"); |
| 257 |
freeaddrinfo(loc); |
| 258 |
return -EINVAL; |
| 259 |
} |
| 260 |
remip = buf; |
| 261 |
} |
| 262 |
|
| 263 |
if ((err = getaddrinfo(remip, remport, &hints, &rem))) { |
| 264 |
fprintf(stderr, "UDP remote address error: %s\n", |
| 265 |
gai_strerror(err)); |
| 266 |
freeaddrinfo(loc); |
| 267 |
return err; |
| 268 |
} |
| 269 |
|
| 270 |
if (rem->ai_family != loc->ai_family) { |
| 271 |
fprintf(stderr, "UDP local and remote AF mismatch\n"); |
| 272 |
freeaddrinfo(rem); |
| 273 |
freeaddrinfo(loc); |
| 274 |
return -EINVAL; |
| 275 |
} |
| 276 |
|
| 277 |
nest = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER_UDP_OPTS); |
| 278 |
mnl_attr_put(nlh, TIPC_NLA_UDP_LOCAL, loc->ai_addrlen, loc->ai_addr); |
| 279 |
mnl_attr_put(nlh, TIPC_NLA_UDP_REMOTE, rem->ai_addrlen, rem->ai_addr); |
| 280 |
mnl_attr_nest_end(nlh, nest); |
| 281 |
|
| 282 |
freeaddrinfo(rem); |
| 283 |
freeaddrinfo(loc); |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static char *cmd_get_media_type(const struct cmd *cmd, struct cmdl *cmdl, |
| 289 |
struct opt *opts) |
| 290 |
{ |
| 291 |
struct opt *opt = get_opt(opts, "media"); |
| 292 |
|
| 293 |
if (!opt) { |
| 294 |
if (help_flag) |
| 295 |
(cmd->help)(cmdl); |
| 296 |
else |
| 297 |
fprintf(stderr, "error, missing bearer media\n"); |
| 298 |
return NULL; |
| 299 |
} |
| 300 |
return opt->val; |
| 301 |
} |
| 302 |
|
| 303 |
static int nl_add_bearer_name(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 304 |
struct cmdl *cmdl, struct opt *opts, |
| 305 |
const struct tipc_sup_media *sup_media) |
| 306 |
{ |
| 307 |
char bname[TIPC_MAX_BEARER_NAME]; |
| 308 |
int err; |
| 309 |
|
| 310 |
if ((err = cmd_get_unique_bearer_name(cmd, cmdl, opts, bname, sup_media))) |
| 311 |
return err; |
| 312 |
|
| 313 |
mnl_attr_put_strz(nlh, TIPC_NLA_BEARER_NAME, bname); |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
int cmd_get_unique_bearer_name(const struct cmd *cmd, struct cmdl *cmdl, |
| 318 |
struct opt *opts, char *bname, |
| 319 |
const struct tipc_sup_media *sup_media) |
| 320 |
{ |
| 321 |
char *media; |
| 322 |
char *identifier; |
| 323 |
struct opt *opt; |
| 324 |
const struct tipc_sup_media *entry; |
| 325 |
|
| 326 |
if (!(media = cmd_get_media_type(cmd, cmdl, opts))) |
| 327 |
return -EINVAL; |
| 328 |
|
| 329 |
for (entry = sup_media; entry->media; entry++) { |
| 330 |
if (strcmp(entry->media, media)) |
| 331 |
continue; |
| 332 |
|
| 333 |
if (!(opt = get_opt(opts, entry->identifier))) { |
| 334 |
if (help_flag) |
| 335 |
(entry->help)(cmdl, media); |
| 336 |
else |
| 337 |
fprintf(stderr, "error, missing bearer %s\n", |
| 338 |
entry->identifier); |
| 339 |
return -EINVAL; |
| 340 |
} |
| 341 |
|
| 342 |
identifier = opt->val; |
| 343 |
snprintf(bname, TIPC_MAX_BEARER_NAME, "%s:%s", media, identifier); |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
fprintf(stderr, "error, invalid media type %s\n", media); |
| 349 |
|
| 350 |
return -EINVAL; |
| 351 |
} |
| 352 |
|
| 353 |
static void cmd_bearer_add_udp_help(struct cmdl *cmdl, char *media) |
| 354 |
{ |
| 355 |
fprintf(stderr, "Usage: %s bearer add media %s name NAME remoteip REMOTEIP\n\n", |
| 356 |
cmdl->argv[0], media); |
| 357 |
} |
| 358 |
|
| 359 |
static void cmd_bearer_add_help(struct cmdl *cmdl) |
| 360 |
{ |
| 361 |
fprintf(stderr, "Usage: %s bearer add media udp name NAME remoteip REMOTEIP\n", |
| 362 |
cmdl->argv[0]); |
| 363 |
} |
| 364 |
|
| 365 |
static int udp_bearer_add(struct nlmsghdr *nlh, struct opt *opts, |
| 366 |
struct cmdl *cmdl) |
| 367 |
{ |
| 368 |
int err; |
| 369 |
struct opt *opt; |
| 370 |
struct nlattr *opts_nest; |
| 371 |
char *remport = "6118"; |
| 372 |
|
| 373 |
opts_nest = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER_UDP_OPTS); |
| 374 |
|
| 375 |
if ((opt = get_opt(opts, "remoteport"))) |
| 376 |
remport = opt->val; |
| 377 |
|
| 378 |
if ((opt = get_opt(opts, "remoteip"))) { |
| 379 |
char *ip = opt->val; |
| 380 |
struct addrinfo *addr = NULL; |
| 381 |
struct addrinfo hints = { |
| 382 |
.ai_family = AF_UNSPEC, |
| 383 |
.ai_socktype = SOCK_DGRAM |
| 384 |
}; |
| 385 |
|
| 386 |
if ((err = getaddrinfo(ip, remport, &hints, &addr))) { |
| 387 |
fprintf(stderr, "UDP address error: %s\n", |
| 388 |
gai_strerror(err)); |
| 389 |
freeaddrinfo(addr); |
| 390 |
return err; |
| 391 |
} |
| 392 |
|
| 393 |
mnl_attr_put(nlh, TIPC_NLA_UDP_REMOTE, addr->ai_addrlen, |
| 394 |
addr->ai_addr); |
| 395 |
freeaddrinfo(addr); |
| 396 |
} else { |
| 397 |
fprintf(stderr, "error, missing remoteip\n"); |
| 398 |
return -EINVAL; |
| 399 |
} |
| 400 |
mnl_attr_nest_end(nlh, opts_nest); |
| 401 |
|
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
static int cmd_bearer_add_media(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 406 |
struct cmdl *cmdl, void *data) |
| 407 |
{ |
| 408 |
int err; |
| 409 |
char *media; |
| 410 |
struct opt *opt; |
| 411 |
struct nlattr *attrs; |
| 412 |
struct opt opts[] = { |
| 413 |
{ "remoteip", OPT_KEYVAL, NULL }, |
| 414 |
{ "remoteport", OPT_KEYVAL, NULL }, |
| 415 |
{ "name", OPT_KEYVAL, NULL }, |
| 416 |
{ "media", OPT_KEYVAL, NULL }, |
| 417 |
{ NULL } |
| 418 |
}; |
| 419 |
const struct tipc_sup_media sup_media[] = { |
| 420 |
{ "udp", "name", cmd_bearer_add_udp_help}, |
| 421 |
{ NULL, }, |
| 422 |
}; |
| 423 |
|
| 424 |
/* Rewind optind to include media in the option list */ |
| 425 |
cmdl->optind--; |
| 426 |
if (parse_opts(opts, cmdl) < 0) |
| 427 |
return -EINVAL; |
| 428 |
|
| 429 |
if (!(opt = get_opt(opts, "media"))) { |
| 430 |
fprintf(stderr, "error, missing media value\n"); |
| 431 |
return -EINVAL; |
| 432 |
} |
| 433 |
media = opt->val; |
| 434 |
|
| 435 |
if (strcmp(media, "udp") != 0) { |
| 436 |
fprintf(stderr, "error, no \"%s\" media specific options available\n", |
| 437 |
media); |
| 438 |
return -EINVAL; |
| 439 |
} |
| 440 |
if (!(opt = get_opt(opts, "name"))) { |
| 441 |
fprintf(stderr, "error, missing media name\n"); |
| 442 |
return -EINVAL; |
| 443 |
} |
| 444 |
|
| 445 |
nlh = msg_init(TIPC_NL_BEARER_ADD); |
| 446 |
if (!nlh) { |
| 447 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 448 |
return -1; |
| 449 |
} |
| 450 |
|
| 451 |
attrs = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER); |
| 452 |
err = nl_add_bearer_name(nlh, cmd, cmdl, opts, sup_media); |
| 453 |
if (err) |
| 454 |
return err; |
| 455 |
|
| 456 |
err = udp_bearer_add(nlh, opts, cmdl); |
| 457 |
if (err) |
| 458 |
return err; |
| 459 |
|
| 460 |
mnl_attr_nest_end(nlh, attrs); |
| 461 |
|
| 462 |
return msg_doit(nlh, NULL, NULL); |
| 463 |
} |
| 464 |
|
| 465 |
static int cmd_bearer_add(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 466 |
struct cmdl *cmdl, void *data) |
| 467 |
{ |
| 468 |
const struct cmd cmds[] = { |
| 469 |
{ "media", cmd_bearer_add_media, cmd_bearer_add_help }, |
| 470 |
{ NULL } |
| 471 |
}; |
| 472 |
|
| 473 |
return run_cmd(nlh, cmd, cmds, cmdl, NULL); |
| 474 |
} |
| 475 |
|
| 476 |
static void cmd_bearer_enable_help(struct cmdl *cmdl) |
| 477 |
{ |
| 478 |
fprintf(stderr, |
| 479 |
"Usage: %s bearer enable [OPTIONS] media MEDIA ARGS...\n\n" |
| 480 |
"OPTIONS\n" |
| 481 |
" domain DOMAIN - Discovery domain\n" |
| 482 |
" priority PRIORITY - Bearer priority\n", |
| 483 |
cmdl->argv[0]); |
| 484 |
print_bearer_media(); |
| 485 |
} |
| 486 |
|
| 487 |
static int cmd_bearer_enable(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 488 |
struct cmdl *cmdl, void *data) |
| 489 |
{ |
| 490 |
int err; |
| 491 |
struct opt *opt; |
| 492 |
struct nlattr *nest; |
| 493 |
struct opt opts[] = { |
| 494 |
{ "device", OPT_KEYVAL, NULL }, |
| 495 |
{ "domain", OPT_KEYVAL, NULL }, |
| 496 |
{ "localip", OPT_KEYVAL, NULL }, |
| 497 |
{ "localport", OPT_KEYVAL, NULL }, |
| 498 |
{ "media", OPT_KEYVAL, NULL }, |
| 499 |
{ "name", OPT_KEYVAL, NULL }, |
| 500 |
{ "priority", OPT_KEYVAL, NULL }, |
| 501 |
{ "remoteip", OPT_KEYVAL, NULL }, |
| 502 |
{ "remoteport", OPT_KEYVAL, NULL }, |
| 503 |
{ NULL } |
| 504 |
}; |
| 505 |
struct tipc_sup_media sup_media[] = { |
| 506 |
{ "udp", "name", cmd_bearer_enable_udp_help}, |
| 507 |
{ "eth", "device", cmd_bearer_enable_l2_help }, |
| 508 |
{ "ib", "device", cmd_bearer_enable_l2_help }, |
| 509 |
{ NULL, }, |
| 510 |
}; |
| 511 |
|
| 512 |
if (parse_opts(opts, cmdl) < 0) { |
| 513 |
if (help_flag) |
| 514 |
(cmd->help)(cmdl); |
| 515 |
return -EINVAL; |
| 516 |
} |
| 517 |
|
| 518 |
nlh = msg_init(TIPC_NL_BEARER_ENABLE); |
| 519 |
if (!nlh) { |
| 520 |
fprintf(stderr, "error: message initialisation failed\n"); |
| 521 |
return -1; |
| 522 |
} |
| 523 |
nest = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER); |
| 524 |
|
| 525 |
if ((opt = get_opt(opts, "domain"))) |
| 526 |
mnl_attr_put_u32(nlh, TIPC_NLA_BEARER_DOMAIN, atoi(opt->val)); |
| 527 |
|
| 528 |
if ((opt = get_opt(opts, "priority"))) { |
| 529 |
struct nlattr *props; |
| 530 |
|
| 531 |
props = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER_PROP); |
| 532 |
mnl_attr_put_u32(nlh, TIPC_NLA_PROP_PRIO, atoi(opt->val)); |
| 533 |
mnl_attr_nest_end(nlh, props); |
| 534 |
} |
| 535 |
|
| 536 |
err = nl_add_bearer_name(nlh, cmd, cmdl, opts, sup_media); |
| 537 |
if (err) |
| 538 |
return err; |
| 539 |
|
| 540 |
opt = get_opt(opts, "media"); |
| 541 |
if (opt && strcmp(opt->val, "udp") == 0) { |
| 542 |
err = nl_add_udp_enable_opts(nlh, opts, cmdl); |
| 543 |
if (err) |
| 544 |
return err; |
| 545 |
} |
| 546 |
mnl_attr_nest_end(nlh, nest); |
| 547 |
|
| 548 |
return msg_doit(nlh, NULL, NULL); |
| 549 |
} |
| 550 |
|
| 551 |
static void cmd_bearer_disable_l2_help(struct cmdl *cmdl, char *media) |
| 552 |
{ |
| 553 |
fprintf(stderr, "Usage: %s bearer disable media %s device DEVICE\n", |
| 554 |
cmdl->argv[0], media); |
| 555 |
} |
| 556 |
|
| 557 |
static void cmd_bearer_disable_udp_help(struct cmdl *cmdl, char *media) |
| 558 |
{ |
| 559 |
fprintf(stderr, "Usage: %s bearer disable media %s name NAME\n", |
| 560 |
cmdl->argv[0], media); |
| 561 |
} |
| 562 |
|
| 563 |
static void cmd_bearer_disable_help(struct cmdl *cmdl) |
| 564 |
{ |
| 565 |
fprintf(stderr, "Usage: %s bearer disable media MEDIA ARGS...\n", |
| 566 |
cmdl->argv[0]); |
| 567 |
print_bearer_media(); |
| 568 |
} |
| 569 |
|
| 570 |
static int cmd_bearer_disable(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 571 |
struct cmdl *cmdl, void *data) |
| 572 |
{ |
| 573 |
int err; |
| 574 |
struct nlattr *nest; |
| 575 |
struct opt opts[] = { |
| 576 |
{ "device", OPT_KEYVAL, NULL }, |
| 577 |
{ "name", OPT_KEYVAL, NULL }, |
| 578 |
{ "media", OPT_KEYVAL, NULL }, |
| 579 |
{ NULL } |
| 580 |
}; |
| 581 |
struct tipc_sup_media sup_media[] = { |
| 582 |
{ "udp", "name", cmd_bearer_disable_udp_help}, |
| 583 |
{ "eth", "device", cmd_bearer_disable_l2_help }, |
| 584 |
{ "ib", "device", cmd_bearer_disable_l2_help }, |
| 585 |
{ NULL, }, |
| 586 |
}; |
| 587 |
|
| 588 |
if (parse_opts(opts, cmdl) < 0) { |
| 589 |
if (help_flag) |
| 590 |
(cmd->help)(cmdl); |
| 591 |
return -EINVAL; |
| 592 |
} |
| 593 |
|
| 594 |
nlh = msg_init(TIPC_NL_BEARER_DISABLE); |
| 595 |
if (!nlh) { |
| 596 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 597 |
return -1; |
| 598 |
} |
| 599 |
|
| 600 |
nest = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER); |
| 601 |
err = nl_add_bearer_name(nlh, cmd, cmdl, opts, sup_media); |
| 602 |
if (err) |
| 603 |
return err; |
| 604 |
mnl_attr_nest_end(nlh, nest); |
| 605 |
|
| 606 |
return msg_doit(nlh, NULL, NULL); |
| 607 |
|
| 608 |
} |
| 609 |
|
| 610 |
static void cmd_bearer_set_help(struct cmdl *cmdl) |
| 611 |
{ |
| 612 |
fprintf(stderr, "Usage: %s bearer set OPTION media MEDIA ARGS...\n", |
| 613 |
cmdl->argv[0]); |
| 614 |
_print_bearer_opts(); |
| 615 |
print_bearer_media(); |
| 616 |
} |
| 617 |
|
| 618 |
static void cmd_bearer_set_udp_help(struct cmdl *cmdl, char *media) |
| 619 |
{ |
| 620 |
fprintf(stderr, "Usage: %s bearer set OPTION media %s name NAME\n\n", |
| 621 |
cmdl->argv[0], media); |
| 622 |
_print_bearer_opts(); |
| 623 |
} |
| 624 |
|
| 625 |
static void cmd_bearer_set_l2_help(struct cmdl *cmdl, char *media) |
| 626 |
{ |
| 627 |
fprintf(stderr, |
| 628 |
"Usage: %s bearer set [OPTION]... media %s device DEVICE\n", |
| 629 |
cmdl->argv[0], media); |
| 630 |
_print_bearer_opts(); |
| 631 |
} |
| 632 |
|
| 633 |
static int cmd_bearer_set_prop(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 634 |
struct cmdl *cmdl, void *data) |
| 635 |
{ |
| 636 |
int err; |
| 637 |
int val; |
| 638 |
int prop; |
| 639 |
struct nlattr *props; |
| 640 |
struct nlattr *attrs; |
| 641 |
struct opt opts[] = { |
| 642 |
{ "device", OPT_KEYVAL, NULL }, |
| 643 |
{ "media", OPT_KEYVAL, NULL }, |
| 644 |
{ "name", OPT_KEYVAL, NULL }, |
| 645 |
{ NULL } |
| 646 |
}; |
| 647 |
struct tipc_sup_media sup_media[] = { |
| 648 |
{ "udp", "name", cmd_bearer_set_udp_help}, |
| 649 |
{ "eth", "device", cmd_bearer_set_l2_help }, |
| 650 |
{ "ib", "device", cmd_bearer_set_l2_help }, |
| 651 |
{ NULL, }, |
| 652 |
}; |
| 653 |
|
| 654 |
if (strcmp(cmd->cmd, "priority") == 0) |
| 655 |
prop = TIPC_NLA_PROP_PRIO; |
| 656 |
else if ((strcmp(cmd->cmd, "tolerance") == 0)) |
| 657 |
prop = TIPC_NLA_PROP_TOL; |
| 658 |
else if ((strcmp(cmd->cmd, "window") == 0)) |
| 659 |
prop = TIPC_NLA_PROP_WIN; |
| 660 |
else if ((strcmp(cmd->cmd, "mtu") == 0)) |
| 661 |
prop = TIPC_NLA_PROP_MTU; |
| 662 |
else |
| 663 |
return -EINVAL; |
| 664 |
|
| 665 |
if (cmdl->optind >= cmdl->argc) { |
| 666 |
fprintf(stderr, "error, missing value\n"); |
| 667 |
return -EINVAL; |
| 668 |
} |
| 669 |
val = atoi(shift_cmdl(cmdl)); |
| 670 |
|
| 671 |
if (parse_opts(opts, cmdl) < 0) |
| 672 |
return -EINVAL; |
| 673 |
|
| 674 |
if (prop == TIPC_NLA_PROP_MTU) { |
| 675 |
char *media = cmd_get_media_type(cmd, cmdl, opts); |
| 676 |
|
| 677 |
if (!media) |
| 678 |
return -EINVAL; |
| 679 |
else if (strcmp(media, "udp")) { |
| 680 |
fprintf(stderr, "error, not supported for media\n"); |
| 681 |
return -EINVAL; |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
nlh = msg_init(TIPC_NL_BEARER_SET); |
| 686 |
if (!nlh) { |
| 687 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 688 |
return -1; |
| 689 |
} |
| 690 |
attrs = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER); |
| 691 |
|
| 692 |
props = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER_PROP); |
| 693 |
mnl_attr_put_u32(nlh, prop, val); |
| 694 |
mnl_attr_nest_end(nlh, props); |
| 695 |
|
| 696 |
err = nl_add_bearer_name(nlh, cmd, cmdl, opts, sup_media); |
| 697 |
if (err) |
| 698 |
return err; |
| 699 |
|
| 700 |
mnl_attr_nest_end(nlh, attrs); |
| 701 |
|
| 702 |
return msg_doit(nlh, NULL, NULL); |
| 703 |
} |
| 704 |
|
| 705 |
static int cmd_bearer_set(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 706 |
struct cmdl *cmdl, void *data) |
| 707 |
{ |
| 708 |
const struct cmd cmds[] = { |
| 709 |
{ "priority", cmd_bearer_set_prop, cmd_bearer_set_help }, |
| 710 |
{ "tolerance", cmd_bearer_set_prop, cmd_bearer_set_help }, |
| 711 |
{ "window", cmd_bearer_set_prop, cmd_bearer_set_help }, |
| 712 |
{ "mtu", cmd_bearer_set_prop, cmd_bearer_set_help }, |
| 713 |
{ NULL } |
| 714 |
}; |
| 715 |
|
| 716 |
return run_cmd(nlh, cmd, cmds, cmdl, NULL); |
| 717 |
} |
| 718 |
|
| 719 |
static void cmd_bearer_get_help(struct cmdl *cmdl) |
| 720 |
{ |
| 721 |
fprintf(stderr, "Usage: %s bearer get [OPTION] media MEDIA ARGS...\n", |
| 722 |
cmdl->argv[0]); |
| 723 |
_print_bearer_opts(); |
| 724 |
print_bearer_media(); |
| 725 |
} |
| 726 |
|
| 727 |
static void cmd_bearer_get_udp_help(struct cmdl *cmdl, char *media) |
| 728 |
{ |
| 729 |
fprintf(stderr, "Usage: %s bearer get [OPTION] media %s name NAME [UDP OPTIONS]\n\n", |
| 730 |
cmdl->argv[0], media); |
| 731 |
fprintf(stderr, |
| 732 |
"UDP OPTIONS\n" |
| 733 |
" remoteip - Remote ip address\n" |
| 734 |
" remoteport - Remote port\n" |
| 735 |
" localip - Local ip address\n" |
| 736 |
" localport - Local port\n\n"); |
| 737 |
_print_bearer_opts(); |
| 738 |
} |
| 739 |
|
| 740 |
static void cmd_bearer_get_l2_help(struct cmdl *cmdl, char *media) |
| 741 |
{ |
| 742 |
fprintf(stderr, |
| 743 |
"Usage: %s bearer get OPTION media %s device DEVICE\n", |
| 744 |
cmdl->argv[0], media); |
| 745 |
_print_bearer_opts(); |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
static int bearer_dump_udp_cb(const struct nlmsghdr *nlh, void *data) |
| 750 |
{ |
| 751 |
struct sockaddr_storage *addr; |
| 752 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 753 |
struct nlattr *info[TIPC_NLA_UDP_MAX + 1] = {}; |
| 754 |
|
| 755 |
mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); |
| 756 |
|
| 757 |
if (!info[TIPC_NLA_UDP_REMOTE]) |
| 758 |
return MNL_CB_ERROR; |
| 759 |
|
| 760 |
addr = mnl_attr_get_payload(info[TIPC_NLA_UDP_REMOTE]); |
| 761 |
|
| 762 |
if (addr->ss_family == AF_INET) { |
| 763 |
struct sockaddr_in *ipv4 = (struct sockaddr_in *) addr; |
| 764 |
|
| 765 |
printf("%s\n", inet_ntoa(ipv4->sin_addr)); |
| 766 |
} else if (addr->ss_family == AF_INET6) { |
| 767 |
char straddr[INET6_ADDRSTRLEN]; |
| 768 |
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *) addr; |
| 769 |
|
| 770 |
if (!inet_ntop(AF_INET6, &ipv6->sin6_addr, straddr, |
| 771 |
sizeof(straddr))) { |
| 772 |
fprintf(stderr, "error, parsing IPv6 addr\n"); |
| 773 |
return MNL_CB_ERROR; |
| 774 |
} |
| 775 |
printf("%s\n", straddr); |
| 776 |
|
| 777 |
} else { |
| 778 |
return MNL_CB_ERROR; |
| 779 |
} |
| 780 |
|
| 781 |
return MNL_CB_OK; |
| 782 |
} |
| 783 |
|
| 784 |
static int bearer_get_udp_cb(const struct nlmsghdr *nlh, void *data) |
| 785 |
{ |
| 786 |
struct cb_data *cb_data = (struct cb_data *) data; |
| 787 |
struct sockaddr_storage *addr; |
| 788 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 789 |
struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
| 790 |
struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1] = {}; |
| 791 |
struct nlattr *opts[TIPC_NLA_UDP_MAX + 1] = {}; |
| 792 |
|
| 793 |
mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); |
| 794 |
if (!info[TIPC_NLA_BEARER]) |
| 795 |
return MNL_CB_ERROR; |
| 796 |
|
| 797 |
mnl_attr_parse_nested(info[TIPC_NLA_BEARER], parse_attrs, attrs); |
| 798 |
if (!attrs[TIPC_NLA_BEARER_UDP_OPTS]) |
| 799 |
return MNL_CB_ERROR; |
| 800 |
|
| 801 |
mnl_attr_parse_nested(attrs[TIPC_NLA_BEARER_UDP_OPTS], parse_attrs, opts); |
| 802 |
if (!opts[TIPC_NLA_UDP_LOCAL]) |
| 803 |
return MNL_CB_ERROR; |
| 804 |
|
| 805 |
if ((cb_data->attr == TIPC_NLA_UDP_REMOTE) && |
| 806 |
(cb_data->prop == UDP_PROP_IP) && |
| 807 |
opts[TIPC_NLA_UDP_MULTI_REMOTEIP]) { |
| 808 |
struct mnlu_gen_socket bearer_nlg; |
| 809 |
struct nlattr *attr; |
| 810 |
struct nlmsghdr *h; |
| 811 |
const char *bname; |
| 812 |
int err = 0; |
| 813 |
|
| 814 |
err = mnlu_gen_socket_open(&bearer_nlg, TIPC_GENL_V2_NAME, |
| 815 |
TIPC_GENL_V2_VERSION); |
| 816 |
if (err) |
| 817 |
return -1; |
| 818 |
|
| 819 |
h = mnlu_gen_socket_cmd_prepare(&bearer_nlg, |
| 820 |
TIPC_NL_UDP_GET_REMOTEIP, |
| 821 |
NLM_F_REQUEST | NLM_F_DUMP); |
| 822 |
if (!h) { |
| 823 |
fprintf(stderr, "error, message initialization failed\n"); |
| 824 |
mnlu_gen_socket_close(&bearer_nlg); |
| 825 |
return -1; |
| 826 |
} |
| 827 |
|
| 828 |
attr = mnl_attr_nest_start(h, TIPC_NLA_BEARER); |
| 829 |
bname = mnl_attr_get_str(attrs[TIPC_NLA_BEARER_NAME]); |
| 830 |
mnl_attr_put_strz(h, TIPC_NLA_BEARER_NAME, bname); |
| 831 |
mnl_attr_nest_end(h, attr); |
| 832 |
|
| 833 |
err = mnlu_gen_socket_sndrcv(&bearer_nlg, h, |
| 834 |
bearer_dump_udp_cb, NULL); |
| 835 |
mnlu_gen_socket_close(&bearer_nlg); |
| 836 |
return err; |
| 837 |
} |
| 838 |
|
| 839 |
addr = mnl_attr_get_payload(opts[cb_data->attr]); |
| 840 |
|
| 841 |
if (addr->ss_family == AF_INET) { |
| 842 |
struct sockaddr_in *ipv4 = (struct sockaddr_in *) addr; |
| 843 |
|
| 844 |
switch (cb_data->prop) { |
| 845 |
case UDP_PROP_IP: |
| 846 |
printf("%s\n", inet_ntoa(ipv4->sin_addr)); |
| 847 |
break; |
| 848 |
case UDP_PROP_PORT: |
| 849 |
printf("%u\n", ntohs(ipv4->sin_port)); |
| 850 |
break; |
| 851 |
default: |
| 852 |
return MNL_CB_ERROR; |
| 853 |
} |
| 854 |
|
| 855 |
} else if (addr->ss_family == AF_INET6) { |
| 856 |
char straddr[INET6_ADDRSTRLEN]; |
| 857 |
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *) addr; |
| 858 |
|
| 859 |
switch (cb_data->prop) { |
| 860 |
case UDP_PROP_IP: |
| 861 |
if (!inet_ntop(AF_INET6, &ipv6->sin6_addr, straddr, |
| 862 |
sizeof(straddr))) { |
| 863 |
fprintf(stderr, "error, parsing IPv6 addr\n"); |
| 864 |
return MNL_CB_ERROR; |
| 865 |
} |
| 866 |
printf("%s\n", straddr); |
| 867 |
break; |
| 868 |
case UDP_PROP_PORT: |
| 869 |
printf("%u\n", ntohs(ipv6->sin6_port)); |
| 870 |
break; |
| 871 |
default: |
| 872 |
return MNL_CB_ERROR; |
| 873 |
} |
| 874 |
|
| 875 |
} else { |
| 876 |
return MNL_CB_ERROR; |
| 877 |
} |
| 878 |
|
| 879 |
return MNL_CB_OK; |
| 880 |
} |
| 881 |
|
| 882 |
static int bearer_get_cb(const struct nlmsghdr *nlh, void *data) |
| 883 |
{ |
| 884 |
int *prop = data; |
| 885 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 886 |
struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
| 887 |
struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1] = {}; |
| 888 |
struct nlattr *props[TIPC_NLA_PROP_MAX + 1] = {}; |
| 889 |
|
| 890 |
mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); |
| 891 |
if (!info[TIPC_NLA_BEARER]) |
| 892 |
return MNL_CB_ERROR; |
| 893 |
|
| 894 |
mnl_attr_parse_nested(info[TIPC_NLA_BEARER], parse_attrs, attrs); |
| 895 |
if (!attrs[TIPC_NLA_BEARER_PROP]) |
| 896 |
return MNL_CB_ERROR; |
| 897 |
|
| 898 |
mnl_attr_parse_nested(attrs[TIPC_NLA_BEARER_PROP], parse_attrs, props); |
| 899 |
if (!props[*prop]) |
| 900 |
return MNL_CB_ERROR; |
| 901 |
|
| 902 |
printf("%u\n", mnl_attr_get_u32(props[*prop])); |
| 903 |
|
| 904 |
return MNL_CB_OK; |
| 905 |
} |
| 906 |
|
| 907 |
static int cmd_bearer_get_media(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 908 |
struct cmdl *cmdl, void *data) |
| 909 |
{ |
| 910 |
int err; |
| 911 |
char *media; |
| 912 |
struct opt *opt; |
| 913 |
struct cb_data cb_data = {0}; |
| 914 |
struct nlattr *attrs; |
| 915 |
struct opt opts[] = { |
| 916 |
{ "localip", OPT_KEY, NULL }, |
| 917 |
{ "localport", OPT_KEY, NULL }, |
| 918 |
{ "remoteip", OPT_KEY, NULL }, |
| 919 |
{ "remoteport", OPT_KEY, NULL }, |
| 920 |
{ "name", OPT_KEYVAL, NULL }, |
| 921 |
{ "media", OPT_KEYVAL, NULL }, |
| 922 |
{ NULL } |
| 923 |
}; |
| 924 |
struct tipc_sup_media sup_media[] = { |
| 925 |
{ "udp", "name", cmd_bearer_get_udp_help}, |
| 926 |
{ NULL, }, |
| 927 |
}; |
| 928 |
|
| 929 |
/* Rewind optind to include media in the option list */ |
| 930 |
cmdl->optind--; |
| 931 |
if (parse_opts(opts, cmdl) < 0) |
| 932 |
return -EINVAL; |
| 933 |
|
| 934 |
if (!(opt = get_opt(opts, "media"))) { |
| 935 |
fprintf(stderr, "error, missing media value\n"); |
| 936 |
return -EINVAL; |
| 937 |
} |
| 938 |
media = opt->val; |
| 939 |
|
| 940 |
if (help_flag) { |
| 941 |
cmd_bearer_get_udp_help(cmdl, media); |
| 942 |
return -EINVAL; |
| 943 |
} |
| 944 |
if (strcmp(media, "udp") != 0) { |
| 945 |
fprintf(stderr, "error, no \"%s\" media specific options\n", media); |
| 946 |
return -EINVAL; |
| 947 |
} |
| 948 |
if (!(opt = get_opt(opts, "name"))) { |
| 949 |
fprintf(stderr, "error, missing media name\n"); |
| 950 |
return -EINVAL; |
| 951 |
} |
| 952 |
|
| 953 |
nlh = msg_init(TIPC_NL_BEARER_GET); |
| 954 |
if (!nlh) { |
| 955 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 956 |
return -1; |
| 957 |
} |
| 958 |
|
| 959 |
attrs = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER); |
| 960 |
err = nl_add_bearer_name(nlh, cmd, cmdl, opts, sup_media); |
| 961 |
if (err) |
| 962 |
return err; |
| 963 |
mnl_attr_nest_end(nlh, attrs); |
| 964 |
cb_data.nlh = nlh; |
| 965 |
|
| 966 |
if (has_opt(opts, "localip")) { |
| 967 |
cb_data.attr = TIPC_NLA_UDP_LOCAL; |
| 968 |
cb_data.prop = UDP_PROP_IP; |
| 969 |
return msg_doit(nlh, bearer_get_udp_cb, &cb_data); |
| 970 |
} else if (has_opt(opts, "localport")) { |
| 971 |
cb_data.attr = TIPC_NLA_UDP_LOCAL; |
| 972 |
cb_data.prop = UDP_PROP_PORT; |
| 973 |
return msg_doit(nlh, bearer_get_udp_cb, &cb_data); |
| 974 |
} else if (has_opt(opts, "remoteip")) { |
| 975 |
cb_data.attr = TIPC_NLA_UDP_REMOTE; |
| 976 |
cb_data.prop = UDP_PROP_IP; |
| 977 |
return msg_doit(nlh, bearer_get_udp_cb, &cb_data); |
| 978 |
} else if (has_opt(opts, "remoteport")) { |
| 979 |
cb_data.attr = TIPC_NLA_UDP_REMOTE; |
| 980 |
cb_data.prop = UDP_PROP_PORT; |
| 981 |
return msg_doit(nlh, bearer_get_udp_cb, &cb_data); |
| 982 |
} |
| 983 |
fprintf(stderr, "error, missing UDP option\n"); |
| 984 |
return -EINVAL; |
| 985 |
} |
| 986 |
|
| 987 |
static int cmd_bearer_get_prop(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 988 |
struct cmdl *cmdl, void *data) |
| 989 |
{ |
| 990 |
int err; |
| 991 |
int prop; |
| 992 |
struct nlattr *attrs; |
| 993 |
struct opt opts[] = { |
| 994 |
{ "device", OPT_KEYVAL, NULL }, |
| 995 |
{ "media", OPT_KEYVAL, NULL }, |
| 996 |
{ "name", OPT_KEYVAL, NULL }, |
| 997 |
{ NULL } |
| 998 |
}; |
| 999 |
struct tipc_sup_media sup_media[] = { |
| 1000 |
{ "udp", "name", cmd_bearer_get_udp_help}, |
| 1001 |
{ "eth", "device", cmd_bearer_get_l2_help }, |
| 1002 |
{ "ib", "device", cmd_bearer_get_l2_help }, |
| 1003 |
{ NULL, }, |
| 1004 |
}; |
| 1005 |
|
| 1006 |
if (help_flag) { |
| 1007 |
(cmd->help)(cmdl); |
| 1008 |
return -EINVAL; |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (strcmp(cmd->cmd, "priority") == 0) |
| 1012 |
prop = TIPC_NLA_PROP_PRIO; |
| 1013 |
else if ((strcmp(cmd->cmd, "tolerance") == 0)) |
| 1014 |
prop = TIPC_NLA_PROP_TOL; |
| 1015 |
else if ((strcmp(cmd->cmd, "window") == 0)) |
| 1016 |
prop = TIPC_NLA_PROP_WIN; |
| 1017 |
else if ((strcmp(cmd->cmd, "mtu") == 0)) |
| 1018 |
prop = TIPC_NLA_PROP_MTU; |
| 1019 |
else |
| 1020 |
return -EINVAL; |
| 1021 |
|
| 1022 |
if (parse_opts(opts, cmdl) < 0) |
| 1023 |
return -EINVAL; |
| 1024 |
|
| 1025 |
if (prop == TIPC_NLA_PROP_MTU) { |
| 1026 |
char *media = cmd_get_media_type(cmd, cmdl, opts); |
| 1027 |
|
| 1028 |
if (!media) |
| 1029 |
return -EINVAL; |
| 1030 |
else if (strcmp(media, "udp")) { |
| 1031 |
fprintf(stderr, "error, not supported for media\n"); |
| 1032 |
return -EINVAL; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
nlh = msg_init(TIPC_NL_BEARER_GET); |
| 1037 |
if (!nlh) { |
| 1038 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 1039 |
return -1; |
| 1040 |
} |
| 1041 |
|
| 1042 |
attrs = mnl_attr_nest_start(nlh, TIPC_NLA_BEARER); |
| 1043 |
err = nl_add_bearer_name(nlh, cmd, cmdl, opts, sup_media); |
| 1044 |
if (err) |
| 1045 |
return err; |
| 1046 |
mnl_attr_nest_end(nlh, attrs); |
| 1047 |
|
| 1048 |
return msg_doit(nlh, bearer_get_cb, &prop); |
| 1049 |
} |
| 1050 |
|
| 1051 |
static int cmd_bearer_get(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 1052 |
struct cmdl *cmdl, void *data) |
| 1053 |
{ |
| 1054 |
const struct cmd cmds[] = { |
| 1055 |
{ "priority", cmd_bearer_get_prop, cmd_bearer_get_help }, |
| 1056 |
{ "tolerance", cmd_bearer_get_prop, cmd_bearer_get_help }, |
| 1057 |
{ "window", cmd_bearer_get_prop, cmd_bearer_get_help }, |
| 1058 |
{ "mtu", cmd_bearer_get_prop, cmd_bearer_get_help }, |
| 1059 |
{ "media", cmd_bearer_get_media, cmd_bearer_get_help }, |
| 1060 |
{ NULL } |
| 1061 |
}; |
| 1062 |
|
| 1063 |
return run_cmd(nlh, cmd, cmds, cmdl, NULL); |
| 1064 |
} |
| 1065 |
|
| 1066 |
static int bearer_list_cb(const struct nlmsghdr *nlh, void *data) |
| 1067 |
{ |
| 1068 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 1069 |
struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
| 1070 |
struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1] = {}; |
| 1071 |
|
| 1072 |
mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); |
| 1073 |
if (!info[TIPC_NLA_BEARER]) { |
| 1074 |
fprintf(stderr, "No bearer in netlink response\n"); |
| 1075 |
return MNL_CB_ERROR; |
| 1076 |
} |
| 1077 |
|
| 1078 |
mnl_attr_parse_nested(info[TIPC_NLA_BEARER], parse_attrs, attrs); |
| 1079 |
if (!attrs[TIPC_NLA_BEARER_NAME]) { |
| 1080 |
fprintf(stderr, "Bearer name missing in netlink response\n"); |
| 1081 |
return MNL_CB_ERROR; |
| 1082 |
} |
| 1083 |
|
| 1084 |
printf("%s\n", mnl_attr_get_str(attrs[TIPC_NLA_BEARER_NAME])); |
| 1085 |
|
| 1086 |
return MNL_CB_OK; |
| 1087 |
} |
| 1088 |
|
| 1089 |
static int cmd_bearer_list(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 1090 |
struct cmdl *cmdl, void *data) |
| 1091 |
{ |
| 1092 |
if (help_flag) { |
| 1093 |
fprintf(stderr, "Usage: %s bearer list\n", cmdl->argv[0]); |
| 1094 |
return -EINVAL; |
| 1095 |
} |
| 1096 |
|
| 1097 |
nlh = msg_init(TIPC_NL_BEARER_GET); |
| 1098 |
if (!nlh) { |
| 1099 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 1100 |
return -1; |
| 1101 |
} |
| 1102 |
|
| 1103 |
return msg_dumpit(nlh, bearer_list_cb, NULL); |
| 1104 |
} |
| 1105 |
|
| 1106 |
void cmd_bearer_help(struct cmdl *cmdl) |
| 1107 |
{ |
| 1108 |
fprintf(stderr, |
| 1109 |
"Usage: %s bearer COMMAND [ARGS] ...\n" |
| 1110 |
"\n" |
| 1111 |
"COMMANDS\n" |
| 1112 |
" add - Add data to existing bearer\n" |
| 1113 |
" enable - Enable a bearer\n" |
| 1114 |
" disable - Disable a bearer\n" |
| 1115 |
" set - Set various bearer properties\n" |
| 1116 |
" get - Get various bearer properties\n" |
| 1117 |
" list - List bearers\n", cmdl->argv[0]); |
| 1118 |
} |
| 1119 |
|
| 1120 |
int cmd_bearer(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl, |
| 1121 |
void *data) |
| 1122 |
{ |
| 1123 |
const struct cmd cmds[] = { |
| 1124 |
{ "add", cmd_bearer_add, cmd_bearer_add_help }, |
| 1125 |
{ "disable", cmd_bearer_disable, cmd_bearer_disable_help }, |
| 1126 |
{ "enable", cmd_bearer_enable, cmd_bearer_enable_help }, |
| 1127 |
{ "get", cmd_bearer_get, cmd_bearer_get_help }, |
| 1128 |
{ "list", cmd_bearer_list, NULL }, |
| 1129 |
{ "set", cmd_bearer_set, cmd_bearer_set_help }, |
| 1130 |
{ NULL } |
| 1131 |
}; |
| 1132 |
|
| 1133 |
return run_cmd(nlh, cmd, cmds, cmdl, NULL); |
| 1134 |
} |