3
0

iprule.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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)), stdout);
  76. }
  77. } else if (r->rtm_src_len) {
  78. printf("0/%d", r->rtm_src_len);
  79. } else {
  80. printf("all");
  81. }
  82. bb_putchar(' ');
  83. if (tb[RTA_DST]) {
  84. if (r->rtm_dst_len != host_len) {
  85. printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
  86. RTA_DATA(tb[RTA_DST]),
  87. abuf, sizeof(abuf)),
  88. r->rtm_dst_len
  89. );
  90. } else {
  91. printf("to %s ", format_host(r->rtm_family,
  92. RTA_PAYLOAD(tb[RTA_DST]),
  93. RTA_DATA(tb[RTA_DST]),
  94. abuf, sizeof(abuf)));
  95. }
  96. } else if (r->rtm_dst_len) {
  97. printf("to 0/%d ", r->rtm_dst_len);
  98. }
  99. if (r->rtm_tos) {
  100. printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1));
  101. }
  102. if (tb[RTA_PROTOINFO]) {
  103. printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
  104. }
  105. if (tb[RTA_IIF]) {
  106. printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
  107. }
  108. if (r->rtm_table)
  109. printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1));
  110. if (tb[RTA_FLOW]) {
  111. uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
  112. uint32_t from = to>>16;
  113. to &= 0xFFFF;
  114. if (from) {
  115. printf("realms %s/",
  116. rtnl_rtrealm_n2a(from, b1));
  117. }
  118. printf("%s ",
  119. rtnl_rtrealm_n2a(to, b1));
  120. }
  121. if (r->rtm_type == RTN_NAT) {
  122. if (tb[RTA_GATEWAY]) {
  123. printf("map-to %s ",
  124. format_host(r->rtm_family,
  125. RTA_PAYLOAD(tb[RTA_GATEWAY]),
  126. RTA_DATA(tb[RTA_GATEWAY]),
  127. abuf, sizeof(abuf)));
  128. } else
  129. printf("masquerade");
  130. } else if (r->rtm_type != RTN_UNICAST)
  131. fputs(rtnl_rtntype_n2a(r->rtm_type, b1), stdout);
  132. bb_putchar('\n');
  133. /*fflush_all();*/
  134. return 0;
  135. }
  136. /* Return value becomes exitcode. It's okay to not return at all */
  137. static int iprule_list(char **argv)
  138. {
  139. struct rtnl_handle rth;
  140. int af = preferred_family;
  141. if (af == AF_UNSPEC)
  142. af = AF_INET;
  143. if (*argv) {
  144. //bb_error_msg("\"rule show\" needs no arguments");
  145. bb_warn_ignoring_args(*argv);
  146. return -1;
  147. }
  148. xrtnl_open(&rth);
  149. xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
  150. xrtnl_dump_filter(&rth, print_rule, NULL);
  151. return 0;
  152. }
  153. /* Return value becomes exitcode. It's okay to not return at all */
  154. static int iprule_modify(int cmd, char **argv)
  155. {
  156. static const char keywords[] ALIGN1 =
  157. "from\0""to\0""preference\0""order\0""priority\0"
  158. "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
  159. "iif\0""nat\0""map-to\0""type\0""help\0";
  160. enum {
  161. ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
  162. ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
  163. ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
  164. };
  165. bool table_ok = 0;
  166. struct rtnl_handle rth;
  167. struct {
  168. struct nlmsghdr n;
  169. struct rtmsg r;
  170. char buf[1024];
  171. } req;
  172. smalluint key;
  173. memset(&req, 0, sizeof(req));
  174. req.n.nlmsg_type = cmd;
  175. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  176. req.n.nlmsg_flags = NLM_F_REQUEST;
  177. req.r.rtm_family = preferred_family;
  178. req.r.rtm_protocol = RTPROT_BOOT;
  179. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  180. req.r.rtm_table = 0;
  181. req.r.rtm_type = RTN_UNSPEC;
  182. if (cmd == RTM_NEWRULE) {
  183. req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
  184. req.r.rtm_type = RTN_UNICAST;
  185. }
  186. while (*argv) {
  187. key = index_in_substrings(keywords, *argv) + 1;
  188. if (key == 0) /* no match found in keywords array, bail out. */
  189. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  190. if (key == ARG_from) {
  191. inet_prefix dst;
  192. NEXT_ARG();
  193. get_prefix(&dst, *argv, req.r.rtm_family);
  194. req.r.rtm_src_len = dst.bitlen;
  195. addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
  196. } else if (key == ARG_to) {
  197. inet_prefix dst;
  198. NEXT_ARG();
  199. get_prefix(&dst, *argv, req.r.rtm_family);
  200. req.r.rtm_dst_len = dst.bitlen;
  201. addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
  202. } else if (key == ARG_preference ||
  203. key == ARG_order ||
  204. key == ARG_priority
  205. ) {
  206. uint32_t pref;
  207. NEXT_ARG();
  208. pref = get_u32(*argv, "preference");
  209. addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
  210. } else if (key == ARG_tos) {
  211. uint32_t tos;
  212. NEXT_ARG();
  213. if (rtnl_dsfield_a2n(&tos, *argv))
  214. invarg(*argv, "TOS");
  215. req.r.rtm_tos = tos;
  216. } else if (key == ARG_fwmark) {
  217. uint32_t fwmark;
  218. NEXT_ARG();
  219. fwmark = get_u32(*argv, "fwmark");
  220. addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
  221. } else if (key == ARG_realms) {
  222. uint32_t realm;
  223. NEXT_ARG();
  224. if (get_rt_realms(&realm, *argv))
  225. invarg(*argv, "realms");
  226. addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
  227. } else if (key == ARG_table ||
  228. key == ARG_lookup
  229. ) {
  230. uint32_t tid;
  231. NEXT_ARG();
  232. if (rtnl_rttable_a2n(&tid, *argv))
  233. invarg(*argv, "table ID");
  234. req.r.rtm_table = tid;
  235. table_ok = 1;
  236. } else if (key == ARG_dev ||
  237. key == ARG_iif
  238. ) {
  239. NEXT_ARG();
  240. addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
  241. } else if (key == ARG_nat ||
  242. key == ARG_map_to
  243. ) {
  244. NEXT_ARG();
  245. addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
  246. req.r.rtm_type = RTN_NAT;
  247. } else {
  248. int type;
  249. if (key == ARG_type) {
  250. NEXT_ARG();
  251. }
  252. if (key == ARG_help)
  253. bb_show_usage();
  254. if (rtnl_rtntype_a2n(&type, *argv))
  255. invarg(*argv, "type");
  256. req.r.rtm_type = type;
  257. }
  258. argv++;
  259. }
  260. if (req.r.rtm_family == AF_UNSPEC)
  261. req.r.rtm_family = AF_INET;
  262. if (!table_ok && cmd == RTM_NEWRULE)
  263. req.r.rtm_table = RT_TABLE_MAIN;
  264. xrtnl_open(&rth);
  265. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  266. return 2;
  267. return 0;
  268. }
  269. /* Return value becomes exitcode. It's okay to not return at all */
  270. int FAST_FUNC do_iprule(char **argv)
  271. {
  272. static const char ip_rule_commands[] ALIGN1 =
  273. "add\0""delete\0""list\0""show\0";
  274. if (*argv) {
  275. smalluint cmd = index_in_substrings(ip_rule_commands, *argv);
  276. if (cmd > 3)
  277. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  278. argv++;
  279. if (cmd < 2)
  280. return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
  281. }
  282. return iprule_list(argv);
  283. }