blob: 8284c645f55ecdff4ca0ed1c62ba00638f56938d [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules
4 *
5 * Copyright (C)2003-2006 Helsinki University of Technology
6 * Copyright (C)2003-2006 USAGI/WIDE Project
7 *
8 * Authors
9 * Thomas Graf <tgraf@suug.ch>
10 * Ville Nuorvala <vnuorval@tcs.hut.fi>
11 */
12
13#include <linux/netdevice.h>
14#include <linux/notifier.h>
15#include <linux/export.h>
16
17#include <net/fib_rules.h>
18#include <net/ipv6.h>
19#include <net/addrconf.h>
20#include <net/ip6_route.h>
21#include <net/netlink.h>
22
23struct fib6_rule {
24 struct fib_rule common;
25 struct rt6key src;
26 struct rt6key dst;
27 u8 tclass;
28};
29
30static bool fib6_rule_matchall(const struct fib_rule *rule)
31{
32 struct fib6_rule *r = container_of(rule, struct fib6_rule, common);
33
34 if (r->dst.plen || r->src.plen || r->tclass)
35 return false;
36 return fib_rule_matchall(rule);
37}
38
39bool fib6_rule_default(const struct fib_rule *rule)
40{
41 if (!fib6_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
42 rule->l3mdev)
43 return false;
44 if (rule->table != RT6_TABLE_LOCAL && rule->table != RT6_TABLE_MAIN)
45 return false;
46 return true;
47}
48EXPORT_SYMBOL_GPL(fib6_rule_default);
49
50int fib6_rules_dump(struct net *net, struct notifier_block *nb)
51{
52 return fib_rules_dump(net, nb, AF_INET6);
53}
54
55unsigned int fib6_rules_seq_read(struct net *net)
56{
57 return fib_rules_seq_read(net, AF_INET6);
58}
59
60/* called with rcu lock held; no reference taken on fib6_info */
61int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
62 struct fib6_result *res, int flags)
63{
64 int err;
65
66 if (net->ipv6.fib6_has_custom_rules) {
67 struct fib_lookup_arg arg = {
68 .lookup_ptr = fib6_table_lookup,
69 .lookup_data = &oif,
70 .result = res,
71 .flags = FIB_LOOKUP_NOREF,
72 };
73
74 l3mdev_update_flow(net, flowi6_to_flowi(fl6));
75
76 err = fib_rules_lookup(net->ipv6.fib6_rules_ops,
77 flowi6_to_flowi(fl6), flags, &arg);
78 } else {
79 err = fib6_table_lookup(net, net->ipv6.fib6_local_tbl, oif,
80 fl6, res, flags);
81 if (err || res->f6i == net->ipv6.fib6_null_entry)
82 err = fib6_table_lookup(net, net->ipv6.fib6_main_tbl,
83 oif, fl6, res, flags);
84 }
85
86 return err;
87}
88
89
90int fib6_rule_lookup_fastpath(struct net *net, struct flowi6 *fl6,
91 struct fib6_result *res,
92 int flags, pol_lookup_fastpath_t lookup_fp)
93{
94 int err;
95
96 if (net->ipv6.fib6_has_custom_rules) {
97 struct fib_lookup_arg arg = {
98 .lookup_ptr = lookup_fp,
99 .lookup_data = NULL,
100 .result = res,
101 .flags = FIB_LOOKUP_NOREF,
102 };
103
104 /* update flow if oif or iif point to device enslaved to l3mdev */
105 l3mdev_update_flow(net, flowi6_to_flowi(fl6));
106
107 err = fib_rules_lookup(net->ipv6.fib6_rules_ops,
108 flowi6_to_flowi(fl6), flags, &arg);
109
110 if (!err && res->f6i == net->ipv6.fib6_null_entry) {
111 res->nh->fib_nh_dev = net->ipv6.ip6_null_entry->dst.dev;
112 res->fib6_flags = net->ipv6.ip6_null_entry->rt6i_flags;
113 return 0;
114 }
115 } else {
116 err = lookup_fp(net, net->ipv6.fib6_local_tbl, fl6, res, flags);
117 if (err || res->f6i == net->ipv6.fib6_null_entry)
118 err = lookup_fp(net, net->ipv6.fib6_main_tbl, fl6, res, flags);
119 }
120
121 return err;
122}
123
124struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
125 const struct sk_buff *skb,
126 int flags, pol_lookup_t lookup)
127{
128 if (net->ipv6.fib6_has_custom_rules) {
129 struct fib6_result res = {};
130 struct fib_lookup_arg arg = {
131 .lookup_ptr = lookup,
132 .lookup_data = skb,
133 .result = &res,
134 .flags = FIB_LOOKUP_NOREF,
135 };
136
137 /* update flow if oif or iif point to device enslaved to l3mdev */
138 l3mdev_update_flow(net, flowi6_to_flowi(fl6));
139
140 fib_rules_lookup(net->ipv6.fib6_rules_ops,
141 flowi6_to_flowi(fl6), flags, &arg);
142
143 if (res.rt6)
144 return &res.rt6->dst;
145 } else {
146 struct rt6_info *rt;
147
148 rt = lookup(net, net->ipv6.fib6_local_tbl, fl6, skb, flags);
149 if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
150 return &rt->dst;
151 ip6_rt_put_flags(rt, flags);
152 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
153 if (rt->dst.error != -EAGAIN)
154 return &rt->dst;
155 ip6_rt_put_flags(rt, flags);
156 }
157
158 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
159 dst_hold(&net->ipv6.ip6_null_entry->dst);
160 return &net->ipv6.ip6_null_entry->dst;
161}
162
163static int fib6_rule_saddr(struct net *net, struct fib_rule *rule, int flags,
164 struct flowi6 *flp6, const struct net_device *dev)
165{
166 struct fib6_rule *r = (struct fib6_rule *)rule;
167
168 /* If we need to find a source address for this traffic,
169 * we check the result if it meets requirement of the rule.
170 */
171 if ((rule->flags & FIB_RULE_FIND_SADDR) &&
172 r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
173 struct in6_addr saddr;
174
175 if (ipv6_dev_get_saddr(net, dev, &flp6->daddr,
176 rt6_flags2srcprefs(flags), &saddr))
177 return -EAGAIN;
178
179 if (!ipv6_prefix_equal(&saddr, &r->src.addr, r->src.plen))
180 return -EAGAIN;
181
182 flp6->saddr = saddr;
183 }
184
185 return 0;
186}
187
188static int fib6_rule_action_fastpath(struct fib_rule *rule, struct flowi *flp,
189 int flags, struct fib_lookup_arg *arg)
190{
191 struct fib6_result *res = arg->result;
192 struct flowi6 *flp6 = &flp->u.ip6;
193 struct fib6_table *table;
194 struct net *net = rule->fr_net;
195 pol_lookup_fastpath_t lookup = arg->lookup_ptr;
196 int err = 0;
197 u32 tb_id;
198
199 switch (rule->action) {
200 case FR_ACT_TO_TBL:
201 break;
202 case FR_ACT_UNREACHABLE:
203 return -ENETUNREACH;
204 case FR_ACT_PROHIBIT:
205 case FR_ACT_POLICY_FAILED:
206 return -EACCES;
207 case FR_ACT_BLACKHOLE:
208 default:
209 return -EINVAL;
210 }
211
212 tb_id = fib_rule_get_table(rule, arg);
213 table = fib6_get_table(net, tb_id);
214 if (!table)
215 return -EAGAIN;
216
217 err = lookup(net, table, flp6, res, flags);
218 if (!err && res->f6i != net->ipv6.fib6_null_entry)
219 err = fib6_rule_saddr(net, rule, flags, flp6,
220 res->nh->fib_nh_dev);
221 else
222 err = -EAGAIN;
223
224 return err;
225}
226
227static int fib6_rule_action_alt(struct fib_rule *rule, struct flowi *flp,
228 int flags, struct fib_lookup_arg *arg)
229{
230 struct fib6_result *res = arg->result;
231 struct flowi6 *flp6 = &flp->u.ip6;
232 struct net *net = rule->fr_net;
233 struct fib6_table *table;
234 int err, *oif;
235 u32 tb_id;
236
237 switch (rule->action) {
238 case FR_ACT_TO_TBL:
239 break;
240 case FR_ACT_UNREACHABLE:
241 return -ENETUNREACH;
242 case FR_ACT_PROHIBIT:
243 return -EACCES;
244 case FR_ACT_BLACKHOLE:
245 default:
246 return -EINVAL;
247 }
248
249 tb_id = fib_rule_get_table(rule, arg);
250 table = fib6_get_table(net, tb_id);
251 if (!table)
252 return -EAGAIN;
253
254 oif = (int *)arg->lookup_data;
255 err = fib6_table_lookup(net, table, *oif, flp6, res, flags);
256 if (!err && res->f6i != net->ipv6.fib6_null_entry)
257 err = fib6_rule_saddr(net, rule, flags, flp6,
258 res->nh->fib_nh_dev);
259 else
260 err = -EAGAIN;
261
262 return err;
263}
264
265static int __fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
266 int flags, struct fib_lookup_arg *arg)
267{
268 struct fib6_result *res = arg->result;
269 struct flowi6 *flp6 = &flp->u.ip6;
270 struct rt6_info *rt = NULL;
271 struct fib6_table *table;
272 struct net *net = rule->fr_net;
273 pol_lookup_t lookup = arg->lookup_ptr;
274 int err = 0;
275 u32 tb_id;
276
277 switch (rule->action) {
278 case FR_ACT_TO_TBL:
279 break;
280 case FR_ACT_UNREACHABLE:
281 err = -ENETUNREACH;
282 rt = net->ipv6.ip6_null_entry;
283 goto discard_pkt;
284 default:
285 case FR_ACT_BLACKHOLE:
286 err = -EINVAL;
287 rt = net->ipv6.ip6_blk_hole_entry;
288 goto discard_pkt;
289 case FR_ACT_PROHIBIT:
290 err = -EACCES;
291 rt = net->ipv6.ip6_prohibit_entry;
292 goto discard_pkt;
293 case FR_ACT_POLICY_FAILED:
294 err = -EACCES;
295 rt = net->ipv6.ip6_policy_failed_entry;
296 goto discard_pkt;
297 }
298
299 tb_id = fib_rule_get_table(rule, arg);
300 table = fib6_get_table(net, tb_id);
301 if (!table) {
302 err = -EAGAIN;
303 goto out;
304 }
305
306 rt = lookup(net, table, flp6, arg->lookup_data, flags);
307 if (rt != net->ipv6.ip6_null_entry) {
308 struct inet6_dev *idev = ip6_dst_idev(&rt->dst);
309
310 if (!idev)
311 goto again;
312 err = fib6_rule_saddr(net, rule, flags, flp6,
313 idev->dev);
314
315 if (err == -EAGAIN)
316 goto again;
317
318 err = rt->dst.error;
319 if (err != -EAGAIN)
320 goto out;
321 }
322again:
323 ip6_rt_put_flags(rt, flags);
324 err = -EAGAIN;
325 rt = NULL;
326 goto out;
327
328discard_pkt:
329 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
330 dst_hold(&rt->dst);
331out:
332 res->rt6 = rt;
333 return err;
334}
335
336static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
337 int flags, struct fib_lookup_arg *arg)
338{
339 if (arg->lookup_ptr == fib6_table_lookup)
340 return fib6_rule_action_alt(rule, flp, flags, arg);
341 else if (arg->lookup_ptr == ip6_pol_route_lookup_fastpath)
342 return fib6_rule_action_fastpath(rule, flp, flags, arg);
343
344 return __fib6_rule_action(rule, flp, flags, arg);
345}
346
347static bool fib6_rule_suppress(struct fib_rule *rule, int flags, struct fib_lookup_arg *arg)
348{
349 struct fib6_result *res = arg->result;
350 struct rt6_info *rt = res->rt6;
351 struct net_device *dev = NULL;
352
353 if (!rt)
354 return false;
355
356 if (rt->rt6i_idev)
357 dev = rt->rt6i_idev->dev;
358
359 /* do not accept result if the route does
360 * not meet the required prefix length
361 */
362 if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
363 goto suppress_route;
364
365 /* do not accept result if the route uses a device
366 * belonging to a forbidden interface group
367 */
368 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
369 goto suppress_route;
370
371 return false;
372
373suppress_route:
374 ip6_rt_put_flags(rt, flags);
375 return true;
376}
377
378static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
379{
380 struct fib6_rule *r = (struct fib6_rule *) rule;
381 struct flowi6 *fl6 = &fl->u.ip6;
382
383 if (r->dst.plen &&
384 !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
385 return 0;
386
387 /*
388 * If FIB_RULE_FIND_SADDR is set and we do not have a
389 * source address for the traffic, we defer check for
390 * source address.
391 */
392 if (r->src.plen) {
393 if (flags & RT6_LOOKUP_F_HAS_SADDR) {
394 if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
395 r->src.plen))
396 return 0;
397 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
398 return 0;
399 }
400
401 if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
402 return 0;
403
404 if (rule->ip_proto && (rule->ip_proto != fl6->flowi6_proto))
405 return 0;
406
407 if (fib_rule_port_range_set(&rule->sport_range) &&
408 !fib_rule_port_inrange(&rule->sport_range, fl6->fl6_sport))
409 return 0;
410
411 if (fib_rule_port_range_set(&rule->dport_range) &&
412 !fib_rule_port_inrange(&rule->dport_range, fl6->fl6_dport))
413 return 0;
414
415 return 1;
416}
417
418static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
419 FRA_GENERIC_POLICY,
420};
421
422static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
423 struct fib_rule_hdr *frh,
424 struct nlattr **tb,
425 struct netlink_ext_ack *extack)
426{
427 int err = -EINVAL;
428 struct net *net = sock_net(skb->sk);
429 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
430
431 if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
432 if (rule->table == RT6_TABLE_UNSPEC) {
433 NL_SET_ERR_MSG(extack, "Invalid table");
434 goto errout;
435 }
436
437 if (fib6_new_table(net, rule->table) == NULL) {
438 err = -ENOBUFS;
439 goto errout;
440 }
441 }
442
443 if (frh->src_len)
444 rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
445
446 if (frh->dst_len)
447 rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
448
449 rule6->src.plen = frh->src_len;
450 rule6->dst.plen = frh->dst_len;
451 rule6->tclass = frh->tos;
452
453 if (fib_rule_requires_fldissect(rule))
454 net->ipv6.fib6_rules_require_fldissect++;
455
456 net->ipv6.fib6_has_custom_rules = true;
457 err = 0;
458errout:
459 return err;
460}
461
462static int fib6_rule_delete(struct fib_rule *rule)
463{
464 struct net *net = rule->fr_net;
465
466 if (net->ipv6.fib6_rules_require_fldissect &&
467 fib_rule_requires_fldissect(rule))
468 net->ipv6.fib6_rules_require_fldissect--;
469
470 return 0;
471}
472
473static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
474 struct nlattr **tb)
475{
476 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
477
478 if (frh->src_len && (rule6->src.plen != frh->src_len))
479 return 0;
480
481 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
482 return 0;
483
484 if (frh->tos && (rule6->tclass != frh->tos))
485 return 0;
486
487 if (frh->src_len &&
488 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
489 return 0;
490
491 if (frh->dst_len &&
492 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
493 return 0;
494
495 return 1;
496}
497
498static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
499 struct fib_rule_hdr *frh)
500{
501 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
502
503 frh->dst_len = rule6->dst.plen;
504 frh->src_len = rule6->src.plen;
505 frh->tos = rule6->tclass;
506
507 if ((rule6->dst.plen &&
508 nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
509 (rule6->src.plen &&
510 nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
511 goto nla_put_failure;
512 return 0;
513
514nla_put_failure:
515 return -ENOBUFS;
516}
517
518static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
519{
520 return nla_total_size(16) /* dst */
521 + nla_total_size(16); /* src */
522}
523
524static void fib6_rule_flush_cache(struct fib_rules_ops *ops)
525{
526 rt_genid_bump_ipv6(ops->fro_net);
527}
528
529static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
530 .family = AF_INET6,
531 .rule_size = sizeof(struct fib6_rule),
532 .addr_size = sizeof(struct in6_addr),
533 .action = fib6_rule_action,
534 .match = fib6_rule_match,
535 .suppress = fib6_rule_suppress,
536 .configure = fib6_rule_configure,
537 .delete = fib6_rule_delete,
538 .compare = fib6_rule_compare,
539 .fill = fib6_rule_fill,
540 .nlmsg_payload = fib6_rule_nlmsg_payload,
541 .flush_cache = fib6_rule_flush_cache,
542 .nlgroup = RTNLGRP_IPV6_RULE,
543 .policy = fib6_rule_policy,
544 .owner = THIS_MODULE,
545 .fro_net = &init_net,
546};
547
548static int __net_init fib6_rules_net_init(struct net *net)
549{
550 struct fib_rules_ops *ops;
551 int err = -ENOMEM;
552
553 ops = fib_rules_register(&fib6_rules_ops_template, net);
554 if (IS_ERR(ops))
555 return PTR_ERR(ops);
556
557 err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
558 if (err)
559 goto out_fib6_rules_ops;
560
561 err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
562 if (err)
563 goto out_fib6_rules_ops;
564
565 net->ipv6.fib6_rules_ops = ops;
566 net->ipv6.fib6_rules_require_fldissect = 0;
567out:
568 return err;
569
570out_fib6_rules_ops:
571 fib_rules_unregister(ops);
572 goto out;
573}
574
575static void __net_exit fib6_rules_net_exit(struct net *net)
576{
577 rtnl_lock();
578 fib_rules_unregister(net->ipv6.fib6_rules_ops);
579 rtnl_unlock();
580}
581
582static struct pernet_operations fib6_rules_net_ops = {
583 .init = fib6_rules_net_init,
584 .exit = fib6_rules_net_exit,
585};
586
587int __init fib6_rules_init(void)
588{
589 return register_pernet_subsys(&fib6_rules_net_ops);
590}
591
592
593void fib6_rules_cleanup(void)
594{
595 unregister_pernet_subsys(&fib6_rules_net_ops);
596}