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