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_constants.h"
21 static int tuntap_user_init(void *data
, void *dev
)
23 struct tuntap_data
*pri
= data
;
29 static void tuntap_add_addr(unsigned char *addr
, unsigned char *netmask
,
32 struct tuntap_data
*pri
= data
;
34 tap_check_ips(pri
->gate_addr
, addr
);
35 if ((pri
->fd
== -1) || pri
->fixed_config
)
37 open_addr(addr
, netmask
, pri
->dev_name
);
40 static void tuntap_del_addr(unsigned char *addr
, unsigned char *netmask
,
43 struct tuntap_data
*pri
= data
;
45 if ((pri
->fd
== -1) || pri
->fixed_config
)
47 close_addr(addr
, netmask
, pri
->dev_name
);
50 struct tuntap_pre_exec_data
{
55 static void tuntap_pre_exec(void *arg
)
57 struct tuntap_pre_exec_data
*data
= arg
;
59 dup2(data
->stdout
, 1);
60 close(data
->close_me
);
63 static int tuntap_open_tramp(char *gate
, int *fd_out
, int me
, int remote
,
64 char *buffer
, int buffer_len
, int *used_out
)
66 struct tuntap_pre_exec_data data
;
67 char version_buf
[sizeof("nnnnn\0")];
68 char *argv
[] = { "uml_net", version_buf
, "tuntap", "up", gate
,
70 char buf
[CMSG_SPACE(sizeof(*fd_out
))];
76 sprintf(version_buf
, "%d", UML_NET_VERSION
);
81 pid
= run_helper(tuntap_pre_exec
, &data
, argv
);
91 iov
= ((struct iovec
) { buffer
, buffer_len
});
99 msg
.msg_control
= buf
;
100 msg
.msg_controllen
= sizeof(buf
);
102 n
= recvmsg(me
, &msg
, 0);
106 printk(UM_KERN_ERR
"tuntap_open_tramp : recvmsg failed - "
107 "errno = %d\n", errno
);
110 helper_wait(pid
, 0, "tuntap_open_tramp");
112 cmsg
= CMSG_FIRSTHDR(&msg
);
114 printk(UM_KERN_ERR
"tuntap_open_tramp : didn't receive a "
118 if ((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
119 (cmsg
->cmsg_type
!= SCM_RIGHTS
)) {
120 printk(UM_KERN_ERR
"tuntap_open_tramp : didn't receive a "
124 *fd_out
= ((int *) CMSG_DATA(cmsg
))[0];
125 os_set_exec_close(*fd_out
);
129 static int tuntap_open(void *data
)
132 struct tuntap_data
*pri
= data
;
133 char *output
, *buffer
;
134 int err
, fds
[2], len
, used
;
136 err
= tap_open_common(pri
->dev
, pri
->gate_addr
);
140 if (pri
->fixed_config
) {
141 pri
->fd
= os_open_file("/dev/net/tun",
142 of_cloexec(of_rdwr(OPENFLAGS())), 0);
144 printk(UM_KERN_ERR
"Failed to open /dev/net/tun, "
145 "err = %d\n", -pri
->fd
);
148 memset(&ifr
, 0, sizeof(ifr
));
149 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
150 strlcpy(ifr
.ifr_name
, pri
->dev_name
, sizeof(ifr
.ifr_name
));
151 if (ioctl(pri
->fd
, TUNSETIFF
, (void *) &ifr
) < 0) {
153 printk(UM_KERN_ERR
"TUNSETIFF failed, errno = %d\n",
160 err
= socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fds
);
163 printk(UM_KERN_ERR
"tuntap_open : socketpair failed - "
164 "errno = %d\n", errno
);
168 buffer
= get_output_buffer(&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(UM_KERN_ERR
"tuntap_open_tramp failed - "
185 pri
->dev_name
= uml_strdup(buffer
);
187 printk("%s", output
);
188 free_output_buffer(buffer
);
191 iter_addresses(pri
->dev
, open_addr
, pri
->dev_name
);
197 static void tuntap_close(int fd
, void *data
)
199 struct tuntap_data
*pri
= data
;
201 if (!pri
->fixed_config
)
202 iter_addresses(pri
->dev
, close_addr
, pri
->dev_name
);
207 const struct net_user_info tuntap_user_info
= {
208 .init
= tuntap_user_init
,
210 .close
= tuntap_close
,
212 .add_address
= tuntap_add_addr
,
213 .delete_address
= tuntap_del_addr
,
214 .mtu
= ETH_MAX_PACKET
,
215 .max_packet
= ETH_MAX_PACKET
+ ETH_HEADER_OTHER
,