iprule.c 7.9 KB

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