iprule.c 9.0 KB

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