2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
12 #include <sys/socket.h>
15 #include <sys/ioctl.h>
17 #include <linux/if_tun.h>
20 #include "kern_util.h"
24 #define MAX_PACKET ETH_MAX_PACKET
26 static int tuntap_user_init(void *data, void *dev)
28 struct tuntap_data *pri = data;
34 static void tuntap_add_addr(unsigned char *addr, unsigned char *netmask,
37 struct tuntap_data *pri = data;
39 tap_check_ips(pri->gate_addr, addr);
40 if((pri->fd == -1) || pri->fixed_config)
42 open_addr(addr, netmask, pri->dev_name);
45 static void tuntap_del_addr(unsigned char *addr, unsigned char *netmask,
48 struct tuntap_data *pri = data;
50 if((pri->fd == -1) || pri->fixed_config)
52 close_addr(addr, netmask, pri->dev_name);
55 struct tuntap_pre_exec_data {
60 static void tuntap_pre_exec(void *arg)
62 struct tuntap_pre_exec_data *data = arg;
64 dup2(data->stdout, 1);
65 os_close_file(data->close_me);
68 static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote,
69 char *buffer, int buffer_len, int *used_out)
71 struct tuntap_pre_exec_data data;
72 char version_buf[sizeof("nnnnn\0")];
73 char *argv[] = { "uml_net", version_buf, "tuntap", "up", gate,
75 char buf[CMSG_SPACE(sizeof(*fd_out))];
81 sprintf(version_buf, "%d", UML_NET_VERSION);
86 pid = run_helper(tuntap_pre_exec, &data, argv);
91 os_close_file(remote);
96 iov = ((struct iovec) { buffer, buffer_len });
104 msg.msg_control = buf;
105 msg.msg_controllen = sizeof(buf);
107 n = recvmsg(me, &msg, 0);
111 printk("tuntap_open_tramp : recvmsg failed - errno = %d\n",
115 CATCH_EINTR(waitpid(pid, NULL, 0));
117 cmsg = CMSG_FIRSTHDR(&msg);
119 printk("tuntap_open_tramp : didn't receive a message\n");
122 if((cmsg->cmsg_level != SOL_SOCKET) ||
123 (cmsg->cmsg_type != SCM_RIGHTS)){
124 printk("tuntap_open_tramp : didn't receive a descriptor\n");
127 *fd_out = ((int *) CMSG_DATA(cmsg))[0];
128 os_set_exec_close(*fd_out, 1);
132 static int tuntap_open(void *data)
135 struct tuntap_data *pri = data;
136 char *output, *buffer;
137 int err, fds[2], len, used;
139 err = tap_open_common(pri->dev, pri->gate_addr);
143 if(pri->fixed_config){
144 pri->fd = os_open_file("/dev/net/tun",
145 of_cloexec(of_rdwr(OPENFLAGS())), 0);
147 printk("Failed to open /dev/net/tun, err = %d\n",
151 memset(&ifr, 0, sizeof(ifr));
152 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
153 strlcpy(ifr.ifr_name, pri->dev_name, sizeof(ifr.ifr_name));
154 if(ioctl(pri->fd, TUNSETIFF, (void *) &ifr) < 0){
156 printk("TUNSETIFF failed, errno = %d\n", errno);
157 os_close_file(pri->fd);
162 err = os_pipe(fds, 0, 0);
164 printk("tuntap_open : os_pipe failed - err = %d\n",
169 buffer = get_output_buffer(&len);
170 if(buffer != NULL) len--;
173 err = tuntap_open_tramp(pri->gate_addr, &pri->fd, fds[0],
174 fds[1], buffer, len, &used);
178 printk("%s", output);
179 free_output_buffer(buffer);
180 printk("tuntap_open_tramp failed - err = %d\n", -err);
184 pri->dev_name = uml_strdup(buffer);
186 printk("%s", output);
187 free_output_buffer(buffer);
189 os_close_file(fds[0]);
190 iter_addresses(pri->dev, open_addr, pri->dev_name);
196 static void tuntap_close(int fd, void *data)
198 struct tuntap_data *pri = data;
200 if(!pri->fixed_config)
201 iter_addresses(pri->dev, close_addr, pri->dev_name);
206 static int tuntap_set_mtu(int mtu, void *data)
211 const struct net_user_info tuntap_user_info = {
212 .init = tuntap_user_init,
214 .close = tuntap_close,
216 .set_mtu = tuntap_set_mtu,
217 .add_address = tuntap_add_addr,
218 .delete_address = tuntap_del_addr,
219 .max_packet = MAX_PACKET