Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 1 | /* Bloated main routine, refactor */ |
| 2 | /* |
Holger Hans Peter Freyther | c840569 | 2011-01-02 20:24:08 +0100 | [diff] [blame] | 3 | * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 4 | * (C) 2010-2011 by On-Waves |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 16 | * |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <mtp_data.h> |
Holger Hans Peter Freyther | b38b33b | 2010-11-26 21:21:04 +0100 | [diff] [blame] | 23 | #include <mtp_level3.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 24 | #include <mtp_pcap.h> |
| 25 | #include <thread.h> |
| 26 | #include <bss_patch.h> |
| 27 | #include <bssap_sccp.h> |
| 28 | #include <bsc_data.h> |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 29 | #include <cellmgr_debug.h> |
Holger Hans Peter Freyther | b3e1068 | 2010-09-30 01:57:45 +0800 | [diff] [blame] | 30 | #include <bsc_sccp.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 31 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 32 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 33 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 34 | #include <osmocom/vty/vty.h> |
Holger Hans Peter Freyther | fdae5c9 | 2010-09-30 01:01:03 +0800 | [diff] [blame] | 35 | #include <osmocom/vty/telnet_interface.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 36 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
| 39 | |
| 40 | #include <fcntl.h> |
| 41 | #include <signal.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | #include <assert.h> |
| 46 | #include <unistd.h> |
| 47 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 48 | #ifndef _GNU_SOURCE |
| 49 | #define _GNU_SOURCE |
| 50 | #endif |
| 51 | #include <getopt.h> |
| 52 | |
Holger Hans Peter Freyther | 4bbfa27 | 2010-09-15 20:53:56 +0800 | [diff] [blame] | 53 | #undef PACKAGE_NAME |
| 54 | #undef PACKAGE_VERSION |
| 55 | #undef PACKAGE_BUGREPORT |
| 56 | #undef PACKAGE_TARNAME |
| 57 | #undef PACKAGE_STRING |
| 58 | #include <cellmgr_config.h> |
| 59 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 60 | static struct log_target *stderr_target; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 61 | |
| 62 | static char *config = "cellmgr_ng.cfg"; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 63 | |
Holger Hans Peter Freyther | 7942abc | 2010-09-30 00:34:46 +0800 | [diff] [blame] | 64 | struct bsc_data bsc; |
| 65 | extern void cell_vty_init(void); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 66 | |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 67 | static void send_reset_ack(struct mtp_link_set *link, int sls); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 68 | static void bsc_resources_released(struct bsc_data *bsc); |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 69 | static void handle_local_sccp(struct mtp_link_set *link, struct msgb *inp, struct sccp_parse_result *res, int sls); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 70 | static void clear_connections(struct bsc_data *bsc); |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 71 | static void send_local_rlsd(struct mtp_link_set *link, struct sccp_parse_result *res); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 72 | |
| 73 | /* send a RSIP to the MGCP GW */ |
| 74 | static void mgcp_reset(struct bsc_data *bsc) |
| 75 | { |
| 76 | static const char mgcp_reset[] = { |
| 77 | "RSIP 1 13@mgw MGCP 1.0\r\n" |
| 78 | }; |
| 79 | |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 80 | mgcp_forward(bsc, (const uint8_t *) mgcp_reset, strlen(mgcp_reset)); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
| 84 | * methods called from the MTP Level3 part |
| 85 | */ |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 86 | void mtp_link_set_forward_sccp(struct mtp_link_set *link, struct msgb *_msg, int sls) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 87 | { |
| 88 | int rc; |
| 89 | struct sccp_parse_result result; |
| 90 | |
| 91 | rc = bss_patch_filter_msg(_msg, &result); |
| 92 | if (rc == BSS_FILTER_RESET) { |
| 93 | LOGP(DMSC, LOGL_NOTICE, "Filtering BSS Reset from the BSC\n"); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 94 | mgcp_reset(&bsc); |
| 95 | send_reset_ack(link, sls); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | /* special responder */ |
Holger Hans Peter Freyther | 0c95c6a | 2010-08-07 02:37:43 +0800 | [diff] [blame] | 100 | if (bsc.msc_link_down) { |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 101 | if (rc == BSS_FILTER_RESET_ACK && bsc.reset_count > 0) { |
| 102 | LOGP(DMSC, LOGL_ERROR, "Received reset ack for closing.\n"); |
| 103 | clear_connections(&bsc); |
| 104 | bsc_resources_released(&bsc); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if (rc != 0 && rc != BSS_FILTER_RLSD && rc != BSS_FILTER_RLC) { |
| 109 | LOGP(DMSC, LOGL_ERROR, "Ignoring unparsable msg during closedown.\n"); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | return handle_local_sccp(link, _msg, &result, sls); |
| 114 | } |
| 115 | |
| 116 | /* update the connection state */ |
Holger Hans Peter Freyther | 5d930f8 | 2010-10-08 23:18:15 +0800 | [diff] [blame] | 117 | update_con_state(link, rc, &result, _msg, 0, sls); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 118 | |
| 119 | if (rc == BSS_FILTER_CLEAR_COMPL) { |
| 120 | send_local_rlsd(link, &result); |
| 121 | } else if (rc == BSS_FILTER_RLC || rc == BSS_FILTER_RLSD) { |
| 122 | LOGP(DMSC, LOGL_DEBUG, "Not forwarding RLC/RLSD to the MSC.\n"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | msc_send_msg(&bsc, rc, &result, _msg); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * handle local message in close down mode |
| 132 | */ |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 133 | static void handle_local_sccp(struct mtp_link_set *link, struct msgb *inpt, struct sccp_parse_result *result, int sls) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 134 | { |
| 135 | /* Handle msg with a reject */ |
| 136 | if (inpt->l2h[0] == SCCP_MSG_TYPE_CR) { |
| 137 | struct sccp_connection_request *cr; |
| 138 | struct msgb *msg; |
| 139 | |
| 140 | LOGP(DINP, LOGL_NOTICE, "Handling CR localy.\n"); |
| 141 | cr = (struct sccp_connection_request *) inpt->l2h; |
| 142 | msg = create_sccp_refuse(&cr->source_local_reference); |
| 143 | if (msg) { |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 144 | mtp_link_set_submit_sccp_data(link, sls, msg->l2h, msgb_l2len(msg)); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 145 | msgb_free(msg); |
| 146 | } |
| 147 | return; |
| 148 | } else if (inpt->l2h[0] == SCCP_MSG_TYPE_DT1 && result->data_len >= 3) { |
| 149 | struct active_sccp_con *con; |
| 150 | struct sccp_data_form1 *form1; |
| 151 | struct msgb *msg; |
| 152 | |
| 153 | if (inpt->l3h[0] == 0 && inpt->l3h[2] == BSS_MAP_MSG_CLEAR_COMPLETE) { |
| 154 | LOGP(DINP, LOGL_DEBUG, "Received Clear Complete. Sending Release.\n"); |
| 155 | |
| 156 | form1 = (struct sccp_data_form1 *) inpt->l2h; |
| 157 | |
| 158 | llist_for_each_entry(con, &bsc.sccp_connections, entry) { |
| 159 | if (memcmp(&form1->destination_local_reference, |
| 160 | &con->dst_ref, sizeof(con->dst_ref)) == 0) { |
| 161 | LOGP(DINP, LOGL_DEBUG, "Sending a release request now.\n"); |
| 162 | msg = create_sccp_rlsd(&con->dst_ref, &con->src_ref); |
| 163 | if (msg) { |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 164 | mtp_link_set_submit_sccp_data(link, con->sls, msg->l2h, msgb_l2len(msg)); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 165 | msgb_free(msg); |
| 166 | } |
| 167 | return; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | LOGP(DINP, LOGL_ERROR, "Could not find connection for the Clear Command.\n"); |
| 172 | } |
| 173 | } else if (inpt->l2h[0] == SCCP_MSG_TYPE_UDT && result->data_len >= 3) { |
| 174 | if (inpt->l3h[0] == 0 && inpt->l3h[2] == BSS_MAP_MSG_RESET_ACKNOWLEDGE) { |
| 175 | LOGP(DINP, LOGL_NOTICE, "Reset ACK. Connecting to the MSC again.\n"); |
| 176 | bsc_resources_released(&bsc); |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /* Update the state, maybe the connection was released? */ |
Holger Hans Peter Freyther | 5d930f8 | 2010-10-08 23:18:15 +0800 | [diff] [blame] | 183 | update_con_state(link, 0, result, inpt, 0, sls); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 184 | if (llist_empty(&bsc.sccp_connections)) |
| 185 | bsc_resources_released(&bsc); |
| 186 | return; |
| 187 | } |
| 188 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 189 | static void clear_connections(struct bsc_data *bsc) |
| 190 | { |
| 191 | struct active_sccp_con *tmp, *con; |
| 192 | |
| 193 | llist_for_each_entry_safe(con, tmp, &bsc->sccp_connections, entry) { |
| 194 | free_con(con); |
| 195 | } |
| 196 | |
Holger Hans Peter Freyther | fe72c16 | 2011-01-04 13:21:52 +0100 | [diff] [blame^] | 197 | link_clear_all(bsc->link_set); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void bsc_resources_released(struct bsc_data *bsc) |
| 201 | { |
| 202 | bsc_del_timer(&bsc->reset_timeout); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void bsc_reset_timeout(void *_data) |
| 206 | { |
| 207 | struct msgb *msg; |
| 208 | struct bsc_data *bsc = (struct bsc_data *) _data; |
| 209 | |
| 210 | /* no reset */ |
| 211 | if (bsc->reset_count > 0) { |
| 212 | LOGP(DINP, LOGL_ERROR, "The BSC did not answer the GSM08.08 reset. Restart MTP\n"); |
Holger Hans Peter Freyther | 644aafb | 2011-01-03 23:51:07 +0100 | [diff] [blame] | 213 | mtp_link_set_stop(bsc->link_set); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 214 | clear_connections(bsc); |
Holger Hans Peter Freyther | fe72c16 | 2011-01-04 13:21:52 +0100 | [diff] [blame^] | 215 | link_reset_all(bsc->link_set); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 216 | bsc_resources_released(bsc); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | msg = create_reset(); |
| 221 | if (!msg) { |
| 222 | bsc_schedule_timer(&bsc->reset_timeout, 10, 0); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | ++bsc->reset_count; |
Holger Hans Peter Freyther | 644aafb | 2011-01-03 23:51:07 +0100 | [diff] [blame] | 227 | mtp_link_set_submit_sccp_data(bsc->link_set, -1, msg->l2h, msgb_l2len(msg)); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 228 | msgb_free(msg); |
| 229 | bsc_schedule_timer(&bsc->reset_timeout, 20, 0); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * We have lost the connection to the MSC. This is tough. We |
| 234 | * can not just bring down the MTP link as this will disable |
| 235 | * the BTS radio. We will have to do the following: |
| 236 | * |
| 237 | * 1.) Bring down all open SCCP connections. As this will close |
| 238 | * all radio resources |
| 239 | * 2.) Bring down all MGCP endpoints |
| 240 | * 3.) Clear the connection data. |
| 241 | * |
| 242 | * To make things worse we need to buffer the BSC messages... atfer |
| 243 | * everything has been sent we will try to connect to the MSC again. |
| 244 | * |
| 245 | * We will have to veriy that all connections are closed properly.. |
| 246 | * this means we need to parse response message. In the case the |
| 247 | * MTP link is going down while we are sending. We will simply |
| 248 | * reconnect to the MSC. |
| 249 | */ |
| 250 | void release_bsc_resources(struct bsc_data *bsc) |
| 251 | { |
| 252 | struct active_sccp_con *tmp; |
| 253 | struct active_sccp_con *con; |
| 254 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 255 | bsc_del_timer(&bsc->reset_timeout); |
| 256 | |
| 257 | /* 2. clear the MGCP endpoints */ |
| 258 | mgcp_reset(bsc); |
| 259 | |
| 260 | /* 1. send BSSMAP Cleanup.. if we have any connection */ |
| 261 | llist_for_each_entry_safe(con, tmp, &bsc->sccp_connections, entry) { |
| 262 | if (!con->has_dst_ref) { |
| 263 | free_con(con); |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | struct msgb *msg = create_clear_command(&con->src_ref); |
| 268 | if (!msg) |
| 269 | continue; |
| 270 | |
| 271 | /* wait for the clear commands */ |
Holger Hans Peter Freyther | 644aafb | 2011-01-03 23:51:07 +0100 | [diff] [blame] | 272 | mtp_link_set_submit_sccp_data(bsc->link_set, con->sls, msg->l2h, msgb_l2len(msg)); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 273 | msgb_free(msg); |
| 274 | } |
| 275 | |
| 276 | if (llist_empty(&bsc->sccp_connections)) { |
| 277 | bsc_resources_released(bsc); |
| 278 | } else { |
| 279 | /* Send a reset in 20 seconds if we fail to bring everything down */ |
| 280 | bsc->reset_timeout.cb = bsc_reset_timeout; |
| 281 | bsc->reset_timeout.data = bsc; |
| 282 | bsc->reset_count = 0; |
| 283 | bsc_schedule_timer(&bsc->reset_timeout, 10, 0); |
| 284 | } |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 285 | } |
| 286 | |
Holger Hans Peter Freyther | c840569 | 2011-01-02 20:24:08 +0100 | [diff] [blame] | 287 | void mtp_linkset_down(struct mtp_link_set *set) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 288 | { |
Holger Hans Peter Freyther | c840569 | 2011-01-02 20:24:08 +0100 | [diff] [blame] | 289 | set->available = 0; |
| 290 | mtp_link_set_stop(set); |
| 291 | clear_connections(set->bsc); |
| 292 | mgcp_reset(set->bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 293 | |
Holger Hans Peter Freyther | 7b7c297 | 2010-08-07 05:41:06 +0800 | [diff] [blame] | 294 | /* If we have an A link send a reset to the MSC */ |
Holger Hans Peter Freyther | c840569 | 2011-01-02 20:24:08 +0100 | [diff] [blame] | 295 | msc_send_reset(set->bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 296 | } |
| 297 | |
Holger Hans Peter Freyther | c840569 | 2011-01-02 20:24:08 +0100 | [diff] [blame] | 298 | void mtp_linkset_up(struct mtp_link_set *set) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 299 | { |
Holger Hans Peter Freyther | c840569 | 2011-01-02 20:24:08 +0100 | [diff] [blame] | 300 | set->available = 1; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 301 | |
| 302 | /* we have not gone through link down */ |
Holger Hans Peter Freyther | c840569 | 2011-01-02 20:24:08 +0100 | [diff] [blame] | 303 | if (set->bsc->msc_link_down) { |
| 304 | clear_connections(set->bsc); |
| 305 | bsc_resources_released(set->bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 306 | } |
| 307 | |
Holger Hans Peter Freyther | c840569 | 2011-01-02 20:24:08 +0100 | [diff] [blame] | 308 | mtp_link_set_reset(set); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
| 312 | * update the connection state and helpers below |
| 313 | */ |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 314 | static void send_rlc_to_bsc(unsigned int sls, struct sccp_source_reference *src, struct sccp_source_reference *dst) |
| 315 | { |
| 316 | struct msgb *msg; |
| 317 | |
| 318 | msg = create_sccp_rlc(src, dst); |
| 319 | if (!msg) |
| 320 | return; |
| 321 | |
Holger Hans Peter Freyther | 644aafb | 2011-01-03 23:51:07 +0100 | [diff] [blame] | 322 | mtp_link_set_submit_sccp_data(bsc.link_set, sls, msg->l2h, msgb_l2len(msg)); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 323 | msgb_free(msg); |
| 324 | } |
| 325 | |
| 326 | static void handle_rlsd(struct sccp_connection_released *rlsd, int from_msc) |
| 327 | { |
| 328 | struct active_sccp_con *con; |
| 329 | |
| 330 | if (from_msc) { |
| 331 | /* search for a connection, reverse src/dest for MSC */ |
| 332 | con = find_con_by_src_dest_ref(&rlsd->destination_local_reference, |
| 333 | &rlsd->source_local_reference); |
| 334 | if (con) { |
| 335 | LOGP(DINP, LOGL_DEBUG, "RLSD conn still alive: local: 0x%x remote: 0x%x\n", |
| 336 | sccp_src_ref_to_int(&con->src_ref), |
| 337 | sccp_src_ref_to_int(&con->dst_ref)); |
| 338 | con->released_from_msc = 1; |
| 339 | } else { |
| 340 | /* send RLC */ |
| 341 | LOGP(DINP, LOGL_DEBUG, "Sending RLC for MSC: src: 0x%x dst: 0x%x\n", |
| 342 | sccp_src_ref_to_int(&rlsd->destination_local_reference), |
| 343 | sccp_src_ref_to_int(&rlsd->source_local_reference)); |
| 344 | msc_send_rlc(&bsc, &rlsd->destination_local_reference, |
| 345 | &rlsd->source_local_reference); |
| 346 | } |
| 347 | } else { |
Holger Hans Peter Freyther | 346e1c4 | 2011-01-02 18:11:37 +0100 | [diff] [blame] | 348 | unsigned int sls = -1; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 349 | con = find_con_by_src_dest_ref(&rlsd->source_local_reference, |
| 350 | &rlsd->destination_local_reference); |
| 351 | if (con) { |
| 352 | LOGP(DINP, LOGL_DEBUG, "Timeout on BSC. Sending RLC. src: 0x%x\n", |
| 353 | sccp_src_ref_to_int(&rlsd->source_local_reference)); |
| 354 | |
| 355 | if (con->released_from_msc) |
| 356 | msc_send_rlc(&bsc, &con->src_ref, &con->dst_ref); |
| 357 | sls = con->sls; |
| 358 | free_con(con); |
| 359 | } else { |
| 360 | LOGP(DINP, LOGL_ERROR, "Timeout on BSC for unknown connection. src: 0x%x\n", |
| 361 | sccp_src_ref_to_int(&rlsd->source_local_reference)); |
| 362 | } |
| 363 | |
| 364 | /* now send a rlc back to the BSC */ |
| 365 | send_rlc_to_bsc(sls, &rlsd->destination_local_reference, &rlsd->source_local_reference); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Update connection state and also send message..... |
| 371 | * |
| 372 | * RLSD from MSC: |
| 373 | * 1.) We don't find the entry in this case we will send a |
| 374 | * forged RLC to the MSC and we are done. |
| 375 | * 2.) We find an entry in this we will need to register that |
| 376 | * we need to send a RLC and we are done for now. |
| 377 | * RLSD from BSC: |
| 378 | * 1.) This is an error we are ignoring for now. |
| 379 | * RLC from BSC: |
| 380 | * 1.) We are destroying the connection, we might send a RLC to |
| 381 | * the MSC if we are waiting for one. |
| 382 | */ |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 383 | void update_con_state(struct mtp_link_set *link, int rc, struct sccp_parse_result *res, struct msgb *msg, int from_msc, int sls) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 384 | { |
| 385 | struct active_sccp_con *con; |
| 386 | struct sccp_connection_request *cr; |
| 387 | struct sccp_connection_confirm *cc; |
| 388 | struct sccp_connection_release_complete *rlc; |
| 389 | struct sccp_connection_refused *cref; |
| 390 | |
| 391 | /* was the header okay? */ |
| 392 | if (rc < 0) |
| 393 | return; |
| 394 | |
| 395 | /* the header was size checked */ |
| 396 | switch (msg->l2h[0]) { |
| 397 | case SCCP_MSG_TYPE_CR: |
| 398 | if (from_msc) { |
| 399 | LOGP(DMSC, LOGL_ERROR, "CR from MSC is not handled.\n"); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | cr = (struct sccp_connection_request *) msg->l2h; |
| 404 | con = find_con_by_src_ref(&cr->source_local_reference); |
| 405 | if (con) { |
| 406 | LOGP(DINP, LOGL_ERROR, "Duplicate SRC reference for: 0x%x. Reusing\n", |
| 407 | sccp_src_ref_to_int(&con->src_ref)); |
| 408 | free_con(con); |
| 409 | } |
| 410 | |
| 411 | con = talloc_zero(NULL, struct active_sccp_con); |
| 412 | if (!con) { |
| 413 | LOGP(DINP, LOGL_ERROR, "Failed to allocate\n"); |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | con->src_ref = cr->source_local_reference; |
| 418 | con->sls = sls; |
Holger Hans Peter Freyther | 5d930f8 | 2010-10-08 23:18:15 +0800 | [diff] [blame] | 419 | con->link = link; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 420 | llist_add_tail(&con->entry, &bsc.sccp_connections); |
| 421 | LOGP(DINP, LOGL_DEBUG, "Adding CR: local ref: 0x%x\n", sccp_src_ref_to_int(&con->src_ref)); |
| 422 | break; |
| 423 | case SCCP_MSG_TYPE_CC: |
| 424 | if (!from_msc) { |
| 425 | LOGP(DINP, LOGL_ERROR, "CC from BSC is not handled.\n"); |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | cc = (struct sccp_connection_confirm *) msg->l2h; |
| 430 | con = find_con_by_src_ref(&cc->destination_local_reference); |
| 431 | if (con) { |
| 432 | con->dst_ref = cc->source_local_reference; |
| 433 | con->has_dst_ref = 1; |
| 434 | LOGP(DINP, LOGL_DEBUG, "Updating CC: local: 0x%x remote: 0x%x\n", |
| 435 | sccp_src_ref_to_int(&con->src_ref), sccp_src_ref_to_int(&con->dst_ref)); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | LOGP(DINP, LOGL_ERROR, "CCed connection can not be found: 0x%x\n", |
| 440 | sccp_src_ref_to_int(&cc->destination_local_reference)); |
| 441 | break; |
| 442 | case SCCP_MSG_TYPE_CREF: |
| 443 | if (!from_msc) { |
| 444 | LOGP(DINP, LOGL_ERROR, "CREF from BSC is not handled.\n"); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | cref = (struct sccp_connection_refused *) msg->l2h; |
| 449 | con = find_con_by_src_ref(&cref->destination_local_reference); |
| 450 | if (con) { |
| 451 | LOGP(DINP, LOGL_DEBUG, "Releasing local: 0x%x\n", sccp_src_ref_to_int(&con->src_ref)); |
| 452 | free_con(con); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | LOGP(DINP, LOGL_ERROR, "CREF from BSC is not handled.\n"); |
| 457 | break; |
| 458 | case SCCP_MSG_TYPE_RLSD: |
| 459 | handle_rlsd((struct sccp_connection_released *) msg->l2h, from_msc); |
| 460 | break; |
| 461 | case SCCP_MSG_TYPE_RLC: |
| 462 | if (from_msc) { |
| 463 | LOGP(DINP, LOGL_ERROR, "RLC from MSC is wrong.\n"); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | rlc = (struct sccp_connection_release_complete *) msg->l2h; |
| 468 | con = find_con_by_src_dest_ref(&rlc->source_local_reference, |
| 469 | &rlc->destination_local_reference); |
| 470 | if (con) { |
| 471 | LOGP(DINP, LOGL_DEBUG, "Releasing local: 0x%x\n", sccp_src_ref_to_int(&con->src_ref)); |
| 472 | if (con->released_from_msc) |
| 473 | msc_send_rlc(&bsc, &con->src_ref, &con->dst_ref); |
| 474 | free_con(con); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | LOGP(DINP, LOGL_ERROR, "RLC can not be found. 0x%x 0x%x\n", |
| 479 | sccp_src_ref_to_int(&rlc->source_local_reference), |
| 480 | sccp_src_ref_to_int(&rlc->destination_local_reference)); |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | static void send_local_rlsd_for_con(void *data) |
| 486 | { |
| 487 | struct msgb *rlsd; |
| 488 | struct active_sccp_con *con = (struct active_sccp_con *) data; |
| 489 | |
| 490 | /* try again in three seconds */ |
| 491 | con->rlc_timeout.data = con; |
| 492 | con->rlc_timeout.cb = send_local_rlsd_for_con; |
| 493 | bsc_schedule_timer(&con->rlc_timeout, 3, 0); |
| 494 | |
| 495 | /* we send this to the BSC so we need to switch src and dest */ |
| 496 | rlsd = create_sccp_rlsd(&con->dst_ref, &con->src_ref); |
| 497 | if (!rlsd) |
| 498 | return; |
| 499 | |
| 500 | ++con->rls_tries; |
| 501 | LOGP(DINP, LOGL_DEBUG, "Sending RLSD for 0x%x the %d time.\n", |
| 502 | sccp_src_ref_to_int(&con->src_ref), con->rls_tries); |
Holger Hans Peter Freyther | 644aafb | 2011-01-03 23:51:07 +0100 | [diff] [blame] | 503 | mtp_link_set_submit_sccp_data(bsc.link_set, con->sls, rlsd->l2h, msgb_l2len(rlsd)); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 504 | msgb_free(rlsd); |
| 505 | } |
| 506 | |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 507 | static void send_local_rlsd(struct mtp_link_set *link, struct sccp_parse_result *res) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 508 | { |
| 509 | struct active_sccp_con *con; |
| 510 | |
| 511 | LOGP(DINP, LOGL_DEBUG, "Received GSM Clear Complete. Sending RLSD locally.\n"); |
| 512 | |
| 513 | con = find_con_by_dest_ref(res->destination_local_reference); |
| 514 | if (!con) |
| 515 | return; |
| 516 | con->rls_tries = 0; |
| 517 | send_local_rlsd_for_con(con); |
| 518 | } |
| 519 | |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 520 | static void send_reset_ack(struct mtp_link_set *link, int sls) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 521 | { |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 522 | static const uint8_t reset_ack[] = { |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 523 | 0x09, 0x00, 0x03, 0x05, 0x7, 0x02, 0x42, 0xfe, |
| 524 | 0x02, 0x42, 0xfe, 0x03, |
| 525 | 0x00, 0x01, 0x31 |
| 526 | }; |
| 527 | |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 528 | mtp_link_set_submit_sccp_data(link, sls, reset_ack, sizeof(reset_ack)); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 529 | } |
| 530 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 531 | static void print_usage() |
| 532 | { |
| 533 | printf("Usage: cellmgr_ng\n"); |
| 534 | } |
| 535 | |
| 536 | static void sigint() |
| 537 | { |
| 538 | static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 539 | static int handled = 0; |
| 540 | |
| 541 | /* failed to lock */ |
| 542 | if (pthread_mutex_trylock(&exit_mutex) != 0) |
| 543 | return; |
| 544 | if (handled) |
| 545 | goto out; |
| 546 | |
| 547 | printf("Terminating.\n"); |
| 548 | handled = 1; |
| 549 | if (bsc.setup) |
Holger Hans Peter Freyther | fe72c16 | 2011-01-04 13:21:52 +0100 | [diff] [blame^] | 550 | link_shutdown_all(bsc.link_set); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 551 | exit(0); |
| 552 | |
| 553 | out: |
| 554 | pthread_mutex_unlock(&exit_mutex); |
| 555 | } |
| 556 | |
| 557 | static void sigusr2() |
| 558 | { |
| 559 | printf("Closing the MSC connection on demand.\n"); |
Holger Hans Peter Freyther | 43d9eec | 2010-08-07 01:54:19 +0800 | [diff] [blame] | 560 | msc_close_connection(&bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static void print_help() |
| 564 | { |
| 565 | printf(" Some useful help...\n"); |
| 566 | printf(" -h --help this text\n"); |
| 567 | printf(" -c --config=CFG The config file to use.\n"); |
| 568 | printf(" -p --pcap=FILE. Write MSUs to the PCAP file.\n"); |
| 569 | printf(" -c --once. Send the SLTM msg only once.\n"); |
Holger Hans Peter Freyther | 4bbfa27 | 2010-09-15 20:53:56 +0800 | [diff] [blame] | 570 | printf(" -v --version. Print the version number\n"); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static void handle_options(int argc, char **argv) |
| 574 | { |
| 575 | while (1) { |
| 576 | int option_index = 0, c; |
| 577 | static struct option long_options[] = { |
| 578 | {"help", 0, 0, 'h'}, |
| 579 | {"config", 1, 0, 'c'}, |
| 580 | {"pcap", 1, 0, 'p'}, |
Holger Hans Peter Freyther | 4bbfa27 | 2010-09-15 20:53:56 +0800 | [diff] [blame] | 581 | {"version", 0, 0, 0}, |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 582 | {0, 0, 0, 0}, |
| 583 | }; |
| 584 | |
Holger Hans Peter Freyther | eefd72c | 2010-12-17 16:22:31 +0100 | [diff] [blame] | 585 | c = getopt_long(argc, argv, "hc:p:v", |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 586 | long_options, &option_index); |
| 587 | if (c == -1) |
| 588 | break; |
| 589 | |
| 590 | switch (c) { |
| 591 | case 'h': |
| 592 | print_usage(); |
| 593 | print_help(); |
| 594 | exit(0); |
| 595 | case 'p': |
Holger Hans Peter Freyther | 644aafb | 2011-01-03 23:51:07 +0100 | [diff] [blame] | 596 | if (bsc.pcap_fd >= 0) |
| 597 | close(bsc.pcap_fd); |
| 598 | bsc.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH); |
| 599 | if (bsc.pcap_fd < 0) { |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 600 | fprintf(stderr, "Failed to open PCAP file.\n"); |
| 601 | exit(0); |
| 602 | } |
Holger Hans Peter Freyther | 644aafb | 2011-01-03 23:51:07 +0100 | [diff] [blame] | 603 | mtp_pcap_write_header(bsc.pcap_fd); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 604 | break; |
| 605 | case 'c': |
| 606 | config = optarg; |
| 607 | break; |
Holger Hans Peter Freyther | 4bbfa27 | 2010-09-15 20:53:56 +0800 | [diff] [blame] | 608 | case 'v': |
| 609 | printf("This is %s version %s.\n", PACKAGE, VERSION); |
| 610 | exit(0); |
| 611 | break; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 612 | default: |
| 613 | fprintf(stderr, "Unknown option.\n"); |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 619 | int main(int argc, char **argv) |
| 620 | { |
Holger Hans Peter Freyther | fdae5c9 | 2010-09-30 01:01:03 +0800 | [diff] [blame] | 621 | int rc; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 622 | INIT_LLIST_HEAD(&bsc.sccp_connections); |
| 623 | |
Holger Hans Peter Freyther | 7942abc | 2010-09-30 00:34:46 +0800 | [diff] [blame] | 624 | bsc.dpc = 1; |
| 625 | bsc.opc = 0; |
Holger Hans Peter Freyther | 7a72556 | 2011-01-01 13:34:58 +0100 | [diff] [blame] | 626 | bsc.sccp_opc = -1; |
Holger Hans Peter Freyther | 7942abc | 2010-09-30 00:34:46 +0800 | [diff] [blame] | 627 | bsc.udp_port = 3456; |
| 628 | bsc.udp_ip = NULL; |
| 629 | bsc.src_port = 1313; |
Holger Hans Peter Freyther | b38b33b | 2010-11-26 21:21:04 +0100 | [diff] [blame] | 630 | bsc.ni_ni = MTP_NI_NATION_NET; |
| 631 | bsc.ni_spare = 0; |
Holger Hans Peter Freyther | 7942abc | 2010-09-30 00:34:46 +0800 | [diff] [blame] | 632 | |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame] | 633 | mtp_link_set_init(); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 634 | thread_init(); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 635 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 636 | log_init(&log_info); |
| 637 | stderr_target = log_target_create_stderr(); |
| 638 | log_add_target(stderr_target); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 639 | |
| 640 | /* enable filters */ |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 641 | log_set_all_filter(stderr_target, 1); |
| 642 | log_set_category_filter(stderr_target, DINP, 1, LOGL_INFO); |
| 643 | log_set_category_filter(stderr_target, DSCCP, 1, LOGL_INFO); |
| 644 | log_set_category_filter(stderr_target, DMSC, 1, LOGL_INFO); |
| 645 | log_set_category_filter(stderr_target, DMGCP, 1, LOGL_INFO); |
| 646 | log_set_print_timestamp(stderr_target, 1); |
| 647 | log_set_use_color(stderr_target, 0); |
| 648 | |
| 649 | sccp_set_log_area(DSCCP); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 650 | |
| 651 | bsc.setup = 0; |
| 652 | bsc.msc_address = "127.0.0.1"; |
Holger Hans Peter Freyther | 644aafb | 2011-01-03 23:51:07 +0100 | [diff] [blame] | 653 | bsc.pcap_fd = -1; |
| 654 | bsc.udp_reset_timeout = 180; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 655 | bsc.ping_time = 20; |
| 656 | bsc.pong_time = 5; |
| 657 | bsc.msc_time = 20; |
| 658 | |
| 659 | handle_options(argc, argv); |
| 660 | |
| 661 | signal(SIGPIPE, SIG_IGN); |
| 662 | signal(SIGINT, sigint); |
| 663 | signal(SIGUSR2, sigusr2); |
| 664 | srand(time(NULL)); |
| 665 | |
| 666 | cell_vty_init(); |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 667 | if (vty_read_config_file(config, NULL) < 0) { |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 668 | fprintf(stderr, "Failed to read the VTY config.\n"); |
| 669 | return -1; |
| 670 | } |
| 671 | |
Holger Hans Peter Freyther | fdae5c9 | 2010-09-30 01:01:03 +0800 | [diff] [blame] | 672 | rc = telnet_init(NULL, NULL, 4242); |
| 673 | if (rc < 0) |
| 674 | return rc; |
| 675 | |
Holger Hans Peter Freyther | a99b04b | 2011-01-02 11:23:54 +0100 | [diff] [blame] | 676 | if (link_init(&bsc) != 0) |
| 677 | return -1; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 678 | |
| 679 | while (1) { |
| 680 | bsc_select_main(0); |
| 681 | } |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |