Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | #include <stdarg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 10 | #include <stddef.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <sys/socket.h> |
| 13 | #include <sys/wait.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 14 | #include <net_user.h> |
| 15 | #include <os.h> |
| 16 | #include <um_malloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | int tap_open_common(void *dev, char *gate_addr) |
| 19 | { |
| 20 | int tap_addr[4]; |
| 21 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 22 | if (gate_addr == NULL) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 23 | return 0; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 24 | if (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0], |
| 25 | &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4) { |
| 26 | printk(UM_KERN_ERR "Invalid tap IP address - '%s'\n", |
| 27 | gate_addr); |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 28 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 30 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Jeff Dike | da00d9a | 2005-06-08 15:48:01 -0700 | [diff] [blame] | 33 | void tap_check_ips(char *gate_addr, unsigned char *eth_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | int tap_addr[4]; |
| 36 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 37 | if ((gate_addr != NULL) && |
| 38 | (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0], |
| 39 | &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) && |
| 40 | (eth_addr[0] == tap_addr[0]) && |
| 41 | (eth_addr[1] == tap_addr[1]) && |
| 42 | (eth_addr[2] == tap_addr[2]) && |
| 43 | (eth_addr[3] == tap_addr[3])) { |
| 44 | printk(UM_KERN_ERR "The tap IP address and the UML eth IP " |
| 45 | "address must be different\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 49 | /* Do reliable error handling as this fails frequently enough. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | void read_output(int fd, char *output, int len) |
| 51 | { |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 52 | int remain, ret, expected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | char c; |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 54 | char *str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 56 | if (output == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | output = &c; |
| 58 | len = sizeof(c); |
| 59 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | *output = '\0'; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 62 | ret = read(fd, &remain, sizeof(remain)); |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 63 | |
| 64 | if (ret != sizeof(remain)) { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 65 | if (ret < 0) |
| 66 | ret = -errno; |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 67 | expected = sizeof(remain); |
| 68 | str = "length"; |
| 69 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 72 | while (remain != 0) { |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 73 | expected = (remain < len) ? remain : len; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 74 | ret = read(fd, output, expected); |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 75 | if (ret != expected) { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 76 | if (ret < 0) |
| 77 | ret = -errno; |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 78 | str = "data"; |
| 79 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 81 | remain -= ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return; |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 85 | |
| 86 | err: |
| 87 | if (ret < 0) |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 88 | printk(UM_KERN_ERR "read_output - read of %s failed, " |
| 89 | "errno = %d\n", str, -ret); |
Paolo 'Blaisorblade' Giarrusso | f462e8f | 2006-02-24 13:03:57 -0800 | [diff] [blame] | 90 | else |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 91 | printk(UM_KERN_ERR "read_output - read of %s failed, read only " |
| 92 | "%d of %d bytes\n", str, ret, expected); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int net_read(int fd, void *buf, int len) |
| 96 | { |
| 97 | int n; |
| 98 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 99 | n = read(fd, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 101 | if ((n < 0) && (errno == EAGAIN)) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 102 | return 0; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 103 | else if (n == 0) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 104 | return -ENOTCONN; |
| 105 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | int net_recvfrom(int fd, void *buf, int len) |
| 109 | { |
| 110 | int n; |
| 111 | |
Jeff Dike | 9ead6fe | 2006-07-10 04:45:15 -0700 | [diff] [blame] | 112 | CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL)); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 113 | if (n < 0) { |
| 114 | if (errno == EAGAIN) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 115 | return 0; |
| 116 | return -errno; |
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 | else if (n == 0) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 119 | return -ENOTCONN; |
| 120 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | int net_write(int fd, void *buf, int len) |
| 124 | { |
| 125 | int n; |
| 126 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 127 | n = write(fd, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 129 | if ((n < 0) && (errno == EAGAIN)) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 130 | return 0; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 131 | else if (n == 0) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 132 | return -ENOTCONN; |
| 133 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int net_send(int fd, void *buf, int len) |
| 137 | { |
| 138 | int n; |
| 139 | |
Jeff Dike | 9ead6fe | 2006-07-10 04:45:15 -0700 | [diff] [blame] | 140 | CATCH_EINTR(n = send(fd, buf, len, 0)); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 141 | if (n < 0) { |
| 142 | if (errno == EAGAIN) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 143 | return 0; |
| 144 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 146 | else if (n == 0) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 147 | return -ENOTCONN; |
| 148 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int net_sendto(int fd, void *buf, int len, void *to, int sock_len) |
| 152 | { |
| 153 | int n; |
| 154 | |
Jeff Dike | 9ead6fe | 2006-07-10 04:45:15 -0700 | [diff] [blame] | 155 | CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to, |
| 156 | sock_len)); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 157 | if (n < 0) { |
| 158 | if (errno == EAGAIN) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 162 | else if (n == 0) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 163 | return -ENOTCONN; |
| 164 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | struct change_pre_exec_data { |
| 168 | int close_me; |
Hans-Werner Hilse | f9bb3b5 | 2015-06-11 11:29:19 +0200 | [diff] [blame] | 169 | int stdout_fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | static void change_pre_exec(void *arg) |
| 173 | { |
| 174 | struct change_pre_exec_data *data = arg; |
| 175 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 176 | close(data->close_me); |
Hans-Werner Hilse | f9bb3b5 | 2015-06-11 11:29:19 +0200 | [diff] [blame] | 177 | dup2(data->stdout_fd, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static int change_tramp(char **argv, char *output, int output_len) |
| 181 | { |
| 182 | int pid, fds[2], err; |
| 183 | struct change_pre_exec_data pe_data; |
| 184 | |
| 185 | err = os_pipe(fds, 1, 0); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 186 | if (err < 0) { |
| 187 | printk(UM_KERN_ERR "change_tramp - pipe failed, err = %d\n", |
| 188 | -err); |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 189 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | pe_data.close_me = fds[0]; |
Hans-Werner Hilse | f9bb3b5 | 2015-06-11 11:29:19 +0200 | [diff] [blame] | 192 | pe_data.stdout_fd = fds[1]; |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 193 | pid = run_helper(change_pre_exec, &pe_data, argv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Paolo 'Blaisorblade' Giarrusso | b1c332c | 2006-04-10 22:53:37 -0700 | [diff] [blame] | 195 | if (pid > 0) /* Avoid hang as we won't get data in failure case. */ |
| 196 | read_output(fds[0], output, output_len); |
| 197 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 198 | close(fds[0]); |
| 199 | close(fds[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | if (pid > 0) |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 202 | helper_wait(pid); |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 203 | return pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void change(char *dev, char *what, unsigned char *addr, |
| 207 | unsigned char *netmask) |
| 208 | { |
| 209 | char addr_buf[sizeof("255.255.255.255\0")]; |
| 210 | char netmask_buf[sizeof("255.255.255.255\0")]; |
| 211 | char version[sizeof("nnnnn\0")]; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 212 | char *argv[] = { "uml_net", version, what, dev, addr_buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | netmask_buf, NULL }; |
| 214 | char *output; |
| 215 | int output_len, pid; |
| 216 | |
| 217 | sprintf(version, "%d", UML_NET_VERSION); |
| 218 | sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 219 | sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | netmask[2], netmask[3]); |
| 221 | |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 222 | output_len = UM_KERN_PAGE_SIZE; |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 223 | output = uml_kmalloc(output_len, UM_GFP_KERNEL); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 224 | if (output == NULL) |
| 225 | printk(UM_KERN_ERR "change : failed to allocate output " |
| 226 | "buffer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | pid = change_tramp(argv, output, output_len); |
Vitaliy Ivanov | fbee8d9 | 2011-07-25 17:12:49 -0700 | [diff] [blame] | 229 | if (pid < 0) { |
| 230 | kfree(output); |
| 231 | return; |
| 232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 234 | if (output != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | printk("%s", output); |
| 236 | kfree(output); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void open_addr(unsigned char *addr, unsigned char *netmask, void *arg) |
| 241 | { |
| 242 | change(arg, "add", addr, netmask); |
| 243 | } |
| 244 | |
| 245 | void close_addr(unsigned char *addr, unsigned char *netmask, void *arg) |
| 246 | { |
| 247 | change(arg, "del", addr, netmask); |
| 248 | } |
| 249 | |
| 250 | char *split_if_spec(char *str, ...) |
| 251 | { |
Geyslan G. Bem | 887a985 | 2015-12-01 17:18:55 -0300 | [diff] [blame] | 252 | char **arg, *end, *ret = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | va_list ap; |
| 254 | |
| 255 | va_start(ap, str); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 256 | while ((arg = va_arg(ap, char **)) != NULL) { |
| 257 | if (*str == '\0') |
Geyslan G. Bem | 887a985 | 2015-12-01 17:18:55 -0300 | [diff] [blame] | 258 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | end = strchr(str, ','); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 260 | if (end != str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | *arg = str; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 262 | if (end == NULL) |
Geyslan G. Bem | 887a985 | 2015-12-01 17:18:55 -0300 | [diff] [blame] | 263 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | *end++ = '\0'; |
| 265 | str = end; |
| 266 | } |
Geyslan G. Bem | 887a985 | 2015-12-01 17:18:55 -0300 | [diff] [blame] | 267 | ret = str; |
| 268 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | va_end(ap); |
Geyslan G. Bem | 887a985 | 2015-12-01 17:18:55 -0300 | [diff] [blame] | 270 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |