b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 |
| 2 | * Copyright (c) 2018 Facebook |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program shows how to use bpf_xdp_adjust_tail() by |
| 9 | * generating ICMPv4 "packet to big" (unreachable/ df bit set frag needed |
| 10 | * to be more preice in case of v4)" where receiving packets bigger then |
| 11 | * 600 bytes. |
| 12 | */ |
| 13 | #define KBUILD_MODNAME "foo" |
| 14 | #include <uapi/linux/bpf.h> |
| 15 | #include <linux/in.h> |
| 16 | #include <linux/if_ether.h> |
| 17 | #include <linux/if_packet.h> |
| 18 | #include <linux/if_vlan.h> |
| 19 | #include <linux/ip.h> |
| 20 | #include <linux/icmp.h> |
| 21 | #include "bpf_helpers.h" |
| 22 | |
| 23 | #define DEFAULT_TTL 64 |
| 24 | #define MAX_PCKT_SIZE 600 |
| 25 | #define ICMP_TOOBIG_SIZE 98 |
| 26 | #define ICMP_TOOBIG_PAYLOAD_SIZE 92 |
| 27 | |
| 28 | struct { |
| 29 | __uint(type, BPF_MAP_TYPE_ARRAY); |
| 30 | __type(key, __u32); |
| 31 | __type(value, __u64); |
| 32 | __uint(max_entries, 1); |
| 33 | } icmpcnt SEC(".maps"); |
| 34 | |
| 35 | static __always_inline void count_icmp(void) |
| 36 | { |
| 37 | u64 key = 0; |
| 38 | u64 *icmp_count; |
| 39 | |
| 40 | icmp_count = bpf_map_lookup_elem(&icmpcnt, &key); |
| 41 | if (icmp_count) |
| 42 | *icmp_count += 1; |
| 43 | } |
| 44 | |
| 45 | static __always_inline void swap_mac(void *data, struct ethhdr *orig_eth) |
| 46 | { |
| 47 | struct ethhdr *eth; |
| 48 | |
| 49 | eth = data; |
| 50 | memcpy(eth->h_source, orig_eth->h_dest, ETH_ALEN); |
| 51 | memcpy(eth->h_dest, orig_eth->h_source, ETH_ALEN); |
| 52 | eth->h_proto = orig_eth->h_proto; |
| 53 | } |
| 54 | |
| 55 | static __always_inline __u16 csum_fold_helper(__u32 csum) |
| 56 | { |
| 57 | csum = (csum & 0xffff) + (csum >> 16); |
| 58 | return ~((csum & 0xffff) + (csum >> 16)); |
| 59 | } |
| 60 | |
| 61 | static __always_inline void ipv4_csum(void *data_start, int data_size, |
| 62 | __u32 *csum) |
| 63 | { |
| 64 | *csum = bpf_csum_diff(0, 0, data_start, data_size, *csum); |
| 65 | *csum = csum_fold_helper(*csum); |
| 66 | } |
| 67 | |
| 68 | static __always_inline int send_icmp4_too_big(struct xdp_md *xdp) |
| 69 | { |
| 70 | int headroom = (int)sizeof(struct iphdr) + (int)sizeof(struct icmphdr); |
| 71 | |
| 72 | if (bpf_xdp_adjust_head(xdp, 0 - headroom)) |
| 73 | return XDP_DROP; |
| 74 | void *data = (void *)(long)xdp->data; |
| 75 | void *data_end = (void *)(long)xdp->data_end; |
| 76 | |
| 77 | if (data + (ICMP_TOOBIG_SIZE + headroom) > data_end) |
| 78 | return XDP_DROP; |
| 79 | |
| 80 | struct iphdr *iph, *orig_iph; |
| 81 | struct icmphdr *icmp_hdr; |
| 82 | struct ethhdr *orig_eth; |
| 83 | __u32 csum = 0; |
| 84 | __u64 off = 0; |
| 85 | |
| 86 | orig_eth = data + headroom; |
| 87 | swap_mac(data, orig_eth); |
| 88 | off += sizeof(struct ethhdr); |
| 89 | iph = data + off; |
| 90 | off += sizeof(struct iphdr); |
| 91 | icmp_hdr = data + off; |
| 92 | off += sizeof(struct icmphdr); |
| 93 | orig_iph = data + off; |
| 94 | icmp_hdr->type = ICMP_DEST_UNREACH; |
| 95 | icmp_hdr->code = ICMP_FRAG_NEEDED; |
| 96 | icmp_hdr->un.frag.mtu = htons(MAX_PCKT_SIZE-sizeof(struct ethhdr)); |
| 97 | icmp_hdr->checksum = 0; |
| 98 | ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum); |
| 99 | icmp_hdr->checksum = csum; |
| 100 | iph->ttl = DEFAULT_TTL; |
| 101 | iph->daddr = orig_iph->saddr; |
| 102 | iph->saddr = orig_iph->daddr; |
| 103 | iph->version = 4; |
| 104 | iph->ihl = 5; |
| 105 | iph->protocol = IPPROTO_ICMP; |
| 106 | iph->tos = 0; |
| 107 | iph->tot_len = htons( |
| 108 | ICMP_TOOBIG_SIZE + headroom - sizeof(struct ethhdr)); |
| 109 | iph->check = 0; |
| 110 | csum = 0; |
| 111 | ipv4_csum(iph, sizeof(struct iphdr), &csum); |
| 112 | iph->check = csum; |
| 113 | count_icmp(); |
| 114 | return XDP_TX; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | static __always_inline int handle_ipv4(struct xdp_md *xdp) |
| 119 | { |
| 120 | void *data_end = (void *)(long)xdp->data_end; |
| 121 | void *data = (void *)(long)xdp->data; |
| 122 | int pckt_size = data_end - data; |
| 123 | int offset; |
| 124 | |
| 125 | if (pckt_size > MAX_PCKT_SIZE) { |
| 126 | offset = pckt_size - ICMP_TOOBIG_SIZE; |
| 127 | if (bpf_xdp_adjust_tail(xdp, 0 - offset)) |
| 128 | return XDP_PASS; |
| 129 | return send_icmp4_too_big(xdp); |
| 130 | } |
| 131 | return XDP_PASS; |
| 132 | } |
| 133 | |
| 134 | SEC("xdp_icmp") |
| 135 | int _xdp_icmp(struct xdp_md *xdp) |
| 136 | { |
| 137 | void *data_end = (void *)(long)xdp->data_end; |
| 138 | void *data = (void *)(long)xdp->data; |
| 139 | struct ethhdr *eth = data; |
| 140 | __u16 h_proto; |
| 141 | |
| 142 | if (eth + 1 > data_end) |
| 143 | return XDP_DROP; |
| 144 | |
| 145 | h_proto = eth->h_proto; |
| 146 | |
| 147 | if (h_proto == htons(ETH_P_IP)) |
| 148 | return handle_ipv4(xdp); |
| 149 | else |
| 150 | return XDP_PASS; |
| 151 | } |
| 152 | |
| 153 | char _license[] SEC("license") = "GPL"; |