Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 1 | /* |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <stdlib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <termios.h> |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 10 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <netinet/in.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "chan_user.h" |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 13 | #include "kern_constants.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "os.h" |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 15 | #include "port.h" |
Paolo 'Blaisorblade' Giarrusso | c13e569 | 2006-10-19 23:28:20 -0700 | [diff] [blame] | 16 | #include "um_malloc.h" |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 17 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | struct port_chan { |
| 20 | int raw; |
| 21 | struct termios tt; |
| 22 | void *kernel_data; |
| 23 | char dev[sizeof("32768\0")]; |
| 24 | }; |
| 25 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 26 | static void *port_init(char *str, int device, const struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | struct port_chan *data; |
| 29 | void *kern_data; |
| 30 | char *end; |
| 31 | int port; |
| 32 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 33 | if (*str != ':') { |
| 34 | printk(UM_KERN_ERR "port_init : channel type 'port' must " |
| 35 | "specify a port number\n"); |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 36 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | str++; |
| 39 | port = strtoul(str, &end, 0); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 40 | if ((*end != '\0') || (end == str)) { |
| 41 | printk(UM_KERN_ERR "port_init : couldn't parse port '%s'\n", |
| 42 | str); |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 43 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | kern_data = port_data(port); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 47 | if (kern_data == NULL) |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 48 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Jeff Dike | e4c4bf9 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 50 | data = kmalloc(sizeof(*data), UM_GFP_KERNEL); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 51 | if (data == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | goto err; |
| 53 | |
| 54 | *data = ((struct port_chan) { .raw = opts->raw, |
| 55 | .kernel_data = kern_data }); |
| 56 | sprintf(data->dev, "%d", port); |
| 57 | |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 58 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | err: |
| 60 | port_kern_free(kern_data); |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 61 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static void port_free(void *d) |
| 65 | { |
| 66 | struct port_chan *data = d; |
| 67 | |
| 68 | port_kern_free(data->kernel_data); |
| 69 | kfree(data); |
| 70 | } |
| 71 | |
| 72 | static int port_open(int input, int output, int primary, void *d, |
| 73 | char **dev_out) |
| 74 | { |
| 75 | struct port_chan *data = d; |
| 76 | int fd, err; |
| 77 | |
| 78 | fd = port_wait(data->kernel_data); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 79 | if ((fd >= 0) && data->raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | CATCH_EINTR(err = tcgetattr(fd, &data->tt)); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 81 | if (err) |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 82 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | err = raw(fd); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 85 | if (err) |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 86 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | *dev_out = data->dev; |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 89 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void port_close(int fd, void *d) |
| 93 | { |
| 94 | struct port_chan *data = d; |
| 95 | |
| 96 | port_remove_dev(data->kernel_data); |
| 97 | os_close_file(fd); |
| 98 | } |
| 99 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 100 | const struct chan_ops port_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .type = "port", |
| 102 | .init = port_init, |
| 103 | .open = port_open, |
| 104 | .close = port_close, |
| 105 | .read = generic_read, |
| 106 | .write = generic_write, |
Paolo 'Blaisorblade' Giarrusso | fd9bc53 | 2005-11-13 16:07:10 -0800 | [diff] [blame] | 107 | .console_write = generic_console_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .window_size = generic_window_size, |
| 109 | .free = port_free, |
| 110 | .winch = 1, |
| 111 | }; |
| 112 | |
| 113 | int port_listen_fd(int port) |
| 114 | { |
| 115 | struct sockaddr_in addr; |
| 116 | int fd, err, arg; |
| 117 | |
| 118 | fd = socket(PF_INET, SOCK_STREAM, 0); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 119 | if (fd == -1) |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 120 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | arg = 1; |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 123 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | err = -errno; |
| 125 | goto out; |
| 126 | } |
| 127 | |
| 128 | addr.sin_family = AF_INET; |
| 129 | addr.sin_port = htons(port); |
| 130 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 131 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | err = -errno; |
| 133 | goto out; |
| 134 | } |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 135 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 136 | if (listen(fd, 1) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | err = -errno; |
| 138 | goto out; |
| 139 | } |
| 140 | |
| 141 | err = os_set_fd_block(fd, 0); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 142 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | goto out; |
| 144 | |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 145 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | out: |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 147 | close(fd); |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 148 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | struct port_pre_exec_data { |
| 152 | int sock_fd; |
| 153 | int pipe_fd; |
| 154 | }; |
| 155 | |
| 156 | void port_pre_exec(void *arg) |
| 157 | { |
| 158 | struct port_pre_exec_data *data = arg; |
| 159 | |
| 160 | dup2(data->sock_fd, 0); |
| 161 | dup2(data->sock_fd, 1); |
| 162 | dup2(data->sock_fd, 2); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 163 | close(data->sock_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | dup2(data->pipe_fd, 3); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 165 | shutdown(3, SHUT_RD); |
| 166 | close(data->pipe_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int port_connection(int fd, int *socket, int *pid_out) |
| 170 | { |
| 171 | int new, err; |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 172 | char *argv[] = { "/usr/sbin/in.telnetd", "-L", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | "/usr/lib/uml/port-helper", NULL }; |
| 174 | struct port_pre_exec_data data; |
| 175 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 176 | new = accept(fd, NULL, 0); |
| 177 | if (new < 0) |
| 178 | return -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | err = os_pipe(socket, 0, 0); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 181 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | goto out_close; |
| 183 | |
| 184 | data = ((struct port_pre_exec_data) |
| 185 | { .sock_fd = new, |
| 186 | .pipe_fd = socket[1] }); |
| 187 | |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 188 | err = run_helper(port_pre_exec, &data, argv); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 189 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | goto out_shutdown; |
| 191 | |
| 192 | *pid_out = err; |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 193 | return new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | out_shutdown: |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 196 | shutdown(socket[0], SHUT_RDWR); |
| 197 | close(socket[0]); |
| 198 | shutdown(socket[1], SHUT_RDWR); |
| 199 | close(socket[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | out_close: |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 201 | close(new); |
Jeff Dike | 67608e0 | 2007-02-10 01:44:02 -0800 | [diff] [blame] | 202 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |