blob: 2b8e4c1df55f7aa403d85566f32fca897043fe1d [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte2483f1b2016-06-19 18:06:02 +02004/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Neels Hofmeyr90843962017-09-04 15:04:35 +020037#include <osmocom/msc/db.h>
38#include <osmocom/msc/debug.h>
39#include <osmocom/msc/gsm_data.h>
40#include <osmocom/msc/gsm_subscriber.h>
41#include <osmocom/msc/gsm_04_11.h>
42#include <osmocom/msc/gsm_04_08.h>
43#include <osmocom/msc/gsm_04_80.h>
44#include <osmocom/msc/gsm_04_14.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020045#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/transaction.h>
47#include <osmocom/msc/ussd.h>
48#include <osmocom/msc/silent_call.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020049#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020050#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010052#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020053#include <osmocom/msc/vlr.h>
54#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080055
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/gsm/gsm48.h>
57#include <osmocom/gsm/gsm0480.h>
58#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020059#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/msgb.h>
61#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010062#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010063#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010064#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020065#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020066#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020067#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020068#endif
Harald Welte52b1f982008-12-23 20:25:15 +000069
Neels Hofmeyr90843962017-09-04 15:04:35 +020070#include <osmocom/msc/msc_ifaces.h>
71#include <osmocom/msc/a_iface.h>
Philipp Maier621ba032017-11-07 17:19:25 +010072#include <osmocom/msc/msc_mgcp.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020073
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020074#include <assert.h>
75
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020076
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020077void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010078void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020079
Harald Welte2483f1b2016-06-19 18:06:02 +020080static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
81 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080082static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020083 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000084
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080086
Neels Hofmeyr9d744252018-03-22 16:09:50 +010087static bool classmark_is_r99(struct gsm_classmark *cm)
88{
89 int rev_lev = 0;
90 if (cm->classmark1_set)
91 rev_lev = cm->classmark1.rev_lev;
92 else if (cm->classmark2_len > 0)
93 rev_lev = (cm->classmark2[0] >> 5) & 0x3;
94 return rev_lev >= 2;
95}
96
Harald Welte71330722017-12-23 19:59:02 +010097/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher */
98static bool classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
99{
100 switch (a5) {
101 case 0:
102 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
103 return true;
104 case 1:
105 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
106 * "The network shall not provide service to an MS which indicates that it does not
107 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
108 * on policy here */
109 /* See 3GPP TS 24.008 10.5.1.7 */
110 if (!cm->classmark1_set) {
111 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
112 return true;
113 } else {
114 if (cm->classmark1.a5_1)
115 return false; /* Inverted logic for this bit! */
116 else
117 return true;
118 }
119 break;
120 case 2:
121 case 3:
122 /* See 3GPP TS 24.008 10.5.1.6 */
123 if (cm->classmark2_len < 3) {
124 DEBUGP(DMSC, "CLASSMARK 2 unknown, assuming MS doesn't support A5/%u\n", a5);
125 return false;
126 } else {
127 if (cm->classmark2[2] & (1 << (a5-2)))
128 return true;
129 else
130 return false;
131 }
132 break;
133 case 4:
134 case 5:
135 case 6:
136 case 7:
137 /* See 3GPP TS 24.008 10.5.1.7 */
138 if (cm->classmark3_len < 1) {
139 DEBUGP(DMSC, "CLASSMARK 3 unknown, assuming MS doesn't support A5/%u\n", a5);
140 return false;
141 } else {
142 if (cm->classmark3[0] & (1 << (a5-4)))
143 return true;
144 else
145 return false;
146 }
147 break;
148 default:
149 return false;
150 }
151}
152
Harald Welte31c00f72011-03-03 23:29:05 +0100153void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
154{
155 net->mncc_recv(net, msg);
156}
157
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800158static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
159 struct gsm_trans *trans)
160{
161 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
162
163 /* if we get passed a transaction reference, do some common
164 * work that the caller no longer has to do */
165 if (trans) {
166 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
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;
Harald Weltec05677b2009-06-26 20:17:06 +02001459
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001460 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001461
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001462 switch (event) {
1463 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001464 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1465 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001466 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001467 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001468 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001469 /* send SETUP request to called party */
1470 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1471 break;
1472 case GSM_PAGING_EXPIRED:
1473 case GSM_PAGING_BUSY:
1474 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001475 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001476 /* Temporarily out of order */
1477 mncc_release_ind(transt->net, transt,
1478 transt->callref,
1479 GSM48_CAUSE_LOC_PRN_S_LU,
1480 GSM48_CC_CAUSE_DEST_OOO);
1481 transt->callref = 0;
1482 transt->paging_request = NULL;
1483 trans_free(transt);
1484 break;
1485 default:
1486 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1487 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001488 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001489
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001490 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001491 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001492}
Harald Welte7584aea2009-02-11 11:44:12 +00001493
Harald Welte4bfdfe72009-06-10 23:11:52 +08001494/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001495static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001496{
Harald Welte53d51f52015-12-03 14:59:04 +01001497 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1498 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Philipp Maier621ba032017-11-07 17:19:25 +01001499 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001500
Harald Welte4bfdfe72009-06-10 23:11:52 +08001501 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001502 return -EIO;
1503
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001504 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001505 return -EIO;
1506
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001507 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001508 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001509
Philipp Maier621ba032017-11-07 17:19:25 +01001510 /* Bridge RTP streams */
1511 rc = msc_mgcp_call_complete(trans1, trans2->conn->rtp.local_port_cn,
1512 trans2->conn->rtp.local_addr_cn);
1513 if (rc)
1514 return -EINVAL;
1515
1516 rc = msc_mgcp_call_complete(trans2, trans1->conn->rtp.local_port_cn,
1517 trans1->conn->rtp.local_addr_cn);
1518 if (rc)
1519 return -EINVAL;
1520
1521 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001522}
1523
Harald Welte4bfdfe72009-06-10 23:11:52 +08001524static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1525{
1526 DEBUGP(DCC, "-> STATUS ENQ\n");
1527 return gsm48_cc_tx_status(trans, msg);
1528}
1529
1530static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1531static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1532
1533static void gsm48_cc_timeout(void *arg)
1534{
1535 struct gsm_trans *trans = arg;
1536 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001537 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1538 int mo_location = GSM48_CAUSE_LOC_USER;
1539 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1540 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001541 struct gsm_mncc mo_rel, l4_rel;
1542
1543 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1544 mo_rel.callref = trans->callref;
1545 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1546 l4_rel.callref = trans->callref;
1547
Harald Weltedcaf5652009-07-23 18:56:43 +02001548 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001549 case 0x303:
1550 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001551 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001552 break;
1553 case 0x310:
1554 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001555 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001556 break;
1557 case 0x313:
1558 disconnect = 1;
1559 /* unknown, did not find it in the specs */
1560 break;
1561 case 0x301:
1562 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001563 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001564 break;
1565 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001566 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001567 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001568 gsm48_cc_tx_release(trans, &trans->cc.msg);
1569 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001570 break; /* stay in release state */
1571 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001572 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573 return;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 case 0x306:
1575 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001576 mo_cause = trans->cc.msg.cause.value;
1577 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001578 break;
1579 case 0x323:
1580 disconnect = 1;
1581 break;
1582 default:
1583 release = 1;
1584 }
1585
1586 if (release && trans->callref) {
1587 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001588 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001589 l4_location, l4_cause);
1590 trans->callref = 0;
1591 }
1592
1593 if (disconnect && trans->callref) {
1594 /* process disconnect towards layer 4 */
1595 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001596 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001597 }
1598
1599 /* process disconnect towards mobile station */
1600 if (disconnect || release) {
1601 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001602 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1603 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1604 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001605 mo_rel.cause.diag_len = 3;
1606
1607 if (disconnect)
1608 gsm48_cc_tx_disconnect(trans, &mo_rel);
1609 if (release)
1610 gsm48_cc_tx_release(trans, &mo_rel);
1611 }
1612
1613}
1614
Max3ffce192016-04-25 15:22:00 +02001615/* disconnect both calls from the bridge */
1616static inline void disconnect_bridge(struct gsm_network *net,
1617 struct gsm_mncc_bridge *bridge, int err)
1618{
1619 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1620 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1621 struct gsm_mncc mx_rel;
1622 if (!trans0 || !trans1)
1623 return;
1624
1625 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1626 trans0->callref, trans1->callref, strerror(err));
1627
1628 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1629 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1630 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1631
1632 mx_rel.callref = trans0->callref;
1633 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1634
1635 mx_rel.callref = trans1->callref;
1636 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1637}
1638
Harald Welte4bfdfe72009-06-10 23:11:52 +08001639static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1640 int sec, int micro)
1641{
1642 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001643 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001644 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001645 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001646}
1647
1648static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1649{
1650 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001651 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1653 struct tlv_parsed tp;
1654 struct gsm_mncc setup;
1655
1656 memset(&setup, 0, sizeof(struct gsm_mncc));
1657 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001658
Harald Welte474d19f2010-03-02 23:18:30 +01001659 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001660 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001661 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1662 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663 setup.emergency = 1;
Harald Welte69c54a82018-02-09 20:41:14 +01001664 /* use destination number as configured by user (if any) */
1665 if (trans->net->emergency.route_to_msisdn) {
1666 setup.fields |= MNCC_F_CALLED;
1667 setup.called.type = 0; /* unknown */
1668 setup.called.plan = 0; /* unknown */
1669 OSMO_STRLCPY_ARRAY(setup.called.number,
1670 trans->net->emergency.route_to_msisdn);
1671 }
Harald Welte0dd01372018-01-24 11:06:19 +01001672 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673
1674 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001675 setup.fields |= MNCC_F_CALLING;
Max98f74672018-02-05 12:57:06 +01001676 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
1677 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001678
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679 /* bearer capability */
1680 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1681 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001682 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001684
1685 /* Create a copy of the bearer capability
1686 * in the transaction struct, so we can use
1687 * this information later */
1688 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1689 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001690 }
1691 /* facility */
1692 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1693 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001694 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001695 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1696 }
1697 /* called party bcd number */
1698 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1699 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001700 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001701 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1702 }
1703 /* user-user */
1704 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1705 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001706 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1708 }
1709 /* ss-version */
1710 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1711 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001712 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001713 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1714 }
1715 /* CLIR suppression */
1716 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1717 setup.clir.sup = 1;
1718 /* CLIR invocation */
1719 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1720 setup.clir.inv = 1;
1721 /* cc cap */
1722 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1723 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001724 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1726 }
1727
Harald Welte4bfdfe72009-06-10 23:11:52 +08001728 new_cc_state(trans, GSM_CSTATE_INITIATED);
1729
Harald Welte79826e12018-01-24 10:50:24 +01001730 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1731 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001732 setup.called.number);
1733
Alexander Couzensb847a212016-08-02 11:34:11 +02001734 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001735
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001737 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001738
Harald Welte13cac662009-07-29 12:10:35 +02001739 /* MNCC code will modify the channel asynchronously, we should
1740 * ipaccess-bind only after the modification has been made to the
1741 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 return 0;
1743}
1744
1745static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001746{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001747 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001748 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001750 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001751
Harald Welte7ccf7782009-02-17 01:43:01 +00001752 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001753
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 /* transaction id must not be assigned */
1755 if (trans->transaction_id != 0xff) { /* unasssigned */
1756 DEBUGP(DCC, "TX Setup with assigned transaction. "
1757 "This is not allowed!\n");
1758 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001759 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001760 GSM48_CAUSE_LOC_PRN_S_LU,
1761 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001763 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 return rc;
1765 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001766
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001768 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001769 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001770 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001772 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001773 GSM48_CAUSE_LOC_PRN_S_LU,
1774 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001776 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 return rc;
1778 }
Harald Welte78283ef2009-07-23 21:36:44 +02001779 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001780
Harald Welte65e74cc2008-12-29 01:55:35 +00001781 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001782
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001784
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 /* bearer capability */
Harald Welte32958872018-02-12 11:56:51 +01001786 if (setup->fields & MNCC_F_BEARER_CAP) {
1787 /* Create a copy of the bearer capability in the transaction struct, so we
1788 * can use this information later */
1789 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte55c8f352010-03-07 23:40:35 +01001790 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte32958872018-02-12 11:56:51 +01001791 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 /* facility */
1793 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001794 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 /* progress */
1796 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001797 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 /* calling party BCD number */
1799 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001800 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 /* called party BCD number */
1802 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001803 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001804 /* user-user */
1805 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001806 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 /* redirecting party BCD number */
1808 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001809 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 /* signal */
1811 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001812 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001813
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001815
Alexander Couzensb847a212016-08-02 11:34:11 +02001816 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001817
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001818 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001819}
1820
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1822{
1823 struct gsm48_hdr *gh = msgb_l3(msg);
1824 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1825 struct tlv_parsed tp;
1826 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001827 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001828
1829 gsm48_stop_cc_timer(trans);
1830 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1831
1832 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1833 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001834
Harald Welte474d19f2010-03-02 23:18:30 +01001835 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836#if 0
1837 /* repeat */
1838 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1839 call_conf.repeat = 1;
1840 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1841 call_conf.repeat = 2;
1842#endif
1843 /* bearer capability */
1844 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1845 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001846 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001847 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001848
1849 /* Create a copy of the bearer capability
1850 * in the transaction struct, so we can use
1851 * this information later */
1852 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1853 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 }
1855 /* cause */
1856 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1857 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001858 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1860 }
1861 /* cc cap */
1862 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1863 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001864 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1866 }
1867
Andreas Eversberg035b8742013-09-19 09:27:06 +02001868 /* IMSI of called subscriber */
Max98f74672018-02-05 12:57:06 +01001869 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
Andreas Eversberg035b8742013-09-19 09:27:06 +02001870
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1872
Philipp Maierfbf66102017-04-09 12:32:51 +02001873 /* Assign call (if not done yet) */
1874 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001875 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001876 trans->assignment_done = true;
1877 }
1878 else
1879 rc = 0;
1880
1881 /* don't continue, if there were problems with
1882 * the call assignment. */
1883 if (rc)
1884 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001885
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001886 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001887 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888}
1889
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001890static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891{
1892 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001893 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001895 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1898
1899 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1900
1901 /* bearer capability */
Harald Welte4804c552018-02-12 12:30:54 +01001902 if (proceeding->fields & MNCC_F_BEARER_CAP) {
Harald Welte55c8f352010-03-07 23:40:35 +01001903 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01001904 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1905 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001906 /* facility */
1907 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001908 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001909 /* progress */
1910 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001911 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001912
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001913 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1914 if (rc)
1915 return rc;
1916
Philipp Maierfbf66102017-04-09 12:32:51 +02001917 /* Assign call (if not done yet) */
1918 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001919 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001920 trans->assignment_done = true;
1921 }
1922 else
1923 rc = 0;
1924
1925 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001926}
1927
1928static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1929{
1930 struct gsm48_hdr *gh = msgb_l3(msg);
1931 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1932 struct tlv_parsed tp;
1933 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001934
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 gsm48_stop_cc_timer(trans);
1936 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1937
1938 memset(&alerting, 0, sizeof(struct gsm_mncc));
1939 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001940 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941 /* facility */
1942 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1943 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001944 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1946 }
1947
1948 /* progress */
1949 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1950 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001951 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1953 }
1954 /* ss-version */
1955 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1956 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001957 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1959 }
1960
1961 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1962
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001963 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001964 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965}
1966
1967static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1968{
1969 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001970 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1972
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 gh->msg_type = GSM48_MT_CC_ALERTING;
1974
1975 /* facility */
1976 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001977 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978 /* progress */
1979 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001980 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981 /* user-user */
1982 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001983 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984
1985 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001986
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001987 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988}
1989
1990static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1991{
1992 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001993 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1995
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996 gh->msg_type = GSM48_MT_CC_PROGRESS;
1997
1998 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001999 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 /* user-user */
2001 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002002 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002004 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005}
2006
2007static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2008{
2009 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002010 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2012
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 gh->msg_type = GSM48_MT_CC_CONNECT;
2014
2015 gsm48_stop_cc_timer(trans);
2016 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2017
2018 /* facility */
2019 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002020 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 /* progress */
2022 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002023 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 /* connected number */
2025 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002026 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 /* user-user */
2028 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002029 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030
2031 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2032
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002033 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034}
2035
2036static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2037{
2038 struct gsm48_hdr *gh = msgb_l3(msg);
2039 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2040 struct tlv_parsed tp;
2041 struct gsm_mncc connect;
2042
2043 gsm48_stop_cc_timer(trans);
2044
2045 memset(&connect, 0, sizeof(struct gsm_mncc));
2046 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002047 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002049 connect.fields |= MNCC_F_CONNECTED;
Max98f74672018-02-05 12:57:06 +01002050 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
2051 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002052
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 /* facility */
2054 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2055 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002056 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2058 }
2059 /* user-user */
2060 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2061 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002062 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2064 }
2065 /* ss-version */
2066 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2067 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002068 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2070 }
2071
2072 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002073 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002075 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076}
2077
2078
2079static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2080{
2081 struct gsm_mncc connect_ack;
2082
2083 gsm48_stop_cc_timer(trans);
2084
2085 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002086 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002087
Harald Welte4bfdfe72009-06-10 23:11:52 +08002088 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2089 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002090
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002091 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092 &connect_ack);
2093}
2094
2095static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2096{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002097 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2099
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2101
2102 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2103
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002104 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105}
2106
2107static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2108{
2109 struct gsm48_hdr *gh = msgb_l3(msg);
2110 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2111 struct tlv_parsed tp;
2112 struct gsm_mncc disc;
2113
2114 gsm48_stop_cc_timer(trans);
2115
2116 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2117
2118 memset(&disc, 0, sizeof(struct gsm_mncc));
2119 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002120 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121 /* cause */
2122 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2123 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002124 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002125 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2126 }
2127 /* facility */
2128 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2129 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002130 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2132 }
2133 /* user-user */
2134 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2135 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002136 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2138 }
2139 /* ss-version */
2140 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2141 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002142 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002143 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2144 }
2145
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002146 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147
2148}
2149
Harald Weltec66b71c2009-06-11 14:23:20 +08002150static struct gsm_mncc_cause default_cause = {
2151 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2152 .coding = 0,
2153 .rec = 0,
2154 .rec_val = 0,
2155 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2156 .diag_len = 0,
2157 .diag = { 0 },
2158};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159
2160static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2161{
2162 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002163 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2165
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2167
2168 gsm48_stop_cc_timer(trans);
2169 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2170
2171 /* cause */
2172 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002173 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174 else
Harald Welte55c8f352010-03-07 23:40:35 +01002175 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176
2177 /* facility */
2178 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002179 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002180 /* progress */
2181 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002182 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 /* user-user */
2184 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002185 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186
2187 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002188 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189
2190 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2191
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002192 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193}
2194
2195static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2196{
2197 struct gsm48_hdr *gh = msgb_l3(msg);
2198 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2199 struct tlv_parsed tp;
2200 struct gsm_mncc rel;
2201 int rc;
2202
2203 gsm48_stop_cc_timer(trans);
2204
2205 memset(&rel, 0, sizeof(struct gsm_mncc));
2206 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002207 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208 /* cause */
2209 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2210 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002211 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002212 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2213 }
2214 /* facility */
2215 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2216 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002217 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002218 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2219 }
2220 /* user-user */
2221 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2222 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002223 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2225 }
2226 /* ss-version */
2227 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2228 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002229 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2231 }
2232
Harald Weltedcaf5652009-07-23 18:56:43 +02002233 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002235 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002237 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002238 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002239 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002240 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 }
2242
2243 new_cc_state(trans, GSM_CSTATE_NULL);
2244
2245 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002246 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247
2248 return rc;
2249}
2250
2251static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2252{
2253 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002254 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2256
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 gh->msg_type = GSM48_MT_CC_RELEASE;
2258
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 gsm48_stop_cc_timer(trans);
2260 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2261
2262 /* cause */
2263 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002264 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 /* facility */
2266 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002267 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268 /* user-user */
2269 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002270 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271
Harald Weltedcaf5652009-07-23 18:56:43 +02002272 trans->cc.T308_second = 0;
2273 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274
Harald Weltedcaf5652009-07-23 18:56:43 +02002275 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2277
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002278 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279}
2280
2281static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2282{
2283 struct gsm48_hdr *gh = msgb_l3(msg);
2284 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2285 struct tlv_parsed tp;
2286 struct gsm_mncc rel;
2287 int rc = 0;
2288
2289 gsm48_stop_cc_timer(trans);
2290
2291 memset(&rel, 0, sizeof(struct gsm_mncc));
2292 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002293 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 /* cause */
2295 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2296 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002297 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2299 }
2300 /* facility */
2301 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2302 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002303 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2305 }
2306 /* user-user */
2307 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2308 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002309 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2311 }
2312 /* ss-version */
2313 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2314 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002315 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2317 }
2318
2319 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002320 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002322 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323 MNCC_REJ_IND, &rel);
2324 break;
2325 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002326 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 MNCC_REL_CNF, &rel);
2328 break;
2329 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002330 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 MNCC_REL_IND, &rel);
2332 }
2333 }
2334
2335 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002336 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337
2338 return rc;
2339}
2340
2341static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2342{
2343 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002344 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002346 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2349
2350 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002351
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352 gsm48_stop_cc_timer(trans);
2353
2354 /* cause */
2355 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002356 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357 /* facility */
2358 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002359 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 /* user-user */
2361 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002362 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363
Harald Weltec7782de2010-12-21 19:31:41 +01002364 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2365
Harald Weltedcaf5652009-07-23 18:56:43 +02002366 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367
Harald Weltec7782de2010-12-21 19:31:41 +01002368 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002369}
2370
2371static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2372{
2373 struct gsm48_hdr *gh = msgb_l3(msg);
2374 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2375 struct tlv_parsed tp;
2376 struct gsm_mncc fac;
2377
2378 memset(&fac, 0, sizeof(struct gsm_mncc));
2379 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002380 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 /* facility */
2382 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2383 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002384 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2386 }
2387 /* ss-version */
2388 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2389 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002390 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002391 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2392 }
2393
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002394 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395}
2396
2397static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2398{
2399 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002400 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2402
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403 gh->msg_type = GSM48_MT_CC_FACILITY;
2404
2405 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002406 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002408 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409}
2410
2411static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2412{
2413 struct gsm_mncc hold;
2414
2415 memset(&hold, 0, sizeof(struct gsm_mncc));
2416 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002417 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418}
2419
2420static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2421{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002422 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2424
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2426
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002427 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428}
2429
2430static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2431{
2432 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002433 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2435
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2437
2438 /* cause */
2439 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002440 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 else
Harald Welte55c8f352010-03-07 23:40:35 +01002442 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002444 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445}
2446
2447static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2448{
2449 struct gsm_mncc retrieve;
2450
2451 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2452 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002453 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002454 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455}
2456
2457static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2458{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002459 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2461
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2463
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002464 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002465}
2466
2467static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2468{
2469 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002470 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2472
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2474
2475 /* cause */
2476 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002477 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478 else
Harald Welte55c8f352010-03-07 23:40:35 +01002479 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002481 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482}
2483
2484static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2485{
2486 struct gsm48_hdr *gh = msgb_l3(msg);
2487 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2488 struct tlv_parsed tp;
2489 struct gsm_mncc dtmf;
2490
2491 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2492 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002493 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494 /* keypad facility */
2495 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2496 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002497 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2499 }
2500
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002501 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502}
2503
2504static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2505{
2506 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002507 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2509
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2511
2512 /* keypad */
2513 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002514 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002516 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517}
2518
2519static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2520{
2521 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002522 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2524
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2526
2527 /* cause */
2528 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002529 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530 else
Harald Welte55c8f352010-03-07 23:40:35 +01002531 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002533 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534}
2535
2536static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2537{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002538 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2540
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2542
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002543 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002544}
2545
2546static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2547{
2548 struct gsm_mncc dtmf;
2549
2550 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2551 dtmf.callref = trans->callref;
2552
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002553 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554}
2555
2556static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2557{
2558 struct gsm48_hdr *gh = msgb_l3(msg);
2559 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2560 struct tlv_parsed tp;
2561 struct gsm_mncc modify;
2562
2563 memset(&modify, 0, sizeof(struct gsm_mncc));
2564 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002565 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566 /* bearer capability */
2567 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2568 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002569 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002571
2572 /* Create a copy of the bearer capability
2573 * in the transaction struct, so we can use
2574 * this information later */
2575 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2576 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577 }
2578
2579 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2580
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002581 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582}
2583
2584static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2585{
2586 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002587 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2589
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590 gh->msg_type = GSM48_MT_CC_MODIFY;
2591
2592 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2593
2594 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002595 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002596 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597
2598 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2599
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002600 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601}
2602
2603static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2604{
2605 struct gsm48_hdr *gh = msgb_l3(msg);
2606 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2607 struct tlv_parsed tp;
2608 struct gsm_mncc modify;
2609
2610 gsm48_stop_cc_timer(trans);
2611
2612 memset(&modify, 0, sizeof(struct gsm_mncc));
2613 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002614 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 /* bearer capability */
2616 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2617 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002618 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002620
2621 /* Create a copy of the bearer capability
2622 * in the transaction struct, so we can use
2623 * this information later */
2624 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2625 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626 }
2627
2628 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2629
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002630 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631}
2632
2633static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2634{
2635 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002636 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2638
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2640
2641 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002642 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002643 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644
2645 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2646
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002647 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648}
2649
2650static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2651{
2652 struct gsm48_hdr *gh = msgb_l3(msg);
2653 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2654 struct tlv_parsed tp;
2655 struct gsm_mncc modify;
2656
2657 gsm48_stop_cc_timer(trans);
2658
2659 memset(&modify, 0, sizeof(struct gsm_mncc));
2660 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002661 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662 /* bearer capability */
2663 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2664 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002665 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002667
2668 /* Create a copy of the bearer capability
2669 * in the transaction struct, so we can use
2670 * this information later */
2671 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2672 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673 }
2674 /* cause */
2675 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2676 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002677 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2679 }
2680
2681 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2682
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002683 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684}
2685
2686static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2687{
2688 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002689 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2691
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2693
2694 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002695 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002696 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002698 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002699
2700 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2701
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002702 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002703}
2704
2705static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2706{
2707 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002708 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2710
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711 gh->msg_type = GSM48_MT_CC_NOTIFY;
2712
2713 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002714 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002715
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002716 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717}
2718
2719static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2720{
2721 struct gsm48_hdr *gh = msgb_l3(msg);
2722 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2723// struct tlv_parsed tp;
2724 struct gsm_mncc notify;
2725
2726 memset(&notify, 0, sizeof(struct gsm_mncc));
2727 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002728// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002730 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002731
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002732 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002733}
2734
2735static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2736{
2737 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002738 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2740
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741 gh->msg_type = GSM48_MT_CC_USER_INFO;
2742
2743 /* user-user */
2744 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002745 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002746 /* more data */
2747 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002748 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002749
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002750 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751}
2752
2753static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2754{
2755 struct gsm48_hdr *gh = msgb_l3(msg);
2756 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2757 struct tlv_parsed tp;
2758 struct gsm_mncc user;
2759
2760 memset(&user, 0, sizeof(struct gsm_mncc));
2761 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002762 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763 /* user-user */
2764 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2765 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002766 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002767 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2768 }
2769 /* more data */
2770 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2771 user.more = 1;
2772
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002773 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774}
2775
Philipp Maierfbf66102017-04-09 12:32:51 +02002776static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2777 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2778 uint32_t payload_msg_type)
2779{
2780 uint8_t data[sizeof(struct gsm_mncc)];
2781 struct gsm_mncc_rtp *rtp;
2782
2783 memset(&data, 0, sizeof(data));
2784 rtp = (struct gsm_mncc_rtp *) &data[0];
2785
2786 rtp->callref = callref;
2787 rtp->msg_type = cmd;
2788 rtp->ip = addr;
2789 rtp->port = port;
2790 rtp->payload_type = payload_type;
2791 rtp->payload_msg_type = payload_msg_type;
2792 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2793}
2794
2795static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2796{
2797 int msg_type;
2798
2799 /* FIXME This has to be set to some meaningful value.
2800 * Possible options are:
2801 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2802 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2803 * (0 if unknown) */
2804 msg_type = GSM_TCHF_FRAME;
2805
Philipp Maier621ba032017-11-07 17:19:25 +01002806 uint32_t addr = inet_addr(trans->conn->rtp.local_addr_cn);
2807 uint16_t port = trans->conn->rtp.local_port_cn;
2808
2809 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2810
2811 if (addr == INADDR_NONE) {
2812 LOGP(DMNCC, LOGL_ERROR,
2813 "(subscriber:%s) external MNCC is signalling invalid IP-Address\n",
2814 vlr_subscr_name(trans->vsub));
2815 return;
2816 }
2817 if (port == 0) {
2818 LOGP(DMNCC, LOGL_ERROR,
2819 "(subscriber:%s) external MNCC is signalling invalid Port\n",
2820 vlr_subscr_name(trans->vsub));
2821 return;
2822 }
Philipp Maierfbf66102017-04-09 12:32:51 +02002823
2824 /* FIXME: This has to be set to some meaningful value,
2825 * before the MSC-Split, this value was pulled from
2826 * lchan->abis_ip.rtp_payload */
2827 uint32_t payload_type = 0;
2828
2829 return mncc_recv_rtp(net, trans->callref, cmd,
2830 addr,
2831 port,
2832 payload_type,
2833 msg_type);
2834}
2835
2836static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2837{
2838 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2839}
2840
2841static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2842{
2843 struct gsm_trans *trans;
2844 int rc;
2845
2846 /* Find callref */
2847 trans = trans_find_by_callref(net, callref);
2848 if (!trans) {
2849 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2850 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2851 return -EIO;
2852 }
2853 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2854 if (!trans->conn) {
2855 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2856 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2857 return 0;
2858 }
2859
2860 trans->conn->mncc_rtp_bridge = 1;
2861
Philipp Maier621ba032017-11-07 17:19:25 +01002862 /* When we call msc_mgcp_call_assignment() we will trigger, depending
Philipp Maierfbf66102017-04-09 12:32:51 +02002863 * on the RAN type the call assignment on the A or Iu interface.
Philipp Maier621ba032017-11-07 17:19:25 +01002864 * msc_mgcp_call_assignment() also takes care about sending the CRCX
Philipp Maierfbf66102017-04-09 12:32:51 +02002865 * command to the MGCP-GW. The CRCX will return the port number,
2866 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2867 * have to return this port number back to the MNCC by sending
2868 * it back with the TCH_RTP_CREATE message. To make sure that
2869 * this message is sent AFTER the response to CRCX from the
Philipp Maier621ba032017-11-07 17:19:25 +01002870 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
Philipp Maierfbf66102017-04-09 12:32:51 +02002871 * to take care of this by setting trans->tch_rtp_create to true.
2872 * This will make sure that gsm48_tch_rtp_create() (below) is
2873 * called as soon as the local port number has become known. */
2874 trans->tch_rtp_create = true;
2875
2876 /* Assign call (if not done yet) */
2877 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01002878 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02002879 trans->assignment_done = true;
2880 }
2881 else
2882 rc = 0;
2883
2884 return rc;
2885}
2886
2887/* Trigger TCH_RTP_CREATE acknowledgement */
2888int gsm48_tch_rtp_create(struct gsm_trans *trans)
2889{
2890 /* This function is called as soon as the port, on which the
2891 * mgcp-gw expects the incoming RTP stream from the remote
2892 * end (e.g. Asterisk) is known. */
2893
2894 struct gsm_subscriber_connection *conn = trans->conn;
2895 struct gsm_network *network = conn->network;
2896
2897 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2898 return 0;
2899}
2900
2901static int tch_rtp_connect(struct gsm_network *net, void *arg)
2902{
2903 struct gsm_trans *trans;
2904 struct gsm_mncc_rtp *rtp = arg;
Philipp Maier621ba032017-11-07 17:19:25 +01002905 struct in_addr addr;
Philipp Maierfbf66102017-04-09 12:32:51 +02002906
2907 /* Find callref */
2908 trans = trans_find_by_callref(net, rtp->callref);
2909 if (!trans) {
2910 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2911 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2912 return -EIO;
2913 }
2914 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2915 if (!trans->conn) {
2916 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2917 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2918 return 0;
2919 }
2920
Philipp Maier621ba032017-11-07 17:19:25 +01002921 addr.s_addr = osmo_htonl(rtp->ip);
2922 return msc_mgcp_call_complete(trans, rtp->port, inet_ntoa(addr));
Philipp Maierfbf66102017-04-09 12:32:51 +02002923}
2924
Harald Welte4bfdfe72009-06-10 23:11:52 +08002925static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002926 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927 int type;
2928 int (*rout) (struct gsm_trans *trans, void *arg);
2929} downstatelist[] = {
2930 /* mobile originating call establishment */
2931 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002932 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002933 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2934 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2935 {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 */
2936 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2937 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2938 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2939 /* mobile terminating call establishment */
2940 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2941 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2942 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2943 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2944 /* signalling during call */
2945 {SBIT(GSM_CSTATE_ACTIVE),
2946 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2947 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2948 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2949 {ALL_STATES,
2950 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2951 {ALL_STATES,
2952 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2953 {ALL_STATES,
2954 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2955 {SBIT(GSM_CSTATE_ACTIVE),
2956 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2957 {SBIT(GSM_CSTATE_ACTIVE),
2958 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2959 {SBIT(GSM_CSTATE_ACTIVE),
2960 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2961 {SBIT(GSM_CSTATE_ACTIVE),
2962 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2963 {SBIT(GSM_CSTATE_ACTIVE),
2964 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2965 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2966 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2967 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2968 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2969 {SBIT(GSM_CSTATE_ACTIVE),
2970 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2971 /* clearing */
2972 {SBIT(GSM_CSTATE_INITIATED),
2973 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2974 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2975 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2976 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2977 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002978};
2979
2980#define DOWNSLLEN \
2981 (sizeof(downstatelist) / sizeof(struct downstate))
2982
2983
Harald Welte76556372010-12-22 23:57:45 +01002984int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002985{
Harald Welte1a6f7982009-08-09 18:52:33 +02002986 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002988 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989 struct gsm_mncc *data = arg, rel;
2990
Harald Welte04dc88f2010-12-22 21:45:05 +01002991 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2992
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993 /* handle special messages */
2994 switch(msg_type) {
2995 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002996 rc = tch_bridge(net, arg);
2997 if (rc < 0)
2998 disconnect_bridge(net, arg, -rc);
2999 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02003000 case MNCC_RTP_CREATE:
3001 return tch_rtp_create(net, data->callref);
3002 case MNCC_RTP_CONNECT:
3003 return tch_rtp_connect(net, arg);
3004 case MNCC_RTP_FREE:
3005 /* unused right now */
3006 return -EIO;
3007
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08003009 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003010 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003011 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003012 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003013 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003014 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
3015 get_mncc_name(msg_type));
3016 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003017 }
3018
3019 memset(&rel, 0, sizeof(struct gsm_mncc));
3020 rel.callref = data->callref;
3021
3022 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003023 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003024
3025 /* Callref unknown */
3026 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02003027 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003028
Harald Welte4a3464c2009-07-04 10:11:24 +02003029 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003030 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3031 "Received '%s' from MNCC with "
3032 "unknown callref %d\n", data->called.number,
3033 get_mncc_name(msg_type), data->callref);
3034 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003035 return mncc_release_ind(net, NULL, data->callref,
3036 GSM48_CAUSE_LOC_PRN_S_LU,
3037 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003039 if (!data->called.number[0] && !data->imsi[0]) {
3040 DEBUGP(DCC, "(bts - trx - ts - ti) "
3041 "Received '%s' from MNCC with "
3042 "no number or IMSI\n", get_mncc_name(msg_type));
3043 /* Invalid number */
3044 return mncc_release_ind(net, NULL, data->callref,
3045 GSM48_CAUSE_LOC_PRN_S_LU,
3046 GSM48_CC_CAUSE_INV_NR_FORMAT);
3047 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003049 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02003050 vsub = vlr_subscr_find_by_msisdn(net->vlr,
3051 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003052 else
Harald Welte2483f1b2016-06-19 18:06:02 +02003053 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003054
3055 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003056 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003057
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02003059 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3061 "Received '%s' from MNCC with "
3062 "unknown subscriber %s\n", data->called.number,
3063 get_mncc_name(msg_type), data->called.number);
3064 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003065 return mncc_release_ind(net, NULL, data->callref,
3066 GSM48_CAUSE_LOC_PRN_S_LU,
3067 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 }
3069 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02003070 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003071 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3072 "Received '%s' from MNCC with "
3073 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01003074 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02003075 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003076 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003077 return mncc_release_ind(net, NULL, data->callref,
3078 GSM48_CAUSE_LOC_PRN_S_LU,
3079 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 }
3081 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003082 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003083 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003085 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003086 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003087 mncc_release_ind(net, NULL, data->callref,
3088 GSM48_CAUSE_LOC_PRN_S_LU,
3089 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003090 return -ENOMEM;
3091 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003092
3093 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02003094 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003095
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003096 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003097 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003098 /* find transaction with this subscriber already paging */
3099 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003100 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003101 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02003102 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003103 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003104 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003105 "Received '%s' from MNCC with "
3106 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003107 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003108 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003109 get_mncc_name(msg_type), vsub->lac);
3110 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003111 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003112 return 0;
3113 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003114 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02003115 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003116
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003117 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003118 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003119 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003120 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003121 trans,
3122 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003123 if (!trans->paging_request) {
3124 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003125 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003126 trans_free(trans);
3127 return 0;
3128 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003129 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 return 0;
3131 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003132
3133 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003134 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte0e2fa5d2018-04-09 16:35:01 +02003135 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Harald Welte2483f1b2016-06-19 18:06:02 +02003136 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003137 } else {
3138 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003139 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003140 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003141
3142 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003143 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144
3145 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003146 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003147 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003148 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003149 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003150 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003151 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3152 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003153 if (msg_type == MNCC_REL_REQ)
3154 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3155 else
3156 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3157 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003158 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003159 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003160 } else {
3161 DEBUGP(DCC, "(ti %02x sub %s) "
3162 "Received '%s' from MNCC in state %d (%s)\n",
3163 trans->transaction_id,
3164 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3165 get_mncc_name(msg_type), trans->cc.state,
3166 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003167 }
3168
Harald Welte4bfdfe72009-06-10 23:11:52 +08003169 /* Find function for current state and message */
3170 for (i = 0; i < DOWNSLLEN; i++)
3171 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003172 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173 break;
3174 if (i == DOWNSLLEN) {
3175 DEBUGP(DCC, "Message unhandled at this state.\n");
3176 return 0;
3177 }
3178
3179 rc = downstatelist[i].rout(trans, arg);
3180
3181 return rc;
3182}
3183
3184
3185static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003186 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003187 int type;
3188 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3189} datastatelist[] = {
3190 /* mobile originating call establishment */
3191 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3192 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3193 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3194 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3195 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3196 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3197 /* mobile terminating call establishment */
3198 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3199 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3200 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3201 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003202 {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 +08003203 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3204 /* signalling during call */
3205 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3206 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3207 {SBIT(GSM_CSTATE_ACTIVE),
3208 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3209 {ALL_STATES,
3210 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3211 {ALL_STATES,
3212 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3213 {ALL_STATES,
3214 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3215 {SBIT(GSM_CSTATE_ACTIVE),
3216 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3217 {SBIT(GSM_CSTATE_ACTIVE),
3218 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3219 {SBIT(GSM_CSTATE_ACTIVE),
3220 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3221 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3222 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3223 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3224 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3225 {SBIT(GSM_CSTATE_ACTIVE),
3226 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3227 /* clearing */
3228 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3229 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3230 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3231 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3232 {ALL_STATES, /* 5.4.3.4 */
3233 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3234};
3235
3236#define DATASLLEN \
3237 (sizeof(datastatelist) / sizeof(struct datastate))
3238
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003239static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003240{
3241 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003242 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003243 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003244 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003246
Harald Welte4bfdfe72009-06-10 23:11:52 +08003247 if (msg_type & 0x80) {
3248 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3249 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003250 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003251
Harald Welte2483f1b2016-06-19 18:06:02 +02003252 if (!conn->vsub) {
3253 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003254 return -EINVAL;
3255 }
3256
Harald Welte4bfdfe72009-06-10 23:11:52 +08003257 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003258 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003259
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003260#if BEFORE_MSCSPLIT
3261 /* Re-enable this log output once we can obtain this information via
3262 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003263 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003264 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003265 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003266 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003267 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3268 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003269#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003270
3271 /* Create transaction */
3272 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003273 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003274 "creating new trans.\n", transaction_id);
3275 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003276 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003277 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003278 transaction_id, new_callref++);
3279 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003280 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003281 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003282 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003283 GSM48_MT_CC_RELEASE_COMPL);
3284 return -ENOMEM;
3285 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003286 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003287 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte0e2fa5d2018-04-09 16:35:01 +02003288 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Harald Welte2483f1b2016-06-19 18:06:02 +02003289 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003290 }
3291
3292 /* find function for current state and message */
3293 for (i = 0; i < DATASLLEN; i++)
3294 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003295 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003296 break;
3297 if (i == DATASLLEN) {
3298 DEBUGP(DCC, "Message unhandled at this state.\n");
3299 return 0;
3300 }
3301
Harald Welte2483f1b2016-06-19 18:06:02 +02003302 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003303
Harald Welte4bfdfe72009-06-10 23:11:52 +08003304 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003305
Harald Welte2483f1b2016-06-19 18:06:02 +02003306 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003307 return rc;
3308}
3309
Harald Welte2483f1b2016-06-19 18:06:02 +02003310static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003311{
Harald Welte2483f1b2016-06-19 18:06:02 +02003312 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3313 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003314
Harald Welte2483f1b2016-06-19 18:06:02 +02003315 switch (pdisc) {
3316 case GSM48_PDISC_MM:
3317 switch (msg_type) {
3318 case GSM48_MT_MM_LOC_UPD_REQUEST:
3319 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003320 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003321 case GSM48_MT_MM_AUTH_RESP:
3322 case GSM48_MT_MM_AUTH_FAIL:
3323 case GSM48_MT_MM_ID_RESP:
3324 case GSM48_MT_MM_TMSI_REALL_COMPL:
3325 case GSM48_MT_MM_IMSI_DETACH_IND:
3326 return true;
3327 default:
3328 break;
3329 }
3330 break;
3331 case GSM48_PDISC_RR:
3332 switch (msg_type) {
3333 case GSM48_MT_RR_CIPH_M_COMPL:
3334 case GSM48_MT_RR_PAG_RESP:
3335 return true;
3336 default:
3337 break;
3338 }
3339 break;
3340 default:
3341 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003342 }
3343
Harald Welte2483f1b2016-06-19 18:06:02 +02003344 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003345}
3346
Harald Welte2483f1b2016-06-19 18:06:02 +02003347void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3348 struct msgb *msg)
3349{
3350
3351 /* If a CM Service Request was received before, this is the request the
3352 * conn was opened for. No need to wait for further messages. */
3353
3354 if (!conn->received_cm_service_request)
3355 return;
3356
3357 if (log_check_level(DMM, LOGL_DEBUG)) {
3358 struct gsm48_hdr *gh = msgb_l3(msg);
3359 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3360 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3361
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003362 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003363 " received_cm_service_request changes to false\n",
3364 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003365 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003366 }
3367 conn->received_cm_service_request = false;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003368 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
Harald Welte2483f1b2016-06-19 18:06:02 +02003369}
3370
Harald Weltec2007852018-02-03 21:08:26 +01003371/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
3372int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
3373{
3374 switch (pdisc) {
3375 case GSM48_PDISC_MM:
3376 case GSM48_PDISC_CC:
3377 case GSM48_PDISC_NC_SS:
3378 return 0;
3379 case GSM48_PDISC_GROUP_CC:
3380 return 1;
3381 case GSM48_PDISC_BCAST_CC:
3382 return 2;
3383 case GSM48_PDISC_LOC:
3384 return 3;
3385 default:
3386 return -1;
3387 }
3388}
3389
3390/* extract the N(SD) and return the modulo value for a R98 message */
3391static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3392{
3393 switch (pdisc) {
3394 case GSM48_PDISC_MM:
3395 case GSM48_PDISC_CC:
3396 case GSM48_PDISC_NC_SS:
3397 *n_sd = (msg_type >> 6) & 0x3;
3398 return 4;
3399 case GSM48_PDISC_GROUP_CC:
3400 case GSM48_PDISC_BCAST_CC:
3401 case GSM48_PDISC_LOC:
3402 *n_sd = (msg_type >> 6) & 0x1;
3403 return 2;
3404 default:
3405 /* no sequence number, we cannot detect dups */
3406 return 0;
3407 }
3408}
3409
3410/* extract the N(SD) and return the modulo value for a R99 message */
3411static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3412{
3413 switch (pdisc) {
3414 case GSM48_PDISC_MM:
3415 case GSM48_PDISC_CC:
3416 case GSM48_PDISC_NC_SS:
3417 case GSM48_PDISC_GROUP_CC:
3418 case GSM48_PDISC_BCAST_CC:
3419 case GSM48_PDISC_LOC:
3420 *n_sd = (msg_type >> 6) & 0x1;
3421 return 2;
3422 default:
3423 /* no sequence number, we cannot detect dups */
3424 return 0;
3425 }
3426}
3427
3428/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
3429static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
3430{
3431 struct gsm48_hdr *gh;
3432 uint8_t pdisc;
3433 uint8_t n_sd, modulo, bin;
3434
3435 gh = msgb_l3(msg);
3436 pdisc = gsm48_hdr_pdisc(gh);
3437
3438 if (conn->classmark.classmark1_set && conn->classmark.classmark1.rev_lev < 2) {
3439 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
3440 } else { /* R99 */
3441 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
3442 }
3443 if (modulo == 0)
3444 return false;
3445 bin = gsm0407_pdisc_ctr_bin(pdisc);
3446 if (bin < 0)
3447 return false;
3448
3449 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
3450 if (n_sd != conn->n_sd_next[bin]) {
3451 /* not what we expected: duplicate */
3452 return true;
3453 } else {
3454 /* as expected: no dup; update expected counter for next message */
3455 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
3456 return false;
3457 }
3458}
Harald Welte2483f1b2016-06-19 18:06:02 +02003459
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003460/* 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 +08003461int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003462{
Philipp Maier91f10c72017-04-20 18:40:37 +02003463 struct gsm48_hdr *gh;
3464 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003465 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003466
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003467 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003468 OSMO_ASSERT(conn);
3469 OSMO_ASSERT(msg);
3470
Philipp Maier91f10c72017-04-20 18:40:37 +02003471 gh = msgb_l3(msg);
3472 pdisc = gsm48_hdr_pdisc(gh);
3473
Harald Weltec2007852018-02-03 21:08:26 +01003474 if (gsm0407_is_duplicate(conn, msg)) {
3475 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
3476 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
3477 return 0;
3478 }
3479
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003480 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3481 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3482 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003483
3484 if (!msc_subscr_conn_is_accepted(conn)
3485 && !msg_is_initially_permitted(gh)) {
3486 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003487 "subscr %s: Message not permitted for initial conn: %s\n",
3488 vlr_subscr_name(conn->vsub),
3489 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003490 return -EACCES;
3491 }
3492
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003493 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3494 LOGP(DMM, LOGL_ERROR,
3495 "%s: Illegal situation: RAN type mismatch:"
3496 " attached via %s, received message via %s\n",
3497 vlr_subscr_name(conn->vsub),
3498 ran_type_name(conn->vsub->cs.attached_via_ran),
3499 ran_type_name(conn->via_ran));
3500 return -EACCES;
3501 }
3502
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003503#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003504 if (silent_call_reroute(conn, msg))
3505 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003506#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003507
Harald Welte52b1f982008-12-23 20:25:15 +00003508 switch (pdisc) {
3509 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003510 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003511 break;
3512 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003513 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003514 break;
3515 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003516 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003517 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003518 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003519 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003520 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003521 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003522 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003523 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3524 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003525 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003526 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003527 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003528 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003529 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003530 case GSM48_PDISC_TEST:
3531 rc = gsm0414_rcv_test(conn, msg);
3532 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003533 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003534 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3535 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003536 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003537 break;
3538 }
3539
3540 return rc;
3541}
Harald Welte8470bf22008-12-25 23:28:35 +00003542
Harald Welte2483f1b2016-06-19 18:06:02 +02003543/***********************************************************************
3544 * VLR integration
3545 ***********************************************************************/
3546
3547/* VLR asks us to send an authentication request */
3548static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3549 bool send_autn)
3550{
3551 struct gsm_subscriber_connection *conn = msc_conn_ref;
3552 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3553 send_autn? at->vec.autn : NULL,
3554 at->key_seq);
3555}
3556
3557/* VLR asks us to send an authentication reject */
3558static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3559{
3560 struct gsm_subscriber_connection *conn = msc_conn_ref;
3561 return gsm48_tx_mm_auth_rej(conn);
3562}
3563
3564/* VLR asks us to transmit an Identity Request of given type */
3565static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3566{
3567 struct gsm_subscriber_connection *conn = msc_conn_ref;
3568 return mm_tx_identity_req(conn, mi_type);
3569}
3570
3571/* VLR asks us to transmit a Location Update Accept */
3572static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3573{
3574 struct gsm_subscriber_connection *conn = msc_conn_ref;
3575 return gsm0408_loc_upd_acc(conn, send_tmsi);
3576}
3577
3578/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02003579static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02003580{
3581 struct gsm_subscriber_connection *conn = msc_conn_ref;
3582 return gsm0408_loc_upd_rej(conn, cause);
3583}
3584
3585/* VLR asks us to transmit a CM Service Accept */
3586static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3587{
3588 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003589 return msc_gsm48_tx_mm_serv_ack(conn);
3590}
3591
3592static int msc_vlr_tx_common_id(void *msc_conn_ref)
3593{
3594 struct gsm_subscriber_connection *conn = msc_conn_ref;
3595 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003596}
3597
Stefan Sperling3a741282018-03-13 21:11:49 +01003598/* VLR asks us to transmit MM info. */
3599static int msc_vlr_tx_mm_info(void *msc_conn_ref)
3600{
3601 struct gsm_subscriber_connection *conn = msc_conn_ref;
3602 if (!conn->network->send_mm_info)
3603 return 0;
3604 return gsm48_tx_mm_info(conn);
3605}
3606
Harald Welte2483f1b2016-06-19 18:06:02 +02003607/* VLR asks us to transmit a CM Service Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02003608static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02003609{
Harald Welte2483f1b2016-06-19 18:06:02 +02003610 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003611 int rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02003612
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003613 rc = msc_gsm48_tx_mm_serv_rej(conn, cause);
3614
3615 if (conn->received_cm_service_request) {
3616 conn->received_cm_service_request = false;
3617 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
3618 }
3619
3620 return rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02003621}
3622
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003623/* For msc_vlr_set_ciph_mode() */
3624osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3625 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3626
Neels Hofmeyrda21a522018-03-02 01:50:09 +01003627/* VLR asks us to start using ciphering.
3628 * (Keep non-static to allow regression testing on this function.) */
3629int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3630 bool umts_aka,
3631 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02003632{
3633 struct gsm_subscriber_connection *conn = msc_conn_ref;
3634 struct vlr_subscr *vsub;
3635 struct gsm_auth_tuple *tuple;
3636
3637 if (!conn || !conn->vsub) {
3638 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3639 " NULL conn/subscriber");
3640 return -EINVAL;
3641 }
3642
3643 vsub = conn->vsub;
3644 tuple = vsub->last_tuple;
3645
3646 if (!tuple) {
3647 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3648 " Command: no auth tuple available\n",
3649 vlr_subscr_name(vsub));
3650 return -EINVAL;
3651 }
3652
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003653 switch (conn->via_ran) {
3654 case RAN_GERAN_A:
3655 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3656 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003657 {
Harald Welte7b222aa2017-12-23 19:30:32 +01003658 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003659 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01003660 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003661
Harald Welte7b222aa2017-12-23 19:30:32 +01003662 for (i = 0; i < 8; i++) {
Harald Welte71330722017-12-23 19:59:02 +01003663 if (net->a5_encryption_mask & (1 << i) &&
3664 classmark_supports_a5(&conn->classmark, i))
Harald Welte7b222aa2017-12-23 19:30:32 +01003665 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
3666 }
3667 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003668
Harald Welte71330722017-12-23 19:59:02 +01003669 if (ei.perm_algo_len == 0) {
3670 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
3671 "between MSC-configured and MS-supported A5 algorithms\n",
3672 vlr_subscr_name(conn->vsub));
3673 return -ENOTSUP;
3674 }
3675
Neels Hofmeyr52821712017-12-18 01:23:42 +01003676 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3677 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3678 * necessarily a match for the UMTS AKA tokens. */
3679 if (umts_aka)
3680 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3681 else
3682 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003683 ei.key_len = sizeof(tuple->vec.kc);
3684
3685 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3686 }
3687
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003688 case RAN_UTRAN_IU:
3689#ifdef BUILD_IU
3690 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3691 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003692 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003693#else
3694 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3695 " built without Iu support\n");
3696 return -ENOTSUP;
3697#endif
3698
3699 default:
3700 break;
3701 }
3702 LOGP(DMM, LOGL_ERROR,
3703 "%s: cannot start ciphering, unknown RAN type %d\n",
3704 vlr_subscr_name(conn->vsub), conn->via_ran);
3705 return -ENOTSUP;
3706}
3707
3708void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3709{
3710 struct vlr_ciph_result vlr_res = {};
3711
3712 if (!conn || !conn->vsub) {
3713 LOGP(DMM, LOGL_ERROR,
3714 "Rx Security Mode Complete for invalid conn\n");
3715 return;
3716 }
3717
3718 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3719 vlr_subscr_name(conn->vsub));
3720
3721 vlr_res.cause = VLR_CIPH_COMPL;
3722 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003723}
3724
3725/* VLR informs us that the subscriber data has somehow been modified */
3726static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3727{
Maxa263bb22017-12-27 13:23:44 +01003728 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3729 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003730}
3731
3732/* VLR informs us that the subscriber has been associated with a conn */
3733static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3734 struct vlr_subscr *vsub)
3735{
3736 struct gsm_subscriber_connection *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01003737 OSMO_ASSERT(vsub);
Harald Welte2483f1b2016-06-19 18:06:02 +02003738 OSMO_ASSERT(!conn->vsub);
3739 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01003740 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003741 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003742}
3743
3744/* operations that we need to implement for libvlr */
3745static const struct vlr_ops msc_vlr_ops = {
3746 .tx_auth_req = msc_vlr_tx_auth_req,
3747 .tx_auth_rej = msc_vlr_tx_auth_rej,
3748 .tx_id_req = msc_vlr_tx_id_req,
3749 .tx_lu_acc = msc_vlr_tx_lu_acc,
3750 .tx_lu_rej = msc_vlr_tx_lu_rej,
3751 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3752 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3753 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003754 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01003755 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02003756 .subscr_update = msc_vlr_subscr_update,
3757 .subscr_assoc = msc_vlr_subscr_assoc,
3758};
3759
3760/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3761int msc_vlr_alloc(struct gsm_network *net)
3762{
3763 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3764 if (!net->vlr)
3765 return -ENOMEM;
3766 net->vlr->user_ctx = net;
3767 return 0;
3768}
3769
3770/* Launch the VLR, i.e. its GSUP connection */
3771int msc_vlr_start(struct gsm_network *net)
3772{
3773 OSMO_ASSERT(net->vlr);
3774 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3775 net->gsup_server_port);
3776}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01003777
3778struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
3779{
3780 struct msgb *msg;
3781 struct gsm48_hdr *gh;
3782
3783 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
3784 if (!msg)
3785 return NULL;
3786
3787 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3788 gh->proto_discr = GSM48_PDISC_MM;
3789 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
3790 gh->data[0] = value;
3791
3792 return msg;
3793}
3794
3795struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
3796{
3797 struct gsm48_hdr *gh;
3798 struct msgb *msg;
3799
3800 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
3801 if (!msg)
3802 return NULL;
3803
3804 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3805 gh->proto_discr = GSM48_PDISC_MM;
3806 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
3807 gh->data[0] = cause;
3808 return msg;
3809}
3810
3811int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
3812{
3813 /* Check the size for the classmark */
3814 if (length < 1 + *classmark2_lv)
3815 return -1;
3816
3817 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
3818 if (length < 2 + *classmark2_lv + mi_lv[0])
3819 return -2;
3820
3821 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
3822 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
3823}
3824
3825int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
3826 char *mi_string, uint8_t *mi_type)
3827{
3828 static const uint32_t classmark_offset =
3829 offsetof(struct gsm48_pag_resp, classmark2);
3830 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
3831 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
3832 mi_string, mi_type);
3833}