| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifdef __linux__ |
| 8 |
|
| 9 |
#include "librist/tun.h" |
| 10 |
|
| 11 |
#include <errno.h> |
| 12 |
#include <fcntl.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#include <unistd.h> |
| 16 |
#include <sys/ioctl.h> |
| 17 |
#include <sys/socket.h> |
| 18 |
#include <net/if.h> |
| 19 |
#include <linux/if_tun.h> |
| 20 |
#include <linux/netlink.h> |
| 21 |
#include <linux/rtnetlink.h> |
| 22 |
#include <linux/if_addr.h> |
| 23 |
#include <arpa/inet.h> |
| 24 |
|
| 25 |
int rist_tun_open(const char *requested_name, char *actual_name, size_t name_len) |
| 26 |
{ |
| 27 |
int fd = open("/dev/net/tun", O_RDWR); |
| 28 |
if (fd < 0) { |
| 29 |
perror("open /dev/net/tun"); |
| 30 |
return -1; |
| 31 |
} |
| 32 |
|
| 33 |
struct ifreq ifr; |
| 34 |
memset(&ifr, 0, sizeof(ifr)); |
| 35 |
ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
| 36 |
|
| 37 |
if (requested_name && requested_name[0]) |
| 38 |
strncpy(ifr.ifr_name, requested_name, IFNAMSIZ - 1); |
| 39 |
|
| 40 |
if (ioctl(fd, TUNSETIFF, &ifr) < 0) { |
| 41 |
perror("ioctl TUNSETIFF"); |
| 42 |
close(fd); |
| 43 |
return -1; |
| 44 |
} |
| 45 |
|
| 46 |
if (actual_name && name_len > 0) |
| 47 |
strncpy(actual_name, ifr.ifr_name, name_len - 1); |
| 48 |
|
| 49 |
return fd; |
| 50 |
} |
| 51 |
|
| 52 |
void rist_tun_close(int fd) |
| 53 |
{ |
| 54 |
if (fd >= 0) |
| 55 |
close(fd); |
| 56 |
} |
| 57 |
|
| 58 |
int rist_tun_read(int fd, uint8_t *buf, size_t len) |
| 59 |
{ |
| 60 |
return (int)read(fd, buf, len); |
| 61 |
} |
| 62 |
|
| 63 |
int rist_tun_write(int fd, const uint8_t *buf, size_t len) |
| 64 |
{ |
| 65 |
return (int)write(fd, buf, len); |
| 66 |
} |
| 67 |
|
| 68 |
static int tun_ioctl_socket(int family) |
| 69 |
{ |
| 70 |
int s = socket(family, SOCK_DGRAM, 0); |
| 71 |
if (s < 0) |
| 72 |
perror(family == AF_INET6 ? "socket AF_INET6" : "socket AF_INET"); |
| 73 |
return s; |
| 74 |
} |
| 75 |
|
| 76 |
static int tun_set_ipv4(const char *dev, const char *ip, int prefix_len) |
| 77 |
{ |
| 78 |
int s = tun_ioctl_socket(AF_INET); |
| 79 |
if (s < 0) |
| 80 |
return -1; |
| 81 |
|
| 82 |
struct ifreq ifr; |
| 83 |
memset(&ifr, 0, sizeof(ifr)); |
| 84 |
strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1); |
| 85 |
|
| 86 |
struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr; |
| 87 |
addr->sin_family = AF_INET; |
| 88 |
|
| 89 |
if (inet_pton(AF_INET, ip, &addr->sin_addr) != 1) { |
| 90 |
fprintf(stderr, "Invalid IPv4 address: %s\n", ip); |
| 91 |
close(s); |
| 92 |
return -1; |
| 93 |
} |
| 94 |
|
| 95 |
if (ioctl(s, SIOCSIFADDR, &ifr) < 0) { |
| 96 |
perror("ioctl SIOCSIFADDR"); |
| 97 |
close(s); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
|
| 101 |
uint32_t mask; |
| 102 |
if (prefix_len == 0) |
| 103 |
mask = 0; |
| 104 |
else if (prefix_len == 32) |
| 105 |
mask = htonl(0xFFFFFFFFu); |
| 106 |
else |
| 107 |
mask = htonl(~((1U << (32 - prefix_len)) - 1)); |
| 108 |
struct sockaddr_in *netmask = (struct sockaddr_in *)&ifr.ifr_netmask; |
| 109 |
netmask->sin_family = AF_INET; |
| 110 |
netmask->sin_addr.s_addr = mask; |
| 111 |
|
| 112 |
if (ioctl(s, SIOCSIFNETMASK, &ifr) < 0) { |
| 113 |
perror("ioctl SIOCSIFNETMASK"); |
| 114 |
close(s); |
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
close(s); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
static int tun_netlink_ack(int fd) |
| 123 |
{ |
| 124 |
char buf[512]; |
| 125 |
struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) }; |
| 126 |
struct sockaddr_nl sa; |
| 127 |
struct msghdr msg = { |
| 128 |
.msg_name = &sa, |
| 129 |
.msg_namelen = sizeof(sa), |
| 130 |
.msg_iov = &iov, |
| 131 |
.msg_iovlen = 1, |
| 132 |
}; |
| 133 |
|
| 134 |
if (recvmsg(fd, &msg, 0) < 0) { |
| 135 |
perror("netlink recvmsg"); |
| 136 |
return -1; |
| 137 |
} |
| 138 |
|
| 139 |
struct nlmsghdr *nlh = (struct nlmsghdr *)buf; |
| 140 |
if (nlh->nlmsg_type == NLMSG_ERROR) { |
| 141 |
struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(nlh); |
| 142 |
if (err->error != 0) { |
| 143 |
errno = -err->error; |
| 144 |
perror("netlink RTM_NEWADDR"); |
| 145 |
return -1; |
| 146 |
} |
| 147 |
} |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
static int tun_add_rtattr(struct nlmsghdr *nlh, size_t max_len, |
| 152 |
unsigned short type, const void *data, size_t len) |
| 153 |
{ |
| 154 |
struct rtattr *rta; |
| 155 |
size_t new_len; |
| 156 |
|
| 157 |
new_len = NLMSG_ALIGN(nlh->nlmsg_len) + RTA_ALIGN(RTA_LENGTH(len)); |
| 158 |
if (new_len > max_len) |
| 159 |
return -1; |
| 160 |
|
| 161 |
rta = (struct rtattr *)((char *)nlh + NLMSG_ALIGN(nlh->nlmsg_len)); |
| 162 |
rta->rta_type = type; |
| 163 |
rta->rta_len = (unsigned short)RTA_LENGTH(len); |
| 164 |
memcpy(RTA_DATA(rta), data, len); |
| 165 |
nlh->nlmsg_len = (unsigned int)new_len; |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static int tun_set_ipv6(const char *dev, const char *ip, int prefix_len) |
| 170 |
{ |
| 171 |
struct in6_addr addr6; |
| 172 |
unsigned int ifindex; |
| 173 |
int fd; |
| 174 |
char reqbuf[512]; |
| 175 |
struct nlmsghdr *nlh; |
| 176 |
struct ifaddrmsg *ifa; |
| 177 |
struct sockaddr_nl sa; |
| 178 |
|
| 179 |
if (inet_pton(AF_INET6, ip, &addr6) != 1) { |
| 180 |
fprintf(stderr, "Invalid IPv6 address: %s\n", ip); |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
ifindex = if_nametoindex(dev); |
| 185 |
if (ifindex == 0) { |
| 186 |
perror("if_nametoindex"); |
| 187 |
return -1; |
| 188 |
} |
| 189 |
|
| 190 |
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 191 |
if (fd < 0) { |
| 192 |
perror("socket AF_NETLINK"); |
| 193 |
return -1; |
| 194 |
} |
| 195 |
|
| 196 |
memset(reqbuf, 0, sizeof(reqbuf)); |
| 197 |
nlh = (struct nlmsghdr *)reqbuf; |
| 198 |
ifa = (struct ifaddrmsg *)(nlh + 1); |
| 199 |
|
| 200 |
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 201 |
nlh->nlmsg_type = RTM_NEWADDR; |
| 202 |
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK; |
| 203 |
nlh->nlmsg_seq = 1; |
| 204 |
|
| 205 |
ifa->ifa_family = AF_INET6; |
| 206 |
ifa->ifa_prefixlen = (unsigned char)prefix_len; |
| 207 |
ifa->ifa_flags = IFA_F_NODAD; |
| 208 |
ifa->ifa_scope = RT_SCOPE_UNIVERSE; |
| 209 |
ifa->ifa_index = ifindex; |
| 210 |
|
| 211 |
if (tun_add_rtattr(nlh, sizeof(reqbuf), IFA_LOCAL, &addr6, sizeof(addr6)) != 0 || |
| 212 |
tun_add_rtattr(nlh, sizeof(reqbuf), IFA_ADDRESS, &addr6, sizeof(addr6)) != 0) { |
| 213 |
fprintf(stderr, "netlink attribute too large\n"); |
| 214 |
close(fd); |
| 215 |
return -1; |
| 216 |
} |
| 217 |
|
| 218 |
memset(&sa, 0, sizeof(sa)); |
| 219 |
sa.nl_family = AF_NETLINK; |
| 220 |
|
| 221 |
struct iovec iov = { .iov_base = reqbuf, .iov_len = nlh->nlmsg_len }; |
| 222 |
struct msghdr msg = { |
| 223 |
.msg_name = &sa, |
| 224 |
.msg_namelen = sizeof(sa), |
| 225 |
.msg_iov = &iov, |
| 226 |
.msg_iovlen = 1, |
| 227 |
}; |
| 228 |
|
| 229 |
if (sendmsg(fd, &msg, 0) < 0) { |
| 230 |
perror("netlink sendmsg"); |
| 231 |
close(fd); |
| 232 |
return -1; |
| 233 |
} |
| 234 |
|
| 235 |
if (tun_netlink_ack(fd) != 0) { |
| 236 |
close(fd); |
| 237 |
return -1; |
| 238 |
} |
| 239 |
|
| 240 |
close(fd); |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
int rist_tun_set_ip(const char *dev, const char *ip, int prefix_len) |
| 245 |
{ |
| 246 |
struct in6_addr addr6; |
| 247 |
struct in_addr addr4; |
| 248 |
|
| 249 |
if (inet_pton(AF_INET6, ip, &addr6) == 1) { |
| 250 |
if (prefix_len < 0 || prefix_len > 128) { |
| 251 |
fprintf(stderr, "Invalid IPv6 prefix length: %d\n", prefix_len); |
| 252 |
return -1; |
| 253 |
} |
| 254 |
return tun_set_ipv6(dev, ip, prefix_len); |
| 255 |
} |
| 256 |
|
| 257 |
if (inet_pton(AF_INET, ip, &addr4) == 1) { |
| 258 |
if (prefix_len < 0 || prefix_len > 32) { |
| 259 |
fprintf(stderr, "Invalid IPv4 prefix length: %d\n", prefix_len); |
| 260 |
return -1; |
| 261 |
} |
| 262 |
return tun_set_ipv4(dev, ip, prefix_len); |
| 263 |
} |
| 264 |
|
| 265 |
fprintf(stderr, "Invalid IP address: %s\n", ip); |
| 266 |
return -1; |
| 267 |
} |
| 268 |
|
| 269 |
int rist_tun_set_mtu(const char *dev, int mtu) |
| 270 |
{ |
| 271 |
int s = tun_ioctl_socket(AF_INET); |
| 272 |
if (s < 0) |
| 273 |
return -1; |
| 274 |
|
| 275 |
struct ifreq ifr; |
| 276 |
memset(&ifr, 0, sizeof(ifr)); |
| 277 |
strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1); |
| 278 |
ifr.ifr_mtu = mtu; |
| 279 |
|
| 280 |
if (ioctl(s, SIOCSIFMTU, &ifr) < 0) { |
| 281 |
perror("ioctl SIOCSIFMTU"); |
| 282 |
close(s); |
| 283 |
return -1; |
| 284 |
} |
| 285 |
|
| 286 |
close(s); |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
int rist_tun_bring_up(const char *dev) |
| 291 |
{ |
| 292 |
int s = tun_ioctl_socket(AF_INET); |
| 293 |
if (s < 0) |
| 294 |
return -1; |
| 295 |
|
| 296 |
struct ifreq ifr; |
| 297 |
memset(&ifr, 0, sizeof(ifr)); |
| 298 |
strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1); |
| 299 |
|
| 300 |
if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) { |
| 301 |
perror("ioctl SIOCGIFFLAGS"); |
| 302 |
close(s); |
| 303 |
return -1; |
| 304 |
} |
| 305 |
|
| 306 |
ifr.ifr_flags |= IFF_UP | IFF_RUNNING; |
| 307 |
|
| 308 |
if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) { |
| 309 |
perror("ioctl SIOCSIFFLAGS"); |
| 310 |
close(s); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
close(s); |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
#endif |