AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
ipv4.c File Reference

IPv4. More...

#include <core/system.h>
#include <core/string.h>
#include <net/byteorder.h>
#include <net/inet.h>
#include <net/tcp.h>
#include <aquila/errno.h>

Go to the source code of this file.

Functions

static int on_link (struct netif *netif, uint32_t ip)
 Returns whether ip is on netif's link (its network).
static int is_broadcast (struct netif *netif, uint32_t ip)
 Returns whether ip is a broadcast address on netif's link.
static int is_loopback (uint32_t ip)
 Returns whether ip is a loopback address (127.0.0.0/8).
int ipv4_is_local (uint32_t ip)
 Says whether an address is one of ours: a loopback address, or an interface's.
uint32_t ipv4_source (struct netif *netif, uint32_t dst)
 Chooses the source address of a packet to dst sent through netif.
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.
struct netif * ipv4_route (uint32_t dst)
 Chooses the interface to send a packet to dst through: the one whose link it is on, or else the first one with a gateway.
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 dst isn't on the link, once ARP knows the hardware address.
int ipv4_send (uint32_t dst, uint8_t proto, struct netbuf *nb)
 Sends a packet to dst, through the interface its route says.

Variables

static uint16_t ip_id
static const uint8_t ether_broadcast [6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

Detailed Description

IPv4.

A packet for us goes to its protocol: ICMP, UDP or TCP. A packet to send gets its IPv4 header here, and goes to its destination directly if that is on the interface's link, or through the interface's gateway otherwise. Fragments aren't supported either way: a fragment that arrives is dropped, and what is sent is never fragmented.

Definition in file ipv4.c.

Function Documentation

◆ ipv4_input()

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.

A packet that is malformed, not for us, or a fragment is dropped; one for a protocol we don't have gets a "protocol unreachable".

Parameters
netifthe interface it came in on
nbthe packet, from its IPv4 header; it is freed or passed on
src_macthe sender's MAC address, to update ARP's cache

Definition at line 84 of file ipv4.c.

References arp_update(), netbuf::data, ipv4_hdr::dst, netif::flags, ipv4_hdr::frag, icmp_input(), icmp_unreachable(), inet_checksum(), netif::ip, IP_PROTO_ICMP, IP_PROTO_TCP, IP_PROTO_UDP, ipv4_input(), ipv4_is_local(), is_broadcast(), ipv4_hdr::len, netbuf::len, netbuf_free(), netbuf_pull(), NETIF_LOOPBACK, ntohs, on_link(), ipv4_hdr::proto, ipv4_hdr::src, netif::stats, tcp_input(), udp_input(), and ipv4_hdr::ver_ihl.

Referenced by ether_input(), ipv4_input(), and net_main().

◆ ipv4_is_local()

int ipv4_is_local ( uint32_t ip)

Says whether an address is one of ours: a loopback address, or an interface's.

Parameters
ipthe address, in network byte order
Returns
1 if it is ours, 0 if not

Definition at line 48 of file ipv4.c.

References netif::ip, ipv4_is_local(), is_loopback(), netifs, and netif::next.

Referenced by icmp_input(), ipv4_input(), ipv4_is_local(), ipv4_output(), ipv4_source(), socket_inet_bind(), socket_tcp_bind(), tcp_input(), and udp_input().

◆ ipv4_output()

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 dst isn't on the link, once ARP knows the hardware address.

A packet to one of our own addresses loops back. The netbuf is freed (or kept by ARP until it can be sent).

Parameters
netifthe interface
srcthe source address, or 0 for ipv4_source()'s
dstthe destination
protothe protocol (IP_PROTO_*)
nbthe payload, with room for the headers before it
Returns
0, -ENETUNREACH if there is no gateway to send it through, or a negative errno from the interface

Definition at line 190 of file ipv4.c.

References arp_lookup(), arp_resolve(), ipv4_hdr::checksum, ipv4_hdr::dst, ether_broadcast, ether_output(), ETHER_TYPE_IPV4, ipv4_hdr::frag, netif::gateway, htons, ipv4_hdr::id, inet_checksum(), ip_id, ipv4_is_local(), ipv4_output(), ipv4_source(), is_broadcast(), ipv4_hdr::len, netbuf::len, netbuf_free(), netbuf_push(), netif_loopback(), on_link(), ipv4_hdr::proto, ipv4_hdr::src, netif::stats, ipv4_hdr::tos, ipv4_hdr::ttl, and ipv4_hdr::ver_ihl.

Referenced by icmp_input(), icmp_unreachable(), ipv4_output(), ipv4_send(), segment(), socket_inet_send(), and tcp_reply_rst().

◆ ipv4_route()

struct netif * ipv4_route ( uint32_t dst)

Chooses the interface to send a packet to dst through: the one whose link it is on, or else the first one with a gateway.

Parameters
dstthe destination
Returns
the interface, or NULL if there is no route

Definition at line 142 of file ipv4.c.

References netif::flags, netif::gateway, netif::ip, ipv4_route(), is_loopback(), NETIF_LOOPBACK, NETIF_UP, netifs, netif::next, NULL, and on_link().

Referenced by ipv4_route(), ipv4_send(), our_mss(), segment(), set_mss(), socket_inet_send(), and socket_tcp_connect().

◆ ipv4_send()

int ipv4_send ( uint32_t dst,
uint8_t proto,
struct netbuf * nb )

Sends a packet to dst, through the interface its route says.

The netbuf is freed.

Parameters
dstthe destination
protothe protocol (IP_PROTO_*)
nbthe payload, with room for the headers before it
Returns
0, -ENETUNREACH if there is no route, or a negative errno

Definition at line 244 of file ipv4.c.

References ipv4_output(), ipv4_route(), ipv4_send(), and netbuf_free().

Referenced by ipv4_send().

◆ ipv4_source()

uint32_t ipv4_source ( struct netif * netif,
uint32_t dst )

Chooses the source address of a packet to dst sent through netif.

A packet to one of our own addresses (127.0.0.0/8 too) is sent from that address; any other is sent from the interface's.

Parameters
netifthe interface it is sent through
dstits destination
Returns
the source address

Definition at line 69 of file ipv4.c.

References netif::ip, ipv4_is_local(), and ipv4_source().

Referenced by ipv4_output(), ipv4_source(), socket_inet_send(), and socket_tcp_connect().

◆ is_broadcast()

int is_broadcast ( struct netif * netif,
uint32_t ip )
static

Returns whether ip is a broadcast address on netif's link.

Definition at line 30 of file ipv4.c.

References netif::ip, is_broadcast(), and netif::netmask.

Referenced by ipv4_input(), ipv4_output(), and is_broadcast().

◆ is_loopback()

int is_loopback ( uint32_t ip)
static

Returns whether ip is a loopback address (127.0.0.0/8).

Definition at line 36 of file ipv4.c.

References is_loopback(), and ntohl.

Referenced by ipv4_is_local(), ipv4_route(), and is_loopback().

◆ on_link()

int on_link ( struct netif * netif,
uint32_t ip )
static

Returns whether ip is on netif's link (its network).

Definition at line 24 of file ipv4.c.

References netif::ip, netif::netmask, and on_link().

Referenced by ipv4_input(), ipv4_output(), ipv4_route(), and on_link().

Variable Documentation

◆ ether_broadcast

const uint8_t ether_broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
static

Definition at line 21 of file ipv4.c.

◆ ip_id

uint16_t ip_id
static

Definition at line 19 of file ipv4.c.

Referenced by ipv4_output().