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