blob: eea073614739da146b1c15da80ab9c7fbd982d71 [file] [log] [blame]
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte59b04682009-06-10 05:40:52 +08002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte59b04682009-06-10 05:40:52 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-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 Welte59b04682009-06-10 05:40:52 +080012 * (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 Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080018 *
Harald Welte0e3e88e2011-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 Welte59b04682009-06-10 05:40:52 +080021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <time.h>
30#include <netinet/in.h>
31
Harald Welteeaf4a522012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut28c47162009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte59b04682009-06-10 05:40:52 +080035#include <openbsc/db.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
39#include <openbsc/gsm_04_11.h>
40#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte59b04682009-06-10 05:40:52 +080042#include <openbsc/abis_rsl.h>
43#include <openbsc/chan_alloc.h>
44#include <openbsc/paging.h>
45#include <openbsc/signal.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020046#include <osmocom/abis/trau_frame.h>
Harald Welte59b04682009-06-10 05:40:52 +080047#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltec2189a62009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther3a0ec172010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020055#include <osmocom/abis/e1_input.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010056#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther89253132010-09-30 18:52:23 +080057
Pablo Neira Ayusodd5fff42011-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 Welte59b04682009-06-10 05:40:52 +080064
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +020065#include <assert.h>
66
Harald Welte (local)8751ee92009-08-15 02:30:58 +020067void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010068void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020069
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020070int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +080071static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020072 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010073static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +080074static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080075
76struct gsm_lai {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020077 uint16_t mcc;
78 uint16_t mnc;
79 uint16_t lac;
Harald Welte59b04682009-06-10 05:40:52 +080080};
81
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020082static uint32_t new_callref = 0x80000001;
Harald Welte03740842009-06-10 23:11:52 +080083
Harald Welte4f272cc2011-03-03 23:29:05 +010084void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
85{
86 net->mncc_recv(net, msg);
87}
88
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080089static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
90 struct gsm_trans *trans)
91{
92 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
93
94 /* if we get passed a transaction reference, do some common
95 * work that the caller no longer has to do */
96 if (trans) {
97 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
98 msg->lchan = trans->conn->lchan;
99 }
100
101
102 if (msg->lchan) {
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200103 struct e1inp_sign_link *sign_link =
104 msg->lchan->ts->trx->rsl_link;
105
106 msg->dst = sign_link;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800107 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
108 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200109 "Sending '%s' to MS.\n",
110 sign_link->trx->bts->nr,
111 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800112 gh->proto_discr & 0xf0,
113 gsm48_cc_msg_name(gh->msg_type));
114 else
115 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200116 "Sending 0x%02x to MS.\n",
117 sign_link->trx->bts->nr,
118 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800119 gh->proto_discr, gh->msg_type);
120 }
121
Holger Hans Peter Freyther6bfcad82010-11-10 10:16:02 +0100122 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800123}
Sylvain Munaut28c47162009-12-24 00:27:26 +0100124
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +0800125int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
126{
127 struct gsm48_hdr *gh;
128 struct msgb *ss_notify;
129
130 ss_notify = gsm0480_create_notifySS(message);
131 if (!ss_notify)
132 return -1;
133
134 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
135 uint8_t *data = msgb_push(ss_notify, 1);
136 data[0] = ss_notify->len - 1;
137 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
138 gh->msg_type = GSM48_MT_CC_FACILITY;
139 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
140}
141
Harald Weltefe01ed72012-01-23 16:40:24 +0100142void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100143{
144 if (!conn->sec_operation)
145 return;
146
147 talloc_free(conn->sec_operation);
148 conn->sec_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800149 msc_release_connection(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100150}
151
Harald Weltefe01ed72012-01-23 16:40:24 +0100152void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100153{
Sylvain Munaut28c47162009-12-24 00:27:26 +0100154 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
155 struct gsm_security_operation);
156}
157
Sylvain Munautc0bdbb42010-12-01 22:37:05 +0100158int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
159 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100160{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800161 struct gsm_network *net = conn->bts->network;
162 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100163 struct gsm_security_operation *op;
164 struct gsm_auth_tuple atuple;
165 int status = -1, rc;
166
167 /* Check if we _can_ enable encryption. Cases where we can't:
168 * - Encryption disabled in config
169 * - Channel already secured (nothing to do)
170 * - Subscriber equipment doesn't support configured encryption
171 */
172 if (!net->a5_encryption) {
173 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800174 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100175 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversbergee995352013-07-10 08:58:03 +0200176 status = GSM_SECURITY_ALREADY;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100177 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
178 net->a5_encryption)) {
179 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
180 status = GSM_SECURITY_NOAVAIL;
181 }
182
183 /* If not done yet, try to get info for this user */
184 if (status < 0) {
185 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
186 if (rc <= 0)
187 status = GSM_SECURITY_NOAVAIL;
188 }
189
190 /* Are we done yet ? */
191 if (status >= 0)
192 return cb ?
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800193 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut28c47162009-12-24 00:27:26 +0100194 0;
195
196 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800197 if (conn->sec_operation)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100198 return -EBUSY;
199
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800200 allocate_security_operation(conn);
201 op = conn->sec_operation;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100202 op->cb = cb;
203 op->cb_data = cb_data;
204 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
205
206 /* FIXME: Should start a timer for completion ... */
207
208 /* Then do whatever is needed ... */
Harald Welte857e15d2010-12-23 18:07:49 +0100209 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100210 /* Start authentication */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800211 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte857e15d2010-12-23 18:07:49 +0100212 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100213 /* Start ciphering directly */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100214 return gsm0808_cipher_mode(conn, net->a5_encryption,
215 op->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100216 }
217
218 return -EINVAL; /* not reached */
219}
220
Harald Welte59b04682009-06-10 05:40:52 +0800221static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
222 struct gsm_subscriber *subscriber)
223{
224 if (!subscriber)
225 return 0;
226
227 /*
228 * Do not send accept yet as more information should arrive. Some
229 * phones will not send us the information and we will have to check
230 * what we want to do with that.
231 */
232 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
233 return 0;
234
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200235 switch (subscriber->net->auth_policy) {
236 case GSM_AUTH_POLICY_CLOSED:
237 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200238 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200239 if (subscriber->authorized)
240 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200241 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200242 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800243 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200244 default:
245 return 0;
246 }
Harald Welte59b04682009-06-10 05:40:52 +0800247}
248
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100249static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800250{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100251 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800252 return;
253
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800254 /* No need to keep the connection up */
255 release_anchor(conn);
256
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200257 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100258 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherbed07092010-12-22 08:53:28 +0100259 conn->loc_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800260 msc_release_connection(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800261}
262
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100263static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800264{
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800265 if (conn->loc_operation)
266 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100267 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800268
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100269 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200270 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800271}
272
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100273static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
274 struct msgb *msg, void *data, void *param)
275{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800276 struct gsm_subscriber_connection *conn = data;
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100277 int rc = 0;
278
279 switch (event) {
280 case GSM_SECURITY_AUTH_FAILED:
281 release_loc_updating_req(conn);
282 break;
283
284 case GSM_SECURITY_NOAVAIL:
285 case GSM_SECURITY_SUCCEEDED:
286 /* We're all good */
287 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800288 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800289 if (conn->bts->network->send_mm_info) {
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100290 /* send MM INFO with network name */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800291 rc = gsm48_tx_mm_info(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100292 }
293
294 /* call subscr_update after putting the loc_upd_acc
295 * in the transmit queue, since S_SUBSCR_ATTACHED might
296 * trigger further action like SMS delivery */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800297 subscr_update(conn->subscr, conn->bts,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100298 GSM_SUBSCRIBER_UPDATE_ATTACHED);
299
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +0100300 /*
301 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
302 * MS needs to respond with a TMSI REALLOCATION COMPLETE
303 * (even if the TMSI is the same).
304 */
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100305 break;
306
307 default:
308 rc = -EINVAL;
309 };
310
311 return rc;
312}
313
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100314static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800315{
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100316 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800317 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100318 conn->loc_operation->key_seq,
319 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800320 return 0;
321}
322
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800323void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800324{
Harald Welte03740842009-06-10 23:11:52 +0800325 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800326
327 /* avoid someone issuing a clear */
328 conn->in_release = 1;
329
Harald Welte59b04682009-06-10 05:40:52 +0800330 /*
331 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800332 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800333 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800334 release_loc_updating_req(conn);
Holger Hans Peter Freytherd91346a2010-12-28 14:57:20 +0100335
336 /* We might need to cancel the paging response or such. */
337 if (conn->sec_operation && conn->sec_operation->cb) {
338 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
339 NULL, conn, conn->sec_operation->cb_data);
340 }
341
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800342 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800343 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800344
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100345 /*
346 * Free all transactions that are associated with the released
347 * connection. The transaction code will inform the CC or SMS
348 * facilities that will send the release indications. As part of
349 * the CC REL_IND the remote leg might be released and this will
350 * trigger the call to trans_free. This is something the llist
351 * macro can not handle and we will need to re-iterate the list.
352 *
353 * TODO: Move the trans_list into the subscriber connection and
354 * create a pending list for MT transactions. These exist before
355 * we have a subscriber connection.
356 */
357restart:
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800358 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100359 if (trans->conn == conn) {
Harald Weltec2189a62009-07-23 18:56:43 +0200360 trans_free(trans);
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100361 goto restart;
362 }
Harald Welte03740842009-06-10 23:11:52 +0800363 }
Harald Welte59b04682009-06-10 05:40:52 +0800364}
365
Harald Weltee8d16172010-12-22 23:17:50 +0100366void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
367{
368 struct gsm_trans *trans, *temp;
369
370 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
371
372 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Weltebd0f33c2010-12-23 02:47:53 +0100373 if (trans->protocol == protocol) {
374 trans->callref = 0;
Harald Weltee8d16172010-12-22 23:17:50 +0100375 trans_free(trans);
Harald Weltebd0f33c2010-12-23 02:47:53 +0100376 }
Harald Weltee8d16172010-12-22 23:17:50 +0100377 }
378}
379
Harald Welte59b04682009-06-10 05:40:52 +0800380/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200381int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800382{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800383 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800384 struct msgb *msg;
385
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200386 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800387
388 msg = gsm48_create_loc_upd_rej(cause);
389 if (!msg) {
390 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
391 return -1;
392 }
Harald Welte59b04682009-06-10 05:40:52 +0800393
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800394 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800395
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100396 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100397 "LAC=%u BTS=%u\n", conn->subscr ?
398 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800399 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100400
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800401 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800402}
403
404/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200405int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800406{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800407 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800408 struct msgb *msg = gsm48_msgb_alloc();
409 struct gsm48_hdr *gh;
410 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200411 uint8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800412
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800413 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800414
415 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
416 gh->proto_discr = GSM48_PDISC_MM;
417 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
418
419 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100420 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800421 bts->network->network_code, bts->location_area_code);
422
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200423 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200424 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800425
426 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
427
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200428 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100429
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800430 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800431}
432
Harald Welte59b04682009-06-10 05:40:52 +0800433/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200434static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800435{
436 struct msgb *msg = gsm48_msgb_alloc();
437 struct gsm48_hdr *gh;
438
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800439 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800440
441 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
442 gh->proto_discr = GSM48_PDISC_MM;
443 gh->msg_type = GSM48_MT_MM_ID_REQ;
444 gh->data[0] = id_type;
445
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800446 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800447}
448
Harald Welte59b04682009-06-10 05:40:52 +0800449
450/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800451static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800452{
453 struct gsm48_hdr *gh = msgb_l3(msg);
454 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200455 struct gsm_bts *bts = lchan->ts->trx->bts;
456 struct gsm_network *net = bts->network;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200457 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200458 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800459
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200460 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800461 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
462 mi_type, mi_string);
463
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200464 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welteedf48af2009-12-13 12:39:18 +0100465
Harald Welte59b04682009-06-10 05:40:52 +0800466 switch (mi_type) {
467 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200468 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100469 if (!conn->subscr) {
470 conn->subscr = subscr_get_by_imsi(net, mi_string);
471 if (!conn->subscr)
472 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200473 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100474 if (conn->loc_operation)
475 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800476 break;
477 case GSM_MI_TYPE_IMEI:
478 case GSM_MI_TYPE_IMEISV:
479 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100480 if (conn->subscr) {
481 db_subscriber_assoc_imei(conn->subscr, mi_string);
482 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200483 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100484 if (conn->loc_operation)
485 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800486 break;
487 }
488
489 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100490 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800491}
492
493
494static void loc_upd_rej_cb(void *data)
495{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100496 struct gsm_subscriber_connection *conn = data;
497 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100498 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800499
Holger Hans Peter Freyther0d91c462011-11-07 12:26:29 +0100500 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800501 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800502 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800503}
504
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100505static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800506{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100507 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
508 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200509 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800510}
511
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200512static const char *lupd_name(uint8_t type)
Harald Welte59b04682009-06-10 05:40:52 +0800513{
514 switch (type) {
515 case GSM48_LUPD_NORMAL:
516 return "NORMAL";
517 case GSM48_LUPD_PERIODIC:
518 return "PEROIDOC";
519 case GSM48_LUPD_IMSI_ATT:
520 return "IMSI ATTACH";
521 default:
522 return "UNKNOWN";
523 }
524}
525
Harald Welte59b04682009-06-10 05:40:52 +0800526/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800527static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800528{
529 struct gsm48_hdr *gh = msgb_l3(msg);
530 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800531 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800532 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200533 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200534 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800535
536 lu = (struct gsm48_loc_upd_req *) gh->data;
537
538 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
539
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200540 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800541
Harald Welte79639662009-06-27 02:58:43 +0200542 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800543 lupd_name(lu->type));
544
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200545 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welteedf48af2009-12-13 12:39:18 +0100546
Harald Welte3edc5a92009-12-22 00:41:05 +0100547 switch (lu->type) {
548 case GSM48_LUPD_NORMAL:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200549 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100550 break;
551 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200552 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100553 break;
554 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200555 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100556 break;
557 }
558
Harald Welte59b04682009-06-10 05:40:52 +0800559 /*
560 * Pseudo Spoof detection: Just drop a second/concurrent
561 * location updating request.
562 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100563 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200564 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100565 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800566 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800567 return 0;
568 }
569
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800570 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800571
Sylvain Munaut360fc582010-06-10 13:36:59 +0200572 conn->loc_operation->key_seq = lu->key_seq;
573
Harald Welte59b04682009-06-10 05:40:52 +0800574 switch (mi_type) {
575 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200576 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800577 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200578 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100579 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800580
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200581 /* look up subscriber based on IMSI, create if not found */
582 subscr = subscr_get_by_imsi(bts->network, mi_string);
583 if (!subscr) {
584 subscr = db_create_subscriber(bts->network, mi_string);
585 }
Harald Welte59b04682009-06-10 05:40:52 +0800586 break;
587 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200588 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800589 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200590 subscr = subscr_get_by_tmsi(bts->network,
591 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800592 if (!subscr) {
593 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200594 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100595 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800596 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200597 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200598 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200599 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800600 break;
601 case GSM_MI_TYPE_IMEI:
602 case GSM_MI_TYPE_IMEISV:
603 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200604 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800605 break;
606 default:
Harald Welte79639662009-06-27 02:58:43 +0200607 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800608 break;
609 }
610
Harald Welteccd69362009-07-04 10:18:00 +0200611 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100612 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200613
Harald Welte03740842009-06-10 23:11:52 +0800614 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200615 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800616 /* FIXME: request id? close channel? */
617 return -EINVAL;
618 }
619
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100620 conn->subscr = subscr;
621 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800622
Harald Welteccd69362009-07-04 10:18:00 +0200623 /* check if we can let the subscriber into our network immediately
624 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100625 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800626}
627
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100628/* Turn int into semi-octet representation: 98 => 0x89 */
629static uint8_t bcdify(uint8_t value)
Harald Welte03740842009-06-10 23:11:52 +0800630{
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100631 uint8_t ret;
632
633 ret = value / 10;
634 ret |= (value % 10) << 4;
635
636 return ret;
Harald Welte03740842009-06-10 23:11:52 +0800637}
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100638
Harald Welte03740842009-06-10 23:11:52 +0800639
Harald Welte59b04682009-06-10 05:40:52 +0800640/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800641int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800642{
643 struct msgb *msg = gsm48_msgb_alloc();
644 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800645 struct gsm_network *net = conn->bts->network;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100646 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200647 uint8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200648 int name_len, name_pad;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100649
Harald Welte03740842009-06-10 23:11:52 +0800650 time_t cur_t;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100651 struct tm* gmt_time;
652 struct tm* local_time;
653 int tzunits;
Harald Welte59b04682009-06-10 05:40:52 +0800654
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800655 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800656
657 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
658 gh->proto_discr = GSM48_PDISC_MM;
659 gh->msg_type = GSM48_MT_MM_INFO;
660
661 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200662#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800663 name_len = strlen(net->name_long);
664 /* 10.5.3.5a */
665 ptr8 = msgb_put(msg, 3);
666 ptr8[0] = GSM48_IE_NAME_LONG;
667 ptr8[1] = name_len*2 +1;
668 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
669
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200670 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800671 for (i = 0; i < name_len; i++)
672 ptr16[i] = htons(net->name_long[i]);
673
674 /* FIXME: Use Cell Broadcast, not UCS-2, since
675 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200676#endif
677 name_len = (strlen(net->name_long)*7)/8;
678 name_pad = (8 - strlen(net->name_long)*7)%8;
679 if (name_pad > 0)
680 name_len++;
681 /* 10.5.3.5a */
682 ptr8 = msgb_put(msg, 3);
683 ptr8[0] = GSM48_IE_NAME_LONG;
684 ptr8[1] = name_len +1;
685 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
686
687 ptr8 = msgb_put(msg, name_len);
688 gsm_7bit_encode(ptr8, net->name_long);
689
Harald Welte59b04682009-06-10 05:40:52 +0800690 }
691
692 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200693#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800694 name_len = strlen(net->name_short);
695 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200696 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200697 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800698 ptr8[1] = name_len*2 + 1;
699 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
700
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200701 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800702 for (i = 0; i < name_len; i++)
703 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200704#endif
705 name_len = (strlen(net->name_short)*7)/8;
706 name_pad = (8 - strlen(net->name_short)*7)%8;
707 if (name_pad > 0)
708 name_len++;
709 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200710 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmannc8188202009-08-13 03:42:07 +0200711 ptr8[0] = GSM48_IE_NAME_SHORT;
712 ptr8[1] = name_len +1;
713 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
714
715 ptr8 = msgb_put(msg, name_len);
716 gsm_7bit_encode(ptr8, net->name_short);
717
Harald Welte59b04682009-06-10 05:40:52 +0800718 }
719
Harald Welte59b04682009-06-10 05:40:52 +0800720 /* Section 10.5.3.9 */
721 cur_t = time(NULL);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100722 gmt_time = gmtime(&cur_t);
723
Harald Welte59b04682009-06-10 05:40:52 +0800724 ptr8 = msgb_put(msg, 8);
725 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100726 ptr8[1] = bcdify(gmt_time->tm_year % 100);
727 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
728 ptr8[3] = bcdify(gmt_time->tm_mday);
729 ptr8[4] = bcdify(gmt_time->tm_hour);
730 ptr8[5] = bcdify(gmt_time->tm_min);
731 ptr8[6] = bcdify(gmt_time->tm_sec);
732
Harald Welte5c3abf92012-07-08 16:48:11 +0200733 if (bts->tz.override) {
734 /* Convert tz.hr and tz.mn to units */
735 if (bts->tz.hr < 0) {
736 tzunits = ((bts->tz.hr/-1)*4);
737 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100738 ptr8[7] = bcdify(tzunits);
739 /* Set negative time */
740 ptr8[7] |= 0x08;
741 }
742 else {
Harald Welte5c3abf92012-07-08 16:48:11 +0200743 tzunits = bts->tz.hr*4;
744 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100745 ptr8[7] = bcdify(tzunits);
746 }
747 }
748 else {
749 /* Need to get GSM offset and convert into 15 min units */
750 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
751 local_time = localtime(&cur_t);
Harald Welteeaf4a522012-04-08 16:59:24 +0200752#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100753 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welteeaf4a522012-04-08 16:59:24 +0200754#else
755#warning find a portable way to obtain the timezone offset
756 tzunits = 0;
757#endif
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100758 if (tzunits < 0) {
759 tzunits = tzunits/-1;
760 ptr8[7] = bcdify(tzunits);
761 /* Flip it to negative */
762 ptr8[7] |= 0x08;
763 }
764 else
765 ptr8[7] = bcdify(tzunits);
766 }
Harald Welte59b04682009-06-10 05:40:52 +0800767
Daniel Willmannc8188202009-08-13 03:42:07 +0200768 DEBUGP(DMM, "-> MM INFO\n");
769
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800770 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800771}
772
Harald Welte36970f62009-08-12 22:56:50 +0200773/* Section 9.2.2 */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200774int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200775{
776 struct msgb *msg = gsm48_msgb_alloc();
777 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200778 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200779
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200780 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200781
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800782 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200783 gh->proto_discr = GSM48_PDISC_MM;
784 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
785
Sylvain Munaut0789c592009-12-24 00:23:46 +0100786 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200787
Harald Welte36970f62009-08-12 22:56:50 +0200788 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200789 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200790 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200791
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800792 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200793}
794
795/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800796int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200797{
798 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800799 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200800}
801
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800802static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800803{
804 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800805 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte59b04682009-06-10 05:40:52 +0800806}
807
808/* 9.2.6 CM service reject */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100809static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +0800810 enum gsm48_reject_value value)
811{
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800812 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800813
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800814 msg = gsm48_create_mm_serv_rej(value);
815 if (!msg) {
816 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
817 return -1;
818 }
Harald Welte59b04682009-06-10 05:40:52 +0800819
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800820 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100821 msg->lchan = conn->lchan;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800822 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800823}
824
Sylvain Munautbadc7112009-12-24 00:28:46 +0100825static int _gsm48_rx_mm_serv_req_sec_cb(
826 unsigned int hooknum, unsigned int event,
827 struct msgb *msg, void *data, void *param)
828{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800829 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100830 int rc = 0;
831
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +0200832 /* auth failed or succeeded, the timer was stopped */
833 conn->expire_timer_stopped = 1;
834
Sylvain Munautbadc7112009-12-24 00:28:46 +0100835 switch (event) {
836 case GSM_SECURITY_AUTH_FAILED:
837 /* Nothing to do */
838 break;
839
840 case GSM_SECURITY_NOAVAIL:
Andreas Eversbergee995352013-07-10 08:58:03 +0200841 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800842 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100843 break;
844
845 case GSM_SECURITY_SUCCEEDED:
846 /* nothing to do. CIPHER MODE COMMAND is
847 * implicit CM SERV ACK */
848 break;
849
850 default:
851 rc = -EINVAL;
852 };
853
854 return rc;
855}
856
Harald Welte59b04682009-06-10 05:40:52 +0800857/*
858 * Handle CM Service Requests
859 * a) Verify that the packet is long enough to contain the information
860 * we require otherwsie reject with INCORRECT_MESSAGE
861 * b) Try to parse the TMSI. If we do not have one reject
862 * c) Check that we know the subscriber with the TMSI otherwise reject
863 * with a HLR cause
864 * d) Set the subscriber on the gsm_lchan and accept
865 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800866static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800867{
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200868 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200869 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800870
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800871 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800872 struct gsm_subscriber *subscr;
873 struct gsm48_hdr *gh = msgb_l3(msg);
874 struct gsm48_service_request *req =
875 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800876 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200877 uint8_t classmark2_len = gh->data[1];
878 uint8_t *classmark2 = gh->data+2;
879 uint8_t mi_len = *(classmark2 + classmark2_len);
880 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte59b04682009-06-10 05:40:52 +0800881
882 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
883 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
884 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800885 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800886 GSM48_REJECT_INCORRECT_MESSAGE);
887 }
888
889 if (msg->data_len < req->mi_len + 6) {
890 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800891 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800892 GSM48_REJECT_INCORRECT_MESSAGE);
893 }
894
895 mi_type = mi[0] & GSM_MI_TYPE_MASK;
896 if (mi_type != GSM_MI_TYPE_TMSI) {
897 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800898 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800899 GSM48_REJECT_INCORRECT_MESSAGE);
900 }
901
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200902 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800903 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
904 req->cm_service_type, mi_type, mi_string);
905
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200906 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welteedf48af2009-12-13 12:39:18 +0100907
Harald Welte583ceaf2009-08-10 10:12:45 +0200908 if (is_siemens_bts(bts))
909 send_siemens_mrpci(msg->lchan, classmark2-1);
910
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200911 subscr = subscr_get_by_tmsi(bts->network,
912 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800913
914 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
915 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800916 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800917 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Holger Hans Peter Freytherfe036952013-01-01 17:18:27 +0100918 if (subscr->lac == GSM_LAC_RESERVED_DETACHED)
Sylvain Munautf1087412012-12-28 00:33:04 +0100919 /* If the subscriber is not attached, reject service */
920 return gsm48_tx_mm_serv_rej(conn,
921 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte59b04682009-06-10 05:40:52 +0800922
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800923 if (!conn->subscr)
924 conn->subscr = subscr;
925 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100926 subscr_put(subscr); /* lchan already has a ref, don't need another one */
927 else {
Harald Welte59b04682009-06-10 05:40:52 +0800928 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
929 subscr_put(subscr);
930 }
931
Harald Weltef6845a72009-07-05 14:08:13 +0200932 subscr->equipment.classmark2_len = classmark2_len;
933 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
934 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800935
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +0200936 /* we will send a MM message soon */
937 conn->expire_timer_stopped = 1;
938
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800939 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +0100940 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800941}
942
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200943static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800944{
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200945 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800946 struct gsm48_hdr *gh = msgb_l3(msg);
947 struct gsm48_imsi_detach_ind *idi =
948 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200949 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200950 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800951 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800952
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200953 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100954 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte59b04682009-06-10 05:40:52 +0800955 mi_type, mi_string);
956
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200957 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100958
Harald Welte59b04682009-06-10 05:40:52 +0800959 switch (mi_type) {
960 case GSM_MI_TYPE_TMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100961 DEBUGPC(DMM, "\n");
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200962 subscr = subscr_get_by_tmsi(bts->network,
963 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800964 break;
965 case GSM_MI_TYPE_IMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100966 DEBUGPC(DMM, "\n");
Harald Welte75350412009-07-23 18:46:00 +0200967 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800968 break;
969 case GSM_MI_TYPE_IMEI:
970 case GSM_MI_TYPE_IMEISV:
971 /* no sim card... FIXME: what to do ? */
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100972 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800973 break;
974 default:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100975 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800976 break;
977 }
978
979 if (subscr) {
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200980 subscr_update(subscr, bts,
Harald Welte59b04682009-06-10 05:40:52 +0800981 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100982 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200983
984 subscr->equipment.classmark1 = idi->classmark1;
985 db_sync_equipment(&subscr->equipment);
986
Harald Welte59b04682009-06-10 05:40:52 +0800987 subscr_put(subscr);
988 } else
989 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
990
Harald Welte43250dd2009-12-20 09:58:40 +0100991 /* FIXME: iterate over all transactions and release them,
992 * imagine an IMSI DETACH happening during an active call! */
993
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200994 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800995 return 0;
996}
997
998static int gsm48_rx_mm_status(struct msgb *msg)
999{
1000 struct gsm48_hdr *gh = msgb_l3(msg);
1001
1002 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1003
1004 return 0;
1005}
1006
Sylvain Munaut28c47162009-12-24 00:27:26 +01001007/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001008static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +01001009{
1010 struct gsm48_hdr *gh = msgb_l3(msg);
1011 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001012 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001013
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001014 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001015 osmo_hexdump(ar->sres, 4));
Sylvain Munaut28c47162009-12-24 00:27:26 +01001016
1017 /* Safety check */
1018 if (!conn->sec_operation) {
1019 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1020 return -EIO;
1021 }
1022
1023 /* Validate SRES */
1024 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001025 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001026 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001027
1028 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001029 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001030
Sylvain Munaut28c47162009-12-24 00:27:26 +01001031 if (cb)
1032 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001033 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001034
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001035 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001036 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001037 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001038 }
1039
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001040 DEBUGPC(DMM, "OK\n");
1041
Sylvain Munaut28c47162009-12-24 00:27:26 +01001042 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +01001043 return gsm0808_cipher_mode(conn, net->a5_encryption,
1044 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001045}
1046
Harald Welte59b04682009-06-10 05:40:52 +08001047/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001048static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001049{
1050 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +08001051 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08001052
1053 switch (gh->msg_type & 0xbf) {
1054 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +02001055 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001056 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001057 break;
1058 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001059 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001060 break;
1061 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001062 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001063 break;
1064 case GSM48_MT_MM_STATUS:
1065 rc = gsm48_rx_mm_status(msg);
1066 break;
1067 case GSM48_MT_MM_TMSI_REALL_COMPL:
1068 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001069 conn->subscr ?
1070 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +08001071 "unknown subscriber");
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +01001072 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +08001073 break;
1074 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001075 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001076 break;
1077 case GSM48_MT_MM_CM_REEST_REQ:
1078 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1079 break;
1080 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001081 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001082 break;
1083 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001084 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001085 gh->msg_type);
1086 break;
1087 }
1088
1089 return rc;
1090}
1091
1092/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001093static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001094{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001095 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001096 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001097 struct gsm48_pag_resp *resp;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001098 uint8_t *classmark2_lv = gh->data + 1;
1099 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001100 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001101 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001102 int rc = 0;
1103
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001104 resp = (struct gsm48_pag_resp *) &gh->data[0];
1105 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1106 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001107 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1108 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001109
Harald Welte59b04682009-06-10 05:40:52 +08001110 switch (mi_type) {
1111 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001112 subscr = subscr_get_by_tmsi(bts->network,
1113 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001114 break;
1115 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001116 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001117 break;
1118 }
1119
1120 if (!subscr) {
1121 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1122 /* FIXME: request id? close channel? */
1123 return -EINVAL;
1124 }
1125 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001126 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001127
Harald Weltef6845a72009-07-05 14:08:13 +02001128 subscr->equipment.classmark2_len = *classmark2_lv;
1129 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1130 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001131
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +02001132 /* We received a paging */
1133 conn->expire_timer_stopped = 1;
1134
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001135 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001136 return rc;
1137}
1138
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001139static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001140{
1141 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001142 uint8_t apdu_id_flags;
1143 uint8_t apdu_len;
1144 uint8_t *apdu_data;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001145
1146 apdu_id_flags = gh->data[0];
1147 apdu_len = gh->data[1];
1148 apdu_data = gh->data+2;
1149
Harald Welted4f21662012-07-16 13:22:19 +02001150 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001151 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)b5103922009-08-15 23:32:44 +02001152
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001153 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001154}
1155
Harald Welte59b04682009-06-10 05:40:52 +08001156/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001157static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001158{
1159 struct gsm48_hdr *gh = msgb_l3(msg);
1160 int rc = 0;
1161
1162 switch (gh->msg_type) {
Harald Welte59b04682009-06-10 05:40:52 +08001163 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001164 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001165 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001166 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001167 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001168 break;
Harald Welte59b04682009-06-10 05:40:52 +08001169 default:
Harald Weltefe01ed72012-01-23 16:40:24 +01001170 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte60e5f4f2009-12-24 12:13:17 +01001171 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001172 break;
1173 }
1174
1175 return rc;
1176}
1177
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001178int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1179 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001180{
1181 struct msgb *msg = gsm48_msgb_alloc();
1182 struct gsm48_hdr *gh;
1183
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001184 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001185
1186 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1187 apdu_id, apdu_len);
1188
1189 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1190 gh->proto_discr = GSM48_PDISC_RR;
1191 gh->msg_type = GSM48_MT_RR_APP_INFO;
1192 gh->data[0] = apdu_id;
1193 gh->data[1] = apdu_len;
1194 memcpy(gh->data+2, apdu, apdu_len);
1195
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001196 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001197}
1198
Harald Welte59b04682009-06-10 05:40:52 +08001199/* Call Control */
1200
1201/* The entire call control code is written in accordance with Figure 7.10c
1202 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1203 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1204 * it for voice */
1205
Harald Welte03740842009-06-10 23:11:52 +08001206static void new_cc_state(struct gsm_trans *trans, int state)
1207{
1208 if (state > 31 || state < 0)
1209 return;
1210
1211 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001212 gsm48_cc_state_name(trans->cc.state),
1213 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001214
Harald Weltec2189a62009-07-23 18:56:43 +02001215 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001216}
1217
1218static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001219{
1220 struct msgb *msg = gsm48_msgb_alloc();
1221 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001222 uint8_t *cause, *call_state;
Harald Welte59b04682009-06-10 05:40:52 +08001223
Harald Welte59b04682009-06-10 05:40:52 +08001224 gh->msg_type = GSM48_MT_CC_STATUS;
1225
1226 cause = msgb_put(msg, 3);
1227 cause[0] = 2;
1228 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1229 cause[2] = 0x80 | 30; /* response to status inquiry */
1230
1231 call_state = msgb_put(msg, 1);
1232 call_state[0] = 0xc0 | 0x00;
1233
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001234 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001235}
1236
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001237static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001238 uint8_t pdisc, uint8_t msg_type)
Harald Welte59b04682009-06-10 05:40:52 +08001239{
1240 struct msgb *msg = gsm48_msgb_alloc();
1241 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1242
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001243 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001244
1245 gh->proto_discr = pdisc;
1246 gh->msg_type = msg_type;
1247
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001248 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001249}
1250
Harald Welte03740842009-06-10 23:11:52 +08001251static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1252{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001253 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltec2189a62009-07-23 18:56:43 +02001254 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001255 osmo_timer_del(&trans->cc.timer);
Harald Weltec2189a62009-07-23 18:56:43 +02001256 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001257 }
1258}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001259
Harald Welte03740842009-06-10 23:11:52 +08001260static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1261 int msg_type, struct gsm_mncc *mncc)
1262{
1263 struct msgb *msg;
Harald Welte17b56522010-12-22 21:45:05 +01001264 unsigned char *data;
Harald Welte03740842009-06-10 23:11:52 +08001265
1266 if (trans)
Harald Weltec51b1922010-12-26 19:06:37 +01001267 if (trans->conn && trans->conn->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001268 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001269 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001270 trans->conn->lchan->ts->trx->bts->nr,
1271 trans->conn->lchan->ts->trx->nr,
1272 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001273 (trans->subscr)?(trans->subscr->extension):"-",
1274 get_mncc_name(msg_type));
1275 else
1276 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1277 "Sending '%s' to MNCC.\n",
1278 (trans->subscr)?(trans->subscr->extension):"-",
1279 get_mncc_name(msg_type));
1280 else
1281 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1282 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1283
1284 mncc->msg_type = msg_type;
1285
Harald Welte9cfc9352009-06-26 19:39:35 +02001286 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001287 if (!msg)
1288 return -ENOMEM;
Harald Welte17b56522010-12-22 21:45:05 +01001289
1290 data = msgb_put(msg, sizeof(struct gsm_mncc));
1291 memcpy(data, mncc, sizeof(struct gsm_mncc));
1292
Harald Weltebe0cb682010-12-22 23:43:29 +01001293 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001294
1295 return 0;
1296}
1297
1298int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001299 uint32_t callref, int location, int value)
Harald Welte03740842009-06-10 23:11:52 +08001300{
1301 struct gsm_mncc rel;
1302
Harald Weltecb0595f2009-06-12 01:54:08 +08001303 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001304 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001305 mncc_set_cause(&rel, location, value);
Andreas Eversberg59e79ee2013-01-25 08:38:29 +01001306 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1307 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08001308 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1309}
1310
Harald Weltec2189a62009-07-23 18:56:43 +02001311/* Call Control Specific transaction release.
1312 * gets called by trans_free, DO NOT CALL YOURSELF! */
1313void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001314{
Harald Welte03740842009-06-10 23:11:52 +08001315 gsm48_stop_cc_timer(trans);
1316
1317 /* send release to L4, if callref still exists */
1318 if (trans->callref) {
1319 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001320 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001321 GSM48_CAUSE_LOC_PRN_S_LU,
1322 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001323 }
Harald Weltec2189a62009-07-23 18:56:43 +02001324 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001325 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001326 if (trans->conn)
1327 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001328}
1329
1330static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001331
Harald Welte59b04682009-06-10 05:40:52 +08001332/* call-back from paging the B-end of the connection */
1333static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001334 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001335{
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001336 int found = 0;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001337 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001338 struct gsm_network **paging_request = param, *net;
Harald Welte03740842009-06-10 23:11:52 +08001339 struct gsm_trans *transt, *tmp;
Harald Welte1ff81b52009-06-26 20:17:06 +02001340
Harald Welte59b04682009-06-10 05:40:52 +08001341 if (hooknum != GSM_HOOK_RR_PAGING)
1342 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001343
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001344 net = *paging_request;
Harald Welte03740842009-06-10 23:11:52 +08001345 if (!net) {
1346 DEBUGP(DCC, "Error Network not set!\n");
1347 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001348 }
1349
Harald Welte03740842009-06-10 23:11:52 +08001350 /* check all tranactions (without lchan) for subscriber */
1351 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001352 if (transt->paging_request != paging_request || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001353 continue;
1354 switch (event) {
1355 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001356 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001357 break;
1358 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001359 transt->subscr->extension);
1360 found = 1;
Harald Welte03740842009-06-10 23:11:52 +08001361 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001362 if (!transt->conn) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001363 transt->paging_request = NULL;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001364 transt->conn = conn;
Holger Hans Peter Freytherfb68bf32010-12-29 16:28:33 +01001365 conn->put_channel = 1;
Harald Welte03740842009-06-10 23:11:52 +08001366 }
1367 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001368 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001369 break;
1370 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001371 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001372 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001373 transt->subscr->extension);
Harald Welte03740842009-06-10 23:11:52 +08001374 /* Temporarily out of order */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001375 found = 1;
Harald Welte0abe5a62009-07-23 19:06:52 +02001376 mncc_release_ind(transt->subscr->net, transt,
1377 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001378 GSM48_CAUSE_LOC_PRN_S_LU,
1379 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001380 transt->callref = 0;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001381 transt->paging_request = NULL;
Harald Weltec2189a62009-07-23 18:56:43 +02001382 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001383 break;
1384 }
1385 }
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001386
1387 talloc_free(paging_request);
1388
1389 /*
1390 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1391 * failure and then the subscr will be poke again. This needs a lot of fixing
1392 * in the subscriber queue code.
1393 */
1394 if (!found && conn)
1395 conn->put_channel = 1;
Harald Welte59b04682009-06-10 05:40:52 +08001396 return 0;
1397}
1398
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001399static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welte3971ad52009-12-19 22:23:05 +01001400
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001401/* handle audio path for handover */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001402static int switch_for_handover(struct gsm_lchan *old_lchan,
1403 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001404{
1405 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001406
1407 if (ipacc_rtp_direct) {
1408 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1409 return 0;
1410 }
1411
1412 /* RTP Proxy mode */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001413 new_rs = new_lchan->abis_ip.rtp_socket;
1414 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001415
1416 if (!new_rs) {
1417 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1418 return -EIO;
1419 }
1420
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001421 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001422
1423 if (!old_rs) {
1424 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1425 return -EIO;
1426 }
1427
1428 /* copy rx_action and reference to other sock */
1429 new_rs->rx_action = old_rs->rx_action;
1430 new_rs->tx_action = old_rs->tx_action;
1431 new_rs->transmit = old_rs->transmit;
1432
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001433 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001434 case RTP_PROXY:
1435 other_rs = old_rs->proxy.other_sock;
1436 rtp_socket_proxy(new_rs, other_rs);
1437 /* delete reference to other end socket to prevent
1438 * rtp_socket_free() from removing the inverse reference */
1439 old_rs->proxy.other_sock = NULL;
1440 break;
1441 case RTP_RECV_UPSTREAM:
1442 new_rs->receive = old_rs->receive;
1443 break;
1444 case RTP_NONE:
1445 break;
1446 }
1447
1448 return 0;
1449}
1450
Harald Welte3c062072009-07-28 18:25:29 +02001451/* some other part of the code sends us a signal */
1452static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1453 void *handler_data, void *signal_data)
1454{
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001455 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte3c062072009-07-28 18:25:29 +02001456 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001457 struct gsm_network *net;
1458 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001459
1460 if (subsys != SS_ABISIP)
1461 return 0;
1462
1463 /* in case we use direct BTS-to-BTS RTP */
1464 if (ipacc_rtp_direct)
1465 return 0;
1466
Harald Welte3c062072009-07-28 18:25:29 +02001467 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001468 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001469 /* in case we don't use direct BTS-to-BTS RTP */
1470 /* the BTS has successfully bound a TCH to a local ip/port,
1471 * which means we can connect our UDP socket to it */
1472 if (lchan->abis_ip.rtp_socket) {
1473 rtp_socket_free(lchan->abis_ip.rtp_socket);
1474 lchan->abis_ip.rtp_socket = NULL;
1475 }
1476
1477 lchan->abis_ip.rtp_socket = rtp_socket_create();
1478 if (!lchan->abis_ip.rtp_socket)
1479 return -EIO;
1480
1481 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1482 lchan->abis_ip.bound_ip,
1483 lchan->abis_ip.bound_port);
1484 if (rc < 0)
1485 return -EIO;
1486
Harald Welte3971ad52009-12-19 22:23:05 +01001487 /* check if any transactions on this lchan still have
1488 * a tch_recv_mncc request pending */
1489 net = lchan->ts->trx->bts->network;
1490 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001491 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001492 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1493 tch_recv_mncc(net, trans->callref, 1);
1494 }
1495 }
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001496
1497 /*
1498 * TODO: this appears to be too early? Why not until after
1499 * the handover detect or the handover complete?
1500 *
1501 * Do we have a handover pending for this new lchan? In that
1502 * case re-route the audio from the old channel to the new one.
1503 */
1504 old_lchan = bsc_handover_pending(lchan);
1505 if (old_lchan)
1506 switch_for_handover(old_lchan, lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001507 break;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001508 case S_ABISIP_DLCX_IND:
1509 /* the BTS tells us a RTP stream has been disconnected */
1510 if (lchan->abis_ip.rtp_socket) {
1511 rtp_socket_free(lchan->abis_ip.rtp_socket);
1512 lchan->abis_ip.rtp_socket = NULL;
1513 }
1514
1515 break;
Harald Welte3c062072009-07-28 18:25:29 +02001516 }
1517
1518 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001519}
1520
Harald Welte59b04682009-06-10 05:40:52 +08001521/* map two ipaccess RTP streams onto each other */
1522static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1523{
1524 struct gsm_bts *bts = lchan->ts->trx->bts;
1525 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001526 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001527
1528 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1529 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1530 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1531
1532 if (bts->type != remote_bts->type) {
Harald Welte8099a712011-08-09 21:53:20 +02001533 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte59b04682009-06-10 05:40:52 +08001534 return -EINVAL;
1535 }
Harald Welte3971ad52009-12-19 22:23:05 +01001536
1537 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001538 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001539 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001540 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3c062072009-07-28 18:25:29 +02001541 if (!ipacc_rtp_direct) {
1542 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001543 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001544 if (rc < 0)
1545 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001546 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte30f93fb2011-02-19 16:48:17 +01001547 if (rc < 0)
1548 return rc;
Harald Welte3c062072009-07-28 18:25:29 +02001549 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301550 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1551 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001552 } else {
1553 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301554 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1555 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301556 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001557 if (rc < 0)
1558 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301559 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1560 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301561 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001562 }
Harald Welte59b04682009-06-10 05:40:52 +08001563 break;
1564 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001565 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar49c843e2011-07-28 00:01:50 +02001566 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte59b04682009-06-10 05:40:52 +08001567 trau_mux_map_lchan(lchan, remote_lchan);
1568 break;
1569 default:
Harald Welte8099a712011-08-09 21:53:20 +02001570 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001571 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001572 }
1573
1574 return 0;
1575}
1576
Harald Welte03740842009-06-10 23:11:52 +08001577/* bridge channels of two transactions */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001578static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001579{
Harald Weltec2189a62009-07-23 18:56:43 +02001580 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1581 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001582
Harald Welte03740842009-06-10 23:11:52 +08001583 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001584 return -EIO;
1585
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001586 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001587 return -EIO;
1588
1589 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001590 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001591}
1592
Harald Welte3971ad52009-12-19 22:23:05 +01001593/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001594static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001595{
1596 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001597 struct gsm_lchan *lchan;
1598 struct gsm_bts *bts;
1599 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001600
Harald Welte03740842009-06-10 23:11:52 +08001601 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001602 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001603 if (!trans)
1604 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001605 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001606 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001607 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001608 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001609
Harald Welte3971ad52009-12-19 22:23:05 +01001610 switch (bts->type) {
1611 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001612 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3971ad52009-12-19 22:23:05 +01001613 if (ipacc_rtp_direct) {
Harald Welte8099a712011-08-09 21:53:20 +02001614 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welte3971ad52009-12-19 22:23:05 +01001615 return -EINVAL;
1616 }
1617 /* in case, we don't have a RTP socket yet, we note this
1618 * in the transaction and try later */
1619 if (!lchan->abis_ip.rtp_socket) {
1620 trans->tch_recv = enable;
1621 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1622 return 0;
1623 }
1624 if (enable) {
1625 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001626 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001627 if (rc < 0)
1628 return rc;
1629 /* assign socket to application interface */
1630 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1631 net, callref);
1632 } else
1633 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1634 net, 0);
1635 break;
1636 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001637 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02001638 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte3971ad52009-12-19 22:23:05 +01001639 if (enable)
1640 return trau_recv_lchan(lchan, callref);
1641 return trau_mux_unmap(NULL, callref);
1642 break;
1643 default:
Harald Welte8099a712011-08-09 21:53:20 +02001644 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001645 return -EINVAL;
1646 }
1647
1648 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001649}
1650
Harald Welte03740842009-06-10 23:11:52 +08001651static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1652{
1653 DEBUGP(DCC, "-> STATUS ENQ\n");
1654 return gsm48_cc_tx_status(trans, msg);
1655}
1656
1657static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1658static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1659
1660static void gsm48_cc_timeout(void *arg)
1661{
1662 struct gsm_trans *trans = arg;
1663 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001664 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1665 int mo_location = GSM48_CAUSE_LOC_USER;
1666 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1667 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001668 struct gsm_mncc mo_rel, l4_rel;
1669
1670 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1671 mo_rel.callref = trans->callref;
1672 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1673 l4_rel.callref = trans->callref;
1674
Harald Weltec2189a62009-07-23 18:56:43 +02001675 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001676 case 0x303:
1677 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001678 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001679 break;
1680 case 0x310:
1681 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001682 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001683 break;
1684 case 0x313:
1685 disconnect = 1;
1686 /* unknown, did not find it in the specs */
1687 break;
1688 case 0x301:
1689 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001690 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001691 break;
1692 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001693 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001694 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001695 gsm48_cc_tx_release(trans, &trans->cc.msg);
1696 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001697 break; /* stay in release state */
1698 }
Harald Weltec2189a62009-07-23 18:56:43 +02001699 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001700 return;
1701// release = 1;
1702// l4_cause = 14;
1703// break;
1704 case 0x306:
1705 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001706 mo_cause = trans->cc.msg.cause.value;
1707 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001708 break;
1709 case 0x323:
1710 disconnect = 1;
1711 break;
1712 default:
1713 release = 1;
1714 }
1715
1716 if (release && trans->callref) {
1717 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001718 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001719 l4_location, l4_cause);
1720 trans->callref = 0;
1721 }
1722
1723 if (disconnect && trans->callref) {
1724 /* process disconnect towards layer 4 */
1725 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001726 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001727 }
1728
1729 /* process disconnect towards mobile station */
1730 if (disconnect || release) {
1731 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001732 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1733 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1734 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001735 mo_rel.cause.diag_len = 3;
1736
1737 if (disconnect)
1738 gsm48_cc_tx_disconnect(trans, &mo_rel);
1739 if (release)
1740 gsm48_cc_tx_release(trans, &mo_rel);
1741 }
1742
1743}
1744
1745static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1746 int sec, int micro)
1747{
1748 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001749 trans->cc.timer.cb = gsm48_cc_timeout;
1750 trans->cc.timer.data = trans;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001751 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltec2189a62009-07-23 18:56:43 +02001752 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001753}
1754
1755static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1756{
1757 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001758 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte03740842009-06-10 23:11:52 +08001759 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1760 struct tlv_parsed tp;
1761 struct gsm_mncc setup;
1762
1763 memset(&setup, 0, sizeof(struct gsm_mncc));
1764 setup.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001765 if (trans->conn && trans->conn->lchan)
1766 setup.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001767 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001768 /* emergency setup is identified by msg_type */
1769 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1770 setup.emergency = 1;
1771
1772 /* use subscriber as calling party number */
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02001773 setup.fields |= MNCC_F_CALLING;
1774 strncpy(setup.calling.number, trans->subscr->extension,
1775 sizeof(setup.calling.number)-1);
1776 strncpy(setup.imsi, trans->subscr->imsi,
1777 sizeof(setup.imsi)-1);
1778
Harald Welte03740842009-06-10 23:11:52 +08001779 /* bearer capability */
1780 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1781 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001782 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001783 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1784 }
1785 /* facility */
1786 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1787 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001788 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001789 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1790 }
1791 /* called party bcd number */
1792 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1793 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001794 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001795 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1796 }
1797 /* user-user */
1798 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1799 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001800 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001801 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1802 }
1803 /* ss-version */
1804 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1805 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001806 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001807 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1808 }
1809 /* CLIR suppression */
1810 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1811 setup.clir.sup = 1;
1812 /* CLIR invocation */
1813 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1814 setup.clir.inv = 1;
1815 /* cc cap */
1816 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1817 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001818 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001819 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1820 }
1821
Harald Welte03740842009-06-10 23:11:52 +08001822 new_cc_state(trans, GSM_CSTATE_INITIATED);
1823
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001824 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1825 subscr_name(trans->subscr), trans->subscr->extension,
1826 setup.called.number);
1827
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001828 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001829
Harald Welte03740842009-06-10 23:11:52 +08001830 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001831 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001832
Harald Welteca745e22009-07-29 12:10:35 +02001833 /* MNCC code will modify the channel asynchronously, we should
1834 * ipaccess-bind only after the modification has been made to the
1835 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001836 return 0;
1837}
1838
1839static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001840{
1841 struct msgb *msg = gsm48_msgb_alloc();
1842 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001843 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001844 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001845
1846 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1847
Harald Welte03740842009-06-10 23:11:52 +08001848 /* transaction id must not be assigned */
1849 if (trans->transaction_id != 0xff) { /* unasssigned */
1850 DEBUGP(DCC, "TX Setup with assigned transaction. "
1851 "This is not allowed!\n");
1852 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001853 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001854 GSM48_CAUSE_LOC_PRN_S_LU,
1855 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001856 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001857 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001858 return rc;
1859 }
1860
1861 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001862 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1863 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001864 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001865 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001866 GSM48_CAUSE_LOC_PRN_S_LU,
1867 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001868 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001869 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001870 return rc;
1871 }
Harald Welte165fc332009-07-23 21:36:44 +02001872 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001873
Harald Welte59b04682009-06-10 05:40:52 +08001874 gh->msg_type = GSM48_MT_CC_SETUP;
1875
Harald Welte03740842009-06-10 23:11:52 +08001876 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001877
Harald Welte03740842009-06-10 23:11:52 +08001878 /* bearer capability */
1879 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001880 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001881 /* facility */
1882 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001883 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001884 /* progress */
1885 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001886 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001887 /* calling party BCD number */
1888 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001889 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001890 /* called party BCD number */
1891 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001892 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001893 /* user-user */
1894 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001895 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001896 /* redirecting party BCD number */
1897 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001898 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001899 /* signal */
1900 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001901 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001902
1903 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001904
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001905 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001906
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001907 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001908}
1909
Harald Welte03740842009-06-10 23:11:52 +08001910static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1911{
1912 struct gsm48_hdr *gh = msgb_l3(msg);
1913 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1914 struct tlv_parsed tp;
1915 struct gsm_mncc call_conf;
1916
1917 gsm48_stop_cc_timer(trans);
1918 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1919
1920 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1921 call_conf.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001922 if (trans->conn && trans->conn->lchan)
1923 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001924 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001925#if 0
1926 /* repeat */
1927 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1928 call_conf.repeat = 1;
1929 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1930 call_conf.repeat = 2;
1931#endif
1932 /* bearer capability */
1933 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1934 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001935 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001936 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1937 }
1938 /* cause */
1939 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1940 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001941 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001942 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1943 }
1944 /* cc cap */
1945 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1946 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001947 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001948 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1949 }
1950
1951 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1952
Harald Welte0abe5a62009-07-23 19:06:52 +02001953 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1954 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001955}
1956
1957static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1958{
1959 struct gsm_mncc *proceeding = arg;
1960 struct msgb *msg = gsm48_msgb_alloc();
1961 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1962
Harald Welte03740842009-06-10 23:11:52 +08001963 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1964
1965 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1966
1967 /* bearer capability */
1968 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001969 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001970 /* facility */
1971 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001972 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001973 /* progress */
1974 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001975 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001976
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001977 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001978}
1979
1980static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1981{
1982 struct gsm48_hdr *gh = msgb_l3(msg);
1983 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1984 struct tlv_parsed tp;
1985 struct gsm_mncc alerting;
1986
1987 gsm48_stop_cc_timer(trans);
1988 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1989
1990 memset(&alerting, 0, sizeof(struct gsm_mncc));
1991 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001992 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001993 /* facility */
1994 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1995 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001996 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001997 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1998 }
1999
2000 /* progress */
2001 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2002 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01002003 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08002004 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2005 }
2006 /* ss-version */
2007 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2008 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002009 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002010 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2011 }
2012
2013 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2014
Harald Welte0abe5a62009-07-23 19:06:52 +02002015 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2016 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08002017}
2018
2019static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2020{
2021 struct gsm_mncc *alerting = arg;
2022 struct msgb *msg = gsm48_msgb_alloc();
2023 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2024
Harald Welte03740842009-06-10 23:11:52 +08002025 gh->msg_type = GSM48_MT_CC_ALERTING;
2026
2027 /* facility */
2028 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002029 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08002030 /* progress */
2031 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002032 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08002033 /* user-user */
2034 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002035 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002036
2037 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2038
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002039 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002040}
2041
2042static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2043{
2044 struct gsm_mncc *progress = arg;
2045 struct msgb *msg = gsm48_msgb_alloc();
2046 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2047
Harald Welte03740842009-06-10 23:11:52 +08002048 gh->msg_type = GSM48_MT_CC_PROGRESS;
2049
2050 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002051 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002052 /* user-user */
2053 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002054 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002055
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002056 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002057}
2058
2059static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2060{
2061 struct gsm_mncc *connect = arg;
2062 struct msgb *msg = gsm48_msgb_alloc();
2063 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2064
Harald Welte03740842009-06-10 23:11:52 +08002065 gh->msg_type = GSM48_MT_CC_CONNECT;
2066
2067 gsm48_stop_cc_timer(trans);
2068 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2069
2070 /* facility */
2071 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002072 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002073 /* progress */
2074 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002075 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002076 /* connected number */
2077 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002078 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002079 /* user-user */
2080 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002081 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002082
2083 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2084
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002085 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002086}
2087
2088static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2089{
2090 struct gsm48_hdr *gh = msgb_l3(msg);
2091 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2092 struct tlv_parsed tp;
2093 struct gsm_mncc connect;
2094
2095 gsm48_stop_cc_timer(trans);
2096
2097 memset(&connect, 0, sizeof(struct gsm_mncc));
2098 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002099 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002100 /* use subscriber as connected party number */
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02002101 connect.fields |= MNCC_F_CONNECTED;
2102 strncpy(connect.connected.number, trans->subscr->extension,
2103 sizeof(connect.connected.number)-1);
2104 strncpy(connect.imsi, trans->subscr->imsi,
2105 sizeof(connect.imsi)-1);
2106
Harald Welte03740842009-06-10 23:11:52 +08002107 /* facility */
2108 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2109 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002110 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002111 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2112 }
2113 /* user-user */
2114 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2115 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002116 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002117 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2118 }
2119 /* ss-version */
2120 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2121 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002122 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002123 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2124 }
2125
2126 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002127 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002128
Harald Welte0abe5a62009-07-23 19:06:52 +02002129 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002130}
2131
2132
2133static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2134{
2135 struct gsm_mncc connect_ack;
2136
2137 gsm48_stop_cc_timer(trans);
2138
2139 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002140 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002141
2142 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2143 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002144
Harald Welte0abe5a62009-07-23 19:06:52 +02002145 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002146 &connect_ack);
2147}
2148
2149static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2150{
2151 struct msgb *msg = gsm48_msgb_alloc();
2152 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2153
Harald Welte03740842009-06-10 23:11:52 +08002154 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2155
2156 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2157
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002158 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002159}
2160
2161static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2162{
2163 struct gsm48_hdr *gh = msgb_l3(msg);
2164 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2165 struct tlv_parsed tp;
2166 struct gsm_mncc disc;
2167
2168 gsm48_stop_cc_timer(trans);
2169
2170 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2171
2172 memset(&disc, 0, sizeof(struct gsm_mncc));
2173 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002174 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002175 /* cause */
2176 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2177 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002178 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002179 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2180 }
2181 /* facility */
2182 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2183 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002184 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002185 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2186 }
2187 /* user-user */
2188 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2189 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002190 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002191 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2192 }
2193 /* ss-version */
2194 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2195 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002196 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002197 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2198 }
2199
Harald Welte0abe5a62009-07-23 19:06:52 +02002200 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002201
2202}
2203
Harald Weltebbc636a2009-06-11 14:23:20 +08002204static struct gsm_mncc_cause default_cause = {
2205 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2206 .coding = 0,
2207 .rec = 0,
2208 .rec_val = 0,
2209 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2210 .diag_len = 0,
2211 .diag = { 0 },
2212};
Harald Welte03740842009-06-10 23:11:52 +08002213
2214static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2215{
2216 struct gsm_mncc *disc = arg;
2217 struct msgb *msg = gsm48_msgb_alloc();
2218 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2219
Harald Welte03740842009-06-10 23:11:52 +08002220 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2221
2222 gsm48_stop_cc_timer(trans);
2223 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2224
2225 /* cause */
2226 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002227 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002228 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002229 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002230
2231 /* facility */
2232 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002233 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002234 /* progress */
2235 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002236 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002237 /* user-user */
2238 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002239 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002240
2241 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002242 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002243
2244 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2245
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002246 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002247}
2248
2249static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2250{
2251 struct gsm48_hdr *gh = msgb_l3(msg);
2252 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2253 struct tlv_parsed tp;
2254 struct gsm_mncc rel;
2255 int rc;
2256
2257 gsm48_stop_cc_timer(trans);
2258
2259 memset(&rel, 0, sizeof(struct gsm_mncc));
2260 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002261 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002262 /* cause */
2263 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2264 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002265 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002266 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2267 }
2268 /* facility */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2270 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002271 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002272 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2273 }
2274 /* user-user */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2276 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002277 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002278 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2279 }
2280 /* ss-version */
2281 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2282 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002283 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002284 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2285 }
2286
Harald Weltec2189a62009-07-23 18:56:43 +02002287 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002288 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002289 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002290 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002291 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002292 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002293 GSM48_MT_CC_RELEASE_COMPL);
2294 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002295 }
2296
2297 new_cc_state(trans, GSM_CSTATE_NULL);
2298
2299 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002300 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002301
2302 return rc;
2303}
2304
2305static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2306{
2307 struct gsm_mncc *rel = arg;
2308 struct msgb *msg = gsm48_msgb_alloc();
2309 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2310
Harald Welte03740842009-06-10 23:11:52 +08002311 gh->msg_type = GSM48_MT_CC_RELEASE;
2312
Harald Welte03740842009-06-10 23:11:52 +08002313 gsm48_stop_cc_timer(trans);
2314 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2315
2316 /* cause */
2317 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002318 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002319 /* facility */
2320 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002321 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002322 /* user-user */
2323 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002324 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002325
Harald Weltec2189a62009-07-23 18:56:43 +02002326 trans->cc.T308_second = 0;
2327 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002328
Harald Weltec2189a62009-07-23 18:56:43 +02002329 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002330 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2331
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002332 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002333}
2334
2335static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2336{
2337 struct gsm48_hdr *gh = msgb_l3(msg);
2338 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2339 struct tlv_parsed tp;
2340 struct gsm_mncc rel;
2341 int rc = 0;
2342
2343 gsm48_stop_cc_timer(trans);
2344
2345 memset(&rel, 0, sizeof(struct gsm_mncc));
2346 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002347 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002348 /* cause */
2349 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2350 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002351 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002352 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2353 }
2354 /* facility */
2355 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2356 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002357 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002358 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2359 }
2360 /* user-user */
2361 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2362 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002363 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002364 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2365 }
2366 /* ss-version */
2367 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2368 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002369 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002370 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2371 }
2372
2373 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002374 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002375 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002376 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002377 MNCC_REJ_IND, &rel);
2378 break;
2379 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002380 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002381 MNCC_REL_CNF, &rel);
2382 break;
2383 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002384 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002385 MNCC_REL_IND, &rel);
2386 }
2387 }
2388
2389 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002390 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002391
2392 return rc;
2393}
2394
2395static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2396{
2397 struct gsm_mncc *rel = arg;
2398 struct msgb *msg = gsm48_msgb_alloc();
2399 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002400 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002401
Harald Welte03740842009-06-10 23:11:52 +08002402 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2403
2404 trans->callref = 0;
2405
2406 gsm48_stop_cc_timer(trans);
2407
2408 /* cause */
2409 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002410 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002411 /* facility */
2412 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002413 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002414 /* user-user */
2415 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002416 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002417
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002418 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2419
Harald Weltec2189a62009-07-23 18:56:43 +02002420 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002421
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002422 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002423}
2424
2425static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2426{
2427 struct gsm48_hdr *gh = msgb_l3(msg);
2428 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2429 struct tlv_parsed tp;
2430 struct gsm_mncc fac;
2431
2432 memset(&fac, 0, sizeof(struct gsm_mncc));
2433 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002434 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002435 /* facility */
2436 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2437 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002438 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002439 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2440 }
2441 /* ss-version */
2442 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2443 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002444 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002445 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2446 }
2447
Harald Welte0abe5a62009-07-23 19:06:52 +02002448 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002449}
2450
2451static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2452{
2453 struct gsm_mncc *fac = arg;
2454 struct msgb *msg = gsm48_msgb_alloc();
2455 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2456
Harald Welte03740842009-06-10 23:11:52 +08002457 gh->msg_type = GSM48_MT_CC_FACILITY;
2458
2459 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002460 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002461
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002462 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002463}
2464
2465static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2466{
2467 struct gsm_mncc hold;
2468
2469 memset(&hold, 0, sizeof(struct gsm_mncc));
2470 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002471 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002472}
2473
2474static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2475{
2476 struct msgb *msg = gsm48_msgb_alloc();
2477 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2478
Harald Welte03740842009-06-10 23:11:52 +08002479 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2480
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002481 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002482}
2483
2484static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2485{
2486 struct gsm_mncc *hold_rej = arg;
2487 struct msgb *msg = gsm48_msgb_alloc();
2488 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2489
Harald Welte03740842009-06-10 23:11:52 +08002490 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2491
2492 /* cause */
2493 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002494 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002495 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002496 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002497
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002498 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002499}
2500
2501static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2502{
2503 struct gsm_mncc retrieve;
2504
2505 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2506 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002507 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2508 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002509}
2510
2511static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2512{
2513 struct msgb *msg = gsm48_msgb_alloc();
2514 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2515
Harald Welte03740842009-06-10 23:11:52 +08002516 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2517
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002518 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002519}
2520
2521static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2522{
2523 struct gsm_mncc *retrieve_rej = arg;
2524 struct msgb *msg = gsm48_msgb_alloc();
2525 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2526
Harald Welte03740842009-06-10 23:11:52 +08002527 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2528
2529 /* cause */
2530 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002531 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002532 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002533 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002534
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002535 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002536}
2537
2538static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2539{
2540 struct gsm48_hdr *gh = msgb_l3(msg);
2541 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2542 struct tlv_parsed tp;
2543 struct gsm_mncc dtmf;
2544
2545 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2546 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002547 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002548 /* keypad facility */
2549 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2550 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002551 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002552 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2553 }
2554
Harald Welte0abe5a62009-07-23 19:06:52 +02002555 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002556}
2557
2558static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2559{
2560 struct gsm_mncc *dtmf = arg;
2561 struct msgb *msg = gsm48_msgb_alloc();
2562 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2563
Harald Welte03740842009-06-10 23:11:52 +08002564 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2565
2566 /* keypad */
2567 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002568 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002569
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002570 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002571}
2572
2573static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2574{
2575 struct gsm_mncc *dtmf = arg;
2576 struct msgb *msg = gsm48_msgb_alloc();
2577 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2578
Harald Welte03740842009-06-10 23:11:52 +08002579 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2580
2581 /* cause */
2582 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002583 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002584 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002585 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002586
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002587 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002588}
2589
2590static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2591{
2592 struct msgb *msg = gsm48_msgb_alloc();
2593 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2594
Harald Welte03740842009-06-10 23:11:52 +08002595 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2596
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002597 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002598}
2599
2600static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2601{
2602 struct gsm_mncc dtmf;
2603
2604 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2605 dtmf.callref = trans->callref;
2606
Harald Welte0abe5a62009-07-23 19:06:52 +02002607 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002608}
2609
2610static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2611{
2612 struct gsm48_hdr *gh = msgb_l3(msg);
2613 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2614 struct tlv_parsed tp;
2615 struct gsm_mncc modify;
2616
2617 memset(&modify, 0, sizeof(struct gsm_mncc));
2618 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002619 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002620 /* bearer capability */
2621 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2622 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002623 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002624 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2625 }
2626
2627 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2628
Harald Welte0abe5a62009-07-23 19:06:52 +02002629 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002630}
2631
2632static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2633{
2634 struct gsm_mncc *modify = arg;
2635 struct msgb *msg = gsm48_msgb_alloc();
2636 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2637
Harald Welte03740842009-06-10 23:11:52 +08002638 gh->msg_type = GSM48_MT_CC_MODIFY;
2639
2640 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2641
2642 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002643 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002644
2645 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2646
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002647 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002648}
2649
2650static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2651{
2652 struct gsm48_hdr *gh = msgb_l3(msg);
2653 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2654 struct tlv_parsed tp;
2655 struct gsm_mncc modify;
2656
2657 gsm48_stop_cc_timer(trans);
2658
2659 memset(&modify, 0, sizeof(struct gsm_mncc));
2660 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002661 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002662 /* bearer capability */
2663 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2664 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002665 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002666 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2667 }
2668
2669 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2670
Harald Welte0abe5a62009-07-23 19:06:52 +02002671 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002672}
2673
2674static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2675{
2676 struct gsm_mncc *modify = arg;
2677 struct msgb *msg = gsm48_msgb_alloc();
2678 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2679
Harald Welte03740842009-06-10 23:11:52 +08002680 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2681
2682 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002683 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002684
2685 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2686
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002687 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002688}
2689
2690static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2691{
2692 struct gsm48_hdr *gh = msgb_l3(msg);
2693 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2694 struct tlv_parsed tp;
2695 struct gsm_mncc modify;
2696
2697 gsm48_stop_cc_timer(trans);
2698
2699 memset(&modify, 0, sizeof(struct gsm_mncc));
2700 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002701 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002702 /* bearer capability */
2703 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2704 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002705 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002706 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2707 }
2708 /* cause */
2709 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2710 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002711 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002712 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2713 }
2714
2715 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2716
Harald Welte0abe5a62009-07-23 19:06:52 +02002717 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002718}
2719
2720static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2721{
2722 struct gsm_mncc *modify = arg;
2723 struct msgb *msg = gsm48_msgb_alloc();
2724 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2725
Harald Welte03740842009-06-10 23:11:52 +08002726 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2727
2728 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002729 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002730 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002731 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002732
2733 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2734
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002735 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002736}
2737
2738static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2739{
2740 struct gsm_mncc *notify = arg;
2741 struct msgb *msg = gsm48_msgb_alloc();
2742 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2743
Harald Welte03740842009-06-10 23:11:52 +08002744 gh->msg_type = GSM48_MT_CC_NOTIFY;
2745
2746 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002747 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002748
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002749 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002750}
2751
2752static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2753{
2754 struct gsm48_hdr *gh = msgb_l3(msg);
2755 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2756// struct tlv_parsed tp;
2757 struct gsm_mncc notify;
2758
2759 memset(&notify, 0, sizeof(struct gsm_mncc));
2760 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002761// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002762 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002763 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002764
Harald Welte0abe5a62009-07-23 19:06:52 +02002765 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002766}
2767
2768static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2769{
2770 struct gsm_mncc *user = arg;
2771 struct msgb *msg = gsm48_msgb_alloc();
2772 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2773
Harald Welte03740842009-06-10 23:11:52 +08002774 gh->msg_type = GSM48_MT_CC_USER_INFO;
2775
2776 /* user-user */
2777 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002778 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002779 /* more data */
2780 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002781 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002782
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002783 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002784}
2785
2786static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2787{
2788 struct gsm48_hdr *gh = msgb_l3(msg);
2789 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2790 struct tlv_parsed tp;
2791 struct gsm_mncc user;
2792
2793 memset(&user, 0, sizeof(struct gsm_mncc));
2794 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002795 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002796 /* user-user */
2797 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2798 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002799 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002800 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2801 }
2802 /* more data */
2803 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2804 user.more = 1;
2805
Harald Welte0abe5a62009-07-23 19:06:52 +02002806 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002807}
2808
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002809static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002810{
2811 struct gsm_mncc *mode = arg;
2812
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01002813 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte03740842009-06-10 23:11:52 +08002814}
2815
2816static struct downstate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02002817 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08002818 int type;
2819 int (*rout) (struct gsm_trans *trans, void *arg);
2820} downstatelist[] = {
2821 /* mobile originating call establishment */
2822 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2823 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2824 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2825 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2826 {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 */
2827 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2828 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2829 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2830 /* mobile terminating call establishment */
2831 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2832 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2833 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2834 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2835 /* signalling during call */
2836 {SBIT(GSM_CSTATE_ACTIVE),
2837 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2838 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2839 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2840 {ALL_STATES,
2841 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2842 {ALL_STATES,
2843 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2844 {ALL_STATES,
2845 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2846 {SBIT(GSM_CSTATE_ACTIVE),
2847 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2848 {SBIT(GSM_CSTATE_ACTIVE),
2849 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2850 {SBIT(GSM_CSTATE_ACTIVE),
2851 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2852 {SBIT(GSM_CSTATE_ACTIVE),
2853 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2854 {SBIT(GSM_CSTATE_ACTIVE),
2855 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2856 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2857 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2858 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2859 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2860 {SBIT(GSM_CSTATE_ACTIVE),
2861 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2862 /* clearing */
2863 {SBIT(GSM_CSTATE_INITIATED),
2864 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2865 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2866 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2867 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2868 MNCC_REL_REQ, gsm48_cc_tx_release},
2869 /* special */
2870 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002871 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002872};
2873
2874#define DOWNSLLEN \
2875 (sizeof(downstatelist) / sizeof(struct downstate))
2876
2877
Harald Weltef4e4b8a2010-12-22 23:57:45 +01002878int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002879{
Harald Welteaa60edb2009-08-09 18:52:33 +02002880 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002881 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002882 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freyther50366f22010-12-27 16:02:25 +01002883 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002884 struct gsm_mncc *data = arg, rel;
2885
Harald Welte17b56522010-12-22 21:45:05 +01002886 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2887
Harald Welte03740842009-06-10 23:11:52 +08002888 /* handle special messages */
2889 switch(msg_type) {
2890 case MNCC_BRIDGE:
2891 return tch_bridge(net, arg);
2892 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002893 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002894 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002895 return tch_recv_mncc(net, data->callref, 1);
2896 case GSM_TCHF_FRAME:
2897 /* Find callref */
2898 trans = trans_find_by_callref(net, data->callref);
Harald Welte17b56522010-12-22 21:45:05 +01002899 if (!trans) {
2900 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002901 return -EIO;
Harald Welte17b56522010-12-22 21:45:05 +01002902 }
2903 if (!trans->conn) {
2904 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002905 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002906 }
2907 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2908 /* This should be LOGL_ERROR or NOTICE, but
2909 * unfortuantely it happens for a couple of frames at
2910 * the beginning of every RTP connection */
2911 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002912 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002913 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002914 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002915 switch (bts->type) {
2916 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02002917 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte17b56522010-12-22 21:45:05 +01002918 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte45d6e4d2010-12-24 12:40:21 +01002919 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002920 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002921 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002922 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002923 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03002924 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02002925 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002926 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002927 default:
Harald Welte8099a712011-08-09 21:53:20 +02002928 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01002929 }
2930 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002931 }
2932
2933 memset(&rel, 0, sizeof(struct gsm_mncc));
2934 rel.callref = data->callref;
2935
2936 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002937 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002938
2939 /* Callref unknown */
2940 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002941 struct gsm_subscriber *subscr;
2942
Harald Welte6e1536e2009-07-04 10:11:24 +02002943 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002944 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2945 "Received '%s' from MNCC with "
2946 "unknown callref %d\n", data->called.number,
2947 get_mncc_name(msg_type), data->callref);
2948 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002949 return mncc_release_ind(net, NULL, data->callref,
2950 GSM48_CAUSE_LOC_PRN_S_LU,
2951 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002952 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002953 if (!data->called.number[0] && !data->imsi[0]) {
2954 DEBUGP(DCC, "(bts - trx - ts - ti) "
2955 "Received '%s' from MNCC with "
2956 "no number or IMSI\n", get_mncc_name(msg_type));
2957 /* Invalid number */
2958 return mncc_release_ind(net, NULL, data->callref,
2959 GSM48_CAUSE_LOC_PRN_S_LU,
2960 GSM48_CC_CAUSE_INV_NR_FORMAT);
2961 }
Harald Welte03740842009-06-10 23:11:52 +08002962 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002963 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002964 subscr = subscr_get_by_extension(net,
2965 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002966 else
Harald Welte75350412009-07-23 18:46:00 +02002967 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002968 /* If subscriber is not found */
2969 if (!subscr) {
2970 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2971 "Received '%s' from MNCC with "
2972 "unknown subscriber %s\n", data->called.number,
2973 get_mncc_name(msg_type), data->called.number);
2974 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002975 return mncc_release_ind(net, NULL, data->callref,
2976 GSM48_CAUSE_LOC_PRN_S_LU,
2977 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002978 }
2979 /* If subscriber is not "attached" */
2980 if (!subscr->lac) {
2981 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2982 "Received '%s' from MNCC with "
2983 "detached subscriber %s\n", data->called.number,
2984 get_mncc_name(msg_type), data->called.number);
2985 subscr_put(subscr);
2986 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002987 return mncc_release_ind(net, NULL, data->callref,
2988 GSM48_CAUSE_LOC_PRN_S_LU,
2989 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002990 }
2991 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002992 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2993 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002994 DEBUGP(DCC, "No memory for trans.\n");
2995 subscr_put(subscr);
2996 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002997 mncc_release_ind(net, NULL, data->callref,
2998 GSM48_CAUSE_LOC_PRN_S_LU,
2999 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08003000 return -ENOMEM;
3001 }
Harald Welte03740842009-06-10 23:11:52 +08003002 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003003 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003004
Harald Welte03740842009-06-10 23:11:52 +08003005 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003006 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003007 /* find transaction with this subscriber already paging */
3008 llist_for_each_entry(transt, &net->trans_list, entry) {
3009 /* Transaction of our lchan? */
3010 if (transt == trans ||
3011 transt->subscr != subscr)
3012 continue;
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003013 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003014 "Received '%s' from MNCC with "
3015 "unallocated channel, paging already "
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003016 "started for lac %d.\n",
Harald Welte03740842009-06-10 23:11:52 +08003017 data->called.number,
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003018 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003019 subscr_put(subscr);
3020 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003021 return 0;
3022 }
3023 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02003024 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01003025
3026 /* Get a channel */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01003027 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3028 if (!trans->paging_request) {
3029 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3030 subscr_put(subscr);
3031 trans_free(trans);
3032 return 0;
3033 }
3034
3035 *trans->paging_request = subscr->net;
3036 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut892f6952010-12-01 22:17:36 +01003037
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003038 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003039 return 0;
3040 }
3041 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003042 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003043 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003044 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003045
3046 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003047 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08003048
3049 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003050 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003051 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003052 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08003053 (trans->subscr)?(trans->subscr->extension):"-",
3054 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003055 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3056 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003057 if (msg_type == MNCC_REL_REQ)
3058 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3059 else
3060 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3061 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003062 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003063 return rc;
3064 }
3065
3066 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3067 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003068 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08003069 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003070 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003071 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003072 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003073
3074 /* Find function for current state and message */
3075 for (i = 0; i < DOWNSLLEN; i++)
3076 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003077 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003078 break;
3079 if (i == DOWNSLLEN) {
3080 DEBUGP(DCC, "Message unhandled at this state.\n");
3081 return 0;
3082 }
3083
3084 rc = downstatelist[i].rout(trans, arg);
3085
3086 return rc;
3087}
3088
3089
3090static struct datastate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003091 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08003092 int type;
3093 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3094} datastatelist[] = {
3095 /* mobile originating call establishment */
3096 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3097 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3098 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3099 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3100 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3101 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3102 /* mobile terminating call establishment */
3103 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3104 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3105 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3106 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003107 {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 Welte03740842009-06-10 23:11:52 +08003108 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3109 /* signalling during call */
3110 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3111 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3112 {SBIT(GSM_CSTATE_ACTIVE),
3113 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3114 {ALL_STATES,
3115 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3116 {ALL_STATES,
3117 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3118 {ALL_STATES,
3119 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3120 {SBIT(GSM_CSTATE_ACTIVE),
3121 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3122 {SBIT(GSM_CSTATE_ACTIVE),
3123 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3124 {SBIT(GSM_CSTATE_ACTIVE),
3125 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3126 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3127 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3128 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3129 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3130 {SBIT(GSM_CSTATE_ACTIVE),
3131 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3132 /* clearing */
3133 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3134 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3135 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3136 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3137 {ALL_STATES, /* 5.4.3.4 */
3138 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3139};
3140
3141#define DATASLLEN \
3142 (sizeof(datastatelist) / sizeof(struct datastate))
3143
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003144static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003145{
3146 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003147 uint8_t msg_type = gh->msg_type & 0xbf;
3148 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003149 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003150 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003151
Harald Welte03740842009-06-10 23:11:52 +08003152 if (msg_type & 0x80) {
3153 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3154 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003155 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003156
Harald Welte03740842009-06-10 23:11:52 +08003157 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003158 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003159
Harald Welte4861c822009-07-23 21:21:14 +02003160 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003161 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003162 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003163 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003164 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3165 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003166
3167 /* Create transaction */
3168 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003169 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003170 "creating new trans.\n", transaction_id);
3171 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003172 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003173 transaction_id, new_callref++);
3174 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003175 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01003176 rc = gsm48_tx_simple(conn,
Harald Welte4861c822009-07-23 21:21:14 +02003177 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003178 GSM48_MT_CC_RELEASE_COMPL);
3179 return -ENOMEM;
3180 }
Harald Welte03740842009-06-10 23:11:52 +08003181 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003182 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003183 }
3184
3185 /* find function for current state and message */
3186 for (i = 0; i < DATASLLEN; i++)
3187 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003188 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003189 break;
3190 if (i == DATASLLEN) {
3191 DEBUGP(DCC, "Message unhandled at this state.\n");
3192 return 0;
3193 }
3194
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02003195 assert(trans->subscr);
3196
Harald Welte03740842009-06-10 23:11:52 +08003197 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003198
3199 return rc;
3200}
3201
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003202/* Create a dummy to wait five seconds */
3203static void release_anchor(struct gsm_subscriber_connection *conn)
3204{
3205 if (!conn->anch_operation)
3206 return;
3207
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003208 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003209 talloc_free(conn->anch_operation);
3210 conn->anch_operation = NULL;
3211}
3212
3213static void anchor_timeout(void *_data)
3214{
3215 struct gsm_subscriber_connection *con = _data;
3216
3217 release_anchor(con);
3218 msc_release_connection(con);
3219}
3220
3221int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3222{
3223 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3224 if (!conn->anch_operation)
3225 return -1;
3226
3227 conn->anch_operation->timeout.data = conn;
3228 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003229 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003230 return 0;
3231}
3232
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003233/* here we get data from the BSC level... */
3234int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003235{
3236 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003237 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte59b04682009-06-10 05:40:52 +08003238 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003239
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003240 if (silent_call_reroute(conn, msg))
3241 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003242
3243 switch (pdisc) {
3244 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003245 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003246 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003247 break;
3248 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003249 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003250 break;
3251 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003252 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003253 break;
3254 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003255 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003256 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003257 break;
3258 case GSM48_PDISC_MM_GPRS:
3259 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003260 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3261 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003262 break;
Harald Welte03115042009-10-16 08:32:58 +02003263 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003264 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003265 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003266 break;
Harald Welte59b04682009-06-10 05:40:52 +08003267 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003268 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3269 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003270 break;
3271 }
3272
3273 return rc;
3274}
3275
Harald Welte3c062072009-07-28 18:25:29 +02003276/*
3277 * This will be ran by the linker when loading the DSO. We use it to
3278 * do system initialization, e.g. registration of signal handlers.
3279 */
3280static __attribute__((constructor)) void on_dso_load_0408(void)
3281{
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02003282 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte3c062072009-07-28 18:25:29 +02003283}