blob: dcb6e113df3b22b139b71a7a9722acc4ef72e2cb [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 Freyther9aa29b52010-08-12 01:41:57 +08005 * (C) 2008-2010 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
Sylvain Munaut28c47162009-12-24 00:27:26 +010032#include <openbsc/auth.h>
Harald Welte59b04682009-06-10 05:40:52 +080033#include <openbsc/db.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/debug.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/gsm_subscriber.h>
37#include <openbsc/gsm_04_11.h>
38#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +080039#include <openbsc/gsm_04_80.h>
Harald Welte59b04682009-06-10 05:40:52 +080040#include <openbsc/abis_rsl.h>
41#include <openbsc/chan_alloc.h>
42#include <openbsc/paging.h>
43#include <openbsc/signal.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020044#include <osmocom/abis/trau_frame.h>
Harald Welte59b04682009-06-10 05:40:52 +080045#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020046#include <openbsc/rtp_proxy.h>
Harald Weltec2189a62009-07-23 18:56:43 +020047#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020048#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010049#include <openbsc/silent_call.h>
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080050#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther3a0ec172010-06-30 12:44:07 +080051#include <openbsc/osmo_msc.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020052#include <osmocom/abis/e1_input.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010053#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther89253132010-09-30 18:52:23 +080054
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010055#include <osmocom/gsm/gsm48.h>
56#include <osmocom/gsm/gsm0480.h>
57#include <osmocom/gsm/gsm_utils.h>
58#include <osmocom/core/msgb.h>
59#include <osmocom/core/talloc.h>
60#include <osmocom/gsm/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080061
Harald Welte (local)8751ee92009-08-15 02:30:58 +020062void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010063void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020064
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020065int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +080066static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020067 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010068static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +080069static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080070
71struct gsm_lai {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020072 uint16_t mcc;
73 uint16_t mnc;
74 uint16_t lac;
Harald Welte59b04682009-06-10 05:40:52 +080075};
76
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020077static uint32_t new_callref = 0x80000001;
Harald Welte03740842009-06-10 23:11:52 +080078
Harald Welte4f272cc2011-03-03 23:29:05 +010079void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
80{
81 net->mncc_recv(net, msg);
82}
83
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080084static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
85 struct gsm_trans *trans)
86{
87 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
88
89 /* if we get passed a transaction reference, do some common
90 * work that the caller no longer has to do */
91 if (trans) {
92 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
93 msg->lchan = trans->conn->lchan;
94 }
95
96
97 if (msg->lchan) {
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +020098 struct e1inp_sign_link *sign_link =
99 msg->lchan->ts->trx->rsl_link;
100
101 msg->dst = sign_link;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800102 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
103 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200104 "Sending '%s' to MS.\n",
105 sign_link->trx->bts->nr,
106 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800107 gh->proto_discr & 0xf0,
108 gsm48_cc_msg_name(gh->msg_type));
109 else
110 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200111 "Sending 0x%02x to MS.\n",
112 sign_link->trx->bts->nr,
113 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800114 gh->proto_discr, gh->msg_type);
115 }
116
Holger Hans Peter Freyther6bfcad82010-11-10 10:16:02 +0100117 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800118}
Sylvain Munaut28c47162009-12-24 00:27:26 +0100119
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +0800120int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
121{
122 struct gsm48_hdr *gh;
123 struct msgb *ss_notify;
124
125 ss_notify = gsm0480_create_notifySS(message);
126 if (!ss_notify)
127 return -1;
128
129 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
130 uint8_t *data = msgb_push(ss_notify, 1);
131 data[0] = ss_notify->len - 1;
132 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
133 gh->msg_type = GSM48_MT_CC_FACILITY;
134 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
135}
136
Sylvain Munaut28c47162009-12-24 00:27:26 +0100137static void release_security_operation(struct gsm_subscriber_connection *conn)
138{
139 if (!conn->sec_operation)
140 return;
141
142 talloc_free(conn->sec_operation);
143 conn->sec_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800144 msc_release_connection(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100145}
146
147static void allocate_security_operation(struct gsm_subscriber_connection *conn)
148{
Sylvain Munaut28c47162009-12-24 00:27:26 +0100149 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
150 struct gsm_security_operation);
151}
152
Sylvain Munautc0bdbb42010-12-01 22:37:05 +0100153int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
154 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100155{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800156 struct gsm_network *net = conn->bts->network;
157 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100158 struct gsm_security_operation *op;
159 struct gsm_auth_tuple atuple;
160 int status = -1, rc;
161
162 /* Check if we _can_ enable encryption. Cases where we can't:
163 * - Encryption disabled in config
164 * - Channel already secured (nothing to do)
165 * - Subscriber equipment doesn't support configured encryption
166 */
167 if (!net->a5_encryption) {
168 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800169 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100170 DEBUGP(DMM, "Requesting to secure an already secure channel");
171 status = GSM_SECURITY_SUCCEEDED;
172 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
173 net->a5_encryption)) {
174 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
175 status = GSM_SECURITY_NOAVAIL;
176 }
177
178 /* If not done yet, try to get info for this user */
179 if (status < 0) {
180 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
181 if (rc <= 0)
182 status = GSM_SECURITY_NOAVAIL;
183 }
184
185 /* Are we done yet ? */
186 if (status >= 0)
187 return cb ?
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800188 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut28c47162009-12-24 00:27:26 +0100189 0;
190
191 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800192 if (conn->sec_operation)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100193 return -EBUSY;
194
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800195 allocate_security_operation(conn);
196 op = conn->sec_operation;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100197 op->cb = cb;
198 op->cb_data = cb_data;
199 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
200
201 /* FIXME: Should start a timer for completion ... */
202
203 /* Then do whatever is needed ... */
Harald Welte857e15d2010-12-23 18:07:49 +0100204 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100205 /* Start authentication */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800206 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte857e15d2010-12-23 18:07:49 +0100207 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100208 /* Start ciphering directly */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100209 return gsm0808_cipher_mode(conn, net->a5_encryption,
210 op->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100211 }
212
213 return -EINVAL; /* not reached */
214}
215
Harald Welte59b04682009-06-10 05:40:52 +0800216static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
217 struct gsm_subscriber *subscriber)
218{
219 if (!subscriber)
220 return 0;
221
222 /*
223 * Do not send accept yet as more information should arrive. Some
224 * phones will not send us the information and we will have to check
225 * what we want to do with that.
226 */
227 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
228 return 0;
229
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200230 switch (subscriber->net->auth_policy) {
231 case GSM_AUTH_POLICY_CLOSED:
232 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200233 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200234 if (subscriber->authorized)
235 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200236 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200237 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800238 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200239 default:
240 return 0;
241 }
Harald Welte59b04682009-06-10 05:40:52 +0800242}
243
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100244static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800245{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100246 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800247 return;
248
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800249 /* No need to keep the connection up */
250 release_anchor(conn);
251
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200252 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100253 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherbed07092010-12-22 08:53:28 +0100254 conn->loc_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800255 msc_release_connection(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800256}
257
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100258static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800259{
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800260 if (conn->loc_operation)
261 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100262 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800263
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100264 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200265 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800266}
267
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100268static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
269 struct msgb *msg, void *data, void *param)
270{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800271 struct gsm_subscriber_connection *conn = data;
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100272 int rc = 0;
273
274 switch (event) {
275 case GSM_SECURITY_AUTH_FAILED:
276 release_loc_updating_req(conn);
277 break;
278
279 case GSM_SECURITY_NOAVAIL:
280 case GSM_SECURITY_SUCCEEDED:
281 /* We're all good */
282 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800283 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800284 if (conn->bts->network->send_mm_info) {
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100285 /* send MM INFO with network name */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800286 rc = gsm48_tx_mm_info(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100287 }
288
289 /* call subscr_update after putting the loc_upd_acc
290 * in the transmit queue, since S_SUBSCR_ATTACHED might
291 * trigger further action like SMS delivery */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800292 subscr_update(conn->subscr, conn->bts,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100293 GSM_SUBSCRIBER_UPDATE_ATTACHED);
294
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +0100295 /*
296 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
297 * MS needs to respond with a TMSI REALLOCATION COMPLETE
298 * (even if the TMSI is the same).
299 */
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100300 break;
301
302 default:
303 rc = -EINVAL;
304 };
305
306 return rc;
307}
308
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100309static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800310{
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100311 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800312 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100313 conn->loc_operation->key_seq,
314 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800315 return 0;
316}
317
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800318void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800319{
Harald Welte03740842009-06-10 23:11:52 +0800320 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800321
322 /* avoid someone issuing a clear */
323 conn->in_release = 1;
324
Harald Welte59b04682009-06-10 05:40:52 +0800325 /*
326 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800327 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800328 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800329 release_loc_updating_req(conn);
Holger Hans Peter Freytherd91346a2010-12-28 14:57:20 +0100330
331 /* We might need to cancel the paging response or such. */
332 if (conn->sec_operation && conn->sec_operation->cb) {
333 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
334 NULL, conn, conn->sec_operation->cb_data);
335 }
336
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800337 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800338 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800339
Harald Welte03740842009-06-10 23:11:52 +0800340 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-07-23 18:56:43 +0200341 /* FIXME: this is not neccessarily the right thing to do, we should
342 * only set trans->lchan to NULL and wait for another lchan to be
343 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800344 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
345 if (trans->conn == conn)
Harald Weltec2189a62009-07-23 18:56:43 +0200346 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800347 }
Harald Welte59b04682009-06-10 05:40:52 +0800348}
349
Harald Weltee8d16172010-12-22 23:17:50 +0100350void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
351{
352 struct gsm_trans *trans, *temp;
353
354 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
355
356 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Weltebd0f33c2010-12-23 02:47:53 +0100357 if (trans->protocol == protocol) {
358 trans->callref = 0;
Harald Weltee8d16172010-12-22 23:17:50 +0100359 trans_free(trans);
Harald Weltebd0f33c2010-12-23 02:47:53 +0100360 }
Harald Weltee8d16172010-12-22 23:17:50 +0100361 }
362}
363
Harald Welte59b04682009-06-10 05:40:52 +0800364/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200365int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800366{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800367 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800368 struct msgb *msg;
369
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200370 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800371
372 msg = gsm48_create_loc_upd_rej(cause);
373 if (!msg) {
374 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
375 return -1;
376 }
Harald Welte59b04682009-06-10 05:40:52 +0800377
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800378 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800379
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100380 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100381 "LAC=%u BTS=%u\n", conn->subscr ?
382 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800383 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100384
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800385 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800386}
387
388/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200389int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800390{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800391 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800392 struct msgb *msg = gsm48_msgb_alloc();
393 struct gsm48_hdr *gh;
394 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200395 uint8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800396
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800397 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800398
399 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
400 gh->proto_discr = GSM48_PDISC_MM;
401 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
402
403 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100404 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800405 bts->network->network_code, bts->location_area_code);
406
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200407 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200408 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800409
410 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
411
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200412 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100413
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800414 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800415}
416
Harald Welte59b04682009-06-10 05:40:52 +0800417/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200418static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800419{
420 struct msgb *msg = gsm48_msgb_alloc();
421 struct gsm48_hdr *gh;
422
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800423 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800424
425 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
426 gh->proto_discr = GSM48_PDISC_MM;
427 gh->msg_type = GSM48_MT_MM_ID_REQ;
428 gh->data[0] = id_type;
429
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800430 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800431}
432
Harald Welte59b04682009-06-10 05:40:52 +0800433
434/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800435static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800436{
437 struct gsm48_hdr *gh = msgb_l3(msg);
438 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200439 struct gsm_bts *bts = lchan->ts->trx->bts;
440 struct gsm_network *net = bts->network;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200441 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200442 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800443
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200444 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800445 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
446 mi_type, mi_string);
447
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200448 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welteedf48af2009-12-13 12:39:18 +0100449
Harald Welte59b04682009-06-10 05:40:52 +0800450 switch (mi_type) {
451 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200452 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100453 if (!conn->subscr) {
454 conn->subscr = subscr_get_by_imsi(net, mi_string);
455 if (!conn->subscr)
456 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200457 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100458 if (conn->loc_operation)
459 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800460 break;
461 case GSM_MI_TYPE_IMEI:
462 case GSM_MI_TYPE_IMEISV:
463 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100464 if (conn->subscr) {
465 db_subscriber_assoc_imei(conn->subscr, mi_string);
466 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200467 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100468 if (conn->loc_operation)
469 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800470 break;
471 }
472
473 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100474 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800475}
476
477
478static void loc_upd_rej_cb(void *data)
479{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100480 struct gsm_subscriber_connection *conn = data;
481 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100482 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800483
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800484 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800485 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800486}
487
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100488static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800489{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100490 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
491 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200492 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800493}
494
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200495static const char *lupd_name(uint8_t type)
Harald Welte59b04682009-06-10 05:40:52 +0800496{
497 switch (type) {
498 case GSM48_LUPD_NORMAL:
499 return "NORMAL";
500 case GSM48_LUPD_PERIODIC:
501 return "PEROIDOC";
502 case GSM48_LUPD_IMSI_ATT:
503 return "IMSI ATTACH";
504 default:
505 return "UNKNOWN";
506 }
507}
508
Harald Welte59b04682009-06-10 05:40:52 +0800509/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800510static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800511{
512 struct gsm48_hdr *gh = msgb_l3(msg);
513 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800514 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800515 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200516 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200517 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800518 int rc;
519
520 lu = (struct gsm48_loc_upd_req *) gh->data;
521
522 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
523
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200524 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800525
Harald Welte79639662009-06-27 02:58:43 +0200526 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800527 lupd_name(lu->type));
528
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200529 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welteedf48af2009-12-13 12:39:18 +0100530
Harald Welte3edc5a92009-12-22 00:41:05 +0100531 switch (lu->type) {
532 case GSM48_LUPD_NORMAL:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200533 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100534 break;
535 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200536 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100537 break;
538 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200539 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100540 break;
541 }
542
Harald Welte59b04682009-06-10 05:40:52 +0800543 /*
544 * Pseudo Spoof detection: Just drop a second/concurrent
545 * location updating request.
546 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100547 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200548 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100549 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800550 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800551 return 0;
552 }
553
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800554 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800555
Sylvain Munaut360fc582010-06-10 13:36:59 +0200556 conn->loc_operation->key_seq = lu->key_seq;
557
Harald Welte59b04682009-06-10 05:40:52 +0800558 switch (mi_type) {
559 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200560 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800561 /* we always want the IMEI, too */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800562 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100563 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800564
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200565 /* look up subscriber based on IMSI, create if not found */
566 subscr = subscr_get_by_imsi(bts->network, mi_string);
567 if (!subscr) {
568 subscr = db_create_subscriber(bts->network, mi_string);
569 }
Harald Welte59b04682009-06-10 05:40:52 +0800570 break;
571 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200572 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800573 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200574 subscr = subscr_get_by_tmsi(bts->network,
575 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800576 if (!subscr) {
577 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800578 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100579 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800580 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200581 /* we always want the IMEI, too */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800582 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200583 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800584 break;
585 case GSM_MI_TYPE_IMEI:
586 case GSM_MI_TYPE_IMEISV:
587 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200588 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800589 break;
590 default:
Harald Welte79639662009-06-27 02:58:43 +0200591 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800592 break;
593 }
594
Harald Welteccd69362009-07-04 10:18:00 +0200595 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100596 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200597
Harald Welte03740842009-06-10 23:11:52 +0800598 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200599 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800600 /* FIXME: request id? close channel? */
601 return -EINVAL;
602 }
603
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100604 conn->subscr = subscr;
605 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800606
Harald Welteccd69362009-07-04 10:18:00 +0200607 /* check if we can let the subscriber into our network immediately
608 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100609 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800610}
611
Harald Welte03740842009-06-10 23:11:52 +0800612#if 0
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200613static uint8_t to_bcd8(uint8_t val)
Harald Welte03740842009-06-10 23:11:52 +0800614{
615 return ((val / 10) << 4) | (val % 10);
616}
617#endif
618
Harald Welte59b04682009-06-10 05:40:52 +0800619/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800620int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800621{
622 struct msgb *msg = gsm48_msgb_alloc();
623 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800624 struct gsm_network *net = conn->bts->network;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200625 uint8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200626 int name_len, name_pad;
Harald Welte03740842009-06-10 23:11:52 +0800627#if 0
628 time_t cur_t;
629 struct tm* cur_time;
630 int tz15min;
631#endif
Harald Welte59b04682009-06-10 05:40:52 +0800632
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800633 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800634
635 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
636 gh->proto_discr = GSM48_PDISC_MM;
637 gh->msg_type = GSM48_MT_MM_INFO;
638
639 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200640#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800641 name_len = strlen(net->name_long);
642 /* 10.5.3.5a */
643 ptr8 = msgb_put(msg, 3);
644 ptr8[0] = GSM48_IE_NAME_LONG;
645 ptr8[1] = name_len*2 +1;
646 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
647
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200648 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800649 for (i = 0; i < name_len; i++)
650 ptr16[i] = htons(net->name_long[i]);
651
652 /* FIXME: Use Cell Broadcast, not UCS-2, since
653 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200654#endif
655 name_len = (strlen(net->name_long)*7)/8;
656 name_pad = (8 - strlen(net->name_long)*7)%8;
657 if (name_pad > 0)
658 name_len++;
659 /* 10.5.3.5a */
660 ptr8 = msgb_put(msg, 3);
661 ptr8[0] = GSM48_IE_NAME_LONG;
662 ptr8[1] = name_len +1;
663 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
664
665 ptr8 = msgb_put(msg, name_len);
666 gsm_7bit_encode(ptr8, net->name_long);
667
Harald Welte59b04682009-06-10 05:40:52 +0800668 }
669
670 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200671#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800672 name_len = strlen(net->name_short);
673 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200674 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200675 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800676 ptr8[1] = name_len*2 + 1;
677 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
678
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200679 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Welte59b04682009-06-10 05:40:52 +0800680 for (i = 0; i < name_len; i++)
681 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200682#endif
683 name_len = (strlen(net->name_short)*7)/8;
684 name_pad = (8 - strlen(net->name_short)*7)%8;
685 if (name_pad > 0)
686 name_len++;
687 /* 10.5.3.5a */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200688 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmannc8188202009-08-13 03:42:07 +0200689 ptr8[0] = GSM48_IE_NAME_SHORT;
690 ptr8[1] = name_len +1;
691 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
692
693 ptr8 = msgb_put(msg, name_len);
694 gsm_7bit_encode(ptr8, net->name_short);
695
Harald Welte59b04682009-06-10 05:40:52 +0800696 }
697
698#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800699 /* Section 10.5.3.9 */
700 cur_t = time(NULL);
Harald Welte03740842009-06-10 23:11:52 +0800701 cur_time = gmtime(&cur_t);
Harald Welte59b04682009-06-10 05:40:52 +0800702 ptr8 = msgb_put(msg, 8);
703 ptr8[0] = GSM48_IE_NET_TIME_TZ;
704 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
705 ptr8[2] = to_bcd8(cur_time->tm_mon);
706 ptr8[3] = to_bcd8(cur_time->tm_mday);
707 ptr8[4] = to_bcd8(cur_time->tm_hour);
708 ptr8[5] = to_bcd8(cur_time->tm_min);
709 ptr8[6] = to_bcd8(cur_time->tm_sec);
710 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
711 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte03740842009-06-10 23:11:52 +0800712 ptr8[7] = to_bcd8(tz15min);
Harald Welte59b04682009-06-10 05:40:52 +0800713 if (tz15min < 0)
Harald Welte03740842009-06-10 23:11:52 +0800714 ptr8[7] |= 0x80;
Harald Welte59b04682009-06-10 05:40:52 +0800715#endif
716
Daniel Willmannc8188202009-08-13 03:42:07 +0200717 DEBUGP(DMM, "-> MM INFO\n");
718
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800719 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800720}
721
Harald Welte36970f62009-08-12 22:56:50 +0200722/* Section 9.2.2 */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200723int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200724{
725 struct msgb *msg = gsm48_msgb_alloc();
726 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200727 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200728
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200729 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200730
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800731 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200732 gh->proto_discr = GSM48_PDISC_MM;
733 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
734
Sylvain Munaut0789c592009-12-24 00:23:46 +0100735 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200736
Harald Welte36970f62009-08-12 22:56:50 +0200737 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200738 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200739 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200740
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800741 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200742}
743
744/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800745int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200746{
747 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800748 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200749}
750
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800751static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800752{
753 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800754 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte59b04682009-06-10 05:40:52 +0800755}
756
757/* 9.2.6 CM service reject */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100758static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +0800759 enum gsm48_reject_value value)
760{
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800761 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800762
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800763 msg = gsm48_create_mm_serv_rej(value);
764 if (!msg) {
765 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
766 return -1;
767 }
Harald Welte59b04682009-06-10 05:40:52 +0800768
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800769 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100770 msg->lchan = conn->lchan;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800771 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800772}
773
Sylvain Munautbadc7112009-12-24 00:28:46 +0100774static int _gsm48_rx_mm_serv_req_sec_cb(
775 unsigned int hooknum, unsigned int event,
776 struct msgb *msg, void *data, void *param)
777{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800778 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100779 int rc = 0;
780
781 switch (event) {
782 case GSM_SECURITY_AUTH_FAILED:
783 /* Nothing to do */
784 break;
785
786 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800787 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100788 break;
789
790 case GSM_SECURITY_SUCCEEDED:
791 /* nothing to do. CIPHER MODE COMMAND is
792 * implicit CM SERV ACK */
793 break;
794
795 default:
796 rc = -EINVAL;
797 };
798
799 return rc;
800}
801
Harald Welte59b04682009-06-10 05:40:52 +0800802/*
803 * Handle CM Service Requests
804 * a) Verify that the packet is long enough to contain the information
805 * we require otherwsie reject with INCORRECT_MESSAGE
806 * b) Try to parse the TMSI. If we do not have one reject
807 * c) Check that we know the subscriber with the TMSI otherwise reject
808 * with a HLR cause
809 * d) Set the subscriber on the gsm_lchan and accept
810 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800811static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800812{
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200813 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200814 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800815
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800816 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800817 struct gsm_subscriber *subscr;
818 struct gsm48_hdr *gh = msgb_l3(msg);
819 struct gsm48_service_request *req =
820 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800821 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200822 uint8_t classmark2_len = gh->data[1];
823 uint8_t *classmark2 = gh->data+2;
824 uint8_t mi_len = *(classmark2 + classmark2_len);
825 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte59b04682009-06-10 05:40:52 +0800826
827 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
828 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
829 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800830 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800831 GSM48_REJECT_INCORRECT_MESSAGE);
832 }
833
834 if (msg->data_len < req->mi_len + 6) {
835 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800836 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800837 GSM48_REJECT_INCORRECT_MESSAGE);
838 }
839
840 mi_type = mi[0] & GSM_MI_TYPE_MASK;
841 if (mi_type != GSM_MI_TYPE_TMSI) {
842 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800843 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800844 GSM48_REJECT_INCORRECT_MESSAGE);
845 }
846
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200847 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800848 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
849 req->cm_service_type, mi_type, mi_string);
850
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200851 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welteedf48af2009-12-13 12:39:18 +0100852
Harald Welte583ceaf2009-08-10 10:12:45 +0200853 if (is_siemens_bts(bts))
854 send_siemens_mrpci(msg->lchan, classmark2-1);
855
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200856 subscr = subscr_get_by_tmsi(bts->network,
857 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800858
859 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
860 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800861 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800862 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
863
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800864 if (!conn->subscr)
865 conn->subscr = subscr;
866 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100867 subscr_put(subscr); /* lchan already has a ref, don't need another one */
868 else {
Harald Welte59b04682009-06-10 05:40:52 +0800869 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
870 subscr_put(subscr);
871 }
872
Harald Weltef6845a72009-07-05 14:08:13 +0200873 subscr->equipment.classmark2_len = classmark2_len;
874 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
875 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800876
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800877 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +0100878 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800879}
880
881static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
882{
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200883 struct e1inp_sign_link *sign_link = msg->lchan->ts->trx->rsl_link;
Harald Welte75350412009-07-23 18:46:00 +0200884 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800885 struct gsm48_hdr *gh = msgb_l3(msg);
886 struct gsm48_imsi_detach_ind *idi =
887 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200888 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200889 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800890 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800891
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200892 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800893 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
894 mi_type, mi_string);
895
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200896 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100897
Harald Welte59b04682009-06-10 05:40:52 +0800898 switch (mi_type) {
899 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200900 subscr = subscr_get_by_tmsi(bts->network,
901 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800902 break;
903 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +0200904 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800905 break;
906 case GSM_MI_TYPE_IMEI:
907 case GSM_MI_TYPE_IMEISV:
908 /* no sim card... FIXME: what to do ? */
909 DEBUGPC(DMM, "unimplemented mobile identity type\n");
910 break;
911 default:
912 DEBUGPC(DMM, "unknown mobile identity type\n");
913 break;
914 }
915
916 if (subscr) {
Pablo Neira Ayuso88c9bba2011-08-17 22:43:54 +0200917 subscr_update(subscr, sign_link->trx->bts,
Harald Welte59b04682009-06-10 05:40:52 +0800918 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100919 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200920
921 subscr->equipment.classmark1 = idi->classmark1;
922 db_sync_equipment(&subscr->equipment);
923
Harald Welte59b04682009-06-10 05:40:52 +0800924 subscr_put(subscr);
925 } else
926 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
927
Harald Welte43250dd2009-12-20 09:58:40 +0100928 /* FIXME: iterate over all transactions and release them,
929 * imagine an IMSI DETACH happening during an active call! */
930
Harald Welte9ed54ff2009-12-12 21:00:48 +0100931 /* subscriber is detached: should we release lchan? */
Harald Welte59b04682009-06-10 05:40:52 +0800932 return 0;
933}
934
935static int gsm48_rx_mm_status(struct msgb *msg)
936{
937 struct gsm48_hdr *gh = msgb_l3(msg);
938
939 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
940
941 return 0;
942}
943
Sylvain Munaut28c47162009-12-24 00:27:26 +0100944/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800945static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100946{
947 struct gsm48_hdr *gh = msgb_l3(msg);
948 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800949 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100950
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200951 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200952 osmo_hexdump(ar->sres, 4));
Sylvain Munaut28c47162009-12-24 00:27:26 +0100953
954 /* Safety check */
955 if (!conn->sec_operation) {
956 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
957 return -EIO;
958 }
959
960 /* Validate SRES */
961 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100962 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100963 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200964
965 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200966 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200967
Sylvain Munaut28c47162009-12-24 00:27:26 +0100968 if (cb)
969 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800970 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100971
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100972 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100973 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100974 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100975 }
976
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200977 DEBUGPC(DMM, "OK\n");
978
Sylvain Munaut28c47162009-12-24 00:27:26 +0100979 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100980 return gsm0808_cipher_mode(conn, net->a5_encryption,
981 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100982}
983
Harald Welte59b04682009-06-10 05:40:52 +0800984/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800985static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800986{
987 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +0800988 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800989
990 switch (gh->msg_type & 0xbf) {
991 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +0200992 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800993 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800994 break;
995 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800996 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800997 break;
998 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800999 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001000 break;
1001 case GSM48_MT_MM_STATUS:
1002 rc = gsm48_rx_mm_status(msg);
1003 break;
1004 case GSM48_MT_MM_TMSI_REALL_COMPL:
1005 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001006 conn->subscr ?
1007 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +08001008 "unknown subscriber");
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +01001009 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +08001010 break;
1011 case GSM48_MT_MM_IMSI_DETACH_IND:
1012 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1013 break;
1014 case GSM48_MT_MM_CM_REEST_REQ:
1015 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1016 break;
1017 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001018 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001019 break;
1020 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001021 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08001022 gh->msg_type);
1023 break;
1024 }
1025
1026 return rc;
1027}
1028
1029/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001030static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001031{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001032 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001033 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001034 struct gsm48_pag_resp *resp;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001035 uint8_t *classmark2_lv = gh->data + 1;
1036 uint8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001037 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001038 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001039 int rc = 0;
1040
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001041 resp = (struct gsm48_pag_resp *) &gh->data[0];
1042 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1043 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001044 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1045 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001046
Harald Welte59b04682009-06-10 05:40:52 +08001047 switch (mi_type) {
1048 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001049 subscr = subscr_get_by_tmsi(bts->network,
1050 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001051 break;
1052 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001053 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001054 break;
1055 }
1056
1057 if (!subscr) {
1058 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1059 /* FIXME: request id? close channel? */
1060 return -EINVAL;
1061 }
1062 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001063 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001064
Harald Weltef6845a72009-07-05 14:08:13 +02001065 subscr->equipment.classmark2_len = *classmark2_lv;
1066 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1067 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001068
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001069 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001070 return rc;
1071}
1072
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001073static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001074{
1075 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001076 struct gsm_subscriber *subscr = conn->subscr;
Harald Welte59b04682009-06-10 05:40:52 +08001077 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001078 uint8_t cm2_len, cm3_len = 0;
1079 uint8_t *cm2, *cm3 = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001080
1081 DEBUGP(DRR, "CLASSMARK CHANGE ");
1082
1083 /* classmark 2 */
1084 cm2_len = gh->data[0];
1085 cm2 = &gh->data[1];
1086 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1087
1088 if (payload_len > cm2_len + 1) {
1089 /* we must have a classmark3 */
1090 if (gh->data[cm2_len+1] != 0x20) {
1091 DEBUGPC(DRR, "ERR CM3 TAG\n");
1092 return -EINVAL;
1093 }
1094 if (cm2_len > 3) {
1095 DEBUGPC(DRR, "CM2 too long!\n");
1096 return -EINVAL;
1097 }
1098
1099 cm3_len = gh->data[cm2_len+2];
1100 cm3 = &gh->data[cm2_len+3];
1101 if (cm3_len > 14) {
1102 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1103 return -EINVAL;
1104 }
1105 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1106 }
1107 if (subscr) {
Harald Weltef6845a72009-07-05 14:08:13 +02001108 subscr->equipment.classmark2_len = cm2_len;
1109 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Welte59b04682009-06-10 05:40:52 +08001110 if (cm3) {
Harald Weltef6845a72009-07-05 14:08:13 +02001111 subscr->equipment.classmark3_len = cm3_len;
1112 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Welte59b04682009-06-10 05:40:52 +08001113 }
Harald Weltef6845a72009-07-05 14:08:13 +02001114 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001115 }
1116
Harald Welte59b04682009-06-10 05:40:52 +08001117 return 0;
1118}
1119
1120static int gsm48_rx_rr_status(struct msgb *msg)
1121{
1122 struct gsm48_hdr *gh = msgb_l3(msg);
1123
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001124 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Welte59b04682009-06-10 05:40:52 +08001125 rr_cause_name(gh->data[0]));
1126
1127 return 0;
1128}
1129
1130static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1131{
Harald Weltef9476812009-12-15 21:36:05 +01001132 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001133
Harald Weltec20bd1d2009-11-29 19:07:28 +01001134 /* This shouldn't actually end up here, as RSL treats
1135 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1136 * directly into gsm48_parse_meas_rep */
1137 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Weltef9476812009-12-15 21:36:05 +01001138 gsm48_parse_meas_rep(meas_rep, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001139
1140 return 0;
1141}
1142
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001143static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001144{
1145 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001146 uint8_t apdu_id_flags;
1147 uint8_t apdu_len;
1148 uint8_t *apdu_data;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001149
1150 apdu_id_flags = gh->data[0];
1151 apdu_len = gh->data[1];
1152 apdu_data = gh->data+2;
1153
1154 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +02001155 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)b5103922009-08-15 23:32:44 +02001156
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001157 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001158}
1159
Sylvain Munaut28c47162009-12-24 00:27:26 +01001160/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001161static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +01001162{
Sylvain Munaut28c47162009-12-24 00:27:26 +01001163 gsm_cbfn *cb;
1164 int rc = 0;
1165
1166 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1167
1168 /* Safety check */
1169 if (!conn->sec_operation) {
1170 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1171 return -EIO;
1172 }
1173
1174 /* FIXME: check for MI (if any) */
1175
1176 /* Call back whatever was in progress (if anything) ... */
1177 cb = conn->sec_operation->cb;
1178 if (cb) {
1179 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001180 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001181 }
1182
1183 /* Complete the operation */
1184 release_security_operation(conn);
1185
1186 return rc;
1187}
1188
Harald Welte6720a432009-11-29 22:45:52 +01001189/* Chapter 9.1.16 Handover complete */
Harald Weltec6b3c8c2009-12-14 17:51:15 +01001190static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +01001191{
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +01001192 struct lchan_signal_data sig;
Harald Welte6720a432009-11-29 22:45:52 +01001193 struct gsm48_hdr *gh = msgb_l3(msg);
1194
1195 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1196 rr_cause_name(gh->data[0]));
1197
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +01001198 sig.lchan = msg->lchan;
1199 sig.mr = NULL;
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02001200 osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
Harald Welte6720a432009-11-29 22:45:52 +01001201 /* FIXME: release old channel */
1202
1203 return 0;
1204}
1205
1206/* Chapter 9.1.17 Handover Failure */
Harald Weltec6b3c8c2009-12-14 17:51:15 +01001207static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +01001208{
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +01001209 struct lchan_signal_data sig;
Harald Welte6720a432009-11-29 22:45:52 +01001210 struct gsm48_hdr *gh = msgb_l3(msg);
1211
1212 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1213 rr_cause_name(gh->data[0]));
1214
Holger Hans Peter Freyther645b3832010-12-27 13:28:20 +01001215 sig.lchan = msg->lchan;
1216 sig.mr = NULL;
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02001217 osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Harald Welte6720a432009-11-29 22:45:52 +01001218 /* FIXME: release allocated new channel */
1219
1220 return 0;
1221}
Harald Welte (local)b5103922009-08-15 23:32:44 +02001222
Harald Welte59b04682009-06-10 05:40:52 +08001223/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001224static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001225{
1226 struct gsm48_hdr *gh = msgb_l3(msg);
1227 int rc = 0;
1228
1229 switch (gh->msg_type) {
1230 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001231 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001232 break;
1233 case GSM48_MT_RR_GPRS_SUSP_REQ:
1234 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1235 break;
1236 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001237 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001238 break;
Harald Welte59b04682009-06-10 05:40:52 +08001239 case GSM48_MT_RR_STATUS:
1240 rc = gsm48_rx_rr_status(msg);
1241 break;
1242 case GSM48_MT_RR_MEAS_REP:
1243 rc = gsm48_rx_rr_meas_rep(msg);
1244 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001245 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001246 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001247 break;
Harald Welted2dd9de2009-08-30 15:37:11 +09001248 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001249 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welted2dd9de2009-08-30 15:37:11 +09001250 break;
Harald Welte323f4e02009-11-29 20:02:20 +01001251 case GSM48_MT_RR_HANDO_COMPL:
Harald Welte6720a432009-11-29 22:45:52 +01001252 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001253 break;
1254 case GSM48_MT_RR_HANDO_FAIL:
Harald Welte6720a432009-11-29 22:45:52 +01001255 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001256 break;
Harald Welte59b04682009-06-10 05:40:52 +08001257 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001258 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1259 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001260 break;
1261 }
1262
1263 return rc;
1264}
1265
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001266int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1267 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001268{
1269 struct msgb *msg = gsm48_msgb_alloc();
1270 struct gsm48_hdr *gh;
1271
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001272 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001273
1274 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1275 apdu_id, apdu_len);
1276
1277 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1278 gh->proto_discr = GSM48_PDISC_RR;
1279 gh->msg_type = GSM48_MT_RR_APP_INFO;
1280 gh->data[0] = apdu_id;
1281 gh->data[1] = apdu_len;
1282 memcpy(gh->data+2, apdu, apdu_len);
1283
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001284 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001285}
1286
Harald Welte59b04682009-06-10 05:40:52 +08001287/* Call Control */
1288
1289/* The entire call control code is written in accordance with Figure 7.10c
1290 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1291 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1292 * it for voice */
1293
Harald Welte03740842009-06-10 23:11:52 +08001294static void new_cc_state(struct gsm_trans *trans, int state)
1295{
1296 if (state > 31 || state < 0)
1297 return;
1298
1299 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001300 gsm48_cc_state_name(trans->cc.state),
1301 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001302
Harald Weltec2189a62009-07-23 18:56:43 +02001303 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001304}
1305
1306static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001307{
1308 struct msgb *msg = gsm48_msgb_alloc();
1309 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001310 uint8_t *cause, *call_state;
Harald Welte59b04682009-06-10 05:40:52 +08001311
Harald Welte59b04682009-06-10 05:40:52 +08001312 gh->msg_type = GSM48_MT_CC_STATUS;
1313
1314 cause = msgb_put(msg, 3);
1315 cause[0] = 2;
1316 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1317 cause[2] = 0x80 | 30; /* response to status inquiry */
1318
1319 call_state = msgb_put(msg, 1);
1320 call_state[0] = 0xc0 | 0x00;
1321
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001322 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001323}
1324
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001325static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001326 uint8_t pdisc, uint8_t msg_type)
Harald Welte59b04682009-06-10 05:40:52 +08001327{
1328 struct msgb *msg = gsm48_msgb_alloc();
1329 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1330
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001331 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001332
1333 gh->proto_discr = pdisc;
1334 gh->msg_type = msg_type;
1335
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001336 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001337}
1338
Harald Welte03740842009-06-10 23:11:52 +08001339static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1340{
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001341 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltec2189a62009-07-23 18:56:43 +02001342 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001343 osmo_timer_del(&trans->cc.timer);
Harald Weltec2189a62009-07-23 18:56:43 +02001344 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001345 }
1346}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001347
Harald Welte03740842009-06-10 23:11:52 +08001348static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1349 int msg_type, struct gsm_mncc *mncc)
1350{
1351 struct msgb *msg;
Harald Welte17b56522010-12-22 21:45:05 +01001352 unsigned char *data;
Harald Welte03740842009-06-10 23:11:52 +08001353
1354 if (trans)
Harald Weltec51b1922010-12-26 19:06:37 +01001355 if (trans->conn && trans->conn->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001356 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001357 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001358 trans->conn->lchan->ts->trx->bts->nr,
1359 trans->conn->lchan->ts->trx->nr,
1360 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001361 (trans->subscr)?(trans->subscr->extension):"-",
1362 get_mncc_name(msg_type));
1363 else
1364 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1365 "Sending '%s' to MNCC.\n",
1366 (trans->subscr)?(trans->subscr->extension):"-",
1367 get_mncc_name(msg_type));
1368 else
1369 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1370 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1371
1372 mncc->msg_type = msg_type;
1373
Harald Welte9cfc9352009-06-26 19:39:35 +02001374 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001375 if (!msg)
1376 return -ENOMEM;
Harald Welte17b56522010-12-22 21:45:05 +01001377
1378 data = msgb_put(msg, sizeof(struct gsm_mncc));
1379 memcpy(data, mncc, sizeof(struct gsm_mncc));
1380
Harald Weltebe0cb682010-12-22 23:43:29 +01001381 cc_tx_to_mncc(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001382
1383 return 0;
1384}
1385
1386int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001387 uint32_t callref, int location, int value)
Harald Welte03740842009-06-10 23:11:52 +08001388{
1389 struct gsm_mncc rel;
1390
Harald Weltecb0595f2009-06-12 01:54:08 +08001391 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001392 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001393 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001394 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1395}
1396
Harald Weltec2189a62009-07-23 18:56:43 +02001397/* Call Control Specific transaction release.
1398 * gets called by trans_free, DO NOT CALL YOURSELF! */
1399void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001400{
Harald Welte03740842009-06-10 23:11:52 +08001401 gsm48_stop_cc_timer(trans);
1402
1403 /* send release to L4, if callref still exists */
1404 if (trans->callref) {
1405 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001406 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001407 GSM48_CAUSE_LOC_PRN_S_LU,
1408 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001409 }
Harald Weltec2189a62009-07-23 18:56:43 +02001410 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001411 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001412 if (trans->conn)
1413 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001414}
1415
1416static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001417
Harald Welte59b04682009-06-10 05:40:52 +08001418/* call-back from paging the B-end of the connection */
1419static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001420 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001421{
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001422 int found = 0;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001423 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001424 struct gsm_network **paging_request = param, *net;
Harald Welte03740842009-06-10 23:11:52 +08001425 struct gsm_trans *transt, *tmp;
Harald Welte1ff81b52009-06-26 20:17:06 +02001426
Harald Welte59b04682009-06-10 05:40:52 +08001427 if (hooknum != GSM_HOOK_RR_PAGING)
1428 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001429
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001430 net = *paging_request;
Harald Welte03740842009-06-10 23:11:52 +08001431 if (!net) {
1432 DEBUGP(DCC, "Error Network not set!\n");
1433 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001434 }
1435
Harald Welte03740842009-06-10 23:11:52 +08001436 /* check all tranactions (without lchan) for subscriber */
1437 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001438 if (transt->paging_request != paging_request || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001439 continue;
1440 switch (event) {
1441 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001442 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001443 break;
1444 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001445 transt->subscr->extension);
1446 found = 1;
Harald Welte03740842009-06-10 23:11:52 +08001447 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001448 if (!transt->conn) {
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001449 transt->paging_request = NULL;
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001450 transt->conn = conn;
Holger Hans Peter Freytherfb68bf32010-12-29 16:28:33 +01001451 conn->put_channel = 1;
Harald Welte03740842009-06-10 23:11:52 +08001452 }
1453 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001454 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001455 break;
1456 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001457 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001458 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001459 transt->subscr->extension);
Harald Welte03740842009-06-10 23:11:52 +08001460 /* Temporarily out of order */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001461 found = 1;
Harald Welte0abe5a62009-07-23 19:06:52 +02001462 mncc_release_ind(transt->subscr->net, transt,
1463 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001464 GSM48_CAUSE_LOC_PRN_S_LU,
1465 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001466 transt->callref = 0;
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001467 transt->paging_request = NULL;
Harald Weltec2189a62009-07-23 18:56:43 +02001468 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001469 break;
1470 }
1471 }
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01001472
1473 talloc_free(paging_request);
1474
1475 /*
1476 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1477 * failure and then the subscr will be poke again. This needs a lot of fixing
1478 * in the subscriber queue code.
1479 */
1480 if (!found && conn)
1481 conn->put_channel = 1;
Harald Welte59b04682009-06-10 05:40:52 +08001482 return 0;
1483}
1484
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001485static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welte3971ad52009-12-19 22:23:05 +01001486
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001487/* handle audio path for handover */
1488static int handle_ho_signal(unsigned int subsys, unsigned int signal,
1489 void *handler_data, void *signal_data)
1490{
1491 struct rtp_socket *old_rs, *new_rs, *other_rs;
1492 struct ho_signal_data *sig = signal_data;
1493
1494 if (subsys != SS_HO || signal != S_HANDOVER_ACK)
1495 return 0;
1496
1497 if (ipacc_rtp_direct) {
1498 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1499 return 0;
1500 }
1501
1502 /* RTP Proxy mode */
1503 new_rs = sig->new_lchan->abis_ip.rtp_socket;
1504 old_rs = sig->old_lchan->abis_ip.rtp_socket;
1505
1506 if (!new_rs) {
1507 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1508 return -EIO;
1509 }
1510
1511 rsl_ipacc_mdcx_to_rtpsock(sig->new_lchan);
1512
1513 if (!old_rs) {
1514 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1515 return -EIO;
1516 }
1517
1518 /* copy rx_action and reference to other sock */
1519 new_rs->rx_action = old_rs->rx_action;
1520 new_rs->tx_action = old_rs->tx_action;
1521 new_rs->transmit = old_rs->transmit;
1522
1523 switch (sig->old_lchan->abis_ip.rtp_socket->rx_action) {
1524 case RTP_PROXY:
1525 other_rs = old_rs->proxy.other_sock;
1526 rtp_socket_proxy(new_rs, other_rs);
1527 /* delete reference to other end socket to prevent
1528 * rtp_socket_free() from removing the inverse reference */
1529 old_rs->proxy.other_sock = NULL;
1530 break;
1531 case RTP_RECV_UPSTREAM:
1532 new_rs->receive = old_rs->receive;
1533 break;
1534 case RTP_NONE:
1535 break;
1536 }
1537
1538 return 0;
1539}
1540
Harald Welte3c062072009-07-28 18:25:29 +02001541/* some other part of the code sends us a signal */
1542static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1543 void *handler_data, void *signal_data)
1544{
1545 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001546 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001547 struct gsm_network *net;
1548 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001549
1550 if (subsys != SS_ABISIP)
1551 return 0;
1552
1553 /* in case we use direct BTS-to-BTS RTP */
1554 if (ipacc_rtp_direct)
1555 return 0;
1556
Harald Welte3c062072009-07-28 18:25:29 +02001557 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001558 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001559 /* in case we don't use direct BTS-to-BTS RTP */
1560 /* the BTS has successfully bound a TCH to a local ip/port,
1561 * which means we can connect our UDP socket to it */
1562 if (lchan->abis_ip.rtp_socket) {
1563 rtp_socket_free(lchan->abis_ip.rtp_socket);
1564 lchan->abis_ip.rtp_socket = NULL;
1565 }
1566
1567 lchan->abis_ip.rtp_socket = rtp_socket_create();
1568 if (!lchan->abis_ip.rtp_socket)
1569 return -EIO;
1570
1571 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1572 lchan->abis_ip.bound_ip,
1573 lchan->abis_ip.bound_port);
1574 if (rc < 0)
1575 return -EIO;
1576
Harald Welte3971ad52009-12-19 22:23:05 +01001577 /* check if any transactions on this lchan still have
1578 * a tch_recv_mncc request pending */
1579 net = lchan->ts->trx->bts->network;
1580 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001581 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001582 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1583 tch_recv_mncc(net, trans->callref, 1);
1584 }
1585 }
Harald Welte3c062072009-07-28 18:25:29 +02001586 break;
Holger Hans Peter Freyther02fca862011-01-06 13:31:41 +01001587 case S_ABISIP_DLCX_IND:
1588 /* the BTS tells us a RTP stream has been disconnected */
1589 if (lchan->abis_ip.rtp_socket) {
1590 rtp_socket_free(lchan->abis_ip.rtp_socket);
1591 lchan->abis_ip.rtp_socket = NULL;
1592 }
1593
1594 break;
Harald Welte3c062072009-07-28 18:25:29 +02001595 }
1596
1597 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001598}
1599
Harald Welte59b04682009-06-10 05:40:52 +08001600/* map two ipaccess RTP streams onto each other */
1601static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1602{
1603 struct gsm_bts *bts = lchan->ts->trx->bts;
1604 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001605 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001606
1607 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1608 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1609 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1610
1611 if (bts->type != remote_bts->type) {
Harald Welte8099a712011-08-09 21:53:20 +02001612 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte59b04682009-06-10 05:40:52 +08001613 return -EINVAL;
1614 }
Harald Welte3971ad52009-12-19 22:23:05 +01001615
1616 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001617 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001618 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001619 if (!ipacc_rtp_direct) {
1620 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001621 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001622 if (rc < 0)
1623 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001624 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte30f93fb2011-02-19 16:48:17 +01001625 if (rc < 0)
1626 return rc;
Harald Welte3c062072009-07-28 18:25:29 +02001627 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301628 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1629 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001630 } else {
1631 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301632 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1633 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301634 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001635 if (rc < 0)
1636 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301637 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1638 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301639 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001640 }
Harald Welte59b04682009-06-10 05:40:52 +08001641 break;
1642 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001643 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar49c843e2011-07-28 00:01:50 +02001644 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte59b04682009-06-10 05:40:52 +08001645 trau_mux_map_lchan(lchan, remote_lchan);
1646 break;
1647 default:
Harald Welte8099a712011-08-09 21:53:20 +02001648 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001649 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001650 }
1651
1652 return 0;
1653}
1654
Harald Welte03740842009-06-10 23:11:52 +08001655/* bridge channels of two transactions */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001656static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001657{
Harald Weltec2189a62009-07-23 18:56:43 +02001658 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1659 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001660
Harald Welte03740842009-06-10 23:11:52 +08001661 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001662 return -EIO;
1663
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001664 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001665 return -EIO;
1666
1667 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001668 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001669}
1670
Harald Welte3971ad52009-12-19 22:23:05 +01001671/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001672static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001673{
1674 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001675 struct gsm_lchan *lchan;
1676 struct gsm_bts *bts;
1677 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001678
Harald Welte03740842009-06-10 23:11:52 +08001679 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001680 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001681 if (!trans)
1682 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001683 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001684 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001685 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001686 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001687
Harald Welte3971ad52009-12-19 22:23:05 +01001688 switch (bts->type) {
1689 case GSM_BTS_TYPE_NANOBTS:
1690 if (ipacc_rtp_direct) {
Harald Welte8099a712011-08-09 21:53:20 +02001691 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welte3971ad52009-12-19 22:23:05 +01001692 return -EINVAL;
1693 }
1694 /* in case, we don't have a RTP socket yet, we note this
1695 * in the transaction and try later */
1696 if (!lchan->abis_ip.rtp_socket) {
1697 trans->tch_recv = enable;
1698 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1699 return 0;
1700 }
1701 if (enable) {
1702 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001703 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001704 if (rc < 0)
1705 return rc;
1706 /* assign socket to application interface */
1707 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1708 net, callref);
1709 } else
1710 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1711 net, 0);
1712 break;
1713 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03001714 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02001715 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte3971ad52009-12-19 22:23:05 +01001716 if (enable)
1717 return trau_recv_lchan(lchan, callref);
1718 return trau_mux_unmap(NULL, callref);
1719 break;
1720 default:
Harald Welte8099a712011-08-09 21:53:20 +02001721 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001722 return -EINVAL;
1723 }
1724
1725 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001726}
1727
Harald Welte03740842009-06-10 23:11:52 +08001728static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1729{
1730 DEBUGP(DCC, "-> STATUS ENQ\n");
1731 return gsm48_cc_tx_status(trans, msg);
1732}
1733
1734static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1735static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1736
1737static void gsm48_cc_timeout(void *arg)
1738{
1739 struct gsm_trans *trans = arg;
1740 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001741 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1742 int mo_location = GSM48_CAUSE_LOC_USER;
1743 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1744 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001745 struct gsm_mncc mo_rel, l4_rel;
1746
1747 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1748 mo_rel.callref = trans->callref;
1749 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1750 l4_rel.callref = trans->callref;
1751
Harald Weltec2189a62009-07-23 18:56:43 +02001752 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001753 case 0x303:
1754 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001755 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001756 break;
1757 case 0x310:
1758 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001759 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001760 break;
1761 case 0x313:
1762 disconnect = 1;
1763 /* unknown, did not find it in the specs */
1764 break;
1765 case 0x301:
1766 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001767 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001768 break;
1769 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001770 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001771 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001772 gsm48_cc_tx_release(trans, &trans->cc.msg);
1773 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001774 break; /* stay in release state */
1775 }
Harald Weltec2189a62009-07-23 18:56:43 +02001776 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001777 return;
1778// release = 1;
1779// l4_cause = 14;
1780// break;
1781 case 0x306:
1782 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001783 mo_cause = trans->cc.msg.cause.value;
1784 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001785 break;
1786 case 0x323:
1787 disconnect = 1;
1788 break;
1789 default:
1790 release = 1;
1791 }
1792
1793 if (release && trans->callref) {
1794 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001795 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001796 l4_location, l4_cause);
1797 trans->callref = 0;
1798 }
1799
1800 if (disconnect && trans->callref) {
1801 /* process disconnect towards layer 4 */
1802 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001803 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001804 }
1805
1806 /* process disconnect towards mobile station */
1807 if (disconnect || release) {
1808 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001809 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1810 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1811 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001812 mo_rel.cause.diag_len = 3;
1813
1814 if (disconnect)
1815 gsm48_cc_tx_disconnect(trans, &mo_rel);
1816 if (release)
1817 gsm48_cc_tx_release(trans, &mo_rel);
1818 }
1819
1820}
1821
1822static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1823 int sec, int micro)
1824{
1825 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001826 trans->cc.timer.cb = gsm48_cc_timeout;
1827 trans->cc.timer.data = trans;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02001828 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltec2189a62009-07-23 18:56:43 +02001829 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001830}
1831
1832static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1833{
1834 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02001835 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte03740842009-06-10 23:11:52 +08001836 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1837 struct tlv_parsed tp;
1838 struct gsm_mncc setup;
1839
1840 memset(&setup, 0, sizeof(struct gsm_mncc));
1841 setup.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001842 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001843 /* emergency setup is identified by msg_type */
1844 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1845 setup.emergency = 1;
1846
1847 /* use subscriber as calling party number */
1848 if (trans->subscr) {
1849 setup.fields |= MNCC_F_CALLING;
1850 strncpy(setup.calling.number, trans->subscr->extension,
1851 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001852 strncpy(setup.imsi, trans->subscr->imsi,
1853 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001854 }
1855 /* bearer capability */
1856 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1857 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001858 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001859 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1860 }
1861 /* facility */
1862 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1863 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001864 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001865 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1866 }
1867 /* called party bcd number */
1868 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1869 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001870 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001871 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1872 }
1873 /* user-user */
1874 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1875 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001876 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001877 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1878 }
1879 /* ss-version */
1880 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1881 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001882 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001883 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1884 }
1885 /* CLIR suppression */
1886 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1887 setup.clir.sup = 1;
1888 /* CLIR invocation */
1889 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1890 setup.clir.inv = 1;
1891 /* cc cap */
1892 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1893 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001894 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001895 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1896 }
1897
Harald Welte03740842009-06-10 23:11:52 +08001898 new_cc_state(trans, GSM_CSTATE_INITIATED);
1899
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001900 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1901 subscr_name(trans->subscr), trans->subscr->extension,
1902 setup.called.number);
1903
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001904 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001905
Harald Welte03740842009-06-10 23:11:52 +08001906 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001907 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001908
Harald Welteca745e22009-07-29 12:10:35 +02001909 /* MNCC code will modify the channel asynchronously, we should
1910 * ipaccess-bind only after the modification has been made to the
1911 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001912 return 0;
1913}
1914
1915static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001916{
1917 struct msgb *msg = gsm48_msgb_alloc();
1918 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001919 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001920 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001921
1922 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1923
Harald Welte03740842009-06-10 23:11:52 +08001924 /* transaction id must not be assigned */
1925 if (trans->transaction_id != 0xff) { /* unasssigned */
1926 DEBUGP(DCC, "TX Setup with assigned transaction. "
1927 "This is not allowed!\n");
1928 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001929 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001930 GSM48_CAUSE_LOC_PRN_S_LU,
1931 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001932 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001933 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001934 return rc;
1935 }
1936
1937 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001938 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1939 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001940 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001941 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001942 GSM48_CAUSE_LOC_PRN_S_LU,
1943 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001944 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001945 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001946 return rc;
1947 }
Harald Welte165fc332009-07-23 21:36:44 +02001948 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001949
Harald Welte59b04682009-06-10 05:40:52 +08001950 gh->msg_type = GSM48_MT_CC_SETUP;
1951
Harald Welte03740842009-06-10 23:11:52 +08001952 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001953
Harald Welte03740842009-06-10 23:11:52 +08001954 /* bearer capability */
1955 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001956 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001957 /* facility */
1958 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001959 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001960 /* progress */
1961 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001962 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001963 /* calling party BCD number */
1964 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001965 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001966 /* called party BCD number */
1967 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001968 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001969 /* user-user */
1970 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001971 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001972 /* redirecting party BCD number */
1973 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001974 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001975 /* signal */
1976 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001977 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001978
1979 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001980
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02001981 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltec56006b2010-12-24 16:06:33 +01001982
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001983 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001984}
1985
Harald Welte03740842009-06-10 23:11:52 +08001986static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1987{
1988 struct gsm48_hdr *gh = msgb_l3(msg);
1989 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1990 struct tlv_parsed tp;
1991 struct gsm_mncc call_conf;
1992
1993 gsm48_stop_cc_timer(trans);
1994 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1995
1996 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1997 call_conf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001998 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001999#if 0
2000 /* repeat */
2001 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2002 call_conf.repeat = 1;
2003 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2004 call_conf.repeat = 2;
2005#endif
2006 /* bearer capability */
2007 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2008 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002009 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002010 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2011 }
2012 /* cause */
2013 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2014 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002015 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08002016 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2017 }
2018 /* cc cap */
2019 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2020 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002021 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08002022 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2023 }
2024
2025 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2026
Harald Welte0abe5a62009-07-23 19:06:52 +02002027 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2028 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08002029}
2030
2031static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2032{
2033 struct gsm_mncc *proceeding = arg;
2034 struct msgb *msg = gsm48_msgb_alloc();
2035 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2036
Harald Welte03740842009-06-10 23:11:52 +08002037 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2038
2039 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2040
2041 /* bearer capability */
2042 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01002043 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002044 /* facility */
2045 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002046 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08002047 /* progress */
2048 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002049 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08002050
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002051 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002052}
2053
2054static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2055{
2056 struct gsm48_hdr *gh = msgb_l3(msg);
2057 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2058 struct tlv_parsed tp;
2059 struct gsm_mncc alerting;
2060
2061 gsm48_stop_cc_timer(trans);
2062 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2063
2064 memset(&alerting, 0, sizeof(struct gsm_mncc));
2065 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002066 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002067 /* facility */
2068 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2069 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002070 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08002071 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2072 }
2073
2074 /* progress */
2075 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2076 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01002077 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08002078 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2079 }
2080 /* ss-version */
2081 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2082 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002083 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002084 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2085 }
2086
2087 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2088
Harald Welte0abe5a62009-07-23 19:06:52 +02002089 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2090 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08002091}
2092
2093static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2094{
2095 struct gsm_mncc *alerting = arg;
2096 struct msgb *msg = gsm48_msgb_alloc();
2097 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2098
Harald Welte03740842009-06-10 23:11:52 +08002099 gh->msg_type = GSM48_MT_CC_ALERTING;
2100
2101 /* facility */
2102 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002103 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08002104 /* progress */
2105 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002106 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08002107 /* user-user */
2108 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002109 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002110
2111 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2112
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002113 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002114}
2115
2116static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2117{
2118 struct gsm_mncc *progress = arg;
2119 struct msgb *msg = gsm48_msgb_alloc();
2120 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2121
Harald Welte03740842009-06-10 23:11:52 +08002122 gh->msg_type = GSM48_MT_CC_PROGRESS;
2123
2124 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01002125 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08002126 /* user-user */
2127 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002128 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002129
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002130 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002131}
2132
2133static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2134{
2135 struct gsm_mncc *connect = arg;
2136 struct msgb *msg = gsm48_msgb_alloc();
2137 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2138
Harald Welte03740842009-06-10 23:11:52 +08002139 gh->msg_type = GSM48_MT_CC_CONNECT;
2140
2141 gsm48_stop_cc_timer(trans);
2142 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2143
2144 /* facility */
2145 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002146 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002147 /* progress */
2148 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002149 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002150 /* connected number */
2151 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002152 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002153 /* user-user */
2154 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002155 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002156
2157 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2158
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002159 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002160}
2161
2162static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2163{
2164 struct gsm48_hdr *gh = msgb_l3(msg);
2165 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2166 struct tlv_parsed tp;
2167 struct gsm_mncc connect;
2168
2169 gsm48_stop_cc_timer(trans);
2170
2171 memset(&connect, 0, sizeof(struct gsm_mncc));
2172 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002173 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002174 /* use subscriber as connected party number */
2175 if (trans->subscr) {
2176 connect.fields |= MNCC_F_CONNECTED;
2177 strncpy(connect.connected.number, trans->subscr->extension,
2178 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002179 strncpy(connect.imsi, trans->subscr->imsi,
2180 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002181 }
2182 /* facility */
2183 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2184 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002185 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002186 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2187 }
2188 /* user-user */
2189 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2190 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002191 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002192 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2193 }
2194 /* ss-version */
2195 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2196 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002197 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002198 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2199 }
2200
2201 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002202 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002203
Harald Welte0abe5a62009-07-23 19:06:52 +02002204 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002205}
2206
2207
2208static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2209{
2210 struct gsm_mncc connect_ack;
2211
2212 gsm48_stop_cc_timer(trans);
2213
2214 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +02002215 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002216
2217 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2218 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002219
Harald Welte0abe5a62009-07-23 19:06:52 +02002220 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002221 &connect_ack);
2222}
2223
2224static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2225{
2226 struct msgb *msg = gsm48_msgb_alloc();
2227 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2228
Harald Welte03740842009-06-10 23:11:52 +08002229 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2230
2231 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2232
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002233 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002234}
2235
2236static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2237{
2238 struct gsm48_hdr *gh = msgb_l3(msg);
2239 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2240 struct tlv_parsed tp;
2241 struct gsm_mncc disc;
2242
2243 gsm48_stop_cc_timer(trans);
2244
2245 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2246
2247 memset(&disc, 0, sizeof(struct gsm_mncc));
2248 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002249 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002250 /* cause */
2251 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2252 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002253 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002254 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2255 }
2256 /* facility */
2257 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2258 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002259 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002260 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2261 }
2262 /* user-user */
2263 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2264 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002265 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002266 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2267 }
2268 /* ss-version */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2270 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002271 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002272 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2273 }
2274
Harald Welte0abe5a62009-07-23 19:06:52 +02002275 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002276
2277}
2278
Harald Weltebbc636a2009-06-11 14:23:20 +08002279static struct gsm_mncc_cause default_cause = {
2280 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2281 .coding = 0,
2282 .rec = 0,
2283 .rec_val = 0,
2284 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2285 .diag_len = 0,
2286 .diag = { 0 },
2287};
Harald Welte03740842009-06-10 23:11:52 +08002288
2289static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2290{
2291 struct gsm_mncc *disc = arg;
2292 struct msgb *msg = gsm48_msgb_alloc();
2293 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2294
Harald Welte03740842009-06-10 23:11:52 +08002295 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2296
2297 gsm48_stop_cc_timer(trans);
2298 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2299
2300 /* cause */
2301 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002302 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002303 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002304 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002305
2306 /* facility */
2307 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002308 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002309 /* progress */
2310 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002311 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002312 /* user-user */
2313 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002314 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002315
2316 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002317 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002318
2319 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2320
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002321 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002322}
2323
2324static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2325{
2326 struct gsm48_hdr *gh = msgb_l3(msg);
2327 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2328 struct tlv_parsed tp;
2329 struct gsm_mncc rel;
2330 int rc;
2331
2332 gsm48_stop_cc_timer(trans);
2333
2334 memset(&rel, 0, sizeof(struct gsm_mncc));
2335 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002336 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002337 /* cause */
2338 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2339 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002340 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002341 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2342 }
2343 /* facility */
2344 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2345 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002346 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002347 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2348 }
2349 /* user-user */
2350 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2351 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002352 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002353 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2354 }
2355 /* ss-version */
2356 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2357 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002358 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002359 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2360 }
2361
Harald Weltec2189a62009-07-23 18:56:43 +02002362 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002363 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002364 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002365 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002366 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002367 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002368 GSM48_MT_CC_RELEASE_COMPL);
2369 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002370 }
2371
2372 new_cc_state(trans, GSM_CSTATE_NULL);
2373
2374 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002375 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002376
2377 return rc;
2378}
2379
2380static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2381{
2382 struct gsm_mncc *rel = arg;
2383 struct msgb *msg = gsm48_msgb_alloc();
2384 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2385
Harald Welte03740842009-06-10 23:11:52 +08002386 gh->msg_type = GSM48_MT_CC_RELEASE;
2387
2388 trans->callref = 0;
2389
2390 gsm48_stop_cc_timer(trans);
2391 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2392
2393 /* cause */
2394 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002395 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002396 /* facility */
2397 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002398 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002399 /* user-user */
2400 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002401 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002402
Harald Weltec2189a62009-07-23 18:56:43 +02002403 trans->cc.T308_second = 0;
2404 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002405
Harald Weltec2189a62009-07-23 18:56:43 +02002406 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002407 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2408
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002409 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002410}
2411
2412static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2413{
2414 struct gsm48_hdr *gh = msgb_l3(msg);
2415 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2416 struct tlv_parsed tp;
2417 struct gsm_mncc rel;
2418 int rc = 0;
2419
2420 gsm48_stop_cc_timer(trans);
2421
2422 memset(&rel, 0, sizeof(struct gsm_mncc));
2423 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002424 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002425 /* cause */
2426 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2427 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002428 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002429 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2430 }
2431 /* facility */
2432 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2433 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002434 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002435 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2436 }
2437 /* user-user */
2438 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2439 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002440 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002441 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2442 }
2443 /* ss-version */
2444 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2445 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002446 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002447 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2448 }
2449
2450 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002451 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002452 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002453 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002454 MNCC_REJ_IND, &rel);
2455 break;
2456 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002457 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002458 MNCC_REL_CNF, &rel);
2459 break;
2460 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002461 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002462 MNCC_REL_IND, &rel);
2463 }
2464 }
2465
2466 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002467 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002468
2469 return rc;
2470}
2471
2472static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2473{
2474 struct gsm_mncc *rel = arg;
2475 struct msgb *msg = gsm48_msgb_alloc();
2476 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002477 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002478
Harald Welte03740842009-06-10 23:11:52 +08002479 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2480
2481 trans->callref = 0;
2482
2483 gsm48_stop_cc_timer(trans);
2484
2485 /* cause */
2486 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002487 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002488 /* facility */
2489 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002490 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002491 /* user-user */
2492 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002493 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002494
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002495 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2496
Harald Weltec2189a62009-07-23 18:56:43 +02002497 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002498
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002499 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002500}
2501
2502static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2503{
2504 struct gsm48_hdr *gh = msgb_l3(msg);
2505 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2506 struct tlv_parsed tp;
2507 struct gsm_mncc fac;
2508
2509 memset(&fac, 0, sizeof(struct gsm_mncc));
2510 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002511 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002512 /* facility */
2513 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2514 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002515 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002516 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2517 }
2518 /* ss-version */
2519 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2520 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002521 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002522 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2523 }
2524
Harald Welte0abe5a62009-07-23 19:06:52 +02002525 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002526}
2527
2528static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2529{
2530 struct gsm_mncc *fac = arg;
2531 struct msgb *msg = gsm48_msgb_alloc();
2532 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2533
Harald Welte03740842009-06-10 23:11:52 +08002534 gh->msg_type = GSM48_MT_CC_FACILITY;
2535
2536 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002537 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002538
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002539 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002540}
2541
2542static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2543{
2544 struct gsm_mncc hold;
2545
2546 memset(&hold, 0, sizeof(struct gsm_mncc));
2547 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002548 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002549}
2550
2551static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2552{
2553 struct msgb *msg = gsm48_msgb_alloc();
2554 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2555
Harald Welte03740842009-06-10 23:11:52 +08002556 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2557
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002558 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002559}
2560
2561static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2562{
2563 struct gsm_mncc *hold_rej = arg;
2564 struct msgb *msg = gsm48_msgb_alloc();
2565 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2566
Harald Welte03740842009-06-10 23:11:52 +08002567 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2568
2569 /* cause */
2570 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002571 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002572 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002573 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002574
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002575 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002576}
2577
2578static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2579{
2580 struct gsm_mncc retrieve;
2581
2582 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2583 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002584 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2585 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002586}
2587
2588static int gsm48_cc_tx_retrieve_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_RETR_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_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2599{
2600 struct gsm_mncc *retrieve_rej = arg;
2601 struct msgb *msg = gsm48_msgb_alloc();
2602 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2603
Harald Welte03740842009-06-10 23:11:52 +08002604 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2605
2606 /* cause */
2607 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002608 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002609 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002610 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002611
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002612 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002613}
2614
2615static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2616{
2617 struct gsm48_hdr *gh = msgb_l3(msg);
2618 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2619 struct tlv_parsed tp;
2620 struct gsm_mncc dtmf;
2621
2622 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2623 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002624 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002625 /* keypad facility */
2626 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2627 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002628 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002629 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2630 }
2631
Harald Welte0abe5a62009-07-23 19:06:52 +02002632 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002633}
2634
2635static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2636{
2637 struct gsm_mncc *dtmf = arg;
2638 struct msgb *msg = gsm48_msgb_alloc();
2639 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2640
Harald Welte03740842009-06-10 23:11:52 +08002641 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2642
2643 /* keypad */
2644 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002645 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002646
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002647 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002648}
2649
2650static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2651{
2652 struct gsm_mncc *dtmf = arg;
2653 struct msgb *msg = gsm48_msgb_alloc();
2654 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2655
Harald Welte03740842009-06-10 23:11:52 +08002656 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2657
2658 /* cause */
2659 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002660 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002661 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002662 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002663
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002664 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002665}
2666
2667static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2668{
2669 struct msgb *msg = gsm48_msgb_alloc();
2670 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2671
Harald Welte03740842009-06-10 23:11:52 +08002672 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2673
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002674 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002675}
2676
2677static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2678{
2679 struct gsm_mncc dtmf;
2680
2681 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2682 dtmf.callref = trans->callref;
2683
Harald Welte0abe5a62009-07-23 19:06:52 +02002684 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002685}
2686
2687static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2688{
2689 struct gsm48_hdr *gh = msgb_l3(msg);
2690 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2691 struct tlv_parsed tp;
2692 struct gsm_mncc modify;
2693
2694 memset(&modify, 0, sizeof(struct gsm_mncc));
2695 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002696 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002697 /* bearer capability */
2698 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2699 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002700 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002701 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2702 }
2703
2704 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2705
Harald Welte0abe5a62009-07-23 19:06:52 +02002706 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002707}
2708
2709static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2710{
2711 struct gsm_mncc *modify = arg;
2712 struct msgb *msg = gsm48_msgb_alloc();
2713 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2714
Harald Welte03740842009-06-10 23:11:52 +08002715 gh->msg_type = GSM48_MT_CC_MODIFY;
2716
2717 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2718
2719 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002720 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002721
2722 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2723
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002724 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002725}
2726
2727static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2728{
2729 struct gsm48_hdr *gh = msgb_l3(msg);
2730 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2731 struct tlv_parsed tp;
2732 struct gsm_mncc modify;
2733
2734 gsm48_stop_cc_timer(trans);
2735
2736 memset(&modify, 0, sizeof(struct gsm_mncc));
2737 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002738 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002739 /* bearer capability */
2740 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2741 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002742 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002743 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2744 }
2745
2746 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2747
Harald Welte0abe5a62009-07-23 19:06:52 +02002748 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002749}
2750
2751static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2752{
2753 struct gsm_mncc *modify = arg;
2754 struct msgb *msg = gsm48_msgb_alloc();
2755 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2756
Harald Welte03740842009-06-10 23:11:52 +08002757 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2758
2759 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002760 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002761
2762 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2763
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002764 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002765}
2766
2767static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2768{
2769 struct gsm48_hdr *gh = msgb_l3(msg);
2770 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2771 struct tlv_parsed tp;
2772 struct gsm_mncc modify;
2773
2774 gsm48_stop_cc_timer(trans);
2775
2776 memset(&modify, 0, sizeof(struct gsm_mncc));
2777 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002778 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002779 /* bearer capability */
2780 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2781 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002782 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002783 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2784 }
2785 /* cause */
2786 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2787 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002788 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002789 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2790 }
2791
2792 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2793
Harald Welte0abe5a62009-07-23 19:06:52 +02002794 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002795}
2796
2797static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2798{
2799 struct gsm_mncc *modify = arg;
2800 struct msgb *msg = gsm48_msgb_alloc();
2801 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2802
Harald Welte03740842009-06-10 23:11:52 +08002803 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2804
2805 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002806 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002807 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002808 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002809
2810 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2811
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002812 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002813}
2814
2815static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2816{
2817 struct gsm_mncc *notify = arg;
2818 struct msgb *msg = gsm48_msgb_alloc();
2819 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2820
Harald Welte03740842009-06-10 23:11:52 +08002821 gh->msg_type = GSM48_MT_CC_NOTIFY;
2822
2823 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002824 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002825
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002826 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002827}
2828
2829static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2830{
2831 struct gsm48_hdr *gh = msgb_l3(msg);
2832 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2833// struct tlv_parsed tp;
2834 struct gsm_mncc notify;
2835
2836 memset(&notify, 0, sizeof(struct gsm_mncc));
2837 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002838// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002839 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002840 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002841
Harald Welte0abe5a62009-07-23 19:06:52 +02002842 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002843}
2844
2845static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2846{
2847 struct gsm_mncc *user = arg;
2848 struct msgb *msg = gsm48_msgb_alloc();
2849 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2850
Harald Welte03740842009-06-10 23:11:52 +08002851 gh->msg_type = GSM48_MT_CC_USER_INFO;
2852
2853 /* user-user */
2854 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002855 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002856 /* more data */
2857 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002858 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002859
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002860 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002861}
2862
2863static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2864{
2865 struct gsm48_hdr *gh = msgb_l3(msg);
2866 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2867 struct tlv_parsed tp;
2868 struct gsm_mncc user;
2869
2870 memset(&user, 0, sizeof(struct gsm_mncc));
2871 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002872 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002873 /* user-user */
2874 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2875 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002876 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002877 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2878 }
2879 /* more data */
2880 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2881 user.more = 1;
2882
Harald Welte0abe5a62009-07-23 19:06:52 +02002883 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002884}
2885
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002886static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002887{
2888 struct gsm_mncc *mode = arg;
2889
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01002890 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte03740842009-06-10 23:11:52 +08002891}
2892
2893static struct downstate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02002894 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08002895 int type;
2896 int (*rout) (struct gsm_trans *trans, void *arg);
2897} downstatelist[] = {
2898 /* mobile originating call establishment */
2899 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2900 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2901 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2902 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2903 {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 */
2904 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2905 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2906 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2907 /* mobile terminating call establishment */
2908 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2909 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2910 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2911 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2912 /* signalling during call */
2913 {SBIT(GSM_CSTATE_ACTIVE),
2914 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2915 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2916 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2917 {ALL_STATES,
2918 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2919 {ALL_STATES,
2920 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2921 {ALL_STATES,
2922 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2923 {SBIT(GSM_CSTATE_ACTIVE),
2924 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2925 {SBIT(GSM_CSTATE_ACTIVE),
2926 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2927 {SBIT(GSM_CSTATE_ACTIVE),
2928 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2929 {SBIT(GSM_CSTATE_ACTIVE),
2930 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2931 {SBIT(GSM_CSTATE_ACTIVE),
2932 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2933 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2934 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2935 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2936 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2937 {SBIT(GSM_CSTATE_ACTIVE),
2938 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2939 /* clearing */
2940 {SBIT(GSM_CSTATE_INITIATED),
2941 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2942 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2943 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2944 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2945 MNCC_REL_REQ, gsm48_cc_tx_release},
2946 /* special */
2947 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002948 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002949};
2950
2951#define DOWNSLLEN \
2952 (sizeof(downstatelist) / sizeof(struct downstate))
2953
2954
Harald Weltef4e4b8a2010-12-22 23:57:45 +01002955int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002956{
Harald Welteaa60edb2009-08-09 18:52:33 +02002957 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002958 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002959 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freyther50366f22010-12-27 16:02:25 +01002960 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002961 struct gsm_mncc *data = arg, rel;
2962
Harald Welte17b56522010-12-22 21:45:05 +01002963 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2964
Harald Welte03740842009-06-10 23:11:52 +08002965 /* handle special messages */
2966 switch(msg_type) {
2967 case MNCC_BRIDGE:
2968 return tch_bridge(net, arg);
2969 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002970 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002971 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002972 return tch_recv_mncc(net, data->callref, 1);
2973 case GSM_TCHF_FRAME:
2974 /* Find callref */
2975 trans = trans_find_by_callref(net, data->callref);
Harald Welte17b56522010-12-22 21:45:05 +01002976 if (!trans) {
2977 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002978 return -EIO;
Harald Welte17b56522010-12-22 21:45:05 +01002979 }
2980 if (!trans->conn) {
2981 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002982 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002983 }
2984 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2985 /* This should be LOGL_ERROR or NOTICE, but
2986 * unfortuantely it happens for a couple of frames at
2987 * the beginning of every RTP connection */
2988 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002989 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002990 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002991 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002992 switch (bts->type) {
2993 case GSM_BTS_TYPE_NANOBTS:
Harald Welte17b56522010-12-22 21:45:05 +01002994 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte45d6e4d2010-12-24 12:40:21 +01002995 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welte3971ad52009-12-19 22:23:05 +01002996 return 0;
Harald Welte17b56522010-12-22 21:45:05 +01002997 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002998 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002999 case GSM_BTS_TYPE_BS11:
Harald Weltef39e3b52011-03-20 06:27:31 -03003000 case GSM_BTS_TYPE_RBS2000:
Harald Weltec0cf0022011-08-05 20:11:18 +02003001 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003002 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01003003 default:
Harald Welte8099a712011-08-09 21:53:20 +02003004 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01003005 }
3006 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08003007 }
3008
3009 memset(&rel, 0, sizeof(struct gsm_mncc));
3010 rel.callref = data->callref;
3011
3012 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02003013 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08003014
3015 /* Callref unknown */
3016 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003017 struct gsm_subscriber *subscr;
3018
Harald Welte6e1536e2009-07-04 10:11:24 +02003019 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08003020 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3021 "Received '%s' from MNCC with "
3022 "unknown callref %d\n", data->called.number,
3023 get_mncc_name(msg_type), data->callref);
3024 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003025 return mncc_release_ind(net, NULL, data->callref,
3026 GSM48_CAUSE_LOC_PRN_S_LU,
3027 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08003028 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003029 if (!data->called.number[0] && !data->imsi[0]) {
3030 DEBUGP(DCC, "(bts - trx - ts - ti) "
3031 "Received '%s' from MNCC with "
3032 "no number or IMSI\n", get_mncc_name(msg_type));
3033 /* Invalid number */
3034 return mncc_release_ind(net, NULL, data->callref,
3035 GSM48_CAUSE_LOC_PRN_S_LU,
3036 GSM48_CC_CAUSE_INV_NR_FORMAT);
3037 }
Harald Welte03740842009-06-10 23:11:52 +08003038 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003039 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02003040 subscr = subscr_get_by_extension(net,
3041 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003042 else
Harald Welte75350412009-07-23 18:46:00 +02003043 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08003044 /* If subscriber is not found */
3045 if (!subscr) {
3046 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3047 "Received '%s' from MNCC with "
3048 "unknown subscriber %s\n", data->called.number,
3049 get_mncc_name(msg_type), data->called.number);
3050 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003051 return mncc_release_ind(net, NULL, data->callref,
3052 GSM48_CAUSE_LOC_PRN_S_LU,
3053 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08003054 }
3055 /* If subscriber is not "attached" */
3056 if (!subscr->lac) {
3057 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3058 "Received '%s' from MNCC with "
3059 "detached subscriber %s\n", data->called.number,
3060 get_mncc_name(msg_type), data->called.number);
3061 subscr_put(subscr);
3062 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003063 return mncc_release_ind(net, NULL, data->callref,
3064 GSM48_CAUSE_LOC_PRN_S_LU,
3065 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08003066 }
3067 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02003068 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3069 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003070 DEBUGP(DCC, "No memory for trans.\n");
3071 subscr_put(subscr);
3072 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003073 mncc_release_ind(net, NULL, data->callref,
3074 GSM48_CAUSE_LOC_PRN_S_LU,
3075 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08003076 return -ENOMEM;
3077 }
Harald Welte03740842009-06-10 23:11:52 +08003078 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003079 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003080
Harald Welte03740842009-06-10 23:11:52 +08003081 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003082 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003083 /* find transaction with this subscriber already paging */
3084 llist_for_each_entry(transt, &net->trans_list, entry) {
3085 /* Transaction of our lchan? */
3086 if (transt == trans ||
3087 transt->subscr != subscr)
3088 continue;
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003089 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003090 "Received '%s' from MNCC with "
3091 "unallocated channel, paging already "
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003092 "started for lac %d.\n",
Harald Welte03740842009-06-10 23:11:52 +08003093 data->called.number,
Holger Hans Peter Freyther346074d2010-12-27 16:08:34 +01003094 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003095 subscr_put(subscr);
3096 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003097 return 0;
3098 }
3099 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02003100 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01003101
3102 /* Get a channel */
Holger Hans Peter Freyther02539b12010-12-29 17:09:07 +01003103 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3104 if (!trans->paging_request) {
3105 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3106 subscr_put(subscr);
3107 trans_free(trans);
3108 return 0;
3109 }
3110
3111 *trans->paging_request = subscr->net;
3112 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut892f6952010-12-01 22:17:36 +01003113
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003114 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003115 return 0;
3116 }
3117 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003118 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003119 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003120 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003121
3122 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003123 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08003124
3125 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003126 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08003127 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003128 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08003129 (trans->subscr)?(trans->subscr->extension):"-",
3130 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003131 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3132 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003133 if (msg_type == MNCC_REL_REQ)
3134 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3135 else
3136 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3137 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003138 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003139 return rc;
3140 }
3141
3142 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3143 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08003144 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08003145 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01003146 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003147 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08003148 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08003149
3150 /* Find function for current state and message */
3151 for (i = 0; i < DOWNSLLEN; i++)
3152 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003153 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003154 break;
3155 if (i == DOWNSLLEN) {
3156 DEBUGP(DCC, "Message unhandled at this state.\n");
3157 return 0;
3158 }
3159
3160 rc = downstatelist[i].rout(trans, arg);
3161
3162 return rc;
3163}
3164
3165
3166static struct datastate {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003167 uint32_t states;
Harald Welte03740842009-06-10 23:11:52 +08003168 int type;
3169 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3170} datastatelist[] = {
3171 /* mobile originating call establishment */
3172 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3173 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3174 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3175 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3176 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3177 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3178 /* mobile terminating call establishment */
3179 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3180 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3181 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3182 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003183 {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 +08003184 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3185 /* signalling during call */
3186 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3187 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3188 {SBIT(GSM_CSTATE_ACTIVE),
3189 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3190 {ALL_STATES,
3191 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3192 {ALL_STATES,
3193 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3194 {ALL_STATES,
3195 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3196 {SBIT(GSM_CSTATE_ACTIVE),
3197 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3198 {SBIT(GSM_CSTATE_ACTIVE),
3199 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3200 {SBIT(GSM_CSTATE_ACTIVE),
3201 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3202 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3203 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3204 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3205 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3206 {SBIT(GSM_CSTATE_ACTIVE),
3207 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3208 /* clearing */
3209 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3210 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3211 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3212 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3213 {ALL_STATES, /* 5.4.3.4 */
3214 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3215};
3216
3217#define DATASLLEN \
3218 (sizeof(datastatelist) / sizeof(struct datastate))
3219
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003220static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003221{
3222 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003223 uint8_t msg_type = gh->msg_type & 0xbf;
3224 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003225 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003226 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003227
Harald Welte03740842009-06-10 23:11:52 +08003228 if (msg_type & 0x80) {
3229 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3230 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003231 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003232
Harald Welte03740842009-06-10 23:11:52 +08003233 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003234 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003235
Harald Welte4861c822009-07-23 21:21:14 +02003236 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003237 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003238 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003239 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003240 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3241 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003242
3243 /* Create transaction */
3244 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003245 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003246 "creating new trans.\n", transaction_id);
3247 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003248 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003249 transaction_id, new_callref++);
3250 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003251 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther3f6430d2011-01-16 18:17:04 +01003252 rc = gsm48_tx_simple(conn,
Harald Welte4861c822009-07-23 21:21:14 +02003253 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003254 GSM48_MT_CC_RELEASE_COMPL);
3255 return -ENOMEM;
3256 }
Harald Welte03740842009-06-10 23:11:52 +08003257 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003258 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003259 }
3260
3261 /* find function for current state and message */
3262 for (i = 0; i < DATASLLEN; i++)
3263 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003264 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003265 break;
3266 if (i == DATASLLEN) {
3267 DEBUGP(DCC, "Message unhandled at this state.\n");
3268 return 0;
3269 }
3270
3271 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003272
3273 return rc;
3274}
3275
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003276/* Create a dummy to wait five seconds */
3277static void release_anchor(struct gsm_subscriber_connection *conn)
3278{
3279 if (!conn->anch_operation)
3280 return;
3281
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003282 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003283 talloc_free(conn->anch_operation);
3284 conn->anch_operation = NULL;
3285}
3286
3287static void anchor_timeout(void *_data)
3288{
3289 struct gsm_subscriber_connection *con = _data;
3290
3291 release_anchor(con);
3292 msc_release_connection(con);
3293}
3294
3295int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3296{
3297 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3298 if (!conn->anch_operation)
3299 return -1;
3300
3301 conn->anch_operation->timeout.data = conn;
3302 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +02003303 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003304 return 0;
3305}
3306
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003307/* here we get data from the BSC level... */
3308int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003309{
3310 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +02003311 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte59b04682009-06-10 05:40:52 +08003312 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003313
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003314 if (silent_call_reroute(conn, msg))
3315 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003316
3317 switch (pdisc) {
3318 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003319 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003320 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003321 break;
3322 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003323 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003324 break;
3325 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003326 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003327 break;
3328 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003329 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003330 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003331 break;
3332 case GSM48_PDISC_MM_GPRS:
3333 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003334 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3335 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003336 break;
Harald Welte03115042009-10-16 08:32:58 +02003337 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003338 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003339 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003340 break;
Harald Welte59b04682009-06-10 05:40:52 +08003341 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003342 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3343 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003344 break;
3345 }
3346
3347 return rc;
3348}
3349
Harald Welte3c062072009-07-28 18:25:29 +02003350/*
3351 * This will be ran by the linker when loading the DSO. We use it to
3352 * do system initialization, e.g. registration of signal handlers.
3353 */
3354static __attribute__((constructor)) void on_dso_load_0408(void)
3355{
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +02003356 osmo_signal_register_handler(SS_HO, handle_ho_signal, NULL);
3357 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte3c062072009-07-28 18:25:29 +02003358}