blob: 6462c5f9a88bcbfe1bfa5b4db17a3c0cd9851f50 [file] [log] [blame]
Holger Freyther219518d2009-01-02 22:04:43 +00001/* 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 <malloc.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <openbsc/telnet_interface.h>
Holger Freytherae61cae2009-01-04 03:46:01 +000029#include <openbsc/gsm_subscriber.h>
Holger Freytherf87573d2009-01-04 03:49:41 +000030#include <openbsc/chan_alloc.h>
Holger Freyther3ae8fd22009-01-04 03:50:40 +000031#include <openbsc/gsm_04_08.h>
Holger Freyther868b8cd2009-01-04 03:57:27 +000032#include <openbsc/msgb.h>
Holger Freyther7448a532009-01-04 20:18:23 +000033#include <openbsc/abis_rsl.h>
Harald Welte38c2f132009-01-06 23:10:57 +000034#include <openbsc/paging.h>
Holger Freyther219518d2009-01-02 22:04:43 +000035
36extern void telnet_parse(struct telnet_connection *connection, char *line);
37
Holger Freytherae61cae2009-01-04 03:46:01 +000038#define WRITE_CONNECTION(fd, msg...) \
39 int ret; \
40 char buf[4096]; \
41 snprintf(buf, sizeof(buf), msg); \
42 ret = write(fd, buf, strlen(buf));
43
44
Holger Freyther219518d2009-01-02 22:04:43 +000045/* per connection data */
46LLIST_HEAD(active_connections);
47
48/* per network data */
49static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
50static struct bsc_fd server_socket = {
51 .when = BSC_FD_READ,
52 .cb = telnet_new_connection,
53 .priv_nr = 0,
54};
55
56void telnet_init(struct gsm_network *network, int port) {
57 struct sockaddr_in sock_addr;
Holger Freytherf0776892009-02-03 20:49:51 +000058 int fd, on = 1;
Holger Freyther219518d2009-01-02 22:04:43 +000059
60 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
61
62 if (fd < 0) {
63 perror("Telnet interface socket creation failed");
64 return;
65 }
66
Holger Freytherf0776892009-02-03 20:49:51 +000067 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
68
Holger Freyther219518d2009-01-02 22:04:43 +000069 memset(&sock_addr, 0, sizeof(sock_addr));
70 sock_addr.sin_family = AF_INET;
71 sock_addr.sin_port = htons(port);
72 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
73
74 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
75 perror("Telnet interface failed to bind");
76 return;
77 }
78
79 if (listen(fd, 0) < 0) {
80 perror("Telnet interface failed to listen");
81 return;
82 }
83
84 server_socket.data = network;
85 server_socket.fd = fd;
86 bsc_register_fd(&server_socket);
87}
88
89void telnet_write_help(int fd) {
90 int ret;
91 static char *msg =
92 "Help for the ad-hoc telnet command line interface\n"
93 "The generic pattern is CMD LEN DATA\\n or just CMD\n"
94 "where CMD is one of the following:\n"
95 "help\n"
96 "page IMSI (type)\n"
97 "call IMSI (number)\n"
98 "get_channel IMSI Add use count on an active channel\n"
99 "put_channel IMSI Remove use count on an active channel\n"
Holger Freytherae61cae2009-01-04 03:46:01 +0000100 "show This will show the channel allocation\n"
Holger Freyther868b8cd2009-01-04 03:57:27 +0000101 "48 IMSI 0xAB 0xEF...Send GSM 04.08. proto and msg byte then data\n"
Holger Freyther219518d2009-01-02 22:04:43 +0000102 "11 IMSI 0xAB 0xEF...Send GSM 04.11\n";
103
104 ret = write(fd, msg, strlen(msg));
105}
106
107static void print_welcome(int fd) {
108 int ret;
109 static char *msg =
110 "Welcome to the OpenBSC Control interface\n"
111 "Copyright (C) 2008, 2009 Harald Welte\n"
112 "Contributions by Daniel Willmann, Jan Lübbe, "
113 "Stefan Schmidt, Holger Freyther\n\n"
114 "License GPLv2+: GNU GPL version 2 or later "
115 "<http://gnu.org/licenses/gpl.html>\n"
116 "This is free software: you are free to change "
117 "and redistribute it.\n"
118 "There is NO WARRANTY, to the extent permitted "
119 "by law.\nType \"help\" to get a short introduction.\n";
120
121 ret = write(fd, msg, strlen(msg));
122}
123
124int telnet_close_client(struct bsc_fd *fd) {
125 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
126
127 close(fd->fd);
128 bsc_unregister_fd(fd);
129 llist_del(&conn->entry);
130 free(conn);
131 return 0;
132}
133
134void telnet_error_client(int fd) {
135 int ret;
136 static char *msg = "Something went wrong. Please try again.\n";
137
138 printf("Error\n");
139 ret = write(fd, msg, strlen(msg));
140}
141
Holger Freytherf87573d2009-01-04 03:49:41 +0000142static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi,
143 const char **error, int fd) {
144 int ret;
145 struct gsm_lchan *lchan;
146 struct gsm_subscriber *subscr;
147
148 subscr = subscr_get_by_imsi(imsi);
149 if (!subscr) {
150 ret = write(fd, error[0], strlen(error[0]));
151 return NULL;
152 }
153
154 lchan = lchan_find(bts, subscr);
155 if (!lchan)
156 ret = write(fd, error[1], strlen(error[1]));
157
158 subscr_put(subscr);
159 return lchan;
160}
161
Holger Freyther7448a532009-01-04 20:18:23 +0000162void telnet_page(struct telnet_connection *connection, const char *imsi, int type) {
163 int ret;
164 static const char* error[] = {
165 "paging: IMSI not found\n",
166 "paging: No channel allocated for IMSI -> will allocate\n" };
167 struct gsm_bts *bts = &connection->network->bts[connection->bts];
168 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
169
170 if (lchan) {
171 static const char *msg = "paging: A Channel is already allocated.\n";
172 ret = write(connection->fd.fd, msg, strlen(msg));
173 return;
174 }
175
176 struct gsm_subscriber *subscr = subscr_get_by_imsi(imsi);
177 if (!subscr)
178 return;
179
Holger Freyther1fd34142009-02-09 23:42:03 +0000180 paging_request(bts, subscr, type);
Holger Freyther7448a532009-01-04 20:18:23 +0000181}
182
Holger Freyther219518d2009-01-02 22:04:43 +0000183void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000184 static const char* error[] = {
185 "put_channel: IMSI not found\n",
186 "put_channel: No channel allocated for IMSI\n" };
187 struct gsm_bts *bts = &connection->network->bts[connection->bts];
188 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
189
190 if (!lchan)
191 return;
192
193 put_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000194}
195
196void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000197 static const char* error[] = {
198 "get_channel: IMSI not found\n",
199 "get_channel: No channel allocated for IMSI\n" };
200 struct gsm_bts *bts = &connection->network->bts[connection->bts];
201 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
202
203 if (!lchan)
204 return;
205
206 use_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000207}
208
209void telnet_call(struct telnet_connection *connection, const char* imsi,
210 const char *origin) {
Holger Freyther3ae8fd22009-01-04 03:50:40 +0000211 static const char* error[] = {
212 "call: IMSI not found\n",
213 "call: No channel allocated for IMSI\n" };
214 struct gsm_bts *bts = &connection->network->bts[connection->bts];
215 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
216
217 if (!lchan)
218 return;
219
220 /* TODO: add the origin */
221 gsm48_cc_tx_setup(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000222}
223
224void telnet_send_gsm_48(struct telnet_connection *connection) {
Holger Freyther868b8cd2009-01-04 03:57:27 +0000225 static const char* error[] = {
226 "48: IMSI not found\n",
227 "48: No channel allocated for IMSI\n" };
Holger Freyther5e76ce62009-01-04 20:15:12 +0000228 int ret;
Holger Freyther868b8cd2009-01-04 03:57:27 +0000229 struct gsm_bts *bts = &connection->network->bts[connection->bts];
230 struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd);
231
232 if (!lchan)
233 return;
234
Holger Freyther5e76ce62009-01-04 20:15:12 +0000235 if (connection->read < 2) {
236 static const char *msg = "48: Need at least two bytes";
237 ret = write(connection->fd.fd, msg, strlen(msg));
238 return;
239 }
240
Holger Freyther868b8cd2009-01-04 03:57:27 +0000241 struct msgb *msg = gsm48_msgb_alloc();
242 struct gsm48_hdr *gh;
243 int i;
244
245 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2);
246 msg->lchan = lchan;
247
248 gh->proto_discr = connection->commands[0];
249 gh->msg_type = connection->commands[1];
250 for (i = 2; i < connection->read; ++i)
251 gh->data[i-2] = connection->commands[i];
252
Holger Freyther4c8f1142009-01-04 20:17:07 +0000253 gsm48_sendmsg(msg);
Holger Freyther219518d2009-01-02 22:04:43 +0000254}
255
256void telnet_send_gsm_11(struct telnet_connection *connection) {
257 printf("sending gsm04.11 message\n");
258}
259
Holger Freytherae61cae2009-01-04 03:46:01 +0000260static void show_bts(int fd, struct gsm_bts *bts) {
261 WRITE_CONNECTION(fd,
262 "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
263 bts->nr, bts->bts_nr, bts->location_area_code,
264 bts->num_trx, bts->c0->arfcn, bts->c0->nr)
265}
266
267static void show_trx(int fd, struct gsm_bts_trx *trx) {
268 WRITE_CONNECTION(fd,
269 " TRX: %u ARFCN: %u\n",
270 trx->nr, trx->arfcn)
271}
272
273static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
274 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000275 " TS: #%u pchan: %12s flags: %u\n",
276 ts->nr, gsm_pchan_name(ts->pchan), ts->flags);
Holger Freytherae61cae2009-01-04 03:46:01 +0000277}
278
279static void show_lchan(int fd, struct gsm_lchan *lchan) {
280 struct gsm_subscriber *subscr = lchan->subscr;
281 WRITE_CONNECTION(fd,
Harald Welte626fe9c2009-01-09 21:24:53 +0000282 " LCHAN: #%u type: %7s count: %d subscriber: %s/%s/%s use: %d loc: %p\n",
Harald Welted4fc1b22009-01-04 16:11:31 +0000283 lchan->nr, gsm_lchan_name(lchan->type),
Harald Welte626fe9c2009-01-09 21:24:53 +0000284 lchan->use_count,
Holger Freytherae61cae2009-01-04 03:46:01 +0000285 subscr ? subscr->imsi : "na",
286 subscr ? subscr->tmsi : "na",
287 subscr ? subscr->name : "na",
288 lchan->use_count, lchan->loc_operation);
289}
290
291void telnet_list_channels(struct telnet_connection *connection) {
292 int bts_no, trx, lchan_no, ts_no;
293 struct gsm_network *network = connection->network;
294
295 for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
296 struct gsm_bts *bts = &network->bts[bts_no];
297 show_bts(connection->fd.fd, bts);
298
299 for (trx = 0; trx < bts->num_trx; ++trx) {
300 show_trx(connection->fd.fd, &bts->trx[trx]);
301 for (ts_no = 0; ts_no < 8; ++ts_no) {
302 show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
303 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
304 struct gsm_lchan *lchan =
305 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
306 show_lchan(connection->fd.fd, lchan);
307 }
308 }
309 }
310 }
311}
312
Holger Freyther219518d2009-01-02 22:04:43 +0000313static int client_data(struct bsc_fd *fd, unsigned int what) {
314 char buf[4096];
315 int ret;
316
317 ret = read(fd->fd, buf, sizeof(buf)-1);
318 buf[ret] = '\0';
319
320 /* connection is gone */
321 if (ret <= 0)
322 return telnet_close_client(fd);
323
324 /* time to parse. This code assumes that the input is line based */
325 telnet_parse((struct telnet_connection*)fd->data, buf);
326
327 return 0;
328}
329
330static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
331 struct telnet_connection *connection;
332 struct sockaddr_in sockaddr;
333 socklen_t len = sizeof(sockaddr);
334 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
335
336 if (new_connection < 0) {
337 perror("telnet accept failed");
338 return -1;
339 }
340
341
342 connection = (struct telnet_connection*)malloc(sizeof(*connection));
343 memset(connection, 0, sizeof(*connection));
344 connection->network = (struct gsm_network*)fd->data;
345 connection->fd.data = connection;
346 connection->fd.fd = new_connection;
347 connection->fd.when = BSC_FD_READ;
348 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000349 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000350 bsc_register_fd(&connection->fd);
351 llist_add_tail(&connection->entry, &active_connections);
352
353 print_welcome(new_connection);
354
355 return 0;
356}