-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize the hook by parsing the packet before processing it
- Loading branch information
1 parent
55ff497
commit 40a1976
Showing
4 changed files
with
131 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "parser.h" | ||
|
||
static const __be32 LOOPBACK_PREFIX = 0x7f000000; | ||
static const __be32 LOOPBACK_MASK = 0xff000000; | ||
|
||
static inline bool is_loopback_addr(__be32 addr) { | ||
return (addr & LOOPBACK_MASK) == LOOPBACK_PREFIX; | ||
} | ||
|
||
void parse_packet(packet_t *packet, struct sk_buff *skb) { | ||
struct iphdr *ip_header = ip_hdr(skb); | ||
struct tcphdr *tcp_header; | ||
struct udphdr *udp_header; | ||
|
||
packet->src_ip = ip_header->saddr; | ||
packet->dst_ip = ip_header->daddr; | ||
packet->dev_name = skb->dev->name; | ||
|
||
if (is_loopback_addr(packet->src_ip) || is_loopback_addr(packet->dst_ip)) { | ||
// In this case we don't care about the rest of the fields, and they | ||
// might contain garbage. | ||
packet->type = PACKET_TYPE_LOOPBACK; | ||
return; | ||
} | ||
|
||
packet->protocol = ip_header->protocol; | ||
|
||
// Noteice that we the store the exact ports, even if they're above 1023. | ||
if (packet->protocol == PROT_TCP) { | ||
tcp_header = tcp_hdr(skb); | ||
packet->src_port = tcp_header->source; | ||
packet->dst_port = tcp_header->dest; | ||
packet->ack = tcp_header->ack; | ||
if (tcp_header->fin && tcp_header->psh && tcp_header->urg) { | ||
packet->type = PACKET_TYPE_XMAS; | ||
} else { | ||
packet->type = PACKET_TYPE_NORMAL; | ||
} | ||
} else if (ip_header->protocol == PROT_UDP) { | ||
udp_header = udp_hdr(skb); | ||
packet->type = PACKET_TYPE_NORMAL; | ||
packet->src_port = udp_header->source; | ||
packet->dst_port = udp_header->dest; | ||
} else if (ip_header->protocol == PROT_ICMP) { | ||
packet->type = PACKET_TYPE_NORMAL; | ||
packet->src_port = 0; | ||
packet->dst_port = 0; | ||
} else { | ||
packet->type = PACKET_TYPE_UNHANDLED_PROTOCOL; | ||
// In this case we don't care about the ports and they might contain | ||
// garbage. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef _PARSER_H_ | ||
#define _PARSER_H_ | ||
|
||
#include "fw.h" | ||
|
||
typedef enum { | ||
PACKET_TYPE_NORMAL, | ||
PACKET_TYPE_XMAS, | ||
PACKET_TYPE_UNHANDLED_PROTOCOL, | ||
PACKET_TYPE_LOOPBACK | ||
} packet_type; | ||
|
||
typedef struct { | ||
packet_type type; | ||
char *dev_name; | ||
__be32 src_ip; | ||
__be32 dst_ip; | ||
__be16 src_port; | ||
__be16 dst_port; | ||
__u8 protocol; | ||
unsigned short ack; | ||
} packet_t; | ||
|
||
void parse_packet(packet_t *packet, struct sk_buff *skb); | ||
|
||
#endif // _PARSER_H_ |