From 01a89d9ace95304db5ec5612ad3e72481599193c Mon Sep 17 00:00:00 2001 From: erwan Date: Sun, 25 Apr 2021 01:05:40 +0200 Subject: [PATCH] Init add --- XDP/xdp_udp.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 XDP/xdp_udp.c diff --git a/XDP/xdp_udp.c b/XDP/xdp_udp.c new file mode 100644 index 0000000..de1b221 --- /dev/null +++ b/XDP/xdp_udp.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + + //Partie offset header en commentaire pour économiser les bytes sur la mémoire réservée pour le Kernel + /* + const unsigned short macLen = 6; //2bytes + const unsigned short macSrc = macLen; //2bytes + const unsigned short macDest = macLen; //2bytes + const unsigned short pktType =2; //2bytes + const unsigned short ethHead = macSrc+macDest+pktType; //=14 + + //Partie offet IP + const unsigned short iPVersionAndHeaderLen = 1; + const unsigned short DSF = 1; + const unsigned short totalLen = 2; + const unsigned short id = 2; + const unsigned short offsetIP = iPVersionAndHeaderLen + DSF + totalLen + id; //=6 + + const unsigned short DF_offset = offsetIP + ethHead; //=20 + */ + + + +SEC("xdp_udp") +int xdp_udp_func(struct xdp_md *ctx) +{ + void *data_end = (void *)(long)ctx->data_end; + void *data = (void *)(long)ctx->data; + + unsigned short mask = 0x4000;//0b 0100 0000 0000 0000 + int DF_offset = 21; + + struct ethhdr *eth = data; //structure ethernet from if_ether.h + int rc = XDP_PASS; // return code + int ipsize; + __u16 header_proto; __u64 length_header_eth; + + length_header_eth = sizeof(*eth);//length in bits of header eth + + if(data + length_header_eth > data_end)//monitor the bound data + return rc; + header_proto = eth->h_proto; + + //Enter only if it's IPV4 + if(header_proto == bpf_htons(ETH_P_IP)) + { + //We monitor protocol IPV4, we want only UDP packet + struct iphdr *ip = data + length_header_eth; + ipsize = sizeof(struct iphdr); + if(ip + ipsize > data_end) { + return rc; + } + if(ip->protocol == IPPROTO_UDP) { + //On souhaite modifier le 21eme byte + // Unsigned short fait 2 bytes + unsigned short *p = data + DF_offset +1 ; + if(p > data_end) return rc; + p[0] = p[0] || mask; + } + } + + + return XDP_PASS; +}