My Kernel v0.1.0
ipv4.h
Go to the documentation of this file.
1
14#ifndef KERNEL_NET_IPV4_H
15#define KERNEL_NET_IPV4_H
16
17#include <kernel/error.h>
18#include <kernel/net.h>
19#include <kernel/types.h>
20
21#include <utils/compiler.h>
22
23#include <arch.h>
24
25struct packet;
26struct net_route;
27
29#define IPV4_VERSION 4
31#define IPV4_MIN_LENGTH 20
33#define IPV4_DEFAULT_TTL 64
34
38struct PACKED ALIGNED(sizeof(uint16_t)) ipv4_header {
39#if defined(ARCH_LITTLE_ENDIAN)
40 uint8_t ihl : 4;
41 uint8_t version : 4;
42#else
43 uint8_t version : 4;
44 uint8_t ihl : 4;
45#endif
46 uint8_t tos;
47 __be uint16_t tot_len;
48 __be uint16_t id;
49 __be uint16_t frag_off;
50 uint8_t ttl;
51 uint8_t protocol;
52 __be uint16_t check;
53 __be ipv4_t saddr;
54 __be ipv4_t daddr;
55};
56
57static_assert(sizeof(struct ipv4_header) == IPV4_MIN_LENGTH);
58
59#define IPV4_FRAG_MASK 0x1FFF
60#define IPV4_RESERVED (0x4 << 13)
61#define IPV4_NOFRAG (0x2 << 13)
62#define IPV4_MORE_FRAG (0x1 << 13)
63
65static inline uint16_t ipv4_fragment_offset(const struct ipv4_header *iphdr)
66{
67 return ntohs(iphdr->frag_off) & IPV4_FRAG_MASK;
68}
69
71static inline bool ipv4_more_framents(const struct ipv4_header *iphdr)
72{
73 return ntohs(iphdr->frag_off) & IPV4_MORE_FRAG;
74}
75
77static inline bool ipv4_is_fragmented(const struct ipv4_header *iphdr)
78{
79 return ntohs(iphdr->frag_off) & (IPV4_MORE_FRAG | IPV4_FRAG_MASK);
80}
81
82/***/
83static inline bool ipv4_is_multicast(__be ipv4_t addr)
84{
85 return (ntohl(addr) >> 28) == 0xE;
86}
87
88/***/
89static inline bool ipv4_is_broadcast(__be ipv4_t addr)
90{
91 return addr == 0XFFFFFFFF;
92}
93
95error_t ipv4_receive_packet(struct packet *packet);
96
100struct packet *ipv4_build_packet(const struct net_route *, u8 protocol,
101 const void *payload, size_t);
102
104static inline __be ipv4_t IPV4(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
105{
106 return htonl(a << 24 | b << 16 | c << 8 | d);
107}
108
109#define FMT_IP "%u.%u.%u.%u"
110#define LOG_IP(ip) \
111 ((uint8_t *)&ip)[0], ((uint8_t *)&ip)[1], ((uint8_t *)&ip)[2], \
112 ((uint8_t *)&ip)[3]
113
114#endif /* KERNEL_NET_IPV4_H */
115
#define IPV4_MIN_LENGTH
Minimum size of an IP header.
Definition: ipv4.h:31
error_t ipv4_receive_packet(struct packet *packet)
Process a newly received IP packet.
Definition: ipv4.c:63
static uint16_t ipv4_fragment_offset(const struct ipv4_header *iphdr)
Definition: ipv4.h:65
static __be ipv4_t IPV4(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Helper to quickly generate an IPv4 address.
Definition: ipv4.h:104
static bool ipv4_is_fragmented(const struct ipv4_header *iphdr)
Definition: ipv4.h:77
struct packet * ipv4_build_packet(const struct net_route *, u8 protocol, const void *payload, size_t)
Build an IP packet The L2/L3 headers are filled using the routing information.
Definition: ipv4.c:147
static bool ipv4_more_framents(const struct ipv4_header *iphdr)
Definition: ipv4.h:71
Structure of the page fault's error code https://wiki.osdev.org/Exceptions#Page_Fault.
Definition: mmu.c:587
Routing structure.
Definition: route.h:14
A network packet.
Definition: packet.h:51
void * payload
Start of the packet's content (L4 and beyond)
Definition: packet.h:73