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) |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 3 | * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * James Leu (jleu@mindspring.net). |
| 5 | * Copyright (C) 2001 by various other people who didn't put their name here. |
| 6 | * Licensed under the GPL. |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <unistd.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <sys/socket.h> |
| 14 | #include <sys/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "etap.h" |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 16 | #include "kern_constants.h" |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 17 | #include "os.h" |
| 18 | #include "net_user.h" |
| 19 | #include "um_malloc.h" |
| 20 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #define MAX_PACKET ETH_MAX_PACKET |
| 23 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 24 | static int etap_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
| 26 | struct ethertap_data *pri = data; |
| 27 | |
| 28 | pri->dev = dev; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 29 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | struct addr_change { |
| 33 | enum { ADD_ADDR, DEL_ADDR } what; |
| 34 | unsigned char addr[4]; |
| 35 | unsigned char netmask[4]; |
| 36 | }; |
| 37 | |
| 38 | static void etap_change(int op, unsigned char *addr, unsigned char *netmask, |
| 39 | int fd) |
| 40 | { |
| 41 | struct addr_change change; |
Paolo 'Blaisorblade' Giarrusso | 802e307 | 2006-04-10 22:53:32 -0700 | [diff] [blame] | 42 | char *output; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | int n; |
| 44 | |
| 45 | change.what = op; |
| 46 | memcpy(change.addr, addr, sizeof(change.addr)); |
| 47 | memcpy(change.netmask, netmask, sizeof(change.netmask)); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 48 | CATCH_EINTR(n = write(fd, &change, sizeof(change))); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 49 | if (n != sizeof(change)) { |
| 50 | printk(UM_KERN_ERR "etap_change - request failed, err = %d\n", |
| 51 | errno); |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
Jeff Dike | e4c4bf99 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 55 | output = kmalloc(UM_KERN_PAGE_SIZE, UM_GFP_KERNEL); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 56 | if (output == NULL) |
| 57 | printk(UM_KERN_ERR "etap_change : Failed to allocate output " |
| 58 | "buffer\n"); |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 59 | read_output(fd, output, UM_KERN_PAGE_SIZE); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 60 | if (output != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | printk("%s", output); |
| 62 | kfree(output); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void etap_open_addr(unsigned char *addr, unsigned char *netmask, |
| 67 | void *arg) |
| 68 | { |
| 69 | etap_change(ADD_ADDR, addr, netmask, *((int *) arg)); |
| 70 | } |
| 71 | |
| 72 | static void etap_close_addr(unsigned char *addr, unsigned char *netmask, |
| 73 | void *arg) |
| 74 | { |
| 75 | etap_change(DEL_ADDR, addr, netmask, *((int *) arg)); |
| 76 | } |
| 77 | |
| 78 | struct etap_pre_exec_data { |
| 79 | int control_remote; |
| 80 | int control_me; |
| 81 | int data_me; |
| 82 | }; |
| 83 | |
| 84 | static void etap_pre_exec(void *arg) |
| 85 | { |
| 86 | struct etap_pre_exec_data *data = arg; |
| 87 | |
| 88 | dup2(data->control_remote, 1); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 89 | close(data->data_me); |
| 90 | close(data->control_me); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 93 | static int etap_tramp(char *dev, char *gate, int control_me, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | int control_remote, int data_me, int data_remote) |
| 95 | { |
| 96 | struct etap_pre_exec_data pe_data; |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 97 | int pid, err, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | char version_buf[sizeof("nnnnn\0")]; |
| 99 | char data_fd_buf[sizeof("nnnnnn\0")]; |
| 100 | char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")]; |
| 101 | char *setup_args[] = { "uml_net", version_buf, "ethertap", dev, |
| 102 | data_fd_buf, gate_buf, NULL }; |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 103 | char *nosetup_args[] = { "uml_net", version_buf, "ethertap", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | dev, data_fd_buf, NULL }; |
| 105 | char **args, c; |
| 106 | |
| 107 | sprintf(data_fd_buf, "%d", data_remote); |
| 108 | sprintf(version_buf, "%d", UML_NET_VERSION); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 109 | if (gate != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | strcpy(gate_buf, gate); |
| 111 | args = setup_args; |
| 112 | } |
| 113 | else args = nosetup_args; |
| 114 | |
| 115 | err = 0; |
| 116 | pe_data.control_remote = control_remote; |
| 117 | pe_data.control_me = control_me; |
| 118 | pe_data.data_me = data_me; |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 119 | pid = run_helper(etap_pre_exec, &pe_data, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 121 | if (pid < 0) |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 122 | err = pid; |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 123 | close(data_remote); |
| 124 | close(control_remote); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 125 | CATCH_EINTR(n = read(control_me, &c, sizeof(c))); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 126 | if (n != sizeof(c)) { |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 127 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 128 | printk(UM_KERN_ERR "etap_tramp : read of status failed, " |
| 129 | "err = %d\n", -err); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 130 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 132 | if (c != 1) { |
| 133 | printk(UM_KERN_ERR "etap_tramp : uml_net failed\n"); |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 134 | err = helper_wait(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 136 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static int etap_open(void *data) |
| 140 | { |
| 141 | struct ethertap_data *pri = data; |
| 142 | char *output; |
| 143 | int data_fds[2], control_fds[2], err, output_len; |
| 144 | |
| 145 | err = tap_open_common(pri->dev, pri->gate_addr); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 146 | if (err) |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 147 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 149 | err = socketpair(AF_UNIX, SOCK_DGRAM, 0, data_fds); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 150 | if (err) { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 151 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 152 | printk(UM_KERN_ERR "etap_open - data socketpair failed - " |
| 153 | "err = %d\n", errno); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 154 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 157 | err = socketpair(AF_UNIX, SOCK_STREAM, 0, control_fds); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 158 | if (err) { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 159 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 160 | printk(UM_KERN_ERR "etap_open - control socketpair failed - " |
| 161 | "err = %d\n", errno); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 162 | goto out_close_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 164 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 165 | err = etap_tramp(pri->dev_name, pri->gate_addr, control_fds[0], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | control_fds[1], data_fds[0], data_fds[1]); |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 167 | output_len = UM_KERN_PAGE_SIZE; |
Jeff Dike | e4c4bf99 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 168 | output = kmalloc(output_len, UM_GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | read_output(control_fds[0], output, output_len); |
| 170 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 171 | if (output == NULL) |
| 172 | printk(UM_KERN_ERR "etap_open : failed to allocate output " |
| 173 | "buffer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | else { |
| 175 | printk("%s", output); |
| 176 | kfree(output); |
| 177 | } |
| 178 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 179 | if (err < 0) { |
| 180 | printk(UM_KERN_ERR "etap_tramp failed - err = %d\n", -err); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 181 | goto out_close_control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | pri->data_fd = data_fds[0]; |
| 185 | pri->control_fd = control_fds[0]; |
| 186 | iter_addresses(pri->dev, etap_open_addr, &pri->control_fd); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 187 | return data_fds[0]; |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 188 | |
| 189 | out_close_control: |
| 190 | close(control_fds[0]); |
| 191 | close(control_fds[1]); |
| 192 | out_close_data: |
| 193 | close(data_fds[0]); |
| 194 | close(data_fds[1]); |
| 195 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void etap_close(int fd, void *data) |
| 199 | { |
| 200 | struct ethertap_data *pri = data; |
| 201 | |
| 202 | iter_addresses(pri->dev, etap_close_addr, &pri->control_fd); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 203 | close(fd); |
| 204 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 205 | if (shutdown(pri->data_fd, SHUT_RDWR) < 0) |
| 206 | printk(UM_KERN_ERR "etap_close - shutdown data socket failed, " |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 207 | "errno = %d\n", errno); |
| 208 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 209 | if (shutdown(pri->control_fd, SHUT_RDWR) < 0) |
| 210 | printk(UM_KERN_ERR "etap_close - shutdown control socket " |
| 211 | "failed, errno = %d\n", errno); |
| 212 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 213 | close(pri->data_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | pri->data_fd = -1; |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 215 | close(pri->control_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | pri->control_fd = -1; |
| 217 | } |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | static void etap_add_addr(unsigned char *addr, unsigned char *netmask, |
| 220 | void *data) |
| 221 | { |
| 222 | struct ethertap_data *pri = data; |
| 223 | |
| 224 | tap_check_ips(pri->gate_addr, addr); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 225 | if (pri->control_fd == -1) |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 226 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | etap_open_addr(addr, netmask, &pri->control_fd); |
| 228 | } |
| 229 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 230 | static void etap_del_addr(unsigned char *addr, unsigned char *netmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | void *data) |
| 232 | { |
| 233 | struct ethertap_data *pri = data; |
| 234 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 235 | if (pri->control_fd == -1) |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 236 | return; |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | etap_close_addr(addr, netmask, &pri->control_fd); |
| 239 | } |
| 240 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 241 | const struct net_user_info ethertap_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | .init = etap_user_init, |
| 243 | .open = etap_open, |
| 244 | .close = etap_close, |
| 245 | .remove = NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | .add_address = etap_add_addr, |
| 247 | .delete_address = etap_del_addr, |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 248 | .mtu = ETH_MAX_PACKET, |
| 249 | .max_packet = ETH_MAX_PACKET + ETH_HEADER_ETHERTAP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | }; |