3
0

iprule.c 8.4 KB

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