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