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