blob: 98fe621b1631de7e71cc0f0889ed5320700f50e3 [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 | ")
19//usage: IF_FEATURE_IP_RULE("rule")
20//usage: "} {COMMAND}"
21//usage:#define ip_full_usage "\n\n"
22//usage: "ip [OPTIONS] OBJECT {COMMAND}\n"
23//usage: "where OBJECT := {"
24//usage: IF_FEATURE_IP_ADDRESS("address | ")
25//usage: IF_FEATURE_IP_ROUTE("route | ")
26//usage: IF_FEATURE_IP_LINK("link | ")
27//usage: IF_FEATURE_IP_TUNNEL("tunnel | ")
28//usage: IF_FEATURE_IP_RULE("rule")
29//usage: "}\n"
30//usage: "OPTIONS := { -f[amily] { inet | inet6 | link } | -o[neline] }"
31//usage:
32//usage:#define ipaddr_trivial_usage
33//usage: "{ {add|del} IFADDR dev STRING | {show|flush}\n"
34//usage: " [dev STRING] [to PREFIX] }"
35//usage:#define ipaddr_full_usage "\n\n"
36//usage: "ipaddr {add|delete} IFADDR dev STRING\n"
37//usage: "ipaddr {show|flush} [dev STRING] [scope SCOPE-ID]\n"
38//usage: " [to PREFIX] [label PATTERN]\n"
39//usage: " IFADDR := PREFIX | ADDR peer PREFIX\n"
40//usage: " [broadcast ADDR] [anycast ADDR]\n"
41//usage: " [label STRING] [scope SCOPE-ID]\n"
42//usage: " SCOPE-ID := [host | link | global | NUMBER]"
43//usage:
44//usage:#define iplink_trivial_usage
45//usage: "{ set DEVICE { up | down | arp { on | off } | show [DEVICE] }"
46//usage:#define iplink_full_usage "\n\n"
47//usage: "iplink set DEVICE { up | down | arp | multicast { on | off } |\n"
48//usage: " dynamic { on | off } |\n"
49//usage: " mtu MTU }\n"
50//usage: "iplink show [DEVICE]"
51//usage:
52//usage:#define iproute_trivial_usage
Denys Vlasenko53782d92011-12-06 18:59:55 +010053//usage: "{ list | flush | add | del | change | append |\n"
54//usage: " replace | test } ROUTE"
Pere Orga5bc8c002011-04-11 03:29:49 +020055//usage:#define iproute_full_usage "\n\n"
56//usage: "iproute { list | flush } SELECTOR\n"
57//usage: "iproute get ADDRESS [from ADDRESS iif STRING]\n"
Denys Vlasenko53782d92011-12-06 18:59:55 +010058//usage: " [oif STRING] [tos TOS]\n"
59//usage: "iproute { add | del | change | append | replace | test } ROUTE\n"
60//usage: " SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n"
61//usage: " ROUTE := [TYPE] PREFIX [tos TOS] [proto RTPROTO] [metric METRIC]"
Pere Orga5bc8c002011-04-11 03:29:49 +020062//usage:
63//usage:#define iprule_trivial_usage
64//usage: "{[list | add | del] RULE}"
65//usage:#define iprule_full_usage "\n\n"
66//usage: "iprule [list | add | del] SELECTOR ACTION\n"
67//usage: " SELECTOR := [from PREFIX] [to PREFIX] [tos TOS] [fwmark FWMARK]\n"
68//usage: " [dev STRING] [pref NUMBER]\n"
69//usage: " ACTION := [table TABLE_ID] [nat ADDRESS]\n"
70//usage: " [prohibit | reject | unreachable]\n"
71//usage: " [realms [SRCREALM/]DSTREALM]\n"
72//usage: " TABLE_ID := [local | main | default | NUMBER]"
73//usage:
74//usage:#define iptunnel_trivial_usage
75//usage: "{ add | change | del | show } [NAME]\n"
76//usage: " [mode { ipip | gre | sit }]\n"
77//usage: " [remote ADDR] [local ADDR] [ttl TTL]"
78//usage:#define iptunnel_full_usage "\n\n"
79//usage: "iptunnel { add | change | del | show } [NAME]\n"
80//usage: " [mode { ipip | gre | sit }] [remote ADDR] [local ADDR]\n"
81//usage: " [[i|o]seq] [[i|o]key KEY] [[i|o]csum]\n"
82//usage: " [ttl TTL] [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]"
83
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000084#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085
Bernhard Reutner-Fischerab187822005-10-26 10:47:26 +000086#include "libiproute/utils.h"
87#include "libiproute/ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000088
Denis Vlasenkod46e6d12007-05-17 12:58:30 +000089#if ENABLE_FEATURE_IP_ADDRESS \
90 || ENABLE_FEATURE_IP_ROUTE \
91 || ENABLE_FEATURE_IP_LINK \
92 || ENABLE_FEATURE_IP_TUNNEL \
93 || ENABLE_FEATURE_IP_RULE
94
Denys Vlasenko2e9b5512010-07-24 23:27:38 +020095static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM)
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +000096{
97 bb_show_usage();
98}
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000099
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200100typedef int FAST_FUNC (*ip_func_ptr_t)(char**);
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200101
102static int ip_do(ip_func_ptr_t ip_func, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000103{
Denis Vlasenko3246c0d2008-12-04 12:15:19 +0000104 argv = ip_parse_common_args(argv + 1);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000105 return ip_func(argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000106}
107
108#if ENABLE_FEATURE_IP_ADDRESS
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000109int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000110int ipaddr_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000111{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000112 return ip_do(do_ipaddr, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000113}
114#endif
115#if ENABLE_FEATURE_IP_LINK
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000116int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000117int iplink_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000118{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000119 return ip_do(do_iplink, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000120}
121#endif
122#if ENABLE_FEATURE_IP_ROUTE
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000123int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000124int iproute_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000125{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000126 return ip_do(do_iproute, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000127}
128#endif
129#if ENABLE_FEATURE_IP_RULE
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000130int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000131int iprule_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000132{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000133 return ip_do(do_iprule, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000134}
135#endif
136#if ENABLE_FEATURE_IP_TUNNEL
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000137int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000138int iptunnel_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000139{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000140 return ip_do(do_iptunnel, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +0000141}
142#endif
143
144
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000145int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000146int ip_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf112daa2002-12-01 23:04:06 +0000147{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000148 static const char keywords[] ALIGN1 =
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000149 IF_FEATURE_IP_ADDRESS("address\0")
150 IF_FEATURE_IP_ROUTE("route\0")
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200151 IF_FEATURE_IP_ROUTE("r\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000152 IF_FEATURE_IP_LINK("link\0")
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200153 IF_FEATURE_IP_TUNNEL("tunnel\0")
154 IF_FEATURE_IP_TUNNEL("tunl\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000155 IF_FEATURE_IP_RULE("rule\0")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000156 ;
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200157 static const ip_func_ptr_t ip_func_ptrs[] = {
158 ip_print_help,
159 IF_FEATURE_IP_ADDRESS(do_ipaddr,)
160 IF_FEATURE_IP_ROUTE(do_iproute,)
161 IF_FEATURE_IP_ROUTE(do_iproute,)
162 IF_FEATURE_IP_LINK(do_iplink,)
163 IF_FEATURE_IP_TUNNEL(do_iptunnel,)
164 IF_FEATURE_IP_TUNNEL(do_iptunnel,)
165 IF_FEATURE_IP_RULE(do_iprule,)
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +0000166 };
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200167 ip_func_ptr_t ip_func;
168 int key;
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +0000169
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000170 argv = ip_parse_common_args(argv + 1);
Denys Vlasenkoe0a62202010-07-24 23:26:58 +0200171 key = *argv ? index_in_substrings(keywords, *argv++) : -1;
172 ip_func = ip_func_ptrs[key + 1];
173
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000174 return ip_func(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175}
Denis Vlasenkod46e6d12007-05-17 12:58:30 +0000176
177#endif /* any of ENABLE_FEATURE_IP_xxx is 1 */