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 oneline;
34 int brief;
35 int json;
36 int timestamp;
37 const char *_SL_;
38 int force;
39 int max_flush_loops = 10;
40 int batch_mode;
41 bool do_all;
42
43 struct rtnl_handle rth = { .fd = -1 };
44
45 static void usage(void) __attribute__((noreturn));
46
usage(void)47 static void usage(void)
48 {
49 fprintf(stderr,
50 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
51 " ip [ -force ] -batch filename\n"
52 "where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |\n"
53 " tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |\n"
54 " netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |\n"
55 " vrf | sr }\n"
56 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
57 " -h[uman-readable] | -iec |\n"
58 " -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |\n"
59 " -4 | -6 | -I | -D | -B | -0 |\n"
60 " -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n"
61 " -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
62 " -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n");
63 exit(-1);
64 }
65
do_help(int argc,char ** argv)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 { "help", do_help },
106 { 0 }
107 };
108
do_cmd(const char * argv0,int argc,char ** argv)109 static int do_cmd(const char *argv0, int argc, char **argv)
110 {
111 const struct cmd *c;
112
113 for (c = cmds; c->cmd; ++c) {
114 if (matches(argv0, c->cmd) == 0)
115 return -(c->func(argc-1, argv+1));
116 }
117
118 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
119 return EXIT_FAILURE;
120 }
121
122 #ifndef ANDROID
batch(const char * name)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 #endif
172
173
main(int argc,char ** argv)174 int main(int argc, char **argv)
175 {
176 char *basename;
177 #ifndef ANDROID
178 char *batch_file = NULL;
179 #endif
180
181 basename = strrchr(argv[0], '/');
182 if (basename == NULL)
183 basename = argv[0];
184 else
185 basename++;
186
187 while (argc > 1) {
188 char *opt = argv[1];
189
190 if (strcmp(opt, "--") == 0) {
191 argc--; argv++;
192 break;
193 }
194 if (opt[0] != '-')
195 break;
196 if (opt[1] == '-')
197 opt++;
198 if (matches(opt, "-loops") == 0) {
199 argc--;
200 argv++;
201 if (argc <= 1)
202 usage();
203 max_flush_loops = atoi(argv[1]);
204 } else if (matches(opt, "-family") == 0) {
205 argc--;
206 argv++;
207 if (argc <= 1)
208 usage();
209 if (strcmp(argv[1], "help") == 0)
210 usage();
211 else
212 preferred_family = read_family(argv[1]);
213 if (preferred_family == AF_UNSPEC)
214 invarg("invalid protocol family", argv[1]);
215 } else if (strcmp(opt, "-4") == 0) {
216 preferred_family = AF_INET;
217 } else if (strcmp(opt, "-6") == 0) {
218 preferred_family = AF_INET6;
219 } else if (strcmp(opt, "-0") == 0) {
220 preferred_family = AF_PACKET;
221 } else if (strcmp(opt, "-I") == 0) {
222 preferred_family = AF_IPX;
223 } else if (strcmp(opt, "-D") == 0) {
224 preferred_family = AF_DECnet;
225 } else if (strcmp(opt, "-M") == 0) {
226 preferred_family = AF_MPLS;
227 } else if (strcmp(opt, "-B") == 0) {
228 preferred_family = AF_BRIDGE;
229 } else if (matches(opt, "-human") == 0 ||
230 matches(opt, "-human-readable") == 0) {
231 ++human_readable;
232 } else if (matches(opt, "-iec") == 0) {
233 ++use_iec;
234 } else if (matches(opt, "-stats") == 0 ||
235 matches(opt, "-statistics") == 0) {
236 ++show_stats;
237 } else if (matches(opt, "-details") == 0) {
238 ++show_details;
239 } else if (matches(opt, "-resolve") == 0) {
240 ++resolve_hosts;
241 } else if (matches(opt, "-oneline") == 0) {
242 ++oneline;
243 } else if (matches(opt, "-timestamp") == 0) {
244 ++timestamp;
245 } else if (matches(opt, "-tshort") == 0) {
246 ++timestamp;
247 ++timestamp_short;
248 #if 0
249 } else if (matches(opt, "-numeric") == 0) {
250 rtnl_names_numeric++;
251 #endif
252 } else if (matches(opt, "-Version") == 0) {
253 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
254 exit(0);
255 } else if (matches(opt, "-force") == 0) {
256 ++force;
257 #ifndef ANDROID
258 } else if (matches(opt, "-batch") == 0) {
259 argc--;
260 argv++;
261 if (argc <= 1)
262 usage();
263 batch_file = argv[1];
264 #endif
265 } else if (matches(opt, "-brief") == 0) {
266 ++brief;
267 } else if (matches(opt, "-json") == 0) {
268 ++json;
269 } else if (matches(opt, "-rcvbuf") == 0) {
270 unsigned int size;
271
272 argc--;
273 argv++;
274 if (argc <= 1)
275 usage();
276 if (get_unsigned(&size, argv[1], 0)) {
277 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
278 argv[1]);
279 exit(-1);
280 }
281 rcvbuf = size;
282 } else if (matches(opt, "-color") == 0) {
283 enable_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 if (json)
304 check_if_color_enabled();
305
306 #ifndef ANDROID
307 if (batch_file)
308 return batch(batch_file);
309 #endif
310
311 if (rtnl_open(&rth, 0) < 0)
312 exit(1);
313
314 if (strlen(basename) > 2)
315 return do_cmd(basename+2, argc, argv);
316
317 if (argc > 1)
318 return do_cmd(argv[1], argc-1, argv+1);
319
320 rtnl_close(&rth);
321 usage();
322 }
323