blob: 3efdb8f29b8575440f22075d1c35f7bd1c15f2ad [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{
Holger Hans Peter Freyther89e3f8a2013-12-27 17:37:27 +0100321 if (!conn->loc_operation)
322 return 0;
323
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100324 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800325 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100326 conn->loc_operation->key_seq,
327 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800328 return 0;
329}
330
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800331void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800332{
Harald Welte03740842009-06-10 23:11:52 +0800333 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800334
335 /* avoid someone issuing a clear */
336 conn->in_release = 1;
337
Harald Welte59b04682009-06-10 05:40:52 +0800338 /*
339 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800340 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800341 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800342 release_loc_updating_req(conn);
Holger Hans Peter Freytherd91346a2010-12-28 14:57:20 +0100343
344 /* We might need to cancel the paging response or such. */
345 if (conn->sec_operation && conn->sec_operation->cb) {
346 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
347 NULL, conn, conn->sec_operation->cb_data);
348 }
349
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800350 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800351 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800352
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100353 /*
354 * Free all transactions that are associated with the released
355 * connection. The transaction code will inform the CC or SMS
356 * facilities that will send the release indications. As part of
357 * the CC REL_IND the remote leg might be released and this will
358 * trigger the call to trans_free. This is something the llist
359 * macro can not handle and we will need to re-iterate the list.
360 *
361 * TODO: Move the trans_list into the subscriber connection and
362 * create a pending list for MT transactions. These exist before
363 * we have a subscriber connection.
364 */
365restart:
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800366 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100367 if (trans->conn == conn) {
Harald Weltec2189a62009-07-23 18:56:43 +0200368 trans_free(trans);
Holger Hans Peter Freyther0b28dbc2012-12-22 18:45:27 +0100369 goto restart;
370 }
Harald Welte03740842009-06-10 23:11:52 +0800371 }
Harald Welte59b04682009-06-10 05:40:52 +0800372}
373
Harald Weltee8d16172010-12-22 23:17:50 +0100374void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
375{
376 struct gsm_trans *trans, *temp;
377
378 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
379
380 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Weltebd0f33c2010-12-23 02:47:53 +0100381 if (trans->protocol == protocol) {
382 trans->callref = 0;
Harald Weltee8d16172010-12-22 23:17:50 +0100383 trans_free(trans);
Harald Weltebd0f33c2010-12-23 02:47:53 +0100384 }
Harald Weltee8d16172010-12-22 23:17:50 +0100385 }
386}
387
Harald Welte59b04682009-06-10 05:40:52 +0800388/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200389int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800390{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800391 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800392 struct msgb *msg;
393
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200394 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800395
396 msg = gsm48_create_loc_upd_rej(cause);
397 if (!msg) {
398 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
399 return -1;
400 }
Harald Welte59b04682009-06-10 05:40:52 +0800401
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800402 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800403
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100404 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100405 "LAC=%u BTS=%u\n", conn->subscr ?
406 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800407 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100408
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800409 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800410}
411
412/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200413int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800414{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800415 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800416 struct msgb *msg = gsm48_msgb_alloc();
417 struct gsm48_hdr *gh;
418 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200419 uint8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800420
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800421 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800422
423 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
424 gh->proto_discr = GSM48_PDISC_MM;
425 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
426
427 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100428 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800429 bts->network->network_code, bts->location_area_code);
430
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200431 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200432 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800433
434 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
435
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200436 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100437
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800438 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800439}
440
Harald Welte59b04682009-06-10 05:40:52 +0800441/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200442static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800443{
444 struct msgb *msg = gsm48_msgb_alloc();
445 struct gsm48_hdr *gh;
446
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800447 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800448
449 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
450 gh->proto_discr = GSM48_PDISC_MM;
451 gh->msg_type = GSM48_MT_MM_ID_REQ;
452 gh->data[0] = id_type;
453
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800454 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800455}
456
Harald Welte59b04682009-06-10 05:40:52 +0800457
458/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800459static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800460{
461 struct gsm48_hdr *gh = msgb_l3(msg);
462 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200463 struct gsm_bts *bts = lchan->ts->trx->bts;
464 struct gsm_network *net = bts->network;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200465 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200466 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800467
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200468 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800469 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
470 mi_type, mi_string);
471
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200472 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welteedf48af2009-12-13 12:39:18 +0100473
Harald Welte59b04682009-06-10 05:40:52 +0800474 switch (mi_type) {
475 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200476 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100477 if (!conn->subscr) {
478 conn->subscr = subscr_get_by_imsi(net, mi_string);
479 if (!conn->subscr)
Holger Hans Peter Freytherabe587d2013-10-04 08:35:11 +0200480 conn->subscr = subscr_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200481 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100482 if (conn->loc_operation)
483 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800484 break;
485 case GSM_MI_TYPE_IMEI:
486 case GSM_MI_TYPE_IMEISV:
487 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100488 if (conn->subscr) {
489 db_subscriber_assoc_imei(conn->subscr, mi_string);
490 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200491 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100492 if (conn->loc_operation)
493 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800494 break;
495 }
496
497 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100498 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800499}
500
501
502static void loc_upd_rej_cb(void *data)
503{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100504 struct gsm_subscriber_connection *conn = data;
505 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100506 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800507
Holger Hans Peter Freyther0d91c462011-11-07 12:26:29 +0100508 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800509 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800510 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800511}
512
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100513static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800514{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100515 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
516 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200517 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800518}
519
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200520static const char *lupd_name(uint8_t type)
Harald Welte59b04682009-06-10 05:40:52 +0800521{
522 switch (type) {
523 case GSM48_LUPD_NORMAL:
524 return "NORMAL";
525 case GSM48_LUPD_PERIODIC:
Alexander Chemeris979d1372013-10-04 02:42:26 +0200526 return "PERIODIC";
Harald Welte59b04682009-06-10 05:40:52 +0800527 case GSM48_LUPD_IMSI_ATT:
528 return "IMSI ATTACH";
529 default:
530 return "UNKNOWN";
531 }
532}
533
Harald Welte59b04682009-06-10 05:40:52 +0800534/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800535static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800536{
537 struct gsm48_hdr *gh = msgb_l3(msg);
538 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800539 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800540 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200541 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200542 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800543
544 lu = (struct gsm48_loc_upd_req *) gh->data;
545
546 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
547
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200548 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800549
Harald Welte79639662009-06-27 02:58:43 +0200550 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800551 lupd_name(lu->type));
552
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200553 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welteedf48af2009-12-13 12:39:18 +0100554
Harald Welte3edc5a92009-12-22 00:41:05 +0100555 switch (lu->type) {
556 case GSM48_LUPD_NORMAL:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200557 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100558 break;
559 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200560 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100561 break;
562 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200563 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100564 break;
565 }
566
Harald Welte59b04682009-06-10 05:40:52 +0800567 /*
568 * Pseudo Spoof detection: Just drop a second/concurrent
569 * location updating request.
570 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100571 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200572 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100573 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800574 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800575 return 0;
576 }
577
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800578 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800579
Sylvain Munaut360fc582010-06-10 13:36:59 +0200580 conn->loc_operation->key_seq = lu->key_seq;
581
Harald Welte59b04682009-06-10 05:40:52 +0800582 switch (mi_type) {
583 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200584 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800585 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200586 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100587 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800588
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200589 /* look up subscriber based on IMSI, create if not found */
590 subscr = subscr_get_by_imsi(bts->network, mi_string);
591 if (!subscr) {
Holger Hans Peter Freytherabe587d2013-10-04 08:35:11 +0200592 subscr = subscr_create_subscriber(bts->network, mi_string);
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200593 }
Harald Welte59b04682009-06-10 05:40:52 +0800594 break;
595 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200596 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800597 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200598 subscr = subscr_get_by_tmsi(bts->network,
599 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800600 if (!subscr) {
601 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200602 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100603 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800604 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200605 /* we always want the IMEI, too */
Holger Hans Peter Freyther1a9f3ee2012-09-11 11:55:03 +0200606 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200607 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800608 break;
609 case GSM_MI_TYPE_IMEI:
610 case GSM_MI_TYPE_IMEISV:
611 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200612 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800613 break;
614 default:
Harald Welte79639662009-06-27 02:58:43 +0200615 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800616 break;
617 }
618
Harald Welteccd69362009-07-04 10:18:00 +0200619 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100620 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200621
Harald Welte03740842009-06-10 23:11:52 +0800622 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200623 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800624 /* FIXME: request id? close channel? */
625 return -EINVAL;
626 }
627
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100628 conn->subscr = subscr;
629 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800630
Harald Welteccd69362009-07-04 10:18:00 +0200631 /* check if we can let the subscriber into our network immediately
632 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100633 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800634}
635
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100636/* Turn int into semi-octet representation: 98 => 0x89 */
637static uint8_t bcdify(uint8_t value)
Harald Welte03740842009-06-10 23:11:52 +0800638{
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100639 uint8_t ret;
640
641 ret = value / 10;
642 ret |= (value % 10) << 4;
643
644 return ret;
Harald Welte03740842009-06-10 23:11:52 +0800645}
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100646
Harald Welte03740842009-06-10 23:11:52 +0800647
Harald Welte59b04682009-06-10 05:40:52 +0800648/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800649int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800650{
651 struct msgb *msg = gsm48_msgb_alloc();
652 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800653 struct gsm_network *net = conn->bts->network;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100654 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200655 uint8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200656 int name_len, name_pad;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100657
Harald Welte03740842009-06-10 23:11:52 +0800658 time_t cur_t;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100659 struct tm* gmt_time;
660 struct tm* local_time;
661 int tzunits;
Harald Welte59b04682009-06-10 05:40:52 +0800662
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800663 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800664
665 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
666 gh->proto_discr = GSM48_PDISC_MM;
667 gh->msg_type = GSM48_MT_MM_INFO;
668
669 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200670#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800671 name_len = strlen(net->name_long);
672 /* 10.5.3.5a */
673 ptr8 = msgb_put(msg, 3);
674 ptr8[0] = GSM48_IE_NAME_LONG;
675 ptr8[1] = name_len*2 +1;
676 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
677
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200678 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800679 for (i = 0; i < name_len; i++)
680 ptr16[i] = htons(net->name_long[i]);
681
682 /* FIXME: Use Cell Broadcast, not UCS-2, since
683 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200684#endif
685 name_len = (strlen(net->name_long)*7)/8;
686 name_pad = (8 - strlen(net->name_long)*7)%8;
687 if (name_pad > 0)
688 name_len++;
689 /* 10.5.3.5a */
690 ptr8 = msgb_put(msg, 3);
691 ptr8[0] = GSM48_IE_NAME_LONG;
692 ptr8[1] = name_len +1;
693 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
694
695 ptr8 = msgb_put(msg, name_len);
696 gsm_7bit_encode(ptr8, net->name_long);
697
Harald Welte59b04682009-06-10 05:40:52 +0800698 }
699
700 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200701#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800702 name_len = strlen(net->name_short);
703 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200704 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200705 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800706 ptr8[1] = name_len*2 + 1;
707 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
708
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200709 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800710 for (i = 0; i < name_len; i++)
711 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200712#endif
713 name_len = (strlen(net->name_short)*7)/8;
714 name_pad = (8 - strlen(net->name_short)*7)%8;
715 if (name_pad > 0)
716 name_len++;
717 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200718 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmannc8188202009-08-13 03:42:07 +0200719 ptr8[0] = GSM48_IE_NAME_SHORT;
720 ptr8[1] = name_len +1;
721 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
722
723 ptr8 = msgb_put(msg, name_len);
724 gsm_7bit_encode(ptr8, net->name_short);
725
Harald Welte59b04682009-06-10 05:40:52 +0800726 }
727
Harald Welte59b04682009-06-10 05:40:52 +0800728 /* Section 10.5.3.9 */
729 cur_t = time(NULL);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100730 gmt_time = gmtime(&cur_t);
731
Harald Welte59b04682009-06-10 05:40:52 +0800732 ptr8 = msgb_put(msg, 8);
733 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100734 ptr8[1] = bcdify(gmt_time->tm_year % 100);
735 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
736 ptr8[3] = bcdify(gmt_time->tm_mday);
737 ptr8[4] = bcdify(gmt_time->tm_hour);
738 ptr8[5] = bcdify(gmt_time->tm_min);
739 ptr8[6] = bcdify(gmt_time->tm_sec);
740
Harald Welte5c3abf92012-07-08 16:48:11 +0200741 if (bts->tz.override) {
742 /* Convert tz.hr and tz.mn to units */
743 if (bts->tz.hr < 0) {
744 tzunits = ((bts->tz.hr/-1)*4);
745 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100746 ptr8[7] = bcdify(tzunits);
747 /* Set negative time */
748 ptr8[7] |= 0x08;
749 }
750 else {
Harald Welte5c3abf92012-07-08 16:48:11 +0200751 tzunits = bts->tz.hr*4;
752 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100753 ptr8[7] = bcdify(tzunits);
754 }
755 }
756 else {
757 /* Need to get GSM offset and convert into 15 min units */
758 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
759 local_time = localtime(&cur_t);
Harald Welteeaf4a522012-04-08 16:59:24 +0200760#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100761 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welteeaf4a522012-04-08 16:59:24 +0200762#else
763#warning find a portable way to obtain the timezone offset
764 tzunits = 0;
765#endif
Gus Bourgf9dc16b2011-12-02 10:18:17 +0100766 if (tzunits < 0) {
767 tzunits = tzunits/-1;
768 ptr8[7] = bcdify(tzunits);
769 /* Flip it to negative */
770 ptr8[7] |= 0x08;
771 }
772 else
773 ptr8[7] = bcdify(tzunits);
774 }
Harald Welte59b04682009-06-10 05:40:52 +0800775
Daniel Willmannc8188202009-08-13 03:42:07 +0200776 DEBUGP(DMM, "-> MM INFO\n");
777
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800778 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800779}
780
Harald Welte36970f62009-08-12 22:56:50 +0200781/* Section 9.2.2 */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200782int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200783{
784 struct msgb *msg = gsm48_msgb_alloc();
785 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200786 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200787
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200788 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200789
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800790 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200791 gh->proto_discr = GSM48_PDISC_MM;
792 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
793
Sylvain Munaut0789c592009-12-24 00:23:46 +0100794 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200795
Harald Welte36970f62009-08-12 22:56:50 +0200796 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200797 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200798 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200799
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800800 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200801}
802
803/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800804int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200805{
806 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800807 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200808}
809
Holger Hans Peter Freyther61e7c8c2013-12-27 17:19:19 +0100810/*
811 * At the 30C3 phones miss their periodic update
812 * interval a lot and then remain unreachable. In case
813 * we still know the TMSI we can just attach it again.
814 */
815static void implit_attach(struct gsm_subscriber_connection *conn)
816{
817 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
818 return;
819
820 subscr_update(conn->subscr, conn->bts,
821 GSM_SUBSCRIBER_UPDATE_ATTACHED);
822}
823
824
Sylvain Munautbadc7112009-12-24 00:28:46 +0100825static int _gsm48_rx_mm_serv_req_sec_cb(
826 unsigned int hooknum, unsigned int event,
827 struct msgb *msg, void *data, void *param)
828{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800829 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100830 int rc = 0;
831
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +0200832 /* auth failed or succeeded, the timer was stopped */
833 conn->expire_timer_stopped = 1;
834
Sylvain Munautbadc7112009-12-24 00:28:46 +0100835 switch (event) {
836 case GSM_SECURITY_AUTH_FAILED:
837 /* Nothing to do */
838 break;
839
840 case GSM_SECURITY_NOAVAIL:
Andreas Eversbergee995352013-07-10 08:58:03 +0200841 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800842 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther61e7c8c2013-12-27 17:19:19 +0100843 implit_attach(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100844 break;
845
846 case GSM_SECURITY_SUCCEEDED:
847 /* nothing to do. CIPHER MODE COMMAND is
848 * implicit CM SERV ACK */
Holger Hans Peter Freyther61e7c8c2013-12-27 17:19:19 +0100849 implit_attach(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100850 break;
851
852 default:
853 rc = -EINVAL;
854 };
855
856 return rc;
857}
858
Harald Welte59b04682009-06-10 05:40:52 +0800859/*
860 * Handle CM Service Requests
861 * a) Verify that the packet is long enough to contain the information
862 * we require otherwsie reject with INCORRECT_MESSAGE
863 * b) Try to parse the TMSI. If we do not have one reject
864 * c) Check that we know the subscriber with the TMSI otherwise reject
865 * with a HLR cause
866 * d) Set the subscriber on the gsm_lchan and accept
867 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800868static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800869{
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200870 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200871 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800872
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800873 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800874 struct gsm_subscriber *subscr;
875 struct gsm48_hdr *gh = msgb_l3(msg);
876 struct gsm48_service_request *req =
877 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800878 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200879 uint8_t classmark2_len = gh->data[1];
880 uint8_t *classmark2 = gh->data+2;
881 uint8_t mi_len = *(classmark2 + classmark2_len);
882 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte59b04682009-06-10 05:40:52 +0800883
884 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
885 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
886 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800887 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800888 GSM48_REJECT_INCORRECT_MESSAGE);
889 }
890
891 if (msg->data_len < req->mi_len + 6) {
892 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800893 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800894 GSM48_REJECT_INCORRECT_MESSAGE);
895 }
896
897 mi_type = mi[0] & GSM_MI_TYPE_MASK;
898 if (mi_type != GSM_MI_TYPE_TMSI) {
899 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800900 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800901 GSM48_REJECT_INCORRECT_MESSAGE);
902 }
903
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200904 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800905 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
906 req->cm_service_type, mi_type, mi_string);
907
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200908 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welteedf48af2009-12-13 12:39:18 +0100909
Harald Welte583ceaf2009-08-10 10:12:45 +0200910 if (is_siemens_bts(bts))
911 send_siemens_mrpci(msg->lchan, classmark2-1);
912
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200913 subscr = subscr_get_by_tmsi(bts->network,
914 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800915
916 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
917 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800918 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800919 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
920
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800921 if (!conn->subscr)
922 conn->subscr = subscr;
923 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100924 subscr_put(subscr); /* lchan already has a ref, don't need another one */
925 else {
Harald Welte59b04682009-06-10 05:40:52 +0800926 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
927 subscr_put(subscr);
928 }
929
Harald Weltef6845a72009-07-05 14:08:13 +0200930 subscr->equipment.classmark2_len = classmark2_len;
931 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
932 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800933
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +0200934 /* we will send a MM message soon */
935 conn->expire_timer_stopped = 1;
936
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800937 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +0100938 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800939}
940
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200941static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800942{
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200943 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800944 struct gsm48_hdr *gh = msgb_l3(msg);
945 struct gsm48_imsi_detach_ind *idi =
946 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200947 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200948 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800949 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800950
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200951 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100952 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte59b04682009-06-10 05:40:52 +0800953 mi_type, mi_string);
954
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200955 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100956
Harald Welte59b04682009-06-10 05:40:52 +0800957 switch (mi_type) {
958 case GSM_MI_TYPE_TMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100959 DEBUGPC(DMM, "\n");
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200960 subscr = subscr_get_by_tmsi(bts->network,
961 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800962 break;
963 case GSM_MI_TYPE_IMSI:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100964 DEBUGPC(DMM, "\n");
Harald Welte75350412009-07-23 18:46:00 +0200965 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800966 break;
967 case GSM_MI_TYPE_IMEI:
968 case GSM_MI_TYPE_IMEISV:
969 /* no sim card... FIXME: what to do ? */
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100970 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800971 break;
972 default:
Daniel Willmann2be30fe2012-12-28 19:29:44 +0100973 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800974 break;
975 }
976
977 if (subscr) {
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200978 subscr_update(subscr, bts,
Harald Welte59b04682009-06-10 05:40:52 +0800979 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100980 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200981
982 subscr->equipment.classmark1 = idi->classmark1;
983 db_sync_equipment(&subscr->equipment);
984
Harald Welte59b04682009-06-10 05:40:52 +0800985 subscr_put(subscr);
986 } else
987 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
988
Harald Welte43250dd2009-12-20 09:58:40 +0100989 /* FIXME: iterate over all transactions and release them,
990 * imagine an IMSI DETACH happening during an active call! */
991
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +0200992 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800993 return 0;
994}
995
996static int gsm48_rx_mm_status(struct msgb *msg)
997{
998 struct gsm48_hdr *gh = msgb_l3(msg);
999
1000 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1001
1002 return 0;
1003}
1004
Sylvain Munaut28c47162009-12-24 00:27:26 +01001005/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001006static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +01001007{
1008 struct gsm48_hdr *gh = msgb_l3(msg);
1009 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001010 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001011
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001012 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001013 osmo_hexdump(ar->sres, 4));
Sylvain Munaut28c47162009-12-24 00:27:26 +01001014
1015 /* Safety check */
1016 if (!conn->sec_operation) {
1017 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1018 return -EIO;
1019 }
1020
1021 /* Validate SRES */
1022 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001023 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001024 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001025
1026 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001027 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001028
Sylvain Munaut28c47162009-12-24 00:27:26 +01001029 if (cb)
1030 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001031 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001032
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001033 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001034 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +01001035 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +01001036 }
1037
Sylvain Munaut20d99c62010-06-10 23:51:41 +02001038 DEBUGPC(DMM, "OK\n");
1039
Sylvain Munaut28c47162009-12-24 00:27:26 +01001040 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +01001041 return gsm0808_cipher_mode(conn, net->a5_encryption,
1042 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001043}
1044
Harald Welte59b04682009-06-10 05:40:52 +08001045/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001046static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001047{
1048 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +08001049 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08001050
1051 switch (gh->msg_type & 0xbf) {
1052 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +02001053 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001054 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001055 break;
1056 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001057 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001058 break;
1059 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001060 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001061 break;
1062 case GSM48_MT_MM_STATUS:
1063 rc = gsm48_rx_mm_status(msg);
1064 break;
1065 case GSM48_MT_MM_TMSI_REALL_COMPL:
1066 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001067 conn->subscr ?
1068 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +08001069 "unknown subscriber");
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +01001070 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +08001071 break;
1072 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freythere9db44d2012-07-10 08:53:27 +02001073 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001074 break;
1075 case GSM48_MT_MM_CM_REEST_REQ:
1076 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1077 break;
1078 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001079 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001080 break;
1081 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001082 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001083 gh->msg_type);
1084 break;
1085 }
1086
1087 return rc;
1088}
1089
1090/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001091static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001092{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001093 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001094 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001095 struct gsm48_pag_resp *resp;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001096 uint8_t *classmark2_lv = gh->data + 1;
1097 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001098 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001099 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001100 int rc = 0;
1101
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001102 resp = (struct gsm48_pag_resp *) &gh->data[0];
1103 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1104 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001105 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1106 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001107
Harald Welte59b04682009-06-10 05:40:52 +08001108 switch (mi_type) {
1109 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001110 subscr = subscr_get_by_tmsi(bts->network,
1111 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001112 break;
1113 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001114 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001115 break;
1116 }
1117
1118 if (!subscr) {
1119 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1120 /* FIXME: request id? close channel? */
1121 return -EINVAL;
1122 }
1123 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001124 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001125
Harald Weltef6845a72009-07-05 14:08:13 +02001126 subscr->equipment.classmark2_len = *classmark2_lv;
1127 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1128 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001129
Holger Hans Peter Freytherc25021f2013-06-30 15:30:47 +02001130 /* We received a paging */
1131 conn->expire_timer_stopped = 1;
1132
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001133 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001134 return rc;
1135}
1136
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001137static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001138{
1139 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001140 uint8_t apdu_id_flags;
1141 uint8_t apdu_len;
1142 uint8_t *apdu_data;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001143
1144 apdu_id_flags = gh->data[0];
1145 apdu_len = gh->data[1];
1146 apdu_data = gh->data+2;
1147
Harald Welted4f21662012-07-16 13:22:19 +02001148 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001149 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)b5103922009-08-15 23:32:44 +02001150
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001151 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001152}
1153
Harald Welte59b04682009-06-10 05:40:52 +08001154/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001155static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001156{
1157 struct gsm48_hdr *gh = msgb_l3(msg);
1158 int rc = 0;
1159
1160 switch (gh->msg_type) {
Harald Welte59b04682009-06-10 05:40:52 +08001161 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001162 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001163 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001164 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001165 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001166 break;
Harald Welte59b04682009-06-10 05:40:52 +08001167 default:
Harald Weltefe01ed72012-01-23 16:40:24 +01001168 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte60e5f4f2009-12-24 12:13:17 +01001169 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001170 break;
1171 }
1172
1173 return rc;
1174}
1175
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001176int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1177 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001178{
1179 struct msgb *msg = gsm48_msgb_alloc();
1180 struct gsm48_hdr *gh;
1181
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001182 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001183
1184 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1185 apdu_id, apdu_len);
1186
1187 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1188 gh->proto_discr = GSM48_PDISC_RR;
1189 gh->msg_type = GSM48_MT_RR_APP_INFO;
1190 gh->data[0] = apdu_id;
1191 gh->data[1] = apdu_len;
1192 memcpy(gh->data+2, apdu, apdu_len);
1193
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001194 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001195}
1196
Harald Welte59b04682009-06-10 05:40:52 +08001197/* Call Control */
1198
1199/* The entire call control code is written in accordance with Figure 7.10c
1200 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1201 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1202 * it for voice */
1203
Harald Welte03740842009-06-10 23:11:52 +08001204static void new_cc_state(struct gsm_trans *trans, int state)
1205{
1206 if (state > 31 || state < 0)
1207 return;
1208
1209 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001210 gsm48_cc_state_name(trans->cc.state),
1211 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001212
Harald Weltec2189a62009-07-23 18:56:43 +02001213 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001214}
1215
1216static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001217{
1218 struct msgb *msg = gsm48_msgb_alloc();
1219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001220 uint8_t *cause, *call_state;
Harald Welte59b04682009-06-10 05:40:52 +08001221
Harald Welte59b04682009-06-10 05:40:52 +08001222 gh->msg_type = GSM48_MT_CC_STATUS;
1223
1224 cause = msgb_put(msg, 3);
1225 cause[0] = 2;
1226 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1227 cause[2] = 0x80 | 30; /* response to status inquiry */
1228
1229 call_state = msgb_put(msg, 1);
1230 call_state[0] = 0xc0 | 0x00;
1231
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001232 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001233}
1234
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001235static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001236 uint8_t pdisc, uint8_t msg_type)
Harald Welte59b04682009-06-10 05:40:52 +08001237{
1238 struct msgb *msg = gsm48_msgb_alloc();
1239 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1240
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001241 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001242
1243 gh->proto_discr = pdisc;
1244 gh->msg_type = msg_type;
1245
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001246 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001247}
1248
Harald Welte03740842009-06-10 23:11:52 +08001249static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1250{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001251 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltec2189a62009-07-23 18:56:43 +02001252 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001253 osmo_timer_del(&trans->cc.timer);
Harald Weltec2189a62009-07-23 18:56:43 +02001254 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001255 }
1256}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001257
Harald Welte03740842009-06-10 23:11:52 +08001258static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1259 int msg_type, struct gsm_mncc *mncc)
1260{
1261 struct msgb *msg;
Harald Welte17b56522010-12-22 21:45:05 +01001262 unsigned char *data;
Harald Welte03740842009-06-10 23:11:52 +08001263
1264 if (trans)
Harald Weltec51b1922010-12-26 19:06:37 +01001265 if (trans->conn && trans->conn->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001266 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001267 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001268 trans->conn->lchan->ts->trx->bts->nr,
1269 trans->conn->lchan->ts->trx->nr,
1270 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001271 (trans->subscr)?(trans->subscr->extension):"-",
1272 get_mncc_name(msg_type));
1273 else
1274 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1275 "Sending '%s' to MNCC.\n",
1276 (trans->subscr)?(trans->subscr->extension):"-",
1277 get_mncc_name(msg_type));
1278 else
1279 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1280 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1281
1282 mncc->msg_type = msg_type;
1283
Harald Welte9cfc9352009-06-26 19:39:35 +02001284 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001285 if (!msg)
1286 return -ENOMEM;
Harald Welte17b56522010-12-22 21:45:05 +01001287
1288 data = msgb_put(msg, sizeof(struct gsm_mncc));
1289 memcpy(data, mncc, sizeof(struct gsm_mncc));
1290
Harald Weltebe0cb682010-12-22 23:43:29 +01001291 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001292
1293 return 0;
1294}
1295
1296int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001297 uint32_t callref, int location, int value)
Harald Welte03740842009-06-10 23:11:52 +08001298{
1299 struct gsm_mncc rel;
1300
Harald Weltecb0595f2009-06-12 01:54:08 +08001301 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001302 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001303 mncc_set_cause(&rel, location, value);
Andreas Eversberg59e79ee2013-01-25 08:38:29 +01001304 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1305 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08001306 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1307}
1308
Harald Weltec2189a62009-07-23 18:56:43 +02001309/* Call Control Specific transaction release.
1310 * gets called by trans_free, DO NOT CALL YOURSELF! */
1311void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001312{
Harald Welte03740842009-06-10 23:11:52 +08001313 gsm48_stop_cc_timer(trans);
1314
1315 /* send release to L4, if callref still exists */
1316 if (trans->callref) {
1317 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001318 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001319 GSM48_CAUSE_LOC_PRN_S_LU,
1320 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001321 }
Harald Weltec2189a62009-07-23 18:56:43 +02001322 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001323 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001324 if (trans->conn)
1325 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001326}
1327
1328static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001329
Harald Welte59b04682009-06-10 05:40:52 +08001330/* call-back from paging the B-end of the connection */
1331static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001332 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001333{
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001334 int found = 0;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001335 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001336 struct gsm_network **paging_request = param, *net;
Harald Welte03740842009-06-10 23:11:52 +08001337 struct gsm_trans *transt, *tmp;
Harald Welte1ff81b52009-06-26 20:17:06 +02001338
Harald Welte59b04682009-06-10 05:40:52 +08001339 if (hooknum != GSM_HOOK_RR_PAGING)
1340 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001341
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001342 net = *paging_request;
Harald Welte03740842009-06-10 23:11:52 +08001343 if (!net) {
1344 DEBUGP(DCC, "Error Network not set!\n");
1345 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001346 }
1347
Harald Welte03740842009-06-10 23:11:52 +08001348 /* check all tranactions (without lchan) for subscriber */
1349 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001350 if (transt->paging_request != paging_request || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001351 continue;
1352 switch (event) {
1353 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001354 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001355 break;
1356 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001357 transt->subscr->extension);
1358 found = 1;
Harald Welte03740842009-06-10 23:11:52 +08001359 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001360 if (!transt->conn) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001361 transt->paging_request = NULL;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001362 transt->conn = conn;
Holger Hans Peter Freytherfb68bf32010-12-29 16:28:33 +01001363 conn->put_channel = 1;
Harald Welte03740842009-06-10 23:11:52 +08001364 }
1365 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001366 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001367 break;
1368 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001369 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001370 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001371 transt->subscr->extension);
Harald Welte03740842009-06-10 23:11:52 +08001372 /* Temporarily out of order */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001373 found = 1;
Harald Welte0abe5a62009-07-23 19:06:52 +02001374 mncc_release_ind(transt->subscr->net, transt,
1375 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001376 GSM48_CAUSE_LOC_PRN_S_LU,
1377 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001378 transt->callref = 0;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001379 transt->paging_request = NULL;
Harald Weltec2189a62009-07-23 18:56:43 +02001380 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001381 break;
1382 }
1383 }
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001384
1385 talloc_free(paging_request);
1386
1387 /*
1388 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1389 * failure and then the subscr will be poke again. This needs a lot of fixing
1390 * in the subscriber queue code.
1391 */
1392 if (!found && conn)
1393 conn->put_channel = 1;
Harald Welte59b04682009-06-10 05:40:52 +08001394 return 0;
1395}
1396
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001397static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welte3971ad52009-12-19 22:23:05 +01001398
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001399/* handle audio path for handover */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001400static int switch_for_handover(struct gsm_lchan *old_lchan,
1401 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001402{
1403 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001404
1405 if (ipacc_rtp_direct) {
1406 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1407 return 0;
1408 }
1409
1410 /* RTP Proxy mode */
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001411 new_rs = new_lchan->abis_ip.rtp_socket;
1412 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001413
1414 if (!new_rs) {
1415 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1416 return -EIO;
1417 }
1418
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001419 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001420
1421 if (!old_rs) {
1422 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1423 return -EIO;
1424 }
1425
1426 /* copy rx_action and reference to other sock */
1427 new_rs->rx_action = old_rs->rx_action;
1428 new_rs->tx_action = old_rs->tx_action;
1429 new_rs->transmit = old_rs->transmit;
1430
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001431 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001432 case RTP_PROXY:
1433 other_rs = old_rs->proxy.other_sock;
1434 rtp_socket_proxy(new_rs, other_rs);
1435 /* delete reference to other end socket to prevent
1436 * rtp_socket_free() from removing the inverse reference */
1437 old_rs->proxy.other_sock = NULL;
1438 break;
1439 case RTP_RECV_UPSTREAM:
1440 new_rs->receive = old_rs->receive;
1441 break;
1442 case RTP_NONE:
1443 break;
1444 }
1445
1446 return 0;
1447}
1448
Harald Welte3c062072009-07-28 18:25:29 +02001449/* some other part of the code sends us a signal */
1450static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1451 void *handler_data, void *signal_data)
1452{
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001453 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte3c062072009-07-28 18:25:29 +02001454 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001455 struct gsm_network *net;
1456 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001457
1458 if (subsys != SS_ABISIP)
1459 return 0;
1460
1461 /* in case we use direct BTS-to-BTS RTP */
1462 if (ipacc_rtp_direct)
1463 return 0;
1464
Harald Welte3c062072009-07-28 18:25:29 +02001465 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001466 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001467 /* in case we don't use direct BTS-to-BTS RTP */
1468 /* the BTS has successfully bound a TCH to a local ip/port,
1469 * which means we can connect our UDP socket to it */
1470 if (lchan->abis_ip.rtp_socket) {
1471 rtp_socket_free(lchan->abis_ip.rtp_socket);
1472 lchan->abis_ip.rtp_socket = NULL;
1473 }
1474
1475 lchan->abis_ip.rtp_socket = rtp_socket_create();
1476 if (!lchan->abis_ip.rtp_socket)
1477 return -EIO;
1478
1479 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1480 lchan->abis_ip.bound_ip,
1481 lchan->abis_ip.bound_port);
1482 if (rc < 0)
1483 return -EIO;
1484
Harald Welte3971ad52009-12-19 22:23:05 +01001485 /* check if any transactions on this lchan still have
1486 * a tch_recv_mncc request pending */
1487 net = lchan->ts->trx->bts->network;
1488 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001489 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001490 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1491 tch_recv_mncc(net, trans->callref, 1);
1492 }
1493 }
Holger Hans Peter Freytherc3649d22012-12-26 10:17:42 +01001494
1495 /*
1496 * TODO: this appears to be too early? Why not until after
1497 * the handover detect or the handover complete?
1498 *
1499 * Do we have a handover pending for this new lchan? In that
1500 * case re-route the audio from the old channel to the new one.
1501 */
1502 old_lchan = bsc_handover_pending(lchan);
1503 if (old_lchan)
1504 switch_for_handover(old_lchan, lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001505 break;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001506 case S_ABISIP_DLCX_IND:
1507 /* the BTS tells us a RTP stream has been disconnected */
1508 if (lchan->abis_ip.rtp_socket) {
1509 rtp_socket_free(lchan->abis_ip.rtp_socket);
1510 lchan->abis_ip.rtp_socket = NULL;
1511 }
1512
1513 break;
Harald Welte3c062072009-07-28 18:25:29 +02001514 }
1515
1516 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001517}
1518
Harald Welte59b04682009-06-10 05:40:52 +08001519/* map two ipaccess RTP streams onto each other */
1520static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1521{
1522 struct gsm_bts *bts = lchan->ts->trx->bts;
1523 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001524 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001525
1526 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1527 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1528 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1529
1530 if (bts->type != remote_bts->type) {
Harald Welte8099a712011-08-09 21:53:20 +02001531 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte59b04682009-06-10 05:40:52 +08001532 return -EINVAL;
1533 }
Harald Welte3971ad52009-12-19 22:23:05 +01001534
1535 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001536 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001537 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001538 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3c062072009-07-28 18:25:29 +02001539 if (!ipacc_rtp_direct) {
1540 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001541 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001542 if (rc < 0)
1543 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001544 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte30f93fb2011-02-19 16:48:17 +01001545 if (rc < 0)
1546 return rc;
Harald Welte3c062072009-07-28 18:25:29 +02001547 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301548 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1549 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001550 } else {
1551 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301552 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1553 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301554 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001555 if (rc < 0)
1556 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301557 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1558 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301559 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001560 }
Harald Welte59b04682009-06-10 05:40:52 +08001561 break;
1562 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001563 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar49c843e2011-07-28 00:01:50 +02001564 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte59b04682009-06-10 05:40:52 +08001565 trau_mux_map_lchan(lchan, remote_lchan);
1566 break;
1567 default:
Harald Welte8099a712011-08-09 21:53:20 +02001568 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001569 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001570 }
1571
1572 return 0;
1573}
1574
Harald Welte03740842009-06-10 23:11:52 +08001575/* bridge channels of two transactions */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001576static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001577{
Harald Weltec2189a62009-07-23 18:56:43 +02001578 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1579 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001580
Harald Welte03740842009-06-10 23:11:52 +08001581 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001582 return -EIO;
1583
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001584 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001585 return -EIO;
1586
1587 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001588 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001589}
1590
Harald Welte3971ad52009-12-19 22:23:05 +01001591/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001592static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001593{
1594 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001595 struct gsm_lchan *lchan;
1596 struct gsm_bts *bts;
1597 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001598
Harald Welte03740842009-06-10 23:11:52 +08001599 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001600 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001601 if (!trans)
1602 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001603 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001604 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001605 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001606 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001607
Harald Welte3971ad52009-12-19 22:23:05 +01001608 switch (bts->type) {
1609 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02001610 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte3971ad52009-12-19 22:23:05 +01001611 if (ipacc_rtp_direct) {
Harald Welte8099a712011-08-09 21:53:20 +02001612 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welte3971ad52009-12-19 22:23:05 +01001613 return -EINVAL;
1614 }
1615 /* in case, we don't have a RTP socket yet, we note this
1616 * in the transaction and try later */
1617 if (!lchan->abis_ip.rtp_socket) {
1618 trans->tch_recv = enable;
1619 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1620 return 0;
1621 }
1622 if (enable) {
1623 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001624 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001625 if (rc < 0)
1626 return rc;
1627 /* assign socket to application interface */
1628 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1629 net, callref);
1630 } else
1631 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1632 net, 0);
1633 break;
1634 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001635 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02001636 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte3971ad52009-12-19 22:23:05 +01001637 if (enable)
1638 return trau_recv_lchan(lchan, callref);
1639 return trau_mux_unmap(NULL, callref);
1640 break;
1641 default:
Harald Welte8099a712011-08-09 21:53:20 +02001642 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001643 return -EINVAL;
1644 }
1645
1646 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001647}
1648
Harald Welte03740842009-06-10 23:11:52 +08001649static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1650{
1651 DEBUGP(DCC, "-> STATUS ENQ\n");
1652 return gsm48_cc_tx_status(trans, msg);
1653}
1654
1655static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1656static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1657
1658static void gsm48_cc_timeout(void *arg)
1659{
1660 struct gsm_trans *trans = arg;
1661 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001662 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1663 int mo_location = GSM48_CAUSE_LOC_USER;
1664 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1665 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001666 struct gsm_mncc mo_rel, l4_rel;
1667
1668 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1669 mo_rel.callref = trans->callref;
1670 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1671 l4_rel.callref = trans->callref;
1672
Harald Weltec2189a62009-07-23 18:56:43 +02001673 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001674 case 0x303:
1675 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001676 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001677 break;
1678 case 0x310:
1679 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001680 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001681 break;
1682 case 0x313:
1683 disconnect = 1;
1684 /* unknown, did not find it in the specs */
1685 break;
1686 case 0x301:
1687 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001688 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001689 break;
1690 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001691 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001692 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001693 gsm48_cc_tx_release(trans, &trans->cc.msg);
1694 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001695 break; /* stay in release state */
1696 }
Harald Weltec2189a62009-07-23 18:56:43 +02001697 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001698 return;
1699// release = 1;
1700// l4_cause = 14;
1701// break;
1702 case 0x306:
1703 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001704 mo_cause = trans->cc.msg.cause.value;
1705 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001706 break;
1707 case 0x323:
1708 disconnect = 1;
1709 break;
1710 default:
1711 release = 1;
1712 }
1713
1714 if (release && trans->callref) {
1715 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001716 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001717 l4_location, l4_cause);
1718 trans->callref = 0;
1719 }
1720
1721 if (disconnect && trans->callref) {
1722 /* process disconnect towards layer 4 */
1723 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001724 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001725 }
1726
1727 /* process disconnect towards mobile station */
1728 if (disconnect || release) {
1729 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001730 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1731 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1732 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001733 mo_rel.cause.diag_len = 3;
1734
1735 if (disconnect)
1736 gsm48_cc_tx_disconnect(trans, &mo_rel);
1737 if (release)
1738 gsm48_cc_tx_release(trans, &mo_rel);
1739 }
1740
1741}
1742
1743static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1744 int sec, int micro)
1745{
1746 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001747 trans->cc.timer.cb = gsm48_cc_timeout;
1748 trans->cc.timer.data = trans;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001749 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltec2189a62009-07-23 18:56:43 +02001750 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001751}
1752
1753static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1754{
1755 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001756 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte03740842009-06-10 23:11:52 +08001757 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1758 struct tlv_parsed tp;
1759 struct gsm_mncc setup;
1760
1761 memset(&setup, 0, sizeof(struct gsm_mncc));
1762 setup.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001763 if (trans->conn && trans->conn->lchan)
1764 setup.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001765 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001766 /* emergency setup is identified by msg_type */
1767 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1768 setup.emergency = 1;
1769
1770 /* use subscriber as calling party number */
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02001771 setup.fields |= MNCC_F_CALLING;
1772 strncpy(setup.calling.number, trans->subscr->extension,
1773 sizeof(setup.calling.number)-1);
1774 strncpy(setup.imsi, trans->subscr->imsi,
1775 sizeof(setup.imsi)-1);
1776
Harald Welte03740842009-06-10 23:11:52 +08001777 /* bearer capability */
1778 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1779 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001780 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001781 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1782 }
1783 /* facility */
1784 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1785 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001786 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001787 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1788 }
1789 /* called party bcd number */
1790 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1791 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001792 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001793 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1794 }
1795 /* user-user */
1796 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1797 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001798 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001799 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1800 }
1801 /* ss-version */
1802 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1803 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001804 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001805 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1806 }
1807 /* CLIR suppression */
1808 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1809 setup.clir.sup = 1;
1810 /* CLIR invocation */
1811 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1812 setup.clir.inv = 1;
1813 /* cc cap */
1814 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1815 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001816 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001817 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1818 }
1819
Harald Welte03740842009-06-10 23:11:52 +08001820 new_cc_state(trans, GSM_CSTATE_INITIATED);
1821
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001822 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1823 subscr_name(trans->subscr), trans->subscr->extension,
1824 setup.called.number);
1825
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001826 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001827
Harald Welte03740842009-06-10 23:11:52 +08001828 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001829 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001830
Harald Welteca745e22009-07-29 12:10:35 +02001831 /* MNCC code will modify the channel asynchronously, we should
1832 * ipaccess-bind only after the modification has been made to the
1833 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001834 return 0;
1835}
1836
1837static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001838{
1839 struct msgb *msg = gsm48_msgb_alloc();
1840 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001841 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001842 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001843
1844 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1845
Harald Welte03740842009-06-10 23:11:52 +08001846 /* transaction id must not be assigned */
1847 if (trans->transaction_id != 0xff) { /* unasssigned */
1848 DEBUGP(DCC, "TX Setup with assigned transaction. "
1849 "This is not allowed!\n");
1850 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001851 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001852 GSM48_CAUSE_LOC_PRN_S_LU,
1853 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001854 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001855 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001856 return rc;
1857 }
1858
1859 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001860 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1861 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001862 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001863 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001864 GSM48_CAUSE_LOC_PRN_S_LU,
1865 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001866 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001867 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001868 return rc;
1869 }
Harald Welte165fc332009-07-23 21:36:44 +02001870 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001871
Harald Welte59b04682009-06-10 05:40:52 +08001872 gh->msg_type = GSM48_MT_CC_SETUP;
1873
Harald Welte03740842009-06-10 23:11:52 +08001874 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001875
Harald Welte03740842009-06-10 23:11:52 +08001876 /* bearer capability */
1877 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001878 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001879 /* facility */
1880 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001881 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001882 /* progress */
1883 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001884 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001885 /* calling party BCD number */
1886 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001887 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001888 /* called party BCD number */
1889 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001890 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001891 /* user-user */
1892 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001893 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001894 /* redirecting party BCD number */
1895 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001896 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001897 /* signal */
1898 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001899 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001900
1901 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001902
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001903 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001904
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001905 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001906}
1907
Harald Welte03740842009-06-10 23:11:52 +08001908static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1909{
1910 struct gsm48_hdr *gh = msgb_l3(msg);
1911 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1912 struct tlv_parsed tp;
1913 struct gsm_mncc call_conf;
1914
1915 gsm48_stop_cc_timer(trans);
1916 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1917
1918 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1919 call_conf.callref = trans->callref;
Harald Welte1ed89d42011-09-01 18:18:43 +02001920 if (trans->conn && trans->conn->lchan)
1921 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte705f2942010-03-02 23:18:30 +01001922 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001923#if 0
1924 /* repeat */
1925 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1926 call_conf.repeat = 1;
1927 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1928 call_conf.repeat = 2;
1929#endif
1930 /* bearer capability */
1931 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1932 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001933 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001934 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1935 }
1936 /* cause */
1937 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1938 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001939 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001940 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1941 }
1942 /* cc cap */
1943 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1944 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001945 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001946 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1947 }
1948
1949 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1950
Harald Welte0abe5a62009-07-23 19:06:52 +02001951 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1952 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001953}
1954
1955static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1956{
1957 struct gsm_mncc *proceeding = arg;
1958 struct msgb *msg = gsm48_msgb_alloc();
1959 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1960
Harald Welte03740842009-06-10 23:11:52 +08001961 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1962
1963 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1964
1965 /* bearer capability */
1966 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001967 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001968 /* facility */
1969 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001970 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001971 /* progress */
1972 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001973 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001974
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001975 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001976}
1977
1978static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1979{
1980 struct gsm48_hdr *gh = msgb_l3(msg);
1981 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1982 struct tlv_parsed tp;
1983 struct gsm_mncc alerting;
1984
1985 gsm48_stop_cc_timer(trans);
1986 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1987
1988 memset(&alerting, 0, sizeof(struct gsm_mncc));
1989 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001990 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001991 /* facility */
1992 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1993 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001994 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001995 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1996 }
1997
1998 /* progress */
1999 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2000 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01002001 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08002002 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2003 }
2004 /* ss-version */
2005 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2006 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002007 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002008 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2009 }
2010
2011 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2012
Harald Welte0abe5a62009-07-23 19:06:52 +02002013 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2014 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08002015}
2016
2017static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2018{
2019 struct gsm_mncc *alerting = arg;
2020 struct msgb *msg = gsm48_msgb_alloc();
2021 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2022
Harald Welte03740842009-06-10 23:11:52 +08002023 gh->msg_type = GSM48_MT_CC_ALERTING;
2024
2025 /* facility */
2026 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002027 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08002028 /* progress */
2029 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002030 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08002031 /* user-user */
2032 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002033 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002034
2035 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2036
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002037 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002038}
2039
2040static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2041{
2042 struct gsm_mncc *progress = arg;
2043 struct msgb *msg = gsm48_msgb_alloc();
2044 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2045
Harald Welte03740842009-06-10 23:11:52 +08002046 gh->msg_type = GSM48_MT_CC_PROGRESS;
2047
2048 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002049 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002050 /* user-user */
2051 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002052 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002053
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002054 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002055}
2056
2057static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2058{
2059 struct gsm_mncc *connect = arg;
2060 struct msgb *msg = gsm48_msgb_alloc();
2061 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2062
Harald Welte03740842009-06-10 23:11:52 +08002063 gh->msg_type = GSM48_MT_CC_CONNECT;
2064
2065 gsm48_stop_cc_timer(trans);
2066 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2067
2068 /* facility */
2069 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002070 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002071 /* progress */
2072 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002073 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002074 /* connected number */
2075 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002076 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002077 /* user-user */
2078 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002079 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002080
2081 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2082
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002083 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002084}
2085
2086static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2087{
2088 struct gsm48_hdr *gh = msgb_l3(msg);
2089 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2090 struct tlv_parsed tp;
2091 struct gsm_mncc connect;
2092
2093 gsm48_stop_cc_timer(trans);
2094
2095 memset(&connect, 0, sizeof(struct gsm_mncc));
2096 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002097 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002098 /* use subscriber as connected party number */
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02002099 connect.fields |= MNCC_F_CONNECTED;
2100 strncpy(connect.connected.number, trans->subscr->extension,
2101 sizeof(connect.connected.number)-1);
2102 strncpy(connect.imsi, trans->subscr->imsi,
2103 sizeof(connect.imsi)-1);
2104
Harald Welte03740842009-06-10 23:11:52 +08002105 /* facility */
2106 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2107 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002108 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002109 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2110 }
2111 /* user-user */
2112 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2113 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002114 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002115 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2116 }
2117 /* ss-version */
2118 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2119 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002120 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002121 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2122 }
2123
2124 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002125 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002126
Harald Welte0abe5a62009-07-23 19:06:52 +02002127 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002128}
2129
2130
2131static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2132{
2133 struct gsm_mncc connect_ack;
2134
2135 gsm48_stop_cc_timer(trans);
2136
2137 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002138 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002139
2140 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2141 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002142
Harald Welte0abe5a62009-07-23 19:06:52 +02002143 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002144 &connect_ack);
2145}
2146
2147static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2148{
2149 struct msgb *msg = gsm48_msgb_alloc();
2150 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2151
Harald Welte03740842009-06-10 23:11:52 +08002152 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2153
2154 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2155
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002156 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002157}
2158
2159static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2160{
2161 struct gsm48_hdr *gh = msgb_l3(msg);
2162 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2163 struct tlv_parsed tp;
2164 struct gsm_mncc disc;
2165
2166 gsm48_stop_cc_timer(trans);
2167
2168 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2169
2170 memset(&disc, 0, sizeof(struct gsm_mncc));
2171 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002172 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002173 /* cause */
2174 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2175 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002176 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002177 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2178 }
2179 /* facility */
2180 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2181 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002182 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002183 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2184 }
2185 /* user-user */
2186 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2187 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002188 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002189 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2190 }
2191 /* ss-version */
2192 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2193 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002194 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002195 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2196 }
2197
Harald Welte0abe5a62009-07-23 19:06:52 +02002198 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002199
2200}
2201
Harald Weltebbc636a2009-06-11 14:23:20 +08002202static struct gsm_mncc_cause default_cause = {
2203 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2204 .coding = 0,
2205 .rec = 0,
2206 .rec_val = 0,
2207 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2208 .diag_len = 0,
2209 .diag = { 0 },
2210};
Harald Welte03740842009-06-10 23:11:52 +08002211
2212static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2213{
2214 struct gsm_mncc *disc = arg;
2215 struct msgb *msg = gsm48_msgb_alloc();
2216 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2217
Harald Welte03740842009-06-10 23:11:52 +08002218 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2219
2220 gsm48_stop_cc_timer(trans);
2221 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2222
2223 /* cause */
2224 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002225 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002226 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002227 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002228
2229 /* facility */
2230 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002231 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002232 /* progress */
2233 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002234 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002235 /* user-user */
2236 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002237 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002238
2239 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002240 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002241
2242 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2243
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002244 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002245}
2246
2247static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2248{
2249 struct gsm48_hdr *gh = msgb_l3(msg);
2250 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2251 struct tlv_parsed tp;
2252 struct gsm_mncc rel;
2253 int rc;
2254
2255 gsm48_stop_cc_timer(trans);
2256
2257 memset(&rel, 0, sizeof(struct gsm_mncc));
2258 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002259 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002260 /* cause */
2261 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2262 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002263 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002264 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2265 }
2266 /* facility */
2267 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2268 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002269 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002270 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2271 }
2272 /* user-user */
2273 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2274 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002275 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002276 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2277 }
2278 /* ss-version */
2279 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2280 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002281 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002282 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2283 }
2284
Harald Weltec2189a62009-07-23 18:56:43 +02002285 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002286 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002287 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002288 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002289 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002290 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002291 GSM48_MT_CC_RELEASE_COMPL);
2292 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002293 }
2294
2295 new_cc_state(trans, GSM_CSTATE_NULL);
2296
2297 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002298 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002299
2300 return rc;
2301}
2302
2303static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2304{
2305 struct gsm_mncc *rel = arg;
2306 struct msgb *msg = gsm48_msgb_alloc();
2307 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2308
Harald Welte03740842009-06-10 23:11:52 +08002309 gh->msg_type = GSM48_MT_CC_RELEASE;
2310
Harald Welte03740842009-06-10 23:11:52 +08002311 gsm48_stop_cc_timer(trans);
2312 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2313
2314 /* cause */
2315 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002316 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002317 /* facility */
2318 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002319 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002320 /* user-user */
2321 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002322 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002323
Harald Weltec2189a62009-07-23 18:56:43 +02002324 trans->cc.T308_second = 0;
2325 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002326
Harald Weltec2189a62009-07-23 18:56:43 +02002327 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002328 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2329
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002330 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002331}
2332
2333static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2334{
2335 struct gsm48_hdr *gh = msgb_l3(msg);
2336 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2337 struct tlv_parsed tp;
2338 struct gsm_mncc rel;
2339 int rc = 0;
2340
2341 gsm48_stop_cc_timer(trans);
2342
2343 memset(&rel, 0, sizeof(struct gsm_mncc));
2344 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002345 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002346 /* cause */
2347 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2348 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002349 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002350 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2351 }
2352 /* facility */
2353 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2354 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002355 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002356 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2357 }
2358 /* user-user */
2359 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2360 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002361 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002362 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2363 }
2364 /* ss-version */
2365 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2366 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002367 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002368 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2369 }
2370
2371 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002372 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002373 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002374 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002375 MNCC_REJ_IND, &rel);
2376 break;
2377 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002378 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002379 MNCC_REL_CNF, &rel);
2380 break;
2381 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002382 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002383 MNCC_REL_IND, &rel);
2384 }
2385 }
2386
2387 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002388 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002389
2390 return rc;
2391}
2392
2393static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2394{
2395 struct gsm_mncc *rel = arg;
2396 struct msgb *msg = gsm48_msgb_alloc();
2397 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002398 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002399
Harald Welte03740842009-06-10 23:11:52 +08002400 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2401
2402 trans->callref = 0;
2403
2404 gsm48_stop_cc_timer(trans);
2405
2406 /* cause */
2407 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002408 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002409 /* facility */
2410 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002411 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002412 /* user-user */
2413 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002414 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002415
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002416 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2417
Harald Weltec2189a62009-07-23 18:56:43 +02002418 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002419
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002420 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002421}
2422
2423static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2424{
2425 struct gsm48_hdr *gh = msgb_l3(msg);
2426 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2427 struct tlv_parsed tp;
2428 struct gsm_mncc fac;
2429
2430 memset(&fac, 0, sizeof(struct gsm_mncc));
2431 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002432 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002433 /* facility */
2434 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2435 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002436 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002437 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2438 }
2439 /* ss-version */
2440 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2441 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002442 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002443 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2444 }
2445
Harald Welte0abe5a62009-07-23 19:06:52 +02002446 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002447}
2448
2449static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2450{
2451 struct gsm_mncc *fac = arg;
2452 struct msgb *msg = gsm48_msgb_alloc();
2453 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2454
Harald Welte03740842009-06-10 23:11:52 +08002455 gh->msg_type = GSM48_MT_CC_FACILITY;
2456
2457 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002458 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002459
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002460 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002461}
2462
2463static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2464{
2465 struct gsm_mncc hold;
2466
2467 memset(&hold, 0, sizeof(struct gsm_mncc));
2468 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002469 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002470}
2471
2472static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2473{
2474 struct msgb *msg = gsm48_msgb_alloc();
2475 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2476
Harald Welte03740842009-06-10 23:11:52 +08002477 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2478
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002479 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002480}
2481
2482static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2483{
2484 struct gsm_mncc *hold_rej = arg;
2485 struct msgb *msg = gsm48_msgb_alloc();
2486 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2487
Harald Welte03740842009-06-10 23:11:52 +08002488 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2489
2490 /* cause */
2491 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002492 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002493 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002494 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002495
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002496 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002497}
2498
2499static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2500{
2501 struct gsm_mncc retrieve;
2502
2503 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2504 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002505 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2506 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002507}
2508
2509static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2510{
2511 struct msgb *msg = gsm48_msgb_alloc();
2512 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2513
Harald Welte03740842009-06-10 23:11:52 +08002514 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2515
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002516 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002517}
2518
2519static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2520{
2521 struct gsm_mncc *retrieve_rej = arg;
2522 struct msgb *msg = gsm48_msgb_alloc();
2523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2524
Harald Welte03740842009-06-10 23:11:52 +08002525 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2526
2527 /* cause */
2528 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002529 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002530 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002531 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002532
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002533 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002534}
2535
2536static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2537{
2538 struct gsm48_hdr *gh = msgb_l3(msg);
2539 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2540 struct tlv_parsed tp;
2541 struct gsm_mncc dtmf;
2542
2543 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2544 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002545 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002546 /* keypad facility */
2547 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2548 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002549 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002550 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2551 }
2552
Harald Welte0abe5a62009-07-23 19:06:52 +02002553 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002554}
2555
2556static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2557{
2558 struct gsm_mncc *dtmf = arg;
2559 struct msgb *msg = gsm48_msgb_alloc();
2560 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2561
Harald Welte03740842009-06-10 23:11:52 +08002562 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2563
2564 /* keypad */
2565 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002566 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002567
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002568 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002569}
2570
2571static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2572{
2573 struct gsm_mncc *dtmf = arg;
2574 struct msgb *msg = gsm48_msgb_alloc();
2575 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2576
Harald Welte03740842009-06-10 23:11:52 +08002577 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2578
2579 /* cause */
2580 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002581 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002582 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002583 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002584
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002585 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002586}
2587
2588static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2589{
2590 struct msgb *msg = gsm48_msgb_alloc();
2591 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2592
Harald Welte03740842009-06-10 23:11:52 +08002593 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2594
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002595 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002596}
2597
2598static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2599{
2600 struct gsm_mncc dtmf;
2601
2602 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2603 dtmf.callref = trans->callref;
2604
Harald Welte0abe5a62009-07-23 19:06:52 +02002605 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002606}
2607
2608static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2609{
2610 struct gsm48_hdr *gh = msgb_l3(msg);
2611 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2612 struct tlv_parsed tp;
2613 struct gsm_mncc modify;
2614
2615 memset(&modify, 0, sizeof(struct gsm_mncc));
2616 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002617 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002618 /* bearer capability */
2619 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2620 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002621 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002622 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2623 }
2624
2625 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2626
Harald Welte0abe5a62009-07-23 19:06:52 +02002627 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002628}
2629
2630static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2631{
2632 struct gsm_mncc *modify = arg;
2633 struct msgb *msg = gsm48_msgb_alloc();
2634 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2635
Harald Welte03740842009-06-10 23:11:52 +08002636 gh->msg_type = GSM48_MT_CC_MODIFY;
2637
2638 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2639
2640 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002641 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002642
2643 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2644
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002645 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002646}
2647
2648static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2649{
2650 struct gsm48_hdr *gh = msgb_l3(msg);
2651 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2652 struct tlv_parsed tp;
2653 struct gsm_mncc modify;
2654
2655 gsm48_stop_cc_timer(trans);
2656
2657 memset(&modify, 0, sizeof(struct gsm_mncc));
2658 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002659 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002660 /* bearer capability */
2661 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2662 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002663 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002664 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2665 }
2666
2667 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2668
Harald Welte0abe5a62009-07-23 19:06:52 +02002669 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002670}
2671
2672static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2673{
2674 struct gsm_mncc *modify = arg;
2675 struct msgb *msg = gsm48_msgb_alloc();
2676 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2677
Harald Welte03740842009-06-10 23:11:52 +08002678 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2679
2680 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002681 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002682
2683 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2684
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002685 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002686}
2687
2688static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2689{
2690 struct gsm48_hdr *gh = msgb_l3(msg);
2691 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2692 struct tlv_parsed tp;
2693 struct gsm_mncc modify;
2694
2695 gsm48_stop_cc_timer(trans);
2696
2697 memset(&modify, 0, sizeof(struct gsm_mncc));
2698 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002699 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002700 /* bearer capability */
2701 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2702 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002703 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002704 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2705 }
2706 /* cause */
2707 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2708 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002709 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002710 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2711 }
2712
2713 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2714
Harald Welte0abe5a62009-07-23 19:06:52 +02002715 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002716}
2717
2718static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2719{
2720 struct gsm_mncc *modify = arg;
2721 struct msgb *msg = gsm48_msgb_alloc();
2722 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2723
Harald Welte03740842009-06-10 23:11:52 +08002724 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2725
2726 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002727 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002728 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002729 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002730
2731 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2732
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002733 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002734}
2735
2736static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2737{
2738 struct gsm_mncc *notify = arg;
2739 struct msgb *msg = gsm48_msgb_alloc();
2740 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2741
Harald Welte03740842009-06-10 23:11:52 +08002742 gh->msg_type = GSM48_MT_CC_NOTIFY;
2743
2744 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002745 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002746
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002747 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002748}
2749
2750static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2751{
2752 struct gsm48_hdr *gh = msgb_l3(msg);
2753 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2754// struct tlv_parsed tp;
2755 struct gsm_mncc notify;
2756
2757 memset(&notify, 0, sizeof(struct gsm_mncc));
2758 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002759// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002760 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002761 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002762
Harald Welte0abe5a62009-07-23 19:06:52 +02002763 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002764}
2765
2766static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2767{
2768 struct gsm_mncc *user = arg;
2769 struct msgb *msg = gsm48_msgb_alloc();
2770 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2771
Harald Welte03740842009-06-10 23:11:52 +08002772 gh->msg_type = GSM48_MT_CC_USER_INFO;
2773
2774 /* user-user */
2775 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002776 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002777 /* more data */
2778 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002779 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002780
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002781 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002782}
2783
2784static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2785{
2786 struct gsm48_hdr *gh = msgb_l3(msg);
2787 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2788 struct tlv_parsed tp;
2789 struct gsm_mncc user;
2790
2791 memset(&user, 0, sizeof(struct gsm_mncc));
2792 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002793 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002794 /* user-user */
2795 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2796 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002797 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002798 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2799 }
2800 /* more data */
2801 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2802 user.more = 1;
2803
Harald Welte0abe5a62009-07-23 19:06:52 +02002804 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002805}
2806
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002807static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002808{
2809 struct gsm_mncc *mode = arg;
2810
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01002811 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte03740842009-06-10 23:11:52 +08002812}
2813
2814static struct downstate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02002815 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08002816 int type;
2817 int (*rout) (struct gsm_trans *trans, void *arg);
2818} downstatelist[] = {
2819 /* mobile originating call establishment */
2820 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2821 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2822 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2823 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2824 {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 */
2825 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2826 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2827 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2828 /* mobile terminating call establishment */
2829 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2830 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2831 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2832 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2833 /* signalling during call */
2834 {SBIT(GSM_CSTATE_ACTIVE),
2835 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2836 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2837 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2838 {ALL_STATES,
2839 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2840 {ALL_STATES,
2841 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2842 {ALL_STATES,
2843 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2844 {SBIT(GSM_CSTATE_ACTIVE),
2845 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2846 {SBIT(GSM_CSTATE_ACTIVE),
2847 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2848 {SBIT(GSM_CSTATE_ACTIVE),
2849 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2850 {SBIT(GSM_CSTATE_ACTIVE),
2851 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2852 {SBIT(GSM_CSTATE_ACTIVE),
2853 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2854 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2855 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2856 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2857 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2858 {SBIT(GSM_CSTATE_ACTIVE),
2859 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2860 /* clearing */
2861 {SBIT(GSM_CSTATE_INITIATED),
2862 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2863 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2864 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2865 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2866 MNCC_REL_REQ, gsm48_cc_tx_release},
2867 /* special */
2868 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002869 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002870};
2871
2872#define DOWNSLLEN \
2873 (sizeof(downstatelist) / sizeof(struct downstate))
2874
2875
Harald Weltef4e4b8a2010-12-22 23:57:45 +01002876int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002877{
Harald Welteaa60edb2009-08-09 18:52:33 +02002878 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002879 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002880 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freyther50366f22010-12-27 16:02:25 +01002881 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002882 struct gsm_mncc *data = arg, rel;
2883
Harald Welte17b56522010-12-22 21:45:05 +01002884 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2885
Harald Welte03740842009-06-10 23:11:52 +08002886 /* handle special messages */
2887 switch(msg_type) {
2888 case MNCC_BRIDGE:
2889 return tch_bridge(net, arg);
2890 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002891 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002892 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002893 return tch_recv_mncc(net, data->callref, 1);
2894 case GSM_TCHF_FRAME:
Andreas Eversberg2c202cd2013-12-06 16:59:10 +01002895 case GSM_TCHF_FRAME_EFR:
Harald Welte3971ad52009-12-19 22:23:05 +01002896 /* Find callref */
2897 trans = trans_find_by_callref(net, data->callref);
Harald Welte17b56522010-12-22 21:45:05 +01002898 if (!trans) {
2899 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002900 return -EIO;
Harald Welte17b56522010-12-22 21:45:05 +01002901 }
2902 if (!trans->conn) {
2903 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002904 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002905 }
2906 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2907 /* This should be LOGL_ERROR or NOTICE, but
2908 * unfortuantely it happens for a couple of frames at
2909 * the beginning of every RTP connection */
2910 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002911 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002912 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002913 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002914 switch (bts->type) {
2915 case GSM_BTS_TYPE_NANOBTS:
Harald Welte35ac0e42012-07-02 19:51:55 +02002916 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte17b56522010-12-22 21:45:05 +01002917 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte45d6e4d2010-12-24 12:40:21 +01002918 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002919 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002920 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002921 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002922 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03002923 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02002924 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002925 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002926 default:
Harald Welte8099a712011-08-09 21:53:20 +02002927 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01002928 }
2929 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002930 }
2931
2932 memset(&rel, 0, sizeof(struct gsm_mncc));
2933 rel.callref = data->callref;
2934
2935 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002936 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002937
2938 /* Callref unknown */
2939 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002940 struct gsm_subscriber *subscr;
2941
Harald Welte6e1536e2009-07-04 10:11:24 +02002942 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002943 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2944 "Received '%s' from MNCC with "
2945 "unknown callref %d\n", data->called.number,
2946 get_mncc_name(msg_type), data->callref);
2947 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002948 return mncc_release_ind(net, NULL, data->callref,
2949 GSM48_CAUSE_LOC_PRN_S_LU,
2950 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002951 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002952 if (!data->called.number[0] && !data->imsi[0]) {
2953 DEBUGP(DCC, "(bts - trx - ts - ti) "
2954 "Received '%s' from MNCC with "
2955 "no number or IMSI\n", get_mncc_name(msg_type));
2956 /* Invalid number */
2957 return mncc_release_ind(net, NULL, data->callref,
2958 GSM48_CAUSE_LOC_PRN_S_LU,
2959 GSM48_CC_CAUSE_INV_NR_FORMAT);
2960 }
Harald Welte03740842009-06-10 23:11:52 +08002961 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002962 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002963 subscr = subscr_get_by_extension(net,
2964 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002965 else
Harald Welte75350412009-07-23 18:46:00 +02002966 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002967 /* If subscriber is not found */
2968 if (!subscr) {
2969 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2970 "Received '%s' from MNCC with "
2971 "unknown subscriber %s\n", data->called.number,
2972 get_mncc_name(msg_type), data->called.number);
2973 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002974 return mncc_release_ind(net, NULL, data->callref,
2975 GSM48_CAUSE_LOC_PRN_S_LU,
2976 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002977 }
2978 /* If subscriber is not "attached" */
2979 if (!subscr->lac) {
2980 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2981 "Received '%s' from MNCC with "
2982 "detached subscriber %s\n", data->called.number,
2983 get_mncc_name(msg_type), data->called.number);
2984 subscr_put(subscr);
2985 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002986 return mncc_release_ind(net, NULL, data->callref,
2987 GSM48_CAUSE_LOC_PRN_S_LU,
2988 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002989 }
2990 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002991 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2992 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002993 DEBUGP(DCC, "No memory for trans.\n");
2994 subscr_put(subscr);
2995 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002996 mncc_release_ind(net, NULL, data->callref,
2997 GSM48_CAUSE_LOC_PRN_S_LU,
2998 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002999 return -ENOMEM;
3000 }
Harald Welte03740842009-06-10 23:11:52 +08003001 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003002 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003003
Harald Welte03740842009-06-10 23:11:52 +08003004 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003005 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003006 /* find transaction with this subscriber already paging */
3007 llist_for_each_entry(transt, &net->trans_list, entry) {
3008 /* Transaction of our lchan? */
3009 if (transt == trans ||
3010 transt->subscr != subscr)
3011 continue;
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003012 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003013 "Received '%s' from MNCC with "
3014 "unallocated channel, paging already "
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003015 "started for lac %d.\n",
Harald Welte03740842009-06-10 23:11:52 +08003016 data->called.number,
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003017 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003018 subscr_put(subscr);
3019 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003020 return 0;
3021 }
3022 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02003023 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01003024
3025 /* Get a channel */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01003026 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3027 if (!trans->paging_request) {
3028 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3029 subscr_put(subscr);
3030 trans_free(trans);
3031 return 0;
3032 }
3033
3034 *trans->paging_request = subscr->net;
3035 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut892f6952010-12-01 22:17:36 +01003036
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003037 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003038 return 0;
3039 }
3040 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003041 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003042 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003043 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003044
3045 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003046 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08003047
3048 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003049 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003050 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003051 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08003052 (trans->subscr)?(trans->subscr->extension):"-",
3053 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003054 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3055 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003056 if (msg_type == MNCC_REL_REQ)
3057 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3058 else
3059 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3060 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003061 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003062 return rc;
3063 }
3064
3065 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3066 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003067 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08003068 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003069 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003070 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003071 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003072
3073 /* Find function for current state and message */
3074 for (i = 0; i < DOWNSLLEN; i++)
3075 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003076 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003077 break;
3078 if (i == DOWNSLLEN) {
3079 DEBUGP(DCC, "Message unhandled at this state.\n");
3080 return 0;
3081 }
3082
3083 rc = downstatelist[i].rout(trans, arg);
3084
3085 return rc;
3086}
3087
3088
3089static struct datastate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003090 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08003091 int type;
3092 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3093} datastatelist[] = {
3094 /* mobile originating call establishment */
3095 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3096 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3097 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3098 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3099 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3100 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3101 /* mobile terminating call establishment */
3102 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3103 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3104 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3105 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003106 {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 +08003107 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3108 /* signalling during call */
3109 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3110 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3111 {SBIT(GSM_CSTATE_ACTIVE),
3112 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3113 {ALL_STATES,
3114 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3115 {ALL_STATES,
3116 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3117 {ALL_STATES,
3118 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3119 {SBIT(GSM_CSTATE_ACTIVE),
3120 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3121 {SBIT(GSM_CSTATE_ACTIVE),
3122 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3123 {SBIT(GSM_CSTATE_ACTIVE),
3124 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3125 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3126 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3127 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3128 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3129 {SBIT(GSM_CSTATE_ACTIVE),
3130 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3131 /* clearing */
3132 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3133 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3134 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3135 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3136 {ALL_STATES, /* 5.4.3.4 */
3137 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3138};
3139
3140#define DATASLLEN \
3141 (sizeof(datastatelist) / sizeof(struct datastate))
3142
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003143static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003144{
3145 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003146 uint8_t msg_type = gh->msg_type & 0xbf;
3147 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003148 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003149 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003150
Harald Welte03740842009-06-10 23:11:52 +08003151 if (msg_type & 0x80) {
3152 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3153 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003154 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003155
Harald Welte03740842009-06-10 23:11:52 +08003156 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003157 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003158
Harald Welte4861c822009-07-23 21:21:14 +02003159 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003160 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003161 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003162 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003163 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3164 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003165
3166 /* Create transaction */
3167 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003168 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003169 "creating new trans.\n", transaction_id);
3170 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003171 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003172 transaction_id, new_callref++);
3173 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003174 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01003175 rc = gsm48_tx_simple(conn,
Harald Welte4861c822009-07-23 21:21:14 +02003176 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003177 GSM48_MT_CC_RELEASE_COMPL);
3178 return -ENOMEM;
3179 }
Harald Welte03740842009-06-10 23:11:52 +08003180 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003181 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003182 }
3183
3184 /* find function for current state and message */
3185 for (i = 0; i < DATASLLEN; i++)
3186 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003187 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003188 break;
3189 if (i == DATASLLEN) {
3190 DEBUGP(DCC, "Message unhandled at this state.\n");
3191 return 0;
3192 }
3193
Holger Hans Peter Freytherac009e52013-07-06 11:45:38 +02003194 assert(trans->subscr);
3195
Harald Welte03740842009-06-10 23:11:52 +08003196 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003197
3198 return rc;
3199}
3200
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003201/* Create a dummy to wait five seconds */
3202static void release_anchor(struct gsm_subscriber_connection *conn)
3203{
3204 if (!conn->anch_operation)
3205 return;
3206
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003207 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003208 talloc_free(conn->anch_operation);
3209 conn->anch_operation = NULL;
3210}
3211
3212static void anchor_timeout(void *_data)
3213{
3214 struct gsm_subscriber_connection *con = _data;
3215
3216 release_anchor(con);
3217 msc_release_connection(con);
3218}
3219
3220int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3221{
3222 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3223 if (!conn->anch_operation)
3224 return -1;
3225
3226 conn->anch_operation->timeout.data = conn;
3227 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003228 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003229 return 0;
3230}
3231
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003232/* here we get data from the BSC level... */
3233int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003234{
3235 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003236 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte59b04682009-06-10 05:40:52 +08003237 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003238
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003239 if (silent_call_reroute(conn, msg))
3240 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003241
3242 switch (pdisc) {
3243 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003244 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003245 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003246 break;
3247 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003248 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003249 break;
3250 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003251 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003252 break;
3253 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003254 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003255 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003256 break;
3257 case GSM48_PDISC_MM_GPRS:
3258 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003259 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3260 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003261 break;
Harald Welte03115042009-10-16 08:32:58 +02003262 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003263 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003264 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003265 break;
Harald Welte59b04682009-06-10 05:40:52 +08003266 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003267 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3268 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003269 break;
3270 }
3271
3272 return rc;
3273}
3274
Harald Welte3c062072009-07-28 18:25:29 +02003275/*
3276 * This will be ran by the linker when loading the DSO. We use it to
3277 * do system initialization, e.g. registration of signal handlers.
3278 */
3279static __attribute__((constructor)) void on_dso_load_0408(void)
3280{
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02003281 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte3c062072009-07-28 18:25:29 +02003282}