]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ip.c
2 * ip.c "ip" utility frontend.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
24 #include "ip_common.h"
25 #include "namespace.h"
28 int preferred_family
= AF_UNSPEC
;
40 int max_flush_loops
= 10;
44 struct rtnl_handle rth
= { .fd
= -1 };
46 static void usage(void) __attribute__((noreturn
));
48 static void usage(void)
51 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
52 " ip [ -force ] -batch filename\n"
53 "where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |\n"
54 " tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |\n"
55 " netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |\n"
57 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
58 " -h[uman-readable] | -iec |\n"
59 " -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |\n"
60 " -4 | -6 | -I | -D | -B | -0 |\n"
61 " -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n"
62 " -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
63 " -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n");
67 static int do_help(int argc
, char **argv
)
73 static const struct cmd
{
75 int (*func
)(int argc
, char **argv
);
77 { "address", do_ipaddr
},
78 { "addrlabel", do_ipaddrlabel
},
79 { "maddress", do_multiaddr
},
80 { "route", do_iproute
},
81 { "rule", do_iprule
},
82 { "neighbor", do_ipneigh
},
83 { "neighbour", do_ipneigh
},
84 { "ntable", do_ipntable
},
85 { "ntbl", do_ipntable
},
86 { "link", do_iplink
},
87 { "l2tp", do_ipl2tp
},
90 { "macsec", do_ipmacsec
},
91 { "tunnel", do_iptunnel
},
92 { "tunl", do_iptunnel
},
93 { "tuntap", do_iptuntap
},
94 { "tap", do_iptuntap
},
95 { "token", do_iptoken
},
96 { "tcpmetrics", do_tcp_metrics
},
97 { "tcp_metrics", do_tcp_metrics
},
98 { "monitor", do_ipmonitor
},
100 { "mroute", do_multiroute
},
101 { "mrule", do_multirule
},
102 { "netns", do_netns
},
103 { "netconf", do_ipnetconf
},
110 static int do_cmd(const char *argv0
, int argc
, char **argv
)
114 for (c
= cmds
; c
->cmd
; ++c
) {
115 if (matches(argv0
, c
->cmd
) == 0)
116 return -(c
->func(argc
-1, argv
+1));
119 fprintf(stderr
, "Object \"%s\" is unknown, try \"ip help\".\n", argv0
);
123 static int batch(const char *name
)
127 int ret
= EXIT_SUCCESS
;
128 int orig_family
= preferred_family
;
132 if (name
&& strcmp(name
, "-") != 0) {
133 if (freopen(name
, "r", stdin
) == NULL
) {
135 "Cannot open file \"%s\" for reading: %s\n",
136 name
, strerror(errno
));
141 if (rtnl_open(&rth
, 0) < 0) {
142 fprintf(stderr
, "Cannot open rtnetlink\n");
147 while (getcmdline(&line
, &len
, stdin
) != -1) {
151 preferred_family
= orig_family
;
153 largc
= makeargs(line
, largv
, 100);
155 continue; /* blank line */
157 if (do_cmd(largv
[0], largc
, largv
)) {
158 fprintf(stderr
, "Command failed %s:%d\n",
173 int main(int argc
, char **argv
)
176 char *batch_file
= NULL
;
178 basename
= strrchr(argv
[0], '/');
179 if (basename
== NULL
)
187 if (strcmp(opt
, "--") == 0) {
195 if (matches(opt
, "-loops") == 0) {
200 max_flush_loops
= atoi(argv
[1]);
201 } else if (matches(opt
, "-family") == 0) {
206 if (strcmp(argv
[1], "help") == 0)
209 preferred_family
= read_family(argv
[1]);
210 if (preferred_family
== AF_UNSPEC
)
211 invarg("invalid protocol family", argv
[1]);
212 } else if (strcmp(opt
, "-4") == 0) {
213 preferred_family
= AF_INET
;
214 } else if (strcmp(opt
, "-6") == 0) {
215 preferred_family
= AF_INET6
;
216 } else if (strcmp(opt
, "-0") == 0) {
217 preferred_family
= AF_PACKET
;
218 } else if (strcmp(opt
, "-I") == 0) {
219 preferred_family
= AF_IPX
;
220 } else if (strcmp(opt
, "-D") == 0) {
221 preferred_family
= AF_DECnet
;
222 } else if (strcmp(opt
, "-M") == 0) {
223 preferred_family
= AF_MPLS
;
224 } else if (strcmp(opt
, "-B") == 0) {
225 preferred_family
= AF_BRIDGE
;
226 } else if (matches(opt
, "-human") == 0 ||
227 matches(opt
, "-human-readable") == 0) {
229 } else if (matches(opt
, "-iec") == 0) {
231 } else if (matches(opt
, "-stats") == 0 ||
232 matches(opt
, "-statistics") == 0) {
234 } else if (matches(opt
, "-details") == 0) {
236 } else if (matches(opt
, "-resolve") == 0) {
238 } else if (matches(opt
, "-oneline") == 0) {
240 } else if (matches(opt
, "-timestamp") == 0) {
242 } else if (matches(opt
, "-tshort") == 0) {
246 } else if (matches(opt
, "-numeric") == 0) {
247 rtnl_names_numeric
++;
249 } else if (matches(opt
, "-Version") == 0) {
250 printf("ip utility, iproute2-ss%s\n", SNAPSHOT
);
252 } else if (matches(opt
, "-force") == 0) {
254 } else if (matches(opt
, "-batch") == 0) {
259 batch_file
= argv
[1];
260 } else if (matches(opt
, "-brief") == 0) {
262 } else if (matches(opt
, "-json") == 0) {
264 } else if (matches(opt
, "-rcvbuf") == 0) {
271 if (get_unsigned(&size
, argv
[1], 0)) {
272 fprintf(stderr
, "Invalid rcvbuf size '%s'\n",
277 } else if (matches(opt
, "-color") == 0) {
279 } else if (matches(opt
, "-help") == 0) {
281 } else if (matches(opt
, "-netns") == 0) {
283 if (netns_switch(argv
[1]))
285 } else if (matches(opt
, "-all") == 0) {
289 "Option \"%s\" is unknown, try \"ip -help\".\n",
296 _SL_
= oneline
? "\\" : "\n";
299 check_if_color_enabled();
302 return batch(batch_file
);
304 if (rtnl_open(&rth
, 0) < 0)
307 if (strlen(basename
) > 2)
308 return do_cmd(basename
+2, argc
, argv
);
311 return do_cmd(argv
[1], argc
-1, argv
+1);