2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <sys/socket.h>
17 #include "user_util.h"
18 #include "kern_util.h"
22 int tap_open_common(void *dev, char *gate_addr)
28 if(sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
29 &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4){
30 printk("Invalid tap IP address - '%s'\n", gate_addr);
36 void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
40 if((gate_addr != NULL) &&
41 (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
42 &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) &&
43 (eth_addr[0] == tap_addr[0]) &&
44 (eth_addr[1] == tap_addr[1]) &&
45 (eth_addr[2] == tap_addr[2]) &&
46 (eth_addr[3] == tap_addr[3])){
47 printk("The tap IP address and the UML eth IP address"
48 " must be different\n");
52 /* Do reliable error handling as this fails frequently enough. */
53 void read_output(int fd, char *output, int len)
55 int remain, ret, expected;
65 ret = os_read_file(fd, &remain, sizeof(remain));
67 if (ret != sizeof(remain)) {
68 expected = sizeof(remain);
74 expected = (remain < len) ? remain : len;
75 ret = os_read_file(fd, output, expected);
76 if (ret != expected) {
87 printk("read_output - read of %s failed, errno = %d\n", str, -ret);
89 printk("read_output - read of %s failed, read only %d of %d bytes\n", str, ret, expected);
92 int net_read(int fd, void *buf, int len)
96 n = os_read_file(fd, buf, len);
105 int net_recvfrom(int fd, void *buf, int len)
109 CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
120 int net_write(int fd, void *buf, int len)
124 n = os_write_file(fd, buf, len);
133 int net_send(int fd, void *buf, int len)
137 CATCH_EINTR(n = send(fd, buf, len, 0));
148 int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
152 CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
164 struct change_pre_exec_data {
169 static void change_pre_exec(void *arg)
171 struct change_pre_exec_data *data = arg;
173 os_close_file(data->close_me);
174 dup2(data->stdout, 1);
177 static int change_tramp(char **argv, char *output, int output_len)
179 int pid, fds[2], err;
180 struct change_pre_exec_data pe_data;
182 err = os_pipe(fds, 1, 0);
184 printk("change_tramp - pipe failed, err = %d\n", -err);
187 pe_data.close_me = fds[0];
188 pe_data.stdout = fds[1];
189 pid = run_helper(change_pre_exec, &pe_data, argv, NULL);
191 if (pid > 0) /* Avoid hang as we won't get data in failure case. */
192 read_output(fds[0], output, output_len);
194 os_close_file(fds[0]);
195 os_close_file(fds[1]);
198 CATCH_EINTR(err = waitpid(pid, NULL, 0));
202 static void change(char *dev, char *what, unsigned char *addr,
203 unsigned char *netmask)
205 char addr_buf[sizeof("255.255.255.255\0")];
206 char netmask_buf[sizeof("255.255.255.255\0")];
207 char version[sizeof("nnnnn\0")];
208 char *argv[] = { "uml_net", version, what, dev, addr_buf,
213 sprintf(version, "%d", UML_NET_VERSION);
214 sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
215 sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
216 netmask[2], netmask[3]);
218 output_len = page_size();
219 output = um_kmalloc(output_len);
221 printk("change : failed to allocate output buffer\n");
223 pid = change_tramp(argv, output, output_len);
227 printk("%s", output);
232 void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
234 change(arg, "add", addr, netmask);
237 void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
239 change(arg, "del", addr, netmask);
242 char *split_if_spec(char *str, ...)
248 while((arg = va_arg(ap, char **)) != NULL){
251 end = strchr(str, ',');