blob: 3d3ed7663a0bfab12f36b05f0cb950cc8434a3aa [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);
Holger Freyther7448a532009-01-04 20:18:23 +0000192}
193
Holger Freyther219518d2009-01-02 22:04:43 +0000194void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000195 static const char* error[] = {
196 "put_channel: IMSI not found\n",
197 "put_channel: No channel allocated for IMSI\n" };
198 struct gsm_bts *bts = &connection->network->bts[connection->bts];
199 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
200
201 if (!lchan)
202 return;
203
204 put_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000205}
206
207void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000208 static const char* error[] = {
209 "get_channel: IMSI not found\n",
210 "get_channel: No channel allocated for IMSI\n" };
211 struct gsm_bts *bts = &connection->network->bts[connection->bts];
212 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
213
214 if (!lchan)
215 return;
216
217 use_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000218}
219
220void telnet_call(struct telnet_connection *connection, const char* imsi,
221 const char *origin) {
Holger Freyther3ae8fd22009-01-04 03:50:40 +0000222 static const char* error[] = {
223 "call: IMSI not found\n",
224 "call: No channel allocated for IMSI\n" };
225 struct gsm_bts *bts = &connection->network->bts[connection->bts];
226 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
227
228 if (!lchan)
229 return;
230
231 /* TODO: add the origin */
Harald Welte49f48b82009-02-17 15:29:33 +0000232 gsm48_cc_tx_setup(lchan, NULL);
Holger Freyther219518d2009-01-02 22:04:43 +0000233}
234
235void telnet_send_gsm_48(struct telnet_connection *connection) {
Holger Freyther868b8cd2009-01-04 03:57:27 +0000236 static const char* error[] = {
237 "48: IMSI not found\n",
238 "48: No channel allocated for IMSI\n" };
Holger Freyther5e76ce62009-01-04 20:15:12 +0000239 int ret;
Holger Freyther868b8cd2009-01-04 03:57:27 +0000240 struct gsm_bts *bts = &connection->network->bts[connection->bts];
241 struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd);
242
243 if (!lchan)
244 return;
245
Holger Freyther5e76ce62009-01-04 20:15:12 +0000246 if (connection->read < 2) {
247 static const char *msg = "48: Need at least two bytes";
248 ret = write(connection->fd.fd, msg, strlen(msg));
249 return;
250 }
251
Holger Freyther868b8cd2009-01-04 03:57:27 +0000252 struct msgb *msg = gsm48_msgb_alloc();
253 struct gsm48_hdr *gh;
254 int i;
255
256 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2);
257 msg->lchan = lchan;
258
259 gh->proto_discr = connection->commands[0];
260 gh->msg_type = connection->commands[1];
261 for (i = 2; i < connection->read; ++i)
262 gh->data[i-2] = connection->commands[i];
263
Holger Freyther4c8f1142009-01-04 20:17:07 +0000264 gsm48_sendmsg(msg);
Holger Freyther219518d2009-01-02 22:04:43 +0000265}
266
267void telnet_send_gsm_11(struct telnet_connection *connection) {
268 printf("sending gsm04.11 message\n");
269}
270
Holger Freytherae61cae2009-01-04 03:46:01 +0000271static void show_bts(int fd, struct gsm_bts *bts) {
272 WRITE_CONNECTION(fd,
273 "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
274 bts->nr, bts->bts_nr, bts->location_area_code,
275 bts->num_trx, bts->c0->arfcn, bts->c0->nr)
276}
277
278static void show_trx(int fd, struct gsm_bts_trx *trx) {
279 WRITE_CONNECTION(fd,
280 " TRX: %u ARFCN: %u\n",
281 trx->nr, trx->arfcn)
282}
283
284static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
285 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000286 " TS: #%u pchan: %12s flags: %u\n",
287 ts->nr, gsm_pchan_name(ts->pchan), ts->flags);
Holger Freytherae61cae2009-01-04 03:46:01 +0000288}
289
290static void show_lchan(int fd, struct gsm_lchan *lchan) {
291 struct gsm_subscriber *subscr = lchan->subscr;
292 WRITE_CONNECTION(fd,
Harald Welte626fe9c2009-01-09 21:24:53 +0000293 " LCHAN: #%u type: %7s count: %d subscriber: %s/%s/%s use: %d loc: %p\n",
Harald Welted4fc1b22009-01-04 16:11:31 +0000294 lchan->nr, gsm_lchan_name(lchan->type),
Harald Welte626fe9c2009-01-09 21:24:53 +0000295 lchan->use_count,
Holger Freytherae61cae2009-01-04 03:46:01 +0000296 subscr ? subscr->imsi : "na",
297 subscr ? subscr->tmsi : "na",
298 subscr ? subscr->name : "na",
299 lchan->use_count, lchan->loc_operation);
300}
301
302void telnet_list_channels(struct telnet_connection *connection) {
303 int bts_no, trx, lchan_no, ts_no;
304 struct gsm_network *network = connection->network;
305
306 for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
307 struct gsm_bts *bts = &network->bts[bts_no];
308 show_bts(connection->fd.fd, bts);
309
310 for (trx = 0; trx < bts->num_trx; ++trx) {
311 show_trx(connection->fd.fd, &bts->trx[trx]);
312 for (ts_no = 0; ts_no < 8; ++ts_no) {
313 show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
314 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
315 struct gsm_lchan *lchan =
316 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
317 show_lchan(connection->fd.fd, lchan);
318 }
319 }
320 }
321 }
322}
323
Holger Freyther219518d2009-01-02 22:04:43 +0000324static int client_data(struct bsc_fd *fd, unsigned int what) {
325 char buf[4096];
326 int ret;
327
328 ret = read(fd->fd, buf, sizeof(buf)-1);
329 buf[ret] = '\0';
330
331 /* connection is gone */
332 if (ret <= 0)
333 return telnet_close_client(fd);
334
335 /* time to parse. This code assumes that the input is line based */
336 telnet_parse((struct telnet_connection*)fd->data, buf);
337
338 return 0;
339}
340
341static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
342 struct telnet_connection *connection;
343 struct sockaddr_in sockaddr;
344 socklen_t len = sizeof(sockaddr);
345 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
346
347 if (new_connection < 0) {
348 perror("telnet accept failed");
349 return -1;
350 }
351
352
353 connection = (struct telnet_connection*)malloc(sizeof(*connection));
354 memset(connection, 0, sizeof(*connection));
355 connection->network = (struct gsm_network*)fd->data;
356 connection->fd.data = connection;
357 connection->fd.fd = new_connection;
358 connection->fd.when = BSC_FD_READ;
359 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000360 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000361 bsc_register_fd(&connection->fd);
362 llist_add_tail(&connection->entry, &active_connections);
363
364 print_welcome(new_connection);
365
366 return 0;
367}
Holger Freyther7aaf1122009-02-14 22:51:13 +0000368
Harald Welte595ad7b2009-02-16 22:05:44 +0000369static int telnet_paging_callback(unsigned int subsys, unsigned int singal,
370 void *handler_data, void *signal_data)
Holger Freyther7aaf1122009-02-14 22:51:13 +0000371{
Harald Welte595ad7b2009-02-16 22:05:44 +0000372 struct paging_signal_data *paging = signal_data;
Holger Freyther7aaf1122009-02-14 22:51:13 +0000373 struct telnet_connection *con;
374
375 llist_for_each_entry(con, &active_connections, entry) {
376 if (paging->lchan) {
377 WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n");
378 show_lchan(con->fd.fd, paging->lchan);
379 } else {
380 WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n",
381 paging->subscr->imsi,
382 paging->subscr->tmsi,
383 paging->subscr->name);
384 }
385 }
386
387 return 0;
388}
Holger Freyther9b177762009-02-16 19:07:18 +0000389
Harald Welte595ad7b2009-02-16 22:05:44 +0000390static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
391 void *handler_data, void *signal_data)
Holger Freyther9b177762009-02-16 19:07:18 +0000392{
Harald Welte595ad7b2009-02-16 22:05:44 +0000393 struct sms_submit *sms = signal_data;
Holger Freyther9b177762009-02-16 19:07:18 +0000394 struct telnet_connection *con;
395
396 llist_for_each_entry(con, &active_connections, entry) {
Harald Welte595ad7b2009-02-16 22:05:44 +0000397 WRITE_CONNECTION(con->fd.fd, "Incoming SMS: %s\n", sms->user_data);
Holger Freyther9b177762009-02-16 19:07:18 +0000398 }
399
400 return 0;
401}