1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-link-set.c Set link attributes
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation version 2.1
8 * of the License.
9 *
10 * Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
11 */
12
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/link.h>
15
16 #include <linux/if.h>
17 #include <linux/netlink.h>
18
19 static struct nl_sock *sock;
20 static int quiet = 0;
21
22 #if 0
23 " changes := [link LINK]\n"
24 " [master MASTER] [qdisc QDISC] [addr ADDR] [broadcast BRD]\n"
25 " [{ up | down }] [{ arp | noarp }] [{ promisc | nopromisc }]\n"
26 " [{ dynamic | nodynamic }] [{ multicast | nomulticast }]\n"
27 " [{ trailers | notrailers }] [{ allmulticast | noallmulticast }]\n");
28 #endif
29
print_usage(void)30 static void print_usage(void)
31 {
32 printf(
33 "Usage: nl-link-set [OPTION]... [LINK]\n"
34 "\n"
35 "Options\n"
36 " -q, --quiet Do not print informal notifications\n"
37 " -h, --help Show this help\n"
38 " -v, --version Show versioning information\n"
39 "\n"
40 "Selecting the Link\n"
41 " -n, --name=NAME link name\n"
42 " -i, --index interface index\n"
43 "Change Options\n"
44 " --rename=NAME rename interface\n"
45 " --mtu=NUM MTU value\n"
46 " --txqlen=NUM TX queue length\n"
47 " --weight=NUM weight\n"
48 " --ifalias=NAME alias name (SNMP IfAlias)\n"
49 " --state=up/down set interface up/down\n"
50 );
51 exit(0);
52 }
53
set_cb(struct nl_object * obj,void * arg)54 static void set_cb(struct nl_object *obj, void *arg)
55 {
56 struct rtnl_link *link = nl_object_priv(obj);
57 struct rtnl_link *change = arg;
58 struct nl_dump_params params = {
59 .dp_type = NL_DUMP_LINE,
60 .dp_fd = stdout,
61 };
62 int err;
63
64 if ((err = rtnl_link_change(sock, link, change, 0)) < 0)
65 nl_cli_fatal(err, "Unable to change link: %s",
66 nl_geterror(err));
67
68 if (!quiet) {
69 printf("Changed ");
70 nl_object_dump(OBJ_CAST(link), ¶ms);
71 }
72 }
73
main(int argc,char * argv[])74 int main(int argc, char *argv[])
75 {
76 struct nl_cache *link_cache;
77 struct rtnl_link *link, *change;
78 int ok = 0;
79
80 sock = nl_cli_alloc_socket();
81 nl_cli_connect(sock, NETLINK_ROUTE);
82 link_cache = nl_cli_link_alloc_cache(sock);
83 link = nl_cli_link_alloc();
84 change = nl_cli_link_alloc();
85
86 for (;;) {
87 int c, optidx = 0;
88 enum {
89 ARG_RENAME = 257,
90 ARG_MTU = 258,
91 ARG_TXQLEN,
92 ARG_WEIGHT,
93 ARG_IFALIAS,
94 ARG_STATE,
95 };
96 static struct option long_opts[] = {
97 { "quiet", 0, 0, 'q' },
98 { "help", 0, 0, 'h' },
99 { "version", 0, 0, 'v' },
100 { "name", 1, 0, 'n' },
101 { "index", 1, 0, 'i' },
102 { "rename", 1, 0, ARG_RENAME },
103 { "mtu", 1, 0, ARG_MTU },
104 { "txqlen", 1, 0, ARG_TXQLEN },
105 { "weight", 1, 0, ARG_WEIGHT },
106 { "ifalias", 1, 0, ARG_IFALIAS },
107 { "state", 1, 0, ARG_STATE },
108 { 0, 0, 0, 0 }
109 };
110
111 c = getopt_long(argc, argv, "qhvn:i:", long_opts, &optidx);
112 if (c == -1)
113 break;
114
115 switch (c) {
116 case 'q': quiet = 1; break;
117 case 'h': print_usage(); break;
118 case 'v': nl_cli_print_version(); break;
119 case 'n': ok++; nl_cli_link_parse_name(link, optarg); break;
120 case 'i': ok++; nl_cli_link_parse_ifindex(link, optarg); break;
121 case ARG_RENAME: nl_cli_link_parse_name(change, optarg); break;
122 case ARG_MTU: nl_cli_link_parse_mtu(change, optarg); break;
123 case ARG_TXQLEN: nl_cli_link_parse_txqlen(change, optarg); break;
124 case ARG_WEIGHT: nl_cli_link_parse_weight(change, optarg); break;
125 case ARG_IFALIAS: nl_cli_link_parse_ifalias(change, optarg); break;
126 case ARG_STATE:
127 if(!strcmp(optarg, "up"))
128 rtnl_link_set_flags(change, IFF_UP);
129 else if(!strcmp(optarg, "down"))
130 rtnl_link_unset_flags(change, IFF_UP);
131 break;
132 }
133 }
134
135 if (!ok)
136 print_usage();
137
138 nl_cache_foreach_filter(link_cache, OBJ_CAST(link), set_cb, change);
139
140 return 0;
141 }
142