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; |
| 29 | u8 tclass; |
| 30 | }; |
| 31 | |
| 32 | static struct fib_rules_ops fib6_rules_ops; |
| 33 | |
| 34 | static struct fib6_rule main_rule = { |
| 35 | .common = { |
| 36 | .refcnt = ATOMIC_INIT(2), |
| 37 | .pref = 0x7FFE, |
| 38 | .action = FR_ACT_TO_TBL, |
| 39 | .table = RT6_TABLE_MAIN, |
| 40 | }, |
| 41 | }; |
| 42 | |
| 43 | static struct fib6_rule local_rule = { |
| 44 | .common = { |
| 45 | .refcnt = ATOMIC_INIT(2), |
| 46 | .pref = 0, |
| 47 | .action = FR_ACT_TO_TBL, |
| 48 | .table = RT6_TABLE_LOCAL, |
| 49 | .flags = FIB_RULE_PERMANENT, |
| 50 | }, |
| 51 | }; |
| 52 | |
| 53 | static LIST_HEAD(fib6_rules); |
| 54 | |
| 55 | struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags, |
| 56 | pol_lookup_t lookup) |
| 57 | { |
| 58 | struct fib_lookup_arg arg = { |
| 59 | .lookup_ptr = lookup, |
| 60 | }; |
| 61 | |
| 62 | fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg); |
| 63 | if (arg.rule) |
| 64 | fib_rule_put(arg.rule); |
| 65 | |
Ville Nuorvala | b142955 | 2006-08-08 16:44:17 -0700 | [diff] [blame^] | 66 | if (arg.result) |
| 67 | return (struct dst_entry *) arg.result; |
| 68 | |
| 69 | dst_hold(&ip6_null_entry.u.dst); |
| 70 | return &ip6_null_entry.u.dst; |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 73 | static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 74 | int flags, struct fib_lookup_arg *arg) |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 75 | { |
| 76 | struct rt6_info *rt = NULL; |
| 77 | struct fib6_table *table; |
| 78 | pol_lookup_t lookup = arg->lookup_ptr; |
| 79 | |
| 80 | switch (rule->action) { |
| 81 | case FR_ACT_TO_TBL: |
| 82 | break; |
| 83 | case FR_ACT_UNREACHABLE: |
| 84 | rt = &ip6_null_entry; |
| 85 | goto discard_pkt; |
| 86 | default: |
| 87 | case FR_ACT_BLACKHOLE: |
| 88 | rt = &ip6_blk_hole_entry; |
| 89 | goto discard_pkt; |
| 90 | case FR_ACT_PROHIBIT: |
| 91 | rt = &ip6_prohibit_entry; |
| 92 | goto discard_pkt; |
| 93 | } |
| 94 | |
| 95 | table = fib6_get_table(rule->table); |
| 96 | if (table) |
| 97 | rt = lookup(table, flp, flags); |
| 98 | |
| 99 | if (rt != &ip6_null_entry) |
| 100 | goto out; |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 101 | dst_release(&rt->u.dst); |
Patrick McHardy | 3226f688 | 2006-08-06 22:24:08 -0700 | [diff] [blame] | 102 | rt = NULL; |
| 103 | goto out; |
| 104 | |
Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 105 | discard_pkt: |
| 106 | dst_hold(&rt->u.dst); |
| 107 | out: |
| 108 | arg->result = rt; |
| 109 | return rt == NULL ? -EAGAIN : 0; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 114 | { |
| 115 | struct fib6_rule *r = (struct fib6_rule *) rule; |
| 116 | |
| 117 | if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen)) |
| 118 | return 0; |
| 119 | |
| 120 | if ((flags & RT6_F_HAS_SADDR) && |
| 121 | !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen)) |
| 122 | return 0; |
| 123 | |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | static struct nla_policy fib6_rule_policy[RTA_MAX+1] __read_mostly = { |
| 128 | [FRA_IFNAME] = { .type = NLA_STRING }, |
| 129 | [FRA_PRIORITY] = { .type = NLA_U32 }, |
| 130 | [FRA_SRC] = { .minlen = sizeof(struct in6_addr) }, |
| 131 | [FRA_DST] = { .minlen = sizeof(struct in6_addr) }, |
| 132 | }; |
| 133 | |
| 134 | static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 135 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, |
| 136 | struct nlattr **tb) |
| 137 | { |
| 138 | int err = -EINVAL; |
| 139 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 140 | |
| 141 | if (frh->src_len > 128 || frh->dst_len > 128 || |
| 142 | (frh->tos & ~IPV6_FLOWINFO_MASK)) |
| 143 | goto errout; |
| 144 | |
| 145 | if (rule->action == FR_ACT_TO_TBL) { |
| 146 | if (rule->table == RT6_TABLE_UNSPEC) |
| 147 | goto errout; |
| 148 | |
| 149 | if (fib6_new_table(rule->table) == NULL) { |
| 150 | err = -ENOBUFS; |
| 151 | goto errout; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (tb[FRA_SRC]) |
| 156 | nla_memcpy(&rule6->src.addr, tb[FRA_SRC], |
| 157 | sizeof(struct in6_addr)); |
| 158 | |
| 159 | if (tb[FRA_DST]) |
| 160 | nla_memcpy(&rule6->dst.addr, tb[FRA_DST], |
| 161 | sizeof(struct in6_addr)); |
| 162 | |
| 163 | rule6->src.plen = frh->src_len; |
| 164 | rule6->dst.plen = frh->dst_len; |
| 165 | rule6->tclass = frh->tos; |
| 166 | |
| 167 | err = 0; |
| 168 | errout: |
| 169 | return err; |
| 170 | } |
| 171 | |
| 172 | static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 173 | struct nlattr **tb) |
| 174 | { |
| 175 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 176 | |
| 177 | if (frh->src_len && (rule6->src.plen != frh->src_len)) |
| 178 | return 0; |
| 179 | |
| 180 | if (frh->dst_len && (rule6->dst.plen != frh->dst_len)) |
| 181 | return 0; |
| 182 | |
| 183 | if (frh->tos && (rule6->tclass != frh->tos)) |
| 184 | return 0; |
| 185 | |
| 186 | if (tb[FRA_SRC] && |
| 187 | nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr))) |
| 188 | return 0; |
| 189 | |
| 190 | if (tb[FRA_DST] && |
| 191 | nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr))) |
| 192 | return 0; |
| 193 | |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 198 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) |
| 199 | { |
| 200 | struct fib6_rule *rule6 = (struct fib6_rule *) rule; |
| 201 | |
| 202 | frh->family = AF_INET6; |
| 203 | frh->dst_len = rule6->dst.plen; |
| 204 | frh->src_len = rule6->src.plen; |
| 205 | frh->tos = rule6->tclass; |
| 206 | |
| 207 | if (rule6->dst.plen) |
| 208 | NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr), |
| 209 | &rule6->dst.addr); |
| 210 | |
| 211 | if (rule6->src.plen) |
| 212 | NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr), |
| 213 | &rule6->src.addr); |
| 214 | |
| 215 | return 0; |
| 216 | |
| 217 | nla_put_failure: |
| 218 | return -ENOBUFS; |
| 219 | } |
| 220 | |
| 221 | int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 222 | { |
| 223 | return fib_rules_dump(skb, cb, AF_INET6); |
| 224 | } |
| 225 | |
| 226 | static u32 fib6_rule_default_pref(void) |
| 227 | { |
| 228 | return 0x3FFF; |
| 229 | } |
| 230 | |
| 231 | static struct fib_rules_ops fib6_rules_ops = { |
| 232 | .family = AF_INET6, |
| 233 | .rule_size = sizeof(struct fib6_rule), |
| 234 | .action = fib6_rule_action, |
| 235 | .match = fib6_rule_match, |
| 236 | .configure = fib6_rule_configure, |
| 237 | .compare = fib6_rule_compare, |
| 238 | .fill = fib6_rule_fill, |
| 239 | .default_pref = fib6_rule_default_pref, |
| 240 | .nlgroup = RTNLGRP_IPV6_RULE, |
| 241 | .policy = fib6_rule_policy, |
| 242 | .rules_list = &fib6_rules, |
| 243 | .owner = THIS_MODULE, |
| 244 | }; |
| 245 | |
| 246 | void __init fib6_rules_init(void) |
| 247 | { |
| 248 | list_add_tail(&local_rule.common.list, &fib6_rules); |
| 249 | list_add_tail(&main_rule.common.list, &fib6_rules); |
| 250 | |
| 251 | fib_rules_register(&fib6_rules_ops); |
| 252 | } |
| 253 | |
| 254 | void fib6_rules_cleanup(void) |
| 255 | { |
| 256 | fib_rules_unregister(&fib6_rules_ops); |
| 257 | } |