]>
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>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
23 #include "ip_common.h"
24 #include "namespace.h"
27 int preferred_family
= AF_UNSPEC
;
37 int max_flush_loops
= 10;
42 struct rtnl_handle rth
= { .fd
= -1 };
44 static void usage(void) __attribute__((noreturn
));
46 static void usage(void)
49 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
50 " ip [ -force ] -batch filename\n"
51 "where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |\n"
52 " tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |\n"
53 " netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |\n"
54 " vrf | sr | nexthop }\n"
55 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
56 " -h[uman-readable] | -iec | -j[son] | -p[retty] |\n"
57 " -f[amily] { inet | inet6 | mpls | bridge | link } |\n"
58 " -4 | -6 | -I | -D | -M | -B | -0 |\n"
59 " -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n"
60 " -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
61 " -rc[vbuf] [size] | -n[etns] name | -N[umeric] | -a[ll] |\n"
66 static int do_help(int argc
, char **argv
)
72 static const struct cmd
{
74 int (*func
)(int argc
, char **argv
);
76 { "address", do_ipaddr
},
77 { "addrlabel", do_ipaddrlabel
},
78 { "maddress", do_multiaddr
},
79 { "route", do_iproute
},
80 { "rule", do_iprule
},
81 { "neighbor", do_ipneigh
},
82 { "neighbour", do_ipneigh
},
83 { "ntable", do_ipntable
},
84 { "ntbl", do_ipntable
},
85 { "link", do_iplink
},
86 { "l2tp", do_ipl2tp
},
89 { "macsec", do_ipmacsec
},
90 { "tunnel", do_iptunnel
},
91 { "tunl", do_iptunnel
},
92 { "tuntap", do_iptuntap
},
93 { "tap", do_iptuntap
},
94 { "token", do_iptoken
},
95 { "tcpmetrics", do_tcp_metrics
},
96 { "tcp_metrics", do_tcp_metrics
},
97 { "monitor", do_ipmonitor
},
99 { "mroute", do_multiroute
},
100 { "mrule", do_multirule
},
101 { "netns", do_netns
},
102 { "netconf", do_ipnetconf
},
105 { "nexthop", do_ipnh
},
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
;
179 /* to run vrf exec without root, capabilities might be set, drop them
180 * if not needed as the first thing.
181 * execv will drop them for the child command.
183 * - cap_dac_override to create the cgroup subdir in /sys
184 * - cap_sys_admin to load the BPF program
185 * - cap_net_admin to set the socket into the cgroup
187 if (argc
< 3 || strcmp(argv
[1], "vrf") != 0 ||
188 strcmp(argv
[2], "exec") != 0)
191 basename
= strrchr(argv
[0], '/');
192 if (basename
== NULL
)
200 if (strcmp(opt
, "--") == 0) {
208 if (matches(opt
, "-loops") == 0) {
213 max_flush_loops
= atoi(argv
[1]);
214 } else if (matches(opt
, "-family") == 0) {
219 if (strcmp(argv
[1], "help") == 0)
222 preferred_family
= read_family(argv
[1]);
223 if (preferred_family
== AF_UNSPEC
)
224 invarg("invalid protocol family", argv
[1]);
225 } else if (strcmp(opt
, "-4") == 0) {
226 preferred_family
= AF_INET
;
227 } else if (strcmp(opt
, "-6") == 0) {
228 preferred_family
= AF_INET6
;
229 } else if (strcmp(opt
, "-0") == 0) {
230 preferred_family
= AF_PACKET
;
231 } else if (strcmp(opt
, "-D") == 0) {
232 preferred_family
= AF_DECnet
;
233 } else if (strcmp(opt
, "-M") == 0) {
234 preferred_family
= AF_MPLS
;
235 } else if (strcmp(opt
, "-B") == 0) {
236 preferred_family
= AF_BRIDGE
;
237 } else if (matches(opt
, "-human") == 0 ||
238 matches(opt
, "-human-readable") == 0) {
240 } else if (matches(opt
, "-iec") == 0) {
242 } else if (matches(opt
, "-stats") == 0 ||
243 matches(opt
, "-statistics") == 0) {
245 } else if (matches(opt
, "-details") == 0) {
247 } else if (matches(opt
, "-resolve") == 0) {
249 } else if (matches(opt
, "-oneline") == 0) {
251 } else if (matches(opt
, "-timestamp") == 0) {
253 } else if (matches(opt
, "-tshort") == 0) {
256 } else if (matches(opt
, "-Version") == 0) {
257 printf("ip utility, iproute2-ss%s\n", SNAPSHOT
);
259 } else if (matches(opt
, "-force") == 0) {
261 } else if (matches(opt
, "-batch") == 0) {
266 batch_file
= argv
[1];
267 } else if (matches(opt
, "-brief") == 0) {
269 } else if (matches(opt
, "-json") == 0) {
271 } else if (matches(opt
, "-pretty") == 0) {
273 } else if (matches(opt
, "-rcvbuf") == 0) {
280 if (get_unsigned(&size
, argv
[1], 0)) {
281 fprintf(stderr
, "Invalid rcvbuf size '%s'\n",
286 } else if (matches_color(opt
, &color
)) {
287 } else if (matches(opt
, "-help") == 0) {
289 } else if (matches(opt
, "-netns") == 0) {
291 if (netns_switch(argv
[1]))
293 } else if (matches(opt
, "-Numeric") == 0) {
295 } else if (matches(opt
, "-all") == 0) {
299 "Option \"%s\" is unknown, try \"ip -help\".\n",
306 _SL_
= oneline
? "\\" : "\n";
308 check_enable_color(color
, json
);
311 return batch(batch_file
);
313 if (rtnl_open(&rth
, 0) < 0)
316 rtnl_set_strict_dump(&rth
);
318 if (strlen(basename
) > 2)
319 return do_cmd(basename
+2, argc
, argv
);
322 return do_cmd(argv
[1], argc
-1, argv
+1);