blob: 600dbb82c75f0e94d26bbccfca9bc2050eeea6e7 [file] [log] [blame]
Philipp Maierfbf66102017-04-09 12:32:51 +02001/* (C) 2017 by sysmocom s.f.m.c. GmbH
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002 * All Rights Reserved
3 *
Philipp Maierfbf66102017-04-09 12:32:51 +02004 * Author: Philipp Maier
5 *
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02006 * 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
Philipp Maierfbf66102017-04-09 12:32:51 +020021#include <osmocom/core/utils.h>
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020022#include <osmocom/core/msgb.h>
23#include <osmocom/core/logging.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020024#include <osmocom/sigtran/sccp_helpers.h>
25#include <osmocom/sigtran/sccp_sap.h>
26#include <osmocom/sigtran/osmo_ss7.h>
27#include <osmocom/sigtran/protocol/m3ua.h>
28#include <osmocom/gsm/gsm0808.h>
29#include <osmocom/gsm/protocol/gsm_08_08.h>
30#include <osmocom/gsm/protocol/gsm_04_08.h>
31#include <osmocom/gsm/gsm0808_utils.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020032#include <osmocom/msc/debug.h>
33#include <osmocom/msc/msc_ifaces.h>
34#include <osmocom/msc/a_iface.h>
35#include <osmocom/msc/a_iface_bssap.h>
36#include <osmocom/msc/transaction.h>
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +020037#include <osmocom/mgcp_client/mgcp_client.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020038#include <osmocom/core/byteswap.h>
39#include <osmocom/sccp/sccp_types.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020040#include <osmocom/msc/a_reset.h>
41#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyrf879fc92017-12-14 03:52:18 +010042#include <osmocom/msc/vlr.h>
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020043
Max43b01b02017-09-15 11:22:30 +020044#include <errno.h>
45
Neels Hofmeyrf879fc92017-12-14 03:52:18 +010046#define LOGPCONN(conn, level, fmt, args...) \
47 LOGP(DMSC, level, "(subscr %s, conn_id %d) " fmt, \
48 vlr_subscr_name(conn ? conn->vsub : NULL), conn ? conn->a.conn_id : -1, \
49 ## args)
50
Neels Hofmeyr04960b12017-12-18 05:17:25 +010051#define LOGPBSCCONN(conn, level, fmt, args...) \
52 LOGP(DMSC, level, "(conn_id %u) " fmt, conn ? conn->conn_id : (uint32_t)(-1), ## args)
53
Philipp Maierfbf66102017-04-09 12:32:51 +020054/* A pointer to the GSM network we work with. By the current paradigm,
55 * there can only be one gsm_network per MSC. The pointer is set once
56 * when calling a_init() */
57static struct gsm_network *gsm_network = NULL;
58
59/* A struct to track currently active connections. We need that information
60 * to handle failure sitautions. In case of a problem, we must know which
61 * connections are currently open and which BSC is responsible. We also need
62 * the data to perform our connection checks (a_reset). All other logic will
63 * look at the connection ids and addresses that are supplied by the
64 * primitives */
65struct bsc_conn {
66 struct llist_head list;
67 uint32_t conn_id; /* Connection identifier */
Harald Welte54a10ef2018-02-09 00:09:16 +010068 struct bsc_context *bsc;
Philipp Maierfbf66102017-04-09 12:32:51 +020069};
70
71/* Internal list with connections we currently maintain. This
72 * list is of type struct bsc_conn (see above) */
73static LLIST_HEAD(active_connections);
74
75/* Record info of a new active connection in the active connection list */
Harald Welte54a10ef2018-02-09 00:09:16 +010076static void record_bsc_con(const void *ctx, struct bsc_context *bsc, uint32_t conn_id)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020077{
Philipp Maierfbf66102017-04-09 12:32:51 +020078 struct bsc_conn *conn;
79
80 conn = talloc_zero(ctx, struct bsc_conn);
81 OSMO_ASSERT(conn);
82
83 conn->conn_id = conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +010084 conn->bsc = bsc;
Philipp Maierfbf66102017-04-09 12:32:51 +020085
86 llist_add_tail(&conn->list, &active_connections);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020087}
88
Philipp Maierfbf66102017-04-09 12:32:51 +020089/* Delete info of a closed connection from the active connection list */
90void a_delete_bsc_con(uint32_t conn_id)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020091{
Philipp Maierfbf66102017-04-09 12:32:51 +020092 struct bsc_conn *conn;
93 struct bsc_conn *conn_temp;
94
Philipp Maierfbf66102017-04-09 12:32:51 +020095 llist_for_each_entry_safe(conn, conn_temp, &active_connections, list) {
96 if (conn->conn_id == conn_id) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +010097 LOGPBSCCONN(conn, LOGL_DEBUG, "Removing A-interface conn\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020098 llist_del(&conn->list);
99 talloc_free(conn);
100 }
101 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200102}
103
Harald Welte54a10ef2018-02-09 00:09:16 +0100104/* Find a specified connection id */
105static struct bsc_conn *find_bsc_con(uint32_t conn_id)
Philipp Maierfbf66102017-04-09 12:32:51 +0200106{
107 struct bsc_conn *conn;
108
109 /* Find the address for the current connection id */
110 llist_for_each_entry(conn, &active_connections, list) {
111 if (conn->conn_id == conn_id) {
Harald Welte54a10ef2018-02-09 00:09:16 +0100112 return conn;
Philipp Maierfbf66102017-04-09 12:32:51 +0200113 }
114 }
115
Harald Welte54a10ef2018-02-09 00:09:16 +0100116 return NULL;
Philipp Maierfbf66102017-04-09 12:32:51 +0200117}
118
Harald Welte54a10ef2018-02-09 00:09:16 +0100119/* Check if a specified connection id has an active SCCP connection */
120static bool check_connection_active(uint32_t conn_id)
121{
122 if (find_bsc_con(conn_id))
123 return true;
124 else
125 return false;
126}
127
128/* Get the context for a specific calling (BSC) address */
129static struct bsc_context *get_bsc_context_by_sccp_addr(const struct osmo_sccp_addr *addr)
Philipp Maierfbf66102017-04-09 12:32:51 +0200130{
131 struct bsc_context *bsc_ctx;
132 struct osmo_ss7_instance *ss7;
133
134 if (!addr)
135 return NULL;
136
137 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
138 if (memcmp(&bsc_ctx->bsc_addr, addr, sizeof(*addr)) == 0)
Harald Welte54a10ef2018-02-09 00:09:16 +0100139 return bsc_ctx;
Philipp Maierfbf66102017-04-09 12:32:51 +0200140 }
141
142 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
143 OSMO_ASSERT(ss7);
144 LOGP(DMSC, LOGL_ERROR, "The calling BSC (%s) is unknown to this MSC ...\n",
145 osmo_sccp_addr_name(ss7, addr));
146 return NULL;
147}
148
Philipp Maier4502f5f2017-09-07 11:39:58 +0200149/* Send DTAP message via A-interface, take ownership of msg */
Philipp Maierfbf66102017-04-09 12:32:51 +0200150int a_iface_tx_dtap(struct msgb *msg)
151{
152 struct gsm_subscriber_connection *conn;
153 struct msgb *msg_resp;
154
155 /* FIXME: Set this to some meaninful value! */
156 uint8_t link_id = 0x00;
157 OSMO_ASSERT(msg);
158 conn = (struct gsm_subscriber_connection *)msg->dst;
159 OSMO_ASSERT(conn);
160 OSMO_ASSERT(conn->a.scu);
161
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100162 LOGPCONN(conn, LOGL_DEBUG, "Passing DTAP message from MSC to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200163
164 msg->l3h = msg->data;
165 msg_resp = gsm0808_create_dtap(msg, link_id);
Philipp Maier4502f5f2017-09-07 11:39:58 +0200166
167 /* gsm0808_create_dtap() has copied the data to msg_resp,
168 * so msg has served its purpose now */
169 msgb_free(msg);
170
Philipp Maierfbf66102017-04-09 12:32:51 +0200171 if (!msg_resp) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100172 LOGPCONN(conn, LOGL_ERROR, "Unable to generate BSSMAP DTAP message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200173 return -EINVAL;
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100174 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200175
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100176 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", osmo_hexdump(msg_resp->data, msg_resp->len));
Philipp Maier4502f5f2017-09-07 11:39:58 +0200177 /* osmo_sccp_tx_data_msg() takes ownership of msg_resp */
Philipp Maierfbf66102017-04-09 12:32:51 +0200178 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
179}
180
181/* Send Cipher mode command via A-interface */
182int a_iface_tx_cipher_mode(const struct gsm_subscriber_connection *conn,
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100183 struct gsm0808_encrypt_info *ei, int include_imeisv)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200184{
185 /* TODO generalize for A- and Iu interfaces, don't name after 08.08 */
Philipp Maierfbf66102017-04-09 12:32:51 +0200186 struct msgb *msg_resp;
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100187 uint8_t crm = 0x01;
Philipp Maierfbf66102017-04-09 12:32:51 +0200188
189 OSMO_ASSERT(conn);
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100190 LOGPCONN(conn, LOGL_DEBUG, "Cipher Mode Command to BSC, %u ciphers (%s)",
191 ei->perm_algo_len, osmo_hexdump_nospc(ei->perm_algo, ei->perm_algo_len));
192 LOGPC(DMSC, LOGL_DEBUG, " key %s\n", osmo_hexdump_nospc(ei->key, ei->key_len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200193
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100194 msg_resp = gsm0808_create_cipher(ei, include_imeisv ? &crm : NULL);
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100195 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", osmo_hexdump(msg_resp->data, msg_resp->len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200196
197 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
198}
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;
204 struct gsm0808_cell_id_list cil;
205 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;
212 cil.id_list_lac[0] = lac;
213 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) {
220 if (a_reset_conn_ready(bsc_ctx->reset)) {
221 LOGP(DMSC, LOGL_DEBUG,
222 "Passing paging message from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
223 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
224 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
225 msg = gsm0808_create_paging(imsi, &tmsi, &cil, NULL);
226 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user,
227 &bsc_ctx->msc_addr, &bsc_ctx->bsc_addr, msg);
228 page_count++;
229 } else {
230 LOGP(DMSC, LOGL_DEBUG,
231 "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)
238 LOGP(DMSC, LOGL_ERROR, "Could not deliver paging because none of the associated BSCs is available!\n");
239
240 return page_count;
241}
242
243/* Convert speech version field */
244static uint8_t convert_Abis_sv_to_A_sv(int speech_ver)
245{
246 /* The speech versions that are transmitted in the Bearer capability
247 * information element, that is transmitted on the Abis interfece
248 * 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;
256 break;
257 case GSM48_BCAP_SV_HR:
258 return GSM0808_PERM_HR1;
259 break;
260 case GSM48_BCAP_SV_EFR:
261 return GSM0808_PERM_FR2;
262 break;
263 case GSM48_BCAP_SV_AMR_F:
264 return GSM0808_PERM_FR3;
265 break;
266 case GSM48_BCAP_SV_AMR_H:
267 return GSM0808_PERM_HR3;
268 break;
269 case GSM48_BCAP_SV_AMR_OFW:
270 return GSM0808_PERM_FR4;
271 break;
272 case GSM48_BCAP_SV_AMR_OHW:
273 return GSM0808_PERM_HR4;
274 break;
275 case GSM48_BCAP_SV_AMR_FW:
276 return GSM0808_PERM_FR5;
277 break;
278 case GSM48_BCAP_SV_AMR_OH:
279 return GSM0808_PERM_HR6;
280 break;
281 }
282
283 /* If nothing matches, tag the result as invalid */
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100284 LOGP(DMSC, LOGL_ERROR, "Invalid permitted speech version: %d\n", speech_ver);
Philipp Maierfbf66102017-04-09 12:32:51 +0200285 return 0xFF;
286}
287
288/* Convert speech preference field */
289static uint8_t convert_Abis_prev_to_A_pref(int radio)
290{
291 /* The Radio channel requirement field that is transmitted in the
292 * Bearer capability information element, that is transmitted on the
293 * Abis interfece uses a different encoding than the Channel rate and
294 * type field that is signalled in the channel type element on the A
295 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
296 * 10.5.102 */
297
298 switch (radio) {
299 case GSM48_BCAP_RRQ_FR_ONLY:
300 return GSM0808_SPEECH_FULL_BM;
301 case GSM48_BCAP_RRQ_DUAL_FR:
302 return GSM0808_SPEECH_FULL_PREF;
303 case GSM48_BCAP_RRQ_DUAL_HR:
304 return GSM0808_SPEECH_HALF_PREF;
305 }
306
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100307 LOGP(DMSC, LOGL_ERROR, "Invalid radio channel preference: %d; defaulting to full rate.\n",
308 radio);
Philipp Maierfbf66102017-04-09 12:32:51 +0200309 return GSM0808_SPEECH_FULL_BM;
310}
311
312/* Assemble the channel type field */
313static int enc_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc)
314{
315 unsigned int i;
316 uint8_t sv;
317 unsigned int count = 0;
318 bool only_gsm_hr = true;
319
320 OSMO_ASSERT(ct);
321 OSMO_ASSERT(bc);
322
323 ct->ch_indctr = GSM0808_CHAN_SPEECH;
324
325 for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) {
326 if (bc->speech_ver[i] == -1)
327 break;
328 sv = convert_Abis_sv_to_A_sv(bc->speech_ver[i]);
329 if (sv != 0xFF) {
330 /* Detect if something else than
331 * GSM HR V1 is supported */
332 if (sv == GSM0808_PERM_HR2 ||
333 sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6)
334 only_gsm_hr = false;
335
336 ct->perm_spch[count] = sv;
337 count++;
338 }
339 }
340 ct->perm_spch_len = count;
341
342 if (only_gsm_hr)
343 /* Note: We must avoid the usage of GSM HR1 as this
344 * codec only offers very poor audio quality. If the
345 * MS only supports GSM HR1 (and full rate), and has
346 * a preference for half rate. Then we will ignore the
347 * preference and assume a preference for full rate. */
348 ct->ch_rate_type = GSM0808_SPEECH_FULL_BM;
349 else
350 ct->ch_rate_type = convert_Abis_prev_to_A_pref(bc->radio);
351
352 if (count)
353 return 0;
354 else
355 return -EINVAL;
356}
357
358/* Assemble the speech codec field */
359static int enc_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
360{
361 unsigned int i;
362 int rc;
363
364 memset(scl, 0, sizeof(*scl));
365 for (i = 0; i < ct->perm_spch_len; i++) {
366 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
367 if (rc != 0)
368 return -EINVAL;
369 }
370 scl->len = i;
371
372 return 0;
373}
374
375/* Send assignment request via A-interface */
376int a_iface_tx_assignment(const struct gsm_trans *trans)
377{
378 struct gsm_subscriber_connection *conn;
379 struct gsm0808_channel_type ct;
380 struct gsm0808_speech_codec_list scl;
381 uint32_t *ci_ptr = NULL;
382 struct msgb *msg;
383 struct sockaddr_storage rtp_addr;
384 struct sockaddr_in rtp_addr_in;
385 int rc;
386
387 OSMO_ASSERT(trans);
388 conn = trans->conn;
389 OSMO_ASSERT(conn);
390
Neels Hofmeyr563e1db2017-12-28 16:31:58 +0100391 LOGPCONN(conn, LOGL_DEBUG, "Sending Assignment Command to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200392
393 /* Channel type */
394 rc = enc_channel_type(&ct, &trans->bearer_cap);
395 if (rc < 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100396 LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate channel type\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200397 return -EINVAL;
398 }
399
400 /* Speech codec list */
401 rc = enc_speech_codec_list(&scl, &ct);
402 if (rc < 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100403 LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate speech codec list\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200404 return -EINVAL;
405 }
406
407 /* Package RTP-Address data */
408 memset(&rtp_addr_in, 0, sizeof(rtp_addr_in));
409 rtp_addr_in.sin_family = AF_INET;
Philipp Maier621ba032017-11-07 17:19:25 +0100410 rtp_addr_in.sin_port = osmo_htons(conn->rtp.local_port_ran);
411 rtp_addr_in.sin_addr.s_addr = inet_addr(conn->rtp.local_addr_ran);
412
413 if (rtp_addr_in.sin_addr.s_addr == INADDR_NONE) {
414 LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Address -- assignment not sent!\n");
415 return -EINVAL;
416 }
417 if (rtp_addr_in.sin_port == 0) {
418 LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Port -- assignment not sent!\n");
419 return -EINVAL;
420 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200421
422 memset(&rtp_addr, 0, sizeof(rtp_addr));
423 memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in));
424
425 msg = gsm0808_create_ass(&ct, NULL, &rtp_addr, &scl, ci_ptr);
426
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100427 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", osmo_hexdump(msg->data, msg->len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200428 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
429}
430
431/* Send clear command via A-interface */
432int a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
433{
434 struct msgb *msg;
435
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100436 LOGPCONN(conn, LOGL_NOTICE, "Sending Clear command to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200437
438 msg = gsm0808_create_clear_command(GSM0808_CAUSE_CALL_CONTROL);
439 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
440}
441
442/* Callback function: Close all open connections */
443static void a_reset_cb(const void *priv)
444{
445 struct msgb *msg;
446 struct bsc_context *bsc_ctx = (struct bsc_context*) priv;
447 struct osmo_ss7_instance *ss7;
448
449 /* Skip if the A interface is not properly initalized yet */
450 if (!gsm_network)
451 return;
452
453 /* Clear all now orphaned subscriber connections */
454 a_clear_all(bsc_ctx->sccp_user, &bsc_ctx->bsc_addr);
455
456 /* Send reset to the remote BSC */
457 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
458 OSMO_ASSERT(ss7);
459 LOGP(DMSC, LOGL_NOTICE, "Sending RESET to BSC %s\n", osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr));
460 msg = gsm0808_create_reset();
461 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, &bsc_ctx->msc_addr,
462 &bsc_ctx->bsc_addr, msg);
463}
464
465/* Add a new BSC connection to our internal list with known BSCs */
Harald Welte54a10ef2018-02-09 00:09:16 +0100466static struct bsc_context *add_bsc(const struct osmo_sccp_addr *msc_addr,
467 const struct osmo_sccp_addr *bsc_addr, struct osmo_sccp_user *scu)
Philipp Maierfbf66102017-04-09 12:32:51 +0200468{
469 struct bsc_context *bsc_ctx;
470 struct osmo_ss7_instance *ss7;
Philipp Maierfbf66102017-04-09 12:32:51 +0200471
472 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
473 OSMO_ASSERT(ss7);
474 LOGP(DMSC, LOGL_NOTICE, "Adding new BSC connection for BSC %s...\n", osmo_sccp_addr_name(ss7, bsc_addr));
475
476 /* Generate and fill up a new bsc context */
477 bsc_ctx = talloc_zero(gsm_network, struct bsc_context);
478 OSMO_ASSERT(bsc_ctx);
479 memcpy(&bsc_ctx->bsc_addr, bsc_addr, sizeof(*bsc_addr));
480 memcpy(&bsc_ctx->msc_addr, msc_addr, sizeof(*msc_addr));
481 bsc_ctx->sccp_user = scu;
482 llist_add_tail(&bsc_ctx->list, &gsm_network->a.bscs);
483
Harald Welte54a10ef2018-02-09 00:09:16 +0100484 return bsc_ctx;
485}
486
487/* start the BSSMAP RESET fsm */
488void a_start_reset(struct bsc_context *bsc_ctx, bool already_connected)
489{
490 char bsc_name[32];
491 OSMO_ASSERT(bsc_ctx->reset == NULL);
Philipp Maierfbf66102017-04-09 12:32:51 +0200492 /* Start reset procedure to make the new connection active */
Harald Welte54a10ef2018-02-09 00:09:16 +0100493 snprintf(bsc_name, sizeof(bsc_name), "bsc-%i", bsc_ctx->bsc_addr.pc);
494 bsc_ctx->reset = a_reset_alloc(bsc_ctx, bsc_name, a_reset_cb, bsc_ctx, already_connected);
495}
496
497/* determine if given msg is a BSSMAP RESET (true) or not (false) */
498static bool bssmap_is_reset(struct msgb *msg)
499{
500 struct bssmap_header *bs = (struct bssmap_header *)msgb_l2(msg);
501
502 if (msgb_l2len(msg) < sizeof(*bs))
503 return false;
504
505 if (bs->type != BSSAP_MSG_BSS_MANAGEMENT)
506 return false;
507
508 if (msg->l2h[sizeof(*bs)] == BSS_MAP_MSG_RESET)
509 return true;
510
511 return false;
Philipp Maierfbf66102017-04-09 12:32:51 +0200512}
513
514/* Callback function, called by the SSCP stack when data arrives */
515static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
516{
517 struct osmo_sccp_user *scu = _scu;
518 struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph;
519 int rc = 0;
520 struct a_conn_info a_conn_info;
Harald Welte54a10ef2018-02-09 00:09:16 +0100521 struct bsc_conn *bsc_con;
522
Philipp Maierfbf66102017-04-09 12:32:51 +0200523 memset(&a_conn_info, 0, sizeof(a_conn_info));
524 a_conn_info.network = gsm_network;
Philipp Maierfbf66102017-04-09 12:32:51 +0200525
526 switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
527 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
528 /* Handle inbound connection indication */
Philipp Maierfbf66102017-04-09 12:32:51 +0200529 a_conn_info.conn_id = scu_prim->u.connect.conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +0100530 a_conn_info.bsc = get_bsc_context_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
531 if (!a_conn_info.bsc) {
532 /* We haven't heard from this BSC before, allocate it */
533 a_conn_info.bsc = add_bsc(&scu_prim->u.connect.called_addr,
534 &scu_prim->u.connect.calling_addr, scu);
535 a_start_reset(a_conn_info.bsc, false);
536 } else {
537 /* This BSC is already known to us, check if we have been through reset yet */
538 if (a_reset_conn_ready(a_conn_info.bsc->reset) == false) {
539 LOGP(DMSC, LOGL_NOTICE, "Refusing N-CONNECT.ind(%u, %s), BSC not reset yet\n",
540 scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
541 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, &a_conn_info.bsc->msc_addr,
542 SCCP_RETURN_CAUSE_UNQUALIFIED);
543 break;
544 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200545
Harald Welte54a10ef2018-02-09 00:09:16 +0100546 osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0);
547 if (msgb_l2len(oph->msg) > 0) {
548 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n",
549 scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
550 rc = a_sccp_rx_dt(scu, &a_conn_info, oph->msg);
551 } else
552 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id);
553
554 record_bsc_con(scu, a_conn_info.bsc, scu_prim->u.connect.conn_id);
Philipp Maierfbf66102017-04-09 12:32:51 +0200555 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200556 break;
557
558 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
559 /* Handle incoming connection oriented data */
Harald Welte54a10ef2018-02-09 00:09:16 +0100560 bsc_con = find_bsc_con(scu_prim->u.data.conn_id);
561 if (!bsc_con) {
562 LOGP(DMSC, LOGL_ERROR, "N-DATA.ind(%u, %s) for unknown conn_id\n",
563 scu_prim->u.data.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
564 break;
565 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200566 a_conn_info.conn_id = scu_prim->u.data.conn_id;
Harald Welte54a10ef2018-02-09 00:09:16 +0100567 a_conn_info.bsc = bsc_con->bsc;
Philipp Maierfbf66102017-04-09 12:32:51 +0200568 LOGP(DMSC, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n",
569 scu_prim->u.data.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100570 a_sccp_rx_dt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200571 break;
572
573 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
574 /* Handle inbound UNITDATA */
Harald Welte54a10ef2018-02-09 00:09:16 +0100575 a_conn_info.bsc = get_bsc_context_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
576 if (!a_conn_info.bsc) {
577 /* We haven't heard from this BSC before, allocate it */
578 a_conn_info.bsc = add_bsc(&scu_prim->u.unitdata.called_addr,
579 &scu_prim->u.unitdata.calling_addr, scu);
580 /* if this not an inbound RESET, trigger an outbound RESET */
581 if (!bssmap_is_reset(oph->msg)) {
582 LOGP(DMSC, LOGL_NOTICE, "Ignoring N-UNITDATA.ind(%s), BSC not reset yet\n",
583 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
584 a_start_reset(a_conn_info.bsc, false);
585 break;
586 }
587 } else {
588 /* This BSC is already known to us, check if we have been through reset yet */
589 if (a_reset_conn_ready(a_conn_info.bsc->reset) == false) {
590 LOGP(DMSC, LOGL_NOTICE, "Ignoring N-UNITDATA.ind(%s), BSC not reset yet\n",
591 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
592 break;
593 }
594 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200595 DEBUGP(DMSC, "N-UNITDATA.ind(%s)\n", osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100596 a_sccp_rx_udt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200597 break;
598
599 default:
Maxc309fe32018-01-24 14:02:38 +0100600 LOGP(DMSC, LOGL_ERROR, "Unhandled SIGTRAN operation %s on primitive %u\n",
601 get_value_string(osmo_prim_op_names, oph->operation), oph->primitive);
Philipp Maierfbf66102017-04-09 12:32:51 +0200602 break;
603 }
604
605 return rc;
606}
607
608/* Clear all subscriber connections on a specified BSC */
609void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr)
610{
611 struct gsm_subscriber_connection *conn;
612 struct gsm_subscriber_connection *conn_temp;
613 struct gsm_network *network = gsm_network;
614
615 OSMO_ASSERT(scu);
616 OSMO_ASSERT(bsc_addr);
617
618 llist_for_each_entry_safe(conn, conn_temp, &network->subscr_conns, entry) {
619 /* Clear only A connections and connections that actually
620 * belong to the specified BSC */
621 if (conn->via_ran == RAN_GERAN_A && memcmp(bsc_addr, &conn->a.bsc_addr, sizeof(conn->a.bsc_addr)) == 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100622 LOGPCONN(conn, LOGL_NOTICE, "Dropping orphaned subscriber connection\n");
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 */
626 if (check_connection_active(conn->a.conn_id)) {
627 osmo_sccp_tx_disconn(scu, conn->a.conn_id, bsc_addr,
628 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
629 a_delete_bsc_con(conn->a.conn_id);
630 }
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 */
642 LOGP(DMSC, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n");
643
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}