iprule.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. /* from <linux/fib_rules.h>: */
  19. #define FRA_FWMARK 10
  20. #define FRA_SUPPRESS_IFGROUP 13
  21. #define FRA_SUPPRESS_PREFIXLEN 14
  22. #define FRA_FWMASK 16
  23. #include "ip_common.h" /* #include "libbb.h" is inside */
  24. #include "rt_names.h"
  25. #include "utils.h"
  26. #include <linux/version.h>
  27. /* RTA_TABLE is not a define, can't test with ifdef. */
  28. /* As a proxy, test which kernels toolchain expects: */
  29. #define HAVE_RTA_TABLE (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19))
  30. /* If you add stuff here, update iprule_full_usage */
  31. static const char keywords[] ALIGN1 =
  32. "from\0""to\0""preference\0""order\0""priority\0"
  33. "tos\0""fwmark\0""realms\0""table\0""lookup\0"
  34. "suppress_prefixlength\0""suppress_ifgroup\0"
  35. "dev\0""iif\0""nat\0""map-to\0""type\0""help\0"
  36. ;
  37. #define keyword_preference (keywords + sizeof("from") + sizeof("to"))
  38. #define keyword_fwmark (keyword_preference + sizeof("preference") + sizeof("order") + sizeof("priority") + sizeof("tos"))
  39. #define keyword_realms (keyword_fwmark + sizeof("fwmark"))
  40. #define keyword_suppress_prefixlength (keyword_realms + sizeof("realms") + sizeof("table") + sizeof("lookup"))
  41. #define keyword_suppress_ifgroup (keyword_suppress_prefixlength + sizeof("suppress_prefixlength"))
  42. enum {
  43. ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
  44. ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup,
  45. ARG_suppress_prefixlength, ARG_suppress_ifgroup,
  46. ARG_dev, ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help,
  47. };
  48. static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
  49. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  50. {
  51. struct rtmsg *r = NLMSG_DATA(n);
  52. int len = n->nlmsg_len;
  53. int host_len = -1;
  54. struct rtattr * tb[RTA_MAX+1];
  55. if (n->nlmsg_type != RTM_NEWRULE)
  56. return 0;
  57. len -= NLMSG_LENGTH(sizeof(*r));
  58. if (len < 0)
  59. return -1;
  60. //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
  61. parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
  62. if (r->rtm_family == AF_INET)
  63. host_len = 32;
  64. else if (r->rtm_family == AF_INET6)
  65. host_len = 128;
  66. /* else if (r->rtm_family == AF_DECnet)
  67. host_len = 16;
  68. else if (r->rtm_family == AF_IPX)
  69. host_len = 80;
  70. */
  71. printf("%u:\t", tb[RTA_PRIORITY] ?
  72. *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
  73. : 0);
  74. printf("from ");
  75. if (tb[RTA_SRC]) {
  76. if (r->rtm_src_len != host_len) {
  77. printf("%s/%u",
  78. rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
  79. r->rtm_src_len
  80. );
  81. } else {
  82. fputs(format_host(r->rtm_family,
  83. RTA_PAYLOAD(tb[RTA_SRC]),
  84. RTA_DATA(tb[RTA_SRC])),
  85. stdout
  86. );
  87. }
  88. } else if (r->rtm_src_len) {
  89. printf("0/%d", r->rtm_src_len);
  90. } else {
  91. printf("all");
  92. }
  93. bb_putchar(' ');
  94. if (tb[RTA_DST]) {
  95. if (r->rtm_dst_len != host_len) {
  96. printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
  97. RTA_DATA(tb[RTA_DST])),
  98. r->rtm_dst_len
  99. );
  100. } else {
  101. printf("to %s ", format_host(r->rtm_family,
  102. RTA_PAYLOAD(tb[RTA_DST]),
  103. RTA_DATA(tb[RTA_DST])));
  104. }
  105. } else if (r->rtm_dst_len) {
  106. printf("to 0/%d ", r->rtm_dst_len);
  107. }
  108. if (r->rtm_tos) {
  109. printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos));
  110. }
  111. if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) {
  112. uint32_t mark = 0, mask = 0;
  113. if (tb[FRA_FWMARK])
  114. mark = *(uint32_t*)RTA_DATA(tb[FRA_FWMARK]);
  115. if (tb[FRA_FWMASK]
  116. && (mask = *(uint32_t*)RTA_DATA(tb[FRA_FWMASK])) != 0xFFFFFFFF
  117. )
  118. printf("fwmark %#x/%#x ", mark, mask);
  119. else
  120. printf("fwmark %#x ", mark);
  121. }
  122. if (tb[RTA_IIF]) {
  123. printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
  124. }
  125. #if HAVE_RTA_TABLE
  126. if (tb[RTA_TABLE])
  127. printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
  128. else
  129. #endif
  130. if (r->rtm_table)
  131. printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
  132. if (tb[FRA_SUPPRESS_PREFIXLEN]) {
  133. int pl = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_PREFIXLEN]);
  134. if (pl != -1)
  135. printf("%s %d ", keyword_suppress_prefixlength, pl);
  136. }
  137. if (tb[FRA_SUPPRESS_IFGROUP]) {
  138. int grp = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_IFGROUP]);
  139. if (grp != -1)
  140. printf("%s %d ", keyword_suppress_ifgroup, grp);
  141. }
  142. if (tb[RTA_FLOW]) {
  143. uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
  144. uint32_t from = to>>16;
  145. to &= 0xFFFF;
  146. if (from) {
  147. printf("realms %s/",
  148. rtnl_rtrealm_n2a(from));
  149. }
  150. printf("%s ",
  151. rtnl_rtrealm_n2a(to));
  152. }
  153. if (r->rtm_type == RTN_NAT) {
  154. if (tb[RTA_GATEWAY]) {
  155. printf("map-to %s ",
  156. format_host(r->rtm_family,
  157. RTA_PAYLOAD(tb[RTA_GATEWAY]),
  158. RTA_DATA(tb[RTA_GATEWAY]))
  159. );
  160. } else
  161. printf("masquerade");
  162. } else if (r->rtm_type != RTN_UNICAST)
  163. fputs(rtnl_rtntype_n2a(r->rtm_type), stdout);
  164. bb_putchar('\n');
  165. /*fflush_all();*/
  166. return 0;
  167. }
  168. /* Return value becomes exitcode. It's okay to not return at all */
  169. static int iprule_list(char **argv)
  170. {
  171. struct rtnl_handle rth;
  172. int af = preferred_family;
  173. if (af == AF_UNSPEC)
  174. af = AF_INET;
  175. if (*argv) {
  176. //bb_error_msg("\"rule show\" needs no arguments");
  177. bb_warn_ignoring_args(*argv);
  178. return -1;
  179. }
  180. xrtnl_open(&rth);
  181. xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
  182. xrtnl_dump_filter(&rth, print_rule, NULL);
  183. return 0;
  184. }
  185. /* Return value becomes exitcode. It's okay to not return at all */
  186. static int iprule_modify(int cmd, char **argv)
  187. {
  188. bool table_ok = 0;
  189. struct rtnl_handle rth;
  190. struct {
  191. struct nlmsghdr n;
  192. struct rtmsg r;
  193. char buf[1024];
  194. } req;
  195. smalluint key;
  196. memset(&req, 0, sizeof(req));
  197. req.n.nlmsg_type = cmd;
  198. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  199. req.n.nlmsg_flags = NLM_F_REQUEST;
  200. req.r.rtm_family = preferred_family;
  201. req.r.rtm_protocol = RTPROT_BOOT;
  202. if (RT_SCOPE_UNIVERSE != 0)
  203. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  204. /*req.r.rtm_table = 0; - already is */
  205. if (RTN_UNSPEC != 0)
  206. req.r.rtm_type = RTN_UNSPEC;
  207. if (cmd == RTM_NEWRULE) {
  208. req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
  209. req.r.rtm_type = RTN_UNICAST;
  210. }
  211. while (*argv) {
  212. key = index_in_substrings(keywords, *argv) + 1;
  213. if (key == 0) /* no match found in keywords array, bail out. */
  214. invarg_1_to_2(*argv, applet_name);
  215. if (key == ARG_from) {
  216. inet_prefix dst;
  217. NEXT_ARG();
  218. get_prefix(&dst, *argv, req.r.rtm_family);
  219. req.r.rtm_src_len = dst.bitlen;
  220. addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
  221. } else if (key == ARG_to) {
  222. inet_prefix dst;
  223. NEXT_ARG();
  224. get_prefix(&dst, *argv, req.r.rtm_family);
  225. req.r.rtm_dst_len = dst.bitlen;
  226. addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
  227. } else if (key == ARG_preference ||
  228. key == ARG_order ||
  229. key == ARG_priority
  230. ) {
  231. uint32_t pref;
  232. NEXT_ARG();
  233. pref = get_u32(*argv, keyword_preference);
  234. addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
  235. } else if (key == ARG_tos) {
  236. uint32_t tos;
  237. NEXT_ARG();
  238. if (rtnl_dsfield_a2n(&tos, *argv))
  239. invarg_1_to_2(*argv, "TOS");
  240. req.r.rtm_tos = tos;
  241. } else if (key == ARG_fwmark) {
  242. char *slash;
  243. uint32_t fwmark, fwmask;
  244. NEXT_ARG();
  245. slash = strchr(*argv, '/');
  246. if (slash)
  247. *slash = '\0';
  248. fwmark = get_u32(*argv, keyword_fwmark);
  249. addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark);
  250. if (slash) {
  251. fwmask = get_u32(slash + 1, "fwmask");
  252. addattr32(&req.n, sizeof(req), FRA_FWMASK, fwmask);
  253. }
  254. } else if (key == ARG_realms) {
  255. uint32_t realm;
  256. NEXT_ARG();
  257. if (get_rt_realms(&realm, *argv))
  258. invarg_1_to_2(*argv, keyword_realms);
  259. addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
  260. } else if (key == ARG_table ||
  261. key == ARG_lookup
  262. ) {
  263. uint32_t tid;
  264. NEXT_ARG();
  265. if (rtnl_rttable_a2n(&tid, *argv))
  266. invarg_1_to_2(*argv, "table ID");
  267. #if HAVE_RTA_TABLE
  268. if (tid > 255) {
  269. req.r.rtm_table = RT_TABLE_UNSPEC;
  270. addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
  271. } else
  272. #endif
  273. req.r.rtm_table = tid;
  274. table_ok = 1;
  275. } else if (key == ARG_suppress_prefixlength) {
  276. int prefix_length;
  277. NEXT_ARG();
  278. prefix_length = get_u32(*argv, keyword_suppress_prefixlength);
  279. addattr32(&req.n, sizeof(req), FRA_SUPPRESS_PREFIXLEN, prefix_length);
  280. } else if (key == ARG_suppress_ifgroup) {
  281. int grp;
  282. NEXT_ARG();
  283. grp = get_u32(*argv, keyword_suppress_ifgroup);
  284. addattr32(&req.n, sizeof(req), FRA_SUPPRESS_IFGROUP, grp);
  285. } else if (key == ARG_dev ||
  286. key == ARG_iif
  287. ) {
  288. NEXT_ARG();
  289. addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
  290. } else if (key == ARG_nat ||
  291. key == ARG_map_to
  292. ) {
  293. NEXT_ARG();
  294. addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
  295. req.r.rtm_type = RTN_NAT;
  296. } else {
  297. int type;
  298. if (key == ARG_type) {
  299. NEXT_ARG();
  300. }
  301. if (key == ARG_help)
  302. bb_show_usage();
  303. if (rtnl_rtntype_a2n(&type, *argv))
  304. invarg_1_to_2(*argv, "type");
  305. req.r.rtm_type = type;
  306. }
  307. argv++;
  308. }
  309. if (req.r.rtm_family == AF_UNSPEC)
  310. req.r.rtm_family = AF_INET;
  311. if (!table_ok && cmd == RTM_NEWRULE)
  312. req.r.rtm_table = RT_TABLE_MAIN;
  313. xrtnl_open(&rth);
  314. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  315. return 2;
  316. return 0;
  317. }
  318. /* Return value becomes exitcode. It's okay to not return at all */
  319. int FAST_FUNC do_iprule(char **argv)
  320. {
  321. static const char ip_rule_commands[] ALIGN1 =
  322. "add\0""delete\0""list\0""show\0";
  323. if (*argv) {
  324. int cmd = index_in_substrings(ip_rule_commands, *argv);
  325. if (cmd < 0)
  326. invarg_1_to_2(*argv, applet_name);
  327. argv++;
  328. if (cmd < 2)
  329. return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
  330. }
  331. return iprule_list(argv);
  332. }