This repository has been archived by the owner on Jun 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ofp_ipv4.h
107 lines (93 loc) · 3.95 KB
/
ofp_ipv4.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef __OFP_IPV4_H__
#define __OFP_IPV4_H__
#if TMC
#include "ofp.h"
#include "ofp_errors.h"
#include <sys/time.h>
#if TMC
#include <tmc/mem.h>
#include <tmc/alloc.h>
#endif
//=======================================================================================================
// Helper methods to access specific data in IPv4 header
//=======================================================================================================
INLINE uint32_t l3_get_ipHeaderLength(const uint8_t *const restrict l3Header)
{
return (*(l3Header) & 0b00001111) * 4;
}
#define OFP_IPV4_OFFSET_IP_SRC 12
INLINE uint32_t l3_get_ip_src(uint8_t* l3Header)
{
return read_uint32_t(l3Header + OFP_IPV4_OFFSET_IP_SRC);
}
#define OFP_IPV4_OFFSET_IP_DEST 16
INLINE uint32_t l3_get_ip_dest(uint8_t* l3Header)
{
return read_uint32_t(l3Header + OFP_IPV4_OFFSET_IP_DEST);
}
INLINE uint32_t l3_get_port_src(uint8_t* l3Header, uint8_t ipHeaderLength)
{
return *(l3Header + ipHeaderLength) * 0x100 | *(l3Header + ipHeaderLength + 1);
}
INLINE uint32_t l3_get_port_dest(uint8_t* l3Header, uint8_t ipHeaderLength)
{
return *(l3Header + ipHeaderLength + 2) * 0x100 | *(l3Header + ipHeaderLength + 3);
}
//=======================================================================================================
//=======================================================================================================
// Packet integrity
//=======================================================================================================
// Returns 1 if packet is valid (L3-wise), <0 otherwise
static INLINE int ofp_ipv4_check_packet(netio_pkt_metadata_t *mda, netio_pkt_t *packet, const uint8_t *const l3Header)
{
// Checksum correct ?
if (NETIO_PKT_L3_CSUM_CALCULATED_M(mda, packet) && !NETIO_PKT_L3_CSUM_CORRECT_M(mda, packet))
{
PRINT_D5("IPv4 Header : invalid checksum");
return OFP_ERR_IP_INVALID_CHECKSUM;
}
if (NETIO_PKT_L3_LENGTH_M(mda, packet) < 20)
{
PRINT_D5("IPv4 : short packet\n");
return OFP_ERR_IP_SHORT_PACKET;
}
if (*l3Header < 0x45) // (4 is the version, 5 is min. IHL)
{
PRINT_D5("IPv4 Header : invalid IHL\n");
return OFP_ERR_IP_INVALID_HEADER;
}
else if (*(l3Header+3) < 0x14 && *(l3Header+2) == 0)
{
PRINT_D5("IPv4 Header : invalid total length\n");
return OFP_ERR_IP_INVALID_HEADER;
}
else if (*(l3Header+9) > 140)
{
PRINT_D5("IPv4 Header : invalid protocol\n");
return OFP_ERR_IP_INVALID_HEADER;
}
// TODO check source address against reserved address blocks
// TODO parse options (if IHL > 5) to check the length ?
return 1;
}
//=======================================================================================================
#ifdef OFP_MAIN
//=======================================================================================================
//=======================================================================================================
// INLINE functions
// Only defined/used in ofp_main.c
// Declared that way to reduce length of ofp_main.c while still
// keeping them inlined.
//=======================================================================================================
//=======================================================================================================
//=======================================================================================================
// L4 Packet work
//=======================================================================================================
static INLINE int packet_work_l4(int work_rank, netio_pkt_t *packet, PacketList* additionalPackets);
//=======================================================================================================
//=======================================================================================================
// PACKET WORK
//=======================================================================================================
#endif // OFP_MAIN
#endif // TMC
#endif // __OFP_IPV4_H__