Finished externalizing helper functions

This commit is contained in:
h3xduck
2021-11-24 10:50:30 -05:00
parent 72fddcac62
commit 0568d5192d
8 changed files with 318 additions and 276 deletions

View File

@@ -0,0 +1,47 @@
#ifndef __PACKET_MANAGER_H__
#define __PACKET_MANAGER_H__
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/if.h>
/* BOUND CHECKING*/
static __always_inline int ethernet_header_bound_check(struct ethhdr *eth, void* data_end){
if ((void *)eth + sizeof(struct ethhdr) > data_end){
return -1;
}
return 0; //OK
}
static __always_inline int ip_header_bound_check(struct iphdr* ip, void* data_end){
if ((void *)ip + sizeof(*ip) > data_end){
return -1;
}
return 0; //OK
}
static __always_inline int tcp_header_bound_check(struct tcphdr* tcp, void* data_end){
if ((void *)tcp + sizeof(*tcp) > data_end){
return -1;
}
return 0; //OK
}
/* UTILITIES */
static __always_inline int get_protocol(void* data){
struct ethhdr *eth = data;
struct iphdr *ip = data + sizeof(*eth);
switch(ip->protocol){
case IPPROTO_TCP:
return IPPROTO_TCP;
case IPPROTO_UDP:
return IPPROTO_UDP;
default:
return -1; //Unknown and not handled.
}
}
#endif