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"
21 #include "user_util.h"
26 #define MAX_PACKET ETH_MAX_PACKET
28 void tuntap_user_init(void *data
, void *dev
)
30 struct tuntap_data
*pri
= data
;
35 static void tuntap_add_addr(unsigned char *addr
, unsigned char *netmask
,
38 struct tuntap_data
*pri
= data
;
40 tap_check_ips(pri
->gate_addr
, addr
);
41 if((pri
->fd
== -1) || pri
->fixed_config
) return;
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
) return;
51 close_addr(addr
, netmask
, pri
->dev_name
);
54 struct tuntap_pre_exec_data
{
59 static void tuntap_pre_exec(void *arg
)
61 struct tuntap_pre_exec_data
*data
= arg
;
63 dup2(data
->stdout
, 1);
64 os_close_file(data
->close_me
);
67 static int tuntap_open_tramp(char *gate
, int *fd_out
, int me
, int remote
,
68 char *buffer
, int buffer_len
, int *used_out
)
70 struct tuntap_pre_exec_data data
;
71 char version_buf
[sizeof("nnnnn\0")];
72 char *argv
[] = { "uml_net", version_buf
, "tuntap", "up", gate
,
74 char buf
[CMSG_SPACE(sizeof(*fd_out
))];
80 sprintf(version_buf
, "%d", UML_NET_VERSION
);
85 pid
= run_helper(tuntap_pre_exec
, &data
, argv
, NULL
);
87 if(pid
< 0) return(-pid
);
89 os_close_file(remote
);
94 iov
= ((struct iovec
) { buffer
, buffer_len
});
102 msg
.msg_control
= buf
;
103 msg
.msg_controllen
= sizeof(buf
);
105 n
= recvmsg(me
, &msg
, 0);
108 printk("tuntap_open_tramp : recvmsg failed - errno = %d\n",
112 CATCH_EINTR(waitpid(pid
, NULL
, 0));
114 cmsg
= CMSG_FIRSTHDR(&msg
);
116 printk("tuntap_open_tramp : didn't receive a message\n");
119 if((cmsg
->cmsg_level
!= SOL_SOCKET
) ||
120 (cmsg
->cmsg_type
!= SCM_RIGHTS
)){
121 printk("tuntap_open_tramp : didn't receive a descriptor\n");
124 *fd_out
= ((int *) CMSG_DATA(cmsg
))[0];
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", of_rdwr(OPENFLAGS()), 0);
142 printk("Failed to open /dev/net/tun, err = %d\n",
146 memset(&ifr
, 0, sizeof(ifr
));
147 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
148 strlcpy(ifr
.ifr_name
, pri
->dev_name
, sizeof(ifr
.ifr_name
));
149 if(ioctl(pri
->fd
, TUNSETIFF
, (void *) &ifr
) < 0){
150 printk("TUNSETIFF failed, errno = %d\n", errno
);
151 os_close_file(pri
->fd
);
156 err
= os_pipe(fds
, 0, 0);
158 printk("tuntap_open : os_pipe failed - err = %d\n",
163 buffer
= get_output_buffer(&len
);
164 if(buffer
!= NULL
) len
--;
167 err
= tuntap_open_tramp(pri
->gate_addr
, &pri
->fd
, fds
[0],
168 fds
[1], buffer
, len
, &used
);
172 printk("%s", output
);
173 free_output_buffer(buffer
);
174 printk("tuntap_open_tramp failed - err = %d\n", -err
);
178 pri
->dev_name
= uml_strdup(buffer
);
180 printk("%s", output
);
181 free_output_buffer(buffer
);
183 os_close_file(fds
[0]);
184 iter_addresses(pri
->dev
, open_addr
, pri
->dev_name
);
190 static void tuntap_close(int fd
, void *data
)
192 struct tuntap_data
*pri
= data
;
194 if(!pri
->fixed_config
)
195 iter_addresses(pri
->dev
, close_addr
, pri
->dev_name
);
200 static int tuntap_set_mtu(int mtu
, void *data
)
205 struct net_user_info tuntap_user_info
= {
206 .init
= tuntap_user_init
,
208 .close
= tuntap_close
,
210 .set_mtu
= tuntap_set_mtu
,
211 .add_address
= tuntap_add_addr
,
212 .delete_address
= tuntap_del_addr
,
213 .max_packet
= MAX_PACKET
217 * Overrides for Emacs so that we follow Linus's tabbing style.
218 * Emacs will notice this stuff at the end of the file and automatically
219 * adjust the settings for this buffer only. This must remain at the end
221 * ---------------------------------------------------------------------------
223 * c-file-style: "linux"