blob: 89bea64eee1c3b186efd7dbc54a40d028b8ed3ba [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
Thomas Graf101367c2006-08-04 03:39:02 -070016#include <linux/netdevice.h>
17
18#include <net/fib_rules.h>
19#include <net/ipv6.h>
20#include <net/ip6_route.h>
21#include <net/netlink.h>
22
23struct fib6_rule
24{
25 struct fib_rule common;
26 struct rt6key src;
27 struct rt6key dst;
YOSHIFUJI Hideaki6c5eb6a2006-08-25 16:04:29 -070028 u32 fwmark;
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -070029 u32 fwmask;
Thomas Graf101367c2006-08-04 03:39:02 -070030 u8 tclass;
31};
32
33static struct fib_rules_ops fib6_rules_ops;
34
35static struct fib6_rule main_rule = {
36 .common = {
37 .refcnt = ATOMIC_INIT(2),
38 .pref = 0x7FFE,
39 .action = FR_ACT_TO_TBL,
40 .table = RT6_TABLE_MAIN,
41 },
42};
43
44static struct fib6_rule local_rule = {
45 .common = {
46 .refcnt = ATOMIC_INIT(2),
47 .pref = 0,
48 .action = FR_ACT_TO_TBL,
49 .table = RT6_TABLE_LOCAL,
50 .flags = FIB_RULE_PERMANENT,
51 },
52};
53
54static LIST_HEAD(fib6_rules);
55
56struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
57 pol_lookup_t lookup)
58{
59 struct fib_lookup_arg arg = {
60 .lookup_ptr = lookup,
61 };
62
63 fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
64 if (arg.rule)
65 fib_rule_put(arg.rule);
66
Ville Nuorvalab1429552006-08-08 16:44:17 -070067 if (arg.result)
68 return (struct dst_entry *) arg.result;
69
70 dst_hold(&ip6_null_entry.u.dst);
71 return &ip6_null_entry.u.dst;
Thomas Graf101367c2006-08-04 03:39:02 -070072}
73
Adrian Bunk8ce11e62006-08-07 21:50:48 -070074static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
75 int flags, struct fib_lookup_arg *arg)
Thomas Graf101367c2006-08-04 03:39:02 -070076{
77 struct rt6_info *rt = NULL;
78 struct fib6_table *table;
79 pol_lookup_t lookup = arg->lookup_ptr;
80
81 switch (rule->action) {
82 case FR_ACT_TO_TBL:
83 break;
84 case FR_ACT_UNREACHABLE:
85 rt = &ip6_null_entry;
86 goto discard_pkt;
87 default:
88 case FR_ACT_BLACKHOLE:
89 rt = &ip6_blk_hole_entry;
90 goto discard_pkt;
91 case FR_ACT_PROHIBIT:
92 rt = &ip6_prohibit_entry;
93 goto discard_pkt;
94 }
95
96 table = fib6_get_table(rule->table);
97 if (table)
98 rt = lookup(table, flp, flags);
99
100 if (rt != &ip6_null_entry)
101 goto out;
Thomas Graf101367c2006-08-04 03:39:02 -0700102 dst_release(&rt->u.dst);
Patrick McHardy3226f6882006-08-06 22:24:08 -0700103 rt = NULL;
104 goto out;
105
Thomas Graf101367c2006-08-04 03:39:02 -0700106discard_pkt:
107 dst_hold(&rt->u.dst);
108out:
109 arg->result = rt;
110 return rt == NULL ? -EAGAIN : 0;
111}
112
113
114static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
115{
116 struct fib6_rule *r = (struct fib6_rule *) rule;
117
Thomas Grafadaa70b2006-10-13 15:01:03 -0700118 if (r->dst.plen &&
119 !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
Thomas Graf101367c2006-08-04 03:39:02 -0700120 return 0;
121
Thomas Grafadaa70b2006-10-13 15:01:03 -0700122 if (r->src.plen) {
123 if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
124 !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
125 return 0;
126 }
Thomas Graf101367c2006-08-04 03:39:02 -0700127
YOSHIFUJI Hideaki2cc67cc2006-08-21 19:18:57 +0900128 if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
129 return 0;
130
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800131 if ((r->fwmark ^ fl->mark) & r->fwmask)
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900132 return 0;
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900133
Thomas Graf101367c2006-08-04 03:39:02 -0700134 return 1;
135}
136
YOSHIFUJI Hideaki2613aad2006-08-25 16:05:00 -0700137static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf5176f912006-08-26 20:13:18 -0700138 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
Thomas Graf101367c2006-08-04 03:39:02 -0700139 [FRA_PRIORITY] = { .type = NLA_U32 },
Thomas Graf5176f912006-08-26 20:13:18 -0700140 [FRA_SRC] = { .len = sizeof(struct in6_addr) },
141 [FRA_DST] = { .len = sizeof(struct in6_addr) },
YOSHIFUJI Hideaki6c5eb6a2006-08-25 16:04:29 -0700142 [FRA_FWMARK] = { .type = NLA_U32 },
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700143 [FRA_FWMASK] = { .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 Hideakicd9d7422006-08-25 16:05:43 -0700176 if (tb[FRA_FWMARK]) {
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900177 rule6->fwmark = nla_get_u32(tb[FRA_FWMARK]);
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700178 if (rule6->fwmark) {
179 /*
180 * if the mark value is non-zero,
181 * all bits are compared by default
182 * unless a mask is explicitly specified.
183 */
184 rule6->fwmask = 0xFFFFFFFF;
185 }
186 }
187
188 if (tb[FRA_FWMASK])
189 rule6->fwmask = nla_get_u32(tb[FRA_FWMASK]);
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900190
Thomas Graf101367c2006-08-04 03:39:02 -0700191 rule6->src.plen = frh->src_len;
192 rule6->dst.plen = frh->dst_len;
193 rule6->tclass = frh->tos;
194
195 err = 0;
196errout:
197 return err;
198}
199
200static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
201 struct nlattr **tb)
202{
203 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
204
205 if (frh->src_len && (rule6->src.plen != frh->src_len))
206 return 0;
207
208 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
209 return 0;
210
211 if (frh->tos && (rule6->tclass != frh->tos))
212 return 0;
213
214 if (tb[FRA_SRC] &&
215 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
216 return 0;
217
218 if (tb[FRA_DST] &&
219 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
220 return 0;
221
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900222 if (tb[FRA_FWMARK] && (rule6->fwmark != nla_get_u32(tb[FRA_FWMARK])))
223 return 0;
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700224
225 if (tb[FRA_FWMASK] && (rule6->fwmask != nla_get_u32(tb[FRA_FWMASK])))
226 return 0;
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900227
Thomas Graf101367c2006-08-04 03:39:02 -0700228 return 1;
229}
230
231static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
232 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
233{
234 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
235
236 frh->family = AF_INET6;
237 frh->dst_len = rule6->dst.plen;
238 frh->src_len = rule6->src.plen;
239 frh->tos = rule6->tclass;
240
241 if (rule6->dst.plen)
242 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
243 &rule6->dst.addr);
244
245 if (rule6->src.plen)
246 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
247 &rule6->src.addr);
248
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900249 if (rule6->fwmark)
250 NLA_PUT_U32(skb, FRA_FWMARK, rule6->fwmark);
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700251
Patrick McHardy366e4ad2006-08-26 16:50:20 -0700252 if (rule6->fwmask || rule6->fwmark)
YOSHIFUJI Hideakicd9d7422006-08-25 16:05:43 -0700253 NLA_PUT_U32(skb, FRA_FWMASK, rule6->fwmask);
YOSHIFUJI Hideaki75bff8f2006-08-21 19:22:01 +0900254
Thomas Graf101367c2006-08-04 03:39:02 -0700255 return 0;
256
257nla_put_failure:
258 return -ENOBUFS;
259}
260
261int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
262{
263 return fib_rules_dump(skb, cb, AF_INET6);
264}
265
266static u32 fib6_rule_default_pref(void)
267{
268 return 0x3FFF;
269}
270
271static struct fib_rules_ops fib6_rules_ops = {
272 .family = AF_INET6,
273 .rule_size = sizeof(struct fib6_rule),
274 .action = fib6_rule_action,
275 .match = fib6_rule_match,
276 .configure = fib6_rule_configure,
277 .compare = fib6_rule_compare,
278 .fill = fib6_rule_fill,
279 .default_pref = fib6_rule_default_pref,
280 .nlgroup = RTNLGRP_IPV6_RULE,
281 .policy = fib6_rule_policy,
282 .rules_list = &fib6_rules,
283 .owner = THIS_MODULE,
284};
285
286void __init fib6_rules_init(void)
287{
288 list_add_tail(&local_rule.common.list, &fib6_rules);
289 list_add_tail(&main_rule.common.list, &fib6_rules);
290
291 fib_rules_register(&fib6_rules_ops);
292}
293
294void fib6_rules_cleanup(void)
295{
296 fib_rules_unregister(&fib6_rules_ops);
297}