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