x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / arch / um / drivers / net_user.c
blobcd14157b556d178d14998a5e024a15629c3491ad
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 <os.h>
16 #include <um_malloc.h>
18 int tap_open_common(void *dev, char *gate_addr)
20 int tap_addr[4];
22 if (gate_addr == NULL)
23 return 0;
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);
28 return -EINVAL;
30 return 0;
33 void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
35 int tap_addr[4];
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");
49 /* Do reliable error handling as this fails frequently enough. */
50 void read_output(int fd, char *output, int len)
52 int remain, ret, expected;
53 char c;
54 char *str;
56 if (output == NULL) {
57 output = &c;
58 len = sizeof(c);
61 *output = '\0';
62 ret = read(fd, &remain, sizeof(remain));
64 if (ret != sizeof(remain)) {
65 if (ret < 0)
66 ret = -errno;
67 expected = sizeof(remain);
68 str = "length";
69 goto err;
72 while (remain != 0) {
73 expected = (remain < len) ? remain : len;
74 ret = read(fd, output, expected);
75 if (ret != expected) {
76 if (ret < 0)
77 ret = -errno;
78 str = "data";
79 goto err;
81 remain -= ret;
84 return;
86 err:
87 if (ret < 0)
88 printk(UM_KERN_ERR "read_output - read of %s failed, "
89 "errno = %d\n", str, -ret);
90 else
91 printk(UM_KERN_ERR "read_output - read of %s failed, read only "
92 "%d of %d bytes\n", str, ret, expected);
95 int net_read(int fd, void *buf, int len)
97 int n;
99 n = read(fd, buf, len);
101 if ((n < 0) && (errno == EAGAIN))
102 return 0;
103 else if (n == 0)
104 return -ENOTCONN;
105 return n;
108 int net_recvfrom(int fd, void *buf, int len)
110 int n;
112 CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
113 if (n < 0) {
114 if (errno == EAGAIN)
115 return 0;
116 return -errno;
118 else if (n == 0)
119 return -ENOTCONN;
120 return n;
123 int net_write(int fd, void *buf, int len)
125 int n;
127 n = write(fd, buf, len);
129 if ((n < 0) && (errno == EAGAIN))
130 return 0;
131 else if (n == 0)
132 return -ENOTCONN;
133 return n;
136 int net_send(int fd, void *buf, int len)
138 int n;
140 CATCH_EINTR(n = send(fd, buf, len, 0));
141 if (n < 0) {
142 if (errno == EAGAIN)
143 return 0;
144 return -errno;
146 else if (n == 0)
147 return -ENOTCONN;
148 return n;
151 int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
153 int n;
155 CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
156 sock_len));
157 if (n < 0) {
158 if (errno == EAGAIN)
159 return 0;
160 return -errno;
162 else if (n == 0)
163 return -ENOTCONN;
164 return n;
167 struct change_pre_exec_data {
168 int close_me;
169 int stdout;
172 static void change_pre_exec(void *arg)
174 struct change_pre_exec_data *data = arg;
176 close(data->close_me);
177 dup2(data->stdout, 1);
180 static int change_tramp(char **argv, char *output, int output_len)
182 int pid, fds[2], err;
183 struct change_pre_exec_data pe_data;
185 err = os_pipe(fds, 1, 0);
186 if (err < 0) {
187 printk(UM_KERN_ERR "change_tramp - pipe failed, err = %d\n",
188 -err);
189 return err;
191 pe_data.close_me = fds[0];
192 pe_data.stdout = fds[1];
193 pid = run_helper(change_pre_exec, &pe_data, argv);
195 if (pid > 0) /* Avoid hang as we won't get data in failure case. */
196 read_output(fds[0], output, output_len);
198 close(fds[0]);
199 close(fds[1]);
201 if (pid > 0)
202 helper_wait(pid);
203 return pid;
206 static void change(char *dev, char *what, unsigned char *addr,
207 unsigned char *netmask)
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")];
212 char *argv[] = { "uml_net", version, what, dev, addr_buf,
213 netmask_buf, NULL };
214 char *output;
215 int output_len, pid;
217 sprintf(version, "%d", UML_NET_VERSION);
218 sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
219 sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
220 netmask[2], netmask[3]);
222 output_len = UM_KERN_PAGE_SIZE;
223 output = uml_kmalloc(output_len, UM_GFP_KERNEL);
224 if (output == NULL)
225 printk(UM_KERN_ERR "change : failed to allocate output "
226 "buffer\n");
228 pid = change_tramp(argv, output, output_len);
229 if (pid < 0) {
230 kfree(output);
231 return;
234 if (output != NULL) {
235 printk("%s", output);
236 kfree(output);
240 void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
242 change(arg, "add", addr, netmask);
245 void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
247 change(arg, "del", addr, netmask);
250 char *split_if_spec(char *str, ...)
252 char **arg, *end;
253 va_list ap;
255 va_start(ap, str);
256 while ((arg = va_arg(ap, char **)) != NULL) {
257 if (*str == '\0')
258 return NULL;
259 end = strchr(str, ',');
260 if (end != str)
261 *arg = str;
262 if (end == NULL)
263 return NULL;
264 *end++ = '\0';
265 str = end;
267 va_end(ap);
268 return str;