blob: 58f97f42d805ded1fd052699b027b18674499b73 [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 Freytherc42ad8b2011-04-18 17:04:00 +020070int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
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
Jan Luebbe06513f22009-08-12 12:48:00 +0200258 switch (subscriber->net->auth_policy) {
259 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 Freyther68884aa2010-03-23 06:41:45 +0100272static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
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 Freyther40494552010-06-28 17:09:29 +0800283 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000284}
285
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100286static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000287{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800288 if (conn->loc_operation)
289 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100290 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000291
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100292 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200293 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000294}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000295
Sylvain Munaut267fba02009-12-24 00:28:01 +0100296static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
297 struct msgb *msg, void *data, void *param)
298{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800299 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100300 int rc = 0;
301
302 switch (event) {
303 case GSM_SECURITY_AUTH_FAILED:
304 release_loc_updating_req(conn);
305 break;
306
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200307 case GSM_SECURITY_ALREADY:
308 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
309 "UPDATING after CM SERVICE REQUEST\n");
310 /* fall through */
311
Sylvain Munaut267fba02009-12-24 00:28:01 +0100312 case GSM_SECURITY_NOAVAIL:
313 case GSM_SECURITY_SUCCEEDED:
314 /* We're all good */
315 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800316 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800317 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100318 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800319 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100320 }
321
322 /* call subscr_update after putting the loc_upd_acc
323 * in the transmit queue, since S_SUBSCR_ATTACHED might
324 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800325 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100326 GSM_SUBSCRIBER_UPDATE_ATTACHED);
327
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100328 /*
329 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
330 * MS needs to respond with a TMSI REALLOCATION COMPLETE
331 * (even if the TMSI is the same).
332 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100333 break;
334
335 default:
336 rc = -EINVAL;
337 };
338
339 return rc;
340}
341
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100342static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000343{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100344 if (!conn->loc_operation)
345 return 0;
346
Sylvain Munaut267fba02009-12-24 00:28:01 +0100347 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800348 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100349 conn->loc_operation->key_seq,
350 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000351 return 0;
352}
353
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800354void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000355{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800356 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800357
358 /* avoid someone issuing a clear */
359 conn->in_release = 1;
360
Holger Freyther7c19f742009-06-06 13:54:35 +0000361 /*
362 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800363 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000364 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800365 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100366
367 /* We might need to cancel the paging response or such. */
368 if (conn->sec_operation && conn->sec_operation->cb) {
369 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
370 NULL, conn, conn->sec_operation->cb_data);
371 }
372
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800373 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800374 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000375
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100376 /*
377 * Free all transactions that are associated with the released
378 * connection. The transaction code will inform the CC or SMS
379 * facilities that will send the release indications. As part of
380 * the CC REL_IND the remote leg might be released and this will
381 * trigger the call to trans_free. This is something the llist
382 * macro can not handle and we will need to re-iterate the list.
383 *
384 * TODO: Move the trans_list into the subscriber connection and
385 * create a pending list for MT transactions. These exist before
386 * we have a subscriber connection.
387 */
388restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800389 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100390 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200391 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100392 goto restart;
393 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800394 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000395}
396
Harald Welte371efe52010-12-22 23:17:50 +0100397void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
398{
399 struct gsm_trans *trans, *temp;
400
401 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
402
403 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100404 if (trans->protocol == protocol) {
405 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100406 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100407 }
Harald Welte371efe52010-12-22 23:17:50 +0100408 }
409}
410
Holger Freyther429e7762008-12-30 13:28:30 +0000411/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200412int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000413{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800414 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800415 struct msgb *msg;
416
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200417 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800418
419 msg = gsm48_create_loc_upd_rej(cause);
420 if (!msg) {
421 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
422 return -1;
423 }
Harald Welte52b1f982008-12-23 20:25:15 +0000424
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800425 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000426
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100427 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100428 "LAC=%u BTS=%u\n", conn->subscr ?
429 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800430 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100431
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800432 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000433}
434
435/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200436int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000437{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800438 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000439 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000440 struct gsm48_hdr *gh;
441 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200442 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000443
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800444 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000445
446 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
447 gh->proto_discr = GSM48_PDISC_MM;
448 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
449
450 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100451 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000452 bts->network->network_code, bts->location_area_code);
453
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200454 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200455 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000456
457 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
458
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200459 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100460
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800461 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000462}
463
Harald Weltebf5e8df2009-02-03 12:59:45 +0000464/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200465static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000466{
467 struct msgb *msg = gsm48_msgb_alloc();
468 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000469
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800470 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000471
472 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
473 gh->proto_discr = GSM48_PDISC_MM;
474 gh->msg_type = GSM48_MT_MM_ID_REQ;
475 gh->data[0] = id_type;
476
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800477 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000478}
479
Harald Welte231ad4f2008-12-27 11:15:38 +0000480
Harald Weltebf5e8df2009-02-03 12:59:45 +0000481/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800482static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000483{
484 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000485 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200486 struct gsm_bts *bts = lchan->ts->trx->bts;
487 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200488 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200489 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000490
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200491 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000492 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000493 mi_type, mi_string);
494
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200495 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100496
Harald Welte75a983f2008-12-27 21:34:06 +0000497 switch (mi_type) {
498 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200499 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100500 if (!conn->subscr) {
501 conn->subscr = subscr_get_by_imsi(net, mi_string);
502 if (!conn->subscr)
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200503 conn->subscr = subscr_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200504 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100505 if (conn->loc_operation)
506 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000507 break;
508 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000509 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000510 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100511 if (conn->subscr) {
512 db_subscriber_assoc_imei(conn->subscr, mi_string);
513 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200514 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100515 if (conn->loc_operation)
516 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000517 break;
518 }
Holger Freyther73487a22008-12-31 18:53:57 +0000519
520 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100521 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000522}
523
Harald Welte255539c2008-12-28 02:26:27 +0000524
525static void loc_upd_rej_cb(void *data)
526{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100527 struct gsm_subscriber_connection *conn = data;
528 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100529 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000530
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100531 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800532 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800533 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000534}
535
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100536static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000537{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100538 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
539 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200540 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000541}
542
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200543static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000544{
545 switch (type) {
546 case GSM48_LUPD_NORMAL:
547 return "NORMAL";
548 case GSM48_LUPD_PERIODIC:
Alexander Chemerisa3d41c92013-10-04 02:42:26 +0200549 return "PERIODIC";
Harald Welte2a139372009-02-22 21:14:55 +0000550 case GSM48_LUPD_IMSI_ATT:
551 return "IMSI ATTACH";
552 default:
553 return "UNKNOWN";
554 }
555}
556
Harald Weltebf5e8df2009-02-03 12:59:45 +0000557/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800558static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000559{
Harald Welte8470bf22008-12-25 23:28:35 +0000560 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000561 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800562 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800563 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200564 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200565 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000566
Harald Welte8470bf22008-12-25 23:28:35 +0000567 lu = (struct gsm48_loc_upd_req *) gh->data;
568
569 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000570
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200571 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000572
Harald Weltea0368542009-06-27 02:58:43 +0200573 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000574 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000575
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200576 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100577
Harald Welte24ff6ee2009-12-22 00:41:05 +0100578 switch (lu->type) {
579 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200580 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100581 break;
582 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200583 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100584 break;
585 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200586 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100587 break;
588 }
589
Holger Freythereaf04692009-06-06 13:54:44 +0000590 /*
591 * Pseudo Spoof detection: Just drop a second/concurrent
592 * location updating request.
593 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100594 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200595 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100596 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800597 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000598 return 0;
599 }
600
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800601 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000602
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200603 conn->loc_operation->key_seq = lu->key_seq;
604
Harald Welte52b1f982008-12-23 20:25:15 +0000605 switch (mi_type) {
606 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200607 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000608 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200609 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100610 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000611
Jan Luebbe370b41d2009-08-12 10:19:34 +0200612 /* look up subscriber based on IMSI, create if not found */
613 subscr = subscr_get_by_imsi(bts->network, mi_string);
614 if (!subscr) {
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200615 subscr = subscr_create_subscriber(bts->network, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200616 }
Harald Welte4b634542008-12-27 01:55:51 +0000617 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000618 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200619 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000620 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200621 subscr = subscr_get_by_tmsi(bts->network,
622 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000623 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000624 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200625 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100626 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000627 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200628 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200629 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200630 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000631 break;
632 case GSM_MI_TYPE_IMEI:
633 case GSM_MI_TYPE_IMEISV:
634 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200635 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000636 break;
637 default:
Harald Weltea0368542009-06-27 02:58:43 +0200638 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000639 break;
640 }
641
Harald Welte24516ea2009-07-04 10:18:00 +0200642 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100643 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200644
Harald Welte4bfdfe72009-06-10 23:11:52 +0800645 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200646 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800647 /* FIXME: request id? close channel? */
648 return -EINVAL;
649 }
650
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100651 conn->subscr = subscr;
652 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000653
Harald Welte24516ea2009-07-04 10:18:00 +0200654 /* check if we can let the subscriber into our network immediately
655 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100656 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000657}
658
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100659/* Turn int into semi-octet representation: 98 => 0x89 */
660static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800661{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100662 uint8_t ret;
663
664 ret = value / 10;
665 ret |= (value % 10) << 4;
666
667 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800668}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100669
Harald Welte4bfdfe72009-06-10 23:11:52 +0800670
Harald Weltedb253af2008-12-30 17:56:55 +0000671/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800672int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000673{
674 struct msgb *msg = gsm48_msgb_alloc();
675 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800676 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100677 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200678 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200679 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100680
Harald Welte4bfdfe72009-06-10 23:11:52 +0800681 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100682 struct tm* gmt_time;
683 struct tm* local_time;
684 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100685 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000686
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800687 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000688
689 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
690 gh->proto_discr = GSM48_PDISC_MM;
691 gh->msg_type = GSM48_MT_MM_INFO;
692
693 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200694#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000695 name_len = strlen(net->name_long);
696 /* 10.5.3.5a */
697 ptr8 = msgb_put(msg, 3);
698 ptr8[0] = GSM48_IE_NAME_LONG;
699 ptr8[1] = name_len*2 +1;
700 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
701
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200702 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000703 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000704 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000705
706 /* FIXME: Use Cell Broadcast, not UCS-2, since
707 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200708#endif
709 name_len = (strlen(net->name_long)*7)/8;
710 name_pad = (8 - strlen(net->name_long)*7)%8;
711 if (name_pad > 0)
712 name_len++;
713 /* 10.5.3.5a */
714 ptr8 = msgb_put(msg, 3);
715 ptr8[0] = GSM48_IE_NAME_LONG;
716 ptr8[1] = name_len +1;
717 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
718
719 ptr8 = msgb_put(msg, name_len);
720 gsm_7bit_encode(ptr8, net->name_long);
721
Harald Weltedb253af2008-12-30 17:56:55 +0000722 }
723
724 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200725#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000726 name_len = strlen(net->name_short);
727 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200728 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200729 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000730 ptr8[1] = name_len*2 + 1;
731 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
732
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200733 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000734 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000735 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200736#endif
737 name_len = (strlen(net->name_short)*7)/8;
738 name_pad = (8 - strlen(net->name_short)*7)%8;
739 if (name_pad > 0)
740 name_len++;
741 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200742 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200743 ptr8[0] = GSM48_IE_NAME_SHORT;
744 ptr8[1] = name_len +1;
745 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
746
747 ptr8 = msgb_put(msg, name_len);
748 gsm_7bit_encode(ptr8, net->name_short);
749
Harald Weltedb253af2008-12-30 17:56:55 +0000750 }
751
Harald Weltedb253af2008-12-30 17:56:55 +0000752 /* Section 10.5.3.9 */
753 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100754 gmt_time = gmtime(&cur_t);
755
Harald Weltedb253af2008-12-30 17:56:55 +0000756 ptr8 = msgb_put(msg, 8);
757 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100758 ptr8[1] = bcdify(gmt_time->tm_year % 100);
759 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
760 ptr8[3] = bcdify(gmt_time->tm_mday);
761 ptr8[4] = bcdify(gmt_time->tm_hour);
762 ptr8[5] = bcdify(gmt_time->tm_min);
763 ptr8[6] = bcdify(gmt_time->tm_sec);
764
Harald Welte45f91712012-07-08 16:48:11 +0200765 if (bts->tz.override) {
766 /* Convert tz.hr and tz.mn to units */
767 if (bts->tz.hr < 0) {
768 tzunits = ((bts->tz.hr/-1)*4);
769 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100770 ptr8[7] = bcdify(tzunits);
771 /* Set negative time */
772 ptr8[7] |= 0x08;
773 }
774 else {
Harald Welte45f91712012-07-08 16:48:11 +0200775 tzunits = bts->tz.hr*4;
776 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100777 ptr8[7] = bcdify(tzunits);
778 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100779 /* Convert DST value */
780 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
781 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100782 }
783 else {
784 /* Need to get GSM offset and convert into 15 min units */
785 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
786 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200787#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100788 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200789#else
790#warning find a portable way to obtain the timezone offset
791 tzunits = 0;
792#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100793 if (tzunits < 0) {
794 tzunits = tzunits/-1;
795 ptr8[7] = bcdify(tzunits);
796 /* Flip it to negative */
797 ptr8[7] |= 0x08;
798 }
799 else
800 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100801
802 /* Does not support DST +2 */
803 if (local_time->tm_isdst)
804 dst = 1;
805 }
806
807 if (dst) {
808 ptr8 = msgb_put(msg, 3);
809 ptr8[0] = GSM48_IE_NET_DST;
810 ptr8[1] = 1;
811 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100812 }
Harald Weltedb253af2008-12-30 17:56:55 +0000813
Daniel Willmanneea93372009-08-13 03:42:07 +0200814 DEBUGP(DMM, "-> MM INFO\n");
815
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800816 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000817}
818
Harald Welte7984d5c2009-08-12 22:56:50 +0200819/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200820int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200821{
822 struct msgb *msg = gsm48_msgb_alloc();
823 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200824 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200825
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200826 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200827
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800828 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200829 gh->proto_discr = GSM48_PDISC_MM;
830 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
831
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100832 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200833
Harald Welte7984d5c2009-08-12 22:56:50 +0200834 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200835 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200836 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200837
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800838 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200839}
840
841/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800842int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200843{
844 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800845 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200846}
847
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100848/*
849 * At the 30C3 phones miss their periodic update
850 * interval a lot and then remain unreachable. In case
851 * we still know the TMSI we can just attach it again.
852 */
853static void implit_attach(struct gsm_subscriber_connection *conn)
854{
855 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
856 return;
857
858 subscr_update(conn->subscr, conn->bts,
859 GSM_SUBSCRIBER_UPDATE_ATTACHED);
860}
861
862
Sylvain Munautba87f452009-12-24 00:28:46 +0100863static int _gsm48_rx_mm_serv_req_sec_cb(
864 unsigned int hooknum, unsigned int event,
865 struct msgb *msg, void *data, void *param)
866{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800867 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100868 int rc = 0;
869
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200870 /* auth failed or succeeded, the timer was stopped */
871 conn->expire_timer_stopped = 1;
872
Sylvain Munautba87f452009-12-24 00:28:46 +0100873 switch (event) {
874 case GSM_SECURITY_AUTH_FAILED:
875 /* Nothing to do */
876 break;
877
878 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200879 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800880 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100881 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100882 break;
883
884 case GSM_SECURITY_SUCCEEDED:
885 /* nothing to do. CIPHER MODE COMMAND is
886 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100887 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100888 break;
889
890 default:
891 rc = -EINVAL;
892 };
893
894 return rc;
895}
896
Harald Welte4ed0e922009-01-10 03:17:30 +0000897/*
898 * Handle CM Service Requests
899 * a) Verify that the packet is long enough to contain the information
900 * we require otherwsie reject with INCORRECT_MESSAGE
901 * b) Try to parse the TMSI. If we do not have one reject
902 * c) Check that we know the subscriber with the TMSI otherwise reject
903 * with a HLR cause
904 * d) Set the subscriber on the gsm_lchan and accept
905 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800906static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000907{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200908 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200909 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000910
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800911 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000912 struct gsm_subscriber *subscr;
913 struct gsm48_hdr *gh = msgb_l3(msg);
914 struct gsm48_service_request *req =
915 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800916 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200917 uint8_t classmark2_len = gh->data[1];
918 uint8_t *classmark2 = gh->data+2;
919 uint8_t mi_len = *(classmark2 + classmark2_len);
920 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000921
Harald Weltec9e02182009-05-01 19:07:53 +0000922 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000923 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000924 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800925 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000926 GSM48_REJECT_INCORRECT_MESSAGE);
927 }
928
929 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000930 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800931 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000932 GSM48_REJECT_INCORRECT_MESSAGE);
933 }
934
Harald Weltec9e02182009-05-01 19:07:53 +0000935 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000936 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000937 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800938 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000939 GSM48_REJECT_INCORRECT_MESSAGE);
940 }
941
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200942 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000943 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000944 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000945
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200946 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100947
Harald Welte3ac7f102009-08-10 10:12:45 +0200948 if (is_siemens_bts(bts))
949 send_siemens_mrpci(msg->lchan, classmark2-1);
950
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200951 subscr = subscr_get_by_tmsi(bts->network,
952 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000953
Harald Welte2a139372009-02-22 21:14:55 +0000954 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000955 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800956 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000957 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
958
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800959 if (!conn->subscr)
960 conn->subscr = subscr;
961 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100962 subscr_put(subscr); /* lchan already has a ref, don't need another one */
963 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000964 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
965 subscr_put(subscr);
966 }
967
Harald Weltec2e302d2009-07-05 14:08:13 +0200968 subscr->equipment.classmark2_len = classmark2_len;
969 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
970 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000971
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200972 /* we will send a MM message soon */
973 conn->expire_timer_stopped = 1;
974
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800975 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100976 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000977}
978
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200979static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000980{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200981 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000982 struct gsm48_hdr *gh = msgb_l3(msg);
983 struct gsm48_imsi_detach_ind *idi =
984 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200985 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200986 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800987 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000988
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200989 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +0100990 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +0000991 mi_type, mi_string);
992
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200993 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100994
Harald Welte2a139372009-02-22 21:14:55 +0000995 switch (mi_type) {
996 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100997 DEBUGPC(DMM, "\n");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200998 subscr = subscr_get_by_tmsi(bts->network,
999 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001000 break;
1001 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001002 DEBUGPC(DMM, "\n");
Harald Welte9176bd42009-07-23 18:46:00 +02001003 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001004 break;
1005 case GSM_MI_TYPE_IMEI:
1006 case GSM_MI_TYPE_IMEISV:
1007 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001008 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001009 break;
1010 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001011 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001012 break;
1013 }
1014
Holger Freyther4a49e772009-04-12 05:37:29 +00001015 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001016 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001017 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001018 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001019
1020 subscr->equipment.classmark1 = idi->classmark1;
1021 db_sync_equipment(&subscr->equipment);
1022
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001023 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001024 } else
Harald Welte2a139372009-02-22 21:14:55 +00001025 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1026
Harald Welte31981a02009-12-20 09:58:40 +01001027 /* FIXME: iterate over all transactions and release them,
1028 * imagine an IMSI DETACH happening during an active call! */
1029
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001030 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001031 return 0;
1032}
1033
Harald Welted2a7f5a2009-06-05 20:08:20 +00001034static int gsm48_rx_mm_status(struct msgb *msg)
1035{
1036 struct gsm48_hdr *gh = msgb_l3(msg);
1037
1038 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1039
1040 return 0;
1041}
1042
Sylvain Munaut30a15382009-12-24 00:27:26 +01001043/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001044static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001045{
1046 struct gsm48_hdr *gh = msgb_l3(msg);
1047 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001048 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001049
Sylvain Munautc593cf12010-06-10 23:51:41 +02001050 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001051 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001052
1053 /* Safety check */
1054 if (!conn->sec_operation) {
1055 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1056 return -EIO;
1057 }
1058
1059 /* Validate SRES */
1060 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001061 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001062 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001063
1064 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001065 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001066
Sylvain Munaut30a15382009-12-24 00:27:26 +01001067 if (cb)
1068 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001069 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001070
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001071 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001072 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001073 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001074 }
1075
Sylvain Munautc593cf12010-06-10 23:51:41 +02001076 DEBUGPC(DMM, "OK\n");
1077
Sylvain Munaut30a15382009-12-24 00:27:26 +01001078 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001079 return gsm0808_cipher_mode(conn, net->a5_encryption,
1080 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001081}
1082
Harald Weltebf5e8df2009-02-03 12:59:45 +00001083/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001084static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001085{
1086 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001087 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001088
1089 switch (gh->msg_type & 0xbf) {
1090 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001091 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001092 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001093 break;
1094 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001095 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001096 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001097 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001098 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001099 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001100 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001101 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001102 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001103 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001104 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001105 conn->subscr ?
1106 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001107 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001108 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001109 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001110 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001111 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001112 break;
1113 case GSM48_MT_MM_CM_REEST_REQ:
1114 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1115 break;
1116 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001117 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001118 break;
1119 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001120 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001121 gh->msg_type);
1122 break;
1123 }
1124
1125 return rc;
1126}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001127
Harald Welte2d35ae62009-02-06 12:02:13 +00001128/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001129static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001130{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001131 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001132 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001133 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001134 uint8_t *classmark2_lv = gh->data + 1;
1135 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001136 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001137 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001138 int rc = 0;
1139
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001140 resp = (struct gsm48_pag_resp *) &gh->data[0];
1141 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1142 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001143 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1144 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001145
Harald Weltefe18d8f2009-02-22 21:14:24 +00001146 switch (mi_type) {
1147 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001148 subscr = subscr_get_by_tmsi(bts->network,
1149 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001150 break;
1151 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001152 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001153 break;
1154 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001155
1156 if (!subscr) {
1157 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001158 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001159 return -EINVAL;
1160 }
1161 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001162 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001163
Harald Weltec2e302d2009-07-05 14:08:13 +02001164 subscr->equipment.classmark2_len = *classmark2_lv;
1165 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1166 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001167
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001168 /* We received a paging */
1169 conn->expire_timer_stopped = 1;
1170
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001171 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001172 return rc;
1173}
1174
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001175static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001176{
1177 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001178 uint8_t apdu_id_flags;
1179 uint8_t apdu_len;
1180 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001181
1182 apdu_id_flags = gh->data[0];
1183 apdu_len = gh->data[1];
1184 apdu_data = gh->data+2;
1185
Harald Welte70f92052012-07-16 13:22:19 +02001186 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001187 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001188
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001189 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001190}
1191
Harald Weltebf5e8df2009-02-03 12:59:45 +00001192/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001193static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001194{
1195 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001196 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001197
1198 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001199 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001200 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001201 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001202 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001203 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001204 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001205 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001206 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001207 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001208 break;
1209 }
1210
Harald Welte2d35ae62009-02-06 12:02:13 +00001211 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001212}
1213
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001214int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1215 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001216{
1217 struct msgb *msg = gsm48_msgb_alloc();
1218 struct gsm48_hdr *gh;
1219
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001220 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001221
1222 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1223 apdu_id, apdu_len);
1224
1225 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1226 gh->proto_discr = GSM48_PDISC_RR;
1227 gh->msg_type = GSM48_MT_RR_APP_INFO;
1228 gh->data[0] = apdu_id;
1229 gh->data[1] = apdu_len;
1230 memcpy(gh->data+2, apdu, apdu_len);
1231
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001232 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001233}
1234
Harald Welte4bc90a12008-12-27 16:32:52 +00001235/* Call Control */
1236
Harald Welte7584aea2009-02-11 11:44:12 +00001237/* The entire call control code is written in accordance with Figure 7.10c
1238 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1239 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1240 * it for voice */
1241
Harald Welte4bfdfe72009-06-10 23:11:52 +08001242static void new_cc_state(struct gsm_trans *trans, int state)
1243{
1244 if (state > 31 || state < 0)
1245 return;
1246
1247 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001248 gsm48_cc_state_name(trans->cc.state),
1249 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001250
Harald Weltedcaf5652009-07-23 18:56:43 +02001251 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001252}
1253
1254static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001255{
1256 struct msgb *msg = gsm48_msgb_alloc();
1257 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001258 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001259
Harald Welte4bc90a12008-12-27 16:32:52 +00001260 gh->msg_type = GSM48_MT_CC_STATUS;
1261
1262 cause = msgb_put(msg, 3);
1263 cause[0] = 2;
1264 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1265 cause[2] = 0x80 | 30; /* response to status inquiry */
1266
1267 call_state = msgb_put(msg, 1);
1268 call_state[0] = 0xc0 | 0x00;
1269
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001270 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001271}
1272
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001273static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001274 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001275{
1276 struct msgb *msg = gsm48_msgb_alloc();
1277 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1278
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001279 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001280
Harald Welte6f4b7532008-12-29 00:39:37 +00001281 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001282 gh->msg_type = msg_type;
1283
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001284 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001285}
1286
Harald Welte4bfdfe72009-06-10 23:11:52 +08001287static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1288{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001289 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001290 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001291 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001292 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001293 }
1294}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001295
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1297 int msg_type, struct gsm_mncc *mncc)
1298{
1299 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001300 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001301
1302 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001303 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001304 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001306 trans->conn->lchan->ts->trx->bts->nr,
1307 trans->conn->lchan->ts->trx->nr,
1308 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001309 (trans->subscr)?(trans->subscr->extension):"-",
1310 get_mncc_name(msg_type));
1311 else
1312 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1313 "Sending '%s' to MNCC.\n",
1314 (trans->subscr)?(trans->subscr->extension):"-",
1315 get_mncc_name(msg_type));
1316 else
1317 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1318 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1319
1320 mncc->msg_type = msg_type;
1321
Harald Welte966636f2009-06-26 19:39:35 +02001322 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001323 if (!msg)
1324 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001325
1326 data = msgb_put(msg, sizeof(struct gsm_mncc));
1327 memcpy(data, mncc, sizeof(struct gsm_mncc));
1328
Harald Welte54209c22010-12-22 23:43:29 +01001329 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001330
1331 return 0;
1332}
1333
1334int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001335 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336{
1337 struct gsm_mncc rel;
1338
Harald Welte92f70c52009-06-12 01:54:08 +08001339 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001341 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001342 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1343 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001344 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1345}
1346
Harald Weltedcaf5652009-07-23 18:56:43 +02001347/* Call Control Specific transaction release.
1348 * gets called by trans_free, DO NOT CALL YOURSELF! */
1349void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351 gsm48_stop_cc_timer(trans);
1352
1353 /* send release to L4, if callref still exists */
1354 if (trans->callref) {
1355 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001356 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001357 GSM48_CAUSE_LOC_PRN_S_LU,
1358 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001360 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001362 if (trans->conn)
1363 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364}
1365
1366static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001367
Harald Welte09e38af2009-02-16 22:52:23 +00001368/* call-back from paging the B-end of the connection */
1369static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001370 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001371{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001372 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001373 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001374 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001376
Harald Welte09e38af2009-02-16 22:52:23 +00001377 if (hooknum != GSM_HOOK_RR_PAGING)
1378 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001379
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001380 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381 if (!net) {
1382 DEBUGP(DCC, "Error Network not set!\n");
1383 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001384 }
Harald Welte7584aea2009-02-11 11:44:12 +00001385
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 /* check all tranactions (without lchan) for subscriber */
1387 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001388 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001389 continue;
1390 switch (event) {
1391 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001392 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 break;
1394 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001395 transt->subscr->extension);
1396 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001397 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001398 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001399 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001400 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001401 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402 }
1403 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001404 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001405 break;
1406 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001407 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001409 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001410 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001411 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001412 mncc_release_ind(transt->subscr->net, transt,
1413 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001414 GSM48_CAUSE_LOC_PRN_S_LU,
1415 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001416 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001417 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001418 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001419 break;
1420 }
1421 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001422
1423 talloc_free(paging_request);
1424
1425 /*
1426 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1427 * failure and then the subscr will be poke again. This needs a lot of fixing
1428 * in the subscriber queue code.
1429 */
1430 if (!found && conn)
1431 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001432 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001433}
Harald Welte7584aea2009-02-11 11:44:12 +00001434
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001435static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001436
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001437/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001438static int switch_for_handover(struct gsm_lchan *old_lchan,
1439 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001440{
1441 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001442
1443 if (ipacc_rtp_direct) {
1444 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1445 return 0;
1446 }
1447
1448 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001449 new_rs = new_lchan->abis_ip.rtp_socket;
1450 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001451
1452 if (!new_rs) {
1453 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1454 return -EIO;
1455 }
1456
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001457 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001458
1459 if (!old_rs) {
1460 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1461 return -EIO;
1462 }
1463
1464 /* copy rx_action and reference to other sock */
1465 new_rs->rx_action = old_rs->rx_action;
1466 new_rs->tx_action = old_rs->tx_action;
1467 new_rs->transmit = old_rs->transmit;
1468
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001469 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001470 case RTP_PROXY:
1471 other_rs = old_rs->proxy.other_sock;
1472 rtp_socket_proxy(new_rs, other_rs);
1473 /* delete reference to other end socket to prevent
1474 * rtp_socket_free() from removing the inverse reference */
1475 old_rs->proxy.other_sock = NULL;
1476 break;
1477 case RTP_RECV_UPSTREAM:
1478 new_rs->receive = old_rs->receive;
1479 break;
1480 case RTP_NONE:
1481 break;
1482 }
1483
1484 return 0;
1485}
1486
Harald Welte805f6442009-07-28 18:25:29 +02001487/* some other part of the code sends us a signal */
1488static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1489 void *handler_data, void *signal_data)
1490{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001491 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001492 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001493 struct gsm_network *net;
1494 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001495
1496 if (subsys != SS_ABISIP)
1497 return 0;
1498
1499 /* in case we use direct BTS-to-BTS RTP */
1500 if (ipacc_rtp_direct)
1501 return 0;
1502
Harald Welte805f6442009-07-28 18:25:29 +02001503 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001504 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001505 /* in case we don't use direct BTS-to-BTS RTP */
1506 /* the BTS has successfully bound a TCH to a local ip/port,
1507 * which means we can connect our UDP socket to it */
1508 if (lchan->abis_ip.rtp_socket) {
1509 rtp_socket_free(lchan->abis_ip.rtp_socket);
1510 lchan->abis_ip.rtp_socket = NULL;
1511 }
1512
1513 lchan->abis_ip.rtp_socket = rtp_socket_create();
1514 if (!lchan->abis_ip.rtp_socket)
1515 return -EIO;
1516
1517 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1518 lchan->abis_ip.bound_ip,
1519 lchan->abis_ip.bound_port);
1520 if (rc < 0)
1521 return -EIO;
1522
Harald Welteda7ab742009-12-19 22:23:05 +01001523 /* check if any transactions on this lchan still have
1524 * a tch_recv_mncc request pending */
1525 net = lchan->ts->trx->bts->network;
1526 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001527 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001528 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1529 tch_recv_mncc(net, trans->callref, 1);
1530 }
1531 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001532
1533 /*
1534 * TODO: this appears to be too early? Why not until after
1535 * the handover detect or the handover complete?
1536 *
1537 * Do we have a handover pending for this new lchan? In that
1538 * case re-route the audio from the old channel to the new one.
1539 */
1540 old_lchan = bsc_handover_pending(lchan);
1541 if (old_lchan)
1542 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001543 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001544 case S_ABISIP_DLCX_IND:
1545 /* the BTS tells us a RTP stream has been disconnected */
1546 if (lchan->abis_ip.rtp_socket) {
1547 rtp_socket_free(lchan->abis_ip.rtp_socket);
1548 lchan->abis_ip.rtp_socket = NULL;
1549 }
1550
1551 break;
Harald Welte805f6442009-07-28 18:25:29 +02001552 }
1553
1554 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001555}
1556
Harald Welte49f48b82009-02-17 15:29:33 +00001557/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001558static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001559{
Harald Welte11fa29c2009-02-19 17:24:39 +00001560 struct gsm_bts *bts = lchan->ts->trx->bts;
1561 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001562 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001563
Harald Welte11fa29c2009-02-19 17:24:39 +00001564 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1565 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1566 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1567
1568 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001569 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001570 return -EINVAL;
1571 }
Harald Welteda7ab742009-12-19 22:23:05 +01001572
1573 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001574 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001575 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001576 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001577 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001578 if (!lchan->abis_ip.rtp_socket) {
1579 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1580 "lchan\n");
1581 return -EIO;
1582 }
1583 if (!remote_lchan->abis_ip.rtp_socket) {
1584 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1585 "remote_lchan\n");
1586 return -EIO;
1587 }
1588
Harald Welte805f6442009-07-28 18:25:29 +02001589 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001590 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001591 if (rc < 0)
1592 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001593 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001594 if (rc < 0)
1595 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001596 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301597 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1598 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001599 } else {
1600 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301601 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1602 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301603 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001604 if (rc < 0)
1605 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301606 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1607 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301608 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001609 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001610 break;
1611 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001612 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001613 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001614 trau_mux_map_lchan(lchan, remote_lchan);
1615 break;
1616 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001617 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001618 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001619 }
Harald Welte49f48b82009-02-17 15:29:33 +00001620
1621 return 0;
1622}
1623
Harald Welte4bfdfe72009-06-10 23:11:52 +08001624/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001625static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001626{
Harald Weltedcaf5652009-07-23 18:56:43 +02001627 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1628 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001629
Harald Welte4bfdfe72009-06-10 23:11:52 +08001630 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001631 return -EIO;
1632
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001633 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 return -EIO;
1635
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001636 /* Which subscriber do we want to track trans1 or trans2? */
1637 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1638
Harald Welte4bfdfe72009-06-10 23:11:52 +08001639 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001640 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001641}
1642
Harald Welteda7ab742009-12-19 22:23:05 +01001643/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001644static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001645{
1646 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001647 struct gsm_lchan *lchan;
1648 struct gsm_bts *bts;
1649 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001650
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001652 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 if (!trans)
1654 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001655 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001657
1658 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001659 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001660 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001661
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001662 /* store receive state */
1663 trans->tch_recv = enable;
1664
Harald Welteda7ab742009-12-19 22:23:05 +01001665 switch (bts->type) {
1666 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001667 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001668 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001669 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001670 return -EINVAL;
1671 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001672 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1673 * will not be connected to our RTP proxy and the socket will
1674 * not be assigned to the application interface. This method
1675 * will be called again, once the audio socket is created and
1676 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001677 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001678 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1679 return 0;
1680 }
1681 if (enable) {
1682 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001683 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001684 if (rc < 0)
1685 return rc;
1686 /* assign socket to application interface */
1687 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1688 net, callref);
1689 } else
1690 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1691 net, 0);
1692 break;
1693 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001694 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001695 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001696 /* In case we don't have a TCH with correct mode, the TRAU muxer
1697 * will not be asigned to the application interface. This is
1698 * performed by switch_trau_mux() after successful handover or
1699 * assignment. */
1700 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1701 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1702 return 0;
1703 }
Harald Welteda7ab742009-12-19 22:23:05 +01001704 if (enable)
1705 return trau_recv_lchan(lchan, callref);
1706 return trau_mux_unmap(NULL, callref);
1707 break;
1708 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001709 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001710 return -EINVAL;
1711 }
1712
1713 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714}
1715
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1717{
1718 DEBUGP(DCC, "-> STATUS ENQ\n");
1719 return gsm48_cc_tx_status(trans, msg);
1720}
1721
1722static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1723static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1724
1725static void gsm48_cc_timeout(void *arg)
1726{
1727 struct gsm_trans *trans = arg;
1728 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001729 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1730 int mo_location = GSM48_CAUSE_LOC_USER;
1731 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1732 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001733 struct gsm_mncc mo_rel, l4_rel;
1734
1735 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1736 mo_rel.callref = trans->callref;
1737 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1738 l4_rel.callref = trans->callref;
1739
Harald Weltedcaf5652009-07-23 18:56:43 +02001740 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741 case 0x303:
1742 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001743 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 break;
1745 case 0x310:
1746 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001747 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 break;
1749 case 0x313:
1750 disconnect = 1;
1751 /* unknown, did not find it in the specs */
1752 break;
1753 case 0x301:
1754 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001755 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 break;
1757 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001758 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001760 gsm48_cc_tx_release(trans, &trans->cc.msg);
1761 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 break; /* stay in release state */
1763 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001764 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 return;
1766// release = 1;
1767// l4_cause = 14;
1768// break;
1769 case 0x306:
1770 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001771 mo_cause = trans->cc.msg.cause.value;
1772 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773 break;
1774 case 0x323:
1775 disconnect = 1;
1776 break;
1777 default:
1778 release = 1;
1779 }
1780
1781 if (release && trans->callref) {
1782 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001783 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784 l4_location, l4_cause);
1785 trans->callref = 0;
1786 }
1787
1788 if (disconnect && trans->callref) {
1789 /* process disconnect towards layer 4 */
1790 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001791 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 }
1793
1794 /* process disconnect towards mobile station */
1795 if (disconnect || release) {
1796 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001797 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1798 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1799 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800 mo_rel.cause.diag_len = 3;
1801
1802 if (disconnect)
1803 gsm48_cc_tx_disconnect(trans, &mo_rel);
1804 if (release)
1805 gsm48_cc_tx_release(trans, &mo_rel);
1806 }
1807
1808}
1809
1810static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1811 int sec, int micro)
1812{
1813 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001814 trans->cc.timer.cb = gsm48_cc_timeout;
1815 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001816 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001817 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818}
1819
1820static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1821{
1822 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001823 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1825 struct tlv_parsed tp;
1826 struct gsm_mncc setup;
1827
1828 memset(&setup, 0, sizeof(struct gsm_mncc));
1829 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001830 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001831 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001832 /* emergency setup is identified by msg_type */
1833 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1834 setup.emergency = 1;
1835
1836 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001837 setup.fields |= MNCC_F_CALLING;
1838 strncpy(setup.calling.number, trans->subscr->extension,
1839 sizeof(setup.calling.number)-1);
1840 strncpy(setup.imsi, trans->subscr->imsi,
1841 sizeof(setup.imsi)-1);
1842
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 /* bearer capability */
1844 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1845 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001846 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001847 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001848 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849 }
1850 /* facility */
1851 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1852 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001853 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1855 }
1856 /* called party bcd number */
1857 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1858 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001859 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001860 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1861 }
1862 /* user-user */
1863 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1864 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001865 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1867 }
1868 /* ss-version */
1869 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1870 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001871 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1873 }
1874 /* CLIR suppression */
1875 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1876 setup.clir.sup = 1;
1877 /* CLIR invocation */
1878 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1879 setup.clir.inv = 1;
1880 /* cc cap */
1881 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1882 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1885 }
1886
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887 new_cc_state(trans, GSM_CSTATE_INITIATED);
1888
Harald Welte (local)47df3992009-12-26 19:45:03 +01001889 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1890 subscr_name(trans->subscr), trans->subscr->extension,
1891 setup.called.number);
1892
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001893 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001894
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001896 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897
Harald Welte13cac662009-07-29 12:10:35 +02001898 /* MNCC code will modify the channel asynchronously, we should
1899 * ipaccess-bind only after the modification has been made to the
1900 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901 return 0;
1902}
1903
1904static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001905{
1906 struct msgb *msg = gsm48_msgb_alloc();
1907 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001909 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001910
Harald Welte7ccf7782009-02-17 01:43:01 +00001911 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001912
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913 /* transaction id must not be assigned */
1914 if (trans->transaction_id != 0xff) { /* unasssigned */
1915 DEBUGP(DCC, "TX Setup with assigned transaction. "
1916 "This is not allowed!\n");
1917 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001918 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001919 GSM48_CAUSE_LOC_PRN_S_LU,
1920 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001922 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 return rc;
1924 }
1925
1926 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001927 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1928 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001929 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001930 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001931 GSM48_CAUSE_LOC_PRN_S_LU,
1932 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001934 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 return rc;
1936 }
Harald Welte78283ef2009-07-23 21:36:44 +02001937 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001938
Harald Welte65e74cc2008-12-29 01:55:35 +00001939 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001940
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001942
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 /* bearer capability */
1944 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001945 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946 /* facility */
1947 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001948 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949 /* progress */
1950 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001951 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 /* calling party BCD number */
1953 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001954 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955 /* called party BCD number */
1956 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001957 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 /* user-user */
1959 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001960 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 /* redirecting party BCD number */
1962 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001963 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001964 /* signal */
1965 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001966 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967
1968 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001969
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001970 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001971
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001972 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001973}
1974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1976{
1977 struct gsm48_hdr *gh = msgb_l3(msg);
1978 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1979 struct tlv_parsed tp;
1980 struct gsm_mncc call_conf;
1981
1982 gsm48_stop_cc_timer(trans);
1983 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1984
1985 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1986 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001987 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001988 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989#if 0
1990 /* repeat */
1991 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1992 call_conf.repeat = 1;
1993 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1994 call_conf.repeat = 2;
1995#endif
1996 /* bearer capability */
1997 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1998 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001999 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002001 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002 }
2003 /* cause */
2004 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2005 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002006 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2008 }
2009 /* cc cap */
2010 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2011 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002012 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2014 }
2015
2016 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2017
Harald Welte596fed42009-07-23 19:06:52 +02002018 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2019 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020}
2021
2022static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2023{
2024 struct gsm_mncc *proceeding = arg;
2025 struct msgb *msg = gsm48_msgb_alloc();
2026 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2027
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2029
2030 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2031
2032 /* bearer capability */
2033 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002034 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 /* facility */
2036 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002037 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 /* progress */
2039 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002040 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002042 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043}
2044
2045static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2046{
2047 struct gsm48_hdr *gh = msgb_l3(msg);
2048 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2049 struct tlv_parsed tp;
2050 struct gsm_mncc alerting;
2051
2052 gsm48_stop_cc_timer(trans);
2053 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2054
2055 memset(&alerting, 0, sizeof(struct gsm_mncc));
2056 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002057 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 /* facility */
2059 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2060 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002061 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2063 }
2064
2065 /* progress */
2066 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2067 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002068 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2070 }
2071 /* ss-version */
2072 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2073 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002074 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2076 }
2077
2078 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2079
Harald Welte596fed42009-07-23 19:06:52 +02002080 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2081 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082}
2083
2084static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2085{
2086 struct gsm_mncc *alerting = arg;
2087 struct msgb *msg = gsm48_msgb_alloc();
2088 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2089
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090 gh->msg_type = GSM48_MT_CC_ALERTING;
2091
2092 /* facility */
2093 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002094 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 /* progress */
2096 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002097 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 /* user-user */
2099 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002100 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101
2102 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2103
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002104 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105}
2106
2107static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2108{
2109 struct gsm_mncc *progress = 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_PROGRESS;
2114
2115 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002116 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 /* user-user */
2118 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002119 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002121 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122}
2123
2124static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2125{
2126 struct gsm_mncc *connect = arg;
2127 struct msgb *msg = gsm48_msgb_alloc();
2128 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2129
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 gh->msg_type = GSM48_MT_CC_CONNECT;
2131
2132 gsm48_stop_cc_timer(trans);
2133 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2134
2135 /* facility */
2136 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002137 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138 /* progress */
2139 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 /* connected number */
2142 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002143 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 /* user-user */
2145 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002146 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147
2148 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2149
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002150 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151}
2152
2153static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2154{
2155 struct gsm48_hdr *gh = msgb_l3(msg);
2156 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2157 struct tlv_parsed tp;
2158 struct gsm_mncc connect;
2159
2160 gsm48_stop_cc_timer(trans);
2161
2162 memset(&connect, 0, sizeof(struct gsm_mncc));
2163 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002164 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002166 connect.fields |= MNCC_F_CONNECTED;
2167 strncpy(connect.connected.number, trans->subscr->extension,
2168 sizeof(connect.connected.number)-1);
2169 strncpy(connect.imsi, trans->subscr->imsi,
2170 sizeof(connect.imsi)-1);
2171
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172 /* facility */
2173 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2174 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002175 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2177 }
2178 /* user-user */
2179 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2180 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002181 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2183 }
2184 /* ss-version */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2186 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002187 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2189 }
2190
2191 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002192 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193
Harald Welte596fed42009-07-23 19:06:52 +02002194 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195}
2196
2197
2198static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2199{
2200 struct gsm_mncc connect_ack;
2201
2202 gsm48_stop_cc_timer(trans);
2203
2204 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002205 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206
2207 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2208 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002209
Harald Welte596fed42009-07-23 19:06:52 +02002210 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 &connect_ack);
2212}
2213
2214static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2215{
2216 struct msgb *msg = gsm48_msgb_alloc();
2217 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2218
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2220
2221 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2222
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002223 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224}
2225
2226static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2227{
2228 struct gsm48_hdr *gh = msgb_l3(msg);
2229 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2230 struct tlv_parsed tp;
2231 struct gsm_mncc disc;
2232
2233 gsm48_stop_cc_timer(trans);
2234
2235 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2236
2237 memset(&disc, 0, sizeof(struct gsm_mncc));
2238 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002239 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 /* cause */
2241 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2242 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002243 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2245 }
2246 /* facility */
2247 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2248 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002249 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2251 }
2252 /* user-user */
2253 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2254 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002255 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2257 }
2258 /* ss-version */
2259 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2260 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002261 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002262 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2263 }
2264
Harald Welte596fed42009-07-23 19:06:52 +02002265 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266
2267}
2268
Harald Weltec66b71c2009-06-11 14:23:20 +08002269static struct gsm_mncc_cause default_cause = {
2270 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2271 .coding = 0,
2272 .rec = 0,
2273 .rec_val = 0,
2274 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2275 .diag_len = 0,
2276 .diag = { 0 },
2277};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278
2279static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2280{
2281 struct gsm_mncc *disc = arg;
2282 struct msgb *msg = gsm48_msgb_alloc();
2283 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2284
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2286
2287 gsm48_stop_cc_timer(trans);
2288 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2289
2290 /* cause */
2291 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002292 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 else
Harald Welte55c8f352010-03-07 23:40:35 +01002294 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295
2296 /* facility */
2297 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002298 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 /* progress */
2300 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002301 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 /* user-user */
2303 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002304 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305
2306 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002307 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308
2309 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2310
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002311 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312}
2313
2314static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2315{
2316 struct gsm48_hdr *gh = msgb_l3(msg);
2317 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2318 struct tlv_parsed tp;
2319 struct gsm_mncc rel;
2320 int rc;
2321
2322 gsm48_stop_cc_timer(trans);
2323
2324 memset(&rel, 0, sizeof(struct gsm_mncc));
2325 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002326 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 /* cause */
2328 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2329 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002330 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2332 }
2333 /* facility */
2334 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2335 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002336 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2338 }
2339 /* user-user */
2340 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2341 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2344 }
2345 /* ss-version */
2346 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2347 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002348 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2350 }
2351
Harald Weltedcaf5652009-07-23 18:56:43 +02002352 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002354 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002356 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002357 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002358 GSM48_MT_CC_RELEASE_COMPL);
2359 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 }
2361
2362 new_cc_state(trans, GSM_CSTATE_NULL);
2363
2364 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002365 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366
2367 return rc;
2368}
2369
2370static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2371{
2372 struct gsm_mncc *rel = arg;
2373 struct msgb *msg = gsm48_msgb_alloc();
2374 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2375
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 gh->msg_type = GSM48_MT_CC_RELEASE;
2377
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 gsm48_stop_cc_timer(trans);
2379 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2380
2381 /* cause */
2382 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002383 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 /* facility */
2385 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002386 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387 /* user-user */
2388 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002389 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390
Harald Weltedcaf5652009-07-23 18:56:43 +02002391 trans->cc.T308_second = 0;
2392 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393
Harald Weltedcaf5652009-07-23 18:56:43 +02002394 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2396
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002397 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398}
2399
2400static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2401{
2402 struct gsm48_hdr *gh = msgb_l3(msg);
2403 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2404 struct tlv_parsed tp;
2405 struct gsm_mncc rel;
2406 int rc = 0;
2407
2408 gsm48_stop_cc_timer(trans);
2409
2410 memset(&rel, 0, sizeof(struct gsm_mncc));
2411 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002412 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413 /* cause */
2414 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2415 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002416 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2418 }
2419 /* facility */
2420 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2421 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002422 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2424 }
2425 /* user-user */
2426 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2427 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002428 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2430 }
2431 /* ss-version */
2432 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2433 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002434 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2436 }
2437
2438 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002439 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002441 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 MNCC_REJ_IND, &rel);
2443 break;
2444 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002445 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 MNCC_REL_CNF, &rel);
2447 break;
2448 default:
Harald Welte596fed42009-07-23 19:06:52 +02002449 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450 MNCC_REL_IND, &rel);
2451 }
2452 }
2453
2454 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002455 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456
2457 return rc;
2458}
2459
2460static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2461{
2462 struct gsm_mncc *rel = arg;
2463 struct msgb *msg = gsm48_msgb_alloc();
2464 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002465 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2468
2469 trans->callref = 0;
2470
2471 gsm48_stop_cc_timer(trans);
2472
2473 /* cause */
2474 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002475 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476 /* facility */
2477 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479 /* user-user */
2480 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002481 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482
Harald Weltec7782de2010-12-21 19:31:41 +01002483 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2484
Harald Weltedcaf5652009-07-23 18:56:43 +02002485 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002486
Harald Weltec7782de2010-12-21 19:31:41 +01002487 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488}
2489
2490static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2491{
2492 struct gsm48_hdr *gh = msgb_l3(msg);
2493 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2494 struct tlv_parsed tp;
2495 struct gsm_mncc fac;
2496
2497 memset(&fac, 0, sizeof(struct gsm_mncc));
2498 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002499 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500 /* facility */
2501 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2502 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002503 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2505 }
2506 /* ss-version */
2507 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2508 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002509 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2511 }
2512
Harald Welte596fed42009-07-23 19:06:52 +02002513 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514}
2515
2516static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2517{
2518 struct gsm_mncc *fac = arg;
2519 struct msgb *msg = gsm48_msgb_alloc();
2520 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2521
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 gh->msg_type = GSM48_MT_CC_FACILITY;
2523
2524 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002525 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002526
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002527 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528}
2529
2530static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2531{
2532 struct gsm_mncc hold;
2533
2534 memset(&hold, 0, sizeof(struct gsm_mncc));
2535 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002536 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537}
2538
2539static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2540{
2541 struct msgb *msg = gsm48_msgb_alloc();
2542 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2543
Harald Welte4bfdfe72009-06-10 23:11:52 +08002544 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2545
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002546 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547}
2548
2549static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2550{
2551 struct gsm_mncc *hold_rej = arg;
2552 struct msgb *msg = gsm48_msgb_alloc();
2553 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2554
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2556
2557 /* cause */
2558 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002559 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560 else
Harald Welte55c8f352010-03-07 23:40:35 +01002561 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002562
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002563 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564}
2565
2566static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2567{
2568 struct gsm_mncc retrieve;
2569
2570 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2571 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002572 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2573 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574}
2575
2576static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2577{
2578 struct msgb *msg = gsm48_msgb_alloc();
2579 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2580
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2582
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002583 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584}
2585
2586static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2587{
2588 struct gsm_mncc *retrieve_rej = arg;
2589 struct msgb *msg = gsm48_msgb_alloc();
2590 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2591
Harald Welte4bfdfe72009-06-10 23:11:52 +08002592 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2593
2594 /* cause */
2595 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002596 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 else
Harald Welte55c8f352010-03-07 23:40:35 +01002598 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002600 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601}
2602
2603static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2604{
2605 struct gsm48_hdr *gh = msgb_l3(msg);
2606 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2607 struct tlv_parsed tp;
2608 struct gsm_mncc dtmf;
2609
2610 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2611 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002612 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613 /* keypad facility */
2614 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2615 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002616 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2618 }
2619
Harald Welte596fed42009-07-23 19:06:52 +02002620 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621}
2622
2623static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2624{
2625 struct gsm_mncc *dtmf = arg;
2626 struct msgb *msg = gsm48_msgb_alloc();
2627 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2628
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2630
2631 /* keypad */
2632 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002633 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002634
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002635 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002636}
2637
2638static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2639{
2640 struct gsm_mncc *dtmf = arg;
2641 struct msgb *msg = gsm48_msgb_alloc();
2642 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2643
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2645
2646 /* cause */
2647 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002648 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649 else
Harald Welte55c8f352010-03-07 23:40:35 +01002650 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002652 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653}
2654
2655static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2656{
2657 struct msgb *msg = gsm48_msgb_alloc();
2658 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2659
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2661
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002662 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663}
2664
2665static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2666{
2667 struct gsm_mncc dtmf;
2668
2669 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2670 dtmf.callref = trans->callref;
2671
Harald Welte596fed42009-07-23 19:06:52 +02002672 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673}
2674
2675static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2676{
2677 struct gsm48_hdr *gh = msgb_l3(msg);
2678 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2679 struct tlv_parsed tp;
2680 struct gsm_mncc modify;
2681
2682 memset(&modify, 0, sizeof(struct gsm_mncc));
2683 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002684 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685 /* bearer capability */
2686 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2687 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002688 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002690 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691 }
2692
2693 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2694
Harald Welte596fed42009-07-23 19:06:52 +02002695 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696}
2697
2698static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2699{
2700 struct gsm_mncc *modify = arg;
2701 struct msgb *msg = gsm48_msgb_alloc();
2702 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2703
Harald Welte4bfdfe72009-06-10 23:11:52 +08002704 gh->msg_type = GSM48_MT_CC_MODIFY;
2705
2706 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2707
2708 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002709 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710
2711 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2712
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002713 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714}
2715
2716static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2717{
2718 struct gsm48_hdr *gh = msgb_l3(msg);
2719 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2720 struct tlv_parsed tp;
2721 struct gsm_mncc modify;
2722
2723 gsm48_stop_cc_timer(trans);
2724
2725 memset(&modify, 0, sizeof(struct gsm_mncc));
2726 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002727 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002728 /* bearer capability */
2729 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2730 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002731 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002733 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 }
2735
2736 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2737
Harald Welte596fed42009-07-23 19:06:52 +02002738 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739}
2740
2741static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2742{
2743 struct gsm_mncc *modify = arg;
2744 struct msgb *msg = gsm48_msgb_alloc();
2745 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2746
Harald Welte4bfdfe72009-06-10 23:11:52 +08002747 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2748
2749 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002750 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751
2752 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2753
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002754 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755}
2756
2757static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2758{
2759 struct gsm48_hdr *gh = msgb_l3(msg);
2760 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2761 struct tlv_parsed tp;
2762 struct gsm_mncc modify;
2763
2764 gsm48_stop_cc_timer(trans);
2765
2766 memset(&modify, 0, sizeof(struct gsm_mncc));
2767 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002768 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002769 /* bearer capability */
2770 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2771 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002772 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002774 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002775 }
2776 /* cause */
2777 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2778 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002779 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2781 }
2782
2783 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2784
Harald Welte596fed42009-07-23 19:06:52 +02002785 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786}
2787
2788static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2789{
2790 struct gsm_mncc *modify = arg;
2791 struct msgb *msg = gsm48_msgb_alloc();
2792 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2793
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2795
2796 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002797 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002799 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800
2801 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2802
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002803 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804}
2805
2806static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2807{
2808 struct gsm_mncc *notify = arg;
2809 struct msgb *msg = gsm48_msgb_alloc();
2810 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2811
Harald Welte4bfdfe72009-06-10 23:11:52 +08002812 gh->msg_type = GSM48_MT_CC_NOTIFY;
2813
2814 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002815 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002817 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818}
2819
2820static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2821{
2822 struct gsm48_hdr *gh = msgb_l3(msg);
2823 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2824// struct tlv_parsed tp;
2825 struct gsm_mncc notify;
2826
2827 memset(&notify, 0, sizeof(struct gsm_mncc));
2828 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002829// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002830 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002831 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832
Harald Welte596fed42009-07-23 19:06:52 +02002833 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834}
2835
2836static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2837{
2838 struct gsm_mncc *user = arg;
2839 struct msgb *msg = gsm48_msgb_alloc();
2840 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2841
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 gh->msg_type = GSM48_MT_CC_USER_INFO;
2843
2844 /* user-user */
2845 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002846 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 /* more data */
2848 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002849 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002851 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002852}
2853
2854static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2855{
2856 struct gsm48_hdr *gh = msgb_l3(msg);
2857 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2858 struct tlv_parsed tp;
2859 struct gsm_mncc user;
2860
2861 memset(&user, 0, sizeof(struct gsm_mncc));
2862 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002863 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864 /* user-user */
2865 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2866 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002867 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002868 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2869 }
2870 /* more data */
2871 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2872 user.more = 1;
2873
Harald Welte596fed42009-07-23 19:06:52 +02002874 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875}
2876
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002877static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878{
2879 struct gsm_mncc *mode = arg;
2880
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002881 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2882 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002883}
2884
2885static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002886 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887 int type;
2888 int (*rout) (struct gsm_trans *trans, void *arg);
2889} downstatelist[] = {
2890 /* mobile originating call establishment */
2891 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2892 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2893 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2894 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2895 {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 */
2896 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2897 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2898 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2899 /* mobile terminating call establishment */
2900 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2901 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2902 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2903 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2904 /* signalling during call */
2905 {SBIT(GSM_CSTATE_ACTIVE),
2906 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2907 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2908 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2909 {ALL_STATES,
2910 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2911 {ALL_STATES,
2912 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2913 {ALL_STATES,
2914 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2915 {SBIT(GSM_CSTATE_ACTIVE),
2916 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2917 {SBIT(GSM_CSTATE_ACTIVE),
2918 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2919 {SBIT(GSM_CSTATE_ACTIVE),
2920 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2921 {SBIT(GSM_CSTATE_ACTIVE),
2922 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2923 {SBIT(GSM_CSTATE_ACTIVE),
2924 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2925 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2926 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2927 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2928 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2929 {SBIT(GSM_CSTATE_ACTIVE),
2930 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2931 /* clearing */
2932 {SBIT(GSM_CSTATE_INITIATED),
2933 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2934 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2935 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2936 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2937 MNCC_REL_REQ, gsm48_cc_tx_release},
2938 /* special */
2939 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002940 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002941};
2942
2943#define DOWNSLLEN \
2944 (sizeof(downstatelist) / sizeof(struct downstate))
2945
2946
Harald Welte76556372010-12-22 23:57:45 +01002947int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948{
Harald Welte1a6f7982009-08-09 18:52:33 +02002949 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002950 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002951 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002952 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002953 struct gsm_mncc *data = arg, rel;
2954
Harald Welte04dc88f2010-12-22 21:45:05 +01002955 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2956
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 /* handle special messages */
2958 switch(msg_type) {
2959 case MNCC_BRIDGE:
2960 return tch_bridge(net, arg);
2961 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002962 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002964 return tch_recv_mncc(net, data->callref, 1);
2965 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002966 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002967 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002968 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01002969 /* Find callref */
2970 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002971 if (!trans) {
2972 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002973 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002974 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002975 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01002976 if (!trans->conn) {
2977 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002978 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002979 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002980 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
2981 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01002982 /* This should be LOGL_ERROR or NOTICE, but
2983 * unfortuantely it happens for a couple of frames at
2984 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002985 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002986 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002987 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002988 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002989 switch (bts->type) {
2990 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002991 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01002992 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002993 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002994 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002995 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002996 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002997 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002998 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002999 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003000 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003001 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003002 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003003 }
3004 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005 }
3006
3007 memset(&rel, 0, sizeof(struct gsm_mncc));
3008 rel.callref = data->callref;
3009
3010 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003011 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003012
3013 /* Callref unknown */
3014 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003015 struct gsm_subscriber *subscr;
3016
Harald Welte4a3464c2009-07-04 10:11:24 +02003017 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003018 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3019 "Received '%s' from MNCC with "
3020 "unknown callref %d\n", data->called.number,
3021 get_mncc_name(msg_type), data->callref);
3022 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003023 return mncc_release_ind(net, NULL, data->callref,
3024 GSM48_CAUSE_LOC_PRN_S_LU,
3025 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003026 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003027 if (!data->called.number[0] && !data->imsi[0]) {
3028 DEBUGP(DCC, "(bts - trx - ts - ti) "
3029 "Received '%s' from MNCC with "
3030 "no number or IMSI\n", get_mncc_name(msg_type));
3031 /* Invalid number */
3032 return mncc_release_ind(net, NULL, data->callref,
3033 GSM48_CAUSE_LOC_PRN_S_LU,
3034 GSM48_CC_CAUSE_INV_NR_FORMAT);
3035 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003037 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003038 subscr = subscr_get_by_extension(net,
3039 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003040 else
Harald Welte9176bd42009-07-23 18:46:00 +02003041 subscr = subscr_get_by_imsi(net, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003042
3043 /* update the subscriber we deal with */
3044 log_set_context(BSC_CTX_SUBSCR, subscr);
3045
Harald Welte4bfdfe72009-06-10 23:11:52 +08003046 /* If subscriber is not found */
3047 if (!subscr) {
3048 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3049 "Received '%s' from MNCC with "
3050 "unknown subscriber %s\n", data->called.number,
3051 get_mncc_name(msg_type), data->called.number);
3052 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003053 return mncc_release_ind(net, NULL, data->callref,
3054 GSM48_CAUSE_LOC_PRN_S_LU,
3055 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 }
3057 /* If subscriber is not "attached" */
3058 if (!subscr->lac) {
3059 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3060 "Received '%s' from MNCC with "
3061 "detached subscriber %s\n", data->called.number,
3062 get_mncc_name(msg_type), data->called.number);
3063 subscr_put(subscr);
3064 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003065 return mncc_release_ind(net, NULL, data->callref,
3066 GSM48_CAUSE_LOC_PRN_S_LU,
3067 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 }
3069 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003070 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3071 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003072 DEBUGP(DCC, "No memory for trans.\n");
3073 subscr_put(subscr);
3074 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003075 mncc_release_ind(net, NULL, data->callref,
3076 GSM48_CAUSE_LOC_PRN_S_LU,
3077 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078 return -ENOMEM;
3079 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003081 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003082
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003084 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003085 /* find transaction with this subscriber already paging */
3086 llist_for_each_entry(transt, &net->trans_list, entry) {
3087 /* Transaction of our lchan? */
3088 if (transt == trans ||
3089 transt->subscr != subscr)
3090 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003091 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003092 "Received '%s' from MNCC with "
3093 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003094 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003095 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003096 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003097 subscr_put(subscr);
3098 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003099 return 0;
3100 }
3101 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003102 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003103
3104 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003105 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3106 if (!trans->paging_request) {
3107 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3108 subscr_put(subscr);
3109 trans_free(trans);
3110 return 0;
3111 }
3112
3113 *trans->paging_request = subscr->net;
3114 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003115
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003116 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003117 return 0;
3118 }
3119 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003120 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003121 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003122 } else {
3123 /* update the subscriber we deal with */
3124 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003125 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003126
3127 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003128 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129
3130 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003131 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003132 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003133 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003134 (trans->subscr)?(trans->subscr->extension):"-",
3135 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003136 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3137 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 if (msg_type == MNCC_REL_REQ)
3139 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3140 else
3141 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3142 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003143 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 return rc;
3145 }
3146
3147 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3148 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003149 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003150 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003151 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003152 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003153 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003154
3155 /* Find function for current state and message */
3156 for (i = 0; i < DOWNSLLEN; i++)
3157 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003158 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003159 break;
3160 if (i == DOWNSLLEN) {
3161 DEBUGP(DCC, "Message unhandled at this state.\n");
3162 return 0;
3163 }
3164
3165 rc = downstatelist[i].rout(trans, arg);
3166
3167 return rc;
3168}
3169
3170
3171static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003172 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173 int type;
3174 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3175} datastatelist[] = {
3176 /* mobile originating call establishment */
3177 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3178 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3179 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3180 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3181 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3182 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3183 /* mobile terminating call establishment */
3184 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3185 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3186 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3187 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003188 {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 +08003189 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3190 /* signalling during call */
3191 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3192 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3193 {SBIT(GSM_CSTATE_ACTIVE),
3194 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3195 {ALL_STATES,
3196 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3197 {ALL_STATES,
3198 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3199 {ALL_STATES,
3200 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3201 {SBIT(GSM_CSTATE_ACTIVE),
3202 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3203 {SBIT(GSM_CSTATE_ACTIVE),
3204 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3205 {SBIT(GSM_CSTATE_ACTIVE),
3206 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3207 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3208 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3209 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3210 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3211 {SBIT(GSM_CSTATE_ACTIVE),
3212 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3213 /* clearing */
3214 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3215 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3216 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3217 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3218 {ALL_STATES, /* 5.4.3.4 */
3219 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3220};
3221
3222#define DATASLLEN \
3223 (sizeof(datastatelist) / sizeof(struct datastate))
3224
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003225static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003226{
3227 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003228 uint8_t msg_type = gh->msg_type & 0xbf;
3229 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003230 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003231 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003232
Harald Welte4bfdfe72009-06-10 23:11:52 +08003233 if (msg_type & 0x80) {
3234 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3235 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003236 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003237
Harald Welte4bfdfe72009-06-10 23:11:52 +08003238 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003239 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003240
Harald Welte6f5aee02009-07-23 21:21:14 +02003241 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003242 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003243 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003244 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003245 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3246 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003247
3248 /* Create transaction */
3249 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003250 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003251 "creating new trans.\n", transaction_id);
3252 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003253 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003254 transaction_id, new_callref++);
3255 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003256 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003257 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003258 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003259 GSM48_MT_CC_RELEASE_COMPL);
3260 return -ENOMEM;
3261 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003262 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003263 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003264 }
3265
3266 /* find function for current state and message */
3267 for (i = 0; i < DATASLLEN; i++)
3268 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003269 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003270 break;
3271 if (i == DATASLLEN) {
3272 DEBUGP(DCC, "Message unhandled at this state.\n");
3273 return 0;
3274 }
3275
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003276 assert(trans->subscr);
3277
Harald Welte4bfdfe72009-06-10 23:11:52 +08003278 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003279
3280 return rc;
3281}
3282
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003283/* Create a dummy to wait five seconds */
3284static void release_anchor(struct gsm_subscriber_connection *conn)
3285{
3286 if (!conn->anch_operation)
3287 return;
3288
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003289 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003290 talloc_free(conn->anch_operation);
3291 conn->anch_operation = NULL;
3292}
3293
3294static void anchor_timeout(void *_data)
3295{
3296 struct gsm_subscriber_connection *con = _data;
3297
3298 release_anchor(con);
3299 msc_release_connection(con);
3300}
3301
3302int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3303{
3304 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3305 if (!conn->anch_operation)
3306 return -1;
3307
3308 conn->anch_operation->timeout.data = conn;
3309 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003310 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003311 return 0;
3312}
3313
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003314/* here we get data from the BSC level... */
3315int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003316{
3317 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003318 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003319 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003320
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003321 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003322 if (silent_call_reroute(conn, msg))
3323 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003324
3325 switch (pdisc) {
3326 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003327 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003328 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003329 break;
3330 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003331 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003332 break;
3333 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003334 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003335 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003336 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003337 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003338 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003339 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003340 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003341 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003342 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3343 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003344 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003345 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003346 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003347 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003348 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003349 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003350 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3351 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003352 break;
3353 }
3354
3355 return rc;
3356}
Harald Welte8470bf22008-12-25 23:28:35 +00003357
Harald Welte805f6442009-07-28 18:25:29 +02003358/*
3359 * This will be ran by the linker when loading the DSO. We use it to
3360 * do system initialization, e.g. registration of signal handlers.
3361 */
3362static __attribute__((constructor)) void on_dso_load_0408(void)
3363{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003364 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003365}