blob: 89cb092c973256c54a5db40f8f2e6150d803f1aa [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>
YOSHIFUJI Hideaki29f6af72007-04-06 11:45:39 -070020#include <net/addrconf.h>
Thomas Graf101367c2006-08-04 03:39:02 -070021#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;
29 u8 tclass;
30};
31
Daniel Lezcano58f09b72008-03-03 23:25:27 -080032struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi *fl,
33 int flags, pol_lookup_t lookup)
Thomas Graf101367c2006-08-04 03:39:02 -070034{
35 struct fib_lookup_arg arg = {
36 .lookup_ptr = lookup,
37 };
38
Daniel Lezcanodcabb812008-03-03 23:33:08 -080039 fib_rules_lookup(net->ipv6.fib6_rules_ops, fl, flags, &arg);
Thomas Graf101367c2006-08-04 03:39:02 -070040 if (arg.rule)
41 fib_rule_put(arg.rule);
42
Ville Nuorvalab1429552006-08-08 16:44:17 -070043 if (arg.result)
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +090044 return arg.result;
Ville Nuorvalab1429552006-08-08 16:44:17 -070045
46 dst_hold(&ip6_null_entry.u.dst);
47 return &ip6_null_entry.u.dst;
Thomas Graf101367c2006-08-04 03:39:02 -070048}
49
Adrian Bunk8ce11e62006-08-07 21:50:48 -070050static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
51 int flags, struct fib_lookup_arg *arg)
Thomas Graf101367c2006-08-04 03:39:02 -070052{
53 struct rt6_info *rt = NULL;
54 struct fib6_table *table;
55 pol_lookup_t lookup = arg->lookup_ptr;
56
57 switch (rule->action) {
58 case FR_ACT_TO_TBL:
59 break;
60 case FR_ACT_UNREACHABLE:
61 rt = &ip6_null_entry;
62 goto discard_pkt;
63 default:
64 case FR_ACT_BLACKHOLE:
65 rt = &ip6_blk_hole_entry;
66 goto discard_pkt;
67 case FR_ACT_PROHIBIT:
68 rt = &ip6_prohibit_entry;
69 goto discard_pkt;
70 }
71
Daniel Lezcanodcabb812008-03-03 23:33:08 -080072 table = fib6_get_table(rule->fr_net, rule->table);
Thomas Graf101367c2006-08-04 03:39:02 -070073 if (table)
74 rt = lookup(table, flp, flags);
75
YOSHIFUJI Hideaki29f6af72007-04-06 11:45:39 -070076 if (rt != &ip6_null_entry) {
77 struct fib6_rule *r = (struct fib6_rule *)rule;
78
79 /*
80 * If we need to find a source address for this traffic,
81 * we check the result if it meets requirement of the rule.
82 */
83 if ((rule->flags & FIB_RULE_FIND_SADDR) &&
84 r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
85 struct in6_addr saddr;
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +090086 if (ipv6_dev_get_saddr(ip6_dst_idev(&rt->u.dst)->dev,
87 &flp->fl6_dst, &saddr))
YOSHIFUJI Hideaki29f6af72007-04-06 11:45:39 -070088 goto again;
89 if (!ipv6_prefix_equal(&saddr, &r->src.addr,
90 r->src.plen))
91 goto again;
92 ipv6_addr_copy(&flp->fl6_src, &saddr);
93 }
Thomas Graf101367c2006-08-04 03:39:02 -070094 goto out;
YOSHIFUJI Hideaki29f6af72007-04-06 11:45:39 -070095 }
96again:
Thomas Graf101367c2006-08-04 03:39:02 -070097 dst_release(&rt->u.dst);
Patrick McHardy3226f6882006-08-06 22:24:08 -070098 rt = NULL;
99 goto out;
100
Thomas Graf101367c2006-08-04 03:39:02 -0700101discard_pkt:
102 dst_hold(&rt->u.dst);
103out:
104 arg->result = rt;
105 return rt == NULL ? -EAGAIN : 0;
106}
107
108
109static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
110{
111 struct fib6_rule *r = (struct fib6_rule *) rule;
112
Thomas Grafadaa70b2006-10-13 15:01:03 -0700113 if (r->dst.plen &&
114 !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
Thomas Graf101367c2006-08-04 03:39:02 -0700115 return 0;
116
YOSHIFUJI Hideaki29f6af72007-04-06 11:45:39 -0700117 /*
118 * If FIB_RULE_FIND_SADDR is set and we do not have a
119 * source address for the traffic, we defer check for
120 * source address.
121 */
Thomas Grafadaa70b2006-10-13 15:01:03 -0700122 if (r->src.plen) {
YOSHIFUJI Hideaki29f6af72007-04-06 11:45:39 -0700123 if (flags & RT6_LOOKUP_F_HAS_SADDR) {
124 if (!ipv6_prefix_equal(&fl->fl6_src, &r->src.addr,
125 r->src.plen))
126 return 0;
127 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
Thomas Grafadaa70b2006-10-13 15:01:03 -0700128 return 0;
129 }
Thomas Graf101367c2006-08-04 03:39:02 -0700130
YOSHIFUJI Hideaki2cc67cc2006-08-21 19:18:57 +0900131 if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
132 return 0;
133
Thomas Graf101367c2006-08-04 03:39:02 -0700134 return 1;
135}
136
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700137static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800138 FRA_GENERIC_POLICY,
Thomas Graf101367c2006-08-04 03:39:02 -0700139};
140
141static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
142 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
143 struct nlattr **tb)
144{
145 int err = -EINVAL;
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800146 struct net *net = skb->sk->sk_net;
Thomas Graf101367c2006-08-04 03:39:02 -0700147 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
148
Thomas Graf101367c2006-08-04 03:39:02 -0700149 if (rule->action == FR_ACT_TO_TBL) {
150 if (rule->table == RT6_TABLE_UNSPEC)
151 goto errout;
152
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800153 if (fib6_new_table(net, rule->table) == NULL) {
Thomas Graf101367c2006-08-04 03:39:02 -0700154 err = -ENOBUFS;
155 goto errout;
156 }
157 }
158
Thomas Grafe1701c62007-03-24 12:46:02 -0700159 if (frh->src_len)
Thomas Graf101367c2006-08-04 03:39:02 -0700160 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
161 sizeof(struct in6_addr));
162
Thomas Grafe1701c62007-03-24 12:46:02 -0700163 if (frh->dst_len)
Thomas Graf101367c2006-08-04 03:39:02 -0700164 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
165 sizeof(struct in6_addr));
166
167 rule6->src.plen = frh->src_len;
168 rule6->dst.plen = frh->dst_len;
169 rule6->tclass = frh->tos;
170
171 err = 0;
172errout:
173 return err;
174}
175
176static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
177 struct nlattr **tb)
178{
179 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
180
181 if (frh->src_len && (rule6->src.plen != frh->src_len))
182 return 0;
183
184 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
185 return 0;
186
187 if (frh->tos && (rule6->tclass != frh->tos))
188 return 0;
189
Thomas Grafe1701c62007-03-24 12:46:02 -0700190 if (frh->src_len &&
Thomas Graf101367c2006-08-04 03:39:02 -0700191 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
192 return 0;
193
Thomas Grafe1701c62007-03-24 12:46:02 -0700194 if (frh->dst_len &&
Thomas Graf101367c2006-08-04 03:39:02 -0700195 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
196 return 0;
197
198 return 1;
199}
200
201static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
202 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
203{
204 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
205
206 frh->family = AF_INET6;
207 frh->dst_len = rule6->dst.plen;
208 frh->src_len = rule6->src.plen;
209 frh->tos = rule6->tclass;
210
211 if (rule6->dst.plen)
212 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
213 &rule6->dst.addr);
214
215 if (rule6->src.plen)
216 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
217 &rule6->src.addr);
218
Thomas Graf101367c2006-08-04 03:39:02 -0700219 return 0;
220
221nla_put_failure:
222 return -ENOBUFS;
223}
224
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800225static u32 fib6_rule_default_pref(struct fib_rules_ops *ops)
Thomas Graf101367c2006-08-04 03:39:02 -0700226{
227 return 0x3FFF;
228}
229
Thomas Graf339bf982006-11-10 14:10:15 -0800230static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
231{
232 return nla_total_size(16) /* dst */
233 + nla_total_size(16); /* src */
234}
235
Daniel Lezcanoeb5564b2008-03-03 23:32:30 -0800236static struct fib_rules_ops fib6_rules_ops_template = {
Thomas Graf101367c2006-08-04 03:39:02 -0700237 .family = AF_INET6,
238 .rule_size = sizeof(struct fib6_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700239 .addr_size = sizeof(struct in6_addr),
Thomas Graf101367c2006-08-04 03:39:02 -0700240 .action = fib6_rule_action,
241 .match = fib6_rule_match,
242 .configure = fib6_rule_configure,
243 .compare = fib6_rule_compare,
244 .fill = fib6_rule_fill,
245 .default_pref = fib6_rule_default_pref,
Thomas Graf339bf982006-11-10 14:10:15 -0800246 .nlmsg_payload = fib6_rule_nlmsg_payload,
Thomas Graf101367c2006-08-04 03:39:02 -0700247 .nlgroup = RTNLGRP_IPV6_RULE,
248 .policy = fib6_rule_policy,
Thomas Graf101367c2006-08-04 03:39:02 -0700249 .owner = THIS_MODULE,
Denis V. Lunev03592382008-01-20 16:46:01 -0800250 .fro_net = &init_net,
Thomas Graf101367c2006-08-04 03:39:02 -0700251};
252
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800253static int fib6_rules_net_init(struct net *net)
Denis V. Lunev2994c632007-11-10 22:12:03 -0800254{
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800255 int err = -ENOMEM;
Denis V. Lunev2994c632007-11-10 22:12:03 -0800256
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800257 net->ipv6.fib6_rules_ops = kmemdup(&fib6_rules_ops_template,
258 sizeof(*net->ipv6.fib6_rules_ops),
259 GFP_KERNEL);
260 if (!net->ipv6.fib6_rules_ops)
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800261 goto out;
262
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800263 net->ipv6.fib6_rules_ops->fro_net = net;
264 INIT_LIST_HEAD(&net->ipv6.fib6_rules_ops->rules_list);
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800265
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800266 err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 0,
267 RT6_TABLE_LOCAL, FIB_RULE_PERMANENT);
268 if (err)
269 goto out_fib6_rules_ops;
270
271 err = fib_default_rule_add(net->ipv6.fib6_rules_ops,
272 0x7FFE, RT6_TABLE_MAIN, 0);
273 if (err)
274 goto out_fib6_default_rule_add;
275
276 err = fib_rules_register(net->ipv6.fib6_rules_ops);
277 if (err)
278 goto out_fib6_default_rule_add;
279out:
280 return err;
281
282out_fib6_default_rule_add:
283 fib_rules_cleanup_ops(net->ipv6.fib6_rules_ops);
284out_fib6_rules_ops:
285 kfree(net->ipv6.fib6_rules_ops);
Daniel Lezcano9eb87f32007-12-07 00:42:52 -0800286 goto out;
Thomas Graf101367c2006-08-04 03:39:02 -0700287}
288
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800289static void fib6_rules_net_exit(struct net *net)
290{
291 fib_rules_unregister(net->ipv6.fib6_rules_ops);
292 kfree(net->ipv6.fib6_rules_ops);
293}
294
295static struct pernet_operations fib6_rules_net_ops = {
296 .init = fib6_rules_net_init,
297 .exit = fib6_rules_net_exit,
298};
299
300int __init fib6_rules_init(void)
301{
302 return register_pernet_subsys(&fib6_rules_net_ops);
303}
304
305
Thomas Graf101367c2006-08-04 03:39:02 -0700306void fib6_rules_cleanup(void)
307{
Daniel Lezcanodcabb812008-03-03 23:33:08 -0800308 return unregister_pernet_subsys(&fib6_rules_net_ops);
Thomas Graf101367c2006-08-04 03:39:02 -0700309}