net: more cleanup

This commit is contained in:
K. Lange 2021-06-10 20:20:55 +09:00
parent 8531fe709e
commit 394e4e3e0c
9 changed files with 108 additions and 40 deletions

View File

@ -108,6 +108,9 @@ $(BASE)/lib/ld.so: linker/linker.c $(BASE)/lib/libc.a | dirs $(LC)
run: system
${EMU} ${EMU_ARGS} ${EMU_KVM} -append "root=/dev/ram0 start=live-session migrate $(EXTRA_ARGS)" -initrd ramdisk.igz
run-vga: system
${EMU} ${EMU_ARGS} ${EMU_KVM} -append "root=/dev/ram0 vid=text start=--vga migrate $(EXTRA_ARGS)" -initrd ramdisk.igz
shell: system
${EMU} -m $(RAM) -smp $(SMP) ${EMU_KVM} -kernel misaka-kernel -append "root=/dev/ram0 start=--headless migrate" -initrd ramdisk.igz \
-nographic -no-reboot -audiodev none,id=id -serial null -serial mon:stdio \

View File

@ -14,6 +14,7 @@
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
extern char * _argv_0;
@ -54,21 +55,21 @@ static int configure_interface(const char * if_name) {
}
uint32_t flags = 0;
ioctl(netdev, 0x12340005, &flags);
ioctl(netdev, SIOCGIFFLAGS, &flags);
uint32_t mtu = 0;
ioctl(netdev, 0x12340006, &mtu);
ioctl(netdev, SIOCGIFMTU, &mtu);
fprintf(stdout,"%s: flags=%d<%s> mtu %d\n", if_name, flags, flagsToStr(flags), mtu);
/* Get IPv4 address */
uint32_t ip_addr = 0;
if (!ioctl(netdev, 0x12340002, &ip_addr)) {
if (!ioctl(netdev, SIOCGIFADDR, &ip_addr)) {
char ip_str[16];
ip_ntoa(ntohl(ip_addr), ip_str);
fprintf(stdout," inet %s", ip_str);
/* Netmask ? */
uint32_t netmask = 0;
if (!ioctl(netdev, 0x12340004, &netmask)) {
if (!ioctl(netdev, SIOCGIFNETMASK, &netmask)) {
ip_ntoa(ntohl(netmask), ip_str);
fprintf(stdout, " netmask %s", ip_str);
@ -80,7 +81,7 @@ static int configure_interface(const char * if_name) {
}
uint8_t ip6_addr[16];
if (!ioctl(netdev, 0x12340003, &ip6_addr)) {
if (!ioctl(netdev, SIOCGIFADDR6, &ip6_addr)) {
/* TODO inet6 address to nice string */
fprintf(stdout," inet6 %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
ip6_addr[0], ip6_addr[1], ip6_addr[2], ip6_addr[3],
@ -91,7 +92,7 @@ static int configure_interface(const char * if_name) {
/* Get ethernet address */
uint8_t mac_addr[6];
if (!ioctl(netdev, 0x12340001, &mac_addr)) {
if (!ioctl(netdev, SIOCGIFHWADDR, &mac_addr)) {
fprintf(stdout," ether %02x:%02x:%02x:%02x:%02x:%02x\n",
mac_addr[0], mac_addr[1], mac_addr[2],
mac_addr[3], mac_addr[4], mac_addr[5]);

View File

@ -26,6 +26,7 @@
#include <sys/ioctl.h>
#include <sys/fswait.h>
#include <sys/socket.h>
#include <net/if.h>
#include <toaru/yutani.h>
#include <toaru/graphics.h>
@ -412,7 +413,7 @@ static void check_network(const char * if_name) {
/* Get IPv4 address */
uint32_t ip_addr;
if (!ioctl(netdev, 0x12340002, &ip_addr)) {
if (!ioctl(netdev, SIOCGIFADDR, &ip_addr)) {
char ip_str[16];
ip_ntoa(ntohl(ip_addr), ip_str);
snprintf(netstat_data[netstat_count], 1023, "%s: %s", if_name, ip_str);

31
base/usr/include/net/if.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <_cheader.h>
_Begin_C_Header
/* I would love to fully implement the Linux API for these, but
* for now these are just cleaner versions of the temporary API
* we currently provide. */
#define SIOCGIFHWADDR 0x12340001 /* Get hardware address */
#define SIOCGIFADDR 0x12340002 /* Get IPv4 address */
#define SIOCSIFADDR 0x12340012 /* Set IPv4 address */
#define SIOCGIFNETMASK 0x12340004 /* Get IPv4 subnet mask */
#define SIOCSIFNETMASK 0x12340014 /* Set IPv4 subnet mask */
#define SIOCGIFADDR6 0x12340003 /* Get IPv6 address */
#define SIOCSIFADDR6 0x12340013 /* Set IPv6 address */
#define SIOCGIFFLAGS 0x12340005 /* Get interface flags */
#define SIOCGIFMTU 0x12340006 /* Get interface mtu */
/**
* Flags for interface status
*/
#define IFF_UP 0x0001
#define IFF_BROADCAST 0x0002
#define IFF_DEBUG 0x0004
#define IFF_LOOPBACK 0x0008
#define IFF_RUNNING 0x0010
#define IFF_MULTICAST 0x0020
_End_C_Header

View File

@ -115,14 +115,6 @@ extern int connect(int sockfd, const struct sockaddr * addr, socklen_t addrlen);
extern int shutdown(int sockfd, int how);
#endif
#define IFF_UP 0x0001
#define IFF_BROADCAST 0x0002
#define IFF_DEBUG 0x0004
#define IFF_LOOPBACK 0x0008
#define IFF_RUNNING 0x0010
#define IFF_MULTICAST 0x0020
_End_C_Header

View File

@ -4,6 +4,7 @@
#include <kernel/printf.h>
#include <kernel/syscall.h>
#include <kernel/vfs.h>
#include <kernel/hashmap.h>
#include <kernel/mod/net.h>
#include <kernel/net/netif.h>
#include <kernel/net/eth.h>
@ -34,6 +35,35 @@ static void ip_ntoa(const uint32_t src_addr, char * out) {
(src_addr & 0xFF));
}
spin_lock_t net_arp_cache_lock = {0};
hashmap_t * net_arp_cache = NULL;
struct ArpCacheEntry {
uint8_t hwaddr[6];
uint16_t flags;
struct EthernetDevice * iface;
};
void net_arp_cache_add(struct EthernetDevice * iface, uint32_t addr, uint8_t * hwaddr, uint16_t flags) {
spin_lock(net_arp_cache_lock);
if (!net_arp_cache) net_arp_cache = hashmap_create_int(10);
struct ArpCacheEntry * entry = hashmap_get(net_arp_cache, (void*)(uintptr_t)addr);
if (!entry) entry = malloc(sizeof(struct ArpCacheEntry));
memcpy(entry->hwaddr, hwaddr, 6);
entry->flags = flags;
entry->iface = iface;
hashmap_set(net_arp_cache, (void*)(uintptr_t)addr, entry);
spin_unlock(net_arp_cache_lock);
}
struct ArpCacheEntry * net_arp_cache_get(uint32_t addr) {
if (!net_arp_cache) return NULL;
spin_lock(net_arp_cache_lock);
struct ArpCacheEntry * out = hashmap_get(net_arp_cache, (void*)(uintptr_t)addr);
spin_unlock(net_arp_cache_lock);
return out;
}
void net_arp_handle(struct arp_header * packet, fs_node_t * nic) {
printf("net: arp: hardware %d protocol %d operation %d hlen %d plen %d\n",
ntohs(packet->arp_htype), ntohs(packet->arp_ptype), ntohs(packet->arp_oper),
@ -42,6 +72,9 @@ void net_arp_handle(struct arp_header * packet, fs_node_t * nic) {
if (ntohs(packet->arp_htype) == 1 && ntohs(packet->arp_ptype) == ETHERNET_TYPE_IPV4) {
/* Ethernet, IPv4 */
if (packet->arp_data.arp_eth_ipv4.arp_spa) {
net_arp_cache_add(eth_dev, packet->arp_data.arp_eth_ipv4.arp_spa, packet->arp_data.arp_eth_ipv4.arp_sha, 0);
}
if (ntohs(packet->arp_oper) == 1) {
char spa[17];
ip_ntoa(ntohl(packet->arp_data.arp_eth_ipv4.arp_spa), spa);

View File

@ -27,6 +27,7 @@
#include <kernel/net/e1000.h>
#include <sys/socket.h>
#include <net/if.h>
#define INTS ((1 << 2) | (1 << 6) | (1 << 7) | (1 << 1) | (1 << 0))
@ -299,33 +300,33 @@ static int ioctl_e1000(fs_node_t * node, unsigned long request, void * argp) {
struct e1000_nic * nic = node->device;
switch (request) {
case 0x12340001:
case SIOCGIFHWADDR:
/* fill argp with mac */
memcpy(argp, nic->mac, 6);
return 0;
case 0x12340002:
case SIOCGIFADDR:
if (nic->ipv4_addr == 0) return -ENOENT;
memcpy(argp, &nic->ipv4_addr, sizeof(nic->ipv4_addr));
return 0;
case 0x12340012:
case SIOCSIFADDR:
memcpy(&nic->ipv4_addr, argp, sizeof(nic->ipv4_addr));
return 0;
case 0x12340004:
case SIOCGIFNETMASK:
if (nic->ipv4_subnet == 0) return -ENOENT;
memcpy(argp, &nic->ipv4_subnet, sizeof(nic->ipv4_subnet));
return 0;
case 0x12340014:
case SIOCSIFNETMASK:
memcpy(&nic->ipv4_subnet, argp, sizeof(nic->ipv4_subnet));
return 0;
case 0x12340003:
case SIOCGIFADDR6:
return -ENOENT;
case 0x12340013:
case SIOCSIFADDR6:
memcpy(&nic->ipv6_addr, argp, sizeof(nic->ipv6_addr));
return 0;
case 0x12340005: {
case SIOCGIFFLAGS: {
uint32_t * flags = argp;
*flags = IFF_RUNNING;
if (nic->link_status) *flags |= IFF_UP;
@ -335,24 +336,12 @@ static int ioctl_e1000(fs_node_t * node, unsigned long request, void * argp) {
return 0;
}
case 0x12340006: {
case SIOCGIFMTU: {
uint32_t * mtu = argp;
*mtu = nic->mtu;
return 0;
}
case 0x123400FF: {
/* discard all */
spin_lock(nic->net_queue_lock);
while (nic->net_queue->length) {
node_t * n = list_dequeue(nic->net_queue);
free(n->value);
free(n);
}
spin_unlock(nic->net_queue_lock);
return 0;
}
default:
return -EINVAL;
}

View File

@ -12,6 +12,8 @@
#include <kernel/syscall.h>
#include <kernel/vfs.h>
#include <kernel/net/netif.h>
#include <sys/socket.h>
struct ipv4_packet {
@ -31,16 +33,31 @@ struct ipv4_packet {
#define IPV4_PROT_UDP 17
#define IPV4_PROT_TCP 6
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));
}
void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) {
char dest[16];
char src[16];
ip_ntoa(ntohl(packet->destination), dest);
ip_ntoa(ntohl(packet->source), src);
switch (packet->protocol) {
case 1:
printf("net: ipv4: %s: ICMP\n", nic->name);
printf("net: ipv4: %s: %s -> %s ICMP\n", nic->name, src, dest);
break;
case IPV4_PROT_UDP:
printf("net: ipv4: %s: udp packet\n", nic->name);
printf("net: ipv4: %s: %s -> %s udp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), ntohs(((uint16_t*)&packet->payload)[1]));
break;
case IPV4_PROT_TCP:
printf("net: ipv4: %s: tcp packet\n", nic->name);
printf("net: ipv4: %s: %s -> %s tcp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), ntohs(((uint16_t*)&packet->payload)[1]));
break;
}
}

View File

@ -119,7 +119,7 @@ static long sys_sysfunc(long fn, char ** args) {
char **arg = args;
PTR_VALIDATE(args);
while (*arg) {
PTR_VALIDATE(*args);
PTR_VALIDATE(*arg);
count++;
arg++;
}
@ -144,6 +144,7 @@ static long sys_sysfunc(long fn, char ** args) {
printf("Bad system function: %ld\n", fn);
return -EINVAL;
}
return -EINVAL;
}
__attribute__((noreturn))