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