blob: f863517c4958b388380a43288eec22a12ee65337 [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
Philipp Maierfbf66102017-04-09 12:32:51 +020051/* A pointer to the GSM network we work with. By the current paradigm,
52 * there can only be one gsm_network per MSC. The pointer is set once
53 * when calling a_init() */
54static struct gsm_network *gsm_network = NULL;
55
56/* A struct to track currently active connections. We need that information
57 * to handle failure sitautions. In case of a problem, we must know which
58 * connections are currently open and which BSC is responsible. We also need
59 * the data to perform our connection checks (a_reset). All other logic will
60 * look at the connection ids and addresses that are supplied by the
61 * primitives */
62struct bsc_conn {
63 struct llist_head list;
64 uint32_t conn_id; /* Connection identifier */
65};
66
67/* Internal list with connections we currently maintain. This
68 * list is of type struct bsc_conn (see above) */
69static LLIST_HEAD(active_connections);
70
71/* Record info of a new active connection in the active connection list */
72static void record_bsc_con(const void *ctx, uint32_t conn_id)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020073{
Philipp Maierfbf66102017-04-09 12:32:51 +020074 struct bsc_conn *conn;
75
76 conn = talloc_zero(ctx, struct bsc_conn);
77 OSMO_ASSERT(conn);
78
79 conn->conn_id = conn_id;
80
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
90 LOGP(DMSC, LOGL_DEBUG,
91 "Removing connection from active sccp-connection list (conn_id=%i)\n",
92 conn_id);
93
94 llist_for_each_entry_safe(conn, conn_temp, &active_connections, list) {
95 if (conn->conn_id == conn_id) {
96 llist_del(&conn->list);
97 talloc_free(conn);
98 }
99 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200100}
101
Philipp Maierfbf66102017-04-09 12:32:51 +0200102/* Check if a specified connection id has an active SCCP connection */
103static bool check_connection_active(uint32_t conn_id)
104{
105 struct bsc_conn *conn;
106
107 /* Find the address for the current connection id */
108 llist_for_each_entry(conn, &active_connections, list) {
109 if (conn->conn_id == conn_id) {
110 return true;
111 }
112 }
113
114 return false;
115}
116
117/* Get the reset context for a specifiec calling (BSC) address */
118static struct a_reset_ctx *get_reset_ctx_by_sccp_addr(const struct osmo_sccp_addr *addr)
119{
120 struct bsc_context *bsc_ctx;
121 struct osmo_ss7_instance *ss7;
122
123 if (!addr)
124 return NULL;
125
126 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
127 if (memcmp(&bsc_ctx->bsc_addr, addr, sizeof(*addr)) == 0)
128 return bsc_ctx->reset;
129 }
130
131 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
132 OSMO_ASSERT(ss7);
133 LOGP(DMSC, LOGL_ERROR, "The calling BSC (%s) is unknown to this MSC ...\n",
134 osmo_sccp_addr_name(ss7, addr));
135 return NULL;
136}
137
Philipp Maier4502f5f2017-09-07 11:39:58 +0200138/* Send DTAP message via A-interface, take ownership of msg */
Philipp Maierfbf66102017-04-09 12:32:51 +0200139int a_iface_tx_dtap(struct msgb *msg)
140{
141 struct gsm_subscriber_connection *conn;
142 struct msgb *msg_resp;
143
144 /* FIXME: Set this to some meaninful value! */
145 uint8_t link_id = 0x00;
146 OSMO_ASSERT(msg);
147 conn = (struct gsm_subscriber_connection *)msg->dst;
148 OSMO_ASSERT(conn);
149 OSMO_ASSERT(conn->a.scu);
150
151 LOGP(DMSC, LOGL_DEBUG, "Passing DTAP message from MSC to BSC (conn_id=%i)\n", conn->a.conn_id);
152
153 msg->l3h = msg->data;
154 msg_resp = gsm0808_create_dtap(msg, link_id);
Philipp Maier4502f5f2017-09-07 11:39:58 +0200155
156 /* gsm0808_create_dtap() has copied the data to msg_resp,
157 * so msg has served its purpose now */
158 msgb_free(msg);
159
Philipp Maierfbf66102017-04-09 12:32:51 +0200160 if (!msg_resp) {
161 LOGP(DMSC, LOGL_ERROR, "Unable to generate BSSMAP DTAP message!\n");
162 return -EINVAL;
163 } else
Philipp Maiera3351e02017-09-11 10:05:05 +0200164 LOGP(DMSC, LOGL_DEBUG, "Message will be sent as BSSMAP DTAP message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200165
166 LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg_resp->data, msg_resp->len));
Philipp Maier4502f5f2017-09-07 11:39:58 +0200167 /* osmo_sccp_tx_data_msg() takes ownership of msg_resp */
Philipp Maierfbf66102017-04-09 12:32:51 +0200168 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
169}
170
171/* Send Cipher mode command via A-interface */
172int a_iface_tx_cipher_mode(const struct gsm_subscriber_connection *conn,
Neels Hofmeyrfb21d9d2017-12-14 03:51:34 +0100173 int cipher, const uint8_t *key, int len, int include_imeisv)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200174{
175 /* TODO generalize for A- and Iu interfaces, don't name after 08.08 */
Philipp Maierfbf66102017-04-09 12:32:51 +0200176 struct msgb *msg_resp;
177 struct gsm0808_encrypt_info ei;
178
179 OSMO_ASSERT(conn);
180
Philipp Maierfbf66102017-04-09 12:32:51 +0200181 uint8_t crm = 0x01;
182 uint8_t *crm_ptr = NULL;
183
Neels Hofmeyrf879fc92017-12-14 03:52:18 +0100184 LOGPCONN(conn, LOGL_DEBUG, "Cipher Mode Command to BSC, cipher=%d key=%s\n",
185 cipher, osmo_hexdump_nospc(key, len));
186
Philipp Maierfbf66102017-04-09 12:32:51 +0200187 /* Setup encryption information */
188 if (len > ENCRY_INFO_KEY_MAXLEN || !key) {
189 LOGP(DMSC, LOGL_ERROR,
190 "Cipher mode command message could not be generated due to invalid key! (conn_id=%i)\n",
191 conn->a.conn_id);
192 return -EINVAL;
193 } else {
194 memcpy(&ei.key, key, len);
195 ei.key_len = len;
196 }
197
198 if (include_imeisv)
199 crm_ptr = &crm;
200
201 ei.perm_algo[0] = (uint8_t) (1 << cipher);
202 ei.perm_algo_len = 1;
203
204 msg_resp = gsm0808_create_cipher(&ei, crm_ptr);
205 LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg_resp->data, msg_resp->len));
206
207 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
208}
209
210/* Page a subscriber via A-interface */
211int a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
212{
213 struct bsc_context *bsc_ctx;
214 struct gsm0808_cell_id_list cil;
215 struct msgb *msg;
216 int page_count = 0;
217 struct osmo_ss7_instance *ss7;
218
219 OSMO_ASSERT(imsi);
220
221 cil.id_discr = CELL_IDENT_LAC;
222 cil.id_list_lac[0] = lac;
223 cil.id_list_len = 1;
224
225 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
226 OSMO_ASSERT(ss7);
227
228 /* Deliver paging request to all known BSCs */
229 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
230 if (a_reset_conn_ready(bsc_ctx->reset)) {
231 LOGP(DMSC, LOGL_DEBUG,
232 "Passing paging message from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
233 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
234 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
235 msg = gsm0808_create_paging(imsi, &tmsi, &cil, NULL);
236 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user,
237 &bsc_ctx->msc_addr, &bsc_ctx->bsc_addr, msg);
238 page_count++;
239 } else {
240 LOGP(DMSC, LOGL_DEBUG,
241 "Connection down, dropping paging from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
242 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
243 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
244 }
245 }
246
247 if (page_count <= 0)
248 LOGP(DMSC, LOGL_ERROR, "Could not deliver paging because none of the associated BSCs is available!\n");
249
250 return page_count;
251}
252
253/* Convert speech version field */
254static uint8_t convert_Abis_sv_to_A_sv(int speech_ver)
255{
256 /* The speech versions that are transmitted in the Bearer capability
257 * information element, that is transmitted on the Abis interfece
258 * use a different encoding than the permitted speech version
259 * identifier, that is signalled in the channel type element on the A
260 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
261 * 10.5.103 */
262
263 switch (speech_ver) {
264 case GSM48_BCAP_SV_FR:
265 return GSM0808_PERM_FR1;
266 break;
267 case GSM48_BCAP_SV_HR:
268 return GSM0808_PERM_HR1;
269 break;
270 case GSM48_BCAP_SV_EFR:
271 return GSM0808_PERM_FR2;
272 break;
273 case GSM48_BCAP_SV_AMR_F:
274 return GSM0808_PERM_FR3;
275 break;
276 case GSM48_BCAP_SV_AMR_H:
277 return GSM0808_PERM_HR3;
278 break;
279 case GSM48_BCAP_SV_AMR_OFW:
280 return GSM0808_PERM_FR4;
281 break;
282 case GSM48_BCAP_SV_AMR_OHW:
283 return GSM0808_PERM_HR4;
284 break;
285 case GSM48_BCAP_SV_AMR_FW:
286 return GSM0808_PERM_FR5;
287 break;
288 case GSM48_BCAP_SV_AMR_OH:
289 return GSM0808_PERM_HR6;
290 break;
291 }
292
293 /* If nothing matches, tag the result as invalid */
294 LOGP(DMSC, LOGL_ERROR, "Invalid permitted speech version / rate detected, discarding.\n");
295 return 0xFF;
296}
297
298/* Convert speech preference field */
299static uint8_t convert_Abis_prev_to_A_pref(int radio)
300{
301 /* The Radio channel requirement field that is transmitted in the
302 * Bearer capability information element, that is transmitted on the
303 * Abis interfece uses a different encoding than the Channel rate and
304 * type field that is signalled in the channel type element on the A
305 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
306 * 10.5.102 */
307
308 switch (radio) {
309 case GSM48_BCAP_RRQ_FR_ONLY:
310 return GSM0808_SPEECH_FULL_BM;
311 case GSM48_BCAP_RRQ_DUAL_FR:
312 return GSM0808_SPEECH_FULL_PREF;
313 case GSM48_BCAP_RRQ_DUAL_HR:
314 return GSM0808_SPEECH_HALF_PREF;
315 }
316
317 LOGP(DMSC, LOGL_ERROR, "Invalid speech version / rate combination preference, defaulting to full rate.\n");
318 return GSM0808_SPEECH_FULL_BM;
319}
320
321/* Assemble the channel type field */
322static int enc_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc)
323{
324 unsigned int i;
325 uint8_t sv;
326 unsigned int count = 0;
327 bool only_gsm_hr = true;
328
329 OSMO_ASSERT(ct);
330 OSMO_ASSERT(bc);
331
332 ct->ch_indctr = GSM0808_CHAN_SPEECH;
333
334 for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) {
335 if (bc->speech_ver[i] == -1)
336 break;
337 sv = convert_Abis_sv_to_A_sv(bc->speech_ver[i]);
338 if (sv != 0xFF) {
339 /* Detect if something else than
340 * GSM HR V1 is supported */
341 if (sv == GSM0808_PERM_HR2 ||
342 sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6)
343 only_gsm_hr = false;
344
345 ct->perm_spch[count] = sv;
346 count++;
347 }
348 }
349 ct->perm_spch_len = count;
350
351 if (only_gsm_hr)
352 /* Note: We must avoid the usage of GSM HR1 as this
353 * codec only offers very poor audio quality. If the
354 * MS only supports GSM HR1 (and full rate), and has
355 * a preference for half rate. Then we will ignore the
356 * preference and assume a preference for full rate. */
357 ct->ch_rate_type = GSM0808_SPEECH_FULL_BM;
358 else
359 ct->ch_rate_type = convert_Abis_prev_to_A_pref(bc->radio);
360
361 if (count)
362 return 0;
363 else
364 return -EINVAL;
365}
366
367/* Assemble the speech codec field */
368static int enc_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
369{
370 unsigned int i;
371 int rc;
372
373 memset(scl, 0, sizeof(*scl));
374 for (i = 0; i < ct->perm_spch_len; i++) {
375 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
376 if (rc != 0)
377 return -EINVAL;
378 }
379 scl->len = i;
380
381 return 0;
382}
383
384/* Send assignment request via A-interface */
385int a_iface_tx_assignment(const struct gsm_trans *trans)
386{
387 struct gsm_subscriber_connection *conn;
388 struct gsm0808_channel_type ct;
389 struct gsm0808_speech_codec_list scl;
390 uint32_t *ci_ptr = NULL;
391 struct msgb *msg;
392 struct sockaddr_storage rtp_addr;
393 struct sockaddr_in rtp_addr_in;
394 int rc;
395
396 OSMO_ASSERT(trans);
397 conn = trans->conn;
398 OSMO_ASSERT(conn);
399
400 LOGP(DMSC, LOGL_ERROR, "Sending assignment command to BSC (conn_id %u)\n", conn->a.conn_id);
401
402 /* Channel type */
403 rc = enc_channel_type(&ct, &trans->bearer_cap);
404 if (rc < 0) {
405 LOGP(DMSC, LOGL_ERROR, "Faild to generate channel type -- assignment not sent!\n");
406 return -EINVAL;
407 }
408
409 /* Speech codec list */
410 rc = enc_speech_codec_list(&scl, &ct);
411 if (rc < 0) {
412 LOGP(DMSC, LOGL_ERROR, "Faild to generate Speech codec list -- assignment not sent!\n");
413 return -EINVAL;
414 }
415
416 /* Package RTP-Address data */
417 memset(&rtp_addr_in, 0, sizeof(rtp_addr_in));
418 rtp_addr_in.sin_family = AF_INET;
419 rtp_addr_in.sin_port = osmo_htons(conn->rtp.port_subscr);
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +0200420 rtp_addr_in.sin_addr.s_addr = osmo_htonl(mgcp_client_remote_addr_n(gsm_network->mgw.client));
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
427 LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg->data, msg->len));
428 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
436 LOGP(DMSC, LOGL_NOTICE, "Sending clear command to BSC (conn_id=%u)\n", conn->a.conn_id);
437
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 */
466static void add_bsc(const struct osmo_sccp_addr *msc_addr, const struct osmo_sccp_addr *bsc_addr,
467 struct osmo_sccp_user *scu)
468{
469 struct bsc_context *bsc_ctx;
470 struct osmo_ss7_instance *ss7;
Philipp Maier8ae3c922017-11-09 11:17:24 +0100471 char bsc_name[32];
Philipp Maierfbf66102017-04-09 12:32:51 +0200472
473 OSMO_ASSERT(bsc_addr);
474 OSMO_ASSERT(msc_addr);
475 OSMO_ASSERT(scu);
476
477 /* Check if we already know this BSC, if yes, skip adding it. */
478 if (get_reset_ctx_by_sccp_addr(bsc_addr))
479 return;
480
481 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
482 OSMO_ASSERT(ss7);
483 LOGP(DMSC, LOGL_NOTICE, "Adding new BSC connection for BSC %s...\n", osmo_sccp_addr_name(ss7, bsc_addr));
484
485 /* Generate and fill up a new bsc context */
486 bsc_ctx = talloc_zero(gsm_network, struct bsc_context);
487 OSMO_ASSERT(bsc_ctx);
488 memcpy(&bsc_ctx->bsc_addr, bsc_addr, sizeof(*bsc_addr));
489 memcpy(&bsc_ctx->msc_addr, msc_addr, sizeof(*msc_addr));
490 bsc_ctx->sccp_user = scu;
491 llist_add_tail(&bsc_ctx->list, &gsm_network->a.bscs);
492
493 /* Start reset procedure to make the new connection active */
Philipp Maier8ae3c922017-11-09 11:17:24 +0100494 snprintf(bsc_name, sizeof(bsc_name), "bsc-%i", bsc_addr->pc);
495 bsc_ctx->reset = a_reset_alloc(bsc_ctx, bsc_name, a_reset_cb, bsc_ctx);
Philipp Maierfbf66102017-04-09 12:32:51 +0200496}
497
498/* Callback function, called by the SSCP stack when data arrives */
499static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
500{
501 struct osmo_sccp_user *scu = _scu;
502 struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph;
503 int rc = 0;
504 struct a_conn_info a_conn_info;
505 memset(&a_conn_info, 0, sizeof(a_conn_info));
506 a_conn_info.network = gsm_network;
507 a_conn_info.reset = NULL;
508
509 switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
510 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
511 /* Handle inbound connection indication */
512 add_bsc(&scu_prim->u.connect.called_addr, &scu_prim->u.connect.calling_addr, scu);
513 a_conn_info.conn_id = scu_prim->u.connect.conn_id;
514 a_conn_info.msc_addr = &scu_prim->u.connect.called_addr;
515 a_conn_info.bsc_addr = &scu_prim->u.connect.calling_addr;
516 a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
517
518 if (a_reset_conn_ready(a_conn_info.reset) == false) {
519 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, a_conn_info.msc_addr,
520 SCCP_RETURN_CAUSE_UNQUALIFIED);
521 break;
522 }
523
524 osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0);
525 if (msgb_l2len(oph->msg) > 0) {
526 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n",
527 scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
528 rc = sccp_rx_dt(scu, &a_conn_info, oph->msg);
529 } else
530 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id);
531
532 record_bsc_con(scu, scu_prim->u.connect.conn_id);
533 break;
534
535 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
536 /* Handle incoming connection oriented data */
537 a_conn_info.conn_id = scu_prim->u.data.conn_id;
538 LOGP(DMSC, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n",
539 scu_prim->u.data.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
540 sccp_rx_dt(scu, &a_conn_info, oph->msg);
541 break;
542
543 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
544 /* Handle inbound UNITDATA */
545 add_bsc(&scu_prim->u.unitdata.called_addr, &scu_prim->u.unitdata.calling_addr, scu);
546 a_conn_info.msc_addr = &scu_prim->u.unitdata.called_addr;
547 a_conn_info.bsc_addr = &scu_prim->u.unitdata.calling_addr;
548 a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
549 DEBUGP(DMSC, "N-UNITDATA.ind(%s)\n", osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
550 sccp_rx_udt(scu, &a_conn_info, oph->msg);
551 break;
552
553 default:
554 LOGP(DMSC, LOGL_ERROR, "Unhandled SIGTRAN primitive: %u:%u\n", oph->primitive, oph->operation);
555 break;
556 }
557
558 return rc;
559}
560
561/* Clear all subscriber connections on a specified BSC */
562void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr)
563{
564 struct gsm_subscriber_connection *conn;
565 struct gsm_subscriber_connection *conn_temp;
566 struct gsm_network *network = gsm_network;
567
568 OSMO_ASSERT(scu);
569 OSMO_ASSERT(bsc_addr);
570
571 llist_for_each_entry_safe(conn, conn_temp, &network->subscr_conns, entry) {
572 /* Clear only A connections and connections that actually
573 * belong to the specified BSC */
574 if (conn->via_ran == RAN_GERAN_A && memcmp(bsc_addr, &conn->a.bsc_addr, sizeof(conn->a.bsc_addr)) == 0) {
575 LOGP(DMSC, LOGL_NOTICE, "Dropping orphaned subscriber connection (conn_id %i)\n",
576 conn->a.conn_id);
577 msc_clear_request(conn, GSM48_CC_CAUSE_SWITCH_CONG);
578
579 /* If there is still an SCCP connection active, remove it now */
580 if (check_connection_active(conn->a.conn_id)) {
581 osmo_sccp_tx_disconn(scu, conn->a.conn_id, bsc_addr,
582 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
583 a_delete_bsc_con(conn->a.conn_id);
584 }
585 }
586 }
587}
588
589/* Initalize A interface connection between to MSC and BSC */
590int a_init(struct osmo_sccp_instance *sccp, struct gsm_network *network)
591{
592 OSMO_ASSERT(sccp);
593 OSMO_ASSERT(network);
594
595 /* FIXME: Remove hardcoded parameters, use parameters in parameter list */
596 LOGP(DMSC, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n");
597
598 /* Set GSM network variable, there can only be
599 * one network by design */
600 if (gsm_network != NULL) {
601 OSMO_ASSERT(gsm_network == network);
602 } else
603 gsm_network = network;
604
605 /* SCCP Protocol stack */
606 osmo_sccp_user_bind(sccp, "OsmoMSC-A", sccp_sap_up, SCCP_SSN_BSSAP);
607
608 return 0;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200609}