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;
29 int resolve_hosts = 0;
33 char *batch_file = NULL;
35 int max_flush_loops = 10;
37 struct rtnl_handle rth = { .fd = -1 };
39 static void usage(void) __attribute__((noreturn));
41 static void usage(void)
44 "Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }\n"
45 " ip [ -force ] -batch filename\n"
46 "where OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |\n"
47 " tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |\n"
49 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
50 " -f[amily] { inet | inet6 | ipx | dnet | link } |\n"
51 " -l[oops] { maximum-addr-flush-attempts } |\n"
52 " -o[neline] | -t[imestamp] | -b[atch] [filename] |\n"
53 " -rc[vbuf] [size]}\n");
57 static int do_help(int argc, char **argv)
62 static const struct cmd {
64 int (*func)(int argc, char **argv);
66 { "address", do_ipaddr },
67 { "addrlabel", do_ipaddrlabel },
68 { "maddress", do_multiaddr },
69 { "route", do_iproute },
70 { "rule", do_iprule },
71 { "neighbor", do_ipneigh },
72 { "neighbour", do_ipneigh },
73 { "ntable", do_ipntable },
74 { "ntbl", do_ipntable },
75 { "link", do_iplink },
76 { "tunnel", do_iptunnel },
77 { "tunl", do_iptunnel },
78 { "tuntap", do_iptuntap },
79 { "tap", do_iptuntap },
80 { "monitor", do_ipmonitor },
82 { "mroute", do_multiroute },
83 { "mrule", do_multirule },
84 { "netns", do_netns },
89 static int do_cmd(const char *argv0, int argc, char **argv)
93 for (c = cmds; c->cmd; ++c) {
94 if (matches(argv0, c->cmd) == 0)
95 return c->func(argc-1, argv+1);
98 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
102 static int batch(const char *name)
108 if (name && strcmp(name, "-") != 0) {
109 if (freopen(name, "r", stdin) == NULL) {
110 fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
111 name, strerror(errno));
116 if (rtnl_open(&rth, 0) < 0) {
117 fprintf(stderr, "Cannot open rtnetlink\n");
122 while (getcmdline(&line, &len, stdin) != -1) {
126 largc = makeargs(line, largv, 100);
128 continue; /* blank line */
130 if (do_cmd(largv[0], largc, largv)) {
131 fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
145 int main(int argc, char **argv)
149 basename = strrchr(argv[0], '/');
150 if (basename == NULL)
157 if (strcmp(opt,"--") == 0) {
165 if (matches(opt, "-loops") == 0) {
170 max_flush_loops = atoi(argv[1]);
171 } else if (matches(opt, "-family") == 0) {
176 if (strcmp(argv[1], "inet") == 0)
177 preferred_family = AF_INET;
178 else if (strcmp(argv[1], "inet6") == 0)
179 preferred_family = AF_INET6;
180 else if (strcmp(argv[1], "dnet") == 0)
181 preferred_family = AF_DECnet;
182 else if (strcmp(argv[1], "link") == 0)
183 preferred_family = AF_PACKET;
184 else if (strcmp(argv[1], "ipx") == 0)
185 preferred_family = AF_IPX;
186 else if (strcmp(argv[1], "help") == 0)
189 invarg(argv[1], "invalid protocol family");
190 } else if (strcmp(opt, "-4") == 0) {
191 preferred_family = AF_INET;
192 } else if (strcmp(opt, "-6") == 0) {
193 preferred_family = AF_INET6;
194 } else if (strcmp(opt, "-0") == 0) {
195 preferred_family = AF_PACKET;
196 } else if (strcmp(opt, "-I") == 0) {
197 preferred_family = AF_IPX;
198 } else if (strcmp(opt, "-D") == 0) {
199 preferred_family = AF_DECnet;
200 } else if (matches(opt, "-stats") == 0 ||
201 matches(opt, "-statistics") == 0) {
203 } else if (matches(opt, "-details") == 0) {
205 } else if (matches(opt, "-resolve") == 0) {
207 } else if (matches(opt, "-oneline") == 0) {
209 } else if (matches(opt, "-timestamp") == 0) {
212 } else if (matches(opt, "-numeric") == 0) {
213 rtnl_names_numeric++;
215 } else if (matches(opt, "-Version") == 0) {
216 printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
218 } else if (matches(opt, "-force") == 0) {
220 } else if (matches(opt, "-batch") == 0) {
225 batch_file = argv[1];
226 } else if (matches(opt, "-rcvbuf") == 0) {
233 if (get_unsigned(&size, argv[1], 0)) {
234 fprintf(stderr, "Invalid rcvbuf size '%s'\n",
239 } else if (matches(opt, "-help") == 0) {
242 fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
248 _SL_ = oneline ? "\\" : "\n" ;
251 return batch(batch_file);
253 if (rtnl_open(&rth, 0) < 0)
256 if (strlen(basename) > 2)
257 return do_cmd(basename+2, argc, argv);
260 return do_cmd(argv[1], argc-1, argv+1);