blob: c41443eb4ff34ac73ada02921ccd184a8ef843b4 [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
Andreas Eversberge36b4c72013-07-10 08:58:03 +0200284 case GSM_SECURITY_ALREADY:
285 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
286 "UPDATING after CM SERVICE REQUEST\n");
287 /* fall through */
288
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100289 case GSM_SECURITY_NOAVAIL:
290 case GSM_SECURITY_SUCCEEDED:
291 /* We're all good */
292 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800293 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800294 if (conn->bts->network->send_mm_info) {
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100295 /* send MM INFO with network name */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800296 rc = gsm48_tx_mm_info(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100297 }
298
299 /* call subscr_update after putting the loc_upd_acc
300 * in the transmit queue, since S_SUBSCR_ATTACHED might
301 * trigger further action like SMS delivery */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800302 subscr_update(conn->subscr, conn->bts,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100303 GSM_SUBSCRIBER_UPDATE_ATTACHED);
304
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +0100305 /*
306 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
307 * MS needs to respond with a TMSI REALLOCATION COMPLETE
308 * (even if the TMSI is the same).
309 */
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100310 break;
311
312 default:
313 rc = -EINVAL;
314 };
315
316 return rc;
317}
318
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100319static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800320{
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100321 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800322 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100323 conn->loc_operation->key_seq,
324 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800325 return 0;
326}
327
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800328void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800329{
Harald Welte03740842009-06-10 23:11:52 +0800330 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800331
332 /* avoid someone issuing a clear */
333 conn->in_release = 1;
334
Harald Welte59b04682009-06-10 05:40:52 +0800335 /*
336 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800337 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800338 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800339 release_loc_updating_req(conn);
Holger Hans Peter Freytherd91346a2010-12-28 14:57:20 +0100340
341 /* We might need to cancel the paging response or such. */
342 if (conn->sec_operation && conn->sec_operation->cb) {
343 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
344 NULL, conn, conn->sec_operation->cb_data);
345 }
346
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800347 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800348 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800349
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100350 /*
351 * Free all transactions that are associated with the released
352 * connection. The transaction code will inform the CC or SMS
353 * facilities that will send the release indications. As part of
354 * the CC REL_IND the remote leg might be released and this will
355 * trigger the call to trans_free. This is something the llist
356 * macro can not handle and we will need to re-iterate the list.
357 *
358 * TODO: Move the trans_list into the subscriber connection and
359 * create a pending list for MT transactions. These exist before
360 * we have a subscriber connection.
361 */
362restart:
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800363 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100364 if (trans->conn == conn) {
Harald Weltec2189a62009-07-23 18:56:43 +0200365 trans_free(trans);
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100366 goto restart;
367 }
Harald Welte03740842009-06-10 23:11:52 +0800368 }
Harald Welte59b04682009-06-10 05:40:52 +0800369}
370
Harald Weltee8d16172010-12-22 23:17:50 +0100371void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
372{
373 struct gsm_trans *trans, *temp;
374
375 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
376
377 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Weltebd0f33c2010-12-23 02:47:53 +0100378 if (trans->protocol == protocol) {
379 trans->callref = 0;
Harald Weltee8d16172010-12-22 23:17:50 +0100380 trans_free(trans);
Harald Weltebd0f33c2010-12-23 02:47:53 +0100381 }
Harald Weltee8d16172010-12-22 23:17:50 +0100382 }
383}
384
Harald Welte59b04682009-06-10 05:40:52 +0800385/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200386int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800387{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800388 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800389 struct msgb *msg;
390
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200391 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800392
393 msg = gsm48_create_loc_upd_rej(cause);
394 if (!msg) {
395 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
396 return -1;
397 }
Harald Welte59b04682009-06-10 05:40:52 +0800398
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800399 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800400
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100401 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100402 "LAC=%u BTS=%u\n", conn->subscr ?
403 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800404 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100405
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800406 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800407}
408
409/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200410int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800411{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800412 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800413 struct msgb *msg = gsm48_msgb_alloc();
414 struct gsm48_hdr *gh;
415 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200416 uint8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800417
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800418 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800419
420 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
421 gh->proto_discr = GSM48_PDISC_MM;
422 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
423
424 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100425 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800426 bts->network->network_code, bts->location_area_code);
427
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200428 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200429 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800430
431 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
432
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200433 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100434
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800435 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800436}
437
Harald Welte59b04682009-06-10 05:40:52 +0800438/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200439static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800440{
441 struct msgb *msg = gsm48_msgb_alloc();
442 struct gsm48_hdr *gh;
443
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800444 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800445
446 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
447 gh->proto_discr = GSM48_PDISC_MM;
448 gh->msg_type = GSM48_MT_MM_ID_REQ;
449 gh->data[0] = id_type;
450
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800451 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800452}
453
Harald Welte59b04682009-06-10 05:40:52 +0800454
455/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800456static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800457{
458 struct gsm48_hdr *gh = msgb_l3(msg);
459 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200460 struct gsm_bts *bts = lchan->ts->trx->bts;
461 struct gsm_network *net = bts->network;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200462 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200463 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800464
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200465 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800466 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
467 mi_type, mi_string);
468
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200469 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welteedf48af2009-12-13 12:39:18 +0100470
Harald Welte59b04682009-06-10 05:40:52 +0800471 switch (mi_type) {
472 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200473 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100474 if (!conn->subscr) {
475 conn->subscr = subscr_get_by_imsi(net, mi_string);
476 if (!conn->subscr)
Holger Hans Peter Freytherabe587d2013-10-04 08:35:11 +0200477 conn->subscr = subscr_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200478 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100479 if (conn->loc_operation)
480 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800481 break;
482 case GSM_MI_TYPE_IMEI:
483 case GSM_MI_TYPE_IMEISV:
484 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100485 if (conn->subscr) {
486 db_subscriber_assoc_imei(conn->subscr, mi_string);
487 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200488 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100489 if (conn->loc_operation)
490 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800491 break;
492 }
493
494 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100495 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800496}
497
498
499static void loc_upd_rej_cb(void *data)
500{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100501 struct gsm_subscriber_connection *conn = data;
502 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100503 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800504
Holger Hans Peter Freyther0d91c462011-11-07 12:26:29 +0100505 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800506 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800507 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800508}
509
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100510static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800511{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100512 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
513 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200514 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800515}
516
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200517static const char *lupd_name(uint8_t type)
Harald Welte59b04682009-06-10 05:40:52 +0800518{
519 switch (type) {
520 case GSM48_LUPD_NORMAL:
521 return "NORMAL";
522 case GSM48_LUPD_PERIODIC:
Alexander Chemeris979d1372013-10-04 02:42:26 +0200523 return "PERIODIC";
Harald Welte59b04682009-06-10 05:40:52 +0800524 case GSM48_LUPD_IMSI_ATT:
525 return "IMSI ATTACH";
526 default:
527 return "UNKNOWN";
528 }
529}
530
Harald Welte59b04682009-06-10 05:40:52 +0800531/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800532static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800533{
534 struct gsm48_hdr *gh = msgb_l3(msg);
535 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800536 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800537 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200538 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200539 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800540
541 lu = (struct gsm48_loc_upd_req *) gh->data;
542
543 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
544
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200545 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800546
Harald Welte79639662009-06-27 02:58:43 +0200547 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800548 lupd_name(lu->type));
549
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200550 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welteedf48af2009-12-13 12:39:18 +0100551
Harald Welte3edc5a92009-12-22 00:41:05 +0100552 switch (lu->type) {
553 case GSM48_LUPD_NORMAL:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200554 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100555 break;
556 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200557 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100558 break;
559 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200560 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100561 break;
562 }
563
Harald Welte59b04682009-06-10 05:40:52 +0800564 /*
565 * Pseudo Spoof detection: Just drop a second/concurrent
566 * location updating request.
567 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100568 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200569 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100570 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800571 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800572 return 0;
573 }
574
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800575 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800576
Sylvain Munaut360fc582010-06-10 13:36:59 +0200577 conn->loc_operation->key_seq = lu->key_seq;
578
Harald Welte59b04682009-06-10 05:40:52 +0800579 switch (mi_type) {
580 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200581 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800582 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200583 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100584 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800585
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200586 /* look up subscriber based on IMSI, create if not found */
587 subscr = subscr_get_by_imsi(bts->network, mi_string);
588 if (!subscr) {
Holger Hans Peter Freytherabe587d2013-10-04 08:35:11 +0200589 subscr = subscr_create_subscriber(bts->network, mi_string);
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200590 }
Harald Welte59b04682009-06-10 05:40:52 +0800591 break;
592 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200593 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800594 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200595 subscr = subscr_get_by_tmsi(bts->network,
596 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800597 if (!subscr) {
598 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200599 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100600 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800601 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200602 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200603 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200604 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800605 break;
606 case GSM_MI_TYPE_IMEI:
607 case GSM_MI_TYPE_IMEISV:
608 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200609 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800610 break;
611 default:
Harald Welte79639662009-06-27 02:58:43 +0200612 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800613 break;
614 }
615
Harald Welteccd69362009-07-04 10:18:00 +0200616 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100617 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200618
Harald Welte03740842009-06-10 23:11:52 +0800619 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200620 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800621 /* FIXME: request id? close channel? */
622 return -EINVAL;
623 }
624
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100625 conn->subscr = subscr;
626 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800627
Harald Welteccd69362009-07-04 10:18:00 +0200628 /* check if we can let the subscriber into our network immediately
629 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100630 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800631}
632
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100633/* Turn int into semi-octet representation: 98 => 0x89 */
634static uint8_t bcdify(uint8_t value)
Harald Welte03740842009-06-10 23:11:52 +0800635{
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100636 uint8_t ret;
637
638 ret = value / 10;
639 ret |= (value % 10) << 4;
640
641 return ret;
Harald Welte03740842009-06-10 23:11:52 +0800642}
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100643
Harald Welte03740842009-06-10 23:11:52 +0800644
Harald Welte59b04682009-06-10 05:40:52 +0800645/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800646int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800647{
648 struct msgb *msg = gsm48_msgb_alloc();
649 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800650 struct gsm_network *net = conn->bts->network;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100651 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200652 uint8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200653 int name_len, name_pad;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100654
Harald Welte03740842009-06-10 23:11:52 +0800655 time_t cur_t;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100656 struct tm* gmt_time;
657 struct tm* local_time;
658 int tzunits;
Harald Welte59b04682009-06-10 05:40:52 +0800659
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800660 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800661
662 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
663 gh->proto_discr = GSM48_PDISC_MM;
664 gh->msg_type = GSM48_MT_MM_INFO;
665
666 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200667#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800668 name_len = strlen(net->name_long);
669 /* 10.5.3.5a */
670 ptr8 = msgb_put(msg, 3);
671 ptr8[0] = GSM48_IE_NAME_LONG;
672 ptr8[1] = name_len*2 +1;
673 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
674
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200675 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800676 for (i = 0; i < name_len; i++)
677 ptr16[i] = htons(net->name_long[i]);
678
679 /* FIXME: Use Cell Broadcast, not UCS-2, since
680 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200681#endif
682 name_len = (strlen(net->name_long)*7)/8;
683 name_pad = (8 - strlen(net->name_long)*7)%8;
684 if (name_pad > 0)
685 name_len++;
686 /* 10.5.3.5a */
687 ptr8 = msgb_put(msg, 3);
688 ptr8[0] = GSM48_IE_NAME_LONG;
689 ptr8[1] = name_len +1;
690 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
691
692 ptr8 = msgb_put(msg, name_len);
693 gsm_7bit_encode(ptr8, net->name_long);
694
Harald Welte59b04682009-06-10 05:40:52 +0800695 }
696
697 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200698#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800699 name_len = strlen(net->name_short);
700 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200701 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200702 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800703 ptr8[1] = name_len*2 + 1;
704 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
705
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200706 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800707 for (i = 0; i < name_len; i++)
708 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200709#endif
710 name_len = (strlen(net->name_short)*7)/8;
711 name_pad = (8 - strlen(net->name_short)*7)%8;
712 if (name_pad > 0)
713 name_len++;
714 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200715 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmannc8188202009-08-13 03:42:07 +0200716 ptr8[0] = GSM48_IE_NAME_SHORT;
717 ptr8[1] = name_len +1;
718 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
719
720 ptr8 = msgb_put(msg, name_len);
721 gsm_7bit_encode(ptr8, net->name_short);
722
Harald Welte59b04682009-06-10 05:40:52 +0800723 }
724
Harald Welte59b04682009-06-10 05:40:52 +0800725 /* Section 10.5.3.9 */
726 cur_t = time(NULL);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100727 gmt_time = gmtime(&cur_t);
728
Harald Welte59b04682009-06-10 05:40:52 +0800729 ptr8 = msgb_put(msg, 8);
730 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100731 ptr8[1] = bcdify(gmt_time->tm_year % 100);
732 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
733 ptr8[3] = bcdify(gmt_time->tm_mday);
734 ptr8[4] = bcdify(gmt_time->tm_hour);
735 ptr8[5] = bcdify(gmt_time->tm_min);
736 ptr8[6] = bcdify(gmt_time->tm_sec);
737
Harald Welte5c3abf92012-07-08 16:48:11 +0200738 if (bts->tz.override) {
739 /* Convert tz.hr and tz.mn to units */
740 if (bts->tz.hr < 0) {
741 tzunits = ((bts->tz.hr/-1)*4);
742 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100743 ptr8[7] = bcdify(tzunits);
744 /* Set negative time */
745 ptr8[7] |= 0x08;
746 }
747 else {
Harald Welte5c3abf92012-07-08 16:48:11 +0200748 tzunits = bts->tz.hr*4;
749 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100750 ptr8[7] = bcdify(tzunits);
751 }
752 }
753 else {
754 /* Need to get GSM offset and convert into 15 min units */
755 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
756 local_time = localtime(&cur_t);
Harald Welteeaf4a522012-04-08 16:59:24 +0200757#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100758 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welteeaf4a522012-04-08 16:59:24 +0200759#else
760#warning find a portable way to obtain the timezone offset
761 tzunits = 0;
762#endif
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100763 if (tzunits < 0) {
764 tzunits = tzunits/-1;
765 ptr8[7] = bcdify(tzunits);
766 /* Flip it to negative */
767 ptr8[7] |= 0x08;
768 }
769 else
770 ptr8[7] = bcdify(tzunits);
771 }
Harald Welte59b04682009-06-10 05:40:52 +0800772
Daniel Willmannc8188202009-08-13 03:42:07 +0200773 DEBUGP(DMM, "-> MM INFO\n");
774
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800775 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800776}
777
Harald Welte36970f62009-08-12 22:56:50 +0200778/* Section 9.2.2 */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200779int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200780{
781 struct msgb *msg = gsm48_msgb_alloc();
782 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200783 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200784
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200785 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200786
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800787 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200788 gh->proto_discr = GSM48_PDISC_MM;
789 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
790
Sylvain Munaut0789c592009-12-24 00:23:46 +0100791 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200792
Harald Welte36970f62009-08-12 22:56:50 +0200793 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200794 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200795 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200796
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800797 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200798}
799
800/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800801int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200802{
803 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800804 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200805}
806
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800807static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800808{
809 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800810 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte59b04682009-06-10 05:40:52 +0800811}
812
813/* 9.2.6 CM service reject */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100814static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +0800815 enum gsm48_reject_value value)
816{
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800817 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800818
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800819 msg = gsm48_create_mm_serv_rej(value);
820 if (!msg) {
821 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
822 return -1;
823 }
Harald Welte59b04682009-06-10 05:40:52 +0800824
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800825 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100826 msg->lchan = conn->lchan;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800827 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800828}
829
Sylvain Munautbadc7112009-12-24 00:28:46 +0100830static int _gsm48_rx_mm_serv_req_sec_cb(
831 unsigned int hooknum, unsigned int event,
832 struct msgb *msg, void *data, void *param)
833{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800834 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100835 int rc = 0;
836
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +0200837 /* auth failed or succeeded, the timer was stopped */
838 conn->expire_timer_stopped = 1;
839
Sylvain Munautbadc7112009-12-24 00:28:46 +0100840 switch (event) {
841 case GSM_SECURITY_AUTH_FAILED:
842 /* Nothing to do */
843 break;
844
845 case GSM_SECURITY_NOAVAIL:
Andreas Eversbergee995352013-07-10 08:58:03 +0200846 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800847 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100848 break;
849
850 case GSM_SECURITY_SUCCEEDED:
851 /* nothing to do. CIPHER MODE COMMAND is
852 * implicit CM SERV ACK */
853 break;
854
855 default:
856 rc = -EINVAL;
857 };
858
859 return rc;
860}
861
Harald Welte59b04682009-06-10 05:40:52 +0800862/*
863 * Handle CM Service Requests
864 * a) Verify that the packet is long enough to contain the information
865 * we require otherwsie reject with INCORRECT_MESSAGE
866 * b) Try to parse the TMSI. If we do not have one reject
867 * c) Check that we know the subscriber with the TMSI otherwise reject
868 * with a HLR cause
869 * d) Set the subscriber on the gsm_lchan and accept
870 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800871static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800872{
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200873 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200874 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800875
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800876 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800877 struct gsm_subscriber *subscr;
878 struct gsm48_hdr *gh = msgb_l3(msg);
879 struct gsm48_service_request *req =
880 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800881 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200882 uint8_t classmark2_len = gh->data[1];
883 uint8_t *classmark2 = gh->data+2;
884 uint8_t mi_len = *(classmark2 + classmark2_len);
885 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte59b04682009-06-10 05:40:52 +0800886
887 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
888 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
889 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800890 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800891 GSM48_REJECT_INCORRECT_MESSAGE);
892 }
893
894 if (msg->data_len < req->mi_len + 6) {
895 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800896 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800897 GSM48_REJECT_INCORRECT_MESSAGE);
898 }
899
900 mi_type = mi[0] & GSM_MI_TYPE_MASK;
901 if (mi_type != GSM_MI_TYPE_TMSI) {
902 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800903 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800904 GSM48_REJECT_INCORRECT_MESSAGE);
905 }
906
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200907 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800908 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
909 req->cm_service_type, mi_type, mi_string);
910
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200911 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welteedf48af2009-12-13 12:39:18 +0100912
Harald Welte583ceaf2009-08-10 10:12:45 +0200913 if (is_siemens_bts(bts))
914 send_siemens_mrpci(msg->lchan, classmark2-1);
915
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200916 subscr = subscr_get_by_tmsi(bts->network,
917 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800918
919 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
920 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800921 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800922 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Holger Hans Peter Freytherfe036952013-01-01 17:18:27 +0100923 if (subscr->lac == GSM_LAC_RESERVED_DETACHED)
Sylvain Munautf1087412012-12-28 00:33:04 +0100924 /* If the subscriber is not attached, reject service */
925 return gsm48_tx_mm_serv_rej(conn,
926 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte59b04682009-06-10 05:40:52 +0800927
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800928 if (!conn->subscr)
929 conn->subscr = subscr;
930 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100931 subscr_put(subscr); /* lchan already has a ref, don't need another one */
932 else {
Harald Welte59b04682009-06-10 05:40:52 +0800933 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
934 subscr_put(subscr);
935 }
936
Harald Weltef6845a72009-07-05 14:08:13 +0200937 subscr->equipment.classmark2_len = classmark2_len;
938 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
939 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800940
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +0200941 /* we will send a MM message soon */
942 conn->expire_timer_stopped = 1;
943
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800944 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +0100945 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800946}
947
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200948static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800949{
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200950 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800951 struct gsm48_hdr *gh = msgb_l3(msg);
952 struct gsm48_imsi_detach_ind *idi =
953 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200954 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200955 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800956 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800957
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200958 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100959 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte59b04682009-06-10 05:40:52 +0800960 mi_type, mi_string);
961
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200962 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100963
Harald Welte59b04682009-06-10 05:40:52 +0800964 switch (mi_type) {
965 case GSM_MI_TYPE_TMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100966 DEBUGPC(DMM, "\n");
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200967 subscr = subscr_get_by_tmsi(bts->network,
968 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800969 break;
970 case GSM_MI_TYPE_IMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100971 DEBUGPC(DMM, "\n");
Harald Welte75350412009-07-23 18:46:00 +0200972 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800973 break;
974 case GSM_MI_TYPE_IMEI:
975 case GSM_MI_TYPE_IMEISV:
976 /* no sim card... FIXME: what to do ? */
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100977 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800978 break;
979 default:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100980 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800981 break;
982 }
983
984 if (subscr) {
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200985 subscr_update(subscr, bts,
Harald Welte59b04682009-06-10 05:40:52 +0800986 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100987 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200988
989 subscr->equipment.classmark1 = idi->classmark1;
990 db_sync_equipment(&subscr->equipment);
991
Harald Welte59b04682009-06-10 05:40:52 +0800992 subscr_put(subscr);
993 } else
994 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
995
Harald Welte43250dd2009-12-20 09:58:40 +0100996 /* FIXME: iterate over all transactions and release them,
997 * imagine an IMSI DETACH happening during an active call! */
998
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200999 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +08001000 return 0;
1001}
1002
1003static int gsm48_rx_mm_status(struct msgb *msg)
1004{
1005 struct gsm48_hdr *gh = msgb_l3(msg);
1006
1007 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1008
1009 return 0;
1010}
1011
Sylvain Munaut28c47162009-12-24 00:27:26 +01001012/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001013static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +01001014{
1015 struct gsm48_hdr *gh = msgb_l3(msg);
1016 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001017 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001018
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001019 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001020 osmo_hexdump(ar->sres, 4));
Sylvain Munaut28c47162009-12-24 00:27:26 +01001021
1022 /* Safety check */
1023 if (!conn->sec_operation) {
1024 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1025 return -EIO;
1026 }
1027
1028 /* Validate SRES */
1029 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001030 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001031 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001032
1033 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001034 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001035
Sylvain Munaut28c47162009-12-24 00:27:26 +01001036 if (cb)
1037 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001038 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001039
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001040 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001041 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001042 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001043 }
1044
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001045 DEBUGPC(DMM, "OK\n");
1046
Sylvain Munaut28c47162009-12-24 00:27:26 +01001047 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +01001048 return gsm0808_cipher_mode(conn, net->a5_encryption,
1049 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001050}
1051
Harald Welte59b04682009-06-10 05:40:52 +08001052/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001053static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001054{
1055 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +08001056 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08001057
1058 switch (gh->msg_type & 0xbf) {
1059 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +02001060 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001061 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001062 break;
1063 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001064 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001065 break;
1066 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001067 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001068 break;
1069 case GSM48_MT_MM_STATUS:
1070 rc = gsm48_rx_mm_status(msg);
1071 break;
1072 case GSM48_MT_MM_TMSI_REALL_COMPL:
1073 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001074 conn->subscr ?
1075 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +08001076 "unknown subscriber");
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +01001077 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +08001078 break;
1079 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001080 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001081 break;
1082 case GSM48_MT_MM_CM_REEST_REQ:
1083 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1084 break;
1085 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001086 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001087 break;
1088 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001089 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001090 gh->msg_type);
1091 break;
1092 }
1093
1094 return rc;
1095}
1096
1097/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001098static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001099{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001100 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001101 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001102 struct gsm48_pag_resp *resp;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001103 uint8_t *classmark2_lv = gh->data + 1;
1104 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001105 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001106 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001107 int rc = 0;
1108
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001109 resp = (struct gsm48_pag_resp *) &gh->data[0];
1110 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1111 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001112 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1113 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001114
Harald Welte59b04682009-06-10 05:40:52 +08001115 switch (mi_type) {
1116 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001117 subscr = subscr_get_by_tmsi(bts->network,
1118 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001119 break;
1120 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001121 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001122 break;
1123 }
1124
1125 if (!subscr) {
1126 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1127 /* FIXME: request id? close channel? */
1128 return -EINVAL;
1129 }
1130 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001131 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001132
Harald Weltef6845a72009-07-05 14:08:13 +02001133 subscr->equipment.classmark2_len = *classmark2_lv;
1134 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1135 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001136
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +02001137 /* We received a paging */
1138 conn->expire_timer_stopped = 1;
1139
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001140 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001141 return rc;
1142}
1143
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001144static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001145{
1146 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001147 uint8_t apdu_id_flags;
1148 uint8_t apdu_len;
1149 uint8_t *apdu_data;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001150
1151 apdu_id_flags = gh->data[0];
1152 apdu_len = gh->data[1];
1153 apdu_data = gh->data+2;
1154
Harald Welted4f21662012-07-16 13:22:19 +02001155 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001156 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)b5103922009-08-15 23:32:44 +02001157
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001158 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001159}
1160
Harald Welte59b04682009-06-10 05:40:52 +08001161/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001162static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001163{
1164 struct gsm48_hdr *gh = msgb_l3(msg);
1165 int rc = 0;
1166
1167 switch (gh->msg_type) {
Harald Welte59b04682009-06-10 05:40:52 +08001168 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001169 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001170 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001171 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001172 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001173 break;
Harald Welte59b04682009-06-10 05:40:52 +08001174 default:
Harald Weltefe01ed72012-01-23 16:40:24 +01001175 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte60e5f4f2009-12-24 12:13:17 +01001176 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001177 break;
1178 }
1179
1180 return rc;
1181}
1182
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001183int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1184 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001185{
1186 struct msgb *msg = gsm48_msgb_alloc();
1187 struct gsm48_hdr *gh;
1188
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001189 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001190
1191 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1192 apdu_id, apdu_len);
1193
1194 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1195 gh->proto_discr = GSM48_PDISC_RR;
1196 gh->msg_type = GSM48_MT_RR_APP_INFO;
1197 gh->data[0] = apdu_id;
1198 gh->data[1] = apdu_len;
1199 memcpy(gh->data+2, apdu, apdu_len);
1200
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001201 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001202}
1203
Harald Welte59b04682009-06-10 05:40:52 +08001204/* Call Control */
1205
1206/* The entire call control code is written in accordance with Figure 7.10c
1207 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1208 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1209 * it for voice */
1210
Harald Welte03740842009-06-10 23:11:52 +08001211static void new_cc_state(struct gsm_trans *trans, int state)
1212{
1213 if (state > 31 || state < 0)
1214 return;
1215
1216 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001217 gsm48_cc_state_name(trans->cc.state),
1218 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001219
Harald Weltec2189a62009-07-23 18:56:43 +02001220 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001221}
1222
1223static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001224{
1225 struct msgb *msg = gsm48_msgb_alloc();
1226 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001227 uint8_t *cause, *call_state;
Harald Welte59b04682009-06-10 05:40:52 +08001228
Harald Welte59b04682009-06-10 05:40:52 +08001229 gh->msg_type = GSM48_MT_CC_STATUS;
1230
1231 cause = msgb_put(msg, 3);
1232 cause[0] = 2;
1233 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1234 cause[2] = 0x80 | 30; /* response to status inquiry */
1235
1236 call_state = msgb_put(msg, 1);
1237 call_state[0] = 0xc0 | 0x00;
1238
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001239 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001240}
1241
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001242static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001243 uint8_t pdisc, uint8_t msg_type)
Harald Welte59b04682009-06-10 05:40:52 +08001244{
1245 struct msgb *msg = gsm48_msgb_alloc();
1246 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1247
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001248 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001249
1250 gh->proto_discr = pdisc;
1251 gh->msg_type = msg_type;
1252
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001253 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001254}
1255
Harald Welte03740842009-06-10 23:11:52 +08001256static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1257{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001258 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltec2189a62009-07-23 18:56:43 +02001259 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001260 osmo_timer_del(&trans->cc.timer);
Harald Weltec2189a62009-07-23 18:56:43 +02001261 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001262 }
1263}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001264
Harald Welte03740842009-06-10 23:11:52 +08001265static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1266 int msg_type, struct gsm_mncc *mncc)
1267{
1268 struct msgb *msg;
Harald Welte17b56522010-12-22 21:45:05 +01001269 unsigned char *data;
Harald Welte03740842009-06-10 23:11:52 +08001270
1271 if (trans)
Harald Weltec51b1922010-12-26 19:06:37 +01001272 if (trans->conn && trans->conn->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001273 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001274 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001275 trans->conn->lchan->ts->trx->bts->nr,
1276 trans->conn->lchan->ts->trx->nr,
1277 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001278 (trans->subscr)?(trans->subscr->extension):"-",
1279 get_mncc_name(msg_type));
1280 else
1281 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1282 "Sending '%s' to MNCC.\n",
1283 (trans->subscr)?(trans->subscr->extension):"-",
1284 get_mncc_name(msg_type));
1285 else
1286 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1287 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1288
1289 mncc->msg_type = msg_type;
1290
Harald Welte9cfc9352009-06-26 19:39:35 +02001291 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001292 if (!msg)
1293 return -ENOMEM;
Harald Welte17b56522010-12-22 21:45:05 +01001294
1295 data = msgb_put(msg, sizeof(struct gsm_mncc));
1296 memcpy(data, mncc, sizeof(struct gsm_mncc));
1297
Harald Weltebe0cb682010-12-22 23:43:29 +01001298 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001299
1300 return 0;
1301}
1302
1303int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001304 uint32_t callref, int location, int value)
Harald Welte03740842009-06-10 23:11:52 +08001305{
1306 struct gsm_mncc rel;
1307
Harald Weltecb0595f2009-06-12 01:54:08 +08001308 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001309 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001310 mncc_set_cause(&rel, location, value);
Andreas Eversberg59e79ee2013-01-25 08:38:29 +01001311 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1312 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08001313 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1314}
1315
Harald Weltec2189a62009-07-23 18:56:43 +02001316/* Call Control Specific transaction release.
1317 * gets called by trans_free, DO NOT CALL YOURSELF! */
1318void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001319{
Harald Welte03740842009-06-10 23:11:52 +08001320 gsm48_stop_cc_timer(trans);
1321
1322 /* send release to L4, if callref still exists */
1323 if (trans->callref) {
1324 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001325 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001326 GSM48_CAUSE_LOC_PRN_S_LU,
1327 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001328 }
Harald Weltec2189a62009-07-23 18:56:43 +02001329 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001330 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001331 if (trans->conn)
1332 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001333}
1334
1335static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001336
Harald Welte59b04682009-06-10 05:40:52 +08001337/* call-back from paging the B-end of the connection */
1338static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001339 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001340{
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001341 int found = 0;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001342 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001343 struct gsm_network **paging_request = param, *net;
Harald Welte03740842009-06-10 23:11:52 +08001344 struct gsm_trans *transt, *tmp;
Harald Welte1ff81b52009-06-26 20:17:06 +02001345
Harald Welte59b04682009-06-10 05:40:52 +08001346 if (hooknum != GSM_HOOK_RR_PAGING)
1347 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001348
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001349 net = *paging_request;
Harald Welte03740842009-06-10 23:11:52 +08001350 if (!net) {
1351 DEBUGP(DCC, "Error Network not set!\n");
1352 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001353 }
1354
Harald Welte03740842009-06-10 23:11:52 +08001355 /* check all tranactions (without lchan) for subscriber */
1356 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001357 if (transt->paging_request != paging_request || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001358 continue;
1359 switch (event) {
1360 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001361 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001362 break;
1363 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001364 transt->subscr->extension);
1365 found = 1;
Harald Welte03740842009-06-10 23:11:52 +08001366 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001367 if (!transt->conn) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001368 transt->paging_request = NULL;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001369 transt->conn = conn;
Holger Hans Peter Freytherfb68bf32010-12-29 16:28:33 +01001370 conn->put_channel = 1;
Harald Welte03740842009-06-10 23:11:52 +08001371 }
1372 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001373 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001374 break;
1375 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001376 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001377 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001378 transt->subscr->extension);
Harald Welte03740842009-06-10 23:11:52 +08001379 /* Temporarily out of order */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001380 found = 1;
Harald Welte0abe5a62009-07-23 19:06:52 +02001381 mncc_release_ind(transt->subscr->net, transt,
1382 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001383 GSM48_CAUSE_LOC_PRN_S_LU,
1384 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001385 transt->callref = 0;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001386 transt->paging_request = NULL;
Harald Weltec2189a62009-07-23 18:56:43 +02001387 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001388 break;
1389 }
1390 }
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001391
1392 talloc_free(paging_request);
1393
1394 /*
1395 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1396 * failure and then the subscr will be poke again. This needs a lot of fixing
1397 * in the subscriber queue code.
1398 */
1399 if (!found && conn)
1400 conn->put_channel = 1;
Harald Welte59b04682009-06-10 05:40:52 +08001401 return 0;
1402}
1403
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001404static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welte3971ad52009-12-19 22:23:05 +01001405
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001406/* handle audio path for handover */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001407static int switch_for_handover(struct gsm_lchan *old_lchan,
1408 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001409{
1410 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001411
1412 if (ipacc_rtp_direct) {
1413 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1414 return 0;
1415 }
1416
1417 /* RTP Proxy mode */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001418 new_rs = new_lchan->abis_ip.rtp_socket;
1419 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001420
1421 if (!new_rs) {
1422 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1423 return -EIO;
1424 }
1425
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001426 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001427
1428 if (!old_rs) {
1429 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1430 return -EIO;
1431 }
1432
1433 /* copy rx_action and reference to other sock */
1434 new_rs->rx_action = old_rs->rx_action;
1435 new_rs->tx_action = old_rs->tx_action;
1436 new_rs->transmit = old_rs->transmit;
1437
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001438 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001439 case RTP_PROXY:
1440 other_rs = old_rs->proxy.other_sock;
1441 rtp_socket_proxy(new_rs, other_rs);
1442 /* delete reference to other end socket to prevent
1443 * rtp_socket_free() from removing the inverse reference */
1444 old_rs->proxy.other_sock = NULL;
1445 break;
1446 case RTP_RECV_UPSTREAM:
1447 new_rs->receive = old_rs->receive;
1448 break;
1449 case RTP_NONE:
1450 break;
1451 }
1452
1453 return 0;
1454}
1455
Harald Welte3c062072009-07-28 18:25:29 +02001456/* some other part of the code sends us a signal */
1457static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1458 void *handler_data, void *signal_data)
1459{
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001460 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte3c062072009-07-28 18:25:29 +02001461 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001462 struct gsm_network *net;
1463 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001464
1465 if (subsys != SS_ABISIP)
1466 return 0;
1467
1468 /* in case we use direct BTS-to-BTS RTP */
1469 if (ipacc_rtp_direct)
1470 return 0;
1471
Harald Welte3c062072009-07-28 18:25:29 +02001472 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001473 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001474 /* in case we don't use direct BTS-to-BTS RTP */
1475 /* the BTS has successfully bound a TCH to a local ip/port,
1476 * which means we can connect our UDP socket to it */
1477 if (lchan->abis_ip.rtp_socket) {
1478 rtp_socket_free(lchan->abis_ip.rtp_socket);
1479 lchan->abis_ip.rtp_socket = NULL;
1480 }
1481
1482 lchan->abis_ip.rtp_socket = rtp_socket_create();
1483 if (!lchan->abis_ip.rtp_socket)
1484 return -EIO;
1485
1486 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1487 lchan->abis_ip.bound_ip,
1488 lchan->abis_ip.bound_port);
1489 if (rc < 0)
1490 return -EIO;
1491
Harald Welte3971ad52009-12-19 22:23:05 +01001492 /* check if any transactions on this lchan still have
1493 * a tch_recv_mncc request pending */
1494 net = lchan->ts->trx->bts->network;
1495 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001496 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001497 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1498 tch_recv_mncc(net, trans->callref, 1);
1499 }
1500 }
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001501
1502 /*
1503 * TODO: this appears to be too early? Why not until after
1504 * the handover detect or the handover complete?
1505 *
1506 * Do we have a handover pending for this new lchan? In that
1507 * case re-route the audio from the old channel to the new one.
1508 */
1509 old_lchan = bsc_handover_pending(lchan);
1510 if (old_lchan)
1511 switch_for_handover(old_lchan, lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001512 break;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001513 case S_ABISIP_DLCX_IND:
1514 /* the BTS tells us a RTP stream has been disconnected */
1515 if (lchan->abis_ip.rtp_socket) {
1516 rtp_socket_free(lchan->abis_ip.rtp_socket);
1517 lchan->abis_ip.rtp_socket = NULL;
1518 }
1519
1520 break;
Harald Welte3c062072009-07-28 18:25:29 +02001521 }
1522
1523 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001524}
1525
Harald Welte59b04682009-06-10 05:40:52 +08001526/* map two ipaccess RTP streams onto each other */
1527static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1528{
1529 struct gsm_bts *bts = lchan->ts->trx->bts;
1530 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001531 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001532
1533 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1534 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1535 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1536
1537 if (bts->type != remote_bts->type) {
Harald Welte8099a712011-08-09 21:53:20 +02001538 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte59b04682009-06-10 05:40:52 +08001539 return -EINVAL;
1540 }
Harald Welte3971ad52009-12-19 22:23:05 +01001541
1542 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001543 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001544 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001545 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3c062072009-07-28 18:25:29 +02001546 if (!ipacc_rtp_direct) {
1547 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001548 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001549 if (rc < 0)
1550 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001551 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte30f93fb2011-02-19 16:48:17 +01001552 if (rc < 0)
1553 return rc;
Harald Welte3c062072009-07-28 18:25:29 +02001554 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301555 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1556 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001557 } else {
1558 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301559 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1560 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301561 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001562 if (rc < 0)
1563 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301564 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1565 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301566 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001567 }
Harald Welte59b04682009-06-10 05:40:52 +08001568 break;
1569 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001570 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar49c843e2011-07-28 00:01:50 +02001571 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte59b04682009-06-10 05:40:52 +08001572 trau_mux_map_lchan(lchan, remote_lchan);
1573 break;
1574 default:
Harald Welte8099a712011-08-09 21:53:20 +02001575 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001576 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001577 }
1578
1579 return 0;
1580}
1581
Harald Welte03740842009-06-10 23:11:52 +08001582/* bridge channels of two transactions */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001583static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001584{
Harald Weltec2189a62009-07-23 18:56:43 +02001585 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1586 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001587
Harald Welte03740842009-06-10 23:11:52 +08001588 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001589 return -EIO;
1590
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001591 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001592 return -EIO;
1593
1594 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001595 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001596}
1597
Harald Welte3971ad52009-12-19 22:23:05 +01001598/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001599static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001600{
1601 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001602 struct gsm_lchan *lchan;
1603 struct gsm_bts *bts;
1604 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001605
Harald Welte03740842009-06-10 23:11:52 +08001606 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001607 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001608 if (!trans)
1609 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001610 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001611 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001612 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001613 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001614
Harald Welte3971ad52009-12-19 22:23:05 +01001615 switch (bts->type) {
1616 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001617 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3971ad52009-12-19 22:23:05 +01001618 if (ipacc_rtp_direct) {
Harald Welte8099a712011-08-09 21:53:20 +02001619 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welte3971ad52009-12-19 22:23:05 +01001620 return -EINVAL;
1621 }
1622 /* in case, we don't have a RTP socket yet, we note this
1623 * in the transaction and try later */
1624 if (!lchan->abis_ip.rtp_socket) {
1625 trans->tch_recv = enable;
1626 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1627 return 0;
1628 }
1629 if (enable) {
1630 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001631 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001632 if (rc < 0)
1633 return rc;
1634 /* assign socket to application interface */
1635 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1636 net, callref);
1637 } else
1638 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1639 net, 0);
1640 break;
1641 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001642 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02001643 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte3971ad52009-12-19 22:23:05 +01001644 if (enable)
1645 return trau_recv_lchan(lchan, callref);
1646 return trau_mux_unmap(NULL, callref);
1647 break;
1648 default:
Harald Welte8099a712011-08-09 21:53:20 +02001649 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001650 return -EINVAL;
1651 }
1652
1653 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001654}
1655
Harald Welte03740842009-06-10 23:11:52 +08001656static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1657{
1658 DEBUGP(DCC, "-> STATUS ENQ\n");
1659 return gsm48_cc_tx_status(trans, msg);
1660}
1661
1662static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1663static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1664
1665static void gsm48_cc_timeout(void *arg)
1666{
1667 struct gsm_trans *trans = arg;
1668 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001669 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1670 int mo_location = GSM48_CAUSE_LOC_USER;
1671 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1672 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001673 struct gsm_mncc mo_rel, l4_rel;
1674
1675 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1676 mo_rel.callref = trans->callref;
1677 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1678 l4_rel.callref = trans->callref;
1679
Harald Weltec2189a62009-07-23 18:56:43 +02001680 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001681 case 0x303:
1682 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001683 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001684 break;
1685 case 0x310:
1686 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001687 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001688 break;
1689 case 0x313:
1690 disconnect = 1;
1691 /* unknown, did not find it in the specs */
1692 break;
1693 case 0x301:
1694 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001695 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001696 break;
1697 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001698 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001699 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001700 gsm48_cc_tx_release(trans, &trans->cc.msg);
1701 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001702 break; /* stay in release state */
1703 }
Harald Weltec2189a62009-07-23 18:56:43 +02001704 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001705 return;
1706// release = 1;
1707// l4_cause = 14;
1708// break;
1709 case 0x306:
1710 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001711 mo_cause = trans->cc.msg.cause.value;
1712 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001713 break;
1714 case 0x323:
1715 disconnect = 1;
1716 break;
1717 default:
1718 release = 1;
1719 }
1720
1721 if (release && trans->callref) {
1722 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001723 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001724 l4_location, l4_cause);
1725 trans->callref = 0;
1726 }
1727
1728 if (disconnect && trans->callref) {
1729 /* process disconnect towards layer 4 */
1730 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001731 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001732 }
1733
1734 /* process disconnect towards mobile station */
1735 if (disconnect || release) {
1736 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001737 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1738 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1739 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001740 mo_rel.cause.diag_len = 3;
1741
1742 if (disconnect)
1743 gsm48_cc_tx_disconnect(trans, &mo_rel);
1744 if (release)
1745 gsm48_cc_tx_release(trans, &mo_rel);
1746 }
1747
1748}
1749
1750static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1751 int sec, int micro)
1752{
1753 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001754 trans->cc.timer.cb = gsm48_cc_timeout;
1755 trans->cc.timer.data = trans;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001756 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltec2189a62009-07-23 18:56:43 +02001757 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001758}
1759
1760static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1761{
1762 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001763 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte03740842009-06-10 23:11:52 +08001764 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1765 struct tlv_parsed tp;
1766 struct gsm_mncc setup;
1767
1768 memset(&setup, 0, sizeof(struct gsm_mncc));
1769 setup.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001770 if (trans->conn && trans->conn->lchan)
1771 setup.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001772 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001773 /* emergency setup is identified by msg_type */
1774 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1775 setup.emergency = 1;
1776
1777 /* use subscriber as calling party number */
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02001778 setup.fields |= MNCC_F_CALLING;
1779 strncpy(setup.calling.number, trans->subscr->extension,
1780 sizeof(setup.calling.number)-1);
1781 strncpy(setup.imsi, trans->subscr->imsi,
1782 sizeof(setup.imsi)-1);
1783
Harald Welte03740842009-06-10 23:11:52 +08001784 /* bearer capability */
1785 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1786 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001787 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001788 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1789 }
1790 /* facility */
1791 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1792 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001793 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001794 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1795 }
1796 /* called party bcd number */
1797 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1798 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001799 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001800 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1801 }
1802 /* user-user */
1803 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1804 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001805 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001806 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1807 }
1808 /* ss-version */
1809 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1810 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001811 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001812 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1813 }
1814 /* CLIR suppression */
1815 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1816 setup.clir.sup = 1;
1817 /* CLIR invocation */
1818 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1819 setup.clir.inv = 1;
1820 /* cc cap */
1821 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1822 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001823 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001824 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1825 }
1826
Harald Welte03740842009-06-10 23:11:52 +08001827 new_cc_state(trans, GSM_CSTATE_INITIATED);
1828
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001829 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1830 subscr_name(trans->subscr), trans->subscr->extension,
1831 setup.called.number);
1832
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001833 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001834
Harald Welte03740842009-06-10 23:11:52 +08001835 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001836 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001837
Harald Welteca745e22009-07-29 12:10:35 +02001838 /* MNCC code will modify the channel asynchronously, we should
1839 * ipaccess-bind only after the modification has been made to the
1840 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001841 return 0;
1842}
1843
1844static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001845{
1846 struct msgb *msg = gsm48_msgb_alloc();
1847 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001848 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001849 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001850
1851 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1852
Harald Welte03740842009-06-10 23:11:52 +08001853 /* transaction id must not be assigned */
1854 if (trans->transaction_id != 0xff) { /* unasssigned */
1855 DEBUGP(DCC, "TX Setup with assigned transaction. "
1856 "This is not allowed!\n");
1857 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001858 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001859 GSM48_CAUSE_LOC_PRN_S_LU,
1860 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001861 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001862 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001863 return rc;
1864 }
1865
1866 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001867 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1868 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001869 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001870 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001871 GSM48_CAUSE_LOC_PRN_S_LU,
1872 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001873 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001874 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001875 return rc;
1876 }
Harald Welte165fc332009-07-23 21:36:44 +02001877 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001878
Harald Welte59b04682009-06-10 05:40:52 +08001879 gh->msg_type = GSM48_MT_CC_SETUP;
1880
Harald Welte03740842009-06-10 23:11:52 +08001881 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001882
Harald Welte03740842009-06-10 23:11:52 +08001883 /* bearer capability */
1884 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001885 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001886 /* facility */
1887 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001888 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001889 /* progress */
1890 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001891 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001892 /* calling party BCD number */
1893 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001894 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001895 /* called party BCD number */
1896 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001897 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001898 /* user-user */
1899 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001900 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001901 /* redirecting party BCD number */
1902 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001903 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001904 /* signal */
1905 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001906 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001907
1908 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001909
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001910 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001911
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001912 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001913}
1914
Harald Welte03740842009-06-10 23:11:52 +08001915static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1916{
1917 struct gsm48_hdr *gh = msgb_l3(msg);
1918 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1919 struct tlv_parsed tp;
1920 struct gsm_mncc call_conf;
1921
1922 gsm48_stop_cc_timer(trans);
1923 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1924
1925 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1926 call_conf.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001927 if (trans->conn && trans->conn->lchan)
1928 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001929 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001930#if 0
1931 /* repeat */
1932 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1933 call_conf.repeat = 1;
1934 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1935 call_conf.repeat = 2;
1936#endif
1937 /* bearer capability */
1938 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1939 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001940 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001941 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1942 }
1943 /* cause */
1944 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1945 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001946 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001947 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1948 }
1949 /* cc cap */
1950 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1951 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001952 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001953 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1954 }
1955
1956 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1957
Harald Welte0abe5a62009-07-23 19:06:52 +02001958 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1959 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001960}
1961
1962static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1963{
1964 struct gsm_mncc *proceeding = arg;
1965 struct msgb *msg = gsm48_msgb_alloc();
1966 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1967
Harald Welte03740842009-06-10 23:11:52 +08001968 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1969
1970 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1971
1972 /* bearer capability */
1973 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001974 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001975 /* facility */
1976 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001977 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001978 /* progress */
1979 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001980 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001981
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001982 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001983}
1984
1985static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1986{
1987 struct gsm48_hdr *gh = msgb_l3(msg);
1988 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1989 struct tlv_parsed tp;
1990 struct gsm_mncc alerting;
1991
1992 gsm48_stop_cc_timer(trans);
1993 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1994
1995 memset(&alerting, 0, sizeof(struct gsm_mncc));
1996 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001997 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001998 /* facility */
1999 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2000 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002001 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08002002 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2003 }
2004
2005 /* progress */
2006 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2007 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01002008 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08002009 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2010 }
2011 /* ss-version */
2012 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2013 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002014 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002015 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2016 }
2017
2018 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2019
Harald Welte0abe5a62009-07-23 19:06:52 +02002020 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2021 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08002022}
2023
2024static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2025{
2026 struct gsm_mncc *alerting = arg;
2027 struct msgb *msg = gsm48_msgb_alloc();
2028 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2029
Harald Welte03740842009-06-10 23:11:52 +08002030 gh->msg_type = GSM48_MT_CC_ALERTING;
2031
2032 /* facility */
2033 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002034 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08002035 /* progress */
2036 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002037 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08002038 /* user-user */
2039 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002040 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002041
2042 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2043
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002044 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002045}
2046
2047static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2048{
2049 struct gsm_mncc *progress = arg;
2050 struct msgb *msg = gsm48_msgb_alloc();
2051 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2052
Harald Welte03740842009-06-10 23:11:52 +08002053 gh->msg_type = GSM48_MT_CC_PROGRESS;
2054
2055 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002056 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002057 /* user-user */
2058 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002059 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002060
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002061 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002062}
2063
2064static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2065{
2066 struct gsm_mncc *connect = arg;
2067 struct msgb *msg = gsm48_msgb_alloc();
2068 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2069
Harald Welte03740842009-06-10 23:11:52 +08002070 gh->msg_type = GSM48_MT_CC_CONNECT;
2071
2072 gsm48_stop_cc_timer(trans);
2073 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2074
2075 /* facility */
2076 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002077 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002078 /* progress */
2079 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002080 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002081 /* connected number */
2082 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002083 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002084 /* user-user */
2085 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002086 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002087
2088 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2089
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002090 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002091}
2092
2093static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2094{
2095 struct gsm48_hdr *gh = msgb_l3(msg);
2096 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2097 struct tlv_parsed tp;
2098 struct gsm_mncc connect;
2099
2100 gsm48_stop_cc_timer(trans);
2101
2102 memset(&connect, 0, sizeof(struct gsm_mncc));
2103 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002104 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002105 /* use subscriber as connected party number */
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02002106 connect.fields |= MNCC_F_CONNECTED;
2107 strncpy(connect.connected.number, trans->subscr->extension,
2108 sizeof(connect.connected.number)-1);
2109 strncpy(connect.imsi, trans->subscr->imsi,
2110 sizeof(connect.imsi)-1);
2111
Harald Welte03740842009-06-10 23:11:52 +08002112 /* facility */
2113 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2114 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002115 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002116 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2117 }
2118 /* user-user */
2119 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2120 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002121 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002122 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2123 }
2124 /* ss-version */
2125 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2126 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002127 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002128 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2129 }
2130
2131 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002132 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002133
Harald Welte0abe5a62009-07-23 19:06:52 +02002134 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002135}
2136
2137
2138static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2139{
2140 struct gsm_mncc connect_ack;
2141
2142 gsm48_stop_cc_timer(trans);
2143
2144 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002145 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002146
2147 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2148 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002149
Harald Welte0abe5a62009-07-23 19:06:52 +02002150 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002151 &connect_ack);
2152}
2153
2154static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2155{
2156 struct msgb *msg = gsm48_msgb_alloc();
2157 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2158
Harald Welte03740842009-06-10 23:11:52 +08002159 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2160
2161 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2162
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002163 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002164}
2165
2166static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2167{
2168 struct gsm48_hdr *gh = msgb_l3(msg);
2169 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2170 struct tlv_parsed tp;
2171 struct gsm_mncc disc;
2172
2173 gsm48_stop_cc_timer(trans);
2174
2175 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2176
2177 memset(&disc, 0, sizeof(struct gsm_mncc));
2178 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002179 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002180 /* cause */
2181 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2182 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002183 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002184 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2185 }
2186 /* facility */
2187 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2188 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002189 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002190 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2191 }
2192 /* user-user */
2193 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2194 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002195 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002196 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2197 }
2198 /* ss-version */
2199 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2200 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002201 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002202 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2203 }
2204
Harald Welte0abe5a62009-07-23 19:06:52 +02002205 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002206
2207}
2208
Harald Weltebbc636a2009-06-11 14:23:20 +08002209static struct gsm_mncc_cause default_cause = {
2210 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2211 .coding = 0,
2212 .rec = 0,
2213 .rec_val = 0,
2214 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2215 .diag_len = 0,
2216 .diag = { 0 },
2217};
Harald Welte03740842009-06-10 23:11:52 +08002218
2219static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2220{
2221 struct gsm_mncc *disc = arg;
2222 struct msgb *msg = gsm48_msgb_alloc();
2223 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2224
Harald Welte03740842009-06-10 23:11:52 +08002225 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2226
2227 gsm48_stop_cc_timer(trans);
2228 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2229
2230 /* cause */
2231 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002232 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002233 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002234 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002235
2236 /* facility */
2237 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002238 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002239 /* progress */
2240 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002241 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002242 /* user-user */
2243 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002244 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002245
2246 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002247 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002248
2249 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2250
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002251 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002252}
2253
2254static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2255{
2256 struct gsm48_hdr *gh = msgb_l3(msg);
2257 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2258 struct tlv_parsed tp;
2259 struct gsm_mncc rel;
2260 int rc;
2261
2262 gsm48_stop_cc_timer(trans);
2263
2264 memset(&rel, 0, sizeof(struct gsm_mncc));
2265 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002266 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002267 /* cause */
2268 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2269 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002270 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002271 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2272 }
2273 /* facility */
2274 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2275 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002276 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002277 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2278 }
2279 /* user-user */
2280 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2281 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002282 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002283 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2284 }
2285 /* ss-version */
2286 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2287 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002288 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002289 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2290 }
2291
Harald Weltec2189a62009-07-23 18:56:43 +02002292 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002293 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002294 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002295 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002296 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002297 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002298 GSM48_MT_CC_RELEASE_COMPL);
2299 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002300 }
2301
2302 new_cc_state(trans, GSM_CSTATE_NULL);
2303
2304 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002305 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002306
2307 return rc;
2308}
2309
2310static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2311{
2312 struct gsm_mncc *rel = arg;
2313 struct msgb *msg = gsm48_msgb_alloc();
2314 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2315
Harald Welte03740842009-06-10 23:11:52 +08002316 gh->msg_type = GSM48_MT_CC_RELEASE;
2317
Harald Welte03740842009-06-10 23:11:52 +08002318 gsm48_stop_cc_timer(trans);
2319 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2320
2321 /* cause */
2322 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002323 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002324 /* facility */
2325 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002326 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002327 /* user-user */
2328 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002329 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002330
Harald Weltec2189a62009-07-23 18:56:43 +02002331 trans->cc.T308_second = 0;
2332 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002333
Harald Weltec2189a62009-07-23 18:56:43 +02002334 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002335 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2336
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002337 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002338}
2339
2340static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2341{
2342 struct gsm48_hdr *gh = msgb_l3(msg);
2343 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2344 struct tlv_parsed tp;
2345 struct gsm_mncc rel;
2346 int rc = 0;
2347
2348 gsm48_stop_cc_timer(trans);
2349
2350 memset(&rel, 0, sizeof(struct gsm_mncc));
2351 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002352 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002353 /* cause */
2354 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2355 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002356 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002357 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2358 }
2359 /* facility */
2360 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2361 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002362 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002363 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2364 }
2365 /* user-user */
2366 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2367 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002368 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002369 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2370 }
2371 /* ss-version */
2372 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2373 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002374 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002375 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2376 }
2377
2378 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002379 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002380 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002381 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002382 MNCC_REJ_IND, &rel);
2383 break;
2384 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002385 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002386 MNCC_REL_CNF, &rel);
2387 break;
2388 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002389 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002390 MNCC_REL_IND, &rel);
2391 }
2392 }
2393
2394 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002395 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002396
2397 return rc;
2398}
2399
2400static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2401{
2402 struct gsm_mncc *rel = arg;
2403 struct msgb *msg = gsm48_msgb_alloc();
2404 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002405 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002406
Harald Welte03740842009-06-10 23:11:52 +08002407 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2408
2409 trans->callref = 0;
2410
2411 gsm48_stop_cc_timer(trans);
2412
2413 /* cause */
2414 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002415 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002416 /* facility */
2417 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002418 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002419 /* user-user */
2420 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002421 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002422
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002423 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2424
Harald Weltec2189a62009-07-23 18:56:43 +02002425 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002426
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002427 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002428}
2429
2430static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2431{
2432 struct gsm48_hdr *gh = msgb_l3(msg);
2433 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2434 struct tlv_parsed tp;
2435 struct gsm_mncc fac;
2436
2437 memset(&fac, 0, sizeof(struct gsm_mncc));
2438 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002439 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002440 /* facility */
2441 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2442 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002443 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002444 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2445 }
2446 /* ss-version */
2447 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2448 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002449 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002450 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2451 }
2452
Harald Welte0abe5a62009-07-23 19:06:52 +02002453 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002454}
2455
2456static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2457{
2458 struct gsm_mncc *fac = arg;
2459 struct msgb *msg = gsm48_msgb_alloc();
2460 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2461
Harald Welte03740842009-06-10 23:11:52 +08002462 gh->msg_type = GSM48_MT_CC_FACILITY;
2463
2464 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002465 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002466
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002467 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002468}
2469
2470static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2471{
2472 struct gsm_mncc hold;
2473
2474 memset(&hold, 0, sizeof(struct gsm_mncc));
2475 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002476 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002477}
2478
2479static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2480{
2481 struct msgb *msg = gsm48_msgb_alloc();
2482 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2483
Harald Welte03740842009-06-10 23:11:52 +08002484 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2485
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002486 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002487}
2488
2489static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2490{
2491 struct gsm_mncc *hold_rej = arg;
2492 struct msgb *msg = gsm48_msgb_alloc();
2493 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2494
Harald Welte03740842009-06-10 23:11:52 +08002495 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2496
2497 /* cause */
2498 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002499 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002500 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002501 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002502
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002503 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002504}
2505
2506static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2507{
2508 struct gsm_mncc retrieve;
2509
2510 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2511 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002512 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2513 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002514}
2515
2516static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2517{
2518 struct msgb *msg = gsm48_msgb_alloc();
2519 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2520
Harald Welte03740842009-06-10 23:11:52 +08002521 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2522
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002523 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002524}
2525
2526static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2527{
2528 struct gsm_mncc *retrieve_rej = arg;
2529 struct msgb *msg = gsm48_msgb_alloc();
2530 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2531
Harald Welte03740842009-06-10 23:11:52 +08002532 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2533
2534 /* cause */
2535 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002536 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002537 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002538 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002539
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002540 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002541}
2542
2543static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2544{
2545 struct gsm48_hdr *gh = msgb_l3(msg);
2546 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2547 struct tlv_parsed tp;
2548 struct gsm_mncc dtmf;
2549
2550 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2551 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002552 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002553 /* keypad facility */
2554 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2555 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002556 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002557 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2558 }
2559
Harald Welte0abe5a62009-07-23 19:06:52 +02002560 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002561}
2562
2563static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2564{
2565 struct gsm_mncc *dtmf = arg;
2566 struct msgb *msg = gsm48_msgb_alloc();
2567 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2568
Harald Welte03740842009-06-10 23:11:52 +08002569 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2570
2571 /* keypad */
2572 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002573 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002574
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002575 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002576}
2577
2578static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2579{
2580 struct gsm_mncc *dtmf = arg;
2581 struct msgb *msg = gsm48_msgb_alloc();
2582 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2583
Harald Welte03740842009-06-10 23:11:52 +08002584 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2585
2586 /* cause */
2587 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002588 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002589 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002590 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002591
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002592 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002593}
2594
2595static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2596{
2597 struct msgb *msg = gsm48_msgb_alloc();
2598 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2599
Harald Welte03740842009-06-10 23:11:52 +08002600 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2601
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002602 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002603}
2604
2605static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2606{
2607 struct gsm_mncc dtmf;
2608
2609 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2610 dtmf.callref = trans->callref;
2611
Harald Welte0abe5a62009-07-23 19:06:52 +02002612 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002613}
2614
2615static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2616{
2617 struct gsm48_hdr *gh = msgb_l3(msg);
2618 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2619 struct tlv_parsed tp;
2620 struct gsm_mncc modify;
2621
2622 memset(&modify, 0, sizeof(struct gsm_mncc));
2623 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002624 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002625 /* bearer capability */
2626 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2627 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002628 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002629 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2630 }
2631
2632 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2633
Harald Welte0abe5a62009-07-23 19:06:52 +02002634 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002635}
2636
2637static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2638{
2639 struct gsm_mncc *modify = arg;
2640 struct msgb *msg = gsm48_msgb_alloc();
2641 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2642
Harald Welte03740842009-06-10 23:11:52 +08002643 gh->msg_type = GSM48_MT_CC_MODIFY;
2644
2645 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2646
2647 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002648 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002649
2650 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2651
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002652 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002653}
2654
2655static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2656{
2657 struct gsm48_hdr *gh = msgb_l3(msg);
2658 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2659 struct tlv_parsed tp;
2660 struct gsm_mncc modify;
2661
2662 gsm48_stop_cc_timer(trans);
2663
2664 memset(&modify, 0, sizeof(struct gsm_mncc));
2665 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002666 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002667 /* bearer capability */
2668 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2669 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002670 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002671 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2672 }
2673
2674 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2675
Harald Welte0abe5a62009-07-23 19:06:52 +02002676 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002677}
2678
2679static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2680{
2681 struct gsm_mncc *modify = arg;
2682 struct msgb *msg = gsm48_msgb_alloc();
2683 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2684
Harald Welte03740842009-06-10 23:11:52 +08002685 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2686
2687 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002688 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002689
2690 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2691
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002692 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002693}
2694
2695static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2696{
2697 struct gsm48_hdr *gh = msgb_l3(msg);
2698 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2699 struct tlv_parsed tp;
2700 struct gsm_mncc modify;
2701
2702 gsm48_stop_cc_timer(trans);
2703
2704 memset(&modify, 0, sizeof(struct gsm_mncc));
2705 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002706 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002707 /* bearer capability */
2708 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2709 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002710 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002711 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2712 }
2713 /* cause */
2714 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2715 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002716 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002717 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2718 }
2719
2720 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2721
Harald Welte0abe5a62009-07-23 19:06:52 +02002722 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002723}
2724
2725static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2726{
2727 struct gsm_mncc *modify = arg;
2728 struct msgb *msg = gsm48_msgb_alloc();
2729 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2730
Harald Welte03740842009-06-10 23:11:52 +08002731 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2732
2733 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002734 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002735 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002736 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002737
2738 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2739
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002740 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002741}
2742
2743static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2744{
2745 struct gsm_mncc *notify = arg;
2746 struct msgb *msg = gsm48_msgb_alloc();
2747 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2748
Harald Welte03740842009-06-10 23:11:52 +08002749 gh->msg_type = GSM48_MT_CC_NOTIFY;
2750
2751 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002752 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002753
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002754 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002755}
2756
2757static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2758{
2759 struct gsm48_hdr *gh = msgb_l3(msg);
2760 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2761// struct tlv_parsed tp;
2762 struct gsm_mncc notify;
2763
2764 memset(&notify, 0, sizeof(struct gsm_mncc));
2765 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002766// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002767 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002768 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002769
Harald Welte0abe5a62009-07-23 19:06:52 +02002770 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002771}
2772
2773static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2774{
2775 struct gsm_mncc *user = arg;
2776 struct msgb *msg = gsm48_msgb_alloc();
2777 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2778
Harald Welte03740842009-06-10 23:11:52 +08002779 gh->msg_type = GSM48_MT_CC_USER_INFO;
2780
2781 /* user-user */
2782 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002783 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002784 /* more data */
2785 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002786 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002787
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002788 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002789}
2790
2791static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2792{
2793 struct gsm48_hdr *gh = msgb_l3(msg);
2794 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2795 struct tlv_parsed tp;
2796 struct gsm_mncc user;
2797
2798 memset(&user, 0, sizeof(struct gsm_mncc));
2799 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002800 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002801 /* user-user */
2802 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2803 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002804 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002805 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2806 }
2807 /* more data */
2808 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2809 user.more = 1;
2810
Harald Welte0abe5a62009-07-23 19:06:52 +02002811 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002812}
2813
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002814static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002815{
2816 struct gsm_mncc *mode = arg;
2817
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01002818 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte03740842009-06-10 23:11:52 +08002819}
2820
2821static struct downstate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02002822 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08002823 int type;
2824 int (*rout) (struct gsm_trans *trans, void *arg);
2825} downstatelist[] = {
2826 /* mobile originating call establishment */
2827 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2828 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2829 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2830 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2831 {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 */
2832 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2833 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2834 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2835 /* mobile terminating call establishment */
2836 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2837 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2838 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2839 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2840 /* signalling during call */
2841 {SBIT(GSM_CSTATE_ACTIVE),
2842 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2843 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2844 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2845 {ALL_STATES,
2846 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2847 {ALL_STATES,
2848 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2849 {ALL_STATES,
2850 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2851 {SBIT(GSM_CSTATE_ACTIVE),
2852 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2853 {SBIT(GSM_CSTATE_ACTIVE),
2854 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2855 {SBIT(GSM_CSTATE_ACTIVE),
2856 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2857 {SBIT(GSM_CSTATE_ACTIVE),
2858 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2859 {SBIT(GSM_CSTATE_ACTIVE),
2860 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2861 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2862 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2863 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2864 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2865 {SBIT(GSM_CSTATE_ACTIVE),
2866 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2867 /* clearing */
2868 {SBIT(GSM_CSTATE_INITIATED),
2869 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2870 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2871 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2872 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2873 MNCC_REL_REQ, gsm48_cc_tx_release},
2874 /* special */
2875 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002876 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002877};
2878
2879#define DOWNSLLEN \
2880 (sizeof(downstatelist) / sizeof(struct downstate))
2881
2882
Harald Weltef4e4b8a2010-12-22 23:57:45 +01002883int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002884{
Harald Welteaa60edb2009-08-09 18:52:33 +02002885 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002886 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002887 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freyther50366f22010-12-27 16:02:25 +01002888 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002889 struct gsm_mncc *data = arg, rel;
2890
Harald Welte17b56522010-12-22 21:45:05 +01002891 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2892
Harald Welte03740842009-06-10 23:11:52 +08002893 /* handle special messages */
2894 switch(msg_type) {
2895 case MNCC_BRIDGE:
2896 return tch_bridge(net, arg);
2897 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002898 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002899 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002900 return tch_recv_mncc(net, data->callref, 1);
2901 case GSM_TCHF_FRAME:
2902 /* Find callref */
2903 trans = trans_find_by_callref(net, data->callref);
Harald Welte17b56522010-12-22 21:45:05 +01002904 if (!trans) {
2905 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002906 return -EIO;
Harald Welte17b56522010-12-22 21:45:05 +01002907 }
2908 if (!trans->conn) {
2909 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002910 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002911 }
2912 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2913 /* This should be LOGL_ERROR or NOTICE, but
2914 * unfortuantely it happens for a couple of frames at
2915 * the beginning of every RTP connection */
2916 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002917 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002918 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002919 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002920 switch (bts->type) {
2921 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02002922 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte17b56522010-12-22 21:45:05 +01002923 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte45d6e4d2010-12-24 12:40:21 +01002924 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002925 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002926 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002927 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002928 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03002929 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02002930 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002931 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002932 default:
Harald Welte8099a712011-08-09 21:53:20 +02002933 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01002934 }
2935 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002936 }
2937
2938 memset(&rel, 0, sizeof(struct gsm_mncc));
2939 rel.callref = data->callref;
2940
2941 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002942 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002943
2944 /* Callref unknown */
2945 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002946 struct gsm_subscriber *subscr;
2947
Harald Welte6e1536e2009-07-04 10:11:24 +02002948 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002949 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2950 "Received '%s' from MNCC with "
2951 "unknown callref %d\n", data->called.number,
2952 get_mncc_name(msg_type), data->callref);
2953 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002954 return mncc_release_ind(net, NULL, data->callref,
2955 GSM48_CAUSE_LOC_PRN_S_LU,
2956 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002957 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002958 if (!data->called.number[0] && !data->imsi[0]) {
2959 DEBUGP(DCC, "(bts - trx - ts - ti) "
2960 "Received '%s' from MNCC with "
2961 "no number or IMSI\n", get_mncc_name(msg_type));
2962 /* Invalid number */
2963 return mncc_release_ind(net, NULL, data->callref,
2964 GSM48_CAUSE_LOC_PRN_S_LU,
2965 GSM48_CC_CAUSE_INV_NR_FORMAT);
2966 }
Harald Welte03740842009-06-10 23:11:52 +08002967 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002968 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002969 subscr = subscr_get_by_extension(net,
2970 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002971 else
Harald Welte75350412009-07-23 18:46:00 +02002972 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002973 /* If subscriber is not found */
2974 if (!subscr) {
2975 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2976 "Received '%s' from MNCC with "
2977 "unknown subscriber %s\n", data->called.number,
2978 get_mncc_name(msg_type), data->called.number);
2979 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002980 return mncc_release_ind(net, NULL, data->callref,
2981 GSM48_CAUSE_LOC_PRN_S_LU,
2982 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002983 }
2984 /* If subscriber is not "attached" */
2985 if (!subscr->lac) {
2986 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2987 "Received '%s' from MNCC with "
2988 "detached subscriber %s\n", data->called.number,
2989 get_mncc_name(msg_type), data->called.number);
2990 subscr_put(subscr);
2991 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002992 return mncc_release_ind(net, NULL, data->callref,
2993 GSM48_CAUSE_LOC_PRN_S_LU,
2994 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002995 }
2996 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002997 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2998 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002999 DEBUGP(DCC, "No memory for trans.\n");
3000 subscr_put(subscr);
3001 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003002 mncc_release_ind(net, NULL, data->callref,
3003 GSM48_CAUSE_LOC_PRN_S_LU,
3004 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08003005 return -ENOMEM;
3006 }
Harald Welte03740842009-06-10 23:11:52 +08003007 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003008 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003009
Harald Welte03740842009-06-10 23:11:52 +08003010 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003011 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003012 /* find transaction with this subscriber already paging */
3013 llist_for_each_entry(transt, &net->trans_list, entry) {
3014 /* Transaction of our lchan? */
3015 if (transt == trans ||
3016 transt->subscr != subscr)
3017 continue;
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003018 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003019 "Received '%s' from MNCC with "
3020 "unallocated channel, paging already "
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003021 "started for lac %d.\n",
Harald Welte03740842009-06-10 23:11:52 +08003022 data->called.number,
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003023 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003024 subscr_put(subscr);
3025 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003026 return 0;
3027 }
3028 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02003029 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01003030
3031 /* Get a channel */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01003032 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3033 if (!trans->paging_request) {
3034 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3035 subscr_put(subscr);
3036 trans_free(trans);
3037 return 0;
3038 }
3039
3040 *trans->paging_request = subscr->net;
3041 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut892f6952010-12-01 22:17:36 +01003042
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003043 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003044 return 0;
3045 }
3046 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003047 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003048 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003049 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003050
3051 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003052 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08003053
3054 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003055 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003056 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003057 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08003058 (trans->subscr)?(trans->subscr->extension):"-",
3059 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003060 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3061 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003062 if (msg_type == MNCC_REL_REQ)
3063 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3064 else
3065 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3066 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003067 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003068 return rc;
3069 }
3070
3071 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3072 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003073 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08003074 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003075 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003076 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003077 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003078
3079 /* Find function for current state and message */
3080 for (i = 0; i < DOWNSLLEN; i++)
3081 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003082 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003083 break;
3084 if (i == DOWNSLLEN) {
3085 DEBUGP(DCC, "Message unhandled at this state.\n");
3086 return 0;
3087 }
3088
3089 rc = downstatelist[i].rout(trans, arg);
3090
3091 return rc;
3092}
3093
3094
3095static struct datastate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003096 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08003097 int type;
3098 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3099} datastatelist[] = {
3100 /* mobile originating call establishment */
3101 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3102 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3103 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3104 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3105 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3106 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3107 /* mobile terminating call establishment */
3108 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3109 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3110 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3111 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003112 {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 +08003113 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3114 /* signalling during call */
3115 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3116 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3117 {SBIT(GSM_CSTATE_ACTIVE),
3118 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3119 {ALL_STATES,
3120 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3121 {ALL_STATES,
3122 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3123 {ALL_STATES,
3124 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3125 {SBIT(GSM_CSTATE_ACTIVE),
3126 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3127 {SBIT(GSM_CSTATE_ACTIVE),
3128 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3129 {SBIT(GSM_CSTATE_ACTIVE),
3130 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3131 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3132 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3133 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3134 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3135 {SBIT(GSM_CSTATE_ACTIVE),
3136 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3137 /* clearing */
3138 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3139 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3140 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3141 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3142 {ALL_STATES, /* 5.4.3.4 */
3143 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3144};
3145
3146#define DATASLLEN \
3147 (sizeof(datastatelist) / sizeof(struct datastate))
3148
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003149static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003150{
3151 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003152 uint8_t msg_type = gh->msg_type & 0xbf;
3153 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003154 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003155 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003156
Harald Welte03740842009-06-10 23:11:52 +08003157 if (msg_type & 0x80) {
3158 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3159 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003160 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003161
Harald Welte03740842009-06-10 23:11:52 +08003162 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003163 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003164
Harald Welte4861c822009-07-23 21:21:14 +02003165 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003166 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003167 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003168 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003169 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3170 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003171
3172 /* Create transaction */
3173 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003174 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003175 "creating new trans.\n", transaction_id);
3176 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003177 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003178 transaction_id, new_callref++);
3179 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003180 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01003181 rc = gsm48_tx_simple(conn,
Harald Welte4861c822009-07-23 21:21:14 +02003182 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003183 GSM48_MT_CC_RELEASE_COMPL);
3184 return -ENOMEM;
3185 }
Harald Welte03740842009-06-10 23:11:52 +08003186 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003187 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003188 }
3189
3190 /* find function for current state and message */
3191 for (i = 0; i < DATASLLEN; i++)
3192 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003193 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003194 break;
3195 if (i == DATASLLEN) {
3196 DEBUGP(DCC, "Message unhandled at this state.\n");
3197 return 0;
3198 }
3199
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02003200 assert(trans->subscr);
3201
Harald Welte03740842009-06-10 23:11:52 +08003202 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003203
3204 return rc;
3205}
3206
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003207/* Create a dummy to wait five seconds */
3208static void release_anchor(struct gsm_subscriber_connection *conn)
3209{
3210 if (!conn->anch_operation)
3211 return;
3212
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003213 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003214 talloc_free(conn->anch_operation);
3215 conn->anch_operation = NULL;
3216}
3217
3218static void anchor_timeout(void *_data)
3219{
3220 struct gsm_subscriber_connection *con = _data;
3221
3222 release_anchor(con);
3223 msc_release_connection(con);
3224}
3225
3226int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3227{
3228 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3229 if (!conn->anch_operation)
3230 return -1;
3231
3232 conn->anch_operation->timeout.data = conn;
3233 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003234 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003235 return 0;
3236}
3237
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003238/* here we get data from the BSC level... */
3239int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003240{
3241 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003242 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte59b04682009-06-10 05:40:52 +08003243 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003244
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003245 if (silent_call_reroute(conn, msg))
3246 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003247
3248 switch (pdisc) {
3249 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003250 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003251 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003252 break;
3253 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003254 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003255 break;
3256 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003257 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003258 break;
3259 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003260 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003261 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003262 break;
3263 case GSM48_PDISC_MM_GPRS:
3264 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003265 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3266 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003267 break;
Harald Welte03115042009-10-16 08:32:58 +02003268 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003269 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003270 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003271 break;
Harald Welte59b04682009-06-10 05:40:52 +08003272 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003273 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3274 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003275 break;
3276 }
3277
3278 return rc;
3279}
3280
Harald Welte3c062072009-07-28 18:25:29 +02003281/*
3282 * This will be ran by the linker when loading the DSO. We use it to
3283 * do system initialization, e.g. registration of signal handlers.
3284 */
3285static __attribute__((constructor)) void on_dso_load_0408(void)
3286{
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02003287 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte3c062072009-07-28 18:25:29 +02003288}