The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
Loading...
Searching...
No Matches
ipv4.h
Go to the documentation of this file.
1
#pragma once
2
3
/**
4
* @file ipv4.h
5
* @brief IPv4 (RFC 791): the header, the checks, and where a packet goes.
6
*
7
* routing: 127.0.0.0/8 goes to the loopback interface; an address on the
8
* local network of an interface goes straight to it; any other address
9
* goes to the gateway of the first interface that has one. 255.255.255.255
10
* goes to all hosts on the first Ethernet interface, also before it has an
11
* address (for DHCP).
12
*
13
* fragments are not put together: a fragment is dropped and counted, and
14
* every packet sent has the "don't fragment" bit set.
15
*/
16
17
#include "
netif.h
"
18
19
#define IPV4_HEADER_SIZE 20
20
#define IPV4_BROADCAST 0xFFFFFFFFu
21
#define IPV4_PACKET_MAX 65535
22
23
#define IP_PROTO_ICMP 1
24
#define IP_PROTO_TCP 6
25
#define IP_PROTO_UDP 17
26
27
typedef
struct
28
{
29
uint8_t
version_ihl
;
// 4 bits version, 4 bits header length in words
30
uint8_t
tos
;
31
uint16_t
total_length
;
32
uint16_t
id
;
33
uint16_t
fragment
;
// flags and the fragment offset
34
uint8_t
ttl
;
35
uint8_t
protocol
;
36
uint16_t
checksum
;
37
uint32_t
source
;
38
uint32_t
destination
;
39
}
__attribute__
((packed)) ipv4_header_t;
40
41
// counters for the whole stack
42
typedef
struct
43
{
44
uint32_t
received
;
45
uint32_t
delivered
;
46
uint32_t
bad_header
;
// wrong version, length or checksum
47
uint32_t
fragments
;
// dropped: fragments are not put together
48
uint32_t
not_for_us
;
49
uint32_t
unknown_protocol
;
50
uint32_t
no_port
;
// UDP: no socket for the port
51
uint32_t
sent
;
52
uint32_t
no_route
;
53
uint32_t
icmp_echo_answered
;
54
uint32_t
icmp_echo_replies
;
55
}
ipv4_stats_t
;
56
57
extern
ipv4_stats_t
ipv4_stats
;
58
59
/**
60
* get the send buffer (from net_init).
61
* @return false if there is no memory.
62
*/
63
bool
ipv4_init
(
void
);
64
65
/**
66
* a packet from an interface (net_lock held).
67
*/
68
void
ipv4_receive
(
netif_t
*
netif
,
const
uint8_t
*packet,
uint32_t
length);
69
70
/**
71
* send a payload to dst (net_lock held).
72
* @param source the source address, or 0 for the address of the
73
* interface the packet goes out on.
74
* @return false if there is no route or the device has no room.
75
*/
76
bool
ipv4_send
(
uint32_t
source,
uint32_t
dst,
uint8_t
protocol,
const
void
*payload,
77
uint32_t
length);
78
79
/**
80
* the sum of the pseudo-header of UDP and TCP (the addresses, the
81
* protocol, the length), to start their checksum with.
82
*/
83
uint32_t
ipv4_pseudo_sum
(
uint32_t
source,
uint32_t
dst,
uint8_t
protocol,
uint32_t
length);
84
85
/**
86
* the source address a packet to dst gets (net_lock held), 0 if there is
87
* no route.
88
*/
89
uint32_t
ipv4_source_for
(
uint32_t
dst);
90
91
/**
92
* the interface a packet to dst goes out on, and the host it goes to
93
* first (dst, or a gateway). net_lock held.
94
* @return NULL if there is no route.
95
*/
96
netif_t
*
ipv4_route
(
uint32_t
dst,
uint32_t
*next_hop);
ipv4_source_for
uint32_t ipv4_source_for(uint32_t dst)
the source address a packet to dst gets (net_lock held), 0 if there is no route.
Definition
ipv4.c:164
ipv4_pseudo_sum
uint32_t ipv4_pseudo_sum(uint32_t source, uint32_t dst, uint8_t protocol, uint32_t length)
the sum of the pseudo-header of UDP and TCP (the addresses, the protocol, the length),...
Definition
ipv4.c:152
ipv4_init
bool ipv4_init(void)
get the send buffer (from net_init).
Definition
ipv4.c:171
ipv4_route
netif_t * ipv4_route(uint32_t dst, uint32_t *next_hop)
the interface a packet to dst goes out on, and the host it goes to first (dst, or a gateway).
Definition
ipv4.c:94
ipv4_send
bool ipv4_send(uint32_t source, uint32_t dst, uint8_t protocol, const void *payload, uint32_t length)
send a payload to dst (net_lock held).
Definition
ipv4.c:177
ipv4_stats
ipv4_stats_t ipv4_stats
Definition
ipv4.c:25
ipv4_receive
void ipv4_receive(netif_t *netif, const uint8_t *packet, uint32_t length)
a packet from an interface (net_lock held).
Definition
ipv4.c:42
uint32_t
unsigned int uint32_t
Definition
fb.h:17
uint8_t
unsigned char uint8_t
Definition
fb.h:18
uint16_t
unsigned short uint16_t
Definition
stdint.h:13
netif.h
network interfaces: the devices that send and receive frames.
netif_t
struct netif netif_t
__attribute__
ELF32 file header.
Definition
elf.h:51
__attribute__::tos
uint8_t tos
Definition
ipv4.h:30
__attribute__::destination
uint32_t destination
Definition
ipv4.h:38
__attribute__::version_ihl
uint8_t version_ihl
Definition
ipv4.h:29
__attribute__::checksum
uint16_t checksum
Definition
ipv4.h:36
__attribute__::source
uint32_t source
Definition
ipv4.h:37
__attribute__::total_length
uint16_t total_length
Definition
ipv4.h:31
__attribute__::protocol
uint8_t protocol
Definition
ipv4.h:35
__attribute__::fragment
uint16_t fragment
Definition
ipv4.h:33
__attribute__::id
uint16_t id
Definition
ipv4.h:32
__attribute__::ttl
uint8_t ttl
Definition
ipv4.h:34
ipv4_stats_t
Definition
ipv4.h:43
ipv4_stats_t::sent
uint32_t sent
Definition
ipv4.h:51
ipv4_stats_t::no_port
uint32_t no_port
Definition
ipv4.h:50
ipv4_stats_t::icmp_echo_replies
uint32_t icmp_echo_replies
Definition
ipv4.h:54
ipv4_stats_t::bad_header
uint32_t bad_header
Definition
ipv4.h:46
ipv4_stats_t::fragments
uint32_t fragments
Definition
ipv4.h:47
ipv4_stats_t::received
uint32_t received
Definition
ipv4.h:44
ipv4_stats_t::icmp_echo_answered
uint32_t icmp_echo_answered
Definition
ipv4.h:53
ipv4_stats_t::not_for_us
uint32_t not_for_us
Definition
ipv4.h:48
ipv4_stats_t::delivered
uint32_t delivered
Definition
ipv4.h:45
ipv4_stats_t::no_route
uint32_t no_route
Definition
ipv4.h:52
ipv4_stats_t::unknown_protocol
uint32_t unknown_protocol
Definition
ipv4.h:49
netif
Definition
netif.h:37
src
kernel
net
ipv4.h
Generated by
1.18.0