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