blob: abf2653f5517cbf6f65b851dd10455e7bc8e27e2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "net_user.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070015#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070017#include "um_malloc.h"
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070018#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20int tap_open_common(void *dev, char *gate_addr)
21{
22 int tap_addr[4];
23
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070024 if (gate_addr == NULL)
Jeff Dike108ffa82006-07-10 04:45:14 -070025 return 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070026 if (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
27 &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4) {
28 printk(UM_KERN_ERR "Invalid tap IP address - '%s'\n",
29 gate_addr);
Jeff Dike108ffa82006-07-10 04:45:14 -070030 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 }
Jeff Dike108ffa82006-07-10 04:45:14 -070032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Jeff Dikeda00d9a2005-06-08 15:48:01 -070035void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 int tap_addr[4];
38
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070039 if ((gate_addr != NULL) &&
40 (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
41 &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) &&
42 (eth_addr[0] == tap_addr[0]) &&
43 (eth_addr[1] == tap_addr[1]) &&
44 (eth_addr[2] == tap_addr[2]) &&
45 (eth_addr[3] == tap_addr[3])) {
46 printk(UM_KERN_ERR "The tap IP address and the UML eth IP "
47 "address must be different\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 }
49}
50
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080051/* Do reliable error handling as this fails frequently enough. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052void read_output(int fd, char *output, int len)
53{
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080054 int remain, ret, expected;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 char c;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080056 char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070058 if (output == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 output = &c;
60 len = sizeof(c);
61 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *output = '\0';
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070064 ret = read(fd, &remain, sizeof(remain));
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080065
66 if (ret != sizeof(remain)) {
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070067 if (ret < 0)
68 ret = -errno;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080069 expected = sizeof(remain);
70 str = "length";
71 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070074 while (remain != 0) {
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080075 expected = (remain < len) ? remain : len;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070076 ret = read(fd, output, expected);
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080077 if (ret != expected) {
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070078 if (ret < 0)
79 ret = -errno;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080080 str = "data";
81 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080083 remain -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080087
88err:
89 if (ret < 0)
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070090 printk(UM_KERN_ERR "read_output - read of %s failed, "
91 "errno = %d\n", str, -ret);
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080092 else
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070093 printk(UM_KERN_ERR "read_output - read of %s failed, read only "
94 "%d of %d bytes\n", str, ret, expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97int net_read(int fd, void *buf, int len)
98{
99 int n;
100
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700101 n = read(fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700103 if ((n < 0) && (errno == EAGAIN))
Jeff Dike108ffa82006-07-10 04:45:14 -0700104 return 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700105 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700106 return -ENOTCONN;
107 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110int net_recvfrom(int fd, void *buf, int len)
111{
112 int n;
113
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700114 CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700115 if (n < 0) {
116 if (errno == EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700117 return 0;
118 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700120 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700121 return -ENOTCONN;
122 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125int net_write(int fd, void *buf, int len)
126{
127 int n;
128
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700129 n = write(fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700131 if ((n < 0) && (errno == EAGAIN))
Jeff Dike108ffa82006-07-10 04:45:14 -0700132 return 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700133 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700134 return -ENOTCONN;
135 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138int net_send(int fd, void *buf, int len)
139{
140 int n;
141
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700142 CATCH_EINTR(n = send(fd, buf, len, 0));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700143 if (n < 0) {
144 if (errno == EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700145 return 0;
146 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700148 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700149 return -ENOTCONN;
150 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
154{
155 int n;
156
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700157 CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
158 sock_len));
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700159 if (n < 0) {
160 if (errno == EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700161 return 0;
162 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700164 else if (n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700165 return -ENOTCONN;
166 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169struct change_pre_exec_data {
170 int close_me;
171 int stdout;
172};
173
174static void change_pre_exec(void *arg)
175{
176 struct change_pre_exec_data *data = arg;
177
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700178 close(data->close_me);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dup2(data->stdout, 1);
180}
181
182static int change_tramp(char **argv, char *output, int output_len)
183{
184 int pid, fds[2], err;
185 struct change_pre_exec_data pe_data;
186
187 err = os_pipe(fds, 1, 0);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700188 if (err < 0) {
189 printk(UM_KERN_ERR "change_tramp - pipe failed, err = %d\n",
190 -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700191 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193 pe_data.close_me = fds[0];
194 pe_data.stdout = fds[1];
Jeff Dikec4399012007-07-15 23:38:56 -0700195 pid = run_helper(change_pre_exec, &pe_data, argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Paolo 'Blaisorblade' Giarrussob1c332c2006-04-10 22:53:37 -0700197 if (pid > 0) /* Avoid hang as we won't get data in failure case. */
198 read_output(fds[0], output, output_len);
199
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700200 close(fds[0]);
201 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (pid > 0)
Jeff Dike1aa351a2008-02-04 22:31:10 -0800204 helper_wait(pid);
Jeff Dike108ffa82006-07-10 04:45:14 -0700205 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static void change(char *dev, char *what, unsigned char *addr,
209 unsigned char *netmask)
210{
211 char addr_buf[sizeof("255.255.255.255\0")];
212 char netmask_buf[sizeof("255.255.255.255\0")];
213 char version[sizeof("nnnnn\0")];
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700214 char *argv[] = { "uml_net", version, what, dev, addr_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 netmask_buf, NULL };
216 char *output;
217 int output_len, pid;
218
219 sprintf(version, "%d", UML_NET_VERSION);
220 sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700221 sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 netmask[2], netmask[3]);
223
Jeff Dike1ffb9162007-05-06 14:51:22 -0700224 output_len = UM_KERN_PAGE_SIZE;
Jeff Dikee4c4bf92007-07-15 23:38:56 -0700225 output = kmalloc(output_len, UM_GFP_KERNEL);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700226 if (output == NULL)
227 printk(UM_KERN_ERR "change : failed to allocate output "
228 "buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 pid = change_tramp(argv, output, output_len);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700231 if (pid < 0) return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700233 if (output != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 printk("%s", output);
235 kfree(output);
236 }
237}
238
239void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
240{
241 change(arg, "add", addr, netmask);
242}
243
244void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
245{
246 change(arg, "del", addr, netmask);
247}
248
249char *split_if_spec(char *str, ...)
250{
251 char **arg, *end;
252 va_list ap;
253
254 va_start(ap, str);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700255 while ((arg = va_arg(ap, char **)) != NULL) {
256 if (*str == '\0')
Jeff Dike108ffa82006-07-10 04:45:14 -0700257 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 end = strchr(str, ',');
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700259 if (end != str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *arg = str;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700261 if (end == NULL)
Jeff Dike108ffa82006-07-10 04:45:14 -0700262 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *end++ = '\0';
264 str = end;
265 }
266 va_end(ap);
Jeff Dike108ffa82006-07-10 04:45:14 -0700267 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}