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