17c128a6bSYonghong Song // SPDX-License-Identifier: GPL-2.0
27c128a6bSYonghong Song /* Copyright (c) 2020 Facebook */
384544f56SYonghong Song #include "bpf_iter.h"
4647b502eSYonghong Song #include "bpf_tracing_net.h"
57c128a6bSYonghong Song #include <bpf/bpf_helpers.h>
67c128a6bSYonghong Song 
77c128a6bSYonghong Song char _license[] SEC("license") = "GPL";
87c128a6bSYonghong Song 
97c128a6bSYonghong Song extern bool CONFIG_IPV6_SUBTREES __kconfig __weak;
107c128a6bSYonghong Song 
117c128a6bSYonghong Song SEC("iter/ipv6_route")
dump_ipv6_route(struct bpf_iter__ipv6_route * ctx)127c128a6bSYonghong Song int dump_ipv6_route(struct bpf_iter__ipv6_route *ctx)
137c128a6bSYonghong Song {
147c128a6bSYonghong Song 	struct seq_file *seq = ctx->meta->seq;
157c128a6bSYonghong Song 	struct fib6_info *rt = ctx->rt;
167c128a6bSYonghong Song 	const struct net_device *dev;
177c128a6bSYonghong Song 	struct fib6_nh *fib6_nh;
187c128a6bSYonghong Song 	unsigned int flags;
197c128a6bSYonghong Song 	struct nexthop *nh;
207c128a6bSYonghong Song 
217c128a6bSYonghong Song 	if (rt == (void *)0)
227c128a6bSYonghong Song 		return 0;
237c128a6bSYonghong Song 
247c128a6bSYonghong Song 	fib6_nh = &rt->fib6_nh[0];
257c128a6bSYonghong Song 	flags = rt->fib6_flags;
267c128a6bSYonghong Song 
277c128a6bSYonghong Song 	/* FIXME: nexthop_is_multipath is not handled here. */
287c128a6bSYonghong Song 	nh = rt->nh;
297c128a6bSYonghong Song 	if (rt->nh)
307c128a6bSYonghong Song 		fib6_nh = &nh->nh_info->fib6_nh;
317c128a6bSYonghong Song 
327c128a6bSYonghong Song 	BPF_SEQ_PRINTF(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
337c128a6bSYonghong Song 
347c128a6bSYonghong Song 	if (CONFIG_IPV6_SUBTREES)
357c128a6bSYonghong Song 		BPF_SEQ_PRINTF(seq, "%pi6 %02x ", &rt->fib6_src.addr,
367c128a6bSYonghong Song 			       rt->fib6_src.plen);
377c128a6bSYonghong Song 	else
387c128a6bSYonghong Song 		BPF_SEQ_PRINTF(seq, "00000000000000000000000000000000 00 ");
397c128a6bSYonghong Song 
407c128a6bSYonghong Song 	if (fib6_nh->fib_nh_gw_family) {
417c128a6bSYonghong Song 		flags |= RTF_GATEWAY;
427c128a6bSYonghong Song 		BPF_SEQ_PRINTF(seq, "%pi6 ", &fib6_nh->fib_nh_gw6);
437c128a6bSYonghong Song 	} else {
447c128a6bSYonghong Song 		BPF_SEQ_PRINTF(seq, "00000000000000000000000000000000 ");
457c128a6bSYonghong Song 	}
467c128a6bSYonghong Song 
477c128a6bSYonghong Song 	dev = fib6_nh->fib_nh_dev;
487c128a6bSYonghong Song 	if (dev)
497c128a6bSYonghong Song 		BPF_SEQ_PRINTF(seq, "%08x %08x %08x %08x %8s\n", rt->fib6_metric,
507c128a6bSYonghong Song 			       rt->fib6_ref.refs.counter, 0, flags, dev->name);
517c128a6bSYonghong Song 	else
527c128a6bSYonghong Song 		BPF_SEQ_PRINTF(seq, "%08x %08x %08x %08x\n", rt->fib6_metric,
537c128a6bSYonghong Song 			       rt->fib6_ref.refs.counter, 0, flags);
547c128a6bSYonghong Song 
557c128a6bSYonghong Song 	return 0;
567c128a6bSYonghong Song }
57