Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
| 3 | * Licensed under the GPL. |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 10 | #include <fcntl.h> |
| 11 | #include <string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <sys/termios.h> |
| 13 | #include <sys/wait.h> |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 14 | #include "kern_constants.h" |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 15 | #include "net_user.h" |
| 16 | #include "os.h" |
| 17 | #include "slip.h" |
| 18 | #include "um_malloc.h" |
| 19 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 21 | static int slip_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
| 23 | struct slip_data *pri = data; |
| 24 | |
| 25 | pri->dev = dev; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 26 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static int set_up_tty(int fd) |
| 30 | { |
| 31 | int i; |
| 32 | struct termios tios; |
| 33 | |
| 34 | if (tcgetattr(fd, &tios) < 0) { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 35 | printk(UM_KERN_ERR "could not get initial terminal " |
| 36 | "attributes\n"); |
| 37 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | tios.c_cflag = CS8 | CREAD | HUPCL | CLOCAL; |
| 41 | tios.c_iflag = IGNBRK | IGNPAR; |
| 42 | tios.c_oflag = 0; |
| 43 | tios.c_lflag = 0; |
| 44 | for (i = 0; i < NCCS; i++) |
| 45 | tios.c_cc[i] = 0; |
| 46 | tios.c_cc[VMIN] = 1; |
| 47 | tios.c_cc[VTIME] = 0; |
| 48 | |
| 49 | cfsetospeed(&tios, B38400); |
| 50 | cfsetispeed(&tios, B38400); |
| 51 | |
| 52 | if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 53 | printk(UM_KERN_ERR "failed to set terminal attributes\n"); |
| 54 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 56 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | struct slip_pre_exec_data { |
| 60 | int stdin; |
| 61 | int stdout; |
| 62 | int close_me; |
| 63 | }; |
| 64 | |
| 65 | static void slip_pre_exec(void *arg) |
| 66 | { |
| 67 | struct slip_pre_exec_data *data = arg; |
| 68 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 69 | if (data->stdin >= 0) |
| 70 | dup2(data->stdin, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | dup2(data->stdout, 1); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 72 | if (data->close_me >= 0) |
| 73 | close(data->close_me); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int slip_tramp(char **argv, int fd) |
| 77 | { |
| 78 | struct slip_pre_exec_data pe_data; |
| 79 | char *output; |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 80 | int pid, fds[2], err, output_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | err = os_pipe(fds, 1, 0); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 83 | if (err < 0) { |
| 84 | printk(UM_KERN_ERR "slip_tramp : pipe failed, err = %d\n", |
| 85 | -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 86 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | err = 0; |
| 90 | pe_data.stdin = fd; |
| 91 | pe_data.stdout = fds[1]; |
| 92 | pe_data.close_me = fds[0]; |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 93 | err = run_helper(slip_pre_exec, &pe_data, argv); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 94 | if (err < 0) |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 95 | goto out_close; |
| 96 | pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 98 | output_len = UM_KERN_PAGE_SIZE; |
Jeff Dike | e4c4bf99 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 99 | output = kmalloc(output_len, UM_GFP_KERNEL); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 100 | if (output == NULL) { |
| 101 | printk(UM_KERN_ERR "slip_tramp : failed to allocate output " |
| 102 | "buffer\n"); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 103 | os_kill_process(pid, 1); |
| 104 | err = -ENOMEM; |
| 105 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 108 | close(fds[1]); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 109 | read_output(fds[0], output, output_len); |
| 110 | printk("%s", output); |
| 111 | |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 112 | err = helper_wait(pid, 0, argv[0]); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 113 | close(fds[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 120 | close(fds[0]); |
| 121 | close(fds[1]); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 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")]; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 131 | char *argv[] = { "uml_net", version_buf, "slip", "up", gate_buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | NULL }; |
| 133 | int sfd, mfd, err; |
| 134 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 135 | err = get_pty(); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 136 | if (err < 0) { |
| 137 | printk(UM_KERN_ERR "slip-open : Failed to open pty, err = %d\n", |
| 138 | -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 139 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 141 | mfd = err; |
| 142 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 143 | err = open(ptsname(mfd), O_RDWR, 0); |
| 144 | if (err < 0) { |
| 145 | printk(UM_KERN_ERR "Couldn't open tty for slip line, " |
| 146 | "err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 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 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 151 | if (set_up_tty(sfd)) |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 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; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 157 | if (pri->gate_addr != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | sprintf(version_buf, "%d", UML_NET_VERSION); |
| 159 | strcpy(gate_buf, pri->gate_addr); |
| 160 | |
| 161 | err = slip_tramp(argv, sfd); |
| 162 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 163 | if (err < 0) { |
| 164 | printk(UM_KERN_ERR "slip_tramp failed - err = %d\n", |
| 165 | -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 166 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | err = os_get_ifname(pri->slave, pri->name); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 169 | if (err < 0) { |
| 170 | printk(UM_KERN_ERR "get_ifname failed, err = %d\n", |
| 171 | -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 172 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | iter_addresses(pri->dev, open_addr, pri->name); |
| 175 | } |
| 176 | else { |
| 177 | err = os_set_slip(sfd); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 178 | if (err < 0) { |
| 179 | printk(UM_KERN_ERR "Failed to set slip discipline " |
| 180 | "encapsulation - err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 181 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 184 | return mfd; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 185 | out_close2: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 186 | close(sfd); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 187 | out_close: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 188 | close(mfd); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 189 | out: |
| 190 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void slip_close(int fd, void *data) |
| 194 | { |
| 195 | struct slip_data *pri = data; |
| 196 | char version_buf[sizeof("nnnnn\0")]; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 197 | char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | NULL }; |
| 199 | int err; |
| 200 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 201 | if (pri->gate_addr != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | iter_addresses(pri->dev, close_addr, pri->name); |
| 203 | |
| 204 | sprintf(version_buf, "%d", UML_NET_VERSION); |
| 205 | |
| 206 | err = slip_tramp(argv, pri->slave); |
| 207 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 208 | if (err != 0) |
| 209 | printk(UM_KERN_ERR "slip_tramp failed - errno = %d\n", -err); |
| 210 | close(fd); |
| 211 | close(pri->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | pri->slave = -1; |
| 213 | } |
| 214 | |
| 215 | int slip_user_read(int fd, void *buf, int len, struct slip_data *pri) |
| 216 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 217 | return slip_proto_read(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | int slip_user_write(int fd, void *buf, int len, struct slip_data *pri) |
| 221 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 222 | return slip_proto_write(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | static void slip_add_addr(unsigned char *addr, unsigned char *netmask, |
| 226 | void *data) |
| 227 | { |
| 228 | struct slip_data *pri = data; |
| 229 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 230 | if (pri->slave < 0) |
| 231 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 240 | if (pri->slave < 0) |
| 241 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | close_addr(addr, netmask, pri->name); |
| 243 | } |
| 244 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 245 | const struct net_user_info slip_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | .init = slip_user_init, |
| 247 | .open = slip_open, |
| 248 | .close = slip_close, |
| 249 | .remove = NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | .add_address = slip_add_addr, |
| 251 | .delete_address = slip_del_addr, |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 252 | .mtu = BUF_SIZE, |
| 253 | .max_packet = BUF_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | }; |