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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "kern_util.h" |
| 12 | #include "user.h" |
| 13 | #include "net_user.h" |
| 14 | #include "slip.h" |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 15 | #include "slip_common.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "os.h" |
Paolo 'Blaisorblade' Giarrusso | c13e569 | 2006-10-19 23:28:20 -0700 | [diff] [blame] | 17 | #include "um_malloc.h" |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 18 | #include "kern_constants.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 20 | static int slip_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | struct slip_data *pri = data; |
| 23 | |
| 24 | pri->dev = dev; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 25 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static int set_up_tty(int fd) |
| 29 | { |
| 30 | int i; |
| 31 | struct termios tios; |
| 32 | |
| 33 | if (tcgetattr(fd, &tios) < 0) { |
| 34 | printk("could not get initial terminal attributes\n"); |
| 35 | return(-1); |
| 36 | } |
| 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) { |
| 51 | printk("failed to set terminal attributes\n"); |
| 52 | return(-1); |
| 53 | } |
| 54 | return(0); |
| 55 | } |
| 56 | |
| 57 | struct slip_pre_exec_data { |
| 58 | int stdin; |
| 59 | int stdout; |
| 60 | int close_me; |
| 61 | }; |
| 62 | |
| 63 | static void slip_pre_exec(void *arg) |
| 64 | { |
| 65 | struct slip_pre_exec_data *data = arg; |
| 66 | |
| 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); |
| 70 | } |
| 71 | |
| 72 | static int slip_tramp(char **argv, int fd) |
| 73 | { |
| 74 | struct slip_pre_exec_data pe_data; |
| 75 | char *output; |
| 76 | int status, pid, fds[2], err, output_len; |
| 77 | |
| 78 | err = os_pipe(fds, 1, 0); |
| 79 | if(err < 0){ |
| 80 | printk("slip_tramp : pipe failed, err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 81 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | err = 0; |
| 85 | pe_data.stdin = fd; |
| 86 | pe_data.stdout = fds[1]; |
| 87 | pe_data.close_me = fds[0]; |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame^] | 88 | err = run_helper(slip_pre_exec, &pe_data, argv); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 89 | if(err < 0) |
| 90 | goto out_close; |
| 91 | pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 93 | output_len = UM_KERN_PAGE_SIZE; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 102 | os_close_file(fds[1]); |
| 103 | read_output(fds[0], output, output_len); |
| 104 | printk("%s", output); |
| 105 | |
| 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; |
| 112 | } |
| 113 | else err = 0; |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | os_close_file(fds[0]); |
| 116 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 117 | out_free: |
| 118 | kfree(output); |
| 119 | return err; |
| 120 | |
| 121 | out_close: |
| 122 | os_close_file(fds[0]); |
| 123 | os_close_file(fds[1]); |
| 124 | out: |
| 125 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static int slip_open(void *data) |
| 129 | { |
| 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; |
| 136 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 137 | err = get_pty(); |
| 138 | if(err < 0){ |
| 139 | printk("slip-open : Failed to open pty, err = %d\n", -err); |
| 140 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 142 | mfd = err; |
| 143 | |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 149 | sfd = err; |
| 150 | |
| 151 | if(set_up_tty(sfd)) |
| 152 | goto out_close2; |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | pri->slave = sfd; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 155 | pri->slip.pos = 0; |
| 156 | pri->slip.esc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if(pri->gate_addr != NULL){ |
| 158 | sprintf(version_buf, "%d", UML_NET_VERSION); |
| 159 | strcpy(gate_buf, pri->gate_addr); |
| 160 | |
| 161 | err = slip_tramp(argv, sfd); |
| 162 | |
| 163 | if(err < 0){ |
| 164 | printk("slip_tramp failed - err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 165 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | err = os_get_ifname(pri->slave, pri->name); |
| 168 | if(err < 0){ |
| 169 | printk("get_ifname failed, err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 170 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | iter_addresses(pri->dev, open_addr, pri->name); |
| 173 | } |
| 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); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 179 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | return(mfd); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 183 | out_close2: |
| 184 | os_close_file(sfd); |
| 185 | out_close: |
| 186 | os_close_file(mfd); |
| 187 | out: |
| 188 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void slip_close(int fd, void *data) |
| 192 | { |
| 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; |
| 198 | |
| 199 | if(pri->gate_addr != NULL) |
| 200 | iter_addresses(pri->dev, close_addr, pri->name); |
| 201 | |
| 202 | sprintf(version_buf, "%d", UML_NET_VERSION); |
| 203 | |
| 204 | err = slip_tramp(argv, pri->slave); |
| 205 | |
| 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; |
| 211 | } |
| 212 | |
| 213 | int slip_user_read(int fd, void *buf, int len, struct slip_data *pri) |
| 214 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 215 | return slip_proto_read(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | int slip_user_write(int fd, void *buf, int len, struct slip_data *pri) |
| 219 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 220 | return slip_proto_write(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static int slip_set_mtu(int mtu, void *data) |
| 224 | { |
| 225 | return(mtu); |
| 226 | } |
| 227 | |
| 228 | static void slip_add_addr(unsigned char *addr, unsigned char *netmask, |
| 229 | void *data) |
| 230 | { |
| 231 | struct slip_data *pri = data; |
| 232 | |
| 233 | if(pri->slave < 0) return; |
| 234 | open_addr(addr, netmask, pri->name); |
| 235 | } |
| 236 | |
| 237 | static void slip_del_addr(unsigned char *addr, unsigned char *netmask, |
| 238 | void *data) |
| 239 | { |
| 240 | struct slip_data *pri = data; |
| 241 | |
| 242 | if(pri->slave < 0) return; |
| 243 | close_addr(addr, netmask, pri->name); |
| 244 | } |
| 245 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 246 | const struct net_user_info slip_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 255 | }; |