]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ip.c
iproute2: add check_libbpf() and get_libbpf_version()
[mirror_iproute2.git] / ip / ip.c
CommitLineData
aba5acdf
SH
1/*
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>
aba5acdf
SH
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
aba5acdf
SH
15#include <fcntl.h>
16#include <sys/socket.h>
17#include <netinet/in.h>
18#include <string.h>
351efcde 19#include <errno.h>
aba5acdf 20
fbef6555 21#include "version.h"
aba5acdf
SH
22#include "utils.h"
23#include "ip_common.h"
52700d40 24#include "namespace.h"
d7bd2db5 25#include "color.h"
31f45088 26#include "rt_names.h"
503e9229 27#include "bpf_util.h"
aba5acdf
SH
28
29int preferred_family = AF_UNSPEC;
ec7aff5c
SH
30int human_readable;
31int use_iec;
32int show_stats;
33int show_details;
ec7aff5c 34int oneline;
5d295bb8 35int brief;
5df60772 36int json;
ec7aff5c 37int timestamp;
ec7aff5c 38int force;
64c79560 39int max_flush_loops = 10;
ec7aff5c
SH
40int batch_mode;
41bool do_all;
64c79560 42
3bfa73ff 43struct rtnl_handle rth = { .fd = -1 };
aba5acdf
SH
44
45static void usage(void) __attribute__((noreturn));
46
47static void usage(void)
48{
49 fprintf(stderr,
8589eb4e
MC
50 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
51 " ip [ -force ] -batch filename\n"
52 "where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |\n"
53 " tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |\n"
54 " netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |\n"
7e0767cd 55 " vrf | sr | nexthop | mptcp }\n"
8589eb4e
MC
56 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
57 " -h[uman-readable] | -iec | -j[son] | -p[retty] |\n"
58 " -f[amily] { inet | inet6 | mpls | bridge | link } |\n"
59 " -4 | -6 | -I | -D | -M | -B | -0 |\n"
60 " -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n"
61 " -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
ca697cee
HL
62 " -rc[vbuf] [size] | -n[etns] name | -N[umeric] | -a[ll] |\n"
63 " -c[olor]}\n");
aba5acdf
SH
64 exit(-1);
65}
66
fc57a9df
SH
67static int do_help(int argc, char **argv)
68{
69 usage();
ec7aff5c 70 return 0;
fc57a9df
SH
71}
72
351efcde
SH
73static const struct cmd {
74 const char *cmd;
75 int (*func)(int argc, char **argv);
76} cmds[] = {
4806867a 77 { "address", do_ipaddr },
4759758c 78 { "addrlabel", do_ipaddrlabel },
ede72396 79 { "maddress", do_multiaddr },
351efcde
SH
80 { "route", do_iproute },
81 { "rule", do_iprule },
ede72396
SH
82 { "neighbor", do_ipneigh },
83 { "neighbour", do_ipneigh },
09954dc6
SH
84 { "ntable", do_ipntable },
85 { "ntbl", do_ipntable },
351efcde 86 { "link", do_iplink },
5aa08f6b 87 { "l2tp", do_ipl2tp },
6928747b 88 { "fou", do_ipfou },
ec71cae0 89 { "ila", do_ipila },
b26fc590 90 { "macsec", do_ipmacsec },
351efcde 91 { "tunnel", do_iptunnel },
ede72396 92 { "tunl", do_iptunnel },
580fbd88
DW
93 { "tuntap", do_iptuntap },
94 { "tap", do_iptuntap },
191b60bd 95 { "token", do_iptoken },
ea63a69b 96 { "tcpmetrics", do_tcp_metrics },
ec7aff5c 97 { "tcp_metrics", do_tcp_metrics },
351efcde
SH
98 { "monitor", do_ipmonitor },
99 { "xfrm", do_xfrm },
84616f83 100 { "mroute", do_multiroute },
b6c8e808 101 { "mrule", do_multirule },
0dc34c77 102 { "netns", do_netns },
9d0efc10 103 { "netconf", do_ipnetconf },
1949f82c 104 { "vrf", do_ipvrf},
93863328 105 { "sr", do_seg6 },
63df8e85 106 { "nexthop", do_ipnh },
7e0767cd 107 { "mptcp", do_mptcp },
fc57a9df 108 { "help", do_help },
351efcde
SH
109 { 0 }
110};
111
112static int do_cmd(const char *argv0, int argc, char **argv)
113{
e25d6970 114 const struct cmd *c;
351efcde 115
ede72396 116 for (c = cmds; c->cmd; ++c) {
ec7aff5c 117 if (matches(argv0, c->cmd) == 0)
7397944d 118 return -(c->func(argc-1, argv+1));
ede72396
SH
119 }
120
351efcde 121 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
7397944d 122 return EXIT_FAILURE;
351efcde
SH
123}
124
1d9a81b8 125static int ip_batch_cmd(int argc, char *argv[], void *data)
351efcde 126{
1d9a81b8 127 const int *orig_family = data;
351efcde 128
1d9a81b8
PM
129 preferred_family = *orig_family;
130 return do_cmd(argv[0], argc, argv);
131}
a3aa47a5 132
1d9a81b8
PM
133static int batch(const char *name)
134{
135 int orig_family = preferred_family;
136 int ret;
351efcde
SH
137
138 if (rtnl_open(&rth, 0) < 0) {
139 fprintf(stderr, "Cannot open rtnetlink\n");
7397944d 140 return EXIT_FAILURE;
351efcde
SH
141 }
142
1d9a81b8 143 ret = do_batch(name, force, ip_batch_cmd, &orig_family);
351efcde
SH
144
145 rtnl_close(&rth);
146 return ret;
147}
148
aba5acdf
SH
149int main(int argc, char **argv)
150{
503e9229 151 const char *libbpf_version;
a3aa47a5 152 char *batch_file = NULL;
503e9229 153 char *basename;
1ca4341d 154 int color = 0;
aba5acdf 155
ba2fc55b
LB
156 /* to run vrf exec without root, capabilities might be set, drop them
157 * if not needed as the first thing.
158 * execv will drop them for the child command.
159 * vrf exec requires:
160 * - cap_dac_override to create the cgroup subdir in /sys
161 * - cap_sys_admin to load the BPF program
162 * - cap_net_admin to set the socket into the cgroup
163 */
164 if (argc < 3 || strcmp(argv[1], "vrf") != 0 ||
165 strcmp(argv[2], "exec") != 0)
166 drop_cap();
167
aba5acdf
SH
168 basename = strrchr(argv[0], '/');
169 if (basename == NULL)
170 basename = argv[0];
171 else
172 basename++;
ae665a52 173
aba5acdf
SH
174 while (argc > 1) {
175 char *opt = argv[1];
ec7aff5c
SH
176
177 if (strcmp(opt, "--") == 0) {
aba5acdf
SH
178 argc--; argv++;
179 break;
180 }
181 if (opt[0] != '-')
182 break;
183 if (opt[1] == '-')
184 opt++;
64c79560
BG
185 if (matches(opt, "-loops") == 0) {
186 argc--;
187 argv++;
188 if (argc <= 1)
189 usage();
ec7aff5c
SH
190 max_flush_loops = atoi(argv[1]);
191 } else if (matches(opt, "-family") == 0) {
aba5acdf
SH
192 argc--;
193 argv++;
194 if (argc <= 1)
195 usage();
45c90d19 196 if (strcmp(argv[1], "help") == 0)
aba5acdf
SH
197 usage();
198 else
45c90d19
EB
199 preferred_family = read_family(argv[1]);
200 if (preferred_family == AF_UNSPEC)
f1675d61 201 invarg("invalid protocol family", argv[1]);
aba5acdf
SH
202 } else if (strcmp(opt, "-4") == 0) {
203 preferred_family = AF_INET;
204 } else if (strcmp(opt, "-6") == 0) {
205 preferred_family = AF_INET6;
206 } else if (strcmp(opt, "-0") == 0) {
207 preferred_family = AF_PACKET;
aba5acdf
SH
208 } else if (strcmp(opt, "-D") == 0) {
209 preferred_family = AF_DECnet;
dacc5d41
EB
210 } else if (strcmp(opt, "-M") == 0) {
211 preferred_family = AF_MPLS;
27bca615
SH
212 } else if (strcmp(opt, "-B") == 0) {
213 preferred_family = AF_BRIDGE;
b68d9837
CH
214 } else if (matches(opt, "-human") == 0 ||
215 matches(opt, "-human-readable") == 0) {
216 ++human_readable;
1e264abc
SH
217 } else if (matches(opt, "-iec") == 0) {
218 ++use_iec;
aba5acdf
SH
219 } else if (matches(opt, "-stats") == 0 ||
220 matches(opt, "-statistics") == 0) {
221 ++show_stats;
1d934839
PM
222 } else if (matches(opt, "-details") == 0) {
223 ++show_details;
aba5acdf
SH
224 } else if (matches(opt, "-resolve") == 0) {
225 ++resolve_hosts;
226 } else if (matches(opt, "-oneline") == 0) {
227 ++oneline;
90f93024
SH
228 } else if (matches(opt, "-timestamp") == 0) {
229 ++timestamp;
79aa79d0
VK
230 } else if (matches(opt, "-tshort") == 0) {
231 ++timestamp;
232 ++timestamp_short;
aba5acdf 233 } else if (matches(opt, "-Version") == 0) {
503e9229
HL
234 printf("ip utility, iproute2-%s", version);
235 libbpf_version = get_libbpf_version();
236 if (libbpf_version)
237 printf(", libbpf %s", libbpf_version);
238 printf("\n");
aba5acdf 239 exit(0);
351efcde
SH
240 } 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];
5d295bb8
AG
248 } else if (matches(opt, "-brief") == 0) {
249 ++brief;
5df60772
JF
250 } else if (matches(opt, "-json") == 0) {
251 ++json;
a233caa0 252 } else if (matches(opt, "-pretty") == 0) {
54336567 253 ++pretty;
7f03191f
PM
254 } else if (matches(opt, "-rcvbuf") == 0) {
255 unsigned int size;
256
257 argc--;
258 argv++;
259 if (argc <= 1)
260 usage();
261 if (get_unsigned(&size, argv[1], 0)) {
262 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
263 argv[1]);
264 exit(-1);
265 }
266 rcvbuf = size;
ff1ab8ed 267 } else if (matches_color(opt, &color)) {
aba5acdf
SH
268 } else if (matches(opt, "-help") == 0) {
269 usage();
52700d40
VK
270 } else if (matches(opt, "-netns") == 0) {
271 NEXT_ARG();
272 if (netns_switch(argv[1]))
273 exit(-1);
ca697cee
HL
274 } else if (matches(opt, "-Numeric") == 0) {
275 ++numeric;
b13ba03f
VK
276 } else if (matches(opt, "-all") == 0) {
277 do_all = true;
aba5acdf 278 } else {
ec7aff5c
SH
279 fprintf(stderr,
280 "Option \"%s\" is unknown, try \"ip -help\".\n",
281 opt);
aba5acdf
SH
282 exit(-1);
283 }
284 argc--; argv++;
285 }
286
ec7aff5c 287 _SL_ = oneline ? "\\" : "\n";
aba5acdf 288
4d82962c 289 check_enable_color(color, json);
5df60772 290
ae665a52 291 if (batch_file)
351efcde 292 return batch(batch_file);
ae665a52 293
351efcde
SH
294 if (rtnl_open(&rth, 0) < 0)
295 exit(1);
296
aea41afc
DA
297 rtnl_set_strict_dump(&rth);
298
ae665a52 299 if (strlen(basename) > 2)
351efcde
SH
300 return do_cmd(basename+2, argc, argv);
301
ae665a52 302 if (argc > 1)
351efcde
SH
303 return do_cmd(argv[1], argc-1, argv+1);
304
305 rtnl_close(&rth);
aba5acdf
SH
306 usage();
307}