xref: /linux/tools/testing/selftests/bpf/progs/bpf_iter_ipv6_route.c (revision 05ee19c18c2bb3dea69e29219017367c4a77e65a)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 
7 char _license[] SEC("license") = "GPL";
8 
9 extern bool CONFIG_IPV6_SUBTREES __kconfig __weak;
10 
11 #define RTF_GATEWAY		0x0002
12 #define IFNAMSIZ		16
13 #define fib_nh_gw_family	nh_common.nhc_gw_family
14 #define fib_nh_gw6		nh_common.nhc_gw.ipv6
15 #define fib_nh_dev		nh_common.nhc_dev
16 
17 SEC("iter/ipv6_route")
18 int dump_ipv6_route(struct bpf_iter__ipv6_route *ctx)
19 {
20 	struct seq_file *seq = ctx->meta->seq;
21 	struct fib6_info *rt = ctx->rt;
22 	const struct net_device *dev;
23 	struct fib6_nh *fib6_nh;
24 	unsigned int flags;
25 	struct nexthop *nh;
26 
27 	if (rt == (void *)0)
28 		return 0;
29 
30 	fib6_nh = &rt->fib6_nh[0];
31 	flags = rt->fib6_flags;
32 
33 	/* FIXME: nexthop_is_multipath is not handled here. */
34 	nh = rt->nh;
35 	if (rt->nh)
36 		fib6_nh = &nh->nh_info->fib6_nh;
37 
38 	BPF_SEQ_PRINTF(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
39 
40 	if (CONFIG_IPV6_SUBTREES)
41 		BPF_SEQ_PRINTF(seq, "%pi6 %02x ", &rt->fib6_src.addr,
42 			       rt->fib6_src.plen);
43 	else
44 		BPF_SEQ_PRINTF(seq, "00000000000000000000000000000000 00 ");
45 
46 	if (fib6_nh->fib_nh_gw_family) {
47 		flags |= RTF_GATEWAY;
48 		BPF_SEQ_PRINTF(seq, "%pi6 ", &fib6_nh->fib_nh_gw6);
49 	} else {
50 		BPF_SEQ_PRINTF(seq, "00000000000000000000000000000000 ");
51 	}
52 
53 	dev = fib6_nh->fib_nh_dev;
54 	if (dev)
55 		BPF_SEQ_PRINTF(seq, "%08x %08x %08x %08x %8s\n", rt->fib6_metric,
56 			       rt->fib6_ref.refs.counter, 0, flags, dev->name);
57 	else
58 		BPF_SEQ_PRINTF(seq, "%08x %08x %08x %08x\n", rt->fib6_metric,
59 			       rt->fib6_ref.refs.counter, 0, flags);
60 
61 	return 0;
62 }
63