iprule.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #include "ip_common.h" /* #include "libbb.h" is inside */
  19. #include "rt_names.h"
  20. #include "utils.h"
  21. /*
  22. static void usage(void) __attribute__((noreturn));
  23. static void usage(void)
  24. {
  25. fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
  26. fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
  27. fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
  28. fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
  29. fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
  30. fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
  31. fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
  32. exit(-1);
  33. }
  34. */
  35. static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
  36. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  37. {
  38. struct rtmsg *r = NLMSG_DATA(n);
  39. int len = n->nlmsg_len;
  40. int host_len = -1;
  41. struct rtattr * tb[RTA_MAX+1];
  42. char abuf[256];
  43. SPRINT_BUF(b1);
  44. if (n->nlmsg_type != RTM_NEWRULE)
  45. return 0;
  46. len -= NLMSG_LENGTH(sizeof(*r));
  47. if (len < 0)
  48. return -1;
  49. memset(tb, 0, sizeof(tb));
  50. parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
  51. if (r->rtm_family == AF_INET)
  52. host_len = 32;
  53. else if (r->rtm_family == AF_INET6)
  54. host_len = 128;
  55. /* else if (r->rtm_family == AF_DECnet)
  56. host_len = 16;
  57. else if (r->rtm_family == AF_IPX)
  58. host_len = 80;
  59. */
  60. printf("%u:\t", tb[RTA_PRIORITY] ?
  61. *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
  62. : 0);
  63. printf("from ");
  64. if (tb[RTA_SRC]) {
  65. if (r->rtm_src_len != host_len) {
  66. printf("%s/%u", rt_addr_n2a(r->rtm_family,
  67. RTA_DATA(tb[RTA_SRC]),
  68. abuf, sizeof(abuf)),
  69. r->rtm_src_len
  70. );
  71. } else {
  72. fputs(format_host(r->rtm_family,
  73. RTA_PAYLOAD(tb[RTA_SRC]),
  74. RTA_DATA(tb[RTA_SRC]),
  75. abuf, sizeof(abuf)),
  76. stdout
  77. );
  78. }
  79. } else if (r->rtm_src_len) {
  80. printf("0/%d", r->rtm_src_len);
  81. } else {
  82. printf("all");
  83. }
  84. bb_putchar(' ');
  85. if (tb[RTA_DST]) {
  86. if (r->rtm_dst_len != host_len) {
  87. printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
  88. RTA_DATA(tb[RTA_DST]),
  89. abuf, sizeof(abuf)),
  90. r->rtm_dst_len
  91. );
  92. } else {
  93. printf("to %s ", format_host(r->rtm_family,
  94. RTA_PAYLOAD(tb[RTA_DST]),
  95. RTA_DATA(tb[RTA_DST]),
  96. abuf, sizeof(abuf)));
  97. }
  98. } else if (r->rtm_dst_len) {
  99. printf("to 0/%d ", r->rtm_dst_len);
  100. }
  101. if (r->rtm_tos) {
  102. printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1));
  103. }
  104. if (tb[RTA_PROTOINFO]) {
  105. printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
  106. }
  107. if (tb[RTA_IIF]) {
  108. printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
  109. }
  110. if (r->rtm_table)
  111. printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1));
  112. if (tb[RTA_FLOW]) {
  113. uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
  114. uint32_t from = to>>16;
  115. to &= 0xFFFF;
  116. if (from) {
  117. printf("realms %s/",
  118. rtnl_rtrealm_n2a(from, b1));
  119. }
  120. printf("%s ",
  121. rtnl_rtrealm_n2a(to, b1));
  122. }
  123. if (r->rtm_type == RTN_NAT) {
  124. if (tb[RTA_GATEWAY]) {
  125. printf("map-to %s ",
  126. format_host(r->rtm_family,
  127. RTA_PAYLOAD(tb[RTA_GATEWAY]),
  128. RTA_DATA(tb[RTA_GATEWAY]),
  129. abuf, sizeof(abuf)));
  130. } else
  131. printf("masquerade");
  132. } else if (r->rtm_type != RTN_UNICAST)
  133. fputs(rtnl_rtntype_n2a(r->rtm_type, b1), stdout);
  134. bb_putchar('\n');
  135. /*fflush_all();*/
  136. return 0;
  137. }
  138. /* Return value becomes exitcode. It's okay to not return at all */
  139. static int iprule_list(char **argv)
  140. {
  141. struct rtnl_handle rth;
  142. int af = preferred_family;
  143. if (af == AF_UNSPEC)
  144. af = AF_INET;
  145. if (*argv) {
  146. //bb_error_msg("\"rule show\" needs no arguments");
  147. bb_warn_ignoring_args(*argv);
  148. return -1;
  149. }
  150. xrtnl_open(&rth);
  151. xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
  152. xrtnl_dump_filter(&rth, print_rule, NULL);
  153. return 0;
  154. }
  155. /* Return value becomes exitcode. It's okay to not return at all */
  156. static int iprule_modify(int cmd, char **argv)
  157. {
  158. static const char keywords[] ALIGN1 =
  159. "from\0""to\0""preference\0""order\0""priority\0"
  160. "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
  161. "iif\0""nat\0""map-to\0""type\0""help\0";
  162. enum {
  163. ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
  164. ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
  165. ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
  166. };
  167. bool table_ok = 0;
  168. struct rtnl_handle rth;
  169. struct {
  170. struct nlmsghdr n;
  171. struct rtmsg r;
  172. char buf[1024];
  173. } req;
  174. smalluint key;
  175. memset(&req, 0, sizeof(req));
  176. req.n.nlmsg_type = cmd;
  177. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  178. req.n.nlmsg_flags = NLM_F_REQUEST;
  179. req.r.rtm_family = preferred_family;
  180. req.r.rtm_protocol = RTPROT_BOOT;
  181. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  182. req.r.rtm_table = 0;
  183. req.r.rtm_type = RTN_UNSPEC;
  184. if (cmd == RTM_NEWRULE) {
  185. req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
  186. req.r.rtm_type = RTN_UNICAST;
  187. }
  188. while (*argv) {
  189. key = index_in_substrings(keywords, *argv) + 1;
  190. if (key == 0) /* no match found in keywords array, bail out. */
  191. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  192. if (key == ARG_from) {
  193. inet_prefix dst;
  194. NEXT_ARG();
  195. get_prefix(&dst, *argv, req.r.rtm_family);
  196. req.r.rtm_src_len = dst.bitlen;
  197. addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
  198. } else if (key == ARG_to) {
  199. inet_prefix dst;
  200. NEXT_ARG();
  201. get_prefix(&dst, *argv, req.r.rtm_family);
  202. req.r.rtm_dst_len = dst.bitlen;
  203. addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
  204. } else if (key == ARG_preference ||
  205. key == ARG_order ||
  206. key == ARG_priority
  207. ) {
  208. uint32_t pref;
  209. NEXT_ARG();
  210. pref = get_u32(*argv, "preference");
  211. addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
  212. } else if (key == ARG_tos) {
  213. uint32_t tos;
  214. NEXT_ARG();
  215. if (rtnl_dsfield_a2n(&tos, *argv))
  216. invarg(*argv, "TOS");
  217. req.r.rtm_tos = tos;
  218. } else if (key == ARG_fwmark) {
  219. uint32_t fwmark;
  220. NEXT_ARG();
  221. fwmark = get_u32(*argv, "fwmark");
  222. addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
  223. } else if (key == ARG_realms) {
  224. uint32_t realm;
  225. NEXT_ARG();
  226. if (get_rt_realms(&realm, *argv))
  227. invarg(*argv, "realms");
  228. addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
  229. } else if (key == ARG_table ||
  230. key == ARG_lookup
  231. ) {
  232. uint32_t tid;
  233. NEXT_ARG();
  234. if (rtnl_rttable_a2n(&tid, *argv))
  235. invarg(*argv, "table ID");
  236. req.r.rtm_table = tid;
  237. table_ok = 1;
  238. } else if (key == ARG_dev ||
  239. key == ARG_iif
  240. ) {
  241. NEXT_ARG();
  242. addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
  243. } else if (key == ARG_nat ||
  244. key == ARG_map_to
  245. ) {
  246. NEXT_ARG();
  247. addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
  248. req.r.rtm_type = RTN_NAT;
  249. } else {
  250. int type;
  251. if (key == ARG_type) {
  252. NEXT_ARG();
  253. }
  254. if (key == ARG_help)
  255. bb_show_usage();
  256. if (rtnl_rtntype_a2n(&type, *argv))
  257. invarg(*argv, "type");
  258. req.r.rtm_type = type;
  259. }
  260. argv++;
  261. }
  262. if (req.r.rtm_family == AF_UNSPEC)
  263. req.r.rtm_family = AF_INET;
  264. if (!table_ok && cmd == RTM_NEWRULE)
  265. req.r.rtm_table = RT_TABLE_MAIN;
  266. xrtnl_open(&rth);
  267. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  268. return 2;
  269. return 0;
  270. }
  271. /* Return value becomes exitcode. It's okay to not return at all */
  272. int FAST_FUNC do_iprule(char **argv)
  273. {
  274. static const char ip_rule_commands[] ALIGN1 =
  275. "add\0""delete\0""list\0""show\0";
  276. if (*argv) {
  277. smalluint cmd = index_in_substrings(ip_rule_commands, *argv);
  278. if (cmd > 3)
  279. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  280. argv++;
  281. if (cmd < 2)
  282. return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
  283. }
  284. return iprule_list(argv);
  285. }