blob: 58c5610585aa94f16874e48ee675b932aa3a2fb2 [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 Welte7b423ed2016-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 Hofmeyra1756f32016-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 Welte8470bf22008-12-25 23:28:35 +000046#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000047#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000048#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000049#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020050#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000051#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020052#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020053#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020054#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010055#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080056#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080057#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010058#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020059#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020060#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010061#include <osmocom/core/bitvec.h>
Harald Welte7b423ed2016-06-19 18:06:02 +020062#include <openbsc/vlr.h>
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +020063#include <openbsc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080064
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010065#include <osmocom/gsm/gsm48.h>
66#include <osmocom/gsm/gsm0480.h>
67#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020068#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010069#include <osmocom/core/msgb.h>
70#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010071#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010072#include <osmocom/gsm/tlv.h>
Neels Hofmeyra1756f32016-05-20 21:59:55 +020073#include <osmocom/crypt/auth.h>
74
75#include <openbsc/msc_ifaces.h>
76
77#ifdef BUILD_IU
78#include <openbsc/iu.h>
79#endif
Harald Welte52b1f982008-12-23 20:25:15 +000080
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020081#include <assert.h>
82
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +020083
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020084void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010085void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020086
Harald Welte7b423ed2016-06-19 18:06:02 +020087static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
88 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080089static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020090 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000091
Harald Welte52b1f982008-12-23 20:25:15 +000092struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020093 uint16_t mcc;
94 uint16_t mnc;
95 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000096};
97
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020098static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080099
Harald Welte31c00f72011-03-03 23:29:05 +0100100void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
101{
102 net->mncc_recv(net, msg);
103}
104
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800105static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
106 struct gsm_trans *trans)
107{
108 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
109
110 /* if we get passed a transaction reference, do some common
111 * work that the caller no longer has to do */
112 if (trans) {
113 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800114 }
115
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200116 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800117}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100118
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800119int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
120{
121 struct gsm48_hdr *gh;
122 struct msgb *ss_notify;
123
124 ss_notify = gsm0480_create_notifySS(message);
125 if (!ss_notify)
126 return -1;
127
128 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
129 uint8_t *data = msgb_push(ss_notify, 1);
130 data[0] = ss_notify->len - 1;
131 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
132 gh->msg_type = GSM48_MT_CC_FACILITY;
133 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
134}
135
Harald Welte7b423ed2016-06-19 18:06:02 +0200136/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100137void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
138{
139 struct gsm_trans *trans, *temp;
140
141 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
142
143 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100144 if (trans->protocol == protocol) {
145 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100146 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100147 }
Harald Welte371efe52010-12-22 23:17:50 +0100148 }
149}
150
Holger Freyther429e7762008-12-30 13:28:30 +0000151/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200152static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000153{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800154 struct msgb *msg;
155
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800156 msg = gsm48_create_loc_upd_rej(cause);
157 if (!msg) {
158 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
159 return -1;
160 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200161
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +0200162 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
163 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100164
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800165 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000166}
167
168/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte7b423ed2016-06-19 18:06:02 +0200169static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
170 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000171{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100172 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000173 struct gsm48_hdr *gh;
174 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200175 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200176
Harald Welte52b1f982008-12-23 20:25:15 +0000177 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
178 gh->proto_discr = GSM48_PDISC_MM;
179 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
180
181 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200182 gsm48_generate_lai(lai, conn->network->country_code,
183 conn->network->network_code,
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +0200184 conn->lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000185
Harald Welte7b423ed2016-06-19 18:06:02 +0200186 if (send_tmsi == GSM_RESERVED_TMSI) {
187 /* we did not allocate a TMSI to the MS, so we need to
188 * include the IMSI in order for the MS to delete any
189 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200190 uint8_t mi[10];
191 int len;
Harald Welte7b423ed2016-06-19 18:06:02 +0200192 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200193 mid = msgb_put(msg, len);
194 memcpy(mid, mi, len);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200195 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
196 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200197 } else {
Harald Welte7b423ed2016-06-19 18:06:02 +0200198 /* Include the TMSI, which means that the MS will send a
199 * TMSI REALLOCATION COMPLETE, and we should wait for
200 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200201 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte7b423ed2016-06-19 18:06:02 +0200202 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200203 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
204 vlr_subscr_name(conn->vsub),
205 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200206 }
Harald Welte7b423ed2016-06-19 18:06:02 +0200207 /* TODO: Follow-on proceed */
208 /* TODO: CTS permission */
209 /* TODO: Equivalent PLMNs */
210 /* TODO: Emergency Number List */
211 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000212
Harald Welte52b1f982008-12-23 20:25:15 +0000213
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800214 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000215}
216
Harald Weltebf5e8df2009-02-03 12:59:45 +0000217/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200218static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000219{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100220 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000221 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000222
Harald Welte231ad4f2008-12-27 11:15:38 +0000223 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
224 gh->proto_discr = GSM48_PDISC_MM;
225 gh->msg_type = GSM48_MT_MM_ID_REQ;
226 gh->data[0] = id_type;
227
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800228 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000229}
230
Harald Weltebf5e8df2009-02-03 12:59:45 +0000231/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800232static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000233{
234 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200235 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200236 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000237
Harald Welte7b423ed2016-06-19 18:06:02 +0200238 if (!conn->vsub) {
239 LOGP(DMM, LOGL_ERROR,
240 "Rx MM Identity Response: invalid: no subscriber\n");
241 return -EINVAL;
242 }
243
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200244 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200245 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
246 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000247
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200248 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100249
Harald Welte7b423ed2016-06-19 18:06:02 +0200250 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000251}
252
Harald Welte7b423ed2016-06-19 18:06:02 +0200253/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200254static const struct value_string lupd_names[] = {
255 { GSM48_LUPD_NORMAL, "NORMAL" },
256 { GSM48_LUPD_PERIODIC, "PERIODIC" },
257 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
258 { 0, NULL }
259};
Harald Welte2a139372009-02-22 21:14:55 +0000260
Harald Welte7b423ed2016-06-19 18:06:02 +0200261/* Chapter 9.2.15: Receive Location Updating Request.
262 * Keep this function non-static for direct invocation by unit tests. */
263int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000264{
Harald Welte7b423ed2016-06-19 18:06:02 +0200265 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
266 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000267 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000268 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200269 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200270 char mi_string[GSM48_MI_SIZE];
Harald Welte7b423ed2016-06-19 18:06:02 +0200271 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte7b423ed2016-06-19 18:06:02 +0200272 uint32_t tmsi;
273 char *imsi;
274 struct osmo_location_area_id old_lai, new_lai;
275 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200276 bool is_utran;
Harald Welte7b423ed2016-06-19 18:06:02 +0200277 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000278
Harald Welte8470bf22008-12-25 23:28:35 +0000279 lu = (struct gsm48_loc_upd_req *) gh->data;
280
281 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000282
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200283 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000284
Harald Welte7b423ed2016-06-19 18:06:02 +0200285 rc = msc_create_conn_fsm(conn, mi_string);
286 if (rc)
287 /* logging already happened in msc_create_conn_fsm() */
288 return rc;
289
290 conn->classmark.classmark1 = lu->classmark1;
291 conn->classmark.classmark1_set = true;
292
293 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
294 gsm48_mi_type_name(mi_type), mi_string,
295 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000296
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200297 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100298
Harald Welte24ff6ee2009-12-22 00:41:05 +0100299 switch (lu->type) {
300 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200301 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte7b423ed2016-06-19 18:06:02 +0200302 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100303 break;
304 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200305 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte7b423ed2016-06-19 18:06:02 +0200306 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100307 break;
308 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200309 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte7b423ed2016-06-19 18:06:02 +0200310 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100311 break;
312 }
313
Harald Welte7b423ed2016-06-19 18:06:02 +0200314 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
315 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
316 /* TODO: 10.5.7.8 Device properties */
317 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200318
Harald Welte52b1f982008-12-23 20:25:15 +0000319 switch (mi_type) {
320 case GSM_MI_TYPE_IMSI:
Harald Welte7b423ed2016-06-19 18:06:02 +0200321 tmsi = GSM_RESERVED_TMSI;
322 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000323 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000324 case GSM_MI_TYPE_TMSI:
Harald Welte7b423ed2016-06-19 18:06:02 +0200325 tmsi = tmsi_from_string(mi_string);
326 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000327 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200328 default:
Harald Weltea0368542009-06-27 02:58:43 +0200329 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte7b423ed2016-06-19 18:06:02 +0200330 tmsi = GSM_RESERVED_TMSI;
331 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000332 break;
333 }
334
Harald Welte7b423ed2016-06-19 18:06:02 +0200335 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
336 &old_lai.plmn.mnc, &old_lai.lac);
337 new_lai.plmn.mcc = conn->network->country_code;
338 new_lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +0200339 new_lai.lac = conn->lac;
Harald Welte7b423ed2016-06-19 18:06:02 +0200340 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200341
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200342 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte7b423ed2016-06-19 18:06:02 +0200343 lu_fsm = vlr_loc_update(conn->conn_fsm,
344 SUBSCR_CONN_E_ACCEPTED,
345 SUBSCR_CONN_E_CN_CLOSE,
346 (void*)&conn_from_lu,
347 net->vlr, conn, vlr_lu_type, tmsi, imsi,
348 &old_lai, &new_lai,
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200349 is_utran || conn->network->authentication_required,
350 is_utran? VLR_CIPH_A5_3
351 : conn->network->a5_encryption,
Harald Welte7b423ed2016-06-19 18:06:02 +0200352 classmark_is_r99(&conn->classmark),
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200353 is_utran,
Harald Welte7b423ed2016-06-19 18:06:02 +0200354 net->vlr->cfg.assign_tmsi);
355 if (!lu_fsm) {
356 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
357 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800358 }
359
Harald Welte7b423ed2016-06-19 18:06:02 +0200360 /* From vlr_loc_update() we expect an implicit dispatch of
361 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
362 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000363
Harald Welte7b423ed2016-06-19 18:06:02 +0200364 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
365 LOGP(DRR, LOGL_ERROR,
366 "%s: internal error during Location Updating attempt\n",
367 mi_string);
368 return -EIO;
369 }
370
371 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000372}
373
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100374/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte7b423ed2016-06-19 18:06:02 +0200375/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100376static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800377{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100378 uint8_t ret;
379
380 ret = value / 10;
381 ret |= (value % 10) << 4;
382
383 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800384}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100385
Harald Welte4bfdfe72009-06-10 23:11:52 +0800386
Harald Weltedb253af2008-12-30 17:56:55 +0000387/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800388int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000389{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100390 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000391 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200392 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200393 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200394 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100395
Harald Welte4bfdfe72009-06-10 23:11:52 +0800396 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100397 struct tm* gmt_time;
398 struct tm* local_time;
399 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100400 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000401
Harald Weltedb253af2008-12-30 17:56:55 +0000402 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
403 gh->proto_discr = GSM48_PDISC_MM;
404 gh->msg_type = GSM48_MT_MM_INFO;
405
406 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200407#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000408 name_len = strlen(net->name_long);
409 /* 10.5.3.5a */
410 ptr8 = msgb_put(msg, 3);
411 ptr8[0] = GSM48_IE_NAME_LONG;
412 ptr8[1] = name_len*2 +1;
413 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
414
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200415 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000416 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000417 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000418
419 /* FIXME: Use Cell Broadcast, not UCS-2, since
420 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200421#endif
422 name_len = (strlen(net->name_long)*7)/8;
423 name_pad = (8 - strlen(net->name_long)*7)%8;
424 if (name_pad > 0)
425 name_len++;
426 /* 10.5.3.5a */
427 ptr8 = msgb_put(msg, 3);
428 ptr8[0] = GSM48_IE_NAME_LONG;
429 ptr8[1] = name_len +1;
430 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
431
432 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100433 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200434
Harald Weltedb253af2008-12-30 17:56:55 +0000435 }
436
437 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200438#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000439 name_len = strlen(net->name_short);
440 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200441 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200442 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000443 ptr8[1] = name_len*2 + 1;
444 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
445
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200446 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000447 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000448 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200449#endif
450 name_len = (strlen(net->name_short)*7)/8;
451 name_pad = (8 - strlen(net->name_short)*7)%8;
452 if (name_pad > 0)
453 name_len++;
454 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200455 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200456 ptr8[0] = GSM48_IE_NAME_SHORT;
457 ptr8[1] = name_len +1;
458 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
459
460 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100461 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200462
Harald Weltedb253af2008-12-30 17:56:55 +0000463 }
464
Harald Weltedb253af2008-12-30 17:56:55 +0000465 /* Section 10.5.3.9 */
466 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100467 gmt_time = gmtime(&cur_t);
468
Harald Weltedb253af2008-12-30 17:56:55 +0000469 ptr8 = msgb_put(msg, 8);
470 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100471 ptr8[1] = bcdify(gmt_time->tm_year % 100);
472 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
473 ptr8[3] = bcdify(gmt_time->tm_mday);
474 ptr8[4] = bcdify(gmt_time->tm_hour);
475 ptr8[5] = bcdify(gmt_time->tm_min);
476 ptr8[6] = bcdify(gmt_time->tm_sec);
477
Neels Hofmeyr73983952016-05-10 13:29:33 +0200478 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200479 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200480 if (net->tz.hr < 0) {
481 tzunits = ((net->tz.hr/-1)*4);
482 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100483 ptr8[7] = bcdify(tzunits);
484 /* Set negative time */
485 ptr8[7] |= 0x08;
486 }
487 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200488 tzunits = net->tz.hr*4;
489 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100490 ptr8[7] = bcdify(tzunits);
491 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100492 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200493 if (net->tz.dst >= 0 && net->tz.dst <= 2)
494 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100495 }
496 else {
497 /* Need to get GSM offset and convert into 15 min units */
498 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200499#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolseee44dbcf2017-07-05 12:57:16 +0200500 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100501 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200502#else
Benoit Bolseee44dbcf2017-07-05 12:57:16 +0200503 /* find timezone offset */
504 time_t utc;
505 double offsetFromUTC;
506 utc = mktime(gmt_time);
507 local_time = localtime(&cur_t);
508 offsetFromUTC = difftime(cur_t, utc);
509 if (local_time->tm_isdst)
510 offsetFromUTC += 3600.0;
511 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200512#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100513 if (tzunits < 0) {
514 tzunits = tzunits/-1;
515 ptr8[7] = bcdify(tzunits);
516 /* Flip it to negative */
517 ptr8[7] |= 0x08;
518 }
519 else
520 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100521
522 /* Does not support DST +2 */
523 if (local_time->tm_isdst)
524 dst = 1;
525 }
526
527 if (dst) {
528 ptr8 = msgb_put(msg, 3);
529 ptr8[0] = GSM48_IE_NET_DST;
530 ptr8[1] = 1;
531 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100532 }
Harald Weltedb253af2008-12-30 17:56:55 +0000533
Daniel Willmanneea93372009-08-13 03:42:07 +0200534 DEBUGP(DMM, "-> MM INFO\n");
535
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800536 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000537}
538
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100539/*! Send an Authentication Request to MS on the given subscriber connection
540 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
541 * \param[in] conn Subscriber connection to send on.
542 * \param[in] rand Random challenge token to send, must be 16 bytes long.
543 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
544 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
545 * \param[in] key_seq auth tuple's sequence number.
546 */
547int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
548 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200549{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100550 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200551 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200552 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200553
Neels Hofmeyr39a7c752017-02-14 17:42:25 +0100554 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100555 if (autn)
Neels Hofmeyr39a7c752017-02-14 17:42:25 +0100556 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200557
Harald Welte7984d5c2009-08-12 22:56:50 +0200558 gh->proto_discr = GSM48_PDISC_MM;
559 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
560
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100561 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200562
Harald Welte7984d5c2009-08-12 22:56:50 +0200563 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100564 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200565 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200566 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200567
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100568
569 /* 16 bytes AUTN */
570 if (autn)
571 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
572
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800573 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200574}
575
576/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800577int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200578{
579 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800580 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200581}
582
Harald Welte7b423ed2016-06-19 18:06:02 +0200583static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
584static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
585
586static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100587{
Harald Welte7b423ed2016-06-19 18:06:02 +0200588 uint8_t mi_type;
589 char mi_string[GSM48_MI_SIZE];
590 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100591
Harald Welte7b423ed2016-06-19 18:06:02 +0200592 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
593 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100594
Harald Welte7b423ed2016-06-19 18:06:02 +0200595 switch (mi_type) {
596 case GSM_MI_TYPE_IMSI:
597 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
598 goto accept_reuse;
599 break;
600 case GSM_MI_TYPE_TMSI:
601 tmsi = osmo_load32be(mi_lv+2);
602 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
603 goto accept_reuse;
604 break;
605 case GSM_MI_TYPE_IMEI:
606 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
607 goto accept_reuse;
608 break;
609 default:
610 break;
611 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100612
Harald Welte7b423ed2016-06-19 18:06:02 +0200613 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
614 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
615 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
616 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100617
Harald Welte7b423ed2016-06-19 18:06:02 +0200618accept_reuse:
619 DEBUGP(DMM, "%s: re-using already accepted connection\n",
620 vlr_subscr_name(conn->vsub));
621 conn->received_cm_service_request = true;
622 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100623}
624
Harald Welte4ed0e922009-01-10 03:17:30 +0000625/*
626 * Handle CM Service Requests
627 * a) Verify that the packet is long enough to contain the information
628 * we require otherwsie reject with INCORRECT_MESSAGE
629 * b) Try to parse the TMSI. If we do not have one reject
630 * c) Check that we know the subscriber with the TMSI otherwise reject
631 * with a HLR cause
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +0200632 * d) Set the subscriber on the conn and accept
Harald Welte7b423ed2016-06-19 18:06:02 +0200633 *
634 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000635 */
Harald Welte7b423ed2016-06-19 18:06:02 +0200636int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000637{
Harald Welte7b423ed2016-06-19 18:06:02 +0200638 static const enum subscr_conn_from conn_from_cm_service_req =
639 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
640 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200641 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200642 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000643
Harald Welteba4cf162009-01-10 01:49:35 +0000644 struct gsm48_hdr *gh = msgb_l3(msg);
645 struct gsm48_service_request *req =
646 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800647 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200648 uint8_t classmark2_len = gh->data[1];
649 uint8_t *classmark2 = gh->data+2;
650 uint8_t mi_len = *(classmark2 + classmark2_len);
651 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte7b423ed2016-06-19 18:06:02 +0200652 struct osmo_location_area_id lai;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200653 bool is_utran;
Harald Welte7b423ed2016-06-19 18:06:02 +0200654 int rc;
655
656 lai.plmn.mcc = conn->network->country_code;
657 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +0200658 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000659
Harald Weltec9e02182009-05-01 19:07:53 +0000660 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000661 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000662 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +0200663 return msc_gsm48_tx_mm_serv_rej(conn,
664 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000665 }
666
667 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000668 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +0200669 return msc_gsm48_tx_mm_serv_rej(conn,
670 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000671 }
672
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200673 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000674 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200675
676 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200677 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
678 req->cm_service_type, gsm48_mi_type_name(mi_type),
679 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200680 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200681 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
682 req->cm_service_type, gsm48_mi_type_name(mi_type),
683 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200684 } else {
685 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +0200686 return msc_gsm48_tx_mm_serv_rej(conn,
687 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000688 }
689
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200690 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7b423ed2016-06-19 18:06:02 +0200691 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
692 conn->classmark.classmark2_len = classmark2_len;
693
694 if (conn->conn_fsm) {
695 if (msc_subscr_conn_is_accepted(conn))
696 return cm_serv_reuse_conn(conn, mi-1);
697 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
698 vlr_subscr_name(conn->vsub));
699 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
700 return -EINVAL;
701 }
702
703 rc = msc_create_conn_fsm(conn, mi_string);
704 if (rc) {
705 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
706 /* logging already happened in msc_create_conn_fsm() */
707 return rc;
708 }
Harald Welte7659de12009-12-13 12:39:18 +0100709
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200710 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte7b423ed2016-06-19 18:06:02 +0200711 vlr_proc_acc_req(conn->conn_fsm,
712 SUBSCR_CONN_E_ACCEPTED,
713 SUBSCR_CONN_E_CN_CLOSE,
714 (void*)&conn_from_cm_service_req,
715 net->vlr, conn,
716 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200717 is_utran || conn->network->authentication_required,
718 is_utran? VLR_CIPH_A5_3
719 : conn->network->a5_encryption,
Harald Welte7b423ed2016-06-19 18:06:02 +0200720 classmark_is_r99(&conn->classmark),
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200721 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000722
Harald Welte7b423ed2016-06-19 18:06:02 +0200723 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000724}
725
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200726static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000727{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200728 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000729 struct gsm48_hdr *gh = msgb_l3(msg);
730 struct gsm48_imsi_detach_ind *idi =
731 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200732 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200733 char mi_string[GSM48_MI_SIZE];
Harald Welte7b423ed2016-06-19 18:06:02 +0200734 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000735
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200736 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte7b423ed2016-06-19 18:06:02 +0200737 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
738 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000739
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200740 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100741
Harald Welte2a139372009-02-22 21:14:55 +0000742 switch (mi_type) {
743 case GSM_MI_TYPE_TMSI:
Harald Welte7b423ed2016-06-19 18:06:02 +0200744 vsub = vlr_subscr_find_by_tmsi(network->vlr,
745 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000746 break;
747 case GSM_MI_TYPE_IMSI:
Harald Welte7b423ed2016-06-19 18:06:02 +0200748 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000749 break;
750 case GSM_MI_TYPE_IMEI:
751 case GSM_MI_TYPE_IMEISV:
752 /* no sim card... FIXME: what to do ? */
Harald Welte7b423ed2016-06-19 18:06:02 +0200753 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
754 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000755 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200756 default:
Harald Welte7b423ed2016-06-19 18:06:02 +0200757 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
758 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000759 break;
760 }
761
Harald Welte7b423ed2016-06-19 18:06:02 +0200762 /* TODO? We used to remember the subscriber's classmark1 here and
763 * stored it in the old sqlite db, but now we store it in a conn that
764 * will be discarded anyway: */
765 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200766
Harald Welte7b423ed2016-06-19 18:06:02 +0200767 if (!vsub) {
768 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
769 gsm48_mi_type_name(mi_type), mi_string);
770 } else {
771 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
772 vlr_subscr_rx_imsi_detach(vsub);
773 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
774 vlr_subscr_put(vsub);
775 }
Harald Welte2a139372009-02-22 21:14:55 +0000776
Harald Welte7b423ed2016-06-19 18:06:02 +0200777 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000778 return 0;
779}
780
Harald Welted2a7f5a2009-06-05 20:08:20 +0000781static int gsm48_rx_mm_status(struct msgb *msg)
782{
783 struct gsm48_hdr *gh = msgb_l3(msg);
784
785 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
786
787 return 0;
788}
789
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100790static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
791 struct gsm_subscriber_connection *conn,
792 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100793{
794 struct gsm48_hdr *gh = msgb_l3(msg);
795 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100796
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100797 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
798 LOGP(DMM, LOGL_ERROR,
799 "%s: MM AUTHENTICATION RESPONSE:"
800 " l3 length invalid: %u\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200801 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100802 return -EINVAL;
803 }
804
805 *res_len = sizeof(ar->sres);
806 memcpy(res, ar->sres, sizeof(ar->sres));
807 return 0;
808}
809
810static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
811 struct gsm_subscriber_connection *conn,
812 struct msgb *msg)
813{
814 struct gsm48_hdr *gh;
815 uint8_t *data;
816 uint8_t iei;
817 uint8_t ie_len;
818 unsigned int data_len;
819
820 /* First parse the GSM part */
821 if (parse_gsm_auth_resp(res, res_len, conn, msg))
822 return -EINVAL;
823 OSMO_ASSERT(*res_len == 4);
824
825 /* Then add the extended res part */
826 gh = msgb_l3(msg);
827 data = gh->data + sizeof(struct gsm48_auth_resp);
828 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
829
830 if (data_len < 3) {
831 LOGP(DMM, LOGL_ERROR,
832 "%s: MM AUTHENTICATION RESPONSE:"
833 " l3 length invalid: %u\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200834 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100835 return -EINVAL;
836 }
837
838 iei = data[0];
839 ie_len = data[1];
840 if (iei != GSM48_IE_AUTH_RES_EXT) {
841 LOGP(DMM, LOGL_ERROR,
842 "%s: MM R99 AUTHENTICATION RESPONSE:"
843 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200844 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100845 GSM48_IE_AUTH_RES_EXT, iei);
846 return -EINVAL;
847 }
848
849 if (ie_len > 12) {
850 LOGP(DMM, LOGL_ERROR,
851 "%s: MM R99 AUTHENTICATION RESPONSE:"
852 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200853 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100854 return -EINVAL;
855 }
856
857 *res_len += ie_len;
858 memcpy(res + 4, &data[2], ie_len);
859 return 0;
860}
861
862/* Chapter 9.2.3: Authentication Response */
863static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
864{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100865 uint8_t res[16];
866 uint8_t res_len;
867 int rc;
868 bool is_r99;
869
Harald Welte7b423ed2016-06-19 18:06:02 +0200870 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100871 LOGP(DMM, LOGL_ERROR,
872 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte7b423ed2016-06-19 18:06:02 +0200873 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100874 return -EINVAL;
875 }
876
877 if (msgb_l3len(msg) >
878 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
879 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
880 is_r99 = true;
881 } else {
882 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
883 is_r99 = false;
884 }
885
886 if (rc) {
Harald Welte7b423ed2016-06-19 18:06:02 +0200887 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100888 return -EINVAL;
889 }
890
891 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200892 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100893 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
894 osmo_hexdump_nospc(res, res_len));
895
Harald Welte7b423ed2016-06-19 18:06:02 +0200896 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
897 conn->via_ran == RAN_UTRAN_IU,
898 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100899}
900
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100901static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
902{
903 struct gsm48_hdr *gh = msgb_l3(msg);
904 uint8_t cause;
905 uint8_t auts_tag;
906 uint8_t auts_len;
907 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100908
Harald Welte7b423ed2016-06-19 18:06:02 +0200909 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100910 LOGP(DMM, LOGL_ERROR,
911 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte7b423ed2016-06-19 18:06:02 +0200912 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100913 return -EINVAL;
914 }
915
916 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
917 LOGP(DMM, LOGL_ERROR,
918 "%s: MM R99 AUTHENTICATION FAILURE:"
919 " l3 length invalid: %u\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200920 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
921 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100922 return -EINVAL;
923 }
924
925 cause = gh->data[0];
926
927 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
928 LOGP(DMM, LOGL_INFO,
929 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200930 vlr_subscr_name(conn->vsub), cause);
931 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
932 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100933 }
934
935 /* This is a Synch Failure procedure, which should pass an AUTS to
936 * resynchronize the sequence nr with the HLR. Expecting exactly one
937 * TLV with 14 bytes of AUTS. */
938
939 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
940 LOGP(DMM, LOGL_INFO,
941 "%s: MM R99 AUTHENTICATION FAILURE:"
942 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200943 vlr_subscr_name(conn->vsub));
944 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100945 return -EINVAL;
946 }
947
948 auts_tag = gh->data[1];
949 auts_len = gh->data[2];
950 auts = &gh->data[3];
951
952 if (auts_tag != GSM48_IE_AUTS
953 || auts_len != 14) {
954 LOGP(DMM, LOGL_INFO,
955 "%s: MM R99 AUTHENTICATION FAILURE:"
956 " invalid Synch Failure:"
957 " expected AUTS IE 0x%02x of 14 bytes,"
958 " got IE 0x%02x of %u bytes\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200959 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100960 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte7b423ed2016-06-19 18:06:02 +0200961 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100962 return -EINVAL;
963 }
964
965 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
966 LOGP(DMM, LOGL_INFO,
967 "%s: MM R99 AUTHENTICATION FAILURE:"
968 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200969 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
970 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100971 return -EINVAL;
972 }
973
974 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
975 * large enough. */
976
977 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte7b423ed2016-06-19 18:06:02 +0200978 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100979
Harald Welte7b423ed2016-06-19 18:06:02 +0200980 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
981}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100982
Harald Welte7b423ed2016-06-19 18:06:02 +0200983static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
984{
985 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
986 vlr_subscr_name(conn->vsub));
987 if (!conn->vsub) {
988 LOGP(DMM, LOGL_ERROR,
989 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
990 return -EINVAL;
991 }
992 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100993}
994
Harald Weltebf5e8df2009-02-03 12:59:45 +0000995/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800996static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000997{
998 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800999 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001000
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001001 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001002 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001003 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001004 break;
1005 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001006 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001007 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001008 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001009 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001010 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001011 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001012 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001013 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001014 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte7b423ed2016-06-19 18:06:02 +02001015 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001016 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001017 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001018 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001019 break;
1020 case GSM48_MT_MM_CM_REEST_REQ:
1021 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1022 break;
1023 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001024 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001025 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001026 case GSM48_MT_MM_AUTH_FAIL:
1027 rc = gsm48_rx_mm_auth_fail(conn, msg);
1028 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001029 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001030 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001031 gh->msg_type);
1032 break;
1033 }
1034
1035 return rc;
1036}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001037
Harald Welte7b423ed2016-06-19 18:06:02 +02001038static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1039{
1040 /* Check the size for the classmark */
1041 if (tot_len < 1 + *classmark2_lv)
1042 return NULL;
1043
1044 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1045 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1046 return NULL;
1047
1048 return mi_lv;
1049}
1050
Harald Welte2d35ae62009-02-06 12:02:13 +00001051/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001052static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001053{
Harald Welte7b423ed2016-06-19 18:06:02 +02001054 static const enum subscr_conn_from conn_from_paging_resp =
1055 SUBSCR_CONN_FROM_PAGING_RESP;
1056 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001057 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001058 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001059 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte7b423ed2016-06-19 18:06:02 +02001060 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001061 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001062 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001063 int rc = 0;
Harald Welte7b423ed2016-06-19 18:06:02 +02001064 struct osmo_location_area_id lai;
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001065 bool is_utran;
Harald Welte7b423ed2016-06-19 18:06:02 +02001066
1067 lai.plmn.mcc = conn->network->country_code;
1068 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02001069 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001070
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001071 resp = (struct gsm48_pag_resp *) &gh->data[0];
1072 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1073 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001074 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1075 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001076
Harald Welte7b423ed2016-06-19 18:06:02 +02001077 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1078 if (!mi_lv) {
1079 /* FIXME */
1080 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001081 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001082
Harald Welte7b423ed2016-06-19 18:06:02 +02001083 rc = msc_create_conn_fsm(conn, mi_string);
1084 if (rc)
1085 /* logging already happened in msc_create_conn_fsm() */
1086 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001087
Harald Welte7b423ed2016-06-19 18:06:02 +02001088 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1089 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001090
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001091 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte7b423ed2016-06-19 18:06:02 +02001092 vlr_proc_acc_req(conn->conn_fsm,
1093 SUBSCR_CONN_E_ACCEPTED,
1094 SUBSCR_CONN_E_CN_CLOSE,
1095 (void*)&conn_from_paging_resp,
1096 net->vlr, conn,
1097 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001098 is_utran || conn->network->authentication_required,
1099 is_utran? VLR_CIPH_A5_3
1100 : conn->network->a5_encryption,
Harald Welte7b423ed2016-06-19 18:06:02 +02001101 classmark_is_r99(&conn->classmark),
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001102 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001103
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001104 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001105}
1106
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001107static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001108{
1109 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001110 uint8_t apdu_id_flags;
1111 uint8_t apdu_len;
1112 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001113
1114 apdu_id_flags = gh->data[0];
1115 apdu_len = gh->data[1];
1116 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001117
Max8dc8f232017-02-09 19:13:02 +01001118 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001119 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001120
Harald Welte7b423ed2016-06-19 18:06:02 +02001121 /* we're not using the app info blob anywhere, so ignore. */
1122#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001123 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte7b423ed2016-06-19 18:06:02 +02001124#else
1125 return 0;
1126#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001127}
1128
Harald Weltebf5e8df2009-02-03 12:59:45 +00001129/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001130static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001131{
1132 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001133 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001134
1135 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001136 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001137 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001138 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001139 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001140 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001141 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001142 default:
Philippd37981e2016-10-10 15:11:41 +02001143 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1144 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001145 break;
1146 }
1147
Harald Welte2d35ae62009-02-06 12:02:13 +00001148 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001149}
1150
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001151int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1152 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001153{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001154 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001155 struct gsm48_hdr *gh;
1156
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001157 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1158 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001159
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001160 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1161 gh->proto_discr = GSM48_PDISC_RR;
1162 gh->msg_type = GSM48_MT_RR_APP_INFO;
1163 gh->data[0] = apdu_id;
1164 gh->data[1] = apdu_len;
1165 memcpy(gh->data+2, apdu, apdu_len);
1166
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001167 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001168}
1169
Alexander Couzens92f552f2016-08-23 07:32:27 +02001170/* FIXME: this count_statistics is a state machine behaviour. we should convert
1171 * the complete call control into a state machine. Afterwards we can move this
1172 * code into state transitions.
1173 */
1174static void count_statistics(struct gsm_trans *trans, int new_state)
1175{
1176 int old_state = trans->cc.state;
1177 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1178
1179 if (old_state == new_state)
1180 return;
1181
1182 /* state incoming */
1183 switch (new_state) {
1184 case GSM_CSTATE_ACTIVE:
1185 osmo_counter_inc(trans->net->active_calls);
1186 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1187 break;
1188 }
1189
1190 /* state outgoing */
1191 switch (old_state) {
1192 case GSM_CSTATE_ACTIVE:
1193 osmo_counter_dec(trans->net->active_calls);
1194 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1195 new_state == GSM_CSTATE_DISCONNECT_IND)
1196 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1197 else
1198 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1199 break;
1200 }
1201}
1202
Harald Welte4bc90a12008-12-27 16:32:52 +00001203/* Call Control */
1204
Harald Welte7584aea2009-02-11 11:44:12 +00001205/* The entire call control code is written in accordance with Figure 7.10c
1206 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1207 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1208 * it for voice */
1209
Harald Welte4bfdfe72009-06-10 23:11:52 +08001210static void new_cc_state(struct gsm_trans *trans, int state)
1211{
1212 if (state > 31 || state < 0)
1213 return;
1214
1215 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001216 gsm48_cc_state_name(trans->cc.state),
1217 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001218
Alexander Couzens92f552f2016-08-23 07:32:27 +02001219 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001220 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001221}
1222
1223static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001224{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001225 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001226 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001227 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001228
Harald Welte4bc90a12008-12-27 16:32:52 +00001229 gh->msg_type = GSM48_MT_CC_STATUS;
1230
1231 cause = msgb_put(msg, 3);
1232 cause[0] = 2;
1233 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1234 cause[2] = 0x80 | 30; /* response to status inquiry */
1235
1236 call_state = msgb_put(msg, 1);
1237 call_state[0] = 0xc0 | 0x00;
1238
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001239 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001240}
1241
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001242static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001243 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001244{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001245 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001246 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1247
Harald Welte6f4b7532008-12-29 00:39:37 +00001248 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001249 gh->msg_type = msg_type;
1250
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001251 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001252}
1253
Harald Welte4bfdfe72009-06-10 23:11:52 +08001254static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1255{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001256 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001257 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001258 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001259 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001260 }
1261}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001262
Harald Welte4bfdfe72009-06-10 23:11:52 +08001263static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1264 int msg_type, struct gsm_mncc *mncc)
1265{
1266 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001267 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001268
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02001269#if BEFORE_MSCSPLIT
1270 /* Re-enable this log output once we can obtain this information via
1271 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001272 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001273 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001274 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001275 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001276 trans->conn->lchan->ts->trx->bts->nr,
1277 trans->conn->lchan->ts->trx->nr,
1278 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte7b423ed2016-06-19 18:06:02 +02001279 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001280 get_mncc_name(msg_type));
1281 else
1282 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1283 "Sending '%s' to MNCC.\n",
Harald Welte7b423ed2016-06-19 18:06:02 +02001284 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285 get_mncc_name(msg_type));
1286 else
1287 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1288 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02001289#else
1290 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1291#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001292
1293 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001294
Harald Welte966636f2009-06-26 19:39:35 +02001295 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296 if (!msg)
1297 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001298
1299 data = msgb_put(msg, sizeof(struct gsm_mncc));
1300 memcpy(data, mncc, sizeof(struct gsm_mncc));
1301
Harald Welte54209c22010-12-22 23:43:29 +01001302 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001303
1304 return 0;
1305}
1306
1307int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001308 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001309{
1310 struct gsm_mncc rel;
1311
Harald Welte92f70c52009-06-12 01:54:08 +08001312 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001313 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001314 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001315 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1316 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001317 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1318}
1319
Harald Weltedcaf5652009-07-23 18:56:43 +02001320/* Call Control Specific transaction release.
1321 * gets called by trans_free, DO NOT CALL YOURSELF! */
1322void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001323{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001324 gsm48_stop_cc_timer(trans);
1325
1326 /* send release to L4, if callref still exists */
1327 if (trans->callref) {
1328 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001329 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001330 GSM48_CAUSE_LOC_PRN_S_LU,
1331 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001332 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001333 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001334 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001335}
1336
1337static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001338
Harald Welte09e38af2009-02-16 22:52:23 +00001339/* call-back from paging the B-end of the connection */
1340static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001341 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001342{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001343 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001344 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001345
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001346 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001347
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001348 switch (event) {
1349 case GSM_PAGING_SUCCEEDED:
Harald Welte7b423ed2016-06-19 18:06:02 +02001350 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1351 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001352 OSMO_ASSERT(conn);
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02001353 /* Assign conn */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001354 transt->conn = conn;
1355 /* send SETUP request to called party */
1356 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1357 break;
1358 case GSM_PAGING_EXPIRED:
1359 case GSM_PAGING_BUSY:
1360 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte7b423ed2016-06-19 18:06:02 +02001361 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001362 /* Temporarily out of order */
1363 mncc_release_ind(transt->net, transt,
1364 transt->callref,
1365 GSM48_CAUSE_LOC_PRN_S_LU,
1366 GSM48_CC_CAUSE_DEST_OOO);
1367 transt->callref = 0;
1368 transt->paging_request = NULL;
1369 trans_free(transt);
1370 break;
1371 default:
1372 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1373 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001375
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001376 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001377 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001378}
Harald Welte7584aea2009-02-11 11:44:12 +00001379
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001381static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001382{
Harald Welte53d51f52015-12-03 14:59:04 +01001383 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1384 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001385
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001387 return -EIO;
1388
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001389 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 return -EIO;
1391
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001392 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte7b423ed2016-06-19 18:06:02 +02001393 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001394
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001395 return msc_call_bridge(trans1, trans2);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001396}
1397
Harald Welte4bfdfe72009-06-10 23:11:52 +08001398static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1399{
1400 DEBUGP(DCC, "-> STATUS ENQ\n");
1401 return gsm48_cc_tx_status(trans, msg);
1402}
1403
1404static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1405static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1406
1407static void gsm48_cc_timeout(void *arg)
1408{
1409 struct gsm_trans *trans = arg;
1410 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001411 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1412 int mo_location = GSM48_CAUSE_LOC_USER;
1413 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1414 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001415 struct gsm_mncc mo_rel, l4_rel;
1416
1417 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1418 mo_rel.callref = trans->callref;
1419 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1420 l4_rel.callref = trans->callref;
1421
Harald Weltedcaf5652009-07-23 18:56:43 +02001422 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001423 case 0x303:
1424 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001425 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 break;
1427 case 0x310:
1428 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001429 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001430 break;
1431 case 0x313:
1432 disconnect = 1;
1433 /* unknown, did not find it in the specs */
1434 break;
1435 case 0x301:
1436 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001437 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001438 break;
1439 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001440 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001441 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001442 gsm48_cc_tx_release(trans, &trans->cc.msg);
1443 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001444 break; /* stay in release state */
1445 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001446 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001447 return;
1448// release = 1;
1449// l4_cause = 14;
1450// break;
1451 case 0x306:
1452 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001453 mo_cause = trans->cc.msg.cause.value;
1454 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001455 break;
1456 case 0x323:
1457 disconnect = 1;
1458 break;
1459 default:
1460 release = 1;
1461 }
1462
1463 if (release && trans->callref) {
1464 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001465 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001466 l4_location, l4_cause);
1467 trans->callref = 0;
1468 }
1469
1470 if (disconnect && trans->callref) {
1471 /* process disconnect towards layer 4 */
1472 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001473 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001474 }
1475
1476 /* process disconnect towards mobile station */
1477 if (disconnect || release) {
1478 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001479 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1480 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1481 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001482 mo_rel.cause.diag_len = 3;
1483
1484 if (disconnect)
1485 gsm48_cc_tx_disconnect(trans, &mo_rel);
1486 if (release)
1487 gsm48_cc_tx_release(trans, &mo_rel);
1488 }
1489
1490}
1491
Max3ffce192016-04-25 15:22:00 +02001492/* disconnect both calls from the bridge */
1493static inline void disconnect_bridge(struct gsm_network *net,
1494 struct gsm_mncc_bridge *bridge, int err)
1495{
1496 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1497 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1498 struct gsm_mncc mx_rel;
1499 if (!trans0 || !trans1)
1500 return;
1501
1502 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1503 trans0->callref, trans1->callref, strerror(err));
1504
1505 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1506 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1507 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1508
1509 mx_rel.callref = trans0->callref;
1510 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1511
1512 mx_rel.callref = trans1->callref;
1513 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1514}
1515
Harald Welte4bfdfe72009-06-10 23:11:52 +08001516static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1517 int sec, int micro)
1518{
1519 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001520 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001521 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001522 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001523}
1524
1525static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1526{
1527 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001528 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001529 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1530 struct tlv_parsed tp;
1531 struct gsm_mncc setup;
1532
1533 memset(&setup, 0, sizeof(struct gsm_mncc));
1534 setup.callref = trans->callref;
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02001535
Harald Welte474d19f2010-03-02 23:18:30 +01001536 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001537 /* emergency setup is identified by msg_type */
1538 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1539 setup.emergency = 1;
1540
1541 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001542 setup.fields |= MNCC_F_CALLING;
Harald Welte7b423ed2016-06-19 18:06:02 +02001543 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1544 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001545
Harald Welte4bfdfe72009-06-10 23:11:52 +08001546 /* bearer capability */
1547 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1548 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001549 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001550 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1551 }
1552 /* facility */
1553 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1554 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001555 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001556 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1557 }
1558 /* called party bcd number */
1559 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1560 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001561 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001562 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1563 }
1564 /* user-user */
1565 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1566 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001567 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001568 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1569 }
1570 /* ss-version */
1571 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1572 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001573 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1575 }
1576 /* CLIR suppression */
1577 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1578 setup.clir.sup = 1;
1579 /* CLIR invocation */
1580 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1581 setup.clir.inv = 1;
1582 /* cc cap */
1583 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1584 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001585 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1587 }
1588
Harald Welte4bfdfe72009-06-10 23:11:52 +08001589 new_cc_state(trans, GSM_CSTATE_INITIATED);
1590
Harald Welte (local)47df3992009-12-26 19:45:03 +01001591 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
Harald Welte7b423ed2016-06-19 18:06:02 +02001592 vlr_subscr_name(trans->vsub), trans->vsub->msisdn,
Harald Welte (local)47df3992009-12-26 19:45:03 +01001593 setup.called.number);
1594
Alexander Couzensb847a212016-08-02 11:34:11 +02001595 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001596
Harald Welte4bfdfe72009-06-10 23:11:52 +08001597 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001598 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001599
Harald Welte13cac662009-07-29 12:10:35 +02001600 /* MNCC code will modify the channel asynchronously, we should
1601 * ipaccess-bind only after the modification has been made to the
1602 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001603 return 0;
1604}
1605
1606static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001607{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001608 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001609 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001610 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001611 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001612
Harald Welte7ccf7782009-02-17 01:43:01 +00001613 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001614
Harald Welte4bfdfe72009-06-10 23:11:52 +08001615 /* transaction id must not be assigned */
1616 if (trans->transaction_id != 0xff) { /* unasssigned */
1617 DEBUGP(DCC, "TX Setup with assigned transaction. "
1618 "This is not allowed!\n");
1619 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001620 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001621 GSM48_CAUSE_LOC_PRN_S_LU,
1622 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001623 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001624 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001625 return rc;
1626 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001627
Harald Welte4bfdfe72009-06-10 23:11:52 +08001628 /* Get free transaction_id */
Harald Welte7b423ed2016-06-19 18:06:02 +02001629 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001630 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001631 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001632 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001633 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001634 GSM48_CAUSE_LOC_PRN_S_LU,
1635 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001636 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001637 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001638 return rc;
1639 }
Harald Welte78283ef2009-07-23 21:36:44 +02001640 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001641
Harald Welte65e74cc2008-12-29 01:55:35 +00001642 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001643
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001645
Harald Welte4bfdfe72009-06-10 23:11:52 +08001646 /* bearer capability */
1647 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001648 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001649 /* facility */
1650 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001651 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 /* progress */
1653 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001654 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655 /* calling party BCD number */
1656 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001657 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658 /* called party BCD number */
1659 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001660 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001661 /* user-user */
1662 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001663 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001664 /* redirecting party BCD number */
1665 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001666 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001667 /* signal */
1668 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001669 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001670
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001672
Alexander Couzensb847a212016-08-02 11:34:11 +02001673 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001674
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001675 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001676}
1677
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1679{
1680 struct gsm48_hdr *gh = msgb_l3(msg);
1681 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1682 struct tlv_parsed tp;
1683 struct gsm_mncc call_conf;
1684
1685 gsm48_stop_cc_timer(trans);
1686 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1687
1688 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1689 call_conf.callref = trans->callref;
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02001690
Harald Welte474d19f2010-03-02 23:18:30 +01001691 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692#if 0
1693 /* repeat */
1694 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1695 call_conf.repeat = 1;
1696 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1697 call_conf.repeat = 2;
1698#endif
1699 /* bearer capability */
1700 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1701 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001702 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1704 }
1705 /* cause */
1706 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1707 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001708 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1710 }
1711 /* cc cap */
1712 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1713 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001714 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1716 }
1717
Andreas Eversberg035b8742013-09-19 09:27:06 +02001718 /* IMSI of called subscriber */
Harald Welte7b423ed2016-06-19 18:06:02 +02001719 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02001720
Harald Welte4bfdfe72009-06-10 23:11:52 +08001721 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1722
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001723 msc_call_assignment(trans);
1724
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001725 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001726 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727}
1728
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001729static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730{
1731 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001732 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001733 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001734 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1737
1738 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1739
1740 /* bearer capability */
1741 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001742 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 /* facility */
1744 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001745 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746 /* progress */
1747 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001748 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749
Neels Hofmeyra1756f32016-05-20 21:59:55 +02001750 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1751 if (rc)
1752 return rc;
1753
1754 return msc_call_assignment(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755}
1756
1757static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1758{
1759 struct gsm48_hdr *gh = msgb_l3(msg);
1760 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1761 struct tlv_parsed tp;
1762 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001763
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 gsm48_stop_cc_timer(trans);
1765 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1766
1767 memset(&alerting, 0, sizeof(struct gsm_mncc));
1768 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001769 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 /* facility */
1771 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1772 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001773 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1775 }
1776
1777 /* progress */
1778 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1779 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001780 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001781 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1782 }
1783 /* ss-version */
1784 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1785 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001786 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001787 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1788 }
1789
1790 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1791
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001792 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001793 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794}
1795
1796static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1797{
1798 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001799 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1801
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 gh->msg_type = GSM48_MT_CC_ALERTING;
1803
1804 /* facility */
1805 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001806 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 /* progress */
1808 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001809 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 /* user-user */
1811 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001812 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001813
1814 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001815
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001816 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817}
1818
1819static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1820{
1821 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001822 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1824
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 gh->msg_type = GSM48_MT_CC_PROGRESS;
1826
1827 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001828 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829 /* user-user */
1830 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001831 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001832
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001833 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834}
1835
1836static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1837{
1838 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001839 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001840 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1841
Harald Welte4bfdfe72009-06-10 23:11:52 +08001842 gh->msg_type = GSM48_MT_CC_CONNECT;
1843
1844 gsm48_stop_cc_timer(trans);
1845 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1846
1847 /* facility */
1848 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001849 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 /* progress */
1851 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001852 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 /* connected number */
1854 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001855 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 /* user-user */
1857 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001858 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859
1860 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1861
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001862 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863}
1864
1865static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1866{
1867 struct gsm48_hdr *gh = msgb_l3(msg);
1868 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1869 struct tlv_parsed tp;
1870 struct gsm_mncc connect;
1871
1872 gsm48_stop_cc_timer(trans);
1873
1874 memset(&connect, 0, sizeof(struct gsm_mncc));
1875 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001876 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001877 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001878 connect.fields |= MNCC_F_CONNECTED;
Harald Welte7b423ed2016-06-19 18:06:02 +02001879 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
1880 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001881
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 /* facility */
1883 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1884 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001885 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1887 }
1888 /* user-user */
1889 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1890 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001891 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001892 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1893 }
1894 /* ss-version */
1895 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1896 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001897 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001898 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1899 }
1900
1901 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02001902 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001904 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905}
1906
1907
1908static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1909{
1910 struct gsm_mncc connect_ack;
1911
1912 gsm48_stop_cc_timer(trans);
1913
1914 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02001915 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001916
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1918 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01001919
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001920 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 &connect_ack);
1922}
1923
1924static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1925{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001926 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1928
Harald Welte4bfdfe72009-06-10 23:11:52 +08001929 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1930
1931 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1932
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001933 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934}
1935
1936static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1937{
1938 struct gsm48_hdr *gh = msgb_l3(msg);
1939 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1940 struct tlv_parsed tp;
1941 struct gsm_mncc disc;
1942
1943 gsm48_stop_cc_timer(trans);
1944
1945 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1946
1947 memset(&disc, 0, sizeof(struct gsm_mncc));
1948 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001949 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950 /* cause */
1951 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1952 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001953 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1955 }
1956 /* facility */
1957 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1958 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1961 }
1962 /* user-user */
1963 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1964 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001965 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1967 }
1968 /* ss-version */
1969 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1970 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001971 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1973 }
1974
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001975 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976
1977}
1978
Harald Weltec66b71c2009-06-11 14:23:20 +08001979static struct gsm_mncc_cause default_cause = {
1980 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1981 .coding = 0,
1982 .rec = 0,
1983 .rec_val = 0,
1984 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1985 .diag_len = 0,
1986 .diag = { 0 },
1987};
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988
1989static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1990{
1991 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001992 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1994
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1996
1997 gsm48_stop_cc_timer(trans);
1998 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1999
2000 /* cause */
2001 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002002 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003 else
Harald Welte55c8f352010-03-07 23:40:35 +01002004 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005
2006 /* facility */
2007 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002008 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 /* progress */
2010 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002011 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012 /* user-user */
2013 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002014 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015
2016 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002017 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002018
2019 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2020
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002021 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022}
2023
2024static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2025{
2026 struct gsm48_hdr *gh = msgb_l3(msg);
2027 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2028 struct tlv_parsed tp;
2029 struct gsm_mncc rel;
2030 int rc;
2031
2032 gsm48_stop_cc_timer(trans);
2033
2034 memset(&rel, 0, sizeof(struct gsm_mncc));
2035 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002036 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 /* cause */
2038 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2039 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002040 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2042 }
2043 /* facility */
2044 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2045 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002046 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2048 }
2049 /* user-user */
2050 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2051 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002052 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2054 }
2055 /* ss-version */
2056 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2057 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002058 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002059 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2060 }
2061
Harald Weltedcaf5652009-07-23 18:56:43 +02002062 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002064 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002066 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002067 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002068 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002069 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 }
2071
2072 new_cc_state(trans, GSM_CSTATE_NULL);
2073
2074 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002075 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076
2077 return rc;
2078}
2079
2080static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2081{
2082 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002083 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2085
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086 gh->msg_type = GSM48_MT_CC_RELEASE;
2087
Harald Welte4bfdfe72009-06-10 23:11:52 +08002088 gsm48_stop_cc_timer(trans);
2089 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2090
2091 /* cause */
2092 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002093 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 /* facility */
2095 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 /* user-user */
2098 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002099 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100
Harald Weltedcaf5652009-07-23 18:56:43 +02002101 trans->cc.T308_second = 0;
2102 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103
Harald Weltedcaf5652009-07-23 18:56:43 +02002104 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2106
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002107 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108}
2109
2110static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2111{
2112 struct gsm48_hdr *gh = msgb_l3(msg);
2113 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2114 struct tlv_parsed tp;
2115 struct gsm_mncc rel;
2116 int rc = 0;
2117
2118 gsm48_stop_cc_timer(trans);
2119
2120 memset(&rel, 0, sizeof(struct gsm_mncc));
2121 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002122 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 /* cause */
2124 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2125 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002126 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2128 }
2129 /* facility */
2130 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2131 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002132 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2134 }
2135 /* user-user */
2136 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2137 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002138 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002139 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2140 }
2141 /* ss-version */
2142 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2143 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002144 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2146 }
2147
2148 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002149 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002151 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152 MNCC_REJ_IND, &rel);
2153 break;
2154 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002155 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156 MNCC_REL_CNF, &rel);
2157 break;
2158 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002159 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160 MNCC_REL_IND, &rel);
2161 }
2162 }
2163
2164 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002165 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166
2167 return rc;
2168}
2169
2170static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2171{
2172 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002173 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002175 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2178
2179 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002180
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 gsm48_stop_cc_timer(trans);
2182
2183 /* cause */
2184 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002185 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186 /* facility */
2187 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002188 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189 /* user-user */
2190 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002191 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192
Harald Weltec7782de2010-12-21 19:31:41 +01002193 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2194
Harald Weltedcaf5652009-07-23 18:56:43 +02002195 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196
Harald Weltec7782de2010-12-21 19:31:41 +01002197 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198}
2199
2200static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2201{
2202 struct gsm48_hdr *gh = msgb_l3(msg);
2203 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2204 struct tlv_parsed tp;
2205 struct gsm_mncc fac;
2206
2207 memset(&fac, 0, sizeof(struct gsm_mncc));
2208 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002209 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 /* facility */
2211 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2212 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002213 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2215 }
2216 /* ss-version */
2217 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2218 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002219 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2221 }
2222
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002223 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224}
2225
2226static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2227{
2228 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002229 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2231
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 gh->msg_type = GSM48_MT_CC_FACILITY;
2233
2234 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002235 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002237 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238}
2239
2240static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2241{
2242 struct gsm_mncc hold;
2243
2244 memset(&hold, 0, sizeof(struct gsm_mncc));
2245 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002246 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247}
2248
2249static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2250{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002251 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2253
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2255
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002256 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257}
2258
2259static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2260{
2261 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002262 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2264
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2266
2267 /* cause */
2268 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002269 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002270 else
Harald Welte55c8f352010-03-07 23:40:35 +01002271 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002273 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274}
2275
2276static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2277{
2278 struct gsm_mncc retrieve;
2279
2280 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2281 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002282 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002283 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284}
2285
2286static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2287{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002288 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2290
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2292
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002293 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294}
2295
2296static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2297{
2298 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002299 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002300 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2301
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2303
2304 /* cause */
2305 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002306 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 else
Harald Welte55c8f352010-03-07 23:40:35 +01002308 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002310 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311}
2312
2313static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2314{
2315 struct gsm48_hdr *gh = msgb_l3(msg);
2316 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2317 struct tlv_parsed tp;
2318 struct gsm_mncc dtmf;
2319
2320 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2321 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002322 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323 /* keypad facility */
2324 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2325 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002326 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2328 }
2329
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002330 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331}
2332
2333static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2334{
2335 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002336 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2338
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2340
2341 /* keypad */
2342 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002343 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002345 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346}
2347
2348static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2349{
2350 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002351 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2353
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2355
2356 /* cause */
2357 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002358 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 else
Harald Welte55c8f352010-03-07 23:40:35 +01002360 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002362 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363}
2364
2365static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2366{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002367 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2369
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2371
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002372 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373}
2374
2375static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2376{
2377 struct gsm_mncc dtmf;
2378
2379 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2380 dtmf.callref = trans->callref;
2381
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002382 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383}
2384
2385static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2386{
2387 struct gsm48_hdr *gh = msgb_l3(msg);
2388 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2389 struct tlv_parsed tp;
2390 struct gsm_mncc modify;
2391
2392 memset(&modify, 0, sizeof(struct gsm_mncc));
2393 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002394 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 /* bearer capability */
2396 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2397 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002398 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2400 }
2401
2402 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2403
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002404 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405}
2406
2407static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2408{
2409 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002410 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2412
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413 gh->msg_type = GSM48_MT_CC_MODIFY;
2414
2415 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2416
2417 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002418 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419
2420 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2421
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002422 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423}
2424
2425static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2426{
2427 struct gsm48_hdr *gh = msgb_l3(msg);
2428 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2429 struct tlv_parsed tp;
2430 struct gsm_mncc modify;
2431
2432 gsm48_stop_cc_timer(trans);
2433
2434 memset(&modify, 0, sizeof(struct gsm_mncc));
2435 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002436 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437 /* bearer capability */
2438 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2439 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002440 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2442 }
2443
2444 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2445
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002446 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447}
2448
2449static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2450{
2451 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002452 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2454
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2456
2457 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002458 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459
2460 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2461
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002462 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463}
2464
2465static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2466{
2467 struct gsm48_hdr *gh = msgb_l3(msg);
2468 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2469 struct tlv_parsed tp;
2470 struct gsm_mncc modify;
2471
2472 gsm48_stop_cc_timer(trans);
2473
2474 memset(&modify, 0, sizeof(struct gsm_mncc));
2475 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002476 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477 /* bearer capability */
2478 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2479 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002480 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2482 }
2483 /* cause */
2484 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2485 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002486 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2488 }
2489
2490 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2491
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002492 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493}
2494
2495static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2496{
2497 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002498 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2500
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2502
2503 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002504 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002506 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507
2508 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2509
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002510 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511}
2512
2513static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2514{
2515 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002516 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2518
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519 gh->msg_type = GSM48_MT_CC_NOTIFY;
2520
2521 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002522 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002524 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525}
2526
2527static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2528{
2529 struct gsm48_hdr *gh = msgb_l3(msg);
2530 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2531// struct tlv_parsed tp;
2532 struct gsm_mncc notify;
2533
2534 memset(&notify, 0, sizeof(struct gsm_mncc));
2535 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002536// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002538 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002540 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541}
2542
2543static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2544{
2545 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002546 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2548
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549 gh->msg_type = GSM48_MT_CC_USER_INFO;
2550
2551 /* user-user */
2552 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002553 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554 /* more data */
2555 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002556 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002558 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559}
2560
2561static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2562{
2563 struct gsm48_hdr *gh = msgb_l3(msg);
2564 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2565 struct tlv_parsed tp;
2566 struct gsm_mncc user;
2567
2568 memset(&user, 0, sizeof(struct gsm_mncc));
2569 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002570 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571 /* user-user */
2572 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2573 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002574 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2576 }
2577 /* more data */
2578 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2579 user.more = 1;
2580
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002581 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582}
2583
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002585 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586 int type;
2587 int (*rout) (struct gsm_trans *trans, void *arg);
2588} downstatelist[] = {
2589 /* mobile originating call establishment */
2590 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyra1756f32016-05-20 21:59:55 +02002591 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002592 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2593 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2594 {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 */
2595 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2596 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2597 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2598 /* mobile terminating call establishment */
2599 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2600 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2601 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2602 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2603 /* signalling during call */
2604 {SBIT(GSM_CSTATE_ACTIVE),
2605 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2606 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2607 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2608 {ALL_STATES,
2609 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2610 {ALL_STATES,
2611 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2612 {ALL_STATES,
2613 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2614 {SBIT(GSM_CSTATE_ACTIVE),
2615 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2616 {SBIT(GSM_CSTATE_ACTIVE),
2617 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2618 {SBIT(GSM_CSTATE_ACTIVE),
2619 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2620 {SBIT(GSM_CSTATE_ACTIVE),
2621 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2622 {SBIT(GSM_CSTATE_ACTIVE),
2623 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2624 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2625 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2626 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2627 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2628 {SBIT(GSM_CSTATE_ACTIVE),
2629 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2630 /* clearing */
2631 {SBIT(GSM_CSTATE_INITIATED),
2632 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2633 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2634 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2635 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2636 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637};
2638
2639#define DOWNSLLEN \
2640 (sizeof(downstatelist) / sizeof(struct downstate))
2641
2642
Harald Welte76556372010-12-22 23:57:45 +01002643int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644{
Harald Welte1a6f7982009-08-09 18:52:33 +02002645 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002647 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648 struct gsm_mncc *data = arg, rel;
2649
Harald Welte04dc88f2010-12-22 21:45:05 +01002650 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2651
Harald Welte4bfdfe72009-06-10 23:11:52 +08002652 /* handle special messages */
2653 switch(msg_type) {
2654 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002655 rc = tch_bridge(net, arg);
2656 if (rc < 0)
2657 disconnect_bridge(net, arg, -rc);
2658 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 case MNCC_FRAME_RECV:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002661 case MNCC_RTP_CREATE:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002662 case MNCC_RTP_CONNECT:
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002663 case MNCC_RTP_FREE:
Harald Welteda7ab742009-12-19 22:23:05 +01002664 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002665 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002666 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002667 case GSM_TCH_FRAME_AMR:
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002668 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2669 get_mncc_name(msg_type));
2670 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 }
2672
2673 memset(&rel, 0, sizeof(struct gsm_mncc));
2674 rel.callref = data->callref;
2675
2676 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002677 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678
2679 /* Callref unknown */
2680 if (!trans) {
Harald Welte7b423ed2016-06-19 18:06:02 +02002681 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002682
Harald Welte4a3464c2009-07-04 10:11:24 +02002683 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2685 "Received '%s' from MNCC with "
2686 "unknown callref %d\n", data->called.number,
2687 get_mncc_name(msg_type), data->callref);
2688 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002689 return mncc_release_ind(net, NULL, data->callref,
2690 GSM48_CAUSE_LOC_PRN_S_LU,
2691 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002693 if (!data->called.number[0] && !data->imsi[0]) {
2694 DEBUGP(DCC, "(bts - trx - ts - ti) "
2695 "Received '%s' from MNCC with "
2696 "no number or IMSI\n", get_mncc_name(msg_type));
2697 /* Invalid number */
2698 return mncc_release_ind(net, NULL, data->callref,
2699 GSM48_CAUSE_LOC_PRN_S_LU,
2700 GSM48_CC_CAUSE_INV_NR_FORMAT);
2701 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002703 if (data->called.number[0])
Harald Welte7b423ed2016-06-19 18:06:02 +02002704 vsub = vlr_subscr_find_by_msisdn(net->vlr,
2705 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002706 else
Harald Welte7b423ed2016-06-19 18:06:02 +02002707 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002708
2709 /* update the subscriber we deal with */
Harald Welte7b423ed2016-06-19 18:06:02 +02002710 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002711
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712 /* If subscriber is not found */
Harald Welte7b423ed2016-06-19 18:06:02 +02002713 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2715 "Received '%s' from MNCC with "
2716 "unknown subscriber %s\n", data->called.number,
2717 get_mncc_name(msg_type), data->called.number);
2718 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002719 return mncc_release_ind(net, NULL, data->callref,
2720 GSM48_CAUSE_LOC_PRN_S_LU,
2721 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722 }
2723 /* If subscriber is not "attached" */
Harald Welte7b423ed2016-06-19 18:06:02 +02002724 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2726 "Received '%s' from MNCC with "
2727 "detached subscriber %s\n", data->called.number,
2728 get_mncc_name(msg_type), data->called.number);
Harald Welte7b423ed2016-06-19 18:06:02 +02002729 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002731 return mncc_release_ind(net, NULL, data->callref,
2732 GSM48_CAUSE_LOC_PRN_S_LU,
2733 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 }
2735 /* Create transaction */
Harald Welte7b423ed2016-06-19 18:06:02 +02002736 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02002737 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte7b423ed2016-06-19 18:06:02 +02002739 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002741 mncc_release_ind(net, NULL, data->callref,
2742 GSM48_CAUSE_LOC_PRN_S_LU,
2743 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744 return -ENOMEM;
2745 }
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002746
2747 /* Find conn */
Harald Welte7b423ed2016-06-19 18:06:02 +02002748 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002749
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002750 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002751 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002752 /* find transaction with this subscriber already paging */
2753 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002754 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755 if (transt == trans ||
Harald Welte7b423ed2016-06-19 18:06:02 +02002756 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002757 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002758 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002759 "Received '%s' from MNCC with "
2760 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002761 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762 data->called.number,
Harald Welte7b423ed2016-06-19 18:06:02 +02002763 get_mncc_name(msg_type), vsub->lac);
2764 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002765 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766 return 0;
2767 }
Neels Hofmeyra1756f32016-05-20 21:59:55 +02002768 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02002769 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002770
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02002771 /* Request a channel */
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002772 trans->paging_request = subscr_request_conn(
Harald Welte7b423ed2016-06-19 18:06:02 +02002773 vsub,
Harald Welte7b423ed2016-06-19 18:06:02 +02002774 setup_trig_pag_evt,
Neels Hofmeyra1756f32016-05-20 21:59:55 +02002775 trans,
2776 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002777 if (!trans->paging_request) {
2778 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte7b423ed2016-06-19 18:06:02 +02002779 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002780 trans_free(trans);
2781 return 0;
2782 }
Harald Welte7b423ed2016-06-19 18:06:02 +02002783 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784 return 0;
2785 }
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002786
2787 /* Assign conn */
Harald Welte7b423ed2016-06-19 18:06:02 +02002788 trans->conn = msc_subscr_conn_get(conn);
2789 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002790 } else {
2791 /* update the subscriber we deal with */
Harald Welte7b423ed2016-06-19 18:06:02 +02002792 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002794
2795 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002796 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797
2798 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002799 if (!conn) {
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002800 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002801 "Received '%s' from MNCC in paging state\n",
Harald Welte7b423ed2016-06-19 18:06:02 +02002802 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002804 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2805 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002806 if (msg_type == MNCC_REL_REQ)
2807 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2808 else
2809 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2810 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002811 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002812 return rc;
2813 }
2814
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002815 DEBUGP(DCC, "(ti %02x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816 "Received '%s' from MNCC in state %d (%s)\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817 trans->transaction_id,
Harald Welte7b423ed2016-06-19 18:06:02 +02002818 vlr_subscr_msisdn_or_name(trans->conn->vsub),
Harald Weltedcaf5652009-07-23 18:56:43 +02002819 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002820 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002821
2822 /* Find function for current state and message */
2823 for (i = 0; i < DOWNSLLEN; i++)
2824 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002825 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826 break;
2827 if (i == DOWNSLLEN) {
2828 DEBUGP(DCC, "Message unhandled at this state.\n");
2829 return 0;
2830 }
2831
2832 rc = downstatelist[i].rout(trans, arg);
2833
2834 return rc;
2835}
2836
2837
2838static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002839 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840 int type;
2841 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2842} datastatelist[] = {
2843 /* mobile originating call establishment */
2844 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2845 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2846 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2847 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2848 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2849 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2850 /* mobile terminating call establishment */
2851 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2852 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2853 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2854 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002855 {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 +08002856 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2857 /* signalling during call */
2858 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2859 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2860 {SBIT(GSM_CSTATE_ACTIVE),
2861 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2862 {ALL_STATES,
2863 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2864 {ALL_STATES,
2865 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2866 {ALL_STATES,
2867 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2868 {SBIT(GSM_CSTATE_ACTIVE),
2869 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2870 {SBIT(GSM_CSTATE_ACTIVE),
2871 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2872 {SBIT(GSM_CSTATE_ACTIVE),
2873 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2874 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2875 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2876 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2877 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2878 {SBIT(GSM_CSTATE_ACTIVE),
2879 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2880 /* clearing */
2881 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2882 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2883 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2884 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2885 {ALL_STATES, /* 5.4.3.4 */
2886 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2887};
2888
2889#define DATASLLEN \
2890 (sizeof(datastatelist) / sizeof(struct datastate))
2891
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08002892static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00002893{
2894 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01002895 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01002896 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02002897 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002898 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002899
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900 if (msg_type & 0x80) {
2901 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2902 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002903 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002904
Harald Welte7b423ed2016-06-19 18:06:02 +02002905 if (!conn->vsub) {
2906 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01002907 return -EINVAL;
2908 }
2909
Harald Welte4bfdfe72009-06-10 23:11:52 +08002910 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002911 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002912
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002913#if BEFORE_MSCSPLIT
2914 /* Re-enable this log output once we can obtain this information via
2915 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02002916 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08002918 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte7b423ed2016-06-19 18:06:02 +02002919 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf12010-03-25 12:13:02 +08002920 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2921 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyr0e5a7c42017-05-08 15:12:20 +02002922#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923
2924 /* Create transaction */
2925 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002926 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927 "creating new trans.\n", transaction_id);
2928 /* Create transaction */
Harald Welte7b423ed2016-06-19 18:06:02 +02002929 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01002930 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02002931 transaction_id, new_callref++);
2932 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002933 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002934 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002935 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 GSM48_MT_CC_RELEASE_COMPL);
2937 return -ENOMEM;
2938 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939 /* Assign transaction */
Harald Welte7b423ed2016-06-19 18:06:02 +02002940 trans->conn = msc_subscr_conn_get(conn);
2941 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 }
2943
2944 /* find function for current state and message */
2945 for (i = 0; i < DATASLLEN; i++)
2946 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002947 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948 break;
2949 if (i == DATASLLEN) {
2950 DEBUGP(DCC, "Message unhandled at this state.\n");
2951 return 0;
2952 }
2953
Harald Welte7b423ed2016-06-19 18:06:02 +02002954 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002955
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00002957
Harald Welte7b423ed2016-06-19 18:06:02 +02002958 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00002959 return rc;
2960}
2961
Harald Welte7b423ed2016-06-19 18:06:02 +02002962static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08002963{
Harald Welte7b423ed2016-06-19 18:06:02 +02002964 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
2965 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08002966
Harald Welte7b423ed2016-06-19 18:06:02 +02002967 switch (pdisc) {
2968 case GSM48_PDISC_MM:
2969 switch (msg_type) {
2970 case GSM48_MT_MM_LOC_UPD_REQUEST:
2971 case GSM48_MT_MM_CM_SERV_REQ:
2972 case GSM48_MT_MM_AUTH_RESP:
2973 case GSM48_MT_MM_AUTH_FAIL:
2974 case GSM48_MT_MM_ID_RESP:
2975 case GSM48_MT_MM_TMSI_REALL_COMPL:
2976 case GSM48_MT_MM_IMSI_DETACH_IND:
2977 return true;
2978 default:
2979 break;
2980 }
2981 break;
2982 case GSM48_PDISC_RR:
2983 switch (msg_type) {
2984 case GSM48_MT_RR_CIPH_M_COMPL:
2985 case GSM48_MT_RR_PAG_RESP:
2986 return true;
2987 default:
2988 break;
2989 }
2990 break;
2991 default:
2992 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02002993 }
2994
Harald Welte7b423ed2016-06-19 18:06:02 +02002995 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02002996}
2997
Harald Welte7b423ed2016-06-19 18:06:02 +02002998void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
2999 struct msgb *msg)
3000{
3001
3002 /* If a CM Service Request was received before, this is the request the
3003 * conn was opened for. No need to wait for further messages. */
3004
3005 if (!conn->received_cm_service_request)
3006 return;
3007
3008 if (log_check_level(DMM, LOGL_DEBUG)) {
3009 struct gsm48_hdr *gh = msgb_l3(msg);
3010 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3011 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3012
Neels Hofmeyr717c7b92017-03-10 02:15:20 +01003013 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte7b423ed2016-06-19 18:06:02 +02003014 " received_cm_service_request changes to false\n",
3015 vlr_subscr_name(conn->vsub),
Neels Hofmeyr717c7b92017-03-10 02:15:20 +01003016 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte7b423ed2016-06-19 18:06:02 +02003017 }
3018 conn->received_cm_service_request = false;
3019}
3020
3021
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003022/* 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 +08003023int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003024{
3025 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003026 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003027 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003028
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003029 OSMO_ASSERT(conn);
3030 OSMO_ASSERT(msg);
3031
Neels Hofmeyr717c7b92017-03-10 02:15:20 +01003032 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3033 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3034 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte7b423ed2016-06-19 18:06:02 +02003035
3036 if (!msc_subscr_conn_is_accepted(conn)
3037 && !msg_is_initially_permitted(gh)) {
3038 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr717c7b92017-03-10 02:15:20 +01003039 "subscr %s: Message not permitted for initial conn: %s\n",
3040 vlr_subscr_name(conn->vsub),
3041 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte7b423ed2016-06-19 18:06:02 +02003042 return -EACCES;
3043 }
3044
Neels Hofmeyra1756f32016-05-20 21:59:55 +02003045 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3046 LOGP(DMM, LOGL_ERROR,
3047 "%s: Illegal situation: RAN type mismatch:"
3048 " attached via %s, received message via %s\n",
3049 vlr_subscr_name(conn->vsub),
3050 ran_type_name(conn->vsub->cs.attached_via_ran),
3051 ran_type_name(conn->via_ran));
3052 return -EACCES;
3053 }
3054
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003055#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003056 if (silent_call_reroute(conn, msg))
3057 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003058#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003059
Harald Welte52b1f982008-12-23 20:25:15 +00003060 switch (pdisc) {
3061 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003062 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003063 break;
3064 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003065 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003066 break;
3067 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003068 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003069 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003070 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003071 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003072 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003073 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003074 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003075 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3076 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003077 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003078 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003079 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003080 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003081 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003082 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003083 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3084 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003085 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003086 break;
3087 }
3088
3089 return rc;
3090}
Harald Welte8470bf22008-12-25 23:28:35 +00003091
Harald Welte7b423ed2016-06-19 18:06:02 +02003092/***********************************************************************
3093 * VLR integration
3094 ***********************************************************************/
3095
3096/* VLR asks us to send an authentication request */
3097static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3098 bool send_autn)
3099{
3100 struct gsm_subscriber_connection *conn = msc_conn_ref;
3101 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3102 send_autn? at->vec.autn : NULL,
3103 at->key_seq);
3104}
3105
3106/* VLR asks us to send an authentication reject */
3107static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3108{
3109 struct gsm_subscriber_connection *conn = msc_conn_ref;
3110 return gsm48_tx_mm_auth_rej(conn);
3111}
3112
3113/* VLR asks us to transmit an Identity Request of given type */
3114static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3115{
3116 struct gsm_subscriber_connection *conn = msc_conn_ref;
3117 return mm_tx_identity_req(conn, mi_type);
3118}
3119
3120/* VLR asks us to transmit a Location Update Accept */
3121static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3122{
3123 struct gsm_subscriber_connection *conn = msc_conn_ref;
3124 return gsm0408_loc_upd_acc(conn, send_tmsi);
3125}
3126
3127/* VLR asks us to transmit a Location Update Reject */
3128static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3129{
3130 struct gsm_subscriber_connection *conn = msc_conn_ref;
3131 return gsm0408_loc_upd_rej(conn, cause);
3132}
3133
3134/* VLR asks us to transmit a CM Service Accept */
3135static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3136{
3137 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyra1756f32016-05-20 21:59:55 +02003138 return msc_gsm48_tx_mm_serv_ack(conn);
3139}
3140
3141static int msc_vlr_tx_common_id(void *msc_conn_ref)
3142{
3143 struct gsm_subscriber_connection *conn = msc_conn_ref;
3144 return msc_tx_common_id(conn);
Harald Welte7b423ed2016-06-19 18:06:02 +02003145}
3146
3147/* VLR asks us to transmit a CM Service Reject */
3148static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3149{
3150 uint8_t cause;
3151 struct gsm_subscriber_connection *conn = msc_conn_ref;
3152 conn->received_cm_service_request = false;
3153
3154 switch (result) {
3155 default:
3156 case VLR_PR_ARQ_RES_NONE:
3157 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3158 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3159 cause = GSM48_REJECT_NETWORK_FAILURE;
3160 break;
3161 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3162 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3163 break;
3164 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3165 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3166 break;
3167 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3168 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3169 break;
3170 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3171 cause = GSM48_REJECT_ILLEGAL_MS;
3172 break;
3173 case VLR_PR_ARQ_RES_TIMEOUT:
3174 cause = GSM48_REJECT_CONGESTION;
3175 break;
3176 };
3177
Neels Hofmeyra1756f32016-05-20 21:59:55 +02003178 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte7b423ed2016-06-19 18:06:02 +02003179}
3180
3181/* VLR asks us to start using ciphering */
3182static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3183 enum vlr_ciph ciph,
3184 bool retrieve_imeisv)
3185{
3186 struct gsm_subscriber_connection *conn = msc_conn_ref;
3187 struct vlr_subscr *vsub;
3188 struct gsm_auth_tuple *tuple;
3189
3190 if (!conn || !conn->vsub) {
3191 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3192 " NULL conn/subscriber");
3193 return -EINVAL;
3194 }
3195
3196 vsub = conn->vsub;
3197 tuple = vsub->last_tuple;
3198
3199 if (!tuple) {
3200 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3201 " Command: no auth tuple available\n",
3202 vlr_subscr_name(vsub));
3203 return -EINVAL;
3204 }
3205
Neels Hofmeyra1756f32016-05-20 21:59:55 +02003206 switch (conn->via_ran) {
3207 case RAN_GERAN_A:
3208 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3209 vlr_subscr_name(conn->vsub));
3210 return msc_gsm0808_tx_cipher_mode(conn, ciph, tuple->vec.kc, 8,
3211 retrieve_imeisv);
3212 case RAN_UTRAN_IU:
3213#ifdef BUILD_IU
3214 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3215 vlr_subscr_name(conn->vsub));
3216 return iu_tx_sec_mode_cmd(conn->iu.ue_ctx, tuple, 0, 1);
3217#else
3218 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3219 " built without Iu support\n");
3220 return -ENOTSUP;
3221#endif
3222
3223 default:
3224 break;
3225 }
3226 LOGP(DMM, LOGL_ERROR,
3227 "%s: cannot start ciphering, unknown RAN type %d\n",
3228 vlr_subscr_name(conn->vsub), conn->via_ran);
3229 return -ENOTSUP;
3230}
3231
3232void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3233{
3234 struct vlr_ciph_result vlr_res = {};
3235
3236 if (!conn || !conn->vsub) {
3237 LOGP(DMM, LOGL_ERROR,
3238 "Rx Security Mode Complete for invalid conn\n");
3239 return;
3240 }
3241
3242 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3243 vlr_subscr_name(conn->vsub));
3244
3245 vlr_res.cause = VLR_CIPH_COMPL;
3246 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte7b423ed2016-06-19 18:06:02 +02003247}
3248
3249/* VLR informs us that the subscriber data has somehow been modified */
3250static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3251{
3252 /* FIXME */
3253}
3254
3255/* VLR informs us that the subscriber has been associated with a conn */
3256static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3257 struct vlr_subscr *vsub)
3258{
3259 struct gsm_subscriber_connection *conn = msc_conn_ref;
3260 OSMO_ASSERT(!conn->vsub);
3261 conn->vsub = vlr_subscr_get(vsub);
Neels Hofmeyra1756f32016-05-20 21:59:55 +02003262 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte7b423ed2016-06-19 18:06:02 +02003263}
3264
3265/* operations that we need to implement for libvlr */
3266static const struct vlr_ops msc_vlr_ops = {
3267 .tx_auth_req = msc_vlr_tx_auth_req,
3268 .tx_auth_rej = msc_vlr_tx_auth_rej,
3269 .tx_id_req = msc_vlr_tx_id_req,
3270 .tx_lu_acc = msc_vlr_tx_lu_acc,
3271 .tx_lu_rej = msc_vlr_tx_lu_rej,
3272 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3273 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3274 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyra1756f32016-05-20 21:59:55 +02003275 .tx_common_id = msc_vlr_tx_common_id,
Harald Welte7b423ed2016-06-19 18:06:02 +02003276 .subscr_update = msc_vlr_subscr_update,
3277 .subscr_assoc = msc_vlr_subscr_assoc,
3278};
3279
3280/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3281int msc_vlr_alloc(struct gsm_network *net)
3282{
3283 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3284 if (!net->vlr)
3285 return -ENOMEM;
3286 net->vlr->user_ctx = net;
3287 return 0;
3288}
3289
3290/* Launch the VLR, i.e. its GSUP connection */
3291int msc_vlr_start(struct gsm_network *net)
3292{
3293 OSMO_ASSERT(net->vlr);
3294 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3295 net->gsup_server_port);
3296}