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 | |
| 16 | #include <linux/config.h> |
| 17 | #include <linux/netdevice.h> |
| 18 | |
| 19 | #include <net/fib_rules.h> |
| 20 | #include <net/ipv6.h> |
| 21 | #include <net/ip6_route.h> |
| 22 | #include <net/netlink.h> |
| 23 | |
| 24 | struct fib6_rule |
| 25 | { |
| 26 | struct fib_rule common; |
| 27 | struct rt6key src; |
| 28 | struct rt6key dst; |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame^] | 29 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
| 30 | u8 fwmark; |
| 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 | |
| 120 | if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen)) |
| 121 | return 0; |
| 122 | |
YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 123 | if ((flags & RT6_LOOKUP_F_HAS_SADDR) && |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 124 | !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen)) |
| 125 | return 0; |
| 126 | |
YOSHIFUJI Hideaki | 2cc67cc | 2006-08-21 19:18:57 +0900 | [diff] [blame] | 127 | if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff)) |
| 128 | return 0; |
| 129 | |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame^] | 130 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
| 131 | if (r->fwmark && (r->fwmark != fl->fl6_fwmark)) |
| 132 | return 0; |
| 133 | #endif |
| 134 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | static struct nla_policy fib6_rule_policy[RTA_MAX+1] __read_mostly = { |
| 139 | [FRA_IFNAME] = { .type = NLA_STRING }, |
| 140 | [FRA_PRIORITY] = { .type = NLA_U32 }, |
| 141 | [FRA_SRC] = { .minlen = sizeof(struct in6_addr) }, |
| 142 | [FRA_DST] = { .minlen = sizeof(struct in6_addr) }, |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 143 | [FRA_TABLE] = { .type = NLA_U32 }, |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 147 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, |
| 148 | struct nlattr **tb) |
| 149 | { |
| 150 | int err = -EINVAL; |
| 151 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 152 | |
| 153 | if (frh->src_len > 128 || frh->dst_len > 128 || |
| 154 | (frh->tos & ~IPV6_FLOWINFO_MASK)) |
| 155 | goto errout; |
| 156 | |
| 157 | if (rule->action == FR_ACT_TO_TBL) { |
| 158 | if (rule->table == RT6_TABLE_UNSPEC) |
| 159 | goto errout; |
| 160 | |
| 161 | if (fib6_new_table(rule->table) == NULL) { |
| 162 | err = -ENOBUFS; |
| 163 | goto errout; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (tb[FRA_SRC]) |
| 168 | nla_memcpy(&rule6->src.addr, tb[FRA_SRC], |
| 169 | sizeof(struct in6_addr)); |
| 170 | |
| 171 | if (tb[FRA_DST]) |
| 172 | nla_memcpy(&rule6->dst.addr, tb[FRA_DST], |
| 173 | sizeof(struct in6_addr)); |
| 174 | |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame^] | 175 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
| 176 | if (tb[FRA_FWMARK]) |
| 177 | rule6->fwmark = nla_get_u32(tb[FRA_FWMARK]); |
| 178 | #endif |
| 179 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 180 | rule6->src.plen = frh->src_len; |
| 181 | rule6->dst.plen = frh->dst_len; |
| 182 | rule6->tclass = frh->tos; |
| 183 | |
| 184 | err = 0; |
| 185 | errout: |
| 186 | return err; |
| 187 | } |
| 188 | |
| 189 | static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 190 | struct nlattr **tb) |
| 191 | { |
| 192 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 193 | |
| 194 | if (frh->src_len && (rule6->src.plen != frh->src_len)) |
| 195 | return 0; |
| 196 | |
| 197 | if (frh->dst_len && (rule6->dst.plen != frh->dst_len)) |
| 198 | return 0; |
| 199 | |
| 200 | if (frh->tos && (rule6->tclass != frh->tos)) |
| 201 | return 0; |
| 202 | |
| 203 | if (tb[FRA_SRC] && |
| 204 | nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr))) |
| 205 | return 0; |
| 206 | |
| 207 | if (tb[FRA_DST] && |
| 208 | nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr))) |
| 209 | return 0; |
| 210 | |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame^] | 211 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
| 212 | if (tb[FRA_FWMARK] && (rule6->fwmark != nla_get_u32(tb[FRA_FWMARK]))) |
| 213 | return 0; |
| 214 | #endif |
| 215 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 220 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) |
| 221 | { |
| 222 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 223 | |
| 224 | frh->family = AF_INET6; |
| 225 | frh->dst_len = rule6->dst.plen; |
| 226 | frh->src_len = rule6->src.plen; |
| 227 | frh->tos = rule6->tclass; |
| 228 | |
| 229 | if (rule6->dst.plen) |
| 230 | NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr), |
| 231 | &rule6->dst.addr); |
| 232 | |
| 233 | if (rule6->src.plen) |
| 234 | NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr), |
| 235 | &rule6->src.addr); |
| 236 | |
YOSHIFUJI Hideaki | 75bff8f | 2006-08-21 19:22:01 +0900 | [diff] [blame^] | 237 | #ifdef CONFIG_IPV6_ROUTE_FWMARK |
| 238 | if (rule6->fwmark) |
| 239 | NLA_PUT_U32(skb, FRA_FWMARK, rule6->fwmark); |
| 240 | #endif |
| 241 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 242 | return 0; |
| 243 | |
| 244 | nla_put_failure: |
| 245 | return -ENOBUFS; |
| 246 | } |
| 247 | |
| 248 | int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 249 | { |
| 250 | return fib_rules_dump(skb, cb, AF_INET6); |
| 251 | } |
| 252 | |
| 253 | static u32 fib6_rule_default_pref(void) |
| 254 | { |
| 255 | return 0x3FFF; |
| 256 | } |
| 257 | |
| 258 | static struct fib_rules_ops fib6_rules_ops = { |
| 259 | .family = AF_INET6, |
| 260 | .rule_size = sizeof(struct fib6_rule), |
| 261 | .action = fib6_rule_action, |
| 262 | .match = fib6_rule_match, |
| 263 | .configure = fib6_rule_configure, |
| 264 | .compare = fib6_rule_compare, |
| 265 | .fill = fib6_rule_fill, |
| 266 | .default_pref = fib6_rule_default_pref, |
| 267 | .nlgroup = RTNLGRP_IPV6_RULE, |
| 268 | .policy = fib6_rule_policy, |
| 269 | .rules_list = &fib6_rules, |
| 270 | .owner = THIS_MODULE, |
| 271 | }; |
| 272 | |
| 273 | void __init fib6_rules_init(void) |
| 274 | { |
| 275 | list_add_tail(&local_rule.common.list, &fib6_rules); |
| 276 | list_add_tail(&main_rule.common.list, &fib6_rules); |
| 277 | |
| 278 | fib_rules_register(&fib6_rules_ops); |
| 279 | } |
| 280 | |
| 281 | void fib6_rules_cleanup(void) |
| 282 | { |
| 283 | fib_rules_unregister(&fib6_rules_ops); |
| 284 | } |