blob: b9ef139f7bd39003741de30df7d85c21597584bd [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 Freyther9b177762009-02-16 19:07:18 +000032#include <openbsc/gsm_04_11.h>
Holger Freyther868b8cd2009-01-04 03:57:27 +000033#include <openbsc/msgb.h>
Holger Freyther7448a532009-01-04 20:18:23 +000034#include <openbsc/abis_rsl.h>
Harald Welte38c2f132009-01-06 23:10:57 +000035#include <openbsc/paging.h>
Holger Freyther7aaf1122009-02-14 22:51:13 +000036#include <openbsc/signal.h>
Holger Freyther219518d2009-01-02 22:04:43 +000037
38extern void telnet_parse(struct telnet_connection *connection, char *line);
39
Holger Freytherae61cae2009-01-04 03:46:01 +000040#define WRITE_CONNECTION(fd, msg...) \
41 int ret; \
42 char buf[4096]; \
43 snprintf(buf, sizeof(buf), msg); \
44 ret = write(fd, buf, strlen(buf));
45
46
Holger Freyther219518d2009-01-02 22:04:43 +000047/* per connection data */
48LLIST_HEAD(active_connections);
49
50/* per network data */
51static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
Holger Freyther7aaf1122009-02-14 22:51:13 +000052static int telnet_paging_callback(struct signal_data *signal, void *data);
Holger Freyther9b177762009-02-16 19:07:18 +000053static int telnet_sms_callback(struct signal_data *signal, void *data);
Holger Freyther7aaf1122009-02-14 22:51:13 +000054
Holger Freyther219518d2009-01-02 22:04:43 +000055static struct bsc_fd server_socket = {
56 .when = BSC_FD_READ,
57 .cb = telnet_new_connection,
58 .priv_nr = 0,
59};
60
61void telnet_init(struct gsm_network *network, int port) {
62 struct sockaddr_in sock_addr;
Holger Freytherf0776892009-02-03 20:49:51 +000063 int fd, on = 1;
Holger Freyther219518d2009-01-02 22:04:43 +000064
65 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
66
67 if (fd < 0) {
68 perror("Telnet interface socket creation failed");
69 return;
70 }
71
Holger Freytherf0776892009-02-03 20:49:51 +000072 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
73
Holger Freyther219518d2009-01-02 22:04:43 +000074 memset(&sock_addr, 0, sizeof(sock_addr));
75 sock_addr.sin_family = AF_INET;
76 sock_addr.sin_port = htons(port);
77 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
78
79 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
80 perror("Telnet interface failed to bind");
81 return;
82 }
83
84 if (listen(fd, 0) < 0) {
85 perror("Telnet interface failed to listen");
86 return;
87 }
88
89 server_socket.data = network;
90 server_socket.fd = fd;
91 bsc_register_fd(&server_socket);
Holger Freyther7aaf1122009-02-14 22:51:13 +000092
Holger Freyther9b177762009-02-16 19:07:18 +000093 /* register callbacks */
Holger Freyther7aaf1122009-02-14 22:51:13 +000094 register_signal_handler(S_PAGING, telnet_paging_callback, network);
Holger Freyther9b177762009-02-16 19:07:18 +000095 register_signal_handler(S_SMS, telnet_sms_callback, network);
Holger Freyther219518d2009-01-02 22:04:43 +000096}
97
98void telnet_write_help(int fd) {
99 int ret;
100 static char *msg =
101 "Help for the ad-hoc telnet command line interface\n"
102 "The generic pattern is CMD LEN DATA\\n or just CMD\n"
103 "where CMD is one of the following:\n"
104 "help\n"
105 "page IMSI (type)\n"
106 "call IMSI (number)\n"
107 "get_channel IMSI Add use count on an active channel\n"
108 "put_channel IMSI Remove use count on an active channel\n"
Holger Freytherae61cae2009-01-04 03:46:01 +0000109 "show This will show the channel allocation\n"
Holger Freyther868b8cd2009-01-04 03:57:27 +0000110 "48 IMSI 0xAB 0xEF...Send GSM 04.08. proto and msg byte then data\n"
Holger Freyther219518d2009-01-02 22:04:43 +0000111 "11 IMSI 0xAB 0xEF...Send GSM 04.11\n";
112
113 ret = write(fd, msg, strlen(msg));
114}
115
116static void print_welcome(int fd) {
117 int ret;
118 static char *msg =
119 "Welcome to the OpenBSC Control interface\n"
120 "Copyright (C) 2008, 2009 Harald Welte\n"
121 "Contributions by Daniel Willmann, Jan Lübbe, "
122 "Stefan Schmidt, Holger Freyther\n\n"
123 "License GPLv2+: GNU GPL version 2 or later "
124 "<http://gnu.org/licenses/gpl.html>\n"
125 "This is free software: you are free to change "
126 "and redistribute it.\n"
127 "There is NO WARRANTY, to the extent permitted "
128 "by law.\nType \"help\" to get a short introduction.\n";
129
130 ret = write(fd, msg, strlen(msg));
131}
132
133int telnet_close_client(struct bsc_fd *fd) {
134 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
135
136 close(fd->fd);
137 bsc_unregister_fd(fd);
138 llist_del(&conn->entry);
139 free(conn);
140 return 0;
141}
142
143void telnet_error_client(int fd) {
144 int ret;
145 static char *msg = "Something went wrong. Please try again.\n";
146
147 printf("Error\n");
148 ret = write(fd, msg, strlen(msg));
149}
150
Holger Freytherf87573d2009-01-04 03:49:41 +0000151static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi,
152 const char **error, int fd) {
153 int ret;
154 struct gsm_lchan *lchan;
155 struct gsm_subscriber *subscr;
156
157 subscr = subscr_get_by_imsi(imsi);
158 if (!subscr) {
159 ret = write(fd, error[0], strlen(error[0]));
160 return NULL;
161 }
162
163 lchan = lchan_find(bts, subscr);
164 if (!lchan)
165 ret = write(fd, error[1], strlen(error[1]));
166
167 subscr_put(subscr);
168 return lchan;
169}
170
Holger Freyther7448a532009-01-04 20:18:23 +0000171void telnet_page(struct telnet_connection *connection, const char *imsi, int type) {
172 int ret;
173 static const char* error[] = {
174 "paging: IMSI not found\n",
175 "paging: No channel allocated for IMSI -> will allocate\n" };
176 struct gsm_bts *bts = &connection->network->bts[connection->bts];
177 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
178
179 if (lchan) {
180 static const char *msg = "paging: A Channel is already allocated.\n";
181 ret = write(connection->fd.fd, msg, strlen(msg));
182 return;
183 }
184
185 struct gsm_subscriber *subscr = subscr_get_by_imsi(imsi);
186 if (!subscr)
187 return;
188
Holger Freyther1fd34142009-02-09 23:42:03 +0000189 paging_request(bts, subscr, type);
Holger Freyther7448a532009-01-04 20:18:23 +0000190}
191
Holger Freyther219518d2009-01-02 22:04:43 +0000192void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000193 static const char* error[] = {
194 "put_channel: IMSI not found\n",
195 "put_channel: No channel allocated for IMSI\n" };
196 struct gsm_bts *bts = &connection->network->bts[connection->bts];
197 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
198
199 if (!lchan)
200 return;
201
202 put_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000203}
204
205void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000206 static const char* error[] = {
207 "get_channel: IMSI not found\n",
208 "get_channel: No channel allocated for IMSI\n" };
209 struct gsm_bts *bts = &connection->network->bts[connection->bts];
210 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
211
212 if (!lchan)
213 return;
214
215 use_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000216}
217
218void telnet_call(struct telnet_connection *connection, const char* imsi,
219 const char *origin) {
Holger Freyther3ae8fd22009-01-04 03:50:40 +0000220 static const char* error[] = {
221 "call: IMSI not found\n",
222 "call: No channel allocated for IMSI\n" };
223 struct gsm_bts *bts = &connection->network->bts[connection->bts];
224 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
225
226 if (!lchan)
227 return;
228
229 /* TODO: add the origin */
230 gsm48_cc_tx_setup(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000231}
232
233void telnet_send_gsm_48(struct telnet_connection *connection) {
Holger Freyther868b8cd2009-01-04 03:57:27 +0000234 static const char* error[] = {
235 "48: IMSI not found\n",
236 "48: No channel allocated for IMSI\n" };
Holger Freyther5e76ce62009-01-04 20:15:12 +0000237 int ret;
Holger Freyther868b8cd2009-01-04 03:57:27 +0000238 struct gsm_bts *bts = &connection->network->bts[connection->bts];
239 struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd);
240
241 if (!lchan)
242 return;
243
Holger Freyther5e76ce62009-01-04 20:15:12 +0000244 if (connection->read < 2) {
245 static const char *msg = "48: Need at least two bytes";
246 ret = write(connection->fd.fd, msg, strlen(msg));
247 return;
248 }
249
Holger Freyther868b8cd2009-01-04 03:57:27 +0000250 struct msgb *msg = gsm48_msgb_alloc();
251 struct gsm48_hdr *gh;
252 int i;
253
254 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2);
255 msg->lchan = lchan;
256
257 gh->proto_discr = connection->commands[0];
258 gh->msg_type = connection->commands[1];
259 for (i = 2; i < connection->read; ++i)
260 gh->data[i-2] = connection->commands[i];
261
Holger Freyther4c8f1142009-01-04 20:17:07 +0000262 gsm48_sendmsg(msg);
Holger Freyther219518d2009-01-02 22:04:43 +0000263}
264
265void telnet_send_gsm_11(struct telnet_connection *connection) {
266 printf("sending gsm04.11 message\n");
267}
268
Holger Freytherae61cae2009-01-04 03:46:01 +0000269static void show_bts(int fd, struct gsm_bts *bts) {
270 WRITE_CONNECTION(fd,
271 "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
272 bts->nr, bts->bts_nr, bts->location_area_code,
273 bts->num_trx, bts->c0->arfcn, bts->c0->nr)
274}
275
276static void show_trx(int fd, struct gsm_bts_trx *trx) {
277 WRITE_CONNECTION(fd,
278 " TRX: %u ARFCN: %u\n",
279 trx->nr, trx->arfcn)
280}
281
282static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
283 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000284 " TS: #%u pchan: %12s flags: %u\n",
285 ts->nr, gsm_pchan_name(ts->pchan), ts->flags);
Holger Freytherae61cae2009-01-04 03:46:01 +0000286}
287
288static void show_lchan(int fd, struct gsm_lchan *lchan) {
289 struct gsm_subscriber *subscr = lchan->subscr;
290 WRITE_CONNECTION(fd,
Harald Welte626fe9c2009-01-09 21:24:53 +0000291 " LCHAN: #%u type: %7s count: %d subscriber: %s/%s/%s use: %d loc: %p\n",
Harald Welted4fc1b22009-01-04 16:11:31 +0000292 lchan->nr, gsm_lchan_name(lchan->type),
Harald Welte626fe9c2009-01-09 21:24:53 +0000293 lchan->use_count,
Holger Freytherae61cae2009-01-04 03:46:01 +0000294 subscr ? subscr->imsi : "na",
295 subscr ? subscr->tmsi : "na",
296 subscr ? subscr->name : "na",
297 lchan->use_count, lchan->loc_operation);
298}
299
300void telnet_list_channels(struct telnet_connection *connection) {
301 int bts_no, trx, lchan_no, ts_no;
302 struct gsm_network *network = connection->network;
303
304 for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
305 struct gsm_bts *bts = &network->bts[bts_no];
306 show_bts(connection->fd.fd, bts);
307
308 for (trx = 0; trx < bts->num_trx; ++trx) {
309 show_trx(connection->fd.fd, &bts->trx[trx]);
310 for (ts_no = 0; ts_no < 8; ++ts_no) {
311 show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
312 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
313 struct gsm_lchan *lchan =
314 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
315 show_lchan(connection->fd.fd, lchan);
316 }
317 }
318 }
319 }
320}
321
Holger Freyther219518d2009-01-02 22:04:43 +0000322static int client_data(struct bsc_fd *fd, unsigned int what) {
323 char buf[4096];
324 int ret;
325
326 ret = read(fd->fd, buf, sizeof(buf)-1);
327 buf[ret] = '\0';
328
329 /* connection is gone */
330 if (ret <= 0)
331 return telnet_close_client(fd);
332
333 /* time to parse. This code assumes that the input is line based */
334 telnet_parse((struct telnet_connection*)fd->data, buf);
335
336 return 0;
337}
338
339static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
340 struct telnet_connection *connection;
341 struct sockaddr_in sockaddr;
342 socklen_t len = sizeof(sockaddr);
343 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
344
345 if (new_connection < 0) {
346 perror("telnet accept failed");
347 return -1;
348 }
349
350
351 connection = (struct telnet_connection*)malloc(sizeof(*connection));
352 memset(connection, 0, sizeof(*connection));
353 connection->network = (struct gsm_network*)fd->data;
354 connection->fd.data = connection;
355 connection->fd.fd = new_connection;
356 connection->fd.when = BSC_FD_READ;
357 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000358 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000359 bsc_register_fd(&connection->fd);
360 llist_add_tail(&connection->entry, &active_connections);
361
362 print_welcome(new_connection);
363
364 return 0;
365}
Holger Freyther7aaf1122009-02-14 22:51:13 +0000366
367static int telnet_paging_callback(struct signal_data *signal, void *data)
368{
369 struct paging_signal_data *paging =
370 (struct paging_signal_data *) signal;
371 struct telnet_connection *con;
372
373 llist_for_each_entry(con, &active_connections, entry) {
374 if (paging->lchan) {
375 WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n");
376 show_lchan(con->fd.fd, paging->lchan);
377 } else {
378 WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n",
379 paging->subscr->imsi,
380 paging->subscr->tmsi,
381 paging->subscr->name);
382 }
383 }
384
385 return 0;
386}
Holger Freyther9b177762009-02-16 19:07:18 +0000387
388static int telnet_sms_callback(struct signal_data *signal, void *data)
389{
390 struct sms_signal_data *sms =
391 (struct sms_signal_data *) signal;
392 struct telnet_connection *con;
393
394 llist_for_each_entry(con, &active_connections, entry) {
395 WRITE_CONNECTION(con->fd.fd, "Incoming SMS: %s\n", sms->sms->user_data);
396 }
397
398 return 0;
399}