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