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>
217cf245a3SToke Høiland-Jørgensen #include <bpf/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
288fdf5b78SDaniel T. Lee /* volatile to prevent compiler optimizations */
298fdf5b78SDaniel T. Lee static volatile __u32 max_pcktsz = MAX_PCKT_SIZE;
308fdf5b78SDaniel T. Lee
31451d1dc8SDaniel T. Lee struct {
32451d1dc8SDaniel T. Lee __uint(type, BPF_MAP_TYPE_ARRAY);
33451d1dc8SDaniel T. Lee __type(key, __u32);
34451d1dc8SDaniel T. Lee __type(value, __u64);
35451d1dc8SDaniel T. Lee __uint(max_entries, 1);
36451d1dc8SDaniel T. Lee } icmpcnt SEC(".maps");
37c6ffd1ffSNikita V. Shirokov
count_icmp(void)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
swap_mac(void * data,struct ethhdr * orig_eth)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
csum_fold_helper(__u32 csum)58c6ffd1ffSNikita V. Shirokov static __always_inline __u16 csum_fold_helper(__u32 csum)
59c6ffd1ffSNikita V. Shirokov {
60*e05f6098SYuan Chen csum = (csum & 0xffff) + (csum >> 16);
61c6ffd1ffSNikita V. Shirokov return ~((csum & 0xffff) + (csum >> 16));
62c6ffd1ffSNikita V. Shirokov }
63c6ffd1ffSNikita V. Shirokov
ipv4_csum(void * data_start,int data_size,__u32 * csum)64c6ffd1ffSNikita V. Shirokov static __always_inline void ipv4_csum(void *data_start, int data_size,
65c6ffd1ffSNikita V. Shirokov __u32 *csum)
66c6ffd1ffSNikita V. Shirokov {
67c6ffd1ffSNikita V. Shirokov *csum = bpf_csum_diff(0, 0, data_start, data_size, *csum);
68c6ffd1ffSNikita V. Shirokov *csum = csum_fold_helper(*csum);
69c6ffd1ffSNikita V. Shirokov }
70c6ffd1ffSNikita V. Shirokov
send_icmp4_too_big(struct xdp_md * xdp)71c6ffd1ffSNikita V. Shirokov static __always_inline int send_icmp4_too_big(struct xdp_md *xdp)
72c6ffd1ffSNikita V. Shirokov {
73c6ffd1ffSNikita V. Shirokov int headroom = (int)sizeof(struct iphdr) + (int)sizeof(struct icmphdr);
74c6ffd1ffSNikita V. Shirokov
75c6ffd1ffSNikita V. Shirokov if (bpf_xdp_adjust_head(xdp, 0 - headroom))
76c6ffd1ffSNikita V. Shirokov return XDP_DROP;
77c6ffd1ffSNikita V. Shirokov void *data = (void *)(long)xdp->data;
78c6ffd1ffSNikita V. Shirokov void *data_end = (void *)(long)xdp->data_end;
79c6ffd1ffSNikita V. Shirokov
80c6ffd1ffSNikita V. Shirokov if (data + (ICMP_TOOBIG_SIZE + headroom) > data_end)
81c6ffd1ffSNikita V. Shirokov return XDP_DROP;
82c6ffd1ffSNikita V. Shirokov
83c6ffd1ffSNikita V. Shirokov struct iphdr *iph, *orig_iph;
84c6ffd1ffSNikita V. Shirokov struct icmphdr *icmp_hdr;
85c6ffd1ffSNikita V. Shirokov struct ethhdr *orig_eth;
86c6ffd1ffSNikita V. Shirokov __u32 csum = 0;
87c6ffd1ffSNikita V. Shirokov __u64 off = 0;
88c6ffd1ffSNikita V. Shirokov
89c6ffd1ffSNikita V. Shirokov orig_eth = data + headroom;
90c6ffd1ffSNikita V. Shirokov swap_mac(data, orig_eth);
91c6ffd1ffSNikita V. Shirokov off += sizeof(struct ethhdr);
92c6ffd1ffSNikita V. Shirokov iph = data + off;
93c6ffd1ffSNikita V. Shirokov off += sizeof(struct iphdr);
94c6ffd1ffSNikita V. Shirokov icmp_hdr = data + off;
95c6ffd1ffSNikita V. Shirokov off += sizeof(struct icmphdr);
96c6ffd1ffSNikita V. Shirokov orig_iph = data + off;
97c6ffd1ffSNikita V. Shirokov icmp_hdr->type = ICMP_DEST_UNREACH;
98c6ffd1ffSNikita V. Shirokov icmp_hdr->code = ICMP_FRAG_NEEDED;
998fdf5b78SDaniel T. Lee icmp_hdr->un.frag.mtu = htons(max_pcktsz - sizeof(struct ethhdr));
100c6ffd1ffSNikita V. Shirokov icmp_hdr->checksum = 0;
101c6ffd1ffSNikita V. Shirokov ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum);
102c6ffd1ffSNikita V. Shirokov icmp_hdr->checksum = csum;
103c6ffd1ffSNikita V. Shirokov iph->ttl = DEFAULT_TTL;
104c6ffd1ffSNikita V. Shirokov iph->daddr = orig_iph->saddr;
105c6ffd1ffSNikita V. Shirokov iph->saddr = orig_iph->daddr;
106c6ffd1ffSNikita V. Shirokov iph->version = 4;
107c6ffd1ffSNikita V. Shirokov iph->ihl = 5;
108c6ffd1ffSNikita V. Shirokov iph->protocol = IPPROTO_ICMP;
109c6ffd1ffSNikita V. Shirokov iph->tos = 0;
110c6ffd1ffSNikita V. Shirokov iph->tot_len = htons(
111c6ffd1ffSNikita V. Shirokov ICMP_TOOBIG_SIZE + headroom - sizeof(struct ethhdr));
112c6ffd1ffSNikita V. Shirokov iph->check = 0;
113c6ffd1ffSNikita V. Shirokov csum = 0;
114c6ffd1ffSNikita V. Shirokov ipv4_csum(iph, sizeof(struct iphdr), &csum);
115c6ffd1ffSNikita V. Shirokov iph->check = csum;
116c6ffd1ffSNikita V. Shirokov count_icmp();
117c6ffd1ffSNikita V. Shirokov return XDP_TX;
118c6ffd1ffSNikita V. Shirokov }
119c6ffd1ffSNikita V. Shirokov
120c6ffd1ffSNikita V. Shirokov
handle_ipv4(struct xdp_md * xdp)121c6ffd1ffSNikita V. Shirokov static __always_inline int handle_ipv4(struct xdp_md *xdp)
122c6ffd1ffSNikita V. Shirokov {
123c6ffd1ffSNikita V. Shirokov void *data_end = (void *)(long)xdp->data_end;
124c6ffd1ffSNikita V. Shirokov void *data = (void *)(long)xdp->data;
125c6ffd1ffSNikita V. Shirokov int pckt_size = data_end - data;
126c6ffd1ffSNikita V. Shirokov int offset;
127c6ffd1ffSNikita V. Shirokov
1288fdf5b78SDaniel T. Lee if (pckt_size > max(max_pcktsz, ICMP_TOOBIG_SIZE)) {
129c6ffd1ffSNikita V. Shirokov offset = pckt_size - ICMP_TOOBIG_SIZE;
130c6ffd1ffSNikita V. Shirokov if (bpf_xdp_adjust_tail(xdp, 0 - offset))
131c6ffd1ffSNikita V. Shirokov return XDP_PASS;
132c6ffd1ffSNikita V. Shirokov return send_icmp4_too_big(xdp);
133c6ffd1ffSNikita V. Shirokov }
134c6ffd1ffSNikita V. Shirokov return XDP_PASS;
135c6ffd1ffSNikita V. Shirokov }
136c6ffd1ffSNikita V. Shirokov
137c6ffd1ffSNikita V. Shirokov SEC("xdp_icmp")
_xdp_icmp(struct xdp_md * xdp)138c6ffd1ffSNikita V. Shirokov int _xdp_icmp(struct xdp_md *xdp)
139c6ffd1ffSNikita V. Shirokov {
140c6ffd1ffSNikita V. Shirokov void *data_end = (void *)(long)xdp->data_end;
141c6ffd1ffSNikita V. Shirokov void *data = (void *)(long)xdp->data;
142c6ffd1ffSNikita V. Shirokov struct ethhdr *eth = data;
143c6ffd1ffSNikita V. Shirokov __u16 h_proto;
144c6ffd1ffSNikita V. Shirokov
145c6ffd1ffSNikita V. Shirokov if (eth + 1 > data_end)
146c6ffd1ffSNikita V. Shirokov return XDP_DROP;
147c6ffd1ffSNikita V. Shirokov
148c6ffd1ffSNikita V. Shirokov h_proto = eth->h_proto;
149c6ffd1ffSNikita V. Shirokov
150c6ffd1ffSNikita V. Shirokov if (h_proto == htons(ETH_P_IP))
151c6ffd1ffSNikita V. Shirokov return handle_ipv4(xdp);
152c6ffd1ffSNikita V. Shirokov else
153c6ffd1ffSNikita V. Shirokov return XDP_PASS;
154c6ffd1ffSNikita V. Shirokov }
155c6ffd1ffSNikita V. Shirokov
156c6ffd1ffSNikita V. Shirokov char _license[] SEC("license") = "GPL";
157