net_user.c 5.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 
 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
 * Licensed under the GPL
 */

#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include "user.h"
#include "user_util.h"
#include "kern_util.h"
#include "net_user.h"
#include "os.h"

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

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

J
Jeff Dike 已提交
35
void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
{
	int tap_addr[4];

	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("The tap IP address and the UML eth IP address"
		       " must be different\n");
	}
}

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

	if(output == NULL){
		output = &c;
		len = sizeof(c);
	}
		
	*output = '\0';
64 65 66 67 68 69
	ret = os_read_file(fd, &remain, sizeof(remain));

	if (ret != sizeof(remain)) {
		expected = sizeof(remain);
		str = "length";
		goto err;
L
Linus Torvalds 已提交
70 71 72
	}

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

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

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

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

	n = os_read_file(fd,  buf,  len);

	if(n == -EAGAIN)
J
Jeff Dike 已提交
98
		return 0;
L
Linus Torvalds 已提交
99
	else if(n == 0)
J
Jeff Dike 已提交
100 101
		return -ENOTCONN;
	return n;
L
Linus Torvalds 已提交
102 103 104 105 106 107
}

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

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

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

	n = os_write_file(fd, buf, len);

	if(n == -EAGAIN)
J
Jeff Dike 已提交
126
		return 0;
L
Linus Torvalds 已提交
127
	else if(n == 0)
J
Jeff Dike 已提交
128 129
		return -ENOTCONN;
	return n;
L
Linus Torvalds 已提交
130 131 132 133 134 135
}

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

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

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

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

struct change_pre_exec_data {
	int close_me;
	int stdout;
};

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

	os_close_file(data->close_me);
	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);
	if(err < 0){
		printk("change_tramp - pipe failed, err = %d\n", -err);
J
Jeff Dike 已提交
184
		return err;
L
Linus Torvalds 已提交
185 186 187 188 189
	}
	pe_data.close_me = fds[0];
	pe_data.stdout = fds[1];
	pid = run_helper(change_pre_exec, &pe_data, argv, NULL);

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

L
Linus Torvalds 已提交
193 194 195 196 197
	os_close_file(fds[0]);
	os_close_file(fds[1]);

	if (pid > 0)
		CATCH_EINTR(err = waitpid(pid, NULL, 0));
J
Jeff Dike 已提交
198
	return pid;
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
}

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")];
	char *argv[] = { "uml_net", version, what, dev, addr_buf, 
			 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]);
	sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1], 
		netmask[2], netmask[3]);

	output_len = page_size();
	output = um_kmalloc(output_len);
	if(output == NULL)
		printk("change : failed to allocate output buffer\n");

	pid = change_tramp(argv, output, output_len);
	if(pid < 0) return;

	if(output != NULL){
		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);
	while((arg = va_arg(ap, char **)) != NULL){
		if(*str == '\0')
J
Jeff Dike 已提交
249
			return NULL;
L
Linus Torvalds 已提交
250 251 252 253
		end = strchr(str, ',');
		if(end != str)
			*arg = str;
		if(end == NULL)
J
Jeff Dike 已提交
254
			return NULL;
L
Linus Torvalds 已提交
255 256 257 258
		*end++ = '\0';
		str = end;
	}
	va_end(ap);
J
Jeff Dike 已提交
259
	return str;
L
Linus Torvalds 已提交
260
}