blob: 28cba5b85912474c6d7a983c10ba7e4d1dd3e996 [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 Welte2483f1b2016-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>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020034#include <openssl/rand.h>
Harald Welte52b1f982008-12-23 20:25:15 +000035
Harald Welte9c3dc902012-04-08 16:59:24 +020036#include "bscconfig.h"
37
Sylvain Munaut30a15382009-12-24 00:27:26 +010038#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000039#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/debug.h>
41#include <openbsc/gsm_data.h>
42#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000043#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000044#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080045#include <openbsc/gsm_04_80.h>
Harald Welteeac38c32017-05-29 18:02:53 +020046#include <openbsc/gsm_04_14.h>
Harald Welte8470bf22008-12-25 23:28:35 +000047#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000048#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000049#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000050#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000052#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020053#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020054#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020055#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010056#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080057#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080058#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010059#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020060#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020061#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010062#include <osmocom/core/bitvec.h>
Harald Welte2483f1b2016-06-19 18:06:02 +020063#include <openbsc/vlr.h>
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020064#include <openbsc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080065
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010066#include <osmocom/gsm/gsm48.h>
67#include <osmocom/gsm/gsm0480.h>
68#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020069#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010070#include <osmocom/core/msgb.h>
71#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010072#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010073#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020074#include <osmocom/crypt/auth.h>
75
76#include <openbsc/msc_ifaces.h>
77
78#ifdef BUILD_IU
79#include <openbsc/iu.h>
80#endif
Harald Welte52b1f982008-12-23 20:25:15 +000081
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020082#include <assert.h>
83
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020084
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020085void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010086void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020087
Harald Welte2483f1b2016-06-19 18:06:02 +020088static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
89 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080090static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020091 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000092
Harald Welte52b1f982008-12-23 20:25:15 +000093struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020094 uint16_t mcc;
95 uint16_t mnc;
96 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000097};
98
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020099static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800100
Harald Welte31c00f72011-03-03 23:29:05 +0100101void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
102{
103 net->mncc_recv(net, msg);
104}
105
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800106static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
107 struct gsm_trans *trans)
108{
109 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
110
111 /* if we get passed a transaction reference, do some common
112 * work that the caller no longer has to do */
113 if (trans) {
114 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800115 }
116
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200117 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800118}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100119
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800120int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
121{
122 struct gsm48_hdr *gh;
123 struct msgb *ss_notify;
124
125 ss_notify = gsm0480_create_notifySS(message);
126 if (!ss_notify)
127 return -1;
128
129 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
130 uint8_t *data = msgb_push(ss_notify, 1);
131 data[0] = ss_notify->len - 1;
132 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
133 gh->msg_type = GSM48_MT_CC_FACILITY;
134 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
135}
136
Harald Welte2483f1b2016-06-19 18:06:02 +0200137/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100138void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
139{
140 struct gsm_trans *trans, *temp;
141
142 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
143
144 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100145 if (trans->protocol == protocol) {
146 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100147 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100148 }
Harald Welte371efe52010-12-22 23:17:50 +0100149 }
150}
151
Holger Freyther429e7762008-12-30 13:28:30 +0000152/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200153static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000154{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800155 struct msgb *msg;
156
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800157 msg = gsm48_create_loc_upd_rej(cause);
158 if (!msg) {
159 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
160 return -1;
161 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200162
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200163 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
164 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100165
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800166 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000167}
168
169/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200170static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
171 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000172{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100173 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000174 struct gsm48_hdr *gh;
175 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200176 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200177
Harald Welte52b1f982008-12-23 20:25:15 +0000178 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
179 gh->proto_discr = GSM48_PDISC_MM;
180 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
181
182 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200183 gsm48_generate_lai(lai, conn->network->country_code,
184 conn->network->network_code,
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200185 conn->lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000186
Harald Welte2483f1b2016-06-19 18:06:02 +0200187 if (send_tmsi == GSM_RESERVED_TMSI) {
188 /* we did not allocate a TMSI to the MS, so we need to
189 * include the IMSI in order for the MS to delete any
190 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200191 uint8_t mi[10];
192 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200193 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200194 mid = msgb_put(msg, len);
195 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200196 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
197 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200198 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200199 /* Include the TMSI, which means that the MS will send a
200 * TMSI REALLOCATION COMPLETE, and we should wait for
201 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200202 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200203 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200204 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
205 vlr_subscr_name(conn->vsub),
206 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200207 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200208 /* TODO: Follow-on proceed */
209 /* TODO: CTS permission */
210 /* TODO: Equivalent PLMNs */
211 /* TODO: Emergency Number List */
212 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000213
Harald Welte52b1f982008-12-23 20:25:15 +0000214
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800215 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000216}
217
Harald Weltebf5e8df2009-02-03 12:59:45 +0000218/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200219static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000220{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100221 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000222 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000223
Harald Welte231ad4f2008-12-27 11:15:38 +0000224 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
225 gh->proto_discr = GSM48_PDISC_MM;
226 gh->msg_type = GSM48_MT_MM_ID_REQ;
227 gh->data[0] = id_type;
228
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800229 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000230}
231
Harald Weltebf5e8df2009-02-03 12:59:45 +0000232/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800233static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000234{
235 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200236 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200237 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000238
Harald Welte2483f1b2016-06-19 18:06:02 +0200239 if (!conn->vsub) {
240 LOGP(DMM, LOGL_ERROR,
241 "Rx MM Identity Response: invalid: no subscriber\n");
242 return -EINVAL;
243 }
244
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200245 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200246 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
247 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000248
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200249 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100250
Harald Welte2483f1b2016-06-19 18:06:02 +0200251 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000252}
253
Harald Welte2483f1b2016-06-19 18:06:02 +0200254/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200255static const struct value_string lupd_names[] = {
256 { GSM48_LUPD_NORMAL, "NORMAL" },
257 { GSM48_LUPD_PERIODIC, "PERIODIC" },
258 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
259 { 0, NULL }
260};
Harald Welte2a139372009-02-22 21:14:55 +0000261
Harald Welte2483f1b2016-06-19 18:06:02 +0200262/* Chapter 9.2.15: Receive Location Updating Request.
263 * Keep this function non-static for direct invocation by unit tests. */
264int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000265{
Harald Welte2483f1b2016-06-19 18:06:02 +0200266 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
267 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000268 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000269 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200270 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200271 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200272 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200273 uint32_t tmsi;
274 char *imsi;
275 struct osmo_location_area_id old_lai, new_lai;
276 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200277 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200278 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000279
Harald Welte8470bf22008-12-25 23:28:35 +0000280 lu = (struct gsm48_loc_upd_req *) gh->data;
281
282 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000283
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200284 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000285
Harald Welte2483f1b2016-06-19 18:06:02 +0200286 rc = msc_create_conn_fsm(conn, mi_string);
287 if (rc)
288 /* logging already happened in msc_create_conn_fsm() */
289 return rc;
290
291 conn->classmark.classmark1 = lu->classmark1;
292 conn->classmark.classmark1_set = true;
293
294 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
295 gsm48_mi_type_name(mi_type), mi_string,
296 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000297
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200298 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100299
Harald Welte24ff6ee2009-12-22 00:41:05 +0100300 switch (lu->type) {
301 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200302 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200303 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100304 break;
305 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200306 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200307 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100308 break;
309 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200310 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200311 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100312 break;
313 }
314
Harald Welte2483f1b2016-06-19 18:06:02 +0200315 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
316 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
317 /* TODO: 10.5.7.8 Device properties */
318 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200319
Harald Welte52b1f982008-12-23 20:25:15 +0000320 switch (mi_type) {
321 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200322 tmsi = GSM_RESERVED_TMSI;
323 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000324 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000325 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200326 tmsi = tmsi_from_string(mi_string);
327 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000328 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200329 default:
Harald Weltea0368542009-06-27 02:58:43 +0200330 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200331 tmsi = GSM_RESERVED_TMSI;
332 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000333 break;
334 }
335
Harald Welte2483f1b2016-06-19 18:06:02 +0200336 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
337 &old_lai.plmn.mnc, &old_lai.lac);
338 new_lai.plmn.mcc = conn->network->country_code;
339 new_lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200340 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200341 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200342
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200343 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200344 lu_fsm = vlr_loc_update(conn->conn_fsm,
345 SUBSCR_CONN_E_ACCEPTED,
346 SUBSCR_CONN_E_CN_CLOSE,
347 (void*)&conn_from_lu,
348 net->vlr, conn, vlr_lu_type, tmsi, imsi,
349 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200350 is_utran || conn->network->authentication_required,
351 is_utran? VLR_CIPH_A5_3
352 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200353 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200354 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200355 net->vlr->cfg.assign_tmsi);
356 if (!lu_fsm) {
357 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
358 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800359 }
360
Harald Welte2483f1b2016-06-19 18:06:02 +0200361 /* From vlr_loc_update() we expect an implicit dispatch of
362 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
363 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000364
Harald Welte2483f1b2016-06-19 18:06:02 +0200365 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
366 LOGP(DRR, LOGL_ERROR,
367 "%s: internal error during Location Updating attempt\n",
368 mi_string);
369 return -EIO;
370 }
371
372 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000373}
374
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100375/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200376/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100377static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800378{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100379 uint8_t ret;
380
381 ret = value / 10;
382 ret |= (value % 10) << 4;
383
384 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800385}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100386
Harald Welte4bfdfe72009-06-10 23:11:52 +0800387
Harald Weltedb253af2008-12-30 17:56:55 +0000388/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800389int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000390{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100391 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000392 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200393 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200394 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200395 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100396
Harald Welte4bfdfe72009-06-10 23:11:52 +0800397 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100398 struct tm* gmt_time;
399 struct tm* local_time;
400 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100401 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000402
Harald Weltedb253af2008-12-30 17:56:55 +0000403 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
404 gh->proto_discr = GSM48_PDISC_MM;
405 gh->msg_type = GSM48_MT_MM_INFO;
406
407 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200408#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000409 name_len = strlen(net->name_long);
410 /* 10.5.3.5a */
411 ptr8 = msgb_put(msg, 3);
412 ptr8[0] = GSM48_IE_NAME_LONG;
413 ptr8[1] = name_len*2 +1;
414 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
415
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200416 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000417 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000418 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000419
420 /* FIXME: Use Cell Broadcast, not UCS-2, since
421 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200422#endif
423 name_len = (strlen(net->name_long)*7)/8;
424 name_pad = (8 - strlen(net->name_long)*7)%8;
425 if (name_pad > 0)
426 name_len++;
427 /* 10.5.3.5a */
428 ptr8 = msgb_put(msg, 3);
429 ptr8[0] = GSM48_IE_NAME_LONG;
430 ptr8[1] = name_len +1;
431 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
432
433 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100434 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200435
Harald Weltedb253af2008-12-30 17:56:55 +0000436 }
437
438 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200439#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000440 name_len = strlen(net->name_short);
441 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200442 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200443 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000444 ptr8[1] = name_len*2 + 1;
445 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
446
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200447 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000448 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000449 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200450#endif
451 name_len = (strlen(net->name_short)*7)/8;
452 name_pad = (8 - strlen(net->name_short)*7)%8;
453 if (name_pad > 0)
454 name_len++;
455 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200456 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200457 ptr8[0] = GSM48_IE_NAME_SHORT;
458 ptr8[1] = name_len +1;
459 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
460
461 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100462 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200463
Harald Weltedb253af2008-12-30 17:56:55 +0000464 }
465
Harald Weltedb253af2008-12-30 17:56:55 +0000466 /* Section 10.5.3.9 */
467 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100468 gmt_time = gmtime(&cur_t);
469
Harald Weltedb253af2008-12-30 17:56:55 +0000470 ptr8 = msgb_put(msg, 8);
471 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100472 ptr8[1] = bcdify(gmt_time->tm_year % 100);
473 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
474 ptr8[3] = bcdify(gmt_time->tm_mday);
475 ptr8[4] = bcdify(gmt_time->tm_hour);
476 ptr8[5] = bcdify(gmt_time->tm_min);
477 ptr8[6] = bcdify(gmt_time->tm_sec);
478
Neels Hofmeyr73983952016-05-10 13:29:33 +0200479 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200480 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200481 if (net->tz.hr < 0) {
482 tzunits = ((net->tz.hr/-1)*4);
483 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100484 ptr8[7] = bcdify(tzunits);
485 /* Set negative time */
486 ptr8[7] |= 0x08;
487 }
488 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200489 tzunits = net->tz.hr*4;
490 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100491 ptr8[7] = bcdify(tzunits);
492 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100493 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200494 if (net->tz.dst >= 0 && net->tz.dst <= 2)
495 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100496 }
497 else {
498 /* Need to get GSM offset and convert into 15 min units */
499 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200500#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200501 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100502 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200503#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200504 /* find timezone offset */
505 time_t utc;
506 double offsetFromUTC;
507 utc = mktime(gmt_time);
508 local_time = localtime(&cur_t);
509 offsetFromUTC = difftime(cur_t, utc);
510 if (local_time->tm_isdst)
511 offsetFromUTC += 3600.0;
512 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200513#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100514 if (tzunits < 0) {
515 tzunits = tzunits/-1;
516 ptr8[7] = bcdify(tzunits);
517 /* Flip it to negative */
518 ptr8[7] |= 0x08;
519 }
520 else
521 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100522
523 /* Does not support DST +2 */
524 if (local_time->tm_isdst)
525 dst = 1;
526 }
527
528 if (dst) {
529 ptr8 = msgb_put(msg, 3);
530 ptr8[0] = GSM48_IE_NET_DST;
531 ptr8[1] = 1;
532 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100533 }
Harald Weltedb253af2008-12-30 17:56:55 +0000534
Daniel Willmanneea93372009-08-13 03:42:07 +0200535 DEBUGP(DMM, "-> MM INFO\n");
536
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800537 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000538}
539
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100540/*! Send an Authentication Request to MS on the given subscriber connection
541 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
542 * \param[in] conn Subscriber connection to send on.
543 * \param[in] rand Random challenge token to send, must be 16 bytes long.
544 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
545 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
546 * \param[in] key_seq auth tuple's sequence number.
547 */
548int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
549 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200550{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100551 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200553 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200554
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100555 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100556 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100557 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200558
Harald Welte7984d5c2009-08-12 22:56:50 +0200559 gh->proto_discr = GSM48_PDISC_MM;
560 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
561
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100562 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200563
Harald Welte7984d5c2009-08-12 22:56:50 +0200564 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100565 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200566 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200567 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200568
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100569
570 /* 16 bytes AUTN */
571 if (autn)
572 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
573
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800574 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200575}
576
577/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800578int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200579{
580 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800581 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200582}
583
Harald Welte2483f1b2016-06-19 18:06:02 +0200584static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
585static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
586
587static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100588{
Harald Welte2483f1b2016-06-19 18:06:02 +0200589 uint8_t mi_type;
590 char mi_string[GSM48_MI_SIZE];
591 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100592
Harald Welte2483f1b2016-06-19 18:06:02 +0200593 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
594 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100595
Harald Welte2483f1b2016-06-19 18:06:02 +0200596 switch (mi_type) {
597 case GSM_MI_TYPE_IMSI:
598 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
599 goto accept_reuse;
600 break;
601 case GSM_MI_TYPE_TMSI:
602 tmsi = osmo_load32be(mi_lv+2);
603 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
604 goto accept_reuse;
605 break;
606 case GSM_MI_TYPE_IMEI:
607 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
608 goto accept_reuse;
609 break;
610 default:
611 break;
612 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100613
Harald Welte2483f1b2016-06-19 18:06:02 +0200614 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
615 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
616 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
617 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100618
Harald Welte2483f1b2016-06-19 18:06:02 +0200619accept_reuse:
620 DEBUGP(DMM, "%s: re-using already accepted connection\n",
621 vlr_subscr_name(conn->vsub));
622 conn->received_cm_service_request = true;
623 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100624}
625
Harald Welte4ed0e922009-01-10 03:17:30 +0000626/*
627 * Handle CM Service Requests
628 * a) Verify that the packet is long enough to contain the information
629 * we require otherwsie reject with INCORRECT_MESSAGE
630 * b) Try to parse the TMSI. If we do not have one reject
631 * c) Check that we know the subscriber with the TMSI otherwise reject
632 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200633 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200634 *
635 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000636 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200637int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000638{
Harald Welte2483f1b2016-06-19 18:06:02 +0200639 static const enum subscr_conn_from conn_from_cm_service_req =
640 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
641 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200642 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200643 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000644
Harald Welteba4cf162009-01-10 01:49:35 +0000645 struct gsm48_hdr *gh = msgb_l3(msg);
646 struct gsm48_service_request *req =
647 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800648 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200649 uint8_t classmark2_len = gh->data[1];
650 uint8_t *classmark2 = gh->data+2;
651 uint8_t mi_len = *(classmark2 + classmark2_len);
652 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200653 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200654 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200655 int rc;
656
657 lai.plmn.mcc = conn->network->country_code;
658 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200659 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000660
Harald Weltec9e02182009-05-01 19:07:53 +0000661 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000662 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000663 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-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
668 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000669 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200670 return msc_gsm48_tx_mm_serv_rej(conn,
671 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000672 }
673
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200674 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000675 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200676
677 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200678 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
679 req->cm_service_type, gsm48_mi_type_name(mi_type),
680 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200681 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200682 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
683 req->cm_service_type, gsm48_mi_type_name(mi_type),
684 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200685 } else {
686 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200687 return msc_gsm48_tx_mm_serv_rej(conn,
688 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000689 }
690
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200691 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200692 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
693 conn->classmark.classmark2_len = classmark2_len;
694
695 if (conn->conn_fsm) {
696 if (msc_subscr_conn_is_accepted(conn))
697 return cm_serv_reuse_conn(conn, mi-1);
698 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
699 vlr_subscr_name(conn->vsub));
700 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
701 return -EINVAL;
702 }
703
704 rc = msc_create_conn_fsm(conn, mi_string);
705 if (rc) {
706 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
707 /* logging already happened in msc_create_conn_fsm() */
708 return rc;
709 }
Harald Welte7659de12009-12-13 12:39:18 +0100710
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200711 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200712 vlr_proc_acc_req(conn->conn_fsm,
713 SUBSCR_CONN_E_ACCEPTED,
714 SUBSCR_CONN_E_CN_CLOSE,
715 (void*)&conn_from_cm_service_req,
716 net->vlr, conn,
717 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200718 is_utran || conn->network->authentication_required,
719 is_utran? VLR_CIPH_A5_3
720 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200721 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200722 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000723
Harald Welte2483f1b2016-06-19 18:06:02 +0200724 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000725}
726
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200727static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000728{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200729 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000730 struct gsm48_hdr *gh = msgb_l3(msg);
731 struct gsm48_imsi_detach_ind *idi =
732 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200733 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200734 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200735 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000736
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200737 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200738 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
739 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000740
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200741 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100742
Harald Welte2a139372009-02-22 21:14:55 +0000743 switch (mi_type) {
744 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200745 vsub = vlr_subscr_find_by_tmsi(network->vlr,
746 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000747 break;
748 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200749 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000750 break;
751 case GSM_MI_TYPE_IMEI:
752 case GSM_MI_TYPE_IMEISV:
753 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200754 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
755 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000756 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200757 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200758 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
759 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000760 break;
761 }
762
Harald Welte2483f1b2016-06-19 18:06:02 +0200763 /* TODO? We used to remember the subscriber's classmark1 here and
764 * stored it in the old sqlite db, but now we store it in a conn that
765 * will be discarded anyway: */
766 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200767
Harald Welte2483f1b2016-06-19 18:06:02 +0200768 if (!vsub) {
769 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
770 gsm48_mi_type_name(mi_type), mi_string);
771 } else {
772 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
773 vlr_subscr_rx_imsi_detach(vsub);
774 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
775 vlr_subscr_put(vsub);
776 }
Harald Welte2a139372009-02-22 21:14:55 +0000777
Harald Welte2483f1b2016-06-19 18:06:02 +0200778 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000779 return 0;
780}
781
Harald Welted2a7f5a2009-06-05 20:08:20 +0000782static int gsm48_rx_mm_status(struct msgb *msg)
783{
784 struct gsm48_hdr *gh = msgb_l3(msg);
785
786 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
787
788 return 0;
789}
790
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100791static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
792 struct gsm_subscriber_connection *conn,
793 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100794{
795 struct gsm48_hdr *gh = msgb_l3(msg);
796 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100797
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100798 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
799 LOGP(DMM, LOGL_ERROR,
800 "%s: MM AUTHENTICATION RESPONSE:"
801 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200802 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100803 return -EINVAL;
804 }
805
806 *res_len = sizeof(ar->sres);
807 memcpy(res, ar->sres, sizeof(ar->sres));
808 return 0;
809}
810
811static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
812 struct gsm_subscriber_connection *conn,
813 struct msgb *msg)
814{
815 struct gsm48_hdr *gh;
816 uint8_t *data;
817 uint8_t iei;
818 uint8_t ie_len;
819 unsigned int data_len;
820
821 /* First parse the GSM part */
822 if (parse_gsm_auth_resp(res, res_len, conn, msg))
823 return -EINVAL;
824 OSMO_ASSERT(*res_len == 4);
825
826 /* Then add the extended res part */
827 gh = msgb_l3(msg);
828 data = gh->data + sizeof(struct gsm48_auth_resp);
829 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
830
831 if (data_len < 3) {
832 LOGP(DMM, LOGL_ERROR,
833 "%s: MM AUTHENTICATION RESPONSE:"
834 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200835 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100836 return -EINVAL;
837 }
838
839 iei = data[0];
840 ie_len = data[1];
841 if (iei != GSM48_IE_AUTH_RES_EXT) {
842 LOGP(DMM, LOGL_ERROR,
843 "%s: MM R99 AUTHENTICATION RESPONSE:"
844 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200845 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100846 GSM48_IE_AUTH_RES_EXT, iei);
847 return -EINVAL;
848 }
849
850 if (ie_len > 12) {
851 LOGP(DMM, LOGL_ERROR,
852 "%s: MM R99 AUTHENTICATION RESPONSE:"
853 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200854 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100855 return -EINVAL;
856 }
857
858 *res_len += ie_len;
859 memcpy(res + 4, &data[2], ie_len);
860 return 0;
861}
862
863/* Chapter 9.2.3: Authentication Response */
864static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
865{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100866 uint8_t res[16];
867 uint8_t res_len;
868 int rc;
869 bool is_r99;
870
Harald Welte2483f1b2016-06-19 18:06:02 +0200871 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100872 LOGP(DMM, LOGL_ERROR,
873 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200874 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100875 return -EINVAL;
876 }
877
878 if (msgb_l3len(msg) >
879 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
880 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
881 is_r99 = true;
882 } else {
883 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
884 is_r99 = false;
885 }
886
887 if (rc) {
Harald Welte2483f1b2016-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 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200893 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100894 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
895 osmo_hexdump_nospc(res, res_len));
896
Harald Welte2483f1b2016-06-19 18:06:02 +0200897 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
898 conn->via_ran == RAN_UTRAN_IU,
899 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100900}
901
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100902static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
903{
904 struct gsm48_hdr *gh = msgb_l3(msg);
905 uint8_t cause;
906 uint8_t auts_tag;
907 uint8_t auts_len;
908 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100909
Harald Welte2483f1b2016-06-19 18:06:02 +0200910 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100911 LOGP(DMM, LOGL_ERROR,
912 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200913 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100914 return -EINVAL;
915 }
916
917 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
918 LOGP(DMM, LOGL_ERROR,
919 "%s: MM R99 AUTHENTICATION FAILURE:"
920 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200921 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
922 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100923 return -EINVAL;
924 }
925
926 cause = gh->data[0];
927
928 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
929 LOGP(DMM, LOGL_INFO,
930 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200931 vlr_subscr_name(conn->vsub), cause);
932 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
933 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100934 }
935
936 /* This is a Synch Failure procedure, which should pass an AUTS to
937 * resynchronize the sequence nr with the HLR. Expecting exactly one
938 * TLV with 14 bytes of AUTS. */
939
940 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
941 LOGP(DMM, LOGL_INFO,
942 "%s: MM R99 AUTHENTICATION FAILURE:"
943 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200944 vlr_subscr_name(conn->vsub));
945 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100946 return -EINVAL;
947 }
948
949 auts_tag = gh->data[1];
950 auts_len = gh->data[2];
951 auts = &gh->data[3];
952
953 if (auts_tag != GSM48_IE_AUTS
954 || auts_len != 14) {
955 LOGP(DMM, LOGL_INFO,
956 "%s: MM R99 AUTHENTICATION FAILURE:"
957 " invalid Synch Failure:"
958 " expected AUTS IE 0x%02x of 14 bytes,"
959 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200960 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100961 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200962 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100963 return -EINVAL;
964 }
965
966 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
967 LOGP(DMM, LOGL_INFO,
968 "%s: MM R99 AUTHENTICATION FAILURE:"
969 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200970 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
971 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100972 return -EINVAL;
973 }
974
975 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
976 * large enough. */
977
978 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200979 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100980
Harald Welte2483f1b2016-06-19 18:06:02 +0200981 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
982}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100983
Harald Welte2483f1b2016-06-19 18:06:02 +0200984static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
985{
986 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
987 vlr_subscr_name(conn->vsub));
988 if (!conn->vsub) {
989 LOGP(DMM, LOGL_ERROR,
990 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
991 return -EINVAL;
992 }
993 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100994}
995
Harald Weltebf5e8df2009-02-03 12:59:45 +0000996/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800997static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000998{
999 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001000 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001001
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001002 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001003 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001004 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001005 break;
1006 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001007 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001008 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001009 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001010 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001011 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001012 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001013 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001014 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001015 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001016 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001017 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001018 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001019 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001020 break;
1021 case GSM48_MT_MM_CM_REEST_REQ:
1022 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1023 break;
1024 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001025 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001026 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001027 case GSM48_MT_MM_AUTH_FAIL:
1028 rc = gsm48_rx_mm_auth_fail(conn, msg);
1029 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001030 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001031 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001032 gh->msg_type);
1033 break;
1034 }
1035
1036 return rc;
1037}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001038
Harald Welte2483f1b2016-06-19 18:06:02 +02001039static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1040{
1041 /* Check the size for the classmark */
1042 if (tot_len < 1 + *classmark2_lv)
1043 return NULL;
1044
1045 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1046 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1047 return NULL;
1048
1049 return mi_lv;
1050}
1051
Harald Welte2d35ae62009-02-06 12:02:13 +00001052/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001053static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001054{
Harald Welte2483f1b2016-06-19 18:06:02 +02001055 static const enum subscr_conn_from conn_from_paging_resp =
1056 SUBSCR_CONN_FROM_PAGING_RESP;
1057 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001058 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001059 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001060 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001061 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001062 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001063 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001064 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001065 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001066 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001067
1068 lai.plmn.mcc = conn->network->country_code;
1069 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001070 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001071
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001072 resp = (struct gsm48_pag_resp *) &gh->data[0];
1073 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1074 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001075 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1076 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001077
Harald Welte2483f1b2016-06-19 18:06:02 +02001078 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1079 if (!mi_lv) {
1080 /* FIXME */
1081 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001082 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001083
Harald Welte2483f1b2016-06-19 18:06:02 +02001084 rc = msc_create_conn_fsm(conn, mi_string);
1085 if (rc)
1086 /* logging already happened in msc_create_conn_fsm() */
1087 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001088
Harald Welte2483f1b2016-06-19 18:06:02 +02001089 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1090 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001091
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001092 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +02001093 vlr_proc_acc_req(conn->conn_fsm,
1094 SUBSCR_CONN_E_ACCEPTED,
1095 SUBSCR_CONN_E_CN_CLOSE,
1096 (void*)&conn_from_paging_resp,
1097 net->vlr, conn,
1098 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001099 is_utran || conn->network->authentication_required,
1100 is_utran? VLR_CIPH_A5_3
1101 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +02001102 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001103 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001104
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001105 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001106}
1107
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001108static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001109{
1110 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001111 uint8_t apdu_id_flags;
1112 uint8_t apdu_len;
1113 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001114
1115 apdu_id_flags = gh->data[0];
1116 apdu_len = gh->data[1];
1117 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001118
Max8dc8f232017-02-09 19:13:02 +01001119 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001120 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001121
Harald Welte2483f1b2016-06-19 18:06:02 +02001122 /* we're not using the app info blob anywhere, so ignore. */
1123#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001124 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001125#else
1126 return 0;
1127#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001128}
1129
Harald Weltebf5e8df2009-02-03 12:59:45 +00001130/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001131static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001132{
1133 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001134 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001135
1136 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001137 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001138 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001139 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001140 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001141 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001142 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001143 default:
Philippd37981e2016-10-10 15:11:41 +02001144 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1145 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001146 break;
1147 }
1148
Harald Welte2d35ae62009-02-06 12:02:13 +00001149 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001150}
1151
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001152int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1153 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001154{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001155 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001156 struct gsm48_hdr *gh;
1157
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001158 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1159 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001160
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001161 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1162 gh->proto_discr = GSM48_PDISC_RR;
1163 gh->msg_type = GSM48_MT_RR_APP_INFO;
1164 gh->data[0] = apdu_id;
1165 gh->data[1] = apdu_len;
1166 memcpy(gh->data+2, apdu, apdu_len);
1167
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001168 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001169}
1170
Alexander Couzens92f552f2016-08-23 07:32:27 +02001171/* FIXME: this count_statistics is a state machine behaviour. we should convert
1172 * the complete call control into a state machine. Afterwards we can move this
1173 * code into state transitions.
1174 */
1175static void count_statistics(struct gsm_trans *trans, int new_state)
1176{
1177 int old_state = trans->cc.state;
1178 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1179
1180 if (old_state == new_state)
1181 return;
1182
1183 /* state incoming */
1184 switch (new_state) {
1185 case GSM_CSTATE_ACTIVE:
1186 osmo_counter_inc(trans->net->active_calls);
1187 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1188 break;
1189 }
1190
1191 /* state outgoing */
1192 switch (old_state) {
1193 case GSM_CSTATE_ACTIVE:
1194 osmo_counter_dec(trans->net->active_calls);
1195 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1196 new_state == GSM_CSTATE_DISCONNECT_IND)
1197 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1198 else
1199 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1200 break;
1201 }
1202}
1203
Harald Welte4bc90a12008-12-27 16:32:52 +00001204/* Call Control */
1205
Harald Welte7584aea2009-02-11 11:44:12 +00001206/* The entire call control code is written in accordance with Figure 7.10c
1207 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1208 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1209 * it for voice */
1210
Harald Welte4bfdfe72009-06-10 23:11:52 +08001211static void new_cc_state(struct gsm_trans *trans, int state)
1212{
1213 if (state > 31 || state < 0)
1214 return;
1215
1216 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001217 gsm48_cc_state_name(trans->cc.state),
1218 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001219
Alexander Couzens92f552f2016-08-23 07:32:27 +02001220 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001221 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001222}
1223
1224static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001225{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001226 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001227 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001228 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001229
Harald Welte4bc90a12008-12-27 16:32:52 +00001230 gh->msg_type = GSM48_MT_CC_STATUS;
1231
1232 cause = msgb_put(msg, 3);
1233 cause[0] = 2;
1234 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1235 cause[2] = 0x80 | 30; /* response to status inquiry */
1236
1237 call_state = msgb_put(msg, 1);
1238 call_state[0] = 0xc0 | 0x00;
1239
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001240 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001241}
1242
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001243static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001244 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001245{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001246 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001247 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1248
Harald Welte6f4b7532008-12-29 00:39:37 +00001249 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001250 gh->msg_type = msg_type;
1251
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001252 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001253}
1254
Harald Welte4bfdfe72009-06-10 23:11:52 +08001255static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1256{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001257 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001258 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001259 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001260 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001261 }
1262}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001263
Harald Welte4bfdfe72009-06-10 23:11:52 +08001264static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1265 int msg_type, struct gsm_mncc *mncc)
1266{
1267 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001268 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001269
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001270#if BEFORE_MSCSPLIT
1271 /* Re-enable this log output once we can obtain this information via
1272 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001273 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001274 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001275 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001276 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001277 trans->conn->lchan->ts->trx->bts->nr,
1278 trans->conn->lchan->ts->trx->nr,
1279 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001280 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001281 get_mncc_name(msg_type));
1282 else
1283 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1284 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001285 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286 get_mncc_name(msg_type));
1287 else
1288 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1289 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001290#else
1291 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1292#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001293
1294 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001295
Harald Welte966636f2009-06-26 19:39:35 +02001296 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297 if (!msg)
1298 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001299
1300 data = msgb_put(msg, sizeof(struct gsm_mncc));
1301 memcpy(data, mncc, sizeof(struct gsm_mncc));
1302
Harald Welte54209c22010-12-22 23:43:29 +01001303 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001304
1305 return 0;
1306}
1307
1308int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001309 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001310{
1311 struct gsm_mncc rel;
1312
Harald Welte92f70c52009-06-12 01:54:08 +08001313 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001315 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001316 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1317 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1319}
1320
Harald Weltedcaf5652009-07-23 18:56:43 +02001321/* Call Control Specific transaction release.
1322 * gets called by trans_free, DO NOT CALL YOURSELF! */
1323void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001324{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001325 gsm48_stop_cc_timer(trans);
1326
1327 /* send release to L4, if callref still exists */
1328 if (trans->callref) {
1329 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001330 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001331 GSM48_CAUSE_LOC_PRN_S_LU,
1332 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001333 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001334 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001335 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336}
1337
1338static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001339
Harald Welte09e38af2009-02-16 22:52:23 +00001340/* call-back from paging the B-end of the connection */
1341static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001342 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001343{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001344 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001345 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001346
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001347 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001348
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001349 switch (event) {
1350 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001351 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1352 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001353 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001354 /* Assign conn */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001355 transt->conn = conn;
1356 /* send SETUP request to called party */
1357 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1358 break;
1359 case GSM_PAGING_EXPIRED:
1360 case GSM_PAGING_BUSY:
1361 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001362 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001363 /* Temporarily out of order */
1364 mncc_release_ind(transt->net, transt,
1365 transt->callref,
1366 GSM48_CAUSE_LOC_PRN_S_LU,
1367 GSM48_CC_CAUSE_DEST_OOO);
1368 transt->callref = 0;
1369 transt->paging_request = NULL;
1370 trans_free(transt);
1371 break;
1372 default:
1373 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1374 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001376
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001377 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001378 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001379}
Harald Welte7584aea2009-02-11 11:44:12 +00001380
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001382static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001383{
Harald Welte53d51f52015-12-03 14:59:04 +01001384 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1385 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001386
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001388 return -EIO;
1389
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001390 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001391 return -EIO;
1392
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001393 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001394 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001395
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001396 return msc_call_bridge(trans1, trans2);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001397}
1398
Harald Welte4bfdfe72009-06-10 23:11:52 +08001399static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1400{
1401 DEBUGP(DCC, "-> STATUS ENQ\n");
1402 return gsm48_cc_tx_status(trans, msg);
1403}
1404
1405static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1406static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1407
1408static void gsm48_cc_timeout(void *arg)
1409{
1410 struct gsm_trans *trans = arg;
1411 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001412 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1413 int mo_location = GSM48_CAUSE_LOC_USER;
1414 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1415 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001416 struct gsm_mncc mo_rel, l4_rel;
1417
1418 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1419 mo_rel.callref = trans->callref;
1420 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1421 l4_rel.callref = trans->callref;
1422
Harald Weltedcaf5652009-07-23 18:56:43 +02001423 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001424 case 0x303:
1425 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001426 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 break;
1428 case 0x310:
1429 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001430 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001431 break;
1432 case 0x313:
1433 disconnect = 1;
1434 /* unknown, did not find it in the specs */
1435 break;
1436 case 0x301:
1437 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001438 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001439 break;
1440 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001441 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001442 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001443 gsm48_cc_tx_release(trans, &trans->cc.msg);
1444 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001445 break; /* stay in release state */
1446 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001447 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001448 return;
1449// release = 1;
1450// l4_cause = 14;
1451// break;
1452 case 0x306:
1453 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001454 mo_cause = trans->cc.msg.cause.value;
1455 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001456 break;
1457 case 0x323:
1458 disconnect = 1;
1459 break;
1460 default:
1461 release = 1;
1462 }
1463
1464 if (release && trans->callref) {
1465 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001466 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001467 l4_location, l4_cause);
1468 trans->callref = 0;
1469 }
1470
1471 if (disconnect && trans->callref) {
1472 /* process disconnect towards layer 4 */
1473 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001474 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001475 }
1476
1477 /* process disconnect towards mobile station */
1478 if (disconnect || release) {
1479 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001480 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1481 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1482 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001483 mo_rel.cause.diag_len = 3;
1484
1485 if (disconnect)
1486 gsm48_cc_tx_disconnect(trans, &mo_rel);
1487 if (release)
1488 gsm48_cc_tx_release(trans, &mo_rel);
1489 }
1490
1491}
1492
Max3ffce192016-04-25 15:22:00 +02001493/* disconnect both calls from the bridge */
1494static inline void disconnect_bridge(struct gsm_network *net,
1495 struct gsm_mncc_bridge *bridge, int err)
1496{
1497 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1498 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1499 struct gsm_mncc mx_rel;
1500 if (!trans0 || !trans1)
1501 return;
1502
1503 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1504 trans0->callref, trans1->callref, strerror(err));
1505
1506 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1507 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1508 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1509
1510 mx_rel.callref = trans0->callref;
1511 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1512
1513 mx_rel.callref = trans1->callref;
1514 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1515}
1516
Harald Welte4bfdfe72009-06-10 23:11:52 +08001517static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1518 int sec, int micro)
1519{
1520 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001521 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001522 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001523 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001524}
1525
1526static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1527{
1528 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001529 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001530 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1531 struct tlv_parsed tp;
1532 struct gsm_mncc setup;
1533
1534 memset(&setup, 0, sizeof(struct gsm_mncc));
1535 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001536
Harald Welte474d19f2010-03-02 23:18:30 +01001537 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001538 /* emergency setup is identified by msg_type */
1539 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1540 setup.emergency = 1;
1541
1542 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001543 setup.fields |= MNCC_F_CALLING;
Harald Welte2483f1b2016-06-19 18:06:02 +02001544 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1545 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001546
Harald Welte4bfdfe72009-06-10 23:11:52 +08001547 /* bearer capability */
1548 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1549 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001550 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001551 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1552 }
1553 /* facility */
1554 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1555 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001556 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001557 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1558 }
1559 /* called party bcd number */
1560 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1561 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001562 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001563 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1564 }
1565 /* user-user */
1566 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1567 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001568 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1570 }
1571 /* ss-version */
1572 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1573 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001574 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001575 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1576 }
1577 /* CLIR suppression */
1578 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1579 setup.clir.sup = 1;
1580 /* CLIR invocation */
1581 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1582 setup.clir.inv = 1;
1583 /* cc cap */
1584 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1585 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001586 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001587 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1588 }
1589
Harald Welte4bfdfe72009-06-10 23:11:52 +08001590 new_cc_state(trans, GSM_CSTATE_INITIATED);
1591
Harald Welte (local)47df3992009-12-26 19:45:03 +01001592 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001593 vlr_subscr_name(trans->vsub), trans->vsub->msisdn,
Harald Welte (local)47df3992009-12-26 19:45:03 +01001594 setup.called.number);
1595
Alexander Couzensb847a212016-08-02 11:34:11 +02001596 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001597
Harald Welte4bfdfe72009-06-10 23:11:52 +08001598 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001599 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001600
Harald Welte13cac662009-07-29 12:10:35 +02001601 /* MNCC code will modify the channel asynchronously, we should
1602 * ipaccess-bind only after the modification has been made to the
1603 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001604 return 0;
1605}
1606
1607static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001608{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001609 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001610 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001611 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001612 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001613
Harald Welte7ccf7782009-02-17 01:43:01 +00001614 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001615
Harald Welte4bfdfe72009-06-10 23:11:52 +08001616 /* transaction id must not be assigned */
1617 if (trans->transaction_id != 0xff) { /* unasssigned */
1618 DEBUGP(DCC, "TX Setup with assigned transaction. "
1619 "This is not allowed!\n");
1620 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001621 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001622 GSM48_CAUSE_LOC_PRN_S_LU,
1623 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001624 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001625 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001626 return rc;
1627 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001628
Harald Welte4bfdfe72009-06-10 23:11:52 +08001629 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001630 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001631 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001632 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001633 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001634 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001635 GSM48_CAUSE_LOC_PRN_S_LU,
1636 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001638 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001639 return rc;
1640 }
Harald Welte78283ef2009-07-23 21:36:44 +02001641 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001642
Harald Welte65e74cc2008-12-29 01:55:35 +00001643 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001644
Harald Welte4bfdfe72009-06-10 23:11:52 +08001645 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001646
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647 /* bearer capability */
1648 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001649 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001650 /* facility */
1651 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001652 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 /* progress */
1654 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001655 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656 /* calling party BCD number */
1657 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001658 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 /* called party BCD number */
1660 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001661 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 /* user-user */
1663 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001664 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001665 /* redirecting party BCD number */
1666 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001667 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668 /* signal */
1669 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001670 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001671
Harald Welte4bfdfe72009-06-10 23:11:52 +08001672 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001673
Alexander Couzensb847a212016-08-02 11:34:11 +02001674 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001675
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001676 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001677}
1678
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1680{
1681 struct gsm48_hdr *gh = msgb_l3(msg);
1682 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1683 struct tlv_parsed tp;
1684 struct gsm_mncc call_conf;
1685
1686 gsm48_stop_cc_timer(trans);
1687 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1688
1689 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1690 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001691
Harald Welte474d19f2010-03-02 23:18:30 +01001692 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001693#if 0
1694 /* repeat */
1695 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1696 call_conf.repeat = 1;
1697 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1698 call_conf.repeat = 2;
1699#endif
1700 /* bearer capability */
1701 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1702 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001703 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001704 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1705 }
1706 /* cause */
1707 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1708 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001709 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001710 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1711 }
1712 /* cc cap */
1713 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1714 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001715 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1717 }
1718
Andreas Eversberg035b8742013-09-19 09:27:06 +02001719 /* IMSI of called subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +02001720 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02001721
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1723
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001724 msc_call_assignment(trans);
1725
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001726 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001727 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001728}
1729
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001730static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731{
1732 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001733 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001735 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1738
1739 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1740
1741 /* bearer capability */
1742 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001743 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 /* facility */
1745 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001746 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747 /* progress */
1748 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001749 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001750
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001751 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1752 if (rc)
1753 return rc;
1754
1755 return msc_call_assignment(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756}
1757
1758static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1759{
1760 struct gsm48_hdr *gh = msgb_l3(msg);
1761 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1762 struct tlv_parsed tp;
1763 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001764
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 gsm48_stop_cc_timer(trans);
1766 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1767
1768 memset(&alerting, 0, sizeof(struct gsm_mncc));
1769 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001770 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 /* facility */
1772 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1773 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001774 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1776 }
1777
1778 /* progress */
1779 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1780 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001781 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1783 }
1784 /* ss-version */
1785 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1786 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001787 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1789 }
1790
1791 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1792
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001793 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001794 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795}
1796
1797static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1798{
1799 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001800 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1802
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 gh->msg_type = GSM48_MT_CC_ALERTING;
1804
1805 /* facility */
1806 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001807 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 /* progress */
1809 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001810 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811 /* user-user */
1812 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001813 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814
1815 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001816
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001817 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818}
1819
1820static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1821{
1822 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001823 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1825
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 gh->msg_type = GSM48_MT_CC_PROGRESS;
1827
1828 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001829 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830 /* user-user */
1831 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001832 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001833
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001834 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835}
1836
1837static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1838{
1839 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001840 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1842
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 gh->msg_type = GSM48_MT_CC_CONNECT;
1844
1845 gsm48_stop_cc_timer(trans);
1846 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1847
1848 /* facility */
1849 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001850 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001851 /* progress */
1852 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001853 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 /* connected number */
1855 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001856 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857 /* user-user */
1858 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001859 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001860
1861 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1862
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001863 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864}
1865
1866static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1867{
1868 struct gsm48_hdr *gh = msgb_l3(msg);
1869 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1870 struct tlv_parsed tp;
1871 struct gsm_mncc connect;
1872
1873 gsm48_stop_cc_timer(trans);
1874
1875 memset(&connect, 0, sizeof(struct gsm_mncc));
1876 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001877 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001879 connect.fields |= MNCC_F_CONNECTED;
Harald Welte2483f1b2016-06-19 18:06:02 +02001880 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
1881 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001882
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883 /* facility */
1884 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1885 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001886 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1888 }
1889 /* user-user */
1890 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1891 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001892 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1894 }
1895 /* ss-version */
1896 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1897 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001898 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1900 }
1901
1902 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02001903 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001904
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001905 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001906}
1907
1908
1909static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1910{
1911 struct gsm_mncc connect_ack;
1912
1913 gsm48_stop_cc_timer(trans);
1914
1915 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02001916 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001917
Harald Welte4bfdfe72009-06-10 23:11:52 +08001918 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1919 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01001920
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001921 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001922 &connect_ack);
1923}
1924
1925static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1926{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001927 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1929
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1931
1932 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1933
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001934 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935}
1936
1937static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1938{
1939 struct gsm48_hdr *gh = msgb_l3(msg);
1940 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1941 struct tlv_parsed tp;
1942 struct gsm_mncc disc;
1943
1944 gsm48_stop_cc_timer(trans);
1945
1946 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1947
1948 memset(&disc, 0, sizeof(struct gsm_mncc));
1949 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001950 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 /* cause */
1952 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1953 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001954 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1956 }
1957 /* facility */
1958 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1959 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001960 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1962 }
1963 /* user-user */
1964 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1965 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001966 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1968 }
1969 /* ss-version */
1970 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1971 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001972 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1974 }
1975
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001976 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977
1978}
1979
Harald Weltec66b71c2009-06-11 14:23:20 +08001980static struct gsm_mncc_cause default_cause = {
1981 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1982 .coding = 0,
1983 .rec = 0,
1984 .rec_val = 0,
1985 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1986 .diag_len = 0,
1987 .diag = { 0 },
1988};
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989
1990static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1991{
1992 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001993 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1995
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1997
1998 gsm48_stop_cc_timer(trans);
1999 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2000
2001 /* cause */
2002 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002003 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 else
Harald Welte55c8f352010-03-07 23:40:35 +01002005 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006
2007 /* facility */
2008 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002009 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 /* progress */
2011 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002012 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 /* user-user */
2014 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002015 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016
2017 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002018 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019
2020 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2021
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002022 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023}
2024
2025static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2026{
2027 struct gsm48_hdr *gh = msgb_l3(msg);
2028 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2029 struct tlv_parsed tp;
2030 struct gsm_mncc rel;
2031 int rc;
2032
2033 gsm48_stop_cc_timer(trans);
2034
2035 memset(&rel, 0, sizeof(struct gsm_mncc));
2036 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002037 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 /* cause */
2039 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2040 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002041 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2043 }
2044 /* facility */
2045 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2046 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002047 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2049 }
2050 /* user-user */
2051 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2052 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002053 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2055 }
2056 /* ss-version */
2057 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2058 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002059 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2061 }
2062
Harald Weltedcaf5652009-07-23 18:56:43 +02002063 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002065 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002067 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002068 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002069 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002070 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002071 }
2072
2073 new_cc_state(trans, GSM_CSTATE_NULL);
2074
2075 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002076 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077
2078 return rc;
2079}
2080
2081static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2082{
2083 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002084 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2086
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 gh->msg_type = GSM48_MT_CC_RELEASE;
2088
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 gsm48_stop_cc_timer(trans);
2090 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2091
2092 /* cause */
2093 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002094 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 /* facility */
2096 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002097 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 /* user-user */
2099 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002100 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101
Harald Weltedcaf5652009-07-23 18:56:43 +02002102 trans->cc.T308_second = 0;
2103 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104
Harald Weltedcaf5652009-07-23 18:56:43 +02002105 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2107
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002108 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109}
2110
2111static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2112{
2113 struct gsm48_hdr *gh = msgb_l3(msg);
2114 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2115 struct tlv_parsed tp;
2116 struct gsm_mncc rel;
2117 int rc = 0;
2118
2119 gsm48_stop_cc_timer(trans);
2120
2121 memset(&rel, 0, sizeof(struct gsm_mncc));
2122 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002123 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 /* cause */
2125 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2126 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002127 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2129 }
2130 /* facility */
2131 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2132 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002133 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2135 }
2136 /* user-user */
2137 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2138 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002139 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2141 }
2142 /* ss-version */
2143 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2144 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002145 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2147 }
2148
2149 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002150 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002152 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 MNCC_REJ_IND, &rel);
2154 break;
2155 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002156 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 MNCC_REL_CNF, &rel);
2158 break;
2159 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002160 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161 MNCC_REL_IND, &rel);
2162 }
2163 }
2164
2165 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002166 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167
2168 return rc;
2169}
2170
2171static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2172{
2173 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002174 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002176 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2179
2180 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002181
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182 gsm48_stop_cc_timer(trans);
2183
2184 /* cause */
2185 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 /* facility */
2188 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002189 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190 /* user-user */
2191 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002192 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193
Harald Weltec7782de2010-12-21 19:31:41 +01002194 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2195
Harald Weltedcaf5652009-07-23 18:56:43 +02002196 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197
Harald Weltec7782de2010-12-21 19:31:41 +01002198 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199}
2200
2201static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2202{
2203 struct gsm48_hdr *gh = msgb_l3(msg);
2204 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2205 struct tlv_parsed tp;
2206 struct gsm_mncc fac;
2207
2208 memset(&fac, 0, sizeof(struct gsm_mncc));
2209 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002210 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 /* facility */
2212 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2213 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002214 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2216 }
2217 /* ss-version */
2218 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2219 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002220 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2222 }
2223
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002224 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225}
2226
2227static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2228{
2229 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002230 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2232
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233 gh->msg_type = GSM48_MT_CC_FACILITY;
2234
2235 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002238 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239}
2240
2241static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2242{
2243 struct gsm_mncc hold;
2244
2245 memset(&hold, 0, sizeof(struct gsm_mncc));
2246 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002247 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248}
2249
2250static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2251{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002252 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2254
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2256
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002257 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258}
2259
2260static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2261{
2262 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002263 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2265
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2267
2268 /* cause */
2269 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002270 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271 else
Harald Welte55c8f352010-03-07 23:40:35 +01002272 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002274 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275}
2276
2277static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2278{
2279 struct gsm_mncc retrieve;
2280
2281 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2282 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002283 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002284 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285}
2286
2287static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2288{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002289 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2291
Harald Welte4bfdfe72009-06-10 23:11:52 +08002292 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2293
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002294 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295}
2296
2297static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2298{
2299 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002300 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2302
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2304
2305 /* cause */
2306 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002307 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 else
Harald Welte55c8f352010-03-07 23:40:35 +01002309 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002311 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312}
2313
2314static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2315{
2316 struct gsm48_hdr *gh = msgb_l3(msg);
2317 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2318 struct tlv_parsed tp;
2319 struct gsm_mncc dtmf;
2320
2321 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2322 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002323 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 /* keypad facility */
2325 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2326 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002327 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2329 }
2330
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002331 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332}
2333
2334static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2335{
2336 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002337 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2339
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2341
2342 /* keypad */
2343 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002344 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002346 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347}
2348
2349static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2350{
2351 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002352 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2354
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2356
2357 /* cause */
2358 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002359 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 else
Harald Welte55c8f352010-03-07 23:40:35 +01002361 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002363 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364}
2365
2366static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2367{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002368 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002369 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2370
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2372
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002373 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002374}
2375
2376static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2377{
2378 struct gsm_mncc dtmf;
2379
2380 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2381 dtmf.callref = trans->callref;
2382
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002383 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384}
2385
2386static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2387{
2388 struct gsm48_hdr *gh = msgb_l3(msg);
2389 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2390 struct tlv_parsed tp;
2391 struct gsm_mncc modify;
2392
2393 memset(&modify, 0, sizeof(struct gsm_mncc));
2394 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002395 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396 /* bearer capability */
2397 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2398 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002399 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2401 }
2402
2403 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2404
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002405 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406}
2407
2408static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2409{
2410 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002411 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2413
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 gh->msg_type = GSM48_MT_CC_MODIFY;
2415
2416 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2417
2418 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002419 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420
2421 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2422
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002423 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424}
2425
2426static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2427{
2428 struct gsm48_hdr *gh = msgb_l3(msg);
2429 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2430 struct tlv_parsed tp;
2431 struct gsm_mncc modify;
2432
2433 gsm48_stop_cc_timer(trans);
2434
2435 memset(&modify, 0, sizeof(struct gsm_mncc));
2436 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002437 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002438 /* bearer capability */
2439 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2440 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002441 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2443 }
2444
2445 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2446
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002447 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002448}
2449
2450static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2451{
2452 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002453 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2455
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2457
2458 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002459 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460
2461 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2462
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002463 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464}
2465
2466static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2467{
2468 struct gsm48_hdr *gh = msgb_l3(msg);
2469 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2470 struct tlv_parsed tp;
2471 struct gsm_mncc modify;
2472
2473 gsm48_stop_cc_timer(trans);
2474
2475 memset(&modify, 0, sizeof(struct gsm_mncc));
2476 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002477 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478 /* bearer capability */
2479 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2480 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002481 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2483 }
2484 /* cause */
2485 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2486 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002487 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2489 }
2490
2491 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2492
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002493 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494}
2495
2496static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2497{
2498 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002499 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2501
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2503
2504 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002505 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002507 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508
2509 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2510
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002511 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512}
2513
2514static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2515{
2516 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002517 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002518 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2519
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520 gh->msg_type = GSM48_MT_CC_NOTIFY;
2521
2522 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002523 gsm48_encode_notify(msg, notify->notify);
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_notify(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 notify;
2534
2535 memset(&notify, 0, sizeof(struct gsm_mncc));
2536 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002537// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002538 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002539 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002541 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542}
2543
2544static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2545{
2546 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002547 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2549
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550 gh->msg_type = GSM48_MT_CC_USER_INFO;
2551
2552 /* user-user */
2553 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002554 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555 /* more data */
2556 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002557 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002559 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560}
2561
2562static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2563{
2564 struct gsm48_hdr *gh = msgb_l3(msg);
2565 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2566 struct tlv_parsed tp;
2567 struct gsm_mncc user;
2568
2569 memset(&user, 0, sizeof(struct gsm_mncc));
2570 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002571 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 /* user-user */
2573 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2574 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002575 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2577 }
2578 /* more data */
2579 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2580 user.more = 1;
2581
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002582 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583}
2584
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002586 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587 int type;
2588 int (*rout) (struct gsm_trans *trans, void *arg);
2589} downstatelist[] = {
2590 /* mobile originating call establishment */
2591 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002592 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2594 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2595 {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 */
2596 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2597 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2598 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2599 /* mobile terminating call establishment */
2600 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2601 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2602 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2603 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2604 /* signalling during call */
2605 {SBIT(GSM_CSTATE_ACTIVE),
2606 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2607 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2608 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2609 {ALL_STATES,
2610 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2611 {ALL_STATES,
2612 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2613 {ALL_STATES,
2614 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2615 {SBIT(GSM_CSTATE_ACTIVE),
2616 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2617 {SBIT(GSM_CSTATE_ACTIVE),
2618 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2619 {SBIT(GSM_CSTATE_ACTIVE),
2620 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2621 {SBIT(GSM_CSTATE_ACTIVE),
2622 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2623 {SBIT(GSM_CSTATE_ACTIVE),
2624 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2625 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2626 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2627 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2628 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2629 {SBIT(GSM_CSTATE_ACTIVE),
2630 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2631 /* clearing */
2632 {SBIT(GSM_CSTATE_INITIATED),
2633 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2634 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2635 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2636 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2637 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638};
2639
2640#define DOWNSLLEN \
2641 (sizeof(downstatelist) / sizeof(struct downstate))
2642
2643
Harald Welte76556372010-12-22 23:57:45 +01002644int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645{
Harald Welte1a6f7982009-08-09 18:52:33 +02002646 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002648 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649 struct gsm_mncc *data = arg, rel;
2650
Harald Welte04dc88f2010-12-22 21:45:05 +01002651 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2652
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 /* handle special messages */
2654 switch(msg_type) {
2655 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002656 rc = tch_bridge(net, arg);
2657 if (rc < 0)
2658 disconnect_bridge(net, arg, -rc);
2659 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661 case MNCC_FRAME_RECV:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002662 case MNCC_RTP_CREATE:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002663 case MNCC_RTP_CONNECT:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002664 case MNCC_RTP_FREE:
Harald Welteda7ab742009-12-19 22:23:05 +01002665 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002666 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002667 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002668 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002669 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2670 get_mncc_name(msg_type));
2671 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 }
2673
2674 memset(&rel, 0, sizeof(struct gsm_mncc));
2675 rel.callref = data->callref;
2676
2677 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002678 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679
2680 /* Callref unknown */
2681 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02002682 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002683
Harald Welte4a3464c2009-07-04 10:11:24 +02002684 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2686 "Received '%s' from MNCC with "
2687 "unknown callref %d\n", data->called.number,
2688 get_mncc_name(msg_type), data->callref);
2689 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002690 return mncc_release_ind(net, NULL, data->callref,
2691 GSM48_CAUSE_LOC_PRN_S_LU,
2692 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002694 if (!data->called.number[0] && !data->imsi[0]) {
2695 DEBUGP(DCC, "(bts - trx - ts - ti) "
2696 "Received '%s' from MNCC with "
2697 "no number or IMSI\n", get_mncc_name(msg_type));
2698 /* Invalid number */
2699 return mncc_release_ind(net, NULL, data->callref,
2700 GSM48_CAUSE_LOC_PRN_S_LU,
2701 GSM48_CC_CAUSE_INV_NR_FORMAT);
2702 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002703 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002704 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02002705 vsub = vlr_subscr_find_by_msisdn(net->vlr,
2706 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002707 else
Harald Welte2483f1b2016-06-19 18:06:02 +02002708 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002709
2710 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002711 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002712
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02002714 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002715 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2716 "Received '%s' from MNCC with "
2717 "unknown subscriber %s\n", data->called.number,
2718 get_mncc_name(msg_type), data->called.number);
2719 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002720 return mncc_release_ind(net, NULL, data->callref,
2721 GSM48_CAUSE_LOC_PRN_S_LU,
2722 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723 }
2724 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02002725 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2727 "Received '%s' from MNCC with "
2728 "detached subscriber %s\n", data->called.number,
2729 get_mncc_name(msg_type), data->called.number);
Harald Welte2483f1b2016-06-19 18:06:02 +02002730 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002731 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002732 return mncc_release_ind(net, NULL, data->callref,
2733 GSM48_CAUSE_LOC_PRN_S_LU,
2734 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735 }
2736 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02002737 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02002738 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002740 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002742 mncc_release_ind(net, NULL, data->callref,
2743 GSM48_CAUSE_LOC_PRN_S_LU,
2744 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745 return -ENOMEM;
2746 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002747
2748 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02002749 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002750
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002751 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002752 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753 /* find transaction with this subscriber already paging */
2754 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002755 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02002757 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002758 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002759 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002760 "Received '%s' from MNCC with "
2761 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002762 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02002764 get_mncc_name(msg_type), vsub->lac);
2765 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002766 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002767 return 0;
2768 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002769 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02002770 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002771
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02002772 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002773 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02002774 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02002775 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002776 trans,
2777 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002778 if (!trans->paging_request) {
2779 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002780 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002781 trans_free(trans);
2782 return 0;
2783 }
Harald Welte2483f1b2016-06-19 18:06:02 +02002784 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002785 return 0;
2786 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002787
2788 /* Assign conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02002789 trans->conn = msc_subscr_conn_get(conn);
2790 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002791 } else {
2792 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002793 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002795
2796 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002797 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798
2799 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002800 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002801 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002802 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02002803 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002805 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2806 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807 if (msg_type == MNCC_REL_REQ)
2808 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2809 else
2810 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2811 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002812 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002813 return rc;
2814 }
2815
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002816 DEBUGP(DCC, "(ti %02x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817 "Received '%s' from MNCC in state %d (%s)\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02002819 vlr_subscr_msisdn_or_name(trans->conn->vsub),
Harald Weltedcaf5652009-07-23 18:56:43 +02002820 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08002821 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822
2823 /* Find function for current state and message */
2824 for (i = 0; i < DOWNSLLEN; i++)
2825 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002826 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 break;
2828 if (i == DOWNSLLEN) {
2829 DEBUGP(DCC, "Message unhandled at this state.\n");
2830 return 0;
2831 }
2832
2833 rc = downstatelist[i].rout(trans, arg);
2834
2835 return rc;
2836}
2837
2838
2839static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002840 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 int type;
2842 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2843} datastatelist[] = {
2844 /* mobile originating call establishment */
2845 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2846 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2847 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2848 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2849 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2850 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2851 /* mobile terminating call establishment */
2852 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2853 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2854 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2855 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002856 {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 +08002857 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2858 /* signalling during call */
2859 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2860 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2861 {SBIT(GSM_CSTATE_ACTIVE),
2862 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2863 {ALL_STATES,
2864 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2865 {ALL_STATES,
2866 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2867 {ALL_STATES,
2868 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2869 {SBIT(GSM_CSTATE_ACTIVE),
2870 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2871 {SBIT(GSM_CSTATE_ACTIVE),
2872 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2873 {SBIT(GSM_CSTATE_ACTIVE),
2874 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2875 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2876 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2877 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2878 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2879 {SBIT(GSM_CSTATE_ACTIVE),
2880 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2881 /* clearing */
2882 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2883 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2884 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2885 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2886 {ALL_STATES, /* 5.4.3.4 */
2887 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2888};
2889
2890#define DATASLLEN \
2891 (sizeof(datastatelist) / sizeof(struct datastate))
2892
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08002893static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00002894{
2895 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01002896 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01002897 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02002898 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002899 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002900
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901 if (msg_type & 0x80) {
2902 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2903 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002904 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002905
Harald Welte2483f1b2016-06-19 18:06:02 +02002906 if (!conn->vsub) {
2907 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01002908 return -EINVAL;
2909 }
2910
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002912 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002913
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002914#if BEFORE_MSCSPLIT
2915 /* Re-enable this log output once we can obtain this information via
2916 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02002917 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002918 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08002919 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02002920 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08002921 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2922 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002923#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924
2925 /* Create transaction */
2926 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002927 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002928 "creating new trans.\n", transaction_id);
2929 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02002930 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01002931 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02002932 transaction_id, new_callref++);
2933 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002935 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002936 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002937 GSM48_MT_CC_RELEASE_COMPL);
2938 return -ENOMEM;
2939 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 /* Assign transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02002941 trans->conn = msc_subscr_conn_get(conn);
2942 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943 }
2944
2945 /* find function for current state and message */
2946 for (i = 0; i < DATASLLEN; i++)
2947 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002948 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 break;
2950 if (i == DATASLLEN) {
2951 DEBUGP(DCC, "Message unhandled at this state.\n");
2952 return 0;
2953 }
2954
Harald Welte2483f1b2016-06-19 18:06:02 +02002955 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002956
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002958
Harald Welte2483f1b2016-06-19 18:06:02 +02002959 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00002960 return rc;
2961}
2962
Harald Welte2483f1b2016-06-19 18:06:02 +02002963static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08002964{
Harald Welte2483f1b2016-06-19 18:06:02 +02002965 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
2966 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08002967
Harald Welte2483f1b2016-06-19 18:06:02 +02002968 switch (pdisc) {
2969 case GSM48_PDISC_MM:
2970 switch (msg_type) {
2971 case GSM48_MT_MM_LOC_UPD_REQUEST:
2972 case GSM48_MT_MM_CM_SERV_REQ:
2973 case GSM48_MT_MM_AUTH_RESP:
2974 case GSM48_MT_MM_AUTH_FAIL:
2975 case GSM48_MT_MM_ID_RESP:
2976 case GSM48_MT_MM_TMSI_REALL_COMPL:
2977 case GSM48_MT_MM_IMSI_DETACH_IND:
2978 return true;
2979 default:
2980 break;
2981 }
2982 break;
2983 case GSM48_PDISC_RR:
2984 switch (msg_type) {
2985 case GSM48_MT_RR_CIPH_M_COMPL:
2986 case GSM48_MT_RR_PAG_RESP:
2987 return true;
2988 default:
2989 break;
2990 }
2991 break;
2992 default:
2993 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02002994 }
2995
Harald Welte2483f1b2016-06-19 18:06:02 +02002996 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02002997}
2998
Harald Welte2483f1b2016-06-19 18:06:02 +02002999void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3000 struct msgb *msg)
3001{
3002
3003 /* If a CM Service Request was received before, this is the request the
3004 * conn was opened for. No need to wait for further messages. */
3005
3006 if (!conn->received_cm_service_request)
3007 return;
3008
3009 if (log_check_level(DMM, LOGL_DEBUG)) {
3010 struct gsm48_hdr *gh = msgb_l3(msg);
3011 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3012 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3013
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003014 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003015 " received_cm_service_request changes to false\n",
3016 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003017 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003018 }
3019 conn->received_cm_service_request = false;
3020}
3021
3022
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003023/* 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 +08003024int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003025{
3026 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003027 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003028 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003029
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003030 OSMO_ASSERT(conn);
3031 OSMO_ASSERT(msg);
3032
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003033 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3034 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3035 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003036
3037 if (!msc_subscr_conn_is_accepted(conn)
3038 && !msg_is_initially_permitted(gh)) {
3039 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003040 "subscr %s: Message not permitted for initial conn: %s\n",
3041 vlr_subscr_name(conn->vsub),
3042 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003043 return -EACCES;
3044 }
3045
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003046 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3047 LOGP(DMM, LOGL_ERROR,
3048 "%s: Illegal situation: RAN type mismatch:"
3049 " attached via %s, received message via %s\n",
3050 vlr_subscr_name(conn->vsub),
3051 ran_type_name(conn->vsub->cs.attached_via_ran),
3052 ran_type_name(conn->via_ran));
3053 return -EACCES;
3054 }
3055
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003056#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003057 if (silent_call_reroute(conn, msg))
3058 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003059#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003060
Harald Welte52b1f982008-12-23 20:25:15 +00003061 switch (pdisc) {
3062 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003063 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003064 break;
3065 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003066 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003067 break;
3068 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003069 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003070 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003071 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003072 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003073 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003074 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003075 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003076 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3077 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003078 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003079 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003080 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003081 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003082 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003083 case GSM48_PDISC_TEST:
3084 rc = gsm0414_rcv_test(conn, msg);
3085 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003086 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003087 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3088 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003089 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003090 break;
3091 }
3092
3093 return rc;
3094}
Harald Welte8470bf22008-12-25 23:28:35 +00003095
Harald Welte2483f1b2016-06-19 18:06:02 +02003096/***********************************************************************
3097 * VLR integration
3098 ***********************************************************************/
3099
3100/* VLR asks us to send an authentication request */
3101static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3102 bool send_autn)
3103{
3104 struct gsm_subscriber_connection *conn = msc_conn_ref;
3105 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3106 send_autn? at->vec.autn : NULL,
3107 at->key_seq);
3108}
3109
3110/* VLR asks us to send an authentication reject */
3111static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3112{
3113 struct gsm_subscriber_connection *conn = msc_conn_ref;
3114 return gsm48_tx_mm_auth_rej(conn);
3115}
3116
3117/* VLR asks us to transmit an Identity Request of given type */
3118static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3119{
3120 struct gsm_subscriber_connection *conn = msc_conn_ref;
3121 return mm_tx_identity_req(conn, mi_type);
3122}
3123
3124/* VLR asks us to transmit a Location Update Accept */
3125static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3126{
3127 struct gsm_subscriber_connection *conn = msc_conn_ref;
3128 return gsm0408_loc_upd_acc(conn, send_tmsi);
3129}
3130
3131/* VLR asks us to transmit a Location Update Reject */
3132static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3133{
3134 struct gsm_subscriber_connection *conn = msc_conn_ref;
3135 return gsm0408_loc_upd_rej(conn, cause);
3136}
3137
3138/* VLR asks us to transmit a CM Service Accept */
3139static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3140{
3141 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003142 return msc_gsm48_tx_mm_serv_ack(conn);
3143}
3144
3145static int msc_vlr_tx_common_id(void *msc_conn_ref)
3146{
3147 struct gsm_subscriber_connection *conn = msc_conn_ref;
3148 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003149}
3150
3151/* VLR asks us to transmit a CM Service Reject */
3152static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3153{
3154 uint8_t cause;
3155 struct gsm_subscriber_connection *conn = msc_conn_ref;
3156 conn->received_cm_service_request = false;
3157
3158 switch (result) {
3159 default:
3160 case VLR_PR_ARQ_RES_NONE:
3161 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3162 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3163 cause = GSM48_REJECT_NETWORK_FAILURE;
3164 break;
3165 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3166 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3167 break;
3168 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3169 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3170 break;
3171 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3172 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3173 break;
3174 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3175 cause = GSM48_REJECT_ILLEGAL_MS;
3176 break;
3177 case VLR_PR_ARQ_RES_TIMEOUT:
3178 cause = GSM48_REJECT_CONGESTION;
3179 break;
3180 };
3181
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003182 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003183}
3184
3185/* VLR asks us to start using ciphering */
3186static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3187 enum vlr_ciph ciph,
3188 bool retrieve_imeisv)
3189{
3190 struct gsm_subscriber_connection *conn = msc_conn_ref;
3191 struct vlr_subscr *vsub;
3192 struct gsm_auth_tuple *tuple;
3193
3194 if (!conn || !conn->vsub) {
3195 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3196 " NULL conn/subscriber");
3197 return -EINVAL;
3198 }
3199
3200 vsub = conn->vsub;
3201 tuple = vsub->last_tuple;
3202
3203 if (!tuple) {
3204 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3205 " Command: no auth tuple available\n",
3206 vlr_subscr_name(vsub));
3207 return -EINVAL;
3208 }
3209
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003210 switch (conn->via_ran) {
3211 case RAN_GERAN_A:
3212 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3213 vlr_subscr_name(conn->vsub));
3214 return msc_gsm0808_tx_cipher_mode(conn, ciph, tuple->vec.kc, 8,
3215 retrieve_imeisv);
3216 case RAN_UTRAN_IU:
3217#ifdef BUILD_IU
3218 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3219 vlr_subscr_name(conn->vsub));
3220 return iu_tx_sec_mode_cmd(conn->iu.ue_ctx, tuple, 0, 1);
3221#else
3222 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3223 " built without Iu support\n");
3224 return -ENOTSUP;
3225#endif
3226
3227 default:
3228 break;
3229 }
3230 LOGP(DMM, LOGL_ERROR,
3231 "%s: cannot start ciphering, unknown RAN type %d\n",
3232 vlr_subscr_name(conn->vsub), conn->via_ran);
3233 return -ENOTSUP;
3234}
3235
3236void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3237{
3238 struct vlr_ciph_result vlr_res = {};
3239
3240 if (!conn || !conn->vsub) {
3241 LOGP(DMM, LOGL_ERROR,
3242 "Rx Security Mode Complete for invalid conn\n");
3243 return;
3244 }
3245
3246 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3247 vlr_subscr_name(conn->vsub));
3248
3249 vlr_res.cause = VLR_CIPH_COMPL;
3250 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003251}
3252
3253/* VLR informs us that the subscriber data has somehow been modified */
3254static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3255{
3256 /* FIXME */
3257}
3258
3259/* VLR informs us that the subscriber has been associated with a conn */
3260static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3261 struct vlr_subscr *vsub)
3262{
3263 struct gsm_subscriber_connection *conn = msc_conn_ref;
3264 OSMO_ASSERT(!conn->vsub);
3265 conn->vsub = vlr_subscr_get(vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003266 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003267}
3268
3269/* operations that we need to implement for libvlr */
3270static const struct vlr_ops msc_vlr_ops = {
3271 .tx_auth_req = msc_vlr_tx_auth_req,
3272 .tx_auth_rej = msc_vlr_tx_auth_rej,
3273 .tx_id_req = msc_vlr_tx_id_req,
3274 .tx_lu_acc = msc_vlr_tx_lu_acc,
3275 .tx_lu_rej = msc_vlr_tx_lu_rej,
3276 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3277 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3278 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003279 .tx_common_id = msc_vlr_tx_common_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003280 .subscr_update = msc_vlr_subscr_update,
3281 .subscr_assoc = msc_vlr_subscr_assoc,
3282};
3283
3284/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3285int msc_vlr_alloc(struct gsm_network *net)
3286{
3287 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3288 if (!net->vlr)
3289 return -ENOMEM;
3290 net->vlr->user_ctx = net;
3291 return 0;
3292}
3293
3294/* Launch the VLR, i.e. its GSUP connection */
3295int msc_vlr_start(struct gsm_network *net)
3296{
3297 OSMO_ASSERT(net->vlr);
3298 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3299 net->gsup_server_port);
3300}