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