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 |
| 16 | */ |
| 17 | |
| 18 | #include "libbb.h" |
| 19 | #include <syslog.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <netinet/in.h> |
| 22 | #include <netinet/ip.h> |
| 23 | #include <arpa/inet.h> |
| 24 | |
| 25 | #include "rt_names.h" |
| 26 | #include "utils.h" |
Bernhard Reutner-Fischer | 620e57b | 2007-01-22 17:42:37 +0000 | [diff] [blame] | 27 | #include "ip_common.h" |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 28 | /* |
| 29 | static void usage(void) __attribute__((noreturn)); |
| 30 | |
| 31 | static void usage(void) |
| 32 | { |
| 33 | fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n"); |
| 34 | fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n"); |
| 35 | fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n"); |
| 36 | fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n"); |
| 37 | fprintf(stderr, " [ prohibit | reject | unreachable ]\n"); |
| 38 | fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n"); |
| 39 | fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n"); |
| 40 | exit(-1); |
| 41 | } |
| 42 | */ |
| 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 { |
| 88 | fprintf(fp, "%s", format_host(r->rtm_family, |
| 89 | RTA_PAYLOAD(tb[RTA_SRC]), |
| 90 | RTA_DATA(tb[RTA_SRC]), |
| 91 | abuf, sizeof(abuf)) |
| 92 | ); |
| 93 | } |
| 94 | } else if (r->rtm_src_len) { |
| 95 | fprintf(fp, "0/%d", r->rtm_src_len); |
| 96 | } else { |
| 97 | fprintf(fp, "all"); |
| 98 | } |
| 99 | fprintf(fp, " "); |
| 100 | |
| 101 | if (tb[RTA_DST]) { |
| 102 | if (r->rtm_dst_len != host_len) { |
| 103 | fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family, |
| 104 | RTA_PAYLOAD(tb[RTA_DST]), |
| 105 | RTA_DATA(tb[RTA_DST]), |
| 106 | abuf, sizeof(abuf)), |
| 107 | r->rtm_dst_len |
| 108 | ); |
| 109 | } else { |
| 110 | fprintf(fp, "to %s ", format_host(r->rtm_family, |
| 111 | RTA_PAYLOAD(tb[RTA_DST]), |
| 112 | RTA_DATA(tb[RTA_DST]), |
| 113 | abuf, sizeof(abuf))); |
| 114 | } |
| 115 | } else if (r->rtm_dst_len) { |
| 116 | fprintf(fp, "to 0/%d ", r->rtm_dst_len); |
| 117 | } |
| 118 | |
| 119 | if (r->rtm_tos) { |
| 120 | fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1))); |
| 121 | } |
| 122 | if (tb[RTA_PROTOINFO]) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 123 | fprintf(fp, "fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO])); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (tb[RTA_IIF]) { |
| 127 | fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF])); |
| 128 | } |
| 129 | |
| 130 | if (r->rtm_table) |
| 131 | fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1))); |
| 132 | |
| 133 | if (tb[RTA_FLOW]) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 134 | uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]); |
| 135 | uint32_t from = to>>16; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 136 | to &= 0xFFFF; |
| 137 | if (from) { |
| 138 | fprintf(fp, "realms %s/", |
| 139 | rtnl_rtrealm_n2a(from, b1, sizeof(b1))); |
| 140 | } |
| 141 | fprintf(fp, "%s ", |
| 142 | rtnl_rtrealm_n2a(to, b1, sizeof(b1))); |
| 143 | } |
| 144 | |
| 145 | if (r->rtm_type == RTN_NAT) { |
| 146 | if (tb[RTA_GATEWAY]) { |
| 147 | fprintf(fp, "map-to %s ", |
| 148 | format_host(r->rtm_family, |
| 149 | RTA_PAYLOAD(tb[RTA_GATEWAY]), |
| 150 | RTA_DATA(tb[RTA_GATEWAY]), |
| 151 | abuf, sizeof(abuf))); |
| 152 | } else |
| 153 | fprintf(fp, "masquerade"); |
| 154 | } else if (r->rtm_type != RTN_UNICAST) |
| 155 | fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1))); |
| 156 | |
| 157 | fprintf(fp, "\n"); |
| 158 | fflush(fp); |
| 159 | return 0; |
| 160 | } |
| 161 | |
Bernhard Reutner-Fischer | 620e57b | 2007-01-22 17:42:37 +0000 | [diff] [blame] | 162 | static int iprule_list(int argc, 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 | |
| 170 | if (argc > 0) { |
| 171 | bb_error_msg("\"rule show\" needs no arguments"); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | if (rtnl_open(&rth, 0) < 0) |
| 176 | return 1; |
| 177 | |
| 178 | if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { |
| 179 | bb_perror_msg("Cannot send dump request"); |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) { |
| 184 | bb_error_msg("Dump terminated"); |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | |
Bernhard Reutner-Fischer | 620e57b | 2007-01-22 17:42:37 +0000 | [diff] [blame] | 192 | static int iprule_modify(int cmd, int argc, char **argv) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 193 | { |
| 194 | int table_ok = 0; |
| 195 | struct rtnl_handle rth; |
| 196 | struct { |
| 197 | struct nlmsghdr n; |
| 198 | struct rtmsg r; |
| 199 | char buf[1024]; |
| 200 | } req; |
| 201 | |
| 202 | memset(&req, 0, sizeof(req)); |
| 203 | |
| 204 | req.n.nlmsg_type = cmd; |
| 205 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 206 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 207 | req.r.rtm_family = preferred_family; |
| 208 | req.r.rtm_protocol = RTPROT_BOOT; |
| 209 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
| 210 | req.r.rtm_table = 0; |
| 211 | req.r.rtm_type = RTN_UNSPEC; |
| 212 | |
| 213 | if (cmd == RTM_NEWRULE) { |
| 214 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; |
| 215 | req.r.rtm_type = RTN_UNICAST; |
| 216 | } |
| 217 | |
| 218 | while (argc > 0) { |
| 219 | if (strcmp(*argv, "from") == 0) { |
| 220 | inet_prefix dst; |
| 221 | NEXT_ARG(); |
| 222 | get_prefix(&dst, *argv, req.r.rtm_family); |
| 223 | req.r.rtm_src_len = dst.bitlen; |
| 224 | addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen); |
| 225 | } else if (strcmp(*argv, "to") == 0) { |
| 226 | inet_prefix dst; |
| 227 | NEXT_ARG(); |
| 228 | get_prefix(&dst, *argv, req.r.rtm_family); |
| 229 | req.r.rtm_dst_len = dst.bitlen; |
| 230 | addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); |
| 231 | } else if (matches(*argv, "preference") == 0 || |
| 232 | matches(*argv, "order") == 0 || |
| 233 | matches(*argv, "priority") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 234 | uint32_t pref; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 235 | NEXT_ARG(); |
| 236 | if (get_u32(&pref, *argv, 0)) |
| 237 | invarg("preference value", *argv); |
| 238 | addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref); |
| 239 | } else if (strcmp(*argv, "tos") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 240 | uint32_t tos; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 241 | NEXT_ARG(); |
| 242 | if (rtnl_dsfield_a2n(&tos, *argv)) |
| 243 | invarg("TOS value", *argv); |
| 244 | req.r.rtm_tos = tos; |
| 245 | } else if (strcmp(*argv, "fwmark") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 246 | uint32_t fwmark; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 247 | NEXT_ARG(); |
| 248 | if (get_u32(&fwmark, *argv, 0)) |
| 249 | invarg("fwmark value", *argv); |
| 250 | addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark); |
| 251 | } else if (matches(*argv, "realms") == 0) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 252 | uint32_t realm; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 253 | NEXT_ARG(); |
| 254 | if (get_rt_realms(&realm, *argv)) |
| 255 | invarg("realms", *argv); |
| 256 | addattr32(&req.n, sizeof(req), RTA_FLOW, realm); |
| 257 | } else if (matches(*argv, "table") == 0 || |
| 258 | strcmp(*argv, "lookup") == 0) { |
Bernhard Reutner-Fischer | 4259972 | 2007-01-23 01:27:17 +0000 | [diff] [blame^] | 259 | unsigned int tid; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 260 | NEXT_ARG(); |
| 261 | if (rtnl_rttable_a2n(&tid, *argv)) |
| 262 | invarg("table ID", *argv); |
| 263 | req.r.rtm_table = tid; |
| 264 | table_ok = 1; |
| 265 | } else if (strcmp(*argv, "dev") == 0 || |
| 266 | strcmp(*argv, "iif") == 0) { |
| 267 | NEXT_ARG(); |
| 268 | addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1); |
| 269 | } else if (strcmp(*argv, "nat") == 0 || |
| 270 | matches(*argv, "map-to") == 0) { |
| 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 | |
| 277 | if (strcmp(*argv, "type") == 0) { |
| 278 | NEXT_ARG(); |
| 279 | } |
| 280 | if (matches(*argv, "help") == 0) |
| 281 | bb_show_usage(); |
| 282 | if (rtnl_rtntype_a2n(&type, *argv)) |
| 283 | invarg("Failed to parse rule type", *argv); |
| 284 | req.r.rtm_type = type; |
| 285 | } |
| 286 | argc--; |
| 287 | argv++; |
| 288 | } |
| 289 | |
| 290 | if (req.r.rtm_family == AF_UNSPEC) |
| 291 | req.r.rtm_family = AF_INET; |
| 292 | |
| 293 | if (!table_ok && cmd == RTM_NEWRULE) |
| 294 | req.r.rtm_table = RT_TABLE_MAIN; |
| 295 | |
| 296 | if (rtnl_open(&rth, 0) < 0) |
| 297 | return 1; |
| 298 | |
| 299 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 300 | return 2; |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | int do_iprule(int argc, char **argv) |
| 306 | { |
| 307 | static const char * const ip_rule_commands[] = |
| 308 | {"add", "delete", "list", "show", 0}; |
| 309 | int command_num = 2; |
| 310 | int cmd; |
| 311 | |
| 312 | if (argc < 1) |
| 313 | return iprule_list(0, NULL); |
| 314 | if (*argv) |
| 315 | command_num = index_in_substr_array(ip_rule_commands, *argv); |
| 316 | switch (command_num) { |
| 317 | case 0: /* add */ |
| 318 | cmd = RTM_NEWRULE; |
| 319 | break; |
| 320 | case 1: /* delete */ |
| 321 | cmd = RTM_DELRULE; |
| 322 | break; |
| 323 | case 2: /* list */ |
| 324 | case 3: /* show */ |
| 325 | return iprule_list(argc-1, argv+1); |
| 326 | break; |
| 327 | default: |
| 328 | bb_error_msg_and_die("unknown command %s", *argv); |
| 329 | } |
| 330 | return iprule_modify(cmd, argc-1, argv+1); |
| 331 | } |
| 332 | |