* [PATCH bpf-next] samples/bpf: Decrement ttl in fib forwarding example
@ 2018-05-15 23:20 David Ahern
2018-05-16 17:17 ` Y Song
2018-05-16 20:09 ` Daniel Borkmann
0 siblings, 2 replies; 3+ messages in thread
From: David Ahern @ 2018-05-15 23:20 UTC (permalink / raw)
To: netdev, borkmann, ast; +Cc: David Ahern
Only consider forwarding packets if ttl in received packet is > 1 and
decrement ttl before handing off to bpf_redirect_map.
Signed-off-by: David Ahern <dsahern@gmail.com>
---
samples/bpf/xdp_fwd_kern.c | 39 +++++++++++++++++++++++++++++++--------
1 file changed, 31 insertions(+), 8 deletions(-)
diff --git a/samples/bpf/xdp_fwd_kern.c b/samples/bpf/xdp_fwd_kern.c
index cdf4fc383cc9..4a6be0f87505 100644
--- a/samples/bpf/xdp_fwd_kern.c
+++ b/samples/bpf/xdp_fwd_kern.c
@@ -30,12 +30,24 @@ struct bpf_map_def SEC("maps") tx_port = {
.max_entries = 64,
};
+/* from include/net/ip.h */
+static __always_inline int ip_decrease_ttl(struct iphdr *iph)
+{
+ u32 check = (__force u32)iph->check;
+
+ check += (__force u32)htons(0x0100);
+ iph->check = (__force __sum16)(check + (check >= 0xFFFF));
+ return --iph->ttl;
+}
+
static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct bpf_fib_lookup fib_params;
struct ethhdr *eth = data;
+ struct ipv6hdr *ip6h;
+ struct iphdr *iph;
int out_index;
u16 h_proto;
u64 nh_off;
@@ -48,11 +60,14 @@ static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
h_proto = eth->h_proto;
if (h_proto == htons(ETH_P_IP)) {
- struct iphdr *iph = data + nh_off;
+ iph = data + nh_off;
if (iph + 1 > data_end)
return XDP_DROP;
+ if (iph->ttl <= 1)
+ return XDP_PASS;
+
fib_params.family = AF_INET;
fib_params.tos = iph->tos;
fib_params.l4_protocol = iph->protocol;
@@ -64,19 +79,22 @@ static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
} else if (h_proto == htons(ETH_P_IPV6)) {
struct in6_addr *src = (struct in6_addr *) fib_params.ipv6_src;
struct in6_addr *dst = (struct in6_addr *) fib_params.ipv6_dst;
- struct ipv6hdr *iph = data + nh_off;
- if (iph + 1 > data_end)
+ ip6h = data + nh_off;
+ if (ip6h + 1 > data_end)
return XDP_DROP;
+ if (ip6h->hop_limit <= 1)
+ return XDP_PASS;
+
fib_params.family = AF_INET6;
- fib_params.flowlabel = *(__be32 *)iph & IPV6_FLOWINFO_MASK;
- fib_params.l4_protocol = iph->nexthdr;
+ fib_params.flowlabel = *(__be32 *)ip6h & IPV6_FLOWINFO_MASK;
+ fib_params.l4_protocol = ip6h->nexthdr;
fib_params.sport = 0;
fib_params.dport = 0;
- fib_params.tot_len = ntohs(iph->payload_len);
- *src = iph->saddr;
- *dst = iph->daddr;
+ fib_params.tot_len = ntohs(ip6h->payload_len);
+ *src = ip6h->saddr;
+ *dst = ip6h->daddr;
} else {
return XDP_PASS;
}
@@ -92,6 +110,11 @@ static __always_inline int xdp_fwd_flags(struct xdp_md *ctx, u32 flags)
* forwarding packets are dropped.
*/
if (out_index > 0) {
+ if (h_proto == htons(ETH_P_IP))
+ ip_decrease_ttl(iph);
+ else if (h_proto == htons(ETH_P_IPV6))
+ ip6h->hop_limit--;
+
memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);
memcpy(eth->h_source, fib_params.smac, ETH_ALEN);
return bpf_redirect_map(&tx_port, out_index, 0);
--
2.11.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH bpf-next] samples/bpf: Decrement ttl in fib forwarding example
2018-05-15 23:20 [PATCH bpf-next] samples/bpf: Decrement ttl in fib forwarding example David Ahern
@ 2018-05-16 17:17 ` Y Song
2018-05-16 20:09 ` Daniel Borkmann
1 sibling, 0 replies; 3+ messages in thread
From: Y Song @ 2018-05-16 17:17 UTC (permalink / raw)
To: David Ahern; +Cc: netdev, Daniel Borkmann, Alexei Starovoitov
On Tue, May 15, 2018 at 4:20 PM, David Ahern <dsahern@gmail.com> wrote:
> Only consider forwarding packets if ttl in received packet is > 1 and
> decrement ttl before handing off to bpf_redirect_map.
>
> Signed-off-by: David Ahern <dsahern@gmail.com>
I did not test this patch, but it looks good to me with visual inspection.
Acked-by: Yonghong Song <yhs@fb.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH bpf-next] samples/bpf: Decrement ttl in fib forwarding example
2018-05-15 23:20 [PATCH bpf-next] samples/bpf: Decrement ttl in fib forwarding example David Ahern
2018-05-16 17:17 ` Y Song
@ 2018-05-16 20:09 ` Daniel Borkmann
1 sibling, 0 replies; 3+ messages in thread
From: Daniel Borkmann @ 2018-05-16 20:09 UTC (permalink / raw)
To: David Ahern, netdev, borkmann, ast
On 05/16/2018 01:20 AM, David Ahern wrote:
> Only consider forwarding packets if ttl in received packet is > 1 and
> decrement ttl before handing off to bpf_redirect_map.
>
> Signed-off-by: David Ahern <dsahern@gmail.com>
Looks good, applied to bpf-next, thanks David!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2018-05-16 20:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-15 23:20 [PATCH bpf-next] samples/bpf: Decrement ttl in fib forwarding example David Ahern
2018-05-16 17:17 ` Y Song
2018-05-16 20:09 ` Daniel Borkmann
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.