blob: 3e864943581a5972d45f21c421272af8e86d38dd [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 Hofmeyre2f24d52017-05-08 15:12:20 +020042
Philipp Maierfbf66102017-04-09 12:32:51 +020043/* A pointer to the GSM network we work with. By the current paradigm,
44 * there can only be one gsm_network per MSC. The pointer is set once
45 * when calling a_init() */
46static struct gsm_network *gsm_network = NULL;
47
48/* A struct to track currently active connections. We need that information
49 * to handle failure sitautions. In case of a problem, we must know which
50 * connections are currently open and which BSC is responsible. We also need
51 * the data to perform our connection checks (a_reset). All other logic will
52 * look at the connection ids and addresses that are supplied by the
53 * primitives */
54struct bsc_conn {
55 struct llist_head list;
56 uint32_t conn_id; /* Connection identifier */
57};
58
59/* Internal list with connections we currently maintain. This
60 * list is of type struct bsc_conn (see above) */
61static LLIST_HEAD(active_connections);
62
63/* Record info of a new active connection in the active connection list */
64static void record_bsc_con(const void *ctx, uint32_t conn_id)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020065{
Philipp Maierfbf66102017-04-09 12:32:51 +020066 struct bsc_conn *conn;
67
68 conn = talloc_zero(ctx, struct bsc_conn);
69 OSMO_ASSERT(conn);
70
71 conn->conn_id = conn_id;
72
73 llist_add_tail(&conn->list, &active_connections);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020074}
75
Philipp Maierfbf66102017-04-09 12:32:51 +020076/* Delete info of a closed connection from the active connection list */
77void a_delete_bsc_con(uint32_t conn_id)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020078{
Philipp Maierfbf66102017-04-09 12:32:51 +020079 struct bsc_conn *conn;
80 struct bsc_conn *conn_temp;
81
82 LOGP(DMSC, LOGL_DEBUG,
83 "Removing connection from active sccp-connection list (conn_id=%i)\n",
84 conn_id);
85
86 llist_for_each_entry_safe(conn, conn_temp, &active_connections, list) {
87 if (conn->conn_id == conn_id) {
88 llist_del(&conn->list);
89 talloc_free(conn);
90 }
91 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020092}
93
Philipp Maierfbf66102017-04-09 12:32:51 +020094/* Check if a specified connection id has an active SCCP connection */
95static bool check_connection_active(uint32_t conn_id)
96{
97 struct bsc_conn *conn;
98
99 /* Find the address for the current connection id */
100 llist_for_each_entry(conn, &active_connections, list) {
101 if (conn->conn_id == conn_id) {
102 return true;
103 }
104 }
105
106 return false;
107}
108
109/* Get the reset context for a specifiec calling (BSC) address */
110static struct a_reset_ctx *get_reset_ctx_by_sccp_addr(const struct osmo_sccp_addr *addr)
111{
112 struct bsc_context *bsc_ctx;
113 struct osmo_ss7_instance *ss7;
114
115 if (!addr)
116 return NULL;
117
118 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
119 if (memcmp(&bsc_ctx->bsc_addr, addr, sizeof(*addr)) == 0)
120 return bsc_ctx->reset;
121 }
122
123 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
124 OSMO_ASSERT(ss7);
125 LOGP(DMSC, LOGL_ERROR, "The calling BSC (%s) is unknown to this MSC ...\n",
126 osmo_sccp_addr_name(ss7, addr));
127 return NULL;
128}
129
Philipp Maier4502f5f2017-09-07 11:39:58 +0200130/* Send DTAP message via A-interface, take ownership of msg */
Philipp Maierfbf66102017-04-09 12:32:51 +0200131int a_iface_tx_dtap(struct msgb *msg)
132{
133 struct gsm_subscriber_connection *conn;
134 struct msgb *msg_resp;
135
136 /* FIXME: Set this to some meaninful value! */
137 uint8_t link_id = 0x00;
138 OSMO_ASSERT(msg);
139 conn = (struct gsm_subscriber_connection *)msg->dst;
140 OSMO_ASSERT(conn);
141 OSMO_ASSERT(conn->a.scu);
142
143 LOGP(DMSC, LOGL_DEBUG, "Passing DTAP message from MSC to BSC (conn_id=%i)\n", conn->a.conn_id);
144
145 msg->l3h = msg->data;
146 msg_resp = gsm0808_create_dtap(msg, link_id);
Philipp Maier4502f5f2017-09-07 11:39:58 +0200147
148 /* gsm0808_create_dtap() has copied the data to msg_resp,
149 * so msg has served its purpose now */
150 msgb_free(msg);
151
Philipp Maierfbf66102017-04-09 12:32:51 +0200152 if (!msg_resp) {
153 LOGP(DMSC, LOGL_ERROR, "Unable to generate BSSMAP DTAP message!\n");
154 return -EINVAL;
155 } else
Philipp Maiera3351e02017-09-11 10:05:05 +0200156 LOGP(DMSC, LOGL_DEBUG, "Message will be sent as BSSMAP DTAP message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200157
158 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 +0200159 /* osmo_sccp_tx_data_msg() takes ownership of msg_resp */
Philipp Maierfbf66102017-04-09 12:32:51 +0200160 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
161}
162
163/* Send Cipher mode command via A-interface */
164int a_iface_tx_cipher_mode(const struct gsm_subscriber_connection *conn,
165 int cipher, const const uint8_t *key, int len, int include_imeisv)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200166{
167 /* TODO generalize for A- and Iu interfaces, don't name after 08.08 */
Philipp Maierfbf66102017-04-09 12:32:51 +0200168 struct msgb *msg_resp;
169 struct gsm0808_encrypt_info ei;
170
171 OSMO_ASSERT(conn);
172
173 LOGP(DMSC, LOGL_DEBUG, "Passing Cipher mode command message from MSC to BSC (conn_id=%i)\n", conn->a.conn_id);
174 uint8_t crm = 0x01;
175 uint8_t *crm_ptr = NULL;
176
177 /* Setup encryption information */
178 if (len > ENCRY_INFO_KEY_MAXLEN || !key) {
179 LOGP(DMSC, LOGL_ERROR,
180 "Cipher mode command message could not be generated due to invalid key! (conn_id=%i)\n",
181 conn->a.conn_id);
182 return -EINVAL;
183 } else {
184 memcpy(&ei.key, key, len);
185 ei.key_len = len;
186 }
187
188 if (include_imeisv)
189 crm_ptr = &crm;
190
191 ei.perm_algo[0] = (uint8_t) (1 << cipher);
192 ei.perm_algo_len = 1;
193
194 msg_resp = gsm0808_create_cipher(&ei, crm_ptr);
195 LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg_resp->data, msg_resp->len));
196
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 */
284 LOGP(DMSC, LOGL_ERROR, "Invalid permitted speech version / rate detected, discarding.\n");
285 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
307 LOGP(DMSC, LOGL_ERROR, "Invalid speech version / rate combination preference, defaulting to full rate.\n");
308 return GSM0808_SPEECH_FULL_BM;
309}
310
311/* Assemble the channel type field */
312static int enc_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc)
313{
314 unsigned int i;
315 uint8_t sv;
316 unsigned int count = 0;
317 bool only_gsm_hr = true;
318
319 OSMO_ASSERT(ct);
320 OSMO_ASSERT(bc);
321
322 ct->ch_indctr = GSM0808_CHAN_SPEECH;
323
324 for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) {
325 if (bc->speech_ver[i] == -1)
326 break;
327 sv = convert_Abis_sv_to_A_sv(bc->speech_ver[i]);
328 if (sv != 0xFF) {
329 /* Detect if something else than
330 * GSM HR V1 is supported */
331 if (sv == GSM0808_PERM_HR2 ||
332 sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6)
333 only_gsm_hr = false;
334
335 ct->perm_spch[count] = sv;
336 count++;
337 }
338 }
339 ct->perm_spch_len = count;
340
341 if (only_gsm_hr)
342 /* Note: We must avoid the usage of GSM HR1 as this
343 * codec only offers very poor audio quality. If the
344 * MS only supports GSM HR1 (and full rate), and has
345 * a preference for half rate. Then we will ignore the
346 * preference and assume a preference for full rate. */
347 ct->ch_rate_type = GSM0808_SPEECH_FULL_BM;
348 else
349 ct->ch_rate_type = convert_Abis_prev_to_A_pref(bc->radio);
350
351 if (count)
352 return 0;
353 else
354 return -EINVAL;
355}
356
357/* Assemble the speech codec field */
358static int enc_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
359{
360 unsigned int i;
361 int rc;
362
363 memset(scl, 0, sizeof(*scl));
364 for (i = 0; i < ct->perm_spch_len; i++) {
365 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
366 if (rc != 0)
367 return -EINVAL;
368 }
369 scl->len = i;
370
371 return 0;
372}
373
374/* Send assignment request via A-interface */
375int a_iface_tx_assignment(const struct gsm_trans *trans)
376{
377 struct gsm_subscriber_connection *conn;
378 struct gsm0808_channel_type ct;
379 struct gsm0808_speech_codec_list scl;
380 uint32_t *ci_ptr = NULL;
381 struct msgb *msg;
382 struct sockaddr_storage rtp_addr;
383 struct sockaddr_in rtp_addr_in;
384 int rc;
385
386 OSMO_ASSERT(trans);
387 conn = trans->conn;
388 OSMO_ASSERT(conn);
389
390 LOGP(DMSC, LOGL_ERROR, "Sending assignment command to BSC (conn_id %u)\n", conn->a.conn_id);
391
392 /* Channel type */
393 rc = enc_channel_type(&ct, &trans->bearer_cap);
394 if (rc < 0) {
395 LOGP(DMSC, LOGL_ERROR, "Faild to generate channel type -- assignment not sent!\n");
396 return -EINVAL;
397 }
398
399 /* Speech codec list */
400 rc = enc_speech_codec_list(&scl, &ct);
401 if (rc < 0) {
402 LOGP(DMSC, LOGL_ERROR, "Faild to generate Speech codec list -- assignment not sent!\n");
403 return -EINVAL;
404 }
405
406 /* Package RTP-Address data */
407 memset(&rtp_addr_in, 0, sizeof(rtp_addr_in));
408 rtp_addr_in.sin_family = AF_INET;
409 rtp_addr_in.sin_port = osmo_htons(conn->rtp.port_subscr);
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +0200410 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 +0200411
412 memset(&rtp_addr, 0, sizeof(rtp_addr));
413 memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in));
414
415 msg = gsm0808_create_ass(&ct, NULL, &rtp_addr, &scl, ci_ptr);
416
417 LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg->data, msg->len));
418 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
419}
420
421/* Send clear command via A-interface */
422int a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
423{
424 struct msgb *msg;
425
426 LOGP(DMSC, LOGL_NOTICE, "Sending clear command to BSC (conn_id=%u)\n", conn->a.conn_id);
427
428 msg = gsm0808_create_clear_command(GSM0808_CAUSE_CALL_CONTROL);
429 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
430}
431
432/* Callback function: Close all open connections */
433static void a_reset_cb(const void *priv)
434{
435 struct msgb *msg;
436 struct bsc_context *bsc_ctx = (struct bsc_context*) priv;
437 struct osmo_ss7_instance *ss7;
438
439 /* Skip if the A interface is not properly initalized yet */
440 if (!gsm_network)
441 return;
442
443 /* Clear all now orphaned subscriber connections */
444 a_clear_all(bsc_ctx->sccp_user, &bsc_ctx->bsc_addr);
445
446 /* Send reset to the remote BSC */
447 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
448 OSMO_ASSERT(ss7);
449 LOGP(DMSC, LOGL_NOTICE, "Sending RESET to BSC %s\n", osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr));
450 msg = gsm0808_create_reset();
451 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, &bsc_ctx->msc_addr,
452 &bsc_ctx->bsc_addr, msg);
453}
454
455/* Add a new BSC connection to our internal list with known BSCs */
456static void add_bsc(const struct osmo_sccp_addr *msc_addr, const struct osmo_sccp_addr *bsc_addr,
457 struct osmo_sccp_user *scu)
458{
459 struct bsc_context *bsc_ctx;
460 struct osmo_ss7_instance *ss7;
461
462 OSMO_ASSERT(bsc_addr);
463 OSMO_ASSERT(msc_addr);
464 OSMO_ASSERT(scu);
465
466 /* Check if we already know this BSC, if yes, skip adding it. */
467 if (get_reset_ctx_by_sccp_addr(bsc_addr))
468 return;
469
470 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
471 OSMO_ASSERT(ss7);
472 LOGP(DMSC, LOGL_NOTICE, "Adding new BSC connection for BSC %s...\n", osmo_sccp_addr_name(ss7, bsc_addr));
473
474 /* Generate and fill up a new bsc context */
475 bsc_ctx = talloc_zero(gsm_network, struct bsc_context);
476 OSMO_ASSERT(bsc_ctx);
477 memcpy(&bsc_ctx->bsc_addr, bsc_addr, sizeof(*bsc_addr));
478 memcpy(&bsc_ctx->msc_addr, msc_addr, sizeof(*msc_addr));
479 bsc_ctx->sccp_user = scu;
480 llist_add_tail(&bsc_ctx->list, &gsm_network->a.bscs);
481
482 /* Start reset procedure to make the new connection active */
483 bsc_ctx->reset = a_reset_alloc(bsc_ctx, osmo_sccp_addr_name(ss7, bsc_addr), a_reset_cb, bsc_ctx);
484}
485
486/* Callback function, called by the SSCP stack when data arrives */
487static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
488{
489 struct osmo_sccp_user *scu = _scu;
490 struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph;
491 int rc = 0;
492 struct a_conn_info a_conn_info;
493 memset(&a_conn_info, 0, sizeof(a_conn_info));
494 a_conn_info.network = gsm_network;
495 a_conn_info.reset = NULL;
496
497 switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
498 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
499 /* Handle inbound connection indication */
500 add_bsc(&scu_prim->u.connect.called_addr, &scu_prim->u.connect.calling_addr, scu);
501 a_conn_info.conn_id = scu_prim->u.connect.conn_id;
502 a_conn_info.msc_addr = &scu_prim->u.connect.called_addr;
503 a_conn_info.bsc_addr = &scu_prim->u.connect.calling_addr;
504 a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
505
506 if (a_reset_conn_ready(a_conn_info.reset) == false) {
507 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, a_conn_info.msc_addr,
508 SCCP_RETURN_CAUSE_UNQUALIFIED);
509 break;
510 }
511
512 osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0);
513 if (msgb_l2len(oph->msg) > 0) {
514 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n",
515 scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
516 rc = sccp_rx_dt(scu, &a_conn_info, oph->msg);
517 } else
518 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id);
519
520 record_bsc_con(scu, scu_prim->u.connect.conn_id);
521 break;
522
523 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
524 /* Handle incoming connection oriented data */
525 a_conn_info.conn_id = scu_prim->u.data.conn_id;
526 LOGP(DMSC, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n",
527 scu_prim->u.data.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
528 sccp_rx_dt(scu, &a_conn_info, oph->msg);
529 break;
530
531 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
532 /* Handle inbound UNITDATA */
533 add_bsc(&scu_prim->u.unitdata.called_addr, &scu_prim->u.unitdata.calling_addr, scu);
534 a_conn_info.msc_addr = &scu_prim->u.unitdata.called_addr;
535 a_conn_info.bsc_addr = &scu_prim->u.unitdata.calling_addr;
536 a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
537 DEBUGP(DMSC, "N-UNITDATA.ind(%s)\n", osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
538 sccp_rx_udt(scu, &a_conn_info, oph->msg);
539 break;
540
541 default:
542 LOGP(DMSC, LOGL_ERROR, "Unhandled SIGTRAN primitive: %u:%u\n", oph->primitive, oph->operation);
543 break;
544 }
545
546 return rc;
547}
548
549/* Clear all subscriber connections on a specified BSC */
550void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr)
551{
552 struct gsm_subscriber_connection *conn;
553 struct gsm_subscriber_connection *conn_temp;
554 struct gsm_network *network = gsm_network;
555
556 OSMO_ASSERT(scu);
557 OSMO_ASSERT(bsc_addr);
558
559 llist_for_each_entry_safe(conn, conn_temp, &network->subscr_conns, entry) {
560 /* Clear only A connections and connections that actually
561 * belong to the specified BSC */
562 if (conn->via_ran == RAN_GERAN_A && memcmp(bsc_addr, &conn->a.bsc_addr, sizeof(conn->a.bsc_addr)) == 0) {
563 LOGP(DMSC, LOGL_NOTICE, "Dropping orphaned subscriber connection (conn_id %i)\n",
564 conn->a.conn_id);
565 msc_clear_request(conn, GSM48_CC_CAUSE_SWITCH_CONG);
566
567 /* If there is still an SCCP connection active, remove it now */
568 if (check_connection_active(conn->a.conn_id)) {
569 osmo_sccp_tx_disconn(scu, conn->a.conn_id, bsc_addr,
570 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
571 a_delete_bsc_con(conn->a.conn_id);
572 }
573 }
574 }
575}
576
577/* Initalize A interface connection between to MSC and BSC */
578int a_init(struct osmo_sccp_instance *sccp, struct gsm_network *network)
579{
580 OSMO_ASSERT(sccp);
581 OSMO_ASSERT(network);
582
583 /* FIXME: Remove hardcoded parameters, use parameters in parameter list */
584 LOGP(DMSC, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n");
585
586 /* Set GSM network variable, there can only be
587 * one network by design */
588 if (gsm_network != NULL) {
589 OSMO_ASSERT(gsm_network == network);
590 } else
591 gsm_network = network;
592
593 /* SCCP Protocol stack */
594 osmo_sccp_user_bind(sccp, "OsmoMSC-A", sccp_sap_up, SCCP_SSN_BSSAP);
595
596 return 0;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200597}