blob: 0d6b66c64a813731c465c4e2ecaf33ffa97e327c [file] [log] [blame]
Jeff Dikecd1ae0e2007-10-16 01:27:29 -07001/*
2 * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL.
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <errno.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070010#include <fcntl.h>
11#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <sys/termios.h>
13#include <sys/wait.h>
Al Viro37185b32012-10-08 03:27:32 +010014#include <net_user.h>
15#include <os.h>
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070016#include "slip.h"
Al Viro37185b32012-10-08 03:27:32 +010017#include <um_malloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jeff Dikef34d9d22007-05-06 14:51:04 -070019static int slip_user_init(void *data, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
21 struct slip_data *pri = data;
22
23 pri->dev = dev;
Jeff Dikef34d9d22007-05-06 14:51:04 -070024 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025}
26
27static int set_up_tty(int fd)
28{
29 int i;
30 struct termios tios;
31
32 if (tcgetattr(fd, &tios) < 0) {
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070033 printk(UM_KERN_ERR "could not get initial terminal "
34 "attributes\n");
35 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 }
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) {
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070051 printk(UM_KERN_ERR "failed to set terminal attributes\n");
52 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57struct slip_pre_exec_data {
Hans-Werner Hilsef9bb3b52015-06-11 11:29:19 +020058 int stdin_fd;
59 int stdout_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int close_me;
61};
62
63static void slip_pre_exec(void *arg)
64{
65 struct slip_pre_exec_data *data = arg;
66
Hans-Werner Hilsef9bb3b52015-06-11 11:29:19 +020067 if (data->stdin_fd >= 0)
68 dup2(data->stdin_fd, 0);
69 dup2(data->stdout_fd, 1);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070070 if (data->close_me >= 0)
71 close(data->close_me);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74static int slip_tramp(char **argv, int fd)
75{
76 struct slip_pre_exec_data pe_data;
77 char *output;
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080078 int pid, fds[2], err, output_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 err = os_pipe(fds, 1, 0);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070081 if (err < 0) {
82 printk(UM_KERN_ERR "slip_tramp : pipe failed, err = %d\n",
83 -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -070084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87 err = 0;
Hans-Werner Hilsef9bb3b52015-06-11 11:29:19 +020088 pe_data.stdin_fd = fd;
89 pe_data.stdout_fd = fds[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 pe_data.close_me = fds[0];
Jeff Dikec4399012007-07-15 23:38:56 -070091 err = run_helper(slip_pre_exec, &pe_data, argv);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070092 if (err < 0)
Jeff Dikea3c77c62005-06-13 15:52:18 -070093 goto out_close;
94 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Jeff Dike1ffb9162007-05-06 14:51:22 -070096 output_len = UM_KERN_PAGE_SIZE;
Jeff Dike43f5b302008-05-12 14:01:52 -070097 output = uml_kmalloc(output_len, UM_GFP_KERNEL);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -070098 if (output == NULL) {
99 printk(UM_KERN_ERR "slip_tramp : failed to allocate output "
100 "buffer\n");
Jeff Dikea3c77c62005-06-13 15:52:18 -0700101 os_kill_process(pid, 1);
102 err = -ENOMEM;
Vitaliy Ivanov9a8beb92011-07-25 17:12:51 -0700103 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700106 close(fds[1]);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700107 read_output(fds[0], output, output_len);
108 printk("%s", output);
109
Jeff Dike1aa351a2008-02-04 22:31:10 -0800110 err = helper_wait(pid);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700111 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Jeff Dikea3c77c62005-06-13 15:52:18 -0700113 kfree(output);
114 return err;
115
116out_close:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700117 close(fds[0]);
118 close(fds[1]);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700119out:
120 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static int slip_open(void *data)
124{
125 struct slip_data *pri = data;
126 char version_buf[sizeof("nnnnn\0")];
127 char gate_buf[sizeof("nnn.nnn.nnn.nnn\0")];
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700128 char *argv[] = { "uml_net", version_buf, "slip", "up", gate_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 NULL };
130 int sfd, mfd, err;
131
Jeff Dikea3c77c62005-06-13 15:52:18 -0700132 err = get_pty();
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700133 if (err < 0) {
134 printk(UM_KERN_ERR "slip-open : Failed to open pty, err = %d\n",
135 -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Jeff Dikea3c77c62005-06-13 15:52:18 -0700138 mfd = err;
139
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700140 err = open(ptsname(mfd), O_RDWR, 0);
141 if (err < 0) {
142 printk(UM_KERN_ERR "Couldn't open tty for slip line, "
143 "err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700144 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Jeff Dikea3c77c62005-06-13 15:52:18 -0700146 sfd = err;
147
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700148 if (set_up_tty(sfd))
Jeff Dikea3c77c62005-06-13 15:52:18 -0700149 goto out_close2;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 pri->slave = sfd;
Jeff Dikea3c77c62005-06-13 15:52:18 -0700152 pri->slip.pos = 0;
153 pri->slip.esc = 0;
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700154 if (pri->gate_addr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 sprintf(version_buf, "%d", UML_NET_VERSION);
156 strcpy(gate_buf, pri->gate_addr);
157
158 err = slip_tramp(argv, sfd);
159
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700160 if (err < 0) {
161 printk(UM_KERN_ERR "slip_tramp failed - err = %d\n",
162 -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);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700166 if (err < 0) {
167 printk(UM_KERN_ERR "get_ifname failed, err = %d\n",
168 -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700169 goto out_close2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 iter_addresses(pri->dev, open_addr, pri->name);
172 }
173 else {
174 err = os_set_slip(sfd);
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700175 if (err < 0) {
176 printk(UM_KERN_ERR "Failed to set slip discipline "
177 "encapsulation - err = %d\n", -err);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700178 goto out_close2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 }
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700181 return mfd;
Jeff Dikea3c77c62005-06-13 15:52:18 -0700182out_close2:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700183 close(sfd);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700184out_close:
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700185 close(mfd);
Jeff Dikea3c77c62005-06-13 15:52:18 -0700186out:
187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190static void slip_close(int fd, void *data)
191{
192 struct slip_data *pri = data;
193 char version_buf[sizeof("nnnnn\0")];
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700194 char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 NULL };
196 int err;
197
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700198 if (pri->gate_addr != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 iter_addresses(pri->dev, close_addr, pri->name);
200
201 sprintf(version_buf, "%d", UML_NET_VERSION);
202
203 err = slip_tramp(argv, pri->slave);
204
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700205 if (err != 0)
206 printk(UM_KERN_ERR "slip_tramp failed - errno = %d\n", -err);
207 close(fd);
208 close(pri->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 pri->slave = -1;
210}
211
212int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
213{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700214 return slip_proto_read(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
218{
Jeff Dikea3c77c62005-06-13 15:52:18 -0700219 return slip_proto_write(fd, buf, len, &pri->slip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static void slip_add_addr(unsigned char *addr, unsigned char *netmask,
223 void *data)
224{
225 struct slip_data *pri = data;
226
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700227 if (pri->slave < 0)
228 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 open_addr(addr, netmask, pri->name);
230}
231
232static void slip_del_addr(unsigned char *addr, unsigned char *netmask,
233 void *data)
234{
235 struct slip_data *pri = data;
236
Jeff Dikecd1ae0e2007-10-16 01:27:29 -0700237 if (pri->slave < 0)
238 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 close_addr(addr, netmask, pri->name);
240}
241
Jeff Dike5e7672e2006-09-27 01:50:33 -0700242const struct net_user_info slip_user_info = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .init = slip_user_init,
244 .open = slip_open,
245 .close = slip_close,
246 .remove = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .add_address = slip_add_addr,
248 .delete_address = slip_del_addr,
Jeff Dikeb53f35a2007-10-16 01:27:31 -0700249 .mtu = BUF_SIZE,
250 .max_packet = BUF_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};