9 #include <sys/termios.h>
11 #include <sys/ioctl.h>
12 #include <sys/signal.h>
13 #include "user_util.h"
14 #include "kern_util.h"
18 #include "slip_proto.h"
22 void slip_user_init(void *data, void *dev)
24 struct slip_data *pri = data;
29 static int set_up_tty(int fd)
34 if (tcgetattr(fd, &tios) < 0) {
35 printk("could not get initial terminal attributes\n");
39 tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL;
40 tios.c_iflag = IGNBRK | IGNPAR;
43 for (i = 0; i < NCCS; i++)
48 cfsetospeed(&tios, B38400);
49 cfsetispeed(&tios, B38400);
51 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
52 printk("failed to set terminal attributes\n");
58 struct slip_pre_exec_data {
64 static void slip_pre_exec(void *arg)
66 struct slip_pre_exec_data *data = arg;
68 if(data->stdin != -1) dup2(data->stdin, 0);
69 dup2(data->stdout, 1);
70 if(data->close_me != -1) close(data->close_me);
73 static int slip_tramp(char **argv, int fd)
75 struct slip_pre_exec_data pe_data;
77 int status, pid, fds[2], err, output_len;
79 err = os_pipe(fds, 1, 0);
81 printk("slip_tramp : pipe failed, errno = %d\n", -err);
87 pe_data.stdout = fds[1];
88 pe_data.close_me = fds[0];
89 pid = run_helper(slip_pre_exec, &pe_data, argv, NULL);
91 if(pid < 0) err = pid;
93 output_len = page_size();
94 output = um_kmalloc(output_len);
96 printk("slip_tramp : failed to allocate output "
100 read_output(fds[0], output, output_len);
102 printk("%s", output);
105 if(waitpid(pid, &status, 0) < 0) err = errno;
106 else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){
107 printk("'%s' didn't exit with status 0\n", argv[0]);
114 static int slip_open(void *data)
116 struct slip_data *pri = data;
117 char version_buf[sizeof("nnnnn\0")];
118 char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")];
119 char *argv[] = { "uml_net", version_buf, "slip", "up", gate_buf,
121 int sfd, mfd, disc, sencap, err;
123 if((mfd = get_pty()) < 0){
124 printk("umn : Failed to open pty\n");
127 if((sfd = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0)) < 0){
128 printk("Couldn't open tty for slip line\n");
131 if(set_up_tty(sfd)) return(-1);
135 if(pri->gate_addr != NULL){
136 sprintf(version_buf, "%d", UML_NET_VERSION);
137 strcpy(gate_buf, pri->gate_addr);
139 err = slip_tramp(argv, sfd);
142 printk("slip_tramp failed - errno = %d\n", err);
145 if(ioctl(pri->slave, SIOCGIFNAME, pri->name) < 0){
146 printk("SIOCGIFNAME failed, errno = %d\n", errno);
149 iter_addresses(pri->dev, open_addr, pri->name);
153 if(ioctl(sfd, TIOCSETD, &disc) < 0){
154 printk("Failed to set slip line discipline - "
155 "errno = %d\n", errno);
159 if(ioctl(sfd, SIOCSIFENCAP, &sencap) < 0){
160 printk("Failed to set slip encapsulation - "
161 "errno = %d\n", errno);
168 static void slip_close(int fd, void *data)
170 struct slip_data *pri = data;
171 char version_buf[sizeof("nnnnn\0")];
172 char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name,
176 if(pri->gate_addr != NULL)
177 iter_addresses(pri->dev, close_addr, pri->name);
179 sprintf(version_buf, "%d", UML_NET_VERSION);
181 err = slip_tramp(argv, -1);
184 printk("slip_tramp failed - errno = %d\n", err);
190 int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
192 int i, n, size, start;
196 while(i < pri->more) {
197 size = slip_unesc(pri->ibuf[i++],
198 pri->ibuf, &pri->pos, &pri->esc);
200 memcpy(buf, pri->ibuf, size);
201 memmove(pri->ibuf, &pri->ibuf[i], pri->more-i);
202 pri->more=pri->more-i;
209 n = net_read(fd, &pri->ibuf[pri->pos], sizeof(pri->ibuf) - pri->pos);
210 if(n <= 0) return(n);
213 for(i = 0; i < n; i++){
214 size = slip_unesc(pri->ibuf[start + i],
215 pri->ibuf, &pri->pos, &pri->esc);
217 memcpy(buf, pri->ibuf, size);
218 memmove(pri->ibuf, &pri->ibuf[start+i+1], n-(i+1));
226 int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
230 actual = slip_esc(buf, pri->obuf, len);
231 n = net_write(fd, pri->obuf, actual);
236 static int slip_set_mtu(int mtu, void *data)
241 static void slip_add_addr(unsigned char *addr, unsigned char *netmask,
244 struct slip_data *pri = data;
246 if(pri->slave == -1) return;
247 open_addr(addr, netmask, pri->name);
250 static void slip_del_addr(unsigned char *addr, unsigned char *netmask,
253 struct slip_data *pri = data;
255 if(pri->slave == -1) return;
256 close_addr(addr, netmask, pri->name);
259 struct net_user_info slip_user_info = {
260 .init = slip_user_init,
264 .set_mtu = slip_set_mtu,
265 .add_address = slip_add_addr,
266 .delete_address = slip_del_addr,
267 .max_packet = BUF_SIZE
271 * Overrides for Emacs so that we follow Linus's tabbing style.
272 * Emacs will notice this stuff at the end of the file and automatically
273 * adjust the settings for this buffer only. This must remain at the end
275 * ---------------------------------------------------------------------------
277 * c-file-style: "linux"