blob: a90ff172f94aa1256ac913d30ad98f8854d0c388 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte2483f1b2016-06-19 18:06:02 +02004/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Neels Hofmeyr90843962017-09-04 15:04:35 +020037#include <osmocom/msc/db.h>
38#include <osmocom/msc/debug.h>
39#include <osmocom/msc/gsm_data.h>
40#include <osmocom/msc/gsm_subscriber.h>
41#include <osmocom/msc/gsm_04_11.h>
42#include <osmocom/msc/gsm_04_08.h>
43#include <osmocom/msc/gsm_04_80.h>
44#include <osmocom/msc/gsm_04_14.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020045#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/transaction.h>
47#include <osmocom/msc/ussd.h>
48#include <osmocom/msc/silent_call.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020049#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020050#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010052#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020053#include <osmocom/msc/vlr.h>
54#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080055
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/gsm/gsm48.h>
57#include <osmocom/gsm/gsm0480.h>
58#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020059#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/msgb.h>
61#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010062#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010063#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010064#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020065#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020066#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020067#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020068#endif
Harald Welte52b1f982008-12-23 20:25:15 +000069
Neels Hofmeyr90843962017-09-04 15:04:35 +020070#include <osmocom/msc/msc_ifaces.h>
71#include <osmocom/msc/a_iface.h>
Philipp Maier621ba032017-11-07 17:19:25 +010072#include <osmocom/msc/msc_mgcp.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020073
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020074#include <assert.h>
75
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020076
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020077void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010078void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020079
Harald Welte2483f1b2016-06-19 18:06:02 +020080static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
81 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080082static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020083 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000084
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080086
Neels Hofmeyr9d744252018-03-22 16:09:50 +010087static bool classmark_is_r99(struct gsm_classmark *cm)
88{
89 int rev_lev = 0;
90 if (cm->classmark1_set)
91 rev_lev = cm->classmark1.rev_lev;
92 else if (cm->classmark2_len > 0)
93 rev_lev = (cm->classmark2[0] >> 5) & 0x3;
94 return rev_lev >= 2;
95}
96
Harald Welte71330722017-12-23 19:59:02 +010097/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher */
98static bool classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
99{
100 switch (a5) {
101 case 0:
102 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
103 return true;
104 case 1:
105 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
106 * "The network shall not provide service to an MS which indicates that it does not
107 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
108 * on policy here */
109 /* See 3GPP TS 24.008 10.5.1.7 */
110 if (!cm->classmark1_set) {
111 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
112 return true;
113 } else {
114 if (cm->classmark1.a5_1)
115 return false; /* Inverted logic for this bit! */
116 else
117 return true;
118 }
119 break;
120 case 2:
121 case 3:
122 /* See 3GPP TS 24.008 10.5.1.6 */
123 if (cm->classmark2_len < 3) {
124 DEBUGP(DMSC, "CLASSMARK 2 unknown, assuming MS doesn't support A5/%u\n", a5);
125 return false;
126 } else {
127 if (cm->classmark2[2] & (1 << (a5-2)))
128 return true;
129 else
130 return false;
131 }
132 break;
133 case 4:
134 case 5:
135 case 6:
136 case 7:
137 /* See 3GPP TS 24.008 10.5.1.7 */
138 if (cm->classmark3_len < 1) {
139 DEBUGP(DMSC, "CLASSMARK 3 unknown, assuming MS doesn't support A5/%u\n", a5);
140 return false;
141 } else {
142 if (cm->classmark3[0] & (1 << (a5-4)))
143 return true;
144 else
145 return false;
146 }
147 break;
148 default:
149 return false;
150 }
151}
152
Harald Welte31c00f72011-03-03 23:29:05 +0100153void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
154{
155 net->mncc_recv(net, msg);
156}
157
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800158static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
159 struct gsm_trans *trans)
160{
161 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
162
163 /* if we get passed a transaction reference, do some common
164 * work that the caller no longer has to do */
165 if (trans) {
166 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800167 }
168
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200169 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800170}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100171
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800172int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
173{
174 struct gsm48_hdr *gh;
175 struct msgb *ss_notify;
176
177 ss_notify = gsm0480_create_notifySS(message);
178 if (!ss_notify)
179 return -1;
180
181 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
182 uint8_t *data = msgb_push(ss_notify, 1);
183 data[0] = ss_notify->len - 1;
184 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
185 gh->msg_type = GSM48_MT_CC_FACILITY;
186 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
187}
188
Harald Welte2483f1b2016-06-19 18:06:02 +0200189/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100190void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
191{
192 struct gsm_trans *trans, *temp;
193
194 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
195
196 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100197 if (trans->protocol == protocol) {
198 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100199 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100200 }
Harald Welte371efe52010-12-22 23:17:50 +0100201 }
202}
203
Holger Freyther429e7762008-12-30 13:28:30 +0000204/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200205static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000206{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800207 struct msgb *msg;
208
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800209 msg = gsm48_create_loc_upd_rej(cause);
210 if (!msg) {
211 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
212 return -1;
213 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200214
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200215 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
216 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100217
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800218 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000219}
220
221/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200222static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
223 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000224{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100225 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000226 struct gsm48_hdr *gh;
227 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200228 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100229 struct osmo_location_area_id laid = {
230 .plmn = conn->network->plmn,
231 .lac = conn->lac,
232 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200233
Harald Welte52b1f982008-12-23 20:25:15 +0000234 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
235 gh->proto_discr = GSM48_PDISC_MM;
236 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
237
238 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100239 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000240
Harald Welte2483f1b2016-06-19 18:06:02 +0200241 if (send_tmsi == GSM_RESERVED_TMSI) {
242 /* we did not allocate a TMSI to the MS, so we need to
243 * include the IMSI in order for the MS to delete any
244 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200245 uint8_t mi[10];
246 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200247 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200248 mid = msgb_put(msg, len);
249 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200250 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
251 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200252 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200253 /* Include the TMSI, which means that the MS will send a
254 * TMSI REALLOCATION COMPLETE, and we should wait for
255 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200256 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200257 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200258 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
259 vlr_subscr_name(conn->vsub),
260 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200261 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200262 /* TODO: Follow-on proceed */
263 /* TODO: CTS permission */
264 /* TODO: Equivalent PLMNs */
265 /* TODO: Emergency Number List */
266 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000267
Harald Welte52b1f982008-12-23 20:25:15 +0000268
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800269 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000270}
271
Harald Weltebf5e8df2009-02-03 12:59:45 +0000272/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200273static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000274{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100275 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000276 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000277
Harald Welte231ad4f2008-12-27 11:15:38 +0000278 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
279 gh->proto_discr = GSM48_PDISC_MM;
280 gh->msg_type = GSM48_MT_MM_ID_REQ;
281 gh->data[0] = id_type;
282
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800283 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000284}
285
Harald Weltebf5e8df2009-02-03 12:59:45 +0000286/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800287static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000288{
289 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200290 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200291 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000292
Harald Welte2483f1b2016-06-19 18:06:02 +0200293 if (!conn->vsub) {
294 LOGP(DMM, LOGL_ERROR,
295 "Rx MM Identity Response: invalid: no subscriber\n");
296 return -EINVAL;
297 }
298
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200299 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200300 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
301 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000302
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200303 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100304
Harald Welte2483f1b2016-06-19 18:06:02 +0200305 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000306}
307
Harald Welte2483f1b2016-06-19 18:06:02 +0200308/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200309static const struct value_string lupd_names[] = {
310 { GSM48_LUPD_NORMAL, "NORMAL" },
311 { GSM48_LUPD_PERIODIC, "PERIODIC" },
312 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
313 { 0, NULL }
314};
Harald Welte2a139372009-02-22 21:14:55 +0000315
Harald Welte2483f1b2016-06-19 18:06:02 +0200316/* Chapter 9.2.15: Receive Location Updating Request.
317 * Keep this function non-static for direct invocation by unit tests. */
318int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000319{
Harald Welte2483f1b2016-06-19 18:06:02 +0200320 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000321 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000322 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200323 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200324 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200325 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200326 uint32_t tmsi;
327 char *imsi;
328 struct osmo_location_area_id old_lai, new_lai;
329 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200330 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200331 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000332
Harald Welte8470bf22008-12-25 23:28:35 +0000333 lu = (struct gsm48_loc_upd_req *) gh->data;
334
335 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000336
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200337 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000338
Harald Welte2483f1b2016-06-19 18:06:02 +0200339 rc = msc_create_conn_fsm(conn, mi_string);
340 if (rc)
341 /* logging already happened in msc_create_conn_fsm() */
342 return rc;
343
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200344 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_LU, mi_string);
345
Harald Welte2483f1b2016-06-19 18:06:02 +0200346 conn->classmark.classmark1 = lu->classmark1;
347 conn->classmark.classmark1_set = true;
348
349 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
350 gsm48_mi_type_name(mi_type), mi_string,
351 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000352
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200353 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100354
Harald Welte24ff6ee2009-12-22 00:41:05 +0100355 switch (lu->type) {
356 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200357 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200358 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100359 break;
360 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200361 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200362 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100363 break;
364 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200365 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200366 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100367 break;
368 }
369
Harald Welte2483f1b2016-06-19 18:06:02 +0200370 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
371 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
372 /* TODO: 10.5.7.8 Device properties */
373 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200374
Harald Welte52b1f982008-12-23 20:25:15 +0000375 switch (mi_type) {
376 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200377 tmsi = GSM_RESERVED_TMSI;
378 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000379 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000380 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200381 tmsi = tmsi_from_string(mi_string);
382 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000383 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200384 default:
Harald Weltea0368542009-06-27 02:58:43 +0200385 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200386 tmsi = GSM_RESERVED_TMSI;
387 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000388 break;
389 }
390
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100391 gsm48_decode_lai2(&lu->lai, &old_lai);
392 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200393 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200394 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200395
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200396 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200397 lu_fsm = vlr_loc_update(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200398 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200399 net->vlr, conn, vlr_lu_type, tmsi, imsi,
400 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200401 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100402 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200403 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200404 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200405 net->vlr->cfg.assign_tmsi);
406 if (!lu_fsm) {
407 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
408 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800409 }
410
Harald Welte2483f1b2016-06-19 18:06:02 +0200411 /* From vlr_loc_update() we expect an implicit dispatch of
412 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
413 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000414
Harald Welte2483f1b2016-06-19 18:06:02 +0200415 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
416 LOGP(DRR, LOGL_ERROR,
417 "%s: internal error during Location Updating attempt\n",
418 mi_string);
419 return -EIO;
420 }
421
422 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000423}
424
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100425/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200426/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100427static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800428{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100429 uint8_t ret;
430
431 ret = value / 10;
432 ret |= (value % 10) << 4;
433
434 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800435}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100436
Harald Welte4bfdfe72009-06-10 23:11:52 +0800437
Harald Weltedb253af2008-12-30 17:56:55 +0000438/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800439int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000440{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100441 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000442 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200443 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200444 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200445 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100446
Harald Welte4bfdfe72009-06-10 23:11:52 +0800447 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100448 struct tm* gmt_time;
449 struct tm* local_time;
450 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100451 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000452
Harald Weltedb253af2008-12-30 17:56:55 +0000453 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
454 gh->proto_discr = GSM48_PDISC_MM;
455 gh->msg_type = GSM48_MT_MM_INFO;
456
457 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200458#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000459 name_len = strlen(net->name_long);
460 /* 10.5.3.5a */
461 ptr8 = msgb_put(msg, 3);
462 ptr8[0] = GSM48_IE_NAME_LONG;
463 ptr8[1] = name_len*2 +1;
464 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
465
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200466 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000467 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000468 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000469
470 /* FIXME: Use Cell Broadcast, not UCS-2, since
471 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200472#endif
473 name_len = (strlen(net->name_long)*7)/8;
474 name_pad = (8 - strlen(net->name_long)*7)%8;
475 if (name_pad > 0)
476 name_len++;
477 /* 10.5.3.5a */
478 ptr8 = msgb_put(msg, 3);
479 ptr8[0] = GSM48_IE_NAME_LONG;
480 ptr8[1] = name_len +1;
481 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
482
483 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100484 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200485
Harald Weltedb253af2008-12-30 17:56:55 +0000486 }
487
488 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200489#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000490 name_len = strlen(net->name_short);
491 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200492 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200493 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000494 ptr8[1] = name_len*2 + 1;
495 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
496
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200497 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000498 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000499 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200500#endif
501 name_len = (strlen(net->name_short)*7)/8;
502 name_pad = (8 - strlen(net->name_short)*7)%8;
503 if (name_pad > 0)
504 name_len++;
505 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200506 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200507 ptr8[0] = GSM48_IE_NAME_SHORT;
508 ptr8[1] = name_len +1;
509 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
510
511 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100512 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200513
Harald Weltedb253af2008-12-30 17:56:55 +0000514 }
515
Harald Weltedb253af2008-12-30 17:56:55 +0000516 /* Section 10.5.3.9 */
517 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100518 gmt_time = gmtime(&cur_t);
519
Harald Weltedb253af2008-12-30 17:56:55 +0000520 ptr8 = msgb_put(msg, 8);
521 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100522 ptr8[1] = bcdify(gmt_time->tm_year % 100);
523 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
524 ptr8[3] = bcdify(gmt_time->tm_mday);
525 ptr8[4] = bcdify(gmt_time->tm_hour);
526 ptr8[5] = bcdify(gmt_time->tm_min);
527 ptr8[6] = bcdify(gmt_time->tm_sec);
528
Neels Hofmeyr73983952016-05-10 13:29:33 +0200529 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200530 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200531 if (net->tz.hr < 0) {
532 tzunits = ((net->tz.hr/-1)*4);
533 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100534 ptr8[7] = bcdify(tzunits);
535 /* Set negative time */
536 ptr8[7] |= 0x08;
537 }
538 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200539 tzunits = net->tz.hr*4;
540 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100541 ptr8[7] = bcdify(tzunits);
542 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100543 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200544 if (net->tz.dst >= 0 && net->tz.dst <= 2)
545 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100546 }
547 else {
548 /* Need to get GSM offset and convert into 15 min units */
549 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200550#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200551 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100552 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200553#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200554 /* find timezone offset */
555 time_t utc;
556 double offsetFromUTC;
557 utc = mktime(gmt_time);
558 local_time = localtime(&cur_t);
559 offsetFromUTC = difftime(cur_t, utc);
560 if (local_time->tm_isdst)
561 offsetFromUTC += 3600.0;
562 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200563#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100564 if (tzunits < 0) {
565 tzunits = tzunits/-1;
566 ptr8[7] = bcdify(tzunits);
567 /* Flip it to negative */
568 ptr8[7] |= 0x08;
569 }
570 else
571 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100572
573 /* Does not support DST +2 */
574 if (local_time->tm_isdst)
575 dst = 1;
576 }
577
578 if (dst) {
579 ptr8 = msgb_put(msg, 3);
580 ptr8[0] = GSM48_IE_NET_DST;
581 ptr8[1] = 1;
582 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100583 }
Harald Weltedb253af2008-12-30 17:56:55 +0000584
Daniel Willmanneea93372009-08-13 03:42:07 +0200585 DEBUGP(DMM, "-> MM INFO\n");
586
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800587 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000588}
589
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100590/*! Send an Authentication Request to MS on the given subscriber connection
591 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
592 * \param[in] conn Subscriber connection to send on.
593 * \param[in] rand Random challenge token to send, must be 16 bytes long.
594 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
595 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
596 * \param[in] key_seq auth tuple's sequence number.
597 */
598int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
599 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200600{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100601 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200602 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200603 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200604
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100605 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100606 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100607 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200608
Harald Welte7984d5c2009-08-12 22:56:50 +0200609 gh->proto_discr = GSM48_PDISC_MM;
610 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
611
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100612 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200613
Harald Welte7984d5c2009-08-12 22:56:50 +0200614 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100615 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200616 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200617 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200618
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100619
620 /* 16 bytes AUTN */
621 if (autn)
622 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
623
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800624 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200625}
626
627/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800628int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200629{
630 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800631 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200632}
633
Harald Welte2483f1b2016-06-19 18:06:02 +0200634static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
635static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
636
637static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100638{
Harald Welte2483f1b2016-06-19 18:06:02 +0200639 uint8_t mi_type;
640 char mi_string[GSM48_MI_SIZE];
641 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100642
Harald Welte2483f1b2016-06-19 18:06:02 +0200643 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
644 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100645
Harald Welte2483f1b2016-06-19 18:06:02 +0200646 switch (mi_type) {
647 case GSM_MI_TYPE_IMSI:
648 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
649 goto accept_reuse;
650 break;
651 case GSM_MI_TYPE_TMSI:
652 tmsi = osmo_load32be(mi_lv+2);
653 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
654 goto accept_reuse;
655 break;
656 case GSM_MI_TYPE_IMEI:
657 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
658 goto accept_reuse;
659 break;
660 default:
661 break;
662 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100663
Harald Welte2483f1b2016-06-19 18:06:02 +0200664 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
665 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
666 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
667 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100668
Harald Welte2483f1b2016-06-19 18:06:02 +0200669accept_reuse:
670 DEBUGP(DMM, "%s: re-using already accepted connection\n",
671 vlr_subscr_name(conn->vsub));
672 conn->received_cm_service_request = true;
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200673 msc_subscr_conn_update_id(conn, conn->complete_layer3_type, mi_string);
Harald Welte2483f1b2016-06-19 18:06:02 +0200674 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100675}
676
Harald Welte4ed0e922009-01-10 03:17:30 +0000677/*
678 * Handle CM Service Requests
679 * a) Verify that the packet is long enough to contain the information
680 * we require otherwsie reject with INCORRECT_MESSAGE
681 * b) Try to parse the TMSI. If we do not have one reject
682 * c) Check that we know the subscriber with the TMSI otherwise reject
683 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200684 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200685 *
686 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000687 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200688int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000689{
Harald Welte2483f1b2016-06-19 18:06:02 +0200690 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200691 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200692 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000693
Harald Welteba4cf162009-01-10 01:49:35 +0000694 struct gsm48_hdr *gh = msgb_l3(msg);
695 struct gsm48_service_request *req =
696 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800697 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200698 uint8_t classmark2_len = gh->data[1];
699 uint8_t *classmark2 = gh->data+2;
700 uint8_t mi_len = *(classmark2 + classmark2_len);
701 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200702 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200703 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200704 int rc;
705
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100706 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200707 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000708
Harald Weltec9e02182009-05-01 19:07:53 +0000709 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000710 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000711 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200712 return msc_gsm48_tx_mm_serv_rej(conn,
713 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000714 }
715
716 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000717 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200718 return msc_gsm48_tx_mm_serv_rej(conn,
719 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000720 }
721
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200722 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000723 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welte3995d2e2018-01-24 11:23:54 +0100724 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
725 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200726
Harald Welte3995d2e2018-01-24 11:23:54 +0100727 switch (mi_type) {
728 case GSM_MI_TYPE_IMSI:
729 case GSM_MI_TYPE_TMSI:
730 /* continue below */
731 break;
732 case GSM_MI_TYPE_IMEI:
733 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
734 /* We don't do emergency calls by IMEI */
735 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
736 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
737 }
738 /* fall-through for non-emergency setup */
739 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200740 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200741 return msc_gsm48_tx_mm_serv_rej(conn,
742 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000743 }
744
Harald Welte37382ec2018-01-24 12:49:34 +0100745 switch (req->cm_service_type) {
746 case GSM48_CMSERV_MO_CALL_PACKET:
747 case GSM48_CMSERV_EMERGENCY:
748 case GSM48_CMSERV_SMS:
749 case GSM48_CMSERV_SUP_SERV:
750 /* continue below */
751 break;
752 default:
753 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
754 }
755
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200756 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200757 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
758 conn->classmark.classmark2_len = classmark2_len;
759
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200760 if (conn->fi) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200761 if (msc_subscr_conn_is_accepted(conn))
762 return cm_serv_reuse_conn(conn, mi-1);
763 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
764 vlr_subscr_name(conn->vsub));
765 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
766 return -EINVAL;
767 }
768
769 rc = msc_create_conn_fsm(conn, mi_string);
770 if (rc) {
771 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
772 /* logging already happened in msc_create_conn_fsm() */
773 return rc;
774 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200775 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_CM_SERVICE_REQ, mi_string);
Harald Welte7659de12009-12-13 12:39:18 +0100776
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200777 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200778 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200779 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200780 net->vlr, conn,
781 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200782 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100783 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200784 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200785 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000786
Harald Welte2483f1b2016-06-19 18:06:02 +0200787 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000788}
789
Harald Welte49f8fcb2018-01-24 21:40:19 +0100790/* Receive a CM Re-establish Request */
791static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
792{
793 uint8_t mi_type;
794 char mi_string[GSM48_MI_SIZE];
795 struct gsm48_hdr *gh = msgb_l3(msg);
796
797 uint8_t classmark2_len = gh->data[1];
798 uint8_t *classmark2 = gh->data+2;
799 uint8_t mi_len = *(classmark2 + classmark2_len);
800 uint8_t *mi = (classmark2 + classmark2_len + 1);
801
802 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
803 mi_type = mi[0] & GSM_MI_TYPE_MASK;
804 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
805
806 /* we don't support CM call re-establishment */
807 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
808}
809
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200810static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000811{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200812 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000813 struct gsm48_hdr *gh = msgb_l3(msg);
814 struct gsm48_imsi_detach_ind *idi =
815 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200816 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200817 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200818 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000819
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200820 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200821 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
822 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000823
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200824 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100825
Harald Welte2a139372009-02-22 21:14:55 +0000826 switch (mi_type) {
827 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200828 vsub = vlr_subscr_find_by_tmsi(network->vlr,
829 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000830 break;
831 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200832 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000833 break;
834 case GSM_MI_TYPE_IMEI:
835 case GSM_MI_TYPE_IMEISV:
836 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200837 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
838 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000839 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200840 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200841 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
842 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000843 break;
844 }
845
Harald Welte2483f1b2016-06-19 18:06:02 +0200846 /* TODO? We used to remember the subscriber's classmark1 here and
847 * stored it in the old sqlite db, but now we store it in a conn that
848 * will be discarded anyway: */
849 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200850
Harald Welte2483f1b2016-06-19 18:06:02 +0200851 if (!vsub) {
852 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
853 gsm48_mi_type_name(mi_type), mi_string);
854 } else {
855 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100856
857 if (vsub->cs.is_paging)
858 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
859
Harald Welte2483f1b2016-06-19 18:06:02 +0200860 vlr_subscr_rx_imsi_detach(vsub);
861 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
862 vlr_subscr_put(vsub);
863 }
Harald Welte2a139372009-02-22 21:14:55 +0000864
Harald Welte2483f1b2016-06-19 18:06:02 +0200865 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000866 return 0;
867}
868
Harald Welted2a7f5a2009-06-05 20:08:20 +0000869static int gsm48_rx_mm_status(struct msgb *msg)
870{
871 struct gsm48_hdr *gh = msgb_l3(msg);
872
873 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
874
875 return 0;
876}
877
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100878static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
879 struct gsm_subscriber_connection *conn,
880 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100881{
882 struct gsm48_hdr *gh = msgb_l3(msg);
883 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100884
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100885 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
886 LOGP(DMM, LOGL_ERROR,
887 "%s: MM AUTHENTICATION RESPONSE:"
888 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200889 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100890 return -EINVAL;
891 }
892
893 *res_len = sizeof(ar->sres);
894 memcpy(res, ar->sres, sizeof(ar->sres));
895 return 0;
896}
897
898static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
899 struct gsm_subscriber_connection *conn,
900 struct msgb *msg)
901{
902 struct gsm48_hdr *gh;
903 uint8_t *data;
904 uint8_t iei;
905 uint8_t ie_len;
906 unsigned int data_len;
907
908 /* First parse the GSM part */
909 if (parse_gsm_auth_resp(res, res_len, conn, msg))
910 return -EINVAL;
911 OSMO_ASSERT(*res_len == 4);
912
913 /* Then add the extended res part */
914 gh = msgb_l3(msg);
915 data = gh->data + sizeof(struct gsm48_auth_resp);
916 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
917
918 if (data_len < 3) {
919 LOGP(DMM, LOGL_ERROR,
920 "%s: MM AUTHENTICATION RESPONSE:"
921 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200922 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100923 return -EINVAL;
924 }
925
926 iei = data[0];
927 ie_len = data[1];
928 if (iei != GSM48_IE_AUTH_RES_EXT) {
929 LOGP(DMM, LOGL_ERROR,
930 "%s: MM R99 AUTHENTICATION RESPONSE:"
931 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200932 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100933 GSM48_IE_AUTH_RES_EXT, iei);
934 return -EINVAL;
935 }
936
937 if (ie_len > 12) {
938 LOGP(DMM, LOGL_ERROR,
939 "%s: MM R99 AUTHENTICATION RESPONSE:"
940 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200941 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100942 return -EINVAL;
943 }
944
945 *res_len += ie_len;
946 memcpy(res + 4, &data[2], ie_len);
947 return 0;
948}
949
950/* Chapter 9.2.3: Authentication Response */
951static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
952{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100953 uint8_t res[16];
954 uint8_t res_len;
955 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100956 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100957
Harald Welte2483f1b2016-06-19 18:06:02 +0200958 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100959 LOGP(DMM, LOGL_ERROR,
960 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-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
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100965 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
966
967 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100968 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100969 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100970 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100971
972 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100973 LOGP(DMM, LOGL_ERROR,
974 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
975 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100976 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100977 memset(res, 0, sizeof(res));
978 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100979 }
980
981 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200982 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +0100983 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100984 osmo_hexdump_nospc(res, res_len));
985
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100986 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->classmark),
Harald Welte2483f1b2016-06-19 18:06:02 +0200987 conn->via_ran == RAN_UTRAN_IU,
988 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100989}
990
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100991static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
992{
993 struct gsm48_hdr *gh = msgb_l3(msg);
994 uint8_t cause;
995 uint8_t auts_tag;
996 uint8_t auts_len;
997 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100998
Harald Welte2483f1b2016-06-19 18:06:02 +0200999 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001000 LOGP(DMM, LOGL_ERROR,
1001 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001002 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001003 return -EINVAL;
1004 }
1005
1006 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1007 LOGP(DMM, LOGL_ERROR,
1008 "%s: MM R99 AUTHENTICATION FAILURE:"
1009 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001010 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1011 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001012 return -EINVAL;
1013 }
1014
1015 cause = gh->data[0];
1016
1017 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1018 LOGP(DMM, LOGL_INFO,
1019 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001020 vlr_subscr_name(conn->vsub), cause);
1021 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1022 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001023 }
1024
1025 /* This is a Synch Failure procedure, which should pass an AUTS to
1026 * resynchronize the sequence nr with the HLR. Expecting exactly one
1027 * TLV with 14 bytes of AUTS. */
1028
1029 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1030 LOGP(DMM, LOGL_INFO,
1031 "%s: MM R99 AUTHENTICATION FAILURE:"
1032 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001033 vlr_subscr_name(conn->vsub));
1034 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001035 return -EINVAL;
1036 }
1037
1038 auts_tag = gh->data[1];
1039 auts_len = gh->data[2];
1040 auts = &gh->data[3];
1041
1042 if (auts_tag != GSM48_IE_AUTS
1043 || auts_len != 14) {
1044 LOGP(DMM, LOGL_INFO,
1045 "%s: MM R99 AUTHENTICATION FAILURE:"
1046 " invalid Synch Failure:"
1047 " expected AUTS IE 0x%02x of 14 bytes,"
1048 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001049 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001050 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +02001051 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001052 return -EINVAL;
1053 }
1054
1055 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1056 LOGP(DMM, LOGL_INFO,
1057 "%s: MM R99 AUTHENTICATION FAILURE:"
1058 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001059 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1060 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001061 return -EINVAL;
1062 }
1063
1064 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1065 * large enough. */
1066
1067 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001068 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001069
Harald Welte2483f1b2016-06-19 18:06:02 +02001070 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1071}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001072
Harald Welte2483f1b2016-06-19 18:06:02 +02001073static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1074{
1075 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1076 vlr_subscr_name(conn->vsub));
1077 if (!conn->vsub) {
1078 LOGP(DMM, LOGL_ERROR,
1079 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1080 return -EINVAL;
1081 }
1082 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001083}
1084
Harald Weltebf5e8df2009-02-03 12:59:45 +00001085/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001086static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001087{
1088 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001089 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001090
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001091 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001092 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001093 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001094 break;
1095 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001096 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001097 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001098 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001099 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001100 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001101 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001102 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001103 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001104 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001105 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001106 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001107 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001108 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001109 break;
1110 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001111 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001112 break;
1113 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001114 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001115 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001116 case GSM48_MT_MM_AUTH_FAIL:
1117 rc = gsm48_rx_mm_auth_fail(conn, msg);
1118 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001119 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001120 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001121 gh->msg_type);
1122 break;
1123 }
1124
1125 return rc;
1126}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001127
Harald Welte2483f1b2016-06-19 18:06:02 +02001128static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1129{
1130 /* Check the size for the classmark */
1131 if (tot_len < 1 + *classmark2_lv)
1132 return NULL;
1133
1134 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1135 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1136 return NULL;
1137
1138 return mi_lv;
1139}
1140
Harald Welte2d35ae62009-02-06 12:02:13 +00001141/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001142static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001143{
Harald Welte2483f1b2016-06-19 18:06:02 +02001144 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001145 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001146 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001147 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001148 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001149 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001150 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001151 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001152 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001153 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001154
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001155 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001156 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001157
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001158 resp = (struct gsm48_pag_resp *) &gh->data[0];
1159 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1160 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001161 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1162 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001163
Harald Welte2483f1b2016-06-19 18:06:02 +02001164 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1165 if (!mi_lv) {
1166 /* FIXME */
1167 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001168 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001169
Harald Welte2483f1b2016-06-19 18:06:02 +02001170 rc = msc_create_conn_fsm(conn, mi_string);
1171 if (rc)
1172 /* logging already happened in msc_create_conn_fsm() */
1173 return rc;
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001174 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_PAGING_RESP, mi_string);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001175
Harald Welte2483f1b2016-06-19 18:06:02 +02001176 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1177 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001178
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001179 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +02001180 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001181 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +02001182 net->vlr, conn,
1183 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001184 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001185 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +02001186 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001187 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001188
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001189 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001190}
1191
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001192static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001193{
1194 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001195 uint8_t apdu_id_flags;
1196 uint8_t apdu_len;
1197 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001198
1199 apdu_id_flags = gh->data[0];
1200 apdu_len = gh->data[1];
1201 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001202
Max8dc8f232017-02-09 19:13:02 +01001203 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001204 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001205
Harald Welte2483f1b2016-06-19 18:06:02 +02001206 /* we're not using the app info blob anywhere, so ignore. */
1207#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001208 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001209#else
1210 return 0;
1211#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001212}
1213
Harald Weltebf5e8df2009-02-03 12:59:45 +00001214/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001215static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001216{
1217 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001218 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001219
1220 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001221 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001222 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001223 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001224 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001225 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001226 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001227 default:
Philippd37981e2016-10-10 15:11:41 +02001228 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1229 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001230 break;
1231 }
1232
Harald Welte2d35ae62009-02-06 12:02:13 +00001233 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001234}
1235
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001236int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1237 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001238{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001239 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001240 struct gsm48_hdr *gh;
1241
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001242 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1243 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001244
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001245 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1246 gh->proto_discr = GSM48_PDISC_RR;
1247 gh->msg_type = GSM48_MT_RR_APP_INFO;
1248 gh->data[0] = apdu_id;
1249 gh->data[1] = apdu_len;
1250 memcpy(gh->data+2, apdu, apdu_len);
1251
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001252 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001253}
1254
Alexander Couzens92f552f2016-08-23 07:32:27 +02001255/* FIXME: this count_statistics is a state machine behaviour. we should convert
1256 * the complete call control into a state machine. Afterwards we can move this
1257 * code into state transitions.
1258 */
1259static void count_statistics(struct gsm_trans *trans, int new_state)
1260{
1261 int old_state = trans->cc.state;
1262 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1263
1264 if (old_state == new_state)
1265 return;
1266
1267 /* state incoming */
1268 switch (new_state) {
1269 case GSM_CSTATE_ACTIVE:
1270 osmo_counter_inc(trans->net->active_calls);
1271 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1272 break;
1273 }
1274
1275 /* state outgoing */
1276 switch (old_state) {
1277 case GSM_CSTATE_ACTIVE:
1278 osmo_counter_dec(trans->net->active_calls);
1279 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1280 new_state == GSM_CSTATE_DISCONNECT_IND)
1281 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1282 else
1283 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1284 break;
1285 }
1286}
1287
Harald Welte4bc90a12008-12-27 16:32:52 +00001288/* Call Control */
1289
Harald Welte7584aea2009-02-11 11:44:12 +00001290/* The entire call control code is written in accordance with Figure 7.10c
1291 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1292 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1293 * it for voice */
1294
Harald Welte4bfdfe72009-06-10 23:11:52 +08001295static void new_cc_state(struct gsm_trans *trans, int state)
1296{
1297 if (state > 31 || state < 0)
1298 return;
1299
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001300 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1301 trans->transaction_id,
1302 vlr_subscr_name(trans->vsub),
1303 gsm48_cc_state_name(trans->cc.state),
1304 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305
Alexander Couzens92f552f2016-08-23 07:32:27 +02001306 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001307 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001308}
1309
1310static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001311{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001312 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001313 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001314 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001315
Harald Welte4bc90a12008-12-27 16:32:52 +00001316 gh->msg_type = GSM48_MT_CC_STATUS;
1317
1318 cause = msgb_put(msg, 3);
1319 cause[0] = 2;
1320 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1321 cause[2] = 0x80 | 30; /* response to status inquiry */
1322
1323 call_state = msgb_put(msg, 1);
1324 call_state[0] = 0xc0 | 0x00;
1325
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001326 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001327}
1328
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001329static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001330 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001331{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001332 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001333 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1334
Harald Welte6f4b7532008-12-29 00:39:37 +00001335 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001336 gh->msg_type = msg_type;
1337
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001338 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001339}
1340
Harald Welte4bfdfe72009-06-10 23:11:52 +08001341static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1342{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001343 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001344 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001345 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001346 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347 }
1348}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001349
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1351 int msg_type, struct gsm_mncc *mncc)
1352{
1353 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001354 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001355
Philipp Maierfbf66102017-04-09 12:32:51 +02001356 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1357
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001358#if BEFORE_MSCSPLIT
1359 /* Re-enable this log output once we can obtain this information via
1360 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001362 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001363 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001365 trans->conn->lchan->ts->trx->bts->nr,
1366 trans->conn->lchan->ts->trx->nr,
1367 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001368 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 get_mncc_name(msg_type));
1370 else
1371 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1372 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001373 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 get_mncc_name(msg_type));
1375 else
1376 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1377 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001378#else
1379 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1380#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381
1382 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001383
Harald Welte966636f2009-06-26 19:39:35 +02001384 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001385 if (!msg)
1386 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001387
1388 data = msgb_put(msg, sizeof(struct gsm_mncc));
1389 memcpy(data, mncc, sizeof(struct gsm_mncc));
1390
Harald Welte54209c22010-12-22 23:43:29 +01001391 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392
1393 return 0;
1394}
1395
1396int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001397 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001398{
1399 struct gsm_mncc rel;
1400
Harald Welte92f70c52009-06-12 01:54:08 +08001401 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001403 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001404 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1405 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1407}
1408
Harald Weltedcaf5652009-07-23 18:56:43 +02001409/* Call Control Specific transaction release.
1410 * gets called by trans_free, DO NOT CALL YOURSELF! */
1411void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001412{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001413 gsm48_stop_cc_timer(trans);
1414
Philipp Maier621ba032017-11-07 17:19:25 +01001415 /* Initiate the teardown of the related connections on the MGW */
1416 msc_mgcp_call_release(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001417
Harald Welte4bfdfe72009-06-10 23:11:52 +08001418 /* send release to L4, if callref still exists */
1419 if (trans->callref) {
1420 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001421 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001422 GSM48_CAUSE_LOC_PRN_S_LU,
1423 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001424 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001425 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427}
1428
1429static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001430
Harald Welte09e38af2009-02-16 22:52:23 +00001431/* call-back from paging the B-end of the connection */
1432static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001433 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001434{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001435 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001436 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001437
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001438 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001439
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001440 switch (event) {
1441 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001442 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1443 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001444 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001445 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001446 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001447 /* send SETUP request to called party */
1448 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1449 break;
1450 case GSM_PAGING_EXPIRED:
1451 case GSM_PAGING_BUSY:
1452 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001453 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001454 /* Temporarily out of order */
1455 mncc_release_ind(transt->net, transt,
1456 transt->callref,
1457 GSM48_CAUSE_LOC_PRN_S_LU,
1458 GSM48_CC_CAUSE_DEST_OOO);
1459 transt->callref = 0;
1460 transt->paging_request = NULL;
1461 trans_free(transt);
1462 break;
1463 default:
1464 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1465 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001466 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001467
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001468 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001469 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001470}
Harald Welte7584aea2009-02-11 11:44:12 +00001471
Harald Welte4bfdfe72009-06-10 23:11:52 +08001472/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001473static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001474{
Harald Welte53d51f52015-12-03 14:59:04 +01001475 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1476 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Philipp Maier621ba032017-11-07 17:19:25 +01001477 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001478
Harald Welte4bfdfe72009-06-10 23:11:52 +08001479 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001480 return -EIO;
1481
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001482 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001483 return -EIO;
1484
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001485 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001486 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001487
Philipp Maier621ba032017-11-07 17:19:25 +01001488 /* Bridge RTP streams */
1489 rc = msc_mgcp_call_complete(trans1, trans2->conn->rtp.local_port_cn,
1490 trans2->conn->rtp.local_addr_cn);
1491 if (rc)
1492 return -EINVAL;
1493
1494 rc = msc_mgcp_call_complete(trans2, trans1->conn->rtp.local_port_cn,
1495 trans1->conn->rtp.local_addr_cn);
1496 if (rc)
1497 return -EINVAL;
1498
1499 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001500}
1501
Harald Welte4bfdfe72009-06-10 23:11:52 +08001502static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1503{
1504 DEBUGP(DCC, "-> STATUS ENQ\n");
1505 return gsm48_cc_tx_status(trans, msg);
1506}
1507
1508static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1509static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1510
1511static void gsm48_cc_timeout(void *arg)
1512{
1513 struct gsm_trans *trans = arg;
1514 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001515 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1516 int mo_location = GSM48_CAUSE_LOC_USER;
1517 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1518 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001519 struct gsm_mncc mo_rel, l4_rel;
1520
1521 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1522 mo_rel.callref = trans->callref;
1523 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1524 l4_rel.callref = trans->callref;
1525
Harald Weltedcaf5652009-07-23 18:56:43 +02001526 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001527 case 0x303:
1528 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001529 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001530 break;
1531 case 0x310:
1532 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001533 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001534 break;
1535 case 0x313:
1536 disconnect = 1;
1537 /* unknown, did not find it in the specs */
1538 break;
1539 case 0x301:
1540 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001541 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001542 break;
1543 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001544 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001545 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001546 gsm48_cc_tx_release(trans, &trans->cc.msg);
1547 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001548 break; /* stay in release state */
1549 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001550 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001551 return;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001552 case 0x306:
1553 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001554 mo_cause = trans->cc.msg.cause.value;
1555 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001556 break;
1557 case 0x323:
1558 disconnect = 1;
1559 break;
1560 default:
1561 release = 1;
1562 }
1563
1564 if (release && trans->callref) {
1565 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001566 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001567 l4_location, l4_cause);
1568 trans->callref = 0;
1569 }
1570
1571 if (disconnect && trans->callref) {
1572 /* process disconnect towards layer 4 */
1573 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001574 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001575 }
1576
1577 /* process disconnect towards mobile station */
1578 if (disconnect || release) {
1579 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001580 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1581 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1582 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001583 mo_rel.cause.diag_len = 3;
1584
1585 if (disconnect)
1586 gsm48_cc_tx_disconnect(trans, &mo_rel);
1587 if (release)
1588 gsm48_cc_tx_release(trans, &mo_rel);
1589 }
1590
1591}
1592
Max3ffce192016-04-25 15:22:00 +02001593/* disconnect both calls from the bridge */
1594static inline void disconnect_bridge(struct gsm_network *net,
1595 struct gsm_mncc_bridge *bridge, int err)
1596{
1597 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1598 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1599 struct gsm_mncc mx_rel;
1600 if (!trans0 || !trans1)
1601 return;
1602
1603 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1604 trans0->callref, trans1->callref, strerror(err));
1605
1606 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1607 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1608 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1609
1610 mx_rel.callref = trans0->callref;
1611 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1612
1613 mx_rel.callref = trans1->callref;
1614 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1615}
1616
Harald Welte4bfdfe72009-06-10 23:11:52 +08001617static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1618 int sec, int micro)
1619{
1620 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001621 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001622 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001623 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001624}
1625
1626static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1627{
1628 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001629 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001630 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1631 struct tlv_parsed tp;
1632 struct gsm_mncc setup;
1633
1634 memset(&setup, 0, sizeof(struct gsm_mncc));
1635 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001636
Harald Welte474d19f2010-03-02 23:18:30 +01001637 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001638 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001639 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1640 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001641 setup.emergency = 1;
Harald Welte69c54a82018-02-09 20:41:14 +01001642 /* use destination number as configured by user (if any) */
1643 if (trans->net->emergency.route_to_msisdn) {
1644 setup.fields |= MNCC_F_CALLED;
1645 setup.called.type = 0; /* unknown */
1646 setup.called.plan = 0; /* unknown */
1647 OSMO_STRLCPY_ARRAY(setup.called.number,
1648 trans->net->emergency.route_to_msisdn);
1649 }
Harald Welte0dd01372018-01-24 11:06:19 +01001650 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651
1652 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001653 setup.fields |= MNCC_F_CALLING;
Max98f74672018-02-05 12:57:06 +01001654 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
1655 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001656
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 /* bearer capability */
1658 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1659 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001660 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001661 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001662
1663 /* Create a copy of the bearer capability
1664 * in the transaction struct, so we can use
1665 * this information later */
1666 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1667 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668 }
1669 /* facility */
1670 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1671 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001672 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1674 }
1675 /* called party bcd number */
1676 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1677 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001678 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1680 }
1681 /* user-user */
1682 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1683 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001684 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1686 }
1687 /* ss-version */
1688 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1689 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001690 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001691 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1692 }
1693 /* CLIR suppression */
1694 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1695 setup.clir.sup = 1;
1696 /* CLIR invocation */
1697 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1698 setup.clir.inv = 1;
1699 /* cc cap */
1700 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1701 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001702 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1704 }
1705
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706 new_cc_state(trans, GSM_CSTATE_INITIATED);
1707
Harald Welte79826e12018-01-24 10:50:24 +01001708 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1709 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001710 setup.called.number);
1711
Alexander Couzensb847a212016-08-02 11:34:11 +02001712 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001713
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001715 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716
Harald Welte13cac662009-07-29 12:10:35 +02001717 /* MNCC code will modify the channel asynchronously, we should
1718 * ipaccess-bind only after the modification has been made to the
1719 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720 return 0;
1721}
1722
1723static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001724{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001725 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001726 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001728 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001729
Harald Welte7ccf7782009-02-17 01:43:01 +00001730 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001731
Harald Welte4bfdfe72009-06-10 23:11:52 +08001732 /* transaction id must not be assigned */
1733 if (trans->transaction_id != 0xff) { /* unasssigned */
1734 DEBUGP(DCC, "TX Setup with assigned transaction. "
1735 "This is not allowed!\n");
1736 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001737 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001738 GSM48_CAUSE_LOC_PRN_S_LU,
1739 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001741 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 return rc;
1743 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001744
Harald Welte4bfdfe72009-06-10 23:11:52 +08001745 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001746 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001747 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001748 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001750 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001751 GSM48_CAUSE_LOC_PRN_S_LU,
1752 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001753 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001754 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755 return rc;
1756 }
Harald Welte78283ef2009-07-23 21:36:44 +02001757 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001758
Harald Welte65e74cc2008-12-29 01:55:35 +00001759 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001760
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001762
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763 /* bearer capability */
Harald Welte32958872018-02-12 11:56:51 +01001764 if (setup->fields & MNCC_F_BEARER_CAP) {
1765 /* Create a copy of the bearer capability in the transaction struct, so we
1766 * can use this information later */
1767 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte55c8f352010-03-07 23:40:35 +01001768 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte32958872018-02-12 11:56:51 +01001769 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 /* facility */
1771 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001772 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773 /* progress */
1774 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001775 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 /* calling party BCD number */
1777 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001778 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 /* called party BCD number */
1780 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001781 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 /* user-user */
1783 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001784 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 /* redirecting party BCD number */
1786 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001787 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 /* signal */
1789 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001790 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001791
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001793
Alexander Couzensb847a212016-08-02 11:34:11 +02001794 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001795
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001796 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001797}
1798
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1800{
1801 struct gsm48_hdr *gh = msgb_l3(msg);
1802 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1803 struct tlv_parsed tp;
1804 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001805 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001806
1807 gsm48_stop_cc_timer(trans);
1808 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1809
1810 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1811 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001812
Harald Welte474d19f2010-03-02 23:18:30 +01001813 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814#if 0
1815 /* repeat */
1816 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1817 call_conf.repeat = 1;
1818 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1819 call_conf.repeat = 2;
1820#endif
1821 /* bearer capability */
1822 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1823 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001824 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001826
1827 /* Create a copy of the bearer capability
1828 * in the transaction struct, so we can use
1829 * this information later */
1830 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1831 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001832 }
1833 /* cause */
1834 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1835 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001836 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1838 }
1839 /* cc cap */
1840 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1841 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001842 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1844 }
1845
Andreas Eversberg035b8742013-09-19 09:27:06 +02001846 /* IMSI of called subscriber */
Max98f74672018-02-05 12:57:06 +01001847 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
Andreas Eversberg035b8742013-09-19 09:27:06 +02001848
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1850
Philipp Maierfbf66102017-04-09 12:32:51 +02001851 /* Assign call (if not done yet) */
1852 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001853 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001854 trans->assignment_done = true;
1855 }
1856 else
1857 rc = 0;
1858
1859 /* don't continue, if there were problems with
1860 * the call assignment. */
1861 if (rc)
1862 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001863
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001864 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001865 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866}
1867
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001868static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869{
1870 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001871 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001873 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874
Harald Welte4bfdfe72009-06-10 23:11:52 +08001875 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1876
1877 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1878
1879 /* bearer capability */
Harald Welte4804c552018-02-12 12:30:54 +01001880 if (proceeding->fields & MNCC_F_BEARER_CAP) {
Harald Welte55c8f352010-03-07 23:40:35 +01001881 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01001882 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1883 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 /* facility */
1885 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001886 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887 /* progress */
1888 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001889 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001891 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1892 if (rc)
1893 return rc;
1894
Philipp Maierfbf66102017-04-09 12:32:51 +02001895 /* Assign call (if not done yet) */
1896 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001897 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001898 trans->assignment_done = true;
1899 }
1900 else
1901 rc = 0;
1902
1903 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001904}
1905
1906static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1907{
1908 struct gsm48_hdr *gh = msgb_l3(msg);
1909 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1910 struct tlv_parsed tp;
1911 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001912
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913 gsm48_stop_cc_timer(trans);
1914 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1915
1916 memset(&alerting, 0, sizeof(struct gsm_mncc));
1917 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001918 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919 /* facility */
1920 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1921 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001922 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1924 }
1925
1926 /* progress */
1927 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1928 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001929 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1931 }
1932 /* ss-version */
1933 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1934 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001935 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1937 }
1938
1939 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1940
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001941 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001942 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943}
1944
1945static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1946{
1947 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001948 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1950
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 gh->msg_type = GSM48_MT_CC_ALERTING;
1952
1953 /* facility */
1954 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001955 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001956 /* progress */
1957 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001958 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001959 /* user-user */
1960 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001961 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962
1963 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001964
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001965 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966}
1967
1968static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1969{
1970 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001971 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1973
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 gh->msg_type = GSM48_MT_CC_PROGRESS;
1975
1976 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001977 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978 /* user-user */
1979 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001980 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001982 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983}
1984
1985static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1986{
1987 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001988 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1990
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 gh->msg_type = GSM48_MT_CC_CONNECT;
1992
1993 gsm48_stop_cc_timer(trans);
1994 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1995
1996 /* facility */
1997 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001998 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 /* progress */
2000 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002001 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002 /* connected number */
2003 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002004 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005 /* user-user */
2006 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002007 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008
2009 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2010
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002011 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012}
2013
2014static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2015{
2016 struct gsm48_hdr *gh = msgb_l3(msg);
2017 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2018 struct tlv_parsed tp;
2019 struct gsm_mncc connect;
2020
2021 gsm48_stop_cc_timer(trans);
2022
2023 memset(&connect, 0, sizeof(struct gsm_mncc));
2024 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002025 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002027 connect.fields |= MNCC_F_CONNECTED;
Max98f74672018-02-05 12:57:06 +01002028 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
2029 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002030
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031 /* facility */
2032 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2033 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002034 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2036 }
2037 /* user-user */
2038 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2039 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002040 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2042 }
2043 /* ss-version */
2044 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2045 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002046 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2048 }
2049
2050 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002051 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002053 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054}
2055
2056
2057static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2058{
2059 struct gsm_mncc connect_ack;
2060
2061 gsm48_stop_cc_timer(trans);
2062
2063 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002064 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002065
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2067 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002068
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002069 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 &connect_ack);
2071}
2072
2073static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2074{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002075 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2077
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2079
2080 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2081
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002082 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083}
2084
2085static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2086{
2087 struct gsm48_hdr *gh = msgb_l3(msg);
2088 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2089 struct tlv_parsed tp;
2090 struct gsm_mncc disc;
2091
2092 gsm48_stop_cc_timer(trans);
2093
2094 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2095
2096 memset(&disc, 0, sizeof(struct gsm_mncc));
2097 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002098 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099 /* cause */
2100 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2101 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002102 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2104 }
2105 /* facility */
2106 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2107 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002108 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2110 }
2111 /* user-user */
2112 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2113 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002114 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2116 }
2117 /* ss-version */
2118 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2119 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002120 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2122 }
2123
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002124 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002125
2126}
2127
Harald Weltec66b71c2009-06-11 14:23:20 +08002128static struct gsm_mncc_cause default_cause = {
2129 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2130 .coding = 0,
2131 .rec = 0,
2132 .rec_val = 0,
2133 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2134 .diag_len = 0,
2135 .diag = { 0 },
2136};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137
2138static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2139{
2140 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002141 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2143
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2145
2146 gsm48_stop_cc_timer(trans);
2147 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2148
2149 /* cause */
2150 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002151 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152 else
Harald Welte55c8f352010-03-07 23:40:35 +01002153 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154
2155 /* facility */
2156 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002157 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158 /* progress */
2159 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002160 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161 /* user-user */
2162 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002163 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164
2165 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002166 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167
2168 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2169
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002170 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171}
2172
2173static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2174{
2175 struct gsm48_hdr *gh = msgb_l3(msg);
2176 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2177 struct tlv_parsed tp;
2178 struct gsm_mncc rel;
2179 int rc;
2180
2181 gsm48_stop_cc_timer(trans);
2182
2183 memset(&rel, 0, sizeof(struct gsm_mncc));
2184 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002185 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186 /* cause */
2187 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2188 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002189 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2191 }
2192 /* facility */
2193 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2194 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002195 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2197 }
2198 /* user-user */
2199 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2200 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002201 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002202 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2203 }
2204 /* ss-version */
2205 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2206 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002207 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2209 }
2210
Harald Weltedcaf5652009-07-23 18:56:43 +02002211 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002212 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002213 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002215 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002216 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002217 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002218 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 }
2220
2221 new_cc_state(trans, GSM_CSTATE_NULL);
2222
2223 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002224 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225
2226 return rc;
2227}
2228
2229static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2230{
2231 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002232 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2234
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235 gh->msg_type = GSM48_MT_CC_RELEASE;
2236
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 gsm48_stop_cc_timer(trans);
2238 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2239
2240 /* cause */
2241 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002242 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 /* facility */
2244 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002245 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246 /* user-user */
2247 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002248 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002249
Harald Weltedcaf5652009-07-23 18:56:43 +02002250 trans->cc.T308_second = 0;
2251 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252
Harald Weltedcaf5652009-07-23 18:56:43 +02002253 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
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_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2260{
2261 struct gsm48_hdr *gh = msgb_l3(msg);
2262 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2263 struct tlv_parsed tp;
2264 struct gsm_mncc rel;
2265 int rc = 0;
2266
2267 gsm48_stop_cc_timer(trans);
2268
2269 memset(&rel, 0, sizeof(struct gsm_mncc));
2270 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002271 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 /* cause */
2273 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2274 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002275 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2277 }
2278 /* facility */
2279 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2280 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002281 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002282 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2283 }
2284 /* user-user */
2285 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2286 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002287 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2289 }
2290 /* ss-version */
2291 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2292 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002293 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2295 }
2296
2297 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002298 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002300 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 MNCC_REJ_IND, &rel);
2302 break;
2303 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002304 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305 MNCC_REL_CNF, &rel);
2306 break;
2307 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002308 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 MNCC_REL_IND, &rel);
2310 }
2311 }
2312
2313 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002314 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315
2316 return rc;
2317}
2318
2319static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2320{
2321 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002322 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002324 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2327
2328 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002329
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 gsm48_stop_cc_timer(trans);
2331
2332 /* cause */
2333 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002334 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002335 /* facility */
2336 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002337 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 /* user-user */
2339 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002340 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341
Harald Weltec7782de2010-12-21 19:31:41 +01002342 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2343
Harald Weltedcaf5652009-07-23 18:56:43 +02002344 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345
Harald Weltec7782de2010-12-21 19:31:41 +01002346 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347}
2348
2349static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2350{
2351 struct gsm48_hdr *gh = msgb_l3(msg);
2352 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2353 struct tlv_parsed tp;
2354 struct gsm_mncc fac;
2355
2356 memset(&fac, 0, sizeof(struct gsm_mncc));
2357 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002358 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 /* facility */
2360 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2361 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002362 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2364 }
2365 /* ss-version */
2366 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2367 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002368 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002369 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2370 }
2371
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002372 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373}
2374
2375static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2376{
2377 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002378 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002379 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2380
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 gh->msg_type = GSM48_MT_CC_FACILITY;
2382
2383 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002384 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002386 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387}
2388
2389static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2390{
2391 struct gsm_mncc hold;
2392
2393 memset(&hold, 0, sizeof(struct gsm_mncc));
2394 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002395 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396}
2397
2398static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2399{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002400 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2402
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2404
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002405 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406}
2407
2408static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2409{
2410 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002411 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2413
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2415
2416 /* cause */
2417 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002418 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419 else
Harald Welte55c8f352010-03-07 23:40:35 +01002420 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421
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_retrieve(struct gsm_trans *trans, struct msgb *msg)
2426{
2427 struct gsm_mncc retrieve;
2428
2429 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2430 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002431 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002432 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433}
2434
2435static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2436{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002437 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002438 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2439
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2441
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002442 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443}
2444
2445static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2446{
2447 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002448 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002449 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2450
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2452
2453 /* cause */
2454 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002455 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456 else
Harald Welte55c8f352010-03-07 23:40:35 +01002457 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002459 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460}
2461
2462static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2463{
2464 struct gsm48_hdr *gh = msgb_l3(msg);
2465 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2466 struct tlv_parsed tp;
2467 struct gsm_mncc dtmf;
2468
2469 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2470 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002471 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472 /* keypad facility */
2473 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2474 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002475 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2477 }
2478
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002479 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480}
2481
2482static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2483{
2484 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002485 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002486 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2487
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2489
2490 /* keypad */
2491 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002492 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002494 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495}
2496
2497static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2498{
2499 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002500 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2502
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2504
2505 /* cause */
2506 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002507 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 else
Harald Welte55c8f352010-03-07 23:40:35 +01002509 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002511 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512}
2513
2514static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2515{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002516 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
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_STOP_DTMF_ACK;
2520
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002521 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522}
2523
2524static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2525{
2526 struct gsm_mncc dtmf;
2527
2528 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2529 dtmf.callref = trans->callref;
2530
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002531 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532}
2533
2534static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2535{
2536 struct gsm48_hdr *gh = msgb_l3(msg);
2537 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2538 struct tlv_parsed tp;
2539 struct gsm_mncc modify;
2540
2541 memset(&modify, 0, sizeof(struct gsm_mncc));
2542 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002543 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002544 /* bearer capability */
2545 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2546 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002547 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002549
2550 /* Create a copy of the bearer capability
2551 * in the transaction struct, so we can use
2552 * this information later */
2553 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2554 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555 }
2556
2557 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2558
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002559 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560}
2561
2562static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2563{
2564 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002565 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2567
Harald Welte4bfdfe72009-06-10 23:11:52 +08002568 gh->msg_type = GSM48_MT_CC_MODIFY;
2569
2570 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2571
2572 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002573 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002574 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575
2576 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2577
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002578 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579}
2580
2581static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2582{
2583 struct gsm48_hdr *gh = msgb_l3(msg);
2584 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2585 struct tlv_parsed tp;
2586 struct gsm_mncc modify;
2587
2588 gsm48_stop_cc_timer(trans);
2589
2590 memset(&modify, 0, sizeof(struct gsm_mncc));
2591 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002592 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593 /* bearer capability */
2594 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2595 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002596 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002598
2599 /* Create a copy of the bearer capability
2600 * in the transaction struct, so we can use
2601 * this information later */
2602 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2603 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604 }
2605
2606 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2607
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002608 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609}
2610
2611static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2612{
2613 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002614 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2616
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2618
2619 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002620 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002621 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002622
2623 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2624
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002625 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626}
2627
2628static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2629{
2630 struct gsm48_hdr *gh = msgb_l3(msg);
2631 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2632 struct tlv_parsed tp;
2633 struct gsm_mncc modify;
2634
2635 gsm48_stop_cc_timer(trans);
2636
2637 memset(&modify, 0, sizeof(struct gsm_mncc));
2638 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002639 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002640 /* bearer capability */
2641 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2642 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002643 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002645
2646 /* Create a copy of the bearer capability
2647 * in the transaction struct, so we can use
2648 * this information later */
2649 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2650 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651 }
2652 /* cause */
2653 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2654 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002655 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2657 }
2658
2659 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2660
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002661 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662}
2663
2664static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2665{
2666 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002667 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002668 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2669
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2671
2672 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002673 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002674 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002676 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677
2678 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2679
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002680 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681}
2682
2683static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2684{
2685 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002686 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2688
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 gh->msg_type = GSM48_MT_CC_NOTIFY;
2690
2691 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002692 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002694 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002695}
2696
2697static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2698{
2699 struct gsm48_hdr *gh = msgb_l3(msg);
2700 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2701// struct tlv_parsed tp;
2702 struct gsm_mncc notify;
2703
2704 memset(&notify, 0, sizeof(struct gsm_mncc));
2705 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002706// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002708 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002710 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711}
2712
2713static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2714{
2715 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002716 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2718
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 gh->msg_type = GSM48_MT_CC_USER_INFO;
2720
2721 /* user-user */
2722 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002723 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724 /* more data */
2725 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002726 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002728 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729}
2730
2731static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2732{
2733 struct gsm48_hdr *gh = msgb_l3(msg);
2734 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2735 struct tlv_parsed tp;
2736 struct gsm_mncc user;
2737
2738 memset(&user, 0, sizeof(struct gsm_mncc));
2739 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002740 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741 /* user-user */
2742 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2743 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002744 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2746 }
2747 /* more data */
2748 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2749 user.more = 1;
2750
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002751 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002752}
2753
Philipp Maierfbf66102017-04-09 12:32:51 +02002754static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2755 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2756 uint32_t payload_msg_type)
2757{
2758 uint8_t data[sizeof(struct gsm_mncc)];
2759 struct gsm_mncc_rtp *rtp;
2760
2761 memset(&data, 0, sizeof(data));
2762 rtp = (struct gsm_mncc_rtp *) &data[0];
2763
2764 rtp->callref = callref;
2765 rtp->msg_type = cmd;
2766 rtp->ip = addr;
2767 rtp->port = port;
2768 rtp->payload_type = payload_type;
2769 rtp->payload_msg_type = payload_msg_type;
2770 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2771}
2772
2773static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2774{
2775 int msg_type;
2776
2777 /* FIXME This has to be set to some meaningful value.
2778 * Possible options are:
2779 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2780 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2781 * (0 if unknown) */
2782 msg_type = GSM_TCHF_FRAME;
2783
Philipp Maier621ba032017-11-07 17:19:25 +01002784 uint32_t addr = inet_addr(trans->conn->rtp.local_addr_cn);
2785 uint16_t port = trans->conn->rtp.local_port_cn;
2786
2787 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2788
2789 if (addr == INADDR_NONE) {
2790 LOGP(DMNCC, LOGL_ERROR,
2791 "(subscriber:%s) external MNCC is signalling invalid IP-Address\n",
2792 vlr_subscr_name(trans->vsub));
2793 return;
2794 }
2795 if (port == 0) {
2796 LOGP(DMNCC, LOGL_ERROR,
2797 "(subscriber:%s) external MNCC is signalling invalid Port\n",
2798 vlr_subscr_name(trans->vsub));
2799 return;
2800 }
Philipp Maierfbf66102017-04-09 12:32:51 +02002801
2802 /* FIXME: This has to be set to some meaningful value,
2803 * before the MSC-Split, this value was pulled from
2804 * lchan->abis_ip.rtp_payload */
2805 uint32_t payload_type = 0;
2806
2807 return mncc_recv_rtp(net, trans->callref, cmd,
2808 addr,
2809 port,
2810 payload_type,
2811 msg_type);
2812}
2813
2814static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2815{
2816 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2817}
2818
2819static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2820{
2821 struct gsm_trans *trans;
2822 int rc;
2823
2824 /* Find callref */
2825 trans = trans_find_by_callref(net, callref);
2826 if (!trans) {
2827 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2828 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2829 return -EIO;
2830 }
2831 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2832 if (!trans->conn) {
2833 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2834 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2835 return 0;
2836 }
2837
2838 trans->conn->mncc_rtp_bridge = 1;
2839
Philipp Maier621ba032017-11-07 17:19:25 +01002840 /* When we call msc_mgcp_call_assignment() we will trigger, depending
Philipp Maierfbf66102017-04-09 12:32:51 +02002841 * on the RAN type the call assignment on the A or Iu interface.
Philipp Maier621ba032017-11-07 17:19:25 +01002842 * msc_mgcp_call_assignment() also takes care about sending the CRCX
Philipp Maierfbf66102017-04-09 12:32:51 +02002843 * command to the MGCP-GW. The CRCX will return the port number,
2844 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2845 * have to return this port number back to the MNCC by sending
2846 * it back with the TCH_RTP_CREATE message. To make sure that
2847 * this message is sent AFTER the response to CRCX from the
Philipp Maier621ba032017-11-07 17:19:25 +01002848 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
Philipp Maierfbf66102017-04-09 12:32:51 +02002849 * to take care of this by setting trans->tch_rtp_create to true.
2850 * This will make sure that gsm48_tch_rtp_create() (below) is
2851 * called as soon as the local port number has become known. */
2852 trans->tch_rtp_create = true;
2853
2854 /* Assign call (if not done yet) */
2855 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01002856 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02002857 trans->assignment_done = true;
2858 }
2859 else
2860 rc = 0;
2861
2862 return rc;
2863}
2864
2865/* Trigger TCH_RTP_CREATE acknowledgement */
2866int gsm48_tch_rtp_create(struct gsm_trans *trans)
2867{
2868 /* This function is called as soon as the port, on which the
2869 * mgcp-gw expects the incoming RTP stream from the remote
2870 * end (e.g. Asterisk) is known. */
2871
2872 struct gsm_subscriber_connection *conn = trans->conn;
2873 struct gsm_network *network = conn->network;
2874
2875 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2876 return 0;
2877}
2878
2879static int tch_rtp_connect(struct gsm_network *net, void *arg)
2880{
2881 struct gsm_trans *trans;
2882 struct gsm_mncc_rtp *rtp = arg;
Philipp Maier621ba032017-11-07 17:19:25 +01002883 struct in_addr addr;
Philipp Maierfbf66102017-04-09 12:32:51 +02002884
2885 /* Find callref */
2886 trans = trans_find_by_callref(net, rtp->callref);
2887 if (!trans) {
2888 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2889 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2890 return -EIO;
2891 }
2892 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2893 if (!trans->conn) {
2894 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2895 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2896 return 0;
2897 }
2898
Philipp Maier621ba032017-11-07 17:19:25 +01002899 addr.s_addr = osmo_htonl(rtp->ip);
2900 return msc_mgcp_call_complete(trans, rtp->port, inet_ntoa(addr));
Philipp Maierfbf66102017-04-09 12:32:51 +02002901}
2902
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002904 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905 int type;
2906 int (*rout) (struct gsm_trans *trans, void *arg);
2907} downstatelist[] = {
2908 /* mobile originating call establishment */
2909 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002910 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2912 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2913 {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 */
2914 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2915 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2916 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2917 /* mobile terminating call establishment */
2918 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2919 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2920 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2921 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2922 /* signalling during call */
2923 {SBIT(GSM_CSTATE_ACTIVE),
2924 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2925 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2926 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2927 {ALL_STATES,
2928 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2929 {ALL_STATES,
2930 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2931 {ALL_STATES,
2932 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2933 {SBIT(GSM_CSTATE_ACTIVE),
2934 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2935 {SBIT(GSM_CSTATE_ACTIVE),
2936 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2937 {SBIT(GSM_CSTATE_ACTIVE),
2938 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2939 {SBIT(GSM_CSTATE_ACTIVE),
2940 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2941 {SBIT(GSM_CSTATE_ACTIVE),
2942 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2943 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2944 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2945 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2946 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2947 {SBIT(GSM_CSTATE_ACTIVE),
2948 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2949 /* clearing */
2950 {SBIT(GSM_CSTATE_INITIATED),
2951 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2952 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2953 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2954 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2955 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956};
2957
2958#define DOWNSLLEN \
2959 (sizeof(downstatelist) / sizeof(struct downstate))
2960
2961
Harald Welte76556372010-12-22 23:57:45 +01002962int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963{
Harald Welte1a6f7982009-08-09 18:52:33 +02002964 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002966 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002967 struct gsm_mncc *data = arg, rel;
2968
Harald Welte04dc88f2010-12-22 21:45:05 +01002969 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2970
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971 /* handle special messages */
2972 switch(msg_type) {
2973 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002974 rc = tch_bridge(net, arg);
2975 if (rc < 0)
2976 disconnect_bridge(net, arg, -rc);
2977 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002978 case MNCC_RTP_CREATE:
2979 return tch_rtp_create(net, data->callref);
2980 case MNCC_RTP_CONNECT:
2981 return tch_rtp_connect(net, arg);
2982 case MNCC_RTP_FREE:
2983 /* unused right now */
2984 return -EIO;
2985
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002988 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002989 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002990 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002991 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002992 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2993 get_mncc_name(msg_type));
2994 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002995 }
2996
2997 memset(&rel, 0, sizeof(struct gsm_mncc));
2998 rel.callref = data->callref;
2999
3000 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003001 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003002
3003 /* Callref unknown */
3004 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02003005 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003006
Harald Welte4a3464c2009-07-04 10:11:24 +02003007 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3009 "Received '%s' from MNCC with "
3010 "unknown callref %d\n", data->called.number,
3011 get_mncc_name(msg_type), data->callref);
3012 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003013 return mncc_release_ind(net, NULL, data->callref,
3014 GSM48_CAUSE_LOC_PRN_S_LU,
3015 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003017 if (!data->called.number[0] && !data->imsi[0]) {
3018 DEBUGP(DCC, "(bts - trx - ts - ti) "
3019 "Received '%s' from MNCC with "
3020 "no number or IMSI\n", get_mncc_name(msg_type));
3021 /* Invalid number */
3022 return mncc_release_ind(net, NULL, data->callref,
3023 GSM48_CAUSE_LOC_PRN_S_LU,
3024 GSM48_CC_CAUSE_INV_NR_FORMAT);
3025 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003026 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003027 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02003028 vsub = vlr_subscr_find_by_msisdn(net->vlr,
3029 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003030 else
Harald Welte2483f1b2016-06-19 18:06:02 +02003031 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003032
3033 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003034 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003035
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02003037 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3039 "Received '%s' from MNCC with "
3040 "unknown subscriber %s\n", data->called.number,
3041 get_mncc_name(msg_type), data->called.number);
3042 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003043 return mncc_release_ind(net, NULL, data->callref,
3044 GSM48_CAUSE_LOC_PRN_S_LU,
3045 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003046 }
3047 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02003048 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3050 "Received '%s' from MNCC with "
3051 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01003052 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02003053 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003054 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003055 return mncc_release_ind(net, NULL, data->callref,
3056 GSM48_CAUSE_LOC_PRN_S_LU,
3057 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058 }
3059 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003060 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003061 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003062 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003063 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003065 mncc_release_ind(net, NULL, data->callref,
3066 GSM48_CAUSE_LOC_PRN_S_LU,
3067 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 return -ENOMEM;
3069 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003070
3071 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02003072 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003073
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003074 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003075 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003076 /* find transaction with this subscriber already paging */
3077 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003078 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003079 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02003080 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003081 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003082 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 "Received '%s' from MNCC with "
3084 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003085 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003086 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003087 get_mncc_name(msg_type), vsub->lac);
3088 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003089 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003090 return 0;
3091 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003092 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02003093 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003094
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003095 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003096 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003097 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003098 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003099 trans,
3100 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003101 if (!trans->paging_request) {
3102 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003103 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003104 trans_free(trans);
3105 return 0;
3106 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003107 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003108 return 0;
3109 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003110
3111 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003112 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003113 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003114 } else {
3115 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003116 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003117 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003118
3119 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003120 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003121
3122 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003123 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003124 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003125 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003126 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003127 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003128 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3129 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 if (msg_type == MNCC_REL_REQ)
3131 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3132 else
3133 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3134 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003135 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003136 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003137 } else {
3138 DEBUGP(DCC, "(ti %02x sub %s) "
3139 "Received '%s' from MNCC in state %d (%s)\n",
3140 trans->transaction_id,
3141 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3142 get_mncc_name(msg_type), trans->cc.state,
3143 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 }
3145
Harald Welte4bfdfe72009-06-10 23:11:52 +08003146 /* Find function for current state and message */
3147 for (i = 0; i < DOWNSLLEN; i++)
3148 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003149 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003150 break;
3151 if (i == DOWNSLLEN) {
3152 DEBUGP(DCC, "Message unhandled at this state.\n");
3153 return 0;
3154 }
3155
3156 rc = downstatelist[i].rout(trans, arg);
3157
3158 return rc;
3159}
3160
3161
3162static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003163 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003164 int type;
3165 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3166} datastatelist[] = {
3167 /* mobile originating call establishment */
3168 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3169 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3170 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3171 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3172 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3173 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3174 /* mobile terminating call establishment */
3175 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3176 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3177 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3178 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003179 {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 +08003180 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3181 /* signalling during call */
3182 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3183 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3184 {SBIT(GSM_CSTATE_ACTIVE),
3185 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3186 {ALL_STATES,
3187 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3188 {ALL_STATES,
3189 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3190 {ALL_STATES,
3191 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3192 {SBIT(GSM_CSTATE_ACTIVE),
3193 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3194 {SBIT(GSM_CSTATE_ACTIVE),
3195 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3196 {SBIT(GSM_CSTATE_ACTIVE),
3197 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3198 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3199 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3200 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3201 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3202 {SBIT(GSM_CSTATE_ACTIVE),
3203 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3204 /* clearing */
3205 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3206 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3207 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3208 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3209 {ALL_STATES, /* 5.4.3.4 */
3210 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3211};
3212
3213#define DATASLLEN \
3214 (sizeof(datastatelist) / sizeof(struct datastate))
3215
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003216static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003217{
3218 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003219 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003220 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003221 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003222 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003223
Harald Welte4bfdfe72009-06-10 23:11:52 +08003224 if (msg_type & 0x80) {
3225 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3226 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003227 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003228
Harald Welte2483f1b2016-06-19 18:06:02 +02003229 if (!conn->vsub) {
3230 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003231 return -EINVAL;
3232 }
3233
Harald Welte4bfdfe72009-06-10 23:11:52 +08003234 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003235 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003236
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003237#if BEFORE_MSCSPLIT
3238 /* Re-enable this log output once we can obtain this information via
3239 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003240 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003241 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003242 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003243 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003244 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3245 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003246#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003247
3248 /* Create transaction */
3249 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003250 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003251 "creating new trans.\n", transaction_id);
3252 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003253 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003254 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003255 transaction_id, new_callref++);
3256 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003257 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003258 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003259 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003260 GSM48_MT_CC_RELEASE_COMPL);
3261 return -ENOMEM;
3262 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003263 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003264 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003265 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003266 }
3267
3268 /* find function for current state and message */
3269 for (i = 0; i < DATASLLEN; i++)
3270 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003271 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003272 break;
3273 if (i == DATASLLEN) {
3274 DEBUGP(DCC, "Message unhandled at this state.\n");
3275 return 0;
3276 }
3277
Harald Welte2483f1b2016-06-19 18:06:02 +02003278 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003279
Harald Welte4bfdfe72009-06-10 23:11:52 +08003280 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003281
Harald Welte2483f1b2016-06-19 18:06:02 +02003282 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003283 return rc;
3284}
3285
Harald Welte2483f1b2016-06-19 18:06:02 +02003286static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003287{
Harald Welte2483f1b2016-06-19 18:06:02 +02003288 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3289 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003290
Harald Welte2483f1b2016-06-19 18:06:02 +02003291 switch (pdisc) {
3292 case GSM48_PDISC_MM:
3293 switch (msg_type) {
3294 case GSM48_MT_MM_LOC_UPD_REQUEST:
3295 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003296 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003297 case GSM48_MT_MM_AUTH_RESP:
3298 case GSM48_MT_MM_AUTH_FAIL:
3299 case GSM48_MT_MM_ID_RESP:
3300 case GSM48_MT_MM_TMSI_REALL_COMPL:
3301 case GSM48_MT_MM_IMSI_DETACH_IND:
3302 return true;
3303 default:
3304 break;
3305 }
3306 break;
3307 case GSM48_PDISC_RR:
3308 switch (msg_type) {
3309 case GSM48_MT_RR_CIPH_M_COMPL:
3310 case GSM48_MT_RR_PAG_RESP:
3311 return true;
3312 default:
3313 break;
3314 }
3315 break;
3316 default:
3317 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003318 }
3319
Harald Welte2483f1b2016-06-19 18:06:02 +02003320 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003321}
3322
Harald Welte2483f1b2016-06-19 18:06:02 +02003323void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3324 struct msgb *msg)
3325{
3326
3327 /* If a CM Service Request was received before, this is the request the
3328 * conn was opened for. No need to wait for further messages. */
3329
3330 if (!conn->received_cm_service_request)
3331 return;
3332
3333 if (log_check_level(DMM, LOGL_DEBUG)) {
3334 struct gsm48_hdr *gh = msgb_l3(msg);
3335 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3336 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3337
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003338 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003339 " received_cm_service_request changes to false\n",
3340 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003341 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003342 }
3343 conn->received_cm_service_request = false;
3344}
3345
Harald Weltec2007852018-02-03 21:08:26 +01003346/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
3347int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
3348{
3349 switch (pdisc) {
3350 case GSM48_PDISC_MM:
3351 case GSM48_PDISC_CC:
3352 case GSM48_PDISC_NC_SS:
3353 return 0;
3354 case GSM48_PDISC_GROUP_CC:
3355 return 1;
3356 case GSM48_PDISC_BCAST_CC:
3357 return 2;
3358 case GSM48_PDISC_LOC:
3359 return 3;
3360 default:
3361 return -1;
3362 }
3363}
3364
3365/* extract the N(SD) and return the modulo value for a R98 message */
3366static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3367{
3368 switch (pdisc) {
3369 case GSM48_PDISC_MM:
3370 case GSM48_PDISC_CC:
3371 case GSM48_PDISC_NC_SS:
3372 *n_sd = (msg_type >> 6) & 0x3;
3373 return 4;
3374 case GSM48_PDISC_GROUP_CC:
3375 case GSM48_PDISC_BCAST_CC:
3376 case GSM48_PDISC_LOC:
3377 *n_sd = (msg_type >> 6) & 0x1;
3378 return 2;
3379 default:
3380 /* no sequence number, we cannot detect dups */
3381 return 0;
3382 }
3383}
3384
3385/* extract the N(SD) and return the modulo value for a R99 message */
3386static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3387{
3388 switch (pdisc) {
3389 case GSM48_PDISC_MM:
3390 case GSM48_PDISC_CC:
3391 case GSM48_PDISC_NC_SS:
3392 case GSM48_PDISC_GROUP_CC:
3393 case GSM48_PDISC_BCAST_CC:
3394 case GSM48_PDISC_LOC:
3395 *n_sd = (msg_type >> 6) & 0x1;
3396 return 2;
3397 default:
3398 /* no sequence number, we cannot detect dups */
3399 return 0;
3400 }
3401}
3402
3403/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
3404static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
3405{
3406 struct gsm48_hdr *gh;
3407 uint8_t pdisc;
3408 uint8_t n_sd, modulo, bin;
3409
3410 gh = msgb_l3(msg);
3411 pdisc = gsm48_hdr_pdisc(gh);
3412
3413 if (conn->classmark.classmark1_set && conn->classmark.classmark1.rev_lev < 2) {
3414 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
3415 } else { /* R99 */
3416 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
3417 }
3418 if (modulo == 0)
3419 return false;
3420 bin = gsm0407_pdisc_ctr_bin(pdisc);
3421 if (bin < 0)
3422 return false;
3423
3424 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
3425 if (n_sd != conn->n_sd_next[bin]) {
3426 /* not what we expected: duplicate */
3427 return true;
3428 } else {
3429 /* as expected: no dup; update expected counter for next message */
3430 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
3431 return false;
3432 }
3433}
Harald Welte2483f1b2016-06-19 18:06:02 +02003434
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003435/* 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 +08003436int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003437{
Philipp Maier91f10c72017-04-20 18:40:37 +02003438 struct gsm48_hdr *gh;
3439 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003440 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003441
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003442 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003443 OSMO_ASSERT(conn);
3444 OSMO_ASSERT(msg);
3445
Philipp Maier91f10c72017-04-20 18:40:37 +02003446 gh = msgb_l3(msg);
3447 pdisc = gsm48_hdr_pdisc(gh);
3448
Harald Weltec2007852018-02-03 21:08:26 +01003449 if (gsm0407_is_duplicate(conn, msg)) {
3450 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
3451 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
3452 return 0;
3453 }
3454
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003455 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3456 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3457 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003458
3459 if (!msc_subscr_conn_is_accepted(conn)
3460 && !msg_is_initially_permitted(gh)) {
3461 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003462 "subscr %s: Message not permitted for initial conn: %s\n",
3463 vlr_subscr_name(conn->vsub),
3464 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003465 return -EACCES;
3466 }
3467
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003468 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3469 LOGP(DMM, LOGL_ERROR,
3470 "%s: Illegal situation: RAN type mismatch:"
3471 " attached via %s, received message via %s\n",
3472 vlr_subscr_name(conn->vsub),
3473 ran_type_name(conn->vsub->cs.attached_via_ran),
3474 ran_type_name(conn->via_ran));
3475 return -EACCES;
3476 }
3477
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003478#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003479 if (silent_call_reroute(conn, msg))
3480 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003481#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003482
Harald Welte52b1f982008-12-23 20:25:15 +00003483 switch (pdisc) {
3484 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003485 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003486 break;
3487 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003488 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003489 break;
3490 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003491 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003492 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003493 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003494 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003495 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003496 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003497 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003498 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3499 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003500 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003501 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003502 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003503 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003504 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003505 case GSM48_PDISC_TEST:
3506 rc = gsm0414_rcv_test(conn, msg);
3507 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003508 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003509 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3510 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003511 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003512 break;
3513 }
3514
3515 return rc;
3516}
Harald Welte8470bf22008-12-25 23:28:35 +00003517
Harald Welte2483f1b2016-06-19 18:06:02 +02003518/***********************************************************************
3519 * VLR integration
3520 ***********************************************************************/
3521
3522/* VLR asks us to send an authentication request */
3523static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3524 bool send_autn)
3525{
3526 struct gsm_subscriber_connection *conn = msc_conn_ref;
3527 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3528 send_autn? at->vec.autn : NULL,
3529 at->key_seq);
3530}
3531
3532/* VLR asks us to send an authentication reject */
3533static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3534{
3535 struct gsm_subscriber_connection *conn = msc_conn_ref;
3536 return gsm48_tx_mm_auth_rej(conn);
3537}
3538
3539/* VLR asks us to transmit an Identity Request of given type */
3540static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3541{
3542 struct gsm_subscriber_connection *conn = msc_conn_ref;
3543 return mm_tx_identity_req(conn, mi_type);
3544}
3545
3546/* VLR asks us to transmit a Location Update Accept */
3547static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3548{
3549 struct gsm_subscriber_connection *conn = msc_conn_ref;
3550 return gsm0408_loc_upd_acc(conn, send_tmsi);
3551}
3552
3553/* VLR asks us to transmit a Location Update Reject */
3554static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3555{
3556 struct gsm_subscriber_connection *conn = msc_conn_ref;
3557 return gsm0408_loc_upd_rej(conn, cause);
3558}
3559
3560/* VLR asks us to transmit a CM Service Accept */
3561static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3562{
3563 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003564 return msc_gsm48_tx_mm_serv_ack(conn);
3565}
3566
3567static int msc_vlr_tx_common_id(void *msc_conn_ref)
3568{
3569 struct gsm_subscriber_connection *conn = msc_conn_ref;
3570 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003571}
3572
Stefan Sperling3a741282018-03-13 21:11:49 +01003573/* VLR asks us to transmit MM info. */
3574static int msc_vlr_tx_mm_info(void *msc_conn_ref)
3575{
3576 struct gsm_subscriber_connection *conn = msc_conn_ref;
3577 if (!conn->network->send_mm_info)
3578 return 0;
3579 return gsm48_tx_mm_info(conn);
3580}
3581
Harald Welte2483f1b2016-06-19 18:06:02 +02003582/* VLR asks us to transmit a CM Service Reject */
3583static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3584{
3585 uint8_t cause;
3586 struct gsm_subscriber_connection *conn = msc_conn_ref;
3587 conn->received_cm_service_request = false;
3588
3589 switch (result) {
3590 default:
3591 case VLR_PR_ARQ_RES_NONE:
3592 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3593 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3594 cause = GSM48_REJECT_NETWORK_FAILURE;
3595 break;
3596 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3597 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3598 break;
3599 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3600 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3601 break;
3602 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3603 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3604 break;
3605 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3606 cause = GSM48_REJECT_ILLEGAL_MS;
3607 break;
3608 case VLR_PR_ARQ_RES_TIMEOUT:
3609 cause = GSM48_REJECT_CONGESTION;
3610 break;
3611 };
3612
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003613 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003614}
3615
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003616/* For msc_vlr_set_ciph_mode() */
3617osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3618 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3619
Neels Hofmeyrda21a522018-03-02 01:50:09 +01003620/* VLR asks us to start using ciphering.
3621 * (Keep non-static to allow regression testing on this function.) */
3622int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3623 bool umts_aka,
3624 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02003625{
3626 struct gsm_subscriber_connection *conn = msc_conn_ref;
3627 struct vlr_subscr *vsub;
3628 struct gsm_auth_tuple *tuple;
3629
3630 if (!conn || !conn->vsub) {
3631 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3632 " NULL conn/subscriber");
3633 return -EINVAL;
3634 }
3635
3636 vsub = conn->vsub;
3637 tuple = vsub->last_tuple;
3638
3639 if (!tuple) {
3640 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3641 " Command: no auth tuple available\n",
3642 vlr_subscr_name(vsub));
3643 return -EINVAL;
3644 }
3645
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003646 switch (conn->via_ran) {
3647 case RAN_GERAN_A:
3648 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3649 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003650 {
Harald Welte7b222aa2017-12-23 19:30:32 +01003651 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003652 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01003653 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003654
Harald Welte7b222aa2017-12-23 19:30:32 +01003655 for (i = 0; i < 8; i++) {
Harald Welte71330722017-12-23 19:59:02 +01003656 if (net->a5_encryption_mask & (1 << i) &&
3657 classmark_supports_a5(&conn->classmark, i))
Harald Welte7b222aa2017-12-23 19:30:32 +01003658 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
3659 }
3660 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003661
Harald Welte71330722017-12-23 19:59:02 +01003662 if (ei.perm_algo_len == 0) {
3663 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
3664 "between MSC-configured and MS-supported A5 algorithms\n",
3665 vlr_subscr_name(conn->vsub));
3666 return -ENOTSUP;
3667 }
3668
Neels Hofmeyr52821712017-12-18 01:23:42 +01003669 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3670 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3671 * necessarily a match for the UMTS AKA tokens. */
3672 if (umts_aka)
3673 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3674 else
3675 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003676 ei.key_len = sizeof(tuple->vec.kc);
3677
3678 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3679 }
3680
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003681 case RAN_UTRAN_IU:
3682#ifdef BUILD_IU
3683 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3684 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003685 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003686#else
3687 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3688 " built without Iu support\n");
3689 return -ENOTSUP;
3690#endif
3691
3692 default:
3693 break;
3694 }
3695 LOGP(DMM, LOGL_ERROR,
3696 "%s: cannot start ciphering, unknown RAN type %d\n",
3697 vlr_subscr_name(conn->vsub), conn->via_ran);
3698 return -ENOTSUP;
3699}
3700
3701void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3702{
3703 struct vlr_ciph_result vlr_res = {};
3704
3705 if (!conn || !conn->vsub) {
3706 LOGP(DMM, LOGL_ERROR,
3707 "Rx Security Mode Complete for invalid conn\n");
3708 return;
3709 }
3710
3711 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3712 vlr_subscr_name(conn->vsub));
3713
3714 vlr_res.cause = VLR_CIPH_COMPL;
3715 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003716}
3717
3718/* VLR informs us that the subscriber data has somehow been modified */
3719static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3720{
Maxa263bb22017-12-27 13:23:44 +01003721 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3722 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003723}
3724
3725/* VLR informs us that the subscriber has been associated with a conn */
3726static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3727 struct vlr_subscr *vsub)
3728{
3729 struct gsm_subscriber_connection *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01003730 OSMO_ASSERT(vsub);
Harald Welte2483f1b2016-06-19 18:06:02 +02003731 OSMO_ASSERT(!conn->vsub);
3732 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01003733 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003734 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003735}
3736
3737/* operations that we need to implement for libvlr */
3738static const struct vlr_ops msc_vlr_ops = {
3739 .tx_auth_req = msc_vlr_tx_auth_req,
3740 .tx_auth_rej = msc_vlr_tx_auth_rej,
3741 .tx_id_req = msc_vlr_tx_id_req,
3742 .tx_lu_acc = msc_vlr_tx_lu_acc,
3743 .tx_lu_rej = msc_vlr_tx_lu_rej,
3744 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3745 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3746 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003747 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01003748 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02003749 .subscr_update = msc_vlr_subscr_update,
3750 .subscr_assoc = msc_vlr_subscr_assoc,
3751};
3752
3753/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3754int msc_vlr_alloc(struct gsm_network *net)
3755{
3756 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3757 if (!net->vlr)
3758 return -ENOMEM;
3759 net->vlr->user_ctx = net;
3760 return 0;
3761}
3762
3763/* Launch the VLR, i.e. its GSUP connection */
3764int msc_vlr_start(struct gsm_network *net)
3765{
3766 OSMO_ASSERT(net->vlr);
3767 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3768 net->gsup_server_port);
3769}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01003770
3771struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
3772{
3773 struct msgb *msg;
3774 struct gsm48_hdr *gh;
3775
3776 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
3777 if (!msg)
3778 return NULL;
3779
3780 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3781 gh->proto_discr = GSM48_PDISC_MM;
3782 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
3783 gh->data[0] = value;
3784
3785 return msg;
3786}
3787
3788struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
3789{
3790 struct gsm48_hdr *gh;
3791 struct msgb *msg;
3792
3793 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
3794 if (!msg)
3795 return NULL;
3796
3797 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3798 gh->proto_discr = GSM48_PDISC_MM;
3799 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
3800 gh->data[0] = cause;
3801 return msg;
3802}
3803
3804int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
3805{
3806 /* Check the size for the classmark */
3807 if (length < 1 + *classmark2_lv)
3808 return -1;
3809
3810 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
3811 if (length < 2 + *classmark2_lv + mi_lv[0])
3812 return -2;
3813
3814 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
3815 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
3816}
3817
3818int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
3819 char *mi_string, uint8_t *mi_type)
3820{
3821 static const uint32_t classmark_offset =
3822 offsetof(struct gsm48_pag_resp, classmark2);
3823 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
3824 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
3825 mi_string, mi_type);
3826}