Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 "user_util.h" |
| 12 | #include "kern_util.h" |
| 13 | #include "user.h" |
| 14 | #include "net_user.h" |
| 15 | #include "slip.h" |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 16 | #include "slip_common.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "os.h" |
| 18 | |
| 19 | void slip_user_init(void *data, void *dev) |
| 20 | { |
| 21 | struct slip_data *pri = data; |
| 22 | |
| 23 | pri->dev = dev; |
| 24 | } |
| 25 | |
| 26 | static int set_up_tty(int fd) |
| 27 | { |
| 28 | int i; |
| 29 | struct termios tios; |
| 30 | |
| 31 | if (tcgetattr(fd, &tios) < 0) { |
| 32 | printk("could not get initial terminal attributes\n"); |
| 33 | return(-1); |
| 34 | } |
| 35 | |
| 36 | tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL; |
| 37 | tios.c_iflag = IGNBRK | IGNPAR; |
| 38 | tios.c_oflag = 0; |
| 39 | tios.c_lflag = 0; |
| 40 | for (i = 0; i < NCCS; i++) |
| 41 | tios.c_cc[i] = 0; |
| 42 | tios.c_cc[VMIN] = 1; |
| 43 | tios.c_cc[VTIME] = 0; |
| 44 | |
| 45 | cfsetospeed(&tios, B38400); |
| 46 | cfsetispeed(&tios, B38400); |
| 47 | |
| 48 | if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) { |
| 49 | printk("failed to set terminal attributes\n"); |
| 50 | return(-1); |
| 51 | } |
| 52 | return(0); |
| 53 | } |
| 54 | |
| 55 | struct slip_pre_exec_data { |
| 56 | int stdin; |
| 57 | int stdout; |
| 58 | int close_me; |
| 59 | }; |
| 60 | |
| 61 | static void slip_pre_exec(void *arg) |
| 62 | { |
| 63 | struct slip_pre_exec_data *data = arg; |
| 64 | |
| 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); |
| 68 | } |
| 69 | |
| 70 | static int slip_tramp(char **argv, int fd) |
| 71 | { |
| 72 | struct slip_pre_exec_data pe_data; |
| 73 | char *output; |
| 74 | int status, pid, fds[2], err, output_len; |
| 75 | |
| 76 | err = os_pipe(fds, 1, 0); |
| 77 | if(err < 0){ |
| 78 | printk("slip_tramp : pipe failed, err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 79 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | err = 0; |
| 83 | pe_data.stdin = fd; |
| 84 | pe_data.stdout = fds[1]; |
| 85 | pe_data.close_me = fds[0]; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 86 | err = run_helper(slip_pre_exec, &pe_data, argv, NULL); |
| 87 | if(err < 0) |
| 88 | goto out_close; |
| 89 | pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 91 | output_len = page_size(); |
| 92 | output = um_kmalloc(output_len); |
| 93 | if(output == NULL){ |
| 94 | printk("slip_tramp : failed to allocate output buffer\n"); |
| 95 | os_kill_process(pid, 1); |
| 96 | err = -ENOMEM; |
| 97 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 100 | os_close_file(fds[1]); |
| 101 | read_output(fds[0], output, output_len); |
| 102 | printk("%s", output); |
| 103 | |
| 104 | CATCH_EINTR(err = waitpid(pid, &status, 0)); |
| 105 | if(err < 0) |
| 106 | err = errno; |
| 107 | else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){ |
| 108 | printk("'%s' didn't exit with status 0\n", argv[0]); |
| 109 | err = -EINVAL; |
| 110 | } |
| 111 | else err = 0; |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | os_close_file(fds[0]); |
| 114 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 115 | out_free: |
| 116 | kfree(output); |
| 117 | return err; |
| 118 | |
| 119 | out_close: |
| 120 | os_close_file(fds[0]); |
| 121 | os_close_file(fds[1]); |
| 122 | out: |
| 123 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int slip_open(void *data) |
| 127 | { |
| 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, |
| 132 | NULL }; |
| 133 | int sfd, mfd, err; |
| 134 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 135 | err = get_pty(); |
| 136 | if(err < 0){ |
| 137 | printk("slip-open : Failed to open pty, err = %d\n", -err); |
| 138 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 140 | mfd = err; |
| 141 | |
| 142 | err = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0); |
| 143 | if(err < 0){ |
| 144 | printk("Couldn't open tty for slip line, err = %d\n", -err); |
| 145 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 147 | sfd = err; |
| 148 | |
| 149 | if(set_up_tty(sfd)) |
| 150 | goto out_close2; |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | pri->slave = sfd; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 153 | pri->slip.pos = 0; |
| 154 | pri->slip.esc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if(pri->gate_addr != NULL){ |
| 156 | sprintf(version_buf, "%d", UML_NET_VERSION); |
| 157 | strcpy(gate_buf, pri->gate_addr); |
| 158 | |
| 159 | err = slip_tramp(argv, sfd); |
| 160 | |
| 161 | if(err < 0){ |
| 162 | printk("slip_tramp failed - err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 163 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | err = os_get_ifname(pri->slave, pri->name); |
| 166 | if(err < 0){ |
| 167 | printk("get_ifname failed, err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 168 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | iter_addresses(pri->dev, open_addr, pri->name); |
| 171 | } |
| 172 | else { |
| 173 | err = os_set_slip(sfd); |
| 174 | if(err < 0){ |
| 175 | printk("Failed to set slip discipline encapsulation - " |
| 176 | "err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 177 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | return(mfd); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 181 | out_close2: |
| 182 | os_close_file(sfd); |
| 183 | out_close: |
| 184 | os_close_file(mfd); |
| 185 | out: |
| 186 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static void slip_close(int fd, void *data) |
| 190 | { |
| 191 | struct slip_data *pri = data; |
| 192 | char version_buf[sizeof("nnnnn\0")]; |
| 193 | char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name, |
| 194 | NULL }; |
| 195 | int err; |
| 196 | |
| 197 | if(pri->gate_addr != NULL) |
| 198 | iter_addresses(pri->dev, close_addr, pri->name); |
| 199 | |
| 200 | sprintf(version_buf, "%d", UML_NET_VERSION); |
| 201 | |
| 202 | err = slip_tramp(argv, pri->slave); |
| 203 | |
| 204 | if(err != 0) |
| 205 | printk("slip_tramp failed - errno = %d\n", -err); |
| 206 | os_close_file(fd); |
| 207 | os_close_file(pri->slave); |
| 208 | pri->slave = -1; |
| 209 | } |
| 210 | |
| 211 | int slip_user_read(int fd, void *buf, int len, struct slip_data *pri) |
| 212 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 213 | return slip_proto_read(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | int slip_user_write(int fd, void *buf, int len, struct slip_data *pri) |
| 217 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 218 | return slip_proto_write(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int slip_set_mtu(int mtu, void *data) |
| 222 | { |
| 223 | return(mtu); |
| 224 | } |
| 225 | |
| 226 | static void slip_add_addr(unsigned char *addr, unsigned char *netmask, |
| 227 | void *data) |
| 228 | { |
| 229 | struct slip_data *pri = data; |
| 230 | |
| 231 | if(pri->slave < 0) return; |
| 232 | open_addr(addr, netmask, pri->name); |
| 233 | } |
| 234 | |
| 235 | static void slip_del_addr(unsigned char *addr, unsigned char *netmask, |
| 236 | void *data) |
| 237 | { |
| 238 | struct slip_data *pri = data; |
| 239 | |
| 240 | if(pri->slave < 0) return; |
| 241 | close_addr(addr, netmask, pri->name); |
| 242 | } |
| 243 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame^] | 244 | const struct net_user_info slip_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | .init = slip_user_init, |
| 246 | .open = slip_open, |
| 247 | .close = slip_close, |
| 248 | .remove = NULL, |
| 249 | .set_mtu = slip_set_mtu, |
| 250 | .add_address = slip_add_addr, |
| 251 | .delete_address = slip_del_addr, |
| 252 | .max_packet = BUF_SIZE |
| 253 | }; |