blob: c29d2f31f94184590e11fda3d60ae9b64855b825 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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>
shemminger351efcd2005-09-01 19:21:50 +000025#include <errno.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000026
27#include "SNAPSHOT.h"
28#include "utils.h"
29#include "ip_common.h"
30
31int preferred_family = AF_UNSPEC;
32int show_stats = 0;
33int resolve_hosts = 0;
34int oneline = 0;
shemminger90f93022005-06-07 21:55:55 +000035int timestamp = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000036char * _SL_ = NULL;
shemminger351efcd2005-09-01 19:21:50 +000037char *batch_file = NULL;
38int force = 0;
39struct rtnl_handle rth;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000040
41static void usage(void) __attribute__((noreturn));
42
43static void usage(void)
44{
45 fprintf(stderr,
46"Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
shemminger3d418dc2005-09-01 22:29:33 +000047" ip [ -force ] [-batch filename\n"
shemminger09954dc2006-01-10 18:43:32 +000048"where OBJECT := { link | addr | route | rule | neigh | ntable | tunnel |\n"
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000049" maddr | mroute | monitor | xfrm }\n"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000050" OPTIONS := { -V[ersion] | -s[tatistics] | -r[esolve] |\n"
shemminger90f93022005-06-07 21:55:55 +000051" -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
52" -o[neline] | -t[imestamp] }\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000053 exit(-1);
54}
55
shemmingerfc57a9d2005-09-07 17:43:00 +000056static int do_help(int argc, char **argv)
57{
58 usage();
59}
60
shemminger351efcd2005-09-01 19:21:50 +000061static const struct cmd {
62 const char *cmd;
63 int (*func)(int argc, char **argv);
64} cmds[] = {
shemmingerede72392005-11-22 17:30:43 +000065 { "address", do_ipaddr },
66 { "maddress", do_multiaddr },
shemminger351efcd2005-09-01 19:21:50 +000067 { "route", do_iproute },
68 { "rule", do_iprule },
shemmingerede72392005-11-22 17:30:43 +000069 { "neighbor", do_ipneigh },
70 { "neighbour", do_ipneigh },
shemminger09954dc2006-01-10 18:43:32 +000071 { "ntable", do_ipntable },
72 { "ntbl", do_ipntable },
shemminger351efcd2005-09-01 19:21:50 +000073 { "link", do_iplink },
74 { "tunnel", do_iptunnel },
shemmingerede72392005-11-22 17:30:43 +000075 { "tunl", do_iptunnel },
shemminger351efcd2005-09-01 19:21:50 +000076 { "monitor", do_ipmonitor },
77 { "xfrm", do_xfrm },
shemminger84616f82005-10-07 16:41:34 +000078 { "mroute", do_multiroute },
shemmingerfc57a9d2005-09-07 17:43:00 +000079 { "help", do_help },
shemminger351efcd2005-09-01 19:21:50 +000080 { 0 }
81};
82
83static int do_cmd(const char *argv0, int argc, char **argv)
84{
shemmingere25d6972005-12-02 20:02:26 +000085 const struct cmd *c;
shemminger351efcd2005-09-01 19:21:50 +000086
shemmingerede72392005-11-22 17:30:43 +000087 for (c = cmds; c->cmd; ++c) {
shemmingere25d6972005-12-02 20:02:26 +000088 if (matches(argv0, c->cmd) == 0)
89 return c->func(argc-1, argv+1);
shemmingerede72392005-11-22 17:30:43 +000090 }
91
shemminger351efcd2005-09-01 19:21:50 +000092 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
shemmingerede72392005-11-22 17:30:43 +000093 return -1;
shemminger351efcd2005-09-01 19:21:50 +000094}
95
96static int batch(const char *name)
97{
98 char *line = NULL;
99 size_t len = 0;
100 int ret = 0;
101 int lineno = 0;
102
103 if (name && strcmp(name, "-") != 0) {
104 if (freopen(name, "r", stdin) == NULL) {
105 fprintf(stderr, "Cannot open file \"%s\" for reading: %s=n",
106 name, strerror(errno));
107 return -1;
108 }
109 }
110
111 if (rtnl_open(&rth, 0) < 0) {
112 fprintf(stderr, "Cannot open rtnetlink\n");
113 return -1;
114 }
115
116 while (getcmdline(&line, &len, stdin) != -1) {
117 char *largv[100];
118 int largc;
119
120 largc = makeargs(line, largv, 100);
121 if (largc == 0)
122 continue; /* blank line */
123
124 if (do_cmd(largv[0], largc, largv)) {
125 fprintf(stderr, "Command failed %s:%d\n", name, lineno);
126 ret = 1;
127 if (!force)
128 break;
129 }
130 }
shemminger8ed63ab2005-09-21 19:33:17 +0000131 if (line)
132 free(line);
shemminger351efcd2005-09-01 19:21:50 +0000133
134 rtnl_close(&rth);
135 return ret;
136}
137
138
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000139int main(int argc, char **argv)
140{
141 char *basename;
142
143 basename = strrchr(argv[0], '/');
144 if (basename == NULL)
145 basename = argv[0];
146 else
147 basename++;
148
149 while (argc > 1) {
150 char *opt = argv[1];
151 if (strcmp(opt,"--") == 0) {
152 argc--; argv++;
153 break;
154 }
155 if (opt[0] != '-')
156 break;
157 if (opt[1] == '-')
158 opt++;
159 if (matches(opt, "-family") == 0) {
160 argc--;
161 argv++;
162 if (argc <= 1)
163 usage();
164 if (strcmp(argv[1], "inet") == 0)
165 preferred_family = AF_INET;
166 else if (strcmp(argv[1], "inet6") == 0)
167 preferred_family = AF_INET6;
168 else if (strcmp(argv[1], "dnet") == 0)
169 preferred_family = AF_DECnet;
170 else if (strcmp(argv[1], "link") == 0)
171 preferred_family = AF_PACKET;
172 else if (strcmp(argv[1], "ipx") == 0)
173 preferred_family = AF_IPX;
174 else if (strcmp(argv[1], "help") == 0)
175 usage();
176 else
177 invarg(argv[1], "invalid protocol family");
178 } else if (strcmp(opt, "-4") == 0) {
179 preferred_family = AF_INET;
180 } else if (strcmp(opt, "-6") == 0) {
181 preferred_family = AF_INET6;
182 } else if (strcmp(opt, "-0") == 0) {
183 preferred_family = AF_PACKET;
184 } else if (strcmp(opt, "-I") == 0) {
185 preferred_family = AF_IPX;
186 } else if (strcmp(opt, "-D") == 0) {
187 preferred_family = AF_DECnet;
188 } else if (matches(opt, "-stats") == 0 ||
189 matches(opt, "-statistics") == 0) {
190 ++show_stats;
191 } else if (matches(opt, "-resolve") == 0) {
192 ++resolve_hosts;
193 } else if (matches(opt, "-oneline") == 0) {
194 ++oneline;
shemminger90f93022005-06-07 21:55:55 +0000195 } else if (matches(opt, "-timestamp") == 0) {
196 ++timestamp;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000197#if 0
198 } else if (matches(opt, "-numeric") == 0) {
199 rtnl_names_numeric++;
200#endif
201 } else if (matches(opt, "-Version") == 0) {
202 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
203 exit(0);
shemminger351efcd2005-09-01 19:21:50 +0000204 } else if (matches(opt, "-force") == 0) {
205 ++force;
206 } else if (matches(opt, "-batch") == 0) {
207 argc--;
208 argv++;
209 if (argc <= 1)
210 usage();
211 batch_file = argv[1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000212 } else if (matches(opt, "-help") == 0) {
213 usage();
214 } else {
215 fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
216 exit(-1);
217 }
218 argc--; argv++;
219 }
220
221 _SL_ = oneline ? "\\" : "\n" ;
222
shemminger351efcd2005-09-01 19:21:50 +0000223 if (batch_file)
224 return batch(batch_file);
225
226 if (rtnl_open(&rth, 0) < 0)
227 exit(1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000228
shemminger351efcd2005-09-01 19:21:50 +0000229 if (strlen(basename) > 2)
230 return do_cmd(basename+2, argc, argv);
231
232 if (argc > 1)
233 return do_cmd(argv[1], argc-1, argv+1);
234
235 rtnl_close(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000236 usage();
237}