blob: da946e3e1bf26322a3d7078c53868ca005378ec2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stddef.h>
7#include <stdarg.h>
8#include <unistd.h>
9#include <stdio.h>
10#include <errno.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/socket.h>
14#include <sys/wait.h>
Jeff Dikef3e7ed22006-09-29 01:58:46 -070015#include <sys/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "kern_util.h"
18#include "net_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070020#include "um_malloc.h"
Jeff Dike1ffb9162007-05-06 14:51:22 -070021#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23int tap_open_common(void *dev, char *gate_addr)
24{
25 int tap_addr[4];
26
Jeff Dike108ffa82006-07-10 04:45:14 -070027 if(gate_addr == NULL)
28 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 if(sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
30 &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4){
31 printk("Invalid tap IP address - '%s'\n", gate_addr);
Jeff Dike108ffa82006-07-10 04:45:14 -070032 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 }
Jeff Dike108ffa82006-07-10 04:45:14 -070034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Jeff Dikeda00d9a2005-06-08 15:48:01 -070037void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 int tap_addr[4];
40
41 if((gate_addr != NULL) &&
42 (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
43 &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) &&
44 (eth_addr[0] == tap_addr[0]) &&
45 (eth_addr[1] == tap_addr[1]) &&
46 (eth_addr[2] == tap_addr[2]) &&
47 (eth_addr[3] == tap_addr[3])){
48 printk("The tap IP address and the UML eth IP address"
49 " must be different\n");
50 }
51}
52
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080053/* Do reliable error handling as this fails frequently enough. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054void read_output(int fd, char *output, int len)
55{
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080056 int remain, ret, expected;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 char c;
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080058 char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if(output == NULL){
61 output = &c;
62 len = sizeof(c);
63 }
64
65 *output = '\0';
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070066 ret = os_read_file(fd, &remain, sizeof(remain));
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080067
68 if (ret != sizeof(remain)) {
69 expected = sizeof(remain);
70 str = "length";
71 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73
74 while(remain != 0){
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080075 expected = (remain < len) ? remain : len;
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070076 ret = os_read_file(fd, output, expected);
Paolo 'Blaisorblade' Giarrussof462e8f2006-02-24 13:03:57 -080077 if (ret != expected) {
78 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)
88 printk("read_output - read of %s failed, errno = %d\n", str, -ret);
89 else
90 printk("read_output - read of %s failed, read only %d of %d bytes\n", str, ret, expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93int net_read(int fd, void *buf, int len)
94{
95 int n;
96
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070097 n = os_read_file(fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 if(n == -EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 else if(n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700102 return -ENOTCONN;
103 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106int net_recvfrom(int fd, void *buf, int len)
107{
108 int n;
109
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700110 CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if(n < 0){
Jeff Dike108ffa82006-07-10 04:45:14 -0700112 if(errno == EAGAIN)
113 return 0;
114 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700116 else if(n == 0)
117 return -ENOTCONN;
118 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121int net_write(int fd, void *buf, int len)
122{
123 int n;
124
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700125 n = os_write_file(fd, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if(n == -EAGAIN)
Jeff Dike108ffa82006-07-10 04:45:14 -0700128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 else if(n == 0)
Jeff Dike108ffa82006-07-10 04:45:14 -0700130 return -ENOTCONN;
131 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134int net_send(int fd, void *buf, int len)
135{
136 int n;
137
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700138 CATCH_EINTR(n = send(fd, buf, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if(n < 0){
Jeff Dike108ffa82006-07-10 04:45:14 -0700140 if(errno == EAGAIN)
141 return 0;
142 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700144 else if(n == 0)
145 return -ENOTCONN;
146 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
150{
151 int n;
152
Jeff Dike9ead6fe2006-07-10 04:45:15 -0700153 CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
154 sock_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if(n < 0){
Jeff Dike108ffa82006-07-10 04:45:14 -0700156 if(errno == EAGAIN)
157 return 0;
158 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Jeff Dike108ffa82006-07-10 04:45:14 -0700160 else if(n == 0)
161 return -ENOTCONN;
162 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165struct change_pre_exec_data {
166 int close_me;
167 int stdout;
168};
169
170static void change_pre_exec(void *arg)
171{
172 struct change_pre_exec_data *data = arg;
173
174 os_close_file(data->close_me);
175 dup2(data->stdout, 1);
176}
177
178static int change_tramp(char **argv, char *output, int output_len)
179{
180 int pid, fds[2], err;
181 struct change_pre_exec_data pe_data;
182
183 err = os_pipe(fds, 1, 0);
184 if(err < 0){
185 printk("change_tramp - pipe failed, err = %d\n", -err);
Jeff Dike108ffa82006-07-10 04:45:14 -0700186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 pe_data.close_me = fds[0];
189 pe_data.stdout = fds[1];
Jeff Dikec4399012007-07-15 23:38:56 -0700190 pid = run_helper(change_pre_exec, &pe_data, argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Paolo 'Blaisorblade' Giarrussob1c332c2006-04-10 22:53:37 -0700192 if (pid > 0) /* Avoid hang as we won't get data in failure case. */
193 read_output(fds[0], output, output_len);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 os_close_file(fds[0]);
196 os_close_file(fds[1]);
197
198 if (pid > 0)
199 CATCH_EINTR(err = waitpid(pid, NULL, 0));
Jeff Dike108ffa82006-07-10 04:45:14 -0700200 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203static void change(char *dev, char *what, unsigned char *addr,
204 unsigned char *netmask)
205{
206 char addr_buf[sizeof("255.255.255.255\0")];
207 char netmask_buf[sizeof("255.255.255.255\0")];
208 char version[sizeof("nnnnn\0")];
209 char *argv[] = { "uml_net", version, what, dev, addr_buf,
210 netmask_buf, NULL };
211 char *output;
212 int output_len, pid;
213
214 sprintf(version, "%d", UML_NET_VERSION);
215 sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
216 sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
217 netmask[2], netmask[3]);
218
Jeff Dike1ffb9162007-05-06 14:51:22 -0700219 output_len = UM_KERN_PAGE_SIZE;
Jeff Dikee4c4bf992007-07-15 23:38:56 -0700220 output = kmalloc(output_len, UM_GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if(output == NULL)
222 printk("change : failed to allocate output buffer\n");
223
224 pid = change_tramp(argv, output, output_len);
225 if(pid < 0) return;
226
227 if(output != NULL){
228 printk("%s", output);
229 kfree(output);
230 }
231}
232
233void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
234{
235 change(arg, "add", addr, netmask);
236}
237
238void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
239{
240 change(arg, "del", addr, netmask);
241}
242
243char *split_if_spec(char *str, ...)
244{
245 char **arg, *end;
246 va_list ap;
247
248 va_start(ap, str);
249 while((arg = va_arg(ap, char **)) != NULL){
250 if(*str == '\0')
Jeff Dike108ffa82006-07-10 04:45:14 -0700251 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 end = strchr(str, ',');
253 if(end != str)
254 *arg = str;
255 if(end == NULL)
Jeff Dike108ffa82006-07-10 04:45:14 -0700256 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *end++ = '\0';
258 str = end;
259 }
260 va_end(ap);
Jeff Dike108ffa82006-07-10 04:45:14 -0700261 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}