blob: 2b1baf0fbe5868630180a0540b2668f06803e575 [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>
42#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyrf879fc92017-12-14 03:52:18 +010043#include <osmocom/msc/vlr.h>
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020044
Max43b01b02017-09-15 11:22:30 +020045#include <errno.h>
46
Philipp Maierfbf66102017-04-09 12:32:51 +020047/* A pointer to the GSM network we work with. By the current paradigm,
48 * there can only be one gsm_network per MSC. The pointer is set once
49 * when calling a_init() */
50static struct gsm_network *gsm_network = NULL;
51
52/* A struct to track currently active connections. We need that information
53 * to handle failure sitautions. In case of a problem, we must know which
54 * connections are currently open and which BSC is responsible. We also need
55 * the data to perform our connection checks (a_reset). All other logic will
56 * look at the connection ids and addresses that are supplied by the
57 * primitives */
58struct bsc_conn {
59 struct llist_head list;
60 uint32_t conn_id; /* Connection identifier */
Harald Welte54a10ef2018-02-09 00:09:16 +010061 struct bsc_context *bsc;
Philipp Maierfbf66102017-04-09 12:32:51 +020062};
63
64/* Internal list with connections we currently maintain. This
65 * list is of type struct bsc_conn (see above) */
66static LLIST_HEAD(active_connections);
67
68/* Record info of a new active connection in the active connection list */
Harald Welte54a10ef2018-02-09 00:09:16 +010069static void record_bsc_con(const void *ctx, struct bsc_context *bsc, uint32_t conn_id)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020070{
Philipp Maierfbf66102017-04-09 12:32:51 +020071 struct bsc_conn *conn;
72
73 conn = talloc_zero(ctx, struct bsc_conn);
74 OSMO_ASSERT(conn);
75
76 conn->conn_id = conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +010077 conn->bsc = bsc;
Philipp Maierfbf66102017-04-09 12:32:51 +020078
79 llist_add_tail(&conn->list, &active_connections);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020080}
81
Philipp Maierfbf66102017-04-09 12:32:51 +020082/* Delete info of a closed connection from the active connection list */
83void a_delete_bsc_con(uint32_t conn_id)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020084{
Philipp Maierfbf66102017-04-09 12:32:51 +020085 struct bsc_conn *conn;
86 struct bsc_conn *conn_temp;
87
Philipp Maierfbf66102017-04-09 12:32:51 +020088 llist_for_each_entry_safe(conn, conn_temp, &active_connections, list) {
89 if (conn->conn_id == conn_id) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +010090 LOGPBSCCONN(conn, LOGL_DEBUG, "Removing A-interface conn\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020091 llist_del(&conn->list);
92 talloc_free(conn);
93 }
94 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020095}
96
Harald Welte54a10ef2018-02-09 00:09:16 +010097/* Find a specified connection id */
98static struct bsc_conn *find_bsc_con(uint32_t conn_id)
Philipp Maierfbf66102017-04-09 12:32:51 +020099{
100 struct bsc_conn *conn;
101
102 /* Find the address for the current connection id */
103 llist_for_each_entry(conn, &active_connections, list) {
104 if (conn->conn_id == conn_id) {
Harald Welte54a10ef2018-02-09 00:09:16 +0100105 return conn;
Philipp Maierfbf66102017-04-09 12:32:51 +0200106 }
107 }
108
Harald Welte54a10ef2018-02-09 00:09:16 +0100109 return NULL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200110}
111
Harald Welte54a10ef2018-02-09 00:09:16 +0100112/* Check if a specified connection id has an active SCCP connection */
113static bool check_connection_active(uint32_t conn_id)
114{
115 if (find_bsc_con(conn_id))
116 return true;
117 else
118 return false;
119}
120
121/* Get the context for a specific calling (BSC) address */
122static struct bsc_context *get_bsc_context_by_sccp_addr(const struct osmo_sccp_addr *addr)
Philipp Maierfbf66102017-04-09 12:32:51 +0200123{
124 struct bsc_context *bsc_ctx;
125 struct osmo_ss7_instance *ss7;
126
127 if (!addr)
128 return NULL;
129
130 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
131 if (memcmp(&bsc_ctx->bsc_addr, addr, sizeof(*addr)) == 0)
Harald Welte54a10ef2018-02-09 00:09:16 +0100132 return bsc_ctx;
Philipp Maierfbf66102017-04-09 12:32:51 +0200133 }
134
135 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
136 OSMO_ASSERT(ss7);
Harald Welte1f477442018-02-09 01:49:01 +0100137 LOGP(DBSSAP, LOGL_NOTICE, "The calling BSC (%s) is unknown to this MSC ...\n",
Philipp Maierfbf66102017-04-09 12:32:51 +0200138 osmo_sccp_addr_name(ss7, addr));
139 return NULL;
140}
141
Philipp Maier4502f5f2017-09-07 11:39:58 +0200142/* Send DTAP message via A-interface, take ownership of msg */
Philipp Maierfbf66102017-04-09 12:32:51 +0200143int a_iface_tx_dtap(struct msgb *msg)
144{
145 struct gsm_subscriber_connection *conn;
146 struct msgb *msg_resp;
147
148 /* FIXME: Set this to some meaninful value! */
149 uint8_t link_id = 0x00;
150 OSMO_ASSERT(msg);
151 conn = (struct gsm_subscriber_connection *)msg->dst;
152 OSMO_ASSERT(conn);
153 OSMO_ASSERT(conn->a.scu);
154
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100155 LOGPCONN(conn, LOGL_DEBUG, "Passing DTAP message from MSC to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200156
157 msg->l3h = msg->data;
158 msg_resp = gsm0808_create_dtap(msg, link_id);
Philipp Maier4502f5f2017-09-07 11:39:58 +0200159
160 /* gsm0808_create_dtap() has copied the data to msg_resp,
161 * so msg has served its purpose now */
162 msgb_free(msg);
163
Philipp Maierfbf66102017-04-09 12:32:51 +0200164 if (!msg_resp) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100165 LOGPCONN(conn, LOGL_ERROR, "Unable to generate BSSMAP DTAP message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200166 return -EINVAL;
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100167 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200168
Harald Weltea41b6302018-02-09 00:27:56 +0100169 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", msgb_hexdump_l2(msg_resp));
Philipp Maier4502f5f2017-09-07 11:39:58 +0200170 /* osmo_sccp_tx_data_msg() takes ownership of msg_resp */
Philipp Maierfbf66102017-04-09 12:32:51 +0200171 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
172}
173
174/* Send Cipher mode command via A-interface */
175int a_iface_tx_cipher_mode(const struct gsm_subscriber_connection *conn,
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100176 struct gsm0808_encrypt_info *ei, int include_imeisv)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200177{
178 /* TODO generalize for A- and Iu interfaces, don't name after 08.08 */
Philipp Maierfbf66102017-04-09 12:32:51 +0200179 struct msgb *msg_resp;
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100180 uint8_t crm = 0x01;
Philipp Maierfbf66102017-04-09 12:32:51 +0200181
182 OSMO_ASSERT(conn);
Harald Weltefb7ba912018-02-09 01:05:27 +0100183 LOGPCONN(conn, LOGL_DEBUG, "Tx BSSMAP CIPHER MODE COMMAND to BSC, %u ciphers (%s)",
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100184 ei->perm_algo_len, osmo_hexdump_nospc(ei->perm_algo, ei->perm_algo_len));
Harald Welte1f477442018-02-09 01:49:01 +0100185 LOGPC(DBSSAP, LOGL_DEBUG, " key %s\n", osmo_hexdump_nospc(ei->key, ei->key_len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200186
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100187 msg_resp = gsm0808_create_cipher(ei, include_imeisv ? &crm : NULL);
Harald Weltea41b6302018-02-09 00:27:56 +0100188 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", msgb_hexdump_l2(msg_resp));
Philipp Maierfbf66102017-04-09 12:32:51 +0200189
190 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
191}
192
193/* Page a subscriber via A-interface */
194int a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
195{
196 struct bsc_context *bsc_ctx;
Stefan Sperling621c7292018-02-16 13:40:42 +0100197 struct gsm0808_cell_id_list2 cil;
Philipp Maierfbf66102017-04-09 12:32:51 +0200198 struct msgb *msg;
199 int page_count = 0;
200 struct osmo_ss7_instance *ss7;
201
202 OSMO_ASSERT(imsi);
203
204 cil.id_discr = CELL_IDENT_LAC;
Stefan Sperling621c7292018-02-16 13:40:42 +0100205 cil.id_list[0].lac = lac;
Philipp Maierfbf66102017-04-09 12:32:51 +0200206 cil.id_list_len = 1;
207
208 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
209 OSMO_ASSERT(ss7);
210
211 /* Deliver paging request to all known BSCs */
212 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
213 if (a_reset_conn_ready(bsc_ctx->reset)) {
Harald Welte1f477442018-02-09 01:49:01 +0100214 LOGP(DBSSAP, LOGL_DEBUG,
Harald Weltefb7ba912018-02-09 01:05:27 +0100215 "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 +0200216 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
217 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
Stefan Sperling621c7292018-02-16 13:40:42 +0100218 msg = gsm0808_create_paging2(imsi, &tmsi, &cil, NULL);
Philipp Maierfbf66102017-04-09 12:32:51 +0200219 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user,
220 &bsc_ctx->msc_addr, &bsc_ctx->bsc_addr, msg);
221 page_count++;
222 } else {
Harald Welte1f477442018-02-09 01:49:01 +0100223 LOGP(DBSSAP, LOGL_DEBUG,
Philipp Maierfbf66102017-04-09 12:32:51 +0200224 "Connection down, dropping paging from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
225 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
226 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
227 }
228 }
229
230 if (page_count <= 0)
Harald Welte1f477442018-02-09 01:49:01 +0100231 LOGP(DBSSAP, LOGL_ERROR, "Could not deliver paging because none of the associated BSCs is available!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200232
233 return page_count;
234}
235
236/* Convert speech version field */
Harald Welte474e5a72018-02-12 10:12:35 +0100237static uint8_t convert_speech_version_l3_to_A(int speech_ver)
Philipp Maierfbf66102017-04-09 12:32:51 +0200238{
239 /* The speech versions that are transmitted in the Bearer capability
Harald Weltef417b8c2018-02-12 10:10:05 +0100240 * information element, that is transmitted on the Layer 3 (CC)
Philipp Maierfbf66102017-04-09 12:32:51 +0200241 * use a different encoding than the permitted speech version
242 * identifier, that is signalled in the channel type element on the A
243 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
244 * 10.5.103 */
245
246 switch (speech_ver) {
247 case GSM48_BCAP_SV_FR:
248 return GSM0808_PERM_FR1;
Philipp Maierfbf66102017-04-09 12:32:51 +0200249 case GSM48_BCAP_SV_HR:
250 return GSM0808_PERM_HR1;
Philipp Maierfbf66102017-04-09 12:32:51 +0200251 case GSM48_BCAP_SV_EFR:
252 return GSM0808_PERM_FR2;
Philipp Maierfbf66102017-04-09 12:32:51 +0200253 case GSM48_BCAP_SV_AMR_F:
254 return GSM0808_PERM_FR3;
Philipp Maierfbf66102017-04-09 12:32:51 +0200255 case GSM48_BCAP_SV_AMR_H:
256 return GSM0808_PERM_HR3;
Philipp Maierfbf66102017-04-09 12:32:51 +0200257 case GSM48_BCAP_SV_AMR_OFW:
258 return GSM0808_PERM_FR4;
Philipp Maierfbf66102017-04-09 12:32:51 +0200259 case GSM48_BCAP_SV_AMR_OHW:
260 return GSM0808_PERM_HR4;
Philipp Maierfbf66102017-04-09 12:32:51 +0200261 case GSM48_BCAP_SV_AMR_FW:
262 return GSM0808_PERM_FR5;
Philipp Maierfbf66102017-04-09 12:32:51 +0200263 case GSM48_BCAP_SV_AMR_OH:
264 return GSM0808_PERM_HR6;
Philipp Maierfbf66102017-04-09 12:32:51 +0200265 }
266
267 /* If nothing matches, tag the result as invalid */
Harald Welte1f477442018-02-09 01:49:01 +0100268 LOGP(DBSSAP, LOGL_ERROR, "Invalid permitted speech version: %d\n", speech_ver);
Philipp Maierfbf66102017-04-09 12:32:51 +0200269 return 0xFF;
270}
271
272/* Convert speech preference field */
Harald Welte474e5a72018-02-12 10:12:35 +0100273static uint8_t convert_speech_pref_l3_to_A(int radio)
Philipp Maierfbf66102017-04-09 12:32:51 +0200274{
275 /* The Radio channel requirement field that is transmitted in the
276 * Bearer capability information element, that is transmitted on the
Harald Weltef417b8c2018-02-12 10:10:05 +0100277 * Layer 3 (CC) uses a different encoding than the Channel rate and
Philipp Maierfbf66102017-04-09 12:32:51 +0200278 * type field that is signalled in the channel type element on the A
279 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
280 * 10.5.102 */
281
282 switch (radio) {
283 case GSM48_BCAP_RRQ_FR_ONLY:
284 return GSM0808_SPEECH_FULL_BM;
285 case GSM48_BCAP_RRQ_DUAL_FR:
286 return GSM0808_SPEECH_FULL_PREF;
287 case GSM48_BCAP_RRQ_DUAL_HR:
288 return GSM0808_SPEECH_HALF_PREF;
289 }
290
Harald Welte1f477442018-02-09 01:49:01 +0100291 LOGP(DBSSAP, LOGL_ERROR, "Invalid radio channel preference: %d; defaulting to full rate.\n",
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100292 radio);
Philipp Maierfbf66102017-04-09 12:32:51 +0200293 return GSM0808_SPEECH_FULL_BM;
294}
295
296/* Assemble the channel type field */
297static int enc_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc)
298{
299 unsigned int i;
300 uint8_t sv;
301 unsigned int count = 0;
302 bool only_gsm_hr = true;
303
304 OSMO_ASSERT(ct);
305 OSMO_ASSERT(bc);
306
307 ct->ch_indctr = GSM0808_CHAN_SPEECH;
308
309 for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) {
310 if (bc->speech_ver[i] == -1)
311 break;
Harald Welte474e5a72018-02-12 10:12:35 +0100312 sv = convert_speech_version_l3_to_A(bc->speech_ver[i]);
Philipp Maierfbf66102017-04-09 12:32:51 +0200313 if (sv != 0xFF) {
314 /* Detect if something else than
315 * GSM HR V1 is supported */
316 if (sv == GSM0808_PERM_HR2 ||
317 sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6)
318 only_gsm_hr = false;
319
320 ct->perm_spch[count] = sv;
321 count++;
322 }
323 }
324 ct->perm_spch_len = count;
325
326 if (only_gsm_hr)
327 /* Note: We must avoid the usage of GSM HR1 as this
328 * codec only offers very poor audio quality. If the
329 * MS only supports GSM HR1 (and full rate), and has
330 * a preference for half rate. Then we will ignore the
331 * preference and assume a preference for full rate. */
332 ct->ch_rate_type = GSM0808_SPEECH_FULL_BM;
333 else
Harald Welte474e5a72018-02-12 10:12:35 +0100334 ct->ch_rate_type = convert_speech_pref_l3_to_A(bc->radio);
Philipp Maierfbf66102017-04-09 12:32:51 +0200335
336 if (count)
337 return 0;
338 else
339 return -EINVAL;
340}
341
342/* Assemble the speech codec field */
343static int enc_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
344{
345 unsigned int i;
346 int rc;
347
348 memset(scl, 0, sizeof(*scl));
349 for (i = 0; i < ct->perm_spch_len; i++) {
350 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
351 if (rc != 0)
352 return -EINVAL;
353 }
354 scl->len = i;
355
356 return 0;
357}
358
359/* Send assignment request via A-interface */
360int a_iface_tx_assignment(const struct gsm_trans *trans)
361{
362 struct gsm_subscriber_connection *conn;
363 struct gsm0808_channel_type ct;
364 struct gsm0808_speech_codec_list scl;
365 uint32_t *ci_ptr = NULL;
366 struct msgb *msg;
367 struct sockaddr_storage rtp_addr;
368 struct sockaddr_in rtp_addr_in;
369 int rc;
370
371 OSMO_ASSERT(trans);
372 conn = trans->conn;
373 OSMO_ASSERT(conn);
374
Harald Weltefb7ba912018-02-09 01:05:27 +0100375 LOGPCONN(conn, LOGL_DEBUG, "Tx BSSMAP ASSIGNMENT COMMAND to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200376
377 /* Channel type */
378 rc = enc_channel_type(&ct, &trans->bearer_cap);
379 if (rc < 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100380 LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate channel type\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200381 return -EINVAL;
382 }
383
384 /* Speech codec list */
385 rc = enc_speech_codec_list(&scl, &ct);
386 if (rc < 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100387 LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate speech codec list\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200388 return -EINVAL;
389 }
390
391 /* Package RTP-Address data */
392 memset(&rtp_addr_in, 0, sizeof(rtp_addr_in));
393 rtp_addr_in.sin_family = AF_INET;
Philipp Maier621ba032017-11-07 17:19:25 +0100394 rtp_addr_in.sin_port = osmo_htons(conn->rtp.local_port_ran);
395 rtp_addr_in.sin_addr.s_addr = inet_addr(conn->rtp.local_addr_ran);
396
397 if (rtp_addr_in.sin_addr.s_addr == INADDR_NONE) {
398 LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Address -- assignment not sent!\n");
399 return -EINVAL;
400 }
401 if (rtp_addr_in.sin_port == 0) {
402 LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Port -- assignment not sent!\n");
403 return -EINVAL;
404 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200405
406 memset(&rtp_addr, 0, sizeof(rtp_addr));
407 memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in));
408
409 msg = gsm0808_create_ass(&ct, NULL, &rtp_addr, &scl, ci_ptr);
410
Harald Weltea41b6302018-02-09 00:27:56 +0100411 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", msgb_hexdump_l2(msg));
Philipp Maierfbf66102017-04-09 12:32:51 +0200412 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
413}
414
415/* Send clear command via A-interface */
416int a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
417{
418 struct msgb *msg;
419
Harald Weltefb7ba912018-02-09 01:05:27 +0100420 LOGPCONN(conn, LOGL_INFO, "Tx BSSMAP CLEAR COMMAND to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200421
422 msg = gsm0808_create_clear_command(GSM0808_CAUSE_CALL_CONTROL);
423 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
424}
425
426/* Callback function: Close all open connections */
427static void a_reset_cb(const void *priv)
428{
429 struct msgb *msg;
430 struct bsc_context *bsc_ctx = (struct bsc_context*) priv;
431 struct osmo_ss7_instance *ss7;
432
433 /* Skip if the A interface is not properly initalized yet */
434 if (!gsm_network)
435 return;
436
437 /* Clear all now orphaned subscriber connections */
438 a_clear_all(bsc_ctx->sccp_user, &bsc_ctx->bsc_addr);
439
440 /* Send reset to the remote BSC */
441 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
442 OSMO_ASSERT(ss7);
Harald Welte1f477442018-02-09 01:49:01 +0100443 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 +0200444 msg = gsm0808_create_reset();
445 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, &bsc_ctx->msc_addr,
446 &bsc_ctx->bsc_addr, msg);
447}
448
449/* Add a new BSC connection to our internal list with known BSCs */
Harald Welte54a10ef2018-02-09 00:09:16 +0100450static struct bsc_context *add_bsc(const struct osmo_sccp_addr *msc_addr,
451 const struct osmo_sccp_addr *bsc_addr, struct osmo_sccp_user *scu)
Philipp Maierfbf66102017-04-09 12:32:51 +0200452{
453 struct bsc_context *bsc_ctx;
454 struct osmo_ss7_instance *ss7;
Philipp Maierfbf66102017-04-09 12:32:51 +0200455
456 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
457 OSMO_ASSERT(ss7);
Harald Welte1f477442018-02-09 01:49:01 +0100458 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 +0200459
460 /* Generate and fill up a new bsc context */
461 bsc_ctx = talloc_zero(gsm_network, struct bsc_context);
462 OSMO_ASSERT(bsc_ctx);
463 memcpy(&bsc_ctx->bsc_addr, bsc_addr, sizeof(*bsc_addr));
464 memcpy(&bsc_ctx->msc_addr, msc_addr, sizeof(*msc_addr));
465 bsc_ctx->sccp_user = scu;
466 llist_add_tail(&bsc_ctx->list, &gsm_network->a.bscs);
467
Harald Welte54a10ef2018-02-09 00:09:16 +0100468 return bsc_ctx;
469}
470
471/* start the BSSMAP RESET fsm */
472void a_start_reset(struct bsc_context *bsc_ctx, bool already_connected)
473{
474 char bsc_name[32];
475 OSMO_ASSERT(bsc_ctx->reset == NULL);
Philipp Maierfbf66102017-04-09 12:32:51 +0200476 /* Start reset procedure to make the new connection active */
Harald Welte54a10ef2018-02-09 00:09:16 +0100477 snprintf(bsc_name, sizeof(bsc_name), "bsc-%i", bsc_ctx->bsc_addr.pc);
478 bsc_ctx->reset = a_reset_alloc(bsc_ctx, bsc_name, a_reset_cb, bsc_ctx, already_connected);
479}
480
Philipp Maierce1298b2018-02-22 16:44:41 +0100481/* determine if given msg is BSSMAP RESET related (true) or not (false) */
Harald Welte54a10ef2018-02-09 00:09:16 +0100482static bool bssmap_is_reset(struct msgb *msg)
483{
484 struct bssmap_header *bs = (struct bssmap_header *)msgb_l2(msg);
485
486 if (msgb_l2len(msg) < sizeof(*bs))
487 return false;
488
489 if (bs->type != BSSAP_MSG_BSS_MANAGEMENT)
490 return false;
491
492 if (msg->l2h[sizeof(*bs)] == BSS_MAP_MSG_RESET)
493 return true;
494
Philipp Maierce1298b2018-02-22 16:44:41 +0100495 if (msg->l2h[sizeof(*bs)] == BSS_MAP_MSG_RESET_ACKNOWLEDGE)
496 return true;
497
Harald Welte54a10ef2018-02-09 00:09:16 +0100498 return false;
Philipp Maierfbf66102017-04-09 12:32:51 +0200499}
500
501/* Callback function, called by the SSCP stack when data arrives */
502static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
503{
504 struct osmo_sccp_user *scu = _scu;
505 struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph;
506 int rc = 0;
507 struct a_conn_info a_conn_info;
Harald Welte54a10ef2018-02-09 00:09:16 +0100508 struct bsc_conn *bsc_con;
509
Philipp Maierfbf66102017-04-09 12:32:51 +0200510 memset(&a_conn_info, 0, sizeof(a_conn_info));
511 a_conn_info.network = gsm_network;
Philipp Maierfbf66102017-04-09 12:32:51 +0200512
513 switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
514 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
515 /* Handle inbound connection indication */
Philipp Maierfbf66102017-04-09 12:32:51 +0200516 a_conn_info.conn_id = scu_prim->u.connect.conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +0100517 a_conn_info.bsc = get_bsc_context_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
518 if (!a_conn_info.bsc) {
519 /* We haven't heard from this BSC before, allocate it */
520 a_conn_info.bsc = add_bsc(&scu_prim->u.connect.called_addr,
521 &scu_prim->u.connect.calling_addr, scu);
522 a_start_reset(a_conn_info.bsc, false);
523 } else {
524 /* This BSC is already known to us, check if we have been through reset yet */
525 if (a_reset_conn_ready(a_conn_info.bsc->reset) == false) {
Harald Welte1f477442018-02-09 01:49:01 +0100526 LOGP(DBSSAP, LOGL_NOTICE, "Refusing N-CONNECT.ind(%u, %s), BSC not reset yet\n",
Harald Weltea41b6302018-02-09 00:27:56 +0100527 scu_prim->u.connect.conn_id, msgb_hexdump_l2(oph->msg));
Harald Welte54a10ef2018-02-09 00:09:16 +0100528 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, &a_conn_info.bsc->msc_addr,
529 SCCP_RETURN_CAUSE_UNQUALIFIED);
530 break;
531 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200532
Harald Welte54a10ef2018-02-09 00:09:16 +0100533 osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0);
534 if (msgb_l2len(oph->msg) > 0) {
Harald Welte1f477442018-02-09 01:49:01 +0100535 LOGP(DBSSAP, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n",
Harald Weltea41b6302018-02-09 00:27:56 +0100536 scu_prim->u.connect.conn_id, msgb_hexdump_l2(oph->msg));
Harald Welte54a10ef2018-02-09 00:09:16 +0100537 rc = a_sccp_rx_dt(scu, &a_conn_info, oph->msg);
Harald Welte8a991ed2018-03-18 21:56:04 +0100538 } else {
Harald Welte1f477442018-02-09 01:49:01 +0100539 LOGP(DBSSAP, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id);
Harald Welte8a991ed2018-03-18 21:56:04 +0100540 rc = -ENODATA;
541 }
542
543 if (rc < 0) {
544 /* initial message (COMPL L3) caused some error, we didn't allocate
545 * a subscriber_conn and must close the connection again */
546 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id,
547 &a_conn_info.bsc->msc_addr,
548 SCCP_RETURN_CAUSE_UNQUALIFIED);
549 } else
550 record_bsc_con(scu, a_conn_info.bsc, scu_prim->u.connect.conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +0200551 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200552 break;
553
554 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
555 /* Handle incoming connection oriented data */
Harald Welte54a10ef2018-02-09 00:09:16 +0100556 bsc_con = find_bsc_con(scu_prim->u.data.conn_id);
557 if (!bsc_con) {
Harald Welte1f477442018-02-09 01:49:01 +0100558 LOGP(DBSSAP, LOGL_ERROR, "N-DATA.ind(%u, %s) for unknown conn_id\n",
Harald Weltea41b6302018-02-09 00:27:56 +0100559 scu_prim->u.data.conn_id, msgb_hexdump_l2(oph->msg));
Harald Welte54a10ef2018-02-09 00:09:16 +0100560 break;
561 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200562 a_conn_info.conn_id = scu_prim->u.data.conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +0100563 a_conn_info.bsc = bsc_con->bsc;
Harald Welte1f477442018-02-09 01:49:01 +0100564 LOGP(DBSSAP, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n",
Harald Weltea41b6302018-02-09 00:27:56 +0100565 scu_prim->u.data.conn_id, msgb_hexdump_l2(oph->msg));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100566 a_sccp_rx_dt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200567 break;
568
569 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
570 /* Handle inbound UNITDATA */
Philipp Maierce1298b2018-02-22 16:44:41 +0100571
572 /* Get BSC context, create a new one if necessary */
Harald Welte54a10ef2018-02-09 00:09:16 +0100573 a_conn_info.bsc = get_bsc_context_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
574 if (!a_conn_info.bsc) {
575 /* We haven't heard from this BSC before, allocate it */
576 a_conn_info.bsc = add_bsc(&scu_prim->u.unitdata.called_addr,
577 &scu_prim->u.unitdata.calling_addr, scu);
Philipp Maierce1298b2018-02-22 16:44:41 +0100578 /* Make sure that reset procedure is started */
579 a_start_reset(a_conn_info.bsc, false);
Harald Welte54a10ef2018-02-09 00:09:16 +0100580 }
Philipp Maierce1298b2018-02-22 16:44:41 +0100581
582 /* As long as we are in the reset phase, only reset related BSSMAP messages may pass
583 * beond here. */
584 if (!bssmap_is_reset(oph->msg) && a_reset_conn_ready(a_conn_info.bsc->reset) == false) {
585 LOGP(DBSSAP, LOGL_NOTICE, "Ignoring N-UNITDATA.ind(%s), BSC not reset yet\n",
586 msgb_hexdump_l2(oph->msg));
587 break;
588 }
589
Harald Welte1f477442018-02-09 01:49:01 +0100590 DEBUGP(DBSSAP, "N-UNITDATA.ind(%s)\n", msgb_hexdump_l2(oph->msg));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100591 a_sccp_rx_udt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200592 break;
593
594 default:
Harald Welte1f477442018-02-09 01:49:01 +0100595 LOGP(DBSSAP, LOGL_ERROR, "Unhandled SIGTRAN operation %s on primitive %u\n",
Maxc309fe32018-01-24 14:02:38 +0100596 get_value_string(osmo_prim_op_names, oph->operation), oph->primitive);
Philipp Maierfbf66102017-04-09 12:32:51 +0200597 break;
598 }
599
Harald Weltea172e9e2018-02-09 21:33:24 +0100600 /* We didn't transfer msgb ownership to any downstream functions so we rely on
601 * this single/central location to free() the msgb wrapping the primitive */
602 msgb_free(oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200603 return rc;
604}
605
606/* Clear all subscriber connections on a specified BSC */
607void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr)
608{
609 struct gsm_subscriber_connection *conn;
610 struct gsm_subscriber_connection *conn_temp;
611 struct gsm_network *network = gsm_network;
612
613 OSMO_ASSERT(scu);
614 OSMO_ASSERT(bsc_addr);
615
616 llist_for_each_entry_safe(conn, conn_temp, &network->subscr_conns, entry) {
617 /* Clear only A connections and connections that actually
618 * belong to the specified BSC */
619 if (conn->via_ran == RAN_GERAN_A && memcmp(bsc_addr, &conn->a.bsc_addr, sizeof(conn->a.bsc_addr)) == 0) {
Harald Welte80620d22018-02-10 10:24:15 +0100620 uint32_t conn_id = conn->a.conn_id;
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100621 LOGPCONN(conn, LOGL_NOTICE, "Dropping orphaned subscriber connection\n");
Harald Welte80620d22018-02-10 10:24:15 +0100622 /* This call will/may talloc_free(conn), so we must save conn_id above */
Philipp Maierfbf66102017-04-09 12:32:51 +0200623 msc_clear_request(conn, GSM48_CC_CAUSE_SWITCH_CONG);
624
625 /* If there is still an SCCP connection active, remove it now */
Harald Welte80620d22018-02-10 10:24:15 +0100626 if (check_connection_active(conn_id)) {
627 osmo_sccp_tx_disconn(scu, conn_id, bsc_addr,
Philipp Maierfbf66102017-04-09 12:32:51 +0200628 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
Harald Welte80620d22018-02-10 10:24:15 +0100629 a_delete_bsc_con(conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +0200630 }
631 }
632 }
633}
634
635/* Initalize A interface connection between to MSC and BSC */
636int a_init(struct osmo_sccp_instance *sccp, struct gsm_network *network)
637{
638 OSMO_ASSERT(sccp);
639 OSMO_ASSERT(network);
640
641 /* FIXME: Remove hardcoded parameters, use parameters in parameter list */
Harald Welte1f477442018-02-09 01:49:01 +0100642 LOGP(DBSSAP, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200643
644 /* Set GSM network variable, there can only be
645 * one network by design */
646 if (gsm_network != NULL) {
647 OSMO_ASSERT(gsm_network == network);
648 } else
649 gsm_network = network;
650
651 /* SCCP Protocol stack */
652 osmo_sccp_user_bind(sccp, "OsmoMSC-A", sccp_sap_up, SCCP_SSN_BSSAP);
653
654 return 0;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200655}