2009-01-02 22:04:43 +00:00
|
|
|
/* minimalistic telnet/network interface it might turn into a wire interface */
|
|
|
|
/* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <openbsc/telnet_interface.h>
|
2009-01-04 03:46:01 +00:00
|
|
|
#include <openbsc/gsm_subscriber.h>
|
2009-01-04 03:49:41 +00:00
|
|
|
#include <openbsc/chan_alloc.h>
|
2009-01-04 03:50:40 +00:00
|
|
|
#include <openbsc/gsm_04_08.h>
|
2009-02-16 19:07:18 +00:00
|
|
|
#include <openbsc/gsm_04_11.h>
|
2009-01-04 03:57:27 +00:00
|
|
|
#include <openbsc/msgb.h>
|
2009-01-04 20:18:23 +00:00
|
|
|
#include <openbsc/abis_rsl.h>
|
2009-01-06 23:10:57 +00:00
|
|
|
#include <openbsc/paging.h>
|
2009-02-14 22:51:13 +00:00
|
|
|
#include <openbsc/signal.h>
|
2009-01-02 22:04:43 +00:00
|
|
|
|
|
|
|
extern void telnet_parse(struct telnet_connection *connection, char *line);
|
|
|
|
|
2009-01-04 03:46:01 +00:00
|
|
|
#define WRITE_CONNECTION(fd, msg...) \
|
|
|
|
int ret; \
|
|
|
|
char buf[4096]; \
|
|
|
|
snprintf(buf, sizeof(buf), msg); \
|
|
|
|
ret = write(fd, buf, strlen(buf));
|
|
|
|
|
|
|
|
|
2009-01-02 22:04:43 +00:00
|
|
|
/* per connection data */
|
|
|
|
LLIST_HEAD(active_connections);
|
|
|
|
|
|
|
|
/* per network data */
|
|
|
|
static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
|
2009-02-16 22:05:44 +00:00
|
|
|
static int telnet_paging_callback(unsigned int subsys, unsigned int signal,
|
|
|
|
void *handler_data, void *signal_data);
|
|
|
|
static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
|
|
|
|
void *handler_data, void *signal_data);
|
2009-02-14 22:51:13 +00:00
|
|
|
|
2009-01-02 22:04:43 +00:00
|
|
|
static struct bsc_fd server_socket = {
|
|
|
|
.when = BSC_FD_READ,
|
|
|
|
.cb = telnet_new_connection,
|
|
|
|
.priv_nr = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
void telnet_init(struct gsm_network *network, int port) {
|
|
|
|
struct sockaddr_in sock_addr;
|
2009-02-03 20:49:51 +00:00
|
|
|
int fd, on = 1;
|
2009-01-02 22:04:43 +00:00
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("Telnet interface socket creation failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-03 20:49:51 +00:00
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
|
|
|
|
2009-01-02 22:04:43 +00:00
|
|
|
memset(&sock_addr, 0, sizeof(sock_addr));
|
|
|
|
sock_addr.sin_family = AF_INET;
|
|
|
|
sock_addr.sin_port = htons(port);
|
|
|
|
sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
|
|
|
|
if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
|
|
|
|
perror("Telnet interface failed to bind");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(fd, 0) < 0) {
|
|
|
|
perror("Telnet interface failed to listen");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_socket.data = network;
|
|
|
|
server_socket.fd = fd;
|
|
|
|
bsc_register_fd(&server_socket);
|
2009-02-14 22:51:13 +00:00
|
|
|
|
2009-02-16 19:07:18 +00:00
|
|
|
/* register callbacks */
|
2009-02-16 22:05:44 +00:00
|
|
|
register_signal_handler(SS_PAGING, telnet_paging_callback, network);
|
|
|
|
register_signal_handler(SS_SMS, telnet_sms_callback, network);
|
2009-01-02 22:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void telnet_write_help(int fd) {
|
|
|
|
int ret;
|
|
|
|
static char *msg =
|
|
|
|
"Help for the ad-hoc telnet command line interface\n"
|
|
|
|
"The generic pattern is CMD LEN DATA\\n or just CMD\n"
|
|
|
|
"where CMD is one of the following:\n"
|
|
|
|
"help\n"
|
|
|
|
"page IMSI (type)\n"
|
|
|
|
"call IMSI (number)\n"
|
|
|
|
"get_channel IMSI Add use count on an active channel\n"
|
|
|
|
"put_channel IMSI Remove use count on an active channel\n"
|
2009-01-04 03:46:01 +00:00
|
|
|
"show This will show the channel allocation\n"
|
2009-01-04 03:57:27 +00:00
|
|
|
"48 IMSI 0xAB 0xEF...Send GSM 04.08. proto and msg byte then data\n"
|
2009-01-02 22:04:43 +00:00
|
|
|
"11 IMSI 0xAB 0xEF...Send GSM 04.11\n";
|
|
|
|
|
|
|
|
ret = write(fd, msg, strlen(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_welcome(int fd) {
|
|
|
|
int ret;
|
|
|
|
static char *msg =
|
|
|
|
"Welcome to the OpenBSC Control interface\n"
|
|
|
|
"Copyright (C) 2008, 2009 Harald Welte\n"
|
|
|
|
"Contributions by Daniel Willmann, Jan Lübbe, "
|
|
|
|
"Stefan Schmidt, Holger Freyther\n\n"
|
|
|
|
"License GPLv2+: GNU GPL version 2 or later "
|
|
|
|
"<http://gnu.org/licenses/gpl.html>\n"
|
|
|
|
"This is free software: you are free to change "
|
|
|
|
"and redistribute it.\n"
|
|
|
|
"There is NO WARRANTY, to the extent permitted "
|
|
|
|
"by law.\nType \"help\" to get a short introduction.\n";
|
|
|
|
|
|
|
|
ret = write(fd, msg, strlen(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
int telnet_close_client(struct bsc_fd *fd) {
|
|
|
|
struct telnet_connection *conn = (struct telnet_connection*)fd->data;
|
|
|
|
|
|
|
|
close(fd->fd);
|
|
|
|
bsc_unregister_fd(fd);
|
|
|
|
llist_del(&conn->entry);
|
|
|
|
free(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void telnet_error_client(int fd) {
|
|
|
|
int ret;
|
|
|
|
static char *msg = "Something went wrong. Please try again.\n";
|
|
|
|
|
|
|
|
printf("Error\n");
|
|
|
|
ret = write(fd, msg, strlen(msg));
|
|
|
|
}
|
|
|
|
|
2009-01-04 03:49:41 +00:00
|
|
|
static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi,
|
|
|
|
const char **error, int fd) {
|
|
|
|
int ret;
|
|
|
|
struct gsm_lchan *lchan;
|
|
|
|
struct gsm_subscriber *subscr;
|
|
|
|
|
|
|
|
subscr = subscr_get_by_imsi(imsi);
|
|
|
|
if (!subscr) {
|
|
|
|
ret = write(fd, error[0], strlen(error[0]));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lchan = lchan_find(bts, subscr);
|
|
|
|
if (!lchan)
|
|
|
|
ret = write(fd, error[1], strlen(error[1]));
|
|
|
|
|
|
|
|
subscr_put(subscr);
|
|
|
|
return lchan;
|
|
|
|
}
|
|
|
|
|
2009-01-04 20:18:23 +00:00
|
|
|
void telnet_page(struct telnet_connection *connection, const char *imsi, int type) {
|
|
|
|
int ret;
|
|
|
|
static const char* error[] = {
|
|
|
|
"paging: IMSI not found\n",
|
|
|
|
"paging: No channel allocated for IMSI -> will allocate\n" };
|
|
|
|
struct gsm_bts *bts = &connection->network->bts[connection->bts];
|
|
|
|
struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
|
|
|
|
|
|
|
|
if (lchan) {
|
|
|
|
static const char *msg = "paging: A Channel is already allocated.\n";
|
|
|
|
ret = write(connection->fd.fd, msg, strlen(msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gsm_subscriber *subscr = subscr_get_by_imsi(imsi);
|
|
|
|
if (!subscr)
|
|
|
|
return;
|
|
|
|
|
2009-02-09 23:42:03 +00:00
|
|
|
paging_request(bts, subscr, type);
|
2009-02-16 22:05:44 +00:00
|
|
|
paging_update_buffer_space(bts, 100);
|
2009-01-04 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
2009-01-02 22:04:43 +00:00
|
|
|
void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
|
2009-01-04 03:49:41 +00:00
|
|
|
static const char* error[] = {
|
|
|
|
"put_channel: IMSI not found\n",
|
|
|
|
"put_channel: No channel allocated for IMSI\n" };
|
|
|
|
struct gsm_bts *bts = &connection->network->bts[connection->bts];
|
|
|
|
struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
|
|
|
|
|
|
|
|
if (!lchan)
|
|
|
|
return;
|
|
|
|
|
|
|
|
put_lchan(lchan);
|
2009-01-02 22:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
|
2009-01-04 03:49:41 +00:00
|
|
|
static const char* error[] = {
|
|
|
|
"get_channel: IMSI not found\n",
|
|
|
|
"get_channel: No channel allocated for IMSI\n" };
|
|
|
|
struct gsm_bts *bts = &connection->network->bts[connection->bts];
|
|
|
|
struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
|
|
|
|
|
|
|
|
if (!lchan)
|
|
|
|
return;
|
|
|
|
|
|
|
|
use_lchan(lchan);
|
2009-01-02 22:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void telnet_call(struct telnet_connection *connection, const char* imsi,
|
|
|
|
const char *origin) {
|
2009-01-04 03:50:40 +00:00
|
|
|
static const char* error[] = {
|
|
|
|
"call: IMSI not found\n",
|
|
|
|
"call: No channel allocated for IMSI\n" };
|
|
|
|
struct gsm_bts *bts = &connection->network->bts[connection->bts];
|
|
|
|
struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
|
|
|
|
|
|
|
|
if (!lchan)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* TODO: add the origin */
|
|
|
|
gsm48_cc_tx_setup(lchan);
|
2009-01-02 22:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void telnet_send_gsm_48(struct telnet_connection *connection) {
|
2009-01-04 03:57:27 +00:00
|
|
|
static const char* error[] = {
|
|
|
|
"48: IMSI not found\n",
|
|
|
|
"48: No channel allocated for IMSI\n" };
|
2009-01-04 20:15:12 +00:00
|
|
|
int ret;
|
2009-01-04 03:57:27 +00:00
|
|
|
struct gsm_bts *bts = &connection->network->bts[connection->bts];
|
|
|
|
struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd);
|
|
|
|
|
|
|
|
if (!lchan)
|
|
|
|
return;
|
|
|
|
|
2009-01-04 20:15:12 +00:00
|
|
|
if (connection->read < 2) {
|
|
|
|
static const char *msg = "48: Need at least two bytes";
|
|
|
|
ret = write(connection->fd.fd, msg, strlen(msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-04 03:57:27 +00:00
|
|
|
struct msgb *msg = gsm48_msgb_alloc();
|
|
|
|
struct gsm48_hdr *gh;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2);
|
|
|
|
msg->lchan = lchan;
|
|
|
|
|
|
|
|
gh->proto_discr = connection->commands[0];
|
|
|
|
gh->msg_type = connection->commands[1];
|
|
|
|
for (i = 2; i < connection->read; ++i)
|
|
|
|
gh->data[i-2] = connection->commands[i];
|
|
|
|
|
2009-01-04 20:17:07 +00:00
|
|
|
gsm48_sendmsg(msg);
|
2009-01-02 22:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void telnet_send_gsm_11(struct telnet_connection *connection) {
|
|
|
|
printf("sending gsm04.11 message\n");
|
|
|
|
}
|
|
|
|
|
2009-01-04 03:46:01 +00:00
|
|
|
static void show_bts(int fd, struct gsm_bts *bts) {
|
|
|
|
WRITE_CONNECTION(fd,
|
|
|
|
"BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
|
|
|
|
bts->nr, bts->bts_nr, bts->location_area_code,
|
|
|
|
bts->num_trx, bts->c0->arfcn, bts->c0->nr)
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_trx(int fd, struct gsm_bts_trx *trx) {
|
|
|
|
WRITE_CONNECTION(fd,
|
|
|
|
" TRX: %u ARFCN: %u\n",
|
|
|
|
trx->nr, trx->arfcn)
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
|
|
|
|
WRITE_CONNECTION(fd,
|
2009-01-04 16:11:31 +00:00
|
|
|
" TS: #%u pchan: %12s flags: %u\n",
|
|
|
|
ts->nr, gsm_pchan_name(ts->pchan), ts->flags);
|
2009-01-04 03:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_lchan(int fd, struct gsm_lchan *lchan) {
|
|
|
|
struct gsm_subscriber *subscr = lchan->subscr;
|
|
|
|
WRITE_CONNECTION(fd,
|
2009-01-09 21:24:53 +00:00
|
|
|
" LCHAN: #%u type: %7s count: %d subscriber: %s/%s/%s use: %d loc: %p\n",
|
2009-01-04 16:11:31 +00:00
|
|
|
lchan->nr, gsm_lchan_name(lchan->type),
|
2009-01-09 21:24:53 +00:00
|
|
|
lchan->use_count,
|
2009-01-04 03:46:01 +00:00
|
|
|
subscr ? subscr->imsi : "na",
|
|
|
|
subscr ? subscr->tmsi : "na",
|
|
|
|
subscr ? subscr->name : "na",
|
|
|
|
lchan->use_count, lchan->loc_operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void telnet_list_channels(struct telnet_connection *connection) {
|
|
|
|
int bts_no, trx, lchan_no, ts_no;
|
|
|
|
struct gsm_network *network = connection->network;
|
|
|
|
|
|
|
|
for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
|
|
|
|
struct gsm_bts *bts = &network->bts[bts_no];
|
|
|
|
show_bts(connection->fd.fd, bts);
|
|
|
|
|
|
|
|
for (trx = 0; trx < bts->num_trx; ++trx) {
|
|
|
|
show_trx(connection->fd.fd, &bts->trx[trx]);
|
|
|
|
for (ts_no = 0; ts_no < 8; ++ts_no) {
|
|
|
|
show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
|
|
|
|
for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
|
|
|
|
struct gsm_lchan *lchan =
|
|
|
|
&bts->trx[trx].ts[ts_no].lchan[lchan_no];
|
|
|
|
show_lchan(connection->fd.fd, lchan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-02 22:04:43 +00:00
|
|
|
static int client_data(struct bsc_fd *fd, unsigned int what) {
|
|
|
|
char buf[4096];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read(fd->fd, buf, sizeof(buf)-1);
|
|
|
|
buf[ret] = '\0';
|
|
|
|
|
|
|
|
/* connection is gone */
|
|
|
|
if (ret <= 0)
|
|
|
|
return telnet_close_client(fd);
|
|
|
|
|
|
|
|
/* time to parse. This code assumes that the input is line based */
|
|
|
|
telnet_parse((struct telnet_connection*)fd->data, buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
|
|
|
|
struct telnet_connection *connection;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
socklen_t len = sizeof(sockaddr);
|
|
|
|
int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
|
|
|
|
|
|
|
|
if (new_connection < 0) {
|
|
|
|
perror("telnet accept failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
connection = (struct telnet_connection*)malloc(sizeof(*connection));
|
|
|
|
memset(connection, 0, sizeof(*connection));
|
|
|
|
connection->network = (struct gsm_network*)fd->data;
|
|
|
|
connection->fd.data = connection;
|
|
|
|
connection->fd.fd = new_connection;
|
|
|
|
connection->fd.when = BSC_FD_READ;
|
|
|
|
connection->fd.cb = client_data;
|
2009-01-04 03:49:41 +00:00
|
|
|
connection->bts = 0;
|
2009-01-02 22:04:43 +00:00
|
|
|
bsc_register_fd(&connection->fd);
|
|
|
|
llist_add_tail(&connection->entry, &active_connections);
|
|
|
|
|
|
|
|
print_welcome(new_connection);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-14 22:51:13 +00:00
|
|
|
|
2009-02-16 22:05:44 +00:00
|
|
|
static int telnet_paging_callback(unsigned int subsys, unsigned int singal,
|
|
|
|
void *handler_data, void *signal_data)
|
2009-02-14 22:51:13 +00:00
|
|
|
{
|
2009-02-16 22:05:44 +00:00
|
|
|
struct paging_signal_data *paging = signal_data;
|
2009-02-14 22:51:13 +00:00
|
|
|
struct telnet_connection *con;
|
|
|
|
|
|
|
|
llist_for_each_entry(con, &active_connections, entry) {
|
|
|
|
if (paging->lchan) {
|
|
|
|
WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n");
|
|
|
|
show_lchan(con->fd.fd, paging->lchan);
|
|
|
|
} else {
|
|
|
|
WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n",
|
|
|
|
paging->subscr->imsi,
|
|
|
|
paging->subscr->tmsi,
|
|
|
|
paging->subscr->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-16 19:07:18 +00:00
|
|
|
|
2009-02-16 22:05:44 +00:00
|
|
|
static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
|
|
|
|
void *handler_data, void *signal_data)
|
2009-02-16 19:07:18 +00:00
|
|
|
{
|
2009-02-16 22:05:44 +00:00
|
|
|
struct sms_submit *sms = signal_data;
|
2009-02-16 19:07:18 +00:00
|
|
|
struct telnet_connection *con;
|
|
|
|
|
|
|
|
llist_for_each_entry(con, &active_connections, entry) {
|
2009-02-16 22:05:44 +00:00
|
|
|
WRITE_CONNECTION(con->fd.fd, "Incoming SMS: %s\n", sms->user_data);
|
2009-02-16 19:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|