summary refs log tree commit diff
path: root/host/start-vm/net-util.c
blob: 44f8e8a435beaf3fb94b475b6f1d2df08ecded98 (plain) (blame)
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
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2022 Alyssa Ross <hi@alyssa.is>

#include <err.h>
#include <fcntl.h>
#include <net/if.h>
#include <string.h>
#include <unistd.h>

#include <sys/ioctl.h>

#include <linux/if_tun.h>
#include <linux/sockios.h>

// ifr_name doesn't have to be null terminated.
#pragma GCC diagnostic ignored "-Wstringop-truncation"

int if_up(const char *name)
{
	struct ifreq req;
	int fd, r = -1;

	if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1)
		return -1;

	strncpy(req.ifr_name, name, IFNAMSIZ);
	if (ioctl(fd, SIOCGIFFLAGS, &req) == -1)
		goto out;
	req.ifr_flags |= IFF_UP;
	r = ioctl(fd, SIOCSIFFLAGS, &req);
out:
	close(fd);
	return r;
}

int if_rename(const char *name, const char *newname)
{
	int fd, r;
	struct ifreq ifr;

	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
	strncpy(ifr.ifr_newname, newname, sizeof ifr.ifr_newname);

	if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1)
		return -1;
	r = ioctl(fd, SIOCSIFNAME, &ifr);
	close(fd);
	return r;
}

int if_down(const char *name)
{
	struct ifreq ifr;
	int fd, r = -1;

	if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1)
		return -1;

	strncpy(ifr.ifr_name, name, IFNAMSIZ);
	if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1)
		goto out;
	ifr.ifr_flags &= ~IFF_UP;
	r = ioctl(fd, SIOCSIFFLAGS, &ifr);
out:
	close(fd);
	return r;
}

int bridge_add(const char *name)
{
	int fd, r;
	if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1)
		return -1;
	r = ioctl(fd, SIOCBRADDBR, name);
	close(fd);
	return r;
}

int bridge_add_if(const char *brname, const char *ifname)
{
	struct ifreq ifr;
	int fd, r;

	strncpy(ifr.ifr_name, brname, IFNAMSIZ);
	if (!(ifr.ifr_ifindex = if_nametoindex(ifname)))
		return -1;

	if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1)
		return -1;

	r = ioctl(fd, SIOCBRADDIF, &ifr);
	close(fd);
	return r;
}

int bridge_remove_if(const char *brname, const char *ifname)
{
	struct ifreq ifr;
	int fd, r;

	strncpy(ifr.ifr_name, brname, IFNAMSIZ);
	if (!(ifr.ifr_ifindex = if_nametoindex(ifname)))
		return -1;

	if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1)
		return -1;

	r = ioctl(fd, SIOCBRDELIF, &ifr);
	close(fd);
	return r;
}

int bridge_delete(const char *name)
{
	int fd, r;

	if (if_down(name) == -1)
		warn("setting %s down", name);

	if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) == -1)
		return -1;

	r = ioctl(fd, SIOCBRDELBR, name);
	close(fd);
	return r;
}

int tap_open(const char *name, int flags)
{
	struct ifreq ifr;
	int fd;

	if ((fd = open("/dev/net/tun", O_RDWR)) == -1)
		return -1;

	strncpy(ifr.ifr_name, name, IFNAMSIZ);
	ifr.ifr_flags = IFF_TAP|flags;
	if (!ioctl(fd, TUNSETIFF, &ifr))
		return fd;

	close(fd);
	return -1;
}