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;
31 static struct fib_rules_ops fib6_rules_ops;
33 static struct fib6_rule main_rule = {
35 .refcnt = ATOMIC_INIT(2),
37 .action = FR_ACT_TO_TBL,
38 .table = RT6_TABLE_MAIN,
42 static struct fib6_rule local_rule = {
44 .refcnt = ATOMIC_INIT(2),
46 .action = FR_ACT_TO_TBL,
47 .table = RT6_TABLE_LOCAL,
48 .flags = FIB_RULE_PERMANENT,
52 static LIST_HEAD(fib6_rules);
54 struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
57 struct fib_lookup_arg arg = {
61 fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
63 fib_rule_put(arg.rule);
66 return (struct dst_entry *) arg.result;
68 dst_hold(&ip6_null_entry.u.dst);
69 return &ip6_null_entry.u.dst;
72 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
73 int flags, struct fib_lookup_arg *arg)
75 struct rt6_info *rt = NULL;
76 struct fib6_table *table;
77 pol_lookup_t lookup = arg->lookup_ptr;
79 switch (rule->action) {
82 case FR_ACT_UNREACHABLE:
86 case FR_ACT_BLACKHOLE:
87 rt = &ip6_blk_hole_entry;
90 rt = &ip6_prohibit_entry;
94 table = fib6_get_table(rule->table);
96 rt = lookup(table, flp, flags);
98 if (rt != &ip6_null_entry)
100 dst_release(&rt->u.dst);
105 dst_hold(&rt->u.dst);
108 return rt == NULL ? -EAGAIN : 0;
112 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
114 struct fib6_rule *r = (struct fib6_rule *) rule;
117 !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
121 if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
122 !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
126 if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
132 static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
133 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
134 [FRA_PRIORITY] = { .type = NLA_U32 },
135 [FRA_SRC] = { .len = sizeof(struct in6_addr) },
136 [FRA_DST] = { .len = sizeof(struct in6_addr) },
137 [FRA_FWMARK] = { .type = NLA_U32 },
138 [FRA_FWMASK] = { .type = NLA_U32 },
139 [FRA_TABLE] = { .type = NLA_U32 },
142 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
143 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
147 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
149 if (frh->src_len > 128 || frh->dst_len > 128 ||
150 (frh->tos & ~IPV6_FLOWINFO_MASK))
153 if (rule->action == FR_ACT_TO_TBL) {
154 if (rule->table == RT6_TABLE_UNSPEC)
157 if (fib6_new_table(rule->table) == NULL) {
164 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
165 sizeof(struct in6_addr));
168 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
169 sizeof(struct in6_addr));
171 rule6->src.plen = frh->src_len;
172 rule6->dst.plen = frh->dst_len;
173 rule6->tclass = frh->tos;
180 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
183 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
185 if (frh->src_len && (rule6->src.plen != frh->src_len))
188 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
191 if (frh->tos && (rule6->tclass != frh->tos))
195 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
199 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
205 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
206 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
208 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
210 frh->family = AF_INET6;
211 frh->dst_len = rule6->dst.plen;
212 frh->src_len = rule6->src.plen;
213 frh->tos = rule6->tclass;
216 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
220 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
229 int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
231 return fib_rules_dump(skb, cb, AF_INET6);
234 static u32 fib6_rule_default_pref(void)
239 static struct fib_rules_ops fib6_rules_ops = {
241 .rule_size = sizeof(struct fib6_rule),
242 .action = fib6_rule_action,
243 .match = fib6_rule_match,
244 .configure = fib6_rule_configure,
245 .compare = fib6_rule_compare,
246 .fill = fib6_rule_fill,
247 .default_pref = fib6_rule_default_pref,
248 .nlgroup = RTNLGRP_IPV6_RULE,
249 .policy = fib6_rule_policy,
250 .rules_list = &fib6_rules,
251 .owner = THIS_MODULE,
254 void __init fib6_rules_init(void)
256 list_add_tail(&local_rule.common.list, &fib6_rules);
257 list_add_tail(&main_rule.common.list, &fib6_rules);
259 fib_rules_register(&fib6_rules_ops);
262 void fib6_rules_cleanup(void)
264 fib_rules_unregister(&fib6_rules_ops);