Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 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. |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 9 | * |
| 10 | * Changes: |
| 11 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 12 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 13 | * initially integrated into busybox by Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 16 | #include <netinet/in.h> |
| 17 | #include <netinet/ip.h> |
| 18 | #include <arpa/inet.h> |
| 19 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 20 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 21 | #include "rt_names.h" |
| 22 | #include "utils.h" |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 23 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 24 | /* |
| 25 | static void usage(void) __attribute__((noreturn)); |
| 26 | |
| 27 | static void usage(void) |
| 28 | { |
| 29 | fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n"); |
| 30 | fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n"); |
| 31 | fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n"); |
| 32 | fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n"); |
| 33 | fprintf(stderr, " [ prohibit | reject | unreachable ]\n"); |
| 34 | fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n"); |
| 35 | fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n"); |
| 36 | exit(-1); |
| 37 | } |
| 38 | */ |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 39 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 40 | static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 41 | struct nlmsghdr *n, void *arg UNUSED_PARAM) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 42 | { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 43 | struct rtmsg *r = NLMSG_DATA(n); |
| 44 | int len = n->nlmsg_len; |
| 45 | int host_len = -1; |
| 46 | struct rtattr * tb[RTA_MAX+1]; |
| 47 | char abuf[256]; |
| 48 | SPRINT_BUF(b1); |
| 49 | |
| 50 | if (n->nlmsg_type != RTM_NEWRULE) |
| 51 | return 0; |
| 52 | |
| 53 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 54 | if (len < 0) |
| 55 | return -1; |
| 56 | |
| 57 | memset(tb, 0, sizeof(tb)); |
| 58 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); |
| 59 | |
| 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 | */ |
Bernhard Reutner-Fischer | 1b35498 | 2010-05-25 17:22:17 +0200 | [diff] [blame] | 69 | printf("%u:\t", tb[RTA_PRIORITY] ? |
| 70 | *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]) |
| 71 | : 0); |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 72 | printf("from "); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 73 | if (tb[RTA_SRC]) { |
| 74 | if (r->rtm_src_len != host_len) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 75 | printf("%s/%u", rt_addr_n2a(r->rtm_family, |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 76 | RTA_DATA(tb[RTA_SRC]), |
| 77 | abuf, sizeof(abuf)), |
| 78 | r->rtm_src_len |
| 79 | ); |
| 80 | } else { |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 81 | fputs(format_host(r->rtm_family, |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 82 | RTA_PAYLOAD(tb[RTA_SRC]), |
| 83 | RTA_DATA(tb[RTA_SRC]), |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 84 | abuf, sizeof(abuf)), stdout); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 85 | } |
| 86 | } else if (r->rtm_src_len) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 87 | printf("0/%d", r->rtm_src_len); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 88 | } else { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 89 | printf("all"); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 90 | } |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 91 | bb_putchar(' '); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 92 | |
| 93 | if (tb[RTA_DST]) { |
| 94 | if (r->rtm_dst_len != host_len) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 95 | printf("to %s/%u ", rt_addr_n2a(r->rtm_family, |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 96 | RTA_DATA(tb[RTA_DST]), |
| 97 | abuf, sizeof(abuf)), |
| 98 | r->rtm_dst_len |
| 99 | ); |
| 100 | } else { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 101 | printf("to %s ", format_host(r->rtm_family, |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 102 | RTA_PAYLOAD(tb[RTA_DST]), |
| 103 | RTA_DATA(tb[RTA_DST]), |
| 104 | abuf, sizeof(abuf))); |
| 105 | } |
| 106 | } else if (r->rtm_dst_len) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 107 | printf("to 0/%d ", r->rtm_dst_len); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (r->rtm_tos) { |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 111 | printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1)); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 112 | } |
| 113 | if (tb[RTA_PROTOINFO]) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 114 | printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO])); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (tb[RTA_IIF]) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 118 | printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF])); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (r->rtm_table) |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 122 | printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1)); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 123 | |
| 124 | if (tb[RTA_FLOW]) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 125 | uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]); |
| 126 | uint32_t from = to>>16; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 127 | to &= 0xFFFF; |
| 128 | if (from) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 129 | printf("realms %s/", |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 130 | rtnl_rtrealm_n2a(from, b1)); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 131 | } |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 132 | printf("%s ", |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 133 | rtnl_rtrealm_n2a(to, b1)); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (r->rtm_type == RTN_NAT) { |
| 137 | if (tb[RTA_GATEWAY]) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 138 | printf("map-to %s ", |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 139 | format_host(r->rtm_family, |
| 140 | RTA_PAYLOAD(tb[RTA_GATEWAY]), |
| 141 | RTA_DATA(tb[RTA_GATEWAY]), |
| 142 | abuf, sizeof(abuf))); |
| 143 | } else |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 144 | printf("masquerade"); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 145 | } else if (r->rtm_type != RTN_UNICAST) |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 146 | fputs(rtnl_rtntype_n2a(r->rtm_type, b1), stdout); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 148 | bb_putchar('\n'); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 149 | /*fflush_all();*/ |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 153 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 154 | static int iprule_list(char **argv) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 155 | { |
| 156 | struct rtnl_handle rth; |
| 157 | int af = preferred_family; |
| 158 | |
| 159 | if (af == AF_UNSPEC) |
| 160 | af = AF_INET; |
| 161 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 162 | if (*argv) { |
Bernhard Reutner-Fischer | 4d9a358 | 2007-01-28 00:20:46 +0000 | [diff] [blame] | 163 | //bb_error_msg("\"rule show\" needs no arguments"); |
Denys Vlasenko | a355da0 | 2010-01-04 13:16:08 +0100 | [diff] [blame] | 164 | bb_warn_ignoring_args(*argv); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 165 | return -1; |
| 166 | } |
| 167 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 168 | xrtnl_open(&rth); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 169 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 170 | xrtnl_wilddump_request(&rth, af, RTM_GETRULE); |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 171 | xrtnl_dump_filter(&rth, print_rule, NULL); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 176 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 177 | static int iprule_modify(int cmd, char **argv) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 178 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 179 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 180 | "from\0""to\0""preference\0""order\0""priority\0" |
| 181 | "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0" |
| 182 | "iif\0""nat\0""map-to\0""type\0""help\0"; |
| 183 | enum { |
| 184 | ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority, |
| 185 | ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev, |
| 186 | ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help |
| 187 | }; |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 188 | bool table_ok = 0; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 189 | struct rtnl_handle rth; |
| 190 | struct { |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 191 | struct nlmsghdr n; |
| 192 | struct rtmsg r; |
| 193 | char buf[1024]; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 194 | } req; |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 195 | smalluint key; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 196 | |
| 197 | memset(&req, 0, sizeof(req)); |
| 198 | |
| 199 | req.n.nlmsg_type = cmd; |
| 200 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 201 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 202 | req.r.rtm_family = preferred_family; |
| 203 | req.r.rtm_protocol = RTPROT_BOOT; |
| 204 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
| 205 | req.r.rtm_table = 0; |
| 206 | req.r.rtm_type = RTN_UNSPEC; |
| 207 | |
| 208 | if (cmd == RTM_NEWRULE) { |
| 209 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; |
| 210 | req.r.rtm_type = RTN_UNICAST; |
| 211 | } |
| 212 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 213 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 214 | key = index_in_substrings(keywords, *argv) + 1; |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 215 | if (key == 0) /* no match found in keywords array, bail out. */ |
| 216 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
| 217 | if (key == ARG_from) { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 218 | inet_prefix dst; |
| 219 | NEXT_ARG(); |
| 220 | get_prefix(&dst, *argv, req.r.rtm_family); |
| 221 | req.r.rtm_src_len = dst.bitlen; |
| 222 | addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen); |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 223 | } else if (key == ARG_to) { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 224 | inet_prefix dst; |
| 225 | NEXT_ARG(); |
| 226 | get_prefix(&dst, *argv, req.r.rtm_family); |
| 227 | req.r.rtm_dst_len = dst.bitlen; |
| 228 | addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 229 | } else if (key == ARG_preference || |
| 230 | key == ARG_order || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 231 | key == ARG_priority |
| 232 | ) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 233 | uint32_t pref; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 234 | NEXT_ARG(); |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 235 | pref = get_u32(*argv, "preference"); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 236 | addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref); |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 237 | } else if (key == ARG_tos) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 238 | uint32_t tos; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 239 | NEXT_ARG(); |
| 240 | if (rtnl_dsfield_a2n(&tos, *argv)) |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 241 | invarg(*argv, "TOS"); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 242 | req.r.rtm_tos = tos; |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 243 | } else if (key == ARG_fwmark) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 244 | uint32_t fwmark; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 245 | NEXT_ARG(); |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 246 | fwmark = get_u32(*argv, "fwmark"); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 247 | addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark); |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 248 | } else if (key == ARG_realms) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 249 | uint32_t realm; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 250 | NEXT_ARG(); |
| 251 | if (get_rt_realms(&realm, *argv)) |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 252 | invarg(*argv, "realms"); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 253 | addattr32(&req.n, sizeof(req), RTA_FLOW, realm); |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 254 | } else if (key == ARG_table || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 255 | key == ARG_lookup |
| 256 | ) { |
Bernhard Reutner-Fischer | 4d9a358 | 2007-01-28 00:20:46 +0000 | [diff] [blame] | 257 | uint32_t tid; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 258 | NEXT_ARG(); |
| 259 | if (rtnl_rttable_a2n(&tid, *argv)) |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 260 | invarg(*argv, "table ID"); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 261 | req.r.rtm_table = tid; |
| 262 | table_ok = 1; |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 263 | } else if (key == ARG_dev || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 264 | key == ARG_iif |
| 265 | ) { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 266 | NEXT_ARG(); |
| 267 | addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1); |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 268 | } else if (key == ARG_nat || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 269 | key == ARG_map_to |
| 270 | ) { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 271 | NEXT_ARG(); |
| 272 | addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv)); |
| 273 | req.r.rtm_type = RTN_NAT; |
| 274 | } else { |
| 275 | int type; |
| 276 | |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 277 | if (key == ARG_type) { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 278 | NEXT_ARG(); |
| 279 | } |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 280 | if (key == ARG_help) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 281 | bb_show_usage(); |
| 282 | if (rtnl_rtntype_a2n(&type, *argv)) |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 283 | invarg(*argv, "type"); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 284 | req.r.rtm_type = type; |
| 285 | } |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 286 | argv++; |
| 287 | } |
| 288 | |
| 289 | if (req.r.rtm_family == AF_UNSPEC) |
| 290 | req.r.rtm_family = AF_INET; |
| 291 | |
| 292 | if (!table_ok && cmd == RTM_NEWRULE) |
| 293 | req.r.rtm_table = RT_TABLE_MAIN; |
| 294 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 295 | xrtnl_open(&rth); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 296 | |
| 297 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 298 | return 2; |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 303 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denys Vlasenko | 2e9b551 | 2010-07-24 23:27:38 +0200 | [diff] [blame] | 304 | int FAST_FUNC do_iprule(char **argv) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 305 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 306 | static const char ip_rule_commands[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 307 | "add\0""delete\0""list\0""show\0"; |
Bernhard Reutner-Fischer | 1b35498 | 2010-05-25 17:22:17 +0200 | [diff] [blame] | 308 | if (*argv) { |
| 309 | smalluint cmd = index_in_substrings(ip_rule_commands, *argv); |
| 310 | if (cmd > 3) |
| 311 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
| 312 | argv++; |
| 313 | if (cmd < 2) |
| 314 | return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 315 | } |
Bernhard Reutner-Fischer | 1b35498 | 2010-05-25 17:22:17 +0200 | [diff] [blame] | 316 | return iprule_list(argv); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 317 | } |