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