]>
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"
26 int preferred_family
= AF_UNSPEC
;
27 int human_readable
= 0;
31 int resolve_hosts
= 0;
36 int max_flush_loops
= 10;
39 struct rtnl_handle rth
= { .fd
= -1 };
41 static void usage(void) __attribute__((noreturn
));
43 static void usage(void)
46 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
47 " ip [ -force ] -batch filename\n"
48 "where OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
49 " tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
50 " netns | l2tp | fou | tcp_metrics | token | netconf }\n"
51 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
52 " -h[uman-readable] | -iec |\n"
53 " -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |\n"
54 " -4 | -6 | -I | -D | -B | -0 |\n"
55 " -l[oops] { maximum-addr-flush-attempts } |\n"
56 " -o[neline] | -t[imestamp] | -b[atch] [filename] |\n"
57 " -rc[vbuf] [size]}\n");
61 static int do_help(int argc
, char **argv
)
67 static const struct cmd
{
69 int (*func
)(int argc
, char **argv
);
71 { "address", do_ipaddr
},
72 { "addrlabel", do_ipaddrlabel
},
73 { "maddress", do_multiaddr
},
74 { "route", do_iproute
},
75 { "rule", do_iprule
},
76 { "neighbor", do_ipneigh
},
77 { "neighbour", do_ipneigh
},
78 { "ntable", do_ipntable
},
79 { "ntbl", do_ipntable
},
80 { "link", do_iplink
},
81 { "l2tp", do_ipl2tp
},
83 { "tunnel", do_iptunnel
},
84 { "tunl", do_iptunnel
},
85 { "tuntap", do_iptuntap
},
86 { "tap", do_iptuntap
},
87 { "token", do_iptoken
},
88 { "tcpmetrics", do_tcp_metrics
},
89 { "tcp_metrics",do_tcp_metrics
},
90 { "monitor", do_ipmonitor
},
92 { "mroute", do_multiroute
},
93 { "mrule", do_multirule
},
94 { "netns", do_netns
},
95 { "netconf", do_ipnetconf
},
100 static int do_cmd(const char *argv0
, int argc
, char **argv
)
104 for (c
= cmds
; c
->cmd
; ++c
) {
105 if (matches(argv0
, c
->cmd
) == 0) {
106 return -(c
->func(argc
-1, argv
+1));
110 fprintf(stderr
, "Object \"%s\" is unknown, try \"ip help\".\n", argv0
);
114 static int batch(const char *name
)
118 int ret
= EXIT_SUCCESS
;
122 if (name
&& strcmp(name
, "-") != 0) {
123 if (freopen(name
, "r", stdin
) == NULL
) {
124 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s\n",
125 name
, strerror(errno
));
130 if (rtnl_open(&rth
, 0) < 0) {
131 fprintf(stderr
, "Cannot open rtnetlink\n");
136 while (getcmdline(&line
, &len
, stdin
) != -1) {
140 largc
= makeargs(line
, largv
, 100);
142 continue; /* blank line */
144 if (do_cmd(largv
[0], largc
, largv
)) {
145 fprintf(stderr
, "Command failed %s:%d\n", name
, cmdlineno
);
159 int main(int argc
, char **argv
)
162 char *batch_file
= NULL
;
164 basename
= strrchr(argv
[0], '/');
165 if (basename
== NULL
)
172 if (strcmp(opt
,"--") == 0) {
180 if (matches(opt
, "-loops") == 0) {
185 max_flush_loops
= atoi(argv
[1]);
186 } else if (matches(opt
, "-family") == 0) {
191 if (strcmp(argv
[1], "inet") == 0)
192 preferred_family
= AF_INET
;
193 else if (strcmp(argv
[1], "inet6") == 0)
194 preferred_family
= AF_INET6
;
195 else if (strcmp(argv
[1], "dnet") == 0)
196 preferred_family
= AF_DECnet
;
197 else if (strcmp(argv
[1], "link") == 0)
198 preferred_family
= AF_PACKET
;
199 else if (strcmp(argv
[1], "ipx") == 0)
200 preferred_family
= AF_IPX
;
201 else if (strcmp(argv
[1], "bridge") == 0)
202 preferred_family
= AF_BRIDGE
;
203 else if (strcmp(argv
[1], "help") == 0)
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
, "-B") == 0) {
218 preferred_family
= AF_BRIDGE
;
219 } else if (matches(opt
, "-human") == 0 ||
220 matches(opt
, "-human-readable") == 0) {
222 } else if (matches(opt
, "-iec") == 0) {
224 } else if (matches(opt
, "-stats") == 0 ||
225 matches(opt
, "-statistics") == 0) {
227 } else if (matches(opt
, "-details") == 0) {
229 } else if (matches(opt
, "-resolve") == 0) {
231 } else if (matches(opt
, "-oneline") == 0) {
233 } else if (matches(opt
, "-timestamp") == 0) {
236 } else if (matches(opt
, "-numeric") == 0) {
237 rtnl_names_numeric
++;
239 } else if (matches(opt
, "-Version") == 0) {
240 printf("ip utility, iproute2-ss%s\n", SNAPSHOT
);
242 } else if (matches(opt
, "-force") == 0) {
244 } else if (matches(opt
, "-batch") == 0) {
249 batch_file
= argv
[1];
250 } else if (matches(opt
, "-rcvbuf") == 0) {
257 if (get_unsigned(&size
, argv
[1], 0)) {
258 fprintf(stderr
, "Invalid rcvbuf size '%s'\n",
263 } else if (matches(opt
, "-help") == 0) {
266 fprintf(stderr
, "Option \"%s\" is unknown, try \"ip -help\".\n", opt
);
272 _SL_
= oneline
? "\\" : "\n" ;
275 return batch(batch_file
);
277 if (rtnl_open(&rth
, 0) < 0)
280 if (strlen(basename
) > 2)
281 return do_cmd(basename
+2, argc
, argv
);
284 return do_cmd(argv
[1], argc
-1, argv
+1);