Debian Codegraph @

src/tun_linux.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC. All right reserved.
2 * Author: Sergio Ammirata, Ph.D. <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
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 <arpa/inet.h>
21
22 int rist_tun_open(const char *requested_name, char *actual_name, size_t name_len)
23 {
24 int fd = open("/dev/net/tun", O_RDWR);
25 if (fd < 0) {
26 perror("open /dev/net/tun");
27 return -1;
28 }
29
30 struct ifreq ifr;
31 memset(&ifr, 0, sizeof(ifr));
32 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
33
34 if (requested_name && requested_name[0])
35 strncpy(ifr.ifr_name, requested_name, IFNAMSIZ - 1);
36
37 if (ioctl(fd, TUNSETIFF, &ifr) < 0) {
38 perror("ioctl TUNSETIFF");
39 close(fd);
40 return -1;
41 }
42
43 if (actual_name && name_len > 0)
44 strncpy(actual_name, ifr.ifr_name, name_len - 1);
45
46 return fd;
47 }
48
49 void rist_tun_close(int fd)
50 {
51 if (fd >= 0)
52 close(fd);
53 }
54
55 int rist_tun_read(int fd, uint8_t *buf, size_t len)
56 {
57 return (int)read(fd, buf, len);
58 }
59
60 int rist_tun_write(int fd, const uint8_t *buf, size_t len)
61 {
62 return (int)write(fd, buf, len);
63 }
64
65 static int tun_ioctl_socket(void)
66 {
67 int s = socket(AF_INET, SOCK_DGRAM, 0);
68 if (s < 0)
69 perror("socket AF_INET");
70 return s;
71 }
72
73 int rist_tun_set_ip(const char *dev, const char *ip, int prefix_len)
74 {
75 if (prefix_len < 0 || prefix_len > 32) {
76 fprintf(stderr, "Invalid prefix length: %d\n", prefix_len);
77 return -1;
78 }
79 int s = tun_ioctl_socket();
80 if (s < 0)
81 return -1;
82
83 struct ifreq ifr;
84 memset(&ifr, 0, sizeof(ifr));
85 strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);
86
87 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr;
88 addr->sin_family = AF_INET;
89
90 if (inet_pton(AF_INET, ip, &addr->sin_addr) != 1) {
91 fprintf(stderr, "Invalid IP address: %s\n", ip);
92 close(s);
93 return -1;
94 }
95
96 if (ioctl(s, SIOCSIFADDR, &ifr) < 0) {
97 perror("ioctl SIOCSIFADDR");
98 close(s);
99 return -1;
100 }
101
102 uint32_t mask;
103 if (prefix_len == 0)
104 mask = 0;
105 else if (prefix_len == 32)
106 mask = htonl(0xFFFFFFFFu);
107 else
108 mask = htonl(~((1U << (32 - prefix_len)) - 1));
109 struct sockaddr_in *netmask = (struct sockaddr_in *)&ifr.ifr_netmask;
110 netmask->sin_family = AF_INET;
111 netmask->sin_addr.s_addr = mask;
112
113 if (ioctl(s, SIOCSIFNETMASK, &ifr) < 0) {
114 perror("ioctl SIOCSIFNETMASK");
115 close(s);
116 return -1;
117 }
118
119 close(s);
120 return 0;
121 }
122
123 int rist_tun_set_mtu(const char *dev, int mtu)
124 {
125 int s = tun_ioctl_socket();
126 if (s < 0)
127 return -1;
128
129 struct ifreq ifr;
130 memset(&ifr, 0, sizeof(ifr));
131 strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);
132 ifr.ifr_mtu = mtu;
133
134 if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
135 perror("ioctl SIOCSIFMTU");
136 close(s);
137 return -1;
138 }
139
140 close(s);
141 return 0;
142 }
143
144 int rist_tun_bring_up(const char *dev)
145 {
146 int s = tun_ioctl_socket();
147 if (s < 0)
148 return -1;
149
150 struct ifreq ifr;
151 memset(&ifr, 0, sizeof(ifr));
152 strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);
153
154 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
155 perror("ioctl SIOCGIFFLAGS");
156 close(s);
157 return -1;
158 }
159
160 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
161
162 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
163 perror("ioctl SIOCSIFFLAGS");
164 close(s);
165 return -1;
166 }
167
168 close(s);
169 return 0;
170 }
171
172 #endif /* __linux__ */