|
| 1 | +#include "wolfip_freertos.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <fcntl.h> |
| 6 | +#include <sys/ioctl.h> |
| 7 | +#include <linux/if.h> |
| 8 | +#include <linux/if_tun.h> |
| 9 | +#include <arpa/inet.h> |
| 10 | +#include <sys/time.h> |
| 11 | +#include <poll.h> |
| 12 | +#include <sys/socket.h> |
| 13 | +#include <sys/random.h> |
| 14 | + |
| 15 | +/* Implementation of wolfIP's required random number generator */ |
| 16 | +uint32_t wolfIP_getrandom(void) { |
| 17 | + uint32_t ret; |
| 18 | + getrandom(&ret, sizeof(ret), 0); |
| 19 | + return ret; |
| 20 | +} |
| 21 | + |
| 22 | +static struct wolfIP *g_wolfip = NULL; |
| 23 | +static TaskHandle_t g_network_task = NULL; |
| 24 | +static int tap_fd = -1; |
| 25 | + |
| 26 | +/* TUN/TAP device functions */ |
| 27 | +static int tap_init(struct ll *dev, const char *ifname) { |
| 28 | + struct ifreq ifr; |
| 29 | + int sock_fd; |
| 30 | + |
| 31 | + if ((tap_fd = open("/dev/net/tun", O_RDWR)) < 0) { |
| 32 | + perror("Error opening /dev/net/tun"); |
| 33 | + return -1; |
| 34 | + } |
| 35 | + |
| 36 | + memset(&ifr, 0, sizeof(ifr)); |
| 37 | + ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 38 | + strncpy(ifr.ifr_name, ifname, IFNAMSIZ); |
| 39 | + |
| 40 | + if (ioctl(tap_fd, TUNSETIFF, (void *)&ifr) < 0) { |
| 41 | + perror("ioctl TUNSETIFF"); |
| 42 | + close(tap_fd); |
| 43 | + return -1; |
| 44 | + } |
| 45 | + |
| 46 | + /* Get MAC address */ |
| 47 | + if (ioctl(tap_fd, SIOCGIFHWADDR, &ifr) < 0) { |
| 48 | + perror("ioctl SIOCGIFHWADDR"); |
| 49 | + close(tap_fd); |
| 50 | + return -1; |
| 51 | + } |
| 52 | + |
| 53 | + strncpy(dev->ifname, ifname, sizeof(dev->ifname) - 1); |
| 54 | + memcpy(dev->mac, ifr.ifr_hwaddr.sa_data, 6); |
| 55 | + dev->mac[5] ^= 1; /* Make MAC unique */ |
| 56 | + |
| 57 | + /* Configure network interface */ |
| 58 | + sock_fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 59 | + if (sock_fd < 0) { |
| 60 | + perror("socket"); |
| 61 | + close(tap_fd); |
| 62 | + return -1; |
| 63 | + } |
| 64 | + |
| 65 | + /* Set interface UP */ |
| 66 | + if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) { |
| 67 | + perror("ioctl SIOCGIFFLAGS"); |
| 68 | + close(sock_fd); |
| 69 | + return -1; |
| 70 | + } |
| 71 | + ifr.ifr_flags |= IFF_UP | IFF_RUNNING; |
| 72 | + if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) { |
| 73 | + perror("ioctl SIOCSIFFLAGS"); |
| 74 | + close(sock_fd); |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + close(sock_fd); |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +static int tap_poll(struct ll *ll, void *buf, uint32_t len) { |
| 83 | + struct pollfd pfd; |
| 84 | + int ret; |
| 85 | + |
| 86 | + pfd.fd = tap_fd; |
| 87 | + pfd.events = POLLIN; |
| 88 | + ret = poll(&pfd, 1, 1); /* Short timeout */ |
| 89 | + |
| 90 | + if (ret < 0) { |
| 91 | + perror("poll"); |
| 92 | + return -1; |
| 93 | + } |
| 94 | + if (ret == 0) { |
| 95 | + return 0; |
| 96 | + } |
| 97 | + |
| 98 | + return read(tap_fd, buf, len); |
| 99 | +} |
| 100 | + |
| 101 | +static int tap_send(struct ll *ll, void *buf, uint32_t len) { |
| 102 | + return write(tap_fd, buf, len); |
| 103 | +} |
| 104 | + |
| 105 | +/* Network task implementation */ |
| 106 | +static void wolfIP_NetworkTask(void *pvParameters) { |
| 107 | + TickType_t last_wake_time; |
| 108 | + const TickType_t frequency = pdMS_TO_TICKS(WOLFIP_POLL_INTERVAL_MS); |
| 109 | + struct timeval tv; |
| 110 | + |
| 111 | + last_wake_time = xTaskGetTickCount(); |
| 112 | + |
| 113 | + while (1) { |
| 114 | + gettimeofday(&tv, NULL); |
| 115 | + wolfIP_poll(g_wolfip, tv.tv_sec * 1000 + tv.tv_usec / 1000); |
| 116 | + vTaskDelayUntil(&last_wake_time, frequency); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +int wolfIP_FreeRTOS_Init(void) { |
| 121 | + struct ll *tapdev; |
| 122 | + |
| 123 | + /* Initialize wolfIP */ |
| 124 | + wolfIP_init_static(&g_wolfip); |
| 125 | + if (!g_wolfip) { |
| 126 | + printf("Failed to initialize wolfIP\n"); |
| 127 | + return -1; |
| 128 | + } |
| 129 | + |
| 130 | + /* Setup TUN/TAP interface */ |
| 131 | + tapdev = wolfIP_getdev(g_wolfip); |
| 132 | + if (!tapdev) { |
| 133 | + printf("Failed to get device from wolfIP\n"); |
| 134 | + return -1; |
| 135 | + } |
| 136 | + |
| 137 | + /* Initialize TAP device */ |
| 138 | + if (tap_init(tapdev, "wtap0") < 0) { |
| 139 | + printf("Failed to initialize TAP device\n"); |
| 140 | + return -1; |
| 141 | + } |
| 142 | + |
| 143 | + /* Set device callbacks */ |
| 144 | + tapdev->poll = tap_poll; |
| 145 | + tapdev->send = tap_send; |
| 146 | + |
| 147 | + /* Configure IP settings */ |
| 148 | + wolfIP_ipconfig_set(g_wolfip, |
| 149 | + atoip4("192.168.1.10"), /* IP */ |
| 150 | + atoip4("255.255.255.0"), /* Netmask */ |
| 151 | + atoip4("192.168.1.1")); /* Gateway */ |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +int wolfIP_FreeRTOS_Start(void) { |
| 157 | + BaseType_t ret; |
| 158 | + |
| 159 | + if (!g_wolfip) { |
| 160 | + printf("wolfIP not initialized\n"); |
| 161 | + return -1; |
| 162 | + } |
| 163 | + |
| 164 | + ret = xTaskCreate(wolfIP_NetworkTask, |
| 165 | + "WolfIP_Net", |
| 166 | + WOLFIP_TASK_STACK_SIZE, |
| 167 | + NULL, |
| 168 | + WOLFIP_TASK_PRIORITY, |
| 169 | + &g_network_task); |
| 170 | + |
| 171 | + return (ret == pdPASS) ? 0 : -1; |
| 172 | +} |
0 commit comments