forked from jserv/nstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnstack_ip.h
More file actions
222 lines (185 loc) · 4.41 KB
/
nstack_ip.h
File metadata and controls
222 lines (185 loc) · 4.41 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* nstack IP service.
* @addtogroup IP
* @{
*/
#ifndef NSTACK_IP_H
#define NSTACK_IP_H
#include "linker_set.h"
#include "nstack_ether.h"
#include "nstack_in.h"
#define IP_STR_LEN 17
/**
* Stack ip "10.0.0.2" and subnet mask "255.255.255.0" in decimal form
* @{
*/
#define STACK_IP 167772162
#define SUBNET_MASK 4294967040
/**
* @}
*/
/**
* IP Route descriptor.
*/
struct ip_route {
in_addr_t r_network; /*!< Network address. */
in_addr_t r_netmask; /*!< Network mask. */
in_addr_t r_gw; /*!< Gateway IP. */
in_addr_t r_iface; /*!< Interface address. */
int r_iface_handle; /*!< Interface ether_handle. */
};
/**
* IP Packet Header.
*/
struct ip_hdr {
uint8_t ip_vhl;
uint8_t ip_tos;
uint16_t ip_len;
uint16_t ip_id;
uint16_t ip_foff;
uint8_t ip_ttl;
uint8_t ip_proto;
uint16_t ip_csum;
uint32_t ip_src;
uint32_t ip_dst;
uint8_t ip_opt[0];
} __attribute__((packed, aligned(4)));
/**
* IP Packet Header Defaults
* @{
*/
/* v4 and 5 * 4 octets */
#define IP_VHL_DEFAULT 0x45 /*!< Default value for version and ihl. */
#define IP_TOS_DEFAULT 0x0 /*!< Default type of service and no ECN */
#define IP_TOFF_DEFAULT 0x4000
#define IP_TTL_DEFAULT 64
/**
* @}
*/
/**
* IP Packet header values.
*/
/**
* Get IP version.
*/
#define IP_VERSION(_ip_hdr_) (((ip_hdr)->ip_vhl & 0x40) >> 4)
#define IP_FALGS_DF 0x4000
#define IP_FLAGS_MF 0x2000
/**
* Max IP packet size in bytes.
*/
#define IP_MAX_BYTES 65535
/**
* IP protocol numbers.
* @{
*/
#define IP_PROTO_ICMP 1
#define IP_PROTO_IGMP 2
#define IP_PROTO_TCP 6
#define IP_PROTO_UDP 17
#define IP_PROTO_SCTP 132
/**
* @}
*/
/**
* @}
*/
struct _ip_proto_handler {
uint16_t proto_id;
int (*fn)(const struct ip_hdr *hdr, uint8_t *payload, size_t bsize);
};
/**
* Declare an IP input chain handler.
*/
#define IP_PROTO_INPUT_HANDLER(_proto_id_, _handler_fn_) \
static struct _ip_proto_handler _ip_proto_handler_##_handler_fn_ = { \
.proto_id = _proto_id_, \
.fn = _handler_fn_, \
}; \
DATA_SET(_ip_proto_handlers, _ip_proto_handler_##_handler_fn_)
int ip_config(int ether_handle, in_addr_t ip_addr, in_addr_t netmask);
/**
* IP Packet manipulation.
* @{
*/
/**
* Calculate the Internet checksum.
*/
uint16_t ip_checksum(void *dp, size_t bsize);
/**
* Get the header length of an IP packet.
*/
static inline size_t ip_hdr_hlen(const struct ip_hdr *ip)
{
return (ip->ip_vhl & 0x0f) * 4;
}
/**
* @}
*/
/**
* RIB
* @{
*/
/**
* Update a route.
* @param[in] route is a pointer to a route struct; the information will be
* copied from the struct.
*/
int ip_route_update(struct ip_route *route);
/**
* Remove a route from routing table.
*/
int ip_route_remove(struct ip_route *route);
/**
* Get routing information for a network.
* @param[out] route is a pointer to a ip_route struct that will be updated
* if a route is found.
*/
int ip_route_find_by_network(in_addr_t ip, struct ip_route *route);
/**
* Get routing information for a source IP addess.
* The function can be also used for source IP address validation by setting
* route pointer argument to NULL.
*/
int ip_route_find_by_iface(in_addr_t addr, struct ip_route *route);
/**
* @}
*/
/**
* IP packet handling and manipulation.
* @{
*/
void ip_hton(const struct ip_hdr *host, struct ip_hdr *net);
size_t ip_ntoh(const struct ip_hdr *net, struct ip_hdr *host);
int ip_input(const struct ether_hdr *e_hdr, uint8_t *payload, size_t bsize);
/**
* Construct a reply header from a received IP packet header.
* Swaps src and dst etc.
* @param host_ip_hd is a pointer to a IP packet header that should be reversed.
* @param bsize is the size of the packet data.
* @returns Returns the size of the header.
*/
size_t ip_reply_header(struct ip_hdr *host_ip_hdr, size_t bsize);
/**
* @}
*/
/**
* Send an IP packet to a destination.
*/
int ip_send(in_addr_t dst, uint8_t proto, const uint8_t *buf, size_t bsize);
/**
* IP Fragmentation
* @{
*/
static inline int ip_fragment_is_frag(struct ip_hdr *hdr)
{
return (!!(hdr->ip_foff & IP_FLAGS_MF) || !!(hdr->ip_foff & 0x1fff));
}
int ip_fragment_input(struct ip_hdr *ip_hdr, uint8_t *rx_packet);
/**
* @}
*/
#endif /* NSTACK_IP_H */
/**
* @}
*/