The manual
ginnOS
The reference: libc, the libraries and the kernel
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
27typedef struct
28{
29 uint8_t version_ihl; // 4 bits version, 4 bits header length in words
33 uint16_t fragment; // flags and the fragment offset
39} __attribute__((packed)) ipv4_header_t;
40
41// counters for the whole stack
42typedef struct
43{
46 uint32_t bad_header; // wrong version, length or checksum
47 uint32_t fragments; // dropped: fragments are not put together
50 uint32_t no_port; // UDP: no socket for the port
56
58
59/**
60 * get the send buffer (from net_init).
61 * @return false if there is no memory.
62 */
63bool ipv4_init(void);
64
65/**
66 * a packet from an interface (net_lock held).
67 */
68void 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 */
76bool 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 */
83uint32_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 */
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 */
96netif_t *ipv4_route(uint32_t dst, uint32_t *next_hop);
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
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
bool ipv4_init(void)
get the send buffer (from net_init).
Definition ipv4.c:171
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
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_t ipv4_stats
Definition ipv4.c:25
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
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
unsigned short uint16_t
Definition stdint.h:13
network interfaces: the devices that send and receive frames.
struct netif netif_t
ELF32 file header.
Definition elf.h:51
uint8_t tos
Definition ipv4.h:30
uint32_t destination
Definition ipv4.h:38
uint8_t version_ihl
Definition ipv4.h:29
uint16_t checksum
Definition ipv4.h:36
uint32_t source
Definition ipv4.h:37
uint16_t total_length
Definition ipv4.h:31
uint8_t protocol
Definition ipv4.h:35
uint16_t fragment
Definition ipv4.h:33
uint16_t id
Definition ipv4.h:32
uint8_t ttl
Definition ipv4.h:34
Definition ipv4.h:43
uint32_t sent
Definition ipv4.h:51
uint32_t no_port
Definition ipv4.h:50
uint32_t icmp_echo_replies
Definition ipv4.h:54
uint32_t bad_header
Definition ipv4.h:46
uint32_t fragments
Definition ipv4.h:47
uint32_t received
Definition ipv4.h:44
uint32_t icmp_echo_answered
Definition ipv4.h:53
uint32_t not_for_us
Definition ipv4.h:48
uint32_t delivered
Definition ipv4.h:45
uint32_t no_route
Definition ipv4.h:52
uint32_t unknown_protocol
Definition ipv4.h:49
Definition netif.h:37