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