blob: 166ef17499d47d56b17fbcf45ce400859dced5ea [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;
Stephen Hemmingerec7aff52015-07-26 21:46:44 -070029int human_readable;
30int use_iec;
31int show_stats;
32int show_details;
33int resolve_hosts;
34int oneline;
Andy Gospodarek5d295bb2015-08-28 13:45:41 -040035int brief;
Stephen Hemmingerec7aff52015-07-26 21:46:44 -070036int timestamp;
37const char *_SL_;
38int force;
Ben Greear64c79562010-12-01 11:13:51 -080039int max_flush_loops = 10;
Stephen Hemmingerec7aff52015-07-26 21:46:44 -070040int batch_mode;
41bool do_all;
Ben Greear64c79562010-12-01 11:13:51 -080042
Stephen Hemminger3bfa73f2006-09-26 10:41:57 -070043struct rtnl_handle rth = { .fd = -1 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000044
45static void usage(void) __attribute__((noreturn));
46
47static void usage(void)
48{
49 fprintf(stderr,
50"Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
Petr Jediný10494d22008-08-07 16:45:33 +020051" ip [ -force ] -batch filename\n"
Phil Sutter7a53aa52016-03-02 19:19:49 +010052"where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |\n"
Pavel Šimerda142434d2015-08-12 22:04:06 +020053" tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |\n"
Sabrina Dubrocab26fc592016-06-08 09:34:21 -070054" netns | l2tp | fou | macsec | tcp_metrics | token | netconf }\n"
Patrick McHardy1d934832007-08-22 10:49:01 -070055" OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
Stephen Hemminger1e264ab2014-11-02 12:49:19 -080056" -h[uman-readable] | -iec |\n"
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050057" -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |\n"
Eric W. Biedermanf4809172013-03-11 14:59:55 -070058" -4 | -6 | -I | -D | -B | -0 |\n"
Andy Gospodarek5d295bb2015-08-28 13:45:41 -040059" -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n"
Vadim Kochana9255352014-12-28 11:47:43 +020060" -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
Mathias Nymand7bd2db2015-04-28 13:18:21 +030061" -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062 exit(-1);
63}
64
shemmingerfc57a9d2005-09-07 17:43:00 +000065static int do_help(int argc, char **argv)
66{
67 usage();
Stephen Hemmingerec7aff52015-07-26 21:46:44 -070068 return 0;
shemmingerfc57a9d2005-09-07 17:43:00 +000069}
70
shemminger351efcd2005-09-01 19:21:50 +000071static const struct cmd {
72 const char *cmd;
73 int (*func)(int argc, char **argv);
74} cmds[] = {
Stephen Hemminger48068672014-02-17 10:56:31 -080075 { "address", do_ipaddr },
YOSHIFUJI Hideaki / 吉藤英明47597582008-02-14 03:20:12 +110076 { "addrlabel", do_ipaddrlabel },
shemmingerede72392005-11-22 17:30:43 +000077 { "maddress", do_multiaddr },
shemminger351efcd2005-09-01 19:21:50 +000078 { "route", do_iproute },
79 { "rule", do_iprule },
shemmingerede72392005-11-22 17:30:43 +000080 { "neighbor", do_ipneigh },
81 { "neighbour", do_ipneigh },
shemminger09954dc2006-01-10 18:43:32 +000082 { "ntable", do_ipntable },
83 { "ntbl", do_ipntable },
shemminger351efcd2005-09-01 19:21:50 +000084 { "link", do_iplink },
Stephen Hemminger5aa08f62012-01-20 08:16:02 -080085 { "l2tp", do_ipl2tp },
Tom Herbert69287472014-11-05 10:06:24 -080086 { "fou", do_ipfou },
Sabrina Dubrocab26fc592016-06-08 09:34:21 -070087 { "macsec", do_ipmacsec },
shemminger351efcd2005-09-01 19:21:50 +000088 { "tunnel", do_iptunnel },
shemmingerede72392005-11-22 17:30:43 +000089 { "tunl", do_iptunnel },
David Woodhouse580fbd82009-09-19 09:48:43 -070090 { "tuntap", do_iptuntap },
91 { "tap", do_iptuntap },
Daniel Borkmann191b60b2013-04-30 06:22:50 +000092 { "token", do_iptoken },
Julian Anastasovea63a692012-10-03 12:07:39 +000093 { "tcpmetrics", do_tcp_metrics },
Stephen Hemmingerec7aff52015-07-26 21:46:44 -070094 { "tcp_metrics", do_tcp_metrics },
shemminger351efcd2005-09-01 19:21:50 +000095 { "monitor", do_ipmonitor },
96 { "xfrm", do_xfrm },
shemminger84616f82005-10-07 16:41:34 +000097 { "mroute", do_multiroute },
Patrick McHardyb6c8e802010-06-09 17:41:10 +020098 { "mrule", do_multirule },
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070099 { "netns", do_netns },
Nicolas Dichtel9d0efc12012-12-12 09:09:23 -0800100 { "netconf", do_ipnetconf },
shemmingerfc57a9d2005-09-07 17:43:00 +0000101 { "help", do_help },
shemminger351efcd2005-09-01 19:21:50 +0000102 { 0 }
103};
104
105static int do_cmd(const char *argv0, int argc, char **argv)
106{
shemmingere25d6972005-12-02 20:02:26 +0000107 const struct cmd *c;
shemminger351efcd2005-09-01 19:21:50 +0000108
shemmingerede72392005-11-22 17:30:43 +0000109 for (c = cmds; c->cmd; ++c) {
Stephen Hemmingerec7aff52015-07-26 21:46:44 -0700110 if (matches(argv0, c->cmd) == 0)
Stephen Hemminger73979442011-10-13 08:38:33 -0700111 return -(c->func(argc-1, argv+1));
shemmingerede72392005-11-22 17:30:43 +0000112 }
113
shemminger351efcd2005-09-01 19:21:50 +0000114 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
Stephen Hemminger73979442011-10-13 08:38:33 -0700115 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000116}
117
118static int batch(const char *name)
119{
120 char *line = NULL;
121 size_t len = 0;
Stephen Hemminger73979442011-10-13 08:38:33 -0700122 int ret = EXIT_SUCCESS;
Antti Paila531d5da2015-07-31 13:55:31 +0300123 int orig_family = preferred_family;
shemminger351efcd2005-09-01 19:21:50 +0000124
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700125 batch_mode = 1;
126
shemminger351efcd2005-09-01 19:21:50 +0000127 if (name && strcmp(name, "-") != 0) {
128 if (freopen(name, "r", stdin) == NULL) {
Stephen Hemmingerec7aff52015-07-26 21:46:44 -0700129 fprintf(stderr,
130 "Cannot open file \"%s\" for reading: %s\n",
shemminger351efcd2005-09-01 19:21:50 +0000131 name, strerror(errno));
Stephen Hemminger73979442011-10-13 08:38:33 -0700132 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000133 }
134 }
135
136 if (rtnl_open(&rth, 0) < 0) {
137 fprintf(stderr, "Cannot open rtnetlink\n");
Stephen Hemminger73979442011-10-13 08:38:33 -0700138 return EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000139 }
140
Stephen Hemminger3e4f6a32010-03-03 16:31:09 -0800141 cmdlineno = 0;
shemminger351efcd2005-09-01 19:21:50 +0000142 while (getcmdline(&line, &len, stdin) != -1) {
143 char *largv[100];
144 int largc;
145
Antti Paila531d5da2015-07-31 13:55:31 +0300146 preferred_family = orig_family;
147
shemminger351efcd2005-09-01 19:21:50 +0000148 largc = makeargs(line, largv, 100);
149 if (largc == 0)
150 continue; /* blank line */
151
152 if (do_cmd(largv[0], largc, largv)) {
Stephen Hemmingerec7aff52015-07-26 21:46:44 -0700153 fprintf(stderr, "Command failed %s:%d\n",
154 name, cmdlineno);
Stephen Hemminger73979442011-10-13 08:38:33 -0700155 ret = EXIT_FAILURE;
shemminger351efcd2005-09-01 19:21:50 +0000156 if (!force)
157 break;
158 }
159 }
shemminger8ed63ab2005-09-21 19:33:17 +0000160 if (line)
161 free(line);
shemminger351efcd2005-09-01 19:21:50 +0000162
163 rtnl_close(&rth);
164 return ret;
165}
166
167
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000168int main(int argc, char **argv)
169{
170 char *basename;
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700171 char *batch_file = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000172
173 basename = strrchr(argv[0], '/');
174 if (basename == NULL)
175 basename = argv[0];
176 else
177 basename++;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800178
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000179 while (argc > 1) {
180 char *opt = argv[1];
Stephen Hemmingerec7aff52015-07-26 21:46:44 -0700181
182 if (strcmp(opt, "--") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000183 argc--; argv++;
184 break;
185 }
186 if (opt[0] != '-')
187 break;
188 if (opt[1] == '-')
189 opt++;
Ben Greear64c79562010-12-01 11:13:51 -0800190 if (matches(opt, "-loops") == 0) {
191 argc--;
192 argv++;
193 if (argc <= 1)
194 usage();
Stephen Hemmingerec7aff52015-07-26 21:46:44 -0700195 max_flush_loops = atoi(argv[1]);
196 } else if (matches(opt, "-family") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000197 argc--;
198 argv++;
199 if (argc <= 1)
200 usage();
Eric W. Biederman45c90d12015-03-15 14:50:03 -0500201 if (strcmp(argv[1], "help") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000202 usage();
203 else
Eric W. Biederman45c90d12015-03-15 14:50:03 -0500204 preferred_family = read_family(argv[1]);
205 if (preferred_family == AF_UNSPEC)
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000206 invarg("invalid protocol family", argv[1]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000207 } 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;
Eric W. Biedermandacc5d42015-03-15 14:53:45 -0500217 } else if (strcmp(opt, "-M") == 0) {
218 preferred_family = AF_MPLS;
Stephen Hemminger27bca612012-09-17 15:50:27 -0700219 } else if (strcmp(opt, "-B") == 0) {
220 preferred_family = AF_BRIDGE;
Christian Hesseb68d9832014-10-31 22:33:13 +0100221 } else if (matches(opt, "-human") == 0 ||
222 matches(opt, "-human-readable") == 0) {
223 ++human_readable;
Stephen Hemminger1e264ab2014-11-02 12:49:19 -0800224 } else if (matches(opt, "-iec") == 0) {
225 ++use_iec;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000226 } else if (matches(opt, "-stats") == 0 ||
227 matches(opt, "-statistics") == 0) {
228 ++show_stats;
Patrick McHardy1d934832007-08-22 10:49:01 -0700229 } else if (matches(opt, "-details") == 0) {
230 ++show_details;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000231 } else if (matches(opt, "-resolve") == 0) {
232 ++resolve_hosts;
233 } else if (matches(opt, "-oneline") == 0) {
234 ++oneline;
shemminger90f93022005-06-07 21:55:55 +0000235 } else if (matches(opt, "-timestamp") == 0) {
236 ++timestamp;
Vadim Kochan79aa79d2014-12-23 00:13:58 +0200237 } else if (matches(opt, "-tshort") == 0) {
238 ++timestamp;
239 ++timestamp_short;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000240#if 0
241 } else if (matches(opt, "-numeric") == 0) {
242 rtnl_names_numeric++;
243#endif
244 } else if (matches(opt, "-Version") == 0) {
245 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
246 exit(0);
shemminger351efcd2005-09-01 19:21:50 +0000247 } else if (matches(opt, "-force") == 0) {
248 ++force;
249 } else if (matches(opt, "-batch") == 0) {
250 argc--;
251 argv++;
252 if (argc <= 1)
253 usage();
254 batch_file = argv[1];
Andy Gospodarek5d295bb2015-08-28 13:45:41 -0400255 } else if (matches(opt, "-brief") == 0) {
256 ++brief;
Patrick McHardy7f031912009-10-28 20:50:48 +0100257 } else if (matches(opt, "-rcvbuf") == 0) {
258 unsigned int size;
259
260 argc--;
261 argv++;
262 if (argc <= 1)
263 usage();
264 if (get_unsigned(&size, argv[1], 0)) {
265 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
266 argv[1]);
267 exit(-1);
268 }
269 rcvbuf = size;
Mathias Nymand7bd2db2015-04-28 13:18:21 +0300270 } else if (matches(opt, "-color") == 0) {
271 enable_color();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000272 } else if (matches(opt, "-help") == 0) {
273 usage();
Vadim Kochan52700d42014-12-24 23:04:09 +0200274 } else if (matches(opt, "-netns") == 0) {
275 NEXT_ARG();
276 if (netns_switch(argv[1]))
277 exit(-1);
Vadim Kochanb13ba032015-01-18 16:10:18 +0200278 } else if (matches(opt, "-all") == 0) {
279 do_all = true;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000280 } else {
Stephen Hemmingerec7aff52015-07-26 21:46:44 -0700281 fprintf(stderr,
282 "Option \"%s\" is unknown, try \"ip -help\".\n",
283 opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000284 exit(-1);
285 }
286 argc--; argv++;
287 }
288
Stephen Hemmingerec7aff52015-07-26 21:46:44 -0700289 _SL_ = oneline ? "\\" : "\n";
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000290
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800291 if (batch_file)
shemminger351efcd2005-09-01 19:21:50 +0000292 return batch(batch_file);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800293
shemminger351efcd2005-09-01 19:21:50 +0000294 if (rtnl_open(&rth, 0) < 0)
295 exit(1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000296
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800297 if (strlen(basename) > 2)
shemminger351efcd2005-09-01 19:21:50 +0000298 return do_cmd(basename+2, argc, argv);
299
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800300 if (argc > 1)
shemminger351efcd2005-09-01 19:21:50 +0000301 return do_cmd(argv[1], argc-1, argv+1);
302
303 rtnl_close(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000304 usage();
305}