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