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