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"
17 #include "kern_util.h"
22 static int tuntap_user_init(void *data
, void *dev
)
24 struct tuntap_data
*pri
= data
;
30 static void tuntap_add_addr(unsigned char *addr
, unsigned char *netmask
,
33 struct tuntap_data
*pri
= data
;
35 tap_check_ips(pri
->gate_addr
, addr
);
36 if ((pri
->fd
== -1) || pri
->fixed_config
)
38 open_addr(addr
, netmask
, pri
->dev_name
);
41 static void tuntap_del_addr(unsigned char *addr
, unsigned char *netmask
,
44 struct tuntap_data
*pri
= data
;
46 if ((pri
->fd
== -1) || pri
->fixed_config
)
48 close_addr(addr
, netmask
, pri
->dev_name
);
51 struct tuntap_pre_exec_data
{
56 static void tuntap_pre_exec(void *arg
)
58 struct tuntap_pre_exec_data
*data
= arg
;
60 dup2(data
->stdout
, 1);
61 close(data
->close_me
);
64 static int tuntap_open_tramp(char *gate
, int *fd_out
, int me
, int remote
,
65 char *buffer
, int buffer_len
, int *used_out
)
67 struct tuntap_pre_exec_data data
;
68 char version_buf
[sizeof("nnnnn\0")];
69 char *argv
[] = { "uml_net", version_buf
, "tuntap", "up", gate
,
71 char buf
[CMSG_SPACE(sizeof(*fd_out
))];
77 sprintf(version_buf
, "%d", UML_NET_VERSION
);
82 pid
= run_helper(tuntap_pre_exec
, &data
, argv
);
92 iov
= ((struct iovec
) { buffer
, buffer_len
});
100 msg
.msg_control
= buf
;
101 msg
.msg_controllen
= sizeof(buf
);
103 n
= recvmsg(me
, &msg
, 0);
107 printk(UM_KERN_ERR
"tuntap_open_tramp : recvmsg failed - "
108 "errno = %d\n", errno
);
113 cmsg
= CMSG_FIRSTHDR(&msg
);
115 printk(UM_KERN_ERR
"tuntap_open_tramp : didn't receive a "
119 if ((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
120 (cmsg
->cmsg_type
!= SCM_RIGHTS
)) {
121 printk(UM_KERN_ERR
"tuntap_open_tramp : didn't receive a "
125 *fd_out
= ((int *) CMSG_DATA(cmsg
))[0];
126 os_set_exec_close(*fd_out
);
130 static int tuntap_open(void *data
)
133 struct tuntap_data
*pri
= data
;
134 char *output
, *buffer
;
135 int err
, fds
[2], len
, used
;
137 err
= tap_open_common(pri
->dev
, pri
->gate_addr
);
141 if (pri
->fixed_config
) {
142 pri
->fd
= os_open_file("/dev/net/tun",
143 of_cloexec(of_rdwr(OPENFLAGS())), 0);
145 printk(UM_KERN_ERR
"Failed to open /dev/net/tun, "
146 "err = %d\n", -pri
->fd
);
149 memset(&ifr
, 0, sizeof(ifr
));
150 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
151 strlcpy(ifr
.ifr_name
, pri
->dev_name
, sizeof(ifr
.ifr_name
));
152 if (ioctl(pri
->fd
, TUNSETIFF
, &ifr
) < 0) {
154 printk(UM_KERN_ERR
"TUNSETIFF failed, errno = %d\n",
161 err
= socketpair(AF_UNIX
, SOCK_DGRAM
, 0, fds
);
164 printk(UM_KERN_ERR
"tuntap_open : socketpair failed - "
165 "errno = %d\n", errno
);
169 buffer
= get_output_buffer(&len
);
174 err
= tuntap_open_tramp(pri
->gate_addr
, &pri
->fd
, fds
[0],
175 fds
[1], buffer
, len
, &used
);
179 printk("%s", output
);
180 free_output_buffer(buffer
);
181 printk(UM_KERN_ERR
"tuntap_open_tramp failed - "
186 pri
->dev_name
= uml_strdup(buffer
);
188 printk("%s", output
);
189 free_output_buffer(buffer
);
192 iter_addresses(pri
->dev
, open_addr
, pri
->dev_name
);
198 static void tuntap_close(int fd
, void *data
)
200 struct tuntap_data
*pri
= data
;
202 if (!pri
->fixed_config
)
203 iter_addresses(pri
->dev
, close_addr
, pri
->dev_name
);
208 const struct net_user_info tuntap_user_info
= {
209 .init
= tuntap_user_init
,
211 .close
= tuntap_close
,
213 .add_address
= tuntap_add_addr
,
214 .delete_address
= tuntap_del_addr
,
215 .mtu
= ETH_MAX_PACKET
,
216 .max_packet
= ETH_MAX_PACKET
+ ETH_HEADER_OTHER
,