Linux 2.6.31.6
[linux/fpc-iii.git] / arch / um / drivers / net_user.c
blob9415dd9e63ef1e98c15505733c3ba1c1eff02029
1 /*
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
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"
20 int tap_open_common(void *dev, char *gate_addr)
22 int tap_addr[4];
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;
32 return 0;
35 void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
37 int tap_addr[4];
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");
51 /* Do reliable error handling as this fails frequently enough. */
52 void read_output(int fd, char *output, int len)
54 int remain, ret, expected;
55 char c;
56 char *str;
58 if (output == NULL) {
59 output = &c;
60 len = sizeof(c);
63 *output = '\0';
64 ret = read(fd, &remain, sizeof(remain));
66 if (ret != sizeof(remain)) {
67 if (ret < 0)
68 ret = -errno;
69 expected = sizeof(remain);
70 str = "length";
71 goto err;
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;
83 remain -= ret;
86 return;
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);
97 int net_read(int fd, void *buf, int len)
99 int n;
101 n = read(fd, buf, len);
103 if ((n < 0) && (errno == EAGAIN))
104 return 0;
105 else if (n == 0)
106 return -ENOTCONN;
107 return n;
110 int net_recvfrom(int fd, void *buf, int len)
112 int n;
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;
120 else if (n == 0)
121 return -ENOTCONN;
122 return n;
125 int net_write(int fd, void *buf, int len)
127 int n;
129 n = write(fd, buf, len);
131 if ((n < 0) && (errno == EAGAIN))
132 return 0;
133 else if (n == 0)
134 return -ENOTCONN;
135 return n;
138 int net_send(int fd, void *buf, int len)
140 int n;
142 CATCH_EINTR(n = send(fd, buf, len, 0));
143 if (n < 0) {
144 if (errno == EAGAIN)
145 return 0;
146 return -errno;
148 else if (n == 0)
149 return -ENOTCONN;
150 return n;
153 int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
155 int n;
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;
164 else if (n == 0)
165 return -ENOTCONN;
166 return n;
169 struct change_pre_exec_data {
170 int close_me;
171 int stdout;
174 static void change_pre_exec(void *arg)
176 struct change_pre_exec_data *data = arg;
178 close(data->close_me);
179 dup2(data->stdout, 1);
182 static int change_tramp(char **argv, char *output, int output_len)
184 int pid, fds[2], err;
185 struct change_pre_exec_data pe_data;
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;
193 pe_data.close_me = fds[0];
194 pe_data.stdout = fds[1];
195 pid = run_helper(change_pre_exec, &pe_data, argv);
197 if (pid > 0) /* Avoid hang as we won't get data in failure case. */
198 read_output(fds[0], output, output_len);
200 close(fds[0]);
201 close(fds[1]);
203 if (pid > 0)
204 helper_wait(pid);
205 return pid;
208 static void change(char *dev, char *what, unsigned char *addr,
209 unsigned char *netmask)
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;
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]);
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");
230 pid = change_tramp(argv, output, output_len);
231 if (pid < 0) return;
233 if (output != NULL) {
234 printk("%s", output);
235 kfree(output);
239 void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
241 change(arg, "add", addr, netmask);
244 void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
246 change(arg, "del", addr, netmask);
249 char *split_if_spec(char *str, ...)
251 char **arg, *end;
252 va_list ap;
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;
266 va_end(ap);
267 return str;