blob: 435f0dc28121809430b85d65d8f9b6ed77c36826 [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 Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 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>
28#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000029#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000030#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Harald Welte9c3dc902012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut30a15382009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000035#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000044#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000045#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020046#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000047#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020055#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080057
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010058#include <osmocom/gsm/gsm48.h>
59#include <osmocom/gsm/gsm0480.h>
60#include <osmocom/gsm/gsm_utils.h>
61#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
63#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000064
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020065#include <assert.h>
66
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020067void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010068void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020069
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +020070static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080071static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020072 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010073static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080074static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000075
Harald Welte52b1f982008-12-23 20:25:15 +000076struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020077 uint16_t mcc;
78 uint16_t mnc;
79 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000080};
81
Andreas Eversberga83d5112013-12-07 18:32:28 +010082static int apply_codec_restrictions(struct gsm_bts *bts,
83 struct gsm_mncc_bearer_cap *bcap)
84{
85 int i, j;
86
87 /* remove unsupported speech versions from list */
88 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
89 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
90 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
91 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
92 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
93 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
94 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
95 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
96 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
99 }
100 bcap->speech_ver[j] = -1;
101
102 return 0;
103}
104
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200105static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800106
Harald Welte31c00f72011-03-03 23:29:05 +0100107void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
108{
109 net->mncc_recv(net, msg);
110}
111
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800112static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
113 struct gsm_trans *trans)
114{
115 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
116
117 /* if we get passed a transaction reference, do some common
118 * work that the caller no longer has to do */
119 if (trans) {
120 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
121 msg->lchan = trans->conn->lchan;
122 }
123
124
125 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200126 struct e1inp_sign_link *sign_link =
127 msg->lchan->ts->trx->rsl_link;
128
129 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800130 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
131 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200132 "Sending '%s' to MS.\n",
133 sign_link->trx->bts->nr,
134 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800135 gh->proto_discr & 0xf0,
136 gsm48_cc_msg_name(gh->msg_type));
137 else
138 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200139 "Sending 0x%02x to MS.\n",
140 sign_link->trx->bts->nr,
141 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800142 gh->proto_discr, gh->msg_type);
143 }
144
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100145 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800146}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100147
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800148int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
149{
150 struct gsm48_hdr *gh;
151 struct msgb *ss_notify;
152
153 ss_notify = gsm0480_create_notifySS(message);
154 if (!ss_notify)
155 return -1;
156
157 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
158 uint8_t *data = msgb_push(ss_notify, 1);
159 data[0] = ss_notify->len - 1;
160 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
161 gh->msg_type = GSM48_MT_CC_FACILITY;
162 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
163}
164
Harald Weltecf149ee2012-01-23 16:40:24 +0100165void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100166{
167 if (!conn->sec_operation)
168 return;
169
170 talloc_free(conn->sec_operation);
171 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800172 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100173}
174
Harald Weltecf149ee2012-01-23 16:40:24 +0100175void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100176{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100177 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
178 struct gsm_security_operation);
179}
180
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100181int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
182 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800184 struct gsm_network *net = conn->bts->network;
185 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100186 struct gsm_security_operation *op;
187 struct gsm_auth_tuple atuple;
188 int status = -1, rc;
189
190 /* Check if we _can_ enable encryption. Cases where we can't:
191 * - Encryption disabled in config
192 * - Channel already secured (nothing to do)
193 * - Subscriber equipment doesn't support configured encryption
194 */
195 if (!net->a5_encryption) {
196 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800197 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100198 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200199 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100200 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
201 net->a5_encryption)) {
202 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
203 status = GSM_SECURITY_NOAVAIL;
204 }
205
206 /* If not done yet, try to get info for this user */
207 if (status < 0) {
208 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
209 if (rc <= 0)
210 status = GSM_SECURITY_NOAVAIL;
211 }
212
213 /* Are we done yet ? */
214 if (status >= 0)
215 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800216 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100217 0;
218
219 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800220 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100221 return -EBUSY;
222
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800223 allocate_security_operation(conn);
224 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100225 op->cb = cb;
226 op->cb_data = cb_data;
227 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
228
229 /* FIXME: Should start a timer for completion ... */
230
231 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100232 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100233 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800234 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100235 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100236 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100237 return gsm0808_cipher_mode(conn, net->a5_encryption,
238 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100239 }
240
241 return -EINVAL; /* not reached */
242}
243
Holger Freyther73487a22008-12-31 18:53:57 +0000244static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
245 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000246{
247 if (!subscriber)
248 return 0;
249
Holger Freyther73487a22008-12-31 18:53:57 +0000250 /*
251 * Do not send accept yet as more information should arrive. Some
252 * phones will not send us the information and we will have to check
253 * what we want to do with that.
254 */
255 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
256 return 0;
257
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100258 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200259 case GSM_AUTH_POLICY_CLOSED:
260 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200261 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200262 if (subscriber->authorized)
263 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200264 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200265 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000266 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 default:
268 return 0;
269 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000270}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000271
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100272static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000273{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100274 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000275 return;
276
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800277 /* No need to keep the connection up */
278 release_anchor(conn);
279
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200280 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100281 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100282 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100283 if (release)
284 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000285}
286
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100287static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000288{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800289 if (conn->loc_operation)
290 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100291 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000292
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100293 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200294 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000295}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000296
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200297static int finish_lu(struct gsm_subscriber_connection *conn)
298{
299 int rc = 0;
300 int avoid_tmsi = conn->bts->network->avoid_tmsi;
301
302 /* We're all good */
303 if (avoid_tmsi) {
304 conn->subscr->tmsi = GSM_RESERVED_TMSI;
305 db_sync_subscriber(conn->subscr);
306 } else {
307 db_subscriber_alloc_tmsi(conn->subscr);
308 }
309
310 rc = gsm0408_loc_upd_acc(conn);
311 if (conn->bts->network->send_mm_info) {
312 /* send MM INFO with network name */
313 rc = gsm48_tx_mm_info(conn);
314 }
315
316 /* call subscr_update after putting the loc_upd_acc
317 * in the transmit queue, since S_SUBSCR_ATTACHED might
318 * trigger further action like SMS delivery */
319 subscr_update(conn->subscr, conn->bts,
320 GSM_SUBSCRIBER_UPDATE_ATTACHED);
321
322 /*
323 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
324 * MS needs to respond with a TMSI REALLOCATION COMPLETE
325 * (even if the TMSI is the same).
326 */
327 if (avoid_tmsi)
328 release_loc_updating_req(conn, 1);
329
330 return rc;
331}
332
Sylvain Munaut267fba02009-12-24 00:28:01 +0100333static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
334 struct msgb *msg, void *data, void *param)
335{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800336 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100337 int rc = 0;
338
339 switch (event) {
340 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100341 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100342 break;
343
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200344 case GSM_SECURITY_ALREADY:
345 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
346 "UPDATING after CM SERVICE REQUEST\n");
347 /* fall through */
348
Sylvain Munaut267fba02009-12-24 00:28:01 +0100349 case GSM_SECURITY_NOAVAIL:
350 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200351 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100352 break;
353
354 default:
355 rc = -EINVAL;
356 };
357
358 return rc;
359}
360
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100361static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000362{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100363 if (!conn->loc_operation)
364 return 0;
365
Sylvain Munaut267fba02009-12-24 00:28:01 +0100366 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800367 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100368 conn->loc_operation->key_seq,
369 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000370 return 0;
371}
372
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800373void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000374{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800375 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800376
377 /* avoid someone issuing a clear */
378 conn->in_release = 1;
379
Holger Freyther7c19f742009-06-06 13:54:35 +0000380 /*
381 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800382 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000383 */
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100384 release_loc_updating_req(conn, 1);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100385
386 /* We might need to cancel the paging response or such. */
387 if (conn->sec_operation && conn->sec_operation->cb) {
388 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
389 NULL, conn, conn->sec_operation->cb_data);
390 }
391
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800392 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800393 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000394
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100395 /*
396 * Free all transactions that are associated with the released
397 * connection. The transaction code will inform the CC or SMS
398 * facilities that will send the release indications. As part of
399 * the CC REL_IND the remote leg might be released and this will
400 * trigger the call to trans_free. This is something the llist
401 * macro can not handle and we will need to re-iterate the list.
402 *
403 * TODO: Move the trans_list into the subscriber connection and
404 * create a pending list for MT transactions. These exist before
405 * we have a subscriber connection.
406 */
407restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800408 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100409 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200410 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100411 goto restart;
412 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800413 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000414}
415
Harald Welte371efe52010-12-22 23:17:50 +0100416void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
417{
418 struct gsm_trans *trans, *temp;
419
420 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
421
422 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100423 if (trans->protocol == protocol) {
424 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100425 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100426 }
Harald Welte371efe52010-12-22 23:17:50 +0100427 }
428}
429
Holger Freyther429e7762008-12-30 13:28:30 +0000430/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200431int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000432{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800433 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800434 struct msgb *msg;
435
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200436 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800437
438 msg = gsm48_create_loc_upd_rej(cause);
439 if (!msg) {
440 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
441 return -1;
442 }
Harald Welte52b1f982008-12-23 20:25:15 +0000443
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800444 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000445
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100446 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100447 "LAC=%u BTS=%u\n", conn->subscr ?
448 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800449 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100450
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800451 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000452}
453
454/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200455static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000456{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800457 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000458 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000459 struct gsm48_hdr *gh;
460 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200461 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000462
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800463 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000464
465 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
466 gh->proto_discr = GSM48_PDISC_MM;
467 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
468
469 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100470 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000471 bts->network->network_code, bts->location_area_code);
472
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200473 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
474 uint8_t mi[10];
475 int len;
476 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
477 mid = msgb_put(msg, len);
478 memcpy(mid, mi, len);
479 } else {
480 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
481 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
482 }
Harald Welte52b1f982008-12-23 20:25:15 +0000483
484 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
485
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200486 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100487
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800488 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000489}
490
Harald Weltebf5e8df2009-02-03 12:59:45 +0000491/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200492static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000493{
494 struct msgb *msg = gsm48_msgb_alloc();
495 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000496
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800497 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000498
499 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
500 gh->proto_discr = GSM48_PDISC_MM;
501 gh->msg_type = GSM48_MT_MM_ID_REQ;
502 gh->data[0] = id_type;
503
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800504 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000505}
506
Harald Welte231ad4f2008-12-27 11:15:38 +0000507
Harald Weltebf5e8df2009-02-03 12:59:45 +0000508/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800509static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000510{
511 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000512 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200513 struct gsm_bts *bts = lchan->ts->trx->bts;
514 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200515 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200516 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000517
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200518 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200519 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
520 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000521
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200522 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100523
Harald Welte75a983f2008-12-27 21:34:06 +0000524 switch (mi_type) {
525 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200526 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100527 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100528 conn->subscr = subscr_get_by_imsi(net->subscr_group,
529 mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100530 if (!conn->subscr && net->create_subscriber)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100531 conn->subscr = subscr_create_subscriber(
532 net->subscr_group, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200533 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100534 if (!conn->subscr && conn->loc_operation) {
535 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
536 release_loc_updating_req(conn, 1);
537 return 0;
538 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100539 if (conn->loc_operation)
540 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000541 break;
542 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000543 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000544 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100545 if (conn->subscr) {
546 db_subscriber_assoc_imei(conn->subscr, mi_string);
547 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200548 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100549 if (conn->loc_operation)
550 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000551 break;
552 }
Holger Freyther73487a22008-12-31 18:53:57 +0000553
554 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100555 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000556}
557
Harald Welte255539c2008-12-28 02:26:27 +0000558
559static void loc_upd_rej_cb(void *data)
560{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100561 struct gsm_subscriber_connection *conn = data;
562 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100563 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000564
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100565 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800566 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100567 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000568}
569
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100570static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000571{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100572 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
573 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200574 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000575}
576
Harald Welteb9845f92015-08-16 18:07:48 +0200577static const struct value_string lupd_names[] = {
578 { GSM48_LUPD_NORMAL, "NORMAL" },
579 { GSM48_LUPD_PERIODIC, "PERIODIC" },
580 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
581 { 0, NULL }
582};
Harald Welte2a139372009-02-22 21:14:55 +0000583
Harald Weltebf5e8df2009-02-03 12:59:45 +0000584/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800585static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000586{
Harald Welte8470bf22008-12-25 23:28:35 +0000587 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000588 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800589 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800590 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200591 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200592 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000593
Harald Welte8470bf22008-12-25 23:28:35 +0000594 lu = (struct gsm48_loc_upd_req *) gh->data;
595
596 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000597
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200598 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000599
Harald Welteb9845f92015-08-16 18:07:48 +0200600 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
601 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000602
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200603 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100604
Harald Welte24ff6ee2009-12-22 00:41:05 +0100605 switch (lu->type) {
606 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200607 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100608 break;
609 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200610 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100611 break;
612 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200613 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100614 break;
615 }
616
Holger Freythereaf04692009-06-06 13:54:44 +0000617 /*
618 * Pseudo Spoof detection: Just drop a second/concurrent
619 * location updating request.
620 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100621 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200622 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100623 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800624 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000625 return 0;
626 }
627
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800628 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000629
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200630 conn->loc_operation->key_seq = lu->key_seq;
631
Harald Welte52b1f982008-12-23 20:25:15 +0000632 switch (mi_type) {
633 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200634 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000635 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200636 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100637 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000638
Jan Luebbe370b41d2009-08-12 10:19:34 +0200639 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100640 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100641 if (!subscr && bts->network->create_subscriber) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100642 subscr = subscr_create_subscriber(
643 bts->network->subscr_group, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200644 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100645 if (!subscr) {
646 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
647 release_loc_updating_req(conn, 0);
648 return 0;
649 }
Harald Welte4b634542008-12-27 01:55:51 +0000650 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000651 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200652 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000653 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100654 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200655 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000656 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000657 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200658 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100659 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000660 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200661 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200662 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200663 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000664 break;
665 case GSM_MI_TYPE_IMEI:
666 case GSM_MI_TYPE_IMEISV:
667 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200668 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000669 break;
670 default:
Harald Weltea0368542009-06-27 02:58:43 +0200671 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000672 break;
673 }
674
Harald Welte24516ea2009-07-04 10:18:00 +0200675 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100676 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200677
Harald Welte4bfdfe72009-06-10 23:11:52 +0800678 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200679 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800680 /* FIXME: request id? close channel? */
681 return -EINVAL;
682 }
683
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100684 conn->subscr = subscr;
685 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000686
Harald Welte24516ea2009-07-04 10:18:00 +0200687 /* check if we can let the subscriber into our network immediately
688 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100689 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000690}
691
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100692/* Turn int into semi-octet representation: 98 => 0x89 */
693static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800694{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100695 uint8_t ret;
696
697 ret = value / 10;
698 ret |= (value % 10) << 4;
699
700 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800701}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100702
Harald Welte4bfdfe72009-06-10 23:11:52 +0800703
Harald Weltedb253af2008-12-30 17:56:55 +0000704/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800705int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000706{
707 struct msgb *msg = gsm48_msgb_alloc();
708 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800709 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100710 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200711 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200712 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100713
Harald Welte4bfdfe72009-06-10 23:11:52 +0800714 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100715 struct tm* gmt_time;
716 struct tm* local_time;
717 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100718 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000719
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800720 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000721
722 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
723 gh->proto_discr = GSM48_PDISC_MM;
724 gh->msg_type = GSM48_MT_MM_INFO;
725
726 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200727#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000728 name_len = strlen(net->name_long);
729 /* 10.5.3.5a */
730 ptr8 = msgb_put(msg, 3);
731 ptr8[0] = GSM48_IE_NAME_LONG;
732 ptr8[1] = name_len*2 +1;
733 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
734
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200735 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000736 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000737 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000738
739 /* FIXME: Use Cell Broadcast, not UCS-2, since
740 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200741#endif
742 name_len = (strlen(net->name_long)*7)/8;
743 name_pad = (8 - strlen(net->name_long)*7)%8;
744 if (name_pad > 0)
745 name_len++;
746 /* 10.5.3.5a */
747 ptr8 = msgb_put(msg, 3);
748 ptr8[0] = GSM48_IE_NAME_LONG;
749 ptr8[1] = name_len +1;
750 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
751
752 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100753 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200754
Harald Weltedb253af2008-12-30 17:56:55 +0000755 }
756
757 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200758#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000759 name_len = strlen(net->name_short);
760 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200761 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200762 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000763 ptr8[1] = name_len*2 + 1;
764 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
765
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200766 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000767 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000768 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200769#endif
770 name_len = (strlen(net->name_short)*7)/8;
771 name_pad = (8 - strlen(net->name_short)*7)%8;
772 if (name_pad > 0)
773 name_len++;
774 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200775 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200776 ptr8[0] = GSM48_IE_NAME_SHORT;
777 ptr8[1] = name_len +1;
778 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
779
780 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100781 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200782
Harald Weltedb253af2008-12-30 17:56:55 +0000783 }
784
Harald Weltedb253af2008-12-30 17:56:55 +0000785 /* Section 10.5.3.9 */
786 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100787 gmt_time = gmtime(&cur_t);
788
Harald Weltedb253af2008-12-30 17:56:55 +0000789 ptr8 = msgb_put(msg, 8);
790 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100791 ptr8[1] = bcdify(gmt_time->tm_year % 100);
792 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
793 ptr8[3] = bcdify(gmt_time->tm_mday);
794 ptr8[4] = bcdify(gmt_time->tm_hour);
795 ptr8[5] = bcdify(gmt_time->tm_min);
796 ptr8[6] = bcdify(gmt_time->tm_sec);
797
Harald Welte45f91712012-07-08 16:48:11 +0200798 if (bts->tz.override) {
799 /* Convert tz.hr and tz.mn to units */
800 if (bts->tz.hr < 0) {
801 tzunits = ((bts->tz.hr/-1)*4);
802 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100803 ptr8[7] = bcdify(tzunits);
804 /* Set negative time */
805 ptr8[7] |= 0x08;
806 }
807 else {
Harald Welte45f91712012-07-08 16:48:11 +0200808 tzunits = bts->tz.hr*4;
809 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100810 ptr8[7] = bcdify(tzunits);
811 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100812 /* Convert DST value */
813 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
814 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100815 }
816 else {
817 /* Need to get GSM offset and convert into 15 min units */
818 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
819 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200820#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100821 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200822#else
823#warning find a portable way to obtain the timezone offset
824 tzunits = 0;
825#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100826 if (tzunits < 0) {
827 tzunits = tzunits/-1;
828 ptr8[7] = bcdify(tzunits);
829 /* Flip it to negative */
830 ptr8[7] |= 0x08;
831 }
832 else
833 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100834
835 /* Does not support DST +2 */
836 if (local_time->tm_isdst)
837 dst = 1;
838 }
839
840 if (dst) {
841 ptr8 = msgb_put(msg, 3);
842 ptr8[0] = GSM48_IE_NET_DST;
843 ptr8[1] = 1;
844 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100845 }
Harald Weltedb253af2008-12-30 17:56:55 +0000846
Daniel Willmanneea93372009-08-13 03:42:07 +0200847 DEBUGP(DMM, "-> MM INFO\n");
848
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800849 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000850}
851
Harald Welte7984d5c2009-08-12 22:56:50 +0200852/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200853int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200854{
855 struct msgb *msg = gsm48_msgb_alloc();
856 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200857 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200858
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200859 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200860
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800861 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200862 gh->proto_discr = GSM48_PDISC_MM;
863 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
864
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100865 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200866
Harald Welte7984d5c2009-08-12 22:56:50 +0200867 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200868 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200869 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200870
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800871 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200872}
873
874/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800875int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200876{
877 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800878 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200879}
880
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100881/*
882 * At the 30C3 phones miss their periodic update
883 * interval a lot and then remain unreachable. In case
884 * we still know the TMSI we can just attach it again.
885 */
886static void implit_attach(struct gsm_subscriber_connection *conn)
887{
888 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
889 return;
890
891 subscr_update(conn->subscr, conn->bts,
892 GSM_SUBSCRIBER_UPDATE_ATTACHED);
893}
894
895
Sylvain Munautba87f452009-12-24 00:28:46 +0100896static int _gsm48_rx_mm_serv_req_sec_cb(
897 unsigned int hooknum, unsigned int event,
898 struct msgb *msg, void *data, void *param)
899{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800900 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100901 int rc = 0;
902
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200903 /* auth failed or succeeded, the timer was stopped */
904 conn->expire_timer_stopped = 1;
905
Sylvain Munautba87f452009-12-24 00:28:46 +0100906 switch (event) {
907 case GSM_SECURITY_AUTH_FAILED:
908 /* Nothing to do */
909 break;
910
911 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200912 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800913 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100914 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100915 break;
916
917 case GSM_SECURITY_SUCCEEDED:
918 /* nothing to do. CIPHER MODE COMMAND is
919 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100920 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100921 break;
922
923 default:
924 rc = -EINVAL;
925 };
926
927 return rc;
928}
929
Harald Welte4ed0e922009-01-10 03:17:30 +0000930/*
931 * Handle CM Service Requests
932 * a) Verify that the packet is long enough to contain the information
933 * we require otherwsie reject with INCORRECT_MESSAGE
934 * b) Try to parse the TMSI. If we do not have one reject
935 * c) Check that we know the subscriber with the TMSI otherwise reject
936 * with a HLR cause
937 * d) Set the subscriber on the gsm_lchan and accept
938 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800939static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000940{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200941 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200942 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000943
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800944 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000945 struct gsm_subscriber *subscr;
946 struct gsm48_hdr *gh = msgb_l3(msg);
947 struct gsm48_service_request *req =
948 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800949 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200950 uint8_t classmark2_len = gh->data[1];
951 uint8_t *classmark2 = gh->data+2;
952 uint8_t mi_len = *(classmark2 + classmark2_len);
953 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000954
Harald Weltec9e02182009-05-01 19:07:53 +0000955 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000956 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000957 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800958 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000959 GSM48_REJECT_INCORRECT_MESSAGE);
960 }
961
962 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000963 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800964 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000965 GSM48_REJECT_INCORRECT_MESSAGE);
966 }
967
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200968 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000969 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200970
971 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200972 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
973 req->cm_service_type, gsm48_mi_type_name(mi_type),
974 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200975 subscr = subscr_get_by_imsi(bts->network->subscr_group,
976 mi_string);
977 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200978 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
979 req->cm_service_type, gsm48_mi_type_name(mi_type),
980 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200981 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
982 tmsi_from_string(mi_string));
983 } else {
984 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800985 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000986 GSM48_REJECT_INCORRECT_MESSAGE);
987 }
988
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200989 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100990
Harald Welte3ac7f102009-08-10 10:12:45 +0200991 if (is_siemens_bts(bts))
992 send_siemens_mrpci(msg->lchan, classmark2-1);
993
Holger Freythereb443982009-06-04 13:58:42 +0000994
Harald Welte2a139372009-02-22 21:14:55 +0000995 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000996 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800997 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +0200998 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +0000999
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001000 if (!conn->subscr)
1001 conn->subscr = subscr;
1002 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001003 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1004 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001005 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1006 subscr_put(subscr);
1007 }
1008
Harald Weltec2e302d2009-07-05 14:08:13 +02001009 subscr->equipment.classmark2_len = classmark2_len;
1010 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1011 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001012
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001013 /* we will send a MM message soon */
1014 conn->expire_timer_stopped = 1;
1015
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001016 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001017 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001018}
1019
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001020static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001021{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001022 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001023 struct gsm48_hdr *gh = msgb_l3(msg);
1024 struct gsm48_imsi_detach_ind *idi =
1025 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001026 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001027 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001028 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001029
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001030 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001031 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1032 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001033
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001034 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001035
Harald Welte2a139372009-02-22 21:14:55 +00001036 switch (mi_type) {
1037 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001038 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001039 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001040 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001041 break;
1042 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001043 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001044 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1045 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001046 break;
1047 case GSM_MI_TYPE_IMEI:
1048 case GSM_MI_TYPE_IMEISV:
1049 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001050 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001051 break;
1052 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001053 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001054 break;
1055 }
1056
Holger Freyther4a49e772009-04-12 05:37:29 +00001057 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001058 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001059 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001060 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001061
1062 subscr->equipment.classmark1 = idi->classmark1;
1063 db_sync_equipment(&subscr->equipment);
1064
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001065 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001066 } else
Harald Welte2a139372009-02-22 21:14:55 +00001067 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1068
Harald Welte31981a02009-12-20 09:58:40 +01001069 /* FIXME: iterate over all transactions and release them,
1070 * imagine an IMSI DETACH happening during an active call! */
1071
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001072 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001073 return 0;
1074}
1075
Harald Welted2a7f5a2009-06-05 20:08:20 +00001076static int gsm48_rx_mm_status(struct msgb *msg)
1077{
1078 struct gsm48_hdr *gh = msgb_l3(msg);
1079
1080 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1081
1082 return 0;
1083}
1084
Sylvain Munaut30a15382009-12-24 00:27:26 +01001085/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001086static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001087{
1088 struct gsm48_hdr *gh = msgb_l3(msg);
1089 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001090 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001091
Sylvain Munautc593cf12010-06-10 23:51:41 +02001092 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001093 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001094
1095 /* Safety check */
1096 if (!conn->sec_operation) {
1097 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1098 return -EIO;
1099 }
1100
1101 /* Validate SRES */
1102 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001103 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001104 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001105
1106 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001107 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001108
Sylvain Munaut30a15382009-12-24 00:27:26 +01001109 if (cb)
1110 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001111 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001112
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001113 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001114 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001115 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001116 }
1117
Sylvain Munautc593cf12010-06-10 23:51:41 +02001118 DEBUGPC(DMM, "OK\n");
1119
Sylvain Munaut30a15382009-12-24 00:27:26 +01001120 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001121 return gsm0808_cipher_mode(conn, net->a5_encryption,
1122 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001123}
1124
Harald Weltebf5e8df2009-02-03 12:59:45 +00001125/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001126static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001127{
1128 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001129 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001130
1131 switch (gh->msg_type & 0xbf) {
1132 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001133 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001134 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001135 break;
1136 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001137 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001138 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001139 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001140 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001141 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001142 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001143 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001144 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001145 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001146 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001147 conn->subscr ?
1148 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001149 "unknown subscriber");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001150 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001151 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001152 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001153 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001154 break;
1155 case GSM48_MT_MM_CM_REEST_REQ:
1156 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1157 break;
1158 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001159 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001160 break;
1161 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001162 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001163 gh->msg_type);
1164 break;
1165 }
1166
1167 return rc;
1168}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001169
Harald Welte2d35ae62009-02-06 12:02:13 +00001170/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001171static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001172{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001173 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001174 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001175 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001176 uint8_t *classmark2_lv = gh->data + 1;
1177 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001178 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001179 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001180 int rc = 0;
1181
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001182 resp = (struct gsm48_pag_resp *) &gh->data[0];
1183 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1184 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001185 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1186 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001187
Harald Weltefe18d8f2009-02-22 21:14:24 +00001188 switch (mi_type) {
1189 case GSM_MI_TYPE_TMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001190 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001191 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001192 break;
1193 case GSM_MI_TYPE_IMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001194 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1195 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001196 break;
1197 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001198
1199 if (!subscr) {
1200 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001201 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001202 return -EINVAL;
1203 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001204 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001205 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001206 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001207
Harald Weltec2e302d2009-07-05 14:08:13 +02001208 subscr->equipment.classmark2_len = *classmark2_lv;
1209 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1210 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001211
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001212 /* We received a paging */
1213 conn->expire_timer_stopped = 1;
1214
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001215 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001216 return rc;
1217}
1218
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001219static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001220{
1221 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001222 uint8_t apdu_id_flags;
1223 uint8_t apdu_len;
1224 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001225
1226 apdu_id_flags = gh->data[0];
1227 apdu_len = gh->data[1];
1228 apdu_data = gh->data+2;
1229
Harald Welte70f92052012-07-16 13:22:19 +02001230 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001231 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001232
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001233 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001234}
1235
Harald Weltebf5e8df2009-02-03 12:59:45 +00001236/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001237static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001238{
1239 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001240 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001241
1242 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001243 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001244 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001245 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001246 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001247 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001248 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001249 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001250 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001251 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001252 break;
1253 }
1254
Harald Welte2d35ae62009-02-06 12:02:13 +00001255 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001256}
1257
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001258int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1259 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001260{
1261 struct msgb *msg = gsm48_msgb_alloc();
1262 struct gsm48_hdr *gh;
1263
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001264 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001265
1266 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1267 apdu_id, apdu_len);
1268
1269 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1270 gh->proto_discr = GSM48_PDISC_RR;
1271 gh->msg_type = GSM48_MT_RR_APP_INFO;
1272 gh->data[0] = apdu_id;
1273 gh->data[1] = apdu_len;
1274 memcpy(gh->data+2, apdu, apdu_len);
1275
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001276 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001277}
1278
Harald Welte4bc90a12008-12-27 16:32:52 +00001279/* Call Control */
1280
Harald Welte7584aea2009-02-11 11:44:12 +00001281/* The entire call control code is written in accordance with Figure 7.10c
1282 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1283 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1284 * it for voice */
1285
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286static void new_cc_state(struct gsm_trans *trans, int state)
1287{
1288 if (state > 31 || state < 0)
1289 return;
1290
1291 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001292 gsm48_cc_state_name(trans->cc.state),
1293 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001294
Harald Weltedcaf5652009-07-23 18:56:43 +02001295 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296}
1297
1298static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001299{
1300 struct msgb *msg = gsm48_msgb_alloc();
1301 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001302 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001303
Harald Welte4bc90a12008-12-27 16:32:52 +00001304 gh->msg_type = GSM48_MT_CC_STATUS;
1305
1306 cause = msgb_put(msg, 3);
1307 cause[0] = 2;
1308 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1309 cause[2] = 0x80 | 30; /* response to status inquiry */
1310
1311 call_state = msgb_put(msg, 1);
1312 call_state[0] = 0xc0 | 0x00;
1313
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001314 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001315}
1316
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001317static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001318 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001319{
1320 struct msgb *msg = gsm48_msgb_alloc();
1321 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1322
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001323 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001324
Harald Welte6f4b7532008-12-29 00:39:37 +00001325 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001326 gh->msg_type = msg_type;
1327
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001328 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001329}
1330
Harald Welte4bfdfe72009-06-10 23:11:52 +08001331static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1332{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001333 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001334 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001335 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001336 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337 }
1338}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001339
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1341 int msg_type, struct gsm_mncc *mncc)
1342{
1343 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001344 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345
1346 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001347 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001348 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001349 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001350 trans->conn->lchan->ts->trx->bts->nr,
1351 trans->conn->lchan->ts->trx->nr,
1352 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353 (trans->subscr)?(trans->subscr->extension):"-",
1354 get_mncc_name(msg_type));
1355 else
1356 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1357 "Sending '%s' to MNCC.\n",
1358 (trans->subscr)?(trans->subscr->extension):"-",
1359 get_mncc_name(msg_type));
1360 else
1361 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1362 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1363
1364 mncc->msg_type = msg_type;
1365
Harald Welte966636f2009-06-26 19:39:35 +02001366 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 if (!msg)
1368 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001369
1370 data = msgb_put(msg, sizeof(struct gsm_mncc));
1371 memcpy(data, mncc, sizeof(struct gsm_mncc));
1372
Harald Welte54209c22010-12-22 23:43:29 +01001373 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374
1375 return 0;
1376}
1377
1378int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001379 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380{
1381 struct gsm_mncc rel;
1382
Harald Welte92f70c52009-06-12 01:54:08 +08001383 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001384 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001385 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001386 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1387 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1389}
1390
Harald Weltedcaf5652009-07-23 18:56:43 +02001391/* Call Control Specific transaction release.
1392 * gets called by trans_free, DO NOT CALL YOURSELF! */
1393void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001394{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001395 gsm48_stop_cc_timer(trans);
1396
1397 /* send release to L4, if callref still exists */
1398 if (trans->callref) {
1399 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001400 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001401 GSM48_CAUSE_LOC_PRN_S_LU,
1402 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001403 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001404 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001405 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001406 if (trans->conn)
1407 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408}
1409
1410static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001411
Harald Welte09e38af2009-02-16 22:52:23 +00001412/* call-back from paging the B-end of the connection */
1413static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001414 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001415{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001416 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001417 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001418
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001419 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001420
Harald Welte4bfdfe72009-06-10 23:11:52 +08001421 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001422 switch (event) {
1423 case GSM_PAGING_SUCCEEDED:
1424 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001425 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001426 /* Assign lchan */
1427 transt->conn = conn;
1428 /* send SETUP request to called party */
1429 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1430 break;
1431 case GSM_PAGING_EXPIRED:
1432 case GSM_PAGING_BUSY:
1433 DEBUGP(DCC, "Paging subscr %s expired!\n",
1434 transt->subscr->extension);
1435 /* Temporarily out of order */
1436 mncc_release_ind(transt->net, transt,
1437 transt->callref,
1438 GSM48_CAUSE_LOC_PRN_S_LU,
1439 GSM48_CC_CAUSE_DEST_OOO);
1440 transt->callref = 0;
1441 transt->paging_request = NULL;
1442 trans_free(transt);
1443 break;
1444 default:
1445 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1446 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001447 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001448
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001449 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001450 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001451}
Harald Welte7584aea2009-02-11 11:44:12 +00001452
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001453static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001454
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001455/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001456static int switch_for_handover(struct gsm_lchan *old_lchan,
1457 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001458{
1459 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001460
1461 if (ipacc_rtp_direct) {
1462 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1463 return 0;
1464 }
1465
1466 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001467 new_rs = new_lchan->abis_ip.rtp_socket;
1468 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001469
1470 if (!new_rs) {
1471 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1472 return -EIO;
1473 }
1474
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001475 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001476
1477 if (!old_rs) {
1478 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1479 return -EIO;
1480 }
1481
1482 /* copy rx_action and reference to other sock */
1483 new_rs->rx_action = old_rs->rx_action;
1484 new_rs->tx_action = old_rs->tx_action;
1485 new_rs->transmit = old_rs->transmit;
1486
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001487 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001488 case RTP_PROXY:
1489 other_rs = old_rs->proxy.other_sock;
1490 rtp_socket_proxy(new_rs, other_rs);
1491 /* delete reference to other end socket to prevent
1492 * rtp_socket_free() from removing the inverse reference */
1493 old_rs->proxy.other_sock = NULL;
1494 break;
1495 case RTP_RECV_UPSTREAM:
1496 new_rs->receive = old_rs->receive;
1497 break;
1498 case RTP_NONE:
1499 break;
1500 }
1501
1502 return 0;
1503}
1504
Harald Welte805f6442009-07-28 18:25:29 +02001505/* some other part of the code sends us a signal */
1506static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1507 void *handler_data, void *signal_data)
1508{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001509 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001510 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001511 struct gsm_network *net;
1512 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001513
1514 if (subsys != SS_ABISIP)
1515 return 0;
1516
1517 /* in case we use direct BTS-to-BTS RTP */
1518 if (ipacc_rtp_direct)
1519 return 0;
1520
Harald Welte805f6442009-07-28 18:25:29 +02001521 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001522 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001523 /* in case we don't use direct BTS-to-BTS RTP */
1524 /* the BTS has successfully bound a TCH to a local ip/port,
1525 * which means we can connect our UDP socket to it */
1526 if (lchan->abis_ip.rtp_socket) {
1527 rtp_socket_free(lchan->abis_ip.rtp_socket);
1528 lchan->abis_ip.rtp_socket = NULL;
1529 }
1530
1531 lchan->abis_ip.rtp_socket = rtp_socket_create();
1532 if (!lchan->abis_ip.rtp_socket)
1533 return -EIO;
1534
1535 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1536 lchan->abis_ip.bound_ip,
1537 lchan->abis_ip.bound_port);
1538 if (rc < 0)
1539 return -EIO;
1540
Harald Welteda7ab742009-12-19 22:23:05 +01001541 /* check if any transactions on this lchan still have
1542 * a tch_recv_mncc request pending */
1543 net = lchan->ts->trx->bts->network;
1544 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001545 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001546 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1547 tch_recv_mncc(net, trans->callref, 1);
1548 }
1549 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001550
1551 /*
1552 * TODO: this appears to be too early? Why not until after
1553 * the handover detect or the handover complete?
1554 *
1555 * Do we have a handover pending for this new lchan? In that
1556 * case re-route the audio from the old channel to the new one.
1557 */
1558 old_lchan = bsc_handover_pending(lchan);
1559 if (old_lchan)
1560 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001561 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001562 case S_ABISIP_DLCX_IND:
1563 /* the BTS tells us a RTP stream has been disconnected */
1564 if (lchan->abis_ip.rtp_socket) {
1565 rtp_socket_free(lchan->abis_ip.rtp_socket);
1566 lchan->abis_ip.rtp_socket = NULL;
1567 }
1568
1569 break;
Harald Welte805f6442009-07-28 18:25:29 +02001570 }
1571
1572 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001573}
1574
Harald Welte49f48b82009-02-17 15:29:33 +00001575/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001576static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001577{
Harald Welte11fa29c2009-02-19 17:24:39 +00001578 struct gsm_bts *bts = lchan->ts->trx->bts;
1579 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001580 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001581
Harald Welte11fa29c2009-02-19 17:24:39 +00001582 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1583 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1584 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1585
1586 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001587 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001588 return -EINVAL;
1589 }
Harald Welteda7ab742009-12-19 22:23:05 +01001590
1591 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001592 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001593 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001594 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001595 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001596 if (!lchan->abis_ip.rtp_socket) {
1597 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1598 "lchan\n");
1599 return -EIO;
1600 }
1601 if (!remote_lchan->abis_ip.rtp_socket) {
1602 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1603 "remote_lchan\n");
1604 return -EIO;
1605 }
1606
Harald Welte805f6442009-07-28 18:25:29 +02001607 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001608 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001609 if (rc < 0)
1610 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001611 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001612 if (rc < 0)
1613 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001614 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301615 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1616 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001617 } else {
1618 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301619 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1620 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301621 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001622 if (rc < 0)
1623 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301624 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1625 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301626 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001627 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001628 break;
1629 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001630 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001631 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001632 trau_mux_map_lchan(lchan, remote_lchan);
1633 break;
1634 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001635 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001636 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001637 }
Harald Welte49f48b82009-02-17 15:29:33 +00001638
1639 return 0;
1640}
1641
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001643static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001644{
Harald Weltedcaf5652009-07-23 18:56:43 +02001645 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1646 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001647
Harald Welte4bfdfe72009-06-10 23:11:52 +08001648 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001649 return -EIO;
1650
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001651 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 return -EIO;
1653
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001654 /* Which subscriber do we want to track trans1 or trans2? */
1655 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1656
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001658 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001659}
1660
Harald Welteda7ab742009-12-19 22:23:05 +01001661/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001662static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663{
1664 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001665 struct gsm_lchan *lchan;
1666 struct gsm_bts *bts;
1667 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001668
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001670 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 if (!trans)
1672 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001673 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001675
1676 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001677 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001678 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001680 /* store receive state */
1681 trans->tch_recv = enable;
1682
Harald Welteda7ab742009-12-19 22:23:05 +01001683 switch (bts->type) {
1684 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001685 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001686 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001687 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001688 return -EINVAL;
1689 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001690 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1691 * will not be connected to our RTP proxy and the socket will
1692 * not be assigned to the application interface. This method
1693 * will be called again, once the audio socket is created and
1694 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001695 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001696 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1697 return 0;
1698 }
1699 if (enable) {
1700 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001701 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001702 if (rc < 0)
1703 return rc;
1704 /* assign socket to application interface */
1705 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1706 net, callref);
1707 } else
1708 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1709 net, 0);
1710 break;
1711 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001712 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001713 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001714 /* In case we don't have a TCH with correct mode, the TRAU muxer
1715 * will not be asigned to the application interface. This is
1716 * performed by switch_trau_mux() after successful handover or
1717 * assignment. */
1718 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1719 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1720 return 0;
1721 }
Harald Welteda7ab742009-12-19 22:23:05 +01001722 if (enable)
1723 return trau_recv_lchan(lchan, callref);
1724 return trau_mux_unmap(NULL, callref);
1725 break;
1726 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001727 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001728 return -EINVAL;
1729 }
1730
1731 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001732}
1733
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1735{
1736 DEBUGP(DCC, "-> STATUS ENQ\n");
1737 return gsm48_cc_tx_status(trans, msg);
1738}
1739
1740static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1741static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1742
1743static void gsm48_cc_timeout(void *arg)
1744{
1745 struct gsm_trans *trans = arg;
1746 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001747 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1748 int mo_location = GSM48_CAUSE_LOC_USER;
1749 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1750 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 struct gsm_mncc mo_rel, l4_rel;
1752
1753 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1754 mo_rel.callref = trans->callref;
1755 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1756 l4_rel.callref = trans->callref;
1757
Harald Weltedcaf5652009-07-23 18:56:43 +02001758 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 case 0x303:
1760 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001761 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 break;
1763 case 0x310:
1764 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001765 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 break;
1767 case 0x313:
1768 disconnect = 1;
1769 /* unknown, did not find it in the specs */
1770 break;
1771 case 0x301:
1772 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001773 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 break;
1775 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001776 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001778 gsm48_cc_tx_release(trans, &trans->cc.msg);
1779 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 break; /* stay in release state */
1781 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001782 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 return;
1784// release = 1;
1785// l4_cause = 14;
1786// break;
1787 case 0x306:
1788 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001789 mo_cause = trans->cc.msg.cause.value;
1790 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 break;
1792 case 0x323:
1793 disconnect = 1;
1794 break;
1795 default:
1796 release = 1;
1797 }
1798
1799 if (release && trans->callref) {
1800 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001801 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 l4_location, l4_cause);
1803 trans->callref = 0;
1804 }
1805
1806 if (disconnect && trans->callref) {
1807 /* process disconnect towards layer 4 */
1808 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001809 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 }
1811
1812 /* process disconnect towards mobile station */
1813 if (disconnect || release) {
1814 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001815 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1816 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1817 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 mo_rel.cause.diag_len = 3;
1819
1820 if (disconnect)
1821 gsm48_cc_tx_disconnect(trans, &mo_rel);
1822 if (release)
1823 gsm48_cc_tx_release(trans, &mo_rel);
1824 }
1825
1826}
1827
1828static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1829 int sec, int micro)
1830{
1831 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001832 trans->cc.timer.cb = gsm48_cc_timeout;
1833 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001834 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001835 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836}
1837
1838static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1839{
1840 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001841 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001842 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1843 struct tlv_parsed tp;
1844 struct gsm_mncc setup;
1845
1846 memset(&setup, 0, sizeof(struct gsm_mncc));
1847 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001848 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001849 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 /* emergency setup is identified by msg_type */
1851 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1852 setup.emergency = 1;
1853
1854 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001855 setup.fields |= MNCC_F_CALLING;
1856 strncpy(setup.calling.number, trans->subscr->extension,
1857 sizeof(setup.calling.number)-1);
1858 strncpy(setup.imsi, trans->subscr->imsi,
1859 sizeof(setup.imsi)-1);
1860
Harald Welte4bfdfe72009-06-10 23:11:52 +08001861 /* bearer capability */
1862 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1863 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001864 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001866 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001867 }
1868 /* facility */
1869 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1870 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001871 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1873 }
1874 /* called party bcd number */
1875 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1876 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1879 }
1880 /* user-user */
1881 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1882 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1885 }
1886 /* ss-version */
1887 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1888 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001889 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1891 }
1892 /* CLIR suppression */
1893 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1894 setup.clir.sup = 1;
1895 /* CLIR invocation */
1896 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1897 setup.clir.inv = 1;
1898 /* cc cap */
1899 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1900 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001901 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001902 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1903 }
1904
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 new_cc_state(trans, GSM_CSTATE_INITIATED);
1906
Harald Welte (local)47df3992009-12-26 19:45:03 +01001907 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1908 subscr_name(trans->subscr), trans->subscr->extension,
1909 setup.called.number);
1910
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001911 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001912
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001914 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915
Harald Welte13cac662009-07-29 12:10:35 +02001916 /* MNCC code will modify the channel asynchronously, we should
1917 * ipaccess-bind only after the modification has been made to the
1918 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919 return 0;
1920}
1921
1922static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001923{
1924 struct msgb *msg = gsm48_msgb_alloc();
1925 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001926 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001927 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001928
Harald Welte7ccf7782009-02-17 01:43:01 +00001929 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001930
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 /* transaction id must not be assigned */
1932 if (trans->transaction_id != 0xff) { /* unasssigned */
1933 DEBUGP(DCC, "TX Setup with assigned transaction. "
1934 "This is not allowed!\n");
1935 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001936 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001937 GSM48_CAUSE_LOC_PRN_S_LU,
1938 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001940 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941 return rc;
1942 }
1943
1944 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001945 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
1946 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001947 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001949 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001950 GSM48_CAUSE_LOC_PRN_S_LU,
1951 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001953 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954 return rc;
1955 }
Harald Welte78283ef2009-07-23 21:36:44 +02001956 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001957
Harald Welte65e74cc2008-12-29 01:55:35 +00001958 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001959
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001961
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 /* bearer capability */
1963 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001964 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 /* facility */
1966 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 /* progress */
1969 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001970 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 /* calling party BCD number */
1972 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001973 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 /* called party BCD number */
1975 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001976 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977 /* user-user */
1978 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001979 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 /* redirecting party BCD number */
1981 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001982 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 /* signal */
1984 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001985 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986
1987 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001988
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001989 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001990
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001991 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001992}
1993
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1995{
1996 struct gsm48_hdr *gh = msgb_l3(msg);
1997 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1998 struct tlv_parsed tp;
1999 struct gsm_mncc call_conf;
2000
2001 gsm48_stop_cc_timer(trans);
2002 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2003
2004 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2005 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002006 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002007 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008#if 0
2009 /* repeat */
2010 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2011 call_conf.repeat = 1;
2012 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2013 call_conf.repeat = 2;
2014#endif
2015 /* bearer capability */
2016 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2017 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002018 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002020 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 }
2022 /* cause */
2023 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2024 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002025 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2027 }
2028 /* cc cap */
2029 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2030 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002031 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2033 }
2034
Andreas Eversberg035b8742013-09-19 09:27:06 +02002035 /* IMSI of called subscriber */
2036 strncpy(call_conf.imsi, trans->subscr->imsi,
2037 sizeof(call_conf.imsi)-1);
2038
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2040
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002041 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002042 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043}
2044
2045static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2046{
2047 struct gsm_mncc *proceeding = arg;
2048 struct msgb *msg = gsm48_msgb_alloc();
2049 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2050
Harald Welte4bfdfe72009-06-10 23:11:52 +08002051 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2052
2053 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2054
2055 /* bearer capability */
2056 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002057 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 /* facility */
2059 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002060 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061 /* progress */
2062 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002063 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002065 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066}
2067
2068static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2069{
2070 struct gsm48_hdr *gh = msgb_l3(msg);
2071 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2072 struct tlv_parsed tp;
2073 struct gsm_mncc alerting;
2074
2075 gsm48_stop_cc_timer(trans);
2076 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2077
2078 memset(&alerting, 0, sizeof(struct gsm_mncc));
2079 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002080 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 /* facility */
2082 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2083 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002084 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2086 }
2087
2088 /* progress */
2089 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2090 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002091 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2093 }
2094 /* ss-version */
2095 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2096 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002097 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2099 }
2100
2101 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2102
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002103 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002104 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105}
2106
2107static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2108{
2109 struct gsm_mncc *alerting = arg;
2110 struct msgb *msg = gsm48_msgb_alloc();
2111 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2112
Harald Welte4bfdfe72009-06-10 23:11:52 +08002113 gh->msg_type = GSM48_MT_CC_ALERTING;
2114
2115 /* facility */
2116 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 /* progress */
2119 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002120 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121 /* user-user */
2122 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124
2125 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2126
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002127 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128}
2129
2130static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2131{
2132 struct gsm_mncc *progress = arg;
2133 struct msgb *msg = gsm48_msgb_alloc();
2134 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2135
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 gh->msg_type = GSM48_MT_CC_PROGRESS;
2137
2138 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002139 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140 /* user-user */
2141 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002142 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002143
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002144 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145}
2146
2147static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2148{
2149 struct gsm_mncc *connect = arg;
2150 struct msgb *msg = gsm48_msgb_alloc();
2151 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2152
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 gh->msg_type = GSM48_MT_CC_CONNECT;
2154
2155 gsm48_stop_cc_timer(trans);
2156 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2157
2158 /* facility */
2159 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002160 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161 /* progress */
2162 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002163 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164 /* connected number */
2165 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002166 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167 /* user-user */
2168 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002169 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170
2171 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2172
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002173 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174}
2175
2176static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2177{
2178 struct gsm48_hdr *gh = msgb_l3(msg);
2179 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2180 struct tlv_parsed tp;
2181 struct gsm_mncc connect;
2182
2183 gsm48_stop_cc_timer(trans);
2184
2185 memset(&connect, 0, sizeof(struct gsm_mncc));
2186 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002187 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002189 connect.fields |= MNCC_F_CONNECTED;
2190 strncpy(connect.connected.number, trans->subscr->extension,
2191 sizeof(connect.connected.number)-1);
2192 strncpy(connect.imsi, trans->subscr->imsi,
2193 sizeof(connect.imsi)-1);
2194
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195 /* facility */
2196 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2197 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002198 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2200 }
2201 /* user-user */
2202 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2203 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002204 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2206 }
2207 /* ss-version */
2208 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2209 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002210 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2212 }
2213
2214 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002215 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002217 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002218}
2219
2220
2221static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2222{
2223 struct gsm_mncc connect_ack;
2224
2225 gsm48_stop_cc_timer(trans);
2226
2227 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002228 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229
2230 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2231 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002232
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002233 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 &connect_ack);
2235}
2236
2237static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2238{
2239 struct msgb *msg = gsm48_msgb_alloc();
2240 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2241
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2243
2244 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2245
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002246 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247}
2248
2249static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2250{
2251 struct gsm48_hdr *gh = msgb_l3(msg);
2252 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2253 struct tlv_parsed tp;
2254 struct gsm_mncc disc;
2255
2256 gsm48_stop_cc_timer(trans);
2257
2258 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2259
2260 memset(&disc, 0, sizeof(struct gsm_mncc));
2261 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002262 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 /* cause */
2264 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2265 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002266 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2268 }
2269 /* facility */
2270 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2271 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002272 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2274 }
2275 /* user-user */
2276 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2277 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002278 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2280 }
2281 /* ss-version */
2282 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2283 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002284 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2286 }
2287
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002288 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289
2290}
2291
Harald Weltec66b71c2009-06-11 14:23:20 +08002292static struct gsm_mncc_cause default_cause = {
2293 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2294 .coding = 0,
2295 .rec = 0,
2296 .rec_val = 0,
2297 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2298 .diag_len = 0,
2299 .diag = { 0 },
2300};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301
2302static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2303{
2304 struct gsm_mncc *disc = arg;
2305 struct msgb *msg = gsm48_msgb_alloc();
2306 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2307
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2309
2310 gsm48_stop_cc_timer(trans);
2311 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2312
2313 /* cause */
2314 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002315 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316 else
Harald Welte55c8f352010-03-07 23:40:35 +01002317 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318
2319 /* facility */
2320 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002321 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 /* progress */
2323 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002324 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 /* user-user */
2326 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002327 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328
2329 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002330 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331
2332 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2333
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002334 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002335}
2336
2337static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2338{
2339 struct gsm48_hdr *gh = msgb_l3(msg);
2340 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2341 struct tlv_parsed tp;
2342 struct gsm_mncc rel;
2343 int rc;
2344
2345 gsm48_stop_cc_timer(trans);
2346
2347 memset(&rel, 0, sizeof(struct gsm_mncc));
2348 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002349 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350 /* cause */
2351 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2352 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002353 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2355 }
2356 /* facility */
2357 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2358 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002359 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2361 }
2362 /* user-user */
2363 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2364 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002365 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2367 }
2368 /* ss-version */
2369 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2370 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002371 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2373 }
2374
Harald Weltedcaf5652009-07-23 18:56:43 +02002375 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002377 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002379 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002380 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002381 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002382 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383 }
2384
2385 new_cc_state(trans, GSM_CSTATE_NULL);
2386
2387 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002388 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002389
2390 return rc;
2391}
2392
2393static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2394{
2395 struct gsm_mncc *rel = arg;
2396 struct msgb *msg = gsm48_msgb_alloc();
2397 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2398
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399 gh->msg_type = GSM48_MT_CC_RELEASE;
2400
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 gsm48_stop_cc_timer(trans);
2402 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2403
2404 /* cause */
2405 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002406 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407 /* facility */
2408 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002409 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410 /* user-user */
2411 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002412 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413
Harald Weltedcaf5652009-07-23 18:56:43 +02002414 trans->cc.T308_second = 0;
2415 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002416
Harald Weltedcaf5652009-07-23 18:56:43 +02002417 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2419
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002420 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421}
2422
2423static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2424{
2425 struct gsm48_hdr *gh = msgb_l3(msg);
2426 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2427 struct tlv_parsed tp;
2428 struct gsm_mncc rel;
2429 int rc = 0;
2430
2431 gsm48_stop_cc_timer(trans);
2432
2433 memset(&rel, 0, sizeof(struct gsm_mncc));
2434 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002435 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436 /* cause */
2437 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2438 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2441 }
2442 /* facility */
2443 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2444 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002445 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2447 }
2448 /* user-user */
2449 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2450 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002451 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2453 }
2454 /* ss-version */
2455 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2456 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002457 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2459 }
2460
2461 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002462 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002464 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002465 MNCC_REJ_IND, &rel);
2466 break;
2467 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002468 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 MNCC_REL_CNF, &rel);
2470 break;
2471 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002472 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473 MNCC_REL_IND, &rel);
2474 }
2475 }
2476
2477 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002478 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479
2480 return rc;
2481}
2482
2483static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2484{
2485 struct gsm_mncc *rel = arg;
2486 struct msgb *msg = gsm48_msgb_alloc();
2487 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002488 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2491
2492 trans->callref = 0;
2493
2494 gsm48_stop_cc_timer(trans);
2495
2496 /* cause */
2497 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002498 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499 /* facility */
2500 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002501 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502 /* user-user */
2503 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002504 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505
Harald Weltec7782de2010-12-21 19:31:41 +01002506 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2507
Harald Weltedcaf5652009-07-23 18:56:43 +02002508 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509
Harald Weltec7782de2010-12-21 19:31:41 +01002510 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511}
2512
2513static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2514{
2515 struct gsm48_hdr *gh = msgb_l3(msg);
2516 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2517 struct tlv_parsed tp;
2518 struct gsm_mncc fac;
2519
2520 memset(&fac, 0, sizeof(struct gsm_mncc));
2521 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002522 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523 /* facility */
2524 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2525 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002526 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2528 }
2529 /* ss-version */
2530 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2531 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002532 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2534 }
2535
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002536 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537}
2538
2539static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2540{
2541 struct gsm_mncc *fac = arg;
2542 struct msgb *msg = gsm48_msgb_alloc();
2543 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2544
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545 gh->msg_type = GSM48_MT_CC_FACILITY;
2546
2547 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002548 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002550 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551}
2552
2553static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2554{
2555 struct gsm_mncc hold;
2556
2557 memset(&hold, 0, sizeof(struct gsm_mncc));
2558 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002559 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560}
2561
2562static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2563{
2564 struct msgb *msg = gsm48_msgb_alloc();
2565 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2566
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2568
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002569 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570}
2571
2572static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2573{
2574 struct gsm_mncc *hold_rej = arg;
2575 struct msgb *msg = gsm48_msgb_alloc();
2576 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2577
Harald Welte4bfdfe72009-06-10 23:11:52 +08002578 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2579
2580 /* cause */
2581 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002582 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583 else
Harald Welte55c8f352010-03-07 23:40:35 +01002584 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002586 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587}
2588
2589static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2590{
2591 struct gsm_mncc retrieve;
2592
2593 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2594 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002595 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002596 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597}
2598
2599static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2600{
2601 struct msgb *msg = gsm48_msgb_alloc();
2602 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2603
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2605
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002606 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607}
2608
2609static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2610{
2611 struct gsm_mncc *retrieve_rej = arg;
2612 struct msgb *msg = gsm48_msgb_alloc();
2613 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2614
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2616
2617 /* cause */
2618 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002619 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620 else
Harald Welte55c8f352010-03-07 23:40:35 +01002621 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002622
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002623 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002624}
2625
2626static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2627{
2628 struct gsm48_hdr *gh = msgb_l3(msg);
2629 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2630 struct tlv_parsed tp;
2631 struct gsm_mncc dtmf;
2632
2633 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2634 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002635 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002636 /* keypad facility */
2637 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2638 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002639 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002640 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2641 }
2642
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002643 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644}
2645
2646static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2647{
2648 struct gsm_mncc *dtmf = arg;
2649 struct msgb *msg = gsm48_msgb_alloc();
2650 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2651
Harald Welte4bfdfe72009-06-10 23:11:52 +08002652 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2653
2654 /* keypad */
2655 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002656 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002658 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659}
2660
2661static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2662{
2663 struct gsm_mncc *dtmf = arg;
2664 struct msgb *msg = gsm48_msgb_alloc();
2665 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2666
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2668
2669 /* cause */
2670 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002671 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 else
Harald Welte55c8f352010-03-07 23:40:35 +01002673 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002675 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002676}
2677
2678static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2679{
2680 struct msgb *msg = gsm48_msgb_alloc();
2681 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2682
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2684
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002685 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002686}
2687
2688static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2689{
2690 struct gsm_mncc dtmf;
2691
2692 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2693 dtmf.callref = trans->callref;
2694
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002695 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696}
2697
2698static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2699{
2700 struct gsm48_hdr *gh = msgb_l3(msg);
2701 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2702 struct tlv_parsed tp;
2703 struct gsm_mncc modify;
2704
2705 memset(&modify, 0, sizeof(struct gsm_mncc));
2706 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002707 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708 /* bearer capability */
2709 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2710 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002711 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002713 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714 }
2715
2716 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2717
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002718 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719}
2720
2721static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2722{
2723 struct gsm_mncc *modify = arg;
2724 struct msgb *msg = gsm48_msgb_alloc();
2725 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2726
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727 gh->msg_type = GSM48_MT_CC_MODIFY;
2728
2729 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2730
2731 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002732 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002733
2734 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2735
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002736 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737}
2738
2739static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2740{
2741 struct gsm48_hdr *gh = msgb_l3(msg);
2742 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2743 struct tlv_parsed tp;
2744 struct gsm_mncc modify;
2745
2746 gsm48_stop_cc_timer(trans);
2747
2748 memset(&modify, 0, sizeof(struct gsm_mncc));
2749 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002750 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751 /* bearer capability */
2752 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2753 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002754 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002756 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002757 }
2758
2759 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2760
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002761 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762}
2763
2764static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2765{
2766 struct gsm_mncc *modify = arg;
2767 struct msgb *msg = gsm48_msgb_alloc();
2768 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2769
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2771
2772 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002773 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774
2775 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2776
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002777 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002778}
2779
2780static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2781{
2782 struct gsm48_hdr *gh = msgb_l3(msg);
2783 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2784 struct tlv_parsed tp;
2785 struct gsm_mncc modify;
2786
2787 gsm48_stop_cc_timer(trans);
2788
2789 memset(&modify, 0, sizeof(struct gsm_mncc));
2790 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002791 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792 /* bearer capability */
2793 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2794 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002795 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002796 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002797 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798 }
2799 /* cause */
2800 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2801 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002802 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2804 }
2805
2806 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2807
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002808 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809}
2810
2811static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2812{
2813 struct gsm_mncc *modify = arg;
2814 struct msgb *msg = gsm48_msgb_alloc();
2815 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2816
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2818
2819 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002820 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002821 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002822 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823
2824 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2825
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002826 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827}
2828
2829static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2830{
2831 struct gsm_mncc *notify = arg;
2832 struct msgb *msg = gsm48_msgb_alloc();
2833 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2834
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835 gh->msg_type = GSM48_MT_CC_NOTIFY;
2836
2837 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002838 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002839
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002840 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841}
2842
2843static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2844{
2845 struct gsm48_hdr *gh = msgb_l3(msg);
2846 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2847// struct tlv_parsed tp;
2848 struct gsm_mncc notify;
2849
2850 memset(&notify, 0, sizeof(struct gsm_mncc));
2851 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002852// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002854 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002856 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857}
2858
2859static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2860{
2861 struct gsm_mncc *user = arg;
2862 struct msgb *msg = gsm48_msgb_alloc();
2863 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2864
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865 gh->msg_type = GSM48_MT_CC_USER_INFO;
2866
2867 /* user-user */
2868 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002869 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002870 /* more data */
2871 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002872 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002874 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875}
2876
2877static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2878{
2879 struct gsm48_hdr *gh = msgb_l3(msg);
2880 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2881 struct tlv_parsed tp;
2882 struct gsm_mncc user;
2883
2884 memset(&user, 0, sizeof(struct gsm_mncc));
2885 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002886 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887 /* user-user */
2888 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2889 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002890 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2892 }
2893 /* more data */
2894 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2895 user.more = 1;
2896
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002897 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002898}
2899
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002900static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901{
2902 struct gsm_mncc *mode = arg;
2903
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002904 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2905 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002906}
2907
2908static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002909 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002910 int type;
2911 int (*rout) (struct gsm_trans *trans, void *arg);
2912} downstatelist[] = {
2913 /* mobile originating call establishment */
2914 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2915 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2916 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2917 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2918 {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 */
2919 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2920 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2921 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2922 /* mobile terminating call establishment */
2923 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2924 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2925 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2926 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2927 /* signalling during call */
2928 {SBIT(GSM_CSTATE_ACTIVE),
2929 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2930 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2931 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2932 {ALL_STATES,
2933 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2934 {ALL_STATES,
2935 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2936 {ALL_STATES,
2937 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2938 {SBIT(GSM_CSTATE_ACTIVE),
2939 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2940 {SBIT(GSM_CSTATE_ACTIVE),
2941 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2942 {SBIT(GSM_CSTATE_ACTIVE),
2943 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2944 {SBIT(GSM_CSTATE_ACTIVE),
2945 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2946 {SBIT(GSM_CSTATE_ACTIVE),
2947 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2948 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2949 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2950 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2951 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2952 {SBIT(GSM_CSTATE_ACTIVE),
2953 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2954 /* clearing */
2955 {SBIT(GSM_CSTATE_INITIATED),
2956 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2957 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2958 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2959 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2960 MNCC_REL_REQ, gsm48_cc_tx_release},
2961 /* special */
2962 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002963 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964};
2965
2966#define DOWNSLLEN \
2967 (sizeof(downstatelist) / sizeof(struct downstate))
2968
2969
Harald Welte76556372010-12-22 23:57:45 +01002970int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971{
Harald Welte1a6f7982009-08-09 18:52:33 +02002972 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002973 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002974 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002975 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976 struct gsm_mncc *data = arg, rel;
2977
Harald Welte04dc88f2010-12-22 21:45:05 +01002978 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2979
Harald Welte4bfdfe72009-06-10 23:11:52 +08002980 /* handle special messages */
2981 switch(msg_type) {
2982 case MNCC_BRIDGE:
2983 return tch_bridge(net, arg);
2984 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002985 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002987 return tch_recv_mncc(net, data->callref, 1);
2988 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002989 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002990 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002991 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01002992 /* Find callref */
2993 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002994 if (!trans) {
2995 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002996 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002997 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002998 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01002999 if (!trans->conn) {
3000 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003001 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003002 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003003 if (!trans->conn->lchan) {
3004 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3005 return 0;
3006 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003007 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3008 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003009 /* This should be LOGL_ERROR or NOTICE, but
3010 * unfortuantely it happens for a couple of frames at
3011 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003012 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003013 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003014 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003015 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003016 switch (bts->type) {
3017 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003018 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003019 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003020 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003021 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003022 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003023 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003024 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003025 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003026 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003027 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003028 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003029 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003030 }
3031 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003032 }
3033
3034 memset(&rel, 0, sizeof(struct gsm_mncc));
3035 rel.callref = data->callref;
3036
3037 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003038 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003039
3040 /* Callref unknown */
3041 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003042 struct gsm_subscriber *subscr;
3043
Harald Welte4a3464c2009-07-04 10:11:24 +02003044 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003045 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3046 "Received '%s' from MNCC with "
3047 "unknown callref %d\n", data->called.number,
3048 get_mncc_name(msg_type), data->callref);
3049 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003050 return mncc_release_ind(net, NULL, data->callref,
3051 GSM48_CAUSE_LOC_PRN_S_LU,
3052 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003053 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003054 if (!data->called.number[0] && !data->imsi[0]) {
3055 DEBUGP(DCC, "(bts - trx - ts - ti) "
3056 "Received '%s' from MNCC with "
3057 "no number or IMSI\n", get_mncc_name(msg_type));
3058 /* Invalid number */
3059 return mncc_release_ind(net, NULL, data->callref,
3060 GSM48_CAUSE_LOC_PRN_S_LU,
3061 GSM48_CC_CAUSE_INV_NR_FORMAT);
3062 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003064 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003065 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003066 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003067 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003068 subscr = subscr_get_by_imsi(net->subscr_group,
3069 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003070
3071 /* update the subscriber we deal with */
3072 log_set_context(BSC_CTX_SUBSCR, subscr);
3073
Harald Welte4bfdfe72009-06-10 23:11:52 +08003074 /* If subscriber is not found */
3075 if (!subscr) {
3076 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3077 "Received '%s' from MNCC with "
3078 "unknown subscriber %s\n", data->called.number,
3079 get_mncc_name(msg_type), data->called.number);
3080 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003081 return mncc_release_ind(net, NULL, data->callref,
3082 GSM48_CAUSE_LOC_PRN_S_LU,
3083 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084 }
3085 /* If subscriber is not "attached" */
3086 if (!subscr->lac) {
3087 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3088 "Received '%s' from MNCC with "
3089 "detached subscriber %s\n", data->called.number,
3090 get_mncc_name(msg_type), data->called.number);
3091 subscr_put(subscr);
3092 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003093 return mncc_release_ind(net, NULL, data->callref,
3094 GSM48_CAUSE_LOC_PRN_S_LU,
3095 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096 }
3097 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003098 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003099 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003100 DEBUGP(DCC, "No memory for trans.\n");
3101 subscr_put(subscr);
3102 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003103 mncc_release_ind(net, NULL, data->callref,
3104 GSM48_CAUSE_LOC_PRN_S_LU,
3105 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106 return -ENOMEM;
3107 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003108 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003109 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003110
Harald Welte4bfdfe72009-06-10 23:11:52 +08003111 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003112 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003113 /* find transaction with this subscriber already paging */
3114 llist_for_each_entry(transt, &net->trans_list, entry) {
3115 /* Transaction of our lchan? */
3116 if (transt == trans ||
3117 transt->subscr != subscr)
3118 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003119 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003120 "Received '%s' from MNCC with "
3121 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003122 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003123 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003124 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003125 subscr_put(subscr);
3126 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003127 return 0;
3128 }
3129 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003130 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003131
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003132 /* Request a channel */
3133 trans->paging_request = subscr_request_channel(subscr,
3134 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3135 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003136 if (!trans->paging_request) {
3137 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3138 subscr_put(subscr);
3139 trans_free(trans);
3140 return 0;
3141 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003142 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143 return 0;
3144 }
3145 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003146 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003147 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003148 } else {
3149 /* update the subscriber we deal with */
3150 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003152
3153 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003154 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003155
3156 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003157 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003158 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003159 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003160 (trans->subscr)?(trans->subscr->extension):"-",
3161 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003162 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3163 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003164 if (msg_type == MNCC_REL_REQ)
3165 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3166 else
3167 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3168 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003169 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003170 return rc;
3171 }
3172
3173 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3174 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003175 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003176 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003177 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003178 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003179 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003180
3181 /* Find function for current state and message */
3182 for (i = 0; i < DOWNSLLEN; i++)
3183 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003184 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003185 break;
3186 if (i == DOWNSLLEN) {
3187 DEBUGP(DCC, "Message unhandled at this state.\n");
3188 return 0;
3189 }
3190
3191 rc = downstatelist[i].rout(trans, arg);
3192
3193 return rc;
3194}
3195
3196
3197static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003198 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003199 int type;
3200 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3201} datastatelist[] = {
3202 /* mobile originating call establishment */
3203 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3204 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3205 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3206 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3207 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3208 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3209 /* mobile terminating call establishment */
3210 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3211 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3212 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3213 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003214 {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 +08003215 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3216 /* signalling during call */
3217 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3218 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3219 {SBIT(GSM_CSTATE_ACTIVE),
3220 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3221 {ALL_STATES,
3222 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3223 {ALL_STATES,
3224 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3225 {ALL_STATES,
3226 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3227 {SBIT(GSM_CSTATE_ACTIVE),
3228 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3229 {SBIT(GSM_CSTATE_ACTIVE),
3230 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3231 {SBIT(GSM_CSTATE_ACTIVE),
3232 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3233 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3234 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3235 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3236 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3237 {SBIT(GSM_CSTATE_ACTIVE),
3238 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3239 /* clearing */
3240 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3241 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3242 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3243 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3244 {ALL_STATES, /* 5.4.3.4 */
3245 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3246};
3247
3248#define DATASLLEN \
3249 (sizeof(datastatelist) / sizeof(struct datastate))
3250
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003251static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003252{
3253 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003254 uint8_t msg_type = gh->msg_type & 0xbf;
3255 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003256 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003257 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003258
Harald Welte4bfdfe72009-06-10 23:11:52 +08003259 if (msg_type & 0x80) {
3260 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3261 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003262 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003263
Harald Welte4bfdfe72009-06-10 23:11:52 +08003264 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003265 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003266
Harald Welte6f5aee02009-07-23 21:21:14 +02003267 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003268 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003269 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003270 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003271 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3272 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003273
3274 /* Create transaction */
3275 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003276 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003277 "creating new trans.\n", transaction_id);
3278 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003279 trans = trans_alloc(conn->bts->network, conn->subscr,
3280 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003281 transaction_id, new_callref++);
3282 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003283 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003284 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003285 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003286 GSM48_MT_CC_RELEASE_COMPL);
3287 return -ENOMEM;
3288 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003289 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003290 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003291 }
3292
3293 /* find function for current state and message */
3294 for (i = 0; i < DATASLLEN; i++)
3295 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003296 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003297 break;
3298 if (i == DATASLLEN) {
3299 DEBUGP(DCC, "Message unhandled at this state.\n");
3300 return 0;
3301 }
3302
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003303 assert(trans->subscr);
3304
Harald Welte4bfdfe72009-06-10 23:11:52 +08003305 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003306
3307 return rc;
3308}
3309
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003310/* Create a dummy to wait five seconds */
3311static void release_anchor(struct gsm_subscriber_connection *conn)
3312{
3313 if (!conn->anch_operation)
3314 return;
3315
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003316 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003317 talloc_free(conn->anch_operation);
3318 conn->anch_operation = NULL;
3319}
3320
3321static void anchor_timeout(void *_data)
3322{
3323 struct gsm_subscriber_connection *con = _data;
3324
3325 release_anchor(con);
3326 msc_release_connection(con);
3327}
3328
3329int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3330{
3331 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3332 if (!conn->anch_operation)
3333 return -1;
3334
3335 conn->anch_operation->timeout.data = conn;
3336 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003337 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003338 return 0;
3339}
3340
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003341/* here we get data from the BSC level... */
3342int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003343{
3344 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003345 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003346 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003347
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003348 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003349 if (silent_call_reroute(conn, msg))
3350 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003351
3352 switch (pdisc) {
3353 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003354 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003355 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003356 break;
3357 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003358 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003359 break;
3360 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003361 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003362 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003363 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003364 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003365 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003366 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003367 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003368 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003369 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3370 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003371 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003372 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003373 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003374 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003375 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003376 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003377 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3378 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003379 break;
3380 }
3381
3382 return rc;
3383}
Harald Welte8470bf22008-12-25 23:28:35 +00003384
Harald Welte805f6442009-07-28 18:25:29 +02003385/*
3386 * This will be ran by the linker when loading the DSO. We use it to
3387 * do system initialization, e.g. registration of signal handlers.
3388 */
3389static __attribute__((constructor)) void on_dso_load_0408(void)
3390{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003391 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003392}