config: add size information for udhcpc
[oweals/busybox.git] / networking / ip.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4  *
5  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  *
7  * Changes:
8  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
9  * Bernhard Reutner-Fischer rewrote to use index_in_substr_array
10  */
11 //config:config IP
12 //config:       bool "ip (35 kb)"
13 //config:       default y
14 //config:       select PLATFORM_LINUX
15 //config:       help
16 //config:       The "ip" applet is a TCP/IP interface configuration and routing
17 //config:       utility.
18 //config:       Short forms (enabled below) are busybox-specific extensions.
19 //config:       The standard "ip" utility does not provide them. If you are
20 //config:       trying to be portable, it's better to use "ip CMD" forms.
21 //config:
22 //config:config IPADDR
23 //config:       bool "ipaddr (14 kb)"
24 //config:       default y
25 //config:       select FEATURE_IP_ADDRESS
26 //config:       select PLATFORM_LINUX
27 //config:       help
28 //config:       Short form of "ip addr"
29 //config:
30 //config:config IPLINK
31 //config:       bool "iplink (17 kb)"
32 //config:       default y
33 //config:       select FEATURE_IP_LINK
34 //config:       select PLATFORM_LINUX
35 //config:       help
36 //config:       Short form of "ip link"
37 //config:
38 //config:config IPROUTE
39 //config:       bool "iproute (15 kb)"
40 //config:       default y
41 //config:       select FEATURE_IP_ROUTE
42 //config:       select PLATFORM_LINUX
43 //config:       help
44 //config:       Short form of "ip route"
45 //config:
46 //config:config IPTUNNEL
47 //config:       bool "iptunnel (9.6 kb)"
48 //config:       default y
49 //config:       select FEATURE_IP_TUNNEL
50 //config:       select PLATFORM_LINUX
51 //config:       help
52 //config:       Short form of "ip tunnel"
53 //config:
54 //config:config IPRULE
55 //config:       bool "iprule (10 kb)"
56 //config:       default y
57 //config:       select FEATURE_IP_RULE
58 //config:       select PLATFORM_LINUX
59 //config:       help
60 //config:       Short form of "ip rule"
61 //config:
62 //config:config IPNEIGH
63 //config:       bool "ipneigh (8.3 kb)"
64 //config:       default y
65 //config:       select FEATURE_IP_NEIGH
66 //config:       select PLATFORM_LINUX
67 //config:       help
68 //config:       Short form of "ip neigh"
69 //config:
70 //config:config FEATURE_IP_ADDRESS
71 //config:       bool "ip address"
72 //config:       default y
73 //config:       depends on IP || IPADDR
74 //config:       help
75 //config:       Address manipulation support for the "ip" applet.
76 //config:
77 //config:config FEATURE_IP_LINK
78 //config:       bool "ip link"
79 //config:       default y
80 //config:       depends on IP || IPLINK
81 //config:       help
82 //config:       Configure network devices with "ip".
83 //config:
84 //config:config FEATURE_IP_ROUTE
85 //config:       bool "ip route"
86 //config:       default y
87 //config:       depends on IP || IPROUTE
88 //config:       help
89 //config:       Add support for routing table management to "ip".
90 //config:
91 //config:config FEATURE_IP_ROUTE_DIR
92 //config:       string "ip route configuration directory"
93 //config:       default "/etc/iproute2"
94 //config:       depends on FEATURE_IP_ROUTE
95 //config:       help
96 //config:       Location of the "ip" applet routing configuration.
97 //config:
98 //config:config FEATURE_IP_TUNNEL
99 //config:       bool "ip tunnel"
100 //config:       default y
101 //config:       depends on IP || IPTUNNEL
102 //config:       help
103 //config:       Add support for tunneling commands to "ip".
104 //config:
105 //config:config FEATURE_IP_RULE
106 //config:       bool "ip rule"
107 //config:       default y
108 //config:       depends on IP || IPRULE
109 //config:       help
110 //config:       Add support for rule commands to "ip".
111 //config:
112 //config:config FEATURE_IP_NEIGH
113 //config:       bool "ip neighbor"
114 //config:       default y
115 //config:       depends on IP || IPNEIGH
116 //config:       help
117 //config:       Add support for neighbor commands to "ip".
118 //config:
119 //config:config FEATURE_IP_RARE_PROTOCOLS
120 //config:       bool "Support displaying rarely used link types"
121 //config:       default n
122 //config:       depends on IP || IPADDR || IPLINK || IPROUTE || IPTUNNEL || IPRULE || IPNEIGH
123 //config:       help
124 //config:       If you are not going to use links of type "frad", "econet",
125 //config:       "bif" etc, you probably don't need to enable this.
126 //config:       Ethernet, wireless, infrared, ppp/slip, ip tunnelling
127 //config:       link types are supported without this option selected.
128
129 //applet:IF_IP(      APPLET_NOEXEC(ip      , ip      , BB_DIR_SBIN, BB_SUID_DROP, ip      ))
130 //applet:IF_IPADDR(  APPLET_NOEXEC(ipaddr  , ipaddr  , BB_DIR_SBIN, BB_SUID_DROP, ipaddr  ))
131 //applet:IF_IPLINK(  APPLET_NOEXEC(iplink  , iplink  , BB_DIR_SBIN, BB_SUID_DROP, iplink  ))
132 //applet:IF_IPROUTE( APPLET_NOEXEC(iproute , iproute , BB_DIR_SBIN, BB_SUID_DROP, iproute ))
133 //applet:IF_IPRULE(  APPLET_NOEXEC(iprule  , iprule  , BB_DIR_SBIN, BB_SUID_DROP, iprule  ))
134 //applet:IF_IPTUNNEL(APPLET_NOEXEC(iptunnel, iptunnel, BB_DIR_SBIN, BB_SUID_DROP, iptunnel))
135 //applet:IF_IPNEIGH( APPLET_NOEXEC(ipneigh , ipneigh , BB_DIR_SBIN, BB_SUID_DROP, ipneigh ))
136
137 //kbuild:lib-$(CONFIG_IP) += ip.o
138 //kbuild:lib-$(CONFIG_IPADDR) += ip.o
139 //kbuild:lib-$(CONFIG_IPLINK) += ip.o
140 //kbuild:lib-$(CONFIG_IPROUTE) += ip.o
141 //kbuild:lib-$(CONFIG_IPRULE) += ip.o
142 //kbuild:lib-$(CONFIG_IPTUNNEL) += ip.o
143 //kbuild:lib-$(CONFIG_IPNEIGH) += ip.o
144
145 //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
146 //usage:#define ipaddr_trivial_usage
147 //usage:       "add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]"
148 //usage:#define ipaddr_full_usage "\n\n"
149 //usage:       "ipaddr add|change|replace|delete dev IFACE IFADDR\n"
150 //usage:       "        IFADDR := PREFIX | ADDR peer PREFIX [broadcast ADDR|+|-]\n"
151 //usage:       "                [anycast ADDR] [label STRING] [scope SCOPE]\n"
152 //usage:       "        PREFIX := ADDR[/MASK]\n"
153 //usage:       "        SCOPE := [host|link|global|NUMBER]\n"
154 //usage:       "ipaddr show|flush [dev IFACE] [scope SCOPE] [to PREFIX] [label PATTERN]"
155 //usage:
156 //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
157 //usage:#define iplink_trivial_usage
158 //usage:       /*Usage:iplink*/"set IFACE [up|down] [arp on|off] [multicast on|off]\n"
159 //usage:       "        [promisc on|off] [mtu NUM] [name NAME] [qlen NUM] [address MAC]\n"
160 //usage:       "        [master IFACE | nomaster]"
161 // * short help shows only "set" command, long help continues (with just one "\n")
162 // * and shows all other commands:
163 //usage:#define iplink_full_usage "\n"
164 //usage:       "iplink add [link IFACE] IFACE [address MAC] type TYPE [ARGS]\n"
165 //usage:       "iplink delete IFACE type TYPE [ARGS]\n"
166 //usage:       "        TYPE ARGS := vlan VLANARGS | vrf table NUM\n"
167 //usage:       "        VLANARGS := id VLANID [protocol 802.1q|802.1ad] [reorder_hdr on|off]\n"
168 //usage:       "                [gvrp on|off] [mvrp on|off] [loose_binding on|off]\n"
169 //usage:       "iplink show [IFACE]"
170 //upstream man ip-link:
171 //=====================
172 //ip link add [link DEV] [ name ] NAME
173 //                   [ txqueuelen PACKETS ]
174 //                   [ address LLADDR ]
175 //                   [ broadcast LLADDR ]
176 //                   [ mtu MTU ] [index IDX ]
177 //                   [ numtxqueues QUEUE_COUNT ]
178 //                   [ numrxqueues QUEUE_COUNT ]
179 //                   type TYPE [ ARGS ]
180 //       ip link delete { DEVICE | dev DEVICE | group DEVGROUP } type TYPE [ ARGS ]
181 //       ip link set { DEVICE | dev DEVICE | group DEVGROUP } [ { up | down } ]
182 //                      [ arp { on | off } ]
183 //                      [ dynamic { on | off } ]
184 //                      [ multicast { on | off } ]
185 //                      [ allmulticast { on | off } ]
186 //                      [ promisc { on | off } ]
187 //                      [ trailers { on | off } ]
188 //                      [ txqueuelen PACKETS ]
189 //                      [ name NEWNAME ]
190 //                      [ address LLADDR ]
191 //                      [ broadcast LLADDR ]
192 //                      [ mtu MTU ]
193 //                      [ netns { PID | NAME } ]
194 //                      [ link-netnsid ID ]
195 //            [ alias NAME ]
196 //                      [ vf NUM [ mac LLADDR ]
197 //                 [ vlan VLANID [ qos VLAN-QOS ] ]
198 //                 [ rate TXRATE ]
199 //                 [ spoofchk { on | off} ]
200 //                 [ query_rss { on | off} ]
201 //                 [ state { auto | enable | disable} ] ]
202 //                 [ trust { on | off} ] ]
203 //            [ master DEVICE ]
204 //            [ nomaster ]
205 //            [ addrgenmode { eui64 | none | stable_secret | random } ]
206 //                      [ protodown { on | off } ]
207 //       ip link show [ DEVICE | group GROUP ] [up] [master DEV] [type TYPE]
208 //       ip link help [ TYPE ]
209 //TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | macvtap |
210 //          bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan |
211 //          gre | gretap | ip6gre | ip6gretap | vti | nlmon |
212 //          bond_slave | ipvlan | geneve | bridge_slave | vrf }
213 //usage:
214 //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
215 //usage:#define iproute_trivial_usage
216 //usage:       "list|flush|add|del|change|append|replace|test ROUTE"
217 //usage:#define iproute_full_usage "\n\n"
218 //usage:       "iproute list|flush SELECTOR\n"
219 //usage:       "        SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n"
220 //usage:       "        PREFIX := default|ADDR[/MASK]\n"
221 //usage:       "iproute get ADDR [from ADDR iif IFACE]\n"
222 //usage:       "        [oif IFACE] [tos TOS]\n"
223 //usage:       "iproute add|del|change|append|replace|test ROUTE\n"
224 //usage:       "        ROUTE := NODE_SPEC [INFO_SPEC]\n"
225 //usage:       "        NODE_SPEC := PREFIX"IF_FEATURE_IP_RULE(" [table TABLE_ID]")" [proto RTPROTO] [scope SCOPE] [metric METRIC]\n"
226 //usage:       "        INFO_SPEC := NH OPTIONS\n"
227 //usage:       "        NH := [via [inet|inet6] ADDR] [dev IFACE] [src ADDR] [onlink]\n"
228 //usage:       "        OPTIONS := [mtu [lock] NUM] [advmss [lock] NUM]"
229 //upstream man ip-route:
230 //======================
231 //ip route { show | flush } SELECTOR
232 //ip route save SELECTOR
233 //ip route restore
234 //ip route get ADDRESS [ from ADDRESS iif STRING  ] [ oif STRING ] [ tos TOS ]
235 //ip route { add | del | change | append | replace } ROUTE
236 //SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ] [ table TABLE_ID ] [ proto RTPROTO ] [ type TYPE ] [ scope SCOPE ]
237 //ROUTE := NODE_SPEC [ INFO_SPEC ]
238 //NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ] [ table TABLE_ID ] [ proto RTPROTO ] [ scope SCOPE ] [ metric METRIC ]
239 //INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ] ...
240 //NH := [ encap ENCAP ] [ via [ FAMILY ] ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS
241 // ..............................................................^ I guess [src ADDRESS] should be here
242 //FAMILY := [ inet | inet6 | ipx | dnet | mpls | bridge | link ]
243 //OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ] [ as [ to ] ADDRESS ] rtt TIME ] [ rttvar TIME ] [ reordering NUMBER ] [ window NUMBER ] [ cwnd NUMBER ] [ ssthresh REALM ] [ realms REALM ]
244 //        [ rto_min TIME ] [ initcwnd NUMBER ] [ initrwnd NUMBER ] [ features FEATURES ] [ quickack BOOL ] [ congctl NAME ] [ pref PREF ] [ expires TIME ]
245 //TYPE := [ unicast | local | broadcast | multicast | throw | unreachable | prohibit | blackhole | nat ]
246 //TABLE_ID := [ local | main | default | all | NUMBER ]
247 //SCOPE := [ host | link | global | NUMBER ]
248 //NHFLAGS := [ onlink | pervasive ]
249 //RTPROTO := [ kernel | boot | static | NUMBER ]
250 //FEATURES := [ ecn | ]
251 //PREF := [ low | medium | high ]
252 //ENCAP := [ MPLS | IP ]
253 //ENCAP_MPLS := mpls [ LABEL ]
254 //ENCAP_IP := ip id TUNNEL_ID dst REMOTE_IP [ tos TOS ] [ ttl TTL ]
255 //usage:
256 //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
257 //usage:#define iprule_trivial_usage
258 //usage:       "[list] | add|del SELECTOR ACTION"
259 //usage:#define iprule_full_usage "\n\n"
260 //usage:       "        SELECTOR := [from PREFIX] [to PREFIX] [tos TOS] [fwmark FWMARK]\n"
261 //usage:       "                        [dev IFACE] [pref NUMBER]\n"
262 //usage:       "        ACTION := [table TABLE_ID] [nat ADDR]\n"
263 //usage:       "                        [prohibit|reject|unreachable]\n"
264 //usage:       "                        [realms [SRCREALM/]DSTREALM]\n"
265 //usage:       "        TABLE_ID := [local|main|default|NUMBER]"
266 //usage:
267 //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
268 //usage:#define iptunnel_trivial_usage
269 //usage:       "add|change|del|show [NAME]\n"
270 //usage:       "        [mode ipip|gre|sit] [remote ADDR] [local ADDR] [ttl TTL]"
271 //usage:#define iptunnel_full_usage "\n\n"
272 //usage:       "iptunnel add|change|del|show [NAME]\n"
273 //usage:       "        [mode ipip|gre|sit] [remote ADDR] [local ADDR]\n"
274 //usage:       "        [[i|o]seq] [[i|o]key KEY] [[i|o]csum]\n"
275 //usage:       "        [ttl TTL] [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]"
276 //usage:
277 //usage:#define ipneigh_trivial_usage
278 //usage:       "show|flush [to PREFIX] [dev DEV] [nud STATE]"
279 //usage:#define ipneigh_full_usage ""
280 //usage:
281 //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE
282 //usage:# define IP_BAR_LINK   "|"
283 //usage:#else
284 //usage:# define IP_BAR_LINK   ""
285 //usage:#endif
286 //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK
287 //usage:# define IP_BAR_TUNNEL "|"
288 //usage:#else
289 //usage:# define IP_BAR_TUNNEL ""
290 //usage:#endif
291 //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK || ENABLE_FEATURE_IP_TUNNEL
292 //usage:# define IP_BAR_NEIGH  "|"
293 //usage:#else
294 //usage:# define IP_BAR_NEIGH  ""
295 //usage:#endif
296 //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK || ENABLE_FEATURE_IP_TUNNEL || ENABLE_FEATURE_IP_NEIGH
297 //usage:# define IP_BAR_RULE   "|"
298 //usage:#else
299 //usage:# define IP_BAR_RULE   ""
300 //usage:#endif
301 //usage:
302 //usage:#define ip_trivial_usage
303 //usage:       "[OPTIONS] "
304 //usage:        IF_FEATURE_IP_ADDRESS("address")
305 //usage:        IF_FEATURE_IP_ROUTE(  IF_FEATURE_IP_ADDRESS("|")"route")
306 //usage:        IF_FEATURE_IP_LINK(   IP_BAR_LINK  "link")
307 //usage:        IF_FEATURE_IP_TUNNEL( IP_BAR_TUNNEL"tunnel")
308 //usage:        IF_FEATURE_IP_NEIGH(  IP_BAR_NEIGH "neigh")
309 //usage:        IF_FEATURE_IP_RULE(   IP_BAR_RULE  "rule")
310 //usage:       " [ARGS]"
311 //usage:#define ip_full_usage "\n\n"
312 //usage:       "OPTIONS := -f[amily] inet|inet6|link | -o[neline]\n"
313 //usage:        IF_FEATURE_IP_ADDRESS("\n"
314 //usage:        "ip addr "ipaddr_trivial_usage)
315 //usage:        IF_FEATURE_IP_ROUTE("\n"
316 //usage:        "ip route "iproute_trivial_usage)
317 //usage:        IF_FEATURE_IP_LINK("\n"
318 //usage:        "ip link "iplink_trivial_usage)
319 //usage:        IF_FEATURE_IP_TUNNEL("\n"
320 //usage:        "ip tunnel "iptunnel_trivial_usage)
321 //usage:        IF_FEATURE_IP_NEIGH("\n"
322 //usage:        "ip neigh "ipneigh_trivial_usage)
323 //usage:        IF_FEATURE_IP_RULE("\n"
324 //usage:        "ip rule "iprule_trivial_usage)
325
326 #include "libbb.h"
327
328 #include "libiproute/utils.h"
329 #include "libiproute/ip_common.h"
330
331 typedef int FAST_FUNC (*ip_func_ptr_t)(char**);
332
333 #if ENABLE_IPADDR \
334  || ENABLE_IPLINK \
335  || ENABLE_IPROUTE \
336  || ENABLE_IPRULE \
337  || ENABLE_IPTUNNEL \
338  || ENABLE_IPNEIGH
339 static int ip_do(ip_func_ptr_t ip_func, char **argv)
340 {
341         argv = ip_parse_common_args(argv + 1);
342         return ip_func(argv);
343 }
344 #endif
345
346 #if ENABLE_IPADDR
347 int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
348 int ipaddr_main(int argc UNUSED_PARAM, char **argv)
349 {
350         return ip_do(do_ipaddr, argv);
351 }
352 #endif
353 #if ENABLE_IPLINK
354 int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
355 int iplink_main(int argc UNUSED_PARAM, char **argv)
356 {
357         return ip_do(do_iplink, argv);
358 }
359 #endif
360 #if ENABLE_IPROUTE
361 int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
362 int iproute_main(int argc UNUSED_PARAM, char **argv)
363 {
364         return ip_do(do_iproute, argv);
365 }
366 #endif
367 #if ENABLE_IPRULE
368 int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
369 int iprule_main(int argc UNUSED_PARAM, char **argv)
370 {
371         return ip_do(do_iprule, argv);
372 }
373 #endif
374 #if ENABLE_IPTUNNEL
375 int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
376 int iptunnel_main(int argc UNUSED_PARAM, char **argv)
377 {
378         return ip_do(do_iptunnel, argv);
379 }
380 #endif
381 #if ENABLE_IPNEIGH
382 int ipneigh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
383 int ipneigh_main(int argc UNUSED_PARAM, char **argv)
384 {
385         return ip_do(do_ipneigh, argv);
386 }
387 #endif
388
389 #if ENABLE_IP
390 static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM)
391 {
392         bb_show_usage();
393 }
394
395 int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
396 int ip_main(int argc UNUSED_PARAM, char **argv)
397 {
398         static const char keywords[] ALIGN1 = ""
399                 IF_FEATURE_IP_ADDRESS("address\0")
400                 IF_FEATURE_IP_ROUTE("route\0")
401                 IF_FEATURE_IP_ROUTE("r\0")
402                 IF_FEATURE_IP_LINK("link\0")
403                 IF_FEATURE_IP_TUNNEL("tunnel\0")
404                 IF_FEATURE_IP_TUNNEL("tunl\0")
405                 IF_FEATURE_IP_RULE("rule\0")
406                 IF_FEATURE_IP_NEIGH("neigh\0")
407                 ;
408         static const ip_func_ptr_t ip_func_ptrs[] = {
409                 ip_print_help,
410                 IF_FEATURE_IP_ADDRESS(do_ipaddr,)
411                 IF_FEATURE_IP_ROUTE(do_iproute,)
412                 IF_FEATURE_IP_ROUTE(do_iproute,)
413                 IF_FEATURE_IP_LINK(do_iplink,)
414                 IF_FEATURE_IP_TUNNEL(do_iptunnel,)
415                 IF_FEATURE_IP_TUNNEL(do_iptunnel,)
416                 IF_FEATURE_IP_RULE(do_iprule,)
417                 IF_FEATURE_IP_NEIGH(do_ipneigh,)
418         };
419         ip_func_ptr_t ip_func;
420         int key;
421
422         argv = ip_parse_common_args(argv + 1);
423         if (ARRAY_SIZE(ip_func_ptrs) > 1 && *argv)
424                 key = index_in_substrings(keywords, *argv++);
425         else
426                 key = -1;
427         ip_func = ip_func_ptrs[key + 1];
428
429         return ip_func(argv);
430 }
431 #endif