blob: 7da53fda58b99d7182b78c4587ae4de71ec14a5d [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>
Holger Freyther219518d2009-01-02 22:04:43 +000034
35extern void telnet_parse(struct telnet_connection *connection, char *line);
36
Holger Freytherae61cae2009-01-04 03:46:01 +000037#define WRITE_CONNECTION(fd, msg...) \
38 int ret; \
39 char buf[4096]; \
40 snprintf(buf, sizeof(buf), msg); \
41 ret = write(fd, buf, strlen(buf));
42
43
Holger Freyther219518d2009-01-02 22:04:43 +000044/* per connection data */
45LLIST_HEAD(active_connections);
46
47/* per network data */
48static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
49static struct bsc_fd server_socket = {
50 .when = BSC_FD_READ,
51 .cb = telnet_new_connection,
52 .priv_nr = 0,
53};
54
55void telnet_init(struct gsm_network *network, int port) {
56 struct sockaddr_in sock_addr;
57 int fd;
58
59 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
60
61 if (fd < 0) {
62 perror("Telnet interface socket creation failed");
63 return;
64 }
65
66 memset(&sock_addr, 0, sizeof(sock_addr));
67 sock_addr.sin_family = AF_INET;
68 sock_addr.sin_port = htons(port);
69 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
70
71 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
72 perror("Telnet interface failed to bind");
73 return;
74 }
75
76 if (listen(fd, 0) < 0) {
77 perror("Telnet interface failed to listen");
78 return;
79 }
80
81 server_socket.data = network;
82 server_socket.fd = fd;
83 bsc_register_fd(&server_socket);
84}
85
86void telnet_write_help(int fd) {
87 int ret;
88 static char *msg =
89 "Help for the ad-hoc telnet command line interface\n"
90 "The generic pattern is CMD LEN DATA\\n or just CMD\n"
91 "where CMD is one of the following:\n"
92 "help\n"
93 "page IMSI (type)\n"
94 "call IMSI (number)\n"
95 "get_channel IMSI Add use count on an active channel\n"
96 "put_channel IMSI Remove use count on an active channel\n"
Holger Freytherae61cae2009-01-04 03:46:01 +000097 "show This will show the channel allocation\n"
Holger Freyther868b8cd2009-01-04 03:57:27 +000098 "48 IMSI 0xAB 0xEF...Send GSM 04.08. proto and msg byte then data\n"
Holger Freyther219518d2009-01-02 22:04:43 +000099 "11 IMSI 0xAB 0xEF...Send GSM 04.11\n";
100
101 ret = write(fd, msg, strlen(msg));
102}
103
104static void print_welcome(int fd) {
105 int ret;
106 static char *msg =
107 "Welcome to the OpenBSC Control interface\n"
108 "Copyright (C) 2008, 2009 Harald Welte\n"
109 "Contributions by Daniel Willmann, Jan Lübbe, "
110 "Stefan Schmidt, Holger Freyther\n\n"
111 "License GPLv2+: GNU GPL version 2 or later "
112 "<http://gnu.org/licenses/gpl.html>\n"
113 "This is free software: you are free to change "
114 "and redistribute it.\n"
115 "There is NO WARRANTY, to the extent permitted "
116 "by law.\nType \"help\" to get a short introduction.\n";
117
118 ret = write(fd, msg, strlen(msg));
119}
120
121int telnet_close_client(struct bsc_fd *fd) {
122 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
123
124 close(fd->fd);
125 bsc_unregister_fd(fd);
126 llist_del(&conn->entry);
127 free(conn);
128 return 0;
129}
130
131void telnet_error_client(int fd) {
132 int ret;
133 static char *msg = "Something went wrong. Please try again.\n";
134
135 printf("Error\n");
136 ret = write(fd, msg, strlen(msg));
137}
138
Holger Freytherf87573d2009-01-04 03:49:41 +0000139static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi,
140 const char **error, int fd) {
141 int ret;
142 struct gsm_lchan *lchan;
143 struct gsm_subscriber *subscr;
144
145 subscr = subscr_get_by_imsi(imsi);
146 if (!subscr) {
147 ret = write(fd, error[0], strlen(error[0]));
148 return NULL;
149 }
150
151 lchan = lchan_find(bts, subscr);
152 if (!lchan)
153 ret = write(fd, error[1], strlen(error[1]));
154
155 subscr_put(subscr);
156 return lchan;
157}
158
Holger Freyther7448a532009-01-04 20:18:23 +0000159void telnet_page(struct telnet_connection *connection, const char *imsi, int type) {
160 int ret;
161 static const char* error[] = {
162 "paging: IMSI not found\n",
163 "paging: No channel allocated for IMSI -> will allocate\n" };
164 struct gsm_bts *bts = &connection->network->bts[connection->bts];
165 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
166
167 if (lchan) {
168 static const char *msg = "paging: A Channel is already allocated.\n";
169 ret = write(connection->fd.fd, msg, strlen(msg));
170 return;
171 }
172
173 struct gsm_subscriber *subscr = subscr_get_by_imsi(imsi);
174 if (!subscr)
175 return;
176
177 rsl_paging_cmd_subscr(bts, type, subscr);
178}
179
Holger Freyther219518d2009-01-02 22:04:43 +0000180void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000181 static const char* error[] = {
182 "put_channel: IMSI not found\n",
183 "put_channel: No channel allocated for IMSI\n" };
184 struct gsm_bts *bts = &connection->network->bts[connection->bts];
185 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
186
187 if (!lchan)
188 return;
189
190 put_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000191}
192
193void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000194 static const char* error[] = {
195 "get_channel: IMSI not found\n",
196 "get_channel: No channel allocated for IMSI\n" };
197 struct gsm_bts *bts = &connection->network->bts[connection->bts];
198 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
199
200 if (!lchan)
201 return;
202
203 use_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000204}
205
206void telnet_call(struct telnet_connection *connection, const char* imsi,
207 const char *origin) {
Holger Freyther3ae8fd22009-01-04 03:50:40 +0000208 static const char* error[] = {
209 "call: IMSI not found\n",
210 "call: 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 /* TODO: add the origin */
218 gsm48_cc_tx_setup(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000219}
220
221void telnet_send_gsm_48(struct telnet_connection *connection) {
Holger Freyther868b8cd2009-01-04 03:57:27 +0000222 static const char* error[] = {
223 "48: IMSI not found\n",
224 "48: No channel allocated for IMSI\n" };
Holger Freyther5e76ce62009-01-04 20:15:12 +0000225 int ret;
Holger Freyther868b8cd2009-01-04 03:57:27 +0000226 struct gsm_bts *bts = &connection->network->bts[connection->bts];
227 struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd);
228
229 if (!lchan)
230 return;
231
Holger Freyther5e76ce62009-01-04 20:15:12 +0000232 if (connection->read < 2) {
233 static const char *msg = "48: Need at least two bytes";
234 ret = write(connection->fd.fd, msg, strlen(msg));
235 return;
236 }
237
Holger Freyther868b8cd2009-01-04 03:57:27 +0000238 struct msgb *msg = gsm48_msgb_alloc();
239 struct gsm48_hdr *gh;
240 int i;
241
242 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2);
243 msg->lchan = lchan;
244
245 gh->proto_discr = connection->commands[0];
246 gh->msg_type = connection->commands[1];
247 for (i = 2; i < connection->read; ++i)
248 gh->data[i-2] = connection->commands[i];
249
Holger Freyther4c8f1142009-01-04 20:17:07 +0000250 gsm48_sendmsg(msg);
Holger Freyther219518d2009-01-02 22:04:43 +0000251}
252
253void telnet_send_gsm_11(struct telnet_connection *connection) {
254 printf("sending gsm04.11 message\n");
255}
256
Holger Freytherae61cae2009-01-04 03:46:01 +0000257static void show_bts(int fd, struct gsm_bts *bts) {
258 WRITE_CONNECTION(fd,
259 "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
260 bts->nr, bts->bts_nr, bts->location_area_code,
261 bts->num_trx, bts->c0->arfcn, bts->c0->nr)
262}
263
264static void show_trx(int fd, struct gsm_bts_trx *trx) {
265 WRITE_CONNECTION(fd,
266 " TRX: %u ARFCN: %u\n",
267 trx->nr, trx->arfcn)
268}
269
270static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
271 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000272 " TS: #%u pchan: %12s flags: %u\n",
273 ts->nr, gsm_pchan_name(ts->pchan), ts->flags);
Holger Freytherae61cae2009-01-04 03:46:01 +0000274}
275
276static void show_lchan(int fd, struct gsm_lchan *lchan) {
277 struct gsm_subscriber *subscr = lchan->subscr;
278 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000279 " LCHAN: #%u type: %7s subscriber: %s/%s/%s use: %d loc: %p\n",
280 lchan->nr, gsm_lchan_name(lchan->type),
Holger Freytherae61cae2009-01-04 03:46:01 +0000281 subscr ? subscr->imsi : "na",
282 subscr ? subscr->tmsi : "na",
283 subscr ? subscr->name : "na",
284 lchan->use_count, lchan->loc_operation);
285}
286
287void telnet_list_channels(struct telnet_connection *connection) {
288 int bts_no, trx, lchan_no, ts_no;
289 struct gsm_network *network = connection->network;
290
291 for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
292 struct gsm_bts *bts = &network->bts[bts_no];
293 show_bts(connection->fd.fd, bts);
294
295 for (trx = 0; trx < bts->num_trx; ++trx) {
296 show_trx(connection->fd.fd, &bts->trx[trx]);
297 for (ts_no = 0; ts_no < 8; ++ts_no) {
298 show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
299 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
300 struct gsm_lchan *lchan =
301 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
302 show_lchan(connection->fd.fd, lchan);
303 }
304 }
305 }
306 }
307}
308
Holger Freyther219518d2009-01-02 22:04:43 +0000309static int client_data(struct bsc_fd *fd, unsigned int what) {
310 char buf[4096];
311 int ret;
312
313 ret = read(fd->fd, buf, sizeof(buf)-1);
314 buf[ret] = '\0';
315
316 /* connection is gone */
317 if (ret <= 0)
318 return telnet_close_client(fd);
319
320 /* time to parse. This code assumes that the input is line based */
321 telnet_parse((struct telnet_connection*)fd->data, buf);
322
323 return 0;
324}
325
326static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
327 struct telnet_connection *connection;
328 struct sockaddr_in sockaddr;
329 socklen_t len = sizeof(sockaddr);
330 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
331
332 if (new_connection < 0) {
333 perror("telnet accept failed");
334 return -1;
335 }
336
337
338 connection = (struct telnet_connection*)malloc(sizeof(*connection));
339 memset(connection, 0, sizeof(*connection));
340 connection->network = (struct gsm_network*)fd->data;
341 connection->fd.data = connection;
342 connection->fd.fd = new_connection;
343 connection->fd.when = BSC_FD_READ;
344 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000345 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000346 bsc_register_fd(&connection->fd);
347 llist_add_tail(&connection->entry, &active_connections);
348
349 print_welcome(new_connection);
350
351 return 0;
352}