3
0

ip.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /* would need to make the " | " optional depending on more than one selected: */
  12. //usage:#define ip_trivial_usage
  13. //usage: "[OPTIONS] {"
  14. //usage: IF_FEATURE_IP_ADDRESS("address | ")
  15. //usage: IF_FEATURE_IP_ROUTE("route | ")
  16. //usage: IF_FEATURE_IP_LINK("link | ")
  17. //usage: IF_FEATURE_IP_TUNNEL("tunnel | ")
  18. //usage: IF_FEATURE_IP_RULE("rule")
  19. //usage: "} {COMMAND}"
  20. //usage:#define ip_full_usage "\n\n"
  21. //usage: "ip [OPTIONS] OBJECT {COMMAND}\n"
  22. //usage: "where OBJECT := {"
  23. //usage: IF_FEATURE_IP_ADDRESS("address | ")
  24. //usage: IF_FEATURE_IP_ROUTE("route | ")
  25. //usage: IF_FEATURE_IP_LINK("link | ")
  26. //usage: IF_FEATURE_IP_TUNNEL("tunnel | ")
  27. //usage: IF_FEATURE_IP_RULE("rule")
  28. //usage: "}\n"
  29. //usage: "OPTIONS := { -f[amily] { inet | inet6 | link } | -o[neline] }"
  30. //usage:
  31. //usage:#define ipaddr_trivial_usage
  32. //usage: "{ {add|del} IFADDR dev STRING | {show|flush}\n"
  33. //usage: " [dev STRING] [to PREFIX] }"
  34. //usage:#define ipaddr_full_usage "\n\n"
  35. //usage: "ipaddr {add|change|replace|delete} IFADDR dev STRING\n"
  36. //usage: "ipaddr {show|flush} [dev STRING] [scope SCOPE-ID]\n"
  37. //usage: " [to PREFIX] [label PATTERN]\n"
  38. //usage: " IFADDR := PREFIX | ADDR peer PREFIX\n"
  39. //usage: " [broadcast ADDR] [anycast ADDR]\n"
  40. //usage: " [label STRING] [scope SCOPE-ID]\n"
  41. //usage: " SCOPE-ID := [host | link | global | NUMBER]"
  42. //usage:
  43. //usage:#define iplink_trivial_usage
  44. //usage: "{ set DEVICE { up | down | arp { on | off } | show [DEVICE] }"
  45. //usage:#define iplink_full_usage "\n\n"
  46. //usage: "iplink set DEVICE { up | down | arp | multicast { on | off } |\n"
  47. //usage: " dynamic { on | off } |\n"
  48. //usage: " mtu MTU }\n"
  49. //usage: "iplink show [DEVICE]"
  50. //usage:
  51. //usage:#define iproute_trivial_usage
  52. //usage: "{ list | flush | add | del | change | append |\n"
  53. //usage: " replace | test } ROUTE"
  54. //usage:#define iproute_full_usage "\n\n"
  55. //usage: "iproute { list | flush } SELECTOR\n"
  56. //usage: "iproute get ADDRESS [from ADDRESS iif STRING]\n"
  57. //usage: " [oif STRING] [tos TOS]\n"
  58. //usage: "iproute { add | del | change | append | replace | test } ROUTE\n"
  59. //usage: " SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n"
  60. //usage: " ROUTE := [TYPE] PREFIX [tos TOS] [proto RTPROTO] [metric METRIC]"
  61. //usage:
  62. //usage:#define iprule_trivial_usage
  63. //usage: "{[list | add | del] RULE}"
  64. //usage:#define iprule_full_usage "\n\n"
  65. //usage: "iprule [list | add | del] SELECTOR ACTION\n"
  66. //usage: " SELECTOR := [from PREFIX] [to PREFIX] [tos TOS] [fwmark FWMARK]\n"
  67. //usage: " [dev STRING] [pref NUMBER]\n"
  68. //usage: " ACTION := [table TABLE_ID] [nat ADDRESS]\n"
  69. //usage: " [prohibit | reject | unreachable]\n"
  70. //usage: " [realms [SRCREALM/]DSTREALM]\n"
  71. //usage: " TABLE_ID := [local | main | default | NUMBER]"
  72. //usage:
  73. //usage:#define iptunnel_trivial_usage
  74. //usage: "{ add | change | del | show } [NAME]\n"
  75. //usage: " [mode { ipip | gre | sit }]\n"
  76. //usage: " [remote ADDR] [local ADDR] [ttl TTL]"
  77. //usage:#define iptunnel_full_usage "\n\n"
  78. //usage: "iptunnel { add | change | del | show } [NAME]\n"
  79. //usage: " [mode { ipip | gre | sit }] [remote ADDR] [local ADDR]\n"
  80. //usage: " [[i|o]seq] [[i|o]key KEY] [[i|o]csum]\n"
  81. //usage: " [ttl TTL] [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]"
  82. #include "libbb.h"
  83. #include "libiproute/utils.h"
  84. #include "libiproute/ip_common.h"
  85. #if ENABLE_FEATURE_IP_ADDRESS \
  86. || ENABLE_FEATURE_IP_ROUTE \
  87. || ENABLE_FEATURE_IP_LINK \
  88. || ENABLE_FEATURE_IP_TUNNEL \
  89. || ENABLE_FEATURE_IP_RULE
  90. static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM)
  91. {
  92. bb_show_usage();
  93. }
  94. typedef int FAST_FUNC (*ip_func_ptr_t)(char**);
  95. static int ip_do(ip_func_ptr_t ip_func, char **argv)
  96. {
  97. argv = ip_parse_common_args(argv + 1);
  98. return ip_func(argv);
  99. }
  100. #if ENABLE_FEATURE_IP_ADDRESS
  101. int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  102. int ipaddr_main(int argc UNUSED_PARAM, char **argv)
  103. {
  104. return ip_do(do_ipaddr, argv);
  105. }
  106. #endif
  107. #if ENABLE_FEATURE_IP_LINK
  108. int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  109. int iplink_main(int argc UNUSED_PARAM, char **argv)
  110. {
  111. return ip_do(do_iplink, argv);
  112. }
  113. #endif
  114. #if ENABLE_FEATURE_IP_ROUTE
  115. int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  116. int iproute_main(int argc UNUSED_PARAM, char **argv)
  117. {
  118. return ip_do(do_iproute, argv);
  119. }
  120. #endif
  121. #if ENABLE_FEATURE_IP_RULE
  122. int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  123. int iprule_main(int argc UNUSED_PARAM, char **argv)
  124. {
  125. return ip_do(do_iprule, argv);
  126. }
  127. #endif
  128. #if ENABLE_FEATURE_IP_TUNNEL
  129. int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  130. int iptunnel_main(int argc UNUSED_PARAM, char **argv)
  131. {
  132. return ip_do(do_iptunnel, argv);
  133. }
  134. #endif
  135. int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  136. int ip_main(int argc UNUSED_PARAM, char **argv)
  137. {
  138. static const char keywords[] ALIGN1 =
  139. IF_FEATURE_IP_ADDRESS("address\0")
  140. IF_FEATURE_IP_ROUTE("route\0")
  141. IF_FEATURE_IP_ROUTE("r\0")
  142. IF_FEATURE_IP_LINK("link\0")
  143. IF_FEATURE_IP_TUNNEL("tunnel\0")
  144. IF_FEATURE_IP_TUNNEL("tunl\0")
  145. IF_FEATURE_IP_RULE("rule\0")
  146. ;
  147. static const ip_func_ptr_t ip_func_ptrs[] = {
  148. ip_print_help,
  149. IF_FEATURE_IP_ADDRESS(do_ipaddr,)
  150. IF_FEATURE_IP_ROUTE(do_iproute,)
  151. IF_FEATURE_IP_ROUTE(do_iproute,)
  152. IF_FEATURE_IP_LINK(do_iplink,)
  153. IF_FEATURE_IP_TUNNEL(do_iptunnel,)
  154. IF_FEATURE_IP_TUNNEL(do_iptunnel,)
  155. IF_FEATURE_IP_RULE(do_iprule,)
  156. };
  157. ip_func_ptr_t ip_func;
  158. int key;
  159. argv = ip_parse_common_args(argv + 1);
  160. key = *argv ? index_in_substrings(keywords, *argv++) : -1;
  161. ip_func = ip_func_ptrs[key + 1];
  162. return ip_func(argv);
  163. }
  164. #endif /* any of ENABLE_FEATURE_IP_xxx is 1 */