]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ip.c
Merge branch 'iproute2-master' into iproute2-next
[mirror_iproute2.git] / ip / ip.c
1 /*
2 * ip.c "ip" utility frontend.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <string.h>
19 #include <errno.h>
20
21 #include "SNAPSHOT.h"
22 #include "utils.h"
23 #include "ip_common.h"
24 #include "namespace.h"
25 #include "color.h"
26
27 int preferred_family = AF_UNSPEC;
28 int human_readable;
29 int use_iec;
30 int show_stats;
31 int show_details;
32 int oneline;
33 int brief;
34 int json;
35 int timestamp;
36 const char *_SL_;
37 int force;
38 int max_flush_loops = 10;
39 int batch_mode;
40 bool do_all;
41
42 struct rtnl_handle rth = { .fd = -1 };
43
44 static void usage(void) __attribute__((noreturn));
45
46 static void usage(void)
47 {
48 fprintf(stderr,
49 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
50 " ip [ -force ] -batch filename\n"
51 "where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |\n"
52 " tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |\n"
53 " netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |\n"
54 " vrf | sr }\n"
55 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
56 " -h[uman-readable] | -iec | -j[son] | -p[retty] |\n"
57 " -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |\n"
58 " -4 | -6 | -I | -D | -B | -0 |\n"
59 " -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n"
60 " -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
61 " -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n");
62 exit(-1);
63 }
64
65 static int do_help(int argc, char **argv)
66 {
67 usage();
68 return 0;
69 }
70
71 static const struct cmd {
72 const char *cmd;
73 int (*func)(int argc, char **argv);
74 } cmds[] = {
75 { "address", do_ipaddr },
76 { "addrlabel", do_ipaddrlabel },
77 { "maddress", do_multiaddr },
78 { "route", do_iproute },
79 { "rule", do_iprule },
80 { "neighbor", do_ipneigh },
81 { "neighbour", do_ipneigh },
82 { "ntable", do_ipntable },
83 { "ntbl", do_ipntable },
84 { "link", do_iplink },
85 { "l2tp", do_ipl2tp },
86 { "fou", do_ipfou },
87 { "ila", do_ipila },
88 { "macsec", do_ipmacsec },
89 { "tunnel", do_iptunnel },
90 { "tunl", do_iptunnel },
91 { "tuntap", do_iptuntap },
92 { "tap", do_iptuntap },
93 { "token", do_iptoken },
94 { "tcpmetrics", do_tcp_metrics },
95 { "tcp_metrics", do_tcp_metrics },
96 { "monitor", do_ipmonitor },
97 { "xfrm", do_xfrm },
98 { "mroute", do_multiroute },
99 { "mrule", do_multirule },
100 { "netns", do_netns },
101 { "netconf", do_ipnetconf },
102 { "vrf", do_ipvrf},
103 { "sr", do_seg6 },
104 { "help", do_help },
105 { 0 }
106 };
107
108 static int do_cmd(const char *argv0, int argc, char **argv)
109 {
110 const struct cmd *c;
111
112 for (c = cmds; c->cmd; ++c) {
113 if (matches(argv0, c->cmd) == 0)
114 return -(c->func(argc-1, argv+1));
115 }
116
117 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
118 return EXIT_FAILURE;
119 }
120
121 static int batch(const char *name)
122 {
123 char *line = NULL;
124 size_t len = 0;
125 int ret = EXIT_SUCCESS;
126 int orig_family = preferred_family;
127
128 batch_mode = 1;
129
130 if (name && strcmp(name, "-") != 0) {
131 if (freopen(name, "r", stdin) == NULL) {
132 fprintf(stderr,
133 "Cannot open file \"%s\" for reading: %s\n",
134 name, strerror(errno));
135 return EXIT_FAILURE;
136 }
137 }
138
139 if (rtnl_open(&rth, 0) < 0) {
140 fprintf(stderr, "Cannot open rtnetlink\n");
141 return EXIT_FAILURE;
142 }
143
144 cmdlineno = 0;
145 while (getcmdline(&line, &len, stdin) != -1) {
146 char *largv[100];
147 int largc;
148
149 preferred_family = orig_family;
150
151 largc = makeargs(line, largv, 100);
152 if (largc == 0)
153 continue; /* blank line */
154
155 if (do_cmd(largv[0], largc, largv)) {
156 fprintf(stderr, "Command failed %s:%d\n",
157 name, cmdlineno);
158 ret = EXIT_FAILURE;
159 if (!force)
160 break;
161 }
162 }
163 if (line)
164 free(line);
165
166 rtnl_close(&rth);
167 return ret;
168 }
169
170
171 int main(int argc, char **argv)
172 {
173 char *basename;
174 char *batch_file = NULL;
175 int color = 0;
176
177 /* to run vrf exec without root, capabilities might be set, drop them
178 * if not needed as the first thing.
179 * execv will drop them for the child command.
180 * vrf exec requires:
181 * - cap_dac_override to create the cgroup subdir in /sys
182 * - cap_sys_admin to load the BPF program
183 * - cap_net_admin to set the socket into the cgroup
184 */
185 if (argc < 3 || strcmp(argv[1], "vrf") != 0 ||
186 strcmp(argv[2], "exec") != 0)
187 drop_cap();
188
189 basename = strrchr(argv[0], '/');
190 if (basename == NULL)
191 basename = argv[0];
192 else
193 basename++;
194
195 while (argc > 1) {
196 char *opt = argv[1];
197
198 if (strcmp(opt, "--") == 0) {
199 argc--; argv++;
200 break;
201 }
202 if (opt[0] != '-')
203 break;
204 if (opt[1] == '-')
205 opt++;
206 if (matches(opt, "-loops") == 0) {
207 argc--;
208 argv++;
209 if (argc <= 1)
210 usage();
211 max_flush_loops = atoi(argv[1]);
212 } else if (matches(opt, "-family") == 0) {
213 argc--;
214 argv++;
215 if (argc <= 1)
216 usage();
217 if (strcmp(argv[1], "help") == 0)
218 usage();
219 else
220 preferred_family = read_family(argv[1]);
221 if (preferred_family == AF_UNSPEC)
222 invarg("invalid protocol family", argv[1]);
223 } else if (strcmp(opt, "-4") == 0) {
224 preferred_family = AF_INET;
225 } else if (strcmp(opt, "-6") == 0) {
226 preferred_family = AF_INET6;
227 } else if (strcmp(opt, "-0") == 0) {
228 preferred_family = AF_PACKET;
229 } else if (strcmp(opt, "-I") == 0) {
230 preferred_family = AF_IPX;
231 } else if (strcmp(opt, "-D") == 0) {
232 preferred_family = AF_DECnet;
233 } else if (strcmp(opt, "-M") == 0) {
234 preferred_family = AF_MPLS;
235 } else if (strcmp(opt, "-B") == 0) {
236 preferred_family = AF_BRIDGE;
237 } else if (matches(opt, "-human") == 0 ||
238 matches(opt, "-human-readable") == 0) {
239 ++human_readable;
240 } else if (matches(opt, "-iec") == 0) {
241 ++use_iec;
242 } else if (matches(opt, "-stats") == 0 ||
243 matches(opt, "-statistics") == 0) {
244 ++show_stats;
245 } else if (matches(opt, "-details") == 0) {
246 ++show_details;
247 } else if (matches(opt, "-resolve") == 0) {
248 ++resolve_hosts;
249 } else if (matches(opt, "-oneline") == 0) {
250 ++oneline;
251 } else if (matches(opt, "-timestamp") == 0) {
252 ++timestamp;
253 } else if (matches(opt, "-tshort") == 0) {
254 ++timestamp;
255 ++timestamp_short;
256 } else if (matches(opt, "-Version") == 0) {
257 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
258 exit(0);
259 } else if (matches(opt, "-force") == 0) {
260 ++force;
261 } else if (matches(opt, "-batch") == 0) {
262 argc--;
263 argv++;
264 if (argc <= 1)
265 usage();
266 batch_file = argv[1];
267 } else if (matches(opt, "-brief") == 0) {
268 ++brief;
269 } else if (matches(opt, "-json") == 0) {
270 ++json;
271 } else if (matches(opt, "-pretty") == 0) {
272 ++pretty;
273 } else if (matches(opt, "-rcvbuf") == 0) {
274 unsigned int size;
275
276 argc--;
277 argv++;
278 if (argc <= 1)
279 usage();
280 if (get_unsigned(&size, argv[1], 0)) {
281 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
282 argv[1]);
283 exit(-1);
284 }
285 rcvbuf = size;
286 } else if (matches(opt, "-color") == 0) {
287 ++color;
288 } else if (matches(opt, "-help") == 0) {
289 usage();
290 } else if (matches(opt, "-netns") == 0) {
291 NEXT_ARG();
292 if (netns_switch(argv[1]))
293 exit(-1);
294 } else if (matches(opt, "-all") == 0) {
295 do_all = true;
296 } else {
297 fprintf(stderr,
298 "Option \"%s\" is unknown, try \"ip -help\".\n",
299 opt);
300 exit(-1);
301 }
302 argc--; argv++;
303 }
304
305 _SL_ = oneline ? "\\" : "\n";
306
307 if (color && !json)
308 enable_color();
309
310 if (batch_file)
311 return batch(batch_file);
312
313 if (rtnl_open(&rth, 0) < 0)
314 exit(1);
315
316 if (strlen(basename) > 2)
317 return do_cmd(basename+2, argc, argv);
318
319 if (argc > 1)
320 return do_cmd(argv[1], argc-1, argv+1);
321
322 rtnl_close(&rth);
323 usage();
324 }