This commit is contained in:
erwan 2021-04-25 01:05:40 +02:00
parent a0fccea2dd
commit 01a89d9ace

68
XDP/xdp_udp.c Normal file
View file

@ -0,0 +1,68 @@
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/in.h>
//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;
}