blob: edc2de3ee2f454b355dcab8883951a66dc45d0ae [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "kern_util.h"
12#include "user.h"
13#include "net_user.h"
14#include "slip.h"
Jeff Dikea3c77c62005-06-13 15:52:18 -070015#include "slip_common.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 Dike1ffb9162007-05-06 14:51:22 -070018#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Jeff Dikef34d9d22007-05-06 14:51:04 -070020static int slip_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 struct slip_data *pri = data;
23
24 pri->dev = dev;
Jeff Dikef34d9d22007-05-06 14:51:04 -070025 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026}
27
28static int set_up_tty(int fd)
29{
30 int i;
31 struct termios tios;
32
33 if (tcgetattr(fd, &tios) < 0) {
34 printk("could not get initial terminal attributes\n");
35 return(-1);
36 }
37
38 tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL;
39 tios.c_iflag = IGNBRK | IGNPAR;
40 tios.c_oflag = 0;
41 tios.c_lflag = 0;
42 for (i = 0; i < NCCS; i++)
43 tios.c_cc[i] = 0;
44 tios.c_cc[VMIN] = 1;
45 tios.c_cc[VTIME] = 0;
46
47 cfsetospeed(&tios, B38400);
48 cfsetispeed(&tios, B38400);
49
50 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
51 printk("failed to set terminal attributes\n");
52 return(-1);
53 }
54 return(0);
55}
56
57struct slip_pre_exec_data {
58 int stdin;
59 int stdout;
60 int close_me;
61};
62
63static void slip_pre_exec(void *arg)
64{
65 struct slip_pre_exec_data *data = arg;
66
67 if(data->stdin >= 0) dup2(data->stdin, 0);
68 dup2(data->stdout, 1);
69 if(data->close_me >= 0) os_close_file(data->close_me);
70}
71
72static int slip_tramp(char **argv, int fd)
73{
74 struct slip_pre_exec_data pe_data;
75 char *output;
76 int status, pid, fds[2], err, output_len;
77
78 err = os_pipe(fds, 1, 0);
79 if(err < 0){
80 printk("slip_tramp : pipe failed, err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -070081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
84 err = 0;
85 pe_data.stdin = fd;
86 pe_data.stdout = fds[1];
87 pe_data.close_me = fds[0];
Jeff Dikec4399012007-07-15 23:38:56 -070088 err = run_helper(slip_pre_exec, &pe_data, argv);
Jeff Dikea3c77c62005-06-13 15:52:18 -070089 if(err < 0)
90 goto out_close;
91 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Jeff Dike1ffb9162007-05-06 14:51:22 -070093 output_len = UM_KERN_PAGE_SIZE;
Jeff Dikea3c77c62005-06-13 15:52:18 -070094 output = um_kmalloc(output_len);
95 if(output == NULL){
96 printk("slip_tramp : failed to allocate output buffer\n");
97 os_kill_process(pid, 1);
98 err = -ENOMEM;
99 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
Jeff Dikea3c77c62005-06-13 15:52:18 -0700102 os_close_file(fds[1]);
103 read_output(fds[0], output, output_len);
104 printk("%s", output);
105
106 CATCH_EINTR(err = waitpid(pid, &status, 0));
107 if(err < 0)
108 err = errno;
109 else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){
110 printk("'%s' didn't exit with status 0\n", argv[0]);
111 err = -EINVAL;
112 }
113 else err = 0;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 os_close_file(fds[0]);
116
Jeff Dikea3c77c62005-06-13 15:52:18 -0700117out_free:
118 kfree(output);
119 return err;
120
121out_close:
122 os_close_file(fds[0]);
123 os_close_file(fds[1]);
124out:
125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static int slip_open(void *data)
129{
130 struct slip_data *pri = data;
131 char version_buf[sizeof("nnnnn\0")];
132 char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")];
133 char *argv[] = { "uml_net", version_buf, "slip", "up", gate_buf,
134 NULL };
135 int sfd, mfd, err;
136
Jeff Dikea3c77c62005-06-13 15:52:18 -0700137 err = get_pty();
138 if(err < 0){
139 printk("slip-open : Failed to open pty, err = %d\n", -err);
140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Jeff Dikea3c77c62005-06-13 15:52:18 -0700142 mfd = err;
143
144 err = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0);
145 if(err < 0){
146 printk("Couldn't open tty for slip line, err = %d\n", -err);
147 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Jeff Dikea3c77c62005-06-13 15:52:18 -0700149 sfd = err;
150
151 if(set_up_tty(sfd))
152 goto out_close2;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 pri->slave = sfd;
Jeff Dikea3c77c62005-06-13 15:52:18 -0700155 pri->slip.pos = 0;
156 pri->slip.esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if(pri->gate_addr != NULL){
158 sprintf(version_buf, "%d", UML_NET_VERSION);
159 strcpy(gate_buf, pri->gate_addr);
160
161 err = slip_tramp(argv, sfd);
162
163 if(err < 0){
164 printk("slip_tramp failed - err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700165 goto out_close2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 err = os_get_ifname(pri->slave, pri->name);
168 if(err < 0){
169 printk("get_ifname failed, err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700170 goto out_close2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 iter_addresses(pri->dev, open_addr, pri->name);
173 }
174 else {
175 err = os_set_slip(sfd);
176 if(err < 0){
177 printk("Failed to set slip discipline encapsulation - "
178 "err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700179 goto out_close2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 }
182 return(mfd);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700183out_close2:
184 os_close_file(sfd);
185out_close:
186 os_close_file(mfd);
187out:
188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191static void slip_close(int fd, void *data)
192{
193 struct slip_data *pri = data;
194 char version_buf[sizeof("nnnnn\0")];
195 char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name,
196 NULL };
197 int err;
198
199 if(pri->gate_addr != NULL)
200 iter_addresses(pri->dev, close_addr, pri->name);
201
202 sprintf(version_buf, "%d", UML_NET_VERSION);
203
204 err = slip_tramp(argv, pri->slave);
205
206 if(err != 0)
207 printk("slip_tramp failed - errno = %d\n", -err);
208 os_close_file(fd);
209 os_close_file(pri->slave);
210 pri->slave = -1;
211}
212
213int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
214{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700215 return slip_proto_read(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
219{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700220 return slip_proto_write(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static int slip_set_mtu(int mtu, void *data)
224{
225 return(mtu);
226}
227
228static void slip_add_addr(unsigned char *addr, unsigned char *netmask,
229 void *data)
230{
231 struct slip_data *pri = data;
232
233 if(pri->slave < 0) return;
234 open_addr(addr, netmask, pri->name);
235}
236
237static void slip_del_addr(unsigned char *addr, unsigned char *netmask,
238 void *data)
239{
240 struct slip_data *pri = data;
241
242 if(pri->slave < 0) return;
243 close_addr(addr, netmask, pri->name);
244}
245
Jeff Dike5e7672e2006-09-27 01:50:33 -0700246const struct net_user_info slip_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .init = slip_user_init,
248 .open = slip_open,
249 .close = slip_close,
250 .remove = NULL,
251 .set_mtu = slip_set_mtu,
252 .add_address = slip_add_addr,
253 .delete_address = slip_del_addr,
254 .max_packet = BUF_SIZE
255};