blob: 25f67a69d3fa375af249ca2ad328aa1c85b695f1 [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);
Harald Welte595ad7b2009-02-16 22:05:44 +000052static int telnet_paging_callback(unsigned int subsys, unsigned int signal,
53 void *handler_data, void *signal_data);
54static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
55 void *handler_data, void *signal_data);
Holger Freyther7aaf1122009-02-14 22:51:13 +000056
Holger Freyther219518d2009-01-02 22:04:43 +000057static struct bsc_fd server_socket = {
58 .when = BSC_FD_READ,
59 .cb = telnet_new_connection,
60 .priv_nr = 0,
61};
62
63void telnet_init(struct gsm_network *network, int port) {
64 struct sockaddr_in sock_addr;
Holger Freytherf0776892009-02-03 20:49:51 +000065 int fd, on = 1;
Holger Freyther219518d2009-01-02 22:04:43 +000066
67 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
68
69 if (fd < 0) {
70 perror("Telnet interface socket creation failed");
71 return;
72 }
73
Holger Freytherf0776892009-02-03 20:49:51 +000074 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
75
Holger Freyther219518d2009-01-02 22:04:43 +000076 memset(&sock_addr, 0, sizeof(sock_addr));
77 sock_addr.sin_family = AF_INET;
78 sock_addr.sin_port = htons(port);
79 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
80
81 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
82 perror("Telnet interface failed to bind");
83 return;
84 }
85
86 if (listen(fd, 0) < 0) {
87 perror("Telnet interface failed to listen");
88 return;
89 }
90
91 server_socket.data = network;
92 server_socket.fd = fd;
93 bsc_register_fd(&server_socket);
Holger Freyther7aaf1122009-02-14 22:51:13 +000094
Holger Freyther9b177762009-02-16 19:07:18 +000095 /* register callbacks */
Harald Welte595ad7b2009-02-16 22:05:44 +000096 register_signal_handler(SS_PAGING, telnet_paging_callback, network);
97 register_signal_handler(SS_SMS, telnet_sms_callback, network);
Holger Freyther219518d2009-01-02 22:04:43 +000098}
99
100void telnet_write_help(int fd) {
101 int ret;
102 static char *msg =
103 "Help for the ad-hoc telnet command line interface\n"
104 "The generic pattern is CMD LEN DATA\\n or just CMD\n"
105 "where CMD is one of the following:\n"
106 "help\n"
107 "page IMSI (type)\n"
108 "call IMSI (number)\n"
109 "get_channel IMSI Add use count on an active channel\n"
110 "put_channel IMSI Remove use count on an active channel\n"
Holger Freytherae61cae2009-01-04 03:46:01 +0000111 "show This will show the channel allocation\n"
Holger Freyther868b8cd2009-01-04 03:57:27 +0000112 "48 IMSI 0xAB 0xEF...Send GSM 04.08. proto and msg byte then data\n"
Holger Freyther219518d2009-01-02 22:04:43 +0000113 "11 IMSI 0xAB 0xEF...Send GSM 04.11\n";
114
115 ret = write(fd, msg, strlen(msg));
116}
117
118static void print_welcome(int fd) {
119 int ret;
120 static char *msg =
121 "Welcome to the OpenBSC Control interface\n"
122 "Copyright (C) 2008, 2009 Harald Welte\n"
123 "Contributions by Daniel Willmann, Jan Lübbe, "
124 "Stefan Schmidt, Holger Freyther\n\n"
125 "License GPLv2+: GNU GPL version 2 or later "
126 "<http://gnu.org/licenses/gpl.html>\n"
127 "This is free software: you are free to change "
128 "and redistribute it.\n"
129 "There is NO WARRANTY, to the extent permitted "
130 "by law.\nType \"help\" to get a short introduction.\n";
131
132 ret = write(fd, msg, strlen(msg));
133}
134
135int telnet_close_client(struct bsc_fd *fd) {
136 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
137
138 close(fd->fd);
139 bsc_unregister_fd(fd);
140 llist_del(&conn->entry);
141 free(conn);
142 return 0;
143}
144
145void telnet_error_client(int fd) {
146 int ret;
147 static char *msg = "Something went wrong. Please try again.\n";
148
149 printf("Error\n");
150 ret = write(fd, msg, strlen(msg));
151}
152
Holger Freytherf87573d2009-01-04 03:49:41 +0000153static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi,
154 const char **error, int fd) {
155 int ret;
156 struct gsm_lchan *lchan;
157 struct gsm_subscriber *subscr;
158
159 subscr = subscr_get_by_imsi(imsi);
160 if (!subscr) {
161 ret = write(fd, error[0], strlen(error[0]));
162 return NULL;
163 }
164
165 lchan = lchan_find(bts, subscr);
166 if (!lchan)
167 ret = write(fd, error[1], strlen(error[1]));
168
169 subscr_put(subscr);
170 return lchan;
171}
172
Holger Freyther7448a532009-01-04 20:18:23 +0000173void telnet_page(struct telnet_connection *connection, const char *imsi, int type) {
174 int ret;
175 static const char* error[] = {
176 "paging: IMSI not found\n",
177 "paging: No channel allocated for IMSI -> will allocate\n" };
178 struct gsm_bts *bts = &connection->network->bts[connection->bts];
179 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
180
181 if (lchan) {
182 static const char *msg = "paging: A Channel is already allocated.\n";
183 ret = write(connection->fd.fd, msg, strlen(msg));
184 return;
185 }
186
187 struct gsm_subscriber *subscr = subscr_get_by_imsi(imsi);
188 if (!subscr)
189 return;
190
Harald Welte09e38af2009-02-16 22:52:23 +0000191 paging_request(bts, subscr, type, NULL, NULL);
Harald Welte595ad7b2009-02-16 22:05:44 +0000192 paging_update_buffer_space(bts, 100);
Holger Freyther7448a532009-01-04 20:18:23 +0000193}
194
Holger Freyther219518d2009-01-02 22:04:43 +0000195void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000196 static const char* error[] = {
197 "put_channel: IMSI not found\n",
198 "put_channel: No channel allocated for IMSI\n" };
199 struct gsm_bts *bts = &connection->network->bts[connection->bts];
200 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
201
202 if (!lchan)
203 return;
204
205 put_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000206}
207
208void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000209 static const char* error[] = {
210 "get_channel: IMSI not found\n",
211 "get_channel: No channel allocated for IMSI\n" };
212 struct gsm_bts *bts = &connection->network->bts[connection->bts];
213 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
214
215 if (!lchan)
216 return;
217
218 use_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000219}
220
221void telnet_call(struct telnet_connection *connection, const char* imsi,
222 const char *origin) {
Holger Freyther3ae8fd22009-01-04 03:50:40 +0000223 static const char* error[] = {
224 "call: IMSI not found\n",
225 "call: No channel allocated for IMSI\n" };
226 struct gsm_bts *bts = &connection->network->bts[connection->bts];
227 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
228
229 if (!lchan)
230 return;
231
232 /* TODO: add the origin */
Harald Welte09e38af2009-02-16 22:52:23 +0000233 gsm48_cc_tx_setup(lchan, NULL);
Holger Freyther219518d2009-01-02 22:04:43 +0000234}
235
236void telnet_send_gsm_48(struct telnet_connection *connection) {
Holger Freyther868b8cd2009-01-04 03:57:27 +0000237 static const char* error[] = {
238 "48: IMSI not found\n",
239 "48: No channel allocated for IMSI\n" };
Holger Freyther5e76ce62009-01-04 20:15:12 +0000240 int ret;
Holger Freyther868b8cd2009-01-04 03:57:27 +0000241 struct gsm_bts *bts = &connection->network->bts[connection->bts];
242 struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd);
243
244 if (!lchan)
245 return;
246
Holger Freyther5e76ce62009-01-04 20:15:12 +0000247 if (connection->read < 2) {
248 static const char *msg = "48: Need at least two bytes";
249 ret = write(connection->fd.fd, msg, strlen(msg));
250 return;
251 }
252
Holger Freyther868b8cd2009-01-04 03:57:27 +0000253 struct msgb *msg = gsm48_msgb_alloc();
254 struct gsm48_hdr *gh;
255 int i;
256
257 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2);
258 msg->lchan = lchan;
259
260 gh->proto_discr = connection->commands[0];
261 gh->msg_type = connection->commands[1];
262 for (i = 2; i < connection->read; ++i)
263 gh->data[i-2] = connection->commands[i];
264
Holger Freyther4c8f1142009-01-04 20:17:07 +0000265 gsm48_sendmsg(msg);
Holger Freyther219518d2009-01-02 22:04:43 +0000266}
267
268void telnet_send_gsm_11(struct telnet_connection *connection) {
269 printf("sending gsm04.11 message\n");
270}
271
Holger Freytherae61cae2009-01-04 03:46:01 +0000272static void show_bts(int fd, struct gsm_bts *bts) {
273 WRITE_CONNECTION(fd,
274 "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
275 bts->nr, bts->bts_nr, bts->location_area_code,
276 bts->num_trx, bts->c0->arfcn, bts->c0->nr)
277}
278
279static void show_trx(int fd, struct gsm_bts_trx *trx) {
280 WRITE_CONNECTION(fd,
281 " TRX: %u ARFCN: %u\n",
282 trx->nr, trx->arfcn)
283}
284
285static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
286 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000287 " TS: #%u pchan: %12s flags: %u\n",
288 ts->nr, gsm_pchan_name(ts->pchan), ts->flags);
Holger Freytherae61cae2009-01-04 03:46:01 +0000289}
290
291static void show_lchan(int fd, struct gsm_lchan *lchan) {
292 struct gsm_subscriber *subscr = lchan->subscr;
293 WRITE_CONNECTION(fd,
Harald Welte626fe9c2009-01-09 21:24:53 +0000294 " LCHAN: #%u type: %7s count: %d subscriber: %s/%s/%s use: %d loc: %p\n",
Harald Welted4fc1b22009-01-04 16:11:31 +0000295 lchan->nr, gsm_lchan_name(lchan->type),
Harald Welte626fe9c2009-01-09 21:24:53 +0000296 lchan->use_count,
Holger Freytherae61cae2009-01-04 03:46:01 +0000297 subscr ? subscr->imsi : "na",
298 subscr ? subscr->tmsi : "na",
299 subscr ? subscr->name : "na",
300 lchan->use_count, lchan->loc_operation);
301}
302
303void telnet_list_channels(struct telnet_connection *connection) {
304 int bts_no, trx, lchan_no, ts_no;
305 struct gsm_network *network = connection->network;
306
307 for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
308 struct gsm_bts *bts = &network->bts[bts_no];
309 show_bts(connection->fd.fd, bts);
310
311 for (trx = 0; trx < bts->num_trx; ++trx) {
312 show_trx(connection->fd.fd, &bts->trx[trx]);
313 for (ts_no = 0; ts_no < 8; ++ts_no) {
314 show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
315 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
316 struct gsm_lchan *lchan =
317 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
318 show_lchan(connection->fd.fd, lchan);
319 }
320 }
321 }
322 }
323}
324
Holger Freyther219518d2009-01-02 22:04:43 +0000325static int client_data(struct bsc_fd *fd, unsigned int what) {
326 char buf[4096];
327 int ret;
328
329 ret = read(fd->fd, buf, sizeof(buf)-1);
330 buf[ret] = '\0';
331
332 /* connection is gone */
333 if (ret <= 0)
334 return telnet_close_client(fd);
335
336 /* time to parse. This code assumes that the input is line based */
337 telnet_parse((struct telnet_connection*)fd->data, buf);
338
339 return 0;
340}
341
342static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
343 struct telnet_connection *connection;
344 struct sockaddr_in sockaddr;
345 socklen_t len = sizeof(sockaddr);
346 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
347
348 if (new_connection < 0) {
349 perror("telnet accept failed");
350 return -1;
351 }
352
353
354 connection = (struct telnet_connection*)malloc(sizeof(*connection));
355 memset(connection, 0, sizeof(*connection));
356 connection->network = (struct gsm_network*)fd->data;
357 connection->fd.data = connection;
358 connection->fd.fd = new_connection;
359 connection->fd.when = BSC_FD_READ;
360 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000361 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000362 bsc_register_fd(&connection->fd);
363 llist_add_tail(&connection->entry, &active_connections);
364
365 print_welcome(new_connection);
366
367 return 0;
368}
Holger Freyther7aaf1122009-02-14 22:51:13 +0000369
Harald Welte595ad7b2009-02-16 22:05:44 +0000370static int telnet_paging_callback(unsigned int subsys, unsigned int singal,
371 void *handler_data, void *signal_data)
Holger Freyther7aaf1122009-02-14 22:51:13 +0000372{
Harald Welte595ad7b2009-02-16 22:05:44 +0000373 struct paging_signal_data *paging = signal_data;
Holger Freyther7aaf1122009-02-14 22:51:13 +0000374 struct telnet_connection *con;
375
376 llist_for_each_entry(con, &active_connections, entry) {
377 if (paging->lchan) {
378 WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n");
379 show_lchan(con->fd.fd, paging->lchan);
380 } else {
381 WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n",
382 paging->subscr->imsi,
383 paging->subscr->tmsi,
384 paging->subscr->name);
385 }
386 }
387
388 return 0;
389}
Holger Freyther9b177762009-02-16 19:07:18 +0000390
Harald Welte595ad7b2009-02-16 22:05:44 +0000391static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
392 void *handler_data, void *signal_data)
Holger Freyther9b177762009-02-16 19:07:18 +0000393{
Harald Welte595ad7b2009-02-16 22:05:44 +0000394 struct sms_submit *sms = signal_data;
Holger Freyther9b177762009-02-16 19:07:18 +0000395 struct telnet_connection *con;
396
397 llist_for_each_entry(con, &active_connections, entry) {
Harald Welte595ad7b2009-02-16 22:05:44 +0000398 WRITE_CONNECTION(con->fd.fd, "Incoming SMS: %s\n", sms->user_data);
Holger Freyther9b177762009-02-16 19:07:18 +0000399 }
400
401 return 0;
402}