blob: beec6fa0561608994f16a0cac5b7d881ede2168d [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>
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070045#include <osmocom/msc/gsm_09_11.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020047#include <osmocom/msc/transaction.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020048#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);
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200167 OMSC_LINKID_CB(msg) = trans->dlci;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800168 }
169
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200170 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800171}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100172
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800173int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
174{
175 struct gsm48_hdr *gh;
176 struct msgb *ss_notify;
177
178 ss_notify = gsm0480_create_notifySS(message);
179 if (!ss_notify)
180 return -1;
181
182 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
183 uint8_t *data = msgb_push(ss_notify, 1);
184 data[0] = ss_notify->len - 1;
185 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
186 gh->msg_type = GSM48_MT_CC_FACILITY;
187 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
188}
189
Harald Welte2483f1b2016-06-19 18:06:02 +0200190/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100191void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
192{
193 struct gsm_trans *trans, *temp;
194
195 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
196
197 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100198 if (trans->protocol == protocol) {
199 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100200 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100201 }
Harald Welte371efe52010-12-22 23:17:50 +0100202 }
203}
204
Holger Freyther429e7762008-12-30 13:28:30 +0000205/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200206static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000207{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800208 struct msgb *msg;
209
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800210 msg = gsm48_create_loc_upd_rej(cause);
211 if (!msg) {
212 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
213 return -1;
214 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200215
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200216 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
217 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100218
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800219 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000220}
221
222/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200223static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
224 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000225{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100226 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000227 struct gsm48_hdr *gh;
228 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200229 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100230 struct osmo_location_area_id laid = {
231 .plmn = conn->network->plmn,
232 .lac = conn->lac,
233 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200234
Harald Welte52b1f982008-12-23 20:25:15 +0000235 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
236 gh->proto_discr = GSM48_PDISC_MM;
237 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
238
239 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100240 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000241
Harald Welte2483f1b2016-06-19 18:06:02 +0200242 if (send_tmsi == GSM_RESERVED_TMSI) {
243 /* we did not allocate a TMSI to the MS, so we need to
244 * include the IMSI in order for the MS to delete any
245 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200246 uint8_t mi[10];
247 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200248 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200249 mid = msgb_put(msg, len);
250 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200251 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
252 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200253 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200254 /* Include the TMSI, which means that the MS will send a
255 * TMSI REALLOCATION COMPLETE, and we should wait for
256 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200257 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200258 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200259 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
260 vlr_subscr_name(conn->vsub),
261 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200262 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200263 /* TODO: Follow-on proceed */
264 /* TODO: CTS permission */
265 /* TODO: Equivalent PLMNs */
266 /* TODO: Emergency Number List */
267 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000268
Harald Welte52b1f982008-12-23 20:25:15 +0000269
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800270 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000271}
272
Harald Weltebf5e8df2009-02-03 12:59:45 +0000273/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200274static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000275{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100276 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000277 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000278
Harald Welte231ad4f2008-12-27 11:15:38 +0000279 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
280 gh->proto_discr = GSM48_PDISC_MM;
281 gh->msg_type = GSM48_MT_MM_ID_REQ;
282 gh->data[0] = id_type;
283
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800284 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000285}
286
Harald Weltebf5e8df2009-02-03 12:59:45 +0000287/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800288static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000289{
290 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200291 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200292 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000293
Harald Welte2483f1b2016-06-19 18:06:02 +0200294 if (!conn->vsub) {
295 LOGP(DMM, LOGL_ERROR,
296 "Rx MM Identity Response: invalid: no subscriber\n");
297 return -EINVAL;
298 }
299
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200300 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200301 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
302 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000303
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200304 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100305
Harald Welte2483f1b2016-06-19 18:06:02 +0200306 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000307}
308
Harald Welte2483f1b2016-06-19 18:06:02 +0200309/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200310static const struct value_string lupd_names[] = {
311 { GSM48_LUPD_NORMAL, "NORMAL" },
312 { GSM48_LUPD_PERIODIC, "PERIODIC" },
313 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
314 { 0, NULL }
315};
Harald Welte2a139372009-02-22 21:14:55 +0000316
Harald Welte2483f1b2016-06-19 18:06:02 +0200317/* Chapter 9.2.15: Receive Location Updating Request.
318 * Keep this function non-static for direct invocation by unit tests. */
319int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000320{
Harald Welte2483f1b2016-06-19 18:06:02 +0200321 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000322 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000323 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200324 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200325 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200326 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200327 uint32_t tmsi;
328 char *imsi;
329 struct osmo_location_area_id old_lai, new_lai;
330 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200331 bool is_utran;
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
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200339 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
340 LOGP(DMM, LOGL_ERROR,
341 "Cannot accept another LU, conn already busy establishing authenticity;"
342 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
343 gsm48_mi_type_name(mi_type), mi_string, get_value_string(lupd_names, lu->type));
344 return -EINVAL;
345 }
346
347 if (msc_subscr_conn_is_accepted(conn)) {
348 LOGP(DMM, LOGL_ERROR,
349 "Cannot accept another LU, conn already established;"
350 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
351 gsm48_mi_type_name(mi_type), mi_string, get_value_string(lupd_names, lu->type));
352 return -EINVAL;
353 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200354
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200355 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_LU, mi_string);
356
Harald Welte2483f1b2016-06-19 18:06:02 +0200357 conn->classmark.classmark1 = lu->classmark1;
358 conn->classmark.classmark1_set = true;
359
360 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
361 gsm48_mi_type_name(mi_type), mi_string,
362 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000363
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200364 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100365
Harald Welte24ff6ee2009-12-22 00:41:05 +0100366 switch (lu->type) {
367 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200368 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200369 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100370 break;
371 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200372 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200373 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100374 break;
375 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200376 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200377 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100378 break;
379 }
380
Harald Welte2483f1b2016-06-19 18:06:02 +0200381 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
382 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
383 /* TODO: 10.5.7.8 Device properties */
384 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200385
Harald Welte52b1f982008-12-23 20:25:15 +0000386 switch (mi_type) {
387 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200388 tmsi = GSM_RESERVED_TMSI;
389 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000390 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000391 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200392 tmsi = tmsi_from_string(mi_string);
393 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000394 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200395 default:
Harald Weltea0368542009-06-27 02:58:43 +0200396 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200397 tmsi = GSM_RESERVED_TMSI;
398 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000399 break;
400 }
401
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100402 gsm48_decode_lai2(&lu->lai, &old_lai);
403 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200404 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200405 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200406
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200407 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200408 lu_fsm = vlr_loc_update(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200409 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200410 net->vlr, conn, vlr_lu_type, tmsi, imsi,
411 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200412 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100413 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200414 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200415 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200416 net->vlr->cfg.assign_tmsi);
417 if (!lu_fsm) {
418 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
419 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800420 }
421
Harald Welte2483f1b2016-06-19 18:06:02 +0200422 /* From vlr_loc_update() we expect an implicit dispatch of
423 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
424 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000425
Harald Welte2483f1b2016-06-19 18:06:02 +0200426 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
427 LOGP(DRR, LOGL_ERROR,
428 "%s: internal error during Location Updating attempt\n",
429 mi_string);
430 return -EIO;
431 }
432
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200433 msc_subscr_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200434 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000435}
436
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100437/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200438/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100439static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800440{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100441 uint8_t ret;
442
443 ret = value / 10;
444 ret |= (value % 10) << 4;
445
446 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800447}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100448
Harald Welte4bfdfe72009-06-10 23:11:52 +0800449
Harald Weltedb253af2008-12-30 17:56:55 +0000450/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800451int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000452{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100453 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000454 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200455 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200456 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200457 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100458
Harald Welte4bfdfe72009-06-10 23:11:52 +0800459 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100460 struct tm* gmt_time;
461 struct tm* local_time;
462 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100463 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000464
Harald Weltedb253af2008-12-30 17:56:55 +0000465 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
466 gh->proto_discr = GSM48_PDISC_MM;
467 gh->msg_type = GSM48_MT_MM_INFO;
468
469 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200470#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000471 name_len = strlen(net->name_long);
472 /* 10.5.3.5a */
473 ptr8 = msgb_put(msg, 3);
474 ptr8[0] = GSM48_IE_NAME_LONG;
475 ptr8[1] = name_len*2 +1;
476 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
477
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200478 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000479 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000480 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000481
482 /* FIXME: Use Cell Broadcast, not UCS-2, since
483 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200484#endif
485 name_len = (strlen(net->name_long)*7)/8;
486 name_pad = (8 - strlen(net->name_long)*7)%8;
487 if (name_pad > 0)
488 name_len++;
489 /* 10.5.3.5a */
490 ptr8 = msgb_put(msg, 3);
491 ptr8[0] = GSM48_IE_NAME_LONG;
492 ptr8[1] = name_len +1;
493 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
494
495 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100496 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200497
Harald Weltedb253af2008-12-30 17:56:55 +0000498 }
499
500 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200501#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000502 name_len = strlen(net->name_short);
503 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200504 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200505 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000506 ptr8[1] = name_len*2 + 1;
507 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
508
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200509 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000510 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000511 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200512#endif
513 name_len = (strlen(net->name_short)*7)/8;
514 name_pad = (8 - strlen(net->name_short)*7)%8;
515 if (name_pad > 0)
516 name_len++;
517 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200518 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200519 ptr8[0] = GSM48_IE_NAME_SHORT;
520 ptr8[1] = name_len +1;
521 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
522
523 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100524 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200525
Harald Weltedb253af2008-12-30 17:56:55 +0000526 }
527
Harald Weltedb253af2008-12-30 17:56:55 +0000528 /* Section 10.5.3.9 */
529 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100530 gmt_time = gmtime(&cur_t);
531
Harald Weltedb253af2008-12-30 17:56:55 +0000532 ptr8 = msgb_put(msg, 8);
533 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100534 ptr8[1] = bcdify(gmt_time->tm_year % 100);
535 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
536 ptr8[3] = bcdify(gmt_time->tm_mday);
537 ptr8[4] = bcdify(gmt_time->tm_hour);
538 ptr8[5] = bcdify(gmt_time->tm_min);
539 ptr8[6] = bcdify(gmt_time->tm_sec);
540
Neels Hofmeyr73983952016-05-10 13:29:33 +0200541 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200542 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200543 if (net->tz.hr < 0) {
544 tzunits = ((net->tz.hr/-1)*4);
545 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100546 ptr8[7] = bcdify(tzunits);
547 /* Set negative time */
548 ptr8[7] |= 0x08;
549 }
550 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200551 tzunits = net->tz.hr*4;
552 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100553 ptr8[7] = bcdify(tzunits);
554 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100555 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200556 if (net->tz.dst >= 0 && net->tz.dst <= 2)
557 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100558 }
559 else {
560 /* Need to get GSM offset and convert into 15 min units */
561 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200562#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200563 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100564 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200565#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200566 /* find timezone offset */
567 time_t utc;
568 double offsetFromUTC;
569 utc = mktime(gmt_time);
570 local_time = localtime(&cur_t);
571 offsetFromUTC = difftime(cur_t, utc);
572 if (local_time->tm_isdst)
573 offsetFromUTC += 3600.0;
574 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200575#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100576 if (tzunits < 0) {
577 tzunits = tzunits/-1;
578 ptr8[7] = bcdify(tzunits);
579 /* Flip it to negative */
580 ptr8[7] |= 0x08;
581 }
582 else
583 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100584
585 /* Does not support DST +2 */
586 if (local_time->tm_isdst)
587 dst = 1;
588 }
589
590 if (dst) {
591 ptr8 = msgb_put(msg, 3);
592 ptr8[0] = GSM48_IE_NET_DST;
593 ptr8[1] = 1;
594 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100595 }
Harald Weltedb253af2008-12-30 17:56:55 +0000596
Daniel Willmanneea93372009-08-13 03:42:07 +0200597 DEBUGP(DMM, "-> MM INFO\n");
598
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800599 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000600}
601
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100602/*! Send an Authentication Request to MS on the given subscriber connection
603 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
604 * \param[in] conn Subscriber connection to send on.
605 * \param[in] rand Random challenge token to send, must be 16 bytes long.
606 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
607 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
608 * \param[in] key_seq auth tuple's sequence number.
609 */
610int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
611 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200612{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100613 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200614 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200615 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200616
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100617 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100618 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100619 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200620
Harald Welte7984d5c2009-08-12 22:56:50 +0200621 gh->proto_discr = GSM48_PDISC_MM;
622 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
623
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100624 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200625
Harald Welte7984d5c2009-08-12 22:56:50 +0200626 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100627 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200628 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200629 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200630
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100631
632 /* 16 bytes AUTN */
633 if (autn)
634 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
635
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800636 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200637}
638
639/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800640int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200641{
642 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800643 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200644}
645
Harald Welte2483f1b2016-06-19 18:06:02 +0200646static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200647static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value result);
Harald Welte2483f1b2016-06-19 18:06:02 +0200648
649static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100650{
Harald Welte2483f1b2016-06-19 18:06:02 +0200651 uint8_t mi_type;
652 char mi_string[GSM48_MI_SIZE];
653 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100654
Harald Welte2483f1b2016-06-19 18:06:02 +0200655 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
656 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100657
Harald Welte2483f1b2016-06-19 18:06:02 +0200658 switch (mi_type) {
659 case GSM_MI_TYPE_IMSI:
660 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
661 goto accept_reuse;
662 break;
663 case GSM_MI_TYPE_TMSI:
664 tmsi = osmo_load32be(mi_lv+2);
665 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
666 goto accept_reuse;
667 break;
668 case GSM_MI_TYPE_IMEI:
669 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
670 goto accept_reuse;
671 break;
672 default:
673 break;
674 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100675
Harald Welte2483f1b2016-06-19 18:06:02 +0200676 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
677 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200678 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_ILLEGAL_MS);
Harald Welte2483f1b2016-06-19 18:06:02 +0200679 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100680
Harald Welte2483f1b2016-06-19 18:06:02 +0200681accept_reuse:
682 DEBUGP(DMM, "%s: re-using already accepted connection\n",
683 vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200684
685 if (!conn->received_cm_service_request) {
686 conn->received_cm_service_request = true;
687 msc_subscr_conn_get(conn, MSC_CONN_USE_CM_SERVICE);
688 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200689 msc_subscr_conn_update_id(conn, conn->complete_layer3_type, mi_string);
Harald Welte2483f1b2016-06-19 18:06:02 +0200690 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100691}
692
Harald Welte4ed0e922009-01-10 03:17:30 +0000693/*
694 * Handle CM Service Requests
695 * a) Verify that the packet is long enough to contain the information
696 * we require otherwsie reject with INCORRECT_MESSAGE
697 * b) Try to parse the TMSI. If we do not have one reject
698 * c) Check that we know the subscriber with the TMSI otherwise reject
699 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200700 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200701 *
702 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000703 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200704int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000705{
Harald Welte2483f1b2016-06-19 18:06:02 +0200706 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200707 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200708 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000709
Harald Welteba4cf162009-01-10 01:49:35 +0000710 struct gsm48_hdr *gh = msgb_l3(msg);
711 struct gsm48_service_request *req =
712 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800713 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200714 uint8_t classmark2_len = gh->data[1];
715 uint8_t *classmark2 = gh->data+2;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200716 uint8_t *mi_p = classmark2 + classmark2_len;
717 uint8_t mi_len = *mi_p;
718 uint8_t *mi = mi_p + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +0200719 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200720 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200721
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100722 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200723 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000724
725 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200726 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200727 return msc_gsm48_tx_mm_serv_rej(conn,
728 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000729 }
730
731 if (msg->data_len < req->mi_len + 6) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200732 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200733 return msc_gsm48_tx_mm_serv_rej(conn,
734 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000735 }
736
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200737 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000738 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200739 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x MI(%s)=%s\n",
740 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200741
Harald Welte3995d2e2018-01-24 11:23:54 +0100742 switch (mi_type) {
743 case GSM_MI_TYPE_IMSI:
744 case GSM_MI_TYPE_TMSI:
745 /* continue below */
746 break;
747 case GSM_MI_TYPE_IMEI:
748 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
749 /* We don't do emergency calls by IMEI */
750 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
751 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
752 }
753 /* fall-through for non-emergency setup */
754 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200755 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200756 return msc_gsm48_tx_mm_serv_rej(conn,
757 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000758 }
759
Harald Welte37382ec2018-01-24 12:49:34 +0100760 switch (req->cm_service_type) {
761 case GSM48_CMSERV_MO_CALL_PACKET:
762 case GSM48_CMSERV_EMERGENCY:
763 case GSM48_CMSERV_SMS:
764 case GSM48_CMSERV_SUP_SERV:
765 /* continue below */
766 break;
767 default:
768 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
769 }
770
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200771 if (msc_subscr_conn_is_accepted(conn))
772 return cm_serv_reuse_conn(conn, mi_p);
Harald Welte2483f1b2016-06-19 18:06:02 +0200773
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200774 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
775 LOGP(DMM, LOGL_ERROR,
776 "Cannot accept CM Service Request, conn already busy establishing authenticity\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200777 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_CONGESTION);
Harald Welte2483f1b2016-06-19 18:06:02 +0200778 return -EINVAL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200779 /* or should we accept and note down the service request anyway? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200780 }
781
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200782 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_CM_SERVICE_REQ, mi_string);
Harald Welte7659de12009-12-13 12:39:18 +0100783
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200784 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, mi_p);
785 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
786 conn->classmark.classmark2_len = classmark2_len;
787
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200788 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200789 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200790 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200791 net->vlr, conn,
792 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200793 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100794 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200795 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200796 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000797
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200798 msc_subscr_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200799 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000800}
801
Harald Welte49f8fcb2018-01-24 21:40:19 +0100802/* Receive a CM Re-establish Request */
803static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
804{
805 uint8_t mi_type;
806 char mi_string[GSM48_MI_SIZE];
807 struct gsm48_hdr *gh = msgb_l3(msg);
808
809 uint8_t classmark2_len = gh->data[1];
810 uint8_t *classmark2 = gh->data+2;
811 uint8_t mi_len = *(classmark2 + classmark2_len);
812 uint8_t *mi = (classmark2 + classmark2_len + 1);
813
814 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
815 mi_type = mi[0] & GSM_MI_TYPE_MASK;
816 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
817
818 /* we don't support CM call re-establishment */
819 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
820}
821
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200822static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000823{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200824 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000825 struct gsm48_hdr *gh = msgb_l3(msg);
826 struct gsm48_imsi_detach_ind *idi =
827 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200828 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200829 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200830 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000831
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200832 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200833 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
834 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000835
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200836 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100837
Harald Welte2a139372009-02-22 21:14:55 +0000838 switch (mi_type) {
839 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200840 vsub = vlr_subscr_find_by_tmsi(network->vlr,
841 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000842 break;
843 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200844 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000845 break;
846 case GSM_MI_TYPE_IMEI:
847 case GSM_MI_TYPE_IMEISV:
848 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200849 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
850 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000851 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200852 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200853 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
854 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000855 break;
856 }
857
Harald Welte2483f1b2016-06-19 18:06:02 +0200858 /* TODO? We used to remember the subscriber's classmark1 here and
859 * stored it in the old sqlite db, but now we store it in a conn that
860 * will be discarded anyway: */
861 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200862
Harald Welte2483f1b2016-06-19 18:06:02 +0200863 if (!vsub) {
864 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
865 gsm48_mi_type_name(mi_type), mi_string);
866 } else {
867 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100868
869 if (vsub->cs.is_paging)
870 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
871
Harald Welte2483f1b2016-06-19 18:06:02 +0200872 vlr_subscr_rx_imsi_detach(vsub);
873 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
874 vlr_subscr_put(vsub);
875 }
Harald Welte2a139372009-02-22 21:14:55 +0000876
Harald Welte2483f1b2016-06-19 18:06:02 +0200877 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000878 return 0;
879}
880
Harald Welted2a7f5a2009-06-05 20:08:20 +0000881static int gsm48_rx_mm_status(struct msgb *msg)
882{
883 struct gsm48_hdr *gh = msgb_l3(msg);
884
885 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
886
887 return 0;
888}
889
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100890static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
891 struct gsm_subscriber_connection *conn,
892 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100893{
894 struct gsm48_hdr *gh = msgb_l3(msg);
895 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100896
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100897 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
898 LOGP(DMM, LOGL_ERROR,
899 "%s: MM AUTHENTICATION RESPONSE:"
900 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200901 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100902 return -EINVAL;
903 }
904
905 *res_len = sizeof(ar->sres);
906 memcpy(res, ar->sres, sizeof(ar->sres));
907 return 0;
908}
909
910static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
911 struct gsm_subscriber_connection *conn,
912 struct msgb *msg)
913{
914 struct gsm48_hdr *gh;
915 uint8_t *data;
916 uint8_t iei;
917 uint8_t ie_len;
918 unsigned int data_len;
919
920 /* First parse the GSM part */
921 if (parse_gsm_auth_resp(res, res_len, conn, msg))
922 return -EINVAL;
923 OSMO_ASSERT(*res_len == 4);
924
925 /* Then add the extended res part */
926 gh = msgb_l3(msg);
927 data = gh->data + sizeof(struct gsm48_auth_resp);
928 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
929
930 if (data_len < 3) {
931 LOGP(DMM, LOGL_ERROR,
932 "%s: MM AUTHENTICATION RESPONSE:"
933 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200934 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100935 return -EINVAL;
936 }
937
938 iei = data[0];
939 ie_len = data[1];
940 if (iei != GSM48_IE_AUTH_RES_EXT) {
941 LOGP(DMM, LOGL_ERROR,
942 "%s: MM R99 AUTHENTICATION RESPONSE:"
943 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200944 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100945 GSM48_IE_AUTH_RES_EXT, iei);
946 return -EINVAL;
947 }
948
949 if (ie_len > 12) {
950 LOGP(DMM, LOGL_ERROR,
951 "%s: MM R99 AUTHENTICATION RESPONSE:"
952 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200953 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100954 return -EINVAL;
955 }
956
957 *res_len += ie_len;
958 memcpy(res + 4, &data[2], ie_len);
959 return 0;
960}
961
962/* Chapter 9.2.3: Authentication Response */
963static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
964{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100965 uint8_t res[16];
966 uint8_t res_len;
967 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100968 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100969
Harald Welte2483f1b2016-06-19 18:06:02 +0200970 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100971 LOGP(DMM, LOGL_ERROR,
972 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200973 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100974 return -EINVAL;
975 }
976
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100977 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
978
979 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100980 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100981 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100982 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100983
984 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100985 LOGP(DMM, LOGL_ERROR,
986 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
987 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100988 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100989 memset(res, 0, sizeof(res));
990 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100991 }
992
993 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200994 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +0100995 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100996 osmo_hexdump_nospc(res, res_len));
997
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100998 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->classmark),
Harald Welte2483f1b2016-06-19 18:06:02 +0200999 conn->via_ran == RAN_UTRAN_IU,
1000 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001001}
1002
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001003static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
1004{
1005 struct gsm48_hdr *gh = msgb_l3(msg);
1006 uint8_t cause;
1007 uint8_t auts_tag;
1008 uint8_t auts_len;
1009 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001010
Harald Welte2483f1b2016-06-19 18:06:02 +02001011 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001012 LOGP(DMM, LOGL_ERROR,
1013 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001014 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001015 return -EINVAL;
1016 }
1017
1018 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1019 LOGP(DMM, LOGL_ERROR,
1020 "%s: MM R99 AUTHENTICATION FAILURE:"
1021 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001022 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1023 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001024 return -EINVAL;
1025 }
1026
1027 cause = gh->data[0];
1028
1029 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1030 LOGP(DMM, LOGL_INFO,
1031 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001032 vlr_subscr_name(conn->vsub), cause);
1033 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1034 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001035 }
1036
1037 /* This is a Synch Failure procedure, which should pass an AUTS to
1038 * resynchronize the sequence nr with the HLR. Expecting exactly one
1039 * TLV with 14 bytes of AUTS. */
1040
1041 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1042 LOGP(DMM, LOGL_INFO,
1043 "%s: MM R99 AUTHENTICATION FAILURE:"
1044 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001045 vlr_subscr_name(conn->vsub));
1046 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001047 return -EINVAL;
1048 }
1049
1050 auts_tag = gh->data[1];
1051 auts_len = gh->data[2];
1052 auts = &gh->data[3];
1053
1054 if (auts_tag != GSM48_IE_AUTS
1055 || auts_len != 14) {
1056 LOGP(DMM, LOGL_INFO,
1057 "%s: MM R99 AUTHENTICATION FAILURE:"
1058 " invalid Synch Failure:"
1059 " expected AUTS IE 0x%02x of 14 bytes,"
1060 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001061 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001062 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +02001063 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001064 return -EINVAL;
1065 }
1066
1067 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1068 LOGP(DMM, LOGL_INFO,
1069 "%s: MM R99 AUTHENTICATION FAILURE:"
1070 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001071 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1072 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001073 return -EINVAL;
1074 }
1075
1076 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1077 * large enough. */
1078
1079 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001080 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001081
Harald Welte2483f1b2016-06-19 18:06:02 +02001082 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1083}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001084
Harald Welte2483f1b2016-06-19 18:06:02 +02001085static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1086{
1087 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1088 vlr_subscr_name(conn->vsub));
1089 if (!conn->vsub) {
1090 LOGP(DMM, LOGL_ERROR,
1091 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1092 return -EINVAL;
1093 }
1094 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001095}
1096
Harald Weltebf5e8df2009-02-03 12:59:45 +00001097/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001098static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001099{
1100 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001101 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001102
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001103 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001104 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001105 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001106 break;
1107 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001108 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001109 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001110 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001111 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001112 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001113 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001114 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001115 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001116 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001117 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001118 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001119 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001120 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001121 break;
1122 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001123 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001124 break;
1125 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001126 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001127 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001128 case GSM48_MT_MM_AUTH_FAIL:
1129 rc = gsm48_rx_mm_auth_fail(conn, msg);
1130 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001131 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001132 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001133 gh->msg_type);
1134 break;
1135 }
1136
1137 return rc;
1138}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001139
Harald Welte2483f1b2016-06-19 18:06:02 +02001140static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1141{
1142 /* Check the size for the classmark */
1143 if (tot_len < 1 + *classmark2_lv)
1144 return NULL;
1145
1146 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1147 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1148 return NULL;
1149
1150 return mi_lv;
1151}
1152
Harald Welte2d35ae62009-02-06 12:02:13 +00001153/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001154static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001155{
Harald Welte2483f1b2016-06-19 18:06:02 +02001156 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001157 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001158 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001159 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001160 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001161 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001162 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +02001163 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001164 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001165
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001166 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001167 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001168
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001169 resp = (struct gsm48_pag_resp *) &gh->data[0];
1170 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1171 mi_string, &mi_type);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001172
1173 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
1174 LOGP(DMM, LOGL_ERROR,
1175 "Ignoring Paging Response, conn already busy establishing authenticity\n");
1176 return 0;
1177 }
1178
1179 if (msc_subscr_conn_is_accepted(conn)) {
1180 LOGP(DMM, LOGL_ERROR, "Ignoring Paging Response, conn already established\n");
1181 return 0;
1182 }
1183
1184 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001185
Harald Welte2483f1b2016-06-19 18:06:02 +02001186 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1187 if (!mi_lv) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001188 LOGP(DRR, LOGL_ERROR, "PAGING RESPONSE: invalid Mobile Identity\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001189 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001190 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001191
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001192 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_PAGING_RESP, mi_string);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001193
Harald Welte2483f1b2016-06-19 18:06:02 +02001194 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1195 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001196
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001197 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +02001198 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001199 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +02001200 net->vlr, conn,
1201 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001202 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001203 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +02001204 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001205 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001206
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001207 msc_subscr_conn_complete_layer_3(conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001208 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001209}
1210
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001211static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001212{
1213 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001214 uint8_t apdu_id_flags;
1215 uint8_t apdu_len;
1216 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001217
1218 apdu_id_flags = gh->data[0];
1219 apdu_len = gh->data[1];
1220 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001221
Max8dc8f232017-02-09 19:13:02 +01001222 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001223 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001224
Harald Welte2483f1b2016-06-19 18:06:02 +02001225 /* we're not using the app info blob anywhere, so ignore. */
1226#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001227 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001228#else
1229 return 0;
1230#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001231}
1232
Harald Weltebf5e8df2009-02-03 12:59:45 +00001233/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001234static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001235{
1236 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001237 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001238
1239 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001240 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001241 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001242 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001243 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001244 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001245 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001246 default:
Philippd37981e2016-10-10 15:11:41 +02001247 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1248 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001249 break;
1250 }
1251
Harald Welte2d35ae62009-02-06 12:02:13 +00001252 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001253}
1254
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001255int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1256 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001257{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001258 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001259 struct gsm48_hdr *gh;
1260
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001261 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1262 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001263
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001264 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1265 gh->proto_discr = GSM48_PDISC_RR;
1266 gh->msg_type = GSM48_MT_RR_APP_INFO;
1267 gh->data[0] = apdu_id;
1268 gh->data[1] = apdu_len;
1269 memcpy(gh->data+2, apdu, apdu_len);
1270
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001271 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001272}
1273
Alexander Couzens92f552f2016-08-23 07:32:27 +02001274/* FIXME: this count_statistics is a state machine behaviour. we should convert
1275 * the complete call control into a state machine. Afterwards we can move this
1276 * code into state transitions.
1277 */
1278static void count_statistics(struct gsm_trans *trans, int new_state)
1279{
1280 int old_state = trans->cc.state;
1281 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1282
1283 if (old_state == new_state)
1284 return;
1285
1286 /* state incoming */
1287 switch (new_state) {
1288 case GSM_CSTATE_ACTIVE:
1289 osmo_counter_inc(trans->net->active_calls);
1290 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1291 break;
1292 }
1293
1294 /* state outgoing */
1295 switch (old_state) {
1296 case GSM_CSTATE_ACTIVE:
1297 osmo_counter_dec(trans->net->active_calls);
1298 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1299 new_state == GSM_CSTATE_DISCONNECT_IND)
1300 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1301 else
1302 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1303 break;
1304 }
1305}
1306
Harald Welte4bc90a12008-12-27 16:32:52 +00001307/* Call Control */
1308
Harald Welte7584aea2009-02-11 11:44:12 +00001309/* The entire call control code is written in accordance with Figure 7.10c
1310 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1311 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1312 * it for voice */
1313
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314static void new_cc_state(struct gsm_trans *trans, int state)
1315{
1316 if (state > 31 || state < 0)
1317 return;
1318
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001319 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1320 trans->transaction_id,
1321 vlr_subscr_name(trans->vsub),
1322 gsm48_cc_state_name(trans->cc.state),
1323 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001324
Alexander Couzens92f552f2016-08-23 07:32:27 +02001325 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001326 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001327}
1328
1329static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001330{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001331 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001332 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001333 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001334
Harald Welte4bc90a12008-12-27 16:32:52 +00001335 gh->msg_type = GSM48_MT_CC_STATUS;
1336
1337 cause = msgb_put(msg, 3);
1338 cause[0] = 2;
1339 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1340 cause[2] = 0x80 | 30; /* response to status inquiry */
1341
1342 call_state = msgb_put(msg, 1);
1343 call_state[0] = 0xc0 | 0x00;
1344
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001345 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001346}
1347
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001348static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001349 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001350{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001351 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001352 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1353
Harald Welte6f4b7532008-12-29 00:39:37 +00001354 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001355 gh->msg_type = msg_type;
1356
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001357 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001358}
1359
Harald Welte4bfdfe72009-06-10 23:11:52 +08001360static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1361{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001362 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001363 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001364 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001365 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 }
1367}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001368
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1370 int msg_type, struct gsm_mncc *mncc)
1371{
1372 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001373 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374
Philipp Maierfbf66102017-04-09 12:32:51 +02001375 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1376
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001377#if BEFORE_MSCSPLIT
1378 /* Re-enable this log output once we can obtain this information via
1379 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001381 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001382 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001384 trans->conn->lchan->ts->trx->bts->nr,
1385 trans->conn->lchan->ts->trx->nr,
1386 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001387 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388 get_mncc_name(msg_type));
1389 else
1390 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1391 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001392 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 get_mncc_name(msg_type));
1394 else
1395 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1396 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001397#else
1398 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1399#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001400
1401 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001402
Harald Welte966636f2009-06-26 19:39:35 +02001403 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 if (!msg)
1405 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001406
1407 data = msgb_put(msg, sizeof(struct gsm_mncc));
1408 memcpy(data, mncc, sizeof(struct gsm_mncc));
1409
Harald Welte54209c22010-12-22 23:43:29 +01001410 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411
1412 return 0;
1413}
1414
1415int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001416 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001417{
1418 struct gsm_mncc rel;
1419
Harald Welte92f70c52009-06-12 01:54:08 +08001420 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001421 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001422 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001423 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1424 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001425 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1426}
1427
Harald Weltedcaf5652009-07-23 18:56:43 +02001428/* Call Control Specific transaction release.
1429 * gets called by trans_free, DO NOT CALL YOURSELF! */
1430void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001431{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001432 gsm48_stop_cc_timer(trans);
1433
Philipp Maier621ba032017-11-07 17:19:25 +01001434 /* Initiate the teardown of the related connections on the MGW */
1435 msc_mgcp_call_release(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001436
Harald Welte4bfdfe72009-06-10 23:11:52 +08001437 /* send release to L4, if callref still exists */
1438 if (trans->callref) {
1439 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001440 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001441 GSM48_CAUSE_LOC_PRN_S_LU,
1442 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Neels Hofmeyrdc2514b2018-04-09 23:29:14 +02001443 /* This is a final freeing of the transaction. The MNCC release may have triggered the
1444 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
1445 gsm48_stop_cc_timer(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001446 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001447 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001448 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001449}
1450
1451static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001452
Harald Welte09e38af2009-02-16 22:52:23 +00001453/* call-back from paging the B-end of the connection */
1454static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001455 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001456{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001457 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001458 struct gsm_trans *transt = _transt;
Pau Espin Pedrol25adebe2018-04-24 11:46:58 +02001459 enum gsm_paging_event paging_event = event;
Harald Weltec05677b2009-06-26 20:17:06 +02001460
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001461 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001462
Pau Espin Pedrol25adebe2018-04-24 11:46:58 +02001463 switch (paging_event) {
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001464 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001465 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1466 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001467 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001468 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001469 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Pau Espin Pedrol86f21262018-04-24 14:02:01 +02001470 transt->paging_request = NULL;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001471 /* send SETUP request to called party */
1472 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1473 break;
1474 case GSM_PAGING_EXPIRED:
1475 case GSM_PAGING_BUSY:
Pau Espin Pedrol3f8cec82018-04-24 13:52:00 +02001476 DEBUGP(DCC, "Paging subscr %s %s!\n",
1477 vlr_subscr_msisdn_or_name(transt->vsub),
1478 paging_event == GSM_PAGING_EXPIRED ? "expired" : "busy");
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001479 /* Temporarily out of order */
1480 mncc_release_ind(transt->net, transt,
1481 transt->callref,
1482 GSM48_CAUSE_LOC_PRN_S_LU,
1483 GSM48_CC_CAUSE_DEST_OOO);
1484 transt->callref = 0;
1485 transt->paging_request = NULL;
1486 trans_free(transt);
1487 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001488 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001489
Harald Welte09e38af2009-02-16 22:52:23 +00001490 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001491}
Harald Welte7584aea2009-02-11 11:44:12 +00001492
Harald Welte4bfdfe72009-06-10 23:11:52 +08001493/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001494static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001495{
Harald Welte53d51f52015-12-03 14:59:04 +01001496 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1497 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Philipp Maier621ba032017-11-07 17:19:25 +01001498 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001499
Harald Welte4bfdfe72009-06-10 23:11:52 +08001500 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001501 return -EIO;
1502
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001503 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001504 return -EIO;
1505
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001506 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001507 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001508
Philipp Maier621ba032017-11-07 17:19:25 +01001509 /* Bridge RTP streams */
1510 rc = msc_mgcp_call_complete(trans1, trans2->conn->rtp.local_port_cn,
1511 trans2->conn->rtp.local_addr_cn);
1512 if (rc)
1513 return -EINVAL;
1514
1515 rc = msc_mgcp_call_complete(trans2, trans1->conn->rtp.local_port_cn,
1516 trans1->conn->rtp.local_addr_cn);
1517 if (rc)
1518 return -EINVAL;
1519
1520 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001521}
1522
Harald Welte4bfdfe72009-06-10 23:11:52 +08001523static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1524{
1525 DEBUGP(DCC, "-> STATUS ENQ\n");
1526 return gsm48_cc_tx_status(trans, msg);
1527}
1528
1529static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1530static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1531
1532static void gsm48_cc_timeout(void *arg)
1533{
1534 struct gsm_trans *trans = arg;
1535 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001536 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1537 int mo_location = GSM48_CAUSE_LOC_USER;
1538 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1539 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001540 struct gsm_mncc mo_rel, l4_rel;
1541
1542 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1543 mo_rel.callref = trans->callref;
1544 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1545 l4_rel.callref = trans->callref;
1546
Harald Weltedcaf5652009-07-23 18:56:43 +02001547 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001548 case 0x303:
1549 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001550 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001551 break;
1552 case 0x310:
1553 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001554 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001555 break;
1556 case 0x313:
1557 disconnect = 1;
1558 /* unknown, did not find it in the specs */
1559 break;
1560 case 0x301:
1561 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001562 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001563 break;
1564 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001565 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001566 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001567 gsm48_cc_tx_release(trans, &trans->cc.msg);
1568 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569 break; /* stay in release state */
1570 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001571 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001572 return;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573 case 0x306:
1574 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001575 mo_cause = trans->cc.msg.cause.value;
1576 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 break;
1578 case 0x323:
1579 disconnect = 1;
1580 break;
1581 default:
1582 release = 1;
1583 }
1584
1585 if (release && trans->callref) {
1586 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001587 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001588 l4_location, l4_cause);
1589 trans->callref = 0;
1590 }
1591
1592 if (disconnect && trans->callref) {
1593 /* process disconnect towards layer 4 */
1594 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001595 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001596 }
1597
1598 /* process disconnect towards mobile station */
1599 if (disconnect || release) {
1600 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001601 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1602 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1603 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001604 mo_rel.cause.diag_len = 3;
1605
1606 if (disconnect)
1607 gsm48_cc_tx_disconnect(trans, &mo_rel);
1608 if (release)
1609 gsm48_cc_tx_release(trans, &mo_rel);
1610 }
1611
1612}
1613
Max3ffce192016-04-25 15:22:00 +02001614/* disconnect both calls from the bridge */
1615static inline void disconnect_bridge(struct gsm_network *net,
1616 struct gsm_mncc_bridge *bridge, int err)
1617{
1618 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1619 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1620 struct gsm_mncc mx_rel;
1621 if (!trans0 || !trans1)
1622 return;
1623
1624 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1625 trans0->callref, trans1->callref, strerror(err));
1626
1627 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1628 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1629 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1630
1631 mx_rel.callref = trans0->callref;
1632 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1633
1634 mx_rel.callref = trans1->callref;
1635 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1636}
1637
Harald Welte4bfdfe72009-06-10 23:11:52 +08001638static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1639 int sec, int micro)
1640{
1641 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001642 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001643 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001644 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001645}
1646
1647static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1648{
1649 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001650 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1652 struct tlv_parsed tp;
1653 struct gsm_mncc setup;
1654
1655 memset(&setup, 0, sizeof(struct gsm_mncc));
1656 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001657
Harald Welte474d19f2010-03-02 23:18:30 +01001658 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001660 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1661 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 setup.emergency = 1;
Harald Welte69c54a82018-02-09 20:41:14 +01001663 /* use destination number as configured by user (if any) */
1664 if (trans->net->emergency.route_to_msisdn) {
1665 setup.fields |= MNCC_F_CALLED;
1666 setup.called.type = 0; /* unknown */
1667 setup.called.plan = 0; /* unknown */
1668 OSMO_STRLCPY_ARRAY(setup.called.number,
1669 trans->net->emergency.route_to_msisdn);
1670 }
Harald Welte0dd01372018-01-24 11:06:19 +01001671 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001672
1673 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001674 setup.fields |= MNCC_F_CALLING;
Max98f74672018-02-05 12:57:06 +01001675 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
1676 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001677
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678 /* bearer capability */
1679 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1680 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001681 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001683
1684 /* Create a copy of the bearer capability
1685 * in the transaction struct, so we can use
1686 * this information later */
1687 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1688 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689 }
1690 /* facility */
1691 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1692 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001693 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1695 }
1696 /* called party bcd number */
1697 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1698 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001699 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1701 }
1702 /* user-user */
1703 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1704 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001705 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1707 }
1708 /* ss-version */
1709 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1710 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001711 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1713 }
1714 /* CLIR suppression */
1715 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1716 setup.clir.sup = 1;
1717 /* CLIR invocation */
1718 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1719 setup.clir.inv = 1;
1720 /* cc cap */
1721 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1722 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001723 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001724 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1725 }
1726
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727 new_cc_state(trans, GSM_CSTATE_INITIATED);
1728
Harald Welte79826e12018-01-24 10:50:24 +01001729 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1730 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001731 setup.called.number);
1732
Alexander Couzensb847a212016-08-02 11:34:11 +02001733 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001734
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001736 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737
Harald Welte13cac662009-07-29 12:10:35 +02001738 /* MNCC code will modify the channel asynchronously, we should
1739 * ipaccess-bind only after the modification has been made to the
1740 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741 return 0;
1742}
1743
1744static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001745{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001746 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001747 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001749 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001750
Harald Welte7ccf7782009-02-17 01:43:01 +00001751 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001752
Harald Welte4bfdfe72009-06-10 23:11:52 +08001753 /* transaction id must not be assigned */
1754 if (trans->transaction_id != 0xff) { /* unasssigned */
1755 DEBUGP(DCC, "TX Setup with assigned transaction. "
1756 "This is not allowed!\n");
1757 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001758 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001759 GSM48_CAUSE_LOC_PRN_S_LU,
1760 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001762 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763 return rc;
1764 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001765
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001767 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001768 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001769 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001771 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001772 GSM48_CAUSE_LOC_PRN_S_LU,
1773 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001775 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 return rc;
1777 }
Harald Welte78283ef2009-07-23 21:36:44 +02001778 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001779
Harald Welte65e74cc2008-12-29 01:55:35 +00001780 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001781
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001783
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784 /* bearer capability */
Harald Welte32958872018-02-12 11:56:51 +01001785 if (setup->fields & MNCC_F_BEARER_CAP) {
1786 /* Create a copy of the bearer capability in the transaction struct, so we
1787 * can use this information later */
1788 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte55c8f352010-03-07 23:40:35 +01001789 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte32958872018-02-12 11:56:51 +01001790 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 /* facility */
1792 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001793 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794 /* progress */
1795 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001796 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 /* calling party BCD number */
1798 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001799 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800 /* called party BCD number */
1801 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001802 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 /* user-user */
1804 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001805 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001806 /* redirecting party BCD number */
1807 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001808 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 /* signal */
1810 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001811 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001812
Harald Welte4bfdfe72009-06-10 23:11:52 +08001813 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001814
Alexander Couzensb847a212016-08-02 11:34:11 +02001815 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001816
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001817 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001818}
1819
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1821{
1822 struct gsm48_hdr *gh = msgb_l3(msg);
1823 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1824 struct tlv_parsed tp;
1825 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001826 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827
1828 gsm48_stop_cc_timer(trans);
1829 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1830
1831 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1832 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001833
Harald Welte474d19f2010-03-02 23:18:30 +01001834 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835#if 0
1836 /* repeat */
1837 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1838 call_conf.repeat = 1;
1839 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1840 call_conf.repeat = 2;
1841#endif
1842 /* bearer capability */
1843 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1844 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001845 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001847
1848 /* Create a copy of the bearer capability
1849 * in the transaction struct, so we can use
1850 * this information later */
1851 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1852 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 }
1854 /* cause */
1855 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1856 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001857 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001858 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1859 }
1860 /* cc cap */
1861 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1862 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001863 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1865 }
1866
Andreas Eversberg035b8742013-09-19 09:27:06 +02001867 /* IMSI of called subscriber */
Max98f74672018-02-05 12:57:06 +01001868 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
Andreas Eversberg035b8742013-09-19 09:27:06 +02001869
Harald Welte4bfdfe72009-06-10 23:11:52 +08001870 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1871
Philipp Maierfbf66102017-04-09 12:32:51 +02001872 /* Assign call (if not done yet) */
1873 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001874 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001875 trans->assignment_done = true;
1876 }
1877 else
1878 rc = 0;
1879
1880 /* don't continue, if there were problems with
1881 * the call assignment. */
1882 if (rc)
1883 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001884
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001885 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001886 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887}
1888
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001889static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890{
1891 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001892 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001894 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1897
1898 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1899
1900 /* bearer capability */
Harald Welte4804c552018-02-12 12:30:54 +01001901 if (proceeding->fields & MNCC_F_BEARER_CAP) {
Harald Welte55c8f352010-03-07 23:40:35 +01001902 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01001903 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1904 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 /* facility */
1906 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001907 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908 /* progress */
1909 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001910 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001912 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1913 if (rc)
1914 return rc;
1915
Philipp Maierfbf66102017-04-09 12:32:51 +02001916 /* Assign call (if not done yet) */
1917 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001918 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001919 trans->assignment_done = true;
1920 }
1921 else
1922 rc = 0;
1923
1924 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925}
1926
1927static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1928{
1929 struct gsm48_hdr *gh = msgb_l3(msg);
1930 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1931 struct tlv_parsed tp;
1932 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001933
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934 gsm48_stop_cc_timer(trans);
1935 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1936
1937 memset(&alerting, 0, sizeof(struct gsm_mncc));
1938 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001939 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940 /* facility */
1941 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1942 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001943 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001944 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1945 }
1946
1947 /* progress */
1948 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1949 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001950 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1952 }
1953 /* ss-version */
1954 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1955 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001956 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1958 }
1959
1960 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1961
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001962 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001963 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001964}
1965
1966static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1967{
1968 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001969 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1971
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 gh->msg_type = GSM48_MT_CC_ALERTING;
1973
1974 /* facility */
1975 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001976 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977 /* progress */
1978 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001979 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 /* user-user */
1981 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001982 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983
1984 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001985
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001986 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001987}
1988
1989static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1990{
1991 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001992 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1994
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 gh->msg_type = GSM48_MT_CC_PROGRESS;
1996
1997 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001998 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 /* user-user */
2000 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002001 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002003 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004}
2005
2006static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2007{
2008 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002009 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2011
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012 gh->msg_type = GSM48_MT_CC_CONNECT;
2013
2014 gsm48_stop_cc_timer(trans);
2015 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2016
2017 /* facility */
2018 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002019 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020 /* progress */
2021 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002022 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 /* connected number */
2024 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002025 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 /* user-user */
2027 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002028 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002029
2030 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2031
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002032 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002033}
2034
2035static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2036{
2037 struct gsm48_hdr *gh = msgb_l3(msg);
2038 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2039 struct tlv_parsed tp;
2040 struct gsm_mncc connect;
2041
2042 gsm48_stop_cc_timer(trans);
2043
2044 memset(&connect, 0, sizeof(struct gsm_mncc));
2045 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002046 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002048 connect.fields |= MNCC_F_CONNECTED;
Max98f74672018-02-05 12:57:06 +01002049 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
2050 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002051
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 /* facility */
2053 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2054 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002055 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2057 }
2058 /* user-user */
2059 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2060 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002061 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2063 }
2064 /* ss-version */
2065 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2066 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002067 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2069 }
2070
2071 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002072 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002074 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075}
2076
2077
2078static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2079{
2080 struct gsm_mncc connect_ack;
2081
2082 gsm48_stop_cc_timer(trans);
2083
2084 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002085 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002086
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2088 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002089
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002090 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 &connect_ack);
2092}
2093
2094static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2095{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002096 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2098
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2100
2101 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2102
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002103 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104}
2105
2106static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2107{
2108 struct gsm48_hdr *gh = msgb_l3(msg);
2109 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2110 struct tlv_parsed tp;
2111 struct gsm_mncc disc;
2112
2113 gsm48_stop_cc_timer(trans);
2114
2115 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2116
2117 memset(&disc, 0, sizeof(struct gsm_mncc));
2118 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002119 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120 /* cause */
2121 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2122 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2125 }
2126 /* facility */
2127 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2128 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002129 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2131 }
2132 /* user-user */
2133 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2134 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002135 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2137 }
2138 /* ss-version */
2139 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2140 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002141 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2143 }
2144
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002145 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146
2147}
2148
Harald Weltec66b71c2009-06-11 14:23:20 +08002149static struct gsm_mncc_cause default_cause = {
2150 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2151 .coding = 0,
2152 .rec = 0,
2153 .rec_val = 0,
2154 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2155 .diag_len = 0,
2156 .diag = { 0 },
2157};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158
2159static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2160{
2161 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002162 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2164
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2166
2167 gsm48_stop_cc_timer(trans);
2168 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2169
2170 /* cause */
2171 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002172 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173 else
Harald Welte55c8f352010-03-07 23:40:35 +01002174 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175
2176 /* facility */
2177 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002178 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 /* progress */
2180 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002181 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182 /* user-user */
2183 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002184 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002185
2186 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002187 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188
2189 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2190
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002191 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192}
2193
2194static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2195{
2196 struct gsm48_hdr *gh = msgb_l3(msg);
2197 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2198 struct tlv_parsed tp;
2199 struct gsm_mncc rel;
2200 int rc;
2201
2202 gsm48_stop_cc_timer(trans);
2203
2204 memset(&rel, 0, sizeof(struct gsm_mncc));
2205 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002206 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002207 /* cause */
2208 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2209 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002210 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2212 }
2213 /* facility */
2214 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2215 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002216 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2218 }
2219 /* user-user */
2220 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2221 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002222 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2224 }
2225 /* ss-version */
2226 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2227 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002228 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2230 }
2231
Harald Weltedcaf5652009-07-23 18:56:43 +02002232 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002234 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002236 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002237 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002238 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002239 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 }
2241
2242 new_cc_state(trans, GSM_CSTATE_NULL);
2243
2244 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002245 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246
2247 return rc;
2248}
2249
2250static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2251{
2252 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002253 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2255
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256 gh->msg_type = GSM48_MT_CC_RELEASE;
2257
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 gsm48_stop_cc_timer(trans);
2259 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2260
2261 /* cause */
2262 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002263 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264 /* facility */
2265 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002266 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267 /* user-user */
2268 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002269 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002270
Harald Weltedcaf5652009-07-23 18:56:43 +02002271 trans->cc.T308_second = 0;
2272 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273
Harald Weltedcaf5652009-07-23 18:56:43 +02002274 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2276
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002277 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278}
2279
2280static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2281{
2282 struct gsm48_hdr *gh = msgb_l3(msg);
2283 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2284 struct tlv_parsed tp;
2285 struct gsm_mncc rel;
2286 int rc = 0;
2287
2288 gsm48_stop_cc_timer(trans);
2289
2290 memset(&rel, 0, sizeof(struct gsm_mncc));
2291 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002292 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 /* cause */
2294 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2295 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002296 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2298 }
2299 /* facility */
2300 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2301 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002302 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2304 }
2305 /* user-user */
2306 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2307 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002308 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2310 }
2311 /* ss-version */
2312 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2313 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002314 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2316 }
2317
2318 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002319 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002321 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 MNCC_REJ_IND, &rel);
2323 break;
2324 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002325 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 MNCC_REL_CNF, &rel);
2327 break;
2328 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002329 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 MNCC_REL_IND, &rel);
2331 }
2332 }
2333
2334 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002335 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336
2337 return rc;
2338}
2339
2340static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2341{
2342 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002343 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002345 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2348
2349 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002350
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351 gsm48_stop_cc_timer(trans);
2352
2353 /* cause */
2354 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002355 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002356 /* facility */
2357 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002358 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 /* user-user */
2360 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002361 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362
Harald Weltec7782de2010-12-21 19:31:41 +01002363 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2364
Harald Weltedcaf5652009-07-23 18:56:43 +02002365 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366
Harald Weltec7782de2010-12-21 19:31:41 +01002367 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368}
2369
2370static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2371{
2372 struct gsm48_hdr *gh = msgb_l3(msg);
2373 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2374 struct tlv_parsed tp;
2375 struct gsm_mncc fac;
2376
2377 memset(&fac, 0, sizeof(struct gsm_mncc));
2378 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002379 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 /* facility */
2381 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2382 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002383 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2385 }
2386 /* ss-version */
2387 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2388 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002389 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2391 }
2392
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002393 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394}
2395
2396static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2397{
2398 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002399 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2401
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 gh->msg_type = GSM48_MT_CC_FACILITY;
2403
2404 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002405 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002407 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408}
2409
2410static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2411{
2412 struct gsm_mncc hold;
2413
2414 memset(&hold, 0, sizeof(struct gsm_mncc));
2415 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002416 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417}
2418
2419static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2420{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002421 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2423
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2425
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002426 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427}
2428
2429static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2430{
2431 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002432 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2434
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2436
2437 /* cause */
2438 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 else
Harald Welte55c8f352010-03-07 23:40:35 +01002441 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002443 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444}
2445
2446static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2447{
2448 struct gsm_mncc retrieve;
2449
2450 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2451 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002452 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002453 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454}
2455
2456static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2457{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002458 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2460
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2462
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002463 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464}
2465
2466static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2467{
2468 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002469 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2471
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2473
2474 /* cause */
2475 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002476 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477 else
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002480 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481}
2482
2483static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2484{
2485 struct gsm48_hdr *gh = msgb_l3(msg);
2486 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2487 struct tlv_parsed tp;
2488 struct gsm_mncc dtmf;
2489
2490 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2491 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002492 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 /* keypad facility */
2494 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2495 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002496 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2498 }
2499
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002500 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501}
2502
2503static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2504{
2505 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002506 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2508
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2510
2511 /* keypad */
2512 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002513 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002515 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516}
2517
2518static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2519{
2520 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002521 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2523
Harald Welte4bfdfe72009-06-10 23:11:52 +08002524 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2525
2526 /* cause */
2527 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002528 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529 else
Harald Welte55c8f352010-03-07 23:40:35 +01002530 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002532 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533}
2534
2535static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2536{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002537 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002538 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2539
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2541
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002542 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543}
2544
2545static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2546{
2547 struct gsm_mncc dtmf;
2548
2549 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2550 dtmf.callref = trans->callref;
2551
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002552 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002553}
2554
2555static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2556{
2557 struct gsm48_hdr *gh = msgb_l3(msg);
2558 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2559 struct tlv_parsed tp;
2560 struct gsm_mncc modify;
2561
2562 memset(&modify, 0, sizeof(struct gsm_mncc));
2563 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002564 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002565 /* bearer capability */
2566 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2567 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002568 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002570
2571 /* Create a copy of the bearer capability
2572 * in the transaction struct, so we can use
2573 * this information later */
2574 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2575 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576 }
2577
2578 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2579
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002580 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581}
2582
2583static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2584{
2585 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002586 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2588
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589 gh->msg_type = GSM48_MT_CC_MODIFY;
2590
2591 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2592
2593 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002594 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002595 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596
2597 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2598
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002599 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600}
2601
2602static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2603{
2604 struct gsm48_hdr *gh = msgb_l3(msg);
2605 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2606 struct tlv_parsed tp;
2607 struct gsm_mncc modify;
2608
2609 gsm48_stop_cc_timer(trans);
2610
2611 memset(&modify, 0, sizeof(struct gsm_mncc));
2612 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002613 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614 /* bearer capability */
2615 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2616 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002617 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002618 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002619
2620 /* Create a copy of the bearer capability
2621 * in the transaction struct, so we can use
2622 * this information later */
2623 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2624 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 }
2626
2627 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2628
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002629 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630}
2631
2632static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2633{
2634 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002635 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002636 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2637
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2639
2640 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002641 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002642 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002643
2644 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2645
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002646 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647}
2648
2649static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2650{
2651 struct gsm48_hdr *gh = msgb_l3(msg);
2652 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2653 struct tlv_parsed tp;
2654 struct gsm_mncc modify;
2655
2656 gsm48_stop_cc_timer(trans);
2657
2658 memset(&modify, 0, sizeof(struct gsm_mncc));
2659 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002660 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661 /* bearer capability */
2662 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2663 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002664 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002666
2667 /* Create a copy of the bearer capability
2668 * in the transaction struct, so we can use
2669 * this information later */
2670 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2671 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 }
2673 /* cause */
2674 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2675 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002676 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2678 }
2679
2680 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2681
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002682 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683}
2684
2685static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2686{
2687 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002688 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2690
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2692
2693 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002694 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002695 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002697 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698
2699 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2700
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002701 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702}
2703
2704static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2705{
2706 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002707 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2709
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710 gh->msg_type = GSM48_MT_CC_NOTIFY;
2711
2712 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002713 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002715 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002716}
2717
2718static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2719{
2720 struct gsm48_hdr *gh = msgb_l3(msg);
2721 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2722// struct tlv_parsed tp;
2723 struct gsm_mncc notify;
2724
2725 memset(&notify, 0, sizeof(struct gsm_mncc));
2726 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002727// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002728 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002729 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002731 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732}
2733
2734static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2735{
2736 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002737 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2739
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740 gh->msg_type = GSM48_MT_CC_USER_INFO;
2741
2742 /* user-user */
2743 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002744 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745 /* more data */
2746 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002747 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002748
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002749 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750}
2751
2752static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2753{
2754 struct gsm48_hdr *gh = msgb_l3(msg);
2755 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2756 struct tlv_parsed tp;
2757 struct gsm_mncc user;
2758
2759 memset(&user, 0, sizeof(struct gsm_mncc));
2760 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002761 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762 /* user-user */
2763 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2764 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002765 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2767 }
2768 /* more data */
2769 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2770 user.more = 1;
2771
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002772 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773}
2774
Philipp Maierfbf66102017-04-09 12:32:51 +02002775static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2776 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2777 uint32_t payload_msg_type)
2778{
2779 uint8_t data[sizeof(struct gsm_mncc)];
2780 struct gsm_mncc_rtp *rtp;
2781
2782 memset(&data, 0, sizeof(data));
2783 rtp = (struct gsm_mncc_rtp *) &data[0];
2784
2785 rtp->callref = callref;
2786 rtp->msg_type = cmd;
2787 rtp->ip = addr;
2788 rtp->port = port;
2789 rtp->payload_type = payload_type;
2790 rtp->payload_msg_type = payload_msg_type;
2791 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2792}
2793
2794static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2795{
2796 int msg_type;
2797
2798 /* FIXME This has to be set to some meaningful value.
2799 * Possible options are:
2800 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2801 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2802 * (0 if unknown) */
2803 msg_type = GSM_TCHF_FRAME;
2804
Philipp Maier621ba032017-11-07 17:19:25 +01002805 uint32_t addr = inet_addr(trans->conn->rtp.local_addr_cn);
2806 uint16_t port = trans->conn->rtp.local_port_cn;
2807
2808 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2809
2810 if (addr == INADDR_NONE) {
2811 LOGP(DMNCC, LOGL_ERROR,
2812 "(subscriber:%s) external MNCC is signalling invalid IP-Address\n",
2813 vlr_subscr_name(trans->vsub));
2814 return;
2815 }
2816 if (port == 0) {
2817 LOGP(DMNCC, LOGL_ERROR,
2818 "(subscriber:%s) external MNCC is signalling invalid Port\n",
2819 vlr_subscr_name(trans->vsub));
2820 return;
2821 }
Philipp Maierfbf66102017-04-09 12:32:51 +02002822
2823 /* FIXME: This has to be set to some meaningful value,
2824 * before the MSC-Split, this value was pulled from
2825 * lchan->abis_ip.rtp_payload */
2826 uint32_t payload_type = 0;
2827
2828 return mncc_recv_rtp(net, trans->callref, cmd,
2829 addr,
2830 port,
2831 payload_type,
2832 msg_type);
2833}
2834
2835static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2836{
2837 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2838}
2839
2840static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2841{
2842 struct gsm_trans *trans;
2843 int rc;
2844
2845 /* Find callref */
2846 trans = trans_find_by_callref(net, callref);
2847 if (!trans) {
2848 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2849 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2850 return -EIO;
2851 }
2852 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2853 if (!trans->conn) {
2854 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2855 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2856 return 0;
2857 }
2858
2859 trans->conn->mncc_rtp_bridge = 1;
2860
Philipp Maier621ba032017-11-07 17:19:25 +01002861 /* When we call msc_mgcp_call_assignment() we will trigger, depending
Philipp Maierfbf66102017-04-09 12:32:51 +02002862 * on the RAN type the call assignment on the A or Iu interface.
Philipp Maier621ba032017-11-07 17:19:25 +01002863 * msc_mgcp_call_assignment() also takes care about sending the CRCX
Philipp Maierfbf66102017-04-09 12:32:51 +02002864 * command to the MGCP-GW. The CRCX will return the port number,
2865 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2866 * have to return this port number back to the MNCC by sending
2867 * it back with the TCH_RTP_CREATE message. To make sure that
2868 * this message is sent AFTER the response to CRCX from the
Philipp Maier621ba032017-11-07 17:19:25 +01002869 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
Philipp Maierfbf66102017-04-09 12:32:51 +02002870 * to take care of this by setting trans->tch_rtp_create to true.
2871 * This will make sure that gsm48_tch_rtp_create() (below) is
2872 * called as soon as the local port number has become known. */
2873 trans->tch_rtp_create = true;
2874
2875 /* Assign call (if not done yet) */
2876 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01002877 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02002878 trans->assignment_done = true;
2879 }
2880 else
2881 rc = 0;
2882
2883 return rc;
2884}
2885
2886/* Trigger TCH_RTP_CREATE acknowledgement */
2887int gsm48_tch_rtp_create(struct gsm_trans *trans)
2888{
2889 /* This function is called as soon as the port, on which the
2890 * mgcp-gw expects the incoming RTP stream from the remote
2891 * end (e.g. Asterisk) is known. */
2892
2893 struct gsm_subscriber_connection *conn = trans->conn;
2894 struct gsm_network *network = conn->network;
2895
2896 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2897 return 0;
2898}
2899
2900static int tch_rtp_connect(struct gsm_network *net, void *arg)
2901{
2902 struct gsm_trans *trans;
2903 struct gsm_mncc_rtp *rtp = arg;
Philipp Maier621ba032017-11-07 17:19:25 +01002904 struct in_addr addr;
Philipp Maierfbf66102017-04-09 12:32:51 +02002905
2906 /* Find callref */
2907 trans = trans_find_by_callref(net, rtp->callref);
2908 if (!trans) {
2909 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2910 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2911 return -EIO;
2912 }
2913 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2914 if (!trans->conn) {
2915 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2916 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2917 return 0;
2918 }
2919
Philipp Maier621ba032017-11-07 17:19:25 +01002920 addr.s_addr = osmo_htonl(rtp->ip);
2921 return msc_mgcp_call_complete(trans, rtp->port, inet_ntoa(addr));
Philipp Maierfbf66102017-04-09 12:32:51 +02002922}
2923
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002925 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926 int type;
2927 int (*rout) (struct gsm_trans *trans, void *arg);
2928} downstatelist[] = {
2929 /* mobile originating call establishment */
2930 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002931 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2933 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2934 {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 */
2935 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2936 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2937 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2938 /* mobile terminating call establishment */
2939 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2940 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2941 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2942 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2943 /* signalling during call */
2944 {SBIT(GSM_CSTATE_ACTIVE),
2945 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2946 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2947 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2948 {ALL_STATES,
2949 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2950 {ALL_STATES,
2951 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2952 {ALL_STATES,
2953 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2954 {SBIT(GSM_CSTATE_ACTIVE),
2955 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2956 {SBIT(GSM_CSTATE_ACTIVE),
2957 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2958 {SBIT(GSM_CSTATE_ACTIVE),
2959 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2960 {SBIT(GSM_CSTATE_ACTIVE),
2961 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2962 {SBIT(GSM_CSTATE_ACTIVE),
2963 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2964 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2965 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2966 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2967 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2968 {SBIT(GSM_CSTATE_ACTIVE),
2969 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2970 /* clearing */
2971 {SBIT(GSM_CSTATE_INITIATED),
2972 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2973 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2974 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2975 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2976 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002977};
2978
2979#define DOWNSLLEN \
2980 (sizeof(downstatelist) / sizeof(struct downstate))
2981
2982
Harald Welte76556372010-12-22 23:57:45 +01002983int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984{
Harald Welte1a6f7982009-08-09 18:52:33 +02002985 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002987 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002988 struct gsm_mncc *data = arg, rel;
2989
Harald Welte04dc88f2010-12-22 21:45:05 +01002990 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2991
Harald Welte4bfdfe72009-06-10 23:11:52 +08002992 /* handle special messages */
2993 switch(msg_type) {
2994 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002995 rc = tch_bridge(net, arg);
2996 if (rc < 0)
2997 disconnect_bridge(net, arg, -rc);
2998 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002999 case MNCC_RTP_CREATE:
3000 return tch_rtp_create(net, data->callref);
3001 case MNCC_RTP_CONNECT:
3002 return tch_rtp_connect(net, arg);
3003 case MNCC_RTP_FREE:
3004 /* unused right now */
3005 return -EIO;
3006
Harald Welte4bfdfe72009-06-10 23:11:52 +08003007 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003009 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003010 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003011 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003012 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003013 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
3014 get_mncc_name(msg_type));
3015 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016 }
3017
3018 memset(&rel, 0, sizeof(struct gsm_mncc));
3019 rel.callref = data->callref;
3020
3021 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003022 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003023
3024 /* Callref unknown */
3025 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02003026 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003027
Harald Welte4a3464c2009-07-04 10:11:24 +02003028 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003029 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3030 "Received '%s' from MNCC with "
3031 "unknown callref %d\n", data->called.number,
3032 get_mncc_name(msg_type), data->callref);
3033 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003034 return mncc_release_ind(net, NULL, data->callref,
3035 GSM48_CAUSE_LOC_PRN_S_LU,
3036 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003037 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003038 if (!data->called.number[0] && !data->imsi[0]) {
3039 DEBUGP(DCC, "(bts - trx - ts - ti) "
3040 "Received '%s' from MNCC with "
3041 "no number or IMSI\n", get_mncc_name(msg_type));
3042 /* Invalid number */
3043 return mncc_release_ind(net, NULL, data->callref,
3044 GSM48_CAUSE_LOC_PRN_S_LU,
3045 GSM48_CC_CAUSE_INV_NR_FORMAT);
3046 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003048 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02003049 vsub = vlr_subscr_find_by_msisdn(net->vlr,
3050 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003051 else
Harald Welte2483f1b2016-06-19 18:06:02 +02003052 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003053
3054 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003055 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003056
Harald Welte4bfdfe72009-06-10 23:11:52 +08003057 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02003058 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3060 "Received '%s' from MNCC with "
3061 "unknown subscriber %s\n", data->called.number,
3062 get_mncc_name(msg_type), data->called.number);
3063 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003064 return mncc_release_ind(net, NULL, data->callref,
3065 GSM48_CAUSE_LOC_PRN_S_LU,
3066 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003067 }
3068 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02003069 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003070 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3071 "Received '%s' from MNCC with "
3072 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01003073 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02003074 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003076 return mncc_release_ind(net, NULL, data->callref,
3077 GSM48_CAUSE_LOC_PRN_S_LU,
3078 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003079 }
3080 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003081 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003082 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003084 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003085 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003086 mncc_release_ind(net, NULL, data->callref,
3087 GSM48_CAUSE_LOC_PRN_S_LU,
3088 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003089 return -ENOMEM;
3090 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003091
3092 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02003093 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003094
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003095 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003096 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003097 /* find transaction with this subscriber already paging */
3098 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003099 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003100 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02003101 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003102 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003103 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003104 "Received '%s' from MNCC with "
3105 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003106 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003107 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003108 get_mncc_name(msg_type), vsub->lac);
3109 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003110 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003111 return 0;
3112 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003113 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02003114 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003115
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003116 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003117 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003118 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003119 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003120 trans,
3121 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003122 if (!trans->paging_request) {
3123 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003124 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003125 trans_free(trans);
3126 return 0;
3127 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003128 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129 return 0;
3130 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003131
3132 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003133 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte0e2fa5d2018-04-09 16:35:01 +02003134 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Harald Welte2483f1b2016-06-19 18:06:02 +02003135 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003136 } else {
3137 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003138 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003139 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003140
3141 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003142 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143
3144 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003145 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003146 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003147 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003148 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003149 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003150 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3151 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152 if (msg_type == MNCC_REL_REQ)
3153 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3154 else
3155 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3156 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003157 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003158 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003159 } else {
3160 DEBUGP(DCC, "(ti %02x sub %s) "
3161 "Received '%s' from MNCC in state %d (%s)\n",
3162 trans->transaction_id,
3163 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3164 get_mncc_name(msg_type), trans->cc.state,
3165 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166 }
3167
Harald Welte4bfdfe72009-06-10 23:11:52 +08003168 /* Find function for current state and message */
3169 for (i = 0; i < DOWNSLLEN; i++)
3170 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003171 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003172 break;
3173 if (i == DOWNSLLEN) {
3174 DEBUGP(DCC, "Message unhandled at this state.\n");
3175 return 0;
3176 }
3177
3178 rc = downstatelist[i].rout(trans, arg);
3179
3180 return rc;
3181}
3182
3183
3184static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003185 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003186 int type;
3187 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3188} datastatelist[] = {
3189 /* mobile originating call establishment */
3190 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3191 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3192 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3193 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3194 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3195 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3196 /* mobile terminating call establishment */
3197 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3198 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3199 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3200 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003201 {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 +08003202 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3203 /* signalling during call */
3204 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3205 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3206 {SBIT(GSM_CSTATE_ACTIVE),
3207 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3208 {ALL_STATES,
3209 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3210 {ALL_STATES,
3211 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3212 {ALL_STATES,
3213 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3214 {SBIT(GSM_CSTATE_ACTIVE),
3215 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3216 {SBIT(GSM_CSTATE_ACTIVE),
3217 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3218 {SBIT(GSM_CSTATE_ACTIVE),
3219 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3220 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3221 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3222 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3223 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3224 {SBIT(GSM_CSTATE_ACTIVE),
3225 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3226 /* clearing */
3227 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3228 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3229 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3230 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3231 {ALL_STATES, /* 5.4.3.4 */
3232 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3233};
3234
3235#define DATASLLEN \
3236 (sizeof(datastatelist) / sizeof(struct datastate))
3237
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003238static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003239{
3240 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003241 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003242 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003243 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003244 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003245
Harald Welte4bfdfe72009-06-10 23:11:52 +08003246 if (msg_type & 0x80) {
3247 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3248 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003249 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003250
Harald Welte2483f1b2016-06-19 18:06:02 +02003251 if (!conn->vsub) {
3252 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003253 return -EINVAL;
3254 }
3255
Harald Welte4bfdfe72009-06-10 23:11:52 +08003256 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003257 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003258
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003259#if BEFORE_MSCSPLIT
3260 /* Re-enable this log output once we can obtain this information via
3261 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003262 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003263 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003264 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003265 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003266 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3267 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003268#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003269
3270 /* Create transaction */
3271 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003272 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003273 "creating new trans.\n", transaction_id);
3274 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003275 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003276 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003277 transaction_id, new_callref++);
3278 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003279 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003280 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003281 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003282 GSM48_MT_CC_RELEASE_COMPL);
3283 return -ENOMEM;
3284 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003285 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003286 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte0e2fa5d2018-04-09 16:35:01 +02003287 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Harald Welte2483f1b2016-06-19 18:06:02 +02003288 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003289 }
3290
3291 /* find function for current state and message */
3292 for (i = 0; i < DATASLLEN; i++)
3293 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003294 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003295 break;
3296 if (i == DATASLLEN) {
3297 DEBUGP(DCC, "Message unhandled at this state.\n");
3298 return 0;
3299 }
3300
Harald Welte2483f1b2016-06-19 18:06:02 +02003301 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003302
Harald Welte4bfdfe72009-06-10 23:11:52 +08003303 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003304
Harald Welte2483f1b2016-06-19 18:06:02 +02003305 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003306 return rc;
3307}
3308
Harald Welte2483f1b2016-06-19 18:06:02 +02003309static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003310{
Harald Welte2483f1b2016-06-19 18:06:02 +02003311 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3312 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003313
Harald Welte2483f1b2016-06-19 18:06:02 +02003314 switch (pdisc) {
3315 case GSM48_PDISC_MM:
3316 switch (msg_type) {
3317 case GSM48_MT_MM_LOC_UPD_REQUEST:
3318 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003319 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003320 case GSM48_MT_MM_AUTH_RESP:
3321 case GSM48_MT_MM_AUTH_FAIL:
3322 case GSM48_MT_MM_ID_RESP:
3323 case GSM48_MT_MM_TMSI_REALL_COMPL:
3324 case GSM48_MT_MM_IMSI_DETACH_IND:
3325 return true;
3326 default:
3327 break;
3328 }
3329 break;
3330 case GSM48_PDISC_RR:
3331 switch (msg_type) {
3332 case GSM48_MT_RR_CIPH_M_COMPL:
3333 case GSM48_MT_RR_PAG_RESP:
3334 return true;
3335 default:
3336 break;
3337 }
3338 break;
3339 default:
3340 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003341 }
3342
Harald Welte2483f1b2016-06-19 18:06:02 +02003343 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003344}
3345
Harald Welte2483f1b2016-06-19 18:06:02 +02003346void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3347 struct msgb *msg)
3348{
3349
3350 /* If a CM Service Request was received before, this is the request the
3351 * conn was opened for. No need to wait for further messages. */
3352
3353 if (!conn->received_cm_service_request)
3354 return;
3355
3356 if (log_check_level(DMM, LOGL_DEBUG)) {
3357 struct gsm48_hdr *gh = msgb_l3(msg);
3358 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3359 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3360
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003361 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003362 " received_cm_service_request changes to false\n",
3363 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003364 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003365 }
3366 conn->received_cm_service_request = false;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003367 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
Harald Welte2483f1b2016-06-19 18:06:02 +02003368}
3369
Harald Weltec2007852018-02-03 21:08:26 +01003370/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
3371int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
3372{
3373 switch (pdisc) {
3374 case GSM48_PDISC_MM:
3375 case GSM48_PDISC_CC:
3376 case GSM48_PDISC_NC_SS:
3377 return 0;
3378 case GSM48_PDISC_GROUP_CC:
3379 return 1;
3380 case GSM48_PDISC_BCAST_CC:
3381 return 2;
3382 case GSM48_PDISC_LOC:
3383 return 3;
3384 default:
3385 return -1;
3386 }
3387}
3388
3389/* extract the N(SD) and return the modulo value for a R98 message */
3390static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3391{
3392 switch (pdisc) {
3393 case GSM48_PDISC_MM:
3394 case GSM48_PDISC_CC:
3395 case GSM48_PDISC_NC_SS:
3396 *n_sd = (msg_type >> 6) & 0x3;
3397 return 4;
3398 case GSM48_PDISC_GROUP_CC:
3399 case GSM48_PDISC_BCAST_CC:
3400 case GSM48_PDISC_LOC:
3401 *n_sd = (msg_type >> 6) & 0x1;
3402 return 2;
3403 default:
3404 /* no sequence number, we cannot detect dups */
3405 return 0;
3406 }
3407}
3408
3409/* extract the N(SD) and return the modulo value for a R99 message */
3410static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3411{
3412 switch (pdisc) {
3413 case GSM48_PDISC_MM:
3414 case GSM48_PDISC_CC:
3415 case GSM48_PDISC_NC_SS:
3416 case GSM48_PDISC_GROUP_CC:
3417 case GSM48_PDISC_BCAST_CC:
3418 case GSM48_PDISC_LOC:
3419 *n_sd = (msg_type >> 6) & 0x1;
3420 return 2;
3421 default:
3422 /* no sequence number, we cannot detect dups */
3423 return 0;
3424 }
3425}
3426
3427/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
3428static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
3429{
3430 struct gsm48_hdr *gh;
3431 uint8_t pdisc;
3432 uint8_t n_sd, modulo, bin;
3433
3434 gh = msgb_l3(msg);
3435 pdisc = gsm48_hdr_pdisc(gh);
3436
3437 if (conn->classmark.classmark1_set && conn->classmark.classmark1.rev_lev < 2) {
3438 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
3439 } else { /* R99 */
3440 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
3441 }
3442 if (modulo == 0)
3443 return false;
3444 bin = gsm0407_pdisc_ctr_bin(pdisc);
3445 if (bin < 0)
3446 return false;
3447
3448 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
3449 if (n_sd != conn->n_sd_next[bin]) {
3450 /* not what we expected: duplicate */
3451 return true;
3452 } else {
3453 /* as expected: no dup; update expected counter for next message */
3454 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
3455 return false;
3456 }
3457}
Harald Welte2483f1b2016-06-19 18:06:02 +02003458
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003459/* 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 +08003460int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003461{
Philipp Maier91f10c72017-04-20 18:40:37 +02003462 struct gsm48_hdr *gh;
3463 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003464 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003465
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003466 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003467 OSMO_ASSERT(conn);
3468 OSMO_ASSERT(msg);
3469
Philipp Maier91f10c72017-04-20 18:40:37 +02003470 gh = msgb_l3(msg);
3471 pdisc = gsm48_hdr_pdisc(gh);
3472
Harald Weltec2007852018-02-03 21:08:26 +01003473 if (gsm0407_is_duplicate(conn, msg)) {
3474 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
3475 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
3476 return 0;
3477 }
3478
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003479 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3480 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3481 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003482
3483 if (!msc_subscr_conn_is_accepted(conn)
3484 && !msg_is_initially_permitted(gh)) {
3485 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003486 "subscr %s: Message not permitted for initial conn: %s\n",
3487 vlr_subscr_name(conn->vsub),
3488 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003489 return -EACCES;
3490 }
3491
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003492 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3493 LOGP(DMM, LOGL_ERROR,
3494 "%s: Illegal situation: RAN type mismatch:"
3495 " attached via %s, received message via %s\n",
3496 vlr_subscr_name(conn->vsub),
3497 ran_type_name(conn->vsub->cs.attached_via_ran),
3498 ran_type_name(conn->via_ran));
3499 return -EACCES;
3500 }
3501
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003502#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003503 if (silent_call_reroute(conn, msg))
3504 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003505#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003506
Harald Welte52b1f982008-12-23 20:25:15 +00003507 switch (pdisc) {
3508 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003509 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003510 break;
3511 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003512 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003513 break;
3514 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003515 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003516 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003517 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003518 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003519 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003520 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003521 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003522 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3523 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003524 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003525 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003526 case GSM48_PDISC_NC_SS:
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +07003527 rc = gsm0911_rcv_nc_ss(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003528 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003529 case GSM48_PDISC_TEST:
3530 rc = gsm0414_rcv_test(conn, msg);
3531 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003532 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003533 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3534 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003535 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003536 break;
3537 }
3538
3539 return rc;
3540}
Harald Welte8470bf22008-12-25 23:28:35 +00003541
Harald Welte2483f1b2016-06-19 18:06:02 +02003542/***********************************************************************
3543 * VLR integration
3544 ***********************************************************************/
3545
3546/* VLR asks us to send an authentication request */
3547static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3548 bool send_autn)
3549{
3550 struct gsm_subscriber_connection *conn = msc_conn_ref;
3551 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3552 send_autn? at->vec.autn : NULL,
3553 at->key_seq);
3554}
3555
3556/* VLR asks us to send an authentication reject */
3557static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3558{
3559 struct gsm_subscriber_connection *conn = msc_conn_ref;
3560 return gsm48_tx_mm_auth_rej(conn);
3561}
3562
3563/* VLR asks us to transmit an Identity Request of given type */
3564static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3565{
3566 struct gsm_subscriber_connection *conn = msc_conn_ref;
3567 return mm_tx_identity_req(conn, mi_type);
3568}
3569
3570/* VLR asks us to transmit a Location Update Accept */
3571static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3572{
3573 struct gsm_subscriber_connection *conn = msc_conn_ref;
3574 return gsm0408_loc_upd_acc(conn, send_tmsi);
3575}
3576
3577/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02003578static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02003579{
3580 struct gsm_subscriber_connection *conn = msc_conn_ref;
3581 return gsm0408_loc_upd_rej(conn, cause);
3582}
3583
3584/* VLR asks us to transmit a CM Service Accept */
3585static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3586{
3587 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003588 return msc_gsm48_tx_mm_serv_ack(conn);
3589}
3590
3591static int msc_vlr_tx_common_id(void *msc_conn_ref)
3592{
3593 struct gsm_subscriber_connection *conn = msc_conn_ref;
3594 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003595}
3596
Stefan Sperling3a741282018-03-13 21:11:49 +01003597/* VLR asks us to transmit MM info. */
3598static int msc_vlr_tx_mm_info(void *msc_conn_ref)
3599{
3600 struct gsm_subscriber_connection *conn = msc_conn_ref;
3601 if (!conn->network->send_mm_info)
3602 return 0;
3603 return gsm48_tx_mm_info(conn);
3604}
3605
Harald Welte2483f1b2016-06-19 18:06:02 +02003606/* VLR asks us to transmit a CM Service Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02003607static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02003608{
Harald Welte2483f1b2016-06-19 18:06:02 +02003609 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003610 int rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02003611
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003612 rc = msc_gsm48_tx_mm_serv_rej(conn, cause);
3613
3614 if (conn->received_cm_service_request) {
3615 conn->received_cm_service_request = false;
3616 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
3617 }
3618
3619 return rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02003620}
3621
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003622/* For msc_vlr_set_ciph_mode() */
3623osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3624 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3625
Neels Hofmeyrda21a522018-03-02 01:50:09 +01003626/* VLR asks us to start using ciphering.
3627 * (Keep non-static to allow regression testing on this function.) */
3628int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3629 bool umts_aka,
3630 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02003631{
3632 struct gsm_subscriber_connection *conn = msc_conn_ref;
3633 struct vlr_subscr *vsub;
3634 struct gsm_auth_tuple *tuple;
3635
3636 if (!conn || !conn->vsub) {
3637 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3638 " NULL conn/subscriber");
3639 return -EINVAL;
3640 }
3641
3642 vsub = conn->vsub;
3643 tuple = vsub->last_tuple;
3644
3645 if (!tuple) {
3646 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3647 " Command: no auth tuple available\n",
3648 vlr_subscr_name(vsub));
3649 return -EINVAL;
3650 }
3651
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003652 switch (conn->via_ran) {
3653 case RAN_GERAN_A:
3654 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3655 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003656 {
Harald Welte7b222aa2017-12-23 19:30:32 +01003657 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003658 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01003659 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003660
Harald Welte7b222aa2017-12-23 19:30:32 +01003661 for (i = 0; i < 8; i++) {
Harald Welte71330722017-12-23 19:59:02 +01003662 if (net->a5_encryption_mask & (1 << i) &&
3663 classmark_supports_a5(&conn->classmark, i))
Harald Welte7b222aa2017-12-23 19:30:32 +01003664 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
3665 }
3666 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003667
Harald Welte71330722017-12-23 19:59:02 +01003668 if (ei.perm_algo_len == 0) {
3669 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
3670 "between MSC-configured and MS-supported A5 algorithms\n",
3671 vlr_subscr_name(conn->vsub));
3672 return -ENOTSUP;
3673 }
3674
Neels Hofmeyr52821712017-12-18 01:23:42 +01003675 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3676 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3677 * necessarily a match for the UMTS AKA tokens. */
3678 if (umts_aka)
3679 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3680 else
3681 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003682 ei.key_len = sizeof(tuple->vec.kc);
3683
3684 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3685 }
3686
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003687 case RAN_UTRAN_IU:
3688#ifdef BUILD_IU
3689 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3690 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003691 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003692#else
3693 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3694 " built without Iu support\n");
3695 return -ENOTSUP;
3696#endif
3697
3698 default:
3699 break;
3700 }
3701 LOGP(DMM, LOGL_ERROR,
3702 "%s: cannot start ciphering, unknown RAN type %d\n",
3703 vlr_subscr_name(conn->vsub), conn->via_ran);
3704 return -ENOTSUP;
3705}
3706
3707void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3708{
3709 struct vlr_ciph_result vlr_res = {};
3710
3711 if (!conn || !conn->vsub) {
3712 LOGP(DMM, LOGL_ERROR,
3713 "Rx Security Mode Complete for invalid conn\n");
3714 return;
3715 }
3716
3717 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3718 vlr_subscr_name(conn->vsub));
3719
3720 vlr_res.cause = VLR_CIPH_COMPL;
3721 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003722}
3723
3724/* VLR informs us that the subscriber data has somehow been modified */
3725static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3726{
Maxa263bb22017-12-27 13:23:44 +01003727 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3728 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003729}
3730
3731/* VLR informs us that the subscriber has been associated with a conn */
3732static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3733 struct vlr_subscr *vsub)
3734{
3735 struct gsm_subscriber_connection *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01003736 OSMO_ASSERT(vsub);
Harald Welte2483f1b2016-06-19 18:06:02 +02003737 OSMO_ASSERT(!conn->vsub);
3738 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01003739 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003740 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003741}
3742
3743/* operations that we need to implement for libvlr */
3744static const struct vlr_ops msc_vlr_ops = {
3745 .tx_auth_req = msc_vlr_tx_auth_req,
3746 .tx_auth_rej = msc_vlr_tx_auth_rej,
3747 .tx_id_req = msc_vlr_tx_id_req,
3748 .tx_lu_acc = msc_vlr_tx_lu_acc,
3749 .tx_lu_rej = msc_vlr_tx_lu_rej,
3750 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3751 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3752 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003753 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01003754 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02003755 .subscr_update = msc_vlr_subscr_update,
3756 .subscr_assoc = msc_vlr_subscr_assoc,
3757};
3758
3759/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3760int msc_vlr_alloc(struct gsm_network *net)
3761{
3762 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3763 if (!net->vlr)
3764 return -ENOMEM;
3765 net->vlr->user_ctx = net;
3766 return 0;
3767}
3768
3769/* Launch the VLR, i.e. its GSUP connection */
3770int msc_vlr_start(struct gsm_network *net)
3771{
3772 OSMO_ASSERT(net->vlr);
3773 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3774 net->gsup_server_port);
3775}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01003776
3777struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
3778{
3779 struct msgb *msg;
3780 struct gsm48_hdr *gh;
3781
3782 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
3783 if (!msg)
3784 return NULL;
3785
3786 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3787 gh->proto_discr = GSM48_PDISC_MM;
3788 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
3789 gh->data[0] = value;
3790
3791 return msg;
3792}
3793
3794struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
3795{
3796 struct gsm48_hdr *gh;
3797 struct msgb *msg;
3798
3799 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
3800 if (!msg)
3801 return NULL;
3802
3803 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3804 gh->proto_discr = GSM48_PDISC_MM;
3805 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
3806 gh->data[0] = cause;
3807 return msg;
3808}
3809
3810int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
3811{
3812 /* Check the size for the classmark */
3813 if (length < 1 + *classmark2_lv)
3814 return -1;
3815
3816 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
3817 if (length < 2 + *classmark2_lv + mi_lv[0])
3818 return -2;
3819
3820 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
3821 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
3822}
3823
3824int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
3825 char *mi_string, uint8_t *mi_type)
3826{
3827 static const uint32_t classmark_offset =
3828 offsetof(struct gsm48_pag_resp, classmark2);
3829 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
3830 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
3831 mi_string, mi_type);
3832}