Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
ipv4.h
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HICN_PROTOCOL_IPV4
17 #define HICN_PROTOCOL_IPV4
18 
19 #include "../base.h"
20 #include "../common.h"
21 #include "../protocol.h"
22 
23 /* Headers were adapted from linux' definitions in netinet/ip.h */
24 
25 /*
26  * The length of the IPV4 header struct must be 20 bytes.
27  */
28 #define EXPECTED_IPV4_HDRLEN 20
29 
30 typedef struct
31 {
32  union
33  {
34  struct
35  {
36 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
37  u8 ihl:4;
38  u8 version:4;
39 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
40  u8 version:4;
41  u8 ihl:4;
42 #else
43 #error "Unsupported endianness"
44 #endif
45  };
46 
47  u8 version_ihl;
48  };
49  u8 tos;
50  u16 len;
51  u16 id;
52  u16 frag_off;
53  u8 ttl;
54  u8 protocol;
55  u16 csum;
56  ip4_address_t saddr;
57  ip4_address_t daddr;
59 
60 #define ipv4_header_bytes(ipv4_header) (sizeof(u32) * (ipv4_header->version_ihl & 0xf))
61 
62 #define IPV4_HDRLEN sizeof(_ipv4_header_t)
63 static_assert (EXPECTED_IPV4_HDRLEN == IPV4_HDRLEN,
64  "Size of IPV4 struct does not match its expected size.");
65 
66 /*
67  * The length of the IPV4 pseudo header struct must be 12 bytes.
68  */
69 #define EXPECTED_IPV4_PSHDRLEN 12
70 
71 typedef struct
72 {
73  ip4_address_t ip_src;
74  ip4_address_t ip_dst;
75  u8 zero;
76  u8 protocol;
77  u16 size;
79 
80 #define IPV4_PSHDRLEN sizeof(ipv4_pseudo_header_t)
81 static_assert (EXPECTED_IPV4_PSHDRLEN == IPV4_PSHDRLEN,
82  "Size of IPV4_PSHDR struct does not match its expected size.");
83 
84 /* Default field values */
85 #define IPV4_DEFAULT_VERSION 4
86 #define IPV4_DEFAULT_IHL 5
87 #define IPV4_DEFAULT_TOS 0
88 #define IPV4_DEFAULT_PAYLOAD_LENGTH 0
89 #define IPV4_DEFAULT_ID 300
90 #define IPV4_DEFAULT_FRAG_OFF 0x000
91 #define IPV4_DEFAULT_TTL 64
92 #define IPV4_DEFAULT_PROTOCOL IPPROTO_TCP
93 #define IPV4_DEFAULT_SRC_IP 0, 0, 0, 0
94 #define IPV4_DEFAULT_DST_IP 0, 0, 0, 0
95 
96 
97 #endif /* HICN_PROTOCOL_IPV4 */
98 
99 /*
100  * fd.io coding-style-patch-verification: ON
101  *
102  * Local Variables:
103  * eval: (c-set-style "gnu")
104  * End:
105  */
ipv4_pseudo_header_t
Definition: ipv4.h:71
ip4_address_t
Definition: common.h:167
_ipv4_header_t
Definition: ipv4.h:30