]>
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
;
39 int max_flush_loops
= 10;
43 struct rtnl_handle rth
= { .fd
= -1 };
45 static void usage(void) __attribute__((noreturn
));
47 static void usage(void)
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 }\n"
55 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
56 " -h[uman-readable] | -iec |\n"
57 " -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |\n"
58 " -4 | -6 | -I | -D | -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 | -a[ll] | -c[olor]}\n");
65 static int do_help(int argc
, char **argv
)
71 static const struct cmd
{
73 int (*func
)(int argc
, char **argv
);
75 { "address", do_ipaddr
},
76 { "addrlabel", do_ipaddrlabel
},
77 { "maddress", do_multiaddr
},
78 { "route", do_iproute
},
79 { "rule", do_iprule
},
80 { "neighbor", do_ipneigh
},
81 { "neighbour", do_ipneigh
},
82 { "ntable", do_ipntable
},
83 { "ntbl", do_ipntable
},
84 { "link", do_iplink
},
85 { "l2tp", do_ipl2tp
},
87 { "macsec", do_ipmacsec
},
88 { "tunnel", do_iptunnel
},
89 { "tunl", do_iptunnel
},
90 { "tuntap", do_iptuntap
},
91 { "tap", do_iptuntap
},
92 { "token", do_iptoken
},
93 { "tcpmetrics", do_tcp_metrics
},
94 { "tcp_metrics", do_tcp_metrics
},
95 { "monitor", do_ipmonitor
},
97 { "mroute", do_multiroute
},
98 { "mrule", do_multirule
},
99 { "netns", do_netns
},
100 { "netconf", do_ipnetconf
},
105 static int do_cmd(const char *argv0
, int argc
, char **argv
)
109 for (c
= cmds
; c
->cmd
; ++c
) {
110 if (matches(argv0
, c
->cmd
) == 0)
111 return -(c
->func(argc
-1, argv
+1));
114 fprintf(stderr
, "Object \"%s\" is unknown, try \"ip help\".\n", argv0
);
118 static int batch(const char *name
)
122 int ret
= EXIT_SUCCESS
;
123 int orig_family
= preferred_family
;
127 if (name
&& strcmp(name
, "-") != 0) {
128 if (freopen(name
, "r", stdin
) == NULL
) {
130 "Cannot open file \"%s\" for reading: %s\n",
131 name
, strerror(errno
));
136 if (rtnl_open(&rth
, 0) < 0) {
137 fprintf(stderr
, "Cannot open rtnetlink\n");
142 while (getcmdline(&line
, &len
, stdin
) != -1) {
146 preferred_family
= orig_family
;
148 largc
= makeargs(line
, largv
, 100);
150 continue; /* blank line */
152 if (do_cmd(largv
[0], largc
, largv
)) {
153 fprintf(stderr
, "Command failed %s:%d\n",
168 int main(int argc
, char **argv
)
171 char *batch_file
= NULL
;
173 basename
= strrchr(argv
[0], '/');
174 if (basename
== NULL
)
182 if (strcmp(opt
, "--") == 0) {
190 if (matches(opt
, "-loops") == 0) {
195 max_flush_loops
= atoi(argv
[1]);
196 } else if (matches(opt
, "-family") == 0) {
201 if (strcmp(argv
[1], "help") == 0)
204 preferred_family
= read_family(argv
[1]);
205 if (preferred_family
== AF_UNSPEC
)
206 invarg("invalid protocol family", argv
[1]);
207 } 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
;
217 } else if (strcmp(opt
, "-M") == 0) {
218 preferred_family
= AF_MPLS
;
219 } else if (strcmp(opt
, "-B") == 0) {
220 preferred_family
= AF_BRIDGE
;
221 } else if (matches(opt
, "-human") == 0 ||
222 matches(opt
, "-human-readable") == 0) {
224 } else if (matches(opt
, "-iec") == 0) {
226 } else if (matches(opt
, "-stats") == 0 ||
227 matches(opt
, "-statistics") == 0) {
229 } else if (matches(opt
, "-details") == 0) {
231 } else if (matches(opt
, "-resolve") == 0) {
233 } else if (matches(opt
, "-oneline") == 0) {
235 } else if (matches(opt
, "-timestamp") == 0) {
237 } else if (matches(opt
, "-tshort") == 0) {
241 } else if (matches(opt
, "-numeric") == 0) {
242 rtnl_names_numeric
++;
244 } else if (matches(opt
, "-Version") == 0) {
245 printf("ip utility, iproute2-ss%s\n", SNAPSHOT
);
247 } else if (matches(opt
, "-force") == 0) {
249 } else if (matches(opt
, "-batch") == 0) {
254 batch_file
= argv
[1];
255 } else if (matches(opt
, "-brief") == 0) {
257 } else if (matches(opt
, "-rcvbuf") == 0) {
264 if (get_unsigned(&size
, argv
[1], 0)) {
265 fprintf(stderr
, "Invalid rcvbuf size '%s'\n",
270 } else if (matches(opt
, "-color") == 0) {
272 } else if (matches(opt
, "-help") == 0) {
274 } else if (matches(opt
, "-netns") == 0) {
276 if (netns_switch(argv
[1]))
278 } else if (matches(opt
, "-all") == 0) {
282 "Option \"%s\" is unknown, try \"ip -help\".\n",
289 _SL_
= oneline
? "\\" : "\n";
292 return batch(batch_file
);
294 if (rtnl_open(&rth
, 0) < 0)
297 if (strlen(basename
) > 2)
298 return do_cmd(basename
+2, argc
, argv
);
301 return do_cmd(argv
[1], argc
-1, argv
+1);