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