Pull define-node-cleanup into release branch
[linux-2.6] / arch / um / os-Linux / drivers / tuntap_user.c
1 /* 
2  * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <sys/wait.h>
12 #include <sys/socket.h>
13 #include <sys/un.h>
14 #include <sys/uio.h>
15 #include <sys/ioctl.h>
16 #include <net/if.h>
17 #include <linux/if_tun.h>
18 #include "net_user.h"
19 #include "tuntap.h"
20 #include "kern_util.h"
21 #include "user_util.h"
22 #include "user.h"
23 #include "helper.h"
24 #include "os.h"
25
26 #define MAX_PACKET ETH_MAX_PACKET
27
28 void tuntap_user_init(void *data, void *dev)
29 {
30         struct tuntap_data *pri = data;
31
32         pri->dev = dev;
33 }
34
35 static void tuntap_add_addr(unsigned char *addr, unsigned char *netmask,
36                             void *data)
37 {
38         struct tuntap_data *pri = data;
39
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);
43 }
44
45 static void tuntap_del_addr(unsigned char *addr, unsigned char *netmask,
46                             void *data)
47 {
48         struct tuntap_data *pri = data;
49
50         if((pri->fd == -1) || pri->fixed_config) return;
51         close_addr(addr, netmask, pri->dev_name);
52 }
53
54 struct tuntap_pre_exec_data {
55         int stdout;
56         int close_me;
57 };
58
59 static void tuntap_pre_exec(void *arg)
60 {
61         struct tuntap_pre_exec_data *data = arg;
62         
63         dup2(data->stdout, 1);
64         os_close_file(data->close_me);
65 }
66
67 static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote,
68                              char *buffer, int buffer_len, int *used_out)
69 {
70         struct tuntap_pre_exec_data data;
71         char version_buf[sizeof("nnnnn\0")];
72         char *argv[] = { "uml_net", version_buf, "tuntap", "up", gate,
73                          NULL };
74         char buf[CMSG_SPACE(sizeof(*fd_out))];
75         struct msghdr msg;
76         struct cmsghdr *cmsg;
77         struct iovec iov;
78         int pid, n, err;
79
80         sprintf(version_buf, "%d", UML_NET_VERSION);
81
82         data.stdout = remote;
83         data.close_me = me;
84
85         pid = run_helper(tuntap_pre_exec, &data, argv, NULL);
86
87         if(pid < 0) return(-pid);
88
89         os_close_file(remote);
90
91         msg.msg_name = NULL;
92         msg.msg_namelen = 0;
93         if(buffer != NULL){
94                 iov = ((struct iovec) { buffer, buffer_len });
95                 msg.msg_iov = &iov;
96                 msg.msg_iovlen = 1;
97         }
98         else {
99                 msg.msg_iov = NULL;
100                 msg.msg_iovlen = 0;
101         }
102         msg.msg_control = buf;
103         msg.msg_controllen = sizeof(buf);
104         msg.msg_flags = 0;
105         n = recvmsg(me, &msg, 0);
106         *used_out = n;
107         if(n < 0){
108                 err = -errno;
109                 printk("tuntap_open_tramp : recvmsg failed - errno = %d\n", 
110                        errno);
111                 return err;
112         }
113         CATCH_EINTR(waitpid(pid, NULL, 0));
114
115         cmsg = CMSG_FIRSTHDR(&msg);
116         if(cmsg == NULL){
117                 printk("tuntap_open_tramp : didn't receive a message\n");
118                 return(-EINVAL);
119         }
120         if((cmsg->cmsg_level != SOL_SOCKET) || 
121            (cmsg->cmsg_type != SCM_RIGHTS)){
122                 printk("tuntap_open_tramp : didn't receive a descriptor\n");
123                 return(-EINVAL);
124         }
125         *fd_out = ((int *) CMSG_DATA(cmsg))[0];
126         return(0);
127 }
128
129 static int tuntap_open(void *data)
130 {
131         struct ifreq ifr;
132         struct tuntap_data *pri = data;
133         char *output, *buffer;
134         int err, fds[2], len, used;
135
136         err = tap_open_common(pri->dev, pri->gate_addr);
137         if(err < 0)
138                 return(err);
139
140         if(pri->fixed_config){
141                 pri->fd = os_open_file("/dev/net/tun", of_rdwr(OPENFLAGS()), 0);
142                 if(pri->fd < 0){
143                         printk("Failed to open /dev/net/tun, err = %d\n",
144                                -pri->fd);
145                         return(pri->fd);
146                 }
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, (void *) &ifr) < 0){
151                         err = -errno;
152                         printk("TUNSETIFF failed, errno = %d\n", errno);
153                         os_close_file(pri->fd);
154                         return err;
155                 }
156         }
157         else {
158                 err = os_pipe(fds, 0, 0);
159                 if(err < 0){
160                         printk("tuntap_open : os_pipe failed - err = %d\n",
161                                -err);
162                         return(err);
163                 }
164
165                 buffer = get_output_buffer(&len);
166                 if(buffer != NULL) len--;
167                 used = 0;
168
169                 err = tuntap_open_tramp(pri->gate_addr, &pri->fd, fds[0],
170                                         fds[1], buffer, len, &used);
171
172                 output = buffer;
173                 if(err < 0) {
174                         printk("%s", output);
175                         free_output_buffer(buffer);
176                         printk("tuntap_open_tramp failed - err = %d\n", -err);
177                         return(err);
178                 }
179
180                 pri->dev_name = uml_strdup(buffer);
181                 output += IFNAMSIZ;
182                 printk("%s", output);
183                 free_output_buffer(buffer);
184
185                 os_close_file(fds[0]);
186                 iter_addresses(pri->dev, open_addr, pri->dev_name);
187         }
188
189         return(pri->fd);
190 }
191
192 static void tuntap_close(int fd, void *data)
193 {
194         struct tuntap_data *pri = data;
195
196         if(!pri->fixed_config) 
197                 iter_addresses(pri->dev, close_addr, pri->dev_name);
198         os_close_file(fd);
199         pri->fd = -1;
200 }
201
202 static int tuntap_set_mtu(int mtu, void *data)
203 {
204         return(mtu);
205 }
206
207 struct net_user_info tuntap_user_info = {
208         .init           = tuntap_user_init,
209         .open           = tuntap_open,
210         .close          = tuntap_close,
211         .remove         = NULL,
212         .set_mtu        = tuntap_set_mtu,
213         .add_address    = tuntap_add_addr,
214         .delete_address = tuntap_del_addr,
215         .max_packet     = MAX_PACKET
216 };
217
218 /*
219  * Overrides for Emacs so that we follow Linus's tabbing style.
220  * Emacs will notice this stuff at the end of the file and automatically
221  * adjust the settings for this buffer only.  This must remain at the end
222  * of the file.
223  * ---------------------------------------------------------------------------
224  * Local variables:
225  * c-file-style: "linux"
226  * End:
227  */