blob: cdd8d892bd03a1d6f26d7b2d1a7cd6a62b442725 [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 Hofmeyr703638e2017-12-14 05:30:16 +0100173 struct gsm0808_encrypt_info *ei, 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;
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100177 uint8_t crm = 0x01;
Philipp Maierfbf66102017-04-09 12:32:51 +0200178
179 OSMO_ASSERT(conn);
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100180 LOGPCONN(conn, LOGL_DEBUG, "Cipher Mode Command to BSC, %u ciphers (%s)",
181 ei->perm_algo_len, osmo_hexdump_nospc(ei->perm_algo, ei->perm_algo_len));
182 LOGPC(DMSC, LOGL_DEBUG, " key %s\n", osmo_hexdump_nospc(ei->key, ei->key_len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200183
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100184 msg_resp = gsm0808_create_cipher(ei, include_imeisv ? &crm : NULL);
Philipp Maierfbf66102017-04-09 12:32:51 +0200185 LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg_resp->data, msg_resp->len));
186
187 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
188}
189
190/* Page a subscriber via A-interface */
191int a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
192{
193 struct bsc_context *bsc_ctx;
194 struct gsm0808_cell_id_list cil;
195 struct msgb *msg;
196 int page_count = 0;
197 struct osmo_ss7_instance *ss7;
198
199 OSMO_ASSERT(imsi);
200
201 cil.id_discr = CELL_IDENT_LAC;
202 cil.id_list_lac[0] = lac;
203 cil.id_list_len = 1;
204
205 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
206 OSMO_ASSERT(ss7);
207
208 /* Deliver paging request to all known BSCs */
209 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
210 if (a_reset_conn_ready(bsc_ctx->reset)) {
211 LOGP(DMSC, LOGL_DEBUG,
212 "Passing paging message from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
213 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
214 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
215 msg = gsm0808_create_paging(imsi, &tmsi, &cil, NULL);
216 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user,
217 &bsc_ctx->msc_addr, &bsc_ctx->bsc_addr, msg);
218 page_count++;
219 } else {
220 LOGP(DMSC, LOGL_DEBUG,
221 "Connection down, dropping paging from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
222 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
223 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
224 }
225 }
226
227 if (page_count <= 0)
228 LOGP(DMSC, LOGL_ERROR, "Could not deliver paging because none of the associated BSCs is available!\n");
229
230 return page_count;
231}
232
233/* Convert speech version field */
234static uint8_t convert_Abis_sv_to_A_sv(int speech_ver)
235{
236 /* The speech versions that are transmitted in the Bearer capability
237 * information element, that is transmitted on the Abis interfece
238 * use a different encoding than the permitted speech version
239 * identifier, that is signalled in the channel type element on the A
240 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
241 * 10.5.103 */
242
243 switch (speech_ver) {
244 case GSM48_BCAP_SV_FR:
245 return GSM0808_PERM_FR1;
246 break;
247 case GSM48_BCAP_SV_HR:
248 return GSM0808_PERM_HR1;
249 break;
250 case GSM48_BCAP_SV_EFR:
251 return GSM0808_PERM_FR2;
252 break;
253 case GSM48_BCAP_SV_AMR_F:
254 return GSM0808_PERM_FR3;
255 break;
256 case GSM48_BCAP_SV_AMR_H:
257 return GSM0808_PERM_HR3;
258 break;
259 case GSM48_BCAP_SV_AMR_OFW:
260 return GSM0808_PERM_FR4;
261 break;
262 case GSM48_BCAP_SV_AMR_OHW:
263 return GSM0808_PERM_HR4;
264 break;
265 case GSM48_BCAP_SV_AMR_FW:
266 return GSM0808_PERM_FR5;
267 break;
268 case GSM48_BCAP_SV_AMR_OH:
269 return GSM0808_PERM_HR6;
270 break;
271 }
272
273 /* If nothing matches, tag the result as invalid */
274 LOGP(DMSC, LOGL_ERROR, "Invalid permitted speech version / rate detected, discarding.\n");
275 return 0xFF;
276}
277
278/* Convert speech preference field */
279static uint8_t convert_Abis_prev_to_A_pref(int radio)
280{
281 /* The Radio channel requirement field that is transmitted in the
282 * Bearer capability information element, that is transmitted on the
283 * Abis interfece uses a different encoding than the Channel rate and
284 * type field that is signalled in the channel type element on the A
285 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
286 * 10.5.102 */
287
288 switch (radio) {
289 case GSM48_BCAP_RRQ_FR_ONLY:
290 return GSM0808_SPEECH_FULL_BM;
291 case GSM48_BCAP_RRQ_DUAL_FR:
292 return GSM0808_SPEECH_FULL_PREF;
293 case GSM48_BCAP_RRQ_DUAL_HR:
294 return GSM0808_SPEECH_HALF_PREF;
295 }
296
297 LOGP(DMSC, LOGL_ERROR, "Invalid speech version / rate combination preference, defaulting to full rate.\n");
298 return GSM0808_SPEECH_FULL_BM;
299}
300
301/* Assemble the channel type field */
302static int enc_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc)
303{
304 unsigned int i;
305 uint8_t sv;
306 unsigned int count = 0;
307 bool only_gsm_hr = true;
308
309 OSMO_ASSERT(ct);
310 OSMO_ASSERT(bc);
311
312 ct->ch_indctr = GSM0808_CHAN_SPEECH;
313
314 for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) {
315 if (bc->speech_ver[i] == -1)
316 break;
317 sv = convert_Abis_sv_to_A_sv(bc->speech_ver[i]);
318 if (sv != 0xFF) {
319 /* Detect if something else than
320 * GSM HR V1 is supported */
321 if (sv == GSM0808_PERM_HR2 ||
322 sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6)
323 only_gsm_hr = false;
324
325 ct->perm_spch[count] = sv;
326 count++;
327 }
328 }
329 ct->perm_spch_len = count;
330
331 if (only_gsm_hr)
332 /* Note: We must avoid the usage of GSM HR1 as this
333 * codec only offers very poor audio quality. If the
334 * MS only supports GSM HR1 (and full rate), and has
335 * a preference for half rate. Then we will ignore the
336 * preference and assume a preference for full rate. */
337 ct->ch_rate_type = GSM0808_SPEECH_FULL_BM;
338 else
339 ct->ch_rate_type = convert_Abis_prev_to_A_pref(bc->radio);
340
341 if (count)
342 return 0;
343 else
344 return -EINVAL;
345}
346
347/* Assemble the speech codec field */
348static int enc_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
349{
350 unsigned int i;
351 int rc;
352
353 memset(scl, 0, sizeof(*scl));
354 for (i = 0; i < ct->perm_spch_len; i++) {
355 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
356 if (rc != 0)
357 return -EINVAL;
358 }
359 scl->len = i;
360
361 return 0;
362}
363
364/* Send assignment request via A-interface */
365int a_iface_tx_assignment(const struct gsm_trans *trans)
366{
367 struct gsm_subscriber_connection *conn;
368 struct gsm0808_channel_type ct;
369 struct gsm0808_speech_codec_list scl;
370 uint32_t *ci_ptr = NULL;
371 struct msgb *msg;
372 struct sockaddr_storage rtp_addr;
373 struct sockaddr_in rtp_addr_in;
374 int rc;
375
376 OSMO_ASSERT(trans);
377 conn = trans->conn;
378 OSMO_ASSERT(conn);
379
380 LOGP(DMSC, LOGL_ERROR, "Sending assignment command to BSC (conn_id %u)\n", conn->a.conn_id);
381
382 /* Channel type */
383 rc = enc_channel_type(&ct, &trans->bearer_cap);
384 if (rc < 0) {
385 LOGP(DMSC, LOGL_ERROR, "Faild to generate channel type -- assignment not sent!\n");
386 return -EINVAL;
387 }
388
389 /* Speech codec list */
390 rc = enc_speech_codec_list(&scl, &ct);
391 if (rc < 0) {
392 LOGP(DMSC, LOGL_ERROR, "Faild to generate Speech codec list -- assignment not sent!\n");
393 return -EINVAL;
394 }
395
396 /* Package RTP-Address data */
397 memset(&rtp_addr_in, 0, sizeof(rtp_addr_in));
398 rtp_addr_in.sin_family = AF_INET;
399 rtp_addr_in.sin_port = osmo_htons(conn->rtp.port_subscr);
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +0200400 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 +0200401
402 memset(&rtp_addr, 0, sizeof(rtp_addr));
403 memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in));
404
405 msg = gsm0808_create_ass(&ct, NULL, &rtp_addr, &scl, ci_ptr);
406
407 LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg->data, msg->len));
408 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
409}
410
411/* Send clear command via A-interface */
412int a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
413{
414 struct msgb *msg;
415
416 LOGP(DMSC, LOGL_NOTICE, "Sending clear command to BSC (conn_id=%u)\n", conn->a.conn_id);
417
418 msg = gsm0808_create_clear_command(GSM0808_CAUSE_CALL_CONTROL);
419 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
420}
421
422/* Callback function: Close all open connections */
423static void a_reset_cb(const void *priv)
424{
425 struct msgb *msg;
426 struct bsc_context *bsc_ctx = (struct bsc_context*) priv;
427 struct osmo_ss7_instance *ss7;
428
429 /* Skip if the A interface is not properly initalized yet */
430 if (!gsm_network)
431 return;
432
433 /* Clear all now orphaned subscriber connections */
434 a_clear_all(bsc_ctx->sccp_user, &bsc_ctx->bsc_addr);
435
436 /* Send reset to the remote BSC */
437 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
438 OSMO_ASSERT(ss7);
439 LOGP(DMSC, LOGL_NOTICE, "Sending RESET to BSC %s\n", osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr));
440 msg = gsm0808_create_reset();
441 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, &bsc_ctx->msc_addr,
442 &bsc_ctx->bsc_addr, msg);
443}
444
445/* Add a new BSC connection to our internal list with known BSCs */
446static void add_bsc(const struct osmo_sccp_addr *msc_addr, const struct osmo_sccp_addr *bsc_addr,
447 struct osmo_sccp_user *scu)
448{
449 struct bsc_context *bsc_ctx;
450 struct osmo_ss7_instance *ss7;
Philipp Maier8ae3c922017-11-09 11:17:24 +0100451 char bsc_name[32];
Philipp Maierfbf66102017-04-09 12:32:51 +0200452
453 OSMO_ASSERT(bsc_addr);
454 OSMO_ASSERT(msc_addr);
455 OSMO_ASSERT(scu);
456
457 /* Check if we already know this BSC, if yes, skip adding it. */
458 if (get_reset_ctx_by_sccp_addr(bsc_addr))
459 return;
460
461 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
462 OSMO_ASSERT(ss7);
463 LOGP(DMSC, LOGL_NOTICE, "Adding new BSC connection for BSC %s...\n", osmo_sccp_addr_name(ss7, bsc_addr));
464
465 /* Generate and fill up a new bsc context */
466 bsc_ctx = talloc_zero(gsm_network, struct bsc_context);
467 OSMO_ASSERT(bsc_ctx);
468 memcpy(&bsc_ctx->bsc_addr, bsc_addr, sizeof(*bsc_addr));
469 memcpy(&bsc_ctx->msc_addr, msc_addr, sizeof(*msc_addr));
470 bsc_ctx->sccp_user = scu;
471 llist_add_tail(&bsc_ctx->list, &gsm_network->a.bscs);
472
473 /* Start reset procedure to make the new connection active */
Philipp Maier8ae3c922017-11-09 11:17:24 +0100474 snprintf(bsc_name, sizeof(bsc_name), "bsc-%i", bsc_addr->pc);
475 bsc_ctx->reset = a_reset_alloc(bsc_ctx, bsc_name, a_reset_cb, bsc_ctx);
Philipp Maierfbf66102017-04-09 12:32:51 +0200476}
477
478/* Callback function, called by the SSCP stack when data arrives */
479static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
480{
481 struct osmo_sccp_user *scu = _scu;
482 struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph;
483 int rc = 0;
484 struct a_conn_info a_conn_info;
485 memset(&a_conn_info, 0, sizeof(a_conn_info));
486 a_conn_info.network = gsm_network;
487 a_conn_info.reset = NULL;
488
489 switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
490 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
491 /* Handle inbound connection indication */
492 add_bsc(&scu_prim->u.connect.called_addr, &scu_prim->u.connect.calling_addr, scu);
493 a_conn_info.conn_id = scu_prim->u.connect.conn_id;
494 a_conn_info.msc_addr = &scu_prim->u.connect.called_addr;
495 a_conn_info.bsc_addr = &scu_prim->u.connect.calling_addr;
496 a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
497
498 if (a_reset_conn_ready(a_conn_info.reset) == false) {
499 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, a_conn_info.msc_addr,
500 SCCP_RETURN_CAUSE_UNQUALIFIED);
501 break;
502 }
503
504 osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0);
505 if (msgb_l2len(oph->msg) > 0) {
506 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n",
507 scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
508 rc = sccp_rx_dt(scu, &a_conn_info, oph->msg);
509 } else
510 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id);
511
512 record_bsc_con(scu, scu_prim->u.connect.conn_id);
513 break;
514
515 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
516 /* Handle incoming connection oriented data */
517 a_conn_info.conn_id = scu_prim->u.data.conn_id;
518 LOGP(DMSC, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n",
519 scu_prim->u.data.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
520 sccp_rx_dt(scu, &a_conn_info, oph->msg);
521 break;
522
523 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
524 /* Handle inbound UNITDATA */
525 add_bsc(&scu_prim->u.unitdata.called_addr, &scu_prim->u.unitdata.calling_addr, scu);
526 a_conn_info.msc_addr = &scu_prim->u.unitdata.called_addr;
527 a_conn_info.bsc_addr = &scu_prim->u.unitdata.calling_addr;
528 a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
529 DEBUGP(DMSC, "N-UNITDATA.ind(%s)\n", osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
530 sccp_rx_udt(scu, &a_conn_info, oph->msg);
531 break;
532
533 default:
534 LOGP(DMSC, LOGL_ERROR, "Unhandled SIGTRAN primitive: %u:%u\n", oph->primitive, oph->operation);
535 break;
536 }
537
538 return rc;
539}
540
541/* Clear all subscriber connections on a specified BSC */
542void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr)
543{
544 struct gsm_subscriber_connection *conn;
545 struct gsm_subscriber_connection *conn_temp;
546 struct gsm_network *network = gsm_network;
547
548 OSMO_ASSERT(scu);
549 OSMO_ASSERT(bsc_addr);
550
551 llist_for_each_entry_safe(conn, conn_temp, &network->subscr_conns, entry) {
552 /* Clear only A connections and connections that actually
553 * belong to the specified BSC */
554 if (conn->via_ran == RAN_GERAN_A && memcmp(bsc_addr, &conn->a.bsc_addr, sizeof(conn->a.bsc_addr)) == 0) {
555 LOGP(DMSC, LOGL_NOTICE, "Dropping orphaned subscriber connection (conn_id %i)\n",
556 conn->a.conn_id);
557 msc_clear_request(conn, GSM48_CC_CAUSE_SWITCH_CONG);
558
559 /* If there is still an SCCP connection active, remove it now */
560 if (check_connection_active(conn->a.conn_id)) {
561 osmo_sccp_tx_disconn(scu, conn->a.conn_id, bsc_addr,
562 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
563 a_delete_bsc_con(conn->a.conn_id);
564 }
565 }
566 }
567}
568
569/* Initalize A interface connection between to MSC and BSC */
570int a_init(struct osmo_sccp_instance *sccp, struct gsm_network *network)
571{
572 OSMO_ASSERT(sccp);
573 OSMO_ASSERT(network);
574
575 /* FIXME: Remove hardcoded parameters, use parameters in parameter list */
576 LOGP(DMSC, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n");
577
578 /* Set GSM network variable, there can only be
579 * one network by design */
580 if (gsm_network != NULL) {
581 OSMO_ASSERT(gsm_network == network);
582 } else
583 gsm_network = network;
584
585 /* SCCP Protocol stack */
586 osmo_sccp_user_bind(sccp, "OsmoMSC-A", sccp_sap_up, SCCP_SSN_BSSAP);
587
588 return 0;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200589}