AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
inet.h
Go to the documentation of this file.
1#ifndef _NET_INET_H
2#define _NET_INET_H
3
4/*
5 * The Internet protocols over Ethernet (kernel/net/inet): their headers,
6 * in the network's byte order, and the functions each layer calls on the
7 * next one. See docs/kernel/networking.md.
8 */
9
10#include <core/system.h>
11#include <net/netbuf.h>
12#include <net/netif.h>
13
14#include <aquila/socket.h>
15
16/* What an Ethernet frame carries. */
17#define ETHER_TYPE_IPV4 0x0800
18#define ETHER_TYPE_ARP 0x0806
19
26
28struct arp_hdr {
29 uint16_t htype; /* 1, for Ethernet */
30 uint16_t ptype; /* IPv4 */
31 uint8_t hlen; /* 6 */
32 uint8_t plen; /* 4 */
33 uint16_t op; /* 1 for a request, 2 for a reply */
38} __packed;
39
40/* What an IPv4 packet carries. */
41#define IP_PROTO_ICMP 1
42#define IP_PROTO_TCP 6
43#define IP_PROTO_UDP 17
44
46struct ipv4_hdr {
47 uint8_t ver_ihl; /* the version (4) and the header's length (in 32-bit words) */
49 uint16_t len; /* the whole packet's length */
51 uint16_t frag; /* flags and fragment offset */
57} __packed;
58
59/* ICMP messages' types, and the codes of "unreachable" messages. */
60#define ICMP_ECHO_REPLY 0
61#define ICMP_UNREACH 3
62#define ICMP_ECHO_REQUEST 8
63
64#define ICMP_UNREACH_HOST 1
65#define ICMP_UNREACH_PORT 3
66
75
77struct udp_hdr {
80 uint16_t len; /* the length of the header and the data */
81 uint16_t checksum; /* 0 means none */
82} __packed;
83
84/* Returns the Internet checksum of `len` bytes. */
85uint16_t inet_checksum(const void *data, size_t len);
86
87/* Returns the Internet checksum of the pseudo-header (src, dst, proto,
88 * len) and the data, as UDP and TCP use. */
89uint16_t inet_checksum_pseudo(uint32_t src, uint32_t dst, uint8_t proto, const void *data, size_t len);
90
91struct inet_sock;
92
93/* Each layer's input (a packet that came) and output (one to send). */
94void ether_input(struct netif *netif, struct netbuf *nb);
95int ether_output(struct netif *netif, const uint8_t dst[6], uint16_t type, struct netbuf *nb);
96
97void arp_input(struct netif *netif, struct netbuf *nb);
98void arp_update(struct netif *netif, uint32_t ip, const uint8_t mac[6]);
99int arp_lookup(struct netif *netif, uint32_t ip, uint8_t mac[6]);
100int arp_resolve(struct netif *netif, uint32_t ip, struct netbuf *nb);
101
102void ipv4_input(struct netif *netif, struct netbuf *nb, const uint8_t src_mac[6]);
103int ipv4_output(struct netif *netif, uint32_t src, uint32_t dst, uint8_t proto, struct netbuf *nb);
105int ipv4_send(uint32_t dst, uint8_t proto, struct netbuf *nb);
106struct netif *ipv4_route(uint32_t dst);
108
109void icmp_input(struct netif *netif, struct netbuf *nb, uint32_t src, uint32_t dst);
110void icmp_unreachable(struct netif *netif, const struct ipv4_hdr *ih, int code);
111
112void udp_input(struct netif *netif, struct netbuf *nb, const struct ipv4_hdr *ih);
113
114/* The sockets (net/inet/socket.c): finding the one a packet is for,
115 * giving it the packet or an error, and their addresses. */
116struct inet_sock *inet_sock_find(struct netif *netif, uint8_t proto, uint32_t local, uint16_t port,
117 uint32_t remote, uint16_t remote_port);
118void inet_sock_deliver(struct inet_sock *sock, struct netbuf *nb);
119void inet_sock_error(struct inet_sock *sock, int err);
120int inet_sockaddr_get(const struct sockaddr *addr, socklen_t len, uint32_t *ip, uint16_t *port);
121void inet_sockaddr_put(struct sockaddr *addr, socklen_t *len, uint32_t ip, uint16_t port);
122
123/* The interfaces' ioctl() requests (net/inet/ifioctl.c). */
124int inet_ifioctl(int request, void *argp);
125
126#endif /* ! _NET_INET_H */
#define __packed
Definition system.h:30
int ether_output(struct netif *netif, const uint8_t dst[6], uint16_t type, struct netbuf *nb)
Sends a frame: adds its Ethernet header and gives it to the interface.
Definition ether.c:54
uint16_t inet_checksum(const void *data, size_t len)
Computes the Internet checksum (RFC 1071): the ones' complement of the ones' complement sum of the da...
Definition ether.c:76
void inet_sockaddr_put(struct sockaddr *addr, socklen_t *len, uint32_t ip, uint16_t port)
Writes an AF_INET address, cut to *len bytes if it doesn't fit.
Definition socket.c:306
int inet_ifioctl(int request, void *argp)
Handles an interface ioctl() request on an AF_INET socket.
Definition ifioctl.c:153
struct inet_sock * inet_sock_find(struct netif *netif, uint8_t proto, uint32_t local, uint16_t port, uint32_t remote, uint16_t remote_port)
Finds the socket that a received datagram is for: a socket connected to its sender first,...
Definition socket.c:154
void icmp_input(struct netif *netif, struct netbuf *nb, uint32_t src, uint32_t dst)
Takes in an ICMP message that IPv4 received.
Definition icmp.c:50
struct netif * ipv4_route(uint32_t dst)
Chooses the interface to send a packet to dst through: the one whose link it is on,...
Definition ipv4.c:142
void arp_update(struct netif *netif, uint32_t ip, const uint8_t mac[6])
Records an address's hardware address, replacing the oldest entry if the table is full,...
Definition arp.c:185
int arp_resolve(struct netif *netif, uint32_t ip, struct netbuf *nb)
Holds a packet until the hardware address of its next hop is known, and asks for it.
Definition arp.c:118
void ether_input(struct netif *netif, struct netbuf *nb)
Takes in a frame that an interface received, and passes it to ARP or IPv4.
Definition ether.c:21
void ipv4_input(struct netif *netif, struct netbuf *nb, const uint8_t src_mac[6])
Takes in an IPv4 packet that an interface received, and passes it to its protocol.
Definition ipv4.c:84
int ipv4_send(uint32_t dst, uint8_t proto, struct netbuf *nb)
Sends a packet to dst, through the interface its route says.
Definition ipv4.c:244
int ipv4_is_local(uint32_t ip)
Says whether an address is one of ours: a loopback address, or an interface's.
Definition ipv4.c:48
void icmp_unreachable(struct netif *netif, const struct ipv4_hdr *ih, int code)
Tells the sender of a datagram that it couldn't get where it was sent.
Definition icmp.c:105
void inet_sock_error(struct inet_sock *sock, int err)
Gives a socket an error (from ICMP), which its next call reports.
Definition socket.c:211
uint16_t inet_checksum_pseudo(uint32_t src, uint32_t dst, uint8_t proto, const void *data, size_t len)
Computes the Internet checksum of data with the IPv4 pseudo-header before it (the addresses,...
Definition ether.c:106
uint32_t ipv4_source(struct netif *netif, uint32_t dst)
Chooses the source address of a packet to dst sent through netif.
Definition ipv4.c:69
int ipv4_output(struct netif *netif, uint32_t src, uint32_t dst, uint8_t proto, struct netbuf *nb)
Sends a packet through an interface: adds its IPv4 header and sends it to dst, or to the gateway if d...
Definition ipv4.c:190
void inet_sock_deliver(struct inet_sock *sock, struct netbuf *nb)
Queues a received datagram on its socket, and wakes up its readers.
Definition socket.c:185
void udp_input(struct netif *netif, struct netbuf *nb, const struct ipv4_hdr *ih)
Takes in a UDP datagram that IPv4 received, and delivers it to its socket.
Definition udp.c:24
int arp_lookup(struct netif *netif, uint32_t ip, uint8_t mac[6])
Looks up an address's hardware address.
Definition arp.c:218
void arp_input(struct netif *netif, struct netbuf *nb)
Takes in an ARP packet: the sender's address is recorded, and a request for our address is answered.
Definition arp.c:244
int inet_sockaddr_get(const struct sockaddr *addr, socklen_t len, uint32_t *ip, uint16_t *port)
Reads an AF_INET address.
Definition socket.c:283
unsigned int uint32_t
Definition stddef.h:22
unsigned char uint8_t
Definition stddef.h:20
unsigned short int uint16_t
Definition stddef.h:21
An ARP packet, for IPv4 over Ethernet.
Definition inet.h:28
uint16_t ptype
Definition inet.h:30
uint16_t op
Definition inet.h:33
uint32_t tpa
Definition inet.h:37
uint8_t hlen
Definition inet.h:31
uint32_t spa
Definition inet.h:35
uint8_t plen
Definition inet.h:32
uint8_t sha[6]
Definition inet.h:34
uint16_t htype
Definition inet.h:29
uint8_t tha[6]
Definition inet.h:36
An Ethernet frame's header.
Definition inet.h:21
uint8_t src[6]
Definition inet.h:23
uint8_t dst[6]
Definition inet.h:22
uint16_t type
Definition inet.h:24
An ICMP message's header.
Definition inet.h:68
uint16_t seq
Definition inet.h:73
uint8_t type
Definition inet.h:69
uint16_t id
Definition inet.h:72
uint8_t code
Definition inet.h:70
uint16_t checksum
Definition inet.h:71
A UDP or ping socket.
Definition socket.c:45
uint8_t proto
Definition socket.c:46
An IPv4 packet's header.
Definition inet.h:46
uint16_t len
Definition inet.h:49
uint8_t ttl
Definition inet.h:52
uint16_t id
Definition inet.h:50
uint8_t tos
Definition inet.h:48
uint32_t dst
Definition inet.h:56
uint16_t checksum
Definition inet.h:54
uint8_t ver_ihl
Definition inet.h:47
uint8_t proto
Definition inet.h:53
uint16_t frag
Definition inet.h:51
uint32_t src
Definition inet.h:55
A packet (net/netbuf.c).
Definition netbuf.h:16
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
Definition netif.h:16
uint32_t ip
Definition netif.h:24
A UDP datagram's header.
Definition inet.h:77
uint16_t checksum
Definition inet.h:81
uint16_t src
Definition inet.h:78
uint16_t len
Definition inet.h:80
uint16_t dst
Definition inet.h:79