xref: /linux/arch/um/drivers/net_user.c (revision 6ee738610f41b59733f63718f0bdbcba7d3a3f12)
1 /*
2  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5 
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <stddef.h>
11 #include <string.h>
12 #include <sys/socket.h>
13 #include <sys/wait.h>
14 #include "net_user.h"
15 #include "kern_constants.h"
16 #include "os.h"
17 #include "um_malloc.h"
18 #include "user.h"
19 
20 int tap_open_common(void *dev, char *gate_addr)
21 {
22 	int tap_addr[4];
23 
24 	if (gate_addr == NULL)
25 		return 0;
26 	if (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
27 		  &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4) {
28 		printk(UM_KERN_ERR "Invalid tap IP address - '%s'\n",
29 		       gate_addr);
30 		return -EINVAL;
31 	}
32 	return 0;
33 }
34 
35 void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
36 {
37 	int tap_addr[4];
38 
39 	if ((gate_addr != NULL) &&
40 	    (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
41 		    &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) &&
42 	    (eth_addr[0] == tap_addr[0]) &&
43 	    (eth_addr[1] == tap_addr[1]) &&
44 	    (eth_addr[2] == tap_addr[2]) &&
45 	    (eth_addr[3] == tap_addr[3])) {
46 		printk(UM_KERN_ERR "The tap IP address and the UML eth IP "
47 		       "address must be different\n");
48 	}
49 }
50 
51 /* Do reliable error handling as this fails frequently enough. */
52 void read_output(int fd, char *output, int len)
53 {
54 	int remain, ret, expected;
55 	char c;
56 	char *str;
57 
58 	if (output == NULL) {
59 		output = &c;
60 		len = sizeof(c);
61 	}
62 
63 	*output = '\0';
64 	ret = read(fd, &remain, sizeof(remain));
65 
66 	if (ret != sizeof(remain)) {
67 		if (ret < 0)
68 			ret = -errno;
69 		expected = sizeof(remain);
70 		str = "length";
71 		goto err;
72 	}
73 
74 	while (remain != 0) {
75 		expected = (remain < len) ? remain : len;
76 		ret = read(fd, output, expected);
77 		if (ret != expected) {
78 			if (ret < 0)
79 				ret = -errno;
80 			str = "data";
81 			goto err;
82 		}
83 		remain -= ret;
84 	}
85 
86 	return;
87 
88 err:
89 	if (ret < 0)
90 		printk(UM_KERN_ERR "read_output - read of %s failed, "
91 		       "errno = %d\n", str, -ret);
92 	else
93 		printk(UM_KERN_ERR "read_output - read of %s failed, read only "
94 		       "%d of %d bytes\n", str, ret, expected);
95 }
96 
97 int net_read(int fd, void *buf, int len)
98 {
99 	int n;
100 
101 	n = read(fd,  buf,  len);
102 
103 	if ((n < 0) && (errno == EAGAIN))
104 		return 0;
105 	else if (n == 0)
106 		return -ENOTCONN;
107 	return n;
108 }
109 
110 int net_recvfrom(int fd, void *buf, int len)
111 {
112 	int n;
113 
114 	CATCH_EINTR(n = recvfrom(fd,  buf,  len, 0, NULL, NULL));
115 	if (n < 0) {
116 		if (errno == EAGAIN)
117 			return 0;
118 		return -errno;
119 	}
120 	else if (n == 0)
121 		return -ENOTCONN;
122 	return n;
123 }
124 
125 int net_write(int fd, void *buf, int len)
126 {
127 	int n;
128 
129 	n = write(fd, buf, len);
130 
131 	if ((n < 0) && (errno == EAGAIN))
132 		return 0;
133 	else if (n == 0)
134 		return -ENOTCONN;
135 	return n;
136 }
137 
138 int net_send(int fd, void *buf, int len)
139 {
140 	int n;
141 
142 	CATCH_EINTR(n = send(fd, buf, len, 0));
143 	if (n < 0) {
144 		if (errno == EAGAIN)
145 			return 0;
146 		return -errno;
147 	}
148 	else if (n == 0)
149 		return -ENOTCONN;
150 	return n;
151 }
152 
153 int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
154 {
155 	int n;
156 
157 	CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
158 			       sock_len));
159 	if (n < 0) {
160 		if (errno == EAGAIN)
161 			return 0;
162 		return -errno;
163 	}
164 	else if (n == 0)
165 		return -ENOTCONN;
166 	return n;
167 }
168 
169 struct change_pre_exec_data {
170 	int close_me;
171 	int stdout;
172 };
173 
174 static void change_pre_exec(void *arg)
175 {
176 	struct change_pre_exec_data *data = arg;
177 
178 	close(data->close_me);
179 	dup2(data->stdout, 1);
180 }
181 
182 static int change_tramp(char **argv, char *output, int output_len)
183 {
184 	int pid, fds[2], err;
185 	struct change_pre_exec_data pe_data;
186 
187 	err = os_pipe(fds, 1, 0);
188 	if (err < 0) {
189 		printk(UM_KERN_ERR "change_tramp - pipe failed, err = %d\n",
190 		       -err);
191 		return err;
192 	}
193 	pe_data.close_me = fds[0];
194 	pe_data.stdout = fds[1];
195 	pid = run_helper(change_pre_exec, &pe_data, argv);
196 
197 	if (pid > 0)	/* Avoid hang as we won't get data in failure case. */
198 		read_output(fds[0], output, output_len);
199 
200 	close(fds[0]);
201 	close(fds[1]);
202 
203 	if (pid > 0)
204 		helper_wait(pid);
205 	return pid;
206 }
207 
208 static void change(char *dev, char *what, unsigned char *addr,
209 		   unsigned char *netmask)
210 {
211 	char addr_buf[sizeof("255.255.255.255\0")];
212 	char netmask_buf[sizeof("255.255.255.255\0")];
213 	char version[sizeof("nnnnn\0")];
214 	char *argv[] = { "uml_net", version, what, dev, addr_buf,
215 			 netmask_buf, NULL };
216 	char *output;
217 	int output_len, pid;
218 
219 	sprintf(version, "%d", UML_NET_VERSION);
220 	sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
221 	sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
222 		netmask[2], netmask[3]);
223 
224 	output_len = UM_KERN_PAGE_SIZE;
225 	output = uml_kmalloc(output_len, UM_GFP_KERNEL);
226 	if (output == NULL)
227 		printk(UM_KERN_ERR "change : failed to allocate output "
228 		       "buffer\n");
229 
230 	pid = change_tramp(argv, output, output_len);
231 	if (pid < 0) return;
232 
233 	if (output != NULL) {
234 		printk("%s", output);
235 		kfree(output);
236 	}
237 }
238 
239 void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
240 {
241 	change(arg, "add", addr, netmask);
242 }
243 
244 void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
245 {
246 	change(arg, "del", addr, netmask);
247 }
248 
249 char *split_if_spec(char *str, ...)
250 {
251 	char **arg, *end;
252 	va_list ap;
253 
254 	va_start(ap, str);
255 	while ((arg = va_arg(ap, char **)) != NULL) {
256 		if (*str == '\0')
257 			return NULL;
258 		end = strchr(str, ',');
259 		if (end != str)
260 			*arg = str;
261 		if (end == NULL)
262 			return NULL;
263 		*end++ = '\0';
264 		str = end;
265 	}
266 	va_end(ap);
267 	return str;
268 }
269