blob: 3d64c71f52dee19d20e39ea87a0272460f2a5720 [file] [log] [blame]
Thomas Graf101367c2006-08-04 03:39:02 -07001/*
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
24struct fib6_rule
25{
26 struct fib_rule common;
27 struct rt6key src;
28 struct rt6key dst;
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +090029#ifdef CONFIG_IPV6_ROUTE_FWMARK
YOSHIFUJI Hideaki6c5eb6a2006-08-25 16:04:29 -070030 u32 fwmark;
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +090031#endif
Thomas Graf101367c2006-08-04 03:39:02 -070032 u8 tclass;
33};
34
35static struct fib_rules_ops fib6_rules_ops;
36
37static 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
46static 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
56static LIST_HEAD(fib6_rules);
57
58struct 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 Nuorvalab1429552006-08-08 16:44:17 -070069 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 Graf101367c2006-08-04 03:39:02 -070074}
75
Adrian Bunk8ce11e62006-08-07 21:50:48 -070076static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
77 int flags, struct fib_lookup_arg *arg)
Thomas Graf101367c2006-08-04 03:39:02 -070078{
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 Graf101367c2006-08-04 03:39:02 -0700104 dst_release(&rt->u.dst);
Patrick McHardy3226f6882006-08-06 22:24:08 -0700105 rt = NULL;
106 goto out;
107
Thomas Graf101367c2006-08-04 03:39:02 -0700108discard_pkt:
109 dst_hold(&rt->u.dst);
110out:
111 arg->result = rt;
112 return rt == NULL ? -EAGAIN : 0;
113}
114
115
116static 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 Hideaki77d16f42006-08-23 17:25:05 -0700123 if ((flags & RT6_LOOKUP_F_HAS_SADDR) &&
Thomas Graf101367c2006-08-04 03:39:02 -0700124 !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
125 return 0;
126
YOSHIFUJI Hideaki2cc67cc2006-08-21 19:18:57 +0900127 if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
128 return 0;
129
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900130#ifdef CONFIG_IPV6_ROUTE_FWMARK
131 if (r->fwmark && (r->fwmark != fl->fl6_fwmark))
132 return 0;
133#endif
134
Thomas Graf101367c2006-08-04 03:39:02 -0700135 return 1;
136}
137
YOSHIFUJI Hideaki2613aad2006-08-25 16:05:00 -0700138static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf101367c2006-08-04 03:39:02 -0700139 [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) },
YOSHIFUJI Hideaki6c5eb6a2006-08-25 16:04:29 -0700143 [FRA_FWMARK] = { .type = NLA_U32 },
Patrick McHardy9e762a42006-08-10 23:09:48 -0700144 [FRA_TABLE] = { .type = NLA_U32 },
Thomas Graf101367c2006-08-04 03:39:02 -0700145};
146
147static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
148 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
149 struct nlattr **tb)
150{
151 int err = -EINVAL;
152 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
153
154 if (frh->src_len > 128 || frh->dst_len > 128 ||
155 (frh->tos & ~IPV6_FLOWINFO_MASK))
156 goto errout;
157
158 if (rule->action == FR_ACT_TO_TBL) {
159 if (rule->table == RT6_TABLE_UNSPEC)
160 goto errout;
161
162 if (fib6_new_table(rule->table) == NULL) {
163 err = -ENOBUFS;
164 goto errout;
165 }
166 }
167
168 if (tb[FRA_SRC])
169 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
170 sizeof(struct in6_addr));
171
172 if (tb[FRA_DST])
173 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
174 sizeof(struct in6_addr));
175
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900176#ifdef CONFIG_IPV6_ROUTE_FWMARK
177 if (tb[FRA_FWMARK])
178 rule6->fwmark = nla_get_u32(tb[FRA_FWMARK]);
179#endif
180
Thomas Graf101367c2006-08-04 03:39:02 -0700181 rule6->src.plen = frh->src_len;
182 rule6->dst.plen = frh->dst_len;
183 rule6->tclass = frh->tos;
184
185 err = 0;
186errout:
187 return err;
188}
189
190static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
191 struct nlattr **tb)
192{
193 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
194
195 if (frh->src_len && (rule6->src.plen != frh->src_len))
196 return 0;
197
198 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
199 return 0;
200
201 if (frh->tos && (rule6->tclass != frh->tos))
202 return 0;
203
204 if (tb[FRA_SRC] &&
205 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
206 return 0;
207
208 if (tb[FRA_DST] &&
209 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
210 return 0;
211
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900212#ifdef CONFIG_IPV6_ROUTE_FWMARK
213 if (tb[FRA_FWMARK] && (rule6->fwmark != nla_get_u32(tb[FRA_FWMARK])))
214 return 0;
215#endif
216
Thomas Graf101367c2006-08-04 03:39:02 -0700217 return 1;
218}
219
220static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
221 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
222{
223 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
224
225 frh->family = AF_INET6;
226 frh->dst_len = rule6->dst.plen;
227 frh->src_len = rule6->src.plen;
228 frh->tos = rule6->tclass;
229
230 if (rule6->dst.plen)
231 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
232 &rule6->dst.addr);
233
234 if (rule6->src.plen)
235 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
236 &rule6->src.addr);
237
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900238#ifdef CONFIG_IPV6_ROUTE_FWMARK
239 if (rule6->fwmark)
240 NLA_PUT_U32(skb, FRA_FWMARK, rule6->fwmark);
241#endif
242
Thomas Graf101367c2006-08-04 03:39:02 -0700243 return 0;
244
245nla_put_failure:
246 return -ENOBUFS;
247}
248
249int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
250{
251 return fib_rules_dump(skb, cb, AF_INET6);
252}
253
254static u32 fib6_rule_default_pref(void)
255{
256 return 0x3FFF;
257}
258
259static struct fib_rules_ops fib6_rules_ops = {
260 .family = AF_INET6,
261 .rule_size = sizeof(struct fib6_rule),
262 .action = fib6_rule_action,
263 .match = fib6_rule_match,
264 .configure = fib6_rule_configure,
265 .compare = fib6_rule_compare,
266 .fill = fib6_rule_fill,
267 .default_pref = fib6_rule_default_pref,
268 .nlgroup = RTNLGRP_IPV6_RULE,
269 .policy = fib6_rule_policy,
270 .rules_list = &fib6_rules,
271 .owner = THIS_MODULE,
272};
273
274void __init fib6_rules_init(void)
275{
276 list_add_tail(&local_rule.common.list, &fib6_rules);
277 list_add_tail(&main_rule.common.list, &fib6_rules);
278
279 fib_rules_register(&fib6_rules_ops);
280}
281
282void fib6_rules_cleanup(void)
283{
284 fib_rules_unregister(&fib6_rules_ops);
285}