blob: 107c5e43fa00fec37d6fcf8c3b01851274a9b87e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stddef.h>
7#include <stdarg.h>
8#include <unistd.h>
9#include <stdio.h>
10#include <errno.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/socket.h>
14#include <sys/wait.h>
15#include "user.h"
16#include "user_util.h"
17#include "kern_util.h"
18#include "net_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "os.h"
20
21int tap_open_common(void *dev, char *gate_addr)
22{
23 int tap_addr[4];
24
Jeff Dike108ffa82006-07-10 04:45:14 -070025 if(gate_addr == NULL)
26 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 if(sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
28 &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4){
29 printk("Invalid tap IP address - '%s'\n", gate_addr);
Jeff Dike108ffa82006-07-10 04:45:14 -070030 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 }
Jeff Dike108ffa82006-07-10 04:45:14 -070032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Jeff Dikeda00d9a2005-06-08 15:48:01 -070035void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
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("The tap IP address and the UML eth IP address"
47 " must be different\n");
48 }
49}
50
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080051/* Do reliable error handling as this fails frequently enough. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052void read_output(int fd, char *output, int len)
53{
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080054 int remain, ret, expected;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 char c;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080056 char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if(output == NULL){
59 output = &c;
60 len = sizeof(c);
61 }
62
63 *output = '\0';
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080064 ret = os_read_file(fd, &remain, sizeof(remain));
65
66 if (ret != sizeof(remain)) {
67 expected = sizeof(remain);
68 str = "length";
69 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
72 while(remain != 0){
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080073 expected = (remain < len) ? remain : len;
74 ret = os_read_file(fd, output, expected);
75 if (ret != expected) {
76 str = "data";
77 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080079 remain -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080083
84err:
85 if (ret < 0)
86 printk("read_output - read of %s failed, errno = %d\n", str, -ret);
87 else
88 printk("read_output - read of %s failed, read only %d of %d bytes\n", str, ret, expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91int net_read(int fd, void *buf, int len)
92{
93 int n;
94
95 n = os_read_file(fd, buf, len);
96
97 if(n == -EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -070098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 else if(n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700100 return -ENOTCONN;
101 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104int net_recvfrom(int fd, void *buf, int len)
105{
106 int n;
107
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700108 CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if(n < 0){
Jeff Dike108ffa82006-07-10 04:45:14 -0700110 if(errno == EAGAIN)
111 return 0;
112 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700114 else if(n == 0)
115 return -ENOTCONN;
116 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119int net_write(int fd, void *buf, int len)
120{
121 int n;
122
123 n = os_write_file(fd, buf, len);
124
125 if(n == -EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 else if(n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700128 return -ENOTCONN;
129 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132int net_send(int fd, void *buf, int len)
133{
134 int n;
135
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700136 CATCH_EINTR(n = send(fd, buf, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if(n < 0){
Jeff Dike108ffa82006-07-10 04:45:14 -0700138 if(errno == EAGAIN)
139 return 0;
140 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700142 else if(n == 0)
143 return -ENOTCONN;
144 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
148{
149 int n;
150
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700151 CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
152 sock_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if(n < 0){
Jeff Dike108ffa82006-07-10 04:45:14 -0700154 if(errno == EAGAIN)
155 return 0;
156 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700158 else if(n == 0)
159 return -ENOTCONN;
160 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163struct change_pre_exec_data {
164 int close_me;
165 int stdout;
166};
167
168static void change_pre_exec(void *arg)
169{
170 struct change_pre_exec_data *data = arg;
171
172 os_close_file(data->close_me);
173 dup2(data->stdout, 1);
174}
175
176static int change_tramp(char **argv, char *output, int output_len)
177{
178 int pid, fds[2], err;
179 struct change_pre_exec_data pe_data;
180
181 err = os_pipe(fds, 1, 0);
182 if(err < 0){
183 printk("change_tramp - pipe failed, err = %d\n", -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700184 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 pe_data.close_me = fds[0];
187 pe_data.stdout = fds[1];
188 pid = run_helper(change_pre_exec, &pe_data, argv, NULL);
189
Paolo 'Blaisorblade' Giarrussob1c332c2006-04-10 22:53:37 -0700190 if (pid > 0) /* Avoid hang as we won't get data in failure case. */
191 read_output(fds[0], output, output_len);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 os_close_file(fds[0]);
194 os_close_file(fds[1]);
195
196 if (pid > 0)
197 CATCH_EINTR(err = waitpid(pid, NULL, 0));
Jeff Dike108ffa82006-07-10 04:45:14 -0700198 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static void change(char *dev, char *what, unsigned char *addr,
202 unsigned char *netmask)
203{
204 char addr_buf[sizeof("255.255.255.255\0")];
205 char netmask_buf[sizeof("255.255.255.255\0")];
206 char version[sizeof("nnnnn\0")];
207 char *argv[] = { "uml_net", version, what, dev, addr_buf,
208 netmask_buf, NULL };
209 char *output;
210 int output_len, pid;
211
212 sprintf(version, "%d", UML_NET_VERSION);
213 sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
214 sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
215 netmask[2], netmask[3]);
216
217 output_len = page_size();
218 output = um_kmalloc(output_len);
219 if(output == NULL)
220 printk("change : failed to allocate output buffer\n");
221
222 pid = change_tramp(argv, output, output_len);
223 if(pid < 0) return;
224
225 if(output != NULL){
226 printk("%s", output);
227 kfree(output);
228 }
229}
230
231void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
232{
233 change(arg, "add", addr, netmask);
234}
235
236void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
237{
238 change(arg, "del", addr, netmask);
239}
240
241char *split_if_spec(char *str, ...)
242{
243 char **arg, *end;
244 va_list ap;
245
246 va_start(ap, str);
247 while((arg = va_arg(ap, char **)) != NULL){
248 if(*str == '\0')
Jeff Dike108ffa82006-07-10 04:45:14 -0700249 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 end = strchr(str, ',');
251 if(end != str)
252 *arg = str;
253 if(end == NULL)
Jeff Dike108ffa82006-07-10 04:45:14 -0700254 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *end++ = '\0';
256 str = end;
257 }
258 va_end(ap);
Jeff Dike108ffa82006-07-10 04:45:14 -0700259 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}