blob: 34f5bfaddfc293f20e890be4c9acc4db193a4fe9 [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 Hideakicd9d7422006-08-25 16:05:43 -070031 u32 fwmask;
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +090032#endif
Thomas Graf101367c2006-08-04 03:39:02 -070033 u8 tclass;
34};
35
36static struct fib_rules_ops fib6_rules_ops;
37
38static struct fib6_rule main_rule = {
39 .common = {
40 .refcnt = ATOMIC_INIT(2),
41 .pref = 0x7FFE,
42 .action = FR_ACT_TO_TBL,
43 .table = RT6_TABLE_MAIN,
44 },
45};
46
47static struct fib6_rule local_rule = {
48 .common = {
49 .refcnt = ATOMIC_INIT(2),
50 .pref = 0,
51 .action = FR_ACT_TO_TBL,
52 .table = RT6_TABLE_LOCAL,
53 .flags = FIB_RULE_PERMANENT,
54 },
55};
56
57static LIST_HEAD(fib6_rules);
58
59struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
60 pol_lookup_t lookup)
61{
62 struct fib_lookup_arg arg = {
63 .lookup_ptr = lookup,
64 };
65
66 fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
67 if (arg.rule)
68 fib_rule_put(arg.rule);
69
Ville Nuorvalab1429552006-08-08 16:44:17 -070070 if (arg.result)
71 return (struct dst_entry *) arg.result;
72
73 dst_hold(&ip6_null_entry.u.dst);
74 return &ip6_null_entry.u.dst;
Thomas Graf101367c2006-08-04 03:39:02 -070075}
76
Adrian Bunk8ce11e62006-08-07 21:50:48 -070077static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
78 int flags, struct fib_lookup_arg *arg)
Thomas Graf101367c2006-08-04 03:39:02 -070079{
80 struct rt6_info *rt = NULL;
81 struct fib6_table *table;
82 pol_lookup_t lookup = arg->lookup_ptr;
83
84 switch (rule->action) {
85 case FR_ACT_TO_TBL:
86 break;
87 case FR_ACT_UNREACHABLE:
88 rt = &ip6_null_entry;
89 goto discard_pkt;
90 default:
91 case FR_ACT_BLACKHOLE:
92 rt = &ip6_blk_hole_entry;
93 goto discard_pkt;
94 case FR_ACT_PROHIBIT:
95 rt = &ip6_prohibit_entry;
96 goto discard_pkt;
97 }
98
99 table = fib6_get_table(rule->table);
100 if (table)
101 rt = lookup(table, flp, flags);
102
103 if (rt != &ip6_null_entry)
104 goto out;
Thomas Graf101367c2006-08-04 03:39:02 -0700105 dst_release(&rt->u.dst);
Patrick McHardy3226f6882006-08-06 22:24:08 -0700106 rt = NULL;
107 goto out;
108
Thomas Graf101367c2006-08-04 03:39:02 -0700109discard_pkt:
110 dst_hold(&rt->u.dst);
111out:
112 arg->result = rt;
113 return rt == NULL ? -EAGAIN : 0;
114}
115
116
117static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
118{
119 struct fib6_rule *r = (struct fib6_rule *) rule;
120
121 if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
122 return 0;
123
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700124 if ((flags & RT6_LOOKUP_F_HAS_SADDR) &&
Thomas Graf101367c2006-08-04 03:39:02 -0700125 !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
126 return 0;
127
YOSHIFUJI Hideaki2cc67cc2006-08-21 19:18:57 +0900128 if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
129 return 0;
130
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900131#ifdef CONFIG_IPV6_ROUTE_FWMARK
Patrick McHardy366e4ad2006-08-26 16:50:20 -0700132 if ((r->fwmark ^ fl->fl6_fwmark) & r->fwmask)
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900133 return 0;
134#endif
135
Thomas Graf101367c2006-08-04 03:39:02 -0700136 return 1;
137}
138
YOSHIFUJI Hideaki2613aad2006-08-25 16:05:00 -0700139static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf5176f912006-08-26 20:13:18 -0700140 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
Thomas Graf101367c2006-08-04 03:39:02 -0700141 [FRA_PRIORITY] = { .type = NLA_U32 },
Thomas Graf5176f912006-08-26 20:13:18 -0700142 [FRA_SRC] = { .len = sizeof(struct in6_addr) },
143 [FRA_DST] = { .len = sizeof(struct in6_addr) },
YOSHIFUJI Hideaki6c5eb6a2006-08-25 16:04:29 -0700144 [FRA_FWMARK] = { .type = NLA_U32 },
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700145 [FRA_FWMASK] = { .type = NLA_U32 },
Patrick McHardy9e762a42006-08-10 23:09:48 -0700146 [FRA_TABLE] = { .type = NLA_U32 },
Thomas Graf101367c2006-08-04 03:39:02 -0700147};
148
149static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
150 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
151 struct nlattr **tb)
152{
153 int err = -EINVAL;
154 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
155
156 if (frh->src_len > 128 || frh->dst_len > 128 ||
157 (frh->tos & ~IPV6_FLOWINFO_MASK))
158 goto errout;
159
160 if (rule->action == FR_ACT_TO_TBL) {
161 if (rule->table == RT6_TABLE_UNSPEC)
162 goto errout;
163
164 if (fib6_new_table(rule->table) == NULL) {
165 err = -ENOBUFS;
166 goto errout;
167 }
168 }
169
170 if (tb[FRA_SRC])
171 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
172 sizeof(struct in6_addr));
173
174 if (tb[FRA_DST])
175 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
176 sizeof(struct in6_addr));
177
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900178#ifdef CONFIG_IPV6_ROUTE_FWMARK
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700179 if (tb[FRA_FWMARK]) {
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900180 rule6->fwmark = nla_get_u32(tb[FRA_FWMARK]);
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700181 if (rule6->fwmark) {
182 /*
183 * if the mark value is non-zero,
184 * all bits are compared by default
185 * unless a mask is explicitly specified.
186 */
187 rule6->fwmask = 0xFFFFFFFF;
188 }
189 }
190
191 if (tb[FRA_FWMASK])
192 rule6->fwmask = nla_get_u32(tb[FRA_FWMASK]);
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900193#endif
194
Thomas Graf101367c2006-08-04 03:39:02 -0700195 rule6->src.plen = frh->src_len;
196 rule6->dst.plen = frh->dst_len;
197 rule6->tclass = frh->tos;
198
199 err = 0;
200errout:
201 return err;
202}
203
204static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
205 struct nlattr **tb)
206{
207 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
208
209 if (frh->src_len && (rule6->src.plen != frh->src_len))
210 return 0;
211
212 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
213 return 0;
214
215 if (frh->tos && (rule6->tclass != frh->tos))
216 return 0;
217
218 if (tb[FRA_SRC] &&
219 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
220 return 0;
221
222 if (tb[FRA_DST] &&
223 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
224 return 0;
225
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900226#ifdef CONFIG_IPV6_ROUTE_FWMARK
227 if (tb[FRA_FWMARK] && (rule6->fwmark != nla_get_u32(tb[FRA_FWMARK])))
228 return 0;
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700229
230 if (tb[FRA_FWMASK] && (rule6->fwmask != nla_get_u32(tb[FRA_FWMASK])))
231 return 0;
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900232#endif
233
Thomas Graf101367c2006-08-04 03:39:02 -0700234 return 1;
235}
236
237static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
238 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
239{
240 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
241
242 frh->family = AF_INET6;
243 frh->dst_len = rule6->dst.plen;
244 frh->src_len = rule6->src.plen;
245 frh->tos = rule6->tclass;
246
247 if (rule6->dst.plen)
248 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
249 &rule6->dst.addr);
250
251 if (rule6->src.plen)
252 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
253 &rule6->src.addr);
254
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900255#ifdef CONFIG_IPV6_ROUTE_FWMARK
256 if (rule6->fwmark)
257 NLA_PUT_U32(skb, FRA_FWMARK, rule6->fwmark);
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700258
Patrick McHardy366e4ad2006-08-26 16:50:20 -0700259 if (rule6->fwmask || rule6->fwmark)
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700260 NLA_PUT_U32(skb, FRA_FWMASK, rule6->fwmask);
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900261#endif
262
Thomas Graf101367c2006-08-04 03:39:02 -0700263 return 0;
264
265nla_put_failure:
266 return -ENOBUFS;
267}
268
269int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
270{
271 return fib_rules_dump(skb, cb, AF_INET6);
272}
273
274static u32 fib6_rule_default_pref(void)
275{
276 return 0x3FFF;
277}
278
279static struct fib_rules_ops fib6_rules_ops = {
280 .family = AF_INET6,
281 .rule_size = sizeof(struct fib6_rule),
282 .action = fib6_rule_action,
283 .match = fib6_rule_match,
284 .configure = fib6_rule_configure,
285 .compare = fib6_rule_compare,
286 .fill = fib6_rule_fill,
287 .default_pref = fib6_rule_default_pref,
288 .nlgroup = RTNLGRP_IPV6_RULE,
289 .policy = fib6_rule_policy,
290 .rules_list = &fib6_rules,
291 .owner = THIS_MODULE,
292};
293
294void __init fib6_rules_init(void)
295{
296 list_add_tail(&local_rule.common.list, &fib6_rules);
297 list_add_tail(&main_rule.common.list, &fib6_rules);
298
299 fib_rules_register(&fib6_rules_ops);
300}
301
302void fib6_rules_cleanup(void)
303{
304 fib_rules_unregister(&fib6_rules_ops);
305}