2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
10 #include <linux/if_tun.h>
12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
16 #include "kern_util.h"
20 static int tuntap_user_init(void *data
, void *dev
)
22 struct tuntap_data
*pri
= data
;
28 static void tuntap_add_addr(unsigned char *addr
, unsigned char *netmask
,
31 struct tuntap_data
*pri
= data
;
33 tap_check_ips(pri
->gate_addr
, addr
);
34 if ((pri
->fd
== -1) || pri
->fixed_config
)
36 open_addr(addr
, netmask
, pri
->dev_name
);
39 static void tuntap_del_addr(unsigned char *addr
, unsigned char *netmask
,
42 struct tuntap_data
*pri
= data
;
44 if ((pri
->fd
== -1) || pri
->fixed_config
)
46 close_addr(addr
, netmask
, pri
->dev_name
);
49 struct tuntap_pre_exec_data
{
54 static void tuntap_pre_exec(void *arg
)
56 struct tuntap_pre_exec_data
*data
= arg
;
58 dup2(data
->stdout
, 1);
59 close(data
->close_me
);
62 static int tuntap_open_tramp(char *gate
, int *fd_out
, int me
, int remote
,
63 char *buffer
, int buffer_len
, int *used_out
)
65 struct tuntap_pre_exec_data data
;
66 char version_buf
[sizeof("nnnnn\0")];
67 char *argv
[] = { "uml_net", version_buf
, "tuntap", "up", gate
,
69 char buf
[CMSG_SPACE(sizeof(*fd_out
))];
75 sprintf(version_buf
, "%d", UML_NET_VERSION
);
80 pid
= run_helper(tuntap_pre_exec
, &data
, argv
);
90 iov
= ((struct iovec
) { buffer
, buffer_len
});
98 msg
.msg_control
= buf
;
99 msg
.msg_controllen
= sizeof(buf
);
101 n
= recvmsg(me
, &msg
, 0);
105 printk(UM_KERN_ERR
"tuntap_open_tramp : recvmsg failed - "
106 "errno = %d\n", errno
);
111 cmsg
= CMSG_FIRSTHDR(&msg
);
113 printk(UM_KERN_ERR
"tuntap_open_tramp : didn't receive a "
117 if ((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
118 (cmsg
->cmsg_type
!= SCM_RIGHTS
)) {
119 printk(UM_KERN_ERR
"tuntap_open_tramp : didn't receive a "
123 *fd_out
= ((int *) CMSG_DATA(cmsg
))[0];
124 os_set_exec_close(*fd_out
);
128 static int tuntap_open(void *data
)
131 struct tuntap_data
*pri
= data
;
132 char *output
, *buffer
;
133 int err
, fds
[2], len
, used
;
135 err
= tap_open_common(pri
->dev
, pri
->gate_addr
);
139 if (pri
->fixed_config
) {
140 pri
->fd
= os_open_file("/dev/net/tun",
141 of_cloexec(of_rdwr(OPENFLAGS())), 0);
143 printk(UM_KERN_ERR
"Failed to open /dev/net/tun, "
144 "err = %d\n", -pri
->fd
);
147 memset(&ifr
, 0, sizeof(ifr
));
148 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
149 strlcpy(ifr
.ifr_name
, pri
->dev_name
, sizeof(ifr
.ifr_name
));
150 if (ioctl(pri
->fd
, TUNSETIFF
, &ifr
) < 0) {
152 printk(UM_KERN_ERR
"TUNSETIFF failed, errno = %d\n",
159 err
= socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fds
);
162 printk(UM_KERN_ERR
"tuntap_open : socketpair failed - "
163 "errno = %d\n", errno
);
167 buffer
= get_output_buffer(&len
);
172 err
= tuntap_open_tramp(pri
->gate_addr
, &pri
->fd
, fds
[0],
173 fds
[1], buffer
, len
, &used
);
177 printk("%s", output
);
178 free_output_buffer(buffer
);
179 printk(UM_KERN_ERR
"tuntap_open_tramp failed - "
184 pri
->dev_name
= uml_strdup(buffer
);
186 printk("%s", output
);
187 free_output_buffer(buffer
);
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 const struct net_user_info tuntap_user_info
= {
207 .init
= tuntap_user_init
,
209 .close
= tuntap_close
,
211 .add_address
= tuntap_add_addr
,
212 .delete_address
= tuntap_del_addr
,
213 .mtu
= ETH_MAX_PACKET
,
214 .max_packet
= ETH_MAX_PACKET
+ ETH_HEADER_OTHER
,