blob: ddfe74e9cddf2af2d4232c4c9bb8c14e278faa9f [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Denys Vlasenkoe0a62202010-07-24 23:26:58 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Changes:
Denys Vlasenkofb132e42010-10-29 11:46:52 +02008 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00009 * Bernhard Reutner-Fischer rewrote to use index_in_substr_array
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000010 */
11
Pere Orga5bc8c002011-04-11 03:29:49 +020012/* would need to make the " | " optional depending on more than one selected: */
13//usage:#define ip_trivial_usage
14//usage: "[OPTIONS] {"
15//usage: IF_FEATURE_IP_ADDRESS("address | ")
16//usage: IF_FEATURE_IP_ROUTE("route | ")
17//usage: IF_FEATURE_IP_LINK("link | ")
18//usage: IF_FEATURE_IP_TUNNEL("tunnel | ")
Curt Brune69934702015-10-14 12:53:47 +020019//usage: IF_FEATURE_IP_NEIGH("neigh | ")
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage: IF_FEATURE_IP_RULE("rule")
21//usage: "} {COMMAND}"
22//usage:#define ip_full_usage "\n\n"
23//usage: "ip [OPTIONS] OBJECT {COMMAND}\n"
24//usage: "where OBJECT := {"
25//usage: IF_FEATURE_IP_ADDRESS("address | ")
26//usage: IF_FEATURE_IP_ROUTE("route | ")
27//usage: IF_FEATURE_IP_LINK("link | ")
28//usage: IF_FEATURE_IP_TUNNEL("tunnel | ")
Curt Brune69934702015-10-14 12:53:47 +020029//usage: IF_FEATURE_IP_NEIGH("neigh | ")
Pere Orga5bc8c002011-04-11 03:29:49 +020030//usage: IF_FEATURE_IP_RULE("rule")
31//usage: "}\n"
32//usage: "OPTIONS := { -f[amily] { inet | inet6 | link } | -o[neline] }"
33//usage:
34//usage:#define ipaddr_trivial_usage
35//usage: "{ {add|del} IFADDR dev STRING | {show|flush}\n"
36//usage: " [dev STRING] [to PREFIX] }"
37//usage:#define ipaddr_full_usage "\n\n"
Michael Tokarev6a7cd3d2015-05-20 16:27:44 +030038//usage: "ipaddr {add|change|replace|delete} IFADDR dev STRING\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020039//usage: "ipaddr {show|flush} [dev STRING] [scope SCOPE-ID]\n"
40//usage: " [to PREFIX] [label PATTERN]\n"
41//usage: " IFADDR := PREFIX | ADDR peer PREFIX\n"
42//usage: " [broadcast ADDR] [anycast ADDR]\n"
43//usage: " [label STRING] [scope SCOPE-ID]\n"
44//usage: " SCOPE-ID := [host | link | global | NUMBER]"
45//usage:
46//usage:#define iplink_trivial_usage
47//usage: "{ set DEVICE { up | down | arp { on | off } | show [DEVICE] }"
48//usage:#define iplink_full_usage "\n\n"
49//usage: "iplink set DEVICE { up | down | arp | multicast { on | off } |\n"
50//usage: " dynamic { on | off } |\n"
51//usage: " mtu MTU }\n"
52//usage: "iplink show [DEVICE]"
53//usage:
54//usage:#define iproute_trivial_usage
Denys Vlasenko53782d92011-12-06 18:59:55 +010055//usage: "{ list | flush | add | del | change | append |\n"
56//usage: " replace | test } ROUTE"
Pere Orga5bc8c002011-04-11 03:29:49 +020057//usage:#define iproute_full_usage "\n\n"
58//usage: "iproute { list | flush } SELECTOR\n"
59//usage: "iproute get ADDRESS [from ADDRESS iif STRING]\n"
Denys Vlasenko53782d92011-12-06 18:59:55 +010060//usage: " [oif STRING] [tos TOS]\n"
61//usage: "iproute { add | del | change | append | replace | test } ROUTE\n"
62//usage: " SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n"
63//usage: " ROUTE := [TYPE] PREFIX [tos TOS] [proto RTPROTO] [metric METRIC]"
Pere Orga5bc8c002011-04-11 03:29:49 +020064//usage:
65//usage:#define iprule_trivial_usage
66//usage: "{[list | add | del] RULE}"
67//usage:#define iprule_full_usage "\n\n"
68//usage: "iprule [list | add | del] SELECTOR ACTION\n"
69//usage: " SELECTOR := [from PREFIX] [to PREFIX] [tos TOS] [fwmark FWMARK]\n"
70//usage: " [dev STRING] [pref NUMBER]\n"
71//usage: " ACTION := [table TABLE_ID] [nat ADDRESS]\n"
72//usage: " [prohibit | reject | unreachable]\n"
73//usage: " [realms [SRCREALM/]DSTREALM]\n"
74//usage: " TABLE_ID := [local | main | default | NUMBER]"
75//usage:
76//usage:#define iptunnel_trivial_usage
77//usage: "{ add | change | del | show } [NAME]\n"
78//usage: " [mode { ipip | gre | sit }]\n"
79//usage: " [remote ADDR] [local ADDR] [ttl TTL]"
80//usage:#define iptunnel_full_usage "\n\n"
81//usage: "iptunnel { add | change | del | show } [NAME]\n"
82//usage: " [mode { ipip | gre | sit }] [remote ADDR] [local ADDR]\n"
83//usage: " [[i|o]seq] [[i|o]key KEY] [[i|o]csum]\n"
84//usage: " [ttl TTL] [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]"
Curt Brune69934702015-10-14 12:53:47 +020085//usage:
86//usage:#define ipneigh_trivial_usage
87//usage: "{ show | flush} [ to PREFIX ] [ dev DEV ] [ nud STATE ]"
88//usage:#define ipneigh_full_usage "\n\n"
89//usage: "ipneigh { show | flush} [ to PREFIX ] [ dev DEV ] [ nud STATE ]"
Pere Orga5bc8c002011-04-11 03:29:49 +020090
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000091#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000092
Bernhard Reutner-Fischerab187822005-10-26 10:47:26 +000093#include "libiproute/utils.h"
94#include "libiproute/ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095
Denis Vlasenkod46e6d12007-05-17 12:58:30 +000096#if ENABLE_FEATURE_IP_ADDRESS \
97 || ENABLE_FEATURE_IP_ROUTE \
98 || ENABLE_FEATURE_IP_LINK \
99 || ENABLE_FEATURE_IP_TUNNEL \
Curt Brune69934702015-10-14 12:53:47 +0200100 || ENABLE_FEATURE_IP_RULE \
101 || ENABLE_FEATURE_IP_NEIGH
Denis Vlasenkod46e6d12007-05-17 12:58:30 +0000102
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200103static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM)
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +0000104{
105 bb_show_usage();
106}
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000107
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200108typedef int FAST_FUNC (*ip_func_ptr_t)(char**);
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200109
110static int ip_do(ip_func_ptr_t ip_func, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000111{
Denis Vlasenko3246c0d2008-12-04 12:15:19 +0000112 argv = ip_parse_common_args(argv + 1);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000113 return ip_func(argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000114}
115
116#if ENABLE_FEATURE_IP_ADDRESS
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000117int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000118int ipaddr_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000119{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000120 return ip_do(do_ipaddr, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000121}
122#endif
123#if ENABLE_FEATURE_IP_LINK
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000124int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000125int iplink_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000126{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000127 return ip_do(do_iplink, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000128}
129#endif
130#if ENABLE_FEATURE_IP_ROUTE
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000131int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000132int iproute_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000133{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000134 return ip_do(do_iproute, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000135}
136#endif
137#if ENABLE_FEATURE_IP_RULE
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000138int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000139int iprule_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000140{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000141 return ip_do(do_iprule, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000142}
143#endif
144#if ENABLE_FEATURE_IP_TUNNEL
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000145int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000146int iptunnel_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000147{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000148 return ip_do(do_iptunnel, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000149}
150#endif
Curt Brune69934702015-10-14 12:53:47 +0200151#if ENABLE_FEATURE_IP_NEIGH
152int ipneigh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
153int ipneigh_main(int argc UNUSED_PARAM, char **argv)
154{
155 return ip_do(do_ipneigh, argv);
156}
157#endif
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000158
159
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000160int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000161int ip_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf112daa2002-12-01 23:04:06 +0000162{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000163 static const char keywords[] ALIGN1 =
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000164 IF_FEATURE_IP_ADDRESS("address\0")
165 IF_FEATURE_IP_ROUTE("route\0")
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200166 IF_FEATURE_IP_ROUTE("r\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000167 IF_FEATURE_IP_LINK("link\0")
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200168 IF_FEATURE_IP_TUNNEL("tunnel\0")
169 IF_FEATURE_IP_TUNNEL("tunl\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000170 IF_FEATURE_IP_RULE("rule\0")
Curt Brune69934702015-10-14 12:53:47 +0200171 IF_FEATURE_IP_NEIGH("neigh\0")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000172 ;
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200173 static const ip_func_ptr_t ip_func_ptrs[] = {
174 ip_print_help,
175 IF_FEATURE_IP_ADDRESS(do_ipaddr,)
176 IF_FEATURE_IP_ROUTE(do_iproute,)
177 IF_FEATURE_IP_ROUTE(do_iproute,)
178 IF_FEATURE_IP_LINK(do_iplink,)
179 IF_FEATURE_IP_TUNNEL(do_iptunnel,)
180 IF_FEATURE_IP_TUNNEL(do_iptunnel,)
181 IF_FEATURE_IP_RULE(do_iprule,)
Curt Brune69934702015-10-14 12:53:47 +0200182 IF_FEATURE_IP_NEIGH(do_ipneigh,)
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +0000183 };
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200184 ip_func_ptr_t ip_func;
185 int key;
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +0000186
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000187 argv = ip_parse_common_args(argv + 1);
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200188 key = *argv ? index_in_substrings(keywords, *argv++) : -1;
189 ip_func = ip_func_ptrs[key + 1];
190
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000191 return ip_func(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000192}
Denis Vlasenkod46e6d12007-05-17 12:58:30 +0000193
194#endif /* any of ENABLE_FEATURE_IP_xxx is 1 */