isdn: hfcsusb: constify usb_device_id
[linux-2.6-block.git] / net / ipv6 / fib6_rules.c
CommitLineData
101367c2
TG
1/*
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
101367c2 16#include <linux/netdevice.h>
dcb18f76 17#include <linux/notifier.h>
bc3b2d7f 18#include <linux/export.h>
101367c2
TG
19
20#include <net/fib_rules.h>
21#include <net/ipv6.h>
29f6af77 22#include <net/addrconf.h>
101367c2
TG
23#include <net/ip6_route.h>
24#include <net/netlink.h>
25
911c8541 26struct fib6_rule {
101367c2
TG
27 struct fib_rule common;
28 struct rt6key src;
29 struct rt6key dst;
30 u8 tclass;
31};
32
e3ea9731
IS
33static bool fib6_rule_matchall(const struct fib_rule *rule)
34{
35 struct fib6_rule *r = container_of(rule, struct fib6_rule, common);
36
37 if (r->dst.plen || r->src.plen || r->tclass)
38 return false;
39 return fib_rule_matchall(rule);
40}
41
42bool fib6_rule_default(const struct fib_rule *rule)
43{
44 if (!fib6_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
45 rule->l3mdev)
46 return false;
47 if (rule->table != RT6_TABLE_LOCAL && rule->table != RT6_TABLE_MAIN)
48 return false;
49 return true;
50}
51EXPORT_SYMBOL_GPL(fib6_rule_default);
52
dcb18f76
IS
53int fib6_rules_dump(struct net *net, struct notifier_block *nb)
54{
55 return fib_rules_dump(net, nb, AF_INET6);
56}
57
58unsigned int fib6_rules_seq_read(struct net *net)
59{
60 return fib_rules_seq_read(net, AF_INET6);
61}
62
4c9483b2 63struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
58f09b78 64 int flags, pol_lookup_t lookup)
101367c2
TG
65{
66 struct fib_lookup_arg arg = {
67 .lookup_ptr = lookup,
2c1c0004 68 .flags = FIB_LOOKUP_NOREF,
101367c2
TG
69 };
70
9ee0034b
DA
71 /* update flow if oif or iif point to device enslaved to l3mdev */
72 l3mdev_update_flow(net, flowi6_to_flowi(fl6));
73
4c9483b2
DM
74 fib_rules_lookup(net->ipv6.fib6_rules_ops,
75 flowi6_to_flowi(fl6), flags, &arg);
101367c2 76
07f61557
SP
77 if (arg.result)
78 return arg.result;
b1429553 79
07f61557
SP
80 dst_hold(&net->ipv6.ip6_null_entry->dst);
81 return &net->ipv6.ip6_null_entry->dst;
101367c2
TG
82}
83
8ce11e6a
AB
84static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
85 int flags, struct fib_lookup_arg *arg)
101367c2 86{
4c9483b2 87 struct flowi6 *flp6 = &flp->u.ip6;
101367c2
TG
88 struct rt6_info *rt = NULL;
89 struct fib6_table *table;
8ed67789 90 struct net *net = rule->fr_net;
101367c2 91 pol_lookup_t lookup = arg->lookup_ptr;
46b3a421 92 int err = 0;
96c63fa7 93 u32 tb_id;
101367c2
TG
94
95 switch (rule->action) {
96 case FR_ACT_TO_TBL:
97 break;
98 case FR_ACT_UNREACHABLE:
46b3a421 99 err = -ENETUNREACH;
8ed67789 100 rt = net->ipv6.ip6_null_entry;
101367c2
TG
101 goto discard_pkt;
102 default:
103 case FR_ACT_BLACKHOLE:
46b3a421 104 err = -EINVAL;
8ed67789 105 rt = net->ipv6.ip6_blk_hole_entry;
101367c2
TG
106 goto discard_pkt;
107 case FR_ACT_PROHIBIT:
46b3a421 108 err = -EACCES;
8ed67789 109 rt = net->ipv6.ip6_prohibit_entry;
101367c2
TG
110 goto discard_pkt;
111 }
112
96c63fa7
DA
113 tb_id = fib_rule_get_table(rule, arg);
114 table = fib6_get_table(net, tb_id);
46b3a421
HFS
115 if (!table) {
116 err = -EAGAIN;
117 goto out;
118 }
101367c2 119
46b3a421 120 rt = lookup(net, table, flp6, flags);
8ed67789 121 if (rt != net->ipv6.ip6_null_entry) {
29f6af77
YH
122 struct fib6_rule *r = (struct fib6_rule *)rule;
123
124 /*
125 * If we need to find a source address for this traffic,
126 * we check the result if it meets requirement of the rule.
127 */
128 if ((rule->flags & FIB_RULE_FIND_SADDR) &&
129 r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
130 struct in6_addr saddr;
7cbca67c 131
191cd582 132 if (ipv6_dev_get_saddr(net,
d8d1f30b 133 ip6_dst_idev(&rt->dst)->dev,
4c9483b2 134 &flp6->daddr,
0c9a2ac1 135 rt6_flags2srcprefs(flags),
7cbca67c 136 &saddr))
29f6af77
YH
137 goto again;
138 if (!ipv6_prefix_equal(&saddr, &r->src.addr,
139 r->src.plen))
140 goto again;
4e3fd7a0 141 flp6->saddr = saddr;
29f6af77 142 }
73ba57bf 143 err = rt->dst.error;
07f61557
SP
144 if (err != -EAGAIN)
145 goto out;
29f6af77
YH
146 }
147again:
94e187c0 148 ip6_rt_put(rt);
46b3a421 149 err = -EAGAIN;
3226f688
PM
150 rt = NULL;
151 goto out;
152
101367c2 153discard_pkt:
d8d1f30b 154 dst_hold(&rt->dst);
101367c2
TG
155out:
156 arg->result = rt;
46b3a421 157 return err;
101367c2
TG
158}
159
7764a45a
ST
160static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
161{
162 struct rt6_info *rt = (struct rt6_info *) arg->result;
673498b8
ST
163 struct net_device *dev = NULL;
164
165 if (rt->rt6i_idev)
166 dev = rt->rt6i_idev->dev;
167
7764a45a
ST
168 /* do not accept result if the route does
169 * not meet the required prefix length
170 */
73f5698e 171 if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
6ef94cfa
ST
172 goto suppress_route;
173
174 /* do not accept result if the route uses a device
175 * belonging to a forbidden interface group
176 */
177 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
178 goto suppress_route;
179
180 return false;
181
182suppress_route:
04f0888d
ST
183 ip6_rt_put(rt);
184 return true;
7764a45a 185}
101367c2
TG
186
187static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
188{
189 struct fib6_rule *r = (struct fib6_rule *) rule;
4c9483b2 190 struct flowi6 *fl6 = &fl->u.ip6;
101367c2 191
adaa70bb 192 if (r->dst.plen &&
4c9483b2 193 !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
101367c2
TG
194 return 0;
195
29f6af77
YH
196 /*
197 * If FIB_RULE_FIND_SADDR is set and we do not have a
198 * source address for the traffic, we defer check for
199 * source address.
200 */
adaa70bb 201 if (r->src.plen) {
29f6af77 202 if (flags & RT6_LOOKUP_F_HAS_SADDR) {
4c9483b2 203 if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
29f6af77
YH
204 r->src.plen))
205 return 0;
206 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
adaa70bb
TG
207 return 0;
208 }
101367c2 209
d76ed22b 210 if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
2cc67cc7
YH
211 return 0;
212
101367c2
TG
213 return 1;
214}
215
ef7c79ed 216static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
1f6c9557 217 FRA_GENERIC_POLICY,
101367c2
TG
218};
219
220static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
8b3521ee 221 struct fib_rule_hdr *frh,
101367c2
TG
222 struct nlattr **tb)
223{
224 int err = -EINVAL;
3b1e0a65 225 struct net *net = sock_net(skb->sk);
101367c2
TG
226 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
227
96c63fa7 228 if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
101367c2
TG
229 if (rule->table == RT6_TABLE_UNSPEC)
230 goto errout;
231
dcabb819 232 if (fib6_new_table(net, rule->table) == NULL) {
101367c2
TG
233 err = -ENOBUFS;
234 goto errout;
235 }
236 }
237
e1701c68 238 if (frh->src_len)
67b61f6c 239 rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
101367c2 240
e1701c68 241 if (frh->dst_len)
67b61f6c 242 rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
101367c2
TG
243
244 rule6->src.plen = frh->src_len;
245 rule6->dst.plen = frh->dst_len;
246 rule6->tclass = frh->tos;
247
248 err = 0;
249errout:
250 return err;
251}
252
253static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
254 struct nlattr **tb)
255{
256 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
257
258 if (frh->src_len && (rule6->src.plen != frh->src_len))
259 return 0;
260
261 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
262 return 0;
263
264 if (frh->tos && (rule6->tclass != frh->tos))
265 return 0;
266
e1701c68 267 if (frh->src_len &&
101367c2
TG
268 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
269 return 0;
270
e1701c68 271 if (frh->dst_len &&
101367c2
TG
272 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
273 return 0;
274
275 return 1;
276}
277
278static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
04af8cf6 279 struct fib_rule_hdr *frh)
101367c2
TG
280{
281 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
282
101367c2
TG
283 frh->dst_len = rule6->dst.plen;
284 frh->src_len = rule6->src.plen;
285 frh->tos = rule6->tclass;
286
c78679e8 287 if ((rule6->dst.plen &&
930345ea 288 nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
c78679e8 289 (rule6->src.plen &&
930345ea 290 nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
c78679e8 291 goto nla_put_failure;
101367c2
TG
292 return 0;
293
294nla_put_failure:
295 return -ENOBUFS;
296}
297
339bf98f
TG
298static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
299{
300 return nla_total_size(16) /* dst */
301 + nla_total_size(16); /* src */
302}
303
04a6f82c 304static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
25239cee 305 .family = AF_INET6,
101367c2 306 .rule_size = sizeof(struct fib6_rule),
e1701c68 307 .addr_size = sizeof(struct in6_addr),
101367c2
TG
308 .action = fib6_rule_action,
309 .match = fib6_rule_match,
7764a45a 310 .suppress = fib6_rule_suppress,
101367c2
TG
311 .configure = fib6_rule_configure,
312 .compare = fib6_rule_compare,
313 .fill = fib6_rule_fill,
339bf98f 314 .nlmsg_payload = fib6_rule_nlmsg_payload,
101367c2
TG
315 .nlgroup = RTNLGRP_IPV6_RULE,
316 .policy = fib6_rule_policy,
101367c2 317 .owner = THIS_MODULE,
03592383 318 .fro_net = &init_net,
101367c2
TG
319};
320
2c8c1e72 321static int __net_init fib6_rules_net_init(struct net *net)
101367c2 322{
e9c5158a 323 struct fib_rules_ops *ops;
dcabb819 324 int err = -ENOMEM;
2994c638 325
e9c5158a
EB
326 ops = fib_rules_register(&fib6_rules_ops_template, net);
327 if (IS_ERR(ops))
328 return PTR_ERR(ops);
eb5564b8 329
85b99092 330 err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
dcabb819
DL
331 if (err)
332 goto out_fib6_rules_ops;
9eb87f3f 333
85b99092 334 err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
dcabb819 335 if (err)
e9c5158a 336 goto out_fib6_rules_ops;
9eb87f3f 337
85b99092 338 net->ipv6.fib6_rules_ops = ops;
9eb87f3f 339out:
dcabb819 340 return err;
9eb87f3f 341
dcabb819 342out_fib6_rules_ops:
e9c5158a 343 fib_rules_unregister(ops);
9eb87f3f 344 goto out;
101367c2
TG
345}
346
2c8c1e72 347static void __net_exit fib6_rules_net_exit(struct net *net)
dcabb819 348{
419df12f 349 rtnl_lock();
dcabb819 350 fib_rules_unregister(net->ipv6.fib6_rules_ops);
419df12f 351 rtnl_unlock();
dcabb819
DL
352}
353
354static struct pernet_operations fib6_rules_net_ops = {
355 .init = fib6_rules_net_init,
356 .exit = fib6_rules_net_exit,
357};
358
359int __init fib6_rules_init(void)
360{
361 return register_pernet_subsys(&fib6_rules_net_ops);
362}
363
364
101367c2
TG
365void fib6_rules_cleanup(void)
366{
ff4e1fb0 367 unregister_pernet_subsys(&fib6_rules_net_ops);
101367c2 368}