blob: 89fbec185cc16ec10d2d8df49410cf8bb32080a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <stddef.h>
5#include <sched.h>
6#include <string.h>
7#include <errno.h>
8#include <sys/termios.h>
9#include <sys/wait.h>
10#include <sys/signal.h>
11#include "user_util.h"
12#include "kern_util.h"
13#include "user.h"
14#include "net_user.h"
15#include "slip.h"
Jeff Dikea3c77c62005-06-13 15:52:18 -070016#include "slip_common.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "os.h"
18
19void slip_user_init(void *data, void *dev)
20{
21 struct slip_data *pri = data;
22
23 pri->dev = dev;
24}
25
26static int set_up_tty(int fd)
27{
28 int i;
29 struct termios tios;
30
31 if (tcgetattr(fd, &tios) < 0) {
32 printk("could not get initial terminal attributes\n");
33 return(-1);
34 }
35
36 tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL;
37 tios.c_iflag = IGNBRK | IGNPAR;
38 tios.c_oflag = 0;
39 tios.c_lflag = 0;
40 for (i = 0; i < NCCS; i++)
41 tios.c_cc[i] = 0;
42 tios.c_cc[VMIN] = 1;
43 tios.c_cc[VTIME] = 0;
44
45 cfsetospeed(&tios, B38400);
46 cfsetispeed(&tios, B38400);
47
48 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
49 printk("failed to set terminal attributes\n");
50 return(-1);
51 }
52 return(0);
53}
54
55struct slip_pre_exec_data {
56 int stdin;
57 int stdout;
58 int close_me;
59};
60
61static void slip_pre_exec(void *arg)
62{
63 struct slip_pre_exec_data *data = arg;
64
65 if(data->stdin >= 0) dup2(data->stdin, 0);
66 dup2(data->stdout, 1);
67 if(data->close_me >= 0) os_close_file(data->close_me);
68}
69
70static int slip_tramp(char **argv, int fd)
71{
72 struct slip_pre_exec_data pe_data;
73 char *output;
74 int status, pid, fds[2], err, output_len;
75
76 err = os_pipe(fds, 1, 0);
77 if(err < 0){
78 printk("slip_tramp : pipe failed, err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -070079 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
82 err = 0;
83 pe_data.stdin = fd;
84 pe_data.stdout = fds[1];
85 pe_data.close_me = fds[0];
Jeff Dikea3c77c62005-06-13 15:52:18 -070086 err = run_helper(slip_pre_exec, &pe_data, argv, NULL);
87 if(err < 0)
88 goto out_close;
89 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Jeff Dikea3c77c62005-06-13 15:52:18 -070091 output_len = page_size();
92 output = um_kmalloc(output_len);
93 if(output == NULL){
94 printk("slip_tramp : failed to allocate output buffer\n");
95 os_kill_process(pid, 1);
96 err = -ENOMEM;
97 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
Jeff Dikea3c77c62005-06-13 15:52:18 -0700100 os_close_file(fds[1]);
101 read_output(fds[0], output, output_len);
102 printk("%s", output);
103
104 CATCH_EINTR(err = waitpid(pid, &status, 0));
105 if(err < 0)
106 err = errno;
107 else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){
108 printk("'%s' didn't exit with status 0\n", argv[0]);
109 err = -EINVAL;
110 }
111 else err = 0;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 os_close_file(fds[0]);
114
Jeff Dikea3c77c62005-06-13 15:52:18 -0700115out_free:
116 kfree(output);
117 return err;
118
119out_close:
120 os_close_file(fds[0]);
121 os_close_file(fds[1]);
122out:
123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static int slip_open(void *data)
127{
128 struct slip_data *pri = data;
129 char version_buf[sizeof("nnnnn\0")];
130 char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")];
131 char *argv[] = { "uml_net", version_buf, "slip", "up", gate_buf,
132 NULL };
133 int sfd, mfd, err;
134
Jeff Dikea3c77c62005-06-13 15:52:18 -0700135 err = get_pty();
136 if(err < 0){
137 printk("slip-open : Failed to open pty, err = %d\n", -err);
138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Jeff Dikea3c77c62005-06-13 15:52:18 -0700140 mfd = err;
141
142 err = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0);
143 if(err < 0){
144 printk("Couldn't open tty for slip line, err = %d\n", -err);
145 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Jeff Dikea3c77c62005-06-13 15:52:18 -0700147 sfd = err;
148
149 if(set_up_tty(sfd))
150 goto out_close2;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 pri->slave = sfd;
Jeff Dikea3c77c62005-06-13 15:52:18 -0700153 pri->slip.pos = 0;
154 pri->slip.esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if(pri->gate_addr != NULL){
156 sprintf(version_buf, "%d", UML_NET_VERSION);
157 strcpy(gate_buf, pri->gate_addr);
158
159 err = slip_tramp(argv, sfd);
160
161 if(err < 0){
162 printk("slip_tramp failed - err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700163 goto out_close2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 err = os_get_ifname(pri->slave, pri->name);
166 if(err < 0){
167 printk("get_ifname failed, err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700168 goto out_close2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 iter_addresses(pri->dev, open_addr, pri->name);
171 }
172 else {
173 err = os_set_slip(sfd);
174 if(err < 0){
175 printk("Failed to set slip discipline encapsulation - "
176 "err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700177 goto out_close2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 }
180 return(mfd);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700181out_close2:
182 os_close_file(sfd);
183out_close:
184 os_close_file(mfd);
185out:
186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189static void slip_close(int fd, void *data)
190{
191 struct slip_data *pri = data;
192 char version_buf[sizeof("nnnnn\0")];
193 char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name,
194 NULL };
195 int err;
196
197 if(pri->gate_addr != NULL)
198 iter_addresses(pri->dev, close_addr, pri->name);
199
200 sprintf(version_buf, "%d", UML_NET_VERSION);
201
202 err = slip_tramp(argv, pri->slave);
203
204 if(err != 0)
205 printk("slip_tramp failed - errno = %d\n", -err);
206 os_close_file(fd);
207 os_close_file(pri->slave);
208 pri->slave = -1;
209}
210
211int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
212{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700213 return slip_proto_read(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
217{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700218 return slip_proto_write(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static int slip_set_mtu(int mtu, void *data)
222{
223 return(mtu);
224}
225
226static void slip_add_addr(unsigned char *addr, unsigned char *netmask,
227 void *data)
228{
229 struct slip_data *pri = data;
230
231 if(pri->slave < 0) return;
232 open_addr(addr, netmask, pri->name);
233}
234
235static void slip_del_addr(unsigned char *addr, unsigned char *netmask,
236 void *data)
237{
238 struct slip_data *pri = data;
239
240 if(pri->slave < 0) return;
241 close_addr(addr, netmask, pri->name);
242}
243
244struct net_user_info slip_user_info = {
245 .init = slip_user_init,
246 .open = slip_open,
247 .close = slip_close,
248 .remove = NULL,
249 .set_mtu = slip_set_mtu,
250 .add_address = slip_add_addr,
251 .delete_address = slip_del_addr,
252 .max_packet = BUF_SIZE
253};