Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 9 | #include <string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/if_tun.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 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_constants.h" |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 17 | #include "kern_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "os.h" |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 19 | #include "tuntap.h" |
| 20 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 22 | static int tuntap_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
| 24 | struct tuntap_data *pri = data; |
| 25 | |
| 26 | pri->dev = dev; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 27 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static void tuntap_add_addr(unsigned char *addr, unsigned char *netmask, |
| 31 | void *data) |
| 32 | { |
| 33 | struct tuntap_data *pri = data; |
| 34 | |
| 35 | tap_check_ips(pri->gate_addr, addr); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 36 | if ((pri->fd == -1) || pri->fixed_config) |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 37 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | open_addr(addr, netmask, pri->dev_name); |
| 39 | } |
| 40 | |
| 41 | static void tuntap_del_addr(unsigned char *addr, unsigned char *netmask, |
| 42 | void *data) |
| 43 | { |
| 44 | struct tuntap_data *pri = data; |
| 45 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 46 | if ((pri->fd == -1) || pri->fixed_config) |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 47 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | close_addr(addr, netmask, pri->dev_name); |
| 49 | } |
| 50 | |
| 51 | struct tuntap_pre_exec_data { |
| 52 | int stdout; |
| 53 | int close_me; |
| 54 | }; |
| 55 | |
| 56 | static void tuntap_pre_exec(void *arg) |
| 57 | { |
| 58 | struct tuntap_pre_exec_data *data = arg; |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | dup2(data->stdout, 1); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 61 | close(data->close_me); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote, |
| 65 | char *buffer, int buffer_len, int *used_out) |
| 66 | { |
| 67 | struct tuntap_pre_exec_data data; |
| 68 | char version_buf[sizeof("nnnnn\0")]; |
| 69 | char *argv[] = { "uml_net", version_buf, "tuntap", "up", gate, |
| 70 | NULL }; |
| 71 | char buf[CMSG_SPACE(sizeof(*fd_out))]; |
| 72 | struct msghdr msg; |
| 73 | struct cmsghdr *cmsg; |
| 74 | struct iovec iov; |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 75 | int pid, n, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | sprintf(version_buf, "%d", UML_NET_VERSION); |
| 78 | |
| 79 | data.stdout = remote; |
| 80 | data.close_me = me; |
| 81 | |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 82 | pid = run_helper(tuntap_pre_exec, &data, argv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 84 | if (pid < 0) |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 85 | return -pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 87 | close(remote); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | msg.msg_name = NULL; |
| 90 | msg.msg_namelen = 0; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 91 | if (buffer != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | iov = ((struct iovec) { buffer, buffer_len }); |
| 93 | msg.msg_iov = &iov; |
| 94 | msg.msg_iovlen = 1; |
| 95 | } |
| 96 | else { |
| 97 | msg.msg_iov = NULL; |
| 98 | msg.msg_iovlen = 0; |
| 99 | } |
| 100 | msg.msg_control = buf; |
| 101 | msg.msg_controllen = sizeof(buf); |
| 102 | msg.msg_flags = 0; |
| 103 | n = recvmsg(me, &msg, 0); |
| 104 | *used_out = n; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 105 | if (n < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 106 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 107 | printk(UM_KERN_ERR "tuntap_open_tramp : recvmsg failed - " |
| 108 | "errno = %d\n", errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 109 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 111 | helper_wait(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | cmsg = CMSG_FIRSTHDR(&msg); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 114 | if (cmsg == NULL) { |
| 115 | printk(UM_KERN_ERR "tuntap_open_tramp : didn't receive a " |
| 116 | "message\n"); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 117 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 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 " |
| 122 | "descriptor\n"); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 123 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | *fd_out = ((int *) CMSG_DATA(cmsg))[0]; |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 126 | os_set_exec_close(*fd_out); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 127 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int tuntap_open(void *data) |
| 131 | { |
| 132 | struct ifreq ifr; |
| 133 | struct tuntap_data *pri = data; |
| 134 | char *output, *buffer; |
| 135 | int err, fds[2], len, used; |
| 136 | |
| 137 | err = tap_open_common(pri->dev, pri->gate_addr); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 138 | if (err < 0) |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 139 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 141 | if (pri->fixed_config) { |
Jeff Dike | 1d2ddcf | 2006-02-07 12:58:41 -0800 | [diff] [blame] | 142 | pri->fd = os_open_file("/dev/net/tun", |
| 143 | of_cloexec(of_rdwr(OPENFLAGS())), 0); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 144 | if (pri->fd < 0) { |
| 145 | printk(UM_KERN_ERR "Failed to open /dev/net/tun, " |
| 146 | "err = %d\n", -pri->fd); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 147 | return pri->fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 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)); |
WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 152 | if (ioctl(pri->fd, TUNSETIFF, &ifr) < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 153 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 154 | printk(UM_KERN_ERR "TUNSETIFF failed, errno = %d\n", |
| 155 | errno); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 156 | close(pri->fd); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 157 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | else { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 161 | err = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 162 | if (err) { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 163 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 164 | printk(UM_KERN_ERR "tuntap_open : socketpair failed - " |
| 165 | "errno = %d\n", errno); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 166 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | buffer = get_output_buffer(&len); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 170 | if (buffer != NULL) |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 171 | len--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | used = 0; |
| 173 | |
| 174 | err = tuntap_open_tramp(pri->gate_addr, &pri->fd, fds[0], |
| 175 | fds[1], buffer, len, &used); |
| 176 | |
| 177 | output = buffer; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 178 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | printk("%s", output); |
| 180 | free_output_buffer(buffer); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 181 | printk(UM_KERN_ERR "tuntap_open_tramp failed - " |
| 182 | "err = %d\n", -err); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 183 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | pri->dev_name = uml_strdup(buffer); |
| 187 | output += IFNAMSIZ; |
| 188 | printk("%s", output); |
| 189 | free_output_buffer(buffer); |
| 190 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 191 | close(fds[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | iter_addresses(pri->dev, open_addr, pri->dev_name); |
| 193 | } |
| 194 | |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 195 | return pri->fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void tuntap_close(int fd, void *data) |
| 199 | { |
| 200 | struct tuntap_data *pri = data; |
| 201 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 202 | if (!pri->fixed_config) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | iter_addresses(pri->dev, close_addr, pri->dev_name); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 204 | close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | pri->fd = -1; |
| 206 | } |
| 207 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 208 | const struct net_user_info tuntap_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | .init = tuntap_user_init, |
| 210 | .open = tuntap_open, |
| 211 | .close = tuntap_close, |
| 212 | .remove = NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | .add_address = tuntap_add_addr, |
| 214 | .delete_address = tuntap_del_addr, |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 215 | .mtu = ETH_MAX_PACKET, |
| 216 | .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | }; |