2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <sys/socket.h>
16 #include "user_util.h"
17 #include "kern_util.h"
21 int tap_open_common(void *dev, char *gate_addr)
27 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);
35 void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
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");
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;
64 ret = os_read_file(fd, &remain, sizeof(remain));
66 if (ret != sizeof(remain)) {
67 expected = sizeof(remain);
73 expected = (remain < len) ? remain : len;
74 ret = os_read_file(fd, output, expected);
75 if (ret != expected) {
86 printk("read_output - read of %s failed, errno = %d\n", str, -ret);
88 printk("read_output - read of %s failed, read only %d of %d bytes\n", str, ret, expected);
91 int net_read(int fd, void *buf, int len)
95 n = os_read_file(fd, buf, len);
104 int net_recvfrom(int fd, void *buf, int len)
108 CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
119 int net_write(int fd, void *buf, int len)
123 n = os_write_file(fd, buf, len);
132 int net_send(int fd, void *buf, int len)
136 CATCH_EINTR(n = send(fd, buf, len, 0));
147 int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
151 CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
163 struct change_pre_exec_data {
168 static void change_pre_exec(void *arg)
170 struct change_pre_exec_data *data = arg;
172 os_close_file(data->close_me);
173 dup2(data->stdout, 1);
176 static int change_tramp(char **argv, char *output, int output_len)
178 int pid, fds[2], err;
179 struct change_pre_exec_data pe_data;
181 err = os_pipe(fds, 1, 0);
183 printk("change_tramp - pipe failed, err = %d\n", -err);
186 pe_data.close_me = fds[0];
187 pe_data.stdout = fds[1];
188 pid = run_helper(change_pre_exec, &pe_data, argv, NULL);
190 if (pid > 0) /* Avoid hang as we won't get data in failure case. */
191 read_output(fds[0], output, output_len);
193 os_close_file(fds[0]);
194 os_close_file(fds[1]);
197 CATCH_EINTR(err = waitpid(pid, NULL, 0));
201 static void change(char *dev, char *what, unsigned char *addr,
202 unsigned char *netmask)
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,
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]);
217 output_len = page_size();
218 output = um_kmalloc(output_len);
220 printk("change : failed to allocate output buffer\n");
222 pid = change_tramp(argv, output, output_len);
226 printk("%s", output);
231 void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
233 change(arg, "add", addr, netmask);
236 void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
238 change(arg, "del", addr, netmask);
241 char *split_if_spec(char *str, ...)
247 while((arg = va_arg(ap, char **)) != NULL){
250 end = strchr(str, ',');