blob: e9f8445861dc73639bb32742bb5f6fae327a8b4c [file] [log] [blame]
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07001/*
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <stdio.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07007#include <unistd.h>
8#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <errno.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070010#include <stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <string.h>
12#include <sys/socket.h>
13#include <sys/wait.h>
Al Viro37185b32012-10-08 03:27:32 +010014#include <net_user.h>
15#include <os.h>
16#include <um_malloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18int tap_open_common(void *dev, char *gate_addr)
19{
20 int tap_addr[4];
21
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070022 if (gate_addr == NULL)
Jeff Dike108ffa82006-07-10 04:45:14 -070023 return 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070024 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 Dike108ffa82006-07-10 04:45:14 -070028 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 }
Jeff Dike108ffa82006-07-10 04:45:14 -070030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
Jeff Dikeda00d9a2005-06-08 15:48:01 -070033void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 int tap_addr[4];
36
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070037 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 Torvalds1da177e2005-04-16 15:20:36 -070046 }
47}
48
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080049/* Do reliable error handling as this fails frequently enough. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050void read_output(int fd, char *output, int len)
51{
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080052 int remain, ret, expected;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 char c;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080054 char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070056 if (output == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 output = &c;
58 len = sizeof(c);
59 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *output = '\0';
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070062 ret = read(fd, &remain, sizeof(remain));
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080063
64 if (ret != sizeof(remain)) {
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070065 if (ret < 0)
66 ret = -errno;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080067 expected = sizeof(remain);
68 str = "length";
69 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070072 while (remain != 0) {
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080073 expected = (remain < len) ? remain : len;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070074 ret = read(fd, output, expected);
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080075 if (ret != expected) {
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070076 if (ret < 0)
77 ret = -errno;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080078 str = "data";
79 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080081 remain -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080085
86err:
87 if (ret < 0)
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070088 printk(UM_KERN_ERR "read_output - read of %s failed, "
89 "errno = %d\n", str, -ret);
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080090 else
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070091 printk(UM_KERN_ERR "read_output - read of %s failed, read only "
92 "%d of %d bytes\n", str, ret, expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95int net_read(int fd, void *buf, int len)
96{
97 int n;
98
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070099 n = read(fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700101 if ((n < 0) && (errno == EAGAIN))
Jeff Dike108ffa82006-07-10 04:45:14 -0700102 return 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700103 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700104 return -ENOTCONN;
105 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108int net_recvfrom(int fd, void *buf, int len)
109{
110 int n;
111
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700112 CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700113 if (n < 0) {
114 if (errno == EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700115 return 0;
116 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700118 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700119 return -ENOTCONN;
120 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123int net_write(int fd, void *buf, int len)
124{
125 int n;
126
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700127 n = write(fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700129 if ((n < 0) && (errno == EAGAIN))
Jeff Dike108ffa82006-07-10 04:45:14 -0700130 return 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700131 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700132 return -ENOTCONN;
133 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136int net_send(int fd, void *buf, int len)
137{
138 int n;
139
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700140 CATCH_EINTR(n = send(fd, buf, len, 0));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700141 if (n < 0) {
142 if (errno == EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700143 return 0;
144 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700146 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700147 return -ENOTCONN;
148 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
152{
153 int n;
154
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700155 CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
156 sock_len));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700157 if (n < 0) {
158 if (errno == EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700159 return 0;
160 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700162 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700163 return -ENOTCONN;
164 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167struct change_pre_exec_data {
168 int close_me;
Hans-Werner Hilsef9bb3b52015-06-11 11:29:19 +0200169 int stdout_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170};
171
172static void change_pre_exec(void *arg)
173{
174 struct change_pre_exec_data *data = arg;
175
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700176 close(data->close_me);
Hans-Werner Hilsef9bb3b52015-06-11 11:29:19 +0200177 dup2(data->stdout_fd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static 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 Dikecd1ae0e2007-10-16 01:27:29 -0700186 if (err < 0) {
187 printk(UM_KERN_ERR "change_tramp - pipe failed, err = %d\n",
188 -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191 pe_data.close_me = fds[0];
Hans-Werner Hilsef9bb3b52015-06-11 11:29:19 +0200192 pe_data.stdout_fd = fds[1];
Jeff Dikec4399012007-07-15 23:38:56 -0700193 pid = run_helper(change_pre_exec, &pe_data, argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Paolo 'Blaisorblade' Giarrussob1c332c2006-04-10 22:53:37 -0700195 if (pid > 0) /* Avoid hang as we won't get data in failure case. */
196 read_output(fds[0], output, output_len);
197
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700198 close(fds[0]);
199 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (pid > 0)
Jeff Dike1aa351a2008-02-04 22:31:10 -0800202 helper_wait(pid);
Jeff Dike108ffa82006-07-10 04:45:14 -0700203 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206static 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 Dikecd1ae0e2007-10-16 01:27:29 -0700212 char *argv[] = { "uml_net", version, what, dev, addr_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 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 Dikecd1ae0e2007-10-16 01:27:29 -0700219 sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 netmask[2], netmask[3]);
221
Jeff Dike1ffb9162007-05-06 14:51:22 -0700222 output_len = UM_KERN_PAGE_SIZE;
Jeff Dike43f5b302008-05-12 14:01:52 -0700223 output = uml_kmalloc(output_len, UM_GFP_KERNEL);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700224 if (output == NULL)
225 printk(UM_KERN_ERR "change : failed to allocate output "
226 "buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 pid = change_tramp(argv, output, output_len);
Vitaliy Ivanovfbee8d92011-07-25 17:12:49 -0700229 if (pid < 0) {
230 kfree(output);
231 return;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700234 if (output != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 printk("%s", output);
236 kfree(output);
237 }
238}
239
240void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
241{
242 change(arg, "add", addr, netmask);
243}
244
245void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
246{
247 change(arg, "del", addr, netmask);
248}
249
250char *split_if_spec(char *str, ...)
251{
Geyslan G. Bem887a9852015-12-01 17:18:55 -0300252 char **arg, *end, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 va_list ap;
254
255 va_start(ap, str);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700256 while ((arg = va_arg(ap, char **)) != NULL) {
257 if (*str == '\0')
Geyslan G. Bem887a9852015-12-01 17:18:55 -0300258 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 end = strchr(str, ',');
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700260 if (end != str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *arg = str;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700262 if (end == NULL)
Geyslan G. Bem887a9852015-12-01 17:18:55 -0300263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *end++ = '\0';
265 str = end;
266 }
Geyslan G. Bem887a9852015-12-01 17:18:55 -0300267 ret = str;
268out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 va_end(ap);
Geyslan G. Bem887a9852015-12-01 17:18:55 -0300270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}