Use dentry_path() to create full path to inode object
[pohmelfs.git] / arch / um / os-Linux / drivers / tuntap_user.c
bloba2aacffdd907d1830aab84ba1148dd72450caa98
1 /*
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <linux/if_tun.h>
11 #include <net/if.h>
12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
14 #include <sys/wait.h>
15 #include <sys/uio.h>
16 #include "kern_util.h"
17 #include "os.h"
18 #include "tuntap.h"
20 static int tuntap_user_init(void *data, void *dev)
22 struct tuntap_data *pri = data;
24 pri->dev = dev;
25 return 0;
28 static void tuntap_add_addr(unsigned char *addr, unsigned char *netmask,
29 void *data)
31 struct tuntap_data *pri = data;
33 tap_check_ips(pri->gate_addr, addr);
34 if ((pri->fd == -1) || pri->fixed_config)
35 return;
36 open_addr(addr, netmask, pri->dev_name);
39 static void tuntap_del_addr(unsigned char *addr, unsigned char *netmask,
40 void *data)
42 struct tuntap_data *pri = data;
44 if ((pri->fd == -1) || pri->fixed_config)
45 return;
46 close_addr(addr, netmask, pri->dev_name);
49 struct tuntap_pre_exec_data {
50 int stdout;
51 int close_me;
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,
68 NULL };
69 char buf[CMSG_SPACE(sizeof(*fd_out))];
70 struct msghdr msg;
71 struct cmsghdr *cmsg;
72 struct iovec iov;
73 int pid, n, err;
75 sprintf(version_buf, "%d", UML_NET_VERSION);
77 data.stdout = remote;
78 data.close_me = me;
80 pid = run_helper(tuntap_pre_exec, &data, argv);
82 if (pid < 0)
83 return -pid;
85 close(remote);
87 msg.msg_name = NULL;
88 msg.msg_namelen = 0;
89 if (buffer != NULL) {
90 iov = ((struct iovec) { buffer, buffer_len });
91 msg.msg_iov = &iov;
92 msg.msg_iovlen = 1;
94 else {
95 msg.msg_iov = NULL;
96 msg.msg_iovlen = 0;
98 msg.msg_control = buf;
99 msg.msg_controllen = sizeof(buf);
100 msg.msg_flags = 0;
101 n = recvmsg(me, &msg, 0);
102 *used_out = n;
103 if (n < 0) {
104 err = -errno;
105 printk(UM_KERN_ERR "tuntap_open_tramp : recvmsg failed - "
106 "errno = %d\n", errno);
107 return err;
109 helper_wait(pid);
111 cmsg = CMSG_FIRSTHDR(&msg);
112 if (cmsg == NULL) {
113 printk(UM_KERN_ERR "tuntap_open_tramp : didn't receive a "
114 "message\n");
115 return -EINVAL;
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 "
120 "descriptor\n");
121 return -EINVAL;
123 *fd_out = ((int *) CMSG_DATA(cmsg))[0];
124 os_set_exec_close(*fd_out);
125 return 0;
128 static int tuntap_open(void *data)
130 struct ifreq ifr;
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);
136 if (err < 0)
137 return err;
139 if (pri->fixed_config) {
140 pri->fd = os_open_file("/dev/net/tun",
141 of_cloexec(of_rdwr(OPENFLAGS())), 0);
142 if (pri->fd < 0) {
143 printk(UM_KERN_ERR "Failed to open /dev/net/tun, "
144 "err = %d\n", -pri->fd);
145 return 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) {
151 err = -errno;
152 printk(UM_KERN_ERR "TUNSETIFF failed, errno = %d\n",
153 errno);
154 close(pri->fd);
155 return err;
158 else {
159 err = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds);
160 if (err) {
161 err = -errno;
162 printk(UM_KERN_ERR "tuntap_open : socketpair failed - "
163 "errno = %d\n", errno);
164 return err;
167 buffer = get_output_buffer(&len);
168 if (buffer != NULL)
169 len--;
170 used = 0;
172 err = tuntap_open_tramp(pri->gate_addr, &pri->fd, fds[0],
173 fds[1], buffer, len, &used);
175 output = buffer;
176 if (err < 0) {
177 printk("%s", output);
178 free_output_buffer(buffer);
179 printk(UM_KERN_ERR "tuntap_open_tramp failed - "
180 "err = %d\n", -err);
181 return err;
184 pri->dev_name = uml_strdup(buffer);
185 output += IFNAMSIZ;
186 printk("%s", output);
187 free_output_buffer(buffer);
189 close(fds[0]);
190 iter_addresses(pri->dev, open_addr, pri->dev_name);
193 return pri->fd;
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);
202 close(fd);
203 pri->fd = -1;
206 const struct net_user_info tuntap_user_info = {
207 .init = tuntap_user_init,
208 .open = tuntap_open,
209 .close = tuntap_close,
210 .remove = NULL,
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,