]>
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"
30 #define LIBDIR "/usr/lib"
33 int preferred_family
= AF_UNSPEC
;
43 int max_flush_loops
= 10;
47 struct rtnl_handle rth
= { .fd
= -1 };
49 const char *get_ip_lib_dir(void)
53 lib_dir
= getenv("IP_LIB_DIR");
55 lib_dir
= LIBDIR
"/ip";
60 static void usage(void) __attribute__((noreturn
));
62 static void usage(void)
65 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
66 " ip [ -force ] -batch filename\n"
67 "where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |\n"
68 " tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |\n"
69 " netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |\n"
70 " vrf | sr | nexthop | mptcp }\n"
71 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
72 " -h[uman-readable] | -iec | -j[son] | -p[retty] |\n"
73 " -f[amily] { inet | inet6 | mpls | bridge | link } |\n"
74 " -4 | -6 | -I | -D | -M | -B | -0 |\n"
75 " -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n"
76 " -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
77 " -rc[vbuf] [size] | -n[etns] name | -N[umeric] | -a[ll] |\n"
82 static int do_help(int argc
, char **argv
)
88 static const struct cmd
{
90 int (*func
)(int argc
, char **argv
);
92 { "address", do_ipaddr
},
93 { "addrlabel", do_ipaddrlabel
},
94 { "maddress", do_multiaddr
},
95 { "route", do_iproute
},
96 { "rule", do_iprule
},
97 { "neighbor", do_ipneigh
},
98 { "neighbour", do_ipneigh
},
99 { "ntable", do_ipntable
},
100 { "ntbl", do_ipntable
},
101 { "link", do_iplink
},
102 { "l2tp", do_ipl2tp
},
105 { "macsec", do_ipmacsec
},
106 { "tunnel", do_iptunnel
},
107 { "tunl", do_iptunnel
},
108 { "tuntap", do_iptuntap
},
109 { "tap", do_iptuntap
},
110 { "token", do_iptoken
},
111 { "tcpmetrics", do_tcp_metrics
},
112 { "tcp_metrics", do_tcp_metrics
},
113 { "monitor", do_ipmonitor
},
115 { "mroute", do_multiroute
},
116 { "mrule", do_multirule
},
117 { "netns", do_netns
},
118 { "netconf", do_ipnetconf
},
121 { "nexthop", do_ipnh
},
122 { "mptcp", do_mptcp
},
127 static int do_cmd(const char *argv0
, int argc
, char **argv
)
131 for (c
= cmds
; c
->cmd
; ++c
) {
132 if (matches(argv0
, c
->cmd
) == 0)
133 return -(c
->func(argc
-1, argv
+1));
136 fprintf(stderr
, "Object \"%s\" is unknown, try \"ip help\".\n", argv0
);
140 static int ip_batch_cmd(int argc
, char *argv
[], void *data
)
142 const int *orig_family
= data
;
144 preferred_family
= *orig_family
;
145 return do_cmd(argv
[0], argc
, argv
);
148 static int batch(const char *name
)
150 int orig_family
= preferred_family
;
153 if (rtnl_open(&rth
, 0) < 0) {
154 fprintf(stderr
, "Cannot open rtnetlink\n");
158 ret
= do_batch(name
, force
, ip_batch_cmd
, &orig_family
);
164 int main(int argc
, char **argv
)
166 const char *libbpf_version
;
167 char *batch_file
= NULL
;
171 /* to run vrf exec without root, capabilities might be set, drop them
172 * if not needed as the first thing.
173 * execv will drop them for the child command.
175 * - cap_dac_override to create the cgroup subdir in /sys
176 * - cap_sys_admin to load the BPF program
177 * - cap_net_admin to set the socket into the cgroup
179 if (argc
< 3 || strcmp(argv
[1], "vrf") != 0 ||
180 strcmp(argv
[2], "exec") != 0)
183 basename
= strrchr(argv
[0], '/');
184 if (basename
== NULL
)
192 if (strcmp(opt
, "--") == 0) {
200 if (matches(opt
, "-loops") == 0) {
205 max_flush_loops
= atoi(argv
[1]);
206 } else if (matches(opt
, "-family") == 0) {
211 if (strcmp(argv
[1], "help") == 0)
214 preferred_family
= read_family(argv
[1]);
215 if (preferred_family
== AF_UNSPEC
)
216 invarg("invalid protocol family", argv
[1]);
217 } else if (strcmp(opt
, "-4") == 0) {
218 preferred_family
= AF_INET
;
219 } else if (strcmp(opt
, "-6") == 0) {
220 preferred_family
= AF_INET6
;
221 } else if (strcmp(opt
, "-0") == 0) {
222 preferred_family
= AF_PACKET
;
223 } else if (strcmp(opt
, "-D") == 0) {
224 preferred_family
= AF_DECnet
;
225 } else if (strcmp(opt
, "-M") == 0) {
226 preferred_family
= AF_MPLS
;
227 } else if (strcmp(opt
, "-B") == 0) {
228 preferred_family
= AF_BRIDGE
;
229 } else if (matches(opt
, "-human") == 0 ||
230 matches(opt
, "-human-readable") == 0) {
232 } else if (matches(opt
, "-iec") == 0) {
234 } else if (matches(opt
, "-stats") == 0 ||
235 matches(opt
, "-statistics") == 0) {
237 } else if (matches(opt
, "-details") == 0) {
239 } else if (matches(opt
, "-resolve") == 0) {
241 } else if (matches(opt
, "-oneline") == 0) {
243 } else if (matches(opt
, "-timestamp") == 0) {
245 } else if (matches(opt
, "-tshort") == 0) {
248 } else if (matches(opt
, "-Version") == 0) {
249 printf("ip utility, iproute2-%s", version
);
250 libbpf_version
= get_libbpf_version();
252 printf(", libbpf %s", libbpf_version
);
255 } else if (matches(opt
, "-force") == 0) {
257 } else if (matches(opt
, "-batch") == 0) {
262 batch_file
= argv
[1];
263 } else if (matches(opt
, "-brief") == 0) {
265 } else if (matches(opt
, "-json") == 0) {
267 } else if (matches(opt
, "-pretty") == 0) {
269 } else if (matches(opt
, "-rcvbuf") == 0) {
276 if (get_unsigned(&size
, argv
[1], 0)) {
277 fprintf(stderr
, "Invalid rcvbuf size '%s'\n",
282 } else if (matches_color(opt
, &color
)) {
283 } else if (matches(opt
, "-help") == 0) {
285 } else if (matches(opt
, "-netns") == 0) {
287 if (netns_switch(argv
[1]))
289 } else if (matches(opt
, "-Numeric") == 0) {
291 } else if (matches(opt
, "-all") == 0) {
295 "Option \"%s\" is unknown, try \"ip -help\".\n",
302 _SL_
= oneline
? "\\" : "\n";
304 check_enable_color(color
, json
);
307 return batch(batch_file
);
309 if (rtnl_open(&rth
, 0) < 0)
312 rtnl_set_strict_dump(&rth
);
314 if (strlen(basename
) > 2)
315 return do_cmd(basename
+2, argc
, argv
);
318 return do_cmd(argv
[1], argc
-1, argv
+1);