blob: 9828da1647d90fcea924a404e2b3fd05febdf12d [file] [log] [blame]
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +08001/* main MSC management code... */
2
3/*
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02004 * (C) 2010,2013 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther85531cc2010-10-06 20:37:09 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080021 *
22 */
23
Neels Hofmeyr90843962017-09-04 15:04:35 +020024#include <osmocom/msc/debug.h>
25#include <osmocom/msc/transaction.h>
26#include <osmocom/msc/db.h>
27#include <osmocom/msc/vlr.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020028#include <osmocom/msc/a_iface.h>
Max43b01b02017-09-15 11:22:30 +020029#include <osmocom/msc/gsm_04_08.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020030#include <osmocom/msc/gsm_04_11.h>
Neels Hofmeyrf383d412018-12-19 17:05:13 +010031#include <osmocom/msc/msc_mgcp.h>
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +080032
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020033#include "../../bscconfig.h"
34#ifdef BUILD_IU
35#include <osmocom/ranap/iu_client.h>
36#else
Neels Hofmeyr90843962017-09-04 15:04:35 +020037#include <osmocom/msc/iu_dummy.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020038#endif
39
Neels Hofmeyr55014d02018-03-22 16:43:40 +010040struct gsm_network *gsm_network_init(void *ctx, mncc_recv_cb_t mncc_recv)
41{
42 struct gsm_network *net;
43
44 net = talloc_zero(ctx, struct gsm_network);
45 if (!net)
46 return NULL;
47
48 net->plmn = (struct osmo_plmn_id){ .mcc=1, .mnc=1 };
49
50 /* Permit a compile-time default of A5/3 and A5/1 */
51 net->a5_encryption_mask = (1 << 3) | (1 << 1);
52
53 /* Use 30 min periodic update interval as sane default */
54 net->t3212 = 5;
55
Philipp Maier9ca7b312018-10-10 17:00:49 +020056 net->mncc_guard_timeout = 180;
57
Neels Hofmeyr55014d02018-03-22 16:43:40 +010058 net->paging_response_timer = MSC_PAGING_RESPONSE_TIMER_DEFAULT;
59
60 INIT_LLIST_HEAD(&net->trans_list);
61 INIT_LLIST_HEAD(&net->upqueue);
Neels Hofmeyrc036b792018-11-29 22:37:51 +010062 INIT_LLIST_HEAD(&net->ran_conns);
Neels Hofmeyr55014d02018-03-22 16:43:40 +010063
64 /* init statistics */
65 net->msc_ctrs = rate_ctr_group_alloc(net, &msc_ctrg_desc, 0);
66 if (!net->msc_ctrs) {
67 talloc_free(net);
68 return NULL;
69 }
70 net->active_calls = osmo_counter_alloc("msc.active_calls");
Vadim Yanitskiyad64e2a2018-06-26 18:27:25 +070071 net->active_nc_ss = osmo_counter_alloc("msc.active_nc_ss");
Neels Hofmeyr55014d02018-03-22 16:43:40 +010072
73 net->mncc_recv = mncc_recv;
74
75 INIT_LLIST_HEAD(&net->a.bscs);
76
77 return net;
78}
79
Neels Hofmeyr80447eb2018-12-05 01:11:28 +010080void gsm_network_set_mncc_sock_path(struct gsm_network *net, const char *mncc_sock_path)
81{
82 if (net->mncc_sock_path)
83 talloc_free(net->mncc_sock_path);
84 net->mncc_sock_path = mncc_sock_path ? talloc_strdup(net, mncc_sock_path) : NULL;
85}
86
Harald Welte2483f1b2016-06-19 18:06:02 +020087/* Receive a SAPI-N-REJECT from BSC */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +010088void ran_conn_sapi_n_reject(struct ran_conn *conn, int dlci)
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080089{
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +080090 int sapi = dlci & 0x7;
91
92 if (sapi == UM_SAPI_SMS)
93 gsm411_sapi_n_reject(conn);
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080094}
95
Neels Hofmeyrd03e7282018-11-30 01:20:32 +010096/* receive a Level 3 Complete message.
97 * Ownership of the conn is completely passed to the conn FSM, i.e. for both acceptance and rejection,
98 * the conn FSM shall decide when to release this conn. It may already be discarded before this exits. */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +010099void ran_conn_compl_l3(struct ran_conn *conn,
100 struct msgb *msg, uint16_t chosen_channel)
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800101{
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100102 ran_conn_get(conn, RAN_CONN_USE_COMPL_L3);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800103 gsm0408_dispatch(conn, msg);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100104 ran_conn_put(conn, RAN_CONN_USE_COMPL_L3);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800105}
106
Harald Welte2483f1b2016-06-19 18:06:02 +0200107/* Receive a DTAP message from BSC */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100108void ran_conn_dtap(struct ran_conn *conn, struct msgb *msg)
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800109{
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100110 ran_conn_get(conn, RAN_CONN_USE_DTAP);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800111 gsm0408_dispatch(conn, msg);
Harald Welte2483f1b2016-06-19 18:06:02 +0200112
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100113 ran_conn_put(conn, RAN_CONN_USE_DTAP);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800114}
115
Harald Welte2483f1b2016-06-19 18:06:02 +0200116/* Receive an ASSIGNMENT COMPLETE from BSC */
Neels Hofmeyr85cb2532018-12-11 18:59:27 +0100117void msc_assign_compl(struct ran_conn *conn,
118 uint8_t rr_cause, uint8_t chosen_channel,
119 uint8_t encr_alg_id, uint8_t speec)
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100120{
Neels Hofmeyr85cb2532018-12-11 18:59:27 +0100121 LOGP(DRR, LOGL_DEBUG, "MSC assign complete (do nothing).\n");
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100122}
123
Harald Welte2483f1b2016-06-19 18:06:02 +0200124/* Receive an ASSIGNMENT FAILURE from BSC */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100125void ran_conn_assign_fail(struct ran_conn *conn, uint8_t cause, uint8_t *rr_cause)
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100126{
Neels Hofmeyrf383d412018-12-19 17:05:13 +0100127 LOGPFSMSL(conn->fi, DRR, LOGL_ERROR, "Assignment Failure: cause=%u rr_cause=%u.\n",
128 cause, rr_cause ? *rr_cause : 0);
129 msc_mgcp_ass_fail(conn);
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100130}
131
Harald Welte2483f1b2016-06-19 18:06:02 +0200132/* Receive a CLASSMARK CHANGE from BSC */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100133void ran_conn_classmark_chg(struct ran_conn *conn,
134 const uint8_t *cm2, uint8_t cm2_len,
135 const uint8_t *cm3, uint8_t cm3_len)
Harald Welte95e862c2012-01-23 10:28:35 +0100136{
Neels Hofmeyr68cf9572018-09-18 15:52:58 +0200137 struct gsm_classmark *cm;
138
139 if (!conn->vsub)
140 cm = &conn->temporary_classmark;
141 else
142 cm = &conn->vsub->classmark;
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200143
Harald Welte2483f1b2016-06-19 18:06:02 +0200144 if (cm2 && cm2_len) {
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200145 if (cm2_len > sizeof(cm->classmark2)) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200146 LOGP(DRR, LOGL_NOTICE, "%s: classmark2 is %u bytes, truncating at %zu bytes\n",
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200147 vlr_subscr_name(conn->vsub), cm2_len, sizeof(cm->classmark2));
148 cm2_len = sizeof(cm->classmark2);
Harald Welte95e862c2012-01-23 10:28:35 +0100149 }
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200150 cm->classmark2_len = cm2_len;
151 memcpy(cm->classmark2, cm2, cm2_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200152 }
153 if (cm3 && cm3_len) {
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200154 if (cm3_len > sizeof(cm->classmark3)) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200155 LOGP(DRR, LOGL_NOTICE, "%s: classmark3 is %u bytes, truncating at %zu bytes\n",
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200156 vlr_subscr_name(conn->vsub), cm3_len, sizeof(cm->classmark3));
157 cm3_len = sizeof(cm->classmark3);
Harald Welte2483f1b2016-06-19 18:06:02 +0200158 }
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200159 cm->classmark3_len = cm3_len;
160 memcpy(cm->classmark3, cm3, cm3_len);
Harald Welte95e862c2012-01-23 10:28:35 +0100161 }
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200162
163 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100164 if (conn->fi->state == RAN_CONN_S_WAIT_CLASSMARK_UPDATE)
165 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_CLASSMARK_UPDATE, NULL);
Harald Welte95e862c2012-01-23 10:28:35 +0100166}
167
Harald Welte2483f1b2016-06-19 18:06:02 +0200168/* Receive a CIPHERING MODE COMPLETE from BSC */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100169void ran_conn_cipher_mode_compl(struct ran_conn *conn, struct msgb *msg, uint8_t alg_id)
Harald Weltecf149ee2012-01-23 16:40:24 +0100170{
Harald Welte2483f1b2016-06-19 18:06:02 +0200171 struct vlr_ciph_result ciph_res = { .cause = VLR_CIPH_REJECT };
Harald Weltecf149ee2012-01-23 16:40:24 +0100172
Harald Welte2483f1b2016-06-19 18:06:02 +0200173 if (!conn) {
Harald Welte284c39a2018-01-24 22:38:06 +0100174 LOGP(DRR, LOGL_ERROR, "invalid: rx Ciphering Mode Complete on NULL conn\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200175 return;
176 }
177 if (!conn->vsub) {
Harald Welte284c39a2018-01-24 22:38:06 +0100178 LOGP(DRR, LOGL_ERROR, "invalid: rx Ciphering Mode Complete for NULL subscr\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200179 return;
Harald Weltecf149ee2012-01-23 16:40:24 +0100180 }
181
Harald Welte284c39a2018-01-24 22:38:06 +0100182 DEBUGP(DRR, "%s: CIPHERING MODE COMPLETE\n", vlr_subscr_name(conn->vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +0200183
Harald Welte284c39a2018-01-24 22:38:06 +0100184 if (msg) {
185 struct gsm48_hdr *gh = msgb_l3(msg);
186 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
187 struct tlv_parsed tp;
188 uint8_t mi_type;
Harald Welte2483f1b2016-06-19 18:06:02 +0200189
Harald Welte284c39a2018-01-24 22:38:06 +0100190 if (!gh) {
191 LOGP(DRR, LOGL_ERROR, "invalid: msgb without l3 header\n");
192 return;
193 }
194
195 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
196
197 /* bearer capability */
198 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) {
199 mi_type = TLVP_VAL(&tp, GSM48_IE_MOBILE_ID)[0] & GSM_MI_TYPE_MASK;
200 if (mi_type == GSM_MI_TYPE_IMEISV
201 && TLVP_LEN(&tp, GSM48_IE_MOBILE_ID) > 0) {
Neels Hofmeyrfa10eda2018-03-13 01:22:01 +0100202 gsm48_mi_to_string(ciph_res.imeisv, sizeof(ciph_res.imeisv),
Harald Welte284c39a2018-01-24 22:38:06 +0100203 TLVP_VAL(&tp, GSM48_IE_MOBILE_ID),
204 TLVP_LEN(&tp, GSM48_IE_MOBILE_ID));
Harald Welte284c39a2018-01-24 22:38:06 +0100205 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200206 }
207 }
208
Neels Hofmeyrf41658d2018-11-30 04:35:50 +0100209 conn->geran_encr.alg_id = alg_id;
Neels Hofmeyrb0779bb2018-11-29 23:37:19 +0100210
Harald Welte2483f1b2016-06-19 18:06:02 +0200211 ciph_res.cause = VLR_CIPH_COMPL;
212 vlr_subscr_rx_ciph_res(conn->vsub, &ciph_res);
Harald Weltecf149ee2012-01-23 16:40:24 +0100213}
214
Harald Welte2483f1b2016-06-19 18:06:02 +0200215/* Receive a CLEAR REQUEST from BSC */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100216int ran_conn_clear_request(struct ran_conn *conn, uint32_t cause)
Harald Welte2483f1b2016-06-19 18:06:02 +0200217{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100218 ran_conn_close(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +0200219 return 1;
220}
221
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200222void msc_stop_paging(struct vlr_subscr *vsub)
223{
224 DEBUGP(DPAG, "Paging can stop for %s\n", vlr_subscr_name(vsub));
225 /* tell BSCs and RNCs to stop paging? How? */
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800226}