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> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 14 | #include <net_user.h> |
| 15 | #include <os.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 16 | #include "slip.h" |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 17 | #include <um_malloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 19 | static int slip_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
| 21 | struct slip_data *pri = data; |
| 22 | |
| 23 | pri->dev = dev; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 24 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static int set_up_tty(int fd) |
| 28 | { |
| 29 | int i; |
| 30 | struct termios tios; |
| 31 | |
| 32 | if (tcgetattr(fd, &tios) < 0) { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 33 | printk(UM_KERN_ERR "could not get initial terminal " |
| 34 | "attributes\n"); |
| 35 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 51 | printk(UM_KERN_ERR "failed to set terminal attributes\n"); |
| 52 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 54 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 67 | if (data->stdin >= 0) |
| 68 | dup2(data->stdin, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | dup2(data->stdout, 1); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 70 | if (data->close_me >= 0) |
| 71 | close(data->close_me); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static int slip_tramp(char **argv, int fd) |
| 75 | { |
| 76 | struct slip_pre_exec_data pe_data; |
| 77 | char *output; |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 78 | int pid, fds[2], err, output_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | err = os_pipe(fds, 1, 0); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 81 | if (err < 0) { |
| 82 | printk(UM_KERN_ERR "slip_tramp : pipe failed, err = %d\n", |
| 83 | -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 84 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | err = 0; |
| 88 | pe_data.stdin = fd; |
| 89 | pe_data.stdout = fds[1]; |
| 90 | pe_data.close_me = fds[0]; |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 91 | err = run_helper(slip_pre_exec, &pe_data, argv); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 92 | if (err < 0) |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 93 | goto out_close; |
| 94 | pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 96 | output_len = UM_KERN_PAGE_SIZE; |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 97 | output = uml_kmalloc(output_len, UM_GFP_KERNEL); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 98 | if (output == NULL) { |
| 99 | printk(UM_KERN_ERR "slip_tramp : failed to allocate output " |
| 100 | "buffer\n"); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 101 | os_kill_process(pid, 1); |
| 102 | err = -ENOMEM; |
Vitaliy Ivanov | 9a8beb9 | 2011-07-25 17:12:51 -0700 | [diff] [blame] | 103 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 106 | close(fds[1]); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 107 | read_output(fds[0], output, output_len); |
| 108 | printk("%s", output); |
| 109 | |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 110 | err = helper_wait(pid); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 111 | close(fds[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 113 | kfree(output); |
| 114 | return err; |
| 115 | |
| 116 | out_close: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 117 | close(fds[0]); |
| 118 | close(fds[1]); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 119 | out: |
| 120 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static 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 Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 128 | char *argv[] = { "uml_net", version_buf, "slip", "up", gate_buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | NULL }; |
| 130 | int sfd, mfd, err; |
| 131 | |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 132 | err = get_pty(); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 133 | if (err < 0) { |
| 134 | printk(UM_KERN_ERR "slip-open : Failed to open pty, err = %d\n", |
| 135 | -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 136 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 138 | mfd = err; |
| 139 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 140 | 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 Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 144 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 146 | sfd = err; |
| 147 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 148 | if (set_up_tty(sfd)) |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 149 | goto out_close2; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | pri->slave = sfd; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 152 | pri->slip.pos = 0; |
| 153 | pri->slip.esc = 0; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 154 | if (pri->gate_addr != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | sprintf(version_buf, "%d", UML_NET_VERSION); |
| 156 | strcpy(gate_buf, pri->gate_addr); |
| 157 | |
| 158 | err = slip_tramp(argv, sfd); |
| 159 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 160 | if (err < 0) { |
| 161 | printk(UM_KERN_ERR "slip_tramp failed - err = %d\n", |
| 162 | -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); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 166 | if (err < 0) { |
| 167 | printk(UM_KERN_ERR "get_ifname failed, err = %d\n", |
| 168 | -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 169 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | iter_addresses(pri->dev, open_addr, pri->name); |
| 172 | } |
| 173 | else { |
| 174 | err = os_set_slip(sfd); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 175 | if (err < 0) { |
| 176 | printk(UM_KERN_ERR "Failed to set slip discipline " |
| 177 | "encapsulation - err = %d\n", -err); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 178 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 181 | return mfd; |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 182 | out_close2: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 183 | close(sfd); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 184 | out_close: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 185 | close(mfd); |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 186 | out: |
| 187 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void slip_close(int fd, void *data) |
| 191 | { |
| 192 | struct slip_data *pri = data; |
| 193 | char version_buf[sizeof("nnnnn\0")]; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 194 | char *argv[] = { "uml_net", version_buf, "slip", "down", pri->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | NULL }; |
| 196 | int err; |
| 197 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 198 | if (pri->gate_addr != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | 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 Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 205 | if (err != 0) |
| 206 | printk(UM_KERN_ERR "slip_tramp failed - errno = %d\n", -err); |
| 207 | close(fd); |
| 208 | close(pri->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | pri->slave = -1; |
| 210 | } |
| 211 | |
| 212 | int slip_user_read(int fd, void *buf, int len, struct slip_data *pri) |
| 213 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 214 | return slip_proto_read(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | int slip_user_write(int fd, void *buf, int len, struct slip_data *pri) |
| 218 | { |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 219 | return slip_proto_write(fd, buf, len, &pri->slip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | static void slip_add_addr(unsigned char *addr, unsigned char *netmask, |
| 223 | void *data) |
| 224 | { |
| 225 | struct slip_data *pri = data; |
| 226 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 227 | if (pri->slave < 0) |
| 228 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | open_addr(addr, netmask, pri->name); |
| 230 | } |
| 231 | |
| 232 | static void slip_del_addr(unsigned char *addr, unsigned char *netmask, |
| 233 | void *data) |
| 234 | { |
| 235 | struct slip_data *pri = data; |
| 236 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 237 | if (pri->slave < 0) |
| 238 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | close_addr(addr, netmask, pri->name); |
| 240 | } |
| 241 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 242 | const struct net_user_info slip_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | .init = slip_user_init, |
| 244 | .open = slip_open, |
| 245 | .close = slip_close, |
| 246 | .remove = NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | .add_address = slip_add_addr, |
| 248 | .delete_address = slip_del_addr, |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 249 | .mtu = BUF_SIZE, |
| 250 | .max_packet = BUF_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | }; |