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