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>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <syslog.h>
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <string.h>
20#include <errno.h>
21
22#include "SNAPSHOT.h"
23#include "utils.h"
24#include "ip_common.h"
25
26int preferred_family = AF_UNSPEC;
27int show_stats = 0;
28int show_details = 0;
29int resolve_hosts = 0;
30int oneline = 0;
31int timestamp = 0;
32char * _SL_ = NULL;
33char *batch_file = NULL;
34int force = 0;
35int max_flush_loops = 10;
36
37struct rtnl_handle rth = { .fd = -1 };
38
39static void usage(void) __attribute__((noreturn));
40
41static void usage(void)
42{
43	fprintf(stderr,
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"
48"                   netns | l2tp }\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");
54	exit(-1);
55}
56
57static int do_help(int argc, char **argv)
58{
59	usage();
60}
61
62static const struct cmd {
63	const char *cmd;
64	int (*func)(int argc, char **argv);
65} cmds[] = {
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	{ "l2tp",	do_ipl2tp },
77	{ "tunnel",	do_iptunnel },
78	{ "tunl",	do_iptunnel },
79	{ "tuntap",	do_iptuntap },
80	{ "tap",	do_iptuntap },
81	{ "monitor",	do_ipmonitor },
82	{ "xfrm",	do_xfrm },
83	{ "mroute",	do_multiroute },
84	{ "mrule",	do_multirule },
85	{ "netns",	do_netns },
86	{ "help",	do_help },
87	{ 0,		0 }
88};
89
90static int do_cmd(const char *argv0, int argc, char **argv)
91{
92	const struct cmd *c;
93
94	for (c = cmds; c->cmd; ++c) {
95		if (matches(argv0, c->cmd) == 0) {
96			return -(c->func(argc-1, argv+1));
97		}
98	}
99
100	fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv0);
101	return EXIT_FAILURE;
102}
103
104#ifndef ANDROID
105static int batch(const char *name)
106{
107	char *line = NULL;
108	size_t len = 0;
109	int ret = EXIT_SUCCESS;
110
111	if (name && strcmp(name, "-") != 0) {
112		if (freopen(name, "r", stdin) == NULL) {
113			fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
114				name, strerror(errno));
115			return EXIT_FAILURE;
116		}
117	}
118
119	if (rtnl_open(&rth, 0) < 0) {
120		fprintf(stderr, "Cannot open rtnetlink\n");
121		return EXIT_FAILURE;
122	}
123
124	cmdlineno = 0;
125	while (getcmdline(&line, &len, stdin) != -1) {
126		char *largv[100];
127		int largc;
128
129		largc = makeargs(line, largv, 100);
130		if (largc == 0)
131			continue;	/* blank line */
132
133		if (do_cmd(largv[0], largc, largv)) {
134			fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
135			ret = EXIT_FAILURE;
136			if (!force)
137				break;
138		}
139	}
140	if (line)
141		free(line);
142
143	rtnl_close(&rth);
144	return ret;
145}
146#endif
147
148int main(int argc, char **argv)
149{
150	char *basename;
151
152	basename = strrchr(argv[0], '/');
153	if (basename == NULL)
154		basename = argv[0];
155	else
156		basename++;
157
158	while (argc > 1) {
159		char *opt = argv[1];
160		if (strcmp(opt,"--") == 0) {
161			argc--; argv++;
162			break;
163		}
164		if (opt[0] != '-')
165			break;
166		if (opt[1] == '-')
167			opt++;
168		if (matches(opt, "-loops") == 0) {
169			argc--;
170			argv++;
171			if (argc <= 1)
172				usage();
173                        max_flush_loops = atoi(argv[1]);
174                } else if (matches(opt, "-family") == 0) {
175			argc--;
176			argv++;
177			if (argc <= 1)
178				usage();
179			if (strcmp(argv[1], "inet") == 0)
180				preferred_family = AF_INET;
181			else if (strcmp(argv[1], "inet6") == 0)
182				preferred_family = AF_INET6;
183			else if (strcmp(argv[1], "dnet") == 0)
184				preferred_family = AF_DECnet;
185			else if (strcmp(argv[1], "link") == 0)
186				preferred_family = AF_PACKET;
187			else if (strcmp(argv[1], "ipx") == 0)
188				preferred_family = AF_IPX;
189			else if (strcmp(argv[1], "help") == 0)
190				usage();
191			else
192				invarg(argv[1], "invalid protocol family");
193		} else if (strcmp(opt, "-4") == 0) {
194			preferred_family = AF_INET;
195		} else if (strcmp(opt, "-6") == 0) {
196			preferred_family = AF_INET6;
197		} else if (strcmp(opt, "-0") == 0) {
198			preferred_family = AF_PACKET;
199		} else if (strcmp(opt, "-I") == 0) {
200			preferred_family = AF_IPX;
201		} else if (strcmp(opt, "-D") == 0) {
202			preferred_family = AF_DECnet;
203		} else if (matches(opt, "-stats") == 0 ||
204			   matches(opt, "-statistics") == 0) {
205			++show_stats;
206		} else if (matches(opt, "-details") == 0) {
207			++show_details;
208		} else if (matches(opt, "-resolve") == 0) {
209			++resolve_hosts;
210		} else if (matches(opt, "-oneline") == 0) {
211			++oneline;
212		} else if (matches(opt, "-timestamp") == 0) {
213			++timestamp;
214#if 0
215		} else if (matches(opt, "-numeric") == 0) {
216			rtnl_names_numeric++;
217#endif
218		} else if (matches(opt, "-Version") == 0) {
219			printf("ip utility, iproute2-ss%s\n", SNAPSHOT);
220			exit(0);
221		} else if (matches(opt, "-force") == 0) {
222			++force;
223#ifndef ANDROID
224		} else if (matches(opt, "-batch") == 0) {
225			argc--;
226			argv++;
227			if (argc <= 1)
228				usage();
229			batch_file = argv[1];
230#endif
231		} else if (matches(opt, "-rcvbuf") == 0) {
232			unsigned int size;
233
234			argc--;
235			argv++;
236			if (argc <= 1)
237				usage();
238			if (get_unsigned(&size, argv[1], 0)) {
239				fprintf(stderr, "Invalid rcvbuf size '%s'\n",
240					argv[1]);
241				exit(-1);
242			}
243			rcvbuf = size;
244		} else if (matches(opt, "-help") == 0) {
245			usage();
246		} else {
247			fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
248			exit(-1);
249		}
250		argc--;	argv++;
251	}
252
253	_SL_ = oneline ? "\\" : "\n" ;
254
255#ifndef ANDROID
256	if (batch_file)
257		return batch(batch_file);
258#endif
259
260	if (rtnl_open(&rth, 0) < 0)
261		exit(1);
262
263	if (strlen(basename) > 2)
264		return do_cmd(basename+2, argc, argv);
265
266	if (argc > 1)
267		return do_cmd(argv[1], argc-1, argv+1);
268
269	rtnl_close(&rth);
270	usage();
271}
272