osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * iprule.c "ip rule". |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <syslog.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <netinet/in.h> |
| 20 | #include <netinet/ip.h> |
| 21 | #include <arpa/inet.h> |
| 22 | #include <string.h> |
Thomas Graf | 3123a0c | 2006-11-09 12:38:02 +0100 | [diff] [blame] | 23 | #include <linux/fib_rules.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 24 | |
| 25 | #include "rt_names.h" |
| 26 | #include "utils.h" |
Patrick McHardy | 34e9564 | 2006-08-11 00:14:51 +0200 | [diff] [blame] | 27 | #include "ip_common.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 28 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 29 | extern struct rtnl_handle rth; |
| 30 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 31 | static void usage(void) __attribute__((noreturn)); |
| 32 | |
| 33 | static void usage(void) |
| 34 | { |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 35 | fprintf(stderr, "Usage: ip rule [ list | add | del | flush ] SELECTOR ACTION\n"); |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 36 | fprintf(stderr, "SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]\n"); |
Patrick McHardy | 85eae22 | 2009-12-03 20:07:14 +0000 | [diff] [blame] | 37 | fprintf(stderr, " [ iif STRING ] [ oif STRING ] [ pref NUMBER ]\n"); |
net[shemminger]!shemminger | 526afe4 | 2005-01-17 23:27:23 +0000 | [diff] [blame] | 38 | fprintf(stderr, "ACTION := [ table TABLE_ID ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 39 | fprintf(stderr, " [ prohibit | reject | unreachable ]\n"); |
| 40 | fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n"); |
Thomas Graf | 6b469ca | 2007-03-27 02:04:10 +0200 | [diff] [blame] | 41 | fprintf(stderr, " [ goto NUMBER ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 42 | fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n"); |
| 43 | exit(-1); |
| 44 | } |
| 45 | |
Thomas Graf | 98bde98 | 2006-11-10 12:27:10 +0100 | [diff] [blame] | 46 | int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 47 | { |
| 48 | FILE *fp = (FILE*)arg; |
| 49 | struct rtmsg *r = NLMSG_DATA(n); |
| 50 | int len = n->nlmsg_len; |
| 51 | int host_len = -1; |
Patrick McHardy | 34e9564 | 2006-08-11 00:14:51 +0200 | [diff] [blame] | 52 | __u32 table; |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 53 | struct rtattr * tb[FRA_MAX+1]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 54 | char abuf[256]; |
| 55 | SPRINT_BUF(b1); |
| 56 | |
Thomas Graf | 98bde98 | 2006-11-10 12:27:10 +0100 | [diff] [blame] | 57 | if (n->nlmsg_type != RTM_NEWRULE && n->nlmsg_type != RTM_DELRULE) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | |
| 60 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 61 | if (len < 0) |
| 62 | return -1; |
| 63 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 64 | parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
Thomas Graf | 98bde98 | 2006-11-10 12:27:10 +0100 | [diff] [blame] | 75 | if (n->nlmsg_type == RTM_DELRULE) |
| 76 | fprintf(fp, "Deleted "); |
| 77 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 78 | if (tb[FRA_PRIORITY]) |
| 79 | fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[FRA_PRIORITY])); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 80 | else |
| 81 | fprintf(fp, "0:\t"); |
| 82 | |
Thomas Graf | 3123a0c | 2006-11-09 12:38:02 +0100 | [diff] [blame] | 83 | if (r->rtm_flags & FIB_RULE_INVERT) |
| 84 | fprintf(fp, "not "); |
| 85 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 86 | if (tb[FRA_SRC]) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 87 | if (r->rtm_src_len != host_len) { |
| 88 | fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family, |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 89 | RTA_PAYLOAD(tb[FRA_SRC]), |
| 90 | RTA_DATA(tb[FRA_SRC]), |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 91 | abuf, sizeof(abuf)), |
| 92 | r->rtm_src_len |
| 93 | ); |
| 94 | } else { |
| 95 | fprintf(fp, "from %s ", format_host(r->rtm_family, |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 96 | RTA_PAYLOAD(tb[FRA_SRC]), |
| 97 | RTA_DATA(tb[FRA_SRC]), |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 98 | abuf, sizeof(abuf)) |
| 99 | ); |
| 100 | } |
| 101 | } else if (r->rtm_src_len) { |
| 102 | fprintf(fp, "from 0/%d ", r->rtm_src_len); |
| 103 | } else { |
| 104 | fprintf(fp, "from all "); |
| 105 | } |
| 106 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 107 | if (tb[FRA_DST]) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 108 | if (r->rtm_dst_len != host_len) { |
| 109 | fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family, |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 110 | RTA_PAYLOAD(tb[FRA_DST]), |
| 111 | RTA_DATA(tb[FRA_DST]), |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 112 | abuf, sizeof(abuf)), |
| 113 | r->rtm_dst_len |
| 114 | ); |
| 115 | } else { |
| 116 | fprintf(fp, "to %s ", format_host(r->rtm_family, |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 117 | RTA_PAYLOAD(tb[FRA_DST]), |
| 118 | RTA_DATA(tb[FRA_DST]), |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 119 | abuf, sizeof(abuf))); |
| 120 | } |
| 121 | } else if (r->rtm_dst_len) { |
| 122 | fprintf(fp, "to 0/%d ", r->rtm_dst_len); |
| 123 | } |
| 124 | |
| 125 | if (r->rtm_tos) { |
| 126 | SPRINT_BUF(b1); |
| 127 | fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1))); |
| 128 | } |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 129 | |
| 130 | if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) { |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 131 | __u32 mark = 0, mask = 0; |
| 132 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 133 | if (tb[FRA_FWMARK]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame^] | 134 | mark = rta_getattr_u32(tb[FRA_FWMARK]); |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 135 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 136 | if (tb[FRA_FWMASK] && |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame^] | 137 | (mask = rta_getattr_u32(tb[FRA_FWMASK])) != 0xFFFFFFFF) |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 138 | fprintf(fp, "fwmark 0x%x/0x%x ", mark, mask); |
Stephen Hemminger | 81c6179 | 2006-12-13 17:05:50 -0800 | [diff] [blame] | 139 | else |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 140 | fprintf(fp, "fwmark 0x%x ", mark); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 143 | if (tb[FRA_IFNAME]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame^] | 144 | fprintf(fp, "iif %s ", rta_getattr_str(tb[FRA_IFNAME])); |
Patrick McHardy | 85eae22 | 2009-12-03 20:07:14 +0000 | [diff] [blame] | 145 | if (r->rtm_flags & FIB_RULE_IIF_DETACHED) |
| 146 | fprintf(fp, "[detached] "); |
| 147 | } |
| 148 | |
| 149 | if (tb[FRA_OIFNAME]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame^] | 150 | fprintf(fp, "oif %s ", rta_getattr_str(tb[FRA_OIFNAME])); |
Patrick McHardy | 85eae22 | 2009-12-03 20:07:14 +0000 | [diff] [blame] | 151 | if (r->rtm_flags & FIB_RULE_OIF_DETACHED) |
Thomas Graf | 6b469ca | 2007-03-27 02:04:10 +0200 | [diff] [blame] | 152 | fprintf(fp, "[detached] "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Patrick McHardy | 34e9564 | 2006-08-11 00:14:51 +0200 | [diff] [blame] | 155 | table = rtm_get_table(r, tb); |
| 156 | if (table) |
| 157 | fprintf(fp, "lookup %s ", rtnl_rttable_n2a(table, b1, sizeof(b1))); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 158 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 159 | if (tb[FRA_FLOW]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame^] | 160 | __u32 to = rta_getattr_u32(tb[FRA_FLOW]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 161 | __u32 from = to>>16; |
| 162 | to &= 0xFFFF; |
| 163 | if (from) { |
| 164 | fprintf(fp, "realms %s/", |
| 165 | rtnl_rtrealm_n2a(from, b1, sizeof(b1))); |
| 166 | } |
| 167 | fprintf(fp, "%s ", |
| 168 | rtnl_rtrealm_n2a(to, b1, sizeof(b1))); |
| 169 | } |
| 170 | |
| 171 | if (r->rtm_type == RTN_NAT) { |
| 172 | if (tb[RTA_GATEWAY]) { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 173 | fprintf(fp, "map-to %s ", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 174 | format_host(r->rtm_family, |
| 175 | RTA_PAYLOAD(tb[RTA_GATEWAY]), |
| 176 | RTA_DATA(tb[RTA_GATEWAY]), |
| 177 | abuf, sizeof(abuf))); |
| 178 | } else |
| 179 | fprintf(fp, "masquerade"); |
Thomas Graf | 6b469ca | 2007-03-27 02:04:10 +0200 | [diff] [blame] | 180 | } else if (r->rtm_type == FR_ACT_GOTO) { |
| 181 | fprintf(fp, "goto "); |
| 182 | if (tb[FRA_GOTO]) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame^] | 183 | fprintf(fp, "%u", rta_getattr_u32(tb[FRA_GOTO])); |
Thomas Graf | 6b469ca | 2007-03-27 02:04:10 +0200 | [diff] [blame] | 184 | else |
| 185 | fprintf(fp, "none"); |
| 186 | if (r->rtm_flags & FIB_RULE_UNRESOLVED) |
| 187 | fprintf(fp, " [unresolved]"); |
| 188 | } else if (r->rtm_type == FR_ACT_NOP) |
| 189 | fprintf(fp, "nop"); |
| 190 | else if (r->rtm_type != RTN_UNICAST) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 191 | fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1))); |
| 192 | |
| 193 | fprintf(fp, "\n"); |
| 194 | fflush(fp); |
| 195 | return 0; |
| 196 | } |
| 197 | |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 198 | static int iprule_list(int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 199 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 200 | int af = preferred_family; |
| 201 | |
| 202 | if (af == AF_UNSPEC) |
| 203 | af = AF_INET; |
| 204 | |
| 205 | if (argc > 0) { |
net[shemminger]!shemminger | 0284dcd | 2004-10-07 18:56:07 +0000 | [diff] [blame] | 206 | fprintf(stderr, "\"ip rule show\" does not take any arguments.\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 210 | if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { |
| 211 | perror("Cannot send dump request"); |
| 212 | return 1; |
| 213 | } |
| 214 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 215 | if (rtnl_dump_filter(&rth, print_rule, stdout) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 216 | fprintf(stderr, "Dump terminated\n"); |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 224 | static int iprule_modify(int cmd, int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 225 | { |
| 226 | int table_ok = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 227 | struct { |
| 228 | struct nlmsghdr n; |
| 229 | struct rtmsg r; |
| 230 | char buf[1024]; |
| 231 | } req; |
| 232 | |
| 233 | memset(&req, 0, sizeof(req)); |
| 234 | |
| 235 | req.n.nlmsg_type = cmd; |
| 236 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 237 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 238 | req.r.rtm_family = preferred_family; |
| 239 | req.r.rtm_protocol = RTPROT_BOOT; |
| 240 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
| 241 | req.r.rtm_table = 0; |
| 242 | req.r.rtm_type = RTN_UNSPEC; |
Thomas Graf | 3123a0c | 2006-11-09 12:38:02 +0100 | [diff] [blame] | 243 | req.r.rtm_flags = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 244 | |
| 245 | if (cmd == RTM_NEWRULE) { |
| 246 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; |
| 247 | req.r.rtm_type = RTN_UNICAST; |
| 248 | } |
| 249 | |
| 250 | while (argc > 0) { |
Thomas Graf | 3123a0c | 2006-11-09 12:38:02 +0100 | [diff] [blame] | 251 | if (strcmp(*argv, "not") == 0) { |
| 252 | req.r.rtm_flags |= FIB_RULE_INVERT; |
| 253 | } else if (strcmp(*argv, "from") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 254 | inet_prefix dst; |
| 255 | NEXT_ARG(); |
| 256 | get_prefix(&dst, *argv, req.r.rtm_family); |
| 257 | req.r.rtm_src_len = dst.bitlen; |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 258 | addattr_l(&req.n, sizeof(req), FRA_SRC, &dst.data, dst.bytelen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 259 | } else if (strcmp(*argv, "to") == 0) { |
| 260 | inet_prefix dst; |
| 261 | NEXT_ARG(); |
| 262 | get_prefix(&dst, *argv, req.r.rtm_family); |
| 263 | req.r.rtm_dst_len = dst.bitlen; |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 264 | addattr_l(&req.n, sizeof(req), FRA_DST, &dst.data, dst.bytelen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 265 | } else if (matches(*argv, "preference") == 0 || |
| 266 | matches(*argv, "order") == 0 || |
| 267 | matches(*argv, "priority") == 0) { |
| 268 | __u32 pref; |
| 269 | NEXT_ARG(); |
| 270 | if (get_u32(&pref, *argv, 0)) |
| 271 | invarg("preference value is invalid\n", *argv); |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 272 | addattr32(&req.n, sizeof(req), FRA_PRIORITY, pref); |
Arnd Hannemann | dec0160 | 2010-05-21 04:10:09 +0000 | [diff] [blame] | 273 | } else if (strcmp(*argv, "tos") == 0 || |
| 274 | matches(*argv, "dsfield") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 275 | __u32 tos; |
| 276 | NEXT_ARG(); |
| 277 | if (rtnl_dsfield_a2n(&tos, *argv)) |
| 278 | invarg("TOS value is invalid\n", *argv); |
| 279 | req.r.rtm_tos = tos; |
| 280 | } else if (strcmp(*argv, "fwmark") == 0) { |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 281 | char *slash; |
| 282 | __u32 fwmark, fwmask; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 283 | NEXT_ARG(); |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 284 | if ((slash = strchr(*argv, '/')) != NULL) |
| 285 | *slash = '\0'; |
org[shemminger]!shemminger | 4fb466f | 2004-06-07 22:02:48 +0000 | [diff] [blame] | 286 | if (get_u32(&fwmark, *argv, 0)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 287 | invarg("fwmark value is invalid\n", *argv); |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 288 | addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark); |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 289 | if (slash) { |
| 290 | if (get_u32(&fwmask, slash+1, 0)) |
| 291 | invarg("fwmask value is invalid\n", slash+1); |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 292 | addattr32(&req.n, sizeof(req), FRA_FWMASK, fwmask); |
Patrick McHardy | be7f286 | 2006-12-13 16:58:32 -0800 | [diff] [blame] | 293 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 294 | } else if (matches(*argv, "realms") == 0) { |
| 295 | __u32 realm; |
| 296 | NEXT_ARG(); |
| 297 | if (get_rt_realms(&realm, *argv)) |
| 298 | invarg("invalid realms\n", *argv); |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 299 | addattr32(&req.n, sizeof(req), FRA_FLOW, realm); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 300 | } else if (matches(*argv, "table") == 0 || |
| 301 | strcmp(*argv, "lookup") == 0) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 302 | __u32 tid; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 303 | NEXT_ARG(); |
| 304 | if (rtnl_rttable_a2n(&tid, *argv)) |
| 305 | invarg("invalid table ID\n", *argv); |
Patrick McHardy | 34e9564 | 2006-08-11 00:14:51 +0200 | [diff] [blame] | 306 | if (tid < 256) |
| 307 | req.r.rtm_table = tid; |
| 308 | else { |
| 309 | req.r.rtm_table = RT_TABLE_UNSPEC; |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 310 | addattr32(&req.n, sizeof(req), FRA_TABLE, tid); |
Patrick McHardy | 34e9564 | 2006-08-11 00:14:51 +0200 | [diff] [blame] | 311 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 312 | table_ok = 1; |
| 313 | } else if (strcmp(*argv, "dev") == 0 || |
| 314 | strcmp(*argv, "iif") == 0) { |
| 315 | NEXT_ARG(); |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 316 | addattr_l(&req.n, sizeof(req), FRA_IFNAME, *argv, strlen(*argv)+1); |
Patrick McHardy | 85eae22 | 2009-12-03 20:07:14 +0000 | [diff] [blame] | 317 | } else if (strcmp(*argv, "oif") == 0) { |
| 318 | NEXT_ARG(); |
| 319 | addattr_l(&req.n, sizeof(req), FRA_OIFNAME, *argv, strlen(*argv)+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 320 | } else if (strcmp(*argv, "nat") == 0 || |
| 321 | matches(*argv, "map-to") == 0) { |
| 322 | NEXT_ARG(); |
net[shemminger]!shemminger | 526afe4 | 2005-01-17 23:27:23 +0000 | [diff] [blame] | 323 | fprintf(stderr, "Warning: route NAT is deprecated\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 324 | addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv)); |
| 325 | req.r.rtm_type = RTN_NAT; |
| 326 | } else { |
| 327 | int type; |
| 328 | |
| 329 | if (strcmp(*argv, "type") == 0) { |
| 330 | NEXT_ARG(); |
| 331 | } |
| 332 | if (matches(*argv, "help") == 0) |
| 333 | usage(); |
Thomas Graf | 6b469ca | 2007-03-27 02:04:10 +0200 | [diff] [blame] | 334 | else if (matches(*argv, "goto") == 0) { |
| 335 | __u32 target; |
| 336 | type = FR_ACT_GOTO; |
| 337 | NEXT_ARG(); |
| 338 | if (get_u32(&target, *argv, 0)) |
| 339 | invarg("invalid target\n", *argv); |
| 340 | addattr32(&req.n, sizeof(req), FRA_GOTO, target); |
| 341 | } else if (matches(*argv, "nop") == 0) |
| 342 | type = FR_ACT_NOP; |
| 343 | else if (rtnl_rtntype_a2n(&type, *argv)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 344 | invarg("Failed to parse rule type", *argv); |
| 345 | req.r.rtm_type = type; |
Thomas Graf | 6b469ca | 2007-03-27 02:04:10 +0200 | [diff] [blame] | 346 | table_ok = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 347 | } |
| 348 | argc--; |
| 349 | argv++; |
| 350 | } |
| 351 | |
| 352 | if (req.r.rtm_family == AF_UNSPEC) |
| 353 | req.r.rtm_family = AF_INET; |
| 354 | |
| 355 | if (!table_ok && cmd == RTM_NEWRULE) |
| 356 | req.r.rtm_table = RT_TABLE_MAIN; |
| 357 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 358 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 359 | return 2; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 364 | |
| 365 | static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
| 366 | { |
shemminger | b77caeb | 2005-10-12 22:32:34 +0000 | [diff] [blame] | 367 | struct rtnl_handle rth2; |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 368 | struct rtmsg *r = NLMSG_DATA(n); |
| 369 | int len = n->nlmsg_len; |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 370 | struct rtattr * tb[FRA_MAX+1]; |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 371 | |
| 372 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 373 | if (len < 0) |
| 374 | return -1; |
| 375 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 376 | parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len); |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 377 | |
Patrick McHardy | ad1a12d | 2007-08-22 10:26:12 -0700 | [diff] [blame] | 378 | if (tb[FRA_PRIORITY]) { |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 379 | n->nlmsg_type = RTM_DELRULE; |
| 380 | n->nlmsg_flags = NLM_F_REQUEST; |
| 381 | |
shemminger | b77caeb | 2005-10-12 22:32:34 +0000 | [diff] [blame] | 382 | if (rtnl_open(&rth2, 0) < 0) |
| 383 | return -1; |
| 384 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 385 | if (rtnl_talk(&rth2, n, 0, 0, NULL) < 0) |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 386 | return -2; |
shemminger | b77caeb | 2005-10-12 22:32:34 +0000 | [diff] [blame] | 387 | |
| 388 | rtnl_close(&rth2); |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int iprule_flush(int argc, char **argv) |
| 395 | { |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 396 | int af = preferred_family; |
| 397 | |
| 398 | if (af == AF_UNSPEC) |
| 399 | af = AF_INET; |
| 400 | |
| 401 | if (argc > 0) { |
shemminger | 6608184 | 2005-08-16 21:10:51 +0000 | [diff] [blame] | 402 | fprintf(stderr, "\"ip rule flush\" does not allow arguments\n"); |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 403 | return -1; |
| 404 | } |
| 405 | |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 406 | if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { |
| 407 | perror("Cannot send dump request"); |
| 408 | return 1; |
| 409 | } |
| 410 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 411 | if (rtnl_dump_filter(&rth, flush_rule, NULL) < 0) { |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 412 | fprintf(stderr, "Flush terminated\n"); |
| 413 | return 1; |
| 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 419 | int do_iprule(int argc, char **argv) |
| 420 | { |
| 421 | if (argc < 1) { |
| 422 | return iprule_list(0, NULL); |
| 423 | } else if (matches(argv[0], "list") == 0 || |
| 424 | matches(argv[0], "lst") == 0 || |
| 425 | matches(argv[0], "show") == 0) { |
| 426 | return iprule_list(argc-1, argv+1); |
| 427 | } else if (matches(argv[0], "add") == 0) { |
| 428 | return iprule_modify(RTM_NEWRULE, argc-1, argv+1); |
| 429 | } else if (matches(argv[0], "delete") == 0) { |
| 430 | return iprule_modify(RTM_DELRULE, argc-1, argv+1); |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 431 | } else if (matches(argv[0], "flush") == 0) { |
| 432 | return iprule_flush(argc-1, argv+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 433 | } else if (matches(argv[0], "help") == 0) |
| 434 | usage(); |
| 435 | |
| 436 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv); |
| 437 | exit(-1); |
| 438 | } |
| 439 | |
Patrick McHardy | b6c8e80 | 2010-06-09 17:41:10 +0200 | [diff] [blame] | 440 | int do_multirule(int argc, char **argv) |
| 441 | { |
| 442 | switch (preferred_family) { |
| 443 | case AF_UNSPEC: |
| 444 | case AF_INET: |
| 445 | preferred_family = RTNL_FAMILY_IPMR; |
| 446 | break; |
| 447 | case AF_INET6: |
| 448 | preferred_family = RTNL_FAMILY_IP6MR; |
| 449 | break; |
Ben Greear | 0d1c9b5 | 2010-07-15 15:48:10 -0700 | [diff] [blame] | 450 | case RTNL_FAMILY_IPMR: |
| 451 | case RTNL_FAMILY_IP6MR: |
| 452 | break; |
Patrick McHardy | b6c8e80 | 2010-06-09 17:41:10 +0200 | [diff] [blame] | 453 | default: |
Ben Greear | 0d1c9b5 | 2010-07-15 15:48:10 -0700 | [diff] [blame] | 454 | fprintf(stderr, "Multicast rules are only supported for IPv4/IPv6, was: %i\n", |
| 455 | preferred_family); |
Patrick McHardy | b6c8e80 | 2010-06-09 17:41:10 +0200 | [diff] [blame] | 456 | exit(-1); |
| 457 | } |
| 458 | |
| 459 | return do_iprule(argc, argv); |
| 460 | } |