|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Intel Corporation. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/logging/log.h> |
| 8 | +LOG_MODULE_DECLARE(net_echo_server_sample, LOG_LEVEL_DBG); |
| 9 | + |
| 10 | +#include <zephyr/kernel.h> |
| 11 | +#include <zephyr/sys/base64.h> |
| 12 | +#include <zephyr/net/virtual.h> |
| 13 | +#include <zephyr/net/virtual_mgmt.h> |
| 14 | +#include <zephyr/net/wireguard.h> |
| 15 | + |
| 16 | +/* User data for the interface callback */ |
| 17 | +struct ud { |
| 18 | + struct net_if *vpn[CONFIG_WIREGUARD_MAX_PEER]; |
| 19 | +}; |
| 20 | + |
| 21 | +static void iface_cb(struct net_if *iface, void *user_data) |
| 22 | +{ |
| 23 | + struct ud *ud = user_data; |
| 24 | + |
| 25 | + if (net_if_l2(iface) != &NET_L2_GET_NAME(VIRTUAL)) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + if (!(net_virtual_get_iface_capabilities(iface) & VIRTUAL_INTERFACE_VPN)) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + for (int i = 0; i < CONFIG_WIREGUARD_MAX_PEER; i++) { |
| 34 | + if (ud->vpn[i] != NULL) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + ud->vpn[i] = iface; |
| 39 | + return; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +static int setup_iface(struct net_if *iface, |
| 44 | + const char *my_ip_addr, |
| 45 | + const char *allowed_ip_addr, |
| 46 | + const char *my_private_key, |
| 47 | + const char *peer_public_key) |
| 48 | +{ |
| 49 | + struct virtual_interface_req_params params = { 0 }; |
| 50 | + struct wireguard_peer_config peer_config = { 0 }; |
| 51 | + struct sockaddr_storage addr = { 0 }; |
| 52 | + struct sockaddr *paddr = (struct sockaddr *)&addr; |
| 53 | + struct net_if *peer_iface = NULL; |
| 54 | + uint8_t mask_len = 0; |
| 55 | + uint8_t private_key[NET_VIRTUAL_MAX_PUBLIC_KEY_LEN]; |
| 56 | + struct net_if_addr *ifaddr; |
| 57 | + const char *addr_str, *next; |
| 58 | + bool status, found; |
| 59 | + size_t olen; |
| 60 | + int ret; |
| 61 | + |
| 62 | + (void)base64_decode(private_key, sizeof(private_key), &olen, |
| 63 | + my_private_key, strlen(my_private_key)); |
| 64 | + |
| 65 | + params.private_key.data = private_key; |
| 66 | + params.private_key.len = sizeof(private_key); |
| 67 | + |
| 68 | + ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PRIVATE_KEY, |
| 69 | + iface, ¶ms, sizeof(params)); |
| 70 | + |
| 71 | + memset(private_key, 0, sizeof(private_key)); |
| 72 | + |
| 73 | + if (ret < 0) { |
| 74 | + LOG_ERR("Cannot set private key (%d)", ret); |
| 75 | + return ret; |
| 76 | + } |
| 77 | + |
| 78 | + addr_str = net_ipaddr_parse_mask(my_ip_addr, strlen(my_ip_addr), |
| 79 | + paddr, &mask_len); |
| 80 | + if (addr_str == NULL) { |
| 81 | + LOG_ERR("Cannot parse IP address \"%s\"", my_ip_addr); |
| 82 | + return -EINVAL; |
| 83 | + } |
| 84 | + |
| 85 | + if (paddr->sa_family == AF_INET) { |
| 86 | + struct sockaddr_in *addr4 = (struct sockaddr_in *)paddr; |
| 87 | + struct sockaddr_in mask; |
| 88 | + |
| 89 | + ifaddr = net_if_ipv4_addr_add(iface, &addr4->sin_addr, |
| 90 | + NET_ADDR_MANUAL, 0); |
| 91 | + |
| 92 | + ret = net_mask_len_to_netmask(AF_INET, mask_len, |
| 93 | + (struct sockaddr *)&mask); |
| 94 | + if (ret < 0) { |
| 95 | + LOG_ERR("Invalid network mask length (%d)", ret); |
| 96 | + return ret; |
| 97 | + } |
| 98 | + |
| 99 | + status = net_if_ipv4_set_netmask_by_addr(iface, |
| 100 | + &addr4->sin_addr, |
| 101 | + &mask.sin_addr); |
| 102 | + |
| 103 | + } else if (paddr->sa_family == AF_INET6) { |
| 104 | + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)paddr; |
| 105 | + struct in6_addr netaddr6; |
| 106 | + |
| 107 | + ifaddr = net_if_ipv6_addr_add(iface, &addr6->sin6_addr, |
| 108 | + NET_ADDR_MANUAL, 0); |
| 109 | + |
| 110 | + net_ipv6_addr_prefix_mask((uint8_t *)&addr6->sin6_addr, |
| 111 | + (uint8_t *)&netaddr6, |
| 112 | + mask_len); |
| 113 | + |
| 114 | + if (!net_if_ipv6_prefix_add(iface, &netaddr6, mask_len, |
| 115 | + (uint32_t)0xffffffff)) { |
| 116 | + LOG_ERR("Cannot add %s to interface %d", my_ip_addr, |
| 117 | + net_if_get_by_iface(iface)); |
| 118 | + return -EINVAL; |
| 119 | + } |
| 120 | + |
| 121 | + } else { |
| 122 | + LOG_ERR("Cannot parse IP address \"%s\"", my_ip_addr); |
| 123 | + return -EAFNOSUPPORT; |
| 124 | + } |
| 125 | + |
| 126 | + if (ifaddr == NULL) { |
| 127 | + LOG_ERR("Cannot add IP address \"%s\" to interface %d", |
| 128 | + my_ip_addr, net_if_get_by_iface(iface)); |
| 129 | + return -ENOENT; |
| 130 | + } |
| 131 | + |
| 132 | + peer_config.public_key = peer_public_key; |
| 133 | + |
| 134 | + addr_str = allowed_ip_addr; |
| 135 | + found = false; |
| 136 | + |
| 137 | + do { |
| 138 | + next = net_ipaddr_parse_mask(addr_str, strlen(addr_str), |
| 139 | + paddr, &mask_len); |
| 140 | + if (next == NULL) { |
| 141 | + LOG_ERR("Cannot parse IP address \"%s\"", allowed_ip_addr); |
| 142 | + return -EINVAL; |
| 143 | + } |
| 144 | + |
| 145 | + ARRAY_FOR_EACH(peer_config.allowed_ip, i) { |
| 146 | + if (peer_config.allowed_ip[i].is_valid) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + if (paddr->sa_family == AF_INET) { |
| 151 | + struct sockaddr_in *addr4 = (struct sockaddr_in *)paddr; |
| 152 | + |
| 153 | + memcpy(&peer_config.allowed_ip[i].addr.in_addr, |
| 154 | + &addr4->sin_addr, |
| 155 | + sizeof(struct in_addr)); |
| 156 | + peer_config.allowed_ip[i].addr.family = AF_INET; |
| 157 | + peer_config.allowed_ip[i].is_valid = true; |
| 158 | + peer_config.allowed_ip[i].mask_len = mask_len; |
| 159 | + found = true; |
| 160 | + |
| 161 | + } else if (addr.ss_family == AF_INET6) { |
| 162 | + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)paddr; |
| 163 | + |
| 164 | + memcpy(&peer_config.allowed_ip[i].addr.in6_addr, |
| 165 | + &addr6->sin6_addr, |
| 166 | + sizeof(struct in6_addr)); |
| 167 | + peer_config.allowed_ip[i].addr.family = AF_INET6; |
| 168 | + peer_config.allowed_ip[i].is_valid = true; |
| 169 | + peer_config.allowed_ip[i].mask_len = mask_len; |
| 170 | + found = true; |
| 171 | + |
| 172 | + } else { |
| 173 | + LOG_ERR("Cannot parse IP address \"%s\"", allowed_ip_addr); |
| 174 | + return -EAFNOSUPPORT; |
| 175 | + } |
| 176 | + |
| 177 | + break; |
| 178 | + } |
| 179 | + |
| 180 | + addr_str = next; |
| 181 | + } while (addr_str != NULL && *addr_str != '\0'); |
| 182 | + |
| 183 | + if (!found) { |
| 184 | + LOG_ERR("Not enough space for allowed IP addresses"); |
| 185 | + return -ENOMEM; |
| 186 | + } |
| 187 | + |
| 188 | + if (CONFIG_NET_SAMPLE_VPN_PEER_IP_ADDR[0] == '\0') { |
| 189 | + LOG_ERR("Peer IP address is not set"); |
| 190 | + return -EINVAL; |
| 191 | + } |
| 192 | + |
| 193 | + if (!net_ipaddr_parse(CONFIG_NET_SAMPLE_VPN_PEER_IP_ADDR, |
| 194 | + strlen(CONFIG_NET_SAMPLE_VPN_PEER_IP_ADDR), |
| 195 | + paddr)) { |
| 196 | + LOG_ERR("Cannot parse peer IP address \"%s\"", CONFIG_NET_SAMPLE_VPN_PEER_IP_ADDR); |
| 197 | + return -EINVAL; |
| 198 | + } |
| 199 | + |
| 200 | + if (paddr->sa_family == AF_INET6) { |
| 201 | + memcpy(&peer_config.endpoint_ip, paddr, sizeof(struct sockaddr_in6)); |
| 202 | + } else { |
| 203 | + memcpy(&peer_config.endpoint_ip, paddr, sizeof(struct sockaddr_in)); |
| 204 | + } |
| 205 | + |
| 206 | + ret = wireguard_peer_add(&peer_config, &peer_iface); |
| 207 | + if (ret < 0) { |
| 208 | + LOG_ERR("Cannot add peer (%d)", ret); |
| 209 | + } else if (ret > 0) { |
| 210 | + if (peer_iface != NULL) { |
| 211 | + LOG_INF("Added peer id %d using interface %d", ret, |
| 212 | + net_if_get_by_iface(peer_iface)); |
| 213 | + } else { |
| 214 | + LOG_INF("Added peer id %d", ret); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + LOG_DBG("Interface %d VPN setup done.", net_if_get_by_iface(iface)); |
| 219 | + |
| 220 | + return 0; |
| 221 | +} |
| 222 | + |
| 223 | +int init_vpn(void) |
| 224 | +{ |
| 225 | + struct ud ud; |
| 226 | + int ret; |
| 227 | + |
| 228 | + memset(&ud, 0, sizeof(ud)); |
| 229 | + |
| 230 | + net_if_foreach(iface_cb, &ud); |
| 231 | + |
| 232 | + /* This sample has support for one VPN connection. |
| 233 | + */ |
| 234 | + ret = setup_iface(ud.vpn[0], |
| 235 | + CONFIG_NET_SAMPLE_VPN_MY_ADDR, |
| 236 | + CONFIG_NET_SAMPLE_VPN_ALLOWED_PEER_ADDR, |
| 237 | + CONFIG_NET_SAMPLE_VPN_MY_PRIVATE_KEY, |
| 238 | + CONFIG_NET_SAMPLE_VPN_PEER_PUBLIC_KEY); |
| 239 | + if (ret < 0) { |
| 240 | + return ret; |
| 241 | + } |
| 242 | + |
| 243 | + return 0; |
| 244 | +} |
0 commit comments