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

#include <stdio.h>
J
Jeff Dike 已提交
7 8
#include <unistd.h>
#include <stdarg.h>
L
Linus Torvalds 已提交
9
#include <errno.h>
J
Jeff Dike 已提交
10
#include <stddef.h>
L
Linus Torvalds 已提交
11 12 13
#include <string.h>
#include <sys/socket.h>
#include <sys/wait.h>
14 15 16
#include <net_user.h>
#include <os.h>
#include <um_malloc.h>
L
Linus Torvalds 已提交
17 18 19 20 21

int tap_open_common(void *dev, char *gate_addr)
{
	int tap_addr[4];

J
Jeff Dike 已提交
22
	if (gate_addr == NULL)
J
Jeff Dike 已提交
23
		return 0;
J
Jeff Dike 已提交
24 25 26 27
	if (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
		  &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4) {
		printk(UM_KERN_ERR "Invalid tap IP address - '%s'\n",
		       gate_addr);
J
Jeff Dike 已提交
28
		return -EINVAL;
L
Linus Torvalds 已提交
29
	}
J
Jeff Dike 已提交
30
	return 0;
L
Linus Torvalds 已提交
31 32
}

J
Jeff Dike 已提交
33
void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
L
Linus Torvalds 已提交
34 35 36
{
	int tap_addr[4];

J
Jeff Dike 已提交
37 38 39 40 41 42 43 44 45
	if ((gate_addr != NULL) &&
	    (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
		    &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) &&
	    (eth_addr[0] == tap_addr[0]) &&
	    (eth_addr[1] == tap_addr[1]) &&
	    (eth_addr[2] == tap_addr[2]) &&
	    (eth_addr[3] == tap_addr[3])) {
		printk(UM_KERN_ERR "The tap IP address and the UML eth IP "
		       "address must be different\n");
L
Linus Torvalds 已提交
46 47 48
	}
}

49
/* Do reliable error handling as this fails frequently enough. */
L
Linus Torvalds 已提交
50 51
void read_output(int fd, char *output, int len)
{
52
	int remain, ret, expected;
L
Linus Torvalds 已提交
53
	char c;
54
	char *str;
L
Linus Torvalds 已提交
55

J
Jeff Dike 已提交
56
	if (output == NULL) {
L
Linus Torvalds 已提交
57 58 59
		output = &c;
		len = sizeof(c);
	}
J
Jeff Dike 已提交
60

L
Linus Torvalds 已提交
61
	*output = '\0';
J
Jeff Dike 已提交
62
	ret = read(fd, &remain, sizeof(remain));
63 64

	if (ret != sizeof(remain)) {
J
Jeff Dike 已提交
65 66
		if (ret < 0)
			ret = -errno;
67 68 69
		expected = sizeof(remain);
		str = "length";
		goto err;
L
Linus Torvalds 已提交
70 71
	}

J
Jeff Dike 已提交
72
	while (remain != 0) {
73
		expected = (remain < len) ? remain : len;
J
Jeff Dike 已提交
74
		ret = read(fd, output, expected);
75
		if (ret != expected) {
J
Jeff Dike 已提交
76 77
			if (ret < 0)
				ret = -errno;
78 79
			str = "data";
			goto err;
L
Linus Torvalds 已提交
80
		}
81
		remain -= ret;
L
Linus Torvalds 已提交
82
	}
83

L
Linus Torvalds 已提交
84
	return;
85 86 87

err:
	if (ret < 0)
J
Jeff Dike 已提交
88 89
		printk(UM_KERN_ERR "read_output - read of %s failed, "
		       "errno = %d\n", str, -ret);
90
	else
J
Jeff Dike 已提交
91 92
		printk(UM_KERN_ERR "read_output - read of %s failed, read only "
		       "%d of %d bytes\n", str, ret, expected);
L
Linus Torvalds 已提交
93 94 95 96 97 98
}

int net_read(int fd, void *buf, int len)
{
	int n;

J
Jeff Dike 已提交
99
	n = read(fd,  buf,  len);
L
Linus Torvalds 已提交
100

J
Jeff Dike 已提交
101
	if ((n < 0) && (errno == EAGAIN))
J
Jeff Dike 已提交
102
		return 0;
J
Jeff Dike 已提交
103
	else if (n == 0)
J
Jeff Dike 已提交
104 105
		return -ENOTCONN;
	return n;
L
Linus Torvalds 已提交
106 107 108 109 110 111
}

int net_recvfrom(int fd, void *buf, int len)
{
	int n;

112
	CATCH_EINTR(n = recvfrom(fd,  buf,  len, 0, NULL, NULL));
J
Jeff Dike 已提交
113 114
	if (n < 0) {
		if (errno == EAGAIN)
J
Jeff Dike 已提交
115 116
			return 0;
		return -errno;
L
Linus Torvalds 已提交
117
	}
J
Jeff Dike 已提交
118
	else if (n == 0)
J
Jeff Dike 已提交
119 120
		return -ENOTCONN;
	return n;
L
Linus Torvalds 已提交
121 122 123 124 125 126
}

int net_write(int fd, void *buf, int len)
{
	int n;

J
Jeff Dike 已提交
127
	n = write(fd, buf, len);
L
Linus Torvalds 已提交
128

J
Jeff Dike 已提交
129
	if ((n < 0) && (errno == EAGAIN))
J
Jeff Dike 已提交
130
		return 0;
J
Jeff Dike 已提交
131
	else if (n == 0)
J
Jeff Dike 已提交
132 133
		return -ENOTCONN;
	return n;
L
Linus Torvalds 已提交
134 135 136 137 138 139
}

int net_send(int fd, void *buf, int len)
{
	int n;

140
	CATCH_EINTR(n = send(fd, buf, len, 0));
J
Jeff Dike 已提交
141 142
	if (n < 0) {
		if (errno == EAGAIN)
J
Jeff Dike 已提交
143 144
			return 0;
		return -errno;
L
Linus Torvalds 已提交
145
	}
J
Jeff Dike 已提交
146
	else if (n == 0)
J
Jeff Dike 已提交
147 148
		return -ENOTCONN;
	return n;
L
Linus Torvalds 已提交
149 150 151 152 153 154
}

int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
{
	int n;

155 156
	CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
			       sock_len));
J
Jeff Dike 已提交
157 158
	if (n < 0) {
		if (errno == EAGAIN)
J
Jeff Dike 已提交
159 160
			return 0;
		return -errno;
L
Linus Torvalds 已提交
161
	}
J
Jeff Dike 已提交
162
	else if (n == 0)
J
Jeff Dike 已提交
163 164
		return -ENOTCONN;
	return n;
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173 174 175
}

struct change_pre_exec_data {
	int close_me;
	int stdout;
};

static void change_pre_exec(void *arg)
{
	struct change_pre_exec_data *data = arg;

J
Jeff Dike 已提交
176
	close(data->close_me);
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185
	dup2(data->stdout, 1);
}

static int change_tramp(char **argv, char *output, int output_len)
{
	int pid, fds[2], err;
	struct change_pre_exec_data pe_data;

	err = os_pipe(fds, 1, 0);
J
Jeff Dike 已提交
186 187 188
	if (err < 0) {
		printk(UM_KERN_ERR "change_tramp - pipe failed, err = %d\n",
		       -err);
J
Jeff Dike 已提交
189
		return err;
L
Linus Torvalds 已提交
190 191 192
	}
	pe_data.close_me = fds[0];
	pe_data.stdout = fds[1];
J
Jeff Dike 已提交
193
	pid = run_helper(change_pre_exec, &pe_data, argv);
L
Linus Torvalds 已提交
194

195 196 197
	if (pid > 0)	/* Avoid hang as we won't get data in failure case. */
		read_output(fds[0], output, output_len);

J
Jeff Dike 已提交
198 199
	close(fds[0]);
	close(fds[1]);
L
Linus Torvalds 已提交
200 201

	if (pid > 0)
J
Jeff Dike 已提交
202
		helper_wait(pid);
J
Jeff Dike 已提交
203
	return pid;
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211
}

static void change(char *dev, char *what, unsigned char *addr,
		   unsigned char *netmask)
{
	char addr_buf[sizeof("255.255.255.255\0")];
	char netmask_buf[sizeof("255.255.255.255\0")];
	char version[sizeof("nnnnn\0")];
J
Jeff Dike 已提交
212
	char *argv[] = { "uml_net", version, what, dev, addr_buf,
L
Linus Torvalds 已提交
213 214 215 216 217 218
			 netmask_buf, NULL };
	char *output;
	int output_len, pid;

	sprintf(version, "%d", UML_NET_VERSION);
	sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
J
Jeff Dike 已提交
219
	sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
L
Linus Torvalds 已提交
220 221
		netmask[2], netmask[3]);

J
Jeff Dike 已提交
222
	output_len = UM_KERN_PAGE_SIZE;
J
Jeff Dike 已提交
223
	output = uml_kmalloc(output_len, UM_GFP_KERNEL);
J
Jeff Dike 已提交
224 225 226
	if (output == NULL)
		printk(UM_KERN_ERR "change : failed to allocate output "
		       "buffer\n");
L
Linus Torvalds 已提交
227 228

	pid = change_tramp(argv, output, output_len);
229 230 231 232
	if (pid < 0) {
		kfree(output);
		return;
	}
L
Linus Torvalds 已提交
233

J
Jeff Dike 已提交
234
	if (output != NULL) {
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		printk("%s", output);
		kfree(output);
	}
}

void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
{
	change(arg, "add", addr, netmask);
}

void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
{
	change(arg, "del", addr, netmask);
}

char *split_if_spec(char *str, ...)
{
	char **arg, *end;
	va_list ap;

	va_start(ap, str);
J
Jeff Dike 已提交
256 257
	while ((arg = va_arg(ap, char **)) != NULL) {
		if (*str == '\0')
J
Jeff Dike 已提交
258
			return NULL;
L
Linus Torvalds 已提交
259
		end = strchr(str, ',');
J
Jeff Dike 已提交
260
		if (end != str)
L
Linus Torvalds 已提交
261
			*arg = str;
J
Jeff Dike 已提交
262
		if (end == NULL)
J
Jeff Dike 已提交
263
			return NULL;
L
Linus Torvalds 已提交
264 265 266 267
		*end++ = '\0';
		str = end;
	}
	va_end(ap);
J
Jeff Dike 已提交
268
	return str;
L
Linus Torvalds 已提交
269
}