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