|
Packit Service |
f991dd |
/* SPDX-License-Identifier: LGPL-2.1-only */
|
|
Packit Service |
f991dd |
/*
|
|
Packit Service |
f991dd |
* src/nl-link-set.c Set link attributes
|
|
Packit Service |
f991dd |
*
|
|
Packit Service |
f991dd |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
f991dd |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
f991dd |
* License as published by the Free Software Foundation version 2.1
|
|
Packit Service |
f991dd |
* of the License.
|
|
Packit Service |
f991dd |
*
|
|
Packit Service |
f991dd |
* Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
|
|
Packit Service |
f991dd |
*/
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#include <netlink/cli/utils.h>
|
|
Packit Service |
f991dd |
#include <netlink/cli/link.h>
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#include <linux/if.h>
|
|
Packit Service |
f991dd |
#include <linux/netlink.h>
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static struct nl_sock *sock;
|
|
Packit Service |
f991dd |
static int quiet = 0;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#if 0
|
|
Packit Service |
f991dd |
" changes := [link LINK]\n"
|
|
Packit Service |
f991dd |
" [master MASTER] [qdisc QDISC] [addr ADDR] [broadcast BRD]\n"
|
|
Packit Service |
f991dd |
" [{ up | down }] [{ arp | noarp }] [{ promisc | nopromisc }]\n"
|
|
Packit Service |
f991dd |
" [{ dynamic | nodynamic }] [{ multicast | nomulticast }]\n"
|
|
Packit Service |
f991dd |
" [{ trailers | notrailers }] [{ allmulticast | noallmulticast }]\n");
|
|
Packit Service |
f991dd |
#endif
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void print_usage(void)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
printf(
|
|
Packit Service |
f991dd |
"Usage: nl-link-set [OPTION]... [LINK]\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"Options\n"
|
|
Packit Service |
f991dd |
" -q, --quiet Do not print informal notifications\n"
|
|
Packit Service |
f991dd |
" -h, --help Show this help\n"
|
|
Packit Service |
f991dd |
" -v, --version Show versioning information\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"Selecting the Link\n"
|
|
Packit Service |
f991dd |
" -n, --name=NAME link name\n"
|
|
Packit Service |
f991dd |
" -i, --index interface index\n"
|
|
Packit Service |
f991dd |
"Change Options\n"
|
|
Packit Service |
f991dd |
" --rename=NAME rename interface\n"
|
|
Packit Service |
f991dd |
" --mtu=NUM MTU value\n"
|
|
Packit Service |
f991dd |
" --txqlen=NUM TX queue length\n"
|
|
Packit Service |
f991dd |
" --weight=NUM weight\n"
|
|
Packit Service |
f991dd |
" --ifalias=NAME alias name (SNMP IfAlias)\n"
|
|
Packit Service |
f991dd |
" --state=up/down set interface up/down\n"
|
|
Packit Service |
f991dd |
);
|
|
Packit Service |
f991dd |
exit(0);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void set_cb(struct nl_object *obj, void *arg)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
struct rtnl_link *link = nl_object_priv(obj);
|
|
Packit Service |
f991dd |
struct rtnl_link *change = arg;
|
|
Packit Service |
f991dd |
struct nl_dump_params params = {
|
|
Packit Service |
f991dd |
.dp_type = NL_DUMP_LINE,
|
|
Packit Service |
f991dd |
.dp_fd = stdout,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
int err;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if ((err = rtnl_link_change(sock, link, change, 0)) < 0)
|
|
Packit Service |
f991dd |
nl_cli_fatal(err, "Unable to change link: %s",
|
|
Packit Service |
f991dd |
nl_geterror(err));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (!quiet) {
|
|
Packit Service |
f991dd |
printf("Changed ");
|
|
Packit Service |
f991dd |
nl_object_dump(OBJ_CAST(link), ¶ms);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
int main(int argc, char *argv[])
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
struct nl_cache *link_cache;
|
|
Packit Service |
f991dd |
struct rtnl_link *link, *change;
|
|
Packit Service |
f991dd |
int ok = 0;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
sock = nl_cli_alloc_socket();
|
|
Packit Service |
f991dd |
nl_cli_connect(sock, NETLINK_ROUTE);
|
|
Packit Service |
f991dd |
link_cache = nl_cli_link_alloc_cache(sock);
|
|
Packit Service |
f991dd |
link = nl_cli_link_alloc();
|
|
Packit Service |
f991dd |
change = nl_cli_link_alloc();
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
for (;;) {
|
|
Packit Service |
f991dd |
int c, optidx = 0;
|
|
Packit Service |
f991dd |
enum {
|
|
Packit Service |
f991dd |
ARG_RENAME = 257,
|
|
Packit Service |
f991dd |
ARG_MTU = 258,
|
|
Packit Service |
f991dd |
ARG_TXQLEN,
|
|
Packit Service |
f991dd |
ARG_WEIGHT,
|
|
Packit Service |
f991dd |
ARG_IFALIAS,
|
|
Packit Service |
f991dd |
ARG_STATE,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
static struct option long_opts[] = {
|
|
Packit Service |
f991dd |
{ "quiet", 0, 0, 'q' },
|
|
Packit Service |
f991dd |
{ "help", 0, 0, 'h' },
|
|
Packit Service |
f991dd |
{ "version", 0, 0, 'v' },
|
|
Packit Service |
f991dd |
{ "name", 1, 0, 'n' },
|
|
Packit Service |
f991dd |
{ "index", 1, 0, 'i' },
|
|
Packit Service |
f991dd |
{ "rename", 1, 0, ARG_RENAME },
|
|
Packit Service |
f991dd |
{ "mtu", 1, 0, ARG_MTU },
|
|
Packit Service |
f991dd |
{ "txqlen", 1, 0, ARG_TXQLEN },
|
|
Packit Service |
f991dd |
{ "weight", 1, 0, ARG_WEIGHT },
|
|
Packit Service |
f991dd |
{ "ifalias", 1, 0, ARG_IFALIAS },
|
|
Packit Service |
f991dd |
{ "state", 1, 0, ARG_STATE },
|
|
Packit Service |
f991dd |
{ 0, 0, 0, 0 }
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
c = getopt_long(argc, argv, "qhvn:i:", long_opts, &optidx);
|
|
Packit Service |
f991dd |
if (c == -1)
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
switch (c) {
|
|
Packit Service |
f991dd |
case 'q': quiet = 1; break;
|
|
Packit Service |
f991dd |
case 'h': print_usage(); break;
|
|
Packit Service |
f991dd |
case 'v': nl_cli_print_version(); break;
|
|
Packit Service |
f991dd |
case 'n': ok++; nl_cli_link_parse_name(link, optarg); break;
|
|
Packit Service |
f991dd |
case 'i': ok++; nl_cli_link_parse_ifindex(link, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_RENAME: nl_cli_link_parse_name(change, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_MTU: nl_cli_link_parse_mtu(change, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_TXQLEN: nl_cli_link_parse_txqlen(change, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_WEIGHT: nl_cli_link_parse_weight(change, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_IFALIAS: nl_cli_link_parse_ifalias(change, optarg); break;
|
|
Packit Service |
f991dd |
case ARG_STATE:
|
|
Packit Service |
f991dd |
if(!strcmp(optarg, "up"))
|
|
Packit Service |
f991dd |
rtnl_link_set_flags(change, IFF_UP);
|
|
Packit Service |
f991dd |
else if(!strcmp(optarg, "down"))
|
|
Packit Service |
f991dd |
rtnl_link_unset_flags(change, IFF_UP);
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (!ok)
|
|
Packit Service |
f991dd |
print_usage();
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_cache_foreach_filter(link_cache, OBJ_CAST(link), set_cb, change);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
return 0;
|
|
Packit Service |
f991dd |
}
|