3
0

iprule.c 9.3 KB

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