blob: 9a7dcd02324feb85b09c92978c5322c0b8203cb2 [file] [log] [blame]
Philipp Maierfbf66102017-04-09 12:32:51 +02001/* (C) 2017 by sysmocom s.f.m.c. GmbH
Harald Weltec9e78592018-02-09 01:42:50 +01002 * (C) 2018 by Harald Welte <laforge@gnumonks.org>
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003 * All Rights Reserved
4 *
Philipp Maierfbf66102017-04-09 12:32:51 +02005 * Author: Philipp Maier
6 *
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02007 * 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
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
15 * GNU Affero General Public License for more details.
16 *
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/>.
19 *
20 */
21
Philipp Maierfbf66102017-04-09 12:32:51 +020022#include <osmocom/core/utils.h>
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020023#include <osmocom/core/msgb.h>
24#include <osmocom/core/logging.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020025#include <osmocom/sigtran/sccp_helpers.h>
26#include <osmocom/sigtran/sccp_sap.h>
27#include <osmocom/sigtran/osmo_ss7.h>
28#include <osmocom/sigtran/protocol/m3ua.h>
29#include <osmocom/gsm/gsm0808.h>
30#include <osmocom/gsm/protocol/gsm_08_08.h>
31#include <osmocom/gsm/protocol/gsm_04_08.h>
32#include <osmocom/gsm/gsm0808_utils.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020033#include <osmocom/msc/debug.h>
34#include <osmocom/msc/msc_ifaces.h>
35#include <osmocom/msc/a_iface.h>
36#include <osmocom/msc/a_iface_bssap.h>
37#include <osmocom/msc/transaction.h>
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +020038#include <osmocom/mgcp_client/mgcp_client.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020039#include <osmocom/core/byteswap.h>
40#include <osmocom/sccp/sccp_types.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020041#include <osmocom/msc/a_reset.h>
Neels Hofmeyrf879fc92017-12-14 03:52:18 +010042#include <osmocom/msc/vlr.h>
Neels Hofmeyr46c06e22019-01-04 17:42:05 +010043#include <osmocom/msc/ran_conn.h>
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020044
Max43b01b02017-09-15 11:22:30 +020045#include <errno.h>
46
Neels Hofmeyr46c06e22019-01-04 17:42:05 +010047#define LOGPCONN LOG_RAN_CONN
48
Philipp Maierfbf66102017-04-09 12:32:51 +020049/* A pointer to the GSM network we work with. By the current paradigm,
50 * there can only be one gsm_network per MSC. The pointer is set once
51 * when calling a_init() */
52static struct gsm_network *gsm_network = NULL;
53
54/* A struct to track currently active connections. We need that information
55 * to handle failure sitautions. In case of a problem, we must know which
56 * connections are currently open and which BSC is responsible. We also need
57 * the data to perform our connection checks (a_reset). All other logic will
58 * look at the connection ids and addresses that are supplied by the
59 * primitives */
60struct bsc_conn {
61 struct llist_head list;
62 uint32_t conn_id; /* Connection identifier */
Harald Welte54a10ef2018-02-09 00:09:16 +010063 struct bsc_context *bsc;
Philipp Maierfbf66102017-04-09 12:32:51 +020064};
65
66/* Internal list with connections we currently maintain. This
67 * list is of type struct bsc_conn (see above) */
68static LLIST_HEAD(active_connections);
69
70/* Record info of a new active connection in the active connection list */
Harald Welte54a10ef2018-02-09 00:09:16 +010071static void record_bsc_con(const void *ctx, struct bsc_context *bsc, uint32_t conn_id)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020072{
Philipp Maierfbf66102017-04-09 12:32:51 +020073 struct bsc_conn *conn;
74
75 conn = talloc_zero(ctx, struct bsc_conn);
76 OSMO_ASSERT(conn);
77
78 conn->conn_id = conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +010079 conn->bsc = bsc;
Philipp Maierfbf66102017-04-09 12:32:51 +020080
81 llist_add_tail(&conn->list, &active_connections);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020082}
83
Philipp Maierfbf66102017-04-09 12:32:51 +020084/* Delete info of a closed connection from the active connection list */
85void a_delete_bsc_con(uint32_t conn_id)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020086{
Philipp Maierfbf66102017-04-09 12:32:51 +020087 struct bsc_conn *conn;
88 struct bsc_conn *conn_temp;
89
Philipp Maierfbf66102017-04-09 12:32:51 +020090 llist_for_each_entry_safe(conn, conn_temp, &active_connections, list) {
91 if (conn->conn_id == conn_id) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +010092 LOGP(DBSSAP, LOGL_DEBUG, "(conn%u) Removing A-interface conn\n", conn->conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +020093 llist_del(&conn->list);
94 talloc_free(conn);
95 }
96 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020097}
98
Harald Welte54a10ef2018-02-09 00:09:16 +010099/* Find a specified connection id */
100static struct bsc_conn *find_bsc_con(uint32_t conn_id)
Philipp Maierfbf66102017-04-09 12:32:51 +0200101{
102 struct bsc_conn *conn;
103
104 /* Find the address for the current connection id */
105 llist_for_each_entry(conn, &active_connections, list) {
106 if (conn->conn_id == conn_id) {
Harald Welte54a10ef2018-02-09 00:09:16 +0100107 return conn;
Philipp Maierfbf66102017-04-09 12:32:51 +0200108 }
109 }
110
Harald Welte54a10ef2018-02-09 00:09:16 +0100111 return NULL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200112}
113
Harald Welte54a10ef2018-02-09 00:09:16 +0100114/* Check if a specified connection id has an active SCCP connection */
115static bool check_connection_active(uint32_t conn_id)
116{
117 if (find_bsc_con(conn_id))
118 return true;
119 else
120 return false;
121}
122
123/* Get the context for a specific calling (BSC) address */
124static struct bsc_context *get_bsc_context_by_sccp_addr(const struct osmo_sccp_addr *addr)
Philipp Maierfbf66102017-04-09 12:32:51 +0200125{
126 struct bsc_context *bsc_ctx;
127 struct osmo_ss7_instance *ss7;
128
129 if (!addr)
130 return NULL;
131
132 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
133 if (memcmp(&bsc_ctx->bsc_addr, addr, sizeof(*addr)) == 0)
Harald Welte54a10ef2018-02-09 00:09:16 +0100134 return bsc_ctx;
Philipp Maierfbf66102017-04-09 12:32:51 +0200135 }
136
137 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
138 OSMO_ASSERT(ss7);
Harald Welte1f477442018-02-09 01:49:01 +0100139 LOGP(DBSSAP, LOGL_NOTICE, "The calling BSC (%s) is unknown to this MSC ...\n",
Philipp Maierfbf66102017-04-09 12:32:51 +0200140 osmo_sccp_addr_name(ss7, addr));
141 return NULL;
142}
143
Harald Welte977b5482019-02-18 12:23:43 +0100144/* wrapper around osmo_sccp_tx_data_msg(): Transmit a fully encoded BSSAP (DTAP or BSSMAP) message */
145static int a_iface_tx_bssap(const struct ran_conn *conn, struct msgb *msg)
146{
147 OSMO_ASSERT(conn);
148 OSMO_ASSERT(conn->a.scu);
149
Harald Welte390d1402019-02-18 12:55:31 +0100150 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", msgb_hexdump_l3(msg));
Harald Welte977b5482019-02-18 12:23:43 +0100151 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
152}
153
Philipp Maier4502f5f2017-09-07 11:39:58 +0200154/* Send DTAP message via A-interface, take ownership of msg */
Philipp Maierfbf66102017-04-09 12:32:51 +0200155int a_iface_tx_dtap(struct msgb *msg)
156{
Harald Weltefd96d452019-02-18 12:28:09 +0100157 const struct ran_conn *conn;
Philipp Maierfbf66102017-04-09 12:32:51 +0200158 struct msgb *msg_resp;
159
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200160 uint8_t link_id = OMSC_LINKID_CB(msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200161 OSMO_ASSERT(msg);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100162 conn = (struct ran_conn *)msg->dst;
Philipp Maierfbf66102017-04-09 12:32:51 +0200163 OSMO_ASSERT(conn);
Philipp Maierfbf66102017-04-09 12:32:51 +0200164
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200165 LOGPCONN(conn, LOGL_DEBUG, "Passing DTAP message (DLCI=0x%02x) from MSC to BSC\n", link_id);
Philipp Maierfbf66102017-04-09 12:32:51 +0200166
167 msg->l3h = msg->data;
168 msg_resp = gsm0808_create_dtap(msg, link_id);
Philipp Maier4502f5f2017-09-07 11:39:58 +0200169
170 /* gsm0808_create_dtap() has copied the data to msg_resp,
171 * so msg has served its purpose now */
172 msgb_free(msg);
173
Philipp Maierfbf66102017-04-09 12:32:51 +0200174 if (!msg_resp) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100175 LOGPCONN(conn, LOGL_ERROR, "Unable to generate BSSMAP DTAP message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200176 return -EINVAL;
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100177 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200178
Philipp Maier4502f5f2017-09-07 11:39:58 +0200179 /* osmo_sccp_tx_data_msg() takes ownership of msg_resp */
Harald Welte977b5482019-02-18 12:23:43 +0100180 return a_iface_tx_bssap(conn, msg_resp);
Philipp Maierfbf66102017-04-09 12:32:51 +0200181}
182
183/* Send Cipher mode command via A-interface */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100184int a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100185 struct gsm0808_encrypt_info *ei, int include_imeisv)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200186{
187 /* TODO generalize for A- and Iu interfaces, don't name after 08.08 */
Philipp Maierfbf66102017-04-09 12:32:51 +0200188 struct msgb *msg_resp;
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100189 uint8_t crm = 0x01;
Philipp Maierfbf66102017-04-09 12:32:51 +0200190
191 OSMO_ASSERT(conn);
Harald Weltefb7ba912018-02-09 01:05:27 +0100192 LOGPCONN(conn, LOGL_DEBUG, "Tx BSSMAP CIPHER MODE COMMAND to BSC, %u ciphers (%s)",
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100193 ei->perm_algo_len, osmo_hexdump_nospc(ei->perm_algo, ei->perm_algo_len));
Harald Welte1f477442018-02-09 01:49:01 +0100194 LOGPC(DBSSAP, LOGL_DEBUG, " key %s\n", osmo_hexdump_nospc(ei->key, ei->key_len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200195
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100196 msg_resp = gsm0808_create_cipher(ei, include_imeisv ? &crm : NULL);
Harald Welte977b5482019-02-18 12:23:43 +0100197 return a_iface_tx_bssap(conn, msg_resp);
Philipp Maierfbf66102017-04-09 12:32:51 +0200198}
199
200/* Page a subscriber via A-interface */
201int a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
202{
203 struct bsc_context *bsc_ctx;
Stefan Sperling621c7292018-02-16 13:40:42 +0100204 struct gsm0808_cell_id_list2 cil;
Philipp Maierfbf66102017-04-09 12:32:51 +0200205 struct msgb *msg;
206 int page_count = 0;
207 struct osmo_ss7_instance *ss7;
208
209 OSMO_ASSERT(imsi);
210
211 cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling621c7292018-02-16 13:40:42 +0100212 cil.id_list[0].lac = lac;
Philipp Maierfbf66102017-04-09 12:32:51 +0200213 cil.id_list_len = 1;
214
215 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
216 OSMO_ASSERT(ss7);
217
218 /* Deliver paging request to all known BSCs */
219 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
Philipp Maierf913e5f2018-05-07 10:15:49 +0200220 if (a_reset_conn_ready(bsc_ctx->reset_fsm)) {
Harald Welte1f477442018-02-09 01:49:01 +0100221 LOGP(DBSSAP, LOGL_DEBUG,
Harald Weltefb7ba912018-02-09 01:05:27 +0100222 "Tx BSSMAP paging message from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
Philipp Maierfbf66102017-04-09 12:32:51 +0200223 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
224 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
Stefan Sperling621c7292018-02-16 13:40:42 +0100225 msg = gsm0808_create_paging2(imsi, &tmsi, &cil, NULL);
Philipp Maierfbf66102017-04-09 12:32:51 +0200226 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user,
227 &bsc_ctx->msc_addr, &bsc_ctx->bsc_addr, msg);
228 page_count++;
229 } else {
Harald Welte1f477442018-02-09 01:49:01 +0100230 LOGP(DBSSAP, LOGL_DEBUG,
Philipp Maierfbf66102017-04-09 12:32:51 +0200231 "Connection down, dropping paging from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
232 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
233 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
234 }
235 }
236
237 if (page_count <= 0)
Harald Welte1f477442018-02-09 01:49:01 +0100238 LOGP(DBSSAP, LOGL_ERROR, "Could not deliver paging because none of the associated BSCs is available!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200239
240 return page_count;
241}
242
243/* Convert speech version field */
Harald Welte474e5a72018-02-12 10:12:35 +0100244static uint8_t convert_speech_version_l3_to_A(int speech_ver)
Philipp Maierfbf66102017-04-09 12:32:51 +0200245{
246 /* The speech versions that are transmitted in the Bearer capability
Harald Weltef417b8c2018-02-12 10:10:05 +0100247 * information element, that is transmitted on the Layer 3 (CC)
Philipp Maierfbf66102017-04-09 12:32:51 +0200248 * use a different encoding than the permitted speech version
249 * identifier, that is signalled in the channel type element on the A
250 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
251 * 10.5.103 */
252
253 switch (speech_ver) {
254 case GSM48_BCAP_SV_FR:
255 return GSM0808_PERM_FR1;
Philipp Maierfbf66102017-04-09 12:32:51 +0200256 case GSM48_BCAP_SV_HR:
257 return GSM0808_PERM_HR1;
Philipp Maierfbf66102017-04-09 12:32:51 +0200258 case GSM48_BCAP_SV_EFR:
259 return GSM0808_PERM_FR2;
Philipp Maierfbf66102017-04-09 12:32:51 +0200260 case GSM48_BCAP_SV_AMR_F:
261 return GSM0808_PERM_FR3;
Philipp Maierfbf66102017-04-09 12:32:51 +0200262 case GSM48_BCAP_SV_AMR_H:
263 return GSM0808_PERM_HR3;
Philipp Maierfbf66102017-04-09 12:32:51 +0200264 case GSM48_BCAP_SV_AMR_OFW:
265 return GSM0808_PERM_FR4;
Philipp Maierfbf66102017-04-09 12:32:51 +0200266 case GSM48_BCAP_SV_AMR_OHW:
267 return GSM0808_PERM_HR4;
Philipp Maierfbf66102017-04-09 12:32:51 +0200268 case GSM48_BCAP_SV_AMR_FW:
269 return GSM0808_PERM_FR5;
Philipp Maierfbf66102017-04-09 12:32:51 +0200270 case GSM48_BCAP_SV_AMR_OH:
271 return GSM0808_PERM_HR6;
Philipp Maierfbf66102017-04-09 12:32:51 +0200272 }
273
274 /* If nothing matches, tag the result as invalid */
Harald Welte1f477442018-02-09 01:49:01 +0100275 LOGP(DBSSAP, LOGL_ERROR, "Invalid permitted speech version: %d\n", speech_ver);
Philipp Maierfbf66102017-04-09 12:32:51 +0200276 return 0xFF;
277}
278
279/* Convert speech preference field */
Harald Welte474e5a72018-02-12 10:12:35 +0100280static uint8_t convert_speech_pref_l3_to_A(int radio)
Philipp Maierfbf66102017-04-09 12:32:51 +0200281{
282 /* The Radio channel requirement field that is transmitted in the
283 * Bearer capability information element, that is transmitted on the
Harald Weltef417b8c2018-02-12 10:10:05 +0100284 * Layer 3 (CC) uses a different encoding than the Channel rate and
Philipp Maierfbf66102017-04-09 12:32:51 +0200285 * type field that is signalled in the channel type element on the A
286 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
287 * 10.5.102 */
288
289 switch (radio) {
290 case GSM48_BCAP_RRQ_FR_ONLY:
291 return GSM0808_SPEECH_FULL_BM;
292 case GSM48_BCAP_RRQ_DUAL_FR:
293 return GSM0808_SPEECH_FULL_PREF;
294 case GSM48_BCAP_RRQ_DUAL_HR:
295 return GSM0808_SPEECH_HALF_PREF;
296 }
297
Harald Welte1f477442018-02-09 01:49:01 +0100298 LOGP(DBSSAP, LOGL_ERROR, "Invalid radio channel preference: %d; defaulting to full rate.\n",
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100299 radio);
Philipp Maierfbf66102017-04-09 12:32:51 +0200300 return GSM0808_SPEECH_FULL_BM;
301}
302
303/* Assemble the channel type field */
304static int enc_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc)
305{
306 unsigned int i;
307 uint8_t sv;
308 unsigned int count = 0;
309 bool only_gsm_hr = true;
310
311 OSMO_ASSERT(ct);
312 OSMO_ASSERT(bc);
313
314 ct->ch_indctr = GSM0808_CHAN_SPEECH;
315
316 for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) {
317 if (bc->speech_ver[i] == -1)
318 break;
Harald Welte474e5a72018-02-12 10:12:35 +0100319 sv = convert_speech_version_l3_to_A(bc->speech_ver[i]);
Philipp Maierfbf66102017-04-09 12:32:51 +0200320 if (sv != 0xFF) {
321 /* Detect if something else than
322 * GSM HR V1 is supported */
323 if (sv == GSM0808_PERM_HR2 ||
324 sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6)
325 only_gsm_hr = false;
326
327 ct->perm_spch[count] = sv;
328 count++;
329 }
330 }
331 ct->perm_spch_len = count;
332
333 if (only_gsm_hr)
334 /* Note: We must avoid the usage of GSM HR1 as this
335 * codec only offers very poor audio quality. If the
336 * MS only supports GSM HR1 (and full rate), and has
337 * a preference for half rate. Then we will ignore the
338 * preference and assume a preference for full rate. */
339 ct->ch_rate_type = GSM0808_SPEECH_FULL_BM;
340 else
Harald Welte474e5a72018-02-12 10:12:35 +0100341 ct->ch_rate_type = convert_speech_pref_l3_to_A(bc->radio);
Philipp Maierfbf66102017-04-09 12:32:51 +0200342
343 if (count)
344 return 0;
345 else
346 return -EINVAL;
347}
348
349/* Assemble the speech codec field */
350static int enc_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
351{
352 unsigned int i;
353 int rc;
354
355 memset(scl, 0, sizeof(*scl));
356 for (i = 0; i < ct->perm_spch_len; i++) {
357 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
358 if (rc != 0)
359 return -EINVAL;
360 }
361 scl->len = i;
362
363 return 0;
364}
365
366/* Send assignment request via A-interface */
367int a_iface_tx_assignment(const struct gsm_trans *trans)
368{
Harald Weltefd96d452019-02-18 12:28:09 +0100369 const struct ran_conn *conn;
Philipp Maierfbf66102017-04-09 12:32:51 +0200370 struct gsm0808_channel_type ct;
371 struct gsm0808_speech_codec_list scl;
Philipp Maierfbf66102017-04-09 12:32:51 +0200372 struct msgb *msg;
373 struct sockaddr_storage rtp_addr;
374 struct sockaddr_in rtp_addr_in;
375 int rc;
376
377 OSMO_ASSERT(trans);
378 conn = trans->conn;
379 OSMO_ASSERT(conn);
380
Harald Weltefb7ba912018-02-09 01:05:27 +0100381 LOGPCONN(conn, LOGL_DEBUG, "Tx BSSMAP ASSIGNMENT COMMAND to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200382
383 /* Channel type */
384 rc = enc_channel_type(&ct, &trans->bearer_cap);
385 if (rc < 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100386 LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate channel type\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200387 return -EINVAL;
388 }
389
390 /* Speech codec list */
391 rc = enc_speech_codec_list(&scl, &ct);
392 if (rc < 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100393 LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate speech codec list\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200394 return -EINVAL;
395 }
396
397 /* Package RTP-Address data */
398 memset(&rtp_addr_in, 0, sizeof(rtp_addr_in));
399 rtp_addr_in.sin_family = AF_INET;
Philipp Maier621ba032017-11-07 17:19:25 +0100400 rtp_addr_in.sin_port = osmo_htons(conn->rtp.local_port_ran);
401 rtp_addr_in.sin_addr.s_addr = inet_addr(conn->rtp.local_addr_ran);
402
403 if (rtp_addr_in.sin_addr.s_addr == INADDR_NONE) {
404 LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Address -- assignment not sent!\n");
405 return -EINVAL;
406 }
407 if (rtp_addr_in.sin_port == 0) {
408 LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Port -- assignment not sent!\n");
409 return -EINVAL;
410 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200411
412 memset(&rtp_addr, 0, sizeof(rtp_addr));
413 memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in));
414
Max020ec7a2018-12-17 16:03:55 +0100415 msg = gsm0808_create_ass(&ct, NULL, &rtp_addr, &scl, NULL);
Harald Welte977b5482019-02-18 12:23:43 +0100416 return a_iface_tx_bssap(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200417}
418
419/* Send clear command via A-interface */
Harald Weltefd96d452019-02-18 12:28:09 +0100420int a_iface_tx_clear_cmd(const struct ran_conn *conn)
Philipp Maierfbf66102017-04-09 12:32:51 +0200421{
422 struct msgb *msg;
Philipp Maier896950a2019-02-04 16:46:55 +0100423 struct vlr_subscr *vsub = conn->vsub;
424 bool csfb_ind = false;
Philipp Maierfbf66102017-04-09 12:32:51 +0200425
Harald Weltefb7ba912018-02-09 01:05:27 +0100426 LOGPCONN(conn, LOGL_INFO, "Tx BSSMAP CLEAR COMMAND to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200427
Philipp Maier896950a2019-02-04 16:46:55 +0100428 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
429 csfb_ind = true;
430
431 msg = gsm0808_create_clear_command2(GSM0808_CAUSE_CALL_CONTROL, csfb_ind);
Harald Welte977b5482019-02-18 12:23:43 +0100432 return a_iface_tx_bssap(conn, msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200433}
434
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100435int a_iface_tx_classmark_request(const struct ran_conn *conn)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200436{
437 struct msgb *msg;
438
439 LOGPCONN(conn, LOGL_INFO, "Tx BSSMAP CLASSMARK REQUEST to BSC\n");
440
441 msg = gsm0808_create_classmark_request();
Harald Welte977b5482019-02-18 12:23:43 +0100442 return a_iface_tx_bssap(conn, msg);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200443}
444
Philipp Maierfbf66102017-04-09 12:32:51 +0200445/* Callback function: Close all open connections */
446static void a_reset_cb(const void *priv)
447{
448 struct msgb *msg;
449 struct bsc_context *bsc_ctx = (struct bsc_context*) priv;
450 struct osmo_ss7_instance *ss7;
451
452 /* Skip if the A interface is not properly initalized yet */
453 if (!gsm_network)
454 return;
455
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100456 /* Clear all now orphaned RAN connections */
Philipp Maierfbf66102017-04-09 12:32:51 +0200457 a_clear_all(bsc_ctx->sccp_user, &bsc_ctx->bsc_addr);
458
459 /* Send reset to the remote BSC */
460 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
461 OSMO_ASSERT(ss7);
Harald Welte1f477442018-02-09 01:49:01 +0100462 LOGP(DBSSAP, LOGL_NOTICE, "Tx BSSMAP RESET to BSC %s\n", osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr));
Philipp Maierfbf66102017-04-09 12:32:51 +0200463 msg = gsm0808_create_reset();
464 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, &bsc_ctx->msc_addr,
465 &bsc_ctx->bsc_addr, msg);
466}
467
468/* Add a new BSC connection to our internal list with known BSCs */
Harald Welte54a10ef2018-02-09 00:09:16 +0100469static struct bsc_context *add_bsc(const struct osmo_sccp_addr *msc_addr,
470 const struct osmo_sccp_addr *bsc_addr, struct osmo_sccp_user *scu)
Philipp Maierfbf66102017-04-09 12:32:51 +0200471{
472 struct bsc_context *bsc_ctx;
473 struct osmo_ss7_instance *ss7;
Philipp Maierfbf66102017-04-09 12:32:51 +0200474
475 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
476 OSMO_ASSERT(ss7);
Harald Welte1f477442018-02-09 01:49:01 +0100477 LOGP(DBSSAP, LOGL_NOTICE, "Adding new BSC connection for BSC %s...\n", osmo_sccp_addr_name(ss7, bsc_addr));
Philipp Maierfbf66102017-04-09 12:32:51 +0200478
479 /* Generate and fill up a new bsc context */
480 bsc_ctx = talloc_zero(gsm_network, struct bsc_context);
481 OSMO_ASSERT(bsc_ctx);
482 memcpy(&bsc_ctx->bsc_addr, bsc_addr, sizeof(*bsc_addr));
483 memcpy(&bsc_ctx->msc_addr, msc_addr, sizeof(*msc_addr));
484 bsc_ctx->sccp_user = scu;
485 llist_add_tail(&bsc_ctx->list, &gsm_network->a.bscs);
486
Harald Welte54a10ef2018-02-09 00:09:16 +0100487 return bsc_ctx;
488}
489
490/* start the BSSMAP RESET fsm */
491void a_start_reset(struct bsc_context *bsc_ctx, bool already_connected)
492{
493 char bsc_name[32];
Philipp Maierf913e5f2018-05-07 10:15:49 +0200494 OSMO_ASSERT(bsc_ctx->reset_fsm == NULL);
Philipp Maierfbf66102017-04-09 12:32:51 +0200495 /* Start reset procedure to make the new connection active */
Harald Welte54a10ef2018-02-09 00:09:16 +0100496 snprintf(bsc_name, sizeof(bsc_name), "bsc-%i", bsc_ctx->bsc_addr.pc);
Philipp Maierf913e5f2018-05-07 10:15:49 +0200497 bsc_ctx->reset_fsm = a_reset_alloc(bsc_ctx, bsc_name, a_reset_cb, bsc_ctx, already_connected);
Harald Welte54a10ef2018-02-09 00:09:16 +0100498}
499
Philipp Maierce1298b2018-02-22 16:44:41 +0100500/* determine if given msg is BSSMAP RESET related (true) or not (false) */
Harald Welte54a10ef2018-02-09 00:09:16 +0100501static bool bssmap_is_reset(struct msgb *msg)
502{
503 struct bssmap_header *bs = (struct bssmap_header *)msgb_l2(msg);
504
505 if (msgb_l2len(msg) < sizeof(*bs))
506 return false;
507
508 if (bs->type != BSSAP_MSG_BSS_MANAGEMENT)
509 return false;
510
511 if (msg->l2h[sizeof(*bs)] == BSS_MAP_MSG_RESET)
512 return true;
513
Philipp Maierce1298b2018-02-22 16:44:41 +0100514 if (msg->l2h[sizeof(*bs)] == BSS_MAP_MSG_RESET_ACKNOWLEDGE)
515 return true;
516
Harald Welte54a10ef2018-02-09 00:09:16 +0100517 return false;
Philipp Maierfbf66102017-04-09 12:32:51 +0200518}
519
520/* Callback function, called by the SSCP stack when data arrives */
521static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
522{
523 struct osmo_sccp_user *scu = _scu;
524 struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph;
525 int rc = 0;
526 struct a_conn_info a_conn_info;
Harald Welte54a10ef2018-02-09 00:09:16 +0100527 struct bsc_conn *bsc_con;
528
Philipp Maierfbf66102017-04-09 12:32:51 +0200529 memset(&a_conn_info, 0, sizeof(a_conn_info));
530 a_conn_info.network = gsm_network;
Philipp Maierfbf66102017-04-09 12:32:51 +0200531
532 switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
533 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
534 /* Handle inbound connection indication */
Philipp Maierfbf66102017-04-09 12:32:51 +0200535 a_conn_info.conn_id = scu_prim->u.connect.conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +0100536 a_conn_info.bsc = get_bsc_context_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
537 if (!a_conn_info.bsc) {
538 /* We haven't heard from this BSC before, allocate it */
539 a_conn_info.bsc = add_bsc(&scu_prim->u.connect.called_addr,
540 &scu_prim->u.connect.calling_addr, scu);
541 a_start_reset(a_conn_info.bsc, false);
542 } else {
543 /* This BSC is already known to us, check if we have been through reset yet */
Philipp Maierf913e5f2018-05-07 10:15:49 +0200544 if (a_reset_conn_ready(a_conn_info.bsc->reset_fsm) == false) {
Harald Welte1f477442018-02-09 01:49:01 +0100545 LOGP(DBSSAP, LOGL_NOTICE, "Refusing N-CONNECT.ind(%u, %s), BSC not reset yet\n",
Harald Weltea41b6302018-02-09 00:27:56 +0100546 scu_prim->u.connect.conn_id, msgb_hexdump_l2(oph->msg));
Harald Welte54a10ef2018-02-09 00:09:16 +0100547 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, &a_conn_info.bsc->msc_addr,
548 SCCP_RETURN_CAUSE_UNQUALIFIED);
549 break;
550 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200551
Harald Welte54a10ef2018-02-09 00:09:16 +0100552 osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0);
553 if (msgb_l2len(oph->msg) > 0) {
Harald Welte1f477442018-02-09 01:49:01 +0100554 LOGP(DBSSAP, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n",
Harald Weltea41b6302018-02-09 00:27:56 +0100555 scu_prim->u.connect.conn_id, msgb_hexdump_l2(oph->msg));
Harald Welte54a10ef2018-02-09 00:09:16 +0100556 rc = a_sccp_rx_dt(scu, &a_conn_info, oph->msg);
Harald Welte8a991ed2018-03-18 21:56:04 +0100557 } else {
Harald Welte1f477442018-02-09 01:49:01 +0100558 LOGP(DBSSAP, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id);
Harald Welte8a991ed2018-03-18 21:56:04 +0100559 rc = -ENODATA;
560 }
561
562 if (rc < 0) {
563 /* initial message (COMPL L3) caused some error, we didn't allocate
564 * a subscriber_conn and must close the connection again */
565 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id,
566 &a_conn_info.bsc->msc_addr,
567 SCCP_RETURN_CAUSE_UNQUALIFIED);
568 } else
569 record_bsc_con(scu, a_conn_info.bsc, scu_prim->u.connect.conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +0200570 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200571 break;
572
573 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
574 /* Handle incoming connection oriented data */
Harald Welte54a10ef2018-02-09 00:09:16 +0100575 bsc_con = find_bsc_con(scu_prim->u.data.conn_id);
576 if (!bsc_con) {
Harald Welte1f477442018-02-09 01:49:01 +0100577 LOGP(DBSSAP, LOGL_ERROR, "N-DATA.ind(%u, %s) for unknown conn_id\n",
Harald Weltea41b6302018-02-09 00:27:56 +0100578 scu_prim->u.data.conn_id, msgb_hexdump_l2(oph->msg));
Harald Welte54a10ef2018-02-09 00:09:16 +0100579 break;
580 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200581 a_conn_info.conn_id = scu_prim->u.data.conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +0100582 a_conn_info.bsc = bsc_con->bsc;
Harald Welte1f477442018-02-09 01:49:01 +0100583 LOGP(DBSSAP, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n",
Harald Weltea41b6302018-02-09 00:27:56 +0100584 scu_prim->u.data.conn_id, msgb_hexdump_l2(oph->msg));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100585 a_sccp_rx_dt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200586 break;
587
588 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
589 /* Handle inbound UNITDATA */
Philipp Maierce1298b2018-02-22 16:44:41 +0100590
591 /* Get BSC context, create a new one if necessary */
Harald Welte54a10ef2018-02-09 00:09:16 +0100592 a_conn_info.bsc = get_bsc_context_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
593 if (!a_conn_info.bsc) {
594 /* We haven't heard from this BSC before, allocate it */
595 a_conn_info.bsc = add_bsc(&scu_prim->u.unitdata.called_addr,
596 &scu_prim->u.unitdata.calling_addr, scu);
Philipp Maierce1298b2018-02-22 16:44:41 +0100597 /* Make sure that reset procedure is started */
598 a_start_reset(a_conn_info.bsc, false);
Harald Welte54a10ef2018-02-09 00:09:16 +0100599 }
Philipp Maierce1298b2018-02-22 16:44:41 +0100600
601 /* As long as we are in the reset phase, only reset related BSSMAP messages may pass
602 * beond here. */
Philipp Maierf913e5f2018-05-07 10:15:49 +0200603 if (!bssmap_is_reset(oph->msg) && a_reset_conn_ready(a_conn_info.bsc->reset_fsm) == false) {
Philipp Maierce1298b2018-02-22 16:44:41 +0100604 LOGP(DBSSAP, LOGL_NOTICE, "Ignoring N-UNITDATA.ind(%s), BSC not reset yet\n",
605 msgb_hexdump_l2(oph->msg));
606 break;
607 }
608
Harald Welte1f477442018-02-09 01:49:01 +0100609 DEBUGP(DBSSAP, "N-UNITDATA.ind(%s)\n", msgb_hexdump_l2(oph->msg));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100610 a_sccp_rx_udt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200611 break;
612
613 default:
Harald Welte1f477442018-02-09 01:49:01 +0100614 LOGP(DBSSAP, LOGL_ERROR, "Unhandled SIGTRAN operation %s on primitive %u\n",
Maxc309fe32018-01-24 14:02:38 +0100615 get_value_string(osmo_prim_op_names, oph->operation), oph->primitive);
Philipp Maierfbf66102017-04-09 12:32:51 +0200616 break;
617 }
618
Harald Weltea172e9e2018-02-09 21:33:24 +0100619 /* We didn't transfer msgb ownership to any downstream functions so we rely on
620 * this single/central location to free() the msgb wrapping the primitive */
621 msgb_free(oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200622 return rc;
623}
624
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100625/* Clear all RAN connections on a specified BSC */
Philipp Maierfbf66102017-04-09 12:32:51 +0200626void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr)
627{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100628 struct ran_conn *conn;
629 struct ran_conn *conn_temp;
Philipp Maierfbf66102017-04-09 12:32:51 +0200630 struct gsm_network *network = gsm_network;
631
632 OSMO_ASSERT(scu);
633 OSMO_ASSERT(bsc_addr);
634
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100635 llist_for_each_entry_safe(conn, conn_temp, &network->ran_conns, entry) {
Philipp Maierfbf66102017-04-09 12:32:51 +0200636 /* Clear only A connections and connections that actually
637 * belong to the specified BSC */
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100638 if (conn->via_ran == OSMO_RAT_GERAN_A && memcmp(bsc_addr, &conn->a.bsc_addr, sizeof(conn->a.bsc_addr)) == 0) {
Harald Welte80620d22018-02-10 10:24:15 +0100639 uint32_t conn_id = conn->a.conn_id;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100640 LOGPCONN(conn, LOGL_NOTICE, "Dropping orphaned RAN connection\n");
Harald Welte80620d22018-02-10 10:24:15 +0100641 /* This call will/may talloc_free(conn), so we must save conn_id above */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100642 ran_conn_clear_request(conn, GSM48_CC_CAUSE_SWITCH_CONG);
Philipp Maierfbf66102017-04-09 12:32:51 +0200643
644 /* If there is still an SCCP connection active, remove it now */
Harald Welte80620d22018-02-10 10:24:15 +0100645 if (check_connection_active(conn_id)) {
646 osmo_sccp_tx_disconn(scu, conn_id, bsc_addr,
Philipp Maierfbf66102017-04-09 12:32:51 +0200647 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
Harald Welte80620d22018-02-10 10:24:15 +0100648 a_delete_bsc_con(conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +0200649 }
650 }
651 }
652}
653
654/* Initalize A interface connection between to MSC and BSC */
655int a_init(struct osmo_sccp_instance *sccp, struct gsm_network *network)
656{
657 OSMO_ASSERT(sccp);
658 OSMO_ASSERT(network);
659
660 /* FIXME: Remove hardcoded parameters, use parameters in parameter list */
Harald Welte1f477442018-02-09 01:49:01 +0100661 LOGP(DBSSAP, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200662
663 /* Set GSM network variable, there can only be
664 * one network by design */
665 if (gsm_network != NULL) {
666 OSMO_ASSERT(gsm_network == network);
667 } else
668 gsm_network = network;
669
670 /* SCCP Protocol stack */
671 osmo_sccp_user_bind(sccp, "OsmoMSC-A", sccp_sap_up, SCCP_SSN_BSSAP);
672
673 return 0;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200674}