]>
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 | 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
},
82 { "tunnel", do_iptunnel
},
83 { "tunl", do_iptunnel
},
84 { "tuntap", do_iptuntap
},
85 { "tap", do_iptuntap
},
86 { "token", do_iptoken
},
87 { "tcpmetrics", do_tcp_metrics
},
88 { "tcp_metrics",do_tcp_metrics
},
89 { "monitor", do_ipmonitor
},
91 { "mroute", do_multiroute
},
92 { "mrule", do_multirule
},
93 { "netns", do_netns
},
94 { "netconf", do_ipnetconf
},
99 static int do_cmd(const char *argv0
, int argc
, char **argv
)
103 for (c
= cmds
; c
->cmd
; ++c
) {
104 if (matches(argv0
, c
->cmd
) == 0) {
105 return -(c
->func(argc
-1, argv
+1));
109 fprintf(stderr
, "Object \"%s\" is unknown, try \"ip help\".\n", argv0
);
113 static int batch(const char *name
)
117 int ret
= EXIT_SUCCESS
;
121 if (name
&& strcmp(name
, "-") != 0) {
122 if (freopen(name
, "r", stdin
) == NULL
) {
123 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s\n",
124 name
, strerror(errno
));
129 if (rtnl_open(&rth
, 0) < 0) {
130 fprintf(stderr
, "Cannot open rtnetlink\n");
135 while (getcmdline(&line
, &len
, stdin
) != -1) {
139 largc
= makeargs(line
, largv
, 100);
141 continue; /* blank line */
143 if (do_cmd(largv
[0], largc
, largv
)) {
144 fprintf(stderr
, "Command failed %s:%d\n", name
, cmdlineno
);
158 int main(int argc
, char **argv
)
161 char *batch_file
= NULL
;
163 basename
= strrchr(argv
[0], '/');
164 if (basename
== NULL
)
171 if (strcmp(opt
,"--") == 0) {
179 if (matches(opt
, "-loops") == 0) {
184 max_flush_loops
= atoi(argv
[1]);
185 } else if (matches(opt
, "-family") == 0) {
190 if (strcmp(argv
[1], "inet") == 0)
191 preferred_family
= AF_INET
;
192 else if (strcmp(argv
[1], "inet6") == 0)
193 preferred_family
= AF_INET6
;
194 else if (strcmp(argv
[1], "dnet") == 0)
195 preferred_family
= AF_DECnet
;
196 else if (strcmp(argv
[1], "link") == 0)
197 preferred_family
= AF_PACKET
;
198 else if (strcmp(argv
[1], "ipx") == 0)
199 preferred_family
= AF_IPX
;
200 else if (strcmp(argv
[1], "bridge") == 0)
201 preferred_family
= AF_BRIDGE
;
202 else if (strcmp(argv
[1], "help") == 0)
205 invarg("invalid protocol family", argv
[1]);
206 } else if (strcmp(opt
, "-4") == 0) {
207 preferred_family
= AF_INET
;
208 } else if (strcmp(opt
, "-6") == 0) {
209 preferred_family
= AF_INET6
;
210 } else if (strcmp(opt
, "-0") == 0) {
211 preferred_family
= AF_PACKET
;
212 } else if (strcmp(opt
, "-I") == 0) {
213 preferred_family
= AF_IPX
;
214 } else if (strcmp(opt
, "-D") == 0) {
215 preferred_family
= AF_DECnet
;
216 } else if (strcmp(opt
, "-B") == 0) {
217 preferred_family
= AF_BRIDGE
;
218 } else if (matches(opt
, "-human") == 0 ||
219 matches(opt
, "-human-readable") == 0) {
221 } else if (matches(opt
, "-iec") == 0) {
223 } else if (matches(opt
, "-stats") == 0 ||
224 matches(opt
, "-statistics") == 0) {
226 } else if (matches(opt
, "-details") == 0) {
228 } else if (matches(opt
, "-resolve") == 0) {
230 } else if (matches(opt
, "-oneline") == 0) {
232 } else if (matches(opt
, "-timestamp") == 0) {
235 } else if (matches(opt
, "-numeric") == 0) {
236 rtnl_names_numeric
++;
238 } else if (matches(opt
, "-Version") == 0) {
239 printf("ip utility, iproute2-ss%s\n", SNAPSHOT
);
241 } else if (matches(opt
, "-force") == 0) {
243 } else if (matches(opt
, "-batch") == 0) {
248 batch_file
= argv
[1];
249 } else if (matches(opt
, "-rcvbuf") == 0) {
256 if (get_unsigned(&size
, argv
[1], 0)) {
257 fprintf(stderr
, "Invalid rcvbuf size '%s'\n",
262 } else if (matches(opt
, "-help") == 0) {
265 fprintf(stderr
, "Option \"%s\" is unknown, try \"ip -help\".\n", opt
);
271 _SL_
= oneline
? "\\" : "\n" ;
274 return batch(batch_file
);
276 if (rtnl_open(&rth
, 0) < 0)
279 if (strlen(basename
) > 2)
280 return do_cmd(basename
+2, argc
, argv
);
283 return do_cmd(argv
[1], argc
-1, argv
+1);