]> 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 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
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 | tcp_metrics | token | netconf }\n"
55 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
56 " -h[uman-readable] | -iec |\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 { "tunnel", do_iptunnel },
88 { "tunl", do_iptunnel },
89 { "tuntap", do_iptuntap },
90 { "tap", do_iptuntap },
91 { "token", do_iptoken },
92 { "tcpmetrics", do_tcp_metrics },
93 { "tcp_metrics", do_tcp_metrics },
94 { "monitor", do_ipmonitor },
95 { "xfrm", do_xfrm },
96 { "mroute", do_multiroute },
97 { "mrule", do_multirule },
98 { "netns", do_netns },
99 { "netconf", do_ipnetconf },
100 { "help", do_help },
101 { 0 }
102 };
103
104 static int do_cmd(const char *argv0, int argc, char **argv)
105 {
106 const struct cmd *c;
107
108 for (c = cmds; c->cmd; ++c) {
109 if (matches(argv0, c->cmd) == 0)
110 return -(c->func(argc-1, argv+1));
111 }
112
113 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
114 return EXIT_FAILURE;
115 }
116
117 static int batch(const char *name)
118 {
119 char *line = NULL;
120 size_t len = 0;
121 int ret = EXIT_SUCCESS;
122 int orig_family = preferred_family;
123
124 batch_mode = 1;
125
126 if (name && strcmp(name, "-") != 0) {
127 if (freopen(name, "r", stdin) == NULL) {
128 fprintf(stderr,
129 "Cannot open file \"%s\" for reading: %s\n",
130 name, strerror(errno));
131 return EXIT_FAILURE;
132 }
133 }
134
135 if (rtnl_open(&rth, 0) < 0) {
136 fprintf(stderr, "Cannot open rtnetlink\n");
137 return EXIT_FAILURE;
138 }
139
140 cmdlineno = 0;
141 while (getcmdline(&line, &len, stdin) != -1) {
142 char *largv[100];
143 int largc;
144
145 preferred_family = orig_family;
146
147 largc = makeargs(line, largv, 100);
148 if (largc == 0)
149 continue; /* blank line */
150
151 if (do_cmd(largv[0], largc, largv)) {
152 fprintf(stderr, "Command failed %s:%d\n",
153 name, cmdlineno);
154 ret = EXIT_FAILURE;
155 if (!force)
156 break;
157 }
158 }
159 if (line)
160 free(line);
161
162 rtnl_close(&rth);
163 return ret;
164 }
165
166
167 int main(int argc, char **argv)
168 {
169 char *basename;
170 char *batch_file = NULL;
171
172 basename = strrchr(argv[0], '/');
173 if (basename == NULL)
174 basename = argv[0];
175 else
176 basename++;
177
178 while (argc > 1) {
179 char *opt = argv[1];
180
181 if (strcmp(opt, "--") == 0) {
182 argc--; argv++;
183 break;
184 }
185 if (opt[0] != '-')
186 break;
187 if (opt[1] == '-')
188 opt++;
189 if (matches(opt, "-loops") == 0) {
190 argc--;
191 argv++;
192 if (argc <= 1)
193 usage();
194 max_flush_loops = atoi(argv[1]);
195 } else if (matches(opt, "-family") == 0) {
196 argc--;
197 argv++;
198 if (argc <= 1)
199 usage();
200 if (strcmp(argv[1], "help") == 0)
201 usage();
202 else
203 preferred_family = read_family(argv[1]);
204 if (preferred_family == AF_UNSPEC)
205 invarg("invalid protocol family", argv[1]);
206 } else if (strcmp(opt, "-4") == 0) {
207 preferred_family = AF_INET;
208 } else if (strcmp(opt, "-6") == 0) {
209 preferred_family = AF_INET6;
210 } else if (strcmp(opt, "-0") == 0) {
211 preferred_family = AF_PACKET;
212 } else if (strcmp(opt, "-I") == 0) {
213 preferred_family = AF_IPX;
214 } else if (strcmp(opt, "-D") == 0) {
215 preferred_family = AF_DECnet;
216 } else if (strcmp(opt, "-M") == 0) {
217 preferred_family = AF_MPLS;
218 } else if (strcmp(opt, "-B") == 0) {
219 preferred_family = AF_BRIDGE;
220 } else if (matches(opt, "-human") == 0 ||
221 matches(opt, "-human-readable") == 0) {
222 ++human_readable;
223 } else if (matches(opt, "-iec") == 0) {
224 ++use_iec;
225 } else if (matches(opt, "-stats") == 0 ||
226 matches(opt, "-statistics") == 0) {
227 ++show_stats;
228 } else if (matches(opt, "-details") == 0) {
229 ++show_details;
230 } else if (matches(opt, "-resolve") == 0) {
231 ++resolve_hosts;
232 } else if (matches(opt, "-oneline") == 0) {
233 ++oneline;
234 } else if (matches(opt, "-timestamp") == 0) {
235 ++timestamp;
236 } else if (matches(opt, "-tshort") == 0) {
237 ++timestamp;
238 ++timestamp_short;
239 #if 0
240 } else if (matches(opt, "-numeric") == 0) {
241 rtnl_names_numeric++;
242 #endif
243 } else if (matches(opt, "-Version") == 0) {
244 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
245 exit(0);
246 } else if (matches(opt, "-force") == 0) {
247 ++force;
248 } else if (matches(opt, "-batch") == 0) {
249 argc--;
250 argv++;
251 if (argc <= 1)
252 usage();
253 batch_file = argv[1];
254 } else if (matches(opt, "-brief") == 0) {
255 ++brief;
256 } else if (matches(opt, "-rcvbuf") == 0) {
257 unsigned int size;
258
259 argc--;
260 argv++;
261 if (argc <= 1)
262 usage();
263 if (get_unsigned(&size, argv[1], 0)) {
264 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
265 argv[1]);
266 exit(-1);
267 }
268 rcvbuf = size;
269 } else if (matches(opt, "-color") == 0) {
270 enable_color();
271 } else if (matches(opt, "-help") == 0) {
272 usage();
273 } else if (matches(opt, "-netns") == 0) {
274 NEXT_ARG();
275 if (netns_switch(argv[1]))
276 exit(-1);
277 } else if (matches(opt, "-all") == 0) {
278 do_all = true;
279 } else {
280 fprintf(stderr,
281 "Option \"%s\" is unknown, try \"ip -help\".\n",
282 opt);
283 exit(-1);
284 }
285 argc--; argv++;
286 }
287
288 _SL_ = oneline ? "\\" : "\n";
289
290 if (batch_file)
291 return batch(batch_file);
292
293 if (rtnl_open(&rth, 0) < 0)
294 exit(1);
295
296 if (strlen(basename) > 2)
297 return do_cmd(basename+2, argc, argv);
298
299 if (argc > 1)
300 return do_cmd(argv[1], argc-1, argv+1);
301
302 rtnl_close(&rth);
303 usage();
304 }