]> 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 basename = strrchr(argv[0], '/');
178 if (basename == NULL)
179 basename = argv[0];
180 else
181 basename++;
182
183 while (argc > 1) {
184 char *opt = argv[1];
185
186 if (strcmp(opt, "--") == 0) {
187 argc--; argv++;
188 break;
189 }
190 if (opt[0] != '-')
191 break;
192 if (opt[1] == '-')
193 opt++;
194 if (matches(opt, "-loops") == 0) {
195 argc--;
196 argv++;
197 if (argc <= 1)
198 usage();
199 max_flush_loops = atoi(argv[1]);
200 } else if (matches(opt, "-family") == 0) {
201 argc--;
202 argv++;
203 if (argc <= 1)
204 usage();
205 if (strcmp(argv[1], "help") == 0)
206 usage();
207 else
208 preferred_family = read_family(argv[1]);
209 if (preferred_family == AF_UNSPEC)
210 invarg("invalid protocol family", argv[1]);
211 } else if (strcmp(opt, "-4") == 0) {
212 preferred_family = AF_INET;
213 } else if (strcmp(opt, "-6") == 0) {
214 preferred_family = AF_INET6;
215 } else if (strcmp(opt, "-0") == 0) {
216 preferred_family = AF_PACKET;
217 } else if (strcmp(opt, "-I") == 0) {
218 preferred_family = AF_IPX;
219 } else if (strcmp(opt, "-D") == 0) {
220 preferred_family = AF_DECnet;
221 } else if (strcmp(opt, "-M") == 0) {
222 preferred_family = AF_MPLS;
223 } else if (strcmp(opt, "-B") == 0) {
224 preferred_family = AF_BRIDGE;
225 } else if (matches(opt, "-human") == 0 ||
226 matches(opt, "-human-readable") == 0) {
227 ++human_readable;
228 } else if (matches(opt, "-iec") == 0) {
229 ++use_iec;
230 } else if (matches(opt, "-stats") == 0 ||
231 matches(opt, "-statistics") == 0) {
232 ++show_stats;
233 } else if (matches(opt, "-details") == 0) {
234 ++show_details;
235 } else if (matches(opt, "-resolve") == 0) {
236 ++resolve_hosts;
237 } else if (matches(opt, "-oneline") == 0) {
238 ++oneline;
239 } else if (matches(opt, "-timestamp") == 0) {
240 ++timestamp;
241 } else if (matches(opt, "-tshort") == 0) {
242 ++timestamp;
243 ++timestamp_short;
244 } else if (matches(opt, "-Version") == 0) {
245 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
246 exit(0);
247 } else if (matches(opt, "-force") == 0) {
248 ++force;
249 } else if (matches(opt, "-batch") == 0) {
250 argc--;
251 argv++;
252 if (argc <= 1)
253 usage();
254 batch_file = argv[1];
255 } else if (matches(opt, "-brief") == 0) {
256 ++brief;
257 } else if (matches(opt, "-json") == 0) {
258 ++json;
259 } else if (matches(opt, "-pretty") == 0) {
260 ++pretty;
261 } else if (matches(opt, "-rcvbuf") == 0) {
262 unsigned int size;
263
264 argc--;
265 argv++;
266 if (argc <= 1)
267 usage();
268 if (get_unsigned(&size, argv[1], 0)) {
269 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
270 argv[1]);
271 exit(-1);
272 }
273 rcvbuf = size;
274 } else if (matches(opt, "-color") == 0) {
275 ++color;
276 } else if (matches(opt, "-help") == 0) {
277 usage();
278 } else if (matches(opt, "-netns") == 0) {
279 NEXT_ARG();
280 if (netns_switch(argv[1]))
281 exit(-1);
282 } else if (matches(opt, "-all") == 0) {
283 do_all = true;
284 } else {
285 fprintf(stderr,
286 "Option \"%s\" is unknown, try \"ip -help\".\n",
287 opt);
288 exit(-1);
289 }
290 argc--; argv++;
291 }
292
293 _SL_ = oneline ? "\\" : "\n";
294
295 if (color && !json)
296 enable_color();
297
298 if (batch_file)
299 return batch(batch_file);
300
301 if (rtnl_open(&rth, 0) < 0)
302 exit(1);
303
304 if (strlen(basename) > 2)
305 return do_cmd(basename+2, argc, argv);
306
307 if (argc > 1)
308 return do_cmd(argv[1], argc-1, argv+1);
309
310 rtnl_close(&rth);
311 usage();
312 }