arc: dts: use 'atmel' as manufacturer for at24 in axs10x_mb
[linux/fpc-iii.git] / net / ipv6 / fib6_rules.c
blobb240f24a6e523c387cfc3320ac056a2544f8cffe
1 /*
2 * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules
4 * Copyright (C)2003-2006 Helsinki University of Technology
5 * Copyright (C)2003-2006 USAGI/WIDE Project
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.
11 * Authors
12 * Thomas Graf <tgraf@suug.ch>
13 * Ville Nuorvala <vnuorval@tcs.hut.fi>
16 #include <linux/netdevice.h>
17 #include <linux/notifier.h>
18 #include <linux/export.h>
20 #include <net/fib_rules.h>
21 #include <net/ipv6.h>
22 #include <net/addrconf.h>
23 #include <net/ip6_route.h>
24 #include <net/netlink.h>
26 struct fib6_rule {
27 struct fib_rule common;
28 struct rt6key src;
29 struct rt6key dst;
30 u8 tclass;
33 static bool fib6_rule_matchall(const struct fib_rule *rule)
35 struct fib6_rule *r = container_of(rule, struct fib6_rule, common);
37 if (r->dst.plen || r->src.plen || r->tclass)
38 return false;
39 return fib_rule_matchall(rule);
42 bool fib6_rule_default(const struct fib_rule *rule)
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;
51 EXPORT_SYMBOL_GPL(fib6_rule_default);
53 int fib6_rules_dump(struct net *net, struct notifier_block *nb)
55 return fib_rules_dump(net, nb, AF_INET6);
58 unsigned int fib6_rules_seq_read(struct net *net)
60 return fib_rules_seq_read(net, AF_INET6);
63 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
64 int flags, pol_lookup_t lookup)
66 if (net->ipv6.fib6_has_custom_rules) {
67 struct fib_lookup_arg arg = {
68 .lookup_ptr = lookup,
69 .flags = FIB_LOOKUP_NOREF,
72 /* update flow if oif or iif point to device enslaved to l3mdev */
73 l3mdev_update_flow(net, flowi6_to_flowi(fl6));
75 fib_rules_lookup(net->ipv6.fib6_rules_ops,
76 flowi6_to_flowi(fl6), flags, &arg);
78 if (arg.result)
79 return arg.result;
80 } else {
81 struct rt6_info *rt;
83 rt = lookup(net, net->ipv6.fib6_local_tbl, fl6, flags);
84 if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
85 return &rt->dst;
86 ip6_rt_put(rt);
87 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
88 if (rt->dst.error != -EAGAIN)
89 return &rt->dst;
90 ip6_rt_put(rt);
93 dst_hold(&net->ipv6.ip6_null_entry->dst);
94 return &net->ipv6.ip6_null_entry->dst;
97 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
98 int flags, struct fib_lookup_arg *arg)
100 struct flowi6 *flp6 = &flp->u.ip6;
101 struct rt6_info *rt = NULL;
102 struct fib6_table *table;
103 struct net *net = rule->fr_net;
104 pol_lookup_t lookup = arg->lookup_ptr;
105 int err = 0;
106 u32 tb_id;
108 switch (rule->action) {
109 case FR_ACT_TO_TBL:
110 break;
111 case FR_ACT_UNREACHABLE:
112 err = -ENETUNREACH;
113 rt = net->ipv6.ip6_null_entry;
114 goto discard_pkt;
115 default:
116 case FR_ACT_BLACKHOLE:
117 err = -EINVAL;
118 rt = net->ipv6.ip6_blk_hole_entry;
119 goto discard_pkt;
120 case FR_ACT_PROHIBIT:
121 err = -EACCES;
122 rt = net->ipv6.ip6_prohibit_entry;
123 goto discard_pkt;
126 tb_id = fib_rule_get_table(rule, arg);
127 table = fib6_get_table(net, tb_id);
128 if (!table) {
129 err = -EAGAIN;
130 goto out;
133 rt = lookup(net, table, flp6, flags);
134 if (rt != net->ipv6.ip6_null_entry) {
135 struct fib6_rule *r = (struct fib6_rule *)rule;
138 * If we need to find a source address for this traffic,
139 * we check the result if it meets requirement of the rule.
141 if ((rule->flags & FIB_RULE_FIND_SADDR) &&
142 r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
143 struct in6_addr saddr;
145 if (ipv6_dev_get_saddr(net,
146 ip6_dst_idev(&rt->dst)->dev,
147 &flp6->daddr,
148 rt6_flags2srcprefs(flags),
149 &saddr))
150 goto again;
151 if (!ipv6_prefix_equal(&saddr, &r->src.addr,
152 r->src.plen))
153 goto again;
154 flp6->saddr = saddr;
156 err = rt->dst.error;
157 if (err != -EAGAIN)
158 goto out;
160 again:
161 ip6_rt_put(rt);
162 err = -EAGAIN;
163 rt = NULL;
164 goto out;
166 discard_pkt:
167 dst_hold(&rt->dst);
168 out:
169 arg->result = rt;
170 return err;
173 static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
175 struct rt6_info *rt = (struct rt6_info *) arg->result;
176 struct net_device *dev = NULL;
178 if (rt->rt6i_idev)
179 dev = rt->rt6i_idev->dev;
181 /* do not accept result if the route does
182 * not meet the required prefix length
184 if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
185 goto suppress_route;
187 /* do not accept result if the route uses a device
188 * belonging to a forbidden interface group
190 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
191 goto suppress_route;
193 return false;
195 suppress_route:
196 ip6_rt_put(rt);
197 return true;
200 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
202 struct fib6_rule *r = (struct fib6_rule *) rule;
203 struct flowi6 *fl6 = &fl->u.ip6;
205 if (r->dst.plen &&
206 !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
207 return 0;
210 * If FIB_RULE_FIND_SADDR is set and we do not have a
211 * source address for the traffic, we defer check for
212 * source address.
214 if (r->src.plen) {
215 if (flags & RT6_LOOKUP_F_HAS_SADDR) {
216 if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
217 r->src.plen))
218 return 0;
219 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
220 return 0;
223 if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
224 return 0;
226 return 1;
229 static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
230 FRA_GENERIC_POLICY,
233 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
234 struct fib_rule_hdr *frh,
235 struct nlattr **tb)
237 int err = -EINVAL;
238 struct net *net = sock_net(skb->sk);
239 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
241 if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
242 if (rule->table == RT6_TABLE_UNSPEC)
243 goto errout;
245 if (fib6_new_table(net, rule->table) == NULL) {
246 err = -ENOBUFS;
247 goto errout;
251 if (frh->src_len)
252 rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
254 if (frh->dst_len)
255 rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
257 rule6->src.plen = frh->src_len;
258 rule6->dst.plen = frh->dst_len;
259 rule6->tclass = frh->tos;
261 net->ipv6.fib6_has_custom_rules = true;
262 err = 0;
263 errout:
264 return err;
267 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
268 struct nlattr **tb)
270 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
272 if (frh->src_len && (rule6->src.plen != frh->src_len))
273 return 0;
275 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
276 return 0;
278 if (frh->tos && (rule6->tclass != frh->tos))
279 return 0;
281 if (frh->src_len &&
282 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
283 return 0;
285 if (frh->dst_len &&
286 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
287 return 0;
289 return 1;
292 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
293 struct fib_rule_hdr *frh)
295 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
297 frh->dst_len = rule6->dst.plen;
298 frh->src_len = rule6->src.plen;
299 frh->tos = rule6->tclass;
301 if ((rule6->dst.plen &&
302 nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
303 (rule6->src.plen &&
304 nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
305 goto nla_put_failure;
306 return 0;
308 nla_put_failure:
309 return -ENOBUFS;
312 static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
314 return nla_total_size(16) /* dst */
315 + nla_total_size(16); /* src */
318 static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
319 .family = AF_INET6,
320 .rule_size = sizeof(struct fib6_rule),
321 .addr_size = sizeof(struct in6_addr),
322 .action = fib6_rule_action,
323 .match = fib6_rule_match,
324 .suppress = fib6_rule_suppress,
325 .configure = fib6_rule_configure,
326 .compare = fib6_rule_compare,
327 .fill = fib6_rule_fill,
328 .nlmsg_payload = fib6_rule_nlmsg_payload,
329 .nlgroup = RTNLGRP_IPV6_RULE,
330 .policy = fib6_rule_policy,
331 .owner = THIS_MODULE,
332 .fro_net = &init_net,
335 static int __net_init fib6_rules_net_init(struct net *net)
337 struct fib_rules_ops *ops;
338 int err = -ENOMEM;
340 ops = fib_rules_register(&fib6_rules_ops_template, net);
341 if (IS_ERR(ops))
342 return PTR_ERR(ops);
344 err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
345 if (err)
346 goto out_fib6_rules_ops;
348 err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
349 if (err)
350 goto out_fib6_rules_ops;
352 net->ipv6.fib6_rules_ops = ops;
353 out:
354 return err;
356 out_fib6_rules_ops:
357 fib_rules_unregister(ops);
358 goto out;
361 static void __net_exit fib6_rules_net_exit(struct net *net)
363 rtnl_lock();
364 fib_rules_unregister(net->ipv6.fib6_rules_ops);
365 rtnl_unlock();
368 static struct pernet_operations fib6_rules_net_ops = {
369 .init = fib6_rules_net_init,
370 .exit = fib6_rules_net_exit,
373 int __init fib6_rules_init(void)
375 return register_pernet_subsys(&fib6_rules_net_ops);
379 void fib6_rules_cleanup(void)
381 unregister_pernet_subsys(&fib6_rules_net_ops);