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 | |
| 19 | #include "libbb.h" |
| 20 | #include <syslog.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <netinet/in.h> |
| 23 | #include <netinet/ip.h> |
| 24 | #include <arpa/inet.h> |
| 25 | |
| 26 | #include "rt_names.h" |
| 27 | #include "utils.h" |
Bernhard Reutner-Fischer | 620e57b | 2007-01-22 17:42:37 +0000 | [diff] [blame] | 28 | #include "ip_common.h" |
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 | */ |
| 44 | static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED, |
| 45 | struct nlmsghdr *n, void *arg) |
| 46 | { |
| 47 | FILE *fp = (FILE*)arg; |
| 48 | struct rtmsg *r = NLMSG_DATA(n); |
| 49 | int len = n->nlmsg_len; |
| 50 | int host_len = -1; |
| 51 | struct rtattr * tb[RTA_MAX+1]; |
| 52 | char abuf[256]; |
| 53 | SPRINT_BUF(b1); |
| 54 | |
| 55 | if (n->nlmsg_type != RTM_NEWRULE) |
| 56 | return 0; |
| 57 | |
| 58 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 59 | if (len < 0) |
| 60 | return -1; |
| 61 | |
| 62 | memset(tb, 0, sizeof(tb)); |
| 63 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); |
| 64 | |
| 65 | if (r->rtm_family == AF_INET) |
| 66 | host_len = 32; |
| 67 | else if (r->rtm_family == AF_INET6) |
| 68 | host_len = 128; |
| 69 | /* else if (r->rtm_family == AF_DECnet) |
| 70 | host_len = 16; |
| 71 | else if (r->rtm_family == AF_IPX) |
| 72 | host_len = 80; |
| 73 | */ |
| 74 | if (tb[RTA_PRIORITY]) |
| 75 | fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])); |
| 76 | else |
| 77 | fprintf(fp, "0:\t"); |
| 78 | |
| 79 | fprintf(fp, "from "); |
| 80 | if (tb[RTA_SRC]) { |
| 81 | if (r->rtm_src_len != host_len) { |
| 82 | fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family, |
| 83 | RTA_PAYLOAD(tb[RTA_SRC]), |
| 84 | RTA_DATA(tb[RTA_SRC]), |
| 85 | abuf, sizeof(abuf)), |
| 86 | r->rtm_src_len |
| 87 | ); |
| 88 | } else { |
| 89 | fprintf(fp, "%s", format_host(r->rtm_family, |
| 90 | RTA_PAYLOAD(tb[RTA_SRC]), |
| 91 | RTA_DATA(tb[RTA_SRC]), |
| 92 | abuf, sizeof(abuf)) |
| 93 | ); |
| 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) |
| 156 | fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1))); |
| 157 | |
| 158 | fprintf(fp, "\n"); |
| 159 | fflush(fp); |
| 160 | return 0; |
| 161 | } |
| 162 | |
Bernhard Reutner-Fischer | 620e57b | 2007-01-22 17:42:37 +0000 | [diff] [blame] | 163 | static int iprule_list(int argc, char **argv) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 164 | { |
| 165 | struct rtnl_handle rth; |
| 166 | int af = preferred_family; |
| 167 | |
| 168 | if (af == AF_UNSPEC) |
| 169 | af = AF_INET; |
| 170 | |
| 171 | if (argc > 0) { |
Bernhard Reutner-Fischer | 4d9a358 | 2007-01-28 00:20:46 +0000 | [diff] [blame^] | 172 | //bb_error_msg("\"rule show\" needs no arguments"); |
| 173 | bb_warn_ignoring_args(argc); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | if (rtnl_open(&rth, 0) < 0) |
| 178 | return 1; |
| 179 | |
| 180 | if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { |
| 181 | bb_perror_msg("Cannot send dump request"); |
| 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) { |
| 186 | bb_error_msg("Dump terminated"); |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | |
Bernhard Reutner-Fischer | 620e57b | 2007-01-22 17:42:37 +0000 | [diff] [blame] | 194 | static int iprule_modify(int cmd, int argc, char **argv) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 195 | { |
| 196 | int table_ok = 0; |
| 197 | struct rtnl_handle rth; |
| 198 | struct { |
| 199 | struct nlmsghdr n; |
| 200 | struct rtmsg r; |
| 201 | char buf[1024]; |
| 202 | } req; |
| 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 | |
| 220 | while (argc > 0) { |
| 221 | if (strcmp(*argv, "from") == 0) { |
| 222 | inet_prefix dst; |
| 223 | NEXT_ARG(); |
| 224 | get_prefix(&dst, *argv, req.r.rtm_family); |
| 225 | req.r.rtm_src_len = dst.bitlen; |
| 226 | addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen); |
| 227 | } else if (strcmp(*argv, "to") == 0) { |
| 228 | inet_prefix dst; |
| 229 | NEXT_ARG(); |
| 230 | get_prefix(&dst, *argv, req.r.rtm_family); |
| 231 | req.r.rtm_dst_len = dst.bitlen; |
| 232 | addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); |
| 233 | } else if (matches(*argv, "preference") == 0 || |
| 234 | matches(*argv, "order") == 0 || |
| 235 | matches(*argv, "priority") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 236 | uint32_t pref; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 237 | NEXT_ARG(); |
| 238 | if (get_u32(&pref, *argv, 0)) |
| 239 | invarg("preference value", *argv); |
| 240 | addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref); |
| 241 | } else if (strcmp(*argv, "tos") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 242 | uint32_t tos; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 243 | NEXT_ARG(); |
| 244 | if (rtnl_dsfield_a2n(&tos, *argv)) |
| 245 | invarg("TOS value", *argv); |
| 246 | req.r.rtm_tos = tos; |
| 247 | } else if (strcmp(*argv, "fwmark") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 248 | uint32_t fwmark; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 249 | NEXT_ARG(); |
| 250 | if (get_u32(&fwmark, *argv, 0)) |
| 251 | invarg("fwmark value", *argv); |
| 252 | addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark); |
| 253 | } else if (matches(*argv, "realms") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 254 | uint32_t realm; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 255 | NEXT_ARG(); |
| 256 | if (get_rt_realms(&realm, *argv)) |
| 257 | invarg("realms", *argv); |
| 258 | addattr32(&req.n, sizeof(req), RTA_FLOW, realm); |
| 259 | } else if (matches(*argv, "table") == 0 || |
| 260 | strcmp(*argv, "lookup") == 0) { |
Bernhard Reutner-Fischer | 4d9a358 | 2007-01-28 00:20:46 +0000 | [diff] [blame^] | 261 | uint32_t tid; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 262 | NEXT_ARG(); |
| 263 | if (rtnl_rttable_a2n(&tid, *argv)) |
| 264 | invarg("table ID", *argv); |
| 265 | req.r.rtm_table = tid; |
| 266 | table_ok = 1; |
| 267 | } else if (strcmp(*argv, "dev") == 0 || |
| 268 | strcmp(*argv, "iif") == 0) { |
| 269 | NEXT_ARG(); |
| 270 | addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1); |
| 271 | } else if (strcmp(*argv, "nat") == 0 || |
| 272 | matches(*argv, "map-to") == 0) { |
| 273 | NEXT_ARG(); |
| 274 | addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv)); |
| 275 | req.r.rtm_type = RTN_NAT; |
| 276 | } else { |
| 277 | int type; |
| 278 | |
| 279 | if (strcmp(*argv, "type") == 0) { |
| 280 | NEXT_ARG(); |
| 281 | } |
| 282 | if (matches(*argv, "help") == 0) |
| 283 | bb_show_usage(); |
| 284 | if (rtnl_rtntype_a2n(&type, *argv)) |
| 285 | invarg("Failed to parse rule type", *argv); |
| 286 | req.r.rtm_type = type; |
| 287 | } |
| 288 | argc--; |
| 289 | argv++; |
| 290 | } |
| 291 | |
| 292 | if (req.r.rtm_family == AF_UNSPEC) |
| 293 | req.r.rtm_family = AF_INET; |
| 294 | |
| 295 | if (!table_ok && cmd == RTM_NEWRULE) |
| 296 | req.r.rtm_table = RT_TABLE_MAIN; |
| 297 | |
| 298 | if (rtnl_open(&rth, 0) < 0) |
| 299 | return 1; |
| 300 | |
| 301 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 302 | return 2; |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | int do_iprule(int argc, char **argv) |
| 308 | { |
Bernhard Reutner-Fischer | 4d9a358 | 2007-01-28 00:20:46 +0000 | [diff] [blame^] | 309 | const char * const ip_rule_commands[] = |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 310 | {"add", "delete", "list", "show", 0}; |
Bernhard Reutner-Fischer | 4d9a358 | 2007-01-28 00:20:46 +0000 | [diff] [blame^] | 311 | int cmd = 2; /* list */ |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 312 | |
| 313 | if (argc < 1) |
| 314 | return iprule_list(0, NULL); |
| 315 | if (*argv) |
Bernhard Reutner-Fischer | 4d9a358 | 2007-01-28 00:20:46 +0000 | [diff] [blame^] | 316 | cmd = index_in_substr_array(ip_rule_commands, *argv); |
| 317 | |
| 318 | switch (cmd) { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 319 | case 0: /* add */ |
| 320 | cmd = RTM_NEWRULE; |
| 321 | break; |
| 322 | case 1: /* delete */ |
| 323 | cmd = RTM_DELRULE; |
| 324 | break; |
| 325 | case 2: /* list */ |
| 326 | case 3: /* show */ |
| 327 | return iprule_list(argc-1, argv+1); |
| 328 | break; |
| 329 | default: |
| 330 | bb_error_msg_and_die("unknown command %s", *argv); |
| 331 | } |
| 332 | return iprule_modify(cmd, argc-1, argv+1); |
| 333 | } |
| 334 | |