Add networking stack.

This change adds all the kernel parts of a network stack. The network stack
is partial but implements many of the important parts.

Add if(4) network interface abstraction. Network interfaces are registered
in a global list that can be iterated and each assigned an unique integer
identifier.

Add reference counted packets with a cache that recycles recent packets.

Add support for lo(4) loopback and ether(4) ethernet network interfaces.
The /dev/lo0 loopback device is created automatically on boot.

Add arp(4) address resolution protocol driver for translation of inet(4)
network layer addresses into ether(4) link layer addresses. arp(4) entries
are cached and evicted from the cache when needed or when the entry has not
been used for a while. The cache is limited to 256 entries for now.

Add ip(4) internet protocol version 4 support. IP fragmentation and options
are not implemented yet.

Add tcp(4) transmission control protocol sockets for a reliable transport
layer protocol that provides a reliable byte stream connection between two
hosts. The implementation is incomplete and does not yet implement out of
band data, options, and high performance extensions.

Add udp(4) user datagram protocol sockets for a connectionless transport
layer that provides best-effort delivery of datagrams.

Add ping(4) sockets for a best-effort delivery of echo datagrams.

Change type of sa_family_t from unsigned short to uint16_t.

Add --disable-network-drivers to the kernel(7) options and expose it with a
bootloader menu. tix-iso-bootconfig can set this option by default.

Import CRC32 code from libz for the Ethernet checksum.

This is a compatible ABI change that adds features to socket(2) (AF_INET,
IPPROTO_TCP, IPPROTO_UDP, IPPROTO_PING), the ioctls for if(4), socket
options, and the lo0 loopback interface.

This commit is based on work by Meisaka Yukara contributed as the commit
bbf7f1e8a5238a2bd1fe8eb1d2cc5c9c2421e2c4. Almost no lines of this work
remains in this final commit as it has been rewritten or refactored away
over the years, see the individual file headers for which files contain
remnants of this work.

Co-authored-by: Meisaka Yukara <Meisaka.Yukara@gmail.com>
This commit is contained in:
Jonas 'Sortie' Termansen 2022-12-05 00:35:21 +01:00
parent 3154492dcf
commit 2ef6804ead
63 changed files with 11172 additions and 69 deletions

77
libc/include/net/if.h Normal file
View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2016, 2017 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* net/if.h
* Network interface.
*/
#ifndef _INCLUDE_NET_IF_H
#define _INCLUDE_NET_IF_H
#include <sys/cdefs.h>
#define IF_NAMESIZE 32
#if __USE_SORTIX
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <stddef.h>
#include <stdint.h>
#define IF_HWADDR_MAXSIZE 6
#define IF_TYPE_LOOPBACK 1
#define IF_TYPE_ETHERNET 2
#define IF_FEATURE_ETHERNET_CRC_OFFLOAD (1 << 0)
struct if_info
{
unsigned int linkid;
int type;
int features;
size_t addrlen;
char name[IF_NAMESIZE];
unsigned char addr[IF_HWADDR_MAXSIZE];
};
#define IF_STATUS_FLAGS_UP (1 << 0)
struct if_status
{
int flags;
size_t mtu;
};
struct if_config_ether
{
struct ether_addr address;
};
struct if_config_inet
{
struct in_addr address;
struct in_addr router;
struct in_addr subnet;
};
struct if_config
{
struct if_config_ether ether;
struct if_config_inet inet;
};
#endif
#endif

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2016, 2017 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* netinet/if_ether.h
* Ethernet interfaces.
*/
#ifndef _INCLUDE_NETINET_IF_ETHER_H
#define _INCLUDE_NETINET_IF_ETHER_H
#include <sys/cdefs.h>
#include <stdint.h>
#define ETHER_ADDR_LEN 6
#define ETHER_TYPE_LEN 2
#define ETHER_HDR_LEN (ETHER_ADDR_LEN + ETHER_ADDR_LEN + ETHER_TYPE_LEN) /* 14 */
#define ETHER_CRC_LEN 4
#define ETHER_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN) /* 18 */
#define ETHER_MIN_LEN 64
#define ETHER_MAX_LEN 1518
#define ETHERMTU (ETHER_MAX_LEN - ETHER_LEN) /* 1500 */
#define ETHERMIN (ETHER_MIN_LEN - ETHER_LEN) /* 46 */
#define ETHERADDR_BROADCAST_INIT { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }
#define ETHERTYPE_IP 0x0800
#define ETHERTYPE_ARP 0x0806
#define ETHERTYPE_IPV6 0x86dd
struct ether_addr
{
uint8_t ether_addr_octet[ETHER_ADDR_LEN];
};
struct ether_header
{
uint8_t ether_dhost[ETHER_ADDR_LEN];
uint8_t ether_shost[ETHER_ADDR_LEN];
uint16_t ether_type;
};
struct ether_footer
{
uint32_t ether_crc;
};
#ifdef __cplusplus
extern "C" {
#endif
extern const struct ether_addr etheraddr_broadcast; /* ff:ff:ff:ff:ff:ff */
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2014, 2015 Jonas 'Sortie' Termansen.
* Copyright (c) 2013, 2014, 2015, 2016, 2017 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -23,14 +23,11 @@
#include <sys/cdefs.h>
#include <sys/__/types.h>
#include <__/endian.h>
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __in_port_t_defined
#define __in_port_t_defined
typedef uint16_t in_port_t;
@ -43,7 +40,7 @@ typedef uint32_t in_addr_t;
#ifndef __sa_family_t_defined
#define __sa_family_t_defined
typedef unsigned short int sa_family_t;
typedef uint16_t sa_family_t;
#endif
#ifndef __socklen_t_defined
@ -77,8 +74,6 @@ struct sockaddr_in6
uint32_t sin6_scope_id;
};
extern const struct in6_addr in6addr_any; /* :: */
extern const struct in6_addr in6addr_loopback; /* ::1 */
#define IN6ADDR_ANY_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } }
#define IN6ADDR_LOOPBACK_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } }
@ -90,16 +85,20 @@ struct ipv6_mreq
/* #define SOL_SOCKET 0 - in <sys/socket.h> */
#define IPPROTO_ICMP 1
#define IPPROTO_IP 2
#define IPPROTO_IPV6 3
#define IPPROTO_RAW 4
#define IPPROTO_TCP 5
#define IPPROTO_UDP 6
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
#define IPPROTO_RAW 255
#define IPPROTO_IP 256
#define IPPROTO_IPV6 257
#define IPPROTO_PING 258
#define INADDR_ANY ((in_addr_t) 0x00000000)
#define INADDR_BROADCAST ((in_addr_t) 0xffffffff)
#define INADDR_NONE ((in_addr_t) 0xffffffff)
#define INADDR_LOOPBACK ((in_addr_t) 0x7f000001)
#define INADDR_ANY ((in_addr_t) 0x00000000) /* 0.0.0.0 */
#define INADDR_BROADCAST ((in_addr_t) 0xffffffff) /* 255.255.255.255 */
#define INADDR_LOOPBACK ((in_addr_t) 0x7f000001) /* 127.0.0.1 */
#define INADDR_NONE ((in_addr_t) 0xffffffff) /* 255.255.255.255 */
#if __USE_SORTIX
#define INADDR_LOOPMASK ((in_addr_t) 0xff000000) /* 255.0.0.0 */
#endif
#define INADDR_UNSPEC_GROUP ((in_addr_t) 0xe0000000)
#define INADDR_ALLHOSTS_GROUP ((in_addr_t) 0xe0000001)
@ -178,6 +177,15 @@ struct ipv6_mreq
#define IN_LOOPBACKNET 127
#define IP_EVIL_INTENT 1
#ifdef __cplusplus
extern "C" {
#endif
extern const struct in6_addr in6addr_any; /* :: */
extern const struct in6_addr in6addr_loopback; /* ::1 */
#ifdef __cplusplus
} /* extern "C" */
#endif

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2018 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* netinet/ping.h
* Internet Control Message Protocol Echo.
*/
#ifndef _INCLUDE_NETINET_PING_H
#define _INCLUDE_NETINET_PING_H
#include <sys/cdefs.h>
#include <netinet/in.h>
#endif

View file

@ -14,7 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* netinet/tcp.h
* Definitions for the Internet Transmission Control Protocol.
* Transmission Control Protocol.
*/
#ifndef _INCLUDE_NETINET_TCP_H
@ -22,6 +22,69 @@
#include <sys/cdefs.h>
#if __USE_SORTIX
#include <netinet/in.h>
#endif
#if __USE_SORTIX
typedef uint32_t tcp_seq; /* TCP sequence number. */
/* Control Bits in struct tcphdr th_flags. */
#define TH_FIN (1 << 0) /* No more data from sender. */
#define TH_SYN (1 << 1) /* Synchronize sequence numbers. */
#define TH_RST (1 << 2) /* Reset the connection. */
#define TH_PUSH (1 << 3) /* Push Function. */
#define TH_ACK (1 << 4) /* Acknowledgment field significant. */
#define TH_URG (1 << 5) /* Urgent Pointer field significant. */
struct tcphdr
{
in_port_t th_sport; /* Source Port. */
in_port_t th_dport; /* Destination Port. */
tcp_seq th_seq; /* Sequence Number. */
tcp_seq th_ack; /* Acknowledgment Number. */
__extension__ union
{
__extension__ struct
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t th_x2:4; /* Reserved. */
uint8_t th_off:4; /* Data offset. */
#elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t th_off:4; /* Data offset. */
uint8_t th_x2:4; /* Reserved. */
#else
#warning "You need to add support for your endian"
#endif
};
uint8_t th_offset;
};
uint8_t th_flags; /* Control Bits. */
uint16_t th_win; /* Window. */
uint16_t th_sum; /* Checksum. */
uint16_t th_urp; /* Urgent Pointer. */
};
#define TCP_OFFSET_ENCODE(x) (((x) & 0xF) << 4) /* Encode th_offset. */
#define TCP_OFFSET_DECODE(x) (((x) >> 4) & 0xF) /* Decode th_offset. */
#define TCP_MSS 536 /* Default Maximum Segment Size. */
#define TCPOPT_EOL 0 /* End of Option List. */
#define TCPOPT_NOP 1 /* No-Operation. */
#define TCPOPT_MAXSEG 2 /* Maximum Segment Size. */
#define TCPOLEN_MAXSEG 4 /* Length of Maximum Segment Size. */
/* Maximum header size: 16 * 4 bytes */
#define TCP_MAXHLEN 64
/* Maximum total length of options. */
#define TCP_MAXOLEN (TCP_MAXHLEN - sizeof(struct tcphdr))
#define TCP_MAXWIN 65535 /* Maximum window size. */
#endif
/* Options at the IPPROTO_TCP socket level. */
#define TCP_NODELAY 1
#if __USE_SORTIX

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2018 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* netinet/udp.h
* User Datagram Protocol.
*/
#ifndef _INCLUDE_NETINET_UDP_H
#define _INCLUDE_NETINET_UDP_H
#include <sys/cdefs.h>
#include <netinet/in.h>
struct udphdr
{
in_port_t uh_sport; /* Source Port */
in_port_t uh_dport; /* Destination Port */
uint16_t uh_ulen; /* Length */
uint16_t uh_sum; /* Checksum */
};
#endif

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2014 Jonas 'Sortie' Termansen.
* Copyright (c) 2013, 2014, 2016, 2017 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -24,11 +24,9 @@
#include <sys/__/types.h>
#include <sortix/socket.h>
#include <__/stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <sortix/socket.h>
#ifndef __socklen_t_defined
#define __socklen_t_defined
@ -48,19 +46,11 @@ typedef __ssize_t ssize_t;
#ifndef __sa_family_t_defined
#define __sa_family_t_defined
typedef unsigned short int sa_family_t;
#endif
#ifdef __cplusplus
} /* extern "C" */
typedef __uint16_t sa_family_t;
#endif
#include <sortix/uio.h>
#ifdef __cplusplus
extern "C" {
#endif
struct sockaddr
{
sa_family_t sa_family;
@ -139,8 +129,14 @@ struct linger
#define SO_SNDLOWAT 14
#define SO_SNDTIMEO 15
#define SO_TYPE 16
#if __USE_SORTIX
#define SO_BINDTOINDEX 17
#define SO_BINDTODEVICE 18
#define SO_DOMAIN 19
#define SO_PROTOCOL 20
#endif
#define SOMAXCONN 5
#define SOMAXCONN 128
#define MSG_CTRUNC (1<<0)
#define MSG_DONTROUTE (1<<1)
@ -174,6 +170,10 @@ struct linger
#define SHUT_WR (1 << 1)
#define SHUT_RDWR (SHUT_RD | SHUT_WR)
#ifdef __cplusplus
extern "C" {
#endif
int accept4(int, struct sockaddr* __restrict, socklen_t* __restrict, int);
int accept(int, struct sockaddr* __restrict, socklen_t* __restrict);
int bind(int, const struct sockaddr*, socklen_t);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 Jonas 'Sortie' Termansen.
* Copyright (c) 2013, 2017 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -24,13 +24,15 @@
#include <sys/__/types.h>
#include <__/stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __sa_family_t_defined
#define __sa_family_t_defined
typedef unsigned short int sa_family_t;
typedef __uint16_t sa_family_t;
#endif
struct sockaddr_un