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