Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 1 | /* (C) 2017 by Sysmocom s.f.m.c. GmbH |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * Author: Philipp Maier |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/utils.h> |
| 22 | #include <osmocom/core/msgb.h> |
| 23 | #include <osmocom/core/logging.h> |
| 24 | #include <osmocom/sigtran/sccp_helpers.h> |
| 25 | #include <osmocom/sccp/sccp_types.h> |
| 26 | #include <osmocom/gsm/gsm0808.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 27 | #include <osmocom/gsm/gsm48.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 28 | #include <osmocom/gsm/gsm0808_utils.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 29 | #include <osmocom/msc/debug.h> |
| 30 | #include <osmocom/msc/gsm_data.h> |
| 31 | #include <osmocom/msc/a_iface_bssap.h> |
| 32 | #include <osmocom/msc/a_iface.h> |
| 33 | #include <osmocom/msc/osmo_msc.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 34 | #include <osmocom/core/byteswap.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 35 | #include <osmocom/msc/a_reset.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 36 | #include <osmocom/msc/transaction.h> |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 37 | #include <osmocom/msc/msc_mgcp.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 38 | |
| 39 | #include <errno.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 40 | |
| 41 | #define IP_V4_ADDR_LEN 4 |
| 42 | |
| 43 | /* |
| 44 | * Helper functions to lookup and allocate subscribers |
| 45 | */ |
| 46 | |
| 47 | /* Allocate a new subscriber connection */ |
| 48 | static struct gsm_subscriber_connection *subscr_conn_allocate_a(const struct a_conn_info *a_conn_info, |
| 49 | struct gsm_network *network, |
| 50 | uint16_t lac, struct osmo_sccp_user *scu, int conn_id) |
| 51 | { |
| 52 | struct gsm_subscriber_connection *conn; |
| 53 | |
| 54 | LOGP(DMSC, LOGL_NOTICE, "Allocating A-Interface subscriber conn: lac %i, conn_id %i\n", lac, conn_id); |
| 55 | |
| 56 | conn = talloc_zero(network, struct gsm_subscriber_connection); |
| 57 | if (!conn) |
| 58 | return NULL; |
| 59 | |
| 60 | conn->network = network; |
| 61 | conn->via_ran = RAN_GERAN_A; |
| 62 | conn->lac = lac; |
| 63 | |
| 64 | conn->a.conn_id = conn_id; |
| 65 | conn->a.scu = scu; |
| 66 | |
| 67 | /* Also backup the calling address of the BSC, this allows us to |
| 68 | * identify later which BSC is responsible for this subscriber connection */ |
| 69 | memcpy(&conn->a.bsc_addr, a_conn_info->bsc_addr, sizeof(conn->a.bsc_addr)); |
| 70 | |
| 71 | llist_add_tail(&conn->entry, &network->subscr_conns); |
| 72 | LOGP(DMSC, LOGL_NOTICE, "A-Interface subscriber connection successfully allocated!\n"); |
| 73 | return conn; |
| 74 | } |
| 75 | |
| 76 | /* Return an existing A subscriber connection record for the given |
| 77 | * connection IDs, or return NULL if not found. */ |
| 78 | static struct gsm_subscriber_connection *subscr_conn_lookup_a(const struct gsm_network *network, int conn_id) |
| 79 | { |
| 80 | struct gsm_subscriber_connection *conn; |
| 81 | |
| 82 | OSMO_ASSERT(network); |
| 83 | |
| 84 | DEBUGP(DMSC, "Looking for A subscriber: conn_id %i\n", conn_id); |
| 85 | |
| 86 | /* FIXME: log_subscribers() is defined in iucs.c as static inline, if |
| 87 | * maybe this function should be public to reach it from here? */ |
| 88 | /* log_subscribers(network); */ |
| 89 | |
| 90 | llist_for_each_entry(conn, &network->subscr_conns, entry) { |
| 91 | if (conn->via_ran == RAN_GERAN_A && conn->a.conn_id == conn_id) { |
| 92 | DEBUGP(DIUCS, "Found A subscriber for conn_id %i\n", conn_id); |
| 93 | return conn; |
| 94 | } |
| 95 | } |
| 96 | DEBUGP(DMSC, "No A subscriber found for conn_id %i\n", conn_id); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * BSSMAP handling for UNITDATA |
| 102 | */ |
| 103 | |
| 104 | /* Endpoint to handle BSSMAP reset */ |
| 105 | static void bssmap_rx_reset(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 106 | { |
| 107 | struct gsm_network *network = a_conn_info->network; |
| 108 | struct osmo_ss7_instance *ss7; |
| 109 | |
| 110 | ss7 = osmo_ss7_instance_find(network->a.cs7_instance); |
| 111 | OSMO_ASSERT(ss7); |
| 112 | |
| 113 | LOGP(DMSC, LOGL_NOTICE, "Rx RESET from BSC %s, sending RESET ACK\n", |
| 114 | osmo_sccp_addr_name(ss7, a_conn_info->bsc_addr)); |
| 115 | osmo_sccp_tx_unitdata_msg(scu, a_conn_info->msc_addr, a_conn_info->bsc_addr, gsm0808_create_reset_ack()); |
| 116 | |
| 117 | /* Make sure all orphand subscriber connections will be cleard */ |
| 118 | a_clear_all(scu, a_conn_info->bsc_addr); |
| 119 | |
| 120 | msgb_free(msg); |
| 121 | } |
| 122 | |
| 123 | /* Endpoint to handle BSSMAP reset acknowlegement */ |
| 124 | static void bssmap_rx_reset_ack(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, |
| 125 | struct msgb *msg) |
| 126 | { |
| 127 | |
| 128 | struct gsm_network *network = a_conn_info->network; |
| 129 | struct osmo_ss7_instance *ss7; |
| 130 | |
| 131 | ss7 = osmo_ss7_instance_find(network->a.cs7_instance); |
| 132 | OSMO_ASSERT(ss7); |
| 133 | |
| 134 | if (a_conn_info->reset == NULL) { |
| 135 | LOGP(DMSC, LOGL_ERROR, "Received RESET ACK from an unknown BSC %s, ignoring...\n", |
| 136 | osmo_sccp_addr_name(ss7, a_conn_info->bsc_addr)); |
| 137 | goto fail; |
| 138 | } |
| 139 | |
| 140 | LOGP(DMSC, LOGL_NOTICE, "Received RESET ACK from BSC %s\n", osmo_sccp_addr_name(ss7, a_conn_info->bsc_addr)); |
| 141 | |
| 142 | /* Confirm that we managed to get the reset ack message |
| 143 | * towards the connection reset logic */ |
| 144 | a_reset_ack_confirm(a_conn_info->reset); |
| 145 | |
| 146 | fail: |
| 147 | msgb_free(msg); |
| 148 | } |
| 149 | |
| 150 | /* Handle UNITDATA BSSMAP messages */ |
| 151 | static void bssmap_rcvmsg_udt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 152 | { |
| 153 | /* Note: When in the MSC role, RESET ACK is the only valid message that |
| 154 | * can be received via UNITDATA */ |
| 155 | |
| 156 | if (msgb_l3len(msg) < 1) { |
| 157 | LOGP(DMSC, LOGL_NOTICE, "Error: No data received -- discarding message!\n"); |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 158 | msgb_free(msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
| 162 | LOGP(DMSC, LOGL_NOTICE, "Rx BSC UDT BSSMAP %s\n", gsm0808_bssmap_name(msg->l3h[0])); |
| 163 | |
| 164 | switch (msg->l3h[0]) { |
| 165 | case BSS_MAP_MSG_RESET: |
| 166 | bssmap_rx_reset(scu, a_conn_info, msg); |
| 167 | break; |
| 168 | case BSS_MAP_MSG_RESET_ACKNOWLEDGE: |
| 169 | bssmap_rx_reset_ack(scu, a_conn_info, msg); |
| 170 | break; |
| 171 | default: |
| 172 | LOGP(DMSC, LOGL_NOTICE, "Unimplemented message format: %s -- message discarded!\n", |
| 173 | gsm0808_bssmap_name(msg->l3h[0])); |
| 174 | msgb_free(msg); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* Receive incoming connection less data messages via sccp */ |
Neels Hofmeyr | c1d6925 | 2017-12-18 04:06:04 +0100 | [diff] [blame] | 179 | void a_sccp_rx_udt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 180 | { |
| 181 | /* Note: The only valid message type that can be received |
| 182 | * via UNITDATA are BSS Management messages */ |
| 183 | struct bssmap_header *bs; |
| 184 | |
| 185 | OSMO_ASSERT(scu); |
| 186 | OSMO_ASSERT(a_conn_info); |
| 187 | OSMO_ASSERT(msg); |
| 188 | |
| 189 | LOGP(DMSC, LOGL_NOTICE, "Rx BSC UDT: %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
| 190 | |
| 191 | if (msgb_l2len(msg) < sizeof(*bs)) { |
| 192 | LOGP(DMSC, LOGL_ERROR, "Error: Header is too short -- discarding message!\n"); |
| 193 | msgb_free(msg); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | bs = (struct bssmap_header *)msgb_l2(msg); |
| 198 | if (bs->length < msgb_l2len(msg) - sizeof(*bs)) { |
| 199 | LOGP(DMSC, LOGL_ERROR, "Error: Message is too short -- discarding message!\n"); |
| 200 | msgb_free(msg); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | switch (bs->type) { |
| 205 | case BSSAP_MSG_BSS_MANAGEMENT: |
| 206 | msg->l3h = &msg->l2h[sizeof(struct bssmap_header)]; |
| 207 | bssmap_rcvmsg_udt(scu, a_conn_info, msg); |
| 208 | break; |
| 209 | default: |
| 210 | LOGP(DMSC, LOGL_ERROR, |
| 211 | "Error: Unimplemented message type: %s -- message discarded!\n", gsm0808_bssmap_name(bs->type)); |
| 212 | msgb_free(msg); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * BSSMAP handling for connection oriented data |
| 218 | */ |
| 219 | |
| 220 | /* Endpoint to handle BSSMAP clear request */ |
| 221 | static int bssmap_rx_clear_rqst(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 222 | { |
| 223 | struct gsm_network *network = a_conn_info->network; |
| 224 | struct tlv_parsed tp; |
| 225 | int rc; |
| 226 | struct msgb *msg_resp; |
| 227 | uint8_t cause; |
| 228 | struct gsm_subscriber_connection *conn; |
| 229 | |
| 230 | LOGP(DMSC, LOGL_NOTICE, "BSC requested to clear connection (conn_id=%i)\n", a_conn_info->conn_id); |
| 231 | |
| 232 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 233 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) { |
| 234 | LOGP(DMSC, LOGL_ERROR, "Cause code is missing -- discarding message!\n"); |
| 235 | goto fail; |
| 236 | } |
| 237 | cause = TLVP_VAL(&tp, GSM0808_IE_CAUSE)[0]; |
| 238 | |
| 239 | /* Respond with clear command */ |
| 240 | msg_resp = gsm0808_create_clear_command(GSM0808_CAUSE_CALL_CONTROL); |
| 241 | rc = osmo_sccp_tx_data_msg(scu, a_conn_info->conn_id, msg_resp); |
| 242 | |
| 243 | /* If possible, inform the MSC about the clear request */ |
| 244 | conn = subscr_conn_lookup_a(network, a_conn_info->conn_id); |
| 245 | if (!conn) |
| 246 | goto fail; |
| 247 | msc_clear_request(conn, cause); |
| 248 | |
| 249 | msgb_free(msg); |
| 250 | return rc; |
| 251 | |
| 252 | fail: |
| 253 | msgb_free(msg); |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | |
| 257 | /* Endpoint to handle BSSMAP clear complete */ |
| 258 | static int bssmap_rx_clear_complete(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 259 | { |
| 260 | int rc; |
| 261 | |
| 262 | LOGP(DMSC, LOGL_NOTICE, "Releasing connection (conn_id=%i)\n", a_conn_info->conn_id); |
| 263 | rc = osmo_sccp_tx_disconn(scu, a_conn_info->conn_id, |
| 264 | a_conn_info->msc_addr, SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 265 | |
| 266 | /* Remove the record from the list with active connections. */ |
| 267 | a_delete_bsc_con(a_conn_info->conn_id); |
| 268 | |
| 269 | msgb_free(msg); |
| 270 | return rc; |
| 271 | } |
| 272 | |
| 273 | /* Endpoint to handle layer 3 complete messages */ |
| 274 | static int bssmap_rx_l3_compl(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 275 | { |
| 276 | struct tlv_parsed tp; |
| 277 | struct { |
| 278 | uint8_t ident; |
| 279 | struct gsm48_loc_area_id lai; |
| 280 | uint16_t ci; |
| 281 | } __attribute__ ((packed)) lai_ci; |
| 282 | uint16_t mcc; |
| 283 | uint16_t mnc; |
| 284 | uint16_t lac; |
| 285 | uint8_t data_length; |
| 286 | const uint8_t *data; |
| 287 | int rc; |
| 288 | |
| 289 | struct gsm_network *network = a_conn_info->network; |
| 290 | struct gsm_subscriber_connection *conn; |
| 291 | |
| 292 | LOGP(DMSC, LOGL_NOTICE, "BSC has completed layer 3 connection (conn_id=%i)\n", a_conn_info->conn_id); |
| 293 | |
| 294 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 295 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER)) { |
| 296 | LOGP(DMSC, LOGL_ERROR, "Mandatory CELL IDENTIFIER not present -- discarding message!\n"); |
| 297 | goto fail; |
| 298 | } |
| 299 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 300 | LOGP(DMSC, LOGL_ERROR, "Mandatory LAYER 3 INFORMATION not present -- discarding message!\n"); |
| 301 | goto fail; |
| 302 | } |
| 303 | |
| 304 | /* Parse Cell ID element */ |
| 305 | /* FIXME: Encapsulate this in a parser/generator function inside |
| 306 | * libosmocore, add support for all specified cell identification |
| 307 | * discriminators (see 3GPP ts 3.2.2.17 Cell Identifier) */ |
| 308 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER); |
| 309 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER); |
| 310 | if (sizeof(lai_ci) != data_length) { |
| 311 | LOGP(DMSC, LOGL_ERROR, |
| 312 | "Unable to parse element CELL IDENTIFIER (wrong field length) -- discarding message!\n"); |
| 313 | goto fail; |
| 314 | } |
| 315 | memcpy(&lai_ci, data, sizeof(lai_ci)); |
| 316 | if (lai_ci.ident != CELL_IDENT_WHOLE_GLOBAL) { |
| 317 | LOGP(DMSC, LOGL_ERROR, |
| 318 | "Unable to parse element CELL IDENTIFIER (wrong cell identification discriminator) -- discarding message!\n"); |
| 319 | goto fail; |
| 320 | } |
| 321 | if (gsm48_decode_lai(&lai_ci.lai, &mcc, &mnc, &lac) != 0) { |
| 322 | LOGP(DMSC, LOGL_ERROR, |
| 323 | "Unable to parse element CELL IDENTIFIER (lai decoding failed) -- discarding message!\n"); |
| 324 | goto fail; |
| 325 | } |
| 326 | |
| 327 | /* Parse Layer 3 Information element */ |
| 328 | /* FIXME: This is probably to hackish, compiler also complains "assignment discards ‘const’ qualifier..." */ |
Neels Hofmeyr | 9baedaf | 2017-12-18 04:07:01 +0100 | [diff] [blame] | 329 | msg->l3h = (uint8_t*)TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 330 | msg->tail = msg->l3h + TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 331 | |
| 332 | /* Create new subscriber context */ |
| 333 | conn = subscr_conn_allocate_a(a_conn_info, network, lac, scu, a_conn_info->conn_id); |
| 334 | |
| 335 | /* Handover location update to the MSC code */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 336 | rc = msc_compl_l3(conn, msg, 0); |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 337 | msgb_free(msg); |
| 338 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 339 | if (rc == MSC_CONN_ACCEPT) { |
| 340 | LOGP(DMSC, LOGL_NOTICE, "User has been accepted by MSC.\n"); |
| 341 | return 0; |
| 342 | } else if (rc == MSC_CONN_REJECT) |
| 343 | LOGP(DMSC, LOGL_NOTICE, "User has been rejected by MSC.\n"); |
| 344 | else |
| 345 | LOGP(DMSC, LOGL_NOTICE, "User has been rejected by MSC (unknown error)\n"); |
| 346 | |
| 347 | return -EINVAL; |
| 348 | |
| 349 | fail: |
| 350 | msgb_free(msg); |
| 351 | return -EINVAL; |
| 352 | } |
| 353 | |
| 354 | /* Endpoint to handle BSSMAP classmark update */ |
| 355 | static int bssmap_rx_classmark_upd(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 356 | { |
| 357 | struct gsm_network *network = a_conn_info->network; |
| 358 | struct gsm_subscriber_connection *conn; |
| 359 | struct tlv_parsed tp; |
| 360 | const uint8_t *cm2 = NULL; |
| 361 | const uint8_t *cm3 = NULL; |
| 362 | uint8_t cm2_len = 0; |
| 363 | uint8_t cm3_len = 0; |
| 364 | |
| 365 | conn = subscr_conn_lookup_a(network, a_conn_info->conn_id); |
| 366 | if (!conn) |
| 367 | goto fail; |
| 368 | |
| 369 | LOGP(DMSC, LOGL_NOTICE, "BSC sends clasmark update (conn_id=%i)\n", conn->a.conn_id); |
| 370 | |
| 371 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 372 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2)) { |
| 373 | LOGP(DMSC, LOGL_ERROR, "Mandatory Classmark Information Type 2 not present -- discarding message!\n"); |
| 374 | goto fail; |
| 375 | } |
| 376 | |
| 377 | cm2 = TLVP_VAL(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2); |
| 378 | cm2_len = TLVP_LEN(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T2); |
| 379 | |
| 380 | if (TLVP_PRESENT(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3)) { |
| 381 | cm3 = TLVP_VAL(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3); |
| 382 | cm3_len = TLVP_LEN(&tp, GSM0808_IE_CLASSMARK_INFORMATION_T3); |
| 383 | } |
| 384 | |
| 385 | /* Inform MSC about the classmark change */ |
| 386 | msc_classmark_chg(conn, cm2, cm2_len, cm3, cm3_len); |
| 387 | |
| 388 | msgb_free(msg); |
| 389 | return 0; |
| 390 | |
| 391 | fail: |
| 392 | msgb_free(msg); |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | /* Endpoint to handle BSSMAP cipher mode complete */ |
| 397 | static int bssmap_rx_ciph_compl(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, |
| 398 | struct msgb *msg) |
| 399 | { |
| 400 | /* FIXME: The field GSM0808_IE_LAYER_3_MESSAGE_CONTENTS is optional by |
| 401 | * means of the specification. So there can be messages without L3 info. |
| 402 | * In this case, the code will crash becrause msc_cipher_mode_compl() |
| 403 | * is not able to deal with msg = NULL and apperently |
| 404 | * msc_cipher_mode_compl() was never meant to be used without L3 data. |
| 405 | * This needs to be discussed further! */ |
| 406 | |
| 407 | struct gsm_network *network = a_conn_info->network; |
| 408 | struct gsm_subscriber_connection *conn; |
| 409 | struct tlv_parsed tp; |
| 410 | uint8_t alg_id = 1; |
| 411 | |
| 412 | conn = subscr_conn_lookup_a(network, a_conn_info->conn_id); |
| 413 | if (!conn) |
| 414 | goto fail; |
| 415 | |
| 416 | LOGP(DMSC, LOGL_NOTICE, "BSC sends cipher mode complete (conn_id=%i)\n", conn->a.conn_id); |
| 417 | |
| 418 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 419 | |
| 420 | if (TLVP_PRESENT(&tp, GSM0808_IE_CHOSEN_ENCR_ALG)) { |
| 421 | alg_id = TLVP_VAL(&tp, GSM0808_IE_CHOSEN_ENCR_ALG)[0] - 1; |
| 422 | } |
| 423 | |
| 424 | if (TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS)) { |
Neels Hofmeyr | 9baedaf | 2017-12-18 04:07:01 +0100 | [diff] [blame] | 425 | msg->l3h = (uint8_t*)TLVP_VAL(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 426 | msg->tail = msg->l3h + TLVP_LEN(&tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS); |
| 427 | } else { |
| 428 | msgb_free(msg); |
| 429 | msg = NULL; |
| 430 | } |
| 431 | |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 432 | /* Hand over cipher mode complete message to the MSC */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 433 | msc_cipher_mode_compl(conn, msg, alg_id); |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 434 | msgb_free(msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | fail: |
| 438 | msgb_free(msg); |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | |
| 442 | /* Endpoint to handle BSSMAP cipher mode reject */ |
| 443 | static int bssmap_rx_ciph_rej(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 444 | { |
| 445 | struct gsm_network *network = a_conn_info->network; |
| 446 | struct gsm_subscriber_connection *conn; |
| 447 | struct tlv_parsed tp; |
| 448 | uint8_t cause; |
| 449 | |
| 450 | conn = subscr_conn_lookup_a(network, a_conn_info->conn_id); |
| 451 | if (!conn) |
| 452 | goto fail; |
| 453 | |
| 454 | LOGP(DMSC, LOGL_NOTICE, "BSC sends cipher mode reject (conn_id=%i)\n", conn->a.conn_id); |
| 455 | |
| 456 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 457 | if (!TLVP_PRESENT(&tp, BSS_MAP_MSG_CIPHER_MODE_REJECT)) { |
| 458 | LOGP(DMSC, LOGL_ERROR, "Cause code is missing -- discarding message!\n"); |
| 459 | goto fail; |
| 460 | } |
| 461 | |
| 462 | cause = TLVP_VAL(&tp, BSS_MAP_MSG_CIPHER_MODE_REJECT)[0]; |
| 463 | LOGP(DMSC, LOGL_NOTICE, "Cipher mode rejection cause: %i\n", cause); |
| 464 | |
| 465 | /* FIXME: Can we do something meaningful here? e.g. report to the |
| 466 | * msc code somehow that the cipher mode command has failed. */ |
| 467 | |
| 468 | msgb_free(msg); |
| 469 | return 0; |
| 470 | fail: |
| 471 | msgb_free(msg); |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
| 475 | /* Endpoint to handle BSSMAP assignment failure */ |
| 476 | static int bssmap_rx_ass_fail(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 477 | { |
| 478 | struct gsm_network *network = a_conn_info->network; |
| 479 | struct gsm_subscriber_connection *conn; |
| 480 | struct tlv_parsed tp; |
| 481 | uint8_t cause; |
| 482 | uint8_t *rr_cause_ptr = NULL; |
| 483 | uint8_t rr_cause; |
| 484 | |
| 485 | conn = subscr_conn_lookup_a(network, a_conn_info->conn_id); |
| 486 | if (!conn) |
| 487 | goto fail; |
| 488 | |
| 489 | LOGP(DMSC, LOGL_NOTICE, "BSC sends assignment failure message (conn_id=%i)\n", conn->a.conn_id); |
| 490 | |
| 491 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 492 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) { |
| 493 | LOGP(DMSC, LOGL_ERROR, "Cause code is missing -- discarding message!\n"); |
| 494 | goto fail; |
| 495 | } |
| 496 | cause = TLVP_VAL(&tp, GSM0808_IE_CAUSE)[0]; |
| 497 | |
| 498 | if (TLVP_PRESENT(&tp, GSM0808_IE_RR_CAUSE)) { |
| 499 | rr_cause = TLVP_VAL(&tp, GSM0808_IE_RR_CAUSE)[0]; |
| 500 | rr_cause_ptr = &rr_cause; |
| 501 | } |
| 502 | |
| 503 | /* FIXME: In AoIP, the Assignment failure will carry also an optional |
| 504 | * Codec List (BSS Supported) element. It has to be discussed if we |
| 505 | * can ignore this element. If not, The msc_assign_fail() function |
| 506 | * call has to change. However msc_assign_fail() does nothing in the |
| 507 | * end. So probably we can just leave it as it is. Even for AoIP */ |
| 508 | |
| 509 | /* Inform the MSC about the assignment failure event */ |
| 510 | msc_assign_fail(conn, cause, rr_cause_ptr); |
| 511 | |
| 512 | msgb_free(msg); |
| 513 | return 0; |
| 514 | fail: |
| 515 | msgb_free(msg); |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | |
| 519 | /* Endpoint to handle sapi "n" reject */ |
| 520 | static int bssmap_rx_sapi_n_rej(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, |
| 521 | struct msgb *msg) |
| 522 | { |
| 523 | struct gsm_network *network = a_conn_info->network; |
| 524 | struct gsm_subscriber_connection *conn; |
| 525 | struct tlv_parsed tp; |
| 526 | uint8_t dlci; |
| 527 | |
| 528 | conn = subscr_conn_lookup_a(network, a_conn_info->conn_id); |
| 529 | if (!conn) |
| 530 | goto fail; |
| 531 | |
| 532 | LOGP(DMSC, LOGL_NOTICE, "BSC sends sapi \"n\" reject message (conn_id=%i)\n", conn->a.conn_id); |
| 533 | |
| 534 | /* Note: The MSC code seems not to care about the cause code, but by |
| 535 | * the specification it is mandatory, so we check its presence. See |
| 536 | * also 3GPP TS 48.008 3.2.1.34 SAPI "n" REJECT */ |
| 537 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 538 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CAUSE)) { |
| 539 | LOGP(DMSC, LOGL_ERROR, "Cause code is missing -- discarding message!\n"); |
| 540 | goto fail; |
| 541 | } |
| 542 | |
| 543 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 544 | if (!TLVP_PRESENT(&tp, GSM0808_IE_DLCI)) { |
| 545 | LOGP(DMSC, LOGL_ERROR, "DLCI is missing -- discarding message!\n"); |
| 546 | goto fail; |
| 547 | } |
| 548 | dlci = TLVP_VAL(&tp, GSM0808_IE_DLCI)[0]; |
| 549 | |
| 550 | /* Inform the MSC about the sapi "n" reject event */ |
| 551 | msc_sapi_n_reject(conn, dlci); |
| 552 | |
| 553 | msgb_free(msg); |
| 554 | return 0; |
| 555 | fail: |
| 556 | msgb_free(msg); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | /* Endpoint to handle assignment complete */ |
| 561 | static int bssmap_rx_ass_compl(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, |
| 562 | struct msgb *msg) |
| 563 | { |
| 564 | struct gsm_network *network = a_conn_info->network; |
| 565 | struct gsm_subscriber_connection *conn; |
Neels Hofmeyr | 6c8afe1 | 2017-09-04 01:03:58 +0200 | [diff] [blame] | 566 | struct mgcp_client *mgcp; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 567 | struct tlv_parsed tp; |
| 568 | struct sockaddr_storage rtp_addr; |
| 569 | struct sockaddr_in *rtp_addr_in; |
| 570 | int rc; |
| 571 | |
| 572 | conn = subscr_conn_lookup_a(network, a_conn_info->conn_id); |
| 573 | if (!conn) |
| 574 | goto fail; |
| 575 | |
Neels Hofmeyr | 6c8afe1 | 2017-09-04 01:03:58 +0200 | [diff] [blame] | 576 | mgcp = conn->network->mgw.client; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 577 | OSMO_ASSERT(mgcp); |
| 578 | |
| 579 | LOGP(DMSC, LOGL_NOTICE, "BSC sends assignment complete message (conn_id=%i)\n", conn->a.conn_id); |
| 580 | |
| 581 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 1, msgb_l3len(msg) - 1, 0, 0); |
| 582 | |
| 583 | if (!TLVP_PRESENT(&tp, GSM0808_IE_AOIP_TRASP_ADDR)) { |
| 584 | LOGP(DMSC, LOGL_ERROR, "AoIP transport identifier missing -- discarding message!\n"); |
| 585 | goto fail; |
| 586 | } |
| 587 | |
| 588 | /* Decode AoIP transport address element */ |
| 589 | rc = gsm0808_dec_aoip_trasp_addr(&rtp_addr, TLVP_VAL(&tp, GSM0808_IE_AOIP_TRASP_ADDR), |
| 590 | TLVP_LEN(&tp, GSM0808_IE_AOIP_TRASP_ADDR)); |
| 591 | if (rc < 0) { |
| 592 | LOGP(DMSC, LOGL_ERROR, "Unable to decode aoip transport address.\n"); |
| 593 | goto fail; |
| 594 | } |
| 595 | |
| 596 | /* use address / port supplied with the AoIP |
| 597 | * transport address element */ |
| 598 | if (rtp_addr.ss_family == AF_INET) { |
| 599 | rtp_addr_in = (struct sockaddr_in *)&rtp_addr; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 600 | msc_mgcp_ass_complete(conn, osmo_ntohs(rtp_addr_in->sin_port), inet_ntoa(rtp_addr_in->sin_addr)); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 601 | } else { |
| 602 | LOGP(DMSC, LOGL_ERROR, "Unsopported addressing scheme. (supports only IPV4)\n"); |
| 603 | goto fail; |
| 604 | } |
| 605 | |
| 606 | /* FIXME: Seems to be related to authentication or, |
| 607 | encryption. Is this really in the right place? */ |
| 608 | msc_rx_sec_mode_compl(conn); |
| 609 | |
| 610 | msgb_free(msg); |
| 611 | return 0; |
| 612 | fail: |
| 613 | msgb_free(msg); |
| 614 | return -EINVAL; |
| 615 | } |
| 616 | |
| 617 | /* Handle incoming connection oriented BSSMAP messages */ |
| 618 | static int rx_bssmap(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 619 | { |
| 620 | if (msgb_l3len(msg) < 1) { |
| 621 | LOGP(DMSC, LOGL_NOTICE, "Error: No data received -- discarding message!\n"); |
| 622 | msgb_free(msg); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | LOGP(DMSC, LOGL_NOTICE, "Rx MSC DT1 BSSMAP %s\n", gsm0808_bssmap_name(msg->l3h[0])); |
| 627 | |
| 628 | switch (msg->l3h[0]) { |
| 629 | case BSS_MAP_MSG_CLEAR_RQST: |
| 630 | return bssmap_rx_clear_rqst(scu, a_conn_info, msg); |
| 631 | break; |
| 632 | case BSS_MAP_MSG_CLEAR_COMPLETE: |
| 633 | return bssmap_rx_clear_complete(scu, a_conn_info, msg); |
| 634 | break; |
| 635 | case BSS_MAP_MSG_COMPLETE_LAYER_3: |
| 636 | return bssmap_rx_l3_compl(scu, a_conn_info, msg); |
| 637 | break; |
| 638 | case BSS_MAP_MSG_CLASSMARK_UPDATE: |
| 639 | return bssmap_rx_classmark_upd(scu, a_conn_info, msg); |
| 640 | break; |
| 641 | case BSS_MAP_MSG_CIPHER_MODE_COMPLETE: |
| 642 | return bssmap_rx_ciph_compl(scu, a_conn_info, msg); |
| 643 | break; |
| 644 | case BSS_MAP_MSG_CIPHER_MODE_REJECT: |
| 645 | return bssmap_rx_ciph_rej(scu, a_conn_info, msg); |
| 646 | break; |
| 647 | case BSS_MAP_MSG_ASSIGMENT_FAILURE: |
| 648 | return bssmap_rx_ass_fail(scu, a_conn_info, msg); |
| 649 | break; |
| 650 | case BSS_MAP_MSG_SAPI_N_REJECT: |
| 651 | return bssmap_rx_sapi_n_rej(scu, a_conn_info, msg); |
| 652 | break; |
| 653 | case BSS_MAP_MSG_ASSIGMENT_COMPLETE: |
| 654 | return bssmap_rx_ass_compl(scu, a_conn_info, msg); |
| 655 | break; |
| 656 | default: |
| 657 | LOGP(DMSC, LOGL_ERROR, "Unimplemented msg type: %s\n", gsm0808_bssmap_name(msg->l3h[0])); |
| 658 | msgb_free(msg); |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | |
| 665 | /* Endpoint to handle regular BSSAP DTAP messages */ |
| 666 | static int rx_dtap(const struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
| 667 | { |
| 668 | struct gsm_network *network = a_conn_info->network; |
| 669 | struct gsm_subscriber_connection *conn; |
| 670 | |
| 671 | conn = subscr_conn_lookup_a(network, a_conn_info->conn_id); |
| 672 | if (!conn) { |
| 673 | msgb_free(msg); |
| 674 | return -EINVAL; |
| 675 | } |
| 676 | |
| 677 | LOGP(DMSC, LOGL_NOTICE, "BSC sends layer 3 dtap (conn_id=%i)\n", conn->a.conn_id); |
| 678 | |
| 679 | /* msc_dtap expects the dtap payload in l3h */ |
| 680 | msg->l3h = msg->l2h + 3; |
| 681 | |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 682 | /* Forward dtap payload into the msc */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 683 | msc_dtap(conn, conn->a.conn_id, msg); |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 684 | msgb_free(msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | /* Handle incoming connection oriented messages */ |
Neels Hofmeyr | c1d6925 | 2017-12-18 04:06:04 +0100 | [diff] [blame] | 690 | int a_sccp_rx_dt(struct osmo_sccp_user *scu, const struct a_conn_info *a_conn_info, struct msgb *msg) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 691 | { |
| 692 | OSMO_ASSERT(scu); |
| 693 | OSMO_ASSERT(a_conn_info); |
| 694 | OSMO_ASSERT(msg); |
| 695 | |
| 696 | LOGP(DMSC, LOGL_NOTICE, "Rx BSC DT: %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
| 697 | |
| 698 | if (msgb_l2len(msg) < sizeof(struct bssmap_header)) { |
| 699 | LOGP(DMSC, LOGL_NOTICE, "The header is too short -- discarding message!\n"); |
| 700 | msgb_free(msg); |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 701 | return -EINVAL; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | switch (msg->l2h[0]) { |
| 705 | case BSSAP_MSG_BSS_MANAGEMENT: |
| 706 | msg->l3h = &msg->l2h[sizeof(struct bssmap_header)]; |
| 707 | return rx_bssmap(scu, a_conn_info, msg); |
| 708 | break; |
| 709 | case BSSAP_MSG_DTAP: |
| 710 | return rx_dtap(scu, a_conn_info, msg); |
| 711 | break; |
| 712 | default: |
| 713 | LOGP(DMSC, LOGL_ERROR, "Unimplemented BSSAP msg type: %s\n", gsm0808_bssap_name(msg->l2h[0])); |
| 714 | msgb_free(msg); |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | |
| 718 | return -EINVAL; |
| 719 | } |