iprule.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version
  6. * 2 of the License, or (at your option) any later version.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. *
  10. * Changes:
  11. *
  12. * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
  13. * initially integrated into busybox by Bernhard Reutner-Fischer
  14. */
  15. #include <netinet/in.h>
  16. #include <netinet/ip.h>
  17. #include <arpa/inet.h>
  18. /* from <linux/fib_rules.h>: */
  19. #define FRA_FWMARK 10
  20. #define FRA_SUPPRESS_IFGROUP 13
  21. #define FRA_SUPPRESS_PREFIXLEN 14
  22. #define FRA_FWMASK 16
  23. #include "ip_common.h" /* #include "libbb.h" is inside */
  24. #include "rt_names.h"
  25. #include "utils.h"
  26. #include <linux/version.h>
  27. /* RTA_TABLE is not a define, can't test with ifdef. */
  28. /* As a proxy, test which kernels toolchain expects: */
  29. #define HAVE_RTA_TABLE (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19))
  30. /* If you add stuff here, update iprule_full_usage */
  31. static const char keywords[] ALIGN1 =
  32. "from\0""to\0""preference\0""order\0""priority\0"
  33. "tos\0""fwmark\0""realms\0""table\0""lookup\0"
  34. "suppress_prefixlength\0""suppress_ifgroup\0"
  35. "dev\0""iif\0""nat\0""map-to\0""type\0""help\0"
  36. ;
  37. #define keyword_preference (keywords + sizeof("from") + sizeof("to"))
  38. #define keyword_fwmark (keyword_preference + sizeof("preference") + sizeof("order") + sizeof("priority") + sizeof("tos"))
  39. #define keyword_realms (keyword_fwmark + sizeof("fwmark"))
  40. #define keyword_suppress_prefixlength (keyword_realms + sizeof("realms") + sizeof("table") + sizeof("lookup"))
  41. #define keyword_suppress_ifgroup (keyword_suppress_prefixlength + sizeof("suppress_prefixlength"))
  42. enum {
  43. ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
  44. ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup,
  45. ARG_suppress_prefixlength, ARG_suppress_ifgroup,
  46. ARG_dev, ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help,
  47. };
  48. static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
  49. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  50. {
  51. struct rtmsg *r = NLMSG_DATA(n);
  52. int len = n->nlmsg_len;
  53. int host_len = -1;
  54. struct rtattr * tb[RTA_MAX+1];
  55. if (n->nlmsg_type != RTM_NEWRULE)
  56. return 0;
  57. len -= NLMSG_LENGTH(sizeof(*r));
  58. if (len < 0)
  59. return -1;
  60. //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
  61. parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
  62. if (r->rtm_family == AF_INET)
  63. host_len = 32;
  64. else if (r->rtm_family == AF_INET6)
  65. host_len = 128;
  66. /* else if (r->rtm_family == AF_DECnet)
  67. host_len = 16;
  68. else if (r->rtm_family == AF_IPX)
  69. host_len = 80;
  70. */
  71. printf("%u:\t", tb[RTA_PRIORITY] ?
  72. *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
  73. : 0);
  74. printf("from ");
  75. if (tb[RTA_SRC]) {
  76. if (r->rtm_src_len != host_len) {
  77. printf("%s/%u",
  78. rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
  79. r->rtm_src_len
  80. );
  81. } else {
  82. fputs_stdout(format_host(r->rtm_family,
  83. RTA_PAYLOAD(tb[RTA_SRC]),
  84. RTA_DATA(tb[RTA_SRC]))
  85. );
  86. }
  87. } else if (r->rtm_src_len) {
  88. printf("0/%d", r->rtm_src_len);
  89. } else {
  90. printf("all");
  91. }
  92. bb_putchar(' ');
  93. if (tb[RTA_DST]) {
  94. if (r->rtm_dst_len != host_len) {
  95. printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
  96. RTA_DATA(tb[RTA_DST])),
  97. r->rtm_dst_len
  98. );
  99. } else {
  100. printf("to %s ", format_host(r->rtm_family,
  101. RTA_PAYLOAD(tb[RTA_DST]),
  102. RTA_DATA(tb[RTA_DST])));
  103. }
  104. } else if (r->rtm_dst_len) {
  105. printf("to 0/%d ", r->rtm_dst_len);
  106. }
  107. if (r->rtm_tos) {
  108. printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos));
  109. }
  110. if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) {
  111. uint32_t mark = 0, mask = 0;
  112. if (tb[FRA_FWMARK])
  113. mark = *(uint32_t*)RTA_DATA(tb[FRA_FWMARK]);
  114. if (tb[FRA_FWMASK]
  115. && (mask = *(uint32_t*)RTA_DATA(tb[FRA_FWMASK])) != 0xFFFFFFFF
  116. )
  117. printf("fwmark %#x/%#x ", mark, mask);
  118. else
  119. printf("fwmark %#x ", mark);
  120. }
  121. if (tb[RTA_IIF]) {
  122. printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
  123. }
  124. #if HAVE_RTA_TABLE
  125. if (tb[RTA_TABLE])
  126. printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
  127. else
  128. #endif
  129. if (r->rtm_table)
  130. printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
  131. if (tb[FRA_SUPPRESS_PREFIXLEN]) {
  132. int pl = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_PREFIXLEN]);
  133. if (pl != -1)
  134. printf("%s %d ", keyword_suppress_prefixlength, pl);
  135. }
  136. if (tb[FRA_SUPPRESS_IFGROUP]) {
  137. int grp = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_IFGROUP]);
  138. if (grp != -1)
  139. printf("%s %d ", keyword_suppress_ifgroup, grp);
  140. }
  141. if (tb[RTA_FLOW]) {
  142. uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
  143. uint32_t from = to>>16;
  144. to &= 0xFFFF;
  145. if (from) {
  146. printf("realms %s/",
  147. rtnl_rtrealm_n2a(from));
  148. }
  149. printf("%s ",
  150. rtnl_rtrealm_n2a(to));
  151. }
  152. if (r->rtm_type == RTN_NAT) {
  153. if (tb[RTA_GATEWAY]) {
  154. printf("map-to %s ",
  155. format_host(r->rtm_family,
  156. RTA_PAYLOAD(tb[RTA_GATEWAY]),
  157. RTA_DATA(tb[RTA_GATEWAY]))
  158. );
  159. } else
  160. printf("masquerade");
  161. } else if (r->rtm_type != RTN_UNICAST)
  162. fputs_stdout(rtnl_rtntype_n2a(r->rtm_type));
  163. bb_putchar('\n');
  164. /*fflush_all();*/
  165. return 0;
  166. }
  167. /* Return value becomes exitcode. It's okay to not return at all */
  168. static int iprule_list(char **argv)
  169. {
  170. struct rtnl_handle rth;
  171. int af = preferred_family;
  172. if (af == AF_UNSPEC)
  173. af = AF_INET;
  174. if (*argv) {
  175. //bb_error_msg("\"rule show\" needs no arguments");
  176. bb_warn_ignoring_args(*argv);
  177. return -1;
  178. }
  179. xrtnl_open(&rth);
  180. xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
  181. xrtnl_dump_filter(&rth, print_rule, NULL);
  182. return 0;
  183. }
  184. /* Return value becomes exitcode. It's okay to not return at all */
  185. static int iprule_modify(int cmd, char **argv)
  186. {
  187. bool table_ok = 0;
  188. struct rtnl_handle rth;
  189. struct {
  190. struct nlmsghdr n;
  191. struct rtmsg r;
  192. char buf[1024];
  193. } req;
  194. smalluint key;
  195. memset(&req, 0, sizeof(req));
  196. req.n.nlmsg_type = cmd;
  197. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  198. req.n.nlmsg_flags = NLM_F_REQUEST;
  199. req.r.rtm_family = preferred_family;
  200. req.r.rtm_protocol = RTPROT_BOOT;
  201. if (RT_SCOPE_UNIVERSE != 0)
  202. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  203. /*req.r.rtm_table = 0; - already is */
  204. if (RTN_UNSPEC != 0)
  205. req.r.rtm_type = RTN_UNSPEC;
  206. if (cmd == RTM_NEWRULE) {
  207. req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
  208. req.r.rtm_type = RTN_UNICAST;
  209. }
  210. while (*argv) {
  211. key = index_in_substrings(keywords, *argv) + 1;
  212. if (key == 0) /* no match found in keywords array, bail out. */
  213. invarg_1_to_2(*argv, applet_name);
  214. if (key == ARG_from) {
  215. inet_prefix dst;
  216. NEXT_ARG();
  217. get_prefix(&dst, *argv, req.r.rtm_family);
  218. req.r.rtm_src_len = dst.bitlen;
  219. addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
  220. } else if (key == ARG_to) {
  221. inet_prefix dst;
  222. NEXT_ARG();
  223. get_prefix(&dst, *argv, req.r.rtm_family);
  224. req.r.rtm_dst_len = dst.bitlen;
  225. addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
  226. } else if (key == ARG_preference ||
  227. key == ARG_order ||
  228. key == ARG_priority
  229. ) {
  230. uint32_t pref;
  231. NEXT_ARG();
  232. pref = get_u32(*argv, keyword_preference);
  233. addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
  234. } else if (key == ARG_tos) {
  235. uint32_t tos;
  236. NEXT_ARG();
  237. if (rtnl_dsfield_a2n(&tos, *argv))
  238. invarg_1_to_2(*argv, "TOS");
  239. req.r.rtm_tos = tos;
  240. } else if (key == ARG_fwmark) {
  241. char *slash;
  242. uint32_t fwmark, fwmask;
  243. NEXT_ARG();
  244. slash = strchr(*argv, '/');
  245. if (slash)
  246. *slash = '\0';
  247. fwmark = get_u32(*argv, keyword_fwmark);
  248. addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark);
  249. if (slash) {
  250. fwmask = get_u32(slash + 1, "fwmask");
  251. addattr32(&req.n, sizeof(req), FRA_FWMASK, fwmask);
  252. }
  253. } else if (key == ARG_realms) {
  254. uint32_t realm;
  255. NEXT_ARG();
  256. if (get_rt_realms(&realm, *argv))
  257. invarg_1_to_2(*argv, keyword_realms);
  258. addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
  259. } else if (key == ARG_table ||
  260. key == ARG_lookup
  261. ) {
  262. uint32_t tid;
  263. NEXT_ARG();
  264. if (rtnl_rttable_a2n(&tid, *argv))
  265. invarg_1_to_2(*argv, "table ID");
  266. #if HAVE_RTA_TABLE
  267. if (tid > 255) {
  268. req.r.rtm_table = RT_TABLE_UNSPEC;
  269. addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
  270. } else
  271. #endif
  272. req.r.rtm_table = tid;
  273. table_ok = 1;
  274. } else if (key == ARG_suppress_prefixlength) {
  275. int prefix_length;
  276. NEXT_ARG();
  277. prefix_length = get_u32(*argv, keyword_suppress_prefixlength);
  278. addattr32(&req.n, sizeof(req), FRA_SUPPRESS_PREFIXLEN, prefix_length);
  279. } else if (key == ARG_suppress_ifgroup) {
  280. int grp;
  281. NEXT_ARG();
  282. grp = get_u32(*argv, keyword_suppress_ifgroup);
  283. addattr32(&req.n, sizeof(req), FRA_SUPPRESS_IFGROUP, grp);
  284. } else if (key == ARG_dev ||
  285. key == ARG_iif
  286. ) {
  287. NEXT_ARG();
  288. addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
  289. } else if (key == ARG_nat ||
  290. key == ARG_map_to
  291. ) {
  292. NEXT_ARG();
  293. addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
  294. req.r.rtm_type = RTN_NAT;
  295. } else {
  296. int type;
  297. if (key == ARG_type) {
  298. NEXT_ARG();
  299. }
  300. if (key == ARG_help)
  301. bb_show_usage();
  302. if (rtnl_rtntype_a2n(&type, *argv))
  303. invarg_1_to_2(*argv, "type");
  304. req.r.rtm_type = type;
  305. }
  306. argv++;
  307. }
  308. if (req.r.rtm_family == AF_UNSPEC)
  309. req.r.rtm_family = AF_INET;
  310. if (!table_ok && cmd == RTM_NEWRULE)
  311. req.r.rtm_table = RT_TABLE_MAIN;
  312. xrtnl_open(&rth);
  313. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  314. return 2;
  315. return 0;
  316. }
  317. /* Return value becomes exitcode. It's okay to not return at all */
  318. int FAST_FUNC do_iprule(char **argv)
  319. {
  320. static const char ip_rule_commands[] ALIGN1 =
  321. "add\0""delete\0""list\0""show\0";
  322. if (*argv) {
  323. int cmd = index_in_substrings(ip_rule_commands, *argv);
  324. if (cmd < 0)
  325. invarg_1_to_2(*argv, applet_name);
  326. argv++;
  327. if (cmd < 2)
  328. return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
  329. }
  330. return iprule_list(argv);
  331. }