blob: cd3db9c776f975b87c4b5a4e07a571759b5227ba [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
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100258 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200259 case GSM_AUTH_POLICY_CLOSED:
260 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200261 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200262 if (subscriber->authorized)
263 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200264 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200265 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000266 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 default:
268 return 0;
269 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000270}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000271
Holger Hans Peter 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) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100501 conn->subscr = subscr_get_by_imsi(net->subscr_group,
502 mi_string);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100503 if (!conn->subscr)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100504 conn->subscr = subscr_create_subscriber(
505 net->subscr_group, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200506 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100507 if (conn->loc_operation)
508 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000509 break;
510 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000511 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000512 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100513 if (conn->subscr) {
514 db_subscriber_assoc_imei(conn->subscr, mi_string);
515 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200516 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100517 if (conn->loc_operation)
518 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000519 break;
520 }
Holger Freyther73487a22008-12-31 18:53:57 +0000521
522 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100523 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000524}
525
Harald Welte255539c2008-12-28 02:26:27 +0000526
527static void loc_upd_rej_cb(void *data)
528{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100529 struct gsm_subscriber_connection *conn = data;
530 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100531 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000532
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100533 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800534 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800535 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000536}
537
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100538static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000539{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100540 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
541 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200542 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000543}
544
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200545static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000546{
547 switch (type) {
548 case GSM48_LUPD_NORMAL:
549 return "NORMAL";
550 case GSM48_LUPD_PERIODIC:
Alexander Chemerisa3d41c92013-10-04 02:42:26 +0200551 return "PERIODIC";
Harald Welte2a139372009-02-22 21:14:55 +0000552 case GSM48_LUPD_IMSI_ATT:
553 return "IMSI ATTACH";
554 default:
555 return "UNKNOWN";
556 }
557}
558
Harald Weltebf5e8df2009-02-03 12:59:45 +0000559/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800560static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000561{
Harald Welte8470bf22008-12-25 23:28:35 +0000562 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000563 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800564 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800565 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200566 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200567 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000568
Harald Welte8470bf22008-12-25 23:28:35 +0000569 lu = (struct gsm48_loc_upd_req *) gh->data;
570
571 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000572
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200573 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000574
Harald Weltea0368542009-06-27 02:58:43 +0200575 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000576 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000577
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200578 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100579
Harald Welte24ff6ee2009-12-22 00:41:05 +0100580 switch (lu->type) {
581 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200582 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100583 break;
584 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200585 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100586 break;
587 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200588 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100589 break;
590 }
591
Holger Freythereaf04692009-06-06 13:54:44 +0000592 /*
593 * Pseudo Spoof detection: Just drop a second/concurrent
594 * location updating request.
595 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100596 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200597 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100598 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800599 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000600 return 0;
601 }
602
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800603 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000604
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200605 conn->loc_operation->key_seq = lu->key_seq;
606
Harald Welte52b1f982008-12-23 20:25:15 +0000607 switch (mi_type) {
608 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200609 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000610 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200611 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100612 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000613
Jan Luebbe370b41d2009-08-12 10:19:34 +0200614 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100615 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200616 if (!subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100617 subscr = subscr_create_subscriber(
618 bts->network->subscr_group, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200619 }
Harald Welte4b634542008-12-27 01:55:51 +0000620 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000621 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200622 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000623 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100624 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200625 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000626 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000627 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200628 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100629 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000630 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200631 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200632 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200633 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000634 break;
635 case GSM_MI_TYPE_IMEI:
636 case GSM_MI_TYPE_IMEISV:
637 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200638 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000639 break;
640 default:
Harald Weltea0368542009-06-27 02:58:43 +0200641 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000642 break;
643 }
644
Harald Welte24516ea2009-07-04 10:18:00 +0200645 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100646 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200647
Harald Welte4bfdfe72009-06-10 23:11:52 +0800648 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200649 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800650 /* FIXME: request id? close channel? */
651 return -EINVAL;
652 }
653
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100654 conn->subscr = subscr;
655 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000656
Harald Welte24516ea2009-07-04 10:18:00 +0200657 /* check if we can let the subscriber into our network immediately
658 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100659 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000660}
661
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100662/* Turn int into semi-octet representation: 98 => 0x89 */
663static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800664{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100665 uint8_t ret;
666
667 ret = value / 10;
668 ret |= (value % 10) << 4;
669
670 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800671}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100672
Harald Welte4bfdfe72009-06-10 23:11:52 +0800673
Harald Weltedb253af2008-12-30 17:56:55 +0000674/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800675int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000676{
677 struct msgb *msg = gsm48_msgb_alloc();
678 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800679 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100680 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200681 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200682 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100683
Harald Welte4bfdfe72009-06-10 23:11:52 +0800684 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100685 struct tm* gmt_time;
686 struct tm* local_time;
687 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100688 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000689
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800690 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000691
692 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
693 gh->proto_discr = GSM48_PDISC_MM;
694 gh->msg_type = GSM48_MT_MM_INFO;
695
696 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200697#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000698 name_len = strlen(net->name_long);
699 /* 10.5.3.5a */
700 ptr8 = msgb_put(msg, 3);
701 ptr8[0] = GSM48_IE_NAME_LONG;
702 ptr8[1] = name_len*2 +1;
703 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
704
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200705 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000706 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000707 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000708
709 /* FIXME: Use Cell Broadcast, not UCS-2, since
710 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200711#endif
712 name_len = (strlen(net->name_long)*7)/8;
713 name_pad = (8 - strlen(net->name_long)*7)%8;
714 if (name_pad > 0)
715 name_len++;
716 /* 10.5.3.5a */
717 ptr8 = msgb_put(msg, 3);
718 ptr8[0] = GSM48_IE_NAME_LONG;
719 ptr8[1] = name_len +1;
720 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
721
722 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100723 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200724
Harald Weltedb253af2008-12-30 17:56:55 +0000725 }
726
727 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200728#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000729 name_len = strlen(net->name_short);
730 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200731 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200732 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000733 ptr8[1] = name_len*2 + 1;
734 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
735
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200736 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000737 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000738 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200739#endif
740 name_len = (strlen(net->name_short)*7)/8;
741 name_pad = (8 - strlen(net->name_short)*7)%8;
742 if (name_pad > 0)
743 name_len++;
744 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200745 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200746 ptr8[0] = GSM48_IE_NAME_SHORT;
747 ptr8[1] = name_len +1;
748 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
749
750 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100751 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200752
Harald Weltedb253af2008-12-30 17:56:55 +0000753 }
754
Harald Weltedb253af2008-12-30 17:56:55 +0000755 /* Section 10.5.3.9 */
756 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100757 gmt_time = gmtime(&cur_t);
758
Harald Weltedb253af2008-12-30 17:56:55 +0000759 ptr8 = msgb_put(msg, 8);
760 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100761 ptr8[1] = bcdify(gmt_time->tm_year % 100);
762 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
763 ptr8[3] = bcdify(gmt_time->tm_mday);
764 ptr8[4] = bcdify(gmt_time->tm_hour);
765 ptr8[5] = bcdify(gmt_time->tm_min);
766 ptr8[6] = bcdify(gmt_time->tm_sec);
767
Harald Welte45f91712012-07-08 16:48:11 +0200768 if (bts->tz.override) {
769 /* Convert tz.hr and tz.mn to units */
770 if (bts->tz.hr < 0) {
771 tzunits = ((bts->tz.hr/-1)*4);
772 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100773 ptr8[7] = bcdify(tzunits);
774 /* Set negative time */
775 ptr8[7] |= 0x08;
776 }
777 else {
Harald Welte45f91712012-07-08 16:48:11 +0200778 tzunits = bts->tz.hr*4;
779 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100780 ptr8[7] = bcdify(tzunits);
781 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100782 /* Convert DST value */
783 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
784 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100785 }
786 else {
787 /* Need to get GSM offset and convert into 15 min units */
788 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
789 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200790#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100791 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200792#else
793#warning find a portable way to obtain the timezone offset
794 tzunits = 0;
795#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100796 if (tzunits < 0) {
797 tzunits = tzunits/-1;
798 ptr8[7] = bcdify(tzunits);
799 /* Flip it to negative */
800 ptr8[7] |= 0x08;
801 }
802 else
803 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100804
805 /* Does not support DST +2 */
806 if (local_time->tm_isdst)
807 dst = 1;
808 }
809
810 if (dst) {
811 ptr8 = msgb_put(msg, 3);
812 ptr8[0] = GSM48_IE_NET_DST;
813 ptr8[1] = 1;
814 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100815 }
Harald Weltedb253af2008-12-30 17:56:55 +0000816
Daniel Willmanneea93372009-08-13 03:42:07 +0200817 DEBUGP(DMM, "-> MM INFO\n");
818
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800819 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000820}
821
Harald Welte7984d5c2009-08-12 22:56:50 +0200822/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200823int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200824{
825 struct msgb *msg = gsm48_msgb_alloc();
826 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200827 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200828
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200829 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200830
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800831 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200832 gh->proto_discr = GSM48_PDISC_MM;
833 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
834
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100835 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200836
Harald Welte7984d5c2009-08-12 22:56:50 +0200837 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200838 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200839 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200840
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800841 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200842}
843
844/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800845int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200846{
847 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800848 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200849}
850
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100851/*
852 * At the 30C3 phones miss their periodic update
853 * interval a lot and then remain unreachable. In case
854 * we still know the TMSI we can just attach it again.
855 */
856static void implit_attach(struct gsm_subscriber_connection *conn)
857{
858 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
859 return;
860
861 subscr_update(conn->subscr, conn->bts,
862 GSM_SUBSCRIBER_UPDATE_ATTACHED);
863}
864
865
Sylvain Munautba87f452009-12-24 00:28:46 +0100866static int _gsm48_rx_mm_serv_req_sec_cb(
867 unsigned int hooknum, unsigned int event,
868 struct msgb *msg, void *data, void *param)
869{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800870 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100871 int rc = 0;
872
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200873 /* auth failed or succeeded, the timer was stopped */
874 conn->expire_timer_stopped = 1;
875
Sylvain Munautba87f452009-12-24 00:28:46 +0100876 switch (event) {
877 case GSM_SECURITY_AUTH_FAILED:
878 /* Nothing to do */
879 break;
880
881 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200882 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800883 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100884 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100885 break;
886
887 case GSM_SECURITY_SUCCEEDED:
888 /* nothing to do. CIPHER MODE COMMAND is
889 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100890 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100891 break;
892
893 default:
894 rc = -EINVAL;
895 };
896
897 return rc;
898}
899
Harald Welte4ed0e922009-01-10 03:17:30 +0000900/*
901 * Handle CM Service Requests
902 * a) Verify that the packet is long enough to contain the information
903 * we require otherwsie reject with INCORRECT_MESSAGE
904 * b) Try to parse the TMSI. If we do not have one reject
905 * c) Check that we know the subscriber with the TMSI otherwise reject
906 * with a HLR cause
907 * d) Set the subscriber on the gsm_lchan and accept
908 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800909static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000910{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200911 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200912 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000913
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800914 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000915 struct gsm_subscriber *subscr;
916 struct gsm48_hdr *gh = msgb_l3(msg);
917 struct gsm48_service_request *req =
918 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800919 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200920 uint8_t classmark2_len = gh->data[1];
921 uint8_t *classmark2 = gh->data+2;
922 uint8_t mi_len = *(classmark2 + classmark2_len);
923 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000924
Harald Weltec9e02182009-05-01 19:07:53 +0000925 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000926 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000927 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800928 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000929 GSM48_REJECT_INCORRECT_MESSAGE);
930 }
931
932 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000933 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800934 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000935 GSM48_REJECT_INCORRECT_MESSAGE);
936 }
937
Harald Weltec9e02182009-05-01 19:07:53 +0000938 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000939 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000940 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800941 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000942 GSM48_REJECT_INCORRECT_MESSAGE);
943 }
944
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200945 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000946 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000947 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000948
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200949 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100950
Harald Welte3ac7f102009-08-10 10:12:45 +0200951 if (is_siemens_bts(bts))
952 send_siemens_mrpci(msg->lchan, classmark2-1);
953
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100954 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200955 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000956
Harald Welte2a139372009-02-22 21:14:55 +0000957 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000958 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800959 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000960 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
961
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800962 if (!conn->subscr)
963 conn->subscr = subscr;
964 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100965 subscr_put(subscr); /* lchan already has a ref, don't need another one */
966 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000967 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
968 subscr_put(subscr);
969 }
970
Harald Weltec2e302d2009-07-05 14:08:13 +0200971 subscr->equipment.classmark2_len = classmark2_len;
972 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
973 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000974
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200975 /* we will send a MM message soon */
976 conn->expire_timer_stopped = 1;
977
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800978 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100979 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000980}
981
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200982static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000983{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200984 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000985 struct gsm48_hdr *gh = msgb_l3(msg);
986 struct gsm48_imsi_detach_ind *idi =
987 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200988 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200989 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800990 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000991
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200992 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +0100993 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +0000994 mi_type, mi_string);
995
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200996 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100997
Harald Welte2a139372009-02-22 21:14:55 +0000998 switch (mi_type) {
999 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001000 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001001 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001002 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001003 break;
1004 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001005 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001006 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1007 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001008 break;
1009 case GSM_MI_TYPE_IMEI:
1010 case GSM_MI_TYPE_IMEISV:
1011 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001012 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001013 break;
1014 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001015 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001016 break;
1017 }
1018
Holger Freyther4a49e772009-04-12 05:37:29 +00001019 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001020 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001021 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001022 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001023
1024 subscr->equipment.classmark1 = idi->classmark1;
1025 db_sync_equipment(&subscr->equipment);
1026
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001027 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001028 } else
Harald Welte2a139372009-02-22 21:14:55 +00001029 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1030
Harald Welte31981a02009-12-20 09:58:40 +01001031 /* FIXME: iterate over all transactions and release them,
1032 * imagine an IMSI DETACH happening during an active call! */
1033
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001034 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001035 return 0;
1036}
1037
Harald Welted2a7f5a2009-06-05 20:08:20 +00001038static int gsm48_rx_mm_status(struct msgb *msg)
1039{
1040 struct gsm48_hdr *gh = msgb_l3(msg);
1041
1042 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1043
1044 return 0;
1045}
1046
Sylvain Munaut30a15382009-12-24 00:27:26 +01001047/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001048static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001049{
1050 struct gsm48_hdr *gh = msgb_l3(msg);
1051 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001052 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001053
Sylvain Munautc593cf12010-06-10 23:51:41 +02001054 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001055 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001056
1057 /* Safety check */
1058 if (!conn->sec_operation) {
1059 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1060 return -EIO;
1061 }
1062
1063 /* Validate SRES */
1064 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001065 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001066 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001067
1068 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001069 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001070
Sylvain Munaut30a15382009-12-24 00:27:26 +01001071 if (cb)
1072 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001073 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001074
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001075 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001076 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001077 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001078 }
1079
Sylvain Munautc593cf12010-06-10 23:51:41 +02001080 DEBUGPC(DMM, "OK\n");
1081
Sylvain Munaut30a15382009-12-24 00:27:26 +01001082 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001083 return gsm0808_cipher_mode(conn, net->a5_encryption,
1084 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001085}
1086
Harald Weltebf5e8df2009-02-03 12:59:45 +00001087/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001088static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001089{
1090 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001091 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001092
1093 switch (gh->msg_type & 0xbf) {
1094 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001095 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001096 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001097 break;
1098 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001099 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001100 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001101 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001102 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001103 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001104 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001105 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001106 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001107 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001108 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001109 conn->subscr ?
1110 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001111 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001112 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001113 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001114 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001115 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001116 break;
1117 case GSM48_MT_MM_CM_REEST_REQ:
1118 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1119 break;
1120 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001121 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001122 break;
1123 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001124 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001125 gh->msg_type);
1126 break;
1127 }
1128
1129 return rc;
1130}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001131
Harald Welte2d35ae62009-02-06 12:02:13 +00001132/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001133static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001134{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001135 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001136 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001137 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001138 uint8_t *classmark2_lv = gh->data + 1;
1139 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001140 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001141 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001142 int rc = 0;
1143
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001144 resp = (struct gsm48_pag_resp *) &gh->data[0];
1145 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1146 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001147 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1148 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001149
Harald Weltefe18d8f2009-02-22 21:14:24 +00001150 switch (mi_type) {
1151 case GSM_MI_TYPE_TMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001152 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001153 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001154 break;
1155 case GSM_MI_TYPE_IMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001156 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1157 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001158 break;
1159 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001160
1161 if (!subscr) {
1162 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001163 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001164 return -EINVAL;
1165 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001166 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001167 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001168 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001169
Harald Weltec2e302d2009-07-05 14:08:13 +02001170 subscr->equipment.classmark2_len = *classmark2_lv;
1171 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1172 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001173
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001174 /* We received a paging */
1175 conn->expire_timer_stopped = 1;
1176
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001177 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001178 return rc;
1179}
1180
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001181static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001182{
1183 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001184 uint8_t apdu_id_flags;
1185 uint8_t apdu_len;
1186 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001187
1188 apdu_id_flags = gh->data[0];
1189 apdu_len = gh->data[1];
1190 apdu_data = gh->data+2;
1191
Harald Welte70f92052012-07-16 13:22:19 +02001192 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001193 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001194
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001195 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001196}
1197
Harald Weltebf5e8df2009-02-03 12:59:45 +00001198/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001199static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001200{
1201 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001202 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001203
1204 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001205 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001206 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001207 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001208 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001209 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001210 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001211 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001212 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001213 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001214 break;
1215 }
1216
Harald Welte2d35ae62009-02-06 12:02:13 +00001217 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001218}
1219
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001220int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1221 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001222{
1223 struct msgb *msg = gsm48_msgb_alloc();
1224 struct gsm48_hdr *gh;
1225
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001226 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001227
1228 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1229 apdu_id, apdu_len);
1230
1231 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1232 gh->proto_discr = GSM48_PDISC_RR;
1233 gh->msg_type = GSM48_MT_RR_APP_INFO;
1234 gh->data[0] = apdu_id;
1235 gh->data[1] = apdu_len;
1236 memcpy(gh->data+2, apdu, apdu_len);
1237
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001238 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001239}
1240
Harald Welte4bc90a12008-12-27 16:32:52 +00001241/* Call Control */
1242
Harald Welte7584aea2009-02-11 11:44:12 +00001243/* The entire call control code is written in accordance with Figure 7.10c
1244 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1245 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1246 * it for voice */
1247
Harald Welte4bfdfe72009-06-10 23:11:52 +08001248static void new_cc_state(struct gsm_trans *trans, int state)
1249{
1250 if (state > 31 || state < 0)
1251 return;
1252
1253 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001254 gsm48_cc_state_name(trans->cc.state),
1255 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001256
Harald Weltedcaf5652009-07-23 18:56:43 +02001257 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001258}
1259
1260static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001261{
1262 struct msgb *msg = gsm48_msgb_alloc();
1263 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001264 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001265
Harald Welte4bc90a12008-12-27 16:32:52 +00001266 gh->msg_type = GSM48_MT_CC_STATUS;
1267
1268 cause = msgb_put(msg, 3);
1269 cause[0] = 2;
1270 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1271 cause[2] = 0x80 | 30; /* response to status inquiry */
1272
1273 call_state = msgb_put(msg, 1);
1274 call_state[0] = 0xc0 | 0x00;
1275
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001276 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001277}
1278
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001279static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001280 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001281{
1282 struct msgb *msg = gsm48_msgb_alloc();
1283 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1284
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001285 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001286
Harald Welte6f4b7532008-12-29 00:39:37 +00001287 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001288 gh->msg_type = msg_type;
1289
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001290 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001291}
1292
Harald Welte4bfdfe72009-06-10 23:11:52 +08001293static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1294{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001295 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001296 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001297 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001298 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299 }
1300}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001301
Harald Welte4bfdfe72009-06-10 23:11:52 +08001302static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1303 int msg_type, struct gsm_mncc *mncc)
1304{
1305 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001306 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001307
1308 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001309 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001310 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001311 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001312 trans->conn->lchan->ts->trx->bts->nr,
1313 trans->conn->lchan->ts->trx->nr,
1314 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001315 (trans->subscr)?(trans->subscr->extension):"-",
1316 get_mncc_name(msg_type));
1317 else
1318 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1319 "Sending '%s' to MNCC.\n",
1320 (trans->subscr)?(trans->subscr->extension):"-",
1321 get_mncc_name(msg_type));
1322 else
1323 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1324 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1325
1326 mncc->msg_type = msg_type;
1327
Harald Welte966636f2009-06-26 19:39:35 +02001328 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001329 if (!msg)
1330 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001331
1332 data = msgb_put(msg, sizeof(struct gsm_mncc));
1333 memcpy(data, mncc, sizeof(struct gsm_mncc));
1334
Harald Welte54209c22010-12-22 23:43:29 +01001335 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336
1337 return 0;
1338}
1339
1340int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001341 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342{
1343 struct gsm_mncc rel;
1344
Harald Welte92f70c52009-06-12 01:54:08 +08001345 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001346 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001347 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001348 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1349 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1351}
1352
Harald Weltedcaf5652009-07-23 18:56:43 +02001353/* Call Control Specific transaction release.
1354 * gets called by trans_free, DO NOT CALL YOURSELF! */
1355void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001356{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001357 gsm48_stop_cc_timer(trans);
1358
1359 /* send release to L4, if callref still exists */
1360 if (trans->callref) {
1361 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001362 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001363 GSM48_CAUSE_LOC_PRN_S_LU,
1364 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001365 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001366 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001368 if (trans->conn)
1369 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370}
1371
1372static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001373
Harald Welte09e38af2009-02-16 22:52:23 +00001374/* call-back from paging the B-end of the connection */
1375static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001376 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001377{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001378 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001379 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001380 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001382
Harald Welte09e38af2009-02-16 22:52:23 +00001383 if (hooknum != GSM_HOOK_RR_PAGING)
1384 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001385
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001386 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 if (!net) {
1388 DEBUGP(DCC, "Error Network not set!\n");
1389 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001390 }
Harald Welte7584aea2009-02-11 11:44:12 +00001391
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392 /* check all tranactions (without lchan) for subscriber */
1393 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001394 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001395 continue;
1396 switch (event) {
1397 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001398 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001399 break;
1400 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001401 transt->subscr->extension);
1402 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001403 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001404 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001405 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001406 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001407 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408 }
1409 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001410 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411 break;
1412 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001413 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001415 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001416 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001417 found = 1;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001418 mncc_release_ind(transt->net, transt,
Harald Welte596fed42009-07-23 19:06:52 +02001419 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001420 GSM48_CAUSE_LOC_PRN_S_LU,
1421 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001422 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001423 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001424 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001425 break;
1426 }
1427 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001428
1429 talloc_free(paging_request);
1430
1431 /*
1432 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1433 * failure and then the subscr will be poke again. This needs a lot of fixing
1434 * in the subscriber queue code.
1435 */
1436 if (!found && conn)
1437 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001438 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001439}
Harald Welte7584aea2009-02-11 11:44:12 +00001440
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001441static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001442
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001443/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001444static int switch_for_handover(struct gsm_lchan *old_lchan,
1445 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001446{
1447 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001448
1449 if (ipacc_rtp_direct) {
1450 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1451 return 0;
1452 }
1453
1454 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001455 new_rs = new_lchan->abis_ip.rtp_socket;
1456 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001457
1458 if (!new_rs) {
1459 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1460 return -EIO;
1461 }
1462
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001463 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001464
1465 if (!old_rs) {
1466 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1467 return -EIO;
1468 }
1469
1470 /* copy rx_action and reference to other sock */
1471 new_rs->rx_action = old_rs->rx_action;
1472 new_rs->tx_action = old_rs->tx_action;
1473 new_rs->transmit = old_rs->transmit;
1474
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001475 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001476 case RTP_PROXY:
1477 other_rs = old_rs->proxy.other_sock;
1478 rtp_socket_proxy(new_rs, other_rs);
1479 /* delete reference to other end socket to prevent
1480 * rtp_socket_free() from removing the inverse reference */
1481 old_rs->proxy.other_sock = NULL;
1482 break;
1483 case RTP_RECV_UPSTREAM:
1484 new_rs->receive = old_rs->receive;
1485 break;
1486 case RTP_NONE:
1487 break;
1488 }
1489
1490 return 0;
1491}
1492
Harald Welte805f6442009-07-28 18:25:29 +02001493/* some other part of the code sends us a signal */
1494static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1495 void *handler_data, void *signal_data)
1496{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001497 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001498 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001499 struct gsm_network *net;
1500 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001501
1502 if (subsys != SS_ABISIP)
1503 return 0;
1504
1505 /* in case we use direct BTS-to-BTS RTP */
1506 if (ipacc_rtp_direct)
1507 return 0;
1508
Harald Welte805f6442009-07-28 18:25:29 +02001509 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001510 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001511 /* in case we don't use direct BTS-to-BTS RTP */
1512 /* the BTS has successfully bound a TCH to a local ip/port,
1513 * which means we can connect our UDP socket to it */
1514 if (lchan->abis_ip.rtp_socket) {
1515 rtp_socket_free(lchan->abis_ip.rtp_socket);
1516 lchan->abis_ip.rtp_socket = NULL;
1517 }
1518
1519 lchan->abis_ip.rtp_socket = rtp_socket_create();
1520 if (!lchan->abis_ip.rtp_socket)
1521 return -EIO;
1522
1523 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1524 lchan->abis_ip.bound_ip,
1525 lchan->abis_ip.bound_port);
1526 if (rc < 0)
1527 return -EIO;
1528
Harald Welteda7ab742009-12-19 22:23:05 +01001529 /* check if any transactions on this lchan still have
1530 * a tch_recv_mncc request pending */
1531 net = lchan->ts->trx->bts->network;
1532 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001533 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001534 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1535 tch_recv_mncc(net, trans->callref, 1);
1536 }
1537 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001538
1539 /*
1540 * TODO: this appears to be too early? Why not until after
1541 * the handover detect or the handover complete?
1542 *
1543 * Do we have a handover pending for this new lchan? In that
1544 * case re-route the audio from the old channel to the new one.
1545 */
1546 old_lchan = bsc_handover_pending(lchan);
1547 if (old_lchan)
1548 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001549 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001550 case S_ABISIP_DLCX_IND:
1551 /* the BTS tells us a RTP stream has been disconnected */
1552 if (lchan->abis_ip.rtp_socket) {
1553 rtp_socket_free(lchan->abis_ip.rtp_socket);
1554 lchan->abis_ip.rtp_socket = NULL;
1555 }
1556
1557 break;
Harald Welte805f6442009-07-28 18:25:29 +02001558 }
1559
1560 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001561}
1562
Harald Welte49f48b82009-02-17 15:29:33 +00001563/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001564static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001565{
Harald Welte11fa29c2009-02-19 17:24:39 +00001566 struct gsm_bts *bts = lchan->ts->trx->bts;
1567 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001568 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001569
Harald Welte11fa29c2009-02-19 17:24:39 +00001570 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1571 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1572 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1573
1574 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001575 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001576 return -EINVAL;
1577 }
Harald Welteda7ab742009-12-19 22:23:05 +01001578
1579 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001580 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001581 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001582 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001583 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001584 if (!lchan->abis_ip.rtp_socket) {
1585 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1586 "lchan\n");
1587 return -EIO;
1588 }
1589 if (!remote_lchan->abis_ip.rtp_socket) {
1590 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1591 "remote_lchan\n");
1592 return -EIO;
1593 }
1594
Harald Welte805f6442009-07-28 18:25:29 +02001595 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001596 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001597 if (rc < 0)
1598 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001599 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001600 if (rc < 0)
1601 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001602 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301603 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1604 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001605 } else {
1606 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301607 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1608 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301609 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001610 if (rc < 0)
1611 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301612 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1613 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301614 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001615 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001616 break;
1617 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001618 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001619 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001620 trau_mux_map_lchan(lchan, remote_lchan);
1621 break;
1622 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001623 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001624 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001625 }
Harald Welte49f48b82009-02-17 15:29:33 +00001626
1627 return 0;
1628}
1629
Harald Welte4bfdfe72009-06-10 23:11:52 +08001630/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001631static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001632{
Harald Weltedcaf5652009-07-23 18:56:43 +02001633 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1634 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001635
Harald Welte4bfdfe72009-06-10 23:11:52 +08001636 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001637 return -EIO;
1638
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001639 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001640 return -EIO;
1641
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001642 /* Which subscriber do we want to track trans1 or trans2? */
1643 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1644
Harald Welte4bfdfe72009-06-10 23:11:52 +08001645 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001646 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001647}
1648
Harald Welteda7ab742009-12-19 22:23:05 +01001649/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001650static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651{
1652 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001653 struct gsm_lchan *lchan;
1654 struct gsm_bts *bts;
1655 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001656
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001658 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 if (!trans)
1660 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001661 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001663
1664 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001665 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001666 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001667
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001668 /* store receive state */
1669 trans->tch_recv = enable;
1670
Harald Welteda7ab742009-12-19 22:23:05 +01001671 switch (bts->type) {
1672 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001673 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001674 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001675 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001676 return -EINVAL;
1677 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001678 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1679 * will not be connected to our RTP proxy and the socket will
1680 * not be assigned to the application interface. This method
1681 * will be called again, once the audio socket is created and
1682 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001683 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001684 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1685 return 0;
1686 }
1687 if (enable) {
1688 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001689 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001690 if (rc < 0)
1691 return rc;
1692 /* assign socket to application interface */
1693 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1694 net, callref);
1695 } else
1696 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1697 net, 0);
1698 break;
1699 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001700 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001701 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001702 /* In case we don't have a TCH with correct mode, the TRAU muxer
1703 * will not be asigned to the application interface. This is
1704 * performed by switch_trau_mux() after successful handover or
1705 * assignment. */
1706 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1707 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1708 return 0;
1709 }
Harald Welteda7ab742009-12-19 22:23:05 +01001710 if (enable)
1711 return trau_recv_lchan(lchan, callref);
1712 return trau_mux_unmap(NULL, callref);
1713 break;
1714 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001715 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001716 return -EINVAL;
1717 }
1718
1719 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720}
1721
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1723{
1724 DEBUGP(DCC, "-> STATUS ENQ\n");
1725 return gsm48_cc_tx_status(trans, msg);
1726}
1727
1728static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1729static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1730
1731static void gsm48_cc_timeout(void *arg)
1732{
1733 struct gsm_trans *trans = arg;
1734 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001735 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1736 int mo_location = GSM48_CAUSE_LOC_USER;
1737 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1738 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739 struct gsm_mncc mo_rel, l4_rel;
1740
1741 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1742 mo_rel.callref = trans->callref;
1743 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1744 l4_rel.callref = trans->callref;
1745
Harald Weltedcaf5652009-07-23 18:56:43 +02001746 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747 case 0x303:
1748 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001749 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001750 break;
1751 case 0x310:
1752 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001753 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 break;
1755 case 0x313:
1756 disconnect = 1;
1757 /* unknown, did not find it in the specs */
1758 break;
1759 case 0x301:
1760 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001761 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 break;
1763 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001764 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001766 gsm48_cc_tx_release(trans, &trans->cc.msg);
1767 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 break; /* stay in release state */
1769 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001770 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 return;
1772// release = 1;
1773// l4_cause = 14;
1774// break;
1775 case 0x306:
1776 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001777 mo_cause = trans->cc.msg.cause.value;
1778 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 break;
1780 case 0x323:
1781 disconnect = 1;
1782 break;
1783 default:
1784 release = 1;
1785 }
1786
1787 if (release && trans->callref) {
1788 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001789 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 l4_location, l4_cause);
1791 trans->callref = 0;
1792 }
1793
1794 if (disconnect && trans->callref) {
1795 /* process disconnect towards layer 4 */
1796 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001797 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 }
1799
1800 /* process disconnect towards mobile station */
1801 if (disconnect || release) {
1802 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001803 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1804 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1805 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001806 mo_rel.cause.diag_len = 3;
1807
1808 if (disconnect)
1809 gsm48_cc_tx_disconnect(trans, &mo_rel);
1810 if (release)
1811 gsm48_cc_tx_release(trans, &mo_rel);
1812 }
1813
1814}
1815
1816static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1817 int sec, int micro)
1818{
1819 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001820 trans->cc.timer.cb = gsm48_cc_timeout;
1821 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001822 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001823 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824}
1825
1826static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1827{
1828 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001829 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1831 struct tlv_parsed tp;
1832 struct gsm_mncc setup;
1833
1834 memset(&setup, 0, sizeof(struct gsm_mncc));
1835 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001836 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001837 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 /* emergency setup is identified by msg_type */
1839 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1840 setup.emergency = 1;
1841
1842 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001843 setup.fields |= MNCC_F_CALLING;
1844 strncpy(setup.calling.number, trans->subscr->extension,
1845 sizeof(setup.calling.number)-1);
1846 strncpy(setup.imsi, trans->subscr->imsi,
1847 sizeof(setup.imsi)-1);
1848
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849 /* bearer capability */
1850 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1851 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001852 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001854 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001855 }
1856 /* facility */
1857 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1858 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001859 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001860 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1861 }
1862 /* called party bcd number */
1863 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1864 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001865 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1867 }
1868 /* user-user */
1869 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1870 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001871 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1873 }
1874 /* ss-version */
1875 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1876 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1879 }
1880 /* CLIR suppression */
1881 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1882 setup.clir.sup = 1;
1883 /* CLIR invocation */
1884 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1885 setup.clir.inv = 1;
1886 /* cc cap */
1887 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1888 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001889 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1891 }
1892
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 new_cc_state(trans, GSM_CSTATE_INITIATED);
1894
Harald Welte (local)47df3992009-12-26 19:45:03 +01001895 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1896 subscr_name(trans->subscr), trans->subscr->extension,
1897 setup.called.number);
1898
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001899 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001900
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001902 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903
Harald Welte13cac662009-07-29 12:10:35 +02001904 /* MNCC code will modify the channel asynchronously, we should
1905 * ipaccess-bind only after the modification has been made to the
1906 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 return 0;
1908}
1909
1910static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001911{
1912 struct msgb *msg = gsm48_msgb_alloc();
1913 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001915 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001916
Harald Welte7ccf7782009-02-17 01:43:01 +00001917 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001918
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919 /* transaction id must not be assigned */
1920 if (trans->transaction_id != 0xff) { /* unasssigned */
1921 DEBUGP(DCC, "TX Setup with assigned transaction. "
1922 "This is not allowed!\n");
1923 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001924 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001925 GSM48_CAUSE_LOC_PRN_S_LU,
1926 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001928 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001929 return rc;
1930 }
1931
1932 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001933 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
1934 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001935 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001937 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001938 GSM48_CAUSE_LOC_PRN_S_LU,
1939 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001941 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001942 return rc;
1943 }
Harald Welte78283ef2009-07-23 21:36:44 +02001944 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001945
Harald Welte65e74cc2008-12-29 01:55:35 +00001946 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001947
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001949
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950 /* bearer capability */
1951 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001952 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953 /* facility */
1954 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001955 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001956 /* progress */
1957 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001958 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001959 /* calling party BCD number */
1960 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001961 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 /* called party BCD number */
1963 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001964 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 /* user-user */
1966 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 /* redirecting party BCD number */
1969 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001970 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 /* signal */
1972 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001973 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974
1975 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001976
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001977 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001978
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001979 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001980}
1981
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1983{
1984 struct gsm48_hdr *gh = msgb_l3(msg);
1985 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1986 struct tlv_parsed tp;
1987 struct gsm_mncc call_conf;
1988
1989 gsm48_stop_cc_timer(trans);
1990 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1991
1992 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1993 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001994 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001995 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996#if 0
1997 /* repeat */
1998 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1999 call_conf.repeat = 1;
2000 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2001 call_conf.repeat = 2;
2002#endif
2003 /* bearer capability */
2004 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2005 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002006 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002008 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 }
2010 /* cause */
2011 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2012 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002013 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2015 }
2016 /* cc cap */
2017 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2018 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002019 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2021 }
2022
Andreas Eversberg035b8742013-09-19 09:27:06 +02002023 /* IMSI of called subscriber */
2024 strncpy(call_conf.imsi, trans->subscr->imsi,
2025 sizeof(call_conf.imsi)-1);
2026
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2028
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002029 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002030 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031}
2032
2033static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2034{
2035 struct gsm_mncc *proceeding = arg;
2036 struct msgb *msg = gsm48_msgb_alloc();
2037 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2038
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2040
2041 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2042
2043 /* bearer capability */
2044 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002045 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002046 /* facility */
2047 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 /* progress */
2050 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002051 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002053 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054}
2055
2056static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2057{
2058 struct gsm48_hdr *gh = msgb_l3(msg);
2059 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2060 struct tlv_parsed tp;
2061 struct gsm_mncc alerting;
2062
2063 gsm48_stop_cc_timer(trans);
2064 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2065
2066 memset(&alerting, 0, sizeof(struct gsm_mncc));
2067 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002068 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 /* facility */
2070 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2071 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002072 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2074 }
2075
2076 /* progress */
2077 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2078 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002079 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2081 }
2082 /* ss-version */
2083 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2084 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002085 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2087 }
2088
2089 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2090
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002091 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002092 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093}
2094
2095static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2096{
2097 struct gsm_mncc *alerting = arg;
2098 struct msgb *msg = gsm48_msgb_alloc();
2099 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2100
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101 gh->msg_type = GSM48_MT_CC_ALERTING;
2102
2103 /* facility */
2104 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002105 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 /* progress */
2107 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002108 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109 /* user-user */
2110 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112
2113 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2114
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002115 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116}
2117
2118static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2119{
2120 struct gsm_mncc *progress = arg;
2121 struct msgb *msg = gsm48_msgb_alloc();
2122 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2123
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 gh->msg_type = GSM48_MT_CC_PROGRESS;
2125
2126 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002127 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 /* user-user */
2129 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002130 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002132 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133}
2134
2135static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2136{
2137 struct gsm_mncc *connect = arg;
2138 struct msgb *msg = gsm48_msgb_alloc();
2139 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2140
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 gh->msg_type = GSM48_MT_CC_CONNECT;
2142
2143 gsm48_stop_cc_timer(trans);
2144 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2145
2146 /* facility */
2147 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002148 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149 /* progress */
2150 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002151 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152 /* connected number */
2153 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002154 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155 /* user-user */
2156 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002157 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158
2159 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2160
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002161 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162}
2163
2164static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2165{
2166 struct gsm48_hdr *gh = msgb_l3(msg);
2167 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2168 struct tlv_parsed tp;
2169 struct gsm_mncc connect;
2170
2171 gsm48_stop_cc_timer(trans);
2172
2173 memset(&connect, 0, sizeof(struct gsm_mncc));
2174 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002175 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002177 connect.fields |= MNCC_F_CONNECTED;
2178 strncpy(connect.connected.number, trans->subscr->extension,
2179 sizeof(connect.connected.number)-1);
2180 strncpy(connect.imsi, trans->subscr->imsi,
2181 sizeof(connect.imsi)-1);
2182
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 /* facility */
2184 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2185 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2188 }
2189 /* user-user */
2190 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2191 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002192 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2194 }
2195 /* ss-version */
2196 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2197 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002198 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2200 }
2201
2202 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002203 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002205 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206}
2207
2208
2209static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2210{
2211 struct gsm_mncc connect_ack;
2212
2213 gsm48_stop_cc_timer(trans);
2214
2215 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002216 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217
2218 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2219 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002220
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002221 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002222 &connect_ack);
2223}
2224
2225static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2226{
2227 struct msgb *msg = gsm48_msgb_alloc();
2228 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2229
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2231
2232 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2233
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002234 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235}
2236
2237static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2238{
2239 struct gsm48_hdr *gh = msgb_l3(msg);
2240 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2241 struct tlv_parsed tp;
2242 struct gsm_mncc disc;
2243
2244 gsm48_stop_cc_timer(trans);
2245
2246 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2247
2248 memset(&disc, 0, sizeof(struct gsm_mncc));
2249 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002250 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002251 /* cause */
2252 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2253 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002254 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2256 }
2257 /* facility */
2258 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2259 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002260 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2262 }
2263 /* user-user */
2264 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2265 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002266 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2268 }
2269 /* ss-version */
2270 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2271 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002272 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2274 }
2275
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002276 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277
2278}
2279
Harald Weltec66b71c2009-06-11 14:23:20 +08002280static struct gsm_mncc_cause default_cause = {
2281 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2282 .coding = 0,
2283 .rec = 0,
2284 .rec_val = 0,
2285 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2286 .diag_len = 0,
2287 .diag = { 0 },
2288};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289
2290static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2291{
2292 struct gsm_mncc *disc = arg;
2293 struct msgb *msg = gsm48_msgb_alloc();
2294 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2295
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2297
2298 gsm48_stop_cc_timer(trans);
2299 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2300
2301 /* cause */
2302 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002303 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 else
Harald Welte55c8f352010-03-07 23:40:35 +01002305 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306
2307 /* facility */
2308 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002309 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 /* progress */
2311 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002312 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 /* user-user */
2314 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002315 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316
2317 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002318 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319
2320 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2321
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002322 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323}
2324
2325static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2326{
2327 struct gsm48_hdr *gh = msgb_l3(msg);
2328 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2329 struct tlv_parsed tp;
2330 struct gsm_mncc rel;
2331 int rc;
2332
2333 gsm48_stop_cc_timer(trans);
2334
2335 memset(&rel, 0, sizeof(struct gsm_mncc));
2336 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002337 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 /* cause */
2339 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2340 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002341 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2343 }
2344 /* facility */
2345 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2346 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002347 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2349 }
2350 /* user-user */
2351 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2352 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002353 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2355 }
2356 /* ss-version */
2357 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2358 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002359 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2361 }
2362
Harald Weltedcaf5652009-07-23 18:56:43 +02002363 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002365 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002367 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002368 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002369 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002370 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 }
2372
2373 new_cc_state(trans, GSM_CSTATE_NULL);
2374
2375 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002376 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377
2378 return rc;
2379}
2380
2381static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2382{
2383 struct gsm_mncc *rel = arg;
2384 struct msgb *msg = gsm48_msgb_alloc();
2385 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2386
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387 gh->msg_type = GSM48_MT_CC_RELEASE;
2388
Harald Welte4bfdfe72009-06-10 23:11:52 +08002389 gsm48_stop_cc_timer(trans);
2390 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2391
2392 /* cause */
2393 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002394 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 /* facility */
2396 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002397 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 /* user-user */
2399 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002400 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401
Harald Weltedcaf5652009-07-23 18:56:43 +02002402 trans->cc.T308_second = 0;
2403 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404
Harald Weltedcaf5652009-07-23 18:56:43 +02002405 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2407
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002408 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409}
2410
2411static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2412{
2413 struct gsm48_hdr *gh = msgb_l3(msg);
2414 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2415 struct tlv_parsed tp;
2416 struct gsm_mncc rel;
2417 int rc = 0;
2418
2419 gsm48_stop_cc_timer(trans);
2420
2421 memset(&rel, 0, sizeof(struct gsm_mncc));
2422 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002423 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424 /* cause */
2425 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2426 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002427 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2429 }
2430 /* facility */
2431 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2432 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002433 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2435 }
2436 /* user-user */
2437 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2438 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2441 }
2442 /* ss-version */
2443 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2444 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002445 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2447 }
2448
2449 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002450 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002452 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 MNCC_REJ_IND, &rel);
2454 break;
2455 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002456 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 MNCC_REL_CNF, &rel);
2458 break;
2459 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002460 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461 MNCC_REL_IND, &rel);
2462 }
2463 }
2464
2465 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002466 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467
2468 return rc;
2469}
2470
2471static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2472{
2473 struct gsm_mncc *rel = arg;
2474 struct msgb *msg = gsm48_msgb_alloc();
2475 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002476 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2479
2480 trans->callref = 0;
2481
2482 gsm48_stop_cc_timer(trans);
2483
2484 /* cause */
2485 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002486 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 /* facility */
2488 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002489 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 /* user-user */
2491 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002492 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493
Harald Weltec7782de2010-12-21 19:31:41 +01002494 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2495
Harald Weltedcaf5652009-07-23 18:56:43 +02002496 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497
Harald Weltec7782de2010-12-21 19:31:41 +01002498 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499}
2500
2501static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2502{
2503 struct gsm48_hdr *gh = msgb_l3(msg);
2504 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2505 struct tlv_parsed tp;
2506 struct gsm_mncc fac;
2507
2508 memset(&fac, 0, sizeof(struct gsm_mncc));
2509 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002510 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511 /* facility */
2512 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2513 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002514 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2516 }
2517 /* ss-version */
2518 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2519 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002520 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2522 }
2523
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002524 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525}
2526
2527static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2528{
2529 struct gsm_mncc *fac = arg;
2530 struct msgb *msg = gsm48_msgb_alloc();
2531 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2532
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 gh->msg_type = GSM48_MT_CC_FACILITY;
2534
2535 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002536 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002538 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539}
2540
2541static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2542{
2543 struct gsm_mncc hold;
2544
2545 memset(&hold, 0, sizeof(struct gsm_mncc));
2546 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002547 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548}
2549
2550static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2551{
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_ACK;
2556
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002557 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558}
2559
2560static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2561{
2562 struct gsm_mncc *hold_rej = arg;
2563 struct msgb *msg = gsm48_msgb_alloc();
2564 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2565
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2567
2568 /* cause */
2569 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002570 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571 else
Harald Welte55c8f352010-03-07 23:40:35 +01002572 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002574 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575}
2576
2577static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2578{
2579 struct gsm_mncc retrieve;
2580
2581 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2582 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002583 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002584 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585}
2586
2587static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2588{
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_ACK;
2593
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002594 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595}
2596
2597static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2598{
2599 struct gsm_mncc *retrieve_rej = arg;
2600 struct msgb *msg = gsm48_msgb_alloc();
2601 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2602
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2604
2605 /* cause */
2606 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002607 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608 else
Harald Welte55c8f352010-03-07 23:40:35 +01002609 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002611 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612}
2613
2614static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2615{
2616 struct gsm48_hdr *gh = msgb_l3(msg);
2617 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2618 struct tlv_parsed tp;
2619 struct gsm_mncc dtmf;
2620
2621 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2622 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002623 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002624 /* keypad facility */
2625 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2626 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002627 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002628 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2629 }
2630
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002631 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002632}
2633
2634static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2635{
2636 struct gsm_mncc *dtmf = arg;
2637 struct msgb *msg = gsm48_msgb_alloc();
2638 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2639
Harald Welte4bfdfe72009-06-10 23:11:52 +08002640 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2641
2642 /* keypad */
2643 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002644 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002646 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647}
2648
2649static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2650{
2651 struct gsm_mncc *dtmf = arg;
2652 struct msgb *msg = gsm48_msgb_alloc();
2653 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2654
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2656
2657 /* cause */
2658 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002659 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 else
Harald Welte55c8f352010-03-07 23:40:35 +01002661 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002663 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664}
2665
2666static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2667{
2668 struct msgb *msg = gsm48_msgb_alloc();
2669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2670
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2672
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002673 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674}
2675
2676static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2677{
2678 struct gsm_mncc dtmf;
2679
2680 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2681 dtmf.callref = trans->callref;
2682
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002683 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684}
2685
2686static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2687{
2688 struct gsm48_hdr *gh = msgb_l3(msg);
2689 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2690 struct tlv_parsed tp;
2691 struct gsm_mncc modify;
2692
2693 memset(&modify, 0, sizeof(struct gsm_mncc));
2694 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002695 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696 /* bearer capability */
2697 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2698 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002699 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002701 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 }
2703
2704 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2705
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002706 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707}
2708
2709static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2710{
2711 struct gsm_mncc *modify = arg;
2712 struct msgb *msg = gsm48_msgb_alloc();
2713 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2714
Harald Welte4bfdfe72009-06-10 23:11:52 +08002715 gh->msg_type = GSM48_MT_CC_MODIFY;
2716
2717 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2718
2719 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002720 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721
2722 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2723
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002724 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725}
2726
2727static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2728{
2729 struct gsm48_hdr *gh = msgb_l3(msg);
2730 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2731 struct tlv_parsed tp;
2732 struct gsm_mncc modify;
2733
2734 gsm48_stop_cc_timer(trans);
2735
2736 memset(&modify, 0, sizeof(struct gsm_mncc));
2737 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002738 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739 /* bearer capability */
2740 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2741 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002742 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002744 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745 }
2746
2747 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2748
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002749 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750}
2751
2752static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2753{
2754 struct gsm_mncc *modify = arg;
2755 struct msgb *msg = gsm48_msgb_alloc();
2756 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2757
Harald Welte4bfdfe72009-06-10 23:11:52 +08002758 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2759
2760 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002761 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762
2763 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2764
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002765 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766}
2767
2768static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2769{
2770 struct gsm48_hdr *gh = msgb_l3(msg);
2771 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2772 struct tlv_parsed tp;
2773 struct gsm_mncc modify;
2774
2775 gsm48_stop_cc_timer(trans);
2776
2777 memset(&modify, 0, sizeof(struct gsm_mncc));
2778 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002779 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780 /* bearer capability */
2781 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2782 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002783 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002785 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786 }
2787 /* cause */
2788 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2789 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002790 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2792 }
2793
2794 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2795
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002796 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797}
2798
2799static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2800{
2801 struct gsm_mncc *modify = arg;
2802 struct msgb *msg = gsm48_msgb_alloc();
2803 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2804
Harald Welte4bfdfe72009-06-10 23:11:52 +08002805 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2806
2807 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002808 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002810 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002811
2812 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2813
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002814 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002815}
2816
2817static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2818{
2819 struct gsm_mncc *notify = arg;
2820 struct msgb *msg = gsm48_msgb_alloc();
2821 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2822
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823 gh->msg_type = GSM48_MT_CC_NOTIFY;
2824
2825 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002826 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002828 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002829}
2830
2831static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2832{
2833 struct gsm48_hdr *gh = msgb_l3(msg);
2834 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2835// struct tlv_parsed tp;
2836 struct gsm_mncc notify;
2837
2838 memset(&notify, 0, sizeof(struct gsm_mncc));
2839 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002840// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002842 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002843
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002844 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845}
2846
2847static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2848{
2849 struct gsm_mncc *user = arg;
2850 struct msgb *msg = gsm48_msgb_alloc();
2851 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2852
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853 gh->msg_type = GSM48_MT_CC_USER_INFO;
2854
2855 /* user-user */
2856 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002857 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858 /* more data */
2859 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002860 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002862 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863}
2864
2865static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2866{
2867 struct gsm48_hdr *gh = msgb_l3(msg);
2868 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2869 struct tlv_parsed tp;
2870 struct gsm_mncc user;
2871
2872 memset(&user, 0, sizeof(struct gsm_mncc));
2873 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002874 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875 /* user-user */
2876 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2877 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002878 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2880 }
2881 /* more data */
2882 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2883 user.more = 1;
2884
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002885 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002886}
2887
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002888static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889{
2890 struct gsm_mncc *mode = arg;
2891
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002892 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2893 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894}
2895
2896static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002897 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002898 int type;
2899 int (*rout) (struct gsm_trans *trans, void *arg);
2900} downstatelist[] = {
2901 /* mobile originating call establishment */
2902 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2903 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2904 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2905 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2906 {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 */
2907 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2908 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2909 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2910 /* mobile terminating call establishment */
2911 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2912 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2913 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2914 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2915 /* signalling during call */
2916 {SBIT(GSM_CSTATE_ACTIVE),
2917 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2918 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2919 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2920 {ALL_STATES,
2921 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2922 {ALL_STATES,
2923 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2924 {ALL_STATES,
2925 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2926 {SBIT(GSM_CSTATE_ACTIVE),
2927 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2928 {SBIT(GSM_CSTATE_ACTIVE),
2929 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2930 {SBIT(GSM_CSTATE_ACTIVE),
2931 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2932 {SBIT(GSM_CSTATE_ACTIVE),
2933 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2934 {SBIT(GSM_CSTATE_ACTIVE),
2935 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2936 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2937 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2938 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2939 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2940 {SBIT(GSM_CSTATE_ACTIVE),
2941 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2942 /* clearing */
2943 {SBIT(GSM_CSTATE_INITIATED),
2944 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2945 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2946 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2947 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2948 MNCC_REL_REQ, gsm48_cc_tx_release},
2949 /* special */
2950 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002951 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952};
2953
2954#define DOWNSLLEN \
2955 (sizeof(downstatelist) / sizeof(struct downstate))
2956
2957
Harald Welte76556372010-12-22 23:57:45 +01002958int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959{
Harald Welte1a6f7982009-08-09 18:52:33 +02002960 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002961 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002962 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002963 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964 struct gsm_mncc *data = arg, rel;
2965
Harald Welte04dc88f2010-12-22 21:45:05 +01002966 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2967
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968 /* handle special messages */
2969 switch(msg_type) {
2970 case MNCC_BRIDGE:
2971 return tch_bridge(net, arg);
2972 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002973 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002974 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002975 return tch_recv_mncc(net, data->callref, 1);
2976 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002977 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002978 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002979 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01002980 /* Find callref */
2981 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002982 if (!trans) {
2983 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002984 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002985 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002986 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01002987 if (!trans->conn) {
2988 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002989 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002990 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002991 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
2992 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01002993 /* This should be LOGL_ERROR or NOTICE, but
2994 * unfortuantely it happens for a couple of frames at
2995 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002996 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002997 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002998 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002999 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003000 switch (bts->type) {
3001 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003002 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003003 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003004 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003005 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003006 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003007 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003008 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003009 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003010 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003011 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003012 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003013 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003014 }
3015 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016 }
3017
3018 memset(&rel, 0, sizeof(struct gsm_mncc));
3019 rel.callref = data->callref;
3020
3021 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003022 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003023
3024 /* Callref unknown */
3025 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003026 struct gsm_subscriber *subscr;
3027
Harald Welte4a3464c2009-07-04 10:11:24 +02003028 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003029 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3030 "Received '%s' from MNCC with "
3031 "unknown callref %d\n", data->called.number,
3032 get_mncc_name(msg_type), data->callref);
3033 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003034 return mncc_release_ind(net, NULL, data->callref,
3035 GSM48_CAUSE_LOC_PRN_S_LU,
3036 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003037 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003038 if (!data->called.number[0] && !data->imsi[0]) {
3039 DEBUGP(DCC, "(bts - trx - ts - ti) "
3040 "Received '%s' from MNCC with "
3041 "no number or IMSI\n", get_mncc_name(msg_type));
3042 /* Invalid number */
3043 return mncc_release_ind(net, NULL, data->callref,
3044 GSM48_CAUSE_LOC_PRN_S_LU,
3045 GSM48_CC_CAUSE_INV_NR_FORMAT);
3046 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003048 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003049 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003050 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003051 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003052 subscr = subscr_get_by_imsi(net->subscr_group,
3053 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003054
3055 /* update the subscriber we deal with */
3056 log_set_context(BSC_CTX_SUBSCR, subscr);
3057
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058 /* If subscriber is not found */
3059 if (!subscr) {
3060 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3061 "Received '%s' from MNCC with "
3062 "unknown subscriber %s\n", data->called.number,
3063 get_mncc_name(msg_type), data->called.number);
3064 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003065 return mncc_release_ind(net, NULL, data->callref,
3066 GSM48_CAUSE_LOC_PRN_S_LU,
3067 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 }
3069 /* If subscriber is not "attached" */
3070 if (!subscr->lac) {
3071 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3072 "Received '%s' from MNCC with "
3073 "detached subscriber %s\n", data->called.number,
3074 get_mncc_name(msg_type), data->called.number);
3075 subscr_put(subscr);
3076 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003077 return mncc_release_ind(net, NULL, data->callref,
3078 GSM48_CAUSE_LOC_PRN_S_LU,
3079 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 }
3081 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003082 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003083 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084 DEBUGP(DCC, "No memory for trans.\n");
3085 subscr_put(subscr);
3086 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003087 mncc_release_ind(net, NULL, data->callref,
3088 GSM48_CAUSE_LOC_PRN_S_LU,
3089 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003090 return -ENOMEM;
3091 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003092 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003093 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003094
Harald Welte4bfdfe72009-06-10 23:11:52 +08003095 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003096 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003097 /* find transaction with this subscriber already paging */
3098 llist_for_each_entry(transt, &net->trans_list, entry) {
3099 /* Transaction of our lchan? */
3100 if (transt == trans ||
3101 transt->subscr != subscr)
3102 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003103 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003104 "Received '%s' from MNCC with "
3105 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003106 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003107 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003108 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003109 subscr_put(subscr);
3110 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003111 return 0;
3112 }
3113 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003114 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003115
3116 /* Get a channel */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003117 trans->paging_request = talloc_zero(subscr->group->net,
3118 struct gsm_network*);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003119 if (!trans->paging_request) {
3120 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3121 subscr_put(subscr);
3122 trans_free(trans);
3123 return 0;
3124 }
3125
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003126 *trans->paging_request = subscr->group->net;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003127 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003128
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003129 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 return 0;
3131 }
3132 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003133 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003134 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003135 } else {
3136 /* update the subscriber we deal with */
3137 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003139
3140 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003141 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003142
3143 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003144 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003145 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003146 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003147 (trans->subscr)?(trans->subscr->extension):"-",
3148 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003149 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3150 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151 if (msg_type == MNCC_REL_REQ)
3152 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3153 else
3154 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3155 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003156 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003157 return rc;
3158 }
3159
3160 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3161 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003162 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003163 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003164 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003165 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003166 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003167
3168 /* Find function for current state and message */
3169 for (i = 0; i < DOWNSLLEN; i++)
3170 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003171 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003172 break;
3173 if (i == DOWNSLLEN) {
3174 DEBUGP(DCC, "Message unhandled at this state.\n");
3175 return 0;
3176 }
3177
3178 rc = downstatelist[i].rout(trans, arg);
3179
3180 return rc;
3181}
3182
3183
3184static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003185 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003186 int type;
3187 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3188} datastatelist[] = {
3189 /* mobile originating call establishment */
3190 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3191 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3192 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3193 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3194 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3195 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3196 /* mobile terminating call establishment */
3197 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3198 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3199 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3200 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003201 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003202 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3203 /* signalling during call */
3204 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3205 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3206 {SBIT(GSM_CSTATE_ACTIVE),
3207 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3208 {ALL_STATES,
3209 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3210 {ALL_STATES,
3211 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3212 {ALL_STATES,
3213 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3214 {SBIT(GSM_CSTATE_ACTIVE),
3215 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3216 {SBIT(GSM_CSTATE_ACTIVE),
3217 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3218 {SBIT(GSM_CSTATE_ACTIVE),
3219 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3220 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3221 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3222 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3223 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3224 {SBIT(GSM_CSTATE_ACTIVE),
3225 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3226 /* clearing */
3227 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3228 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3229 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3230 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3231 {ALL_STATES, /* 5.4.3.4 */
3232 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3233};
3234
3235#define DATASLLEN \
3236 (sizeof(datastatelist) / sizeof(struct datastate))
3237
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003238static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003239{
3240 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003241 uint8_t msg_type = gh->msg_type & 0xbf;
3242 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003243 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003244 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003245
Harald Welte4bfdfe72009-06-10 23:11:52 +08003246 if (msg_type & 0x80) {
3247 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3248 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003249 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003250
Harald Welte4bfdfe72009-06-10 23:11:52 +08003251 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003252 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003253
Harald Welte6f5aee02009-07-23 21:21:14 +02003254 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003256 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003257 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003258 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3259 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003260
3261 /* Create transaction */
3262 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003263 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003264 "creating new trans.\n", transaction_id);
3265 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003266 trans = trans_alloc(conn->bts->network, conn->subscr,
3267 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003268 transaction_id, new_callref++);
3269 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003270 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003271 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003272 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003273 GSM48_MT_CC_RELEASE_COMPL);
3274 return -ENOMEM;
3275 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003276 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003277 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003278 }
3279
3280 /* find function for current state and message */
3281 for (i = 0; i < DATASLLEN; i++)
3282 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003283 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003284 break;
3285 if (i == DATASLLEN) {
3286 DEBUGP(DCC, "Message unhandled at this state.\n");
3287 return 0;
3288 }
3289
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003290 assert(trans->subscr);
3291
Harald Welte4bfdfe72009-06-10 23:11:52 +08003292 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003293
3294 return rc;
3295}
3296
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003297/* Create a dummy to wait five seconds */
3298static void release_anchor(struct gsm_subscriber_connection *conn)
3299{
3300 if (!conn->anch_operation)
3301 return;
3302
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003303 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003304 talloc_free(conn->anch_operation);
3305 conn->anch_operation = NULL;
3306}
3307
3308static void anchor_timeout(void *_data)
3309{
3310 struct gsm_subscriber_connection *con = _data;
3311
3312 release_anchor(con);
3313 msc_release_connection(con);
3314}
3315
3316int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3317{
3318 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3319 if (!conn->anch_operation)
3320 return -1;
3321
3322 conn->anch_operation->timeout.data = conn;
3323 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003324 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003325 return 0;
3326}
3327
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003328/* here we get data from the BSC level... */
3329int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003330{
3331 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003332 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003333 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003334
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003335 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003336 if (silent_call_reroute(conn, msg))
3337 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003338
3339 switch (pdisc) {
3340 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003341 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003342 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003343 break;
3344 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003345 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003346 break;
3347 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003348 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003349 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003350 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003351 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003352 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003353 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003354 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003355 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003356 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3357 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003358 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003359 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003360 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003361 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003362 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003363 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003364 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3365 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003366 break;
3367 }
3368
3369 return rc;
3370}
Harald Welte8470bf22008-12-25 23:28:35 +00003371
Harald Welte805f6442009-07-28 18:25:29 +02003372/*
3373 * This will be ran by the linker when loading the DSO. We use it to
3374 * do system initialization, e.g. registration of signal handlers.
3375 */
3376static __attribute__((constructor)) void on_dso_load_0408(void)
3377{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003378 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003379}