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