ip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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"
  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. You generally don't need "ip" to use busybox with
  18. //config: TCP/IP.
  19. //config:
  20. //config:config IPADDR
  21. //config: bool "ipaddr"
  22. //config: default y
  23. //config: select FEATURE_IP_ADDRESS
  24. //config: select PLATFORM_LINUX
  25. //config: help
  26. //config: Support short form of ip addr: ipaddr
  27. //config:
  28. //config:config IPLINK
  29. //config: bool "iplink"
  30. //config: default y
  31. //config: select FEATURE_IP_LINK
  32. //config: select PLATFORM_LINUX
  33. //config: help
  34. //config: Support short form of ip link: iplink
  35. //config:
  36. //config:config IPROUTE
  37. //config: bool "iproute"
  38. //config: default y
  39. //config: select FEATURE_IP_ROUTE
  40. //config: select PLATFORM_LINUX
  41. //config: help
  42. //config: Support short form of ip route: iproute
  43. //config:
  44. //config:config IPTUNNEL
  45. //config: bool "iptunnel"
  46. //config: default y
  47. //config: select FEATURE_IP_TUNNEL
  48. //config: select PLATFORM_LINUX
  49. //config: help
  50. //config: Support short form of ip tunnel: iptunnel
  51. //config:
  52. //config:config IPRULE
  53. //config: bool "iprule"
  54. //config: default y
  55. //config: select FEATURE_IP_RULE
  56. //config: select PLATFORM_LINUX
  57. //config: help
  58. //config: Support short form of ip rule: iprule
  59. //config:
  60. //config:config IPNEIGH
  61. //config: bool "ipneigh"
  62. //config: default y
  63. //config: select FEATURE_IP_NEIGH
  64. //config: select PLATFORM_LINUX
  65. //config: help
  66. //config: Support short form of ip neigh: ipneigh
  67. //config:
  68. //config:config FEATURE_IP_ADDRESS
  69. //config: bool "ip address"
  70. //config: default y
  71. //config: depends on IP || IPADDR
  72. //config: help
  73. //config: Address manipulation support for the "ip" applet.
  74. //config:
  75. //config:config FEATURE_IP_LINK
  76. //config: bool "ip link"
  77. //config: default y
  78. //config: depends on IP || IPLINK
  79. //config: help
  80. //config: Configure network devices with "ip".
  81. //config:
  82. //config:config FEATURE_IP_ROUTE
  83. //config: bool "ip route"
  84. //config: default y
  85. //config: depends on IP || IPROUTE
  86. //config: help
  87. //config: Add support for routing table management to "ip".
  88. //config:
  89. //config:config FEATURE_IP_ROUTE_DIR
  90. //config: string "ip route configuration directory"
  91. //config: default "/etc/iproute2"
  92. //config: depends on FEATURE_IP_ROUTE
  93. //config: help
  94. //config: Location of the "ip" applet routing configuration.
  95. //config:
  96. //config:config FEATURE_IP_TUNNEL
  97. //config: bool "ip tunnel"
  98. //config: default y
  99. //config: depends on IP || IPTUNNEL
  100. //config: help
  101. //config: Add support for tunneling commands to "ip".
  102. //config:
  103. //config:config FEATURE_IP_RULE
  104. //config: bool "ip rule"
  105. //config: default y
  106. //config: depends on IP || IPRULE
  107. //config: help
  108. //config: Add support for rule commands to "ip".
  109. //config:
  110. //config:config FEATURE_IP_NEIGH
  111. //config: bool "ip neighbor"
  112. //config: default y
  113. //config: depends on IP || IPNEIGH
  114. //config: help
  115. //config: Add support for neighbor commands to "ip".
  116. //config:
  117. //config:config FEATURE_IP_RARE_PROTOCOLS
  118. //config: bool "Support displaying rarely used link types"
  119. //config: default n
  120. //config: depends on IP || IPADDR || IPLINK || IPROUTE || IPTUNNEL || IPRULE || IPNEIGH
  121. //config: help
  122. //config: If you are not going to use links of type "frad", "econet",
  123. //config: "bif" etc, you probably don't need to enable this.
  124. //config: Ethernet, wireless, infrared, ppp/slip, ip tunnelling
  125. //config: link types are supported without this option selected.
  126. //applet:IF_IP(APPLET(ip, BB_DIR_SBIN, BB_SUID_DROP))
  127. //applet:IF_IPADDR(APPLET(ipaddr, BB_DIR_SBIN, BB_SUID_DROP))
  128. //applet:IF_IPLINK(APPLET(iplink, BB_DIR_SBIN, BB_SUID_DROP))
  129. //applet:IF_IPROUTE(APPLET(iproute, BB_DIR_SBIN, BB_SUID_DROP))
  130. //applet:IF_IPRULE(APPLET(iprule, BB_DIR_SBIN, BB_SUID_DROP))
  131. //applet:IF_IPTUNNEL(APPLET(iptunnel, BB_DIR_SBIN, BB_SUID_DROP))
  132. //applet:IF_IPNEIGH(APPLET(ipneigh, BB_DIR_SBIN, BB_SUID_DROP))
  133. //kbuild:lib-$(CONFIG_IP) += ip.o
  134. //kbuild:lib-$(CONFIG_IPADDR) += ip.o
  135. //kbuild:lib-$(CONFIG_IPLINK) += ip.o
  136. //kbuild:lib-$(CONFIG_IPROUTE) += ip.o
  137. //kbuild:lib-$(CONFIG_IPRULE) += ip.o
  138. //kbuild:lib-$(CONFIG_IPTUNNEL) += ip.o
  139. //kbuild:lib-$(CONFIG_IPNEIGH) += ip.o
  140. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  141. //usage:#define ipaddr_trivial_usage
  142. //usage: "add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]"
  143. //usage:#define ipaddr_full_usage "\n\n"
  144. //usage: "ipaddr add|change|replace|delete dev IFACE IFADDR\n"
  145. //usage: " IFADDR := PREFIX | ADDR peer PREFIX [broadcast ADDR|+|-]\n"
  146. //usage: " [anycast ADDR] [label STRING] [scope SCOPE]\n"
  147. //usage: " PREFIX := ADDR[/MASK]\n"
  148. //usage: " SCOPE := [host|link|global|NUMBER]\n"
  149. //usage: "ipaddr show|flush [dev IFACE] [scope SCOPE] [to PREFIX] [label PATTERN]"
  150. //usage:
  151. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  152. //usage:#define iplink_trivial_usage
  153. //usage: "set IFACE [up|down] [arp on|off] | show [IFACE]"
  154. //usage:#define iplink_full_usage "\n\n"
  155. //usage: "iplink set IFACE [up|down] [arp on|off] [multicast on|off] [promisc on|off]\n"
  156. //usage: " [mtu NUM] [name NAME] [qlen NUM] [address MAC]\n"
  157. //usage: "iplink show [IFACE]"
  158. //usage:
  159. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  160. //usage:#define iproute_trivial_usage
  161. //usage: "list|flush|add|del|change|append|replace|test ROUTE"
  162. //usage:#define iproute_full_usage "\n\n"
  163. //usage: "iproute list|flush SELECTOR\n"
  164. //usage: " SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n"
  165. //usage: " PREFIX := default|ADDR[/MASK]\n"
  166. //usage: "iproute get ADDR [from ADDR iif IFACE]\n"
  167. //usage: " [oif IFACE] [tos TOS]\n"
  168. //usage: "iproute add|del|change|append|replace|test ROUTE\n"
  169. //usage: " ROUTE := NODE_SPEC [INFO_SPEC]\n"
  170. //usage: " NODE_SPEC := PREFIX"IF_FEATURE_IP_RULE(" [table TABLE_ID]")" [proto RTPROTO] [scope SCOPE] [metric METRIC]\n"
  171. //usage: " INFO_SPEC := NH OPTIONS\n"
  172. //usage: " NH := [via [inet|inet6] ADDR] [dev IFACE] [src ADDR] [onlink]\n"
  173. //usage: " OPTIONS := [mtu [lock] NUM] [advmss [lock] NUM]"
  174. //upstream man ip-route:
  175. //======================
  176. //ip route { show | flush } SELECTOR
  177. //ip route save SELECTOR
  178. //ip route restore
  179. //ip route get ADDRESS [ from ADDRESS iif STRING ] [ oif STRING ] [ tos TOS ]
  180. //ip route { add | del | change | append | replace } ROUTE
  181. //SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ] [ table TABLE_ID ] [ proto RTPROTO ] [ type TYPE ] [ scope SCOPE ]
  182. //ROUTE := NODE_SPEC [ INFO_SPEC ]
  183. //NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ] [ table TABLE_ID ] [ proto RTPROTO ] [ scope SCOPE ] [ metric METRIC ]
  184. //INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ] ...
  185. //NH := [ encap ENCAP ] [ via [ FAMILY ] ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS
  186. // ..............................................................^ I guess [src ADDRESS] should be here
  187. //FAMILY := [ inet | inet6 | ipx | dnet | mpls | bridge | link ]
  188. //OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ] [ as [ to ] ADDRESS ] rtt TIME ] [ rttvar TIME ] [ reordering NUMBER ] [ window NUMBER ] [ cwnd NUMBER ] [ ssthresh REALM ] [ realms REALM ]
  189. // [ rto_min TIME ] [ initcwnd NUMBER ] [ initrwnd NUMBER ] [ features FEATURES ] [ quickack BOOL ] [ congctl NAME ] [ pref PREF ] [ expires TIME ]
  190. //TYPE := [ unicast | local | broadcast | multicast | throw | unreachable | prohibit | blackhole | nat ]
  191. //TABLE_ID := [ local | main | default | all | NUMBER ]
  192. //SCOPE := [ host | link | global | NUMBER ]
  193. //NHFLAGS := [ onlink | pervasive ]
  194. //RTPROTO := [ kernel | boot | static | NUMBER ]
  195. //FEATURES := [ ecn | ]
  196. //PREF := [ low | medium | high ]
  197. //ENCAP := [ MPLS | IP ]
  198. //ENCAP_MPLS := mpls [ LABEL ]
  199. //ENCAP_IP := ip id TUNNEL_ID dst REMOTE_IP [ tos TOS ] [ ttl TTL ]
  200. //usage:
  201. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  202. //usage:#define iprule_trivial_usage
  203. //usage: "[list] | add|del SELECTOR ACTION"
  204. //usage:#define iprule_full_usage "\n\n"
  205. //usage: " SELECTOR := [from PREFIX] [to PREFIX] [tos TOS] [fwmark FWMARK]\n"
  206. //usage: " [dev IFACE] [pref NUMBER]\n"
  207. //usage: " ACTION := [table TABLE_ID] [nat ADDR]\n"
  208. //usage: " [prohibit|reject|unreachable]\n"
  209. //usage: " [realms [SRCREALM/]DSTREALM]\n"
  210. //usage: " TABLE_ID := [local|main|default|NUMBER]"
  211. //usage:
  212. //--------------123456789.123456789.123456789.123456789.123456789.123456789.123456789.123....79
  213. //usage:#define iptunnel_trivial_usage
  214. //usage: "add|change|del|show [NAME]\n"
  215. //usage: " [mode ipip|gre|sit]\n"
  216. //usage: " [remote ADDR] [local ADDR] [ttl TTL]"
  217. //usage:#define iptunnel_full_usage "\n\n"
  218. //usage: "iptunnel add|change|del|show [NAME]\n"
  219. //usage: " [mode ipip|gre|sit] [remote ADDR] [local ADDR]\n"
  220. //usage: " [[i|o]seq] [[i|o]key KEY] [[i|o]csum]\n"
  221. //usage: " [ttl TTL] [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]"
  222. //usage:
  223. //usage:#define ipneigh_trivial_usage
  224. //usage: "show|flush [to PREFIX] [dev DEV] [nud STATE]"
  225. //usage:#define ipneigh_full_usage ""
  226. //usage:
  227. //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE
  228. //usage:# define IP_BAR_LINK "|"
  229. //usage:#else
  230. //usage:# define IP_BAR_LINK ""
  231. //usage:#endif
  232. //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK
  233. //usage:# define IP_BAR_TUNNEL "|"
  234. //usage:#else
  235. //usage:# define IP_BAR_TUNNEL ""
  236. //usage:#endif
  237. //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK || ENABLE_FEATURE_IP_TUNNEL
  238. //usage:# define IP_BAR_NEIGH "|"
  239. //usage:#else
  240. //usage:# define IP_BAR_NEIGH ""
  241. //usage:#endif
  242. //usage:#if ENABLE_FEATURE_IP_ADDRESS || ENABLE_FEATURE_IP_ROUTE || ENABLE_FEATURE_IP_LINK || ENABLE_FEATURE_IP_TUNNEL || ENABLE_FEATURE_IP_NEIGH
  243. //usage:# define IP_BAR_RULE "|"
  244. //usage:#else
  245. //usage:# define IP_BAR_RULE ""
  246. //usage:#endif
  247. //usage:
  248. //usage:#define ip_trivial_usage
  249. //usage: "[OPTIONS] "
  250. //usage: IF_FEATURE_IP_ADDRESS("address")
  251. //usage: IF_FEATURE_IP_ROUTE( IF_FEATURE_IP_ADDRESS("|")"route")
  252. //usage: IF_FEATURE_IP_LINK( IP_BAR_LINK "link")
  253. //usage: IF_FEATURE_IP_TUNNEL( IP_BAR_TUNNEL"tunnel")
  254. //usage: IF_FEATURE_IP_NEIGH( IP_BAR_NEIGH "neigh")
  255. //usage: IF_FEATURE_IP_RULE( IP_BAR_RULE "rule")
  256. //usage: " [COMMAND]"
  257. //usage:#define ip_full_usage "\n\n"
  258. //usage: "OPTIONS := -f[amily] inet|inet6|link | -o[neline]\n"
  259. //usage: "COMMAND :="
  260. //usage: IF_FEATURE_IP_ADDRESS("\n"
  261. //usage: "ip addr "ipaddr_trivial_usage)
  262. //usage: IF_FEATURE_IP_ROUTE("\n"
  263. //usage: "ip route "iproute_trivial_usage)
  264. //usage: IF_FEATURE_IP_LINK("\n"
  265. //usage: "ip link "iplink_trivial_usage)
  266. //usage: IF_FEATURE_IP_TUNNEL("\n"
  267. //usage: "ip tunnel "iptunnel_trivial_usage)
  268. //usage: IF_FEATURE_IP_NEIGH("\n"
  269. //usage: "ip neigh "ipneigh_trivial_usage)
  270. //usage: IF_FEATURE_IP_RULE("\n"
  271. //usage: "ip rule "iprule_trivial_usage)
  272. #include "libbb.h"
  273. #include "libiproute/utils.h"
  274. #include "libiproute/ip_common.h"
  275. typedef int FAST_FUNC (*ip_func_ptr_t)(char**);
  276. #if ENABLE_IPADDR \
  277. || ENABLE_IPLINK \
  278. || ENABLE_IPROUTE \
  279. || ENABLE_IPRULE \
  280. || ENABLE_IPTUNNEL \
  281. || ENABLE_IPNEIGH
  282. static int ip_do(ip_func_ptr_t ip_func, char **argv)
  283. {
  284. argv = ip_parse_common_args(argv + 1);
  285. return ip_func(argv);
  286. }
  287. #endif
  288. #if ENABLE_IPADDR
  289. int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  290. int ipaddr_main(int argc UNUSED_PARAM, char **argv)
  291. {
  292. return ip_do(do_ipaddr, argv);
  293. }
  294. #endif
  295. #if ENABLE_IPLINK
  296. int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  297. int iplink_main(int argc UNUSED_PARAM, char **argv)
  298. {
  299. return ip_do(do_iplink, argv);
  300. }
  301. #endif
  302. #if ENABLE_IPROUTE
  303. int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  304. int iproute_main(int argc UNUSED_PARAM, char **argv)
  305. {
  306. return ip_do(do_iproute, argv);
  307. }
  308. #endif
  309. #if ENABLE_IPRULE
  310. int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  311. int iprule_main(int argc UNUSED_PARAM, char **argv)
  312. {
  313. return ip_do(do_iprule, argv);
  314. }
  315. #endif
  316. #if ENABLE_IPTUNNEL
  317. int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  318. int iptunnel_main(int argc UNUSED_PARAM, char **argv)
  319. {
  320. return ip_do(do_iptunnel, argv);
  321. }
  322. #endif
  323. #if ENABLE_IPNEIGH
  324. int ipneigh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  325. int ipneigh_main(int argc UNUSED_PARAM, char **argv)
  326. {
  327. return ip_do(do_ipneigh, argv);
  328. }
  329. #endif
  330. #if ENABLE_IP
  331. static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM)
  332. {
  333. bb_show_usage();
  334. }
  335. int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  336. int ip_main(int argc UNUSED_PARAM, char **argv)
  337. {
  338. static const char keywords[] ALIGN1 = ""
  339. IF_FEATURE_IP_ADDRESS("address\0")
  340. IF_FEATURE_IP_ROUTE("route\0")
  341. IF_FEATURE_IP_ROUTE("r\0")
  342. IF_FEATURE_IP_LINK("link\0")
  343. IF_FEATURE_IP_TUNNEL("tunnel\0")
  344. IF_FEATURE_IP_TUNNEL("tunl\0")
  345. IF_FEATURE_IP_RULE("rule\0")
  346. IF_FEATURE_IP_NEIGH("neigh\0")
  347. ;
  348. static const ip_func_ptr_t ip_func_ptrs[] = {
  349. ip_print_help,
  350. IF_FEATURE_IP_ADDRESS(do_ipaddr,)
  351. IF_FEATURE_IP_ROUTE(do_iproute,)
  352. IF_FEATURE_IP_ROUTE(do_iproute,)
  353. IF_FEATURE_IP_LINK(do_iplink,)
  354. IF_FEATURE_IP_TUNNEL(do_iptunnel,)
  355. IF_FEATURE_IP_TUNNEL(do_iptunnel,)
  356. IF_FEATURE_IP_RULE(do_iprule,)
  357. IF_FEATURE_IP_NEIGH(do_ipneigh,)
  358. };
  359. ip_func_ptr_t ip_func;
  360. int key;
  361. argv = ip_parse_common_args(argv + 1);
  362. if (ARRAY_SIZE(ip_func_ptrs) > 1 && *argv)
  363. key = index_in_substrings(keywords, *argv++);
  364. else
  365. key = -1;
  366. ip_func = ip_func_ptrs[key + 1];
  367. return ip_func(argv);
  368. }
  369. #endif