iprule.c 8.5 KB

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