]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ip.c
Make ip utility veth driver aware
[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 * Changes:
13 *
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <syslog.h>
21 #include <fcntl.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "SNAPSHOT.h"
28 #include "utils.h"
29 #include "ip_common.h"
30 #include "veth.h"
31
32 int preferred_family = AF_UNSPEC;
33 int show_stats = 0;
34 int resolve_hosts = 0;
35 int oneline = 0;
36 int timestamp = 0;
37 char * _SL_ = NULL;
38 char *batch_file = NULL;
39 int force = 0;
40 struct rtnl_handle rth = { .fd = -1 };
41
42 static void usage(void) __attribute__((noreturn));
43
44 static void usage(void)
45 {
46 fprintf(stderr,
47 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
48 " ip [ -force ] [-batch filename\n"
49 "where OBJECT := { link | addr | route | rule | neigh | ntable | tunnel |\n"
50 " maddr | mroute | monitor | xfrm | veth }\n"
51 " OPTIONS := { -V[ersion] | -s[tatistics] | -r[esolve] |\n"
52 " -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
53 " -o[neline] | -t[imestamp] }\n");
54 exit(-1);
55 }
56
57 static int do_help(int argc, char **argv)
58 {
59 usage();
60 }
61
62 static const struct cmd {
63 const char *cmd;
64 int (*func)(int argc, char **argv);
65 } cmds[] = {
66 { "address", do_ipaddr },
67 { "maddress", do_multiaddr },
68 { "route", do_iproute },
69 { "rule", do_iprule },
70 { "neighbor", do_ipneigh },
71 { "neighbour", do_ipneigh },
72 { "ntable", do_ipntable },
73 { "ntbl", do_ipntable },
74 { "link", do_iplink },
75 { "tunnel", do_iptunnel },
76 { "tunl", do_iptunnel },
77 { "monitor", do_ipmonitor },
78 { "xfrm", do_xfrm },
79 { "mroute", do_multiroute },
80 { "veth", do_veth },
81 { "help", do_help },
82 { 0 }
83 };
84
85 static int do_cmd(const char *argv0, int argc, char **argv)
86 {
87 const struct cmd *c;
88
89 for (c = cmds; c->cmd; ++c) {
90 if (matches(argv0, c->cmd) == 0)
91 return c->func(argc-1, argv+1);
92 }
93
94 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
95 return -1;
96 }
97
98 static int batch(const char *name)
99 {
100 char *line = NULL;
101 size_t len = 0;
102 int ret = 0;
103 int lineno = 0;
104
105 if (name && strcmp(name, "-") != 0) {
106 if (freopen(name, "r", stdin) == NULL) {
107 fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n",
108 name, strerror(errno));
109 return -1;
110 }
111 }
112
113 if (rtnl_open(&rth, 0) < 0) {
114 fprintf(stderr, "Cannot open rtnetlink\n");
115 return -1;
116 }
117
118 while (getcmdline(&line, &len, stdin) != -1) {
119 char *largv[100];
120 int largc;
121
122 largc = makeargs(line, largv, 100);
123 if (largc == 0)
124 continue; /* blank line */
125
126 if (do_cmd(largv[0], largc, largv)) {
127 fprintf(stderr, "Command failed %s:%d\n", name, lineno);
128 ret = 1;
129 if (!force)
130 break;
131 }
132 }
133 if (line)
134 free(line);
135
136 rtnl_close(&rth);
137 return ret;
138 }
139
140
141 int main(int argc, char **argv)
142 {
143 char *basename;
144
145 basename = strrchr(argv[0], '/');
146 if (basename == NULL)
147 basename = argv[0];
148 else
149 basename++;
150
151 while (argc > 1) {
152 char *opt = argv[1];
153 if (strcmp(opt,"--") == 0) {
154 argc--; argv++;
155 break;
156 }
157 if (opt[0] != '-')
158 break;
159 if (opt[1] == '-')
160 opt++;
161 if (matches(opt, "-family") == 0) {
162 argc--;
163 argv++;
164 if (argc <= 1)
165 usage();
166 if (strcmp(argv[1], "inet") == 0)
167 preferred_family = AF_INET;
168 else if (strcmp(argv[1], "inet6") == 0)
169 preferred_family = AF_INET6;
170 else if (strcmp(argv[1], "dnet") == 0)
171 preferred_family = AF_DECnet;
172 else if (strcmp(argv[1], "link") == 0)
173 preferred_family = AF_PACKET;
174 else if (strcmp(argv[1], "ipx") == 0)
175 preferred_family = AF_IPX;
176 else if (strcmp(argv[1], "help") == 0)
177 usage();
178 else
179 invarg(argv[1], "invalid protocol family");
180 } else if (strcmp(opt, "-4") == 0) {
181 preferred_family = AF_INET;
182 } else if (strcmp(opt, "-6") == 0) {
183 preferred_family = AF_INET6;
184 } else if (strcmp(opt, "-0") == 0) {
185 preferred_family = AF_PACKET;
186 } else if (strcmp(opt, "-I") == 0) {
187 preferred_family = AF_IPX;
188 } else if (strcmp(opt, "-D") == 0) {
189 preferred_family = AF_DECnet;
190 } else if (matches(opt, "-stats") == 0 ||
191 matches(opt, "-statistics") == 0) {
192 ++show_stats;
193 } else if (matches(opt, "-resolve") == 0) {
194 ++resolve_hosts;
195 } else if (matches(opt, "-oneline") == 0) {
196 ++oneline;
197 } else if (matches(opt, "-timestamp") == 0) {
198 ++timestamp;
199 #if 0
200 } else if (matches(opt, "-numeric") == 0) {
201 rtnl_names_numeric++;
202 #endif
203 } else if (matches(opt, "-Version") == 0) {
204 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
205 exit(0);
206 } else if (matches(opt, "-force") == 0) {
207 ++force;
208 } else if (matches(opt, "-batch") == 0) {
209 argc--;
210 argv++;
211 if (argc <= 1)
212 usage();
213 batch_file = argv[1];
214 } else if (matches(opt, "-help") == 0) {
215 usage();
216 } else {
217 fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
218 exit(-1);
219 }
220 argc--; argv++;
221 }
222
223 _SL_ = oneline ? "\\" : "\n" ;
224
225 if (batch_file)
226 return batch(batch_file);
227
228 if (rtnl_open(&rth, 0) < 0)
229 exit(1);
230
231 if (strlen(basename) > 2)
232 return do_cmd(basename+2, argc, argv);
233
234 if (argc > 1)
235 return do_cmd(argv[1], argc-1, argv+1);
236
237 rtnl_close(&rth);
238 usage();
239 }