blob: 11e1542d4adc10863df7ab54a44a154523070d21 [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 */
68};
69
70/* Internal list with connections we currently maintain. This
71 * list is of type struct bsc_conn (see above) */
72static LLIST_HEAD(active_connections);
73
74/* Record info of a new active connection in the active connection list */
75static void record_bsc_con(const void *ctx, uint32_t conn_id)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020076{
Philipp Maierfbf66102017-04-09 12:32:51 +020077 struct bsc_conn *conn;
78
79 conn = talloc_zero(ctx, struct bsc_conn);
80 OSMO_ASSERT(conn);
81
82 conn->conn_id = conn_id;
83
84 llist_add_tail(&conn->list, &active_connections);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020085}
86
Philipp Maierfbf66102017-04-09 12:32:51 +020087/* Delete info of a closed connection from the active connection list */
88void a_delete_bsc_con(uint32_t conn_id)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020089{
Philipp Maierfbf66102017-04-09 12:32:51 +020090 struct bsc_conn *conn;
91 struct bsc_conn *conn_temp;
92
Philipp Maierfbf66102017-04-09 12:32:51 +020093 llist_for_each_entry_safe(conn, conn_temp, &active_connections, list) {
94 if (conn->conn_id == conn_id) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +010095 LOGPBSCCONN(conn, LOGL_DEBUG, "Removing A-interface conn\n");
Philipp Maierfbf66102017-04-09 12:32:51 +020096 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
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100151 LOGPCONN(conn, LOGL_DEBUG, "Passing DTAP message from MSC to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200152
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) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100161 LOGPCONN(conn, LOGL_ERROR, "Unable to generate BSSMAP DTAP message!\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200162 return -EINVAL;
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100163 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200164
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100165 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", osmo_hexdump(msg_resp->data, msg_resp->len));
Philipp Maier4502f5f2017-09-07 11:39:58 +0200166 /* osmo_sccp_tx_data_msg() takes ownership of msg_resp */
Philipp Maierfbf66102017-04-09 12:32:51 +0200167 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
168}
169
170/* Send Cipher mode command via A-interface */
171int a_iface_tx_cipher_mode(const struct gsm_subscriber_connection *conn,
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100172 struct gsm0808_encrypt_info *ei, int include_imeisv)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200173{
174 /* TODO generalize for A- and Iu interfaces, don't name after 08.08 */
Philipp Maierfbf66102017-04-09 12:32:51 +0200175 struct msgb *msg_resp;
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100176 uint8_t crm = 0x01;
Philipp Maierfbf66102017-04-09 12:32:51 +0200177
178 OSMO_ASSERT(conn);
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100179 LOGPCONN(conn, LOGL_DEBUG, "Cipher Mode Command to BSC, %u ciphers (%s)",
180 ei->perm_algo_len, osmo_hexdump_nospc(ei->perm_algo, ei->perm_algo_len));
181 LOGPC(DMSC, LOGL_DEBUG, " key %s\n", osmo_hexdump_nospc(ei->key, ei->key_len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200182
Neels Hofmeyr703638e2017-12-14 05:30:16 +0100183 msg_resp = gsm0808_create_cipher(ei, include_imeisv ? &crm : NULL);
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100184 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", osmo_hexdump(msg_resp->data, msg_resp->len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200185
186 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp);
187}
188
189/* Page a subscriber via A-interface */
190int a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
191{
192 struct bsc_context *bsc_ctx;
193 struct gsm0808_cell_id_list cil;
194 struct msgb *msg;
195 int page_count = 0;
196 struct osmo_ss7_instance *ss7;
197
198 OSMO_ASSERT(imsi);
199
200 cil.id_discr = CELL_IDENT_LAC;
201 cil.id_list_lac[0] = lac;
202 cil.id_list_len = 1;
203
204 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
205 OSMO_ASSERT(ss7);
206
207 /* Deliver paging request to all known BSCs */
208 llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) {
209 if (a_reset_conn_ready(bsc_ctx->reset)) {
210 LOGP(DMSC, LOGL_DEBUG,
211 "Passing paging message from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
212 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
213 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
214 msg = gsm0808_create_paging(imsi, &tmsi, &cil, NULL);
215 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user,
216 &bsc_ctx->msc_addr, &bsc_ctx->bsc_addr, msg);
217 page_count++;
218 } else {
219 LOGP(DMSC, LOGL_DEBUG,
220 "Connection down, dropping paging from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n",
221 osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr),
222 osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac);
223 }
224 }
225
226 if (page_count <= 0)
227 LOGP(DMSC, LOGL_ERROR, "Could not deliver paging because none of the associated BSCs is available!\n");
228
229 return page_count;
230}
231
232/* Convert speech version field */
233static uint8_t convert_Abis_sv_to_A_sv(int speech_ver)
234{
235 /* The speech versions that are transmitted in the Bearer capability
236 * information element, that is transmitted on the Abis interfece
237 * use a different encoding than the permitted speech version
238 * identifier, that is signalled in the channel type element on the A
239 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
240 * 10.5.103 */
241
242 switch (speech_ver) {
243 case GSM48_BCAP_SV_FR:
244 return GSM0808_PERM_FR1;
245 break;
246 case GSM48_BCAP_SV_HR:
247 return GSM0808_PERM_HR1;
248 break;
249 case GSM48_BCAP_SV_EFR:
250 return GSM0808_PERM_FR2;
251 break;
252 case GSM48_BCAP_SV_AMR_F:
253 return GSM0808_PERM_FR3;
254 break;
255 case GSM48_BCAP_SV_AMR_H:
256 return GSM0808_PERM_HR3;
257 break;
258 case GSM48_BCAP_SV_AMR_OFW:
259 return GSM0808_PERM_FR4;
260 break;
261 case GSM48_BCAP_SV_AMR_OHW:
262 return GSM0808_PERM_HR4;
263 break;
264 case GSM48_BCAP_SV_AMR_FW:
265 return GSM0808_PERM_FR5;
266 break;
267 case GSM48_BCAP_SV_AMR_OH:
268 return GSM0808_PERM_HR6;
269 break;
270 }
271
272 /* If nothing matches, tag the result as invalid */
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100273 LOGP(DMSC, LOGL_ERROR, "Invalid permitted speech version: %d\n", speech_ver);
Philipp Maierfbf66102017-04-09 12:32:51 +0200274 return 0xFF;
275}
276
277/* Convert speech preference field */
278static uint8_t convert_Abis_prev_to_A_pref(int radio)
279{
280 /* The Radio channel requirement field that is transmitted in the
281 * Bearer capability information element, that is transmitted on the
282 * Abis interfece uses a different encoding than the Channel rate and
283 * type field that is signalled in the channel type element on the A
284 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
285 * 10.5.102 */
286
287 switch (radio) {
288 case GSM48_BCAP_RRQ_FR_ONLY:
289 return GSM0808_SPEECH_FULL_BM;
290 case GSM48_BCAP_RRQ_DUAL_FR:
291 return GSM0808_SPEECH_FULL_PREF;
292 case GSM48_BCAP_RRQ_DUAL_HR:
293 return GSM0808_SPEECH_HALF_PREF;
294 }
295
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100296 LOGP(DMSC, LOGL_ERROR, "Invalid radio channel preference: %d; defaulting to full rate.\n",
297 radio);
Philipp Maierfbf66102017-04-09 12:32:51 +0200298 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
Neels Hofmeyr563e1db2017-12-28 16:31:58 +0100380 LOGPCONN(conn, LOGL_DEBUG, "Sending Assignment Command to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200381
382 /* Channel type */
383 rc = enc_channel_type(&ct, &trans->bearer_cap);
384 if (rc < 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100385 LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate channel type\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200386 return -EINVAL;
387 }
388
389 /* Speech codec list */
390 rc = enc_speech_codec_list(&scl, &ct);
391 if (rc < 0) {
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100392 LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate speech codec list\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200393 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;
Philipp Maier621ba032017-11-07 17:19:25 +0100399 rtp_addr_in.sin_port = osmo_htons(conn->rtp.local_port_ran);
400 rtp_addr_in.sin_addr.s_addr = inet_addr(conn->rtp.local_addr_ran);
401
402 if (rtp_addr_in.sin_addr.s_addr == INADDR_NONE) {
403 LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Address -- assignment not sent!\n");
404 return -EINVAL;
405 }
406 if (rtp_addr_in.sin_port == 0) {
407 LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Port -- assignment not sent!\n");
408 return -EINVAL;
409 }
Philipp Maierfbf66102017-04-09 12:32:51 +0200410
411 memset(&rtp_addr, 0, sizeof(rtp_addr));
412 memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in));
413
414 msg = gsm0808_create_ass(&ct, NULL, &rtp_addr, &scl, ci_ptr);
415
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100416 LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", osmo_hexdump(msg->data, msg->len));
Philipp Maierfbf66102017-04-09 12:32:51 +0200417 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
418}
419
420/* Send clear command via A-interface */
421int a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
422{
423 struct msgb *msg;
424
Neels Hofmeyr04960b12017-12-18 05:17:25 +0100425 LOGPCONN(conn, LOGL_NOTICE, "Sending Clear command to BSC\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200426
427 msg = gsm0808_create_clear_command(GSM0808_CAUSE_CALL_CONTROL);
428 return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg);
429}
430
431/* Callback function: Close all open connections */
432static void a_reset_cb(const void *priv)
433{
434 struct msgb *msg;
435 struct bsc_context *bsc_ctx = (struct bsc_context*) priv;
436 struct osmo_ss7_instance *ss7;
437
438 /* Skip if the A interface is not properly initalized yet */
439 if (!gsm_network)
440 return;
441
442 /* Clear all now orphaned subscriber connections */
443 a_clear_all(bsc_ctx->sccp_user, &bsc_ctx->bsc_addr);
444
445 /* Send reset to the remote BSC */
446 ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance);
447 OSMO_ASSERT(ss7);
448 LOGP(DMSC, LOGL_NOTICE, "Sending RESET to BSC %s\n", osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr));
449 msg = gsm0808_create_reset();
450 osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, &bsc_ctx->msc_addr,
451 &bsc_ctx->bsc_addr, msg);
452}
453
454/* Add a new BSC connection to our internal list with known BSCs */
455static void add_bsc(const struct osmo_sccp_addr *msc_addr, const struct osmo_sccp_addr *bsc_addr,
456 struct osmo_sccp_user *scu)
457{
458 struct bsc_context *bsc_ctx;
459 struct osmo_ss7_instance *ss7;
Philipp Maier8ae3c922017-11-09 11:17:24 +0100460 char bsc_name[32];
Philipp Maierfbf66102017-04-09 12:32:51 +0200461
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 */
Philipp Maier8ae3c922017-11-09 11:17:24 +0100483 snprintf(bsc_name, sizeof(bsc_name), "bsc-%i", bsc_addr->pc);
484 bsc_ctx->reset = a_reset_alloc(bsc_ctx, bsc_name, a_reset_cb, bsc_ctx);
Philipp Maierfbf66102017-04-09 12:32:51 +0200485}
486
487/* Callback function, called by the SSCP stack when data arrives */
488static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
489{
490 struct osmo_sccp_user *scu = _scu;
491 struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph;
492 int rc = 0;
493 struct a_conn_info a_conn_info;
494 memset(&a_conn_info, 0, sizeof(a_conn_info));
495 a_conn_info.network = gsm_network;
496 a_conn_info.reset = NULL;
497
498 switch (OSMO_PRIM_HDR(&scu_prim->oph)) {
499 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
500 /* Handle inbound connection indication */
501 add_bsc(&scu_prim->u.connect.called_addr, &scu_prim->u.connect.calling_addr, scu);
502 a_conn_info.conn_id = scu_prim->u.connect.conn_id;
503 a_conn_info.msc_addr = &scu_prim->u.connect.called_addr;
504 a_conn_info.bsc_addr = &scu_prim->u.connect.calling_addr;
505 a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
506
507 if (a_reset_conn_ready(a_conn_info.reset) == false) {
508 rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, a_conn_info.msc_addr,
509 SCCP_RETURN_CAUSE_UNQUALIFIED);
510 break;
511 }
512
513 osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0);
514 if (msgb_l2len(oph->msg) > 0) {
515 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n",
516 scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100517 rc = a_sccp_rx_dt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200518 } else
519 LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id);
520
521 record_bsc_con(scu, scu_prim->u.connect.conn_id);
522 break;
523
524 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
525 /* Handle incoming connection oriented data */
526 a_conn_info.conn_id = scu_prim->u.data.conn_id;
527 LOGP(DMSC, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n",
528 scu_prim->u.data.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100529 a_sccp_rx_dt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200530 break;
531
532 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
533 /* Handle inbound UNITDATA */
534 add_bsc(&scu_prim->u.unitdata.called_addr, &scu_prim->u.unitdata.calling_addr, scu);
535 a_conn_info.msc_addr = &scu_prim->u.unitdata.called_addr;
536 a_conn_info.bsc_addr = &scu_prim->u.unitdata.calling_addr;
537 a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr);
538 DEBUGP(DMSC, "N-UNITDATA.ind(%s)\n", osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Neels Hofmeyrc1d69252017-12-18 04:06:04 +0100539 a_sccp_rx_udt(scu, &a_conn_info, oph->msg);
Philipp Maierfbf66102017-04-09 12:32:51 +0200540 break;
541
542 default:
Maxc309fe32018-01-24 14:02:38 +0100543 LOGP(DMSC, LOGL_ERROR, "Unhandled SIGTRAN operation %s on primitive %u\n",
544 get_value_string(osmo_prim_op_names, oph->operation), oph->primitive);
Philipp Maierfbf66102017-04-09 12:32:51 +0200545 break;
546 }
547
548 return rc;
549}
550
551/* Clear all subscriber connections on a specified BSC */
552void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr)
553{
554 struct gsm_subscriber_connection *conn;
555 struct gsm_subscriber_connection *conn_temp;
556 struct gsm_network *network = gsm_network;
557
558 OSMO_ASSERT(scu);
559 OSMO_ASSERT(bsc_addr);
560
561 llist_for_each_entry_safe(conn, conn_temp, &network->subscr_conns, entry) {
562 /* Clear only A connections and connections that actually
563 * belong to the specified BSC */
564 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 +0100565 LOGPCONN(conn, LOGL_NOTICE, "Dropping orphaned subscriber connection\n");
Philipp Maierfbf66102017-04-09 12:32:51 +0200566 msc_clear_request(conn, GSM48_CC_CAUSE_SWITCH_CONG);
567
568 /* If there is still an SCCP connection active, remove it now */
569 if (check_connection_active(conn->a.conn_id)) {
570 osmo_sccp_tx_disconn(scu, conn->a.conn_id, bsc_addr,
571 SCCP_RELEASE_CAUSE_END_USER_ORIGINATED);
572 a_delete_bsc_con(conn->a.conn_id);
573 }
574 }
575 }
576}
577
578/* Initalize A interface connection between to MSC and BSC */
579int a_init(struct osmo_sccp_instance *sccp, struct gsm_network *network)
580{
581 OSMO_ASSERT(sccp);
582 OSMO_ASSERT(network);
583
584 /* FIXME: Remove hardcoded parameters, use parameters in parameter list */
585 LOGP(DMSC, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n");
586
587 /* Set GSM network variable, there can only be
588 * one network by design */
589 if (gsm_network != NULL) {
590 OSMO_ASSERT(gsm_network == network);
591 } else
592 gsm_network = network;
593
594 /* SCCP Protocol stack */
595 osmo_sccp_user_bind(sccp, "OsmoMSC-A", sccp_sap_up, SCCP_SSN_BSSAP);
596
597 return 0;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200598}