mirror of
https://github.com/h3xduck/TripleCross.git
synced 2025-12-26 03:13:07 +08:00
Updated project structure, and added new list for the next incoming feature.
This commit is contained in:
59
src/ebpf/include/packet/packet_manager.h
Normal file
59
src/ebpf/include/packet/packet_manager.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef __PACKET_MANAGER_H__
|
||||
#define __PACKET_MANAGER_H__
|
||||
#include <linux/bpf.h>
|
||||
#include <linux/if_ether.h>
|
||||
#include <linux/if.h>
|
||||
#include <linux/limits.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
|
||||
}
|
||||
|
||||
static __always_inline int tcp_payload_bound_check(char* payload, int payload_size, void* data_end){
|
||||
if ((void*)payload + payload_size > 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.
|
||||
}
|
||||
}
|
||||
|
||||
static __always_inline unsigned char* get_payload(struct tcphdr *tcp){
|
||||
return (void *)tcp + tcp->doff*4;
|
||||
}
|
||||
|
||||
#endif
|
||||
67
src/ebpf/include/packet/protocol/ip_helper.h
Normal file
67
src/ebpf/include/packet/protocol/ip_helper.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#ifndef __IP_HELPER_H__
|
||||
#define __IP_HELPER_H__
|
||||
|
||||
#include <linux/ip.h>
|
||||
#include <linux/types.h>
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include <bpf/bpf_endian.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
/**
|
||||
* IP checksum calculation.
|
||||
* Following RFC 1071.
|
||||
* In essence 1's complement of 16-bit groups.
|
||||
* Taken from my own library https://github.com/h3xduck/RawTCP_Lib/blob/master/src/packet.c
|
||||
*/
|
||||
static __always_inline unsigned short checksum(unsigned short *addr, int nbytes){
|
||||
long sum = 0;
|
||||
unsigned short checksum;
|
||||
while(nbytes>1){
|
||||
sum += (unsigned short) *addr++;
|
||||
nbytes -= 2;
|
||||
}
|
||||
if(nbytes>0){
|
||||
sum +=htons((unsigned char)*addr);
|
||||
}
|
||||
|
||||
while (sum>>16){
|
||||
sum = (sum & 0xffff) + (sum >> 16);
|
||||
}
|
||||
|
||||
checksum = ~sum;
|
||||
return checksum;
|
||||
}
|
||||
|
||||
|
||||
static __always_inline __u16 csum_fold_helper(__u32 csum)
|
||||
{
|
||||
//return ~((csum & 0xffff) + (csum >> 16));
|
||||
//The following solves some errors where the last summatory overflows
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; i ++) {
|
||||
if (csum >> 16){
|
||||
csum = (csum & 0xffff) + (csum >> 16);
|
||||
}
|
||||
}
|
||||
return ~csum;
|
||||
}
|
||||
/**
|
||||
* IP checksum calculation.
|
||||
* Following RFC 1071, using BPFs.*
|
||||
*/
|
||||
static __always_inline void ipv4_csum(void *data_start, int data_size, __u32 *csum)
|
||||
{
|
||||
//WITH EBPF HELPERS
|
||||
bpf_printk("csum: %u for data_start %u, data_size %i\n", *csum, data_start, data_size);
|
||||
|
||||
/*unsigned char* p = (unsigned char*) data_start;
|
||||
for(int ii = 0; ii<20; ii++){
|
||||
bpf_printk("B%i: %x\n", ii, p[ii]);
|
||||
}*/
|
||||
|
||||
*csum = bpf_csum_diff(0, 0, data_start, data_size, *csum);
|
||||
*csum = csum_fold_helper(*csum);
|
||||
}
|
||||
|
||||
#endif
|
||||
41
src/ebpf/include/packet/protocol/tcp_helper.h
Normal file
41
src/ebpf/include/packet/protocol/tcp_helper.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef __TCP_HELPER_H__
|
||||
#define __TCP_HELPER_H__
|
||||
|
||||
#include <linux/tcp.h>
|
||||
#include <linux/ip.h>
|
||||
|
||||
|
||||
static __always_inline int get_tcp_src_port(struct tcphdr *tcp){
|
||||
return ntohs(tcp->source);
|
||||
}
|
||||
|
||||
static __always_inline int get_tcp_dest_port(struct tcphdr *tcp){
|
||||
return ntohs(tcp->dest);
|
||||
}
|
||||
|
||||
/**
|
||||
* TCP checksum calculation.
|
||||
* Following RFC 1071.
|
||||
* In essence 1's complement of 16-bit groups.
|
||||
* Taken from my own library https://github.com/h3xduck/RawTCP_Lib/blob/master/src/segment.c
|
||||
*/
|
||||
static __always_inline unsigned short tcp_checksum(unsigned short *addr, int nbytes){
|
||||
long sum = 0;
|
||||
unsigned short checksum;
|
||||
while(nbytes>1){
|
||||
sum += (unsigned short) *addr++;
|
||||
nbytes -= 2;
|
||||
}
|
||||
if(nbytes>0){
|
||||
sum += htons((unsigned char)*addr);
|
||||
}
|
||||
|
||||
while (sum>>16){
|
||||
sum = (sum & 0xffff) + (sum >> 16);
|
||||
}
|
||||
|
||||
checksum = ~sum;
|
||||
return checksum;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user