-
Notifications
You must be signed in to change notification settings - Fork 2
/
itf.c
209 lines (170 loc) · 3.91 KB
/
itf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* dvpn, a multipoint vpn implementation
* Copyright (C) 2015, 2016 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <sys/wait.h>
#include <unistd.h>
#include "itf.h"
#define DEBUG 0
static void print_args(char *const *argv)
{
const char *sep = "";
char *const *ptr;
ptr = argv;
while (*ptr != NULL) {
fprintf(stderr, "%s%s", sep, *ptr);
sep = " ";
ptr++;
}
}
static int spawnvp(const char *file, char *const *argv)
{
pid_t pid;
int status;
if (DEBUG) {
fprintf(stderr, "running \"%s\", \"", file);
print_args(argv);
fprintf(stderr, "\"\n");
}
pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
int err;
execvp(file, argv);
err = errno;
fprintf(stderr, "execvp(\"%s\", \"", file);
print_args(argv);
fprintf(stderr, "\"): %s\n", strerror(err));
exit(1);
}
do {
int ret;
do {
ret = waitpid(pid, &status, 0);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
perror("waitpid");
return -1;
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
if (WIFSIGNALED(status)) {
if (DEBUG) {
fprintf(stderr, "child died with signal %d%s\n",
WTERMSIG(status),
WCOREDUMP(status) ? " and dumped core" : "");
}
return -1;
}
if (WEXITSTATUS(status)) {
if (DEBUG) {
fprintf(stderr, "child terminated with exit code %d\n",
WEXITSTATUS(status));
}
return -1;
}
return 0;
}
int itf_add_addr_v6(const char *itf, const uint8_t *addr, int len)
{
char caddr[64];
char *args[7];
inet_ntop(AF_INET6, addr, caddr, sizeof(caddr));
sprintf(caddr + strlen(caddr), "/%d", len);
args[0] = "ip";
args[1] = "addr";
args[2] = "add";
args[3] = caddr;
args[4] = "dev";
args[5] = (char *)itf;
args[6] = NULL;
return spawnvp("ip", args);
}
static int
__route_v6_direct(char *action, const uint8_t *dest, const char *itf)
{
char daddr[64];
char *args[7];
inet_ntop(AF_INET6, dest, daddr, sizeof(daddr));
args[0] = "ip";
args[1] = "route";
args[2] = action;
args[3] = daddr;
args[4] = "dev";
args[5] = (char *)itf;
args[6] = NULL;
return spawnvp("ip", args);
}
int itf_add_route_v6_direct(const uint8_t *addr, const char *itf)
{
return __route_v6_direct("add", addr, itf);
}
int itf_chg_route_v6_direct(const uint8_t *addr, const char *itf)
{
return __route_v6_direct("chg", addr, itf);
}
int itf_del_route_v6_direct(const uint8_t *addr, const char *itf)
{
return __route_v6_direct("del", addr, itf);
}
int itf_set_mtu(const char *itf, int mtu)
{
char cmtu[32];
char *args[7];
sprintf(cmtu, "%d", mtu);
args[0] = "ip";
args[1] = "link";
args[2] = "set";
args[3] = (char *)itf;
args[4] = "mtu";
args[5] = cmtu;
args[6] = NULL;
return spawnvp("ip", args);
}
int itf_set_state(const char *itf, int up)
{
char *args[6];
args[0] = "ip";
args[1] = "link";
args[2] = "set";
args[3] = (char *)itf;
args[4] = up ? "up" : "down";
args[5] = NULL;
if (spawnvp("ip", args))
return -1;
if (up) {
char path[256];
int fd;
snprintf(path, sizeof(path),
"/proc/sys/net/ipv6/conf/%s/disable_ipv6", itf);
fd = open(path, O_WRONLY);
if (fd >= 0) {
write(fd, "0\n", 2);
close(fd);
}
}
return 0;
}