blob: e3f56c2c2dccf06f3e02d9e3141351f730b757a4 [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 Freyther7aaf1122009-02-14 22:51:13 +000035#include <openbsc/signal.h>
Holger Freyther219518d2009-01-02 22:04:43 +000036
37extern void telnet_parse(struct telnet_connection *connection, char *line);
38
Holger Freytherae61cae2009-01-04 03:46:01 +000039#define WRITE_CONNECTION(fd, msg...) \
40 int ret; \
41 char buf[4096]; \
42 snprintf(buf, sizeof(buf), msg); \
43 ret = write(fd, buf, strlen(buf));
44
45
Holger Freyther219518d2009-01-02 22:04:43 +000046/* per connection data */
47LLIST_HEAD(active_connections);
48
49/* per network data */
50static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
Holger Freyther7aaf1122009-02-14 22:51:13 +000051static int telnet_paging_callback(struct signal_data *signal, void *data);
52
Holger Freyther219518d2009-01-02 22:04:43 +000053static struct bsc_fd server_socket = {
54 .when = BSC_FD_READ,
55 .cb = telnet_new_connection,
56 .priv_nr = 0,
57};
58
59void telnet_init(struct gsm_network *network, int port) {
60 struct sockaddr_in sock_addr;
Holger Freytherf0776892009-02-03 20:49:51 +000061 int fd, on = 1;
Holger Freyther219518d2009-01-02 22:04:43 +000062
63 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
64
65 if (fd < 0) {
66 perror("Telnet interface socket creation failed");
67 return;
68 }
69
Holger Freytherf0776892009-02-03 20:49:51 +000070 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
71
Holger Freyther219518d2009-01-02 22:04:43 +000072 memset(&sock_addr, 0, sizeof(sock_addr));
73 sock_addr.sin_family = AF_INET;
74 sock_addr.sin_port = htons(port);
75 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
76
77 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
78 perror("Telnet interface failed to bind");
79 return;
80 }
81
82 if (listen(fd, 0) < 0) {
83 perror("Telnet interface failed to listen");
84 return;
85 }
86
87 server_socket.data = network;
88 server_socket.fd = fd;
89 bsc_register_fd(&server_socket);
Holger Freyther7aaf1122009-02-14 22:51:13 +000090
91 /* register paging callbacks */
92 register_signal_handler(S_PAGING, telnet_paging_callback, network);
Holger Freyther219518d2009-01-02 22:04:43 +000093}
94
95void telnet_write_help(int fd) {
96 int ret;
97 static char *msg =
98 "Help for the ad-hoc telnet command line interface\n"
99 "The generic pattern is CMD LEN DATA\\n or just CMD\n"
100 "where CMD is one of the following:\n"
101 "help\n"
102 "page IMSI (type)\n"
103 "call IMSI (number)\n"
104 "get_channel IMSI Add use count on an active channel\n"
105 "put_channel IMSI Remove use count on an active channel\n"
Holger Freytherae61cae2009-01-04 03:46:01 +0000106 "show This will show the channel allocation\n"
Holger Freyther868b8cd2009-01-04 03:57:27 +0000107 "48 IMSI 0xAB 0xEF...Send GSM 04.08. proto and msg byte then data\n"
Holger Freyther219518d2009-01-02 22:04:43 +0000108 "11 IMSI 0xAB 0xEF...Send GSM 04.11\n";
109
110 ret = write(fd, msg, strlen(msg));
111}
112
113static void print_welcome(int fd) {
114 int ret;
115 static char *msg =
116 "Welcome to the OpenBSC Control interface\n"
117 "Copyright (C) 2008, 2009 Harald Welte\n"
118 "Contributions by Daniel Willmann, Jan Lübbe, "
119 "Stefan Schmidt, Holger Freyther\n\n"
120 "License GPLv2+: GNU GPL version 2 or later "
121 "<http://gnu.org/licenses/gpl.html>\n"
122 "This is free software: you are free to change "
123 "and redistribute it.\n"
124 "There is NO WARRANTY, to the extent permitted "
125 "by law.\nType \"help\" to get a short introduction.\n";
126
127 ret = write(fd, msg, strlen(msg));
128}
129
130int telnet_close_client(struct bsc_fd *fd) {
131 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
132
133 close(fd->fd);
134 bsc_unregister_fd(fd);
135 llist_del(&conn->entry);
136 free(conn);
137 return 0;
138}
139
140void telnet_error_client(int fd) {
141 int ret;
142 static char *msg = "Something went wrong. Please try again.\n";
143
144 printf("Error\n");
145 ret = write(fd, msg, strlen(msg));
146}
147
Holger Freytherf87573d2009-01-04 03:49:41 +0000148static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi,
149 const char **error, int fd) {
150 int ret;
151 struct gsm_lchan *lchan;
152 struct gsm_subscriber *subscr;
153
154 subscr = subscr_get_by_imsi(imsi);
155 if (!subscr) {
156 ret = write(fd, error[0], strlen(error[0]));
157 return NULL;
158 }
159
160 lchan = lchan_find(bts, subscr);
161 if (!lchan)
162 ret = write(fd, error[1], strlen(error[1]));
163
164 subscr_put(subscr);
165 return lchan;
166}
167
Holger Freyther7448a532009-01-04 20:18:23 +0000168void telnet_page(struct telnet_connection *connection, const char *imsi, int type) {
169 int ret;
170 static const char* error[] = {
171 "paging: IMSI not found\n",
172 "paging: No channel allocated for IMSI -> will allocate\n" };
173 struct gsm_bts *bts = &connection->network->bts[connection->bts];
174 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
175
176 if (lchan) {
177 static const char *msg = "paging: A Channel is already allocated.\n";
178 ret = write(connection->fd.fd, msg, strlen(msg));
179 return;
180 }
181
182 struct gsm_subscriber *subscr = subscr_get_by_imsi(imsi);
183 if (!subscr)
184 return;
185
Holger Freyther1fd34142009-02-09 23:42:03 +0000186 paging_request(bts, subscr, type);
Holger Freyther7448a532009-01-04 20:18:23 +0000187}
188
Holger Freyther219518d2009-01-02 22:04:43 +0000189void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000190 static const char* error[] = {
191 "put_channel: IMSI not found\n",
192 "put_channel: No channel allocated for IMSI\n" };
193 struct gsm_bts *bts = &connection->network->bts[connection->bts];
194 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
195
196 if (!lchan)
197 return;
198
199 put_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000200}
201
202void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000203 static const char* error[] = {
204 "get_channel: IMSI not found\n",
205 "get_channel: No channel allocated for IMSI\n" };
206 struct gsm_bts *bts = &connection->network->bts[connection->bts];
207 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
208
209 if (!lchan)
210 return;
211
212 use_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000213}
214
215void telnet_call(struct telnet_connection *connection, const char* imsi,
216 const char *origin) {
Holger Freyther3ae8fd22009-01-04 03:50:40 +0000217 static const char* error[] = {
218 "call: IMSI not found\n",
219 "call: No channel allocated for IMSI\n" };
220 struct gsm_bts *bts = &connection->network->bts[connection->bts];
221 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
222
223 if (!lchan)
224 return;
225
226 /* TODO: add the origin */
227 gsm48_cc_tx_setup(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000228}
229
230void telnet_send_gsm_48(struct telnet_connection *connection) {
Holger Freyther868b8cd2009-01-04 03:57:27 +0000231 static const char* error[] = {
232 "48: IMSI not found\n",
233 "48: No channel allocated for IMSI\n" };
Holger Freyther5e76ce62009-01-04 20:15:12 +0000234 int ret;
Holger Freyther868b8cd2009-01-04 03:57:27 +0000235 struct gsm_bts *bts = &connection->network->bts[connection->bts];
236 struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd);
237
238 if (!lchan)
239 return;
240
Holger Freyther5e76ce62009-01-04 20:15:12 +0000241 if (connection->read < 2) {
242 static const char *msg = "48: Need at least two bytes";
243 ret = write(connection->fd.fd, msg, strlen(msg));
244 return;
245 }
246
Holger Freyther868b8cd2009-01-04 03:57:27 +0000247 struct msgb *msg = gsm48_msgb_alloc();
248 struct gsm48_hdr *gh;
249 int i;
250
251 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2);
252 msg->lchan = lchan;
253
254 gh->proto_discr = connection->commands[0];
255 gh->msg_type = connection->commands[1];
256 for (i = 2; i < connection->read; ++i)
257 gh->data[i-2] = connection->commands[i];
258
Holger Freyther4c8f1142009-01-04 20:17:07 +0000259 gsm48_sendmsg(msg);
Holger Freyther219518d2009-01-02 22:04:43 +0000260}
261
262void telnet_send_gsm_11(struct telnet_connection *connection) {
263 printf("sending gsm04.11 message\n");
264}
265
Holger Freytherae61cae2009-01-04 03:46:01 +0000266static void show_bts(int fd, struct gsm_bts *bts) {
267 WRITE_CONNECTION(fd,
268 "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
269 bts->nr, bts->bts_nr, bts->location_area_code,
270 bts->num_trx, bts->c0->arfcn, bts->c0->nr)
271}
272
273static void show_trx(int fd, struct gsm_bts_trx *trx) {
274 WRITE_CONNECTION(fd,
275 " TRX: %u ARFCN: %u\n",
276 trx->nr, trx->arfcn)
277}
278
279static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
280 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000281 " TS: #%u pchan: %12s flags: %u\n",
282 ts->nr, gsm_pchan_name(ts->pchan), ts->flags);
Holger Freytherae61cae2009-01-04 03:46:01 +0000283}
284
285static void show_lchan(int fd, struct gsm_lchan *lchan) {
286 struct gsm_subscriber *subscr = lchan->subscr;
287 WRITE_CONNECTION(fd,
Harald Welte626fe9c2009-01-09 21:24:53 +0000288 " LCHAN: #%u type: %7s count: %d subscriber: %s/%s/%s use: %d loc: %p\n",
Harald Welted4fc1b22009-01-04 16:11:31 +0000289 lchan->nr, gsm_lchan_name(lchan->type),
Harald Welte626fe9c2009-01-09 21:24:53 +0000290 lchan->use_count,
Holger Freytherae61cae2009-01-04 03:46:01 +0000291 subscr ? subscr->imsi : "na",
292 subscr ? subscr->tmsi : "na",
293 subscr ? subscr->name : "na",
294 lchan->use_count, lchan->loc_operation);
295}
296
297void telnet_list_channels(struct telnet_connection *connection) {
298 int bts_no, trx, lchan_no, ts_no;
299 struct gsm_network *network = connection->network;
300
301 for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
302 struct gsm_bts *bts = &network->bts[bts_no];
303 show_bts(connection->fd.fd, bts);
304
305 for (trx = 0; trx < bts->num_trx; ++trx) {
306 show_trx(connection->fd.fd, &bts->trx[trx]);
307 for (ts_no = 0; ts_no < 8; ++ts_no) {
308 show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
309 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
310 struct gsm_lchan *lchan =
311 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
312 show_lchan(connection->fd.fd, lchan);
313 }
314 }
315 }
316 }
317}
318
Holger Freyther219518d2009-01-02 22:04:43 +0000319static int client_data(struct bsc_fd *fd, unsigned int what) {
320 char buf[4096];
321 int ret;
322
323 ret = read(fd->fd, buf, sizeof(buf)-1);
324 buf[ret] = '\0';
325
326 /* connection is gone */
327 if (ret <= 0)
328 return telnet_close_client(fd);
329
330 /* time to parse. This code assumes that the input is line based */
331 telnet_parse((struct telnet_connection*)fd->data, buf);
332
333 return 0;
334}
335
336static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
337 struct telnet_connection *connection;
338 struct sockaddr_in sockaddr;
339 socklen_t len = sizeof(sockaddr);
340 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
341
342 if (new_connection < 0) {
343 perror("telnet accept failed");
344 return -1;
345 }
346
347
348 connection = (struct telnet_connection*)malloc(sizeof(*connection));
349 memset(connection, 0, sizeof(*connection));
350 connection->network = (struct gsm_network*)fd->data;
351 connection->fd.data = connection;
352 connection->fd.fd = new_connection;
353 connection->fd.when = BSC_FD_READ;
354 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000355 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000356 bsc_register_fd(&connection->fd);
357 llist_add_tail(&connection->entry, &active_connections);
358
359 print_welcome(new_connection);
360
361 return 0;
362}
Holger Freyther7aaf1122009-02-14 22:51:13 +0000363
364static int telnet_paging_callback(struct signal_data *signal, void *data)
365{
366 struct paging_signal_data *paging =
367 (struct paging_signal_data *) signal;
368 struct telnet_connection *con;
369
370 llist_for_each_entry(con, &active_connections, entry) {
371 if (paging->lchan) {
372 WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n");
373 show_lchan(con->fd.fd, paging->lchan);
374 } else {
375 WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n",
376 paging->subscr->imsi,
377 paging->subscr->tmsi,
378 paging->subscr->name);
379 }
380 }
381
382 return 0;
383}