8 #include <sys/termios.h>
10 #include <sys/signal.h>
11 #include "user_util.h"
12 #include "kern_util.h"
16 #include "slip_common.h"
19 void slip_user_init(void *data, void *dev)
21 struct slip_data *pri = data;
26 static int set_up_tty(int fd)
31 if (tcgetattr(fd, &tios) < 0) {
32 printk("could not get initial terminal attributes\n");
36 tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL;
37 tios.c_iflag = IGNBRK | IGNPAR;
40 for (i = 0; i < NCCS; i++)
45 cfsetospeed(&tios, B38400);
46 cfsetispeed(&tios, B38400);
48 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
49 printk("failed to set terminal attributes\n");
55 struct slip_pre_exec_data {
61 static void slip_pre_exec(void *arg)
63 struct slip_pre_exec_data *data = arg;
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);
70 static int slip_tramp(char **argv, int fd)
72 struct slip_pre_exec_data pe_data;
74 int status, pid, fds[2], err, output_len;
76 err = os_pipe(fds, 1, 0);
78 printk("slip_tramp : pipe failed, err = %d\n", -err);
84 pe_data.stdout = fds[1];
85 pe_data.close_me = fds[0];
86 err = run_helper(slip_pre_exec, &pe_data, argv, NULL);
91 output_len = page_size();
92 output = um_kmalloc(output_len);
94 printk("slip_tramp : failed to allocate output buffer\n");
95 os_kill_process(pid, 1);
100 os_close_file(fds[1]);
101 read_output(fds[0], output, output_len);
102 printk("%s", output);
104 CATCH_EINTR(err = waitpid(pid, &status, 0));
107 else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){
108 printk("'%s' didn't exit with status 0\n", argv[0]);
113 os_close_file(fds[0]);
120 os_close_file(fds[0]);
121 os_close_file(fds[1]);
126 static int slip_open(void *data)
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,
137 printk("slip-open : Failed to open pty, err = %d\n", -err);
142 err = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0);
144 printk("Couldn't open tty for slip line, err = %d\n", -err);
155 if(pri->gate_addr != NULL){
156 sprintf(version_buf, "%d", UML_NET_VERSION);
157 strcpy(gate_buf, pri->gate_addr);
159 err = slip_tramp(argv, sfd);
162 printk("slip_tramp failed - err = %d\n", -err);
165 err = os_get_ifname(pri->slave, pri->name);
167 printk("get_ifname failed, err = %d\n", -err);
170 iter_addresses(pri->dev, open_addr, pri->name);
173 err = os_set_slip(sfd);
175 printk("Failed to set slip discipline encapsulation - "
189 static void slip_close(int fd, void *data)
191 struct slip_data *pri = data;
192 char version_buf[sizeof("nnnnn\0")];
193 char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name,
197 if(pri->gate_addr != NULL)
198 iter_addresses(pri->dev, close_addr, pri->name);
200 sprintf(version_buf, "%d", UML_NET_VERSION);
202 err = slip_tramp(argv, pri->slave);
205 printk("slip_tramp failed - errno = %d\n", -err);
207 os_close_file(pri->slave);
211 int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
213 return slip_proto_read(fd, buf, len, &pri->slip);
216 int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
218 return slip_proto_write(fd, buf, len, &pri->slip);
221 static int slip_set_mtu(int mtu, void *data)
226 static void slip_add_addr(unsigned char *addr, unsigned char *netmask,
229 struct slip_data *pri = data;
231 if(pri->slave < 0) return;
232 open_addr(addr, netmask, pri->name);
235 static void slip_del_addr(unsigned char *addr, unsigned char *netmask,
238 struct slip_data *pri = data;
240 if(pri->slave < 0) return;
241 close_addr(addr, netmask, pri->name);
244 struct net_user_info slip_user_info = {
245 .init = slip_user_init,
249 .set_mtu = slip_set_mtu,
250 .add_address = slip_add_addr,
251 .delete_address = slip_del_addr,
252 .max_packet = BUF_SIZE