blob: 7b010b76ddf092cc7cd6322fcf49dd519a4d2ecf [file] [log] [blame]
Jeff Dike67608e02007-02-10 01:44:02 -08001/*
Jeff Dikee99525f2007-10-16 01:26:41 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <termios.h>
Jeff Dikee99525f2007-10-16 01:26:41 -070010#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <netinet/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "chan_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "os.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070014#include "port.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070015#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17struct port_chan {
18 int raw;
19 struct termios tt;
20 void *kernel_data;
21 char dev[sizeof("32768\0")];
22};
23
Jeff Dike5e7672e2006-09-27 01:50:33 -070024static void *port_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 struct port_chan *data;
27 void *kern_data;
28 char *end;
29 int port;
30
Jeff Dikee99525f2007-10-16 01:26:41 -070031 if (*str != ':') {
32 printk(UM_KERN_ERR "port_init : channel type 'port' must "
33 "specify a port number\n");
Jeff Dike67608e02007-02-10 01:44:02 -080034 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 }
36 str++;
37 port = strtoul(str, &end, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -070038 if ((*end != '\0') || (end == str)) {
39 printk(UM_KERN_ERR "port_init : couldn't parse port '%s'\n",
40 str);
Jeff Dike67608e02007-02-10 01:44:02 -080041 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43
44 kern_data = port_data(port);
Jeff Dikee99525f2007-10-16 01:26:41 -070045 if (kern_data == NULL)
Jeff Dike67608e02007-02-10 01:44:02 -080046 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jeff Dike43f5b302008-05-12 14:01:52 -070048 data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
Jeff Dikee99525f2007-10-16 01:26:41 -070049 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 goto err;
51
52 *data = ((struct port_chan) { .raw = opts->raw,
53 .kernel_data = kern_data });
54 sprintf(data->dev, "%d", port);
55
Jeff Dike67608e02007-02-10 01:44:02 -080056 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 err:
58 port_kern_free(kern_data);
Jeff Dike67608e02007-02-10 01:44:02 -080059 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62static void port_free(void *d)
63{
64 struct port_chan *data = d;
65
66 port_kern_free(data->kernel_data);
67 kfree(data);
68}
69
70static int port_open(int input, int output, int primary, void *d,
71 char **dev_out)
72{
73 struct port_chan *data = d;
74 int fd, err;
75
76 fd = port_wait(data->kernel_data);
Jeff Dikee99525f2007-10-16 01:26:41 -070077 if ((fd >= 0) && data->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
Jeff Dikee99525f2007-10-16 01:26:41 -070079 if (err)
Jeff Dike67608e02007-02-10 01:44:02 -080080 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 err = raw(fd);
Jeff Dikee99525f2007-10-16 01:26:41 -070083 if (err)
Jeff Dike67608e02007-02-10 01:44:02 -080084 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 *dev_out = data->dev;
Jeff Dike67608e02007-02-10 01:44:02 -080087 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90static void port_close(int fd, void *d)
91{
92 struct port_chan *data = d;
93
94 port_remove_dev(data->kernel_data);
95 os_close_file(fd);
96}
97
Jeff Dike5e7672e2006-09-27 01:50:33 -070098const struct chan_ops port_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .type = "port",
100 .init = port_init,
101 .open = port_open,
102 .close = port_close,
103 .read = generic_read,
104 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -0800105 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .window_size = generic_window_size,
107 .free = port_free,
108 .winch = 1,
109};
110
111int port_listen_fd(int port)
112{
113 struct sockaddr_in addr;
114 int fd, err, arg;
115
116 fd = socket(PF_INET, SOCK_STREAM, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -0700117 if (fd == -1)
Jeff Dike67608e02007-02-10 01:44:02 -0800118 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 arg = 1;
Jeff Dikee99525f2007-10-16 01:26:41 -0700121 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 err = -errno;
123 goto out;
124 }
125
126 addr.sin_family = AF_INET;
127 addr.sin_port = htons(port);
128 addr.sin_addr.s_addr = htonl(INADDR_ANY);
Jeff Dikee99525f2007-10-16 01:26:41 -0700129 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 err = -errno;
131 goto out;
132 }
Jeff Dike67608e02007-02-10 01:44:02 -0800133
Jeff Dikee99525f2007-10-16 01:26:41 -0700134 if (listen(fd, 1) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 err = -errno;
136 goto out;
137 }
138
139 err = os_set_fd_block(fd, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -0700140 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 goto out;
142
Jeff Dike67608e02007-02-10 01:44:02 -0800143 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 out:
Jeff Dikee99525f2007-10-16 01:26:41 -0700145 close(fd);
Jeff Dike67608e02007-02-10 01:44:02 -0800146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149struct port_pre_exec_data {
150 int sock_fd;
151 int pipe_fd;
152};
153
WANG Cong1605ec02008-04-28 02:13:56 -0700154static void port_pre_exec(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct port_pre_exec_data *data = arg;
157
158 dup2(data->sock_fd, 0);
159 dup2(data->sock_fd, 1);
160 dup2(data->sock_fd, 2);
Jeff Dikee99525f2007-10-16 01:26:41 -0700161 close(data->sock_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 dup2(data->pipe_fd, 3);
Jeff Dikee99525f2007-10-16 01:26:41 -0700163 shutdown(3, SHUT_RD);
164 close(data->pipe_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167int port_connection(int fd, int *socket, int *pid_out)
168{
169 int new, err;
Jeff Dike67608e02007-02-10 01:44:02 -0800170 char *argv[] = { "/usr/sbin/in.telnetd", "-L",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 "/usr/lib/uml/port-helper", NULL };
172 struct port_pre_exec_data data;
173
Jeff Dikee99525f2007-10-16 01:26:41 -0700174 new = accept(fd, NULL, 0);
175 if (new < 0)
176 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 err = os_pipe(socket, 0, 0);
Jeff Dikee99525f2007-10-16 01:26:41 -0700179 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out_close;
181
182 data = ((struct port_pre_exec_data)
183 { .sock_fd = new,
184 .pipe_fd = socket[1] });
185
Jeff Dikec4399012007-07-15 23:38:56 -0700186 err = run_helper(port_pre_exec, &data, argv);
Jeff Dikee99525f2007-10-16 01:26:41 -0700187 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out_shutdown;
189
190 *pid_out = err;
Jeff Dike67608e02007-02-10 01:44:02 -0800191 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 out_shutdown:
Jeff Dikee99525f2007-10-16 01:26:41 -0700194 shutdown(socket[0], SHUT_RDWR);
195 close(socket[0]);
196 shutdown(socket[1], SHUT_RDWR);
197 close(socket[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 out_close:
Jeff Dikee99525f2007-10-16 01:26:41 -0700199 close(new);
Jeff Dike67608e02007-02-10 01:44:02 -0800200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}