2 * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules
4 * Copyright (C)2003-2006 Helsinki University of Technology
5 * Copyright (C)2003-2006 USAGI/WIDE Project
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.
12 * Thomas Graf <tgraf@suug.ch>
13 * Ville Nuorvala <vnuorval@tcs.hut.fi>
16 #include <linux/netdevice.h>
18 #include <net/fib_rules.h>
20 #include <net/ip6_route.h>
21 #include <net/netlink.h>
25 struct fib_rule common;
28 #ifdef CONFIG_IPV6_ROUTE_FWMARK
35 static struct fib_rules_ops fib6_rules_ops;
37 static struct fib6_rule main_rule = {
39 .refcnt = ATOMIC_INIT(2),
41 .action = FR_ACT_TO_TBL,
42 .table = RT6_TABLE_MAIN,
46 static struct fib6_rule local_rule = {
48 .refcnt = ATOMIC_INIT(2),
50 .action = FR_ACT_TO_TBL,
51 .table = RT6_TABLE_LOCAL,
52 .flags = FIB_RULE_PERMANENT,
56 static LIST_HEAD(fib6_rules);
58 struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
61 struct fib_lookup_arg arg = {
65 fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
67 fib_rule_put(arg.rule);
70 return (struct dst_entry *) arg.result;
72 dst_hold(&ip6_null_entry.u.dst);
73 return &ip6_null_entry.u.dst;
76 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
77 int flags, struct fib_lookup_arg *arg)
79 struct rt6_info *rt = NULL;
80 struct fib6_table *table;
81 pol_lookup_t lookup = arg->lookup_ptr;
83 switch (rule->action) {
86 case FR_ACT_UNREACHABLE:
90 case FR_ACT_BLACKHOLE:
91 rt = &ip6_blk_hole_entry;
94 rt = &ip6_prohibit_entry;
98 table = fib6_get_table(rule->table);
100 rt = lookup(table, flp, flags);
102 if (rt != &ip6_null_entry)
104 dst_release(&rt->u.dst);
109 dst_hold(&rt->u.dst);
112 return rt == NULL ? -EAGAIN : 0;
116 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
118 struct fib6_rule *r = (struct fib6_rule *) rule;
120 if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
123 if ((flags & RT6_LOOKUP_F_HAS_SADDR) &&
124 !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
127 if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
130 #ifdef CONFIG_IPV6_ROUTE_FWMARK
131 if ((r->fwmark ^ fl->fl6_fwmark) & r->fwmask)
138 static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
139 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
140 [FRA_PRIORITY] = { .type = NLA_U32 },
141 [FRA_SRC] = { .len = sizeof(struct in6_addr) },
142 [FRA_DST] = { .len = sizeof(struct in6_addr) },
143 [FRA_FWMARK] = { .type = NLA_U32 },
144 [FRA_FWMASK] = { .type = NLA_U32 },
145 [FRA_TABLE] = { .type = NLA_U32 },
148 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
149 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
153 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
155 if (frh->src_len > 128 || frh->dst_len > 128 ||
156 (frh->tos & ~IPV6_FLOWINFO_MASK))
159 if (rule->action == FR_ACT_TO_TBL) {
160 if (rule->table == RT6_TABLE_UNSPEC)
163 if (fib6_new_table(rule->table) == NULL) {
170 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
171 sizeof(struct in6_addr));
174 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
175 sizeof(struct in6_addr));
177 #ifdef CONFIG_IPV6_ROUTE_FWMARK
178 if (tb[FRA_FWMARK]) {
179 rule6->fwmark = nla_get_u32(tb[FRA_FWMARK]);
182 * if the mark value is non-zero,
183 * all bits are compared by default
184 * unless a mask is explicitly specified.
186 rule6->fwmask = 0xFFFFFFFF;
191 rule6->fwmask = nla_get_u32(tb[FRA_FWMASK]);
194 rule6->src.plen = frh->src_len;
195 rule6->dst.plen = frh->dst_len;
196 rule6->tclass = frh->tos;
203 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
206 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
208 if (frh->src_len && (rule6->src.plen != frh->src_len))
211 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
214 if (frh->tos && (rule6->tclass != frh->tos))
218 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
222 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
225 #ifdef CONFIG_IPV6_ROUTE_FWMARK
226 if (tb[FRA_FWMARK] && (rule6->fwmark != nla_get_u32(tb[FRA_FWMARK])))
229 if (tb[FRA_FWMASK] && (rule6->fwmask != nla_get_u32(tb[FRA_FWMASK])))
236 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
237 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
239 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
241 frh->family = AF_INET6;
242 frh->dst_len = rule6->dst.plen;
243 frh->src_len = rule6->src.plen;
244 frh->tos = rule6->tclass;
247 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
251 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
254 #ifdef CONFIG_IPV6_ROUTE_FWMARK
256 NLA_PUT_U32(skb, FRA_FWMARK, rule6->fwmark);
258 if (rule6->fwmask || rule6->fwmark)
259 NLA_PUT_U32(skb, FRA_FWMASK, rule6->fwmask);
268 int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
270 return fib_rules_dump(skb, cb, AF_INET6);
273 static u32 fib6_rule_default_pref(void)
278 static struct fib_rules_ops fib6_rules_ops = {
280 .rule_size = sizeof(struct fib6_rule),
281 .action = fib6_rule_action,
282 .match = fib6_rule_match,
283 .configure = fib6_rule_configure,
284 .compare = fib6_rule_compare,
285 .fill = fib6_rule_fill,
286 .default_pref = fib6_rule_default_pref,
287 .nlgroup = RTNLGRP_IPV6_RULE,
288 .policy = fib6_rule_policy,
289 .rules_list = &fib6_rules,
290 .owner = THIS_MODULE,
293 void __init fib6_rules_init(void)
295 list_add_tail(&local_rule.common.list, &fib6_rules);
296 list_add_tail(&main_rule.common.list, &fib6_rules);
298 fib_rules_register(&fib6_rules_ops);
301 void fib6_rules_cleanup(void)
303 fib_rules_unregister(&fib6_rules_ops);