blob: c23de740b8581d64bc94c8b189fc797787e056a0 [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"
Vadim Kochan52700d42014-12-24 23:04:09 +020025#include "namespace.h"
Mathias Nymand7bd2db2015-04-28 13:18:21 +030026#include "color.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000027
28int preferred_family = AF_UNSPEC;
Christian Hesseb68d9832014-10-31 22:33:13 +010029int human_readable = 0;
Stephen Hemminger1e264ab2014-11-02 12:49:19 -080030int use_iec = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000031int show_stats = 0;
Patrick McHardy1d934832007-08-22 10:49:01 -070032int show_details = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000033int 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 +000037int force = 0;
Ben Greear64c79562010-12-01 11:13:51 -080038int max_flush_loops = 10;
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -070039int batch_mode = 0;
Vadim Kochanb13ba032015-01-18 16:10:18 +020040bool do_all = false;
Ben Greear64c79562010-12-01 11:13:51 -080041
Stephen Hemminger3bfa73f2006-09-26 10:41:57 -070042struct rtnl_handle rth = { .fd = -1 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000043
44static void usage(void) __attribute__((noreturn));
45
46static void usage(void)
47{
48 fprintf(stderr,
49"Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
Petr Jediný10494d22008-08-07 16:45:33 +020050" ip [ -force ] -batch filename\n"
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110051"where OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070052" tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
Tom Herbert69287472014-11-05 10:06:24 -080053" netns | l2tp | fou | tcp_metrics | token | netconf }\n"
Patrick McHardy1d934832007-08-22 10:49:01 -070054" OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
Stephen Hemminger1e264ab2014-11-02 12:49:19 -080055" -h[uman-readable] | -iec |\n"
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050056" -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |\n"
Eric W. Biedermanf4809172013-03-11 14:59:55 -070057" -4 | -6 | -I | -D | -B | -0 |\n"
Ben Greear64c79562010-12-01 11:13:51 -080058" -l[oops] { maximum-addr-flush-attempts } |\n"
Vadim Kochana9255352014-12-28 11:47:43 +020059" -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
Mathias Nymand7bd2db2015-04-28 13:18:21 +030060" -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000061 exit(-1);
62}
63
shemmingerfc57a9d2005-09-07 17:43:00 +000064static int do_help(int argc, char **argv)
65{
66 usage();
Kees van Reeuwijkecf52422013-02-08 03:32:36 +000067 return 0;
shemmingerfc57a9d2005-09-07 17:43:00 +000068}
69
shemminger351efcd2005-09-01 19:21:50 +000070static const struct cmd {
71 const char *cmd;
72 int (*func)(int argc, char **argv);
73} cmds[] = {
Stephen Hemminger48068672014-02-17 10:56:31 -080074 { "address", do_ipaddr },
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110075 { "addrlabel", do_ipaddrlabel },
shemmingerede72392005-11-22 17:30:43 +000076 { "maddress", do_multiaddr },
shemminger351efcd2005-09-01 19:21:50 +000077 { "route", do_iproute },
78 { "rule", do_iprule },
shemmingerede72392005-11-22 17:30:43 +000079 { "neighbor", do_ipneigh },
80 { "neighbour", do_ipneigh },
shemminger09954dc2006-01-10 18:43:32 +000081 { "ntable", do_ipntable },
82 { "ntbl", do_ipntable },
shemminger351efcd2005-09-01 19:21:50 +000083 { "link", do_iplink },
Stephen Hemminger5aa08f62012-01-20 08:16:02 -080084 { "l2tp", do_ipl2tp },
Tom Herbert69287472014-11-05 10:06:24 -080085 { "fou", do_ipfou },
shemminger351efcd2005-09-01 19:21:50 +000086 { "tunnel", do_iptunnel },
shemmingerede72392005-11-22 17:30:43 +000087 { "tunl", do_iptunnel },
David Woodhouse580fbd82009-09-19 09:48:43 -070088 { "tuntap", do_iptuntap },
89 { "tap", do_iptuntap },
Daniel Borkmann191b60b2013-04-30 06:22:50 +000090 { "token", do_iptoken },
Julian Anastasovea63a692012-10-03 12:07:39 +000091 { "tcpmetrics", do_tcp_metrics },
92 { "tcp_metrics",do_tcp_metrics },
shemminger351efcd2005-09-01 19:21:50 +000093 { "monitor", do_ipmonitor },
94 { "xfrm", do_xfrm },
shemminger84616f82005-10-07 16:41:34 +000095 { "mroute", do_multiroute },
Patrick McHardyb6c8e802010-06-09 17:41:10 +020096 { "mrule", do_multirule },
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070097 { "netns", do_netns },
Nicolas Dichtel9d0efc12012-12-12 09:09:23 -080098 { "netconf", do_ipnetconf },
shemmingerfc57a9d2005-09-07 17:43:00 +000099 { "help", do_help },
shemminger351efcd2005-09-01 19:21:50 +0000100 { 0 }
101};
102
103static int do_cmd(const char *argv0, int argc, char **argv)
104{
shemmingere25d6972005-12-02 20:02:26 +0000105 const struct cmd *c;
shemminger351efcd2005-09-01 19:21:50 +0000106
shemmingerede72392005-11-22 17:30:43 +0000107 for (c = cmds; c->cmd; ++c) {
Stephen Hemminger73979442011-10-13 08:38:33 -0700108 if (matches(argv0, c->cmd) == 0) {
109 return -(c->func(argc-1, argv+1));
110 }
shemmingerede72392005-11-22 17:30:43 +0000111 }
112
shemminger351efcd2005-09-01 19:21:50 +0000113 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
Stephen Hemminger73979442011-10-13 08:38:33 -0700114 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000115}
116
117static int batch(const char *name)
118{
119 char *line = NULL;
120 size_t len = 0;
Stephen Hemminger73979442011-10-13 08:38:33 -0700121 int ret = EXIT_SUCCESS;
shemminger351efcd2005-09-01 19:21:50 +0000122
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700123 batch_mode = 1;
124
shemminger351efcd2005-09-01 19:21:50 +0000125 if (name && strcmp(name, "-") != 0) {
126 if (freopen(name, "r", stdin) == NULL) {
Stephen Hemminger692e0752008-01-21 13:46:08 -0800127 fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
shemminger351efcd2005-09-01 19:21:50 +0000128 name, strerror(errno));
Stephen Hemminger73979442011-10-13 08:38:33 -0700129 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000130 }
131 }
132
133 if (rtnl_open(&rth, 0) < 0) {
134 fprintf(stderr, "Cannot open rtnetlink\n");
Stephen Hemminger73979442011-10-13 08:38:33 -0700135 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000136 }
137
Stephen Hemminger3e4f6a32010-03-03 16:31:09 -0800138 cmdlineno = 0;
shemminger351efcd2005-09-01 19:21:50 +0000139 while (getcmdline(&line, &len, stdin) != -1) {
140 char *largv[100];
141 int largc;
142
143 largc = makeargs(line, largv, 100);
144 if (largc == 0)
145 continue; /* blank line */
146
147 if (do_cmd(largv[0], largc, largv)) {
Stephen Hemminger3e4f6a32010-03-03 16:31:09 -0800148 fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
Stephen Hemminger73979442011-10-13 08:38:33 -0700149 ret = EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000150 if (!force)
151 break;
152 }
153 }
shemminger8ed63ab2005-09-21 19:33:17 +0000154 if (line)
155 free(line);
shemminger351efcd2005-09-01 19:21:50 +0000156
157 rtnl_close(&rth);
158 return ret;
159}
160
161
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000162int main(int argc, char **argv)
163{
164 char *basename;
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700165 char *batch_file = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000166
167 basename = strrchr(argv[0], '/');
168 if (basename == NULL)
169 basename = argv[0];
170 else
171 basename++;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800172
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000173 while (argc > 1) {
174 char *opt = argv[1];
175 if (strcmp(opt,"--") == 0) {
176 argc--; argv++;
177 break;
178 }
179 if (opt[0] != '-')
180 break;
181 if (opt[1] == '-')
182 opt++;
Ben Greear64c79562010-12-01 11:13:51 -0800183 if (matches(opt, "-loops") == 0) {
184 argc--;
185 argv++;
186 if (argc <= 1)
187 usage();
188 max_flush_loops = atoi(argv[1]);
189 } else if (matches(opt, "-family") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000190 argc--;
191 argv++;
192 if (argc <= 1)
193 usage();
Eric W. Biederman45c90d12015-03-15 14:50:03 -0500194 if (strcmp(argv[1], "help") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000195 usage();
196 else
Eric W. Biederman45c90d12015-03-15 14:50:03 -0500197 preferred_family = read_family(argv[1]);
198 if (preferred_family == AF_UNSPEC)
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000199 invarg("invalid protocol family", argv[1]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000200 } else if (strcmp(opt, "-4") == 0) {
201 preferred_family = AF_INET;
202 } else if (strcmp(opt, "-6") == 0) {
203 preferred_family = AF_INET6;
204 } else if (strcmp(opt, "-0") == 0) {
205 preferred_family = AF_PACKET;
206 } else if (strcmp(opt, "-I") == 0) {
207 preferred_family = AF_IPX;
208 } else if (strcmp(opt, "-D") == 0) {
209 preferred_family = AF_DECnet;
Eric W. Biedermandacc5d42015-03-15 14:53:45 -0500210 } else if (strcmp(opt, "-M") == 0) {
211 preferred_family = AF_MPLS;
Stephen Hemminger27bca612012-09-17 15:50:27 -0700212 } else if (strcmp(opt, "-B") == 0) {
213 preferred_family = AF_BRIDGE;
Christian Hesseb68d9832014-10-31 22:33:13 +0100214 } else if (matches(opt, "-human") == 0 ||
215 matches(opt, "-human-readable") == 0) {
216 ++human_readable;
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800217 } else if (matches(opt, "-iec") == 0) {
218 ++use_iec;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000219 } else if (matches(opt, "-stats") == 0 ||
220 matches(opt, "-statistics") == 0) {
221 ++show_stats;
Patrick McHardy1d934832007-08-22 10:49:01 -0700222 } else if (matches(opt, "-details") == 0) {
223 ++show_details;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000224 } else if (matches(opt, "-resolve") == 0) {
225 ++resolve_hosts;
226 } else if (matches(opt, "-oneline") == 0) {
227 ++oneline;
shemminger90f93022005-06-07 21:55:55 +0000228 } else if (matches(opt, "-timestamp") == 0) {
229 ++timestamp;
Vadim Kochan79aa79d2014-12-23 00:13:58 +0200230 } else if (matches(opt, "-tshort") == 0) {
231 ++timestamp;
232 ++timestamp_short;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000233#if 0
234 } else if (matches(opt, "-numeric") == 0) {
235 rtnl_names_numeric++;
236#endif
237 } else if (matches(opt, "-Version") == 0) {
238 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
239 exit(0);
shemminger351efcd2005-09-01 19:21:50 +0000240 } else if (matches(opt, "-force") == 0) {
241 ++force;
242 } else if (matches(opt, "-batch") == 0) {
243 argc--;
244 argv++;
245 if (argc <= 1)
246 usage();
247 batch_file = argv[1];
Patrick McHardy7f031912009-10-28 20:50:48 +0100248 } else if (matches(opt, "-rcvbuf") == 0) {
249 unsigned int size;
250
251 argc--;
252 argv++;
253 if (argc <= 1)
254 usage();
255 if (get_unsigned(&size, argv[1], 0)) {
256 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
257 argv[1]);
258 exit(-1);
259 }
260 rcvbuf = size;
Mathias Nymand7bd2db2015-04-28 13:18:21 +0300261 } else if (matches(opt, "-color") == 0) {
262 enable_color();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000263 } else if (matches(opt, "-help") == 0) {
264 usage();
Vadim Kochan52700d42014-12-24 23:04:09 +0200265 } else if (matches(opt, "-netns") == 0) {
266 NEXT_ARG();
267 if (netns_switch(argv[1]))
268 exit(-1);
Vadim Kochanb13ba032015-01-18 16:10:18 +0200269 } else if (matches(opt, "-all") == 0) {
270 do_all = true;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000271 } else {
272 fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
273 exit(-1);
274 }
275 argc--; argv++;
276 }
277
278 _SL_ = oneline ? "\\" : "\n" ;
279
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800280 if (batch_file)
shemminger351efcd2005-09-01 19:21:50 +0000281 return batch(batch_file);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800282
shemminger351efcd2005-09-01 19:21:50 +0000283 if (rtnl_open(&rth, 0) < 0)
284 exit(1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000285
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800286 if (strlen(basename) > 2)
shemminger351efcd2005-09-01 19:21:50 +0000287 return do_cmd(basename+2, argc, argv);
288
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800289 if (argc > 1)
shemminger351efcd2005-09-01 19:21:50 +0000290 return do_cmd(argv[1], argc-1, argv+1);
291
292 rtnl_close(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000293 usage();
294}