blob: 853cdf9a12e587e5c983ee78481ca54265a953de [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* minimalistic telnet/network interface it might turn into a wire interface */
2/* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <openbsc/telnet_interface.h>
29#include <openbsc/gsm_subscriber.h>
30#include <openbsc/chan_alloc.h>
31#include <openbsc/gsm_04_08.h>
32#include <openbsc/gsm_04_11.h>
Harald Weltef4625b12010-02-20 16:24:02 +010033#include <osmocore/msgb.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/abis_rsl.h>
35#include <openbsc/paging.h>
36#include <openbsc/signal.h>
Harald Weltef4625b12010-02-20 16:24:02 +010037#include <osmocore/talloc.h>
Harald Weltecf2ec4a2009-12-17 23:10:46 +010038#include <openbsc/debug.h>
Harald Welte59b04682009-06-10 05:40:52 +080039
40#include <vty/buffer.h>
41
Harald Welte59b04682009-06-10 05:40:52 +080042/* per connection data */
43LLIST_HEAD(active_connections);
44
Harald Weltea8379772009-06-20 22:36:41 +020045static void *tall_telnet_ctx;
46
Harald Welte59b04682009-06-10 05:40:52 +080047/* per network data */
48static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
Harald Welte59b04682009-06-10 05:40:52 +080049
50static struct bsc_fd server_socket = {
51 .when = BSC_FD_READ,
52 .cb = telnet_new_connection,
53 .priv_nr = 0,
54};
55
Harald Welte67798f72010-05-16 19:30:28 +020056void telnet_init(struct gsm_network *network, int port)
57{
Harald Welte59b04682009-06-10 05:40:52 +080058 struct sockaddr_in sock_addr;
59 int fd, on = 1;
60
Harald Weltea8379772009-06-20 22:36:41 +020061 tall_telnet_ctx = talloc_named_const(tall_bsc_ctx, 1,
62 "telnet_connection");
63
Harald Welte59b04682009-06-10 05:40:52 +080064 bsc_vty_init(network);
65
66 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
67
68 if (fd < 0) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +010069 LOGP(DNM, LOGL_ERROR, "Telnet interface socket creation failed\n");
Harald Welte59b04682009-06-10 05:40:52 +080070 return;
71 }
72
73 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
74
75 memset(&sock_addr, 0, sizeof(sock_addr));
76 sock_addr.sin_family = AF_INET;
77 sock_addr.sin_port = htons(port);
78 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
79
80 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
Harald Welte36ab5452009-12-21 23:36:45 +010081 LOGP(DNM, LOGL_ERROR, "Telnet interface failed to bind\n");
Harald Welte59b04682009-06-10 05:40:52 +080082 return;
83 }
84
85 if (listen(fd, 0) < 0) {
Harald Welte36ab5452009-12-21 23:36:45 +010086 LOGP(DNM, LOGL_ERROR, "Telnet interface failed to listen\n");
Harald Welte59b04682009-06-10 05:40:52 +080087 return;
88 }
89
90 server_socket.data = network;
91 server_socket.fd = fd;
92 bsc_register_fd(&server_socket);
Harald Welte59b04682009-06-10 05:40:52 +080093}
94
Harald Weltefa13cad2010-03-23 00:09:32 +080095extern const char *openbsc_copyright;
Harald Weltefa13cad2010-03-23 00:09:32 +080096
Harald Welte67798f72010-05-16 19:30:28 +020097static void print_welcome(int fd)
98{
Harald Welte59b04682009-06-10 05:40:52 +080099 int ret;
100 static char *msg =
Harald Weltefa13cad2010-03-23 00:09:32 +0800101 "Welcome to the OpenBSC Control interface\n";
Harald Welte59b04682009-06-10 05:40:52 +0800102
103 ret = write(fd, msg, strlen(msg));
Harald Weltefa13cad2010-03-23 00:09:32 +0800104 ret = write(fd, openbsc_copyright, strlen(openbsc_copyright));
Harald Welte59b04682009-06-10 05:40:52 +0800105}
106
Harald Welte67798f72010-05-16 19:30:28 +0200107int telnet_close_client(struct bsc_fd *fd)
108{
Harald Welte59b04682009-06-10 05:40:52 +0800109 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
110
111 close(fd->fd);
112 bsc_unregister_fd(fd);
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100113
114 if (conn->dbg) {
Harald Welte51d2a592010-03-26 21:28:59 +0800115 log_del_target(conn->dbg);
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100116 talloc_free(conn->dbg);
117 }
118
Harald Welte59b04682009-06-10 05:40:52 +0800119 llist_del(&conn->entry);
Harald Welte49a84052009-06-22 01:36:25 +0200120 talloc_free(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800121 return 0;
122}
123
124static int client_data(struct bsc_fd *fd, unsigned int what)
125{
126 struct telnet_connection *conn = fd->data;
127 int rc = 0;
128
129 if (what & BSC_FD_READ) {
130 conn->fd.when &= ~BSC_FD_READ;
131 rc = vty_read(conn->vty);
132 }
133
Harald Welted1bc77e2009-08-07 00:31:23 +0200134 /* vty might have been closed from vithin vty_read() */
135 if (!conn->vty)
136 return rc;
137
Harald Welte59b04682009-06-10 05:40:52 +0800138 if (what & BSC_FD_WRITE) {
139 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
140 if (rc == BUFFER_EMPTY)
141 conn->fd.when &= ~BSC_FD_WRITE;
142 }
143
144 return rc;
145}
146
Harald Welte67798f72010-05-16 19:30:28 +0200147static int telnet_new_connection(struct bsc_fd *fd, unsigned int what)
148{
Harald Welte59b04682009-06-10 05:40:52 +0800149 struct telnet_connection *connection;
150 struct sockaddr_in sockaddr;
151 socklen_t len = sizeof(sockaddr);
152 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
153
154 if (new_connection < 0) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100155 LOGP(DNM, LOGL_ERROR, "telnet accept failed\n");
Harald Welte59b04682009-06-10 05:40:52 +0800156 return -1;
157 }
158
159
Harald Welte857e00d2009-06-26 20:25:23 +0200160 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
Harald Welte59b04682009-06-10 05:40:52 +0800161 connection->network = (struct gsm_network*)fd->data;
162 connection->fd.data = connection;
163 connection->fd.fd = new_connection;
164 connection->fd.when = BSC_FD_READ;
165 connection->fd.cb = client_data;
Harald Welte59b04682009-06-10 05:40:52 +0800166 bsc_register_fd(&connection->fd);
167 llist_add_tail(&connection->entry, &active_connections);
168
169 print_welcome(new_connection);
170
171 connection->vty = vty_create(new_connection, connection);
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100172 if (!connection->vty) {
173 LOGP(DNM, LOGL_ERROR, "couldn't create VTY\n");
Harald Welte59b04682009-06-10 05:40:52 +0800174 return -1;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100175 }
Harald Welte59b04682009-06-10 05:40:52 +0800176
177 return 0;
178}
179
180/* callback from VTY code */
181void vty_event(enum event event, int sock, struct vty *vty)
182{
183 struct telnet_connection *connection = vty->priv;
184 struct bsc_fd *bfd = &connection->fd;
185
Harald Welted1bc77e2009-08-07 00:31:23 +0200186 if (vty->type != VTY_TERM)
187 return;
188
Harald Welte59b04682009-06-10 05:40:52 +0800189 switch (event) {
190 case VTY_READ:
191 bfd->when |= BSC_FD_READ;
192 break;
193 case VTY_WRITE:
194 bfd->when |= BSC_FD_WRITE;
195 break;
Harald Welted1bc77e2009-08-07 00:31:23 +0200196 case VTY_CLOSED:
197 /* vty layer is about to free() vty */
198 connection->vty = NULL;
199 telnet_close_client(bfd);
200 break;
Harald Welte59b04682009-06-10 05:40:52 +0800201 default:
202 break;
203 }
204}
205