tuntap_user.c 4.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/* 
J
Jeff Dike 已提交
2
 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6 7 8
 * Licensed under the GPL
 */

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
J
Jeff Dike 已提交
9 10 11 12
#include <string.h>
#include <linux/if_tun.h>
#include <net/if.h>
#include <sys/ioctl.h>
L
Linus Torvalds 已提交
13
#include <sys/socket.h>
J
Jeff Dike 已提交
14
#include <sys/wait.h>
L
Linus Torvalds 已提交
15
#include <sys/uio.h>
J
Jeff Dike 已提交
16
#include "kern_constants.h"
J
Jeff Dike 已提交
17
#include "kern_util.h"
J
Jeff Dike 已提交
18
#include "os.h"
L
Linus Torvalds 已提交
19 20 21
#include "tuntap.h"
#include "user.h"

22
static int tuntap_user_init(void *data, void *dev)
L
Linus Torvalds 已提交
23 24 25 26
{
	struct tuntap_data *pri = data;

	pri->dev = dev;
27
	return 0;
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35
}

static void tuntap_add_addr(unsigned char *addr, unsigned char *netmask,
			    void *data)
{
	struct tuntap_data *pri = data;

	tap_check_ips(pri->gate_addr, addr);
J
Jeff Dike 已提交
36
	if ((pri->fd == -1) || pri->fixed_config)
J
Jeff Dike 已提交
37
		return;
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45
	open_addr(addr, netmask, pri->dev_name);
}

static void tuntap_del_addr(unsigned char *addr, unsigned char *netmask,
			    void *data)
{
	struct tuntap_data *pri = data;

J
Jeff Dike 已提交
46
	if ((pri->fd == -1) || pri->fixed_config)
J
Jeff Dike 已提交
47
		return;
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58
	close_addr(addr, netmask, pri->dev_name);
}

struct tuntap_pre_exec_data {
	int stdout;
	int close_me;
};

static void tuntap_pre_exec(void *arg)
{
	struct tuntap_pre_exec_data *data = arg;
J
Jeff Dike 已提交
59

L
Linus Torvalds 已提交
60
	dup2(data->stdout, 1);
61
	close(data->close_me);
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
}

static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote,
			     char *buffer, int buffer_len, int *used_out)
{
	struct tuntap_pre_exec_data data;
	char version_buf[sizeof("nnnnn\0")];
	char *argv[] = { "uml_net", version_buf, "tuntap", "up", gate,
			 NULL };
	char buf[CMSG_SPACE(sizeof(*fd_out))];
	struct msghdr msg;
	struct cmsghdr *cmsg;
	struct iovec iov;
75
	int pid, n, err;
L
Linus Torvalds 已提交
76 77 78 79 80 81

	sprintf(version_buf, "%d", UML_NET_VERSION);

	data.stdout = remote;
	data.close_me = me;

J
Jeff Dike 已提交
82
	pid = run_helper(tuntap_pre_exec, &data, argv);
L
Linus Torvalds 已提交
83

J
Jeff Dike 已提交
84
	if (pid < 0)
J
Jeff Dike 已提交
85
		return -pid;
L
Linus Torvalds 已提交
86

87
	close(remote);
L
Linus Torvalds 已提交
88 89 90

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
J
Jeff Dike 已提交
91
	if (buffer != NULL) {
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
		iov = ((struct iovec) { buffer, buffer_len });
		msg.msg_iov = &iov;
		msg.msg_iovlen = 1;
	}
	else {
		msg.msg_iov = NULL;
		msg.msg_iovlen = 0;
	}
	msg.msg_control = buf;
	msg.msg_controllen = sizeof(buf);
	msg.msg_flags = 0;
	n = recvmsg(me, &msg, 0);
	*used_out = n;
J
Jeff Dike 已提交
105
	if (n < 0) {
106
		err = -errno;
J
Jeff Dike 已提交
107 108
		printk(UM_KERN_ERR "tuntap_open_tramp : recvmsg failed - "
		       "errno = %d\n", errno);
109
		return err;
L
Linus Torvalds 已提交
110
	}
J
Jeff Dike 已提交
111
	helper_wait(pid);
L
Linus Torvalds 已提交
112 113

	cmsg = CMSG_FIRSTHDR(&msg);
J
Jeff Dike 已提交
114 115 116
	if (cmsg == NULL) {
		printk(UM_KERN_ERR "tuntap_open_tramp : didn't receive a "
		       "message\n");
J
Jeff Dike 已提交
117
		return -EINVAL;
L
Linus Torvalds 已提交
118
	}
J
Jeff Dike 已提交
119 120 121 122
	if ((cmsg->cmsg_level != SOL_SOCKET) ||
	   (cmsg->cmsg_type != SCM_RIGHTS)) {
		printk(UM_KERN_ERR "tuntap_open_tramp : didn't receive a "
		       "descriptor\n");
J
Jeff Dike 已提交
123
		return -EINVAL;
L
Linus Torvalds 已提交
124 125
	}
	*fd_out = ((int *) CMSG_DATA(cmsg))[0];
126
	os_set_exec_close(*fd_out);
J
Jeff Dike 已提交
127
	return 0;
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137
}

static int tuntap_open(void *data)
{
	struct ifreq ifr;
	struct tuntap_data *pri = data;
	char *output, *buffer;
	int err, fds[2], len, used;

	err = tap_open_common(pri->dev, pri->gate_addr);
J
Jeff Dike 已提交
138
	if (err < 0)
J
Jeff Dike 已提交
139
		return err;
L
Linus Torvalds 已提交
140

J
Jeff Dike 已提交
141
	if (pri->fixed_config) {
142 143
		pri->fd = os_open_file("/dev/net/tun",
				       of_cloexec(of_rdwr(OPENFLAGS())), 0);
J
Jeff Dike 已提交
144 145 146
		if (pri->fd < 0) {
			printk(UM_KERN_ERR "Failed to open /dev/net/tun, "
			       "err = %d\n", -pri->fd);
J
Jeff Dike 已提交
147
			return pri->fd;
L
Linus Torvalds 已提交
148 149 150 151
		}
		memset(&ifr, 0, sizeof(ifr));
		ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
		strlcpy(ifr.ifr_name, pri->dev_name, sizeof(ifr.ifr_name));
W
WANG Cong 已提交
152
		if (ioctl(pri->fd, TUNSETIFF, &ifr) < 0) {
153
			err = -errno;
J
Jeff Dike 已提交
154 155
			printk(UM_KERN_ERR "TUNSETIFF failed, errno = %d\n",
			       errno);
156
			close(pri->fd);
157
			return err;
L
Linus Torvalds 已提交
158 159 160
		}
	}
	else {
161
		err = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds);
J
Jeff Dike 已提交
162
		if (err) {
163
			err = -errno;
J
Jeff Dike 已提交
164 165
			printk(UM_KERN_ERR "tuntap_open : socketpair failed - "
			       "errno = %d\n", errno);
J
Jeff Dike 已提交
166
			return err;
L
Linus Torvalds 已提交
167 168 169
		}

		buffer = get_output_buffer(&len);
J
Jeff Dike 已提交
170
		if (buffer != NULL)
171
			len--;
L
Linus Torvalds 已提交
172 173 174 175 176 177
		used = 0;

		err = tuntap_open_tramp(pri->gate_addr, &pri->fd, fds[0],
					fds[1], buffer, len, &used);

		output = buffer;
J
Jeff Dike 已提交
178
		if (err < 0) {
L
Linus Torvalds 已提交
179 180
			printk("%s", output);
			free_output_buffer(buffer);
J
Jeff Dike 已提交
181 182
			printk(UM_KERN_ERR "tuntap_open_tramp failed - "
			       "err = %d\n", -err);
J
Jeff Dike 已提交
183
			return err;
L
Linus Torvalds 已提交
184 185 186 187 188 189 190
		}

		pri->dev_name = uml_strdup(buffer);
		output += IFNAMSIZ;
		printk("%s", output);
		free_output_buffer(buffer);

191
		close(fds[0]);
L
Linus Torvalds 已提交
192 193 194
		iter_addresses(pri->dev, open_addr, pri->dev_name);
	}

J
Jeff Dike 已提交
195
	return pri->fd;
L
Linus Torvalds 已提交
196 197 198 199 200 201
}

static void tuntap_close(int fd, void *data)
{
	struct tuntap_data *pri = data;

J
Jeff Dike 已提交
202
	if (!pri->fixed_config)
L
Linus Torvalds 已提交
203
		iter_addresses(pri->dev, close_addr, pri->dev_name);
204
	close(fd);
L
Linus Torvalds 已提交
205 206 207
	pri->fd = -1;
}

J
Jeff Dike 已提交
208
const struct net_user_info tuntap_user_info = {
L
Linus Torvalds 已提交
209 210 211 212 213 214
	.init		= tuntap_user_init,
	.open		= tuntap_open,
	.close	 	= tuntap_close,
	.remove	 	= NULL,
	.add_address	= tuntap_add_addr,
	.delete_address = tuntap_del_addr,
J
Jeff Dike 已提交
215 216
	.mtu		= ETH_MAX_PACKET,
	.max_packet	= ETH_MAX_PACKET + ETH_HEADER_OTHER,
L
Linus Torvalds 已提交
217
};