blob: 21ffaaad8028c8b111892eb8be349ec45fd7d5b6 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltea43e0b42016-06-19 18:06:02 +02004/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * 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
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (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.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
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/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Sylvain Munaut30a15382009-12-24 00:27:26 +010037#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000038#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
41#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080044#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000045#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000046#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000047#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000048#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020049#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000050#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020051#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080055#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080056#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010057#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020058#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020059#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/bitvec.h>
Harald Weltea43e0b42016-06-19 18:06:02 +020061#include <openbsc/vlr.h>
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +020062#include <openbsc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080063
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010064#include <osmocom/gsm/gsm48.h>
65#include <osmocom/gsm/gsm0480.h>
66#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020067#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010068#include <osmocom/core/msgb.h>
69#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010070#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010071#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000072
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020073#include <assert.h>
74
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +020075
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020076void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010077void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020078
Harald Weltea43e0b42016-06-19 18:06:02 +020079static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
80 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080081static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020082 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000083
Harald Welte52b1f982008-12-23 20:25:15 +000084struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 uint16_t mcc;
86 uint16_t mnc;
87 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000088};
89
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020090static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080091
Harald Welte31c00f72011-03-03 23:29:05 +010092void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
93{
94 net->mncc_recv(net, msg);
95}
96
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080097static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
98 struct gsm_trans *trans)
99{
100 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
101
102 /* if we get passed a transaction reference, do some common
103 * work that the caller no longer has to do */
104 if (trans) {
105 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800106 }
107
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100108 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800109}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100110
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800111int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
112{
113 struct gsm48_hdr *gh;
114 struct msgb *ss_notify;
115
116 ss_notify = gsm0480_create_notifySS(message);
117 if (!ss_notify)
118 return -1;
119
120 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
121 uint8_t *data = msgb_push(ss_notify, 1);
122 data[0] = ss_notify->len - 1;
123 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
124 gh->msg_type = GSM48_MT_CC_FACILITY;
125 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
126}
127
Harald Weltea43e0b42016-06-19 18:06:02 +0200128/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100129void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
130{
131 struct gsm_trans *trans, *temp;
132
133 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
134
135 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100136 if (trans->protocol == protocol) {
137 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100138 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100139 }
Harald Welte371efe52010-12-22 23:17:50 +0100140 }
141}
142
Holger Freyther429e7762008-12-30 13:28:30 +0000143/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200144int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000145{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800146 struct msgb *msg;
147
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800148 msg = gsm48_create_loc_upd_rej(cause);
149 if (!msg) {
150 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
151 return -1;
152 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200153
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200154 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
155 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100156
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800157 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000158}
159
160/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Weltea43e0b42016-06-19 18:06:02 +0200161static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
162 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000163{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100164 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000165 struct gsm48_hdr *gh;
166 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200167 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200168
Harald Welte52b1f982008-12-23 20:25:15 +0000169 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
170 gh->proto_discr = GSM48_PDISC_MM;
171 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
172
173 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200174 gsm48_generate_lai(lai, conn->network->country_code,
175 conn->network->network_code,
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200176 conn->lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000177
Harald Weltea43e0b42016-06-19 18:06:02 +0200178 if (send_tmsi == GSM_RESERVED_TMSI) {
179 /* we did not allocate a TMSI to the MS, so we need to
180 * include the IMSI in order for the MS to delete any
181 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200182 uint8_t mi[10];
183 int len;
Harald Weltea43e0b42016-06-19 18:06:02 +0200184 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200185 mid = msgb_put(msg, len);
186 memcpy(mid, mi, len);
187 } else {
Harald Weltea43e0b42016-06-19 18:06:02 +0200188 /* Include the TMSI, which means that the MS will send a
189 * TMSI REALLOCATION COMPLETE, and we should wait for
190 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200191 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Weltea43e0b42016-06-19 18:06:02 +0200192 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200193 }
Harald Weltea43e0b42016-06-19 18:06:02 +0200194 /* TODO: Follow-on proceed */
195 /* TODO: CTS permission */
196 /* TODO: Equivalent PLMNs */
197 /* TODO: Emergency Number List */
198 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000199
200 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
201
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800202 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000203}
204
Harald Weltebf5e8df2009-02-03 12:59:45 +0000205/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200206static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000207{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100208 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000209 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000210
Harald Welte231ad4f2008-12-27 11:15:38 +0000211 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
212 gh->proto_discr = GSM48_PDISC_MM;
213 gh->msg_type = GSM48_MT_MM_ID_REQ;
214 gh->data[0] = id_type;
215
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800216 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000217}
218
Harald Weltebf5e8df2009-02-03 12:59:45 +0000219/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800220static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000221{
222 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200223 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200224 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000225
Harald Weltea43e0b42016-06-19 18:06:02 +0200226 if (!conn->vsub) {
227 LOGP(DMM, LOGL_ERROR,
228 "Rx MM Identity Response: invalid: no subscriber\n");
229 return -EINVAL;
230 }
231
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200232 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200233 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
234 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000235
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200236 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100237
Harald Weltea43e0b42016-06-19 18:06:02 +0200238 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000239}
240
Harald Weltea43e0b42016-06-19 18:06:02 +0200241/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200242static const struct value_string lupd_names[] = {
243 { GSM48_LUPD_NORMAL, "NORMAL" },
244 { GSM48_LUPD_PERIODIC, "PERIODIC" },
245 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
246 { 0, NULL }
247};
Harald Welte2a139372009-02-22 21:14:55 +0000248
Harald Weltea43e0b42016-06-19 18:06:02 +0200249/* Chapter 9.2.15: Receive Location Updating Request.
250 * Keep this function non-static for direct invocation by unit tests. */
251int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000252{
Harald Weltea43e0b42016-06-19 18:06:02 +0200253 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
254 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000255 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000256 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200257 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200258 char mi_string[GSM48_MI_SIZE];
Harald Weltea43e0b42016-06-19 18:06:02 +0200259 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
260
261 uint32_t tmsi;
262 char *imsi;
263 struct osmo_location_area_id old_lai, new_lai;
264 struct osmo_fsm_inst *lu_fsm;
265 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000266
Harald Welte8470bf22008-12-25 23:28:35 +0000267 lu = (struct gsm48_loc_upd_req *) gh->data;
268
269 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000270
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200271 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000272
Harald Weltea43e0b42016-06-19 18:06:02 +0200273 rc = msc_create_conn_fsm(conn, mi_string);
274 if (rc)
275 /* logging already happened in msc_create_conn_fsm() */
276 return rc;
277
278 conn->classmark.classmark1 = lu->classmark1;
279 conn->classmark.classmark1_set = true;
280
281 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
282 gsm48_mi_type_name(mi_type), mi_string,
283 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000284
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200285 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100286
Harald Welte24ff6ee2009-12-22 00:41:05 +0100287 switch (lu->type) {
288 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200289 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Weltea43e0b42016-06-19 18:06:02 +0200290 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100291 break;
292 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200293 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Weltea43e0b42016-06-19 18:06:02 +0200294 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100295 break;
296 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200297 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Weltea43e0b42016-06-19 18:06:02 +0200298 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100299 break;
300 }
301
Harald Weltea43e0b42016-06-19 18:06:02 +0200302 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
303 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
304 /* TODO: 10.5.7.8 Device properties */
305 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200306
Harald Welte52b1f982008-12-23 20:25:15 +0000307 switch (mi_type) {
308 case GSM_MI_TYPE_IMSI:
Harald Weltea43e0b42016-06-19 18:06:02 +0200309 tmsi = GSM_RESERVED_TMSI;
310 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000311 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000312 case GSM_MI_TYPE_TMSI:
Harald Weltea43e0b42016-06-19 18:06:02 +0200313 tmsi = tmsi_from_string(mi_string);
314 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000315 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200316 default:
Harald Weltea0368542009-06-27 02:58:43 +0200317 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Weltea43e0b42016-06-19 18:06:02 +0200318 tmsi = GSM_RESERVED_TMSI;
319 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000320 break;
321 }
322
Harald Weltea43e0b42016-06-19 18:06:02 +0200323 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
324 &old_lai.plmn.mnc, &old_lai.lac);
325 new_lai.plmn.mcc = conn->network->country_code;
326 new_lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200327 new_lai.lac = conn->lac;
Harald Weltea43e0b42016-06-19 18:06:02 +0200328 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200329
Harald Weltea43e0b42016-06-19 18:06:02 +0200330 lu_fsm = vlr_loc_update(conn->conn_fsm,
331 SUBSCR_CONN_E_ACCEPTED,
332 SUBSCR_CONN_E_CN_CLOSE,
333 (void*)&conn_from_lu,
334 net->vlr, conn, vlr_lu_type, tmsi, imsi,
335 &old_lai, &new_lai,
336 conn->network->authentication_required,
337 conn->network->a5_encryption,
338 classmark_is_r99(&conn->classmark),
339 conn->via_ran == RAN_UTRAN_IU,
340 net->vlr->cfg.assign_tmsi);
341 if (!lu_fsm) {
342 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
343 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800344 }
345
Harald Weltea43e0b42016-06-19 18:06:02 +0200346 /* From vlr_loc_update() we expect an implicit dispatch of
347 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
348 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000349
Harald Weltea43e0b42016-06-19 18:06:02 +0200350 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
351 LOGP(DRR, LOGL_ERROR,
352 "%s: internal error during Location Updating attempt\n",
353 mi_string);
354 return -EIO;
355 }
356
357 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000358}
359
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100360/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Weltea43e0b42016-06-19 18:06:02 +0200361/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100362static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800363{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100364 uint8_t ret;
365
366 ret = value / 10;
367 ret |= (value % 10) << 4;
368
369 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800370}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100371
Harald Welte4bfdfe72009-06-10 23:11:52 +0800372
Harald Weltedb253af2008-12-30 17:56:55 +0000373/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800374int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000375{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100376 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000377 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200378 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200379 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200380 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100381
Harald Welte4bfdfe72009-06-10 23:11:52 +0800382 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100383 struct tm* gmt_time;
384 struct tm* local_time;
385 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100386 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000387
Harald Weltedb253af2008-12-30 17:56:55 +0000388 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
389 gh->proto_discr = GSM48_PDISC_MM;
390 gh->msg_type = GSM48_MT_MM_INFO;
391
392 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200393#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000394 name_len = strlen(net->name_long);
395 /* 10.5.3.5a */
396 ptr8 = msgb_put(msg, 3);
397 ptr8[0] = GSM48_IE_NAME_LONG;
398 ptr8[1] = name_len*2 +1;
399 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
400
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200401 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000402 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000403 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000404
405 /* FIXME: Use Cell Broadcast, not UCS-2, since
406 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200407#endif
408 name_len = (strlen(net->name_long)*7)/8;
409 name_pad = (8 - strlen(net->name_long)*7)%8;
410 if (name_pad > 0)
411 name_len++;
412 /* 10.5.3.5a */
413 ptr8 = msgb_put(msg, 3);
414 ptr8[0] = GSM48_IE_NAME_LONG;
415 ptr8[1] = name_len +1;
416 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
417
418 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100419 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200420
Harald Weltedb253af2008-12-30 17:56:55 +0000421 }
422
423 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200424#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000425 name_len = strlen(net->name_short);
426 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200427 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200428 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000429 ptr8[1] = name_len*2 + 1;
430 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
431
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200432 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000433 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000434 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200435#endif
436 name_len = (strlen(net->name_short)*7)/8;
437 name_pad = (8 - strlen(net->name_short)*7)%8;
438 if (name_pad > 0)
439 name_len++;
440 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200441 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200442 ptr8[0] = GSM48_IE_NAME_SHORT;
443 ptr8[1] = name_len +1;
444 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
445
446 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100447 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200448
Harald Weltedb253af2008-12-30 17:56:55 +0000449 }
450
Harald Weltedb253af2008-12-30 17:56:55 +0000451 /* Section 10.5.3.9 */
452 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100453 gmt_time = gmtime(&cur_t);
454
Harald Weltedb253af2008-12-30 17:56:55 +0000455 ptr8 = msgb_put(msg, 8);
456 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100457 ptr8[1] = bcdify(gmt_time->tm_year % 100);
458 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
459 ptr8[3] = bcdify(gmt_time->tm_mday);
460 ptr8[4] = bcdify(gmt_time->tm_hour);
461 ptr8[5] = bcdify(gmt_time->tm_min);
462 ptr8[6] = bcdify(gmt_time->tm_sec);
463
Neels Hofmeyr73983952016-05-10 13:29:33 +0200464 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200465 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200466 if (net->tz.hr < 0) {
467 tzunits = ((net->tz.hr/-1)*4);
468 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100469 ptr8[7] = bcdify(tzunits);
470 /* Set negative time */
471 ptr8[7] |= 0x08;
472 }
473 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200474 tzunits = net->tz.hr*4;
475 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100476 ptr8[7] = bcdify(tzunits);
477 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100478 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200479 if (net->tz.dst >= 0 && net->tz.dst <= 2)
480 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100481 }
482 else {
483 /* Need to get GSM offset and convert into 15 min units */
484 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
485 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200486#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100487 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200488#else
489#warning find a portable way to obtain the timezone offset
490 tzunits = 0;
491#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100492 if (tzunits < 0) {
493 tzunits = tzunits/-1;
494 ptr8[7] = bcdify(tzunits);
495 /* Flip it to negative */
496 ptr8[7] |= 0x08;
497 }
498 else
499 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100500
501 /* Does not support DST +2 */
502 if (local_time->tm_isdst)
503 dst = 1;
504 }
505
506 if (dst) {
507 ptr8 = msgb_put(msg, 3);
508 ptr8[0] = GSM48_IE_NET_DST;
509 ptr8[1] = 1;
510 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100511 }
Harald Weltedb253af2008-12-30 17:56:55 +0000512
Daniel Willmanneea93372009-08-13 03:42:07 +0200513 DEBUGP(DMM, "-> MM INFO\n");
514
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800515 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000516}
517
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100518/*! Send an Authentication Request to MS on the given subscriber connection
519 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
520 * \param[in] conn Subscriber connection to send on.
521 * \param[in] rand Random challenge token to send, must be 16 bytes long.
522 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
523 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
524 * \param[in] key_seq auth tuple's sequence number.
525 */
526int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
527 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200528{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100529 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200530 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200531 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200532
Neels Hofmeyr7e2dec72017-02-14 17:42:25 +0100533 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100534 if (autn)
Neels Hofmeyr7e2dec72017-02-14 17:42:25 +0100535 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200536
Harald Welte7984d5c2009-08-12 22:56:50 +0200537 gh->proto_discr = GSM48_PDISC_MM;
538 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
539
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100540 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200541
Harald Welte7984d5c2009-08-12 22:56:50 +0200542 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100543 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200544 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200545 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200546
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100547
548 /* 16 bytes AUTN */
549 if (autn)
550 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
551
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800552 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200553}
554
555/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800556int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200557{
558 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800559 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200560}
561
Harald Weltea43e0b42016-06-19 18:06:02 +0200562static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
563static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
564
565static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100566{
Harald Weltea43e0b42016-06-19 18:06:02 +0200567 uint8_t mi_type;
568 char mi_string[GSM48_MI_SIZE];
569 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100570
Harald Weltea43e0b42016-06-19 18:06:02 +0200571 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
572 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100573
Harald Weltea43e0b42016-06-19 18:06:02 +0200574 switch (mi_type) {
575 case GSM_MI_TYPE_IMSI:
576 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
577 goto accept_reuse;
578 break;
579 case GSM_MI_TYPE_TMSI:
580 tmsi = osmo_load32be(mi_lv+2);
581 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
582 goto accept_reuse;
583 break;
584 case GSM_MI_TYPE_IMEI:
585 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
586 goto accept_reuse;
587 break;
588 default:
589 break;
590 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100591
Harald Weltea43e0b42016-06-19 18:06:02 +0200592 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
593 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
594 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
595 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100596
Harald Weltea43e0b42016-06-19 18:06:02 +0200597accept_reuse:
598 DEBUGP(DMM, "%s: re-using already accepted connection\n",
599 vlr_subscr_name(conn->vsub));
600 conn->received_cm_service_request = true;
601 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100602}
603
Harald Welte4ed0e922009-01-10 03:17:30 +0000604/*
605 * Handle CM Service Requests
606 * a) Verify that the packet is long enough to contain the information
607 * we require otherwsie reject with INCORRECT_MESSAGE
608 * b) Try to parse the TMSI. If we do not have one reject
609 * c) Check that we know the subscriber with the TMSI otherwise reject
610 * with a HLR cause
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200611 * d) Set the subscriber on the conn and accept
Harald Weltea43e0b42016-06-19 18:06:02 +0200612 *
613 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000614 */
Harald Weltea43e0b42016-06-19 18:06:02 +0200615int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000616{
Harald Weltea43e0b42016-06-19 18:06:02 +0200617 static const enum subscr_conn_from conn_from_cm_service_req =
618 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
619 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200620 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200621 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000622
Harald Welteba4cf162009-01-10 01:49:35 +0000623 struct gsm48_hdr *gh = msgb_l3(msg);
624 struct gsm48_service_request *req =
625 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800626 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200627 uint8_t classmark2_len = gh->data[1];
628 uint8_t *classmark2 = gh->data+2;
629 uint8_t mi_len = *(classmark2 + classmark2_len);
630 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Weltea43e0b42016-06-19 18:06:02 +0200631 struct osmo_location_area_id lai;
632 int rc;
633
634 lai.plmn.mcc = conn->network->country_code;
635 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200636 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000637
Harald Weltec9e02182009-05-01 19:07:53 +0000638 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000639 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000640 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200641 return msc_gsm48_tx_mm_serv_rej(conn,
642 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000643 }
644
645 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000646 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200647 return msc_gsm48_tx_mm_serv_rej(conn,
648 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000649 }
650
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200651 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000652 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200653
654 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200655 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
656 req->cm_service_type, gsm48_mi_type_name(mi_type),
657 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200658 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200659 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
660 req->cm_service_type, gsm48_mi_type_name(mi_type),
661 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200662 } else {
663 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +0200664 return msc_gsm48_tx_mm_serv_rej(conn,
665 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000666 }
667
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200668 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Weltea43e0b42016-06-19 18:06:02 +0200669 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
670 conn->classmark.classmark2_len = classmark2_len;
671
672 if (conn->conn_fsm) {
673 if (msc_subscr_conn_is_accepted(conn))
674 return cm_serv_reuse_conn(conn, mi-1);
675 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
676 vlr_subscr_name(conn->vsub));
677 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
678 return -EINVAL;
679 }
680
681 rc = msc_create_conn_fsm(conn, mi_string);
682 if (rc) {
683 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
684 /* logging already happened in msc_create_conn_fsm() */
685 return rc;
686 }
Harald Welte7659de12009-12-13 12:39:18 +0100687
Harald Weltea43e0b42016-06-19 18:06:02 +0200688 vlr_proc_acc_req(conn->conn_fsm,
689 SUBSCR_CONN_E_ACCEPTED,
690 SUBSCR_CONN_E_CN_CLOSE,
691 (void*)&conn_from_cm_service_req,
692 net->vlr, conn,
693 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
694 conn->network->authentication_required,
695 conn->network->a5_encryption,
696 classmark_is_r99(&conn->classmark),
697 conn->via_ran == RAN_UTRAN_IU);
Holger Freythereb443982009-06-04 13:58:42 +0000698
Harald Weltea43e0b42016-06-19 18:06:02 +0200699 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000700}
701
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200702static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000703{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200704 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000705 struct gsm48_hdr *gh = msgb_l3(msg);
706 struct gsm48_imsi_detach_ind *idi =
707 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200708 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200709 char mi_string[GSM48_MI_SIZE];
Harald Weltea43e0b42016-06-19 18:06:02 +0200710 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000711
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200712 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Weltea43e0b42016-06-19 18:06:02 +0200713 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
714 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000715
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200716 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100717
Harald Welte2a139372009-02-22 21:14:55 +0000718 switch (mi_type) {
719 case GSM_MI_TYPE_TMSI:
Harald Weltea43e0b42016-06-19 18:06:02 +0200720 vsub = vlr_subscr_find_by_tmsi(network->vlr,
721 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000722 break;
723 case GSM_MI_TYPE_IMSI:
Harald Weltea43e0b42016-06-19 18:06:02 +0200724 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000725 break;
726 case GSM_MI_TYPE_IMEI:
727 case GSM_MI_TYPE_IMEISV:
728 /* no sim card... FIXME: what to do ? */
Harald Weltea43e0b42016-06-19 18:06:02 +0200729 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
730 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000731 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200732 default:
Harald Weltea43e0b42016-06-19 18:06:02 +0200733 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
734 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000735 break;
736 }
737
Harald Weltea43e0b42016-06-19 18:06:02 +0200738 /* TODO? We used to remember the subscriber's classmark1 here and
739 * stored it in the old sqlite db, but now we store it in a conn that
740 * will be discarded anyway: */
741 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200742
Harald Weltea43e0b42016-06-19 18:06:02 +0200743 if (!vsub) {
744 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
745 gsm48_mi_type_name(mi_type), mi_string);
746 } else {
747 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
748 vlr_subscr_rx_imsi_detach(vsub);
749 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
750 vlr_subscr_put(vsub);
751 }
Harald Welte2a139372009-02-22 21:14:55 +0000752
Harald Weltea43e0b42016-06-19 18:06:02 +0200753 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000754 return 0;
755}
756
Harald Welted2a7f5a2009-06-05 20:08:20 +0000757static int gsm48_rx_mm_status(struct msgb *msg)
758{
759 struct gsm48_hdr *gh = msgb_l3(msg);
760
761 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
762
763 return 0;
764}
765
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100766static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
767 struct gsm_subscriber_connection *conn,
768 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100769{
770 struct gsm48_hdr *gh = msgb_l3(msg);
771 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100772
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100773 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
774 LOGP(DMM, LOGL_ERROR,
775 "%s: MM AUTHENTICATION RESPONSE:"
776 " l3 length invalid: %u\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200777 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100778 return -EINVAL;
779 }
780
781 *res_len = sizeof(ar->sres);
782 memcpy(res, ar->sres, sizeof(ar->sres));
783 return 0;
784}
785
786static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
787 struct gsm_subscriber_connection *conn,
788 struct msgb *msg)
789{
790 struct gsm48_hdr *gh;
791 uint8_t *data;
792 uint8_t iei;
793 uint8_t ie_len;
794 unsigned int data_len;
795
796 /* First parse the GSM part */
797 if (parse_gsm_auth_resp(res, res_len, conn, msg))
798 return -EINVAL;
799 OSMO_ASSERT(*res_len == 4);
800
801 /* Then add the extended res part */
802 gh = msgb_l3(msg);
803 data = gh->data + sizeof(struct gsm48_auth_resp);
804 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
805
806 if (data_len < 3) {
807 LOGP(DMM, LOGL_ERROR,
808 "%s: MM AUTHENTICATION RESPONSE:"
809 " l3 length invalid: %u\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200810 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100811 return -EINVAL;
812 }
813
814 iei = data[0];
815 ie_len = data[1];
816 if (iei != GSM48_IE_AUTH_RES_EXT) {
817 LOGP(DMM, LOGL_ERROR,
818 "%s: MM R99 AUTHENTICATION RESPONSE:"
819 " expected IEI 0x%02x, got 0x%02x\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200820 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100821 GSM48_IE_AUTH_RES_EXT, iei);
822 return -EINVAL;
823 }
824
825 if (ie_len > 12) {
826 LOGP(DMM, LOGL_ERROR,
827 "%s: MM R99 AUTHENTICATION RESPONSE:"
828 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200829 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100830 return -EINVAL;
831 }
832
833 *res_len += ie_len;
834 memcpy(res + 4, &data[2], ie_len);
835 return 0;
836}
837
838/* Chapter 9.2.3: Authentication Response */
839static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
840{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100841 uint8_t res[16];
842 uint8_t res_len;
843 int rc;
844 bool is_r99;
845
Harald Weltea43e0b42016-06-19 18:06:02 +0200846 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100847 LOGP(DMM, LOGL_ERROR,
848 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Weltea43e0b42016-06-19 18:06:02 +0200849 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100850 return -EINVAL;
851 }
852
853 if (msgb_l3len(msg) >
854 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
855 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
856 is_r99 = true;
857 } else {
858 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
859 is_r99 = false;
860 }
861
862 if (rc) {
Harald Weltea43e0b42016-06-19 18:06:02 +0200863 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100864 return -EINVAL;
865 }
866
867 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200868 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100869 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
870 osmo_hexdump_nospc(res, res_len));
871
Harald Weltea43e0b42016-06-19 18:06:02 +0200872 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
873 conn->via_ran == RAN_UTRAN_IU,
874 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100875}
876
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100877static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
878{
879 struct gsm48_hdr *gh = msgb_l3(msg);
880 uint8_t cause;
881 uint8_t auts_tag;
882 uint8_t auts_len;
883 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100884
Harald Weltea43e0b42016-06-19 18:06:02 +0200885 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100886 LOGP(DMM, LOGL_ERROR,
887 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Weltea43e0b42016-06-19 18:06:02 +0200888 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100889 return -EINVAL;
890 }
891
892 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
893 LOGP(DMM, LOGL_ERROR,
894 "%s: MM R99 AUTHENTICATION FAILURE:"
895 " l3 length invalid: %u\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200896 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
897 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100898 return -EINVAL;
899 }
900
901 cause = gh->data[0];
902
903 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
904 LOGP(DMM, LOGL_INFO,
905 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200906 vlr_subscr_name(conn->vsub), cause);
907 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
908 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100909 }
910
911 /* This is a Synch Failure procedure, which should pass an AUTS to
912 * resynchronize the sequence nr with the HLR. Expecting exactly one
913 * TLV with 14 bytes of AUTS. */
914
915 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
916 LOGP(DMM, LOGL_INFO,
917 "%s: MM R99 AUTHENTICATION FAILURE:"
918 " invalid Synch Failure: missing AUTS IE\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200919 vlr_subscr_name(conn->vsub));
920 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100921 return -EINVAL;
922 }
923
924 auts_tag = gh->data[1];
925 auts_len = gh->data[2];
926 auts = &gh->data[3];
927
928 if (auts_tag != GSM48_IE_AUTS
929 || auts_len != 14) {
930 LOGP(DMM, LOGL_INFO,
931 "%s: MM R99 AUTHENTICATION FAILURE:"
932 " invalid Synch Failure:"
933 " expected AUTS IE 0x%02x of 14 bytes,"
934 " got IE 0x%02x of %u bytes\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200935 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100936 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Weltea43e0b42016-06-19 18:06:02 +0200937 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100938 return -EINVAL;
939 }
940
941 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
942 LOGP(DMM, LOGL_INFO,
943 "%s: MM R99 AUTHENTICATION FAILURE:"
944 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200945 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
946 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100947 return -EINVAL;
948 }
949
950 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
951 * large enough. */
952
953 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Weltea43e0b42016-06-19 18:06:02 +0200954 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100955
Harald Weltea43e0b42016-06-19 18:06:02 +0200956 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
957}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100958
Harald Weltea43e0b42016-06-19 18:06:02 +0200959static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
960{
961 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
962 vlr_subscr_name(conn->vsub));
963 if (!conn->vsub) {
964 LOGP(DMM, LOGL_ERROR,
965 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
966 return -EINVAL;
967 }
968 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100969}
970
Harald Weltebf5e8df2009-02-03 12:59:45 +0000971/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800972static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000973{
974 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800975 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000976
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100977 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +0000978 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800979 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000980 break;
981 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800982 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000983 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000984 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800985 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000986 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000987 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000988 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000989 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000990 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Weltea43e0b42016-06-19 18:06:02 +0200991 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +0000992 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000993 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200994 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +0000995 break;
996 case GSM48_MT_MM_CM_REEST_REQ:
997 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
998 break;
999 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001000 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001001 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001002 case GSM48_MT_MM_AUTH_FAIL:
1003 rc = gsm48_rx_mm_auth_fail(conn, msg);
1004 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001005 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001006 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001007 gh->msg_type);
1008 break;
1009 }
1010
1011 return rc;
1012}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001013
Harald Weltea43e0b42016-06-19 18:06:02 +02001014static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1015{
1016 /* Check the size for the classmark */
1017 if (tot_len < 1 + *classmark2_lv)
1018 return NULL;
1019
1020 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1021 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1022 return NULL;
1023
1024 return mi_lv;
1025}
1026
Harald Welte2d35ae62009-02-06 12:02:13 +00001027/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001028static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001029{
Harald Weltea43e0b42016-06-19 18:06:02 +02001030 static const enum subscr_conn_from conn_from_paging_resp =
1031 SUBSCR_CONN_FROM_PAGING_RESP;
1032 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001033 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001034 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001035 uint8_t *classmark2_lv = gh->data + 1;
Harald Weltea43e0b42016-06-19 18:06:02 +02001036 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001037 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001038 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001039 int rc = 0;
Harald Weltea43e0b42016-06-19 18:06:02 +02001040 struct osmo_location_area_id lai;
1041
1042 lai.plmn.mcc = conn->network->country_code;
1043 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02001044 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001045
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001046 resp = (struct gsm48_pag_resp *) &gh->data[0];
1047 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1048 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001049 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1050 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001051
Harald Weltea43e0b42016-06-19 18:06:02 +02001052 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1053 if (!mi_lv) {
1054 /* FIXME */
1055 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001056 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001057
Harald Weltea43e0b42016-06-19 18:06:02 +02001058 rc = msc_create_conn_fsm(conn, mi_string);
1059 if (rc)
1060 /* logging already happened in msc_create_conn_fsm() */
1061 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001062
Harald Weltea43e0b42016-06-19 18:06:02 +02001063 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1064 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001065
Harald Weltea43e0b42016-06-19 18:06:02 +02001066 vlr_proc_acc_req(conn->conn_fsm,
1067 SUBSCR_CONN_E_ACCEPTED,
1068 SUBSCR_CONN_E_CN_CLOSE,
1069 (void*)&conn_from_paging_resp,
1070 net->vlr, conn,
1071 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
1072 conn->network->authentication_required,
1073 conn->network->a5_encryption,
1074 classmark_is_r99(&conn->classmark),
1075 conn->via_ran == RAN_UTRAN_IU);
Holger Freyther053e09d2009-02-14 22:51:06 +00001076
Harald Welte2d35ae62009-02-06 12:02:13 +00001077 return rc;
1078}
1079
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001080static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001081{
1082 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001083 uint8_t apdu_id_flags;
1084 uint8_t apdu_len;
1085 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001086
1087 apdu_id_flags = gh->data[0];
1088 apdu_len = gh->data[1];
1089 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001090
Max8dc8f232017-02-09 19:13:02 +01001091 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001092 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001093
Harald Weltea43e0b42016-06-19 18:06:02 +02001094 /* we're not using the app info blob anywhere, so ignore. */
1095#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001096 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Weltea43e0b42016-06-19 18:06:02 +02001097#else
1098 return 0;
1099#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001100}
1101
Harald Weltebf5e8df2009-02-03 12:59:45 +00001102/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001103static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001104{
1105 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001106 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001107
1108 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001109 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001110 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001111 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001112 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001113 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001114 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001115 default:
Philippd37981e2016-10-10 15:11:41 +02001116 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1117 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001118 break;
1119 }
1120
Harald Welte2d35ae62009-02-06 12:02:13 +00001121 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001122}
1123
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001124int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1125 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001126{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001127 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001128 struct gsm48_hdr *gh;
1129
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001130 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1131 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001132
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001133 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1134 gh->proto_discr = GSM48_PDISC_RR;
1135 gh->msg_type = GSM48_MT_RR_APP_INFO;
1136 gh->data[0] = apdu_id;
1137 gh->data[1] = apdu_len;
1138 memcpy(gh->data+2, apdu, apdu_len);
1139
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001140 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001141}
1142
Alexander Couzens92f552f2016-08-23 07:32:27 +02001143/* FIXME: this count_statistics is a state machine behaviour. we should convert
1144 * the complete call control into a state machine. Afterwards we can move this
1145 * code into state transitions.
1146 */
1147static void count_statistics(struct gsm_trans *trans, int new_state)
1148{
1149 int old_state = trans->cc.state;
1150 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1151
1152 if (old_state == new_state)
1153 return;
1154
1155 /* state incoming */
1156 switch (new_state) {
1157 case GSM_CSTATE_ACTIVE:
1158 osmo_counter_inc(trans->net->active_calls);
1159 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1160 break;
1161 }
1162
1163 /* state outgoing */
1164 switch (old_state) {
1165 case GSM_CSTATE_ACTIVE:
1166 osmo_counter_dec(trans->net->active_calls);
1167 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1168 new_state == GSM_CSTATE_DISCONNECT_IND)
1169 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1170 else
1171 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1172 break;
1173 }
1174}
1175
Harald Welte4bc90a12008-12-27 16:32:52 +00001176/* Call Control */
1177
Harald Welte7584aea2009-02-11 11:44:12 +00001178/* The entire call control code is written in accordance with Figure 7.10c
1179 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1180 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1181 * it for voice */
1182
Harald Welte4bfdfe72009-06-10 23:11:52 +08001183static void new_cc_state(struct gsm_trans *trans, int state)
1184{
1185 if (state > 31 || state < 0)
1186 return;
1187
1188 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001189 gsm48_cc_state_name(trans->cc.state),
1190 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001191
Alexander Couzens92f552f2016-08-23 07:32:27 +02001192 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001193 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001194}
1195
1196static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001197{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001198 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001199 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001200 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001201
Harald Welte4bc90a12008-12-27 16:32:52 +00001202 gh->msg_type = GSM48_MT_CC_STATUS;
1203
1204 cause = msgb_put(msg, 3);
1205 cause[0] = 2;
1206 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1207 cause[2] = 0x80 | 30; /* response to status inquiry */
1208
1209 call_state = msgb_put(msg, 1);
1210 call_state[0] = 0xc0 | 0x00;
1211
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001212 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001213}
1214
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001215static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001216 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001217{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001218 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1220
Harald Welte6f4b7532008-12-29 00:39:37 +00001221 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001222 gh->msg_type = msg_type;
1223
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001224 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001225}
1226
Harald Welte4bfdfe72009-06-10 23:11:52 +08001227static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1228{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001229 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001230 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001231 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001232 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001233 }
1234}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001235
Harald Welte4bfdfe72009-06-10 23:11:52 +08001236static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1237 int msg_type, struct gsm_mncc *mncc)
1238{
1239 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001240 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001241
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02001242#if BEFORE_MSCSPLIT
1243 /* Re-enable this log output once we can obtain this information via
1244 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001245 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001246 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001247 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001248 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001249 trans->conn->lchan->ts->trx->bts->nr,
1250 trans->conn->lchan->ts->trx->nr,
1251 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Weltea43e0b42016-06-19 18:06:02 +02001252 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001253 get_mncc_name(msg_type));
1254 else
1255 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1256 "Sending '%s' to MNCC.\n",
Harald Weltea43e0b42016-06-19 18:06:02 +02001257 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001258 get_mncc_name(msg_type));
1259 else
1260 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1261 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02001262#else
1263 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1264#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001265
1266 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001267
Harald Welte966636f2009-06-26 19:39:35 +02001268 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001269 if (!msg)
1270 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001271
1272 data = msgb_put(msg, sizeof(struct gsm_mncc));
1273 memcpy(data, mncc, sizeof(struct gsm_mncc));
1274
Harald Welte54209c22010-12-22 23:43:29 +01001275 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001276
1277 return 0;
1278}
1279
1280int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001281 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001282{
1283 struct gsm_mncc rel;
1284
Harald Welte92f70c52009-06-12 01:54:08 +08001285 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001287 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001288 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1289 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001290 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1291}
1292
Harald Weltedcaf5652009-07-23 18:56:43 +02001293/* Call Control Specific transaction release.
1294 * gets called by trans_free, DO NOT CALL YOURSELF! */
1295void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297 gsm48_stop_cc_timer(trans);
1298
1299 /* send release to L4, if callref still exists */
1300 if (trans->callref) {
1301 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001302 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001303 GSM48_CAUSE_LOC_PRN_S_LU,
1304 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001306 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001307 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001308}
1309
1310static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001311
Harald Welte09e38af2009-02-16 22:52:23 +00001312/* call-back from paging the B-end of the connection */
1313static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001314 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001315{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001316 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001317 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001318
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001319 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001320
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001321 switch (event) {
1322 case GSM_PAGING_SUCCEEDED:
Harald Weltea43e0b42016-06-19 18:06:02 +02001323 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1324 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001325 OSMO_ASSERT(conn);
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02001326 /* Assign conn */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001327 transt->conn = conn;
1328 /* send SETUP request to called party */
1329 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1330 break;
1331 case GSM_PAGING_EXPIRED:
1332 case GSM_PAGING_BUSY:
1333 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Weltea43e0b42016-06-19 18:06:02 +02001334 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001335 /* Temporarily out of order */
1336 mncc_release_ind(transt->net, transt,
1337 transt->callref,
1338 GSM48_CAUSE_LOC_PRN_S_LU,
1339 GSM48_CC_CAUSE_DEST_OOO);
1340 transt->callref = 0;
1341 transt->paging_request = NULL;
1342 trans_free(transt);
1343 break;
1344 default:
1345 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1346 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001348
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001349 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001350 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001351}
Harald Welte7584aea2009-02-11 11:44:12 +00001352
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001354static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001355{
Harald Welte53d51f52015-12-03 14:59:04 +01001356 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1357 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001358
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001360 return -EIO;
1361
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001362 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 return -EIO;
1364
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001365 /* Which subscriber do we want to track trans1 or trans2? */
Harald Weltea43e0b42016-06-19 18:06:02 +02001366 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001367
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02001368 /* future: msc_call_bridge(trans1, trans2); */
1369 return -1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370}
1371
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1373{
1374 DEBUGP(DCC, "-> STATUS ENQ\n");
1375 return gsm48_cc_tx_status(trans, msg);
1376}
1377
1378static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1379static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1380
1381static void gsm48_cc_timeout(void *arg)
1382{
1383 struct gsm_trans *trans = arg;
1384 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001385 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1386 int mo_location = GSM48_CAUSE_LOC_USER;
1387 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1388 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001389 struct gsm_mncc mo_rel, l4_rel;
1390
1391 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1392 mo_rel.callref = trans->callref;
1393 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1394 l4_rel.callref = trans->callref;
1395
Harald Weltedcaf5652009-07-23 18:56:43 +02001396 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001397 case 0x303:
1398 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001399 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001400 break;
1401 case 0x310:
1402 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001403 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 break;
1405 case 0x313:
1406 disconnect = 1;
1407 /* unknown, did not find it in the specs */
1408 break;
1409 case 0x301:
1410 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001411 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001412 break;
1413 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001414 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001415 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001416 gsm48_cc_tx_release(trans, &trans->cc.msg);
1417 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001418 break; /* stay in release state */
1419 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001420 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001421 return;
1422// release = 1;
1423// l4_cause = 14;
1424// break;
1425 case 0x306:
1426 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001427 mo_cause = trans->cc.msg.cause.value;
1428 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001429 break;
1430 case 0x323:
1431 disconnect = 1;
1432 break;
1433 default:
1434 release = 1;
1435 }
1436
1437 if (release && trans->callref) {
1438 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001439 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001440 l4_location, l4_cause);
1441 trans->callref = 0;
1442 }
1443
1444 if (disconnect && trans->callref) {
1445 /* process disconnect towards layer 4 */
1446 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001447 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001448 }
1449
1450 /* process disconnect towards mobile station */
1451 if (disconnect || release) {
1452 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001453 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1454 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1455 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001456 mo_rel.cause.diag_len = 3;
1457
1458 if (disconnect)
1459 gsm48_cc_tx_disconnect(trans, &mo_rel);
1460 if (release)
1461 gsm48_cc_tx_release(trans, &mo_rel);
1462 }
1463
1464}
1465
Max3ffce192016-04-25 15:22:00 +02001466/* disconnect both calls from the bridge */
1467static inline void disconnect_bridge(struct gsm_network *net,
1468 struct gsm_mncc_bridge *bridge, int err)
1469{
1470 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1471 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1472 struct gsm_mncc mx_rel;
1473 if (!trans0 || !trans1)
1474 return;
1475
1476 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1477 trans0->callref, trans1->callref, strerror(err));
1478
1479 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1480 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1481 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1482
1483 mx_rel.callref = trans0->callref;
1484 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1485
1486 mx_rel.callref = trans1->callref;
1487 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1488}
1489
Harald Welte4bfdfe72009-06-10 23:11:52 +08001490static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1491 int sec, int micro)
1492{
1493 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001494 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001495 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001496 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001497}
1498
1499static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1500{
1501 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001502 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001503 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1504 struct tlv_parsed tp;
1505 struct gsm_mncc setup;
1506
1507 memset(&setup, 0, sizeof(struct gsm_mncc));
1508 setup.callref = trans->callref;
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02001509
Harald Welte474d19f2010-03-02 23:18:30 +01001510 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001511 /* emergency setup is identified by msg_type */
1512 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1513 setup.emergency = 1;
1514
1515 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001516 setup.fields |= MNCC_F_CALLING;
Harald Weltea43e0b42016-06-19 18:06:02 +02001517 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1518 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001519
Harald Welte4bfdfe72009-06-10 23:11:52 +08001520 /* bearer capability */
1521 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1522 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001523 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001524 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1525 }
1526 /* facility */
1527 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1528 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001529 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001530 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1531 }
1532 /* called party bcd number */
1533 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1534 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001535 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001536 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1537 }
1538 /* user-user */
1539 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1540 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001541 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001542 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1543 }
1544 /* ss-version */
1545 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1546 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001547 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001548 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1549 }
1550 /* CLIR suppression */
1551 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1552 setup.clir.sup = 1;
1553 /* CLIR invocation */
1554 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1555 setup.clir.inv = 1;
1556 /* cc cap */
1557 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1558 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001559 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001560 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1561 }
1562
Harald Welte4bfdfe72009-06-10 23:11:52 +08001563 new_cc_state(trans, GSM_CSTATE_INITIATED);
1564
Harald Welte (local)47df3992009-12-26 19:45:03 +01001565 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
Harald Weltea43e0b42016-06-19 18:06:02 +02001566 vlr_subscr_name(trans->vsub), trans->vsub->msisdn,
Harald Welte (local)47df3992009-12-26 19:45:03 +01001567 setup.called.number);
1568
Alexander Couzensb847a212016-08-02 11:34:11 +02001569 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001570
Harald Welte4bfdfe72009-06-10 23:11:52 +08001571 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001572 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573
Harald Welte13cac662009-07-29 12:10:35 +02001574 /* MNCC code will modify the channel asynchronously, we should
1575 * ipaccess-bind only after the modification has been made to the
1576 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 return 0;
1578}
1579
1580static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001581{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001582 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001583 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001584 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001585 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001586
Harald Welte7ccf7782009-02-17 01:43:01 +00001587 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001588
Harald Welte4bfdfe72009-06-10 23:11:52 +08001589 /* transaction id must not be assigned */
1590 if (trans->transaction_id != 0xff) { /* unasssigned */
1591 DEBUGP(DCC, "TX Setup with assigned transaction. "
1592 "This is not allowed!\n");
1593 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001594 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001595 GSM48_CAUSE_LOC_PRN_S_LU,
1596 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001597 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001598 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001599 return rc;
1600 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001601
Harald Welte4bfdfe72009-06-10 23:11:52 +08001602 /* Get free transaction_id */
Harald Weltea43e0b42016-06-19 18:06:02 +02001603 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001604 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001605 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001606 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001607 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001608 GSM48_CAUSE_LOC_PRN_S_LU,
1609 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001610 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001611 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001612 return rc;
1613 }
Harald Welte78283ef2009-07-23 21:36:44 +02001614 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001615
Harald Welte65e74cc2008-12-29 01:55:35 +00001616 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001617
Harald Welte4bfdfe72009-06-10 23:11:52 +08001618 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001619
Harald Welte4bfdfe72009-06-10 23:11:52 +08001620 /* bearer capability */
1621 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001622 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001623 /* facility */
1624 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001625 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001626 /* progress */
1627 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001628 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001629 /* calling party BCD number */
1630 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001631 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001632 /* called party BCD number */
1633 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001634 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001635 /* user-user */
1636 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001637 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001638 /* redirecting party BCD number */
1639 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001640 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001641 /* signal */
1642 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001643 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001644
Harald Welte4bfdfe72009-06-10 23:11:52 +08001645 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001646
Alexander Couzensb847a212016-08-02 11:34:11 +02001647 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001648
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001649 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001650}
1651
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1653{
1654 struct gsm48_hdr *gh = msgb_l3(msg);
1655 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1656 struct tlv_parsed tp;
1657 struct gsm_mncc call_conf;
1658
1659 gsm48_stop_cc_timer(trans);
1660 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1661
1662 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1663 call_conf.callref = trans->callref;
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02001664
Harald Welte474d19f2010-03-02 23:18:30 +01001665 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001666#if 0
1667 /* repeat */
1668 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1669 call_conf.repeat = 1;
1670 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1671 call_conf.repeat = 2;
1672#endif
1673 /* bearer capability */
1674 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1675 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001676 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001677 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1678 }
1679 /* cause */
1680 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1681 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001682 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1684 }
1685 /* cc cap */
1686 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1687 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001688 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1690 }
1691
Andreas Eversberg035b8742013-09-19 09:27:06 +02001692 /* IMSI of called subscriber */
Harald Weltea43e0b42016-06-19 18:06:02 +02001693 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02001694
Harald Welte4bfdfe72009-06-10 23:11:52 +08001695 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1696
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001697 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001698 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699}
1700
1701static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1702{
1703 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001704 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1706
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1708
1709 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1710
1711 /* bearer capability */
1712 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001713 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714 /* facility */
1715 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001716 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717 /* progress */
1718 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001719 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001721 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722}
1723
1724static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1725{
1726 struct gsm48_hdr *gh = msgb_l3(msg);
1727 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1728 struct tlv_parsed tp;
1729 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001730
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731 gsm48_stop_cc_timer(trans);
1732 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1733
1734 memset(&alerting, 0, sizeof(struct gsm_mncc));
1735 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001736 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 /* facility */
1738 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1739 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001740 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1742 }
1743
1744 /* progress */
1745 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1746 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001747 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1749 }
1750 /* ss-version */
1751 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1752 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001753 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1755 }
1756
1757 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1758
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001759 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001760 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761}
1762
1763static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1764{
1765 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001766 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1768
Harald Welte4bfdfe72009-06-10 23:11:52 +08001769 gh->msg_type = GSM48_MT_CC_ALERTING;
1770
1771 /* facility */
1772 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001773 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 /* progress */
1775 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001776 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 /* user-user */
1778 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001779 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780
1781 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001782
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001783 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784}
1785
1786static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1787{
1788 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001789 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1791
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 gh->msg_type = GSM48_MT_CC_PROGRESS;
1793
1794 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001795 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 /* user-user */
1797 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001798 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001800 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801}
1802
1803static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1804{
1805 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001806 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1808
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 gh->msg_type = GSM48_MT_CC_CONNECT;
1810
1811 gsm48_stop_cc_timer(trans);
1812 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1813
1814 /* facility */
1815 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001816 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 /* progress */
1818 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001819 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820 /* connected number */
1821 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001822 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 /* user-user */
1824 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001825 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826
1827 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1828
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001829 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830}
1831
1832static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1833{
1834 struct gsm48_hdr *gh = msgb_l3(msg);
1835 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1836 struct tlv_parsed tp;
1837 struct gsm_mncc connect;
1838
1839 gsm48_stop_cc_timer(trans);
1840
1841 memset(&connect, 0, sizeof(struct gsm_mncc));
1842 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001843 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001845 connect.fields |= MNCC_F_CONNECTED;
Harald Weltea43e0b42016-06-19 18:06:02 +02001846 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
1847 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001848
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849 /* facility */
1850 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1851 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001852 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1854 }
1855 /* user-user */
1856 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1857 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001858 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1860 }
1861 /* ss-version */
1862 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1863 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001864 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1866 }
1867
1868 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02001869 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001870
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001871 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872}
1873
1874
1875static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1876{
1877 struct gsm_mncc connect_ack;
1878
1879 gsm48_stop_cc_timer(trans);
1880
1881 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02001882 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001883
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1885 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01001886
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001887 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 &connect_ack);
1889}
1890
1891static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1892{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001893 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1895
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1897
1898 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1899
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001900 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901}
1902
1903static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1904{
1905 struct gsm48_hdr *gh = msgb_l3(msg);
1906 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1907 struct tlv_parsed tp;
1908 struct gsm_mncc disc;
1909
1910 gsm48_stop_cc_timer(trans);
1911
1912 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1913
1914 memset(&disc, 0, sizeof(struct gsm_mncc));
1915 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001916 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917 /* cause */
1918 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1919 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001920 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1922 }
1923 /* facility */
1924 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1925 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001926 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1928 }
1929 /* user-user */
1930 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1931 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001932 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1934 }
1935 /* ss-version */
1936 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1937 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001938 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1940 }
1941
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001942 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943
1944}
1945
Harald Weltec66b71c2009-06-11 14:23:20 +08001946static struct gsm_mncc_cause default_cause = {
1947 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1948 .coding = 0,
1949 .rec = 0,
1950 .rec_val = 0,
1951 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1952 .diag_len = 0,
1953 .diag = { 0 },
1954};
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955
1956static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1957{
1958 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001959 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1961
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1963
1964 gsm48_stop_cc_timer(trans);
1965 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1966
1967 /* cause */
1968 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01001969 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 else
Harald Welte55c8f352010-03-07 23:40:35 +01001971 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972
1973 /* facility */
1974 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001975 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976 /* progress */
1977 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001978 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979 /* user-user */
1980 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001981 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982
1983 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02001984 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985
1986 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1987
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001988 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989}
1990
1991static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1992{
1993 struct gsm48_hdr *gh = msgb_l3(msg);
1994 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1995 struct tlv_parsed tp;
1996 struct gsm_mncc rel;
1997 int rc;
1998
1999 gsm48_stop_cc_timer(trans);
2000
2001 memset(&rel, 0, sizeof(struct gsm_mncc));
2002 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002003 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 /* cause */
2005 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2006 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002007 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2009 }
2010 /* facility */
2011 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2012 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002013 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2015 }
2016 /* user-user */
2017 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2018 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002019 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2021 }
2022 /* ss-version */
2023 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2024 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002025 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2027 }
2028
Harald Weltedcaf5652009-07-23 18:56:43 +02002029 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002031 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002033 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002034 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002035 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002036 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 }
2038
2039 new_cc_state(trans, GSM_CSTATE_NULL);
2040
2041 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002042 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043
2044 return rc;
2045}
2046
2047static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2048{
2049 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002050 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002051 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2052
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 gh->msg_type = GSM48_MT_CC_RELEASE;
2054
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 gsm48_stop_cc_timer(trans);
2056 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2057
2058 /* cause */
2059 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002060 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061 /* facility */
2062 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002063 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 /* user-user */
2065 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002066 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067
Harald Weltedcaf5652009-07-23 18:56:43 +02002068 trans->cc.T308_second = 0;
2069 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070
Harald Weltedcaf5652009-07-23 18:56:43 +02002071 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2073
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002074 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075}
2076
2077static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2078{
2079 struct gsm48_hdr *gh = msgb_l3(msg);
2080 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2081 struct tlv_parsed tp;
2082 struct gsm_mncc rel;
2083 int rc = 0;
2084
2085 gsm48_stop_cc_timer(trans);
2086
2087 memset(&rel, 0, sizeof(struct gsm_mncc));
2088 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002089 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090 /* cause */
2091 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2092 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002093 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2095 }
2096 /* facility */
2097 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2098 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002099 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2101 }
2102 /* user-user */
2103 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2104 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002105 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2107 }
2108 /* ss-version */
2109 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2110 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2113 }
2114
2115 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002116 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002118 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119 MNCC_REJ_IND, &rel);
2120 break;
2121 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002122 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 MNCC_REL_CNF, &rel);
2124 break;
2125 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002126 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 MNCC_REL_IND, &rel);
2128 }
2129 }
2130
2131 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002132 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133
2134 return rc;
2135}
2136
2137static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2138{
2139 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002140 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002142 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002143
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2145
2146 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002147
Harald Welte4bfdfe72009-06-10 23:11:52 +08002148 gsm48_stop_cc_timer(trans);
2149
2150 /* cause */
2151 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002152 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 /* facility */
2154 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002155 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156 /* user-user */
2157 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002158 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159
Harald Weltec7782de2010-12-21 19:31:41 +01002160 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2161
Harald Weltedcaf5652009-07-23 18:56:43 +02002162 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163
Harald Weltec7782de2010-12-21 19:31:41 +01002164 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165}
2166
2167static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2168{
2169 struct gsm48_hdr *gh = msgb_l3(msg);
2170 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2171 struct tlv_parsed tp;
2172 struct gsm_mncc fac;
2173
2174 memset(&fac, 0, sizeof(struct gsm_mncc));
2175 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002176 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 /* facility */
2178 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2179 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002180 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2182 }
2183 /* ss-version */
2184 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2185 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2188 }
2189
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002190 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191}
2192
2193static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2194{
2195 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002196 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2198
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 gh->msg_type = GSM48_MT_CC_FACILITY;
2200
2201 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002202 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002204 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205}
2206
2207static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2208{
2209 struct gsm_mncc hold;
2210
2211 memset(&hold, 0, sizeof(struct gsm_mncc));
2212 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002213 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214}
2215
2216static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2217{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002218 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2220
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2222
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002223 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224}
2225
2226static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2227{
2228 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002229 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2231
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2233
2234 /* cause */
2235 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 else
Harald Welte55c8f352010-03-07 23:40:35 +01002238 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002240 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241}
2242
2243static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2244{
2245 struct gsm_mncc retrieve;
2246
2247 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2248 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002249 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002250 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002251}
2252
2253static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2254{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002255 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2257
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2259
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002260 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261}
2262
2263static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2264{
2265 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002266 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2268
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2270
2271 /* cause */
2272 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002273 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 else
Harald Welte55c8f352010-03-07 23:40:35 +01002275 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002277 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278}
2279
2280static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2281{
2282 struct gsm48_hdr *gh = msgb_l3(msg);
2283 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2284 struct tlv_parsed tp;
2285 struct gsm_mncc dtmf;
2286
2287 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2288 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002289 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 /* keypad facility */
2291 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2292 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002293 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2295 }
2296
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002297 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298}
2299
2300static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2301{
2302 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002303 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2305
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2307
2308 /* keypad */
2309 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002310 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002312 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313}
2314
2315static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2316{
2317 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002318 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2320
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2322
2323 /* cause */
2324 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002325 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 else
Harald Welte55c8f352010-03-07 23:40:35 +01002327 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002329 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330}
2331
2332static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2333{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002334 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002335 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2336
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2338
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002339 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340}
2341
2342static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2343{
2344 struct gsm_mncc dtmf;
2345
2346 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2347 dtmf.callref = trans->callref;
2348
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002349 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350}
2351
2352static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2353{
2354 struct gsm48_hdr *gh = msgb_l3(msg);
2355 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2356 struct tlv_parsed tp;
2357 struct gsm_mncc modify;
2358
2359 memset(&modify, 0, sizeof(struct gsm_mncc));
2360 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002361 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362 /* bearer capability */
2363 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2364 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002365 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2367 }
2368
2369 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2370
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002371 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372}
2373
2374static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2375{
2376 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002377 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2379
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 gh->msg_type = GSM48_MT_CC_MODIFY;
2381
2382 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2383
2384 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002385 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002386
2387 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2388
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002389 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390}
2391
2392static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2393{
2394 struct gsm48_hdr *gh = msgb_l3(msg);
2395 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2396 struct tlv_parsed tp;
2397 struct gsm_mncc modify;
2398
2399 gsm48_stop_cc_timer(trans);
2400
2401 memset(&modify, 0, sizeof(struct gsm_mncc));
2402 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002403 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 /* bearer capability */
2405 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2406 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002407 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2409 }
2410
2411 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2412
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002413 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414}
2415
2416static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2417{
2418 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002419 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2421
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2423
2424 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002425 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426
2427 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2428
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002429 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430}
2431
2432static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2433{
2434 struct gsm48_hdr *gh = msgb_l3(msg);
2435 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2436 struct tlv_parsed tp;
2437 struct gsm_mncc modify;
2438
2439 gsm48_stop_cc_timer(trans);
2440
2441 memset(&modify, 0, sizeof(struct gsm_mncc));
2442 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002443 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444 /* bearer capability */
2445 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2446 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002447 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002448 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2449 }
2450 /* cause */
2451 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2452 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002453 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2455 }
2456
2457 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2458
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002459 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460}
2461
2462static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2463{
2464 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002465 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2467
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2469
2470 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002471 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002473 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474
2475 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2476
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002477 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478}
2479
2480static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2481{
2482 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002483 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002484 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2485
Harald Welte4bfdfe72009-06-10 23:11:52 +08002486 gh->msg_type = GSM48_MT_CC_NOTIFY;
2487
2488 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002489 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002491 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492}
2493
2494static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2495{
2496 struct gsm48_hdr *gh = msgb_l3(msg);
2497 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2498// struct tlv_parsed tp;
2499 struct gsm_mncc notify;
2500
2501 memset(&notify, 0, sizeof(struct gsm_mncc));
2502 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002503// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002505 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002507 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508}
2509
2510static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2511{
2512 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002513 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2515
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516 gh->msg_type = GSM48_MT_CC_USER_INFO;
2517
2518 /* user-user */
2519 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002520 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521 /* more data */
2522 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002523 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002524
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002525 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002526}
2527
2528static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2529{
2530 struct gsm48_hdr *gh = msgb_l3(msg);
2531 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2532 struct tlv_parsed tp;
2533 struct gsm_mncc user;
2534
2535 memset(&user, 0, sizeof(struct gsm_mncc));
2536 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002537 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002538 /* user-user */
2539 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2540 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002541 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2543 }
2544 /* more data */
2545 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2546 user.more = 1;
2547
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002548 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549}
2550
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002552 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002553 int type;
2554 int (*rout) (struct gsm_trans *trans, void *arg);
2555} downstatelist[] = {
2556 /* mobile originating call establishment */
2557 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2558 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2559 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2560 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2561 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) | SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.2 | 5.2.1.6 | 5.2.1.6 */
2562 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2563 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2564 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2565 /* mobile terminating call establishment */
2566 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2567 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2568 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2569 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2570 /* signalling during call */
2571 {SBIT(GSM_CSTATE_ACTIVE),
2572 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2573 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2574 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2575 {ALL_STATES,
2576 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2577 {ALL_STATES,
2578 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2579 {ALL_STATES,
2580 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2581 {SBIT(GSM_CSTATE_ACTIVE),
2582 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2583 {SBIT(GSM_CSTATE_ACTIVE),
2584 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2585 {SBIT(GSM_CSTATE_ACTIVE),
2586 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2587 {SBIT(GSM_CSTATE_ACTIVE),
2588 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2589 {SBIT(GSM_CSTATE_ACTIVE),
2590 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2591 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2592 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2593 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2594 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2595 {SBIT(GSM_CSTATE_ACTIVE),
2596 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2597 /* clearing */
2598 {SBIT(GSM_CSTATE_INITIATED),
2599 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2600 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2601 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2602 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2603 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604};
2605
2606#define DOWNSLLEN \
2607 (sizeof(downstatelist) / sizeof(struct downstate))
2608
2609
Harald Welte76556372010-12-22 23:57:45 +01002610int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611{
Harald Welte1a6f7982009-08-09 18:52:33 +02002612 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002614 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 struct gsm_mncc *data = arg, rel;
2616
Harald Welte04dc88f2010-12-22 21:45:05 +01002617 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2618
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 /* handle special messages */
2620 switch(msg_type) {
2621 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002622 rc = tch_bridge(net, arg);
2623 if (rc < 0)
2624 disconnect_bridge(net, arg, -rc);
2625 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627 case MNCC_FRAME_RECV:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002628 case MNCC_RTP_CREATE:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002629 case MNCC_RTP_CONNECT:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002630 case MNCC_RTP_FREE:
Harald Welteda7ab742009-12-19 22:23:05 +01002631 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002632 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002633 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002634 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002635 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2636 get_mncc_name(msg_type));
2637 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 }
2639
2640 memset(&rel, 0, sizeof(struct gsm_mncc));
2641 rel.callref = data->callref;
2642
2643 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002644 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645
2646 /* Callref unknown */
2647 if (!trans) {
Harald Weltea43e0b42016-06-19 18:06:02 +02002648 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002649
Harald Welte4a3464c2009-07-04 10:11:24 +02002650 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2652 "Received '%s' from MNCC with "
2653 "unknown callref %d\n", data->called.number,
2654 get_mncc_name(msg_type), data->callref);
2655 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002656 return mncc_release_ind(net, NULL, data->callref,
2657 GSM48_CAUSE_LOC_PRN_S_LU,
2658 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002660 if (!data->called.number[0] && !data->imsi[0]) {
2661 DEBUGP(DCC, "(bts - trx - ts - ti) "
2662 "Received '%s' from MNCC with "
2663 "no number or IMSI\n", get_mncc_name(msg_type));
2664 /* Invalid number */
2665 return mncc_release_ind(net, NULL, data->callref,
2666 GSM48_CAUSE_LOC_PRN_S_LU,
2667 GSM48_CC_CAUSE_INV_NR_FORMAT);
2668 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002670 if (data->called.number[0])
Harald Weltea43e0b42016-06-19 18:06:02 +02002671 vsub = vlr_subscr_find_by_msisdn(net->vlr,
2672 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002673 else
Harald Weltea43e0b42016-06-19 18:06:02 +02002674 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002675
2676 /* update the subscriber we deal with */
Harald Weltea43e0b42016-06-19 18:06:02 +02002677 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002678
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679 /* If subscriber is not found */
Harald Weltea43e0b42016-06-19 18:06:02 +02002680 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2682 "Received '%s' from MNCC with "
2683 "unknown subscriber %s\n", data->called.number,
2684 get_mncc_name(msg_type), data->called.number);
2685 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002686 return mncc_release_ind(net, NULL, data->callref,
2687 GSM48_CAUSE_LOC_PRN_S_LU,
2688 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 }
2690 /* If subscriber is not "attached" */
Harald Weltea43e0b42016-06-19 18:06:02 +02002691 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2693 "Received '%s' from MNCC with "
2694 "detached subscriber %s\n", data->called.number,
2695 get_mncc_name(msg_type), data->called.number);
Harald Weltea43e0b42016-06-19 18:06:02 +02002696 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002698 return mncc_release_ind(net, NULL, data->callref,
2699 GSM48_CAUSE_LOC_PRN_S_LU,
2700 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701 }
2702 /* Create transaction */
Harald Weltea43e0b42016-06-19 18:06:02 +02002703 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02002704 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705 DEBUGP(DCC, "No memory for trans.\n");
Harald Weltea43e0b42016-06-19 18:06:02 +02002706 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002708 mncc_release_ind(net, NULL, data->callref,
2709 GSM48_CAUSE_LOC_PRN_S_LU,
2710 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711 return -ENOMEM;
2712 }
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002713
2714 /* Find conn */
Harald Weltea43e0b42016-06-19 18:06:02 +02002715 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002716
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002717 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002718 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 /* find transaction with this subscriber already paging */
2720 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002721 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722 if (transt == trans ||
Harald Weltea43e0b42016-06-19 18:06:02 +02002723 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002725 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726 "Received '%s' from MNCC with "
2727 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002728 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729 data->called.number,
Harald Weltea43e0b42016-06-19 18:06:02 +02002730 get_mncc_name(msg_type), vsub->lac);
2731 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002732 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002733 return 0;
2734 }
2735 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002736 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002737
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02002738 /* Request a channel */
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002739 trans->paging_request = subscr_request_conn(
Harald Weltea43e0b42016-06-19 18:06:02 +02002740 vsub,
2741 RSL_CHANNEED_TCH_F,
2742 setup_trig_pag_evt,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02002743 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002744 if (!trans->paging_request) {
2745 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Weltea43e0b42016-06-19 18:06:02 +02002746 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002747 trans_free(trans);
2748 return 0;
2749 }
Harald Weltea43e0b42016-06-19 18:06:02 +02002750 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751 return 0;
2752 }
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002753
2754 /* Assign conn */
Harald Weltea43e0b42016-06-19 18:06:02 +02002755 trans->conn = msc_subscr_conn_get(conn);
2756 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002757 } else {
2758 /* update the subscriber we deal with */
Harald Weltea43e0b42016-06-19 18:06:02 +02002759 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002760 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002761
2762 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002763 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764
2765 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002766 if (!conn) {
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002767 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002768 "Received '%s' from MNCC in paging state\n",
Harald Weltea43e0b42016-06-19 18:06:02 +02002769 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002771 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2772 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773 if (msg_type == MNCC_REL_REQ)
2774 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2775 else
2776 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2777 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002778 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779 return rc;
2780 }
2781
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002782 DEBUGP(DCC, "(ti %02x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002783 "Received '%s' from MNCC in state %d (%s)\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784 trans->transaction_id,
Harald Weltea43e0b42016-06-19 18:06:02 +02002785 vlr_subscr_msisdn_or_name(trans->conn->vsub),
Harald Weltedcaf5652009-07-23 18:56:43 +02002786 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002787 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788
2789 /* Find function for current state and message */
2790 for (i = 0; i < DOWNSLLEN; i++)
2791 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002792 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793 break;
2794 if (i == DOWNSLLEN) {
2795 DEBUGP(DCC, "Message unhandled at this state.\n");
2796 return 0;
2797 }
2798
2799 rc = downstatelist[i].rout(trans, arg);
2800
2801 return rc;
2802}
2803
2804
2805static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002806 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807 int type;
2808 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2809} datastatelist[] = {
2810 /* mobile originating call establishment */
2811 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2812 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2813 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2814 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2815 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2816 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2817 /* mobile terminating call establishment */
2818 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2819 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2820 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2821 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002822 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2824 /* signalling during call */
2825 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2826 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2827 {SBIT(GSM_CSTATE_ACTIVE),
2828 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2829 {ALL_STATES,
2830 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2831 {ALL_STATES,
2832 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2833 {ALL_STATES,
2834 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2835 {SBIT(GSM_CSTATE_ACTIVE),
2836 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2837 {SBIT(GSM_CSTATE_ACTIVE),
2838 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2839 {SBIT(GSM_CSTATE_ACTIVE),
2840 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2841 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2842 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2843 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2844 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2845 {SBIT(GSM_CSTATE_ACTIVE),
2846 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2847 /* clearing */
2848 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2849 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2850 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2851 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2852 {ALL_STATES, /* 5.4.3.4 */
2853 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2854};
2855
2856#define DATASLLEN \
2857 (sizeof(datastatelist) / sizeof(struct datastate))
2858
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08002859static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00002860{
2861 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01002862 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01002863 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02002864 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002866
Harald Welte4bfdfe72009-06-10 23:11:52 +08002867 if (msg_type & 0x80) {
2868 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2869 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002870 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002871
Harald Weltea43e0b42016-06-19 18:06:02 +02002872 if (!conn->vsub) {
2873 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01002874 return -EINVAL;
2875 }
2876
Harald Welte4bfdfe72009-06-10 23:11:52 +08002877 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002878 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002879
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002880#if BEFORE_MSCSPLIT
2881 /* Re-enable this log output once we can obtain this information via
2882 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02002883 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08002885 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Weltea43e0b42016-06-19 18:06:02 +02002886 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf12010-03-25 12:13:02 +08002887 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2888 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02002889#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08002890
2891 /* Create transaction */
2892 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002893 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894 "creating new trans.\n", transaction_id);
2895 /* Create transaction */
Harald Weltea43e0b42016-06-19 18:06:02 +02002896 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01002897 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02002898 transaction_id, new_callref++);
2899 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002901 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002902 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903 GSM48_MT_CC_RELEASE_COMPL);
2904 return -ENOMEM;
2905 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002906 /* Assign transaction */
Harald Weltea43e0b42016-06-19 18:06:02 +02002907 trans->conn = msc_subscr_conn_get(conn);
2908 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909 }
2910
2911 /* find function for current state and message */
2912 for (i = 0; i < DATASLLEN; i++)
2913 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002914 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002915 break;
2916 if (i == DATASLLEN) {
2917 DEBUGP(DCC, "Message unhandled at this state.\n");
2918 return 0;
2919 }
2920
Harald Weltea43e0b42016-06-19 18:06:02 +02002921 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002922
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002924
Harald Weltea43e0b42016-06-19 18:06:02 +02002925 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00002926 return rc;
2927}
2928
Harald Weltea43e0b42016-06-19 18:06:02 +02002929static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08002930{
Harald Weltea43e0b42016-06-19 18:06:02 +02002931 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
2932 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08002933
Harald Weltea43e0b42016-06-19 18:06:02 +02002934 switch (pdisc) {
2935 case GSM48_PDISC_MM:
2936 switch (msg_type) {
2937 case GSM48_MT_MM_LOC_UPD_REQUEST:
2938 case GSM48_MT_MM_CM_SERV_REQ:
2939 case GSM48_MT_MM_AUTH_RESP:
2940 case GSM48_MT_MM_AUTH_FAIL:
2941 case GSM48_MT_MM_ID_RESP:
2942 case GSM48_MT_MM_TMSI_REALL_COMPL:
2943 case GSM48_MT_MM_IMSI_DETACH_IND:
2944 return true;
2945 default:
2946 break;
2947 }
2948 break;
2949 case GSM48_PDISC_RR:
2950 switch (msg_type) {
2951 case GSM48_MT_RR_CIPH_M_COMPL:
2952 case GSM48_MT_RR_PAG_RESP:
2953 return true;
2954 default:
2955 break;
2956 }
2957 break;
2958 default:
2959 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02002960 }
2961
Harald Weltea43e0b42016-06-19 18:06:02 +02002962 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02002963}
2964
Harald Weltea43e0b42016-06-19 18:06:02 +02002965void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
2966 struct msgb *msg)
2967{
2968
2969 /* If a CM Service Request was received before, this is the request the
2970 * conn was opened for. No need to wait for further messages. */
2971
2972 if (!conn->received_cm_service_request)
2973 return;
2974
2975 if (log_check_level(DMM, LOGL_DEBUG)) {
2976 struct gsm48_hdr *gh = msgb_l3(msg);
2977 uint8_t pdisc = gsm48_hdr_pdisc(gh);
2978 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2979
2980 DEBUGP(DMM, "%s pdisc=%d msg_type=0x%02x:"
2981 " received_cm_service_request changes to false\n",
2982 vlr_subscr_name(conn->vsub),
2983 pdisc, msg_type);
2984 }
2985 conn->received_cm_service_request = false;
2986}
2987
2988
Neels Hofmeyr378a4922016-05-09 21:07:43 +02002989/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08002990int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00002991{
2992 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01002993 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00002994 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01002995
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02002996 OSMO_ASSERT(conn);
2997 OSMO_ASSERT(msg);
2998
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01002999 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Harald Weltea43e0b42016-06-19 18:06:02 +02003000
3001 if (!msc_subscr_conn_is_accepted(conn)
3002 && !msg_is_initially_permitted(gh)) {
3003 LOGP(DRLL, LOGL_ERROR,
3004 "subscr %s: Message not permitted for initial conn:"
3005 " pdisc=0x%02x msg_type=0x%02x\n",
3006 vlr_subscr_name(conn->vsub), gh->proto_discr, gh->msg_type);
3007 return -EACCES;
3008 }
3009
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003010#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003011 if (silent_call_reroute(conn, msg))
3012 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003013#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003014
Harald Welte52b1f982008-12-23 20:25:15 +00003015 switch (pdisc) {
3016 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003017 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003018 break;
3019 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003020 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003021 break;
3022 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003023 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003024 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003025 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003026 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003027 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003028 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003029 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003030 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3031 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003032 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003033 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003034 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003035 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003036 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003037 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003038 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3039 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003040 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003041 break;
3042 }
3043
3044 return rc;
3045}
Harald Welte8470bf22008-12-25 23:28:35 +00003046
Harald Weltea43e0b42016-06-19 18:06:02 +02003047/***********************************************************************
3048 * VLR integration
3049 ***********************************************************************/
3050
3051/* VLR asks us to send an authentication request */
3052static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3053 bool send_autn)
3054{
3055 struct gsm_subscriber_connection *conn = msc_conn_ref;
3056 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3057 send_autn? at->vec.autn : NULL,
3058 at->key_seq);
3059}
3060
3061/* VLR asks us to send an authentication reject */
3062static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3063{
3064 struct gsm_subscriber_connection *conn = msc_conn_ref;
3065 return gsm48_tx_mm_auth_rej(conn);
3066}
3067
3068/* VLR asks us to transmit an Identity Request of given type */
3069static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3070{
3071 struct gsm_subscriber_connection *conn = msc_conn_ref;
3072 return mm_tx_identity_req(conn, mi_type);
3073}
3074
3075/* VLR asks us to transmit a Location Update Accept */
3076static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3077{
3078 struct gsm_subscriber_connection *conn = msc_conn_ref;
3079 return gsm0408_loc_upd_acc(conn, send_tmsi);
3080}
3081
3082/* VLR asks us to transmit a Location Update Reject */
3083static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3084{
3085 struct gsm_subscriber_connection *conn = msc_conn_ref;
3086 return gsm0408_loc_upd_rej(conn, cause);
3087}
3088
3089/* VLR asks us to transmit a CM Service Accept */
3090static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3091{
3092 struct gsm_subscriber_connection *conn = msc_conn_ref;
3093 return gsm48_tx_mm_serv_ack(conn);
3094}
3095
3096/* VLR asks us to transmit a CM Service Reject */
3097static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3098{
3099 uint8_t cause;
3100 struct gsm_subscriber_connection *conn = msc_conn_ref;
3101 conn->received_cm_service_request = false;
3102
3103 switch (result) {
3104 default:
3105 case VLR_PR_ARQ_RES_NONE:
3106 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3107 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3108 cause = GSM48_REJECT_NETWORK_FAILURE;
3109 break;
3110 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3111 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3112 break;
3113 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3114 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3115 break;
3116 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3117 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3118 break;
3119 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3120 cause = GSM48_REJECT_ILLEGAL_MS;
3121 break;
3122 case VLR_PR_ARQ_RES_TIMEOUT:
3123 cause = GSM48_REJECT_CONGESTION;
3124 break;
3125 };
3126
3127 return gsm48_tx_mm_serv_rej(conn, cause);
3128}
3129
3130/* VLR asks us to start using ciphering */
3131static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3132 enum vlr_ciph ciph,
3133 bool retrieve_imeisv)
3134{
3135 struct gsm_subscriber_connection *conn = msc_conn_ref;
3136 struct vlr_subscr *vsub;
3137 struct gsm_auth_tuple *tuple;
3138
3139 if (!conn || !conn->vsub) {
3140 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3141 " NULL conn/subscriber");
3142 return -EINVAL;
3143 }
3144
3145 vsub = conn->vsub;
3146 tuple = vsub->last_tuple;
3147
3148 if (!tuple) {
3149 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3150 " Command: no auth tuple available\n",
3151 vlr_subscr_name(vsub));
3152 return -EINVAL;
3153 }
3154
3155 /* TODO: MSCSPLIT: don't directly push BSC buttons */
3156 return gsm0808_cipher_mode(conn, ciph, tuple->vec.kc, 8,
3157 retrieve_imeisv);
3158}
3159
3160/* VLR informs us that the subscriber data has somehow been modified */
3161static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3162{
3163 /* FIXME */
3164}
3165
3166/* VLR informs us that the subscriber has been associated with a conn */
3167static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3168 struct vlr_subscr *vsub)
3169{
3170 struct gsm_subscriber_connection *conn = msc_conn_ref;
3171 OSMO_ASSERT(!conn->vsub);
3172 conn->vsub = vlr_subscr_get(vsub);
3173}
3174
3175/* operations that we need to implement for libvlr */
3176static const struct vlr_ops msc_vlr_ops = {
3177 .tx_auth_req = msc_vlr_tx_auth_req,
3178 .tx_auth_rej = msc_vlr_tx_auth_rej,
3179 .tx_id_req = msc_vlr_tx_id_req,
3180 .tx_lu_acc = msc_vlr_tx_lu_acc,
3181 .tx_lu_rej = msc_vlr_tx_lu_rej,
3182 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3183 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3184 .set_ciph_mode = msc_vlr_set_ciph_mode,
3185 .subscr_update = msc_vlr_subscr_update,
3186 .subscr_assoc = msc_vlr_subscr_assoc,
3187};
3188
3189/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3190int msc_vlr_alloc(struct gsm_network *net)
3191{
3192 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3193 if (!net->vlr)
3194 return -ENOMEM;
3195 net->vlr->user_ctx = net;
3196 return 0;
3197}
3198
3199/* Launch the VLR, i.e. its GSUP connection */
3200int msc_vlr_start(struct gsm_network *net)
3201{
3202 OSMO_ASSERT(net->vlr);
3203 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3204 net->gsup_server_port);
3205}
3206
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02003207/* This is a temporary shim merely to ensure that the unit tests still work. It
3208 * shall be removed as soon as Iu and A interface paging is implemented. */
3209int msc_fake_paging_request(struct vlr_subscr *vsub)
Harald Welte805f6442009-07-28 18:25:29 +02003210{
Neels Hofmeyrce4d88b2017-05-08 15:12:20 +02003211 LOGP(DMM, LOGL_ERROR, "Paging currently not implemented in the MSC.\n");
3212 OSMO_ASSERT(false);
3213}
3214
3215/* This is a temporary shim merely to ensure that the unit tests still work. It
3216 * shall be removed as soon as Iu and A interface paging is implemented. */
3217void msc_fake_paging_request_stop(struct vlr_subscr *vsub)
3218{
3219 LOGP(DMM, LOGL_ERROR, "Paging currently not implemented in the MSC.\n");
3220 OSMO_ASSERT(false);
Harald Welte805f6442009-07-28 18:25:29 +02003221}