[IPV4]: Correct rp_filter help text.
[linux-2.6/verdex.git] / arch / um / drivers / slip_user.c
blob78f0e515da8f792ca57334d6ea1a5f26c0657638
1 #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 "kern_util.h"
12 #include "user.h"
13 #include "net_user.h"
14 #include "slip.h"
15 #include "slip_common.h"
16 #include "os.h"
17 #include "um_malloc.h"
18 #include "kern_constants.h"
20 static int slip_user_init(void *data, void *dev)
22 struct slip_data *pri = data;
24 pri->dev = dev;
25 return 0;
28 static int set_up_tty(int fd)
30 int i;
31 struct termios tios;
33 if (tcgetattr(fd, &tios) < 0) {
34 printk("could not get initial terminal attributes\n");
35 return(-1);
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;
47 cfsetospeed(&tios, B38400);
48 cfsetispeed(&tios, B38400);
50 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
51 printk("failed to set terminal attributes\n");
52 return(-1);
54 return(0);
57 struct slip_pre_exec_data {
58 int stdin;
59 int stdout;
60 int close_me;
63 static void slip_pre_exec(void *arg)
65 struct slip_pre_exec_data *data = arg;
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);
72 static int slip_tramp(char **argv, int fd)
74 struct slip_pre_exec_data pe_data;
75 char *output;
76 int status, pid, fds[2], err, output_len;
78 err = os_pipe(fds, 1, 0);
79 if(err < 0){
80 printk("slip_tramp : pipe failed, err = %d\n", -err);
81 goto out;
84 err = 0;
85 pe_data.stdin = fd;
86 pe_data.stdout = fds[1];
87 pe_data.close_me = fds[0];
88 err = run_helper(slip_pre_exec, &pe_data, argv, NULL);
89 if(err < 0)
90 goto out_close;
91 pid = err;
93 output_len = UM_KERN_PAGE_SIZE;
94 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;
102 os_close_file(fds[1]);
103 read_output(fds[0], output, output_len);
104 printk("%s", output);
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;
113 else err = 0;
115 os_close_file(fds[0]);
117 out_free:
118 kfree(output);
119 return err;
121 out_close:
122 os_close_file(fds[0]);
123 os_close_file(fds[1]);
124 out:
125 return err;
128 static int slip_open(void *data)
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;
137 err = get_pty();
138 if(err < 0){
139 printk("slip-open : Failed to open pty, err = %d\n", -err);
140 goto out;
142 mfd = err;
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;
149 sfd = err;
151 if(set_up_tty(sfd))
152 goto out_close2;
154 pri->slave = sfd;
155 pri->slip.pos = 0;
156 pri->slip.esc = 0;
157 if(pri->gate_addr != NULL){
158 sprintf(version_buf, "%d", UML_NET_VERSION);
159 strcpy(gate_buf, pri->gate_addr);
161 err = slip_tramp(argv, sfd);
163 if(err < 0){
164 printk("slip_tramp failed - err = %d\n", -err);
165 goto out_close2;
167 err = os_get_ifname(pri->slave, pri->name);
168 if(err < 0){
169 printk("get_ifname failed, err = %d\n", -err);
170 goto out_close2;
172 iter_addresses(pri->dev, open_addr, pri->name);
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);
179 goto out_close2;
182 return(mfd);
183 out_close2:
184 os_close_file(sfd);
185 out_close:
186 os_close_file(mfd);
187 out:
188 return err;
191 static void slip_close(int fd, void *data)
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;
199 if(pri->gate_addr != NULL)
200 iter_addresses(pri->dev, close_addr, pri->name);
202 sprintf(version_buf, "%d", UML_NET_VERSION);
204 err = slip_tramp(argv, pri->slave);
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;
213 int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
215 return slip_proto_read(fd, buf, len, &pri->slip);
218 int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
220 return slip_proto_write(fd, buf, len, &pri->slip);
223 static int slip_set_mtu(int mtu, void *data)
225 return(mtu);
228 static void slip_add_addr(unsigned char *addr, unsigned char *netmask,
229 void *data)
231 struct slip_data *pri = data;
233 if(pri->slave < 0) return;
234 open_addr(addr, netmask, pri->name);
237 static void slip_del_addr(unsigned char *addr, unsigned char *netmask,
238 void *data)
240 struct slip_data *pri = data;
242 if(pri->slave < 0) return;
243 close_addr(addr, netmask, pri->name);
246 const struct net_user_info slip_user_info = {
247 .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