Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules |
| 3 | * |
| 4 | * Copyright (C)2003-2006 Helsinki University of Technology |
| 5 | * Copyright (C)2003-2006 USAGI/WIDE Project |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation, version 2. |
| 10 | * |
| 11 | * Authors |
| 12 | * Thomas Graf <tgraf@suug.ch> |
| 13 | * Ville Nuorvala <vnuorval@tcs.hut.fi> |
| 14 | */ |
| 15 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 16 | #include <linux/netdevice.h> |
| 17 | |
| 18 | #include <net/fib_rules.h> |
| 19 | #include <net/ipv6.h> |
| 20 | #include <net/ip6_route.h> |
| 21 | #include <net/netlink.h> |
| 22 | |
| 23 | struct fib6_rule |
| 24 | { |
| 25 | struct fib_rule common; |
| 26 | struct rt6key src; |
| 27 | struct rt6key dst; |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 28 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
YOSHIFUJI Hideaki | 6c5eb6a | 2006-08-25 16:04:29 -0700 | [diff] [blame] | 29 | u32 fwmark; |
YOSHIFUJI Hideaki | cd9d742 | 2006-08-25 16:05:43 -0700 | [diff] [blame] | 30 | u32 fwmask; |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 31 | #endif |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 32 | u8 tclass; |
| 33 | }; |
| 34 | |
| 35 | static struct fib_rules_ops fib6_rules_ops; |
| 36 | |
| 37 | static struct fib6_rule main_rule = { |
| 38 | .common = { |
| 39 | .refcnt = ATOMIC_INIT(2), |
| 40 | .pref = 0x7FFE, |
| 41 | .action = FR_ACT_TO_TBL, |
| 42 | .table = RT6_TABLE_MAIN, |
| 43 | }, |
| 44 | }; |
| 45 | |
| 46 | static struct fib6_rule local_rule = { |
| 47 | .common = { |
| 48 | .refcnt = ATOMIC_INIT(2), |
| 49 | .pref = 0, |
| 50 | .action = FR_ACT_TO_TBL, |
| 51 | .table = RT6_TABLE_LOCAL, |
| 52 | .flags = FIB_RULE_PERMANENT, |
| 53 | }, |
| 54 | }; |
| 55 | |
| 56 | static LIST_HEAD(fib6_rules); |
| 57 | |
| 58 | struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags, |
| 59 | pol_lookup_t lookup) |
| 60 | { |
| 61 | struct fib_lookup_arg arg = { |
| 62 | .lookup_ptr = lookup, |
| 63 | }; |
| 64 | |
| 65 | fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg); |
| 66 | if (arg.rule) |
| 67 | fib_rule_put(arg.rule); |
| 68 | |
Ville Nuorvala | b142955 | 2006-08-08 16:44:17 -0700 | [diff] [blame] | 69 | if (arg.result) |
| 70 | return (struct dst_entry *) arg.result; |
| 71 | |
| 72 | dst_hold(&ip6_null_entry.u.dst); |
| 73 | return &ip6_null_entry.u.dst; |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 76 | static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 77 | int flags, struct fib_lookup_arg *arg) |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 78 | { |
| 79 | struct rt6_info *rt = NULL; |
| 80 | struct fib6_table *table; |
| 81 | pol_lookup_t lookup = arg->lookup_ptr; |
| 82 | |
| 83 | switch (rule->action) { |
| 84 | case FR_ACT_TO_TBL: |
| 85 | break; |
| 86 | case FR_ACT_UNREACHABLE: |
| 87 | rt = &ip6_null_entry; |
| 88 | goto discard_pkt; |
| 89 | default: |
| 90 | case FR_ACT_BLACKHOLE: |
| 91 | rt = &ip6_blk_hole_entry; |
| 92 | goto discard_pkt; |
| 93 | case FR_ACT_PROHIBIT: |
| 94 | rt = &ip6_prohibit_entry; |
| 95 | goto discard_pkt; |
| 96 | } |
| 97 | |
| 98 | table = fib6_get_table(rule->table); |
| 99 | if (table) |
| 100 | rt = lookup(table, flp, flags); |
| 101 | |
| 102 | if (rt != &ip6_null_entry) |
| 103 | goto out; |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 104 | dst_release(&rt->u.dst); |
Patrick McHardy | 3226f688 | 2006-08-06 22:24:08 -0700 | [diff] [blame] | 105 | rt = NULL; |
| 106 | goto out; |
| 107 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 108 | discard_pkt: |
| 109 | dst_hold(&rt->u.dst); |
| 110 | out: |
| 111 | arg->result = rt; |
| 112 | return rt == NULL ? -EAGAIN : 0; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 117 | { |
| 118 | struct fib6_rule *r = (struct fib6_rule *) rule; |
| 119 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame^] | 120 | if (r->dst.plen && |
| 121 | !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen)) |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | |
Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame^] | 124 | if (r->src.plen) { |
| 125 | if (!(flags & RT6_LOOKUP_F_HAS_SADDR) || |
| 126 | !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen)) |
| 127 | return 0; |
| 128 | } |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 129 | |
YOSHIFUJI Hideaki | 2cc67cc | 2006-08-21 19:18:57 +0900 | [diff] [blame] | 130 | if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff)) |
| 131 | return 0; |
| 132 | |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 133 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
Patrick McHardy | 366e4ad | 2006-08-26 16:50:20 -0700 | [diff] [blame] | 134 | if ((r->fwmark ^ fl->fl6_fwmark) & r->fwmask) |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 135 | return 0; |
| 136 | #endif |
| 137 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 138 | return 1; |
| 139 | } |
| 140 | |
YOSHIFUJI Hideaki | 2613aad | 2006-08-25 16:05:00 -0700 | [diff] [blame] | 141 | static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 142 | [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 143 | [FRA_PRIORITY] = { .type = NLA_U32 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 144 | [FRA_SRC] = { .len = sizeof(struct in6_addr) }, |
| 145 | [FRA_DST] = { .len = sizeof(struct in6_addr) }, |
YOSHIFUJI Hideaki | 6c5eb6a | 2006-08-25 16:04:29 -0700 | [diff] [blame] | 146 | [FRA_FWMARK] = { .type = NLA_U32 }, |
YOSHIFUJI Hideaki | cd9d742 | 2006-08-25 16:05:43 -0700 | [diff] [blame] | 147 | [FRA_FWMASK] = { .type = NLA_U32 }, |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 148 | [FRA_TABLE] = { .type = NLA_U32 }, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 152 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, |
| 153 | struct nlattr **tb) |
| 154 | { |
| 155 | int err = -EINVAL; |
| 156 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 157 | |
| 158 | if (frh->src_len > 128 || frh->dst_len > 128 || |
| 159 | (frh->tos & ~IPV6_FLOWINFO_MASK)) |
| 160 | goto errout; |
| 161 | |
| 162 | if (rule->action == FR_ACT_TO_TBL) { |
| 163 | if (rule->table == RT6_TABLE_UNSPEC) |
| 164 | goto errout; |
| 165 | |
| 166 | if (fib6_new_table(rule->table) == NULL) { |
| 167 | err = -ENOBUFS; |
| 168 | goto errout; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (tb[FRA_SRC]) |
| 173 | nla_memcpy(&rule6->src.addr, tb[FRA_SRC], |
| 174 | sizeof(struct in6_addr)); |
| 175 | |
| 176 | if (tb[FRA_DST]) |
| 177 | nla_memcpy(&rule6->dst.addr, tb[FRA_DST], |
| 178 | sizeof(struct in6_addr)); |
| 179 | |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 180 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
YOSHIFUJI Hideaki | cd9d742 | 2006-08-25 16:05:43 -0700 | [diff] [blame] | 181 | if (tb[FRA_FWMARK]) { |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 182 | rule6->fwmark = nla_get_u32(tb[FRA_FWMARK]); |
YOSHIFUJI Hideaki | cd9d742 | 2006-08-25 16:05:43 -0700 | [diff] [blame] | 183 | if (rule6->fwmark) { |
| 184 | /* |
| 185 | * if the mark value is non-zero, |
| 186 | * all bits are compared by default |
| 187 | * unless a mask is explicitly specified. |
| 188 | */ |
| 189 | rule6->fwmask = 0xFFFFFFFF; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (tb[FRA_FWMASK]) |
| 194 | rule6->fwmask = nla_get_u32(tb[FRA_FWMASK]); |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 195 | #endif |
| 196 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 197 | rule6->src.plen = frh->src_len; |
| 198 | rule6->dst.plen = frh->dst_len; |
| 199 | rule6->tclass = frh->tos; |
| 200 | |
| 201 | err = 0; |
| 202 | errout: |
| 203 | return err; |
| 204 | } |
| 205 | |
| 206 | static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 207 | struct nlattr **tb) |
| 208 | { |
| 209 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 210 | |
| 211 | if (frh->src_len && (rule6->src.plen != frh->src_len)) |
| 212 | return 0; |
| 213 | |
| 214 | if (frh->dst_len && (rule6->dst.plen != frh->dst_len)) |
| 215 | return 0; |
| 216 | |
| 217 | if (frh->tos && (rule6->tclass != frh->tos)) |
| 218 | return 0; |
| 219 | |
| 220 | if (tb[FRA_SRC] && |
| 221 | nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr))) |
| 222 | return 0; |
| 223 | |
| 224 | if (tb[FRA_DST] && |
| 225 | nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr))) |
| 226 | return 0; |
| 227 | |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 228 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
| 229 | if (tb[FRA_FWMARK] && (rule6->fwmark != nla_get_u32(tb[FRA_FWMARK]))) |
| 230 | return 0; |
YOSHIFUJI Hideaki | cd9d742 | 2006-08-25 16:05:43 -0700 | [diff] [blame] | 231 | |
| 232 | if (tb[FRA_FWMASK] && (rule6->fwmask != nla_get_u32(tb[FRA_FWMASK]))) |
| 233 | return 0; |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 234 | #endif |
| 235 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 240 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) |
| 241 | { |
| 242 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 243 | |
| 244 | frh->family = AF_INET6; |
| 245 | frh->dst_len = rule6->dst.plen; |
| 246 | frh->src_len = rule6->src.plen; |
| 247 | frh->tos = rule6->tclass; |
| 248 | |
| 249 | if (rule6->dst.plen) |
| 250 | NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr), |
| 251 | &rule6->dst.addr); |
| 252 | |
| 253 | if (rule6->src.plen) |
| 254 | NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr), |
| 255 | &rule6->src.addr); |
| 256 | |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 257 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
| 258 | if (rule6->fwmark) |
| 259 | NLA_PUT_U32(skb, FRA_FWMARK, rule6->fwmark); |
YOSHIFUJI Hideaki | cd9d742 | 2006-08-25 16:05:43 -0700 | [diff] [blame] | 260 | |
Patrick McHardy | 366e4ad | 2006-08-26 16:50:20 -0700 | [diff] [blame] | 261 | if (rule6->fwmask || rule6->fwmark) |
YOSHIFUJI Hideaki | cd9d742 | 2006-08-25 16:05:43 -0700 | [diff] [blame] | 262 | NLA_PUT_U32(skb, FRA_FWMASK, rule6->fwmask); |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame] | 263 | #endif |
| 264 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | |
| 267 | nla_put_failure: |
| 268 | return -ENOBUFS; |
| 269 | } |
| 270 | |
| 271 | int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 272 | { |
| 273 | return fib_rules_dump(skb, cb, AF_INET6); |
| 274 | } |
| 275 | |
| 276 | static u32 fib6_rule_default_pref(void) |
| 277 | { |
| 278 | return 0x3FFF; |
| 279 | } |
| 280 | |
| 281 | static struct fib_rules_ops fib6_rules_ops = { |
| 282 | .family = AF_INET6, |
| 283 | .rule_size = sizeof(struct fib6_rule), |
| 284 | .action = fib6_rule_action, |
| 285 | .match = fib6_rule_match, |
| 286 | .configure = fib6_rule_configure, |
| 287 | .compare = fib6_rule_compare, |
| 288 | .fill = fib6_rule_fill, |
| 289 | .default_pref = fib6_rule_default_pref, |
| 290 | .nlgroup = RTNLGRP_IPV6_RULE, |
| 291 | .policy = fib6_rule_policy, |
| 292 | .rules_list = &fib6_rules, |
| 293 | .owner = THIS_MODULE, |
| 294 | }; |
| 295 | |
| 296 | void __init fib6_rules_init(void) |
| 297 | { |
| 298 | list_add_tail(&local_rule.common.list, &fib6_rules); |
| 299 | list_add_tail(&main_rule.common.list, &fib6_rules); |
| 300 | |
| 301 | fib_rules_register(&fib6_rules_ops); |
| 302 | } |
| 303 | |
| 304 | void fib6_rules_cleanup(void) |
| 305 | { |
| 306 | fib_rules_unregister(&fib6_rules_ops); |
| 307 | } |