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