"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | e0a6220 | 2010-07-24 23:26:58 +0200 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
| 7 | * Changes: |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 8 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 9 | * Bernhard Reutner-Fischer rewrote to use index_in_substr_array |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 12 | /* 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 |
| 53 | //usage: "{ list | flush | { add | del | change | append |\n" |
| 54 | //usage: " replace | monitor } ROUTE }" |
| 55 | //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 Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 58 | //usage: " [oif STRING] [tos TOS]\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 59 | //usage: "iproute { add | del | change | append | replace | monitor } ROUTE\n" |
| 60 | //usage: " SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n" |
| 61 | //usage: " ROUTE := [TYPE] PREFIX [tos TOS] [proto RTPROTO]\n" |
| 62 | //usage: " [metric METRIC]" |
| 63 | //usage: |
| 64 | //usage:#define iprule_trivial_usage |
| 65 | //usage: "{[list | add | del] RULE}" |
| 66 | //usage:#define iprule_full_usage "\n\n" |
| 67 | //usage: "iprule [list | add | del] SELECTOR ACTION\n" |
| 68 | //usage: " SELECTOR := [from PREFIX] [to PREFIX] [tos TOS] [fwmark FWMARK]\n" |
| 69 | //usage: " [dev STRING] [pref NUMBER]\n" |
| 70 | //usage: " ACTION := [table TABLE_ID] [nat ADDRESS]\n" |
| 71 | //usage: " [prohibit | reject | unreachable]\n" |
| 72 | //usage: " [realms [SRCREALM/]DSTREALM]\n" |
| 73 | //usage: " TABLE_ID := [local | main | default | NUMBER]" |
| 74 | //usage: |
| 75 | //usage:#define iptunnel_trivial_usage |
| 76 | //usage: "{ add | change | del | show } [NAME]\n" |
| 77 | //usage: " [mode { ipip | gre | sit }]\n" |
| 78 | //usage: " [remote ADDR] [local ADDR] [ttl TTL]" |
| 79 | //usage:#define iptunnel_full_usage "\n\n" |
| 80 | //usage: "iptunnel { add | change | del | show } [NAME]\n" |
| 81 | //usage: " [mode { ipip | gre | sit }] [remote ADDR] [local ADDR]\n" |
| 82 | //usage: " [[i|o]seq] [[i|o]key KEY] [[i|o]csum]\n" |
| 83 | //usage: " [ttl TTL] [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]" |
| 84 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 85 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 86 | |
Bernhard Reutner-Fischer | ab18782 | 2005-10-26 10:47:26 +0000 | [diff] [blame] | 87 | #include "libiproute/utils.h" |
| 88 | #include "libiproute/ip_common.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | d46e6d1 | 2007-05-17 12:58:30 +0000 | [diff] [blame] | 90 | #if ENABLE_FEATURE_IP_ADDRESS \ |
| 91 | || ENABLE_FEATURE_IP_ROUTE \ |
| 92 | || ENABLE_FEATURE_IP_LINK \ |
| 93 | || ENABLE_FEATURE_IP_TUNNEL \ |
| 94 | || ENABLE_FEATURE_IP_RULE |
| 95 | |
Denys Vlasenko | 2e9b551 | 2010-07-24 23:27:38 +0200 | [diff] [blame] | 96 | static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM) |
Bernhard Reutner-Fischer | e87d795 | 2007-04-10 18:43:27 +0000 | [diff] [blame] | 97 | { |
| 98 | bb_show_usage(); |
| 99 | } |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 100 | |
Denys Vlasenko | 2e9b551 | 2010-07-24 23:27:38 +0200 | [diff] [blame] | 101 | typedef int FAST_FUNC (*ip_func_ptr_t)(char**); |
Denys Vlasenko | e0a6220 | 2010-07-24 23:26:58 +0200 | [diff] [blame] | 102 | |
| 103 | static int ip_do(ip_func_ptr_t ip_func, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 104 | { |
Denis Vlasenko | 3246c0d | 2008-12-04 12:15:19 +0000 | [diff] [blame] | 105 | argv = ip_parse_common_args(argv + 1); |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 106 | return ip_func(argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | #if ENABLE_FEATURE_IP_ADDRESS |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 110 | int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 111 | int ipaddr_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 112 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 113 | return ip_do(do_ipaddr, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 114 | } |
| 115 | #endif |
| 116 | #if ENABLE_FEATURE_IP_LINK |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 117 | int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 118 | int iplink_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 119 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 120 | return ip_do(do_iplink, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 121 | } |
| 122 | #endif |
| 123 | #if ENABLE_FEATURE_IP_ROUTE |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 124 | int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 125 | int iproute_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 126 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 127 | return ip_do(do_iproute, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 128 | } |
| 129 | #endif |
| 130 | #if ENABLE_FEATURE_IP_RULE |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 131 | int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 132 | int iprule_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 133 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 134 | return ip_do(do_iprule, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 135 | } |
| 136 | #endif |
| 137 | #if ENABLE_FEATURE_IP_TUNNEL |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 138 | int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 139 | int iptunnel_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 140 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 141 | return ip_do(do_iptunnel, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 142 | } |
| 143 | #endif |
| 144 | |
| 145 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 146 | int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 147 | int ip_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 148 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 149 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 150 | IF_FEATURE_IP_ADDRESS("address\0") |
| 151 | IF_FEATURE_IP_ROUTE("route\0") |
Denys Vlasenko | e0a6220 | 2010-07-24 23:26:58 +0200 | [diff] [blame] | 152 | IF_FEATURE_IP_ROUTE("r\0") |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 153 | IF_FEATURE_IP_LINK("link\0") |
Denys Vlasenko | e0a6220 | 2010-07-24 23:26:58 +0200 | [diff] [blame] | 154 | IF_FEATURE_IP_TUNNEL("tunnel\0") |
| 155 | IF_FEATURE_IP_TUNNEL("tunl\0") |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 156 | IF_FEATURE_IP_RULE("rule\0") |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 157 | ; |
Denys Vlasenko | e0a6220 | 2010-07-24 23:26:58 +0200 | [diff] [blame] | 158 | static const ip_func_ptr_t ip_func_ptrs[] = { |
| 159 | ip_print_help, |
| 160 | IF_FEATURE_IP_ADDRESS(do_ipaddr,) |
| 161 | IF_FEATURE_IP_ROUTE(do_iproute,) |
| 162 | IF_FEATURE_IP_ROUTE(do_iproute,) |
| 163 | IF_FEATURE_IP_LINK(do_iplink,) |
| 164 | IF_FEATURE_IP_TUNNEL(do_iptunnel,) |
| 165 | IF_FEATURE_IP_TUNNEL(do_iptunnel,) |
| 166 | IF_FEATURE_IP_RULE(do_iprule,) |
Bernhard Reutner-Fischer | e87d795 | 2007-04-10 18:43:27 +0000 | [diff] [blame] | 167 | }; |
Denys Vlasenko | e0a6220 | 2010-07-24 23:26:58 +0200 | [diff] [blame] | 168 | ip_func_ptr_t ip_func; |
| 169 | int key; |
Bernhard Reutner-Fischer | e87d795 | 2007-04-10 18:43:27 +0000 | [diff] [blame] | 170 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 171 | argv = ip_parse_common_args(argv + 1); |
Denys Vlasenko | e0a6220 | 2010-07-24 23:26:58 +0200 | [diff] [blame] | 172 | key = *argv ? index_in_substrings(keywords, *argv++) : -1; |
| 173 | ip_func = ip_func_ptrs[key + 1]; |
| 174 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 175 | return ip_func(argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 176 | } |
Denis Vlasenko | d46e6d1 | 2007-05-17 12:58:30 +0000 | [diff] [blame] | 177 | |
| 178 | #endif /* any of ENABLE_FEATURE_IP_xxx is 1 */ |