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