summaryrefslogtreecommitdiffhomepage
path: root/networking/ip.c
blob: 0f52b19dd6371803020babc5866275a00e38fd5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* vi: set sw=4 ts=4: */
/*
 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
 *
 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 * Changes:
 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
 * Bernhard Reutner-Fischer rewrote to use index_in_substr_array
 */
//config:config IP
//config:	bool "ip"
//config:	default y
//config:	select PLATFORM_LINUX
//config:	help
//config:	  The "ip" applet is a TCP/IP interface configuration and routing
//config:	  utility. You generally don't need "ip" to use busybox with
//config:	  TCP/IP.
//config:
//config:config IPADDR
//config:	bool "ipaddr"
//config:	default y
//config:	select FEATURE_IP_ADDRESS
//config:	select PLATFORM_LINUX
//config:	help
//config:	  Support short form of ip addr: ipaddr
//config:
//config:config IPLINK
//config:	bool "iplink"
//config:	default y
//config:	select FEATURE_IP_LINK
//config:	select PLATFORM_LINUX
//config:	help
//config:	  Support short form of ip link: iplink
//config:
//config:config IPROUTE
//config:	bool "iproute"
//config:	default y
//config:	select FEATURE_IP_ROUTE
//config:	select PLATFORM_LINUX
//config:	help
//config:	  Support short form of ip route: iproute
//config:
//config:config IPTUNNEL
//config:	bool "iptunnel"
//config:	default y
//config:	select FEATURE_IP_TUNNEL
//config:	select PLATFORM_LINUX
//config:	help
//config:	  Support short form of ip tunnel: iptunnel
//config:
//config:config IPRULE
//config:	bool "iprule"
//config:	default y
//config:	select FEATURE_IP_RULE
//config:	select PLATFORM_LINUX
//config:	help
//config:	  Support short form of ip rule: iprule
//config:
//config:config IPNEIGH
//config:	bool "ipneigh"
//config:	default y
//config:	select FEATURE_IP_NEIGH
//config:	select PLATFORM_LINUX
//config:	help
//config:	  Support short form of ip neigh: ipneigh
//config:
//config:config FEATURE_IP_ADDRESS
//config:	bool "ip address"
//config:	default y
//config:	depends on IP || IPADDR
//config:	help
//config:	  Address manipulation support for the "ip" applet.
//config:
//config:config FEATURE_IP_LINK
//config:	bool "ip link"
//config:	default y
//config:	depends on IP || IPLINK
//config:	help
//config:	  Configure network devices with "ip".
//config:
//config:config FEATURE_IP_ROUTE
//config:	bool "ip route"
//config:	default y
//config:	depends on IP || IPROUTE
//config:	help
//config:	  Add support for routing table management to "ip".
//config:
//config:config FEATURE_IP_ROUTE_DIR
//config:	string "ip route configuration directory"
//config:	default "/etc/iproute2"
//config:	depends on FEATURE_IP_ROUTE
//config:	help
//config:	  Location of the "ip" applet routing configuration.
//config:
//config:config FEATURE_IP_TUNNEL
//config:	bool "ip tunnel"
//config:	default y
//config:	depends on IP || IPTUNNEL
//config:	help
//config:	  Add support for tunneling commands to "ip".
//config:
//config:config FEATURE_IP_RULE
//config:	bool "ip rule"
//config:	default y
//config:	depends on IP || IPRULE
//config:	help
//config:	  Add support for rule commands to "ip".
//config:
//config:config FEATURE_IP_NEIGH
//config:	bool "ip neighbor"
//config:	default y
//config:	depends on IP || IPNEIGH
//config:	help
//config:	  Add support for neighbor commands to "ip".
//config:
//config:config FEATURE_IP_RARE_PROTOCOLS
//config:	bool "Support displaying rarely used link types"
//config:	default n
//config:	depends on IP || IPADDR || IPLINK || IPROUTE || IPTUNNEL || IPRULE || IPNEIGH
//config:	help
//config:	  If you are not going to use links of type "frad", "econet",
//config:	  "bif" etc, you probably don't need to enable this.
//config:	  Ethernet, wireless, infrared, ppp/slip, ip tunnelling
//config:	  link types are supported without this option selected.

//applet:IF_IP(APPLET(ip, BB_DIR_SBIN, BB_SUID_DROP))
//applet:IF_IPADDR(APPLET(ipaddr, BB_DIR_SBIN, BB_SUID_DROP))
//applet:IF_IPLINK(APPLET(iplink, BB_DIR_SBIN, BB_SUID_DROP))
//applet:IF_IPROUTE(APPLET(iproute, BB_DIR_SBIN, BB_SUID_DROP))
//applet:IF_IPRULE(APPLET(iprule, BB_DIR_SBIN, BB_SUID_DROP))
//applet:IF_IPTUNNEL(APPLET(iptunnel, BB_DIR_SBIN, BB_SUID_DROP))
//applet:IF_IPNEIGH(APPLET(ipneigh, BB_DIR_SBIN, BB_SUID_DROP))

//kbuild:lib-$(CONFIG_IP) += ip.o
//kbuild:lib-$(CONFIG_IPADDR) += ip.o
//kbuild:lib-$(CONFIG_IPLINK) += ip.o
//kbuild:lib-$(CONFIG_IPROUTE) += ip.o
//kbuild:lib-$(CONFIG_IPRULE) += ip.o
//kbuild:lib-$(CONFIG_IPTUNNEL) += ip.o
//kbuild:lib-$(CONFIG_IPNEIGH) += ip.o

//usage:#define ipaddr_trivial_usage
//usage:       "add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]"
//usage:#define ipaddr_full_usage "\n\n"
//usage:       "ipaddr add|change|replace|delete IFADDR dev IFACE\n"
//usage:       "ipaddr show|flush [dev IFACE] [scope SCOPE-ID]\n"
//usage:       "		[to PREFIX] [label PATTERN]\n"
//usage:       "	IFADDR := PREFIX | ADDR peer PREFIX\n"
//usage:       "		[broadcast ADDR] [anycast ADDR]\n"
//usage:       "		[label STRING] [scope SCOPE-ID]\n"
//usage:       "	SCOPE-ID := [host|link|global|NUMBER]"
//usage:
//usage:#define iplink_trivial_usage
//usage:       "set IFACE [up|down] [arp on|off] | show [IFACE]"
//usage:#define iplink_full_usage "\n\n"
//usage:       "iplink set IFACE [up|down]\n"
//usage:       "	[arp on|off]\n"
//usage:       "	[dynamic on|off]\n"
//usage:       "	[multicast on|off]\n"
//usage:       "	[mtu MTU]\n"
//usage:       "iplink show [IFACE]"
//usage:
//usage:#define iproute_trivial_usage
//usage:       "list|flush|add|del|change|append|replace|test ROUTE"
//usage:#define iproute_full_usage "\n\n"
//usage:       "iproute list|flush SELECTOR\n"
//usage:       "iproute get ADDRESS [from ADDRESS iif STRING]\n"
//usage:       "	[oif STRING] [tos TOS]\n"
//usage:       "iproute add|del|change|append|replace|test ROUTE\n"
//usage:       "	SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n"
//usage:       "	ROUTE := [TYPE] PREFIX [tos TOS] [proto RTPROTO] [metric METRIC]"
//usage:
//usage:#define iprule_trivial_usage
//usage:       "[list] | add|del SELECTOR ACTION"
//usage:#define iprule_full_usage "\n\n"
//usage:       "	SELECTOR := [from PREFIX] [to PREFIX] [tos TOS] [fwmark FWMARK]\n"
//usage:       "			[dev IFACE] [pref NUMBER]\n"
//usage:       "	ACTION := [table TABLE_ID] [nat ADDRESS]\n"
//usage:       "			[prohibit|reject|unreachable]\n"
//usage:       "			[realms [SRCREALM/]DSTREALM]\n"
//usage:       "	TABLE_ID := [local|main|default|NUMBER]"
//usage:
//usage:#define iptunnel_trivial_usage
//usage:       "add|change|del|show [NAME]\n"
//usage:       "	[mode ipip|gre|sit]\n"
//usage:       "	[remote ADDR] [local ADDR] [ttl TTL]"
//usage:#define iptunnel_full_usage "\n\n"
//usage:       "iptunnel add|change|del|show [NAME]\n"
//usage:       "	[mode ipip|gre|sit] [remote ADDR] [local ADDR]\n"
//usage:       "	[[i|o]seq] [[i|o]key KEY] [[i|o]csum]\n"
//usage:       "	[ttl TTL] [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]"
//usage:
//usage:#define ipneigh_trivial_usage
//usage:       "show|flush [to PREFIX] [dev DEV] [nud STATE]"
//usage:#define ipneigh_full_usage ""
//usage:
//usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE
//usage:# define IP_BAR_LINK   "|"
//usage:#else
//usage:# define IP_BAR_LINK   ""
//usage:#endif
//usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK
//usage:# define IP_BAR_TUNNEL "|"
//usage:#else
//usage:# define IP_BAR_TUNNEL ""
//usage:#endif
//usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK || ENABLE_FEATURE_IP_TUNNEL
//usage:# define IP_BAR_NEIGH  "|"
//usage:#else
//usage:# define IP_BAR_NEIGH  ""
//usage:#endif
//usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK || ENABLE_FEATURE_IP_TUNNEL || ENABLE_FEATURE_IP_NEIGH
//usage:# define IP_BAR_RULE   "|"
//usage:#else
//usage:# define IP_BAR_RULE   ""
//usage:#endif
//usage:
//usage:#define ip_trivial_usage
//usage:       "[OPTIONS] "
//usage:	IF_FEATURE_IP_ADDRESS("address")
//usage:	IF_FEATURE_IP_ROUTE(  IF_FEATURE_IP_ADDRESS("|")"route")
//usage:	IF_FEATURE_IP_LINK(   IP_BAR_LINK  "link")
//usage:	IF_FEATURE_IP_TUNNEL( IP_BAR_TUNNEL"tunnel")
//usage:	IF_FEATURE_IP_NEIGH(  IP_BAR_NEIGH "neigh")
//usage:	IF_FEATURE_IP_RULE(   IP_BAR_RULE  "rule")
//usage:       " [COMMAND]"
//usage:#define ip_full_usage "\n\n"
//usage:       "OPTIONS := -f[amily] inet|inet6|link | -o[neline]\n"
//usage:       "COMMAND :="
//usage:	IF_FEATURE_IP_ADDRESS("\n"
//usage:	"ip addr "ipaddr_trivial_usage)
//usage:	IF_FEATURE_IP_ROUTE("\n"
//usage:	"ip route "iproute_trivial_usage)
//usage:	IF_FEATURE_IP_LINK("\n"
//usage:	"ip link "iplink_trivial_usage)
//usage:	IF_FEATURE_IP_TUNNEL("\n"
//usage:	"ip tunnel "iptunnel_trivial_usage)
//usage:	IF_FEATURE_IP_NEIGH("\n"
//usage:	"ip neigh "ipneigh_trivial_usage)
//usage:	IF_FEATURE_IP_RULE("\n"
//usage:	"ip rule "iprule_trivial_usage)

#include "libbb.h"

#include "libiproute/utils.h"
#include "libiproute/ip_common.h"

typedef int FAST_FUNC (*ip_func_ptr_t)(char**);

#if ENABLE_IPADDR \
 || ENABLE_IPLINK \
 || ENABLE_IPROUTE \
 || ENABLE_IPRULE \
 || ENABLE_IPTUNNEL \
 || ENABLE_IPNEIGH
static int ip_do(ip_func_ptr_t ip_func, char **argv)
{
	argv = ip_parse_common_args(argv + 1);
	return ip_func(argv);
}
#endif

#if ENABLE_IPADDR
int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int ipaddr_main(int argc UNUSED_PARAM, char **argv)
{
	return ip_do(do_ipaddr, argv);
}
#endif
#if ENABLE_IPLINK
int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int iplink_main(int argc UNUSED_PARAM, char **argv)
{
	return ip_do(do_iplink, argv);
}
#endif
#if ENABLE_IPROUTE
int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int iproute_main(int argc UNUSED_PARAM, char **argv)
{
	return ip_do(do_iproute, argv);
}
#endif
#if ENABLE_IPRULE
int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int iprule_main(int argc UNUSED_PARAM, char **argv)
{
	return ip_do(do_iprule, argv);
}
#endif
#if ENABLE_IPTUNNEL
int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int iptunnel_main(int argc UNUSED_PARAM, char **argv)
{
	return ip_do(do_iptunnel, argv);
}
#endif
#if ENABLE_IPNEIGH
int ipneigh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int ipneigh_main(int argc UNUSED_PARAM, char **argv)
{
	return ip_do(do_ipneigh, argv);
}
#endif

#if ENABLE_IP
static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM)
{
	bb_show_usage();
}

int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int ip_main(int argc UNUSED_PARAM, char **argv)
{
	static const char keywords[] ALIGN1 = ""
		IF_FEATURE_IP_ADDRESS("address\0")
		IF_FEATURE_IP_ROUTE("route\0")
		IF_FEATURE_IP_ROUTE("r\0")
		IF_FEATURE_IP_LINK("link\0")
		IF_FEATURE_IP_TUNNEL("tunnel\0")
		IF_FEATURE_IP_TUNNEL("tunl\0")
		IF_FEATURE_IP_RULE("rule\0")
		IF_FEATURE_IP_NEIGH("neigh\0")
		;
	static const ip_func_ptr_t ip_func_ptrs[] = {
		ip_print_help,
		IF_FEATURE_IP_ADDRESS(do_ipaddr,)
		IF_FEATURE_IP_ROUTE(do_iproute,)
		IF_FEATURE_IP_ROUTE(do_iproute,)
		IF_FEATURE_IP_LINK(do_iplink,)
		IF_FEATURE_IP_TUNNEL(do_iptunnel,)
		IF_FEATURE_IP_TUNNEL(do_iptunnel,)
		IF_FEATURE_IP_RULE(do_iprule,)
		IF_FEATURE_IP_NEIGH(do_ipneigh,)
	};
	ip_func_ptr_t ip_func;
	int key;

	argv = ip_parse_common_args(argv + 1);
	if (ARRAY_SIZE(ip_func_ptrs) > 1 && *argv)
		key = index_in_substrings(keywords, *argv++);
	else
		key = -1;
	ip_func = ip_func_ptrs[key + 1];

	return ip_func(argv);
}
#endif