toaruos/kernel/net/ipv4.c

686 lines
22 KiB
C
Raw Normal View History

2021-05-31 04:47:02 +03:00
/**
* @file kernel/net/ipv4.c
* @brief IPv4 protocol implementation.
*
* @copyright This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* @author 2021 K. Lange
*/
#include <errno.h>
#include <kernel/types.h>
2021-06-10 14:48:48 +03:00
#include <kernel/string.h>
2021-05-31 04:47:02 +03:00
#include <kernel/printf.h>
#include <kernel/syscall.h>
2021-06-14 08:18:23 +03:00
#include <kernel/hashmap.h>
2021-05-31 04:47:02 +03:00
#include <kernel/vfs.h>
2021-06-10 14:20:55 +03:00
#include <kernel/net/netif.h>
2021-06-10 14:48:48 +03:00
#include <kernel/net/eth.h>
2021-06-14 15:35:28 +03:00
#include <kernel/net/ipv4.h>
2021-06-10 14:20:55 +03:00
2021-05-31 04:47:02 +03:00
#include <sys/socket.h>
#ifndef MISAKA_DEBUG_NET
#define printf(...)
#endif
2021-06-10 14:20:55 +03:00
static void ip_ntoa(const uint32_t src_addr, char * out) {
snprintf(out, 16, "%d.%d.%d.%d",
(src_addr & 0xFF000000) >> 24,
(src_addr & 0xFF0000) >> 16,
(src_addr & 0xFF00) >> 8,
(src_addr & 0xFF));
}
2021-06-10 14:48:48 +03:00
static uint16_t icmp_checksum(struct ipv4_packet * packet) {
uint32_t sum = 0;
uint16_t * s = (uint16_t *)packet->payload;
for (int i = 0; i < (ntohs(packet->length) - 20) / 2; ++i) {
sum += ntohs(s[i]);
}
if (sum > 0xFFFF) {
sum = (sum >> 16) + (sum & 0xFFFF);
}
return ~(sum & 0xFFFF) & 0xFFFF;
}
uint16_t calculate_ipv4_checksum(struct ipv4_packet * p) {
uint32_t sum = 0;
uint16_t * s = (uint16_t *)p;
/* TODO: Checksums for options? */
for (int i = 0; i < 10; ++i) {
sum += ntohs(s[i]);
}
if (sum > 0xFFFF) {
sum = (sum >> 16) + (sum & 0xFFFF);
}
return ~(sum & 0xFFFF) & 0xFFFF;
}
2021-06-14 12:55:03 +03:00
uint16_t calculate_tcp_checksum(struct tcp_check_header * p, struct tcp_header * h, void * d, size_t payload_size) {
uint32_t sum = 0;
uint16_t * s = (uint16_t *)p;
/* TODO: Checksums for options? */
for (int i = 0; i < 6; ++i) {
sum += ntohs(s[i]);
if (sum > 0xFFFF) {
sum = (sum >> 16) + (sum & 0xFFFF);
}
}
s = (uint16_t *)h;
for (int i = 0; i < 10; ++i) {
sum += ntohs(s[i]);
if (sum > 0xFFFF) {
sum = (sum >> 16) + (sum & 0xFFFF);
}
}
uint16_t d_words = payload_size / 2;
s = (uint16_t *)d;
for (unsigned int i = 0; i < d_words; ++i) {
sum += ntohs(s[i]);
if (sum > 0xFFFF) {
sum = (sum >> 16) + (sum & 0xFFFF);
}
}
if (d_words * 2 != payload_size) {
uint8_t * t = (uint8_t *)d;
uint8_t tmp[2];
tmp[0] = t[d_words * sizeof(uint16_t)];
tmp[1] = 0;
uint16_t * f = (uint16_t *)tmp;
sum += ntohs(f[0]);
if (sum > 0xFFFF) {
sum = (sum >> 16) + (sum & 0xFFFF);
}
}
return ~(sum & 0xFFFF) & 0xFFFF;
}
2021-06-10 14:48:48 +03:00
static void icmp_handle(struct ipv4_packet * packet, const char * src, const char * dest, fs_node_t * nic) {
struct icmp_header * header = (void*)&packet->payload;
if (header->type == 8 && header->code == 0) {
printf("net: ping with %d bytes of payload\n", ntohs(packet->length));
if (ntohs(packet->length) & 1) packet->length++;
struct ipv4_packet * response = malloc(ntohs(packet->length));
response->length = packet->length;
response->destination = packet->source;
response->source = ((struct EthernetDevice*)nic->device)->ipv4_addr;
response->ttl = 64;
response->protocol = 1;
response->ident = packet->ident;
response->flags_fragment = htons(0x4000);
response->version_ihl = 0x45;
response->dscp_ecn = 0;
response->checksum = 0;
response->checksum = htons(calculate_ipv4_checksum(response));
memcpy(response->payload, packet->payload, ntohs(packet->length));
struct icmp_header * ping_reply = (void*)&response->payload;
ping_reply->csum = 0;
ping_reply->type = 0;
ping_reply->csum = htons(icmp_checksum(response));
/* send ipv4... */
net_eth_send((struct EthernetDevice*)nic->device, ntohs(response->length), response, ETHERNET_TYPE_IPV4, ETHERNET_BROADCAST_MAC);
free(response);
} else {
printf("net: ipv4: %s: %s -> %s ICMP %d (code = %d)\n", nic->name, src, dest, header->type, header->code);
}
}
2021-06-14 08:18:23 +03:00
static hashmap_t * udp_sockets = NULL;
static hashmap_t * tcp_sockets = NULL;
2021-06-14 08:18:23 +03:00
2021-06-14 12:55:03 +03:00
#define TCP_FLAGS_FIN (1 << 0)
#define TCP_FLAGS_SYN (1 << 1)
#define TCP_FLAGS_RES (1 << 2)
#define TCP_FLAGS_PSH (1 << 3)
#define TCP_FLAGS_ACK (1 << 4)
#define TCP_FLAGS_URG (1 << 5)
#define TCP_FLAGS_ECE (1 << 6)
#define TCP_FLAGS_CWR (1 << 7)
#define TCP_FLAGS_NS (1 << 8)
#define DATA_OFFSET_5 (0x5 << 12)
2021-06-14 15:35:28 +03:00
static void tcp_ack(fs_node_t * nic, sock_t * sock, struct ipv4_packet * packet, int isSynAck, size_t payload_len) {
2021-06-14 12:55:03 +03:00
sock->priv[1] = 2;
size_t total_length = sizeof(struct ipv4_packet) + sizeof(struct tcp_header);
struct tcp_header * tcp = (struct tcp_header*)&packet->payload;
struct ipv4_packet * response = malloc(total_length);
response->length = htons(total_length);
response->destination = packet->source;
response->source = ((struct EthernetDevice*)nic->device)->ipv4_addr;
response->ttl = 64;
response->protocol = IPV4_PROT_TCP;
2021-06-14 15:35:28 +03:00
response->ident = htons(1);
response->flags_fragment = htons(0x0);
2021-06-14 12:55:03 +03:00
response->version_ihl = 0x45;
response->dscp_ecn = 0;
response->checksum = 0;
response->checksum = htons(calculate_ipv4_checksum(response));
2021-06-14 15:35:28 +03:00
sock->priv32[0] = isSynAck ? 1 : sock->priv32[0];
sock->priv32[1] = (ntohl(tcp->seq_number) + payload_len) & 0xFFFFFFFF;
2021-06-14 12:55:03 +03:00
2021-06-15 13:19:54 +03:00
int flags = TCP_FLAGS_ACK;
if (ntohs(tcp->flags) & TCP_FLAGS_FIN) {
/* Other side is closed now */
sock->priv32[1]++;
}
2021-06-14 12:55:03 +03:00
/* Stick TCP header into payload */
struct tcp_header * tcp_header = (struct tcp_header*)&response->payload;
tcp_header->source_port = htons(sock->priv[0]);
tcp_header->destination_port = tcp->source_port;
2021-06-14 15:35:28 +03:00
tcp_header->seq_number = htonl(sock->priv32[0]);
tcp_header->ack_number = htonl(sock->priv32[1]);
2021-06-15 13:19:54 +03:00
tcp_header->flags = htons(flags | 0x5000);
2021-06-14 12:55:03 +03:00
tcp_header->window_size = htons(1548-54);
tcp_header->checksum = 0;
tcp_header->urgent = 0;
/* Calculate checksum */
struct tcp_check_header check_hd = {
.source = response->source,
.destination = response->destination,
.zeros = 0,
.protocol = IPV4_PROT_TCP,
.tcp_len = htons(sizeof(struct tcp_header)),
};
tcp_header->checksum = htons(calculate_tcp_checksum(&check_hd, tcp_header, NULL, 0));
net_eth_send((struct EthernetDevice*)nic->device, ntohs(response->length), response, ETHERNET_TYPE_IPV4, ETHERNET_BROADCAST_MAC);
}
2021-06-09 02:23:34 +03:00
void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) {
2021-06-10 14:20:55 +03:00
char dest[16];
char src[16];
ip_ntoa(ntohl(packet->destination), dest);
ip_ntoa(ntohl(packet->source), src);
2021-06-09 02:23:34 +03:00
switch (packet->protocol) {
2021-06-10 03:09:52 +03:00
case 1:
2021-06-10 14:48:48 +03:00
icmp_handle(packet, src, dest, nic);
2021-06-10 03:09:52 +03:00
break;
2021-06-14 08:18:23 +03:00
case IPV4_PROT_UDP: {
uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]);
2021-06-14 12:55:03 +03:00
printf("net: ipv4: %s: %s -> %s udp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port);
2021-06-14 08:18:23 +03:00
if (udp_sockets && hashmap_has(udp_sockets, (void*)(uintptr_t)dest_port)) {
printf("net: udp: received and have a waiting endpoint!\n");
sock_t * sock = hashmap_get(udp_sockets, (void*)(uintptr_t)dest_port);
net_sock_add(sock, packet, ntohs(packet->length));
2021-06-14 08:18:23 +03:00
}
2021-06-09 02:23:34 +03:00
break;
2021-06-14 08:18:23 +03:00
}
2021-06-14 12:55:03 +03:00
case IPV4_PROT_TCP: {
uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]);
printf("net: ipv4: %s: %s -> %s tcp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port);
2021-06-14 12:55:03 +03:00
if (tcp_sockets && hashmap_has(tcp_sockets, (void*)(uintptr_t)dest_port)) {
printf("net: tcp: received and have a waiting endpoint!\n");
2021-06-14 12:55:03 +03:00
/* What kind of packet is this? Is it something we were expecting? */
sock_t * sock = hashmap_get(tcp_sockets, (void*)(uintptr_t)dest_port);
struct tcp_header * tcp = (struct tcp_header*)&packet->payload;
if (sock->priv[1] == 1) {
/* Awaiting SYN ACK, is this one? */
if ((ntohs(tcp->flags) & (TCP_FLAGS_SYN | TCP_FLAGS_ACK)) == (TCP_FLAGS_SYN | TCP_FLAGS_ACK)) {
printf("tcp: synack\n");
2021-06-14 15:35:28 +03:00
tcp_ack(nic, sock, packet, 1, 1);
net_sock_add(sock, packet, ntohs(packet->length));
2021-06-14 12:55:03 +03:00
}
} else if (sock->priv[1] == 2) {
2021-06-14 15:35:28 +03:00
size_t packet_len = ntohs(packet->length) - sizeof(struct ipv4_packet);
size_t hlen = ((ntohs(tcp->flags) & 0xF000) >> 12) * 4;
size_t payload_len = packet_len - hlen;
if (payload_len) {
printf("tcp: acking because payload_len = %zu (hlen=%zu, packet_len=%zu)\n", payload_len, hlen, packet_len);
2021-06-14 15:35:28 +03:00
tcp_ack(nic, sock, packet, 0, payload_len);
net_sock_add(sock, packet, ntohs(packet->length));
2021-06-15 13:19:54 +03:00
} else if (ntohs(tcp->flags) & TCP_FLAGS_FIN) {
tcp_ack(nic, sock, packet, 0, 0);
2021-06-14 15:35:28 +03:00
}
2021-06-14 12:55:03 +03:00
}
}
2021-06-09 02:23:34 +03:00
break;
2021-06-14 12:55:03 +03:00
}
2021-06-09 02:23:34 +03:00
}
}
2021-06-14 08:18:23 +03:00
extern fs_node_t * net_if_any(void);
static spin_lock_t udp_port_lock = {0};
static int next_port = 12345;
static int udp_get_port(sock_t * sock) {
spin_lock(udp_port_lock);
int out = next_port++;
if (!udp_sockets) {
udp_sockets = hashmap_create_int(10);
}
hashmap_set(udp_sockets, (void*)(uintptr_t)out, sock);
sock->priv[0] = out;
spin_unlock(udp_port_lock);
return out;
}
static long sock_udp_send(sock_t * sock, const struct msghdr *msg, int flags) {
printf("udp: send called\n");
if (msg->msg_iovlen > 1) {
printf("net: todo: can't send multiple iovs\n");
return -ENOTSUP;
}
if (msg->msg_iovlen == 0) return 0;
if (msg->msg_namelen != sizeof(struct sockaddr_in)) {
printf("udp: invalid destination address size %ld\n", msg->msg_namelen);
return -EINVAL;
}
if (sock->priv[0] == 0) {
udp_get_port(sock);
printf("udp: assigning port %d to socket\n", sock->priv[0]);
}
struct sockaddr_in * name = msg->msg_name;
char dest[16];
ip_ntoa(ntohl(name->sin_addr.s_addr), dest);
printf("udp: want to send to %s\n", dest);
/* Routing: We need a device to send this on... */
fs_node_t * nic = net_if_any();
size_t total_length = sizeof(struct ipv4_packet) + msg->msg_iov[0].iov_len + sizeof(struct udp_packet);
struct ipv4_packet * response = malloc(total_length);
response->length = htons(total_length);
response->destination = name->sin_addr.s_addr;
response->source = ((struct EthernetDevice*)nic->device)->ipv4_addr;
response->ttl = 64;
response->protocol = IPV4_PROT_UDP;
response->ident = 0;
response->flags_fragment = htons(0x4000);
response->version_ihl = 0x45;
response->dscp_ecn = 0;
response->checksum = 0;
response->checksum = htons(calculate_ipv4_checksum(response));
/* Stick UDP header into payload */
struct udp_packet * udp_packet = (struct udp_packet*)&response->payload;
2021-06-14 08:18:23 +03:00
udp_packet->source_port = htons(sock->priv[0]);
udp_packet->destination_port = name->sin_port;
udp_packet->length = htons(sizeof(struct udp_packet) + msg->msg_iov[0].iov_len);
udp_packet->checksum = 0;
memcpy(response->payload + sizeof(struct udp_packet), msg->msg_iov[0].iov_base, msg->msg_iov[0].iov_len);
net_eth_send((struct EthernetDevice*)nic->device, ntohs(response->length), response, ETHERNET_TYPE_IPV4, ETHERNET_BROADCAST_MAC);
free(response);
return 0;
}
static long sock_udp_recv(sock_t * sock, struct msghdr * msg, int flags) {
printf("udp: recv called\n");
if (!sock->priv[0]) {
printf("udp: recv() but socket has no port\n");
return -EINVAL;
}
if (msg->msg_iovlen > 1) {
printf("net: todo: can't recv multiple iovs\n");
return -ENOTSUP;
}
if (msg->msg_iovlen == 0) return 0;
struct ipv4_packet * data = net_sock_get(sock);
printf("udp: got response, size is %u - sizeof(ipv4) - sizeof(udp) = %lu\n",
ntohs(data->length), ntohs(data->length) - sizeof(struct ipv4_packet) - sizeof(struct udp_packet));
memcpy(msg->msg_iov[0].iov_base, data->payload + 8, ntohs(data->length) - sizeof(struct ipv4_packet) - sizeof(struct udp_packet));
printf("udp: data copied to iov 0, return length?\n");
long resp = ntohs(data->length) - sizeof(struct ipv4_packet) - sizeof(struct udp_packet);
free(data);
return resp;
}
static void sock_udp_close(sock_t * sock) {
if (sock->priv[0] && udp_sockets) {
printf("udp: removing port %d from bound map\n", sock->priv[0]);
spin_lock(udp_port_lock);
hashmap_remove(udp_sockets, (void*)(uintptr_t)sock->priv[0]);
spin_unlock(udp_port_lock);
}
}
static int udp_socket(void) {
printf("udp socket...\n");
sock_t * sock = net_sock_create();
sock->sock_recv = sock_udp_recv;
sock->sock_send = sock_udp_send;
sock->sock_close = sock_udp_close;
return process_append_fd((process_t *)this_core->current_process, (fs_node_t *)sock);
}
static spin_lock_t tcp_port_lock = {0};
static void sock_tcp_close(sock_t * sock) {
if (sock->priv[0] && tcp_sockets) {
printf("tcp: removing port %d from bound map\n", sock->priv[0]);
spin_lock(tcp_port_lock);
hashmap_remove(tcp_sockets, (void*)(uintptr_t)sock->priv[0]);
spin_unlock(tcp_port_lock);
2021-06-15 13:19:54 +03:00
size_t total_length = sizeof(struct ipv4_packet) + sizeof(struct tcp_header);
fs_node_t * nic = net_if_any();
struct ipv4_packet * response = malloc(total_length);
response->length = htons(total_length);
response->destination = ((struct sockaddr_in*)&sock->dest)->sin_addr.s_addr;
response->source = ((struct EthernetDevice*)nic->device)->ipv4_addr;
response->ttl = 64;
response->protocol = IPV4_PROT_TCP;
response->ident = htons(1);
response->flags_fragment = htons(0x0);
response->version_ihl = 0x45;
response->dscp_ecn = 0;
response->checksum = 0;
response->checksum = htons(calculate_ipv4_checksum(response));
/* Stick TCP header into payload */
struct tcp_header * tcp_header = (struct tcp_header*)&response->payload;
tcp_header->source_port = htons(sock->priv[0]);
tcp_header->destination_port = ((struct sockaddr_in*)&sock->dest)->sin_port;
tcp_header->seq_number = htonl(sock->priv32[0]);
tcp_header->ack_number = htonl(sock->priv32[1]);
tcp_header->flags = htons(TCP_FLAGS_FIN | 0x5000);
tcp_header->window_size = htons(1548-54);
tcp_header->checksum = 0;
tcp_header->urgent = 0;
/* Calculate checksum */
struct tcp_check_header check_hd = {
.source = response->source,
.destination = response->destination,
.zeros = 0,
.protocol = IPV4_PROT_TCP,
.tcp_len = htons(sizeof(struct tcp_header)),
};
tcp_header->checksum = htons(calculate_tcp_checksum(&check_hd, tcp_header, tcp_header->payload, 0));
struct ArpCacheEntry * resp = net_arp_cache_get(response->destination);
net_eth_send((struct EthernetDevice*)nic->device, ntohs(response->length), response, ETHERNET_TYPE_IPV4, resp ? resp->hwaddr : ETHERNET_BROADCAST_MAC);
}
}
2021-06-14 15:35:28 +03:00
static int next_tcp_port = 49152;
static int tcp_get_port(sock_t * sock) {
spin_lock(tcp_port_lock);
int out = next_tcp_port++;
if (!tcp_sockets) {
tcp_sockets = hashmap_create_int(10);
}
hashmap_set(tcp_sockets, (void*)(uintptr_t)out, sock);
sock->priv[0] = out;
spin_unlock(tcp_port_lock);
return out;
}
static long sock_tcp_recv(sock_t * sock, struct msghdr * msg, int flags) {
if (!sock->priv[0]) {
printf("tcp: recv() but socket has no port\n");
return -EINVAL;
}
if (msg->msg_iovlen > 1) {
printf("net: todo: can't recv multiple iovs\n");
return -ENOTSUP;
}
if (msg->msg_iovlen == 0) return 0;
if (sock->unread) {
if (sock->unread > msg->msg_iov[0].iov_len) {
sock->unread = 0;
long out = msg->msg_iov[0].iov_len;
sock->unread -= out;
memcpy(msg->msg_iov[0].iov_base, sock->buf, out);
char * x = malloc(sock->unread);
memcpy(x, sock->buf + out, sock->unread);
free(sock->buf);
sock->buf = x;
return out;
} else {
long out = sock->unread;
sock->unread = 0;
memcpy(msg->msg_iov[0].iov_base, sock->buf, out);
free(sock->buf);
sock->buf = NULL;
return out;
}
}
struct ipv4_packet * data = net_sock_get(sock);
2021-06-14 15:35:28 +03:00
long resp = ntohs(data->length) - sizeof(struct ipv4_packet) - sizeof(struct tcp_header);
if (resp > (long)msg->msg_iov[0].iov_len) {
memcpy(msg->msg_iov[0].iov_base, data->payload + sizeof(struct tcp_header),msg->msg_iov[0].iov_len);
resp -= msg->msg_iov[0].iov_len;
sock->unread = resp;
sock->buf = malloc(resp);
memcpy(sock->buf, data->payload + sizeof(struct tcp_header) + msg->msg_iov[0].iov_len, resp);
free(data);
return msg->msg_iov[0].iov_len;
}
2021-06-14 15:35:28 +03:00
memcpy(msg->msg_iov[0].iov_base, data->payload + sizeof(struct tcp_header), resp);
free(data);
2021-06-14 15:35:28 +03:00
return resp;
}
static long sock_tcp_connect(sock_t * sock, const struct sockaddr *addr, socklen_t addrlen) {
const struct sockaddr_in * dest = (const struct sockaddr_in *)addr;
char deststr[16];
ip_ntoa(ntohl(dest->sin_addr.s_addr), deststr);
printf("tcp: connect requested to %s port %d\n", deststr, ntohs(dest->sin_port));
if (sock->priv[1] != 0) {
printf("tcp: socket is already connected?\n");
return -EINVAL;
}
/* Get a port */
tcp_get_port(sock);
printf("tcp: connecting from ephemeral port %d\n", (int)sock->priv[0]);
/* Mark as awaiting connection, send initial SYN */
sock->priv[1] = 1;
2021-06-14 15:35:28 +03:00
memcpy(&sock->dest, addr, addrlen);
fs_node_t * nic = net_if_any();
size_t total_length = sizeof(struct ipv4_packet) + sizeof(struct tcp_header);
struct ipv4_packet * response = malloc(total_length);
response->length = htons(total_length);
response->destination = dest->sin_addr.s_addr;
response->source = ((struct EthernetDevice*)nic->device)->ipv4_addr;
response->ttl = 64;
response->protocol = IPV4_PROT_TCP;
2021-06-14 15:35:28 +03:00
response->ident = htons(1);
response->flags_fragment = htons(0x0);
response->version_ihl = 0x45;
response->dscp_ecn = 0;
response->checksum = 0;
response->checksum = htons(calculate_ipv4_checksum(response));
/* Stick TCP header into payload */
struct tcp_header * tcp_header = (struct tcp_header*)&response->payload;
tcp_header->source_port = htons(sock->priv[0]);
tcp_header->destination_port = dest->sin_port;
tcp_header->seq_number = 0;
tcp_header->ack_number = 0;
tcp_header->flags = htons((1 << 1) | 0x5000);
tcp_header->window_size = htons(1548-54);
tcp_header->checksum = 0;
tcp_header->urgent = 0;
/* Calculate checksum */
struct tcp_check_header check_hd = {
.source = response->source,
.destination = response->destination,
.zeros = 0,
.protocol = IPV4_PROT_TCP,
.tcp_len = htons(sizeof(struct tcp_header)),
};
tcp_header->checksum = htons(calculate_tcp_checksum(&check_hd, tcp_header, NULL, 0));
/* TODO: enqueue tcp packet for potential redelivery */
net_eth_send((struct EthernetDevice*)nic->device, ntohs(response->length), response, ETHERNET_TYPE_IPV4, ETHERNET_BROADCAST_MAC);
free(response);
printf("tcp: waiting for connect to finish\n");
2021-06-14 12:55:03 +03:00
/* wait for signal that we connected or timed out */
2021-06-14 12:55:03 +03:00
struct ipv4_packet * data = net_sock_get(sock);
printf("tcp: connect complete\n");
2021-06-14 12:55:03 +03:00
free(data);
return 0;
}
2021-06-14 12:55:03 +03:00
ssize_t sock_tcp_read(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
printf("tcp: read into buffer of %zu bytes\n", size);
2021-06-14 15:35:28 +03:00
struct iovec _iovec = {
buffer, size
};
struct msghdr _header = {
.msg_name = NULL,
.msg_namelen = 0,
.msg_iov = &_iovec,
.msg_iovlen = 1,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0,
};
return sock_tcp_recv((sock_t*)node, &_header, 0);
2021-06-14 12:55:03 +03:00
}
2021-06-14 15:35:28 +03:00
static long sock_tcp_send(sock_t * sock, const struct msghdr *msg, int flags) {
printf("tcp: send called\n");
2021-06-14 15:35:28 +03:00
if (msg->msg_iovlen > 1) {
printf("net: todo: can't send multiple iovs\n");
return -ENOTSUP;
}
if (msg->msg_iovlen == 0) return 0;
size_t total_length = sizeof(struct ipv4_packet) + sizeof(struct tcp_header) + msg->msg_iov[0].iov_len;
fs_node_t * nic = net_if_any();
struct ipv4_packet * response = malloc(total_length);
response->length = htons(total_length);
response->destination = ((struct sockaddr_in*)&sock->dest)->sin_addr.s_addr;
response->source = ((struct EthernetDevice*)nic->device)->ipv4_addr;
response->ttl = 64;
response->protocol = IPV4_PROT_TCP;
response->ident = htons(1);
response->flags_fragment = htons(0x0);
response->version_ihl = 0x45;
response->dscp_ecn = 0;
response->checksum = 0;
response->checksum = htons(calculate_ipv4_checksum(response));
/* Stick TCP header into payload */
struct tcp_header * tcp_header = (struct tcp_header*)&response->payload;
tcp_header->source_port = htons(sock->priv[0]);
tcp_header->destination_port = ((struct sockaddr_in*)&sock->dest)->sin_port;
tcp_header->seq_number = htonl(sock->priv32[0]);
tcp_header->ack_number = htonl(sock->priv32[1]);
tcp_header->flags = htons(TCP_FLAGS_PSH | TCP_FLAGS_ACK | 0x5000);
tcp_header->window_size = htons(1548-54);
tcp_header->checksum = 0;
tcp_header->urgent = 0;
sock->priv32[0] += msg->msg_iov[0].iov_len;
/* Calculate checksum */
struct tcp_check_header check_hd = {
.source = response->source,
.destination = response->destination,
.zeros = 0,
.protocol = IPV4_PROT_TCP,
.tcp_len = htons(sizeof(struct tcp_header) + msg->msg_iov[0].iov_len),
};
memcpy(tcp_header->payload, msg->msg_iov[0].iov_base, msg->msg_iov[0].iov_len);
tcp_header->checksum = htons(calculate_tcp_checksum(&check_hd, tcp_header, tcp_header->payload, msg->msg_iov[0].iov_len));
struct ArpCacheEntry * resp = net_arp_cache_get(response->destination);
net_eth_send((struct EthernetDevice*)nic->device, ntohs(response->length), response, ETHERNET_TYPE_IPV4, resp ? resp->hwaddr : ETHERNET_BROADCAST_MAC);
return msg->msg_iov[0].iov_len;
}
2021-06-14 12:55:03 +03:00
ssize_t sock_tcp_write(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
printf("tcp: write of %zu bytes\n", size);
2021-06-14 15:35:28 +03:00
struct iovec _iovec = {
(void*)buffer, size
};
struct msghdr _header = {
.msg_name = NULL,
.msg_namelen = 0,
.msg_iov = &_iovec,
.msg_iovlen = 1,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0,
};
return sock_tcp_send((sock_t*)node, &_header, 0);
2021-06-14 12:55:03 +03:00
}
static int tcp_socket(void) {
printf("tcp socket...\n");
sock_t * sock = net_sock_create();
sock->sock_recv = sock_tcp_recv;
sock->sock_send = sock_tcp_send;
sock->sock_close = sock_tcp_close;
sock->sock_connect = sock_tcp_connect;
2021-06-14 12:55:03 +03:00
sock->_fnode.read = sock_tcp_read;
sock->_fnode.write = sock_tcp_write;
int fd = process_append_fd((process_t *)this_core->current_process, (fs_node_t *)sock);
FD_MODE(fd) = 03;
return fd;
}
2021-05-31 04:47:02 +03:00
long net_ipv4_socket(int type, int protocol) {
/* Ignore protocol, make socket for 'type' only... */
switch (type) {
case SOCK_DGRAM:
2021-06-14 08:18:23 +03:00
return udp_socket();
2021-05-31 04:47:02 +03:00
case SOCK_STREAM:
return tcp_socket();
2021-05-31 04:47:02 +03:00
default:
return -EINVAL;
}
}