blob: 632d2717b096f66409f4e7fe6f5cf195e16de842 [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>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000010 */
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>
shemminger351efcd2005-09-01 19:21:50 +000020#include <errno.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000021
22#include "SNAPSHOT.h"
23#include "utils.h"
24#include "ip_common.h"
25
26int preferred_family = AF_UNSPEC;
27int show_stats = 0;
Patrick McHardy1d934832007-08-22 10:49:01 -070028int show_details = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000029int resolve_hosts = 0;
30int oneline = 0;
shemminger90f93022005-06-07 21:55:55 +000031int timestamp = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000032char * _SL_ = NULL;
shemminger351efcd2005-09-01 19:21:50 +000033char *batch_file = NULL;
34int force = 0;
Ben Greear64c79562010-12-01 11:13:51 -080035int max_flush_loops = 10;
36
Stephen Hemminger3bfa73f2006-09-26 10:41:57 -070037struct rtnl_handle rth = { .fd = -1 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000038
39static void usage(void) __attribute__((noreturn));
40
41static void usage(void)
42{
43 fprintf(stderr,
44"Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
Petr Jediný10494d22008-08-07 16:45:33 +020045" ip [ -force ] -batch filename\n"
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110046"where OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070047" tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
Julian Anastasovea63a692012-10-03 12:07:39 +000048" netns | l2tp | tcp_metrics }\n"
Patrick McHardy1d934832007-08-22 10:49:01 -070049" OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
Stephen Hemminger27bca612012-09-17 15:50:27 -070050" -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |\n"
Ben Greear64c79562010-12-01 11:13:51 -080051" -l[oops] { maximum-addr-flush-attempts } |\n"
Patrick McHardy7f031912009-10-28 20:50:48 +010052" -o[neline] | -t[imestamp] | -b[atch] [filename] |\n"
53" -rc[vbuf] [size]}\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000054 exit(-1);
55}
56
shemmingerfc57a9d2005-09-07 17:43:00 +000057static int do_help(int argc, char **argv)
58{
59 usage();
60}
61
shemminger351efcd2005-09-01 19:21:50 +000062static const struct cmd {
63 const char *cmd;
64 int (*func)(int argc, char **argv);
65} cmds[] = {
shemmingerede72392005-11-22 17:30:43 +000066 { "address", do_ipaddr },
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110067 { "addrlabel", do_ipaddrlabel },
shemmingerede72392005-11-22 17:30:43 +000068 { "maddress", do_multiaddr },
shemminger351efcd2005-09-01 19:21:50 +000069 { "route", do_iproute },
70 { "rule", do_iprule },
shemmingerede72392005-11-22 17:30:43 +000071 { "neighbor", do_ipneigh },
72 { "neighbour", do_ipneigh },
shemminger09954dc2006-01-10 18:43:32 +000073 { "ntable", do_ipntable },
74 { "ntbl", do_ipntable },
shemminger351efcd2005-09-01 19:21:50 +000075 { "link", do_iplink },
Stephen Hemminger5aa08f62012-01-20 08:16:02 -080076 { "l2tp", do_ipl2tp },
shemminger351efcd2005-09-01 19:21:50 +000077 { "tunnel", do_iptunnel },
shemmingerede72392005-11-22 17:30:43 +000078 { "tunl", do_iptunnel },
David Woodhouse580fbd82009-09-19 09:48:43 -070079 { "tuntap", do_iptuntap },
80 { "tap", do_iptuntap },
Julian Anastasovea63a692012-10-03 12:07:39 +000081 { "tcpmetrics", do_tcp_metrics },
82 { "tcp_metrics",do_tcp_metrics },
shemminger351efcd2005-09-01 19:21:50 +000083 { "monitor", do_ipmonitor },
84 { "xfrm", do_xfrm },
shemminger84616f82005-10-07 16:41:34 +000085 { "mroute", do_multiroute },
Patrick McHardyb6c8e802010-06-09 17:41:10 +020086 { "mrule", do_multirule },
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070087 { "netns", do_netns },
Nicolas Dichtel9d0efc12012-12-12 09:09:23 -080088 { "netconf", do_ipnetconf },
shemmingerfc57a9d2005-09-07 17:43:00 +000089 { "help", do_help },
shemminger351efcd2005-09-01 19:21:50 +000090 { 0 }
91};
92
93static int do_cmd(const char *argv0, int argc, char **argv)
94{
shemmingere25d6972005-12-02 20:02:26 +000095 const struct cmd *c;
shemminger351efcd2005-09-01 19:21:50 +000096
shemmingerede72392005-11-22 17:30:43 +000097 for (c = cmds; c->cmd; ++c) {
Stephen Hemminger73979442011-10-13 08:38:33 -070098 if (matches(argv0, c->cmd) == 0) {
99 return -(c->func(argc-1, argv+1));
100 }
shemmingerede72392005-11-22 17:30:43 +0000101 }
102
shemminger351efcd2005-09-01 19:21:50 +0000103 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
Stephen Hemminger73979442011-10-13 08:38:33 -0700104 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000105}
106
107static int batch(const char *name)
108{
109 char *line = NULL;
110 size_t len = 0;
Stephen Hemminger73979442011-10-13 08:38:33 -0700111 int ret = EXIT_SUCCESS;
shemminger351efcd2005-09-01 19:21:50 +0000112
113 if (name && strcmp(name, "-") != 0) {
114 if (freopen(name, "r", stdin) == NULL) {
Stephen Hemminger692e0752008-01-21 13:46:08 -0800115 fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
shemminger351efcd2005-09-01 19:21:50 +0000116 name, strerror(errno));
Stephen Hemminger73979442011-10-13 08:38:33 -0700117 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000118 }
119 }
120
121 if (rtnl_open(&rth, 0) < 0) {
122 fprintf(stderr, "Cannot open rtnetlink\n");
Stephen Hemminger73979442011-10-13 08:38:33 -0700123 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000124 }
125
Stephen Hemminger3e4f6a32010-03-03 16:31:09 -0800126 cmdlineno = 0;
shemminger351efcd2005-09-01 19:21:50 +0000127 while (getcmdline(&line, &len, stdin) != -1) {
128 char *largv[100];
129 int largc;
130
131 largc = makeargs(line, largv, 100);
132 if (largc == 0)
133 continue; /* blank line */
134
135 if (do_cmd(largv[0], largc, largv)) {
Stephen Hemminger3e4f6a32010-03-03 16:31:09 -0800136 fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
Stephen Hemminger73979442011-10-13 08:38:33 -0700137 ret = EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000138 if (!force)
139 break;
140 }
141 }
shemminger8ed63ab2005-09-21 19:33:17 +0000142 if (line)
143 free(line);
shemminger351efcd2005-09-01 19:21:50 +0000144
145 rtnl_close(&rth);
146 return ret;
147}
148
149
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000150int main(int argc, char **argv)
151{
152 char *basename;
153
154 basename = strrchr(argv[0], '/');
155 if (basename == NULL)
156 basename = argv[0];
157 else
158 basename++;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800159
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000160 while (argc > 1) {
161 char *opt = argv[1];
162 if (strcmp(opt,"--") == 0) {
163 argc--; argv++;
164 break;
165 }
166 if (opt[0] != '-')
167 break;
168 if (opt[1] == '-')
169 opt++;
Ben Greear64c79562010-12-01 11:13:51 -0800170 if (matches(opt, "-loops") == 0) {
171 argc--;
172 argv++;
173 if (argc <= 1)
174 usage();
175 max_flush_loops = atoi(argv[1]);
176 } else if (matches(opt, "-family") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000177 argc--;
178 argv++;
179 if (argc <= 1)
180 usage();
181 if (strcmp(argv[1], "inet") == 0)
182 preferred_family = AF_INET;
183 else if (strcmp(argv[1], "inet6") == 0)
184 preferred_family = AF_INET6;
185 else if (strcmp(argv[1], "dnet") == 0)
186 preferred_family = AF_DECnet;
187 else if (strcmp(argv[1], "link") == 0)
188 preferred_family = AF_PACKET;
189 else if (strcmp(argv[1], "ipx") == 0)
190 preferred_family = AF_IPX;
Stephen Hemminger27bca612012-09-17 15:50:27 -0700191 else if (strcmp(argv[1], "bridge") == 0)
192 preferred_family = AF_BRIDGE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000193 else if (strcmp(argv[1], "help") == 0)
194 usage();
195 else
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000196 invarg("invalid protocol family", argv[1]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000197 } else if (strcmp(opt, "-4") == 0) {
198 preferred_family = AF_INET;
199 } else if (strcmp(opt, "-6") == 0) {
200 preferred_family = AF_INET6;
201 } else if (strcmp(opt, "-0") == 0) {
202 preferred_family = AF_PACKET;
203 } else if (strcmp(opt, "-I") == 0) {
204 preferred_family = AF_IPX;
205 } else if (strcmp(opt, "-D") == 0) {
206 preferred_family = AF_DECnet;
Stephen Hemminger27bca612012-09-17 15:50:27 -0700207 } else if (strcmp(opt, "-B") == 0) {
208 preferred_family = AF_BRIDGE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000209 } else if (matches(opt, "-stats") == 0 ||
210 matches(opt, "-statistics") == 0) {
211 ++show_stats;
Patrick McHardy1d934832007-08-22 10:49:01 -0700212 } else if (matches(opt, "-details") == 0) {
213 ++show_details;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000214 } else if (matches(opt, "-resolve") == 0) {
215 ++resolve_hosts;
216 } else if (matches(opt, "-oneline") == 0) {
217 ++oneline;
shemminger90f93022005-06-07 21:55:55 +0000218 } else if (matches(opt, "-timestamp") == 0) {
219 ++timestamp;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000220#if 0
221 } else if (matches(opt, "-numeric") == 0) {
222 rtnl_names_numeric++;
223#endif
224 } else if (matches(opt, "-Version") == 0) {
225 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
226 exit(0);
shemminger351efcd2005-09-01 19:21:50 +0000227 } else if (matches(opt, "-force") == 0) {
228 ++force;
229 } else if (matches(opt, "-batch") == 0) {
230 argc--;
231 argv++;
232 if (argc <= 1)
233 usage();
234 batch_file = argv[1];
Patrick McHardy7f031912009-10-28 20:50:48 +0100235 } else if (matches(opt, "-rcvbuf") == 0) {
236 unsigned int size;
237
238 argc--;
239 argv++;
240 if (argc <= 1)
241 usage();
242 if (get_unsigned(&size, argv[1], 0)) {
243 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
244 argv[1]);
245 exit(-1);
246 }
247 rcvbuf = size;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000248 } else if (matches(opt, "-help") == 0) {
249 usage();
250 } else {
251 fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
252 exit(-1);
253 }
254 argc--; argv++;
255 }
256
257 _SL_ = oneline ? "\\" : "\n" ;
258
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800259 if (batch_file)
shemminger351efcd2005-09-01 19:21:50 +0000260 return batch(batch_file);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800261
shemminger351efcd2005-09-01 19:21:50 +0000262 if (rtnl_open(&rth, 0) < 0)
263 exit(1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000264
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800265 if (strlen(basename) > 2)
shemminger351efcd2005-09-01 19:21:50 +0000266 return do_cmd(basename+2, argc, argv);
267
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800268 if (argc > 1)
shemminger351efcd2005-09-01 19:21:50 +0000269 return do_cmd(argv[1], argc-1, argv+1);
270
271 rtnl_close(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000272 usage();
273}