ip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 (34 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 (16 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. //applet:IF_IP( APPLET_NOEXEC(ip , ip , BB_DIR_SBIN, BB_SUID_DROP, ip ))
  129. //applet:IF_IPADDR( APPLET_NOEXEC(ipaddr , ipaddr , BB_DIR_SBIN, BB_SUID_DROP, ipaddr ))
  130. //applet:IF_IPLINK( APPLET_NOEXEC(iplink , iplink , BB_DIR_SBIN, BB_SUID_DROP, iplink ))
  131. //applet:IF_IPROUTE( APPLET_NOEXEC(iproute , iproute , BB_DIR_SBIN, BB_SUID_DROP, iproute ))
  132. //applet:IF_IPRULE( APPLET_NOEXEC(iprule , iprule , BB_DIR_SBIN, BB_SUID_DROP, iprule ))
  133. //applet:IF_IPTUNNEL(APPLET_NOEXEC(iptunnel, iptunnel, BB_DIR_SBIN, BB_SUID_DROP, iptunnel))
  134. //applet:IF_IPNEIGH( APPLET_NOEXEC(ipneigh , ipneigh , BB_DIR_SBIN, BB_SUID_DROP, ipneigh ))
  135. //kbuild:lib-$(CONFIG_IP) += ip.o
  136. //kbuild:lib-$(CONFIG_IPADDR) += ip.o
  137. //kbuild:lib-$(CONFIG_IPLINK) += ip.o
  138. //kbuild:lib-$(CONFIG_IPROUTE) += ip.o
  139. //kbuild:lib-$(CONFIG_IPRULE) += ip.o
  140. //kbuild:lib-$(CONFIG_IPTUNNEL) += ip.o
  141. //kbuild:lib-$(CONFIG_IPNEIGH) += ip.o
  142. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  143. //usage:#define ipaddr_trivial_usage
  144. //usage: "add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]"
  145. //usage:#define ipaddr_full_usage "\n\n"
  146. //usage: "ipaddr add|change|replace|delete dev IFACE IFADDR\n"
  147. //usage: " IFADDR := PREFIX | ADDR peer PREFIX [broadcast ADDR|+|-]\n"
  148. //usage: " [anycast ADDR] [label STRING] [scope SCOPE]\n"
  149. //usage: " PREFIX := ADDR[/MASK]\n"
  150. //usage: " SCOPE := [host|link|global|NUMBER]\n"
  151. //usage: "ipaddr show|flush [dev IFACE] [scope SCOPE] [to PREFIX] [label PATTERN]"
  152. //usage:
  153. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  154. //usage:#define iplink_trivial_usage
  155. //usage: "set IFACE [up|down] [arp on|off] | show [IFACE]"
  156. //usage:#define iplink_full_usage "\n\n"
  157. //usage: "iplink set IFACE [up|down] [arp on|off] [multicast on|off] [promisc on|off]\n"
  158. //usage: " [mtu NUM] [name NAME] [qlen NUM] [address MAC]\n"
  159. //usage: "iplink show [IFACE]"
  160. //usage:
  161. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  162. //usage:#define iproute_trivial_usage
  163. //usage: "list|flush|add|del|change|append|replace|test ROUTE"
  164. //usage:#define iproute_full_usage "\n\n"
  165. //usage: "iproute list|flush SELECTOR\n"
  166. //usage: " SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n"
  167. //usage: " PREFIX := default|ADDR[/MASK]\n"
  168. //usage: "iproute get ADDR [from ADDR iif IFACE]\n"
  169. //usage: " [oif IFACE] [tos TOS]\n"
  170. //usage: "iproute add|del|change|append|replace|test ROUTE\n"
  171. //usage: " ROUTE := NODE_SPEC [INFO_SPEC]\n"
  172. //usage: " NODE_SPEC := PREFIX"IF_FEATURE_IP_RULE(" [table TABLE_ID]")" [proto RTPROTO] [scope SCOPE] [metric METRIC]\n"
  173. //usage: " INFO_SPEC := NH OPTIONS\n"
  174. //usage: " NH := [via [inet|inet6] ADDR] [dev IFACE] [src ADDR] [onlink]\n"
  175. //usage: " OPTIONS := [mtu [lock] NUM] [advmss [lock] NUM]"
  176. //upstream man ip-route:
  177. //======================
  178. //ip route { show | flush } SELECTOR
  179. //ip route save SELECTOR
  180. //ip route restore
  181. //ip route get ADDRESS [ from ADDRESS iif STRING ] [ oif STRING ] [ tos TOS ]
  182. //ip route { add | del | change | append | replace } ROUTE
  183. //SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ] [ table TABLE_ID ] [ proto RTPROTO ] [ type TYPE ] [ scope SCOPE ]
  184. //ROUTE := NODE_SPEC [ INFO_SPEC ]
  185. //NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ] [ table TABLE_ID ] [ proto RTPROTO ] [ scope SCOPE ] [ metric METRIC ]
  186. //INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ] ...
  187. //NH := [ encap ENCAP ] [ via [ FAMILY ] ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS
  188. // ..............................................................^ I guess [src ADDRESS] should be here
  189. //FAMILY := [ inet | inet6 | ipx | dnet | mpls | bridge | link ]
  190. //OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ] [ as [ to ] ADDRESS ] rtt TIME ] [ rttvar TIME ] [ reordering NUMBER ] [ window NUMBER ] [ cwnd NUMBER ] [ ssthresh REALM ] [ realms REALM ]
  191. // [ rto_min TIME ] [ initcwnd NUMBER ] [ initrwnd NUMBER ] [ features FEATURES ] [ quickack BOOL ] [ congctl NAME ] [ pref PREF ] [ expires TIME ]
  192. //TYPE := [ unicast | local | broadcast | multicast | throw | unreachable | prohibit | blackhole | nat ]
  193. //TABLE_ID := [ local | main | default | all | NUMBER ]
  194. //SCOPE := [ host | link | global | NUMBER ]
  195. //NHFLAGS := [ onlink | pervasive ]
  196. //RTPROTO := [ kernel | boot | static | NUMBER ]
  197. //FEATURES := [ ecn | ]
  198. //PREF := [ low | medium | high ]
  199. //ENCAP := [ MPLS | IP ]
  200. //ENCAP_MPLS := mpls [ LABEL ]
  201. //ENCAP_IP := ip id TUNNEL_ID dst REMOTE_IP [ tos TOS ] [ ttl TTL ]
  202. //usage:
  203. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  204. //usage:#define iprule_trivial_usage
  205. //usage: "[list] | add|del SELECTOR ACTION"
  206. //usage:#define iprule_full_usage "\n\n"
  207. //usage: " SELECTOR := [from PREFIX] [to PREFIX] [tos TOS] [fwmark FWMARK]\n"
  208. //usage: " [dev IFACE] [pref NUMBER]\n"
  209. //usage: " ACTION := [table TABLE_ID] [nat ADDR]\n"
  210. //usage: " [prohibit|reject|unreachable]\n"
  211. //usage: " [realms [SRCREALM/]DSTREALM]\n"
  212. //usage: " TABLE_ID := [local|main|default|NUMBER]"
  213. //usage:
  214. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  215. //usage:#define iptunnel_trivial_usage
  216. //usage: "add|change|del|show [NAME]\n"
  217. //usage: " [mode ipip|gre|sit]\n"
  218. //usage: " [remote ADDR] [local ADDR] [ttl TTL]"
  219. //usage:#define iptunnel_full_usage "\n\n"
  220. //usage: "iptunnel add|change|del|show [NAME]\n"
  221. //usage: " [mode ipip|gre|sit] [remote ADDR] [local ADDR]\n"
  222. //usage: " [[i|o]seq] [[i|o]key KEY] [[i|o]csum]\n"
  223. //usage: " [ttl TTL] [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]"
  224. //usage:
  225. //usage:#define ipneigh_trivial_usage
  226. //usage: "show|flush [to PREFIX] [dev DEV] [nud STATE]"
  227. //usage:#define ipneigh_full_usage ""
  228. //usage:
  229. //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE
  230. //usage:# define IP_BAR_LINK "|"
  231. //usage:#else
  232. //usage:# define IP_BAR_LINK ""
  233. //usage:#endif
  234. //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK
  235. //usage:# define IP_BAR_TUNNEL "|"
  236. //usage:#else
  237. //usage:# define IP_BAR_TUNNEL ""
  238. //usage:#endif
  239. //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK || ENABLE_FEATURE_IP_TUNNEL
  240. //usage:# define IP_BAR_NEIGH "|"
  241. //usage:#else
  242. //usage:# define IP_BAR_NEIGH ""
  243. //usage:#endif
  244. //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK || ENABLE_FEATURE_IP_TUNNEL || ENABLE_FEATURE_IP_NEIGH
  245. //usage:# define IP_BAR_RULE "|"
  246. //usage:#else
  247. //usage:# define IP_BAR_RULE ""
  248. //usage:#endif
  249. //usage:
  250. //usage:#define ip_trivial_usage
  251. //usage: "[OPTIONS] "
  252. //usage: IF_FEATURE_IP_ADDRESS("address")
  253. //usage: IF_FEATURE_IP_ROUTE( IF_FEATURE_IP_ADDRESS("|")"route")
  254. //usage: IF_FEATURE_IP_LINK( IP_BAR_LINK "link")
  255. //usage: IF_FEATURE_IP_TUNNEL( IP_BAR_TUNNEL"tunnel")
  256. //usage: IF_FEATURE_IP_NEIGH( IP_BAR_NEIGH "neigh")
  257. //usage: IF_FEATURE_IP_RULE( IP_BAR_RULE "rule")
  258. //usage: " [COMMAND]"
  259. //usage:#define ip_full_usage "\n\n"
  260. //usage: "OPTIONS := -f[amily] inet|inet6|link | -o[neline]\n"
  261. //usage: "COMMAND :="
  262. //usage: IF_FEATURE_IP_ADDRESS("\n"
  263. //usage: "ip addr "ipaddr_trivial_usage)
  264. //usage: IF_FEATURE_IP_ROUTE("\n"
  265. //usage: "ip route "iproute_trivial_usage)
  266. //usage: IF_FEATURE_IP_LINK("\n"
  267. //usage: "ip link "iplink_trivial_usage)
  268. //usage: IF_FEATURE_IP_TUNNEL("\n"
  269. //usage: "ip tunnel "iptunnel_trivial_usage)
  270. //usage: IF_FEATURE_IP_NEIGH("\n"
  271. //usage: "ip neigh "ipneigh_trivial_usage)
  272. //usage: IF_FEATURE_IP_RULE("\n"
  273. //usage: "ip rule "iprule_trivial_usage)
  274. #include "libbb.h"
  275. #include "libiproute/utils.h"
  276. #include "libiproute/ip_common.h"
  277. typedef int FAST_FUNC (*ip_func_ptr_t)(char**);
  278. #if ENABLE_IPADDR \
  279. || ENABLE_IPLINK \
  280. || ENABLE_IPROUTE \
  281. || ENABLE_IPRULE \
  282. || ENABLE_IPTUNNEL \
  283. || ENABLE_IPNEIGH
  284. static int ip_do(ip_func_ptr_t ip_func, char **argv)
  285. {
  286. argv = ip_parse_common_args(argv + 1);
  287. return ip_func(argv);
  288. }
  289. #endif
  290. #if ENABLE_IPADDR
  291. int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  292. int ipaddr_main(int argc UNUSED_PARAM, char **argv)
  293. {
  294. return ip_do(do_ipaddr, argv);
  295. }
  296. #endif
  297. #if ENABLE_IPLINK
  298. int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  299. int iplink_main(int argc UNUSED_PARAM, char **argv)
  300. {
  301. return ip_do(do_iplink, argv);
  302. }
  303. #endif
  304. #if ENABLE_IPROUTE
  305. int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  306. int iproute_main(int argc UNUSED_PARAM, char **argv)
  307. {
  308. return ip_do(do_iproute, argv);
  309. }
  310. #endif
  311. #if ENABLE_IPRULE
  312. int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  313. int iprule_main(int argc UNUSED_PARAM, char **argv)
  314. {
  315. return ip_do(do_iprule, argv);
  316. }
  317. #endif
  318. #if ENABLE_IPTUNNEL
  319. int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  320. int iptunnel_main(int argc UNUSED_PARAM, char **argv)
  321. {
  322. return ip_do(do_iptunnel, argv);
  323. }
  324. #endif
  325. #if ENABLE_IPNEIGH
  326. int ipneigh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  327. int ipneigh_main(int argc UNUSED_PARAM, char **argv)
  328. {
  329. return ip_do(do_ipneigh, argv);
  330. }
  331. #endif
  332. #if ENABLE_IP
  333. static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM)
  334. {
  335. bb_show_usage();
  336. }
  337. int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  338. int ip_main(int argc UNUSED_PARAM, char **argv)
  339. {
  340. static const char keywords[] ALIGN1 = ""
  341. IF_FEATURE_IP_ADDRESS("address\0")
  342. IF_FEATURE_IP_ROUTE("route\0")
  343. IF_FEATURE_IP_ROUTE("r\0")
  344. IF_FEATURE_IP_LINK("link\0")
  345. IF_FEATURE_IP_TUNNEL("tunnel\0")
  346. IF_FEATURE_IP_TUNNEL("tunl\0")
  347. IF_FEATURE_IP_RULE("rule\0")
  348. IF_FEATURE_IP_NEIGH("neigh\0")
  349. ;
  350. static const ip_func_ptr_t ip_func_ptrs[] = {
  351. ip_print_help,
  352. IF_FEATURE_IP_ADDRESS(do_ipaddr,)
  353. IF_FEATURE_IP_ROUTE(do_iproute,)
  354. IF_FEATURE_IP_ROUTE(do_iproute,)
  355. IF_FEATURE_IP_LINK(do_iplink,)
  356. IF_FEATURE_IP_TUNNEL(do_iptunnel,)
  357. IF_FEATURE_IP_TUNNEL(do_iptunnel,)
  358. IF_FEATURE_IP_RULE(do_iprule,)
  359. IF_FEATURE_IP_NEIGH(do_ipneigh,)
  360. };
  361. ip_func_ptr_t ip_func;
  362. int key;
  363. argv = ip_parse_common_args(argv + 1);
  364. if (ARRAY_SIZE(ip_func_ptrs) > 1 && *argv)
  365. key = index_in_substrings(keywords, *argv++);
  366. else
  367. key = -1;
  368. ip_func = ip_func_ptrs[key + 1];
  369. return ip_func(argv);
  370. }
  371. #endif